blob: f738e2bb6ab8e7065d81646aa3f4ec97fe25c5dc [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{
6656 int attr;
6657 int curattr;
6658 int row;
6659 int col = 0;
6660 int maxwidth;
6661 int width;
6662 int n;
6663 int len;
6664 int fillchar;
6665 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006666 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006668 struct stl_hlrec hltab[STL_MAX_ITEM];
6669 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006670 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006671 win_T *ewp;
6672 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006675 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006677 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006678 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006679 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006680 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006681 attr = hl_attr(HLF_TPF);
6682 maxwidth = Columns;
6683# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006684 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006687 else
6688 {
6689 row = W_WINROW(wp) + wp->w_height;
6690 fillchar = fillchar_status(&attr, wp == curwin);
6691 maxwidth = W_WIDTH(wp);
6692
6693 if (draw_ruler)
6694 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006695 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006696 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006697 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006698 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006699 if (*++stl == '-')
6700 stl++;
6701 if (atoi((char *)stl))
6702 while (VIM_ISDIGIT(*stl))
6703 stl++;
6704 if (*stl++ != '(')
6705 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006706 }
6707#ifdef FEAT_VERTSPLIT
6708 col = ru_col - (Columns - W_WIDTH(wp));
6709 if (col < (W_WIDTH(wp) + 1) / 2)
6710 col = (W_WIDTH(wp) + 1) / 2;
6711#else
6712 col = ru_col;
6713 if (col > (Columns + 1) / 2)
6714 col = (Columns + 1) / 2;
6715#endif
6716 maxwidth = W_WIDTH(wp) - col;
6717#ifdef FEAT_WINDOWS
6718 if (!wp->w_status_height)
6719#endif
6720 {
6721 row = Rows - 1;
6722 --maxwidth; /* writing in last column may cause scrolling */
6723 fillchar = ' ';
6724 attr = 0;
6725 }
6726
6727# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006728 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006729# endif
6730 }
6731 else
6732 {
6733 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006734 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006735 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006736 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006737# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006738 use_sandbox = was_set_insecurely((char_u *)"statusline",
6739 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006740# endif
6741 }
6742
6743#ifdef FEAT_VERTSPLIT
6744 col += W_WINCOL(wp);
6745#endif
6746 }
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 if (maxwidth <= 0)
6749 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
Bram Moolenaar61452852011-02-01 18:01:11 +01006751 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6752 * the cursor away and back. */
6753 ewp = wp == NULL ? curwin : wp;
6754 p_crb_save = ewp->w_p_crb;
6755 ewp->w_p_crb = FALSE;
6756
Bram Moolenaar362f3562009-11-03 16:20:34 +00006757 /* Make a copy, because the statusline may include a function call that
6758 * might change the option value and free the memory. */
6759 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006760 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006761 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006762 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006763 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006764 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006766 /* Make all characters printable. */
6767 p = transstr(buf);
6768 if (p != NULL)
6769 {
6770 vim_strncpy(buf, p, sizeof(buf) - 1);
6771 vim_free(p);
6772 }
6773
6774 /* fill up with "fillchar" */
6775 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006776 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
6778#ifdef FEAT_MBYTE
6779 len += (*mb_char2bytes)(fillchar, buf + len);
6780#else
6781 buf[len++] = fillchar;
6782#endif
6783 ++width;
6784 }
6785 buf[len] = NUL;
6786
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006787 /*
6788 * Draw each snippet with the specified highlighting.
6789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 curattr = attr;
6791 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006792 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006794 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 screen_puts_len(p, len, row, col, curattr);
6796 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006797 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006799 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006801 else if (hltab[n].userhl < 0)
6802 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006804 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006805 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806#endif
6807 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006808 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006811
6812 if (wp == NULL)
6813 {
6814 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6815 col = 0;
6816 len = 0;
6817 p = buf;
6818 fillchar = 0;
6819 for (n = 0; tabtab[n].start != NULL; n++)
6820 {
6821 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6822 while (col < len)
6823 TabPageIdxs[col++] = fillchar;
6824 p = tabtab[n].start;
6825 fillchar = tabtab[n].userhl;
6826 }
6827 while (col < Columns)
6828 TabPageIdxs[col++] = fillchar;
6829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830}
6831
6832#endif /* FEAT_STL_OPT */
6833
6834/*
6835 * Output a single character directly to the screen and update ScreenLines.
6836 */
6837 void
6838screen_putchar(c, row, col, attr)
6839 int c;
6840 int row, col;
6841 int attr;
6842{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 char_u buf[MB_MAXBYTES + 1];
6844
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006845#ifdef FEAT_MBYTE
6846 if (has_mbyte)
6847 buf[(*mb_char2bytes)(c, buf)] = NUL;
6848 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006850 {
6851 buf[0] = c;
6852 buf[1] = NUL;
6853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 screen_puts(buf, row, col, attr);
6855}
6856
6857/*
6858 * Get a single character directly from ScreenLines into "bytes[]".
6859 * Also return its attribute in *attrp;
6860 */
6861 void
6862screen_getbytes(row, col, bytes, attrp)
6863 int row, col;
6864 char_u *bytes;
6865 int *attrp;
6866{
6867 unsigned off;
6868
6869 /* safety check */
6870 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6871 {
6872 off = LineOffset[row] + col;
6873 *attrp = ScreenAttrs[off];
6874 bytes[0] = ScreenLines[off];
6875 bytes[1] = NUL;
6876
6877#ifdef FEAT_MBYTE
6878 if (enc_utf8 && ScreenLinesUC[off] != 0)
6879 bytes[utfc_char2bytes(off, bytes)] = NUL;
6880 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6881 {
6882 bytes[0] = ScreenLines[off];
6883 bytes[1] = ScreenLines2[off];
6884 bytes[2] = NUL;
6885 }
6886 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6887 {
6888 bytes[1] = ScreenLines[off + 1];
6889 bytes[2] = NUL;
6890 }
6891#endif
6892 }
6893}
6894
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006895#ifdef FEAT_MBYTE
6896static int screen_comp_differs __ARGS((int, int*));
6897
6898/*
6899 * Return TRUE if composing characters for screen posn "off" differs from
6900 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006901 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006902 */
6903 static int
6904screen_comp_differs(off, u8cc)
6905 int off;
6906 int *u8cc;
6907{
6908 int i;
6909
6910 for (i = 0; i < Screen_mco; ++i)
6911 {
6912 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6913 return TRUE;
6914 if (u8cc[i] == 0)
6915 break;
6916 }
6917 return FALSE;
6918}
6919#endif
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921/*
6922 * Put string '*text' on the screen at position 'row' and 'col', with
6923 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6924 * Note: only outputs within one row, message is truncated at screen boundary!
6925 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6926 */
6927 void
6928screen_puts(text, row, col, attr)
6929 char_u *text;
6930 int row;
6931 int col;
6932 int attr;
6933{
6934 screen_puts_len(text, -1, row, col, attr);
6935}
6936
6937/*
6938 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6939 * a NUL.
6940 */
6941 void
6942screen_puts_len(text, len, row, col, attr)
6943 char_u *text;
6944 int len;
6945 int row;
6946 int col;
6947 int attr;
6948{
6949 unsigned off;
6950 char_u *ptr = text;
6951 int c;
6952#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006953 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 int mbyte_blen = 1;
6955 int mbyte_cells = 1;
6956 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006957 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 int clear_next_cell = FALSE;
6959# ifdef FEAT_ARABIC
6960 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006961 int pc, nc, nc1;
6962 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963# endif
6964#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006965#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6966 int force_redraw_this;
6967 int force_redraw_next = FALSE;
6968#endif
6969 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6972 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006973 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaarc236c162008-07-13 17:41:49 +00006975#ifdef FEAT_MBYTE
6976 /* When drawing over the right halve of a double-wide char clear out the
6977 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006978 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006979# ifdef FEAT_GUI
6980 && !gui.in_use
6981# endif
6982 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006983 {
6984 ScreenLines[off - 1] = ' ';
6985 ScreenAttrs[off - 1] = 0;
6986 if (enc_utf8)
6987 {
6988 ScreenLinesUC[off - 1] = 0;
6989 ScreenLinesC[0][off - 1] = 0;
6990 }
6991 /* redraw the previous cell, make it empty */
6992 screen_char(off - 1, row, col - 1);
6993 /* force the cell at "col" to be redrawn */
6994 force_redraw_next = TRUE;
6995 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006996#endif
6997
Bram Moolenaar367329b2007-08-30 11:53:22 +00006998#ifdef FEAT_MBYTE
6999 max_off = LineOffset[row] + screen_Columns;
7000#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007001 while (col < screen_Columns
7002 && (len < 0 || (int)(ptr - text) < len)
7003 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {
7005 c = *ptr;
7006#ifdef FEAT_MBYTE
7007 /* check if this is the first byte of a multibyte */
7008 if (has_mbyte)
7009 {
7010 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007011 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007013 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7015 mbyte_cells = 1;
7016 else if (enc_dbcs != 0)
7017 mbyte_cells = mbyte_blen;
7018 else /* enc_utf8 */
7019 {
7020 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007021 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 (int)((text + len) - ptr));
7023 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007024 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007026# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 /* Non-BMP character: display as ? or fullwidth ?. */
7028 if (u8c >= 0x10000)
7029 {
7030 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7031 if (attr == 0)
7032 attr = hl_attr(HLF_8);
7033 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035# ifdef FEAT_ARABIC
7036 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7037 {
7038 /* Do Arabic shaping. */
7039 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7040 {
7041 /* Past end of string to be displayed. */
7042 nc = NUL;
7043 nc1 = NUL;
7044 }
7045 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007046 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007047 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7048 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007049 nc1 = pcc[0];
7050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 pc = prev_c;
7052 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007053 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 }
7055 else
7056 prev_c = u8c;
7057# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007058 if (col + mbyte_cells > screen_Columns)
7059 {
7060 /* Only 1 cell left, but character requires 2 cells:
7061 * display a '>' in the last column to avoid wrapping. */
7062 c = '>';
7063 mbyte_cells = 1;
7064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
7066 }
7067#endif
7068
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007069#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7070 force_redraw_this = force_redraw_next;
7071 force_redraw_next = FALSE;
7072#endif
7073
7074 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075#ifdef FEAT_MBYTE
7076 || (mbyte_cells == 2
7077 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7078 || (enc_dbcs == DBCS_JPNU
7079 && c == 0x8e
7080 && ScreenLines2[off] != ptr[1])
7081 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007082 && (ScreenLinesUC[off] !=
7083 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7084 || (ScreenLinesUC[off] != 0
7085 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086#endif
7087 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007088 || exmode_active;
7089
7090 if (need_redraw
7091#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7092 || force_redraw_this
7093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 )
7095 {
7096#if defined(FEAT_GUI) || defined(UNIX)
7097 /* The bold trick makes a single row of pixels appear in the next
7098 * character. When a bold character is removed, the next
7099 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007100 * and for some xterms. */
7101 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102# ifdef FEAT_GUI
7103 gui.in_use
7104# endif
7105# if defined(FEAT_GUI) && defined(UNIX)
7106 ||
7107# endif
7108# ifdef UNIX
7109 term_is_xterm
7110# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007111 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007113 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007115 if (n > HL_ALL)
7116 n = syn_attr2attr(n);
7117 if (n & HL_BOLD)
7118 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
7120#endif
7121#ifdef FEAT_MBYTE
7122 /* When at the end of the text and overwriting a two-cell
7123 * character with a one-cell character, need to clear the next
7124 * cell. Also when overwriting the left halve of a two-cell char
7125 * with the right halve of a two-cell char. Do this only once
7126 * (mb_off2cells() may return 2 on the right halve). */
7127 if (clear_next_cell)
7128 clear_next_cell = FALSE;
7129 else if (has_mbyte
7130 && (len < 0 ? ptr[mbyte_blen] == NUL
7131 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007132 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007134 && (*mb_off2cells)(off, max_off) == 1
7135 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 clear_next_cell = TRUE;
7137
7138 /* Make sure we never leave a second byte of a double-byte behind,
7139 * it confuses mb_off2cells(). */
7140 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007141 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007143 && (*mb_off2cells)(off, max_off) == 1
7144 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 ScreenLines[off + mbyte_blen] = 0;
7146#endif
7147 ScreenLines[off] = c;
7148 ScreenAttrs[off] = attr;
7149#ifdef FEAT_MBYTE
7150 if (enc_utf8)
7151 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007152 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 ScreenLinesUC[off] = 0;
7154 else
7155 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007156 int i;
7157
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007159 for (i = 0; i < Screen_mco; ++i)
7160 {
7161 ScreenLinesC[i][off] = u8cc[i];
7162 if (u8cc[i] == 0)
7163 break;
7164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 }
7166 if (mbyte_cells == 2)
7167 {
7168 ScreenLines[off + 1] = 0;
7169 ScreenAttrs[off + 1] = attr;
7170 }
7171 screen_char(off, row, col);
7172 }
7173 else if (mbyte_cells == 2)
7174 {
7175 ScreenLines[off + 1] = ptr[1];
7176 ScreenAttrs[off + 1] = attr;
7177 screen_char_2(off, row, col);
7178 }
7179 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7180 {
7181 ScreenLines2[off] = ptr[1];
7182 screen_char(off, row, col);
7183 }
7184 else
7185#endif
7186 screen_char(off, row, col);
7187 }
7188#ifdef FEAT_MBYTE
7189 if (has_mbyte)
7190 {
7191 off += mbyte_cells;
7192 col += mbyte_cells;
7193 ptr += mbyte_blen;
7194 if (clear_next_cell)
7195 ptr = (char_u *)" ";
7196 }
7197 else
7198#endif
7199 {
7200 ++off;
7201 ++col;
7202 ++ptr;
7203 }
7204 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007205
7206#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7207 /* If we detected the next character needs to be redrawn, but the text
7208 * doesn't extend up to there, update the character here. */
7209 if (force_redraw_next && col < screen_Columns)
7210 {
7211# ifdef FEAT_MBYTE
7212 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7213 screen_char_2(off, row, col);
7214 else
7215# endif
7216 screen_char(off, row, col);
7217 }
7218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219}
7220
7221#ifdef FEAT_SEARCH_EXTRA
7222/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007223 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 */
7225 static void
7226start_search_hl()
7227{
7228 if (p_hls && !no_hlsearch)
7229 {
7230 last_pat_prog(&search_hl.rm);
7231 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007232# ifdef FEAT_RELTIME
7233 /* Set the time limit to 'redrawtime'. */
7234 profile_setlimit(p_rdt, &search_hl.tm);
7235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 }
7237}
7238
7239/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007240 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 */
7242 static void
7243end_search_hl()
7244{
7245 if (search_hl.rm.regprog != NULL)
7246 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007247 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 search_hl.rm.regprog = NULL;
7249 }
7250}
7251
7252/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007253 * Init for calling prepare_search_hl().
7254 */
7255 static void
7256init_search_hl(wp)
7257 win_T *wp;
7258{
7259 matchitem_T *cur;
7260
7261 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7262 * match */
7263 cur = wp->w_match_head;
7264 while (cur != NULL)
7265 {
7266 cur->hl.rm = cur->match;
7267 if (cur->hlg_id == 0)
7268 cur->hl.attr = 0;
7269 else
7270 cur->hl.attr = syn_id2attr(cur->hlg_id);
7271 cur->hl.buf = wp->w_buffer;
7272 cur->hl.lnum = 0;
7273 cur->hl.first_lnum = 0;
7274# ifdef FEAT_RELTIME
7275 /* Set the time limit to 'redrawtime'. */
7276 profile_setlimit(p_rdt, &(cur->hl.tm));
7277# endif
7278 cur = cur->next;
7279 }
7280 search_hl.buf = wp->w_buffer;
7281 search_hl.lnum = 0;
7282 search_hl.first_lnum = 0;
7283 /* time limit is set at the toplevel, for all windows */
7284}
7285
7286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 * Advance to the match in window "wp" line "lnum" or past it.
7288 */
7289 static void
7290prepare_search_hl(wp, lnum)
7291 win_T *wp;
7292 linenr_T lnum;
7293{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007294 matchitem_T *cur; /* points to the match list */
7295 match_T *shl; /* points to search_hl or a match */
7296 int shl_flag; /* flag to indicate whether search_hl
7297 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 int n;
7299
7300 /*
7301 * When using a multi-line pattern, start searching at the top
7302 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007303 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007305 cur = wp->w_match_head;
7306 shl_flag = FALSE;
7307 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007309 if (shl_flag == FALSE)
7310 {
7311 shl = &search_hl;
7312 shl_flag = TRUE;
7313 }
7314 else
7315 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 if (shl->rm.regprog != NULL
7317 && shl->lnum == 0
7318 && re_multiline(shl->rm.regprog))
7319 {
7320 if (shl->first_lnum == 0)
7321 {
7322# ifdef FEAT_FOLDING
7323 for (shl->first_lnum = lnum;
7324 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7325 if (hasFoldingWin(wp, shl->first_lnum - 1,
7326 NULL, NULL, TRUE, NULL))
7327 break;
7328# else
7329 shl->first_lnum = wp->w_topline;
7330# endif
7331 }
7332 n = 0;
7333 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7334 {
7335 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7336 if (shl->lnum != 0)
7337 {
7338 shl->first_lnum = shl->lnum
7339 + shl->rm.endpos[0].lnum
7340 - shl->rm.startpos[0].lnum;
7341 n = shl->rm.endpos[0].col;
7342 }
7343 else
7344 {
7345 ++shl->first_lnum;
7346 n = 0;
7347 }
7348 }
7349 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007350 if (shl != &search_hl && cur != NULL)
7351 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 }
7353}
7354
7355/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007356 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 * Uses shl->buf.
7358 * Sets shl->lnum and shl->rm contents.
7359 * Note: Assumes a previous match is always before "lnum", unless
7360 * shl->lnum is zero.
7361 * Careful: Any pointers for buffer lines will become invalid.
7362 */
7363 static void
7364next_search_hl(win, shl, lnum, mincol)
7365 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007366 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 linenr_T lnum;
7368 colnr_T mincol; /* minimal column for a match */
7369{
7370 linenr_T l;
7371 colnr_T matchcol;
7372 long nmatched;
7373
7374 if (shl->lnum != 0)
7375 {
7376 /* Check for three situations:
7377 * 1. If the "lnum" is below a previous match, start a new search.
7378 * 2. If the previous match includes "mincol", use it.
7379 * 3. Continue after the previous match.
7380 */
7381 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7382 if (lnum > l)
7383 shl->lnum = 0;
7384 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7385 return;
7386 }
7387
7388 /*
7389 * Repeat searching for a match until one is found that includes "mincol"
7390 * or none is found in this line.
7391 */
7392 called_emsg = FALSE;
7393 for (;;)
7394 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007395#ifdef FEAT_RELTIME
7396 /* Stop searching after passing the time limit. */
7397 if (profile_passed_limit(&(shl->tm)))
7398 {
7399 shl->lnum = 0; /* no match found in time */
7400 break;
7401 }
7402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 /* Three situations:
7404 * 1. No useful previous match: search from start of line.
7405 * 2. Not Vi compatible or empty match: continue at next character.
7406 * Break the loop if this is beyond the end of the line.
7407 * 3. Vi compatible searching: continue at end of previous match.
7408 */
7409 if (shl->lnum == 0)
7410 matchcol = 0;
7411 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7412 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007413 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007415 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007416
7417 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007418 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007419 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007421 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 shl->lnum = 0;
7423 break;
7424 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007425#ifdef FEAT_MBYTE
7426 if (has_mbyte)
7427 matchcol += mb_ptr2len(ml);
7428 else
7429#endif
7430 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432 else
7433 matchcol = shl->rm.endpos[0].col;
7434
7435 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007436 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7437#ifdef FEAT_RELTIME
7438 &(shl->tm)
7439#else
7440 NULL
7441#endif
7442 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007443 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 {
7445 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007446 if (shl == &search_hl)
7447 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007448 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar473de612013-06-08 18:19:48 +02007449 vim_regfree(shl->rm.regprog);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007450 SET_NO_HLSEARCH(TRUE);
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007453 shl->lnum = 0;
7454 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 break;
7456 }
7457 if (nmatched == 0)
7458 {
7459 shl->lnum = 0; /* no match found */
7460 break;
7461 }
7462 if (shl->rm.startpos[0].lnum > 0
7463 || shl->rm.startpos[0].col >= mincol
7464 || nmatched > 1
7465 || shl->rm.endpos[0].col > mincol)
7466 {
7467 shl->lnum += shl->rm.startpos[0].lnum;
7468 break; /* useful match found */
7469 }
7470 }
7471}
7472#endif
7473
7474 static void
7475screen_start_highlight(attr)
7476 int attr;
7477{
7478 attrentry_T *aep = NULL;
7479
7480 screen_attr = attr;
7481 if (full_screen
7482#ifdef WIN3264
7483 && termcap_active
7484#endif
7485 )
7486 {
7487#ifdef FEAT_GUI
7488 if (gui.in_use)
7489 {
7490 char buf[20];
7491
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007492 /* The GUI handles this internally. */
7493 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 OUT_STR(buf);
7495 }
7496 else
7497#endif
7498 {
7499 if (attr > HL_ALL) /* special HL attr. */
7500 {
7501 if (t_colors > 1)
7502 aep = syn_cterm_attr2entry(attr);
7503 else
7504 aep = syn_term_attr2entry(attr);
7505 if (aep == NULL) /* did ":syntax clear" */
7506 attr = 0;
7507 else
7508 attr = aep->ae_attr;
7509 }
7510 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7511 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007512 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7513 && cterm_normal_fg_bold)
7514 /* If the Normal FG color has BOLD attribute and the new HL
7515 * has a FG color defined, clear BOLD. */
7516 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7518 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007519 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7520 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 out_str(T_US);
7522 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7523 out_str(T_CZH);
7524 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7525 out_str(T_MR);
7526
7527 /*
7528 * Output the color or start string after bold etc., in case the
7529 * bold etc. override the color setting.
7530 */
7531 if (aep != NULL)
7532 {
7533 if (t_colors > 1)
7534 {
7535 if (aep->ae_u.cterm.fg_color)
7536 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7537 if (aep->ae_u.cterm.bg_color)
7538 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7539 }
7540 else
7541 {
7542 if (aep->ae_u.term.start != NULL)
7543 out_str(aep->ae_u.term.start);
7544 }
7545 }
7546 }
7547 }
7548}
7549
7550 void
7551screen_stop_highlight()
7552{
7553 int do_ME = FALSE; /* output T_ME code */
7554
7555 if (screen_attr != 0
7556#ifdef WIN3264
7557 && termcap_active
7558#endif
7559 )
7560 {
7561#ifdef FEAT_GUI
7562 if (gui.in_use)
7563 {
7564 char buf[20];
7565
7566 /* use internal GUI code */
7567 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7568 OUT_STR(buf);
7569 }
7570 else
7571#endif
7572 {
7573 if (screen_attr > HL_ALL) /* special HL attr. */
7574 {
7575 attrentry_T *aep;
7576
7577 if (t_colors > 1)
7578 {
7579 /*
7580 * Assume that t_me restores the original colors!
7581 */
7582 aep = syn_cterm_attr2entry(screen_attr);
7583 if (aep != NULL && (aep->ae_u.cterm.fg_color
7584 || aep->ae_u.cterm.bg_color))
7585 do_ME = TRUE;
7586 }
7587 else
7588 {
7589 aep = syn_term_attr2entry(screen_attr);
7590 if (aep != NULL && aep->ae_u.term.stop != NULL)
7591 {
7592 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7593 do_ME = TRUE;
7594 else
7595 out_str(aep->ae_u.term.stop);
7596 }
7597 }
7598 if (aep == NULL) /* did ":syntax clear" */
7599 screen_attr = 0;
7600 else
7601 screen_attr = aep->ae_attr;
7602 }
7603
7604 /*
7605 * Often all ending-codes are equal to T_ME. Avoid outputting the
7606 * same sequence several times.
7607 */
7608 if (screen_attr & HL_STANDOUT)
7609 {
7610 if (STRCMP(T_SE, T_ME) == 0)
7611 do_ME = TRUE;
7612 else
7613 out_str(T_SE);
7614 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007615 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {
7617 if (STRCMP(T_UE, T_ME) == 0)
7618 do_ME = TRUE;
7619 else
7620 out_str(T_UE);
7621 }
7622 if (screen_attr & HL_ITALIC)
7623 {
7624 if (STRCMP(T_CZR, T_ME) == 0)
7625 do_ME = TRUE;
7626 else
7627 out_str(T_CZR);
7628 }
7629 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7630 out_str(T_ME);
7631
7632 if (t_colors > 1)
7633 {
7634 /* set Normal cterm colors */
7635 if (cterm_normal_fg_color != 0)
7636 term_fg_color(cterm_normal_fg_color - 1);
7637 if (cterm_normal_bg_color != 0)
7638 term_bg_color(cterm_normal_bg_color - 1);
7639 if (cterm_normal_fg_bold)
7640 out_str(T_MD);
7641 }
7642 }
7643 }
7644 screen_attr = 0;
7645}
7646
7647/*
7648 * Reset the colors for a cterm. Used when leaving Vim.
7649 * The machine specific code may override this again.
7650 */
7651 void
7652reset_cterm_colors()
7653{
7654 if (t_colors > 1)
7655 {
7656 /* set Normal cterm colors */
7657 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7658 {
7659 out_str(T_OP);
7660 screen_attr = -1;
7661 }
7662 if (cterm_normal_fg_bold)
7663 {
7664 out_str(T_ME);
7665 screen_attr = -1;
7666 }
7667 }
7668}
7669
7670/*
7671 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7672 * using the attributes from ScreenAttrs["off"].
7673 */
7674 static void
7675screen_char(off, row, col)
7676 unsigned off;
7677 int row;
7678 int col;
7679{
7680 int attr;
7681
7682 /* Check for illegal values, just in case (could happen just after
7683 * resizing). */
7684 if (row >= screen_Rows || col >= screen_Columns)
7685 return;
7686
7687 /* Outputting the last character on the screen may scrollup the screen.
7688 * Don't to it! Mark the character invalid (update it when scrolled up) */
7689 if (row == screen_Rows - 1 && col == screen_Columns - 1
7690#ifdef FEAT_RIGHTLEFT
7691 /* account for first command-line character in rightleft mode */
7692 && !cmdmsg_rl
7693#endif
7694 )
7695 {
7696 ScreenAttrs[off] = (sattr_T)-1;
7697 return;
7698 }
7699
7700 /*
7701 * Stop highlighting first, so it's easier to move the cursor.
7702 */
7703#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7704 if (screen_char_attr != 0)
7705 attr = screen_char_attr;
7706 else
7707#endif
7708 attr = ScreenAttrs[off];
7709 if (screen_attr != attr)
7710 screen_stop_highlight();
7711
7712 windgoto(row, col);
7713
7714 if (screen_attr != attr)
7715 screen_start_highlight(attr);
7716
7717#ifdef FEAT_MBYTE
7718 if (enc_utf8 && ScreenLinesUC[off] != 0)
7719 {
7720 char_u buf[MB_MAXBYTES + 1];
7721
7722 /* Convert UTF-8 character to bytes and write it. */
7723
7724 buf[utfc_char2bytes(off, buf)] = NUL;
7725
7726 out_str(buf);
7727 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7728 ++screen_cur_col;
7729 }
7730 else
7731#endif
7732 {
7733#ifdef FEAT_MBYTE
7734 out_flush_check();
7735#endif
7736 out_char(ScreenLines[off]);
7737#ifdef FEAT_MBYTE
7738 /* double-byte character in single-width cell */
7739 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7740 out_char(ScreenLines2[off]);
7741#endif
7742 }
7743
7744 screen_cur_col++;
7745}
7746
7747#ifdef FEAT_MBYTE
7748
7749/*
7750 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7751 * on the screen at position 'row' and 'col'.
7752 * The attributes of the first byte is used for all. This is required to
7753 * output the two bytes of a double-byte character with nothing in between.
7754 */
7755 static void
7756screen_char_2(off, row, col)
7757 unsigned off;
7758 int row;
7759 int col;
7760{
7761 /* Check for illegal values (could be wrong when screen was resized). */
7762 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7763 return;
7764
7765 /* Outputting the last character on the screen may scrollup the screen.
7766 * Don't to it! Mark the character invalid (update it when scrolled up) */
7767 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7768 {
7769 ScreenAttrs[off] = (sattr_T)-1;
7770 return;
7771 }
7772
7773 /* Output the first byte normally (positions the cursor), then write the
7774 * second byte directly. */
7775 screen_char(off, row, col);
7776 out_char(ScreenLines[off + 1]);
7777 ++screen_cur_col;
7778}
7779#endif
7780
7781#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7782/*
7783 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7784 * This uses the contents of ScreenLines[] and doesn't change it.
7785 */
7786 void
7787screen_draw_rectangle(row, col, height, width, invert)
7788 int row;
7789 int col;
7790 int height;
7791 int width;
7792 int invert;
7793{
7794 int r, c;
7795 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007796#ifdef FEAT_MBYTE
7797 int max_off;
7798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007800 /* Can't use ScreenLines unless initialized */
7801 if (ScreenLines == NULL)
7802 return;
7803
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 if (invert)
7805 screen_char_attr = HL_INVERSE;
7806 for (r = row; r < row + height; ++r)
7807 {
7808 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007809#ifdef FEAT_MBYTE
7810 max_off = off + screen_Columns;
7811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 for (c = col; c < col + width; ++c)
7813 {
7814#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007815 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
7817 screen_char_2(off + c, r, c);
7818 ++c;
7819 }
7820 else
7821#endif
7822 {
7823 screen_char(off + c, r, c);
7824#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007825 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 ++c;
7827#endif
7828 }
7829 }
7830 }
7831 screen_char_attr = 0;
7832}
7833#endif
7834
7835#ifdef FEAT_VERTSPLIT
7836/*
7837 * Redraw the characters for a vertically split window.
7838 */
7839 static void
7840redraw_block(row, end, wp)
7841 int row;
7842 int end;
7843 win_T *wp;
7844{
7845 int col;
7846 int width;
7847
7848# ifdef FEAT_CLIPBOARD
7849 clip_may_clear_selection(row, end - 1);
7850# endif
7851
7852 if (wp == NULL)
7853 {
7854 col = 0;
7855 width = Columns;
7856 }
7857 else
7858 {
7859 col = wp->w_wincol;
7860 width = wp->w_width;
7861 }
7862 screen_draw_rectangle(row, col, end - row, width, FALSE);
7863}
7864#endif
7865
7866/*
7867 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7868 * with character 'c1' in first column followed by 'c2' in the other columns.
7869 * Use attributes 'attr'.
7870 */
7871 void
7872screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7873 int start_row, end_row;
7874 int start_col, end_col;
7875 int c1, c2;
7876 int attr;
7877{
7878 int row;
7879 int col;
7880 int off;
7881 int end_off;
7882 int did_delete;
7883 int c;
7884 int norm_term;
7885#if defined(FEAT_GUI) || defined(UNIX)
7886 int force_next = FALSE;
7887#endif
7888
7889 if (end_row > screen_Rows) /* safety check */
7890 end_row = screen_Rows;
7891 if (end_col > screen_Columns) /* safety check */
7892 end_col = screen_Columns;
7893 if (ScreenLines == NULL
7894 || start_row >= end_row
7895 || start_col >= end_col) /* nothing to do */
7896 return;
7897
7898 /* it's a "normal" terminal when not in a GUI or cterm */
7899 norm_term = (
7900#ifdef FEAT_GUI
7901 !gui.in_use &&
7902#endif
7903 t_colors <= 1);
7904 for (row = start_row; row < end_row; ++row)
7905 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007906#ifdef FEAT_MBYTE
7907 if (has_mbyte
7908# ifdef FEAT_GUI
7909 && !gui.in_use
7910# endif
7911 )
7912 {
7913 /* When drawing over the right halve of a double-wide char clear
7914 * out the left halve. When drawing over the left halve of a
7915 * double wide-char clear out the right halve. Only needed in a
7916 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007917 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007918 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007919 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007920 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007921 }
7922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 /*
7924 * Try to use delete-line termcap code, when no attributes or in a
7925 * "normal" terminal, where a bold/italic space is just a
7926 * space.
7927 */
7928 did_delete = FALSE;
7929 if (c2 == ' '
7930 && end_col == Columns
7931 && can_clear(T_CE)
7932 && (attr == 0
7933 || (norm_term
7934 && attr <= HL_ALL
7935 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7936 {
7937 /*
7938 * check if we really need to clear something
7939 */
7940 col = start_col;
7941 if (c1 != ' ') /* don't clear first char */
7942 ++col;
7943
7944 off = LineOffset[row] + col;
7945 end_off = LineOffset[row] + end_col;
7946
7947 /* skip blanks (used often, keep it fast!) */
7948#ifdef FEAT_MBYTE
7949 if (enc_utf8)
7950 while (off < end_off && ScreenLines[off] == ' '
7951 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7952 ++off;
7953 else
7954#endif
7955 while (off < end_off && ScreenLines[off] == ' '
7956 && ScreenAttrs[off] == 0)
7957 ++off;
7958 if (off < end_off) /* something to be cleared */
7959 {
7960 col = off - LineOffset[row];
7961 screen_stop_highlight();
7962 term_windgoto(row, col);/* clear rest of this screen line */
7963 out_str(T_CE);
7964 screen_start(); /* don't know where cursor is now */
7965 col = end_col - col;
7966 while (col--) /* clear chars in ScreenLines */
7967 {
7968 ScreenLines[off] = ' ';
7969#ifdef FEAT_MBYTE
7970 if (enc_utf8)
7971 ScreenLinesUC[off] = 0;
7972#endif
7973 ScreenAttrs[off] = 0;
7974 ++off;
7975 }
7976 }
7977 did_delete = TRUE; /* the chars are cleared now */
7978 }
7979
7980 off = LineOffset[row] + start_col;
7981 c = c1;
7982 for (col = start_col; col < end_col; ++col)
7983 {
7984 if (ScreenLines[off] != c
7985#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007986 || (enc_utf8 && (int)ScreenLinesUC[off]
7987 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988#endif
7989 || ScreenAttrs[off] != attr
7990#if defined(FEAT_GUI) || defined(UNIX)
7991 || force_next
7992#endif
7993 )
7994 {
7995#if defined(FEAT_GUI) || defined(UNIX)
7996 /* The bold trick may make a single row of pixels appear in
7997 * the next character. When a bold character is removed, the
7998 * next character should be redrawn too. This happens for our
7999 * own GUI and for some xterms. */
8000 if (
8001# ifdef FEAT_GUI
8002 gui.in_use
8003# endif
8004# if defined(FEAT_GUI) && defined(UNIX)
8005 ||
8006# endif
8007# ifdef UNIX
8008 term_is_xterm
8009# endif
8010 )
8011 {
8012 if (ScreenLines[off] != ' '
8013 && (ScreenAttrs[off] > HL_ALL
8014 || ScreenAttrs[off] & HL_BOLD))
8015 force_next = TRUE;
8016 else
8017 force_next = FALSE;
8018 }
8019#endif
8020 ScreenLines[off] = c;
8021#ifdef FEAT_MBYTE
8022 if (enc_utf8)
8023 {
8024 if (c >= 0x80)
8025 {
8026 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008027 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 }
8029 else
8030 ScreenLinesUC[off] = 0;
8031 }
8032#endif
8033 ScreenAttrs[off] = attr;
8034 if (!did_delete || c != ' ')
8035 screen_char(off, row, col);
8036 }
8037 ++off;
8038 if (col == start_col)
8039 {
8040 if (did_delete)
8041 break;
8042 c = c2;
8043 }
8044 }
8045 if (end_col == Columns)
8046 LineWraps[row] = FALSE;
8047 if (row == Rows - 1) /* overwritten the command line */
8048 {
8049 redraw_cmdline = TRUE;
8050 if (c1 == ' ' && c2 == ' ')
8051 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008052 if (start_col == 0)
8053 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 }
8055 }
8056}
8057
8058/*
8059 * Check if there should be a delay. Used before clearing or redrawing the
8060 * screen or the command line.
8061 */
8062 void
8063check_for_delay(check_msg_scroll)
8064 int check_msg_scroll;
8065{
8066 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8067 && !did_wait_return
8068 && emsg_silent == 0)
8069 {
8070 out_flush();
8071 ui_delay(1000L, TRUE);
8072 emsg_on_display = FALSE;
8073 if (check_msg_scroll)
8074 msg_scroll = FALSE;
8075 }
8076}
8077
8078/*
8079 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008080 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 * Returns TRUE if there is a valid screen to write to.
8082 * Returns FALSE when starting up and screen not initialized yet.
8083 */
8084 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008085screen_valid(doclear)
8086 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008088 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 return (ScreenLines != NULL);
8090}
8091
8092/*
8093 * Resize the shell to Rows and Columns.
8094 * Allocate ScreenLines[] and associated items.
8095 *
8096 * There may be some time between setting Rows and Columns and (re)allocating
8097 * ScreenLines[]. This happens when starting up and when (manually) changing
8098 * the shell size. Always use screen_Rows and screen_Columns to access items
8099 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8100 * final size of the shell is needed.
8101 */
8102 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008103screenalloc(doclear)
8104 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 int new_row, old_row;
8107#ifdef FEAT_GUI
8108 int old_Rows;
8109#endif
8110 win_T *wp;
8111 int outofmem = FALSE;
8112 int len;
8113 schar_T *new_ScreenLines;
8114#ifdef FEAT_MBYTE
8115 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008116 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008118 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119#endif
8120 sattr_T *new_ScreenAttrs;
8121 unsigned *new_LineOffset;
8122 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008123#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008124 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008125 tabpage_T *tp;
8126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008128 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008129#ifdef FEAT_AUTOCMD
8130 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008132retry:
8133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 /*
8135 * Allocation of the screen buffers is done only when the size changes and
8136 * when Rows and Columns have been set and we have started doing full
8137 * screen stuff.
8138 */
8139 if ((ScreenLines != NULL
8140 && Rows == screen_Rows
8141 && Columns == screen_Columns
8142#ifdef FEAT_MBYTE
8143 && enc_utf8 == (ScreenLinesUC != NULL)
8144 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008145 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146#endif
8147 )
8148 || Rows == 0
8149 || Columns == 0
8150 || (!full_screen && ScreenLines == NULL))
8151 return;
8152
8153 /*
8154 * It's possible that we produce an out-of-memory message below, which
8155 * will cause this function to be called again. To break the loop, just
8156 * return here.
8157 */
8158 if (entered)
8159 return;
8160 entered = TRUE;
8161
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008162 /*
8163 * Note that the window sizes are updated before reallocating the arrays,
8164 * thus we must not redraw here!
8165 */
8166 ++RedrawingDisabled;
8167
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 win_new_shellsize(); /* fit the windows in the new sized shell */
8169
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 comp_col(); /* recompute columns for shown command and ruler */
8171
8172 /*
8173 * We're changing the size of the screen.
8174 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8175 * - Move lines from the old arrays into the new arrays, clear extra
8176 * lines (unless the screen is going to be cleared).
8177 * - Free the old arrays.
8178 *
8179 * If anything fails, make ScreenLines NULL, so we don't do anything!
8180 * Continuing with the old ScreenLines may result in a crash, because the
8181 * size is wrong.
8182 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008183 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008185#ifdef FEAT_AUTOCMD
8186 if (aucmd_win != NULL)
8187 win_free_lsize(aucmd_win);
8188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189
8190 new_ScreenLines = (schar_T *)lalloc((long_u)(
8191 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8192#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008193 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 if (enc_utf8)
8195 {
8196 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8197 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008198 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008199 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8201 }
8202 if (enc_dbcs == DBCS_JPNU)
8203 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8204 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8205#endif
8206 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8207 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8208 new_LineOffset = (unsigned *)lalloc((long_u)(
8209 Rows * sizeof(unsigned)), FALSE);
8210 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008211#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008212 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008215 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {
8217 if (win_alloc_lines(wp) == FAIL)
8218 {
8219 outofmem = TRUE;
8220#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008221 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222#endif
8223 }
8224 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008225#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008226 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8227 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008228 outofmem = TRUE;
8229#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008230#ifdef FEAT_WINDOWS
8231give_up:
8232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008234#ifdef FEAT_MBYTE
8235 for (i = 0; i < p_mco; ++i)
8236 if (new_ScreenLinesC[i] == NULL)
8237 break;
8238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 if (new_ScreenLines == NULL
8240#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008241 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8243#endif
8244 || new_ScreenAttrs == NULL
8245 || new_LineOffset == NULL
8246 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008247#ifdef FEAT_WINDOWS
8248 || new_TabPageIdxs == NULL
8249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 || outofmem)
8251 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008252 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008253 {
8254 /* guess the size */
8255 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8256
8257 /* Remember we did this to avoid getting outofmem messages over
8258 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008259 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 vim_free(new_ScreenLines);
8262 new_ScreenLines = NULL;
8263#ifdef FEAT_MBYTE
8264 vim_free(new_ScreenLinesUC);
8265 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008266 for (i = 0; i < p_mco; ++i)
8267 {
8268 vim_free(new_ScreenLinesC[i]);
8269 new_ScreenLinesC[i] = NULL;
8270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 vim_free(new_ScreenLines2);
8272 new_ScreenLines2 = NULL;
8273#endif
8274 vim_free(new_ScreenAttrs);
8275 new_ScreenAttrs = NULL;
8276 vim_free(new_LineOffset);
8277 new_LineOffset = NULL;
8278 vim_free(new_LineWraps);
8279 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008280#ifdef FEAT_WINDOWS
8281 vim_free(new_TabPageIdxs);
8282 new_TabPageIdxs = NULL;
8283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 }
8285 else
8286 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008287 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008288
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 for (new_row = 0; new_row < Rows; ++new_row)
8290 {
8291 new_LineOffset[new_row] = new_row * Columns;
8292 new_LineWraps[new_row] = FALSE;
8293
8294 /*
8295 * If the screen is not going to be cleared, copy as much as
8296 * possible from the old screen to the new one and clear the rest
8297 * (used when resizing the window at the "--more--" prompt or when
8298 * executing an external command, for the GUI).
8299 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008300 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {
8302 (void)vim_memset(new_ScreenLines + new_row * Columns,
8303 ' ', (size_t)Columns * sizeof(schar_T));
8304#ifdef FEAT_MBYTE
8305 if (enc_utf8)
8306 {
8307 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8308 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008309 for (i = 0; i < p_mco; ++i)
8310 (void)vim_memset(new_ScreenLinesC[i]
8311 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 0, (size_t)Columns * sizeof(u8char_T));
8313 }
8314 if (enc_dbcs == DBCS_JPNU)
8315 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8316 0, (size_t)Columns * sizeof(schar_T));
8317#endif
8318 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8319 0, (size_t)Columns * sizeof(sattr_T));
8320 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008321 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {
8323 if (screen_Columns < Columns)
8324 len = screen_Columns;
8325 else
8326 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008327#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008328 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008329 * may be invalid now. Also when p_mco changes. */
8330 if (!(enc_utf8 && ScreenLinesUC == NULL)
8331 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008332#endif
8333 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8334 ScreenLines + LineOffset[old_row],
8335 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008337 if (enc_utf8 && ScreenLinesUC != NULL
8338 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {
8340 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8341 ScreenLinesUC + LineOffset[old_row],
8342 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008343 for (i = 0; i < p_mco; ++i)
8344 mch_memmove(new_ScreenLinesC[i]
8345 + new_LineOffset[new_row],
8346 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 (size_t)len * sizeof(u8char_T));
8348 }
8349 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8350 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8351 ScreenLines2 + LineOffset[old_row],
8352 (size_t)len * sizeof(schar_T));
8353#endif
8354 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8355 ScreenAttrs + LineOffset[old_row],
8356 (size_t)len * sizeof(sattr_T));
8357 }
8358 }
8359 }
8360 /* Use the last line of the screen for the current line. */
8361 current_ScreenLine = new_ScreenLines + Rows * Columns;
8362 }
8363
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008364 free_screenlines();
8365
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 ScreenLines = new_ScreenLines;
8367#ifdef FEAT_MBYTE
8368 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008369 for (i = 0; i < p_mco; ++i)
8370 ScreenLinesC[i] = new_ScreenLinesC[i];
8371 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 ScreenLines2 = new_ScreenLines2;
8373#endif
8374 ScreenAttrs = new_ScreenAttrs;
8375 LineOffset = new_LineOffset;
8376 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008377#ifdef FEAT_WINDOWS
8378 TabPageIdxs = new_TabPageIdxs;
8379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380
8381 /* It's important that screen_Rows and screen_Columns reflect the actual
8382 * size of ScreenLines[]. Set them before calling anything. */
8383#ifdef FEAT_GUI
8384 old_Rows = screen_Rows;
8385#endif
8386 screen_Rows = Rows;
8387 screen_Columns = Columns;
8388
8389 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008390 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 screenclear2();
8392
8393#ifdef FEAT_GUI
8394 else if (gui.in_use
8395 && !gui.starting
8396 && ScreenLines != NULL
8397 && old_Rows != Rows)
8398 {
8399 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8400 /*
8401 * Adjust the position of the cursor, for when executing an external
8402 * command.
8403 */
8404 if (msg_row >= Rows) /* Rows got smaller */
8405 msg_row = Rows - 1; /* put cursor at last row */
8406 else if (Rows > old_Rows) /* Rows got bigger */
8407 msg_row += Rows - old_Rows; /* put cursor in same place */
8408 if (msg_col >= Columns) /* Columns got smaller */
8409 msg_col = Columns - 1; /* put cursor at last column */
8410 }
8411#endif
8412
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008414 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008415
8416#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008417 /*
8418 * Do not apply autocommands more than 3 times to avoid an endless loop
8419 * in case applying autocommands always changes Rows or Columns.
8420 */
8421 if (starting == 0 && ++retry_count <= 3)
8422 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008423 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008424 /* In rare cases, autocommands may have altered Rows or Columns,
8425 * jump back to check if we need to allocate the screen again. */
8426 goto retry;
8427 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429}
8430
8431 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008432free_screenlines()
8433{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008434#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008435 int i;
8436
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008437 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008438 for (i = 0; i < Screen_mco; ++i)
8439 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008440 vim_free(ScreenLines2);
8441#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008442 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008443 vim_free(ScreenAttrs);
8444 vim_free(LineOffset);
8445 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008446#ifdef FEAT_WINDOWS
8447 vim_free(TabPageIdxs);
8448#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008449}
8450
8451 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452screenclear()
8453{
8454 check_for_delay(FALSE);
8455 screenalloc(FALSE); /* allocate screen buffers if size changed */
8456 screenclear2(); /* clear the screen */
8457}
8458
8459 static void
8460screenclear2()
8461{
8462 int i;
8463
8464 if (starting == NO_SCREEN || ScreenLines == NULL
8465#ifdef FEAT_GUI
8466 || (gui.in_use && gui.starting)
8467#endif
8468 )
8469 return;
8470
8471#ifdef FEAT_GUI
8472 if (!gui.in_use)
8473#endif
8474 screen_attr = -1; /* force setting the Normal colors */
8475 screen_stop_highlight(); /* don't want highlighting here */
8476
8477#ifdef FEAT_CLIPBOARD
8478 /* disable selection without redrawing it */
8479 clip_scroll_selection(9999);
8480#endif
8481
8482 /* blank out ScreenLines */
8483 for (i = 0; i < Rows; ++i)
8484 {
8485 lineclear(LineOffset[i], (int)Columns);
8486 LineWraps[i] = FALSE;
8487 }
8488
8489 if (can_clear(T_CL))
8490 {
8491 out_str(T_CL); /* clear the display */
8492 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008493 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 }
8495 else
8496 {
8497 /* can't clear the screen, mark all chars with invalid attributes */
8498 for (i = 0; i < Rows; ++i)
8499 lineinvalid(LineOffset[i], (int)Columns);
8500 clear_cmdline = TRUE;
8501 }
8502
8503 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8504
8505 win_rest_invalid(firstwin);
8506 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008507#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008508 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 if (must_redraw == CLEAR) /* no need to clear again */
8511 must_redraw = NOT_VALID;
8512 compute_cmdrow();
8513 msg_row = cmdline_row; /* put cursor on last line for messages */
8514 msg_col = 0;
8515 screen_start(); /* don't know where cursor is now */
8516 msg_scrolled = 0; /* can't scroll back */
8517 msg_didany = FALSE;
8518 msg_didout = FALSE;
8519}
8520
8521/*
8522 * Clear one line in ScreenLines.
8523 */
8524 static void
8525lineclear(off, width)
8526 unsigned off;
8527 int width;
8528{
8529 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8530#ifdef FEAT_MBYTE
8531 if (enc_utf8)
8532 (void)vim_memset(ScreenLinesUC + off, 0,
8533 (size_t)width * sizeof(u8char_T));
8534#endif
8535 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8536}
8537
8538/*
8539 * Mark one line in ScreenLines invalid by setting the attributes to an
8540 * invalid value.
8541 */
8542 static void
8543lineinvalid(off, width)
8544 unsigned off;
8545 int width;
8546{
8547 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8548}
8549
8550#ifdef FEAT_VERTSPLIT
8551/*
8552 * Copy part of a Screenline for vertically split window "wp".
8553 */
8554 static void
8555linecopy(to, from, wp)
8556 int to;
8557 int from;
8558 win_T *wp;
8559{
8560 unsigned off_to = LineOffset[to] + wp->w_wincol;
8561 unsigned off_from = LineOffset[from] + wp->w_wincol;
8562
8563 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8564 wp->w_width * sizeof(schar_T));
8565# ifdef FEAT_MBYTE
8566 if (enc_utf8)
8567 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008568 int i;
8569
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8571 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008572 for (i = 0; i < p_mco; ++i)
8573 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8574 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
8576 if (enc_dbcs == DBCS_JPNU)
8577 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8578 wp->w_width * sizeof(schar_T));
8579# endif
8580 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8581 wp->w_width * sizeof(sattr_T));
8582}
8583#endif
8584
8585/*
8586 * Return TRUE if clearing with term string "p" would work.
8587 * It can't work when the string is empty or it won't set the right background.
8588 */
8589 int
8590can_clear(p)
8591 char_u *p;
8592{
8593 return (*p != NUL && (t_colors <= 1
8594#ifdef FEAT_GUI
8595 || gui.in_use
8596#endif
8597 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8598}
8599
8600/*
8601 * Reset cursor position. Use whenever cursor was moved because of outputting
8602 * something directly to the screen (shell commands) or a terminal control
8603 * code.
8604 */
8605 void
8606screen_start()
8607{
8608 screen_cur_row = screen_cur_col = 9999;
8609}
8610
8611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 * Move the cursor to position "row","col" in the screen.
8613 * This tries to find the most efficient way to move, minimizing the number of
8614 * characters sent to the terminal.
8615 */
8616 void
8617windgoto(row, col)
8618 int row;
8619 int col;
8620{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008621 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 int i;
8623 int plan;
8624 int cost;
8625 int wouldbe_col;
8626 int noinvcurs;
8627 char_u *bs;
8628 int goto_cost;
8629 int attr;
8630
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008631#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8633
8634#define PLAN_LE 1
8635#define PLAN_CR 2
8636#define PLAN_NL 3
8637#define PLAN_WRITE 4
8638 /* Can't use ScreenLines unless initialized */
8639 if (ScreenLines == NULL)
8640 return;
8641
8642 if (col != screen_cur_col || row != screen_cur_row)
8643 {
8644 /* Check for valid position. */
8645 if (row < 0) /* window without text lines? */
8646 row = 0;
8647 if (row >= screen_Rows)
8648 row = screen_Rows - 1;
8649 if (col >= screen_Columns)
8650 col = screen_Columns - 1;
8651
8652 /* check if no cursor movement is allowed in highlight mode */
8653 if (screen_attr && *T_MS == NUL)
8654 noinvcurs = HIGHL_COST;
8655 else
8656 noinvcurs = 0;
8657 goto_cost = GOTO_COST + noinvcurs;
8658
8659 /*
8660 * Plan how to do the positioning:
8661 * 1. Use CR to move it to column 0, same row.
8662 * 2. Use T_LE to move it a few columns to the left.
8663 * 3. Use NL to move a few lines down, column 0.
8664 * 4. Move a few columns to the right with T_ND or by writing chars.
8665 *
8666 * Don't do this if the cursor went beyond the last column, the cursor
8667 * position is unknown then (some terminals wrap, some don't )
8668 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008669 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 * characters to move the cursor to the right.
8671 */
8672 if (row >= screen_cur_row && screen_cur_col < Columns)
8673 {
8674 /*
8675 * If the cursor is in the same row, bigger col, we can use CR
8676 * or T_LE.
8677 */
8678 bs = NULL; /* init for GCC */
8679 attr = screen_attr;
8680 if (row == screen_cur_row && col < screen_cur_col)
8681 {
8682 /* "le" is preferred over "bc", because "bc" is obsolete */
8683 if (*T_LE)
8684 bs = T_LE; /* "cursor left" */
8685 else
8686 bs = T_BC; /* "backspace character (old) */
8687 if (*bs)
8688 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8689 else
8690 cost = 999;
8691 if (col + 1 < cost) /* using CR is less characters */
8692 {
8693 plan = PLAN_CR;
8694 wouldbe_col = 0;
8695 cost = 1; /* CR is just one character */
8696 }
8697 else
8698 {
8699 plan = PLAN_LE;
8700 wouldbe_col = col;
8701 }
8702 if (noinvcurs) /* will stop highlighting */
8703 {
8704 cost += noinvcurs;
8705 attr = 0;
8706 }
8707 }
8708
8709 /*
8710 * If the cursor is above where we want to be, we can use CR LF.
8711 */
8712 else if (row > screen_cur_row)
8713 {
8714 plan = PLAN_NL;
8715 wouldbe_col = 0;
8716 cost = (row - screen_cur_row) * 2; /* CR LF */
8717 if (noinvcurs) /* will stop highlighting */
8718 {
8719 cost += noinvcurs;
8720 attr = 0;
8721 }
8722 }
8723
8724 /*
8725 * If the cursor is in the same row, smaller col, just use write.
8726 */
8727 else
8728 {
8729 plan = PLAN_WRITE;
8730 wouldbe_col = screen_cur_col;
8731 cost = 0;
8732 }
8733
8734 /*
8735 * Check if any characters that need to be written have the
8736 * correct attributes. Also avoid UTF-8 characters.
8737 */
8738 i = col - wouldbe_col;
8739 if (i > 0)
8740 cost += i;
8741 if (cost < goto_cost && i > 0)
8742 {
8743 /*
8744 * Check if the attributes are correct without additionally
8745 * stopping highlighting.
8746 */
8747 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8748 while (i && *p++ == attr)
8749 --i;
8750 if (i != 0)
8751 {
8752 /*
8753 * Try if it works when highlighting is stopped here.
8754 */
8755 if (*--p == 0)
8756 {
8757 cost += noinvcurs;
8758 while (i && *p++ == 0)
8759 --i;
8760 }
8761 if (i != 0)
8762 cost = 999; /* different attributes, don't do it */
8763 }
8764#ifdef FEAT_MBYTE
8765 if (enc_utf8)
8766 {
8767 /* Don't use an UTF-8 char for positioning, it's slow. */
8768 for (i = wouldbe_col; i < col; ++i)
8769 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8770 {
8771 cost = 999;
8772 break;
8773 }
8774 }
8775#endif
8776 }
8777
8778 /*
8779 * We can do it without term_windgoto()!
8780 */
8781 if (cost < goto_cost)
8782 {
8783 if (plan == PLAN_LE)
8784 {
8785 if (noinvcurs)
8786 screen_stop_highlight();
8787 while (screen_cur_col > col)
8788 {
8789 out_str(bs);
8790 --screen_cur_col;
8791 }
8792 }
8793 else if (plan == PLAN_CR)
8794 {
8795 if (noinvcurs)
8796 screen_stop_highlight();
8797 out_char('\r');
8798 screen_cur_col = 0;
8799 }
8800 else if (plan == PLAN_NL)
8801 {
8802 if (noinvcurs)
8803 screen_stop_highlight();
8804 while (screen_cur_row < row)
8805 {
8806 out_char('\n');
8807 ++screen_cur_row;
8808 }
8809 screen_cur_col = 0;
8810 }
8811
8812 i = col - screen_cur_col;
8813 if (i > 0)
8814 {
8815 /*
8816 * Use cursor-right if it's one character only. Avoids
8817 * removing a line of pixels from the last bold char, when
8818 * using the bold trick in the GUI.
8819 */
8820 if (T_ND[0] != NUL && T_ND[1] == NUL)
8821 {
8822 while (i-- > 0)
8823 out_char(*T_ND);
8824 }
8825 else
8826 {
8827 int off;
8828
8829 off = LineOffset[row] + screen_cur_col;
8830 while (i-- > 0)
8831 {
8832 if (ScreenAttrs[off] != screen_attr)
8833 screen_stop_highlight();
8834#ifdef FEAT_MBYTE
8835 out_flush_check();
8836#endif
8837 out_char(ScreenLines[off]);
8838#ifdef FEAT_MBYTE
8839 if (enc_dbcs == DBCS_JPNU
8840 && ScreenLines[off] == 0x8e)
8841 out_char(ScreenLines2[off]);
8842#endif
8843 ++off;
8844 }
8845 }
8846 }
8847 }
8848 }
8849 else
8850 cost = 999;
8851
8852 if (cost >= goto_cost)
8853 {
8854 if (noinvcurs)
8855 screen_stop_highlight();
8856 if (row == screen_cur_row && (col > screen_cur_col) &&
8857 *T_CRI != NUL)
8858 term_cursor_right(col - screen_cur_col);
8859 else
8860 term_windgoto(row, col);
8861 }
8862 screen_cur_row = row;
8863 screen_cur_col = col;
8864 }
8865}
8866
8867/*
8868 * Set cursor to its position in the current window.
8869 */
8870 void
8871setcursor()
8872{
8873 if (redrawing())
8874 {
8875 validate_cursor();
8876 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8877 W_WINCOL(curwin) + (
8878#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008879 /* With 'rightleft' set and the cursor on a double-wide
8880 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8882# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008883 (has_mbyte
8884 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8885 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886# endif
8887 1)) :
8888#endif
8889 curwin->w_wcol));
8890 }
8891}
8892
8893
8894/*
8895 * insert 'line_count' lines at 'row' in window 'wp'
8896 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8897 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8898 * scrolling.
8899 * Returns FAIL if the lines are not inserted, OK for success.
8900 */
8901 int
8902win_ins_lines(wp, row, line_count, invalid, mayclear)
8903 win_T *wp;
8904 int row;
8905 int line_count;
8906 int invalid;
8907 int mayclear;
8908{
8909 int did_delete;
8910 int nextrow;
8911 int lastrow;
8912 int retval;
8913
8914 if (invalid)
8915 wp->w_lines_valid = 0;
8916
8917 if (wp->w_height < 5)
8918 return FAIL;
8919
8920 if (line_count > wp->w_height - row)
8921 line_count = wp->w_height - row;
8922
8923 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8924 if (retval != MAYBE)
8925 return retval;
8926
8927 /*
8928 * If there is a next window or a status line, we first try to delete the
8929 * lines at the bottom to avoid messing what is after the window.
8930 * If this fails and there are following windows, don't do anything to avoid
8931 * messing up those windows, better just redraw.
8932 */
8933 did_delete = FALSE;
8934#ifdef FEAT_WINDOWS
8935 if (wp->w_next != NULL || wp->w_status_height)
8936 {
8937 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8938 line_count, (int)Rows, FALSE, NULL) == OK)
8939 did_delete = TRUE;
8940 else if (wp->w_next)
8941 return FAIL;
8942 }
8943#endif
8944 /*
8945 * if no lines deleted, blank the lines that will end up below the window
8946 */
8947 if (!did_delete)
8948 {
8949#ifdef FEAT_WINDOWS
8950 wp->w_redr_status = TRUE;
8951#endif
8952 redraw_cmdline = TRUE;
8953 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8954 lastrow = nextrow + line_count;
8955 if (lastrow > Rows)
8956 lastrow = Rows;
8957 screen_fill(nextrow - line_count, lastrow - line_count,
8958 W_WINCOL(wp), (int)W_ENDCOL(wp),
8959 ' ', ' ', 0);
8960 }
8961
8962 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8963 == FAIL)
8964 {
8965 /* deletion will have messed up other windows */
8966 if (did_delete)
8967 {
8968#ifdef FEAT_WINDOWS
8969 wp->w_redr_status = TRUE;
8970#endif
8971 win_rest_invalid(W_NEXT(wp));
8972 }
8973 return FAIL;
8974 }
8975
8976 return OK;
8977}
8978
8979/*
8980 * delete "line_count" window lines at "row" in window "wp"
8981 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8982 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8983 * scrolling
8984 * Return OK for success, FAIL if the lines are not deleted.
8985 */
8986 int
8987win_del_lines(wp, row, line_count, invalid, mayclear)
8988 win_T *wp;
8989 int row;
8990 int line_count;
8991 int invalid;
8992 int mayclear;
8993{
8994 int retval;
8995
8996 if (invalid)
8997 wp->w_lines_valid = 0;
8998
8999 if (line_count > wp->w_height - row)
9000 line_count = wp->w_height - row;
9001
9002 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9003 if (retval != MAYBE)
9004 return retval;
9005
9006 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9007 (int)Rows, FALSE, NULL) == FAIL)
9008 return FAIL;
9009
9010#ifdef FEAT_WINDOWS
9011 /*
9012 * If there are windows or status lines below, try to put them at the
9013 * correct place. If we can't do that, they have to be redrawn.
9014 */
9015 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9016 {
9017 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9018 line_count, (int)Rows, NULL) == FAIL)
9019 {
9020 wp->w_redr_status = TRUE;
9021 win_rest_invalid(wp->w_next);
9022 }
9023 }
9024 /*
9025 * If this is the last window and there is no status line, redraw the
9026 * command line later.
9027 */
9028 else
9029#endif
9030 redraw_cmdline = TRUE;
9031 return OK;
9032}
9033
9034/*
9035 * Common code for win_ins_lines() and win_del_lines().
9036 * Returns OK or FAIL when the work has been done.
9037 * Returns MAYBE when not finished yet.
9038 */
9039 static int
9040win_do_lines(wp, row, line_count, mayclear, del)
9041 win_T *wp;
9042 int row;
9043 int line_count;
9044 int mayclear;
9045 int del;
9046{
9047 int retval;
9048
9049 if (!redrawing() || line_count <= 0)
9050 return FAIL;
9051
9052 /* only a few lines left: redraw is faster */
9053 if (mayclear && Rows - line_count < 5
9054#ifdef FEAT_VERTSPLIT
9055 && wp->w_width == Columns
9056#endif
9057 )
9058 {
9059 screenclear(); /* will set wp->w_lines_valid to 0 */
9060 return FAIL;
9061 }
9062
9063 /*
9064 * Delete all remaining lines
9065 */
9066 if (row + line_count >= wp->w_height)
9067 {
9068 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9069 W_WINCOL(wp), (int)W_ENDCOL(wp),
9070 ' ', ' ', 0);
9071 return OK;
9072 }
9073
9074 /*
9075 * when scrolling, the message on the command line should be cleared,
9076 * otherwise it will stay there forever.
9077 */
9078 clear_cmdline = TRUE;
9079
9080 /*
9081 * If the terminal can set a scroll region, use that.
9082 * Always do this in a vertically split window. This will redraw from
9083 * ScreenLines[] when t_CV isn't defined. That's faster than using
9084 * win_line().
9085 * Don't use a scroll region when we are going to redraw the text, writing
9086 * a character in the lower right corner of the scroll region causes a
9087 * scroll-up in the DJGPP version.
9088 */
9089 if (scroll_region
9090#ifdef FEAT_VERTSPLIT
9091 || W_WIDTH(wp) != Columns
9092#endif
9093 )
9094 {
9095#ifdef FEAT_VERTSPLIT
9096 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9097#endif
9098 scroll_region_set(wp, row);
9099 if (del)
9100 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9101 wp->w_height - row, FALSE, wp);
9102 else
9103 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9104 wp->w_height - row, wp);
9105#ifdef FEAT_VERTSPLIT
9106 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9107#endif
9108 scroll_region_reset();
9109 return retval;
9110 }
9111
9112#ifdef FEAT_WINDOWS
9113 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9114 return FAIL;
9115#endif
9116
9117 return MAYBE;
9118}
9119
9120/*
9121 * window 'wp' and everything after it is messed up, mark it for redraw
9122 */
9123 static void
9124win_rest_invalid(wp)
9125 win_T *wp;
9126{
9127#ifdef FEAT_WINDOWS
9128 while (wp != NULL)
9129#else
9130 if (wp != NULL)
9131#endif
9132 {
9133 redraw_win_later(wp, NOT_VALID);
9134#ifdef FEAT_WINDOWS
9135 wp->w_redr_status = TRUE;
9136 wp = wp->w_next;
9137#endif
9138 }
9139 redraw_cmdline = TRUE;
9140}
9141
9142/*
9143 * The rest of the routines in this file perform screen manipulations. The
9144 * given operation is performed physically on the screen. The corresponding
9145 * change is also made to the internal screen image. In this way, the editor
9146 * anticipates the effect of editing changes on the appearance of the screen.
9147 * That way, when we call screenupdate a complete redraw isn't usually
9148 * necessary. Another advantage is that we can keep adding code to anticipate
9149 * screen changes, and in the meantime, everything still works.
9150 */
9151
9152/*
9153 * types for inserting or deleting lines
9154 */
9155#define USE_T_CAL 1
9156#define USE_T_CDL 2
9157#define USE_T_AL 3
9158#define USE_T_CE 4
9159#define USE_T_DL 5
9160#define USE_T_SR 6
9161#define USE_NL 7
9162#define USE_T_CD 8
9163#define USE_REDRAW 9
9164
9165/*
9166 * insert lines on the screen and update ScreenLines[]
9167 * 'end' is the line after the scrolled part. Normally it is Rows.
9168 * When scrolling region used 'off' is the offset from the top for the region.
9169 * 'row' and 'end' are relative to the start of the region.
9170 *
9171 * return FAIL for failure, OK for success.
9172 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009173 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174screen_ins_lines(off, row, line_count, end, wp)
9175 int off;
9176 int row;
9177 int line_count;
9178 int end;
9179 win_T *wp; /* NULL or window to use width from */
9180{
9181 int i;
9182 int j;
9183 unsigned temp;
9184 int cursor_row;
9185 int type;
9186 int result_empty;
9187 int can_ce = can_clear(T_CE);
9188
9189 /*
9190 * FAIL if
9191 * - there is no valid screen
9192 * - the screen has to be redrawn completely
9193 * - the line count is less than one
9194 * - the line count is more than 'ttyscroll'
9195 */
9196 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9197 return FAIL;
9198
9199 /*
9200 * There are seven ways to insert lines:
9201 * 0. When in a vertically split window and t_CV isn't set, redraw the
9202 * characters from ScreenLines[].
9203 * 1. Use T_CD (clear to end of display) if it exists and the result of
9204 * the insert is just empty lines
9205 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9206 * present or line_count > 1. It looks better if we do all the inserts
9207 * at once.
9208 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9209 * insert is just empty lines and T_CE is not present or line_count >
9210 * 1.
9211 * 4. Use T_AL (insert line) if it exists.
9212 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9213 * just empty lines.
9214 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9215 * just empty lines.
9216 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9217 * the 'da' flag is not set or we have clear line capability.
9218 * 8. redraw the characters from ScreenLines[].
9219 *
9220 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9221 * the scrollbar for the window. It does have insert line, use that if it
9222 * exists.
9223 */
9224 result_empty = (row + line_count >= end);
9225#ifdef FEAT_VERTSPLIT
9226 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9227 type = USE_REDRAW;
9228 else
9229#endif
9230 if (can_clear(T_CD) && result_empty)
9231 type = USE_T_CD;
9232 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9233 type = USE_T_CAL;
9234 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9235 type = USE_T_CDL;
9236 else if (*T_AL != NUL)
9237 type = USE_T_AL;
9238 else if (can_ce && result_empty)
9239 type = USE_T_CE;
9240 else if (*T_DL != NUL && result_empty)
9241 type = USE_T_DL;
9242 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9243 type = USE_T_SR;
9244 else
9245 return FAIL;
9246
9247 /*
9248 * For clearing the lines screen_del_lines() is used. This will also take
9249 * care of t_db if necessary.
9250 */
9251 if (type == USE_T_CD || type == USE_T_CDL ||
9252 type == USE_T_CE || type == USE_T_DL)
9253 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9254
9255 /*
9256 * If text is retained below the screen, first clear or delete as many
9257 * lines at the bottom of the window as are about to be inserted so that
9258 * the deleted lines won't later surface during a screen_del_lines.
9259 */
9260 if (*T_DB)
9261 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9262
9263#ifdef FEAT_CLIPBOARD
9264 /* Remove a modeless selection when inserting lines halfway the screen
9265 * or not the full width of the screen. */
9266 if (off + row > 0
9267# ifdef FEAT_VERTSPLIT
9268 || (wp != NULL && wp->w_width != Columns)
9269# endif
9270 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009271 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 else
9273 clip_scroll_selection(-line_count);
9274#endif
9275
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276#ifdef FEAT_GUI
9277 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9278 * scrolling is actually carried out. */
9279 gui_dont_update_cursor();
9280#endif
9281
9282 if (*T_CCS != NUL) /* cursor relative to region */
9283 cursor_row = row;
9284 else
9285 cursor_row = row + off;
9286
9287 /*
9288 * Shift LineOffset[] line_count down to reflect the inserted lines.
9289 * Clear the inserted lines in ScreenLines[].
9290 */
9291 row += off;
9292 end += off;
9293 for (i = 0; i < line_count; ++i)
9294 {
9295#ifdef FEAT_VERTSPLIT
9296 if (wp != NULL && wp->w_width != Columns)
9297 {
9298 /* need to copy part of a line */
9299 j = end - 1 - i;
9300 while ((j -= line_count) >= row)
9301 linecopy(j + line_count, j, wp);
9302 j += line_count;
9303 if (can_clear((char_u *)" "))
9304 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9305 else
9306 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9307 LineWraps[j] = FALSE;
9308 }
9309 else
9310#endif
9311 {
9312 j = end - 1 - i;
9313 temp = LineOffset[j];
9314 while ((j -= line_count) >= row)
9315 {
9316 LineOffset[j + line_count] = LineOffset[j];
9317 LineWraps[j + line_count] = LineWraps[j];
9318 }
9319 LineOffset[j + line_count] = temp;
9320 LineWraps[j + line_count] = FALSE;
9321 if (can_clear((char_u *)" "))
9322 lineclear(temp, (int)Columns);
9323 else
9324 lineinvalid(temp, (int)Columns);
9325 }
9326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327
9328 screen_stop_highlight();
9329 windgoto(cursor_row, 0);
9330
9331#ifdef FEAT_VERTSPLIT
9332 /* redraw the characters */
9333 if (type == USE_REDRAW)
9334 redraw_block(row, end, wp);
9335 else
9336#endif
9337 if (type == USE_T_CAL)
9338 {
9339 term_append_lines(line_count);
9340 screen_start(); /* don't know where cursor is now */
9341 }
9342 else
9343 {
9344 for (i = 0; i < line_count; i++)
9345 {
9346 if (type == USE_T_AL)
9347 {
9348 if (i && cursor_row != 0)
9349 windgoto(cursor_row, 0);
9350 out_str(T_AL);
9351 }
9352 else /* type == USE_T_SR */
9353 out_str(T_SR);
9354 screen_start(); /* don't know where cursor is now */
9355 }
9356 }
9357
9358 /*
9359 * With scroll-reverse and 'da' flag set we need to clear the lines that
9360 * have been scrolled down into the region.
9361 */
9362 if (type == USE_T_SR && *T_DA)
9363 {
9364 for (i = 0; i < line_count; ++i)
9365 {
9366 windgoto(off + i, 0);
9367 out_str(T_CE);
9368 screen_start(); /* don't know where cursor is now */
9369 }
9370 }
9371
9372#ifdef FEAT_GUI
9373 gui_can_update_cursor();
9374 if (gui.in_use)
9375 out_flush(); /* always flush after a scroll */
9376#endif
9377 return OK;
9378}
9379
9380/*
9381 * delete lines on the screen and update ScreenLines[]
9382 * 'end' is the line after the scrolled part. Normally it is Rows.
9383 * When scrolling region used 'off' is the offset from the top for the region.
9384 * 'row' and 'end' are relative to the start of the region.
9385 *
9386 * Return OK for success, FAIL if the lines are not deleted.
9387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 int
9389screen_del_lines(off, row, line_count, end, force, wp)
9390 int off;
9391 int row;
9392 int line_count;
9393 int end;
9394 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009395 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396{
9397 int j;
9398 int i;
9399 unsigned temp;
9400 int cursor_row;
9401 int cursor_end;
9402 int result_empty; /* result is empty until end of region */
9403 int can_delete; /* deleting line codes can be used */
9404 int type;
9405
9406 /*
9407 * FAIL if
9408 * - there is no valid screen
9409 * - the screen has to be redrawn completely
9410 * - the line count is less than one
9411 * - the line count is more than 'ttyscroll'
9412 */
9413 if (!screen_valid(TRUE) || line_count <= 0 ||
9414 (!force && line_count > p_ttyscroll))
9415 return FAIL;
9416
9417 /*
9418 * Check if the rest of the current region will become empty.
9419 */
9420 result_empty = row + line_count >= end;
9421
9422 /*
9423 * We can delete lines only when 'db' flag not set or when 'ce' option
9424 * available.
9425 */
9426 can_delete = (*T_DB == NUL || can_clear(T_CE));
9427
9428 /*
9429 * There are six ways to delete lines:
9430 * 0. When in a vertically split window and t_CV isn't set, redraw the
9431 * characters from ScreenLines[].
9432 * 1. Use T_CD if it exists and the result is empty.
9433 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9434 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9435 * none of the other ways work.
9436 * 4. Use T_CE (erase line) if the result is empty.
9437 * 5. Use T_DL (delete line) if it exists.
9438 * 6. redraw the characters from ScreenLines[].
9439 */
9440#ifdef FEAT_VERTSPLIT
9441 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9442 type = USE_REDRAW;
9443 else
9444#endif
9445 if (can_clear(T_CD) && result_empty)
9446 type = USE_T_CD;
9447#if defined(__BEOS__) && defined(BEOS_DR8)
9448 /*
9449 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9450 * its internal termcap... this works okay for tests which test *T_DB !=
9451 * NUL. It has the disadvantage that the user cannot use any :set t_*
9452 * command to get T_DB (back) to empty_option, only :set term=... will do
9453 * the trick...
9454 * Anyway, this hack will hopefully go away with the next OS release.
9455 * (Olaf Seibert)
9456 */
9457 else if (row == 0 && T_DB == empty_option
9458 && (line_count == 1 || *T_CDL == NUL))
9459#else
9460 else if (row == 0 && (
9461#ifndef AMIGA
9462 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9463 * up, so use delete-line command */
9464 line_count == 1 ||
9465#endif
9466 *T_CDL == NUL))
9467#endif
9468 type = USE_NL;
9469 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9470 type = USE_T_CDL;
9471 else if (can_clear(T_CE) && result_empty
9472#ifdef FEAT_VERTSPLIT
9473 && (wp == NULL || wp->w_width == Columns)
9474#endif
9475 )
9476 type = USE_T_CE;
9477 else if (*T_DL != NUL && can_delete)
9478 type = USE_T_DL;
9479 else if (*T_CDL != NUL && can_delete)
9480 type = USE_T_CDL;
9481 else
9482 return FAIL;
9483
9484#ifdef FEAT_CLIPBOARD
9485 /* Remove a modeless selection when deleting lines halfway the screen or
9486 * not the full width of the screen. */
9487 if (off + row > 0
9488# ifdef FEAT_VERTSPLIT
9489 || (wp != NULL && wp->w_width != Columns)
9490# endif
9491 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009492 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 else
9494 clip_scroll_selection(line_count);
9495#endif
9496
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497#ifdef FEAT_GUI
9498 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9499 * scrolling is actually carried out. */
9500 gui_dont_update_cursor();
9501#endif
9502
9503 if (*T_CCS != NUL) /* cursor relative to region */
9504 {
9505 cursor_row = row;
9506 cursor_end = end;
9507 }
9508 else
9509 {
9510 cursor_row = row + off;
9511 cursor_end = end + off;
9512 }
9513
9514 /*
9515 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9516 * Clear the inserted lines in ScreenLines[].
9517 */
9518 row += off;
9519 end += off;
9520 for (i = 0; i < line_count; ++i)
9521 {
9522#ifdef FEAT_VERTSPLIT
9523 if (wp != NULL && wp->w_width != Columns)
9524 {
9525 /* need to copy part of a line */
9526 j = row + i;
9527 while ((j += line_count) <= end - 1)
9528 linecopy(j - line_count, j, wp);
9529 j -= line_count;
9530 if (can_clear((char_u *)" "))
9531 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9532 else
9533 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9534 LineWraps[j] = FALSE;
9535 }
9536 else
9537#endif
9538 {
9539 /* whole width, moving the line pointers is faster */
9540 j = row + i;
9541 temp = LineOffset[j];
9542 while ((j += line_count) <= end - 1)
9543 {
9544 LineOffset[j - line_count] = LineOffset[j];
9545 LineWraps[j - line_count] = LineWraps[j];
9546 }
9547 LineOffset[j - line_count] = temp;
9548 LineWraps[j - line_count] = FALSE;
9549 if (can_clear((char_u *)" "))
9550 lineclear(temp, (int)Columns);
9551 else
9552 lineinvalid(temp, (int)Columns);
9553 }
9554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
9556 screen_stop_highlight();
9557
9558#ifdef FEAT_VERTSPLIT
9559 /* redraw the characters */
9560 if (type == USE_REDRAW)
9561 redraw_block(row, end, wp);
9562 else
9563#endif
9564 if (type == USE_T_CD) /* delete the lines */
9565 {
9566 windgoto(cursor_row, 0);
9567 out_str(T_CD);
9568 screen_start(); /* don't know where cursor is now */
9569 }
9570 else if (type == USE_T_CDL)
9571 {
9572 windgoto(cursor_row, 0);
9573 term_delete_lines(line_count);
9574 screen_start(); /* don't know where cursor is now */
9575 }
9576 /*
9577 * Deleting lines at top of the screen or scroll region: Just scroll
9578 * the whole screen (scroll region) up by outputting newlines on the
9579 * last line.
9580 */
9581 else if (type == USE_NL)
9582 {
9583 windgoto(cursor_end - 1, 0);
9584 for (i = line_count; --i >= 0; )
9585 out_char('\n'); /* cursor will remain on same line */
9586 }
9587 else
9588 {
9589 for (i = line_count; --i >= 0; )
9590 {
9591 if (type == USE_T_DL)
9592 {
9593 windgoto(cursor_row, 0);
9594 out_str(T_DL); /* delete a line */
9595 }
9596 else /* type == USE_T_CE */
9597 {
9598 windgoto(cursor_row + i, 0);
9599 out_str(T_CE); /* erase a line */
9600 }
9601 screen_start(); /* don't know where cursor is now */
9602 }
9603 }
9604
9605 /*
9606 * If the 'db' flag is set, we need to clear the lines that have been
9607 * scrolled up at the bottom of the region.
9608 */
9609 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9610 {
9611 for (i = line_count; i > 0; --i)
9612 {
9613 windgoto(cursor_end - i, 0);
9614 out_str(T_CE); /* erase a line */
9615 screen_start(); /* don't know where cursor is now */
9616 }
9617 }
9618
9619#ifdef FEAT_GUI
9620 gui_can_update_cursor();
9621 if (gui.in_use)
9622 out_flush(); /* always flush after a scroll */
9623#endif
9624
9625 return OK;
9626}
9627
9628/*
9629 * show the current mode and ruler
9630 *
9631 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9632 * If clear_cmdline is FALSE there may be a message there that needs to be
9633 * cleared only if a mode is shown.
9634 * Return the length of the message (0 if no message).
9635 */
9636 int
9637showmode()
9638{
9639 int need_clear;
9640 int length = 0;
9641 int do_mode;
9642 int attr;
9643 int nwr_save;
9644#ifdef FEAT_INS_EXPAND
9645 int sub_attr;
9646#endif
9647
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009648 do_mode = ((p_smd && msg_silent == 0)
9649 && ((State & INSERT)
9650 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651#ifdef FEAT_VISUAL
9652 || VIsual_active
9653#endif
9654 ));
9655 if (do_mode || Recording)
9656 {
9657 /*
9658 * Don't show mode right now, when not redrawing or inside a mapping.
9659 * Call char_avail() only when we are going to show something, because
9660 * it takes a bit of time.
9661 */
9662 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9663 {
9664 redraw_cmdline = TRUE; /* show mode later */
9665 return 0;
9666 }
9667
9668 nwr_save = need_wait_return;
9669
9670 /* wait a bit before overwriting an important message */
9671 check_for_delay(FALSE);
9672
9673 /* if the cmdline is more than one line high, erase top lines */
9674 need_clear = clear_cmdline;
9675 if (clear_cmdline && cmdline_row < Rows - 1)
9676 msg_clr_cmdline(); /* will reset clear_cmdline */
9677
9678 /* Position on the last line in the window, column 0 */
9679 msg_pos_mode();
9680 cursor_off();
9681 attr = hl_attr(HLF_CM); /* Highlight mode */
9682 if (do_mode)
9683 {
9684 MSG_PUTS_ATTR("--", attr);
9685#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009686 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009687# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009688 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009689# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009690 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009691# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009692 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009693# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 MSG_PUTS_ATTR(" IM", attr);
9695# else
9696 MSG_PUTS_ATTR(" XIM", attr);
9697# endif
9698#endif
9699#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9700 if (gui.in_use)
9701 {
9702 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009703 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 }
9705#endif
9706#ifdef FEAT_INS_EXPAND
9707 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9708 {
9709 /* These messages can get long, avoid a wrap in a narrow
9710 * window. Prefer showing edit_submode_extra. */
9711 length = (Rows - msg_row) * Columns - 3;
9712 if (edit_submode_extra != NULL)
9713 length -= vim_strsize(edit_submode_extra);
9714 if (length > 0)
9715 {
9716 if (edit_submode_pre != NULL)
9717 length -= vim_strsize(edit_submode_pre);
9718 if (length - vim_strsize(edit_submode) > 0)
9719 {
9720 if (edit_submode_pre != NULL)
9721 msg_puts_attr(edit_submode_pre, attr);
9722 msg_puts_attr(edit_submode, attr);
9723 }
9724 if (edit_submode_extra != NULL)
9725 {
9726 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9727 if ((int)edit_submode_highl < (int)HLF_COUNT)
9728 sub_attr = hl_attr(edit_submode_highl);
9729 else
9730 sub_attr = attr;
9731 msg_puts_attr(edit_submode_extra, sub_attr);
9732 }
9733 }
9734 length = 0;
9735 }
9736 else
9737#endif
9738 {
9739#ifdef FEAT_VREPLACE
9740 if (State & VREPLACE_FLAG)
9741 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9742 else
9743#endif
9744 if (State & REPLACE_FLAG)
9745 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9746 else if (State & INSERT)
9747 {
9748#ifdef FEAT_RIGHTLEFT
9749 if (p_ri)
9750 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9751#endif
9752 MSG_PUTS_ATTR(_(" INSERT"), attr);
9753 }
9754 else if (restart_edit == 'I')
9755 MSG_PUTS_ATTR(_(" (insert)"), attr);
9756 else if (restart_edit == 'R')
9757 MSG_PUTS_ATTR(_(" (replace)"), attr);
9758 else if (restart_edit == 'V')
9759 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9760#ifdef FEAT_RIGHTLEFT
9761 if (p_hkmap)
9762 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9763# ifdef FEAT_FKMAP
9764 if (p_fkmap)
9765 MSG_PUTS_ATTR(farsi_text_5, attr);
9766# endif
9767#endif
9768#ifdef FEAT_KEYMAP
9769 if (State & LANGMAP)
9770 {
9771# ifdef FEAT_ARABIC
9772 if (curwin->w_p_arab)
9773 MSG_PUTS_ATTR(_(" Arabic"), attr);
9774 else
9775# endif
9776 MSG_PUTS_ATTR(_(" (lang)"), attr);
9777 }
9778#endif
9779 if ((State & INSERT) && p_paste)
9780 MSG_PUTS_ATTR(_(" (paste)"), attr);
9781
9782#ifdef FEAT_VISUAL
9783 if (VIsual_active)
9784 {
9785 char *p;
9786
9787 /* Don't concatenate separate words to avoid translation
9788 * problems. */
9789 switch ((VIsual_select ? 4 : 0)
9790 + (VIsual_mode == Ctrl_V) * 2
9791 + (VIsual_mode == 'V'))
9792 {
9793 case 0: p = N_(" VISUAL"); break;
9794 case 1: p = N_(" VISUAL LINE"); break;
9795 case 2: p = N_(" VISUAL BLOCK"); break;
9796 case 4: p = N_(" SELECT"); break;
9797 case 5: p = N_(" SELECT LINE"); break;
9798 default: p = N_(" SELECT BLOCK"); break;
9799 }
9800 MSG_PUTS_ATTR(_(p), attr);
9801 }
9802#endif
9803 MSG_PUTS_ATTR(" --", attr);
9804 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009805
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 need_clear = TRUE;
9807 }
9808 if (Recording
9809#ifdef FEAT_INS_EXPAND
9810 && edit_submode == NULL /* otherwise it gets too long */
9811#endif
9812 )
9813 {
9814 MSG_PUTS_ATTR(_("recording"), attr);
9815 need_clear = TRUE;
9816 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009817
9818 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 if (need_clear || clear_cmdline)
9820 msg_clr_eos();
9821 msg_didout = FALSE; /* overwrite this message */
9822 length = msg_col;
9823 msg_col = 0;
9824 need_wait_return = nwr_save; /* never ask for hit-return for this */
9825 }
9826 else if (clear_cmdline && msg_silent == 0)
9827 /* Clear the whole command line. Will reset "clear_cmdline". */
9828 msg_clr_cmdline();
9829
9830#ifdef FEAT_CMDL_INFO
9831# ifdef FEAT_VISUAL
9832 /* In Visual mode the size of the selected area must be redrawn. */
9833 if (VIsual_active)
9834 clear_showcmd();
9835# endif
9836
9837 /* If the last window has no status line, the ruler is after the mode
9838 * message and must be redrawn */
9839 if (redrawing()
9840# ifdef FEAT_WINDOWS
9841 && lastwin->w_status_height == 0
9842# endif
9843 )
9844 win_redr_ruler(lastwin, TRUE);
9845#endif
9846 redraw_cmdline = FALSE;
9847 clear_cmdline = FALSE;
9848
9849 return length;
9850}
9851
9852/*
9853 * Position for a mode message.
9854 */
9855 static void
9856msg_pos_mode()
9857{
9858 msg_col = 0;
9859 msg_row = Rows - 1;
9860}
9861
9862/*
9863 * Delete mode message. Used when ESC is typed which is expected to end
9864 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009865 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 */
9867 void
9868unshowmode(force)
9869 int force;
9870{
9871 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009872 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 */
9874 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9875 redraw_cmdline = TRUE; /* delete mode later */
9876 else
9877 {
9878 msg_pos_mode();
9879 if (Recording)
9880 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9881 msg_clr_eos();
9882 }
9883}
9884
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009885#if defined(FEAT_WINDOWS)
9886/*
9887 * Draw the tab pages line at the top of the Vim window.
9888 */
9889 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009890draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009891{
9892 int tabcount = 0;
9893 tabpage_T *tp;
9894 int tabwidth;
9895 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009896 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009897 int attr;
9898 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009899 win_T *cwp;
9900 int wincount;
9901 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009902 int c;
9903 int len;
9904 int attr_sel = hl_attr(HLF_TPS);
9905 int attr_nosel = hl_attr(HLF_TP);
9906 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009907 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009908 int room;
9909 int use_sep_chars = (t_colors < 8
9910#ifdef FEAT_GUI
9911 && !gui.in_use
9912#endif
9913 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009914
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009915 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009916
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009917#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009918 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009919 if (gui_use_tabline())
9920 {
9921 gui_update_tabline();
9922 return;
9923 }
9924#endif
9925
9926 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009927 return;
9928
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009929#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009930
9931 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9932 for (scol = 0; scol < Columns; ++scol)
9933 TabPageIdxs[scol] = 0;
9934
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009935 /* Use the 'tabline' option if it's set. */
9936 if (*p_tal != NUL)
9937 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009938 int save_called_emsg = called_emsg;
9939
9940 /* Check for an error. If there is one we would loop in redrawing the
9941 * screen. Avoid that by making 'tabline' empty. */
9942 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009943 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009944 if (called_emsg)
9945 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009946 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009947 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009948 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009949 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009950#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009951 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009952 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9953 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009954
Bram Moolenaar238a5642006-02-21 22:12:05 +00009955 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9956 if (tabwidth < 6)
9957 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009958
Bram Moolenaar238a5642006-02-21 22:12:05 +00009959 attr = attr_nosel;
9960 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009961 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009962 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9963 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009964 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009965 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009966
Bram Moolenaar238a5642006-02-21 22:12:05 +00009967 if (tp->tp_topframe == topframe)
9968 attr = attr_sel;
9969 if (use_sep_chars && col > 0)
9970 screen_putchar('|', 0, col++, attr);
9971
9972 if (tp->tp_topframe != topframe)
9973 attr = attr_nosel;
9974
9975 screen_putchar(' ', 0, col++, attr);
9976
9977 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009978 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009979 cwp = curwin;
9980 wp = firstwin;
9981 }
9982 else
9983 {
9984 cwp = tp->tp_curwin;
9985 wp = tp->tp_firstwin;
9986 }
9987
9988 modified = FALSE;
9989 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9990 if (bufIsChanged(wp->w_buffer))
9991 modified = TRUE;
9992 if (modified || wincount > 1)
9993 {
9994 if (wincount > 1)
9995 {
9996 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009997 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009998 if (col + len >= Columns - 3)
9999 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010000 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010001#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010002 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010003#else
Bram Moolenaar238a5642006-02-21 22:12:05 +000010004 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010005#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010006 );
10007 col += len;
10008 }
10009 if (modified)
10010 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10011 screen_putchar(' ', 0, col++, attr);
10012 }
10013
10014 room = scol - col + tabwidth - 1;
10015 if (room > 0)
10016 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010017 /* Get buffer name in NameBuff[] */
10018 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010019 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010020 len = vim_strsize(NameBuff);
10021 p = NameBuff;
10022#ifdef FEAT_MBYTE
10023 if (has_mbyte)
10024 while (len > room)
10025 {
10026 len -= ptr2cells(p);
10027 mb_ptr_adv(p);
10028 }
10029 else
10030#endif
10031 if (len > room)
10032 {
10033 p += len - room;
10034 len = room;
10035 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010036 if (len > Columns - col - 1)
10037 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010038
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010039 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010040 col += len;
10041 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010042 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010043
10044 /* Store the tab page number in TabPageIdxs[], so that
10045 * jump_to_mouse() knows where each one is. */
10046 ++tabcount;
10047 while (scol < col)
10048 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010049 }
10050
Bram Moolenaar238a5642006-02-21 22:12:05 +000010051 if (use_sep_chars)
10052 c = '_';
10053 else
10054 c = ' ';
10055 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010056
10057 /* Put an "X" for closing the current tab if there are several. */
10058 if (first_tabpage->tp_next != NULL)
10059 {
10060 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10061 TabPageIdxs[Columns - 1] = -999;
10062 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010063 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010064
10065 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10066 * set. */
10067 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010068}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010069
10070/*
10071 * Get buffer name for "buf" into NameBuff[].
10072 * Takes care of special buffer names and translates special characters.
10073 */
10074 void
10075get_trans_bufname(buf)
10076 buf_T *buf;
10077{
10078 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010079 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010080 else
10081 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10082 trans_characters(NameBuff, MAXPATHL);
10083}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010084#endif
10085
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10087/*
10088 * Get the character to use in a status line. Get its attributes in "*attr".
10089 */
10090 static int
10091fillchar_status(attr, is_curwin)
10092 int *attr;
10093 int is_curwin;
10094{
10095 int fill;
10096 if (is_curwin)
10097 {
10098 *attr = hl_attr(HLF_S);
10099 fill = fill_stl;
10100 }
10101 else
10102 {
10103 *attr = hl_attr(HLF_SNC);
10104 fill = fill_stlnc;
10105 }
10106 /* Use fill when there is highlighting, and highlighting of current
10107 * window differs, or the fillchars differ, or this is not the
10108 * current window */
10109 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10110 || !is_curwin || firstwin == lastwin)
10111 || (fill_stl != fill_stlnc)))
10112 return fill;
10113 if (is_curwin)
10114 return '^';
10115 return '=';
10116}
10117#endif
10118
10119#ifdef FEAT_VERTSPLIT
10120/*
10121 * Get the character to use in a separator between vertically split windows.
10122 * Get its attributes in "*attr".
10123 */
10124 static int
10125fillchar_vsep(attr)
10126 int *attr;
10127{
10128 *attr = hl_attr(HLF_C);
10129 if (*attr == 0 && fill_vert == ' ')
10130 return '|';
10131 else
10132 return fill_vert;
10133}
10134#endif
10135
10136/*
10137 * Return TRUE if redrawing should currently be done.
10138 */
10139 int
10140redrawing()
10141{
10142 return (!RedrawingDisabled
10143 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10144}
10145
10146/*
10147 * Return TRUE if printing messages should currently be done.
10148 */
10149 int
10150messaging()
10151{
10152 return (!(p_lz && char_avail() && !KeyTyped));
10153}
10154
10155/*
10156 * Show current status info in ruler and various other places
10157 * If always is FALSE, only show ruler if position has changed.
10158 */
10159 void
10160showruler(always)
10161 int always;
10162{
10163 if (!always && !redrawing())
10164 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010165#ifdef FEAT_INS_EXPAND
10166 if (pum_visible())
10167 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010168# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010169 /* Don't redraw right now, do it later. */
10170 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010171# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010172 return;
10173 }
10174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010176 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010177 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010178 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 else
10181#endif
10182#ifdef FEAT_CMDL_INFO
10183 win_redr_ruler(curwin, always);
10184#endif
10185
10186#ifdef FEAT_TITLE
10187 if (need_maketitle
10188# ifdef FEAT_STL_OPT
10189 || (p_icon && (stl_syntax & STL_IN_ICON))
10190 || (p_title && (stl_syntax & STL_IN_TITLE))
10191# endif
10192 )
10193 maketitle();
10194#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010195#ifdef FEAT_WINDOWS
10196 /* Redraw the tab pages line if needed. */
10197 if (redraw_tabline)
10198 draw_tabline();
10199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200}
10201
10202#ifdef FEAT_CMDL_INFO
10203 static void
10204win_redr_ruler(wp, always)
10205 win_T *wp;
10206 int always;
10207{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010208#define RULER_BUF_LEN 70
10209 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 int row;
10211 int fillchar;
10212 int attr;
10213 int empty_line = FALSE;
10214 colnr_T virtcol;
10215 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010216 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 int o;
10218#ifdef FEAT_VERTSPLIT
10219 int this_ru_col;
10220 int off = 0;
10221 int width = Columns;
10222# define WITH_OFF(x) x
10223# define WITH_WIDTH(x) x
10224#else
10225# define WITH_OFF(x) 0
10226# define WITH_WIDTH(x) Columns
10227# define this_ru_col ru_col
10228#endif
10229
10230 /* If 'ruler' off or redrawing disabled, don't do anything */
10231 if (!p_ru)
10232 return;
10233
10234 /*
10235 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10236 * after deleting lines, before cursor.lnum is corrected.
10237 */
10238 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10239 return;
10240
10241#ifdef FEAT_INS_EXPAND
10242 /* Don't draw the ruler while doing insert-completion, it might overwrite
10243 * the (long) mode message. */
10244# ifdef FEAT_WINDOWS
10245 if (wp == lastwin && lastwin->w_status_height == 0)
10246# endif
10247 if (edit_submode != NULL)
10248 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010249 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10250 if (pum_visible())
10251 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252#endif
10253
10254#ifdef FEAT_STL_OPT
10255 if (*p_ruf)
10256 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010257 int save_called_emsg = called_emsg;
10258
10259 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010261 if (called_emsg)
10262 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010263 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010264 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 return;
10266 }
10267#endif
10268
10269 /*
10270 * Check if not in Insert mode and the line is empty (will show "0-1").
10271 */
10272 if (!(State & INSERT)
10273 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10274 empty_line = TRUE;
10275
10276 /*
10277 * Only draw the ruler when something changed.
10278 */
10279 validate_virtcol_win(wp);
10280 if ( redraw_cmdline
10281 || always
10282 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10283 || wp->w_cursor.col != wp->w_ru_cursor.col
10284 || wp->w_virtcol != wp->w_ru_virtcol
10285#ifdef FEAT_VIRTUALEDIT
10286 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10287#endif
10288 || wp->w_topline != wp->w_ru_topline
10289 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10290#ifdef FEAT_DIFF
10291 || wp->w_topfill != wp->w_ru_topfill
10292#endif
10293 || empty_line != wp->w_ru_empty)
10294 {
10295 cursor_off();
10296#ifdef FEAT_WINDOWS
10297 if (wp->w_status_height)
10298 {
10299 row = W_WINROW(wp) + wp->w_height;
10300 fillchar = fillchar_status(&attr, wp == curwin);
10301# ifdef FEAT_VERTSPLIT
10302 off = W_WINCOL(wp);
10303 width = W_WIDTH(wp);
10304# endif
10305 }
10306 else
10307#endif
10308 {
10309 row = Rows - 1;
10310 fillchar = ' ';
10311 attr = 0;
10312#ifdef FEAT_VERTSPLIT
10313 width = Columns;
10314 off = 0;
10315#endif
10316 }
10317
10318 /* In list mode virtcol needs to be recomputed */
10319 virtcol = wp->w_virtcol;
10320 if (wp->w_p_list && lcs_tab1 == NUL)
10321 {
10322 wp->w_p_list = FALSE;
10323 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10324 wp->w_p_list = TRUE;
10325 }
10326
10327 /*
10328 * Some sprintfs return the length, some return a pointer.
10329 * To avoid portability problems we use strlen() here.
10330 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010331 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10333 ? 0L
10334 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010335 len = STRLEN(buffer);
10336 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10338 (int)virtcol + 1);
10339
10340 /*
10341 * Add a "50%" if there is room for it.
10342 * On the last line, don't print in the last column (scrolls the
10343 * screen up on some terminals).
10344 */
10345 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010346 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 o = i + vim_strsize(buffer + i + 1);
10348#ifdef FEAT_WINDOWS
10349 if (wp->w_status_height == 0) /* can't use last char of screen */
10350#endif
10351 ++o;
10352#ifdef FEAT_VERTSPLIT
10353 this_ru_col = ru_col - (Columns - width);
10354 if (this_ru_col < 0)
10355 this_ru_col = 0;
10356#endif
10357 /* Never use more than half the window/screen width, leave the other
10358 * half for the filename. */
10359 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10360 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10361 if (this_ru_col + o < WITH_WIDTH(width))
10362 {
10363 while (this_ru_col + o < WITH_WIDTH(width))
10364 {
10365#ifdef FEAT_MBYTE
10366 if (has_mbyte)
10367 i += (*mb_char2bytes)(fillchar, buffer + i);
10368 else
10369#endif
10370 buffer[i++] = fillchar;
10371 ++o;
10372 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010373 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 }
10375 /* Truncate at window boundary. */
10376#ifdef FEAT_MBYTE
10377 if (has_mbyte)
10378 {
10379 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010380 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 {
10382 o += (*mb_ptr2cells)(buffer + i);
10383 if (this_ru_col + o > WITH_WIDTH(width))
10384 {
10385 buffer[i] = NUL;
10386 break;
10387 }
10388 }
10389 }
10390 else
10391#endif
10392 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10393 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10394
10395 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10396 i = redraw_cmdline;
10397 screen_fill(row, row + 1,
10398 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10399 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10400 fillchar, fillchar, attr);
10401 /* don't redraw the cmdline because of showing the ruler */
10402 redraw_cmdline = i;
10403 wp->w_ru_cursor = wp->w_cursor;
10404 wp->w_ru_virtcol = wp->w_virtcol;
10405 wp->w_ru_empty = empty_line;
10406 wp->w_ru_topline = wp->w_topline;
10407 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10408#ifdef FEAT_DIFF
10409 wp->w_ru_topfill = wp->w_topfill;
10410#endif
10411 }
10412}
10413#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010414
10415#if defined(FEAT_LINEBREAK) || defined(PROTO)
10416/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010417 * Return the width of the 'number' and 'relativenumber' column.
10418 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010419 * Otherwise it depends on 'numberwidth' and the line count.
10420 */
10421 int
10422number_width(wp)
10423 win_T *wp;
10424{
10425 int n;
10426 linenr_T lnum;
10427
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010428 if (wp->w_p_rnu && !wp->w_p_nu)
10429 /* cursor line shows "0" */
10430 lnum = wp->w_height;
10431 else
10432 /* cursor line shows absolute line number */
10433 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010434
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010435 if (lnum == wp->w_nrwidth_line_count)
10436 return wp->w_nrwidth_width;
10437 wp->w_nrwidth_line_count = lnum;
10438
10439 n = 0;
10440 do
10441 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010442 lnum /= 10;
10443 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010444 } while (lnum > 0);
10445
10446 /* 'numberwidth' gives the minimal width plus one */
10447 if (n < wp->w_p_nuw - 1)
10448 n = wp->w_p_nuw - 1;
10449
10450 wp->w_nrwidth_width = n;
10451 return n;
10452}
10453#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010454
10455/*
10456 * Return the current cursor column. This is the actual position on the
10457 * screen. First column is 0.
10458 */
10459 int
10460screen_screencol()
10461{
10462 return screen_cur_col;
10463}
10464
10465/*
10466 * Return the current cursor row. This is the actual position on the screen.
10467 * First row is 0.
10468 */
10469 int
10470screen_screenrow()
10471{
10472 return screen_cur_row;
10473}