blob: 9d0d63a5bdefb10269e61868b27156228fdb043e [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;
282 schar_T *screenline; /* copy from ScreenLines[] */
283 sattr_T *screenattr; /* copy from ScreenAttrs[] */
284#ifdef FEAT_MBYTE
285 int i;
286 u8char_T *screenlineUC; /* copy from ScreenLinesUC[] */
287 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
288 schar_T *screenline2; /* copy from ScreenLines2[] */
289#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 }
392 setcursor();
393 }
394
395 vim_free(screenline);
396 vim_free(screenattr);
397#ifdef FEAT_MBYTE
398 if (enc_utf8)
399 {
400 vim_free(screenlineUC);
401 for (i = 0; i < p_mco; ++i)
402 vim_free(screenlineC[i]);
403 }
404 if (enc_dbcs == DBCS_JPNU)
405 vim_free(screenline2);
406#endif
407
408 return ret;
409}
410
411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 * Changed something in the current window, at buffer line "lnum", that
413 * requires that line and possibly other lines to be redrawn.
414 * Used when entering/leaving Insert mode with the cursor on a folded line.
415 * Used to remove the "$" from a change command.
416 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
417 * may become invalid and the whole window will have to be redrawn.
418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 void
420redrawWinline(lnum, invalid)
421 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000422 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423{
424#ifdef FEAT_FOLDING
425 int i;
426#endif
427
428 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
429 curwin->w_redraw_top = lnum;
430 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
431 curwin->w_redraw_bot = lnum;
432 redraw_later(VALID);
433
434#ifdef FEAT_FOLDING
435 if (invalid)
436 {
437 /* A w_lines[] entry for this lnum has become invalid. */
438 i = find_wl_entry(curwin, lnum);
439 if (i >= 0)
440 curwin->w_lines[i].wl_valid = FALSE;
441 }
442#endif
443}
444
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200445#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200446 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447/*
448 * update all windows that are editing the current buffer
449 */
450 void
451update_curbuf(type)
452 int type;
453{
454 redraw_curbuf_later(type);
455 update_screen(type);
456}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458
459/*
460 * update_screen()
461 *
462 * Based on the current value of curwin->w_topline, transfer a screenfull
463 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
464 */
465 void
466update_screen(type)
467 int type;
468{
469 win_T *wp;
470 static int did_intro = FALSE;
471#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
472 int did_one;
473#endif
474
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000475 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (!screen_valid(TRUE))
477 return;
478
479 if (must_redraw)
480 {
481 if (type < must_redraw) /* use maximal type */
482 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000483
484 /* must_redraw is reset here, so that when we run into some weird
485 * reason to redraw while busy redrawing (e.g., asynchronous
486 * scrolling), or update_topline() in win_update() will cause a
487 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 must_redraw = 0;
489 }
490
491 /* Need to update w_lines[]. */
492 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
493 type = NOT_VALID;
494
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000495 /* Postpone the redrawing when it's not needed and when being called
496 * recursively. */
497 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 {
499 redraw_later(type); /* remember type for next time */
500 must_redraw = type;
501 if (type > INVERTED_ALL)
502 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
503 return;
504 }
505
506 updating_screen = TRUE;
507#ifdef FEAT_SYN_HL
508 ++display_tick; /* let syntax code know we're in a next round of
509 * display updating */
510#endif
511
512 /*
513 * if the screen was scrolled up when displaying a message, scroll it down
514 */
515 if (msg_scrolled)
516 {
517 clear_cmdline = TRUE;
518 if (msg_scrolled > Rows - 5) /* clearing is faster */
519 type = CLEAR;
520 else if (type != CLEAR)
521 {
522 check_for_delay(FALSE);
523 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
524 type = CLEAR;
525 FOR_ALL_WINDOWS(wp)
526 {
527 if (W_WINROW(wp) < msg_scrolled)
528 {
529 if (W_WINROW(wp) + wp->w_height > msg_scrolled
530 && wp->w_redr_type < REDRAW_TOP
531 && wp->w_lines_valid > 0
532 && wp->w_topline == wp->w_lines[0].wl_lnum)
533 {
534 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
535 wp->w_redr_type = REDRAW_TOP;
536 }
537 else
538 {
539 wp->w_redr_type = NOT_VALID;
540#ifdef FEAT_WINDOWS
541 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
542 <= msg_scrolled)
543 wp->w_redr_status = TRUE;
544#endif
545 }
546 }
547 }
548 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000549#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000550 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553 msg_scrolled = 0;
554 need_wait_return = FALSE;
555 }
556
557 /* reset cmdline_row now (may have been changed temporarily) */
558 compute_cmdrow();
559
560 /* Check for changed highlighting */
561 if (need_highlight_changed)
562 highlight_changed();
563
564 if (type == CLEAR) /* first clear screen */
565 {
566 screenclear(); /* will reset clear_cmdline */
567 type = NOT_VALID;
568 }
569
570 if (clear_cmdline) /* going to clear cmdline (done below) */
571 check_for_delay(FALSE);
572
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000573#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200574 /* Force redraw when width of 'number' or 'relativenumber' column
575 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000576 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200577 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
578 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000579 curwin->w_redr_type = NOT_VALID;
580#endif
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 /*
583 * Only start redrawing if there is really something to do.
584 */
585 if (type == INVERTED)
586 update_curswant();
587 if (curwin->w_redr_type < type
588 && !((type == VALID
589 && curwin->w_lines[0].wl_valid
590#ifdef FEAT_DIFF
591 && curwin->w_topfill == curwin->w_old_topfill
592 && curwin->w_botfill == curwin->w_old_botfill
593#endif
594 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
595#ifdef FEAT_VISUAL
596 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000597 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
599 && curwin->w_old_visual_mode == VIsual_mode
600 && (curwin->w_valid & VALID_VIRTCOL)
601 && curwin->w_old_curswant == curwin->w_curswant)
602#endif
603 ))
604 curwin->w_redr_type = type;
605
Bram Moolenaar5a305422006-04-28 22:38:25 +0000606#ifdef FEAT_WINDOWS
607 /* Redraw the tab pages line if needed. */
608 if (redraw_tabline || type >= NOT_VALID)
609 draw_tabline();
610#endif
611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#ifdef FEAT_SYN_HL
613 /*
614 * Correct stored syntax highlighting info for changes in each displayed
615 * buffer. Each buffer must only be done once.
616 */
617 FOR_ALL_WINDOWS(wp)
618 {
619 if (wp->w_buffer->b_mod_set)
620 {
621# ifdef FEAT_WINDOWS
622 win_T *wwp;
623
624 /* Check if we already did this buffer. */
625 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
626 if (wwp->w_buffer == wp->w_buffer)
627 break;
628# endif
629 if (
630# ifdef FEAT_WINDOWS
631 wwp == wp &&
632# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200633 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 syn_stack_apply_changes(wp->w_buffer);
635 }
636 }
637#endif
638
639 /*
640 * Go from top to bottom through the windows, redrawing the ones that need
641 * it.
642 */
643#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
644 did_one = FALSE;
645#endif
646#ifdef FEAT_SEARCH_EXTRA
647 search_hl.rm.regprog = NULL;
648#endif
649 FOR_ALL_WINDOWS(wp)
650 {
651 if (wp->w_redr_type != 0)
652 {
653 cursor_off();
654#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
655 if (!did_one)
656 {
657 did_one = TRUE;
658# ifdef FEAT_SEARCH_EXTRA
659 start_search_hl();
660# endif
661# ifdef FEAT_CLIPBOARD
662 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200663 if (clip_star.available && clip_isautosel_star())
664 clip_update_selection(&clip_star);
665 if (clip_plus.available && clip_isautosel_plus())
666 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667# endif
668#ifdef FEAT_GUI
669 /* Remove the cursor before starting to do anything, because
670 * scrolling may make it difficult to redraw the text under
671 * it. */
672 if (gui.in_use)
673 gui_undraw_cursor();
674#endif
675 }
676#endif
677 win_update(wp);
678 }
679
680#ifdef FEAT_WINDOWS
681 /* redraw status line after the window to minimize cursor movement */
682 if (wp->w_redr_status)
683 {
684 cursor_off();
685 win_redr_status(wp);
686 }
687#endif
688 }
689#if defined(FEAT_SEARCH_EXTRA)
690 end_search_hl();
691#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100692#ifdef FEAT_INS_EXPAND
693 /* May need to redraw the popup menu. */
694 if (pum_visible())
695 pum_redraw();
696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698#ifdef FEAT_WINDOWS
699 /* Reset b_mod_set flags. Going through all windows is probably faster
700 * than going through all buffers (there could be many buffers). */
701 for (wp = firstwin; wp != NULL; wp = wp->w_next)
702 wp->w_buffer->b_mod_set = FALSE;
703#else
704 curbuf->b_mod_set = FALSE;
705#endif
706
707 updating_screen = FALSE;
708#ifdef FEAT_GUI
709 gui_may_resize_shell();
710#endif
711
712 /* Clear or redraw the command line. Done last, because scrolling may
713 * mess up the command line. */
714 if (clear_cmdline || redraw_cmdline)
715 showmode();
716
717 /* May put up an introductory message when not editing a file */
718 if (!did_intro && bufempty()
719 && curbuf->b_fname == NULL
720#ifdef FEAT_WINDOWS
721 && firstwin->w_next == NULL
722#endif
723 && vim_strchr(p_shm, SHM_INTRO) == NULL)
724 intro_message(FALSE);
725 did_intro = TRUE;
726
727#ifdef FEAT_GUI
728 /* Redraw the cursor and update the scrollbars when all screen updating is
729 * done. */
730 if (gui.in_use)
731 {
732 out_flush(); /* required before updating the cursor */
733 if (did_one)
734 gui_update_cursor(FALSE, FALSE);
735 gui_update_scrollbars(FALSE);
736 }
737#endif
738}
739
Bram Moolenaar860cae12010-06-05 23:22:07 +0200740#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200741/*
742 * Return TRUE if the cursor line in window "wp" may be concealed, according
743 * to the 'concealcursor' option.
744 */
745 int
746conceal_cursor_line(wp)
747 win_T *wp;
748{
749 int c;
750
751 if (*wp->w_p_cocu == NUL)
752 return FALSE;
753 if (get_real_state() & VISUAL)
754 c = 'v';
755 else if (State & INSERT)
756 c = 'i';
757 else if (State & NORMAL)
758 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200759 else if (State & CMDLINE)
760 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200761 else
762 return FALSE;
763 return vim_strchr(wp->w_p_cocu, c) != NULL;
764}
765
766/*
767 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
768 */
769 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200770conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200771{
772 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
773 {
774 need_cursor_line_redraw = TRUE;
775 /* Need to recompute cursor column, e.g., when starting Visual mode
776 * without concealing. */
777 curs_columns(TRUE);
778 }
779}
780
Bram Moolenaar860cae12010-06-05 23:22:07 +0200781 void
782update_single_line(wp, lnum)
783 win_T *wp;
784 linenr_T lnum;
785{
786 int row;
787 int j;
788
789 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200790 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200791 {
792# ifdef FEAT_GUI
793 /* Remove the cursor before starting to do anything, because scrolling
794 * may make it difficult to redraw the text under it. */
795 if (gui.in_use)
796 gui_undraw_cursor();
797# endif
798 row = 0;
799 for (j = 0; j < wp->w_lines_valid; ++j)
800 {
801 if (lnum == wp->w_lines[j].wl_lnum)
802 {
803 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200804# ifdef FEAT_SEARCH_EXTRA
805 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200806 start_search_hl();
807 prepare_search_hl(wp, lnum);
808# endif
809 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
810# if defined(FEAT_SEARCH_EXTRA)
811 end_search_hl();
812# endif
813 break;
814 }
815 row += wp->w_lines[j].wl_size;
816 }
817# ifdef FEAT_GUI
818 /* Redraw the cursor */
819 if (gui.in_use)
820 {
821 out_flush(); /* required before updating the cursor */
822 gui_update_cursor(FALSE, FALSE);
823 }
824# endif
825 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200826 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200827}
828#endif
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
831static void update_prepare __ARGS((void));
832static void update_finish __ARGS((void));
833
834/*
835 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000836 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 */
838 static void
839update_prepare()
840{
841 cursor_off();
842 updating_screen = TRUE;
843#ifdef FEAT_GUI
844 /* Remove the cursor before starting to do anything, because scrolling may
845 * make it difficult to redraw the text under it. */
846 if (gui.in_use)
847 gui_undraw_cursor();
848#endif
849#ifdef FEAT_SEARCH_EXTRA
850 start_search_hl();
851#endif
852}
853
854/*
855 * Finish updating one or more windows.
856 */
857 static void
858update_finish()
859{
860 if (redraw_cmdline)
861 showmode();
862
863# ifdef FEAT_SEARCH_EXTRA
864 end_search_hl();
865# endif
866
867 updating_screen = FALSE;
868
869# ifdef FEAT_GUI
870 gui_may_resize_shell();
871
872 /* Redraw the cursor and update the scrollbars when all screen updating is
873 * done. */
874 if (gui.in_use)
875 {
876 out_flush(); /* required before updating the cursor */
877 gui_update_cursor(FALSE, FALSE);
878 gui_update_scrollbars(FALSE);
879 }
880# endif
881}
882#endif
883
884#if defined(FEAT_SIGNS) || defined(PROTO)
885 void
886update_debug_sign(buf, lnum)
887 buf_T *buf;
888 linenr_T lnum;
889{
890 win_T *wp;
891 int doit = FALSE;
892
893# ifdef FEAT_FOLDING
894 win_foldinfo.fi_level = 0;
895# endif
896
897 /* update/delete a specific mark */
898 FOR_ALL_WINDOWS(wp)
899 {
900 if (buf != NULL && lnum > 0)
901 {
902 if (wp->w_buffer == buf && lnum >= wp->w_topline
903 && lnum < wp->w_botline)
904 {
905 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
906 wp->w_redraw_top = lnum;
907 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
908 wp->w_redraw_bot = lnum;
909 redraw_win_later(wp, VALID);
910 }
911 }
912 else
913 redraw_win_later(wp, VALID);
914 if (wp->w_redr_type != 0)
915 doit = TRUE;
916 }
917
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100918 /* Return when there is nothing to do, screen updating is already
919 * happening (recursive call) or still starting up. */
920 if (!doit || updating_screen
921#ifdef FEAT_GUI
922 || gui.starting
923#endif
924 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 return;
926
927 /* update all windows that need updating */
928 update_prepare();
929
930# ifdef FEAT_WINDOWS
931 for (wp = firstwin; wp; wp = wp->w_next)
932 {
933 if (wp->w_redr_type != 0)
934 win_update(wp);
935 if (wp->w_redr_status)
936 win_redr_status(wp);
937 }
938# else
939 if (curwin->w_redr_type != 0)
940 win_update(curwin);
941# endif
942
943 update_finish();
944}
945#endif
946
947
948#if defined(FEAT_GUI) || defined(PROTO)
949/*
950 * Update a single window, its status line and maybe the command line msg.
951 * Used for the GUI scrollbar.
952 */
953 void
954updateWindow(wp)
955 win_T *wp;
956{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000957 /* return if already busy updating */
958 if (updating_screen)
959 return;
960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 update_prepare();
962
963#ifdef FEAT_CLIPBOARD
964 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200965 if (clip_star.available && clip_isautosel_star())
966 clip_update_selection(&clip_star);
967 if (clip_plus.available && clip_isautosel_plus())
968 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000974 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000975 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000976 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (wp->w_redr_status
979# ifdef FEAT_CMDL_INFO
980 || p_ru
981# endif
982# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000983 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984# endif
985 )
986 win_redr_status(wp);
987#endif
988
989 update_finish();
990}
991#endif
992
993/*
994 * Update a single window.
995 *
996 * This may cause the windows below it also to be redrawn (when clearing the
997 * screen or scrolling lines).
998 *
999 * How the window is redrawn depends on wp->w_redr_type. Each type also
1000 * implies the one below it.
1001 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001002 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1004 * INVERTED redraw the changed part of the Visual area
1005 * INVERTED_ALL redraw the whole Visual area
1006 * VALID 1. scroll up/down to adjust for a changed w_topline
1007 * 2. update lines at the top when scrolled down
1008 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001009 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 * b_mod_top and b_mod_bot.
1011 * - if wp->w_redraw_top non-zero, redraw lines between
1012 * wp->w_redraw_top and wp->w_redr_bot.
1013 * - continue redrawing when syntax status is invalid.
1014 * 4. if scrolled up, update lines at the bottom.
1015 * This results in three areas that may need updating:
1016 * top: from first row to top_end (when scrolled down)
1017 * mid: from mid_start to mid_end (update inversion or changed text)
1018 * bot: from bot_start to last row (when scrolled up)
1019 */
1020 static void
1021win_update(wp)
1022 win_T *wp;
1023{
1024 buf_T *buf = wp->w_buffer;
1025 int type;
1026 int top_end = 0; /* Below last row of the top area that needs
1027 updating. 0 when no top area updating. */
1028 int mid_start = 999;/* first row of the mid area that needs
1029 updating. 999 when no mid area updating. */
1030 int mid_end = 0; /* Below last row of the mid area that needs
1031 updating. 0 when no mid area updating. */
1032 int bot_start = 999;/* first row of the bot area that needs
1033 updating. 999 when no bot area updating */
1034#ifdef FEAT_VISUAL
1035 int scrolled_down = FALSE; /* TRUE when scrolled down when
1036 w_topline got smaller a bit */
1037#endif
1038#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001039 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 int top_to_mod = FALSE; /* redraw above mod_top */
1041#endif
1042
1043 int row; /* current window row to display */
1044 linenr_T lnum; /* current buffer lnum to display */
1045 int idx; /* current index in w_lines[] */
1046 int srow; /* starting row of the current line */
1047
1048 int eof = FALSE; /* if TRUE, we hit the end of the file */
1049 int didline = FALSE; /* if TRUE, we finished the last line */
1050 int i;
1051 long j;
1052 static int recursive = FALSE; /* being called recursively */
1053 int old_botline = wp->w_botline;
1054#ifdef FEAT_FOLDING
1055 long fold_count;
1056#endif
1057#ifdef FEAT_SYN_HL
1058 /* remember what happened to the previous line, to know if
1059 * check_visual_highlight() can be used */
1060#define DID_NONE 1 /* didn't update a line */
1061#define DID_LINE 2 /* updated a normal line */
1062#define DID_FOLD 3 /* updated a folded line */
1063 int did_update = DID_NONE;
1064 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1065#endif
1066 linenr_T mod_top = 0;
1067 linenr_T mod_bot = 0;
1068#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1069 int save_got_int;
1070#endif
1071
1072 type = wp->w_redr_type;
1073
1074 if (type == NOT_VALID)
1075 {
1076#ifdef FEAT_WINDOWS
1077 wp->w_redr_status = TRUE;
1078#endif
1079 wp->w_lines_valid = 0;
1080 }
1081
1082 /* Window is zero-height: nothing to draw. */
1083 if (wp->w_height == 0)
1084 {
1085 wp->w_redr_type = 0;
1086 return;
1087 }
1088
1089#ifdef FEAT_VERTSPLIT
1090 /* Window is zero-width: Only need to draw the separator. */
1091 if (wp->w_width == 0)
1092 {
1093 /* draw the vertical separator right of this window */
1094 draw_vsep_win(wp, 0);
1095 wp->w_redr_type = 0;
1096 return;
1097 }
1098#endif
1099
1100#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001101 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102#endif
1103
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001104#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001105 /* Force redraw when width of 'number' or 'relativenumber' column
1106 * changes. */
1107 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001108 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001109 {
1110 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001111 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001112 }
1113 else
1114#endif
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1117 {
1118 /*
1119 * When there are both inserted/deleted lines and specific lines to be
1120 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1121 * everything (only happens when redrawing is off for while).
1122 */
1123 type = NOT_VALID;
1124 }
1125 else
1126 {
1127 /*
1128 * Set mod_top to the first line that needs displaying because of
1129 * changes. Set mod_bot to the first line after the changes.
1130 */
1131 mod_top = wp->w_redraw_top;
1132 if (wp->w_redraw_bot != 0)
1133 mod_bot = wp->w_redraw_bot + 1;
1134 else
1135 mod_bot = 0;
1136 wp->w_redraw_top = 0; /* reset for next time */
1137 wp->w_redraw_bot = 0;
1138 if (buf->b_mod_set)
1139 {
1140 if (mod_top == 0 || mod_top > buf->b_mod_top)
1141 {
1142 mod_top = buf->b_mod_top;
1143#ifdef FEAT_SYN_HL
1144 /* Need to redraw lines above the change that may be included
1145 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001146 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001148 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (mod_top < 1)
1150 mod_top = 1;
1151 }
1152#endif
1153 }
1154 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1155 mod_bot = buf->b_mod_bot;
1156
1157#ifdef FEAT_SEARCH_EXTRA
1158 /* When 'hlsearch' is on and using a multi-line search pattern, a
1159 * change in one line may make the Search highlighting in a
1160 * previous line invalid. Simple solution: redraw all visible
1161 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001162 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001164 if (search_hl.rm.regprog != NULL
1165 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001167 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001168 {
1169 cur = wp->w_match_head;
1170 while (cur != NULL)
1171 {
1172 if (cur->match.regprog != NULL
1173 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001174 {
1175 top_to_mod = TRUE;
1176 break;
1177 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001178 cur = cur->next;
1179 }
1180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#endif
1182 }
1183#ifdef FEAT_FOLDING
1184 if (mod_top != 0 && hasAnyFolding(wp))
1185 {
1186 linenr_T lnumt, lnumb;
1187
1188 /*
1189 * A change in a line can cause lines above it to become folded or
1190 * unfolded. Find the top most buffer line that may be affected.
1191 * If the line was previously folded and displayed, get the first
1192 * line of that fold. If the line is folded now, get the first
1193 * folded line. Use the minimum of these two.
1194 */
1195
1196 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1197 * the line below it. If there is no valid entry, use w_topline.
1198 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1199 * to this line. If there is no valid entry, use MAXLNUM. */
1200 lnumt = wp->w_topline;
1201 lnumb = MAXLNUM;
1202 for (i = 0; i < wp->w_lines_valid; ++i)
1203 if (wp->w_lines[i].wl_valid)
1204 {
1205 if (wp->w_lines[i].wl_lastlnum < mod_top)
1206 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1207 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1208 {
1209 lnumb = wp->w_lines[i].wl_lnum;
1210 /* When there is a fold column it might need updating
1211 * in the next line ("J" just above an open fold). */
1212 if (wp->w_p_fdc > 0)
1213 ++lnumb;
1214 }
1215 }
1216
1217 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1218 if (mod_top > lnumt)
1219 mod_top = lnumt;
1220
1221 /* Now do the same for the bottom line (one above mod_bot). */
1222 --mod_bot;
1223 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1224 ++mod_bot;
1225 if (mod_bot < lnumb)
1226 mod_bot = lnumb;
1227 }
1228#endif
1229
1230 /* When a change starts above w_topline and the end is below
1231 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001232 * If the end of the change is above w_topline: do like no change was
1233 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (mod_top != 0 && mod_top < wp->w_topline)
1235 {
1236 if (mod_bot > wp->w_topline)
1237 mod_top = wp->w_topline;
1238#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001239 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 top_end = 1;
1241#endif
1242 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001243
1244 /* When line numbers are displayed need to redraw all lines below
1245 * inserted/deleted lines. */
1246 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1247 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 }
1249
1250 /*
1251 * When only displaying the lines at the top, set top_end. Used when
1252 * window has scrolled down for msg_scrolled.
1253 */
1254 if (type == REDRAW_TOP)
1255 {
1256 j = 0;
1257 for (i = 0; i < wp->w_lines_valid; ++i)
1258 {
1259 j += wp->w_lines[i].wl_size;
1260 if (j >= wp->w_upd_rows)
1261 {
1262 top_end = j;
1263 break;
1264 }
1265 }
1266 if (top_end == 0)
1267 /* not found (cannot happen?): redraw everything */
1268 type = NOT_VALID;
1269 else
1270 /* top area defined, the rest is VALID */
1271 type = VALID;
1272 }
1273
Bram Moolenaar367329b2007-08-30 11:53:22 +00001274 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001275 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1276 * non-zero and thus not FALSE) will indicate that screenclear() was not
1277 * called. */
1278 if (screen_cleared)
1279 screen_cleared = MAYBE;
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 /*
1282 * If there are no changes on the screen that require a complete redraw,
1283 * handle three cases:
1284 * 1: we are off the top of the screen by a few lines: scroll down
1285 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1286 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1287 * w_lines[] that needs updating.
1288 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001289 if ((type == VALID || type == SOME_VALID
1290 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#ifdef FEAT_DIFF
1292 && !wp->w_botfill && !wp->w_old_botfill
1293#endif
1294 )
1295 {
1296 if (mod_top != 0 && wp->w_topline == mod_top)
1297 {
1298 /*
1299 * w_topline is the first changed line, the scrolling will be done
1300 * further down.
1301 */
1302 }
1303 else if (wp->w_lines[0].wl_valid
1304 && (wp->w_topline < wp->w_lines[0].wl_lnum
1305#ifdef FEAT_DIFF
1306 || (wp->w_topline == wp->w_lines[0].wl_lnum
1307 && wp->w_topfill > wp->w_old_topfill)
1308#endif
1309 ))
1310 {
1311 /*
1312 * New topline is above old topline: May scroll down.
1313 */
1314#ifdef FEAT_FOLDING
1315 if (hasAnyFolding(wp))
1316 {
1317 linenr_T ln;
1318
1319 /* count the number of lines we are off, counting a sequence
1320 * of folded lines as one */
1321 j = 0;
1322 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1323 {
1324 ++j;
1325 if (j >= wp->w_height - 2)
1326 break;
1327 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1328 }
1329 }
1330 else
1331#endif
1332 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1333 if (j < wp->w_height - 2) /* not too far off */
1334 {
1335 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1336#ifdef FEAT_DIFF
1337 /* insert extra lines for previously invisible filler lines */
1338 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1339 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1340 - wp->w_old_topfill;
1341#endif
1342 if (i < wp->w_height - 2) /* less than a screen off */
1343 {
1344 /*
1345 * Try to insert the correct number of lines.
1346 * If not the last window, delete the lines at the bottom.
1347 * win_ins_lines may fail when the terminal can't do it.
1348 */
1349 if (i > 0)
1350 check_for_delay(FALSE);
1351 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1352 {
1353 if (wp->w_lines_valid != 0)
1354 {
1355 /* Need to update rows that are new, stop at the
1356 * first one that scrolled down. */
1357 top_end = i;
1358#ifdef FEAT_VISUAL
1359 scrolled_down = TRUE;
1360#endif
1361
1362 /* Move the entries that were scrolled, disable
1363 * the entries for the lines to be redrawn. */
1364 if ((wp->w_lines_valid += j) > wp->w_height)
1365 wp->w_lines_valid = wp->w_height;
1366 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1367 wp->w_lines[idx] = wp->w_lines[idx - j];
1368 while (idx >= 0)
1369 wp->w_lines[idx--].wl_valid = FALSE;
1370 }
1371 }
1372 else
1373 mid_start = 0; /* redraw all lines */
1374 }
1375 else
1376 mid_start = 0; /* redraw all lines */
1377 }
1378 else
1379 mid_start = 0; /* redraw all lines */
1380 }
1381 else
1382 {
1383 /*
1384 * New topline is at or below old topline: May scroll up.
1385 * When topline didn't change, find first entry in w_lines[] that
1386 * needs updating.
1387 */
1388
1389 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1390 j = -1;
1391 row = 0;
1392 for (i = 0; i < wp->w_lines_valid; i++)
1393 {
1394 if (wp->w_lines[i].wl_valid
1395 && wp->w_lines[i].wl_lnum == wp->w_topline)
1396 {
1397 j = i;
1398 break;
1399 }
1400 row += wp->w_lines[i].wl_size;
1401 }
1402 if (j == -1)
1403 {
1404 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1405 * lines */
1406 mid_start = 0;
1407 }
1408 else
1409 {
1410 /*
1411 * Try to delete the correct number of lines.
1412 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1413 */
1414#ifdef FEAT_DIFF
1415 /* If the topline didn't change, delete old filler lines,
1416 * otherwise delete filler lines of the new topline... */
1417 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1418 row += wp->w_old_topfill;
1419 else
1420 row += diff_check_fill(wp, wp->w_topline);
1421 /* ... but don't delete new filler lines. */
1422 row -= wp->w_topfill;
1423#endif
1424 if (row > 0)
1425 {
1426 check_for_delay(FALSE);
1427 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1428 bot_start = wp->w_height - row;
1429 else
1430 mid_start = 0; /* redraw all lines */
1431 }
1432 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1433 {
1434 /*
1435 * Skip the lines (below the deleted lines) that are still
1436 * valid and don't need redrawing. Copy their info
1437 * upwards, to compensate for the deleted lines. Set
1438 * bot_start to the first row that needs redrawing.
1439 */
1440 bot_start = 0;
1441 idx = 0;
1442 for (;;)
1443 {
1444 wp->w_lines[idx] = wp->w_lines[j];
1445 /* stop at line that didn't fit, unless it is still
1446 * valid (no lines deleted) */
1447 if (row > 0 && bot_start + row
1448 + (int)wp->w_lines[j].wl_size > wp->w_height)
1449 {
1450 wp->w_lines_valid = idx + 1;
1451 break;
1452 }
1453 bot_start += wp->w_lines[idx++].wl_size;
1454
1455 /* stop at the last valid entry in w_lines[].wl_size */
1456 if (++j >= wp->w_lines_valid)
1457 {
1458 wp->w_lines_valid = idx;
1459 break;
1460 }
1461 }
1462#ifdef FEAT_DIFF
1463 /* Correct the first entry for filler lines at the top
1464 * when it won't get updated below. */
1465 if (wp->w_p_diff && bot_start > 0)
1466 wp->w_lines[0].wl_size =
1467 plines_win_nofill(wp, wp->w_topline, TRUE)
1468 + wp->w_topfill;
1469#endif
1470 }
1471 }
1472 }
1473
1474 /* When starting redraw in the first line, redraw all lines. When
1475 * there is only one window it's probably faster to clear the screen
1476 * first. */
1477 if (mid_start == 0)
1478 {
1479 mid_end = wp->w_height;
1480 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001481 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001482 /* Clear the screen when it was not done by win_del_lines() or
1483 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1484 * then. */
1485 if (screen_cleared != TRUE)
1486 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001487#ifdef FEAT_WINDOWS
1488 /* The screen was cleared, redraw the tab pages line. */
1489 if (redraw_tabline)
1490 draw_tabline();
1491#endif
1492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001494
1495 /* When win_del_lines() or win_ins_lines() caused the screen to be
1496 * cleared (only happens for the first window) or when screenclear()
1497 * was called directly above, "must_redraw" will have been set to
1498 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1499 if (screen_cleared == TRUE)
1500 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502 else
1503 {
1504 /* Not VALID or INVERTED: redraw all lines. */
1505 mid_start = 0;
1506 mid_end = wp->w_height;
1507 }
1508
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001509 if (type == SOME_VALID)
1510 {
1511 /* SOME_VALID: redraw all lines. */
1512 mid_start = 0;
1513 mid_end = wp->w_height;
1514 type = NOT_VALID;
1515 }
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517#ifdef FEAT_VISUAL
1518 /* check if we are updating or removing the inverted part */
1519 if ((VIsual_active && buf == curwin->w_buffer)
1520 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1521 {
1522 linenr_T from, to;
1523
1524 if (VIsual_active)
1525 {
1526 if (VIsual_active
1527 && (VIsual_mode != wp->w_old_visual_mode
1528 || type == INVERTED_ALL))
1529 {
1530 /*
1531 * If the type of Visual selection changed, redraw the whole
1532 * selection. Also when the ownership of the X selection is
1533 * gained or lost.
1534 */
1535 if (curwin->w_cursor.lnum < VIsual.lnum)
1536 {
1537 from = curwin->w_cursor.lnum;
1538 to = VIsual.lnum;
1539 }
1540 else
1541 {
1542 from = VIsual.lnum;
1543 to = curwin->w_cursor.lnum;
1544 }
1545 /* redraw more when the cursor moved as well */
1546 if (wp->w_old_cursor_lnum < from)
1547 from = wp->w_old_cursor_lnum;
1548 if (wp->w_old_cursor_lnum > to)
1549 to = wp->w_old_cursor_lnum;
1550 if (wp->w_old_visual_lnum < from)
1551 from = wp->w_old_visual_lnum;
1552 if (wp->w_old_visual_lnum > to)
1553 to = wp->w_old_visual_lnum;
1554 }
1555 else
1556 {
1557 /*
1558 * Find the line numbers that need to be updated: The lines
1559 * between the old cursor position and the current cursor
1560 * position. Also check if the Visual position changed.
1561 */
1562 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1563 {
1564 from = curwin->w_cursor.lnum;
1565 to = wp->w_old_cursor_lnum;
1566 }
1567 else
1568 {
1569 from = wp->w_old_cursor_lnum;
1570 to = curwin->w_cursor.lnum;
1571 if (from == 0) /* Visual mode just started */
1572 from = to;
1573 }
1574
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001575 if (VIsual.lnum != wp->w_old_visual_lnum
1576 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
1578 if (wp->w_old_visual_lnum < from
1579 && wp->w_old_visual_lnum != 0)
1580 from = wp->w_old_visual_lnum;
1581 if (wp->w_old_visual_lnum > to)
1582 to = wp->w_old_visual_lnum;
1583 if (VIsual.lnum < from)
1584 from = VIsual.lnum;
1585 if (VIsual.lnum > to)
1586 to = VIsual.lnum;
1587 }
1588 }
1589
1590 /*
1591 * If in block mode and changed column or curwin->w_curswant:
1592 * update all lines.
1593 * First compute the actual start and end column.
1594 */
1595 if (VIsual_mode == Ctrl_V)
1596 {
1597 colnr_T fromc, toc;
1598
1599 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1600 ++toc;
1601 if (curwin->w_curswant == MAXCOL)
1602 toc = MAXCOL;
1603
1604 if (fromc != wp->w_old_cursor_fcol
1605 || toc != wp->w_old_cursor_lcol)
1606 {
1607 if (from > VIsual.lnum)
1608 from = VIsual.lnum;
1609 if (to < VIsual.lnum)
1610 to = VIsual.lnum;
1611 }
1612 wp->w_old_cursor_fcol = fromc;
1613 wp->w_old_cursor_lcol = toc;
1614 }
1615 }
1616 else
1617 {
1618 /* Use the line numbers of the old Visual area. */
1619 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1620 {
1621 from = wp->w_old_cursor_lnum;
1622 to = wp->w_old_visual_lnum;
1623 }
1624 else
1625 {
1626 from = wp->w_old_visual_lnum;
1627 to = wp->w_old_cursor_lnum;
1628 }
1629 }
1630
1631 /*
1632 * There is no need to update lines above the top of the window.
1633 */
1634 if (from < wp->w_topline)
1635 from = wp->w_topline;
1636
1637 /*
1638 * If we know the value of w_botline, use it to restrict the update to
1639 * the lines that are visible in the window.
1640 */
1641 if (wp->w_valid & VALID_BOTLINE)
1642 {
1643 if (from >= wp->w_botline)
1644 from = wp->w_botline - 1;
1645 if (to >= wp->w_botline)
1646 to = wp->w_botline - 1;
1647 }
1648
1649 /*
1650 * Find the minimal part to be updated.
1651 * Watch out for scrolling that made entries in w_lines[] invalid.
1652 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1653 * top_end; need to redraw from top_end to the "to" line.
1654 * A middle mouse click with a Visual selection may change the text
1655 * above the Visual area and reset wl_valid, do count these for
1656 * mid_end (in srow).
1657 */
1658 if (mid_start > 0)
1659 {
1660 lnum = wp->w_topline;
1661 idx = 0;
1662 srow = 0;
1663 if (scrolled_down)
1664 mid_start = top_end;
1665 else
1666 mid_start = 0;
1667 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1668 {
1669 if (wp->w_lines[idx].wl_valid)
1670 mid_start += wp->w_lines[idx].wl_size;
1671 else if (!scrolled_down)
1672 srow += wp->w_lines[idx].wl_size;
1673 ++idx;
1674# ifdef FEAT_FOLDING
1675 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1676 lnum = wp->w_lines[idx].wl_lnum;
1677 else
1678# endif
1679 ++lnum;
1680 }
1681 srow += mid_start;
1682 mid_end = wp->w_height;
1683 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1684 {
1685 if (wp->w_lines[idx].wl_valid
1686 && wp->w_lines[idx].wl_lnum >= to + 1)
1687 {
1688 /* Only update until first row of this line */
1689 mid_end = srow;
1690 break;
1691 }
1692 srow += wp->w_lines[idx].wl_size;
1693 }
1694 }
1695 }
1696
1697 if (VIsual_active && buf == curwin->w_buffer)
1698 {
1699 wp->w_old_visual_mode = VIsual_mode;
1700 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1701 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001702 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 wp->w_old_curswant = curwin->w_curswant;
1704 }
1705 else
1706 {
1707 wp->w_old_visual_mode = 0;
1708 wp->w_old_cursor_lnum = 0;
1709 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001710 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 }
1712#endif /* FEAT_VISUAL */
1713
1714#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1715 /* reset got_int, otherwise regexp won't work */
1716 save_got_int = got_int;
1717 got_int = 0;
1718#endif
1719#ifdef FEAT_FOLDING
1720 win_foldinfo.fi_level = 0;
1721#endif
1722
1723 /*
1724 * Update all the window rows.
1725 */
1726 idx = 0; /* first entry in w_lines[].wl_size */
1727 row = 0;
1728 srow = 0;
1729 lnum = wp->w_topline; /* first line shown in window */
1730 for (;;)
1731 {
1732 /* stop updating when reached the end of the window (check for _past_
1733 * the end of the window is at the end of the loop) */
1734 if (row == wp->w_height)
1735 {
1736 didline = TRUE;
1737 break;
1738 }
1739
1740 /* stop updating when hit the end of the file */
1741 if (lnum > buf->b_ml.ml_line_count)
1742 {
1743 eof = TRUE;
1744 break;
1745 }
1746
1747 /* Remember the starting row of the line that is going to be dealt
1748 * with. It is used further down when the line doesn't fit. */
1749 srow = row;
1750
1751 /*
1752 * Update a line when it is in an area that needs updating, when it
1753 * has changes or w_lines[idx] is invalid.
1754 * bot_start may be halfway a wrapped line after using
1755 * win_del_lines(), check if the current line includes it.
1756 * When syntax folding is being used, the saved syntax states will
1757 * already have been updated, we can't see where the syntax state is
1758 * the same again, just update until the end of the window.
1759 */
1760 if (row < top_end
1761 || (row >= mid_start && row < mid_end)
1762#ifdef FEAT_SEARCH_EXTRA
1763 || top_to_mod
1764#endif
1765 || idx >= wp->w_lines_valid
1766 || (row + wp->w_lines[idx].wl_size > bot_start)
1767 || (mod_top != 0
1768 && (lnum == mod_top
1769 || (lnum >= mod_top
1770 && (lnum < mod_bot
1771#ifdef FEAT_SYN_HL
1772 || did_update == DID_FOLD
1773 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001774 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 && (
1776# ifdef FEAT_FOLDING
1777 (foldmethodIsSyntax(wp)
1778 && hasAnyFolding(wp)) ||
1779# endif
1780 syntax_check_changed(lnum)))
1781#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001782#ifdef FEAT_SEARCH_EXTRA
1783 /* match in fixed position might need redraw */
1784 || wp->w_match_head != NULL
1785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 )))))
1787 {
1788#ifdef FEAT_SEARCH_EXTRA
1789 if (lnum == mod_top)
1790 top_to_mod = FALSE;
1791#endif
1792
1793 /*
1794 * When at start of changed lines: May scroll following lines
1795 * up or down to minimize redrawing.
1796 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001797 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 */
1799 if (lnum == mod_top
1800 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001801 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803 int old_rows = 0;
1804 int new_rows = 0;
1805 int xtra_rows;
1806 linenr_T l;
1807
1808 /* Count the old number of window rows, using w_lines[], which
1809 * should still contain the sizes for the lines as they are
1810 * currently displayed. */
1811 for (i = idx; i < wp->w_lines_valid; ++i)
1812 {
1813 /* Only valid lines have a meaningful wl_lnum. Invalid
1814 * lines are part of the changed area. */
1815 if (wp->w_lines[i].wl_valid
1816 && wp->w_lines[i].wl_lnum == mod_bot)
1817 break;
1818 old_rows += wp->w_lines[i].wl_size;
1819#ifdef FEAT_FOLDING
1820 if (wp->w_lines[i].wl_valid
1821 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1822 {
1823 /* Must have found the last valid entry above mod_bot.
1824 * Add following invalid entries. */
1825 ++i;
1826 while (i < wp->w_lines_valid
1827 && !wp->w_lines[i].wl_valid)
1828 old_rows += wp->w_lines[i++].wl_size;
1829 break;
1830 }
1831#endif
1832 }
1833
1834 if (i >= wp->w_lines_valid)
1835 {
1836 /* We can't find a valid line below the changed lines,
1837 * need to redraw until the end of the window.
1838 * Inserting/deleting lines has no use. */
1839 bot_start = 0;
1840 }
1841 else
1842 {
1843 /* Able to count old number of rows: Count new window
1844 * rows, and may insert/delete lines */
1845 j = idx;
1846 for (l = lnum; l < mod_bot; ++l)
1847 {
1848#ifdef FEAT_FOLDING
1849 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1850 ++new_rows;
1851 else
1852#endif
1853#ifdef FEAT_DIFF
1854 if (l == wp->w_topline)
1855 new_rows += plines_win_nofill(wp, l, TRUE)
1856 + wp->w_topfill;
1857 else
1858#endif
1859 new_rows += plines_win(wp, l, TRUE);
1860 ++j;
1861 if (new_rows > wp->w_height - row - 2)
1862 {
1863 /* it's getting too much, must redraw the rest */
1864 new_rows = 9999;
1865 break;
1866 }
1867 }
1868 xtra_rows = new_rows - old_rows;
1869 if (xtra_rows < 0)
1870 {
1871 /* May scroll text up. If there is not enough
1872 * remaining text or scrolling fails, must redraw the
1873 * rest. If scrolling works, must redraw the text
1874 * below the scrolled text. */
1875 if (row - xtra_rows >= wp->w_height - 2)
1876 mod_bot = MAXLNUM;
1877 else
1878 {
1879 check_for_delay(FALSE);
1880 if (win_del_lines(wp, row,
1881 -xtra_rows, FALSE, FALSE) == FAIL)
1882 mod_bot = MAXLNUM;
1883 else
1884 bot_start = wp->w_height + xtra_rows;
1885 }
1886 }
1887 else if (xtra_rows > 0)
1888 {
1889 /* May scroll text down. If there is not enough
1890 * remaining text of scrolling fails, must redraw the
1891 * rest. */
1892 if (row + xtra_rows >= wp->w_height - 2)
1893 mod_bot = MAXLNUM;
1894 else
1895 {
1896 check_for_delay(FALSE);
1897 if (win_ins_lines(wp, row + old_rows,
1898 xtra_rows, FALSE, FALSE) == FAIL)
1899 mod_bot = MAXLNUM;
1900 else if (top_end > row + old_rows)
1901 /* Scrolled the part at the top that requires
1902 * updating down. */
1903 top_end += xtra_rows;
1904 }
1905 }
1906
1907 /* When not updating the rest, may need to move w_lines[]
1908 * entries. */
1909 if (mod_bot != MAXLNUM && i != j)
1910 {
1911 if (j < i)
1912 {
1913 int x = row + new_rows;
1914
1915 /* move entries in w_lines[] upwards */
1916 for (;;)
1917 {
1918 /* stop at last valid entry in w_lines[] */
1919 if (i >= wp->w_lines_valid)
1920 {
1921 wp->w_lines_valid = j;
1922 break;
1923 }
1924 wp->w_lines[j] = wp->w_lines[i];
1925 /* stop at a line that won't fit */
1926 if (x + (int)wp->w_lines[j].wl_size
1927 > wp->w_height)
1928 {
1929 wp->w_lines_valid = j + 1;
1930 break;
1931 }
1932 x += wp->w_lines[j++].wl_size;
1933 ++i;
1934 }
1935 if (bot_start > x)
1936 bot_start = x;
1937 }
1938 else /* j > i */
1939 {
1940 /* move entries in w_lines[] downwards */
1941 j -= i;
1942 wp->w_lines_valid += j;
1943 if (wp->w_lines_valid > wp->w_height)
1944 wp->w_lines_valid = wp->w_height;
1945 for (i = wp->w_lines_valid; i - j >= idx; --i)
1946 wp->w_lines[i] = wp->w_lines[i - j];
1947
1948 /* The w_lines[] entries for inserted lines are
1949 * now invalid, but wl_size may be used above.
1950 * Reset to zero. */
1951 while (i >= idx)
1952 {
1953 wp->w_lines[i].wl_size = 0;
1954 wp->w_lines[i--].wl_valid = FALSE;
1955 }
1956 }
1957 }
1958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960
1961#ifdef FEAT_FOLDING
1962 /*
1963 * When lines are folded, display one line for all of them.
1964 * Otherwise, display normally (can be several display lines when
1965 * 'wrap' is on).
1966 */
1967 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1968 if (fold_count != 0)
1969 {
1970 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1971 ++row;
1972 --fold_count;
1973 wp->w_lines[idx].wl_folded = TRUE;
1974 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1975# ifdef FEAT_SYN_HL
1976 did_update = DID_FOLD;
1977# endif
1978 }
1979 else
1980#endif
1981 if (idx < wp->w_lines_valid
1982 && wp->w_lines[idx].wl_valid
1983 && wp->w_lines[idx].wl_lnum == lnum
1984 && lnum > wp->w_topline
1985 && !(dy_flags & DY_LASTLINE)
1986 && srow + wp->w_lines[idx].wl_size > wp->w_height
1987#ifdef FEAT_DIFF
1988 && diff_check_fill(wp, lnum) == 0
1989#endif
1990 )
1991 {
1992 /* This line is not going to fit. Don't draw anything here,
1993 * will draw "@ " lines below. */
1994 row = wp->w_height + 1;
1995 }
1996 else
1997 {
1998#ifdef FEAT_SEARCH_EXTRA
1999 prepare_search_hl(wp, lnum);
2000#endif
2001#ifdef FEAT_SYN_HL
2002 /* Let the syntax stuff know we skipped a few lines. */
2003 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002004 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 syntax_end_parsing(syntax_last_parsed + 1);
2006#endif
2007
2008 /*
2009 * Display one line.
2010 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002011 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013#ifdef FEAT_FOLDING
2014 wp->w_lines[idx].wl_folded = FALSE;
2015 wp->w_lines[idx].wl_lastlnum = lnum;
2016#endif
2017#ifdef FEAT_SYN_HL
2018 did_update = DID_LINE;
2019 syntax_last_parsed = lnum;
2020#endif
2021 }
2022
2023 wp->w_lines[idx].wl_lnum = lnum;
2024 wp->w_lines[idx].wl_valid = TRUE;
2025 if (row > wp->w_height) /* past end of screen */
2026 {
2027 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002028 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2030 ++idx;
2031 break;
2032 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002033 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 wp->w_lines[idx].wl_size = row - srow;
2035 ++idx;
2036#ifdef FEAT_FOLDING
2037 lnum += fold_count + 1;
2038#else
2039 ++lnum;
2040#endif
2041 }
2042 else
2043 {
2044 /* This line does not need updating, advance to the next one */
2045 row += wp->w_lines[idx++].wl_size;
2046 if (row > wp->w_height) /* past end of screen */
2047 break;
2048#ifdef FEAT_FOLDING
2049 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2050#else
2051 ++lnum;
2052#endif
2053#ifdef FEAT_SYN_HL
2054 did_update = DID_NONE;
2055#endif
2056 }
2057
2058 if (lnum > buf->b_ml.ml_line_count)
2059 {
2060 eof = TRUE;
2061 break;
2062 }
2063 }
2064 /*
2065 * End of loop over all window lines.
2066 */
2067
2068
2069 if (idx > wp->w_lines_valid)
2070 wp->w_lines_valid = idx;
2071
2072#ifdef FEAT_SYN_HL
2073 /*
2074 * Let the syntax stuff know we stop parsing here.
2075 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002076 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 syntax_end_parsing(syntax_last_parsed + 1);
2078#endif
2079
2080 /*
2081 * If we didn't hit the end of the file, and we didn't finish the last
2082 * line we were working on, then the line didn't fit.
2083 */
2084 wp->w_empty_rows = 0;
2085#ifdef FEAT_DIFF
2086 wp->w_filler_rows = 0;
2087#endif
2088 if (!eof && !didline)
2089 {
2090 if (lnum == wp->w_topline)
2091 {
2092 /*
2093 * Single line that does not fit!
2094 * Don't overwrite it, it can be edited.
2095 */
2096 wp->w_botline = lnum + 1;
2097 }
2098#ifdef FEAT_DIFF
2099 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2100 {
2101 /* Window ends in filler lines. */
2102 wp->w_botline = lnum;
2103 wp->w_filler_rows = wp->w_height - srow;
2104 }
2105#endif
2106 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2107 {
2108 /*
2109 * Last line isn't finished: Display "@@@" at the end.
2110 */
2111 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2112 W_WINROW(wp) + wp->w_height,
2113 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2114 '@', '@', hl_attr(HLF_AT));
2115 set_empty_rows(wp, srow);
2116 wp->w_botline = lnum;
2117 }
2118 else
2119 {
2120 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2121 wp->w_botline = lnum;
2122 }
2123 }
2124 else
2125 {
2126#ifdef FEAT_VERTSPLIT
2127 draw_vsep_win(wp, row);
2128#endif
2129 if (eof) /* we hit the end of the file */
2130 {
2131 wp->w_botline = buf->b_ml.ml_line_count + 1;
2132#ifdef FEAT_DIFF
2133 j = diff_check_fill(wp, wp->w_botline);
2134 if (j > 0 && !wp->w_botfill)
2135 {
2136 /*
2137 * Display filler lines at the end of the file
2138 */
2139 if (char2cells(fill_diff) > 1)
2140 i = '-';
2141 else
2142 i = fill_diff;
2143 if (row + j > wp->w_height)
2144 j = wp->w_height - row;
2145 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2146 row += j;
2147 }
2148#endif
2149 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002150 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 wp->w_botline = lnum;
2152
2153 /* make sure the rest of the screen is blank */
2154 /* put '~'s on rows that aren't part of the file. */
2155 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2156 }
2157
2158 /* Reset the type of redrawing required, the window has been updated. */
2159 wp->w_redr_type = 0;
2160#ifdef FEAT_DIFF
2161 wp->w_old_topfill = wp->w_topfill;
2162 wp->w_old_botfill = wp->w_botfill;
2163#endif
2164
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002165 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
2167 /*
2168 * There is a trick with w_botline. If we invalidate it on each
2169 * change that might modify it, this will cause a lot of expensive
2170 * calls to plines() in update_topline() each time. Therefore the
2171 * value of w_botline is often approximated, and this value is used to
2172 * compute the value of w_topline. If the value of w_botline was
2173 * wrong, check that the value of w_topline is correct (cursor is on
2174 * the visible part of the text). If it's not, we need to redraw
2175 * again. Mostly this just means scrolling up a few lines, so it
2176 * doesn't look too bad. Only do this for the current window (where
2177 * changes are relevant).
2178 */
2179 wp->w_valid |= VALID_BOTLINE;
2180 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2181 {
2182 recursive = TRUE;
2183 curwin->w_valid &= ~VALID_TOPLINE;
2184 update_topline(); /* may invalidate w_botline again */
2185 if (must_redraw != 0)
2186 {
2187 /* Don't update for changes in buffer again. */
2188 i = curbuf->b_mod_set;
2189 curbuf->b_mod_set = FALSE;
2190 win_update(curwin);
2191 must_redraw = 0;
2192 curbuf->b_mod_set = i;
2193 }
2194 recursive = FALSE;
2195 }
2196 }
2197
2198#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2199 /* restore got_int, unless CTRL-C was hit while redrawing */
2200 if (!got_int)
2201 got_int = save_got_int;
2202#endif
2203}
2204
2205#ifdef FEAT_SIGNS
2206static int draw_signcolumn __ARGS((win_T *wp));
2207
2208/*
2209 * Return TRUE when window "wp" has a column to draw signs in.
2210 */
2211 static int
2212draw_signcolumn(wp)
2213 win_T *wp;
2214{
2215 return (wp->w_buffer->b_signlist != NULL
2216# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002217 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218# endif
2219 );
2220}
2221#endif
2222
2223/*
2224 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2225 * as the filler character.
2226 */
2227 static void
2228win_draw_end(wp, c1, c2, row, endrow, hl)
2229 win_T *wp;
2230 int c1;
2231 int c2;
2232 int row;
2233 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002234 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2237 int n = 0;
2238# define FDC_OFF n
2239#else
2240# define FDC_OFF 0
2241#endif
2242
2243#ifdef FEAT_RIGHTLEFT
2244 if (wp->w_p_rl)
2245 {
2246 /* No check for cmdline window: should never be right-left. */
2247# ifdef FEAT_FOLDING
2248 n = wp->w_p_fdc;
2249
2250 if (n > 0)
2251 {
2252 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002253 if (n > W_WIDTH(wp))
2254 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2256 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2257 ' ', ' ', hl_attr(HLF_FC));
2258 }
2259# endif
2260# ifdef FEAT_SIGNS
2261 if (draw_signcolumn(wp))
2262 {
2263 int nn = n + 2;
2264
2265 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002266 if (nn > W_WIDTH(wp))
2267 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2269 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2270 ' ', ' ', hl_attr(HLF_SC));
2271 n = nn;
2272 }
2273# endif
2274 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2275 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2276 c2, c2, hl_attr(hl));
2277 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2278 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2279 c1, c2, hl_attr(hl));
2280 }
2281 else
2282#endif
2283 {
2284#ifdef FEAT_CMDWIN
2285 if (cmdwin_type != 0 && wp == curwin)
2286 {
2287 /* draw the cmdline character in the leftmost column */
2288 n = 1;
2289 if (n > wp->w_width)
2290 n = wp->w_width;
2291 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2292 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2293 cmdwin_type, ' ', hl_attr(HLF_AT));
2294 }
2295#endif
2296#ifdef FEAT_FOLDING
2297 if (wp->w_p_fdc > 0)
2298 {
2299 int nn = n + wp->w_p_fdc;
2300
2301 /* draw the fold column at the left */
2302 if (nn > W_WIDTH(wp))
2303 nn = W_WIDTH(wp);
2304 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2305 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2306 ' ', ' ', hl_attr(HLF_FC));
2307 n = nn;
2308 }
2309#endif
2310#ifdef FEAT_SIGNS
2311 if (draw_signcolumn(wp))
2312 {
2313 int nn = n + 2;
2314
2315 /* draw the sign column after the fold column */
2316 if (nn > W_WIDTH(wp))
2317 nn = W_WIDTH(wp);
2318 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2319 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2320 ' ', ' ', hl_attr(HLF_SC));
2321 n = nn;
2322 }
2323#endif
2324 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2325 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2326 c1, c2, hl_attr(hl));
2327 }
2328 set_empty_rows(wp, row);
2329}
2330
Bram Moolenaar1a384422010-07-14 19:53:30 +02002331#ifdef FEAT_SYN_HL
2332static int advance_color_col __ARGS((int vcol, int **color_cols));
2333
2334/*
2335 * Advance **color_cols and return TRUE when there are columns to draw.
2336 */
2337 static int
2338advance_color_col(vcol, color_cols)
2339 int vcol;
2340 int **color_cols;
2341{
2342 while (**color_cols >= 0 && vcol > **color_cols)
2343 ++*color_cols;
2344 return (**color_cols >= 0);
2345}
2346#endif
2347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348#ifdef FEAT_FOLDING
2349/*
2350 * Display one folded line.
2351 */
2352 static void
2353fold_line(wp, fold_count, foldinfo, lnum, row)
2354 win_T *wp;
2355 long fold_count;
2356 foldinfo_T *foldinfo;
2357 linenr_T lnum;
2358 int row;
2359{
2360 char_u buf[51];
2361 pos_T *top, *bot;
2362 linenr_T lnume = lnum + fold_count - 1;
2363 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002364 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 int col;
2367 int txtcol;
2368 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002369 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371 /* Build the fold line:
2372 * 1. Add the cmdwin_type for the command-line window
2373 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002374 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 * 4. Compose the text
2376 * 5. Add the text
2377 * 6. set highlighting for the Visual area an other text
2378 */
2379 col = 0;
2380
2381 /*
2382 * 1. Add the cmdwin_type for the command-line window
2383 * Ignores 'rightleft', this window is never right-left.
2384 */
2385#ifdef FEAT_CMDWIN
2386 if (cmdwin_type != 0 && wp == curwin)
2387 {
2388 ScreenLines[off] = cmdwin_type;
2389 ScreenAttrs[off] = hl_attr(HLF_AT);
2390#ifdef FEAT_MBYTE
2391 if (enc_utf8)
2392 ScreenLinesUC[off] = 0;
2393#endif
2394 ++col;
2395 }
2396#endif
2397
2398 /*
2399 * 2. Add the 'foldcolumn'
2400 */
2401 fdc = wp->w_p_fdc;
2402 if (fdc > W_WIDTH(wp) - col)
2403 fdc = W_WIDTH(wp) - col;
2404 if (fdc > 0)
2405 {
2406 fill_foldcolumn(buf, wp, TRUE, lnum);
2407#ifdef FEAT_RIGHTLEFT
2408 if (wp->w_p_rl)
2409 {
2410 int i;
2411
2412 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2413 hl_attr(HLF_FC));
2414 /* reverse the fold column */
2415 for (i = 0; i < fdc; ++i)
2416 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2417 }
2418 else
2419#endif
2420 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2421 col += fdc;
2422 }
2423
2424#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002425# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2426 for (ri = 0; ri < l; ++ri) \
2427 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2428 else \
2429 for (ri = 0; ri < l; ++ri) \
2430 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002432# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2433 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#endif
2435
Bram Moolenaar64486672010-05-16 15:46:46 +02002436 /* Set all attributes of the 'number' or 'relativenumber' column and the
2437 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002438 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440#ifdef FEAT_SIGNS
2441 /* If signs are being displayed, add two spaces. */
2442 if (draw_signcolumn(wp))
2443 {
2444 len = W_WIDTH(wp) - col;
2445 if (len > 0)
2446 {
2447 if (len > 2)
2448 len = 2;
2449# ifdef FEAT_RIGHTLEFT
2450 if (wp->w_p_rl)
2451 /* the line number isn't reversed */
2452 copy_text_attr(off + W_WIDTH(wp) - len - col,
2453 (char_u *)" ", len, hl_attr(HLF_FL));
2454 else
2455# endif
2456 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2457 col += len;
2458 }
2459 }
2460#endif
2461
2462 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002463 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002465 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
2467 len = W_WIDTH(wp) - col;
2468 if (len > 0)
2469 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002470 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002471 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01002472 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002473
2474 if (len > w + 1)
2475 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002476
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002477 if (wp->w_p_nu && !wp->w_p_rnu)
2478 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002479 num = (long)lnum;
2480 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002481 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002482 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002483 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002484 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002485 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002486 /* 'number' + 'relativenumber': cursor line shows absolute
2487 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002488 num = lnum;
2489 fmt = "%-*ld ";
2490 }
2491 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002492
Bram Moolenaar700e7342013-01-30 12:31:36 +01002493 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494#ifdef FEAT_RIGHTLEFT
2495 if (wp->w_p_rl)
2496 /* the line number isn't reversed */
2497 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2498 hl_attr(HLF_FL));
2499 else
2500#endif
2501 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2502 col += len;
2503 }
2504 }
2505
2506 /*
2507 * 4. Compose the folded-line string with 'foldtext', if set.
2508 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002509 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
2511 txtcol = col; /* remember where text starts */
2512
2513 /*
2514 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2515 * Right-left text is put in columns 0 - number-col, normal text is put
2516 * in columns number-col - window-width.
2517 */
2518#ifdef FEAT_MBYTE
2519 if (has_mbyte)
2520 {
2521 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002522 int u8c, u8cc[MAX_MCO];
2523 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 int idx;
2525 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002526 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527# ifdef FEAT_ARABIC
2528 int prev_c = 0; /* previous Arabic character */
2529 int prev_c1 = 0; /* first composing char for prev_c */
2530# endif
2531
2532# ifdef FEAT_RIGHTLEFT
2533 if (wp->w_p_rl)
2534 idx = off;
2535 else
2536# endif
2537 idx = off + col;
2538
2539 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2540 for (p = text; *p != NUL; )
2541 {
2542 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002543 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (col + cells > W_WIDTH(wp)
2545# ifdef FEAT_RIGHTLEFT
2546 - (wp->w_p_rl ? col : 0)
2547# endif
2548 )
2549 break;
2550 ScreenLines[idx] = *p;
2551 if (enc_utf8)
2552 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002553 u8c = utfc_ptr2char(p, u8cc);
2554 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
2556 ScreenLinesUC[idx] = 0;
2557#ifdef FEAT_ARABIC
2558 prev_c = u8c;
2559#endif
2560 }
2561 else
2562 {
2563#ifdef FEAT_ARABIC
2564 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2565 {
2566 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002567 int pc, pc1, nc;
2568 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 int firstbyte = *p;
2570
2571 /* The idea of what is the previous and next
2572 * character depends on 'rightleft'. */
2573 if (wp->w_p_rl)
2574 {
2575 pc = prev_c;
2576 pc1 = prev_c1;
2577 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002578 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580 else
2581 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002582 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002584 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586 prev_c = u8c;
2587
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002588 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 pc, pc1, nc);
2590 ScreenLines[idx] = firstbyte;
2591 }
2592 else
2593 prev_c = u8c;
2594#endif
2595 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002596#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (u8c >= 0x10000)
2598 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2599 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002602 for (i = 0; i < Screen_mco; ++i)
2603 {
2604 ScreenLinesC[i][idx] = u8cc[i];
2605 if (u8cc[i] == 0)
2606 break;
2607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609 if (cells > 1)
2610 ScreenLines[idx + 1] = 0;
2611 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002612 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2613 /* double-byte single width character */
2614 ScreenLines2[idx] = p[1];
2615 else if (cells > 1)
2616 /* double-width character */
2617 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 col += cells;
2619 idx += cells;
2620 p += c_len;
2621 }
2622 }
2623 else
2624#endif
2625 {
2626 len = (int)STRLEN(text);
2627 if (len > W_WIDTH(wp) - col)
2628 len = W_WIDTH(wp) - col;
2629 if (len > 0)
2630 {
2631#ifdef FEAT_RIGHTLEFT
2632 if (wp->w_p_rl)
2633 STRNCPY(current_ScreenLine, text, len);
2634 else
2635#endif
2636 STRNCPY(current_ScreenLine + col, text, len);
2637 col += len;
2638 }
2639 }
2640
2641 /* Fill the rest of the line with the fold filler */
2642#ifdef FEAT_RIGHTLEFT
2643 if (wp->w_p_rl)
2644 col -= txtcol;
2645#endif
2646 while (col < W_WIDTH(wp)
2647#ifdef FEAT_RIGHTLEFT
2648 - (wp->w_p_rl ? txtcol : 0)
2649#endif
2650 )
2651 {
2652#ifdef FEAT_MBYTE
2653 if (enc_utf8)
2654 {
2655 if (fill_fold >= 0x80)
2656 {
2657 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002658 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660 else
2661 ScreenLinesUC[off + col] = 0;
2662 }
2663#endif
2664 ScreenLines[off + col++] = fill_fold;
2665 }
2666
2667 if (text != buf)
2668 vim_free(text);
2669
2670 /*
2671 * 6. set highlighting for the Visual area an other text.
2672 * If all folded lines are in the Visual area, highlight the line.
2673 */
2674#ifdef FEAT_VISUAL
2675 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2676 {
2677 if (ltoreq(curwin->w_cursor, VIsual))
2678 {
2679 /* Visual is after curwin->w_cursor */
2680 top = &curwin->w_cursor;
2681 bot = &VIsual;
2682 }
2683 else
2684 {
2685 /* Visual is before curwin->w_cursor */
2686 top = &VIsual;
2687 bot = &curwin->w_cursor;
2688 }
2689 if (lnum >= top->lnum
2690 && lnume <= bot->lnum
2691 && (VIsual_mode != 'v'
2692 || ((lnum > top->lnum
2693 || (lnum == top->lnum
2694 && top->col == 0))
2695 && (lnume < bot->lnum
2696 || (lnume == bot->lnum
2697 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002698 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
2700 if (VIsual_mode == Ctrl_V)
2701 {
2702 /* Visual block mode: highlight the chars part of the block */
2703 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2704 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002705 if (wp->w_old_cursor_lcol != MAXCOL
2706 && wp->w_old_cursor_lcol + txtcol
2707 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 len = wp->w_old_cursor_lcol;
2709 else
2710 len = W_WIDTH(wp) - txtcol;
2711 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002712 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 }
2715 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002718 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721 }
2722#endif
2723
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002724#ifdef FEAT_SYN_HL
2725 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002726 if (wp->w_p_cuc)
2727 {
2728 txtcol += wp->w_virtcol;
2729 if (wp->w_p_wrap)
2730 txtcol -= wp->w_skipcol;
2731 else
2732 txtcol -= wp->w_leftcol;
2733 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2734 ScreenAttrs[off + txtcol] = hl_combine_attr(
2735 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2736 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2740 (int)W_WIDTH(wp), FALSE);
2741
2742 /*
2743 * Update w_cline_height and w_cline_folded if the cursor line was
2744 * updated (saves a call to plines() later).
2745 */
2746 if (wp == curwin
2747 && lnum <= curwin->w_cursor.lnum
2748 && lnume >= curwin->w_cursor.lnum)
2749 {
2750 curwin->w_cline_row = row;
2751 curwin->w_cline_height = 1;
2752 curwin->w_cline_folded = TRUE;
2753 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2754 }
2755}
2756
2757/*
2758 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2759 */
2760 static void
2761copy_text_attr(off, buf, len, attr)
2762 int off;
2763 char_u *buf;
2764 int len;
2765 int attr;
2766{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002767 int i;
2768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 mch_memmove(ScreenLines + off, buf, (size_t)len);
2770# ifdef FEAT_MBYTE
2771 if (enc_utf8)
2772 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2773# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002774 for (i = 0; i < len; ++i)
2775 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776}
2777
2778/*
2779 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002780 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 */
2782 static void
2783fill_foldcolumn(p, wp, closed, lnum)
2784 char_u *p;
2785 win_T *wp;
2786 int closed; /* TRUE of FALSE */
2787 linenr_T lnum; /* current line number */
2788{
2789 int i = 0;
2790 int level;
2791 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002792 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 /* Init to all spaces. */
2795 copy_spaces(p, (size_t)wp->w_p_fdc);
2796
2797 level = win_foldinfo.fi_level;
2798 if (level > 0)
2799 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002800 /* If there is only one column put more info in it. */
2801 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 /* If the column is too narrow, we start at the lowest level that
2804 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002805 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (first_level < 1)
2807 first_level = 1;
2808
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002809 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 {
2811 if (win_foldinfo.fi_lnum == lnum
2812 && first_level + i >= win_foldinfo.fi_low_level)
2813 p[i] = '-';
2814 else if (first_level == 1)
2815 p[i] = '|';
2816 else if (first_level + i <= 9)
2817 p[i] = '0' + first_level + i;
2818 else
2819 p[i] = '>';
2820 if (first_level + i == level)
2821 break;
2822 }
2823 }
2824 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002825 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827#endif /* FEAT_FOLDING */
2828
2829/*
2830 * Display line "lnum" of window 'wp' on the screen.
2831 * Start at row "startrow", stop when "endrow" is reached.
2832 * wp->w_virtcol needs to be valid.
2833 *
2834 * Return the number of last row the line occupies.
2835 */
2836 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002837win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 win_T *wp;
2839 linenr_T lnum;
2840 int startrow;
2841 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002842 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844 int col; /* visual column on screen */
2845 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2846 int c = 0; /* init for GCC */
2847 long vcol = 0; /* virtual column (for tabs) */
2848 long vcol_prev = -1; /* "vcol" of previous character */
2849 char_u *line; /* current line */
2850 char_u *ptr; /* current position in "line" */
2851 int row; /* row in the window, excl w_winrow */
2852 int screen_row; /* row on the screen, incl w_winrow */
2853
2854 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2855 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002856 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 int c_extra = NUL; /* extra chars, all the same */
2858 int extra_attr = 0; /* attributes when n_extra != 0 */
2859 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2860 displaying lcs_eol at end-of-line */
2861 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2862 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2863
2864 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2865 int saved_n_extra = 0;
2866 char_u *saved_p_extra = NULL;
2867 int saved_c_extra = 0;
2868 int saved_char_attr = 0;
2869
2870 int n_attr = 0; /* chars with special attr */
2871 int saved_attr2 = 0; /* char_attr saved for n_attr */
2872 int n_attr3 = 0; /* chars with overruling special attr */
2873 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2874
2875 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2876
2877 int fromcol, tocol; /* start/end of inverting */
2878 int fromcol_prev = -2; /* start of inverting after cursor */
2879 int noinvcur = FALSE; /* don't invert the cursor */
2880#ifdef FEAT_VISUAL
2881 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002882 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883#endif
2884 pos_T pos;
2885 long v;
2886
2887 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002888 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2890 in this line */
2891 int attr = 0; /* attributes for area highlighting */
2892 int area_attr = 0; /* attributes desired by highlighting */
2893 int search_attr = 0; /* attributes desired by 'hlsearch' */
2894#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002895 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 int syntax_attr = 0; /* attributes desired by syntax */
2897 int has_syntax = FALSE; /* this buffer has syntax highl. */
2898 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002899 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002900 int draw_color_col = FALSE; /* highlight colorcolumn */
2901 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002902#endif
2903#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002904 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002905# define SPWORDLEN 150
2906 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002907 int nextlinecol = 0; /* column where nextline[] starts */
2908 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002909 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002910 int spell_attr = 0; /* attributes desired by spelling */
2911 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002912 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2913 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002914 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002915 static int cap_col = -1; /* column to check for Cap word */
2916 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002917 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918#endif
2919 int extra_check; /* has syntax or linebreak */
2920#ifdef FEAT_MBYTE
2921 int multi_attr = 0; /* attributes desired by multibyte */
2922 int mb_l = 1; /* multi-byte byte length */
2923 int mb_c = 0; /* decoded multi-byte character */
2924 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002925 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#endif
2927#ifdef FEAT_DIFF
2928 int filler_lines; /* nr of filler lines to be drawn */
2929 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002930 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 int change_start = MAXCOL; /* first col of changed area */
2932 int change_end = -1; /* last col of changed area */
2933#endif
2934 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2935#ifdef FEAT_LINEBREAK
2936 int need_showbreak = FALSE;
2937#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002938#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2939 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002941 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942#endif
2943#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002944 matchitem_T *cur; /* points to the match list */
2945 match_T *shl; /* points to search_hl or a match */
2946 int shl_flag; /* flag to indicate whether search_hl
2947 has been processed or not */
2948 int prevcol_hl_flag; /* flag to indicate whether prevcol
2949 equals startcol of search_hl or one
2950 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951#endif
2952#ifdef FEAT_ARABIC
2953 int prev_c = 0; /* previous Arabic character */
2954 int prev_c1 = 0; /* first composing char for prev_c */
2955#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002956#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002957 int did_line_attr = 0;
2958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 /* draw_state: items that are drawn in sequence: */
2961#define WL_START 0 /* nothing done yet */
2962#ifdef FEAT_CMDWIN
2963# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2964#else
2965# define WL_CMDLINE WL_START
2966#endif
2967#ifdef FEAT_FOLDING
2968# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2969#else
2970# define WL_FOLD WL_CMDLINE
2971#endif
2972#ifdef FEAT_SIGNS
2973# define WL_SIGN WL_FOLD + 1 /* column for signs */
2974#else
2975# define WL_SIGN WL_FOLD /* column for signs */
2976#endif
2977#define WL_NR WL_SIGN + 1 /* line number */
2978#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2979# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2980#else
2981# define WL_SBR WL_NR
2982#endif
2983#define WL_LINE WL_SBR + 1 /* text in the line */
2984 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002985#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 int feedback_col = 0;
2987 int feedback_old_attr = -1;
2988#endif
2989
Bram Moolenaar860cae12010-06-05 23:22:07 +02002990#ifdef FEAT_CONCEAL
2991 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002992 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002993 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002994 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002995 int is_concealing = FALSE;
2996 int boguscols = 0; /* nonexistent columns added to force
2997 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002998 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002999 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003000# define VCOL_HLC (vcol - vcol_off)
3001#else
3002# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 if (startrow > endrow) /* past the end already! */
3006 return startrow;
3007
3008 row = startrow;
3009 screen_row = row + W_WINROW(wp);
3010
3011 /*
3012 * To speed up the loop below, set extra_check when there is linebreak,
3013 * trailing white space and/or syntax processing to be done.
3014 */
3015#ifdef FEAT_LINEBREAK
3016 extra_check = wp->w_p_lbr;
3017#else
3018 extra_check = 0;
3019#endif
3020#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003021 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 {
3023 /* Prepare for syntax highlighting in this line. When there is an
3024 * error, stop syntax highlighting. */
3025 save_did_emsg = did_emsg;
3026 did_emsg = FALSE;
3027 syntax_start(wp, lnum);
3028 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003029 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 else
3031 {
3032 did_emsg = save_did_emsg;
3033 has_syntax = TRUE;
3034 extra_check = TRUE;
3035 }
3036 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003037
3038 /* Check for columns to display for 'colorcolumn'. */
3039 color_cols = wp->w_p_cc_cols;
3040 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003041 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003042#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003043
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003044#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003045 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003046 && *wp->w_s->b_p_spl != NUL
3047 && wp->w_s->b_langp.ga_len > 0
3048 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003049 {
3050 /* Prepare for spell checking. */
3051 has_spell = TRUE;
3052 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003053
3054 /* Get the start of the next line, so that words that wrap to the next
3055 * line are found too: "et<line-break>al.".
3056 * Trick: skip a few chars for C/shell/Vim comments */
3057 nextline[SPWORDLEN] = NUL;
3058 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3059 {
3060 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3061 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3062 }
3063
3064 /* When a word wrapped from the previous line the start of the current
3065 * line is valid. */
3066 if (lnum == checked_lnum)
3067 cur_checked_col = checked_col;
3068 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003069
3070 /* When there was a sentence end in the previous line may require a
3071 * word starting with capital in this line. In line 1 always check
3072 * the first word. */
3073 if (lnum != capcol_lnum)
3074 cap_col = -1;
3075 if (lnum == 1)
3076 cap_col = 0;
3077 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079#endif
3080
3081 /*
3082 * handle visual active in this window
3083 */
3084 fromcol = -10;
3085 tocol = MAXCOL;
3086#ifdef FEAT_VISUAL
3087 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3088 {
3089 /* Visual is after curwin->w_cursor */
3090 if (ltoreq(curwin->w_cursor, VIsual))
3091 {
3092 top = &curwin->w_cursor;
3093 bot = &VIsual;
3094 }
3095 else /* Visual is before curwin->w_cursor */
3096 {
3097 top = &VIsual;
3098 bot = &curwin->w_cursor;
3099 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003100 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 if (VIsual_mode == Ctrl_V) /* block mode */
3102 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003103 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
3105 fromcol = wp->w_old_cursor_fcol;
3106 tocol = wp->w_old_cursor_lcol;
3107 }
3108 }
3109 else /* non-block mode */
3110 {
3111 if (lnum > top->lnum && lnum <= bot->lnum)
3112 fromcol = 0;
3113 else if (lnum == top->lnum)
3114 {
3115 if (VIsual_mode == 'V') /* linewise */
3116 fromcol = 0;
3117 else
3118 {
3119 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3120 if (gchar_pos(top) == NUL)
3121 tocol = fromcol + 1;
3122 }
3123 }
3124 if (VIsual_mode != 'V' && lnum == bot->lnum)
3125 {
3126 if (*p_sel == 'e' && bot->col == 0
3127#ifdef FEAT_VIRTUALEDIT
3128 && bot->coladd == 0
3129#endif
3130 )
3131 {
3132 fromcol = -10;
3133 tocol = MAXCOL;
3134 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003135 else if (bot->col == MAXCOL)
3136 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 else
3138 {
3139 pos = *bot;
3140 if (*p_sel == 'e')
3141 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3142 else
3143 {
3144 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3145 ++tocol;
3146 }
3147 }
3148 }
3149 }
3150
3151#ifndef MSDOS
3152 /* Check if the character under the cursor should not be inverted */
3153 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3154# ifdef FEAT_GUI
3155 && !gui.in_use
3156# endif
3157 )
3158 noinvcur = TRUE;
3159#endif
3160
3161 /* if inverting in this line set area_highlighting */
3162 if (fromcol >= 0)
3163 {
3164 area_highlighting = TRUE;
3165 attr = hl_attr(HLF_V);
3166#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003167 if ((clip_star.available && !clip_star.owned
3168 && clip_isautosel_star())
3169 || (clip_plus.available && !clip_plus.owned
3170 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 attr = hl_attr(HLF_VNC);
3172#endif
3173 }
3174 }
3175
3176 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003177 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 */
3179 else
3180#endif /* FEAT_VISUAL */
3181 if (highlight_match
3182 && wp == curwin
3183 && lnum >= curwin->w_cursor.lnum
3184 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3185 {
3186 if (lnum == curwin->w_cursor.lnum)
3187 getvcol(curwin, &(curwin->w_cursor),
3188 (colnr_T *)&fromcol, NULL, NULL);
3189 else
3190 fromcol = 0;
3191 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3192 {
3193 pos.lnum = lnum;
3194 pos.col = search_match_endcol;
3195 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3196 }
3197 else
3198 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003199 /* do at least one character; happens when past end of line */
3200 if (fromcol == tocol)
3201 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 area_highlighting = TRUE;
3203 attr = hl_attr(HLF_I);
3204 }
3205
3206#ifdef FEAT_DIFF
3207 filler_lines = diff_check(wp, lnum);
3208 if (filler_lines < 0)
3209 {
3210 if (filler_lines == -1)
3211 {
3212 if (diff_find_change(wp, lnum, &change_start, &change_end))
3213 diff_hlf = HLF_ADD; /* added line */
3214 else if (change_start == 0)
3215 diff_hlf = HLF_TXD; /* changed text */
3216 else
3217 diff_hlf = HLF_CHD; /* changed line */
3218 }
3219 else
3220 diff_hlf = HLF_ADD; /* added line */
3221 filler_lines = 0;
3222 area_highlighting = TRUE;
3223 }
3224 if (lnum == wp->w_topline)
3225 filler_lines = wp->w_topfill;
3226 filler_todo = filler_lines;
3227#endif
3228
3229#ifdef LINE_ATTR
3230# ifdef FEAT_SIGNS
3231 /* If this line has a sign with line highlighting set line_attr. */
3232 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3233 if (v != 0)
3234 line_attr = sign_get_attr((int)v, TRUE);
3235# endif
3236# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3237 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003238 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 line_attr = hl_attr(HLF_L);
3240# endif
3241 if (line_attr != 0)
3242 area_highlighting = TRUE;
3243#endif
3244
3245 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3246 ptr = line;
3247
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003248#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003249 if (has_spell)
3250 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003251 /* For checking first word with a capital skip white space. */
3252 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003253 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003254
Bram Moolenaar30abd282005-06-22 22:35:10 +00003255 /* To be able to spell-check over line boundaries copy the end of the
3256 * current line into nextline[]. Above the start of the next line was
3257 * copied to nextline[SPWORDLEN]. */
3258 if (nextline[SPWORDLEN] == NUL)
3259 {
3260 /* No next line or it is empty. */
3261 nextlinecol = MAXCOL;
3262 nextline_idx = 0;
3263 }
3264 else
3265 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003266 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003267 if (v < SPWORDLEN)
3268 {
3269 /* Short line, use it completely and append the start of the
3270 * next line. */
3271 nextlinecol = 0;
3272 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003273 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003274 nextline_idx = v + 1;
3275 }
3276 else
3277 {
3278 /* Long line, use only the last SPWORDLEN bytes. */
3279 nextlinecol = v - SPWORDLEN;
3280 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3281 nextline_idx = SPWORDLEN + 1;
3282 }
3283 }
3284 }
3285#endif
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 /* find start of trailing whitespace */
3288 if (wp->w_p_list && lcs_trail)
3289 {
3290 trailcol = (colnr_T)STRLEN(ptr);
3291 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3292 --trailcol;
3293 trailcol += (colnr_T) (ptr - line);
3294 extra_check = TRUE;
3295 }
3296
3297 /*
3298 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3299 * first character to be displayed.
3300 */
3301 if (wp->w_p_wrap)
3302 v = wp->w_skipcol;
3303 else
3304 v = wp->w_leftcol;
3305 if (v > 0)
3306 {
3307#ifdef FEAT_MBYTE
3308 char_u *prev_ptr = ptr;
3309#endif
3310 while (vcol < v && *ptr != NUL)
3311 {
3312 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3313 vcol += c;
3314#ifdef FEAT_MBYTE
3315 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003317 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
3319
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003320#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3321 /* When:
3322 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003323 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003324 * - 'virtualedit' is set, or
3325 * - the visual mode is active,
3326 * the end of the line may be before the start of the displayed part.
3327 */
3328 if (vcol < v && (
3329# ifdef FEAT_SYN_HL
3330 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003331 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003332# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3333 ||
3334# endif
3335# endif
3336# ifdef FEAT_VIRTUALEDIT
3337 virtual_active()
3338# ifdef FEAT_VISUAL
3339 ||
3340# endif
3341# endif
3342# ifdef FEAT_VISUAL
3343 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3344# endif
3345 ))
3346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#endif
3350
3351 /* Handle a character that's not completely on the screen: Put ptr at
3352 * that character but skip the first few screen characters. */
3353 if (vcol > v)
3354 {
3355 vcol -= c;
3356#ifdef FEAT_MBYTE
3357 ptr = prev_ptr;
3358#else
3359 --ptr;
3360#endif
3361 n_skip = v - vcol;
3362 }
3363
3364 /*
3365 * Adjust for when the inverted text is before the screen,
3366 * and when the start of the inverted text is before the screen.
3367 */
3368 if (tocol <= vcol)
3369 fromcol = 0;
3370 else if (fromcol >= 0 && fromcol < vcol)
3371 fromcol = vcol;
3372
3373#ifdef FEAT_LINEBREAK
3374 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3375 if (wp->w_p_wrap)
3376 need_showbreak = TRUE;
3377#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003378#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003379 /* When spell checking a word we need to figure out the start of the
3380 * word and if it's badly spelled or not. */
3381 if (has_spell)
3382 {
3383 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003384 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003385 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003386
3387 pos = wp->w_cursor;
3388 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003389 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003390 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003391
3392 /* spell_move_to() may call ml_get() and make "line" invalid */
3393 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3394 ptr = line + linecol;
3395
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003396 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003397 {
3398 /* no bad word found at line start, don't check until end of a
3399 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003400 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003401 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003402 }
3403 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003404 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003405 /* bad word found, use attributes until end of word */
3406 word_end = wp->w_cursor.col + len + 1;
3407
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003408 /* Turn index into actual attributes. */
3409 if (spell_hlf != HLF_COUNT)
3410 spell_attr = highlight_attr[spell_hlf];
3411 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003412 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003413
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003414# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003415 /* Need to restart syntax highlighting for this line. */
3416 if (has_syntax)
3417 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003418# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003419 }
3420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
3422
3423 /*
3424 * Correct highlighting for cursor that can't be disabled.
3425 * Avoids having to check this for each character.
3426 */
3427 if (fromcol >= 0)
3428 {
3429 if (noinvcur)
3430 {
3431 if ((colnr_T)fromcol == wp->w_virtcol)
3432 {
3433 /* highlighting starts at cursor, let it start just after the
3434 * cursor */
3435 fromcol_prev = fromcol;
3436 fromcol = -1;
3437 }
3438 else if ((colnr_T)fromcol < wp->w_virtcol)
3439 /* restart highlighting after the cursor */
3440 fromcol_prev = wp->w_virtcol;
3441 }
3442 if (fromcol >= tocol)
3443 fromcol = -1;
3444 }
3445
3446#ifdef FEAT_SEARCH_EXTRA
3447 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003448 * Handle highlighting the last used search pattern and matches.
3449 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003451 cur = wp->w_match_head;
3452 shl_flag = FALSE;
3453 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003455 if (shl_flag == FALSE)
3456 {
3457 shl = &search_hl;
3458 shl_flag = TRUE;
3459 }
3460 else
3461 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003462 shl->startcol = MAXCOL;
3463 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 shl->attr_cur = 0;
3465 if (shl->rm.regprog != NULL)
3466 {
3467 v = (long)(ptr - line);
3468 next_search_hl(wp, shl, lnum, (colnr_T)v);
3469
3470 /* Need to get the line again, a multi-line regexp may have made it
3471 * invalid. */
3472 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3473 ptr = line + v;
3474
3475 if (shl->lnum != 0 && shl->lnum <= lnum)
3476 {
3477 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003478 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003480 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3482 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003483 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003485 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003487 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
3489#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003490 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003491 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 else
3493#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003494 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003496 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 shl->attr_cur = shl->attr;
3499 search_attr = shl->attr;
3500 }
3501 area_highlighting = TRUE;
3502 }
3503 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003504 if (shl != &search_hl && cur != NULL)
3505 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507#endif
3508
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003509#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003510 /* Cursor line highlighting for 'cursorline' in the current window. Not
3511 * when Visual mode is active, because it's not clear what is selected
3512 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003513 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
3514 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003515 {
3516 line_attr = hl_attr(HLF_CUL);
3517 area_highlighting = TRUE;
3518 }
3519#endif
3520
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003521 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 col = 0;
3523#ifdef FEAT_RIGHTLEFT
3524 if (wp->w_p_rl)
3525 {
3526 /* Rightleft window: process the text in the normal direction, but put
3527 * it in current_ScreenLine[] from right to left. Start at the
3528 * rightmost column of the window. */
3529 col = W_WIDTH(wp) - 1;
3530 off += col;
3531 }
3532#endif
3533
3534 /*
3535 * Repeat for the whole displayed line.
3536 */
3537 for (;;)
3538 {
3539 /* Skip this quickly when working on the text. */
3540 if (draw_state != WL_LINE)
3541 {
3542#ifdef FEAT_CMDWIN
3543 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3544 {
3545 draw_state = WL_CMDLINE;
3546 if (cmdwin_type != 0 && wp == curwin)
3547 {
3548 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003550 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 char_attr = hl_attr(HLF_AT);
3552 }
3553 }
3554#endif
3555
3556#ifdef FEAT_FOLDING
3557 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3558 {
3559 draw_state = WL_FOLD;
3560 if (wp->w_p_fdc > 0)
3561 {
3562 /* Draw the 'foldcolumn'. */
3563 fill_foldcolumn(extra, wp, FALSE, lnum);
3564 n_extra = wp->w_p_fdc;
3565 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003566 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 c_extra = NUL;
3568 char_attr = hl_attr(HLF_FC);
3569 }
3570 }
3571#endif
3572
3573#ifdef FEAT_SIGNS
3574 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3575 {
3576 draw_state = WL_SIGN;
3577 /* Show the sign column when there are any signs in this
3578 * buffer or when using Netbeans. */
3579 if (draw_signcolumn(wp)
3580# ifdef FEAT_DIFF
3581 && filler_todo <= 0
3582# endif
3583 )
3584 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003585 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003587 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588# endif
3589
3590 /* Draw two cells with the sign value or blank. */
3591 c_extra = ' ';
3592 char_attr = hl_attr(HLF_SC);
3593 n_extra = 2;
3594
3595 if (row == startrow)
3596 {
3597 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3598 SIGN_TEXT);
3599# ifdef FEAT_SIGN_ICONS
3600 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3601 SIGN_ICON);
3602 if (gui.in_use && icon_sign != 0)
3603 {
3604 /* Use the image in this position. */
3605 c_extra = SIGN_BYTE;
3606# ifdef FEAT_NETBEANS_INTG
3607 if (buf_signcount(wp->w_buffer, lnum) > 1)
3608 c_extra = MULTISIGN_BYTE;
3609# endif
3610 char_attr = icon_sign;
3611 }
3612 else
3613# endif
3614 if (text_sign != 0)
3615 {
3616 p_extra = sign_get_text(text_sign);
3617 if (p_extra != NULL)
3618 {
3619 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003620 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 char_attr = sign_get_attr(text_sign, FALSE);
3623 }
3624 }
3625 }
3626 }
3627#endif
3628
3629 if (draw_state == WL_NR - 1 && n_extra == 0)
3630 {
3631 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003632 /* Display the absolute or relative line number. After the
3633 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3634 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 && (row == startrow
3636#ifdef FEAT_DIFF
3637 + filler_lines
3638#endif
3639 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3640 {
3641 /* Draw the line number (empty space after wrapping). */
3642 if (row == startrow
3643#ifdef FEAT_DIFF
3644 + filler_lines
3645#endif
3646 )
3647 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003648 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003649 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003650
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003651 if (wp->w_p_nu && !wp->w_p_rnu)
3652 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003653 num = (long)lnum;
3654 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003655 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003656 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003657 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003658 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003659 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003660 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003661 num = lnum;
3662 fmt = "%-*ld ";
3663 }
3664 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003665
Bram Moolenaar700e7342013-01-30 12:31:36 +01003666 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003667 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (wp->w_skipcol > 0)
3669 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3670 *p_extra = '-';
3671#ifdef FEAT_RIGHTLEFT
3672 if (wp->w_p_rl) /* reverse line numbers */
3673 rl_mirror(extra);
3674#endif
3675 p_extra = extra;
3676 c_extra = NUL;
3677 }
3678 else
3679 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003680 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003682#ifdef FEAT_SYN_HL
3683 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003684 * the current line differently.
3685 * TODO: Can we use CursorLine instead of CursorLineNr
3686 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003687 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003688 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003689 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692 }
3693
3694#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3695 if (draw_state == WL_SBR - 1 && n_extra == 0)
3696 {
3697 draw_state = WL_SBR;
3698# ifdef FEAT_DIFF
3699 if (filler_todo > 0)
3700 {
3701 /* Draw "deleted" diff line(s). */
3702 if (char2cells(fill_diff) > 1)
3703 c_extra = '-';
3704 else
3705 c_extra = fill_diff;
3706# ifdef FEAT_RIGHTLEFT
3707 if (wp->w_p_rl)
3708 n_extra = col + 1;
3709 else
3710# endif
3711 n_extra = W_WIDTH(wp) - col;
3712 char_attr = hl_attr(HLF_DED);
3713 }
3714# endif
3715# ifdef FEAT_LINEBREAK
3716 if (*p_sbr != NUL && need_showbreak)
3717 {
3718 /* Draw 'showbreak' at the start of each broken line. */
3719 p_extra = p_sbr;
3720 c_extra = NUL;
3721 n_extra = (int)STRLEN(p_sbr);
3722 char_attr = hl_attr(HLF_AT);
3723 need_showbreak = FALSE;
3724 /* Correct end of highlighted area for 'showbreak',
3725 * required when 'linebreak' is also set. */
3726 if (tocol == vcol)
3727 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003728#ifdef FEAT_SYN_HL
3729 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003730 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003731 char_attr = hl_combine_attr(char_attr, HLF_CLN);
3732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
3734# endif
3735 }
3736#endif
3737
3738 if (draw_state == WL_LINE - 1 && n_extra == 0)
3739 {
3740 draw_state = WL_LINE;
3741 if (saved_n_extra)
3742 {
3743 /* Continue item from end of wrapped line. */
3744 n_extra = saved_n_extra;
3745 c_extra = saved_c_extra;
3746 p_extra = saved_p_extra;
3747 char_attr = saved_char_attr;
3748 }
3749 else
3750 char_attr = 0;
3751 }
3752 }
3753
3754 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003755 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003756 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757#ifdef FEAT_DIFF
3758 && filler_todo <= 0
3759#endif
3760 )
3761 {
3762 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3763 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003764 /* Pretend we have finished updating the window. Except when
3765 * 'cursorcolumn' is set. */
3766#ifdef FEAT_SYN_HL
3767 if (wp->w_p_cuc)
3768 row = wp->w_cline_row + wp->w_cline_height;
3769 else
3770#endif
3771 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 break;
3773 }
3774
3775 if (draw_state == WL_LINE && area_highlighting)
3776 {
3777 /* handle Visual or match highlighting in this line */
3778 if (vcol == fromcol
3779#ifdef FEAT_MBYTE
3780 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3781 && (*mb_ptr2cells)(ptr) > 1)
3782#endif
3783 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003784 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 && vcol < tocol))
3786 area_attr = attr; /* start highlighting */
3787 else if (area_attr != 0
3788 && (vcol == tocol
3789 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792#ifdef FEAT_SEARCH_EXTRA
3793 if (!n_extra)
3794 {
3795 /*
3796 * Check for start/end of search pattern match.
3797 * After end, check for start/end of next match.
3798 * When another match, have to check for start again.
3799 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003800 * Do this for 'search_hl' and the match list (ordered by
3801 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003803 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003804 cur = wp->w_match_head;
3805 shl_flag = FALSE;
3806 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003808 if (shl_flag == FALSE
3809 && ((cur != NULL
3810 && cur->priority > SEARCH_HL_PRIORITY)
3811 || cur == NULL))
3812 {
3813 shl = &search_hl;
3814 shl_flag = TRUE;
3815 }
3816 else
3817 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 while (shl->rm.regprog != NULL)
3819 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003820 if (shl->startcol != MAXCOL
3821 && v >= (long)shl->startcol
3822 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
3824 shl->attr_cur = shl->attr;
3825 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003826 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
3828 shl->attr_cur = 0;
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 next_search_hl(wp, shl, lnum, (colnr_T)v);
3831
3832 /* Need to get the line again, a multi-line regexp
3833 * may have made it invalid. */
3834 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3835 ptr = line + v;
3836
3837 if (shl->lnum == lnum)
3838 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003839 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003841 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003843 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003845 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
3847 /* highlight empty match, try again after
3848 * it */
3849#ifdef FEAT_MBYTE
3850 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003851 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003852 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 else
3854#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003855 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857
3858 /* Loop to check if the match starts at the
3859 * current position */
3860 continue;
3861 }
3862 }
3863 break;
3864 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003865 if (shl != &search_hl && cur != NULL)
3866 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003868
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003869 /* Use attributes from match with highest priority among
3870 * 'search_hl' and the match list. */
3871 search_attr = search_hl.attr_cur;
3872 cur = wp->w_match_head;
3873 shl_flag = FALSE;
3874 while (cur != NULL || shl_flag == FALSE)
3875 {
3876 if (shl_flag == FALSE
3877 && ((cur != NULL
3878 && cur->priority > SEARCH_HL_PRIORITY)
3879 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003880 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003881 shl = &search_hl;
3882 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003883 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003884 else
3885 shl = &cur->hl;
3886 if (shl->attr_cur != 0)
3887 search_attr = shl->attr_cur;
3888 if (shl != &search_hl && cur != NULL)
3889 cur = cur->next;
3890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 }
3892#endif
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003895 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003897 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3898 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003900 if (diff_hlf == HLF_TXD && ptr - line > change_end
3901 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003903 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003906
3907 /* Decide which of the highlight attributes to use. */
3908 attr_pri = TRUE;
3909 if (area_attr != 0)
3910 char_attr = area_attr;
3911 else if (search_attr != 0)
3912 char_attr = search_attr;
3913#ifdef LINE_ATTR
3914 /* Use line_attr when not in the Visual or 'incsearch' area
3915 * (area_attr may be 0 when "noinvcur" is set). */
3916 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003917 || vcol < fromcol || vcol_prev < fromcol_prev
3918 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003919 char_attr = line_attr;
3920#endif
3921 else
3922 {
3923 attr_pri = FALSE;
3924#ifdef FEAT_SYN_HL
3925 if (has_syntax)
3926 char_attr = syntax_attr;
3927 else
3928#endif
3929 char_attr = 0;
3930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932
3933 /*
3934 * Get the next character to put on the screen.
3935 */
3936 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003937 * The "p_extra" points to the extra stuff that is inserted to
3938 * represent special characters (non-printable stuff) and other
3939 * things. When all characters are the same, c_extra is used.
3940 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3941 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3943 */
3944 if (n_extra > 0)
3945 {
3946 if (c_extra != NUL)
3947 {
3948 c = c_extra;
3949#ifdef FEAT_MBYTE
3950 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3951 if (enc_utf8 && (*mb_char2len)(c) > 1)
3952 {
3953 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003954 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003955 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
3957 else
3958 mb_utf8 = FALSE;
3959#endif
3960 }
3961 else
3962 {
3963 c = *p_extra;
3964#ifdef FEAT_MBYTE
3965 if (has_mbyte)
3966 {
3967 mb_c = c;
3968 if (enc_utf8)
3969 {
3970 /* If the UTF-8 character is more than one byte:
3971 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003972 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 mb_utf8 = FALSE;
3974 if (mb_l > n_extra)
3975 mb_l = 1;
3976 else if (mb_l > 1)
3977 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003978 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003980 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 }
3983 else
3984 {
3985 /* if this is a DBCS character, put it in "mb_c" */
3986 mb_l = MB_BYTE2LEN(c);
3987 if (mb_l >= n_extra)
3988 mb_l = 1;
3989 else if (mb_l > 1)
3990 mb_c = (c << 8) + p_extra[1];
3991 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003992 if (mb_l == 0) /* at the NUL at end-of-line */
3993 mb_l = 1;
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 /* If a double-width char doesn't fit display a '>' in the
3996 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003997 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998# ifdef FEAT_RIGHTLEFT
3999 wp->w_p_rl ? (col <= 0) :
4000# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004001 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 && (*mb_char2cells)(mb_c) == 2)
4003 {
4004 c = '>';
4005 mb_c = c;
4006 mb_l = 1;
4007 mb_utf8 = FALSE;
4008 multi_attr = hl_attr(HLF_AT);
4009 /* put the pointer back to output the double-width
4010 * character at the start of the next line. */
4011 ++n_extra;
4012 --p_extra;
4013 }
4014 else
4015 {
4016 n_extra -= mb_l - 1;
4017 p_extra += mb_l - 1;
4018 }
4019 }
4020#endif
4021 ++p_extra;
4022 }
4023 --n_extra;
4024 }
4025 else
4026 {
4027 /*
4028 * Get a character from the line itself.
4029 */
4030 c = *ptr;
4031#ifdef FEAT_MBYTE
4032 if (has_mbyte)
4033 {
4034 mb_c = c;
4035 if (enc_utf8)
4036 {
4037 /* If the UTF-8 character is more than one byte: Decode it
4038 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004039 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 mb_utf8 = FALSE;
4041 if (mb_l > 1)
4042 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004043 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 /* Overlong encoded ASCII or ASCII with composing char
4045 * is displayed normally, except a NUL. */
4046 if (mb_c < 0x80)
4047 c = mb_c;
4048 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004049
4050 /* At start of the line we can have a composing char.
4051 * Draw it as a space with a composing char. */
4052 if (utf_iscomposing(mb_c))
4053 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004054 int i;
4055
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004056 for (i = Screen_mco - 1; i > 0; --i)
4057 u8cc[i] = u8cc[i - 1];
4058 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004059 mb_c = ' ';
4060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062
4063 if ((mb_l == 1 && c >= 0x80)
4064 || (mb_l >= 1 && mb_c == 0)
4065 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004066# ifdef UNICODE16
4067 || mb_c >= 0x10000
4068# endif
4069 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
4071 /*
4072 * Illegal UTF-8 byte: display as <xx>.
4073 * Non-BMP character : display as ? or fullwidth ?.
4074 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004075# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
4079 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004080# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 if (wp->w_p_rl) /* reverse */
4082 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004085# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else if (utf_char2cells(mb_c) != 2)
4087 STRCPY(extra, "?");
4088 else
4089 /* 0xff1f in UTF-8: full-width '?' */
4090 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004091# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092
4093 p_extra = extra;
4094 c = *p_extra;
4095 mb_c = mb_ptr2char_adv(&p_extra);
4096 mb_utf8 = (c >= 0x80);
4097 n_extra = (int)STRLEN(p_extra);
4098 c_extra = NUL;
4099 if (area_attr == 0 && search_attr == 0)
4100 {
4101 n_attr = n_extra + 1;
4102 extra_attr = hl_attr(HLF_8);
4103 saved_attr2 = char_attr; /* save current attr */
4104 }
4105 }
4106 else if (mb_l == 0) /* at the NUL at end-of-line */
4107 mb_l = 1;
4108#ifdef FEAT_ARABIC
4109 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4110 {
4111 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004112 int pc, pc1, nc;
4113 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 /* The idea of what is the previous and next
4116 * character depends on 'rightleft'. */
4117 if (wp->w_p_rl)
4118 {
4119 pc = prev_c;
4120 pc1 = prev_c1;
4121 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004122 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 else
4125 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004126 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004128 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 prev_c = mb_c;
4131
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004132 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
4134 else
4135 prev_c = mb_c;
4136#endif
4137 }
4138 else /* enc_dbcs */
4139 {
4140 mb_l = MB_BYTE2LEN(c);
4141 if (mb_l == 0) /* at the NUL at end-of-line */
4142 mb_l = 1;
4143 else if (mb_l > 1)
4144 {
4145 /* We assume a second byte below 32 is illegal.
4146 * Hopefully this is OK for all double-byte encodings!
4147 */
4148 if (ptr[1] >= 32)
4149 mb_c = (c << 8) + ptr[1];
4150 else
4151 {
4152 if (ptr[1] == NUL)
4153 {
4154 /* head byte at end of line */
4155 mb_l = 1;
4156 transchar_nonprint(extra, c);
4157 }
4158 else
4159 {
4160 /* illegal tail byte */
4161 mb_l = 2;
4162 STRCPY(extra, "XX");
4163 }
4164 p_extra = extra;
4165 n_extra = (int)STRLEN(extra) - 1;
4166 c_extra = NUL;
4167 c = *p_extra++;
4168 if (area_attr == 0 && search_attr == 0)
4169 {
4170 n_attr = n_extra + 1;
4171 extra_attr = hl_attr(HLF_8);
4172 saved_attr2 = char_attr; /* save current attr */
4173 }
4174 mb_c = c;
4175 }
4176 }
4177 }
4178 /* If a double-width char doesn't fit display a '>' in the
4179 * last column; the character is displayed at the start of the
4180 * next line. */
4181 if ((
4182# ifdef FEAT_RIGHTLEFT
4183 wp->w_p_rl ? (col <= 0) :
4184# endif
4185 (col >= W_WIDTH(wp) - 1))
4186 && (*mb_char2cells)(mb_c) == 2)
4187 {
4188 c = '>';
4189 mb_c = c;
4190 mb_utf8 = FALSE;
4191 mb_l = 1;
4192 multi_attr = hl_attr(HLF_AT);
4193 /* Put pointer back so that the character will be
4194 * displayed at the start of the next line. */
4195 --ptr;
4196 }
4197 else if (*ptr != NUL)
4198 ptr += mb_l - 1;
4199
4200 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004201 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004202 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004203 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004206 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 c = ' ';
4208 if (area_attr == 0 && search_attr == 0)
4209 {
4210 n_attr = n_extra + 1;
4211 extra_attr = hl_attr(HLF_AT);
4212 saved_attr2 = char_attr; /* save current attr */
4213 }
4214 mb_c = c;
4215 mb_utf8 = FALSE;
4216 mb_l = 1;
4217 }
4218
4219 }
4220#endif
4221 ++ptr;
4222
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004223 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004224 if (wp->w_p_list && (c == 160
4225#ifdef FEAT_MBYTE
4226 || (mb_utf8 && mb_c == 160)
4227#endif
4228 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004229 {
4230 c = lcs_nbsp;
4231 if (area_attr == 0 && search_attr == 0)
4232 {
4233 n_attr = 1;
4234 extra_attr = hl_attr(HLF_8);
4235 saved_attr2 = char_attr; /* save current attr */
4236 }
4237#ifdef FEAT_MBYTE
4238 mb_c = c;
4239 if (enc_utf8 && (*mb_char2len)(c) > 1)
4240 {
4241 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004242 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004243 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004244 }
4245 else
4246 mb_utf8 = FALSE;
4247#endif
4248 }
4249
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (extra_check)
4251 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004252#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004253 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004254#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004255
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004256#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 /* Get syntax attribute, unless still at the start of the line
4258 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004259 v = (long)(ptr - line);
4260 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
4262 /* Get the syntax attribute for the character. If there
4263 * is an error, disable syntax highlighting. */
4264 save_did_emsg = did_emsg;
4265 did_emsg = FALSE;
4266
Bram Moolenaar217ad922005-03-20 22:37:15 +00004267 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004268# ifdef FEAT_SPELL
4269 has_spell ? &can_spell :
4270# endif
4271 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004274 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004275 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004276 has_syntax = FALSE;
4277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 else
4279 did_emsg = save_did_emsg;
4280
4281 /* Need to get the line again, a multi-line regexp may
4282 * have made it invalid. */
4283 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4284 ptr = line + v;
4285
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004286 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004288 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004289 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004290# ifdef FEAT_CONCEAL
4291 /* no concealing past the end of the line, it interferes
4292 * with line highlighting */
4293 if (c == NUL)
4294 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004295 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004296 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004299#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004300
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004301#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004302 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004303 * Only do this when there is no syntax highlighting, the
4304 * @Spell cluster is not used or the current syntax item
4305 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004306 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004307 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004308 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004309# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004310 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004311 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004312# endif
4313 if (c != 0 && (
4314# ifdef FEAT_SYN_HL
4315 !has_syntax ||
4316# endif
4317 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004318 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004319 char_u *prev_ptr, *p;
4320 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004321 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004322# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004323 if (has_mbyte)
4324 {
4325 prev_ptr = ptr - mb_l;
4326 v -= mb_l - 1;
4327 }
4328 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004329# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004330 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004331
4332 /* Use nextline[] if possible, it has the start of the
4333 * next line concatenated. */
4334 if ((prev_ptr - line) - nextlinecol >= 0)
4335 p = nextline + (prev_ptr - line) - nextlinecol;
4336 else
4337 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004338 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004339 len = spell_check(wp, p, &spell_hlf, &cap_col,
4340 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004341 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004342
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004343 /* In Insert mode only highlight a word that
4344 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004345 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004346 && (State & INSERT) != 0
4347 && wp->w_cursor.lnum == lnum
4348 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004349 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004350 && wp->w_cursor.col < (colnr_T)word_end)
4351 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004352 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004353 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004354 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004355
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004356 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004357 && (p - nextline) + len > nextline_idx)
4358 {
4359 /* Remember that the good word continues at the
4360 * start of the next line. */
4361 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004362 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004363 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004364
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004365 /* Turn index into actual attributes. */
4366 if (spell_hlf != HLF_COUNT)
4367 spell_attr = highlight_attr[spell_hlf];
4368
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004369 if (cap_col > 0)
4370 {
4371 if (p != prev_ptr
4372 && (p - nextline) + cap_col >= nextline_idx)
4373 {
4374 /* Remember that the word in the next line
4375 * must start with a capital. */
4376 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004377 cap_col = (int)((p - nextline) + cap_col
4378 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004379 }
4380 else
4381 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004382 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004383 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004384 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004385 }
4386 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004387 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004388 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004389 char_attr = hl_combine_attr(char_attr, spell_attr);
4390 else
4391 char_attr = hl_combine_attr(spell_attr, char_attr);
4392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393#endif
4394#ifdef FEAT_LINEBREAK
4395 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004396 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 */
4398 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004399 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 {
4401 n_extra = win_lbr_chartabsize(wp, ptr - (
4402# ifdef FEAT_MBYTE
4403 has_mbyte ? mb_l :
4404# endif
4405 1), (colnr_T)vcol, NULL) - 1;
4406 c_extra = ' ';
4407 if (vim_iswhite(c))
4408 c = ' ';
4409 }
4410#endif
4411
4412 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4413 {
4414 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004415 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
4417 n_attr = 1;
4418 extra_attr = hl_attr(HLF_8);
4419 saved_attr2 = char_attr; /* save current attr */
4420 }
4421#ifdef FEAT_MBYTE
4422 mb_c = c;
4423 if (enc_utf8 && (*mb_char2len)(c) > 1)
4424 {
4425 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004426 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004427 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 else
4430 mb_utf8 = FALSE;
4431#endif
4432 }
4433 }
4434
4435 /*
4436 * Handling of non-printable characters.
4437 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004438 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
4440 /*
4441 * when getting a character from the file, we may have to
4442 * turn it into something else on the way to putting it
4443 * into "ScreenLines".
4444 */
4445 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4446 {
4447 /* tab amount depends on current column */
4448 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004449 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4450#ifdef FEAT_CONCEAL
4451 /* Tab alignment should be identical regardless of
4452 * 'conceallevel' value. So tab compensates of all
4453 * previous concealed characters, and thus resets vcol_off
4454 * and boguscols accumulated so far in the line. Note that
4455 * the tab can be longer than 'tabstop' when there
4456 * are concealed characters. */
4457 n_extra += vcol_off;
4458 vcol -= vcol_off;
4459 vcol_off = 0;
4460 col -= boguscols;
4461 boguscols = 0;
4462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463#ifdef FEAT_MBYTE
4464 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4465#endif
4466 if (wp->w_p_list)
4467 {
4468 c = lcs_tab1;
4469 c_extra = lcs_tab2;
4470 n_attr = n_extra + 1;
4471 extra_attr = hl_attr(HLF_8);
4472 saved_attr2 = char_attr; /* save current attr */
4473#ifdef FEAT_MBYTE
4474 mb_c = c;
4475 if (enc_utf8 && (*mb_char2len)(c) > 1)
4476 {
4477 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004478 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004479 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481#endif
4482 }
4483 else
4484 {
4485 c_extra = ' ';
4486 c = ' ';
4487 }
4488 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004489 else if (c == NUL
4490 && ((wp->w_p_list && lcs_eol > 0)
4491 || ((fromcol >= 0 || fromcol_prev >= 0)
4492 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004493#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004494 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004495#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004496 && (
4497# ifdef FEAT_RIGHTLEFT
4498 wp->w_p_rl ? (col >= 0) :
4499# endif
4500 (col < W_WIDTH(wp)))
4501 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004502 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004503 && (colnr_T)vcol == wp->w_virtcol)))
4504 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004506 /* Display a '$' after the line or highlight an extra
4507 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4509 /* For a diff line the highlighting continues after the
4510 * "$". */
4511 if (
4512# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004513 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514# ifdef LINE_ATTR
4515 &&
4516# endif
4517# endif
4518# ifdef LINE_ATTR
4519 line_attr == 0
4520# endif
4521 )
4522#endif
4523 {
4524#ifdef FEAT_VIRTUALEDIT
4525 /* In virtualedit, visual selections may extend
4526 * beyond end of line. */
4527 if (area_highlighting && virtual_active()
4528 && tocol != MAXCOL && vcol < tocol)
4529 n_extra = 0;
4530 else
4531#endif
4532 {
4533 p_extra = at_end_str;
4534 n_extra = 1;
4535 c_extra = NUL;
4536 }
4537 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004538 if (wp->w_p_list)
4539 c = lcs_eol;
4540 else
4541 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 lcs_eol_one = -1;
4543 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004544 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
4546 extra_attr = hl_attr(HLF_AT);
4547 n_attr = 1;
4548 }
4549#ifdef FEAT_MBYTE
4550 mb_c = c;
4551 if (enc_utf8 && (*mb_char2len)(c) > 1)
4552 {
4553 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004554 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004555 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557 else
4558 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4559#endif
4560 }
4561 else if (c != NUL)
4562 {
4563 p_extra = transchar(c);
4564#ifdef FEAT_RIGHTLEFT
4565 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4566 rl_mirror(p_extra); /* reverse "<12>" */
4567#endif
4568 n_extra = byte2cells(c) - 1;
4569 c_extra = NUL;
4570 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004571 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
4573 n_attr = n_extra + 1;
4574 extra_attr = hl_attr(HLF_8);
4575 saved_attr2 = char_attr; /* save current attr */
4576 }
4577#ifdef FEAT_MBYTE
4578 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4579#endif
4580 }
4581#ifdef FEAT_VIRTUALEDIT
4582 else if (VIsual_active
4583 && (VIsual_mode == Ctrl_V
4584 || VIsual_mode == 'v')
4585 && virtual_active()
4586 && tocol != MAXCOL
4587 && vcol < tocol
4588 && (
4589# ifdef FEAT_RIGHTLEFT
4590 wp->w_p_rl ? (col >= 0) :
4591# endif
4592 (col < W_WIDTH(wp))))
4593 {
4594 c = ' ';
4595 --ptr; /* put it back at the NUL */
4596 }
4597#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004598#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 else if ((
4600# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004601 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 ) && (
4605# ifdef FEAT_RIGHTLEFT
4606 wp->w_p_rl ? (col >= 0) :
4607# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004608 (col
4609# ifdef FEAT_CONCEAL
4610 - boguscols
4611# endif
4612 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
4614 /* Highlight until the right side of the window */
4615 c = ' ';
4616 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004617
4618 /* Remember we do the char for line highlighting. */
4619 ++did_line_attr;
4620
4621 /* don't do search HL for the rest of the line */
4622 if (line_attr != 0 && char_attr == search_attr && col > 0)
4623 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624# ifdef FEAT_DIFF
4625 if (diff_hlf == HLF_TXD)
4626 {
4627 diff_hlf = HLF_CHD;
4628 if (attr == 0 || char_attr != attr)
4629 char_attr = hl_attr(diff_hlf);
4630 }
4631# endif
4632 }
4633#endif
4634 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004635
4636#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004637 if ( wp->w_p_cole > 0
4638 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4639 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004640 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004641 && !(lnum_in_visual_area
4642 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004643 {
4644 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004645 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004646 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4647 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004648 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004649 /* First time at this concealed item: display one
4650 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004651 if (syn_get_sub_char() != NUL)
4652 c = syn_get_sub_char();
4653 else if (lcs_conceal != NUL)
4654 c = lcs_conceal;
4655 else
4656 c = ' ';
4657
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004658 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004659
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004660 if (n_extra > 0)
4661 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004662 vcol += n_extra;
4663 if (wp->w_p_wrap && n_extra > 0)
4664 {
4665# ifdef FEAT_RIGHTLEFT
4666 if (wp->w_p_rl)
4667 {
4668 col -= n_extra;
4669 boguscols -= n_extra;
4670 }
4671 else
4672# endif
4673 {
4674 boguscols += n_extra;
4675 col += n_extra;
4676 }
4677 }
4678 n_extra = 0;
4679 n_attr = 0;
4680 }
4681 else if (n_skip == 0)
4682 {
4683 is_concealing = TRUE;
4684 n_skip = 1;
4685 }
4686# ifdef FEAT_MBYTE
4687 mb_c = c;
4688 if (enc_utf8 && (*mb_char2len)(c) > 1)
4689 {
4690 mb_utf8 = TRUE;
4691 u8cc[0] = 0;
4692 c = 0xc0;
4693 }
4694 else
4695 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4696# endif
4697 }
4698 else
4699 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004700 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004701 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004702 }
4703#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004706#ifdef FEAT_CONCEAL
4707 /* In the cursor line and we may be concealing characters: correct
4708 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004709 if (!did_wcol && draw_state == WL_LINE
4710 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004711 && conceal_cursor_line(wp)
4712 && (int)wp->w_virtcol <= vcol + n_skip)
4713 {
4714 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004715 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004716 did_wcol = TRUE;
4717 }
4718#endif
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 /* Don't override visual selection highlighting. */
4721 if (n_attr > 0
4722 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004723 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 char_attr = extra_attr;
4725
Bram Moolenaar81695252004-12-29 20:58:21 +00004726#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 /* XIM don't send preedit_start and preedit_end, but they send
4728 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4729 * im_is_preediting() here. */
4730 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004731 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 && (State & INSERT)
4733 && !p_imdisable
4734 && im_is_preediting()
4735 && draw_state == WL_LINE)
4736 {
4737 colnr_T tcol;
4738
4739 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004740 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 else
4742 tcol = preedit_end_col;
4743 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4744 {
4745 if (feedback_old_attr < 0)
4746 {
4747 feedback_col = 0;
4748 feedback_old_attr = char_attr;
4749 }
4750 char_attr = im_get_feedback_attr(feedback_col);
4751 if (char_attr < 0)
4752 char_attr = feedback_old_attr;
4753 feedback_col++;
4754 }
4755 else if (feedback_old_attr >= 0)
4756 {
4757 char_attr = feedback_old_attr;
4758 feedback_old_attr = -1;
4759 feedback_col = 0;
4760 }
4761 }
4762#endif
4763 /*
4764 * Handle the case where we are in column 0 but not on the first
4765 * character of the line and the user wants us to show us a
4766 * special character (via 'listchars' option "precedes:<char>".
4767 */
4768 if (lcs_prec_todo != NUL
4769 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4770#ifdef FEAT_DIFF
4771 && filler_todo <= 0
4772#endif
4773 && draw_state > WL_NR
4774 && c != NUL)
4775 {
4776 c = lcs_prec;
4777 lcs_prec_todo = NUL;
4778#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004779 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4780 {
4781 /* Double-width character being overwritten by the "precedes"
4782 * character, need to fill up half the character. */
4783 c_extra = MB_FILLER_CHAR;
4784 n_extra = 1;
4785 n_attr = 2;
4786 extra_attr = hl_attr(HLF_AT);
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 mb_c = c;
4789 if (enc_utf8 && (*mb_char2len)(c) > 1)
4790 {
4791 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004792 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004793 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 else
4796 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4797#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004798 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
4800 saved_attr3 = char_attr; /* save current attr */
4801 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4802 n_attr3 = 1;
4803 }
4804 }
4805
4806 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004807 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004809 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004810#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004811 || did_line_attr == 1
4812#endif
4813 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004815#ifdef FEAT_SEARCH_EXTRA
4816 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004817
4818 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004819 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004820 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004821#endif
4822
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004823 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 * highlight match at end of line. If it's beyond the last
4825 * char on the screen, just overwrite that one (tricky!) Not
4826 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004827#ifdef FEAT_SEARCH_EXTRA
4828 prevcol_hl_flag = FALSE;
4829 if (prevcol == (long)search_hl.startcol)
4830 prevcol_hl_flag = TRUE;
4831 else
4832 {
4833 cur = wp->w_match_head;
4834 while (cur != NULL)
4835 {
4836 if (prevcol == (long)cur->hl.startcol)
4837 {
4838 prevcol_hl_flag = TRUE;
4839 break;
4840 }
4841 cur = cur->next;
4842 }
4843 }
4844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004846 && ((area_attr != 0 && vcol == fromcol
4847#ifdef FEAT_VISUAL
4848 && (VIsual_mode != Ctrl_V
4849 || lnum == VIsual.lnum
4850 || lnum == curwin->w_cursor.lnum)
4851#endif
4852 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853#ifdef FEAT_SEARCH_EXTRA
4854 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004855 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004856# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004857 && did_line_attr <= 1
4858# endif
4859 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860#endif
4861 ))
4862 {
4863 int n = 0;
4864
4865#ifdef FEAT_RIGHTLEFT
4866 if (wp->w_p_rl)
4867 {
4868 if (col < 0)
4869 n = 1;
4870 }
4871 else
4872#endif
4873 {
4874 if (col >= W_WIDTH(wp))
4875 n = -1;
4876 }
4877 if (n != 0)
4878 {
4879 /* At the window boundary, highlight the last character
4880 * instead (better than nothing). */
4881 off += n;
4882 col += n;
4883 }
4884 else
4885 {
4886 /* Add a blank character to highlight. */
4887 ScreenLines[off] = ' ';
4888#ifdef FEAT_MBYTE
4889 if (enc_utf8)
4890 ScreenLinesUC[off] = 0;
4891#endif
4892 }
4893#ifdef FEAT_SEARCH_EXTRA
4894 if (area_attr == 0)
4895 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004896 /* Use attributes from match with highest priority among
4897 * 'search_hl' and the match list. */
4898 char_attr = search_hl.attr;
4899 cur = wp->w_match_head;
4900 shl_flag = FALSE;
4901 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004902 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004903 if (shl_flag == FALSE
4904 && ((cur != NULL
4905 && cur->priority > SEARCH_HL_PRIORITY)
4906 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004907 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004908 shl = &search_hl;
4909 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004910 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004911 else
4912 shl = &cur->hl;
4913 if ((ptr - line) - 1 == (long)shl->startcol)
4914 char_attr = shl->attr;
4915 if (shl != &search_hl && cur != NULL)
4916 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919#endif
4920 ScreenAttrs[off] = char_attr;
4921#ifdef FEAT_RIGHTLEFT
4922 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004925 --off;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
4928#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004931 ++off;
4932 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004933 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004934#ifdef FEAT_SYN_HL
4935 eol_hl_off = 1;
4936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
Bram Moolenaar91170f82006-05-05 21:15:17 +00004940 /*
4941 * At end of the text line.
4942 */
4943 if (c == NUL)
4944 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004945#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004946 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4947 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004948 {
4949 /* highlight last char after line */
4950 --col;
4951 --off;
4952 --vcol;
4953 }
4954
Bram Moolenaar1a384422010-07-14 19:53:30 +02004955 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004956 if (wp->w_p_wrap)
4957 v = wp->w_skipcol;
4958 else
4959 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004960
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004961 /* check if line ends before left margin */
4962 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004963 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004964#ifdef FEAT_CONCEAL
4965 /* Get rid of the boguscols now, we want to draw until the right
4966 * edge for 'cursorcolumn'. */
4967 col -= boguscols;
4968 boguscols = 0;
4969#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004970
4971 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004972 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004973
4974 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004975 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4976 && (int)wp->w_virtcol <
4977 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004978 && lnum != wp->w_cursor.lnum)
4979 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004980# ifdef FEAT_RIGHTLEFT
4981 && !wp->w_p_rl
4982# endif
4983 )
4984 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004985 int rightmost_vcol = 0;
4986 int i;
4987
4988 if (wp->w_p_cuc)
4989 rightmost_vcol = wp->w_virtcol;
4990 if (draw_color_col)
4991 /* determine rightmost colorcolumn to possibly draw */
4992 for (i = 0; color_cols[i] >= 0; ++i)
4993 if (rightmost_vcol < color_cols[i])
4994 rightmost_vcol = color_cols[i];
4995
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004996 while (col < W_WIDTH(wp))
4997 {
4998 ScreenLines[off] = ' ';
4999#ifdef FEAT_MBYTE
5000 if (enc_utf8)
5001 ScreenLinesUC[off] = 0;
5002#endif
5003 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005004 if (draw_color_col)
5005 draw_color_col = advance_color_col(VCOL_HLC,
5006 &color_cols);
5007
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005008 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005009 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005010 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005011 ScreenAttrs[off++] = hl_attr(HLF_MC);
5012 else
5013 ScreenAttrs[off++] = 0;
5014
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005015 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005016 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005017
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005018 ++vcol;
5019 }
5020 }
5021#endif
5022
Bram Moolenaar860cae12010-06-05 23:22:07 +02005023 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5024 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 row++;
5026
5027 /*
5028 * Update w_cline_height and w_cline_folded if the cursor line was
5029 * updated (saves a call to plines() later).
5030 */
5031 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5032 {
5033 curwin->w_cline_row = startrow;
5034 curwin->w_cline_height = row - startrow;
5035#ifdef FEAT_FOLDING
5036 curwin->w_cline_folded = FALSE;
5037#endif
5038 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5039 }
5040
5041 break;
5042 }
5043
5044 /* line continues beyond line end */
5045 if (lcs_ext
5046 && !wp->w_p_wrap
5047#ifdef FEAT_DIFF
5048 && filler_todo <= 0
5049#endif
5050 && (
5051#ifdef FEAT_RIGHTLEFT
5052 wp->w_p_rl ? col == 0 :
5053#endif
5054 col == W_WIDTH(wp) - 1)
5055 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005056 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5058 {
5059 c = lcs_ext;
5060 char_attr = hl_attr(HLF_AT);
5061#ifdef FEAT_MBYTE
5062 mb_c = c;
5063 if (enc_utf8 && (*mb_char2len)(c) > 1)
5064 {
5065 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005066 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005067 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069 else
5070 mb_utf8 = FALSE;
5071#endif
5072 }
5073
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005074#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005075 /* advance to the next 'colorcolumn' */
5076 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005077 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005078
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005079 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005080 * highlight the cursor position itself.
5081 * Also highlight the 'colorcolumn' if it is different than
5082 * 'cursorcolumn' */
5083 vcol_save_attr = -1;
5084 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005085 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005086 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005087 && lnum != wp->w_cursor.lnum)
5088 {
5089 vcol_save_attr = char_attr;
5090 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5091 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005092 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005093 {
5094 vcol_save_attr = char_attr;
5095 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5096 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005097 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005098#endif
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 /*
5101 * Store character to be displayed.
5102 * Skip characters that are left of the screen for 'nowrap'.
5103 */
5104 vcol_prev = vcol;
5105 if (draw_state < WL_LINE || n_skip <= 0)
5106 {
5107 /*
5108 * Store the character.
5109 */
5110#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5111 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5112 {
5113 /* A double-wide character is: put first halve in left cell. */
5114 --off;
5115 --col;
5116 }
5117#endif
5118 ScreenLines[off] = c;
5119#ifdef FEAT_MBYTE
5120 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005121 {
5122 if ((mb_c & 0xff00) == 0x8e00)
5123 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 else if (enc_utf8)
5127 {
5128 if (mb_utf8)
5129 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005130 int i;
5131
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005133 if ((c & 0xff) == 0)
5134 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005135 for (i = 0; i < Screen_mco; ++i)
5136 {
5137 ScreenLinesC[i][off] = u8cc[i];
5138 if (u8cc[i] == 0)
5139 break;
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 else
5143 ScreenLinesUC[off] = 0;
5144 }
5145 if (multi_attr)
5146 {
5147 ScreenAttrs[off] = multi_attr;
5148 multi_attr = 0;
5149 }
5150 else
5151#endif
5152 ScreenAttrs[off] = char_attr;
5153
5154#ifdef FEAT_MBYTE
5155 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5156 {
5157 /* Need to fill two screen columns. */
5158 ++off;
5159 ++col;
5160 if (enc_utf8)
5161 /* UTF-8: Put a 0 in the second screen char. */
5162 ScreenLines[off] = 0;
5163 else
5164 /* DBCS: Put second byte in the second screen char. */
5165 ScreenLines[off] = mb_c & 0xff;
5166 ++vcol;
5167 /* When "tocol" is halfway a character, set it to the end of
5168 * the character, otherwise highlighting won't stop. */
5169 if (tocol == vcol)
5170 ++tocol;
5171#ifdef FEAT_RIGHTLEFT
5172 if (wp->w_p_rl)
5173 {
5174 /* now it's time to backup one cell */
5175 --off;
5176 --col;
5177 }
5178#endif
5179 }
5180#endif
5181#ifdef FEAT_RIGHTLEFT
5182 if (wp->w_p_rl)
5183 {
5184 --off;
5185 --col;
5186 }
5187 else
5188#endif
5189 {
5190 ++off;
5191 ++col;
5192 }
5193 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005194#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005195 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005196 {
5197 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005198 ++vcol_off;
5199 if (n_extra > 0)
5200 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005201 if (wp->w_p_wrap)
5202 {
5203 /*
5204 * Special voodoo required if 'wrap' is on.
5205 *
5206 * Advance the column indicator to force the line
5207 * drawing to wrap early. This will make the line
5208 * take up the same screen space when parts are concealed,
5209 * so that cursor line computations aren't messed up.
5210 *
5211 * To avoid the fictitious advance of 'col' causing
5212 * trailing junk to be written out of the screen line
5213 * we are building, 'boguscols' keeps track of the number
5214 * of bad columns we have advanced.
5215 */
5216 if (n_extra > 0)
5217 {
5218 vcol += n_extra;
5219# ifdef FEAT_RIGHTLEFT
5220 if (wp->w_p_rl)
5221 {
5222 col -= n_extra;
5223 boguscols -= n_extra;
5224 }
5225 else
5226# endif
5227 {
5228 col += n_extra;
5229 boguscols += n_extra;
5230 }
5231 n_extra = 0;
5232 n_attr = 0;
5233 }
5234
5235
5236# ifdef FEAT_MBYTE
5237 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5238 {
5239 /* Need to fill two screen columns. */
5240# ifdef FEAT_RIGHTLEFT
5241 if (wp->w_p_rl)
5242 {
5243 --boguscols;
5244 --col;
5245 }
5246 else
5247# endif
5248 {
5249 ++boguscols;
5250 ++col;
5251 }
5252 }
5253# endif
5254
5255# ifdef FEAT_RIGHTLEFT
5256 if (wp->w_p_rl)
5257 {
5258 --boguscols;
5259 --col;
5260 }
5261 else
5262# endif
5263 {
5264 ++boguscols;
5265 ++col;
5266 }
5267 }
5268 else
5269 {
5270 if (n_extra > 0)
5271 {
5272 vcol += n_extra;
5273 n_extra = 0;
5274 n_attr = 0;
5275 }
5276 }
5277
5278 }
5279#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 else
5281 --n_skip;
5282
Bram Moolenaar64486672010-05-16 15:46:46 +02005283 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5284 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005285 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286#ifdef FEAT_DIFF
5287 && filler_todo <= 0
5288#endif
5289 )
5290 ++vcol;
5291
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005292#ifdef FEAT_SYN_HL
5293 if (vcol_save_attr >= 0)
5294 char_attr = vcol_save_attr;
5295#endif
5296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 /* restore attributes after "predeces" in 'listchars' */
5298 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5299 char_attr = saved_attr3;
5300
5301 /* restore attributes after last 'listchars' or 'number' char */
5302 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5303 char_attr = saved_attr2;
5304
5305 /*
5306 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005307 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 */
5309 if ((
5310#ifdef FEAT_RIGHTLEFT
5311 wp->w_p_rl ? (col < 0) :
5312#endif
5313 (col >= W_WIDTH(wp)))
5314 && (*ptr != NUL
5315#ifdef FEAT_DIFF
5316 || filler_todo > 0
5317#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005318 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5320 )
5321 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005322#ifdef FEAT_CONCEAL
5323 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5324 (int)W_WIDTH(wp), wp->w_p_rl);
5325 boguscols = 0;
5326#else
5327 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5328 (int)W_WIDTH(wp), wp->w_p_rl);
5329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 ++row;
5331 ++screen_row;
5332
5333 /* When not wrapping and finished diff lines, or when displayed
5334 * '$' and highlighting until last column, break here. */
5335 if ((!wp->w_p_wrap
5336#ifdef FEAT_DIFF
5337 && filler_todo <= 0
5338#endif
5339 ) || lcs_eol_one == -1)
5340 break;
5341
5342 /* When the window is too narrow draw all "@" lines. */
5343 if (draw_state != WL_LINE
5344#ifdef FEAT_DIFF
5345 && filler_todo <= 0
5346#endif
5347 )
5348 {
5349 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5350#ifdef FEAT_VERTSPLIT
5351 draw_vsep_win(wp, row);
5352#endif
5353 row = endrow;
5354 }
5355
5356 /* When line got too long for screen break here. */
5357 if (row == endrow)
5358 {
5359 ++row;
5360 break;
5361 }
5362
5363 if (screen_cur_row == screen_row - 1
5364#ifdef FEAT_DIFF
5365 && filler_todo <= 0
5366#endif
5367 && W_WIDTH(wp) == Columns)
5368 {
5369 /* Remember that the line wraps, used for modeless copy. */
5370 LineWraps[screen_row - 1] = TRUE;
5371
5372 /*
5373 * Special trick to make copy/paste of wrapped lines work with
5374 * xterm/screen: write an extra character beyond the end of
5375 * the line. This will work with all terminal types
5376 * (regardless of the xn,am settings).
5377 * Only do this on a fast tty.
5378 * Only do this if the cursor is on the current line
5379 * (something has been written in it).
5380 * Don't do this for the GUI.
5381 * Don't do this for double-width characters.
5382 * Don't do this for a window not at the right screen border.
5383 */
5384 if (p_tf
5385#ifdef FEAT_GUI
5386 && !gui.in_use
5387#endif
5388#ifdef FEAT_MBYTE
5389 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005390 && ((*mb_off2cells)(LineOffset[screen_row],
5391 LineOffset[screen_row] + screen_Columns)
5392 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005394 + (int)Columns - 2,
5395 LineOffset[screen_row] + screen_Columns)
5396 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#endif
5398 )
5399 {
5400 /* First make sure we are at the end of the screen line,
5401 * then output the same character again to let the
5402 * terminal know about the wrap. If the terminal doesn't
5403 * auto-wrap, we overwrite the character. */
5404 if (screen_cur_col != W_WIDTH(wp))
5405 screen_char(LineOffset[screen_row - 1]
5406 + (unsigned)Columns - 1,
5407 screen_row - 1, (int)(Columns - 1));
5408
5409#ifdef FEAT_MBYTE
5410 /* When there is a multi-byte character, just output a
5411 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005412 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5413 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 out_char(' ');
5415 else
5416#endif
5417 out_char(ScreenLines[LineOffset[screen_row - 1]
5418 + (Columns - 1)]);
5419 /* force a redraw of the first char on the next line */
5420 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5421 screen_start(); /* don't know where cursor is now */
5422 }
5423 }
5424
5425 col = 0;
5426 off = (unsigned)(current_ScreenLine - ScreenLines);
5427#ifdef FEAT_RIGHTLEFT
5428 if (wp->w_p_rl)
5429 {
5430 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5431 off += col;
5432 }
5433#endif
5434
5435 /* reset the drawing state for the start of a wrapped line */
5436 draw_state = WL_START;
5437 saved_n_extra = n_extra;
5438 saved_p_extra = p_extra;
5439 saved_c_extra = c_extra;
5440 saved_char_attr = char_attr;
5441 n_extra = 0;
5442 lcs_prec_todo = lcs_prec;
5443#ifdef FEAT_LINEBREAK
5444# ifdef FEAT_DIFF
5445 if (filler_todo <= 0)
5446# endif
5447 need_showbreak = TRUE;
5448#endif
5449#ifdef FEAT_DIFF
5450 --filler_todo;
5451 /* When the filler lines are actually below the last line of the
5452 * file, don't draw the line itself, break here. */
5453 if (filler_todo == 0 && wp->w_botfill)
5454 break;
5455#endif
5456 }
5457
5458 } /* for every character in the line */
5459
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005460#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005461 /* After an empty line check first word for capital. */
5462 if (*skipwhite(line) == NUL)
5463 {
5464 capcol_lnum = lnum + 1;
5465 cap_col = 0;
5466 }
5467#endif
5468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 return row;
5470}
5471
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005472#ifdef FEAT_MBYTE
5473static int comp_char_differs __ARGS((int, int));
5474
5475/*
5476 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005477 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005478 */
5479 static int
5480comp_char_differs(off_from, off_to)
5481 int off_from;
5482 int off_to;
5483{
5484 int i;
5485
5486 for (i = 0; i < Screen_mco; ++i)
5487 {
5488 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5489 return TRUE;
5490 if (ScreenLinesC[i][off_from] == 0)
5491 break;
5492 }
5493 return FALSE;
5494}
5495#endif
5496
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497/*
5498 * Check whether the given character needs redrawing:
5499 * - the (first byte of the) character is different
5500 * - the attributes are different
5501 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005502 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 */
5504 static int
5505char_needs_redraw(off_from, off_to, cols)
5506 int off_from;
5507 int off_to;
5508 int cols;
5509{
5510 if (cols > 0
5511 && ((ScreenLines[off_from] != ScreenLines[off_to]
5512 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5513
5514#ifdef FEAT_MBYTE
5515 || (enc_dbcs != 0
5516 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5517 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5518 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5519 : (cols > 1 && ScreenLines[off_from + 1]
5520 != ScreenLines[off_to + 1])))
5521 || (enc_utf8
5522 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5523 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005524 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005525 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5526 && ScreenLines[off_from + 1]
5527 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#endif
5529 ))
5530 return TRUE;
5531 return FALSE;
5532}
5533
5534/*
5535 * Move one "cooked" screen line to the screen, but only the characters that
5536 * have actually changed. Handle insert/delete character.
5537 * "coloff" gives the first column on the screen for this line.
5538 * "endcol" gives the columns where valid characters are.
5539 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5540 * needs to be cleared, negative otherwise.
5541 * "rlflag" is TRUE in a rightleft window:
5542 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5543 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5544 */
5545 static void
5546screen_line(row, coloff, endcol, clear_width
5547#ifdef FEAT_RIGHTLEFT
5548 , rlflag
5549#endif
5550 )
5551 int row;
5552 int coloff;
5553 int endcol;
5554 int clear_width;
5555#ifdef FEAT_RIGHTLEFT
5556 int rlflag;
5557#endif
5558{
5559 unsigned off_from;
5560 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005561#ifdef FEAT_MBYTE
5562 unsigned max_off_from;
5563 unsigned max_off_to;
5564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 int col = 0;
5566#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5567 int hl;
5568#endif
5569 int force = FALSE; /* force update rest of the line */
5570 int redraw_this /* bool: does character need redraw? */
5571#ifdef FEAT_GUI
5572 = TRUE /* For GUI when while-loop empty */
5573#endif
5574 ;
5575 int redraw_next; /* redraw_this for next character */
5576#ifdef FEAT_MBYTE
5577 int clear_next = FALSE;
5578 int char_cells; /* 1: normal char */
5579 /* 2: occupies two display cells */
5580# define CHAR_CELLS char_cells
5581#else
5582# define CHAR_CELLS 1
5583#endif
5584
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005585 /* Check for illegal row and col, just in case. */
5586 if (row >= Rows)
5587 row = Rows - 1;
5588 if (endcol > Columns)
5589 endcol = Columns;
5590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591# ifdef FEAT_CLIPBOARD
5592 clip_may_clear_selection(row, row);
5593# endif
5594
5595 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5596 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005597#ifdef FEAT_MBYTE
5598 max_off_from = off_from + screen_Columns;
5599 max_off_to = LineOffset[row] + screen_Columns;
5600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
5602#ifdef FEAT_RIGHTLEFT
5603 if (rlflag)
5604 {
5605 /* Clear rest first, because it's left of the text. */
5606 if (clear_width > 0)
5607 {
5608 while (col <= endcol && ScreenLines[off_to] == ' '
5609 && ScreenAttrs[off_to] == 0
5610# ifdef FEAT_MBYTE
5611 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5612# endif
5613 )
5614 {
5615 ++off_to;
5616 ++col;
5617 }
5618 if (col <= endcol)
5619 screen_fill(row, row + 1, col + coloff,
5620 endcol + coloff + 1, ' ', ' ', 0);
5621 }
5622 col = endcol + 1;
5623 off_to = LineOffset[row] + col + coloff;
5624 off_from += col;
5625 endcol = (clear_width > 0 ? clear_width : -clear_width);
5626 }
5627#endif /* FEAT_RIGHTLEFT */
5628
5629 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5630
5631 while (col < endcol)
5632 {
5633#ifdef FEAT_MBYTE
5634 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005635 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 else
5637 char_cells = 1;
5638#endif
5639
5640 redraw_this = redraw_next;
5641 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5642 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5643
5644#ifdef FEAT_GUI
5645 /* If the next character was bold, then redraw the current character to
5646 * remove any pixels that might have spilt over into us. This only
5647 * happens in the GUI.
5648 */
5649 if (redraw_next && gui.in_use)
5650 {
5651 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005652 if (hl > HL_ALL)
5653 hl = syn_attr2attr(hl);
5654 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 redraw_this = TRUE;
5656 }
5657#endif
5658
5659 if (redraw_this)
5660 {
5661 /*
5662 * Special handling when 'xs' termcap flag set (hpterm):
5663 * Attributes for characters are stored at the position where the
5664 * cursor is when writing the highlighting code. The
5665 * start-highlighting code must be written with the cursor on the
5666 * first highlighted character. The stop-highlighting code must
5667 * be written with the cursor just after the last highlighted
5668 * character.
5669 * Overwriting a character doesn't remove it's highlighting. Need
5670 * to clear the rest of the line, and force redrawing it
5671 * completely.
5672 */
5673 if ( p_wiv
5674 && !force
5675#ifdef FEAT_GUI
5676 && !gui.in_use
5677#endif
5678 && ScreenAttrs[off_to] != 0
5679 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5680 {
5681 /*
5682 * Need to remove highlighting attributes here.
5683 */
5684 windgoto(row, col + coloff);
5685 out_str(T_CE); /* clear rest of this screen line */
5686 screen_start(); /* don't know where cursor is now */
5687 force = TRUE; /* force redraw of rest of the line */
5688 redraw_next = TRUE; /* or else next char would miss out */
5689
5690 /*
5691 * If the previous character was highlighted, need to stop
5692 * highlighting at this character.
5693 */
5694 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5695 {
5696 screen_attr = ScreenAttrs[off_to - 1];
5697 term_windgoto(row, col + coloff);
5698 screen_stop_highlight();
5699 }
5700 else
5701 screen_attr = 0; /* highlighting has stopped */
5702 }
5703#ifdef FEAT_MBYTE
5704 if (enc_dbcs != 0)
5705 {
5706 /* Check if overwriting a double-byte with a single-byte or
5707 * the other way around requires another character to be
5708 * redrawn. For UTF-8 this isn't needed, because comparing
5709 * ScreenLinesUC[] is sufficient. */
5710 if (char_cells == 1
5711 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005712 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 /* Writing a single-cell character over a double-cell
5715 * character: need to redraw the next cell. */
5716 ScreenLines[off_to + 1] = 0;
5717 redraw_next = TRUE;
5718 }
5719 else if (char_cells == 2
5720 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005721 && (*mb_off2cells)(off_to, max_off_to) == 1
5722 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
5724 /* Writing the second half of a double-cell character over
5725 * a double-cell character: need to redraw the second
5726 * cell. */
5727 ScreenLines[off_to + 2] = 0;
5728 redraw_next = TRUE;
5729 }
5730
5731 if (enc_dbcs == DBCS_JPNU)
5732 ScreenLines2[off_to] = ScreenLines2[off_from];
5733 }
5734 /* When writing a single-width character over a double-width
5735 * character and at the end of the redrawn text, need to clear out
5736 * the right halve of the old character.
5737 * Also required when writing the right halve of a double-width
5738 * char over the left halve of an existing one. */
5739 if (has_mbyte && col + char_cells == endcol
5740 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005741 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005743 && (*mb_off2cells)(off_to, max_off_to) == 1
5744 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 clear_next = TRUE;
5746#endif
5747
5748 ScreenLines[off_to] = ScreenLines[off_from];
5749#ifdef FEAT_MBYTE
5750 if (enc_utf8)
5751 {
5752 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5753 if (ScreenLinesUC[off_from] != 0)
5754 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005755 int i;
5756
5757 for (i = 0; i < Screen_mco; ++i)
5758 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
5760 }
5761 if (char_cells == 2)
5762 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5763#endif
5764
5765#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005766 /* The bold trick makes a single column of pixels appear in the
5767 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 * character should be redrawn too. This happens for our own GUI
5769 * and for some xterms. */
5770 if (
5771# ifdef FEAT_GUI
5772 gui.in_use
5773# endif
5774# if defined(FEAT_GUI) && defined(UNIX)
5775 ||
5776# endif
5777# ifdef UNIX
5778 term_is_xterm
5779# endif
5780 )
5781 {
5782 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005783 if (hl > HL_ALL)
5784 hl = syn_attr2attr(hl);
5785 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 redraw_next = TRUE;
5787 }
5788#endif
5789 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5790#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005791 /* For simplicity set the attributes of second half of a
5792 * double-wide character equal to the first half. */
5793 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005795
5796 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 else
5799#endif
5800 screen_char(off_to, row, col + coloff);
5801 }
5802 else if ( p_wiv
5803#ifdef FEAT_GUI
5804 && !gui.in_use
5805#endif
5806 && col + coloff > 0)
5807 {
5808 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5809 {
5810 /*
5811 * Don't output stop-highlight when moving the cursor, it will
5812 * stop the highlighting when it should continue.
5813 */
5814 screen_attr = 0;
5815 }
5816 else if (screen_attr != 0)
5817 screen_stop_highlight();
5818 }
5819
5820 off_to += CHAR_CELLS;
5821 off_from += CHAR_CELLS;
5822 col += CHAR_CELLS;
5823 }
5824
5825#ifdef FEAT_MBYTE
5826 if (clear_next)
5827 {
5828 /* Clear the second half of a double-wide character of which the left
5829 * half was overwritten with a single-wide character. */
5830 ScreenLines[off_to] = ' ';
5831 if (enc_utf8)
5832 ScreenLinesUC[off_to] = 0;
5833 screen_char(off_to, row, col + coloff);
5834 }
5835#endif
5836
5837 if (clear_width > 0
5838#ifdef FEAT_RIGHTLEFT
5839 && !rlflag
5840#endif
5841 )
5842 {
5843#ifdef FEAT_GUI
5844 int startCol = col;
5845#endif
5846
5847 /* blank out the rest of the line */
5848 while (col < clear_width && ScreenLines[off_to] == ' '
5849 && ScreenAttrs[off_to] == 0
5850#ifdef FEAT_MBYTE
5851 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5852#endif
5853 )
5854 {
5855 ++off_to;
5856 ++col;
5857 }
5858 if (col < clear_width)
5859 {
5860#ifdef FEAT_GUI
5861 /*
5862 * In the GUI, clearing the rest of the line may leave pixels
5863 * behind if the first character cleared was bold. Some bold
5864 * fonts spill over the left. In this case we redraw the previous
5865 * character too. If we didn't skip any blanks above, then we
5866 * only redraw if the character wasn't already redrawn anyway.
5867 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005868 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {
5870 hl = ScreenAttrs[off_to];
5871 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005872 {
5873 int prev_cells = 1;
5874# ifdef FEAT_MBYTE
5875 if (enc_utf8)
5876 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5877 * that its width is 2. */
5878 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5879 else if (enc_dbcs != 0)
5880 {
5881 /* find previous character by counting from first
5882 * column and get its width. */
5883 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005884 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005885
5886 while (off < off_to)
5887 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005888 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005889 off += prev_cells;
5890 }
5891 }
5892
5893 if (enc_dbcs != 0 && prev_cells > 1)
5894 screen_char_2(off_to - prev_cells, row,
5895 col + coloff - prev_cells);
5896 else
5897# endif
5898 screen_char(off_to - prev_cells, row,
5899 col + coloff - prev_cells);
5900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902#endif
5903 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5904 ' ', ' ', 0);
5905#ifdef FEAT_VERTSPLIT
5906 off_to += clear_width - col;
5907 col = clear_width;
5908#endif
5909 }
5910 }
5911
5912 if (clear_width > 0)
5913 {
5914#ifdef FEAT_VERTSPLIT
5915 /* For a window that's left of another, draw the separator char. */
5916 if (col + coloff < Columns)
5917 {
5918 int c;
5919
5920 c = fillchar_vsep(&hl);
5921 if (ScreenLines[off_to] != c
5922# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005923 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5924 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925# endif
5926 || ScreenAttrs[off_to] != hl)
5927 {
5928 ScreenLines[off_to] = c;
5929 ScreenAttrs[off_to] = hl;
5930# ifdef FEAT_MBYTE
5931 if (enc_utf8)
5932 {
5933 if (c >= 0x80)
5934 {
5935 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005936 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938 else
5939 ScreenLinesUC[off_to] = 0;
5940 }
5941# endif
5942 screen_char(off_to, row, col + coloff);
5943 }
5944 }
5945 else
5946#endif
5947 LineWraps[row] = FALSE;
5948 }
5949}
5950
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005951#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005953 * Mirror text "str" for right-left displaying.
5954 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005956 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957rl_mirror(str)
5958 char_u *str;
5959{
5960 char_u *p1, *p2;
5961 int t;
5962
5963 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5964 {
5965 t = *p1;
5966 *p1 = *p2;
5967 *p2 = t;
5968 }
5969}
5970#endif
5971
5972#if defined(FEAT_WINDOWS) || defined(PROTO)
5973/*
5974 * mark all status lines for redraw; used after first :cd
5975 */
5976 void
5977status_redraw_all()
5978{
5979 win_T *wp;
5980
5981 for (wp = firstwin; wp; wp = wp->w_next)
5982 if (wp->w_status_height)
5983 {
5984 wp->w_redr_status = TRUE;
5985 redraw_later(VALID);
5986 }
5987}
5988
5989/*
5990 * mark all status lines of the current buffer for redraw
5991 */
5992 void
5993status_redraw_curbuf()
5994{
5995 win_T *wp;
5996
5997 for (wp = firstwin; wp; wp = wp->w_next)
5998 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5999 {
6000 wp->w_redr_status = TRUE;
6001 redraw_later(VALID);
6002 }
6003}
6004
6005/*
6006 * Redraw all status lines that need to be redrawn.
6007 */
6008 void
6009redraw_statuslines()
6010{
6011 win_T *wp;
6012
6013 for (wp = firstwin; wp; wp = wp->w_next)
6014 if (wp->w_redr_status)
6015 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006016 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006017 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018}
6019#endif
6020
6021#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6022/*
6023 * Redraw all status lines at the bottom of frame "frp".
6024 */
6025 void
6026win_redraw_last_status(frp)
6027 frame_T *frp;
6028{
6029 if (frp->fr_layout == FR_LEAF)
6030 frp->fr_win->w_redr_status = TRUE;
6031 else if (frp->fr_layout == FR_ROW)
6032 {
6033 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6034 win_redraw_last_status(frp);
6035 }
6036 else /* frp->fr_layout == FR_COL */
6037 {
6038 frp = frp->fr_child;
6039 while (frp->fr_next != NULL)
6040 frp = frp->fr_next;
6041 win_redraw_last_status(frp);
6042 }
6043}
6044#endif
6045
6046#ifdef FEAT_VERTSPLIT
6047/*
6048 * Draw the verticap separator right of window "wp" starting with line "row".
6049 */
6050 static void
6051draw_vsep_win(wp, row)
6052 win_T *wp;
6053 int row;
6054{
6055 int hl;
6056 int c;
6057
6058 if (wp->w_vsep_width)
6059 {
6060 /* draw the vertical separator right of this window */
6061 c = fillchar_vsep(&hl);
6062 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6063 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6064 c, ' ', hl);
6065 }
6066}
6067#endif
6068
6069#ifdef FEAT_WILDMENU
6070static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006071static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
6073/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006074 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 */
6076 static int
6077status_match_len(xp, s)
6078 expand_T *xp;
6079 char_u *s;
6080{
6081 int len = 0;
6082
6083#ifdef FEAT_MENU
6084 int emenu = (xp->xp_context == EXPAND_MENUS
6085 || xp->xp_context == EXPAND_MENUNAMES);
6086
6087 /* Check for menu separators - replace with '|'. */
6088 if (emenu && menu_is_separator(s))
6089 return 1;
6090#endif
6091
6092 while (*s != NUL)
6093 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006094 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006095 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006096 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 }
6098
6099 return len;
6100}
6101
6102/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006103 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006104 * These are backslashes used for escaping. Do show backslashes in help tags.
6105 */
6106 static int
6107skip_status_match_char(xp, s)
6108 expand_T *xp;
6109 char_u *s;
6110{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006111 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006112#ifdef FEAT_MENU
6113 || ((xp->xp_context == EXPAND_MENUS
6114 || xp->xp_context == EXPAND_MENUNAMES)
6115 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6116#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006117 )
6118 {
6119#ifndef BACKSLASH_IN_FILENAME
6120 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6121 return 2;
6122#endif
6123 return 1;
6124 }
6125 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006126}
6127
6128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 * Show wildchar matches in the status line.
6130 * Show at least the "match" item.
6131 * We start at item 'first_match' in the list and show all matches that fit.
6132 *
6133 * If inversion is possible we use it. Else '=' characters are used.
6134 */
6135 void
6136win_redr_status_matches(xp, num_matches, matches, match, showtail)
6137 expand_T *xp;
6138 int num_matches;
6139 char_u **matches; /* list of matches */
6140 int match;
6141 int showtail;
6142{
6143#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6144 int row;
6145 char_u *buf;
6146 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006147 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 int fillchar;
6149 int attr;
6150 int i;
6151 int highlight = TRUE;
6152 char_u *selstart = NULL;
6153 int selstart_col = 0;
6154 char_u *selend = NULL;
6155 static int first_match = 0;
6156 int add_left = FALSE;
6157 char_u *s;
6158#ifdef FEAT_MENU
6159 int emenu;
6160#endif
6161#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6162 int l;
6163#endif
6164
6165 if (matches == NULL) /* interrupted completion? */
6166 return;
6167
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006168#ifdef FEAT_MBYTE
6169 if (has_mbyte)
6170 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6171 else
6172#endif
6173 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (buf == NULL)
6175 return;
6176
6177 if (match == -1) /* don't show match but original text */
6178 {
6179 match = 0;
6180 highlight = FALSE;
6181 }
6182 /* count 1 for the ending ">" */
6183 clen = status_match_len(xp, L_MATCH(match)) + 3;
6184 if (match == 0)
6185 first_match = 0;
6186 else if (match < first_match)
6187 {
6188 /* jumping left, as far as we can go */
6189 first_match = match;
6190 add_left = TRUE;
6191 }
6192 else
6193 {
6194 /* check if match fits on the screen */
6195 for (i = first_match; i < match; ++i)
6196 clen += status_match_len(xp, L_MATCH(i)) + 2;
6197 if (first_match > 0)
6198 clen += 2;
6199 /* jumping right, put match at the left */
6200 if ((long)clen > Columns)
6201 {
6202 first_match = match;
6203 /* if showing the last match, we can add some on the left */
6204 clen = 2;
6205 for (i = match; i < num_matches; ++i)
6206 {
6207 clen += status_match_len(xp, L_MATCH(i)) + 2;
6208 if ((long)clen >= Columns)
6209 break;
6210 }
6211 if (i == num_matches)
6212 add_left = TRUE;
6213 }
6214 }
6215 if (add_left)
6216 while (first_match > 0)
6217 {
6218 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6219 if ((long)clen >= Columns)
6220 break;
6221 --first_match;
6222 }
6223
6224 fillchar = fillchar_status(&attr, TRUE);
6225
6226 if (first_match == 0)
6227 {
6228 *buf = NUL;
6229 len = 0;
6230 }
6231 else
6232 {
6233 STRCPY(buf, "< ");
6234 len = 2;
6235 }
6236 clen = len;
6237
6238 i = first_match;
6239 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6240 {
6241 if (i == match)
6242 {
6243 selstart = buf + len;
6244 selstart_col = clen;
6245 }
6246
6247 s = L_MATCH(i);
6248 /* Check for menu separators - replace with '|' */
6249#ifdef FEAT_MENU
6250 emenu = (xp->xp_context == EXPAND_MENUS
6251 || xp->xp_context == EXPAND_MENUNAMES);
6252 if (emenu && menu_is_separator(s))
6253 {
6254 STRCPY(buf + len, transchar('|'));
6255 l = (int)STRLEN(buf + len);
6256 len += l;
6257 clen += l;
6258 }
6259 else
6260#endif
6261 for ( ; *s != NUL; ++s)
6262 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006263 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 clen += ptr2cells(s);
6265#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006266 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
6268 STRNCPY(buf + len, s, l);
6269 s += l - 1;
6270 len += l;
6271 }
6272 else
6273#endif
6274 {
6275 STRCPY(buf + len, transchar_byte(*s));
6276 len += (int)STRLEN(buf + len);
6277 }
6278 }
6279 if (i == match)
6280 selend = buf + len;
6281
6282 *(buf + len++) = ' ';
6283 *(buf + len++) = ' ';
6284 clen += 2;
6285 if (++i == num_matches)
6286 break;
6287 }
6288
6289 if (i != num_matches)
6290 {
6291 *(buf + len++) = '>';
6292 ++clen;
6293 }
6294
6295 buf[len] = NUL;
6296
6297 row = cmdline_row - 1;
6298 if (row >= 0)
6299 {
6300 if (wild_menu_showing == 0)
6301 {
6302 if (msg_scrolled > 0)
6303 {
6304 /* Put the wildmenu just above the command line. If there is
6305 * no room, scroll the screen one line up. */
6306 if (cmdline_row == Rows - 1)
6307 {
6308 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6309 ++msg_scrolled;
6310 }
6311 else
6312 {
6313 ++cmdline_row;
6314 ++row;
6315 }
6316 wild_menu_showing = WM_SCROLLED;
6317 }
6318 else
6319 {
6320 /* Create status line if needed by setting 'laststatus' to 2.
6321 * Set 'winminheight' to zero to avoid that the window is
6322 * resized. */
6323 if (lastwin->w_status_height == 0)
6324 {
6325 save_p_ls = p_ls;
6326 save_p_wmh = p_wmh;
6327 p_ls = 2;
6328 p_wmh = 0;
6329 last_status(FALSE);
6330 }
6331 wild_menu_showing = WM_SHOWN;
6332 }
6333 }
6334
6335 screen_puts(buf, row, 0, attr);
6336 if (selstart != NULL && highlight)
6337 {
6338 *selend = NUL;
6339 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6340 }
6341
6342 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6343 }
6344
6345#ifdef FEAT_VERTSPLIT
6346 win_redraw_last_status(topframe);
6347#else
6348 lastwin->w_redr_status = TRUE;
6349#endif
6350 vim_free(buf);
6351}
6352#endif
6353
6354#if defined(FEAT_WINDOWS) || defined(PROTO)
6355/*
6356 * Redraw the status line of window wp.
6357 *
6358 * If inversion is possible we use it. Else '=' characters are used.
6359 */
6360 void
6361win_redr_status(wp)
6362 win_T *wp;
6363{
6364 int row;
6365 char_u *p;
6366 int len;
6367 int fillchar;
6368 int attr;
6369 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006370 static int busy = FALSE;
6371
6372 /* It's possible to get here recursively when 'statusline' (indirectly)
6373 * invokes ":redrawstatus". Simply ignore the call then. */
6374 if (busy)
6375 return;
6376 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 wp->w_redr_status = FALSE;
6379 if (wp->w_status_height == 0)
6380 {
6381 /* no status line, can only be last window */
6382 redraw_cmdline = TRUE;
6383 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006384 else if (!redrawing()
6385#ifdef FEAT_INS_EXPAND
6386 /* don't update status line when popup menu is visible and may be
6387 * drawn over it */
6388 || pum_visible()
6389#endif
6390 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 {
6392 /* Don't redraw right now, do it later. */
6393 wp->w_redr_status = TRUE;
6394 }
6395#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006396 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 {
6398 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006399 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 }
6401#endif
6402 else
6403 {
6404 fillchar = fillchar_status(&attr, wp == curwin);
6405
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006406 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 p = NameBuff;
6408 len = (int)STRLEN(p);
6409
6410 if (wp->w_buffer->b_help
6411#ifdef FEAT_QUICKFIX
6412 || wp->w_p_pvw
6413#endif
6414 || bufIsChanged(wp->w_buffer)
6415 || wp->w_buffer->b_p_ro)
6416 *(p + len++) = ' ';
6417 if (wp->w_buffer->b_help)
6418 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006419 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 len += (int)STRLEN(p + len);
6421 }
6422#ifdef FEAT_QUICKFIX
6423 if (wp->w_p_pvw)
6424 {
6425 STRCPY(p + len, _("[Preview]"));
6426 len += (int)STRLEN(p + len);
6427 }
6428#endif
6429 if (bufIsChanged(wp->w_buffer))
6430 {
6431 STRCPY(p + len, "[+]");
6432 len += 3;
6433 }
6434 if (wp->w_buffer->b_p_ro)
6435 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006436 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 len += 4;
6438 }
6439
6440#ifndef FEAT_VERTSPLIT
6441 this_ru_col = ru_col;
6442 if (this_ru_col < (Columns + 1) / 2)
6443 this_ru_col = (Columns + 1) / 2;
6444#else
6445 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6446 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6447 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6448 if (this_ru_col <= 1)
6449 {
6450 p = (char_u *)"<"; /* No room for file name! */
6451 len = 1;
6452 }
6453 else
6454#endif
6455#ifdef FEAT_MBYTE
6456 if (has_mbyte)
6457 {
6458 int clen = 0, i;
6459
6460 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006461 clen = mb_string2cells(p, -1);
6462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 /* Find first character that will fit.
6464 * Going from start to end is much faster for DBCS. */
6465 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006466 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 clen -= (*mb_ptr2cells)(p + i);
6468 len = clen;
6469 if (i > 0)
6470 {
6471 p = p + i - 1;
6472 *p = '<';
6473 ++len;
6474 }
6475
6476 }
6477 else
6478#endif
6479 if (len > this_ru_col - 1)
6480 {
6481 p += len - (this_ru_col - 1);
6482 *p = '<';
6483 len = this_ru_col - 1;
6484 }
6485
6486 row = W_WINROW(wp) + wp->w_height;
6487 screen_puts(p, row, W_WINCOL(wp), attr);
6488 screen_fill(row, row + 1, len + W_WINCOL(wp),
6489 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6490
6491 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6492 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6493 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6494 - 1 + W_WINCOL(wp)), attr);
6495
6496#ifdef FEAT_CMDL_INFO
6497 win_redr_ruler(wp, TRUE);
6498#endif
6499 }
6500
6501#ifdef FEAT_VERTSPLIT
6502 /*
6503 * May need to draw the character below the vertical separator.
6504 */
6505 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6506 {
6507 if (stl_connected(wp))
6508 fillchar = fillchar_status(&attr, wp == curwin);
6509 else
6510 fillchar = fillchar_vsep(&attr);
6511 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6512 attr);
6513 }
6514#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006515 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516}
6517
Bram Moolenaar238a5642006-02-21 22:12:05 +00006518#ifdef FEAT_STL_OPT
6519/*
6520 * Redraw the status line according to 'statusline' and take care of any
6521 * errors encountered.
6522 */
6523 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006524redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006525 win_T *wp;
6526{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006527 static int entered = FALSE;
6528 int save_called_emsg = called_emsg;
6529
6530 /* When called recursively return. This can happen when the statusline
6531 * contains an expression that triggers a redraw. */
6532 if (entered)
6533 return;
6534 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006535
6536 called_emsg = FALSE;
6537 win_redr_custom(wp, FALSE);
6538 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006539 {
6540 /* When there is an error disable the statusline, otherwise the
6541 * display is messed up with errors and a redraw triggers the problem
6542 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006543 set_string_option_direct((char_u *)"statusline", -1,
6544 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006545 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006546 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006547 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006548 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006549}
6550#endif
6551
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552# ifdef FEAT_VERTSPLIT
6553/*
6554 * Return TRUE if the status line of window "wp" is connected to the status
6555 * line of the window right of it. If not, then it's a vertical separator.
6556 * Only call if (wp->w_vsep_width != 0).
6557 */
6558 int
6559stl_connected(wp)
6560 win_T *wp;
6561{
6562 frame_T *fr;
6563
6564 fr = wp->w_frame;
6565 while (fr->fr_parent != NULL)
6566 {
6567 if (fr->fr_parent->fr_layout == FR_COL)
6568 {
6569 if (fr->fr_next != NULL)
6570 break;
6571 }
6572 else
6573 {
6574 if (fr->fr_next != NULL)
6575 return TRUE;
6576 }
6577 fr = fr->fr_parent;
6578 }
6579 return FALSE;
6580}
6581# endif
6582
6583#endif /* FEAT_WINDOWS */
6584
6585#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6586/*
6587 * Get the value to show for the language mappings, active 'keymap'.
6588 */
6589 int
6590get_keymap_str(wp, buf, len)
6591 win_T *wp;
6592 char_u *buf; /* buffer for the result */
6593 int len; /* length of buffer */
6594{
6595 char_u *p;
6596
6597 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6598 return FALSE;
6599
6600 {
6601#ifdef FEAT_EVAL
6602 buf_T *old_curbuf = curbuf;
6603 win_T *old_curwin = curwin;
6604 char_u *s;
6605
6606 curbuf = wp->w_buffer;
6607 curwin = wp;
6608 STRCPY(buf, "b:keymap_name"); /* must be writable */
6609 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006610 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 --emsg_skip;
6612 curbuf = old_curbuf;
6613 curwin = old_curwin;
6614 if (p == NULL || *p == NUL)
6615#endif
6616 {
6617#ifdef FEAT_KEYMAP
6618 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6619 p = wp->w_buffer->b_p_keymap;
6620 else
6621#endif
6622 p = (char_u *)"lang";
6623 }
6624 if ((int)(STRLEN(p) + 3) < len)
6625 sprintf((char *)buf, "<%s>", p);
6626 else
6627 buf[0] = NUL;
6628#ifdef FEAT_EVAL
6629 vim_free(s);
6630#endif
6631 }
6632 return buf[0] != NUL;
6633}
6634#endif
6635
6636#if defined(FEAT_STL_OPT) || defined(PROTO)
6637/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006638 * Redraw the status line or ruler of window "wp".
6639 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 */
6641 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006642win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006644 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646 int attr;
6647 int curattr;
6648 int row;
6649 int col = 0;
6650 int maxwidth;
6651 int width;
6652 int n;
6653 int len;
6654 int fillchar;
6655 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006656 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006658 struct stl_hlrec hltab[STL_MAX_ITEM];
6659 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006660 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006661 win_T *ewp;
6662 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663
6664 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006665 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006667 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006668 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006669 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006670 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006671 attr = hl_attr(HLF_TPF);
6672 maxwidth = Columns;
6673# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006674 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006675# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006677 else
6678 {
6679 row = W_WINROW(wp) + wp->w_height;
6680 fillchar = fillchar_status(&attr, wp == curwin);
6681 maxwidth = W_WIDTH(wp);
6682
6683 if (draw_ruler)
6684 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006685 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006686 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006687 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006688 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006689 if (*++stl == '-')
6690 stl++;
6691 if (atoi((char *)stl))
6692 while (VIM_ISDIGIT(*stl))
6693 stl++;
6694 if (*stl++ != '(')
6695 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006696 }
6697#ifdef FEAT_VERTSPLIT
6698 col = ru_col - (Columns - W_WIDTH(wp));
6699 if (col < (W_WIDTH(wp) + 1) / 2)
6700 col = (W_WIDTH(wp) + 1) / 2;
6701#else
6702 col = ru_col;
6703 if (col > (Columns + 1) / 2)
6704 col = (Columns + 1) / 2;
6705#endif
6706 maxwidth = W_WIDTH(wp) - col;
6707#ifdef FEAT_WINDOWS
6708 if (!wp->w_status_height)
6709#endif
6710 {
6711 row = Rows - 1;
6712 --maxwidth; /* writing in last column may cause scrolling */
6713 fillchar = ' ';
6714 attr = 0;
6715 }
6716
6717# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006718 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006719# endif
6720 }
6721 else
6722 {
6723 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006724 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006725 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006726 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006727# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006728 use_sandbox = was_set_insecurely((char_u *)"statusline",
6729 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006730# endif
6731 }
6732
6733#ifdef FEAT_VERTSPLIT
6734 col += W_WINCOL(wp);
6735#endif
6736 }
6737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 if (maxwidth <= 0)
6739 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740
Bram Moolenaar61452852011-02-01 18:01:11 +01006741 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6742 * the cursor away and back. */
6743 ewp = wp == NULL ? curwin : wp;
6744 p_crb_save = ewp->w_p_crb;
6745 ewp->w_p_crb = FALSE;
6746
Bram Moolenaar362f3562009-11-03 16:20:34 +00006747 /* Make a copy, because the statusline may include a function call that
6748 * might change the option value and free the memory. */
6749 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006750 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006751 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006752 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006753 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006754 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006756 /* Make all characters printable. */
6757 p = transstr(buf);
6758 if (p != NULL)
6759 {
6760 vim_strncpy(buf, p, sizeof(buf) - 1);
6761 vim_free(p);
6762 }
6763
6764 /* fill up with "fillchar" */
6765 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006766 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {
6768#ifdef FEAT_MBYTE
6769 len += (*mb_char2bytes)(fillchar, buf + len);
6770#else
6771 buf[len++] = fillchar;
6772#endif
6773 ++width;
6774 }
6775 buf[len] = NUL;
6776
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006777 /*
6778 * Draw each snippet with the specified highlighting.
6779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 curattr = attr;
6781 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006782 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006784 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 screen_puts_len(p, len, row, col, curattr);
6786 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006787 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006789 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006791 else if (hltab[n].userhl < 0)
6792 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006794 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006795 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796#endif
6797 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006798 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 }
6800 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006801
6802 if (wp == NULL)
6803 {
6804 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6805 col = 0;
6806 len = 0;
6807 p = buf;
6808 fillchar = 0;
6809 for (n = 0; tabtab[n].start != NULL; n++)
6810 {
6811 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6812 while (col < len)
6813 TabPageIdxs[col++] = fillchar;
6814 p = tabtab[n].start;
6815 fillchar = tabtab[n].userhl;
6816 }
6817 while (col < Columns)
6818 TabPageIdxs[col++] = fillchar;
6819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820}
6821
6822#endif /* FEAT_STL_OPT */
6823
6824/*
6825 * Output a single character directly to the screen and update ScreenLines.
6826 */
6827 void
6828screen_putchar(c, row, col, attr)
6829 int c;
6830 int row, col;
6831 int attr;
6832{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 char_u buf[MB_MAXBYTES + 1];
6834
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006835#ifdef FEAT_MBYTE
6836 if (has_mbyte)
6837 buf[(*mb_char2bytes)(c, buf)] = NUL;
6838 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006840 {
6841 buf[0] = c;
6842 buf[1] = NUL;
6843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 screen_puts(buf, row, col, attr);
6845}
6846
6847/*
6848 * Get a single character directly from ScreenLines into "bytes[]".
6849 * Also return its attribute in *attrp;
6850 */
6851 void
6852screen_getbytes(row, col, bytes, attrp)
6853 int row, col;
6854 char_u *bytes;
6855 int *attrp;
6856{
6857 unsigned off;
6858
6859 /* safety check */
6860 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6861 {
6862 off = LineOffset[row] + col;
6863 *attrp = ScreenAttrs[off];
6864 bytes[0] = ScreenLines[off];
6865 bytes[1] = NUL;
6866
6867#ifdef FEAT_MBYTE
6868 if (enc_utf8 && ScreenLinesUC[off] != 0)
6869 bytes[utfc_char2bytes(off, bytes)] = NUL;
6870 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6871 {
6872 bytes[0] = ScreenLines[off];
6873 bytes[1] = ScreenLines2[off];
6874 bytes[2] = NUL;
6875 }
6876 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6877 {
6878 bytes[1] = ScreenLines[off + 1];
6879 bytes[2] = NUL;
6880 }
6881#endif
6882 }
6883}
6884
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006885#ifdef FEAT_MBYTE
6886static int screen_comp_differs __ARGS((int, int*));
6887
6888/*
6889 * Return TRUE if composing characters for screen posn "off" differs from
6890 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006891 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006892 */
6893 static int
6894screen_comp_differs(off, u8cc)
6895 int off;
6896 int *u8cc;
6897{
6898 int i;
6899
6900 for (i = 0; i < Screen_mco; ++i)
6901 {
6902 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6903 return TRUE;
6904 if (u8cc[i] == 0)
6905 break;
6906 }
6907 return FALSE;
6908}
6909#endif
6910
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911/*
6912 * Put string '*text' on the screen at position 'row' and 'col', with
6913 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6914 * Note: only outputs within one row, message is truncated at screen boundary!
6915 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6916 */
6917 void
6918screen_puts(text, row, col, attr)
6919 char_u *text;
6920 int row;
6921 int col;
6922 int attr;
6923{
6924 screen_puts_len(text, -1, row, col, attr);
6925}
6926
6927/*
6928 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6929 * a NUL.
6930 */
6931 void
6932screen_puts_len(text, len, row, col, attr)
6933 char_u *text;
6934 int len;
6935 int row;
6936 int col;
6937 int attr;
6938{
6939 unsigned off;
6940 char_u *ptr = text;
6941 int c;
6942#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006943 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 int mbyte_blen = 1;
6945 int mbyte_cells = 1;
6946 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006947 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 int clear_next_cell = FALSE;
6949# ifdef FEAT_ARABIC
6950 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006951 int pc, nc, nc1;
6952 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953# endif
6954#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006955#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6956 int force_redraw_this;
6957 int force_redraw_next = FALSE;
6958#endif
6959 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
6961 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6962 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006963 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
Bram Moolenaarc236c162008-07-13 17:41:49 +00006965#ifdef FEAT_MBYTE
6966 /* When drawing over the right halve of a double-wide char clear out the
6967 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006968 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006969# ifdef FEAT_GUI
6970 && !gui.in_use
6971# endif
6972 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006973 {
6974 ScreenLines[off - 1] = ' ';
6975 ScreenAttrs[off - 1] = 0;
6976 if (enc_utf8)
6977 {
6978 ScreenLinesUC[off - 1] = 0;
6979 ScreenLinesC[0][off - 1] = 0;
6980 }
6981 /* redraw the previous cell, make it empty */
6982 screen_char(off - 1, row, col - 1);
6983 /* force the cell at "col" to be redrawn */
6984 force_redraw_next = TRUE;
6985 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006986#endif
6987
Bram Moolenaar367329b2007-08-30 11:53:22 +00006988#ifdef FEAT_MBYTE
6989 max_off = LineOffset[row] + screen_Columns;
6990#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006991 while (col < screen_Columns
6992 && (len < 0 || (int)(ptr - text) < len)
6993 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 {
6995 c = *ptr;
6996#ifdef FEAT_MBYTE
6997 /* check if this is the first byte of a multibyte */
6998 if (has_mbyte)
6999 {
7000 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007001 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007003 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7005 mbyte_cells = 1;
7006 else if (enc_dbcs != 0)
7007 mbyte_cells = mbyte_blen;
7008 else /* enc_utf8 */
7009 {
7010 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007011 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 (int)((text + len) - ptr));
7013 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007014 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007016# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 /* Non-BMP character: display as ? or fullwidth ?. */
7018 if (u8c >= 0x10000)
7019 {
7020 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7021 if (attr == 0)
7022 attr = hl_attr(HLF_8);
7023 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025# ifdef FEAT_ARABIC
7026 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7027 {
7028 /* Do Arabic shaping. */
7029 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7030 {
7031 /* Past end of string to be displayed. */
7032 nc = NUL;
7033 nc1 = NUL;
7034 }
7035 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007036 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007037 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7038 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007039 nc1 = pcc[0];
7040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 pc = prev_c;
7042 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007043 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 }
7045 else
7046 prev_c = u8c;
7047# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007048 if (col + mbyte_cells > screen_Columns)
7049 {
7050 /* Only 1 cell left, but character requires 2 cells:
7051 * display a '>' in the last column to avoid wrapping. */
7052 c = '>';
7053 mbyte_cells = 1;
7054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 }
7056 }
7057#endif
7058
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007059#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7060 force_redraw_this = force_redraw_next;
7061 force_redraw_next = FALSE;
7062#endif
7063
7064 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065#ifdef FEAT_MBYTE
7066 || (mbyte_cells == 2
7067 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7068 || (enc_dbcs == DBCS_JPNU
7069 && c == 0x8e
7070 && ScreenLines2[off] != ptr[1])
7071 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007072 && (ScreenLinesUC[off] !=
7073 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7074 || (ScreenLinesUC[off] != 0
7075 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076#endif
7077 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007078 || exmode_active;
7079
7080 if (need_redraw
7081#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7082 || force_redraw_this
7083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 )
7085 {
7086#if defined(FEAT_GUI) || defined(UNIX)
7087 /* The bold trick makes a single row of pixels appear in the next
7088 * character. When a bold character is removed, the next
7089 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007090 * and for some xterms. */
7091 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092# ifdef FEAT_GUI
7093 gui.in_use
7094# endif
7095# if defined(FEAT_GUI) && defined(UNIX)
7096 ||
7097# endif
7098# ifdef UNIX
7099 term_is_xterm
7100# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007101 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007103 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007105 if (n > HL_ALL)
7106 n = syn_attr2attr(n);
7107 if (n & HL_BOLD)
7108 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 }
7110#endif
7111#ifdef FEAT_MBYTE
7112 /* When at the end of the text and overwriting a two-cell
7113 * character with a one-cell character, need to clear the next
7114 * cell. Also when overwriting the left halve of a two-cell char
7115 * with the right halve of a two-cell char. Do this only once
7116 * (mb_off2cells() may return 2 on the right halve). */
7117 if (clear_next_cell)
7118 clear_next_cell = FALSE;
7119 else if (has_mbyte
7120 && (len < 0 ? ptr[mbyte_blen] == NUL
7121 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007122 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007124 && (*mb_off2cells)(off, max_off) == 1
7125 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 clear_next_cell = TRUE;
7127
7128 /* Make sure we never leave a second byte of a double-byte behind,
7129 * it confuses mb_off2cells(). */
7130 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007131 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007133 && (*mb_off2cells)(off, max_off) == 1
7134 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 ScreenLines[off + mbyte_blen] = 0;
7136#endif
7137 ScreenLines[off] = c;
7138 ScreenAttrs[off] = attr;
7139#ifdef FEAT_MBYTE
7140 if (enc_utf8)
7141 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007142 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 ScreenLinesUC[off] = 0;
7144 else
7145 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007146 int i;
7147
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007149 for (i = 0; i < Screen_mco; ++i)
7150 {
7151 ScreenLinesC[i][off] = u8cc[i];
7152 if (u8cc[i] == 0)
7153 break;
7154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 }
7156 if (mbyte_cells == 2)
7157 {
7158 ScreenLines[off + 1] = 0;
7159 ScreenAttrs[off + 1] = attr;
7160 }
7161 screen_char(off, row, col);
7162 }
7163 else if (mbyte_cells == 2)
7164 {
7165 ScreenLines[off + 1] = ptr[1];
7166 ScreenAttrs[off + 1] = attr;
7167 screen_char_2(off, row, col);
7168 }
7169 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7170 {
7171 ScreenLines2[off] = ptr[1];
7172 screen_char(off, row, col);
7173 }
7174 else
7175#endif
7176 screen_char(off, row, col);
7177 }
7178#ifdef FEAT_MBYTE
7179 if (has_mbyte)
7180 {
7181 off += mbyte_cells;
7182 col += mbyte_cells;
7183 ptr += mbyte_blen;
7184 if (clear_next_cell)
7185 ptr = (char_u *)" ";
7186 }
7187 else
7188#endif
7189 {
7190 ++off;
7191 ++col;
7192 ++ptr;
7193 }
7194 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007195
7196#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7197 /* If we detected the next character needs to be redrawn, but the text
7198 * doesn't extend up to there, update the character here. */
7199 if (force_redraw_next && col < screen_Columns)
7200 {
7201# ifdef FEAT_MBYTE
7202 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7203 screen_char_2(off, row, col);
7204 else
7205# endif
7206 screen_char(off, row, col);
7207 }
7208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209}
7210
7211#ifdef FEAT_SEARCH_EXTRA
7212/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007213 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 */
7215 static void
7216start_search_hl()
7217{
7218 if (p_hls && !no_hlsearch)
7219 {
7220 last_pat_prog(&search_hl.rm);
7221 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007222# ifdef FEAT_RELTIME
7223 /* Set the time limit to 'redrawtime'. */
7224 profile_setlimit(p_rdt, &search_hl.tm);
7225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
7227}
7228
7229/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007230 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 */
7232 static void
7233end_search_hl()
7234{
7235 if (search_hl.rm.regprog != NULL)
7236 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007237 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 search_hl.rm.regprog = NULL;
7239 }
7240}
7241
7242/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007243 * Init for calling prepare_search_hl().
7244 */
7245 static void
7246init_search_hl(wp)
7247 win_T *wp;
7248{
7249 matchitem_T *cur;
7250
7251 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7252 * match */
7253 cur = wp->w_match_head;
7254 while (cur != NULL)
7255 {
7256 cur->hl.rm = cur->match;
7257 if (cur->hlg_id == 0)
7258 cur->hl.attr = 0;
7259 else
7260 cur->hl.attr = syn_id2attr(cur->hlg_id);
7261 cur->hl.buf = wp->w_buffer;
7262 cur->hl.lnum = 0;
7263 cur->hl.first_lnum = 0;
7264# ifdef FEAT_RELTIME
7265 /* Set the time limit to 'redrawtime'. */
7266 profile_setlimit(p_rdt, &(cur->hl.tm));
7267# endif
7268 cur = cur->next;
7269 }
7270 search_hl.buf = wp->w_buffer;
7271 search_hl.lnum = 0;
7272 search_hl.first_lnum = 0;
7273 /* time limit is set at the toplevel, for all windows */
7274}
7275
7276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 * Advance to the match in window "wp" line "lnum" or past it.
7278 */
7279 static void
7280prepare_search_hl(wp, lnum)
7281 win_T *wp;
7282 linenr_T lnum;
7283{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007284 matchitem_T *cur; /* points to the match list */
7285 match_T *shl; /* points to search_hl or a match */
7286 int shl_flag; /* flag to indicate whether search_hl
7287 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 int n;
7289
7290 /*
7291 * When using a multi-line pattern, start searching at the top
7292 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007293 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007295 cur = wp->w_match_head;
7296 shl_flag = FALSE;
7297 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007299 if (shl_flag == FALSE)
7300 {
7301 shl = &search_hl;
7302 shl_flag = TRUE;
7303 }
7304 else
7305 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 if (shl->rm.regprog != NULL
7307 && shl->lnum == 0
7308 && re_multiline(shl->rm.regprog))
7309 {
7310 if (shl->first_lnum == 0)
7311 {
7312# ifdef FEAT_FOLDING
7313 for (shl->first_lnum = lnum;
7314 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7315 if (hasFoldingWin(wp, shl->first_lnum - 1,
7316 NULL, NULL, TRUE, NULL))
7317 break;
7318# else
7319 shl->first_lnum = wp->w_topline;
7320# endif
7321 }
7322 n = 0;
7323 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7324 {
7325 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7326 if (shl->lnum != 0)
7327 {
7328 shl->first_lnum = shl->lnum
7329 + shl->rm.endpos[0].lnum
7330 - shl->rm.startpos[0].lnum;
7331 n = shl->rm.endpos[0].col;
7332 }
7333 else
7334 {
7335 ++shl->first_lnum;
7336 n = 0;
7337 }
7338 }
7339 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007340 if (shl != &search_hl && cur != NULL)
7341 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
7343}
7344
7345/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007346 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 * Uses shl->buf.
7348 * Sets shl->lnum and shl->rm contents.
7349 * Note: Assumes a previous match is always before "lnum", unless
7350 * shl->lnum is zero.
7351 * Careful: Any pointers for buffer lines will become invalid.
7352 */
7353 static void
7354next_search_hl(win, shl, lnum, mincol)
7355 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007356 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 linenr_T lnum;
7358 colnr_T mincol; /* minimal column for a match */
7359{
7360 linenr_T l;
7361 colnr_T matchcol;
7362 long nmatched;
7363
7364 if (shl->lnum != 0)
7365 {
7366 /* Check for three situations:
7367 * 1. If the "lnum" is below a previous match, start a new search.
7368 * 2. If the previous match includes "mincol", use it.
7369 * 3. Continue after the previous match.
7370 */
7371 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7372 if (lnum > l)
7373 shl->lnum = 0;
7374 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7375 return;
7376 }
7377
7378 /*
7379 * Repeat searching for a match until one is found that includes "mincol"
7380 * or none is found in this line.
7381 */
7382 called_emsg = FALSE;
7383 for (;;)
7384 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007385#ifdef FEAT_RELTIME
7386 /* Stop searching after passing the time limit. */
7387 if (profile_passed_limit(&(shl->tm)))
7388 {
7389 shl->lnum = 0; /* no match found in time */
7390 break;
7391 }
7392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 /* Three situations:
7394 * 1. No useful previous match: search from start of line.
7395 * 2. Not Vi compatible or empty match: continue at next character.
7396 * Break the loop if this is beyond the end of the line.
7397 * 3. Vi compatible searching: continue at end of previous match.
7398 */
7399 if (shl->lnum == 0)
7400 matchcol = 0;
7401 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7402 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007403 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007405 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007406
7407 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007408 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007409 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007411 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 shl->lnum = 0;
7413 break;
7414 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007415#ifdef FEAT_MBYTE
7416 if (has_mbyte)
7417 matchcol += mb_ptr2len(ml);
7418 else
7419#endif
7420 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 }
7422 else
7423 matchcol = shl->rm.endpos[0].col;
7424
7425 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007426 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7427#ifdef FEAT_RELTIME
7428 &(shl->tm)
7429#else
7430 NULL
7431#endif
7432 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007433 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
7435 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007436 if (shl == &search_hl)
7437 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007438 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar473de612013-06-08 18:19:48 +02007439 vim_regfree(shl->rm.regprog);
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007440 no_hlsearch = TRUE;
7441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007443 shl->lnum = 0;
7444 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 break;
7446 }
7447 if (nmatched == 0)
7448 {
7449 shl->lnum = 0; /* no match found */
7450 break;
7451 }
7452 if (shl->rm.startpos[0].lnum > 0
7453 || shl->rm.startpos[0].col >= mincol
7454 || nmatched > 1
7455 || shl->rm.endpos[0].col > mincol)
7456 {
7457 shl->lnum += shl->rm.startpos[0].lnum;
7458 break; /* useful match found */
7459 }
7460 }
7461}
7462#endif
7463
7464 static void
7465screen_start_highlight(attr)
7466 int attr;
7467{
7468 attrentry_T *aep = NULL;
7469
7470 screen_attr = attr;
7471 if (full_screen
7472#ifdef WIN3264
7473 && termcap_active
7474#endif
7475 )
7476 {
7477#ifdef FEAT_GUI
7478 if (gui.in_use)
7479 {
7480 char buf[20];
7481
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007482 /* The GUI handles this internally. */
7483 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 OUT_STR(buf);
7485 }
7486 else
7487#endif
7488 {
7489 if (attr > HL_ALL) /* special HL attr. */
7490 {
7491 if (t_colors > 1)
7492 aep = syn_cterm_attr2entry(attr);
7493 else
7494 aep = syn_term_attr2entry(attr);
7495 if (aep == NULL) /* did ":syntax clear" */
7496 attr = 0;
7497 else
7498 attr = aep->ae_attr;
7499 }
7500 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7501 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007502 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7503 && cterm_normal_fg_bold)
7504 /* If the Normal FG color has BOLD attribute and the new HL
7505 * has a FG color defined, clear BOLD. */
7506 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7508 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007509 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7510 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 out_str(T_US);
7512 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7513 out_str(T_CZH);
7514 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7515 out_str(T_MR);
7516
7517 /*
7518 * Output the color or start string after bold etc., in case the
7519 * bold etc. override the color setting.
7520 */
7521 if (aep != NULL)
7522 {
7523 if (t_colors > 1)
7524 {
7525 if (aep->ae_u.cterm.fg_color)
7526 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7527 if (aep->ae_u.cterm.bg_color)
7528 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7529 }
7530 else
7531 {
7532 if (aep->ae_u.term.start != NULL)
7533 out_str(aep->ae_u.term.start);
7534 }
7535 }
7536 }
7537 }
7538}
7539
7540 void
7541screen_stop_highlight()
7542{
7543 int do_ME = FALSE; /* output T_ME code */
7544
7545 if (screen_attr != 0
7546#ifdef WIN3264
7547 && termcap_active
7548#endif
7549 )
7550 {
7551#ifdef FEAT_GUI
7552 if (gui.in_use)
7553 {
7554 char buf[20];
7555
7556 /* use internal GUI code */
7557 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7558 OUT_STR(buf);
7559 }
7560 else
7561#endif
7562 {
7563 if (screen_attr > HL_ALL) /* special HL attr. */
7564 {
7565 attrentry_T *aep;
7566
7567 if (t_colors > 1)
7568 {
7569 /*
7570 * Assume that t_me restores the original colors!
7571 */
7572 aep = syn_cterm_attr2entry(screen_attr);
7573 if (aep != NULL && (aep->ae_u.cterm.fg_color
7574 || aep->ae_u.cterm.bg_color))
7575 do_ME = TRUE;
7576 }
7577 else
7578 {
7579 aep = syn_term_attr2entry(screen_attr);
7580 if (aep != NULL && aep->ae_u.term.stop != NULL)
7581 {
7582 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7583 do_ME = TRUE;
7584 else
7585 out_str(aep->ae_u.term.stop);
7586 }
7587 }
7588 if (aep == NULL) /* did ":syntax clear" */
7589 screen_attr = 0;
7590 else
7591 screen_attr = aep->ae_attr;
7592 }
7593
7594 /*
7595 * Often all ending-codes are equal to T_ME. Avoid outputting the
7596 * same sequence several times.
7597 */
7598 if (screen_attr & HL_STANDOUT)
7599 {
7600 if (STRCMP(T_SE, T_ME) == 0)
7601 do_ME = TRUE;
7602 else
7603 out_str(T_SE);
7604 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007605 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {
7607 if (STRCMP(T_UE, T_ME) == 0)
7608 do_ME = TRUE;
7609 else
7610 out_str(T_UE);
7611 }
7612 if (screen_attr & HL_ITALIC)
7613 {
7614 if (STRCMP(T_CZR, T_ME) == 0)
7615 do_ME = TRUE;
7616 else
7617 out_str(T_CZR);
7618 }
7619 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7620 out_str(T_ME);
7621
7622 if (t_colors > 1)
7623 {
7624 /* set Normal cterm colors */
7625 if (cterm_normal_fg_color != 0)
7626 term_fg_color(cterm_normal_fg_color - 1);
7627 if (cterm_normal_bg_color != 0)
7628 term_bg_color(cterm_normal_bg_color - 1);
7629 if (cterm_normal_fg_bold)
7630 out_str(T_MD);
7631 }
7632 }
7633 }
7634 screen_attr = 0;
7635}
7636
7637/*
7638 * Reset the colors for a cterm. Used when leaving Vim.
7639 * The machine specific code may override this again.
7640 */
7641 void
7642reset_cterm_colors()
7643{
7644 if (t_colors > 1)
7645 {
7646 /* set Normal cterm colors */
7647 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7648 {
7649 out_str(T_OP);
7650 screen_attr = -1;
7651 }
7652 if (cterm_normal_fg_bold)
7653 {
7654 out_str(T_ME);
7655 screen_attr = -1;
7656 }
7657 }
7658}
7659
7660/*
7661 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7662 * using the attributes from ScreenAttrs["off"].
7663 */
7664 static void
7665screen_char(off, row, col)
7666 unsigned off;
7667 int row;
7668 int col;
7669{
7670 int attr;
7671
7672 /* Check for illegal values, just in case (could happen just after
7673 * resizing). */
7674 if (row >= screen_Rows || col >= screen_Columns)
7675 return;
7676
7677 /* Outputting the last character on the screen may scrollup the screen.
7678 * Don't to it! Mark the character invalid (update it when scrolled up) */
7679 if (row == screen_Rows - 1 && col == screen_Columns - 1
7680#ifdef FEAT_RIGHTLEFT
7681 /* account for first command-line character in rightleft mode */
7682 && !cmdmsg_rl
7683#endif
7684 )
7685 {
7686 ScreenAttrs[off] = (sattr_T)-1;
7687 return;
7688 }
7689
7690 /*
7691 * Stop highlighting first, so it's easier to move the cursor.
7692 */
7693#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7694 if (screen_char_attr != 0)
7695 attr = screen_char_attr;
7696 else
7697#endif
7698 attr = ScreenAttrs[off];
7699 if (screen_attr != attr)
7700 screen_stop_highlight();
7701
7702 windgoto(row, col);
7703
7704 if (screen_attr != attr)
7705 screen_start_highlight(attr);
7706
7707#ifdef FEAT_MBYTE
7708 if (enc_utf8 && ScreenLinesUC[off] != 0)
7709 {
7710 char_u buf[MB_MAXBYTES + 1];
7711
7712 /* Convert UTF-8 character to bytes and write it. */
7713
7714 buf[utfc_char2bytes(off, buf)] = NUL;
7715
7716 out_str(buf);
7717 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7718 ++screen_cur_col;
7719 }
7720 else
7721#endif
7722 {
7723#ifdef FEAT_MBYTE
7724 out_flush_check();
7725#endif
7726 out_char(ScreenLines[off]);
7727#ifdef FEAT_MBYTE
7728 /* double-byte character in single-width cell */
7729 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7730 out_char(ScreenLines2[off]);
7731#endif
7732 }
7733
7734 screen_cur_col++;
7735}
7736
7737#ifdef FEAT_MBYTE
7738
7739/*
7740 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7741 * on the screen at position 'row' and 'col'.
7742 * The attributes of the first byte is used for all. This is required to
7743 * output the two bytes of a double-byte character with nothing in between.
7744 */
7745 static void
7746screen_char_2(off, row, col)
7747 unsigned off;
7748 int row;
7749 int col;
7750{
7751 /* Check for illegal values (could be wrong when screen was resized). */
7752 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7753 return;
7754
7755 /* Outputting the last character on the screen may scrollup the screen.
7756 * Don't to it! Mark the character invalid (update it when scrolled up) */
7757 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7758 {
7759 ScreenAttrs[off] = (sattr_T)-1;
7760 return;
7761 }
7762
7763 /* Output the first byte normally (positions the cursor), then write the
7764 * second byte directly. */
7765 screen_char(off, row, col);
7766 out_char(ScreenLines[off + 1]);
7767 ++screen_cur_col;
7768}
7769#endif
7770
7771#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7772/*
7773 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7774 * This uses the contents of ScreenLines[] and doesn't change it.
7775 */
7776 void
7777screen_draw_rectangle(row, col, height, width, invert)
7778 int row;
7779 int col;
7780 int height;
7781 int width;
7782 int invert;
7783{
7784 int r, c;
7785 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007786#ifdef FEAT_MBYTE
7787 int max_off;
7788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007790 /* Can't use ScreenLines unless initialized */
7791 if (ScreenLines == NULL)
7792 return;
7793
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 if (invert)
7795 screen_char_attr = HL_INVERSE;
7796 for (r = row; r < row + height; ++r)
7797 {
7798 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007799#ifdef FEAT_MBYTE
7800 max_off = off + screen_Columns;
7801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 for (c = col; c < col + width; ++c)
7803 {
7804#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007805 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {
7807 screen_char_2(off + c, r, c);
7808 ++c;
7809 }
7810 else
7811#endif
7812 {
7813 screen_char(off + c, r, c);
7814#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007815 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 ++c;
7817#endif
7818 }
7819 }
7820 }
7821 screen_char_attr = 0;
7822}
7823#endif
7824
7825#ifdef FEAT_VERTSPLIT
7826/*
7827 * Redraw the characters for a vertically split window.
7828 */
7829 static void
7830redraw_block(row, end, wp)
7831 int row;
7832 int end;
7833 win_T *wp;
7834{
7835 int col;
7836 int width;
7837
7838# ifdef FEAT_CLIPBOARD
7839 clip_may_clear_selection(row, end - 1);
7840# endif
7841
7842 if (wp == NULL)
7843 {
7844 col = 0;
7845 width = Columns;
7846 }
7847 else
7848 {
7849 col = wp->w_wincol;
7850 width = wp->w_width;
7851 }
7852 screen_draw_rectangle(row, col, end - row, width, FALSE);
7853}
7854#endif
7855
7856/*
7857 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7858 * with character 'c1' in first column followed by 'c2' in the other columns.
7859 * Use attributes 'attr'.
7860 */
7861 void
7862screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7863 int start_row, end_row;
7864 int start_col, end_col;
7865 int c1, c2;
7866 int attr;
7867{
7868 int row;
7869 int col;
7870 int off;
7871 int end_off;
7872 int did_delete;
7873 int c;
7874 int norm_term;
7875#if defined(FEAT_GUI) || defined(UNIX)
7876 int force_next = FALSE;
7877#endif
7878
7879 if (end_row > screen_Rows) /* safety check */
7880 end_row = screen_Rows;
7881 if (end_col > screen_Columns) /* safety check */
7882 end_col = screen_Columns;
7883 if (ScreenLines == NULL
7884 || start_row >= end_row
7885 || start_col >= end_col) /* nothing to do */
7886 return;
7887
7888 /* it's a "normal" terminal when not in a GUI or cterm */
7889 norm_term = (
7890#ifdef FEAT_GUI
7891 !gui.in_use &&
7892#endif
7893 t_colors <= 1);
7894 for (row = start_row; row < end_row; ++row)
7895 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007896#ifdef FEAT_MBYTE
7897 if (has_mbyte
7898# ifdef FEAT_GUI
7899 && !gui.in_use
7900# endif
7901 )
7902 {
7903 /* When drawing over the right halve of a double-wide char clear
7904 * out the left halve. When drawing over the left halve of a
7905 * double wide-char clear out the right halve. Only needed in a
7906 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007907 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007908 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007909 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007910 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007911 }
7912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 /*
7914 * Try to use delete-line termcap code, when no attributes or in a
7915 * "normal" terminal, where a bold/italic space is just a
7916 * space.
7917 */
7918 did_delete = FALSE;
7919 if (c2 == ' '
7920 && end_col == Columns
7921 && can_clear(T_CE)
7922 && (attr == 0
7923 || (norm_term
7924 && attr <= HL_ALL
7925 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7926 {
7927 /*
7928 * check if we really need to clear something
7929 */
7930 col = start_col;
7931 if (c1 != ' ') /* don't clear first char */
7932 ++col;
7933
7934 off = LineOffset[row] + col;
7935 end_off = LineOffset[row] + end_col;
7936
7937 /* skip blanks (used often, keep it fast!) */
7938#ifdef FEAT_MBYTE
7939 if (enc_utf8)
7940 while (off < end_off && ScreenLines[off] == ' '
7941 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7942 ++off;
7943 else
7944#endif
7945 while (off < end_off && ScreenLines[off] == ' '
7946 && ScreenAttrs[off] == 0)
7947 ++off;
7948 if (off < end_off) /* something to be cleared */
7949 {
7950 col = off - LineOffset[row];
7951 screen_stop_highlight();
7952 term_windgoto(row, col);/* clear rest of this screen line */
7953 out_str(T_CE);
7954 screen_start(); /* don't know where cursor is now */
7955 col = end_col - col;
7956 while (col--) /* clear chars in ScreenLines */
7957 {
7958 ScreenLines[off] = ' ';
7959#ifdef FEAT_MBYTE
7960 if (enc_utf8)
7961 ScreenLinesUC[off] = 0;
7962#endif
7963 ScreenAttrs[off] = 0;
7964 ++off;
7965 }
7966 }
7967 did_delete = TRUE; /* the chars are cleared now */
7968 }
7969
7970 off = LineOffset[row] + start_col;
7971 c = c1;
7972 for (col = start_col; col < end_col; ++col)
7973 {
7974 if (ScreenLines[off] != c
7975#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007976 || (enc_utf8 && (int)ScreenLinesUC[off]
7977 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978#endif
7979 || ScreenAttrs[off] != attr
7980#if defined(FEAT_GUI) || defined(UNIX)
7981 || force_next
7982#endif
7983 )
7984 {
7985#if defined(FEAT_GUI) || defined(UNIX)
7986 /* The bold trick may make a single row of pixels appear in
7987 * the next character. When a bold character is removed, the
7988 * next character should be redrawn too. This happens for our
7989 * own GUI and for some xterms. */
7990 if (
7991# ifdef FEAT_GUI
7992 gui.in_use
7993# endif
7994# if defined(FEAT_GUI) && defined(UNIX)
7995 ||
7996# endif
7997# ifdef UNIX
7998 term_is_xterm
7999# endif
8000 )
8001 {
8002 if (ScreenLines[off] != ' '
8003 && (ScreenAttrs[off] > HL_ALL
8004 || ScreenAttrs[off] & HL_BOLD))
8005 force_next = TRUE;
8006 else
8007 force_next = FALSE;
8008 }
8009#endif
8010 ScreenLines[off] = c;
8011#ifdef FEAT_MBYTE
8012 if (enc_utf8)
8013 {
8014 if (c >= 0x80)
8015 {
8016 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008017 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 }
8019 else
8020 ScreenLinesUC[off] = 0;
8021 }
8022#endif
8023 ScreenAttrs[off] = attr;
8024 if (!did_delete || c != ' ')
8025 screen_char(off, row, col);
8026 }
8027 ++off;
8028 if (col == start_col)
8029 {
8030 if (did_delete)
8031 break;
8032 c = c2;
8033 }
8034 }
8035 if (end_col == Columns)
8036 LineWraps[row] = FALSE;
8037 if (row == Rows - 1) /* overwritten the command line */
8038 {
8039 redraw_cmdline = TRUE;
8040 if (c1 == ' ' && c2 == ' ')
8041 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008042 if (start_col == 0)
8043 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
8045 }
8046}
8047
8048/*
8049 * Check if there should be a delay. Used before clearing or redrawing the
8050 * screen or the command line.
8051 */
8052 void
8053check_for_delay(check_msg_scroll)
8054 int check_msg_scroll;
8055{
8056 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8057 && !did_wait_return
8058 && emsg_silent == 0)
8059 {
8060 out_flush();
8061 ui_delay(1000L, TRUE);
8062 emsg_on_display = FALSE;
8063 if (check_msg_scroll)
8064 msg_scroll = FALSE;
8065 }
8066}
8067
8068/*
8069 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008070 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 * Returns TRUE if there is a valid screen to write to.
8072 * Returns FALSE when starting up and screen not initialized yet.
8073 */
8074 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008075screen_valid(doclear)
8076 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008078 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 return (ScreenLines != NULL);
8080}
8081
8082/*
8083 * Resize the shell to Rows and Columns.
8084 * Allocate ScreenLines[] and associated items.
8085 *
8086 * There may be some time between setting Rows and Columns and (re)allocating
8087 * ScreenLines[]. This happens when starting up and when (manually) changing
8088 * the shell size. Always use screen_Rows and screen_Columns to access items
8089 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8090 * final size of the shell is needed.
8091 */
8092 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008093screenalloc(doclear)
8094 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095{
8096 int new_row, old_row;
8097#ifdef FEAT_GUI
8098 int old_Rows;
8099#endif
8100 win_T *wp;
8101 int outofmem = FALSE;
8102 int len;
8103 schar_T *new_ScreenLines;
8104#ifdef FEAT_MBYTE
8105 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008106 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008108 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109#endif
8110 sattr_T *new_ScreenAttrs;
8111 unsigned *new_LineOffset;
8112 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008113#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008114 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008115 tabpage_T *tp;
8116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008118 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008119#ifdef FEAT_AUTOCMD
8120 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008122retry:
8123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 /*
8125 * Allocation of the screen buffers is done only when the size changes and
8126 * when Rows and Columns have been set and we have started doing full
8127 * screen stuff.
8128 */
8129 if ((ScreenLines != NULL
8130 && Rows == screen_Rows
8131 && Columns == screen_Columns
8132#ifdef FEAT_MBYTE
8133 && enc_utf8 == (ScreenLinesUC != NULL)
8134 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008135 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136#endif
8137 )
8138 || Rows == 0
8139 || Columns == 0
8140 || (!full_screen && ScreenLines == NULL))
8141 return;
8142
8143 /*
8144 * It's possible that we produce an out-of-memory message below, which
8145 * will cause this function to be called again. To break the loop, just
8146 * return here.
8147 */
8148 if (entered)
8149 return;
8150 entered = TRUE;
8151
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008152 /*
8153 * Note that the window sizes are updated before reallocating the arrays,
8154 * thus we must not redraw here!
8155 */
8156 ++RedrawingDisabled;
8157
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 win_new_shellsize(); /* fit the windows in the new sized shell */
8159
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 comp_col(); /* recompute columns for shown command and ruler */
8161
8162 /*
8163 * We're changing the size of the screen.
8164 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8165 * - Move lines from the old arrays into the new arrays, clear extra
8166 * lines (unless the screen is going to be cleared).
8167 * - Free the old arrays.
8168 *
8169 * If anything fails, make ScreenLines NULL, so we don't do anything!
8170 * Continuing with the old ScreenLines may result in a crash, because the
8171 * size is wrong.
8172 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008173 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008175#ifdef FEAT_AUTOCMD
8176 if (aucmd_win != NULL)
8177 win_free_lsize(aucmd_win);
8178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
8180 new_ScreenLines = (schar_T *)lalloc((long_u)(
8181 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8182#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008183 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (enc_utf8)
8185 {
8186 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8187 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008188 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008189 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8191 }
8192 if (enc_dbcs == DBCS_JPNU)
8193 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8194 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8195#endif
8196 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8197 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8198 new_LineOffset = (unsigned *)lalloc((long_u)(
8199 Rows * sizeof(unsigned)), FALSE);
8200 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008201#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008202 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008205 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {
8207 if (win_alloc_lines(wp) == FAIL)
8208 {
8209 outofmem = TRUE;
8210#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008211 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212#endif
8213 }
8214 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008215#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008216 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8217 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008218 outofmem = TRUE;
8219#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008220#ifdef FEAT_WINDOWS
8221give_up:
8222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008224#ifdef FEAT_MBYTE
8225 for (i = 0; i < p_mco; ++i)
8226 if (new_ScreenLinesC[i] == NULL)
8227 break;
8228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 if (new_ScreenLines == NULL
8230#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008231 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8233#endif
8234 || new_ScreenAttrs == NULL
8235 || new_LineOffset == NULL
8236 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008237#ifdef FEAT_WINDOWS
8238 || new_TabPageIdxs == NULL
8239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 || outofmem)
8241 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008242 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008243 {
8244 /* guess the size */
8245 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8246
8247 /* Remember we did this to avoid getting outofmem messages over
8248 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008249 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 vim_free(new_ScreenLines);
8252 new_ScreenLines = NULL;
8253#ifdef FEAT_MBYTE
8254 vim_free(new_ScreenLinesUC);
8255 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008256 for (i = 0; i < p_mco; ++i)
8257 {
8258 vim_free(new_ScreenLinesC[i]);
8259 new_ScreenLinesC[i] = NULL;
8260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 vim_free(new_ScreenLines2);
8262 new_ScreenLines2 = NULL;
8263#endif
8264 vim_free(new_ScreenAttrs);
8265 new_ScreenAttrs = NULL;
8266 vim_free(new_LineOffset);
8267 new_LineOffset = NULL;
8268 vim_free(new_LineWraps);
8269 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008270#ifdef FEAT_WINDOWS
8271 vim_free(new_TabPageIdxs);
8272 new_TabPageIdxs = NULL;
8273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 }
8275 else
8276 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008277 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008278
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 for (new_row = 0; new_row < Rows; ++new_row)
8280 {
8281 new_LineOffset[new_row] = new_row * Columns;
8282 new_LineWraps[new_row] = FALSE;
8283
8284 /*
8285 * If the screen is not going to be cleared, copy as much as
8286 * possible from the old screen to the new one and clear the rest
8287 * (used when resizing the window at the "--more--" prompt or when
8288 * executing an external command, for the GUI).
8289 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008290 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {
8292 (void)vim_memset(new_ScreenLines + new_row * Columns,
8293 ' ', (size_t)Columns * sizeof(schar_T));
8294#ifdef FEAT_MBYTE
8295 if (enc_utf8)
8296 {
8297 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8298 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008299 for (i = 0; i < p_mco; ++i)
8300 (void)vim_memset(new_ScreenLinesC[i]
8301 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 0, (size_t)Columns * sizeof(u8char_T));
8303 }
8304 if (enc_dbcs == DBCS_JPNU)
8305 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8306 0, (size_t)Columns * sizeof(schar_T));
8307#endif
8308 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8309 0, (size_t)Columns * sizeof(sattr_T));
8310 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008311 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {
8313 if (screen_Columns < Columns)
8314 len = screen_Columns;
8315 else
8316 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008317#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008318 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008319 * may be invalid now. Also when p_mco changes. */
8320 if (!(enc_utf8 && ScreenLinesUC == NULL)
8321 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008322#endif
8323 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8324 ScreenLines + LineOffset[old_row],
8325 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008327 if (enc_utf8 && ScreenLinesUC != NULL
8328 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {
8330 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8331 ScreenLinesUC + LineOffset[old_row],
8332 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008333 for (i = 0; i < p_mco; ++i)
8334 mch_memmove(new_ScreenLinesC[i]
8335 + new_LineOffset[new_row],
8336 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 (size_t)len * sizeof(u8char_T));
8338 }
8339 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8340 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8341 ScreenLines2 + LineOffset[old_row],
8342 (size_t)len * sizeof(schar_T));
8343#endif
8344 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8345 ScreenAttrs + LineOffset[old_row],
8346 (size_t)len * sizeof(sattr_T));
8347 }
8348 }
8349 }
8350 /* Use the last line of the screen for the current line. */
8351 current_ScreenLine = new_ScreenLines + Rows * Columns;
8352 }
8353
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008354 free_screenlines();
8355
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 ScreenLines = new_ScreenLines;
8357#ifdef FEAT_MBYTE
8358 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008359 for (i = 0; i < p_mco; ++i)
8360 ScreenLinesC[i] = new_ScreenLinesC[i];
8361 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 ScreenLines2 = new_ScreenLines2;
8363#endif
8364 ScreenAttrs = new_ScreenAttrs;
8365 LineOffset = new_LineOffset;
8366 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008367#ifdef FEAT_WINDOWS
8368 TabPageIdxs = new_TabPageIdxs;
8369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370
8371 /* It's important that screen_Rows and screen_Columns reflect the actual
8372 * size of ScreenLines[]. Set them before calling anything. */
8373#ifdef FEAT_GUI
8374 old_Rows = screen_Rows;
8375#endif
8376 screen_Rows = Rows;
8377 screen_Columns = Columns;
8378
8379 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008380 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 screenclear2();
8382
8383#ifdef FEAT_GUI
8384 else if (gui.in_use
8385 && !gui.starting
8386 && ScreenLines != NULL
8387 && old_Rows != Rows)
8388 {
8389 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8390 /*
8391 * Adjust the position of the cursor, for when executing an external
8392 * command.
8393 */
8394 if (msg_row >= Rows) /* Rows got smaller */
8395 msg_row = Rows - 1; /* put cursor at last row */
8396 else if (Rows > old_Rows) /* Rows got bigger */
8397 msg_row += Rows - old_Rows; /* put cursor in same place */
8398 if (msg_col >= Columns) /* Columns got smaller */
8399 msg_col = Columns - 1; /* put cursor at last column */
8400 }
8401#endif
8402
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008404 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008405
8406#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008407 /*
8408 * Do not apply autocommands more than 3 times to avoid an endless loop
8409 * in case applying autocommands always changes Rows or Columns.
8410 */
8411 if (starting == 0 && ++retry_count <= 3)
8412 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008413 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008414 /* In rare cases, autocommands may have altered Rows or Columns,
8415 * jump back to check if we need to allocate the screen again. */
8416 goto retry;
8417 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419}
8420
8421 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008422free_screenlines()
8423{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008424#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008425 int i;
8426
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008427 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008428 for (i = 0; i < Screen_mco; ++i)
8429 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008430 vim_free(ScreenLines2);
8431#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008432 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008433 vim_free(ScreenAttrs);
8434 vim_free(LineOffset);
8435 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008436#ifdef FEAT_WINDOWS
8437 vim_free(TabPageIdxs);
8438#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008439}
8440
8441 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442screenclear()
8443{
8444 check_for_delay(FALSE);
8445 screenalloc(FALSE); /* allocate screen buffers if size changed */
8446 screenclear2(); /* clear the screen */
8447}
8448
8449 static void
8450screenclear2()
8451{
8452 int i;
8453
8454 if (starting == NO_SCREEN || ScreenLines == NULL
8455#ifdef FEAT_GUI
8456 || (gui.in_use && gui.starting)
8457#endif
8458 )
8459 return;
8460
8461#ifdef FEAT_GUI
8462 if (!gui.in_use)
8463#endif
8464 screen_attr = -1; /* force setting the Normal colors */
8465 screen_stop_highlight(); /* don't want highlighting here */
8466
8467#ifdef FEAT_CLIPBOARD
8468 /* disable selection without redrawing it */
8469 clip_scroll_selection(9999);
8470#endif
8471
8472 /* blank out ScreenLines */
8473 for (i = 0; i < Rows; ++i)
8474 {
8475 lineclear(LineOffset[i], (int)Columns);
8476 LineWraps[i] = FALSE;
8477 }
8478
8479 if (can_clear(T_CL))
8480 {
8481 out_str(T_CL); /* clear the display */
8482 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008483 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 }
8485 else
8486 {
8487 /* can't clear the screen, mark all chars with invalid attributes */
8488 for (i = 0; i < Rows; ++i)
8489 lineinvalid(LineOffset[i], (int)Columns);
8490 clear_cmdline = TRUE;
8491 }
8492
8493 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8494
8495 win_rest_invalid(firstwin);
8496 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008497#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008498 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (must_redraw == CLEAR) /* no need to clear again */
8501 must_redraw = NOT_VALID;
8502 compute_cmdrow();
8503 msg_row = cmdline_row; /* put cursor on last line for messages */
8504 msg_col = 0;
8505 screen_start(); /* don't know where cursor is now */
8506 msg_scrolled = 0; /* can't scroll back */
8507 msg_didany = FALSE;
8508 msg_didout = FALSE;
8509}
8510
8511/*
8512 * Clear one line in ScreenLines.
8513 */
8514 static void
8515lineclear(off, width)
8516 unsigned off;
8517 int width;
8518{
8519 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8520#ifdef FEAT_MBYTE
8521 if (enc_utf8)
8522 (void)vim_memset(ScreenLinesUC + off, 0,
8523 (size_t)width * sizeof(u8char_T));
8524#endif
8525 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8526}
8527
8528/*
8529 * Mark one line in ScreenLines invalid by setting the attributes to an
8530 * invalid value.
8531 */
8532 static void
8533lineinvalid(off, width)
8534 unsigned off;
8535 int width;
8536{
8537 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8538}
8539
8540#ifdef FEAT_VERTSPLIT
8541/*
8542 * Copy part of a Screenline for vertically split window "wp".
8543 */
8544 static void
8545linecopy(to, from, wp)
8546 int to;
8547 int from;
8548 win_T *wp;
8549{
8550 unsigned off_to = LineOffset[to] + wp->w_wincol;
8551 unsigned off_from = LineOffset[from] + wp->w_wincol;
8552
8553 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8554 wp->w_width * sizeof(schar_T));
8555# ifdef FEAT_MBYTE
8556 if (enc_utf8)
8557 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008558 int i;
8559
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8561 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008562 for (i = 0; i < p_mco; ++i)
8563 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8564 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 }
8566 if (enc_dbcs == DBCS_JPNU)
8567 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8568 wp->w_width * sizeof(schar_T));
8569# endif
8570 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8571 wp->w_width * sizeof(sattr_T));
8572}
8573#endif
8574
8575/*
8576 * Return TRUE if clearing with term string "p" would work.
8577 * It can't work when the string is empty or it won't set the right background.
8578 */
8579 int
8580can_clear(p)
8581 char_u *p;
8582{
8583 return (*p != NUL && (t_colors <= 1
8584#ifdef FEAT_GUI
8585 || gui.in_use
8586#endif
8587 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8588}
8589
8590/*
8591 * Reset cursor position. Use whenever cursor was moved because of outputting
8592 * something directly to the screen (shell commands) or a terminal control
8593 * code.
8594 */
8595 void
8596screen_start()
8597{
8598 screen_cur_row = screen_cur_col = 9999;
8599}
8600
8601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 * Move the cursor to position "row","col" in the screen.
8603 * This tries to find the most efficient way to move, minimizing the number of
8604 * characters sent to the terminal.
8605 */
8606 void
8607windgoto(row, col)
8608 int row;
8609 int col;
8610{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008611 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 int i;
8613 int plan;
8614 int cost;
8615 int wouldbe_col;
8616 int noinvcurs;
8617 char_u *bs;
8618 int goto_cost;
8619 int attr;
8620
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008621#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8623
8624#define PLAN_LE 1
8625#define PLAN_CR 2
8626#define PLAN_NL 3
8627#define PLAN_WRITE 4
8628 /* Can't use ScreenLines unless initialized */
8629 if (ScreenLines == NULL)
8630 return;
8631
8632 if (col != screen_cur_col || row != screen_cur_row)
8633 {
8634 /* Check for valid position. */
8635 if (row < 0) /* window without text lines? */
8636 row = 0;
8637 if (row >= screen_Rows)
8638 row = screen_Rows - 1;
8639 if (col >= screen_Columns)
8640 col = screen_Columns - 1;
8641
8642 /* check if no cursor movement is allowed in highlight mode */
8643 if (screen_attr && *T_MS == NUL)
8644 noinvcurs = HIGHL_COST;
8645 else
8646 noinvcurs = 0;
8647 goto_cost = GOTO_COST + noinvcurs;
8648
8649 /*
8650 * Plan how to do the positioning:
8651 * 1. Use CR to move it to column 0, same row.
8652 * 2. Use T_LE to move it a few columns to the left.
8653 * 3. Use NL to move a few lines down, column 0.
8654 * 4. Move a few columns to the right with T_ND or by writing chars.
8655 *
8656 * Don't do this if the cursor went beyond the last column, the cursor
8657 * position is unknown then (some terminals wrap, some don't )
8658 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008659 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 * characters to move the cursor to the right.
8661 */
8662 if (row >= screen_cur_row && screen_cur_col < Columns)
8663 {
8664 /*
8665 * If the cursor is in the same row, bigger col, we can use CR
8666 * or T_LE.
8667 */
8668 bs = NULL; /* init for GCC */
8669 attr = screen_attr;
8670 if (row == screen_cur_row && col < screen_cur_col)
8671 {
8672 /* "le" is preferred over "bc", because "bc" is obsolete */
8673 if (*T_LE)
8674 bs = T_LE; /* "cursor left" */
8675 else
8676 bs = T_BC; /* "backspace character (old) */
8677 if (*bs)
8678 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8679 else
8680 cost = 999;
8681 if (col + 1 < cost) /* using CR is less characters */
8682 {
8683 plan = PLAN_CR;
8684 wouldbe_col = 0;
8685 cost = 1; /* CR is just one character */
8686 }
8687 else
8688 {
8689 plan = PLAN_LE;
8690 wouldbe_col = col;
8691 }
8692 if (noinvcurs) /* will stop highlighting */
8693 {
8694 cost += noinvcurs;
8695 attr = 0;
8696 }
8697 }
8698
8699 /*
8700 * If the cursor is above where we want to be, we can use CR LF.
8701 */
8702 else if (row > screen_cur_row)
8703 {
8704 plan = PLAN_NL;
8705 wouldbe_col = 0;
8706 cost = (row - screen_cur_row) * 2; /* CR LF */
8707 if (noinvcurs) /* will stop highlighting */
8708 {
8709 cost += noinvcurs;
8710 attr = 0;
8711 }
8712 }
8713
8714 /*
8715 * If the cursor is in the same row, smaller col, just use write.
8716 */
8717 else
8718 {
8719 plan = PLAN_WRITE;
8720 wouldbe_col = screen_cur_col;
8721 cost = 0;
8722 }
8723
8724 /*
8725 * Check if any characters that need to be written have the
8726 * correct attributes. Also avoid UTF-8 characters.
8727 */
8728 i = col - wouldbe_col;
8729 if (i > 0)
8730 cost += i;
8731 if (cost < goto_cost && i > 0)
8732 {
8733 /*
8734 * Check if the attributes are correct without additionally
8735 * stopping highlighting.
8736 */
8737 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8738 while (i && *p++ == attr)
8739 --i;
8740 if (i != 0)
8741 {
8742 /*
8743 * Try if it works when highlighting is stopped here.
8744 */
8745 if (*--p == 0)
8746 {
8747 cost += noinvcurs;
8748 while (i && *p++ == 0)
8749 --i;
8750 }
8751 if (i != 0)
8752 cost = 999; /* different attributes, don't do it */
8753 }
8754#ifdef FEAT_MBYTE
8755 if (enc_utf8)
8756 {
8757 /* Don't use an UTF-8 char for positioning, it's slow. */
8758 for (i = wouldbe_col; i < col; ++i)
8759 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8760 {
8761 cost = 999;
8762 break;
8763 }
8764 }
8765#endif
8766 }
8767
8768 /*
8769 * We can do it without term_windgoto()!
8770 */
8771 if (cost < goto_cost)
8772 {
8773 if (plan == PLAN_LE)
8774 {
8775 if (noinvcurs)
8776 screen_stop_highlight();
8777 while (screen_cur_col > col)
8778 {
8779 out_str(bs);
8780 --screen_cur_col;
8781 }
8782 }
8783 else if (plan == PLAN_CR)
8784 {
8785 if (noinvcurs)
8786 screen_stop_highlight();
8787 out_char('\r');
8788 screen_cur_col = 0;
8789 }
8790 else if (plan == PLAN_NL)
8791 {
8792 if (noinvcurs)
8793 screen_stop_highlight();
8794 while (screen_cur_row < row)
8795 {
8796 out_char('\n');
8797 ++screen_cur_row;
8798 }
8799 screen_cur_col = 0;
8800 }
8801
8802 i = col - screen_cur_col;
8803 if (i > 0)
8804 {
8805 /*
8806 * Use cursor-right if it's one character only. Avoids
8807 * removing a line of pixels from the last bold char, when
8808 * using the bold trick in the GUI.
8809 */
8810 if (T_ND[0] != NUL && T_ND[1] == NUL)
8811 {
8812 while (i-- > 0)
8813 out_char(*T_ND);
8814 }
8815 else
8816 {
8817 int off;
8818
8819 off = LineOffset[row] + screen_cur_col;
8820 while (i-- > 0)
8821 {
8822 if (ScreenAttrs[off] != screen_attr)
8823 screen_stop_highlight();
8824#ifdef FEAT_MBYTE
8825 out_flush_check();
8826#endif
8827 out_char(ScreenLines[off]);
8828#ifdef FEAT_MBYTE
8829 if (enc_dbcs == DBCS_JPNU
8830 && ScreenLines[off] == 0x8e)
8831 out_char(ScreenLines2[off]);
8832#endif
8833 ++off;
8834 }
8835 }
8836 }
8837 }
8838 }
8839 else
8840 cost = 999;
8841
8842 if (cost >= goto_cost)
8843 {
8844 if (noinvcurs)
8845 screen_stop_highlight();
8846 if (row == screen_cur_row && (col > screen_cur_col) &&
8847 *T_CRI != NUL)
8848 term_cursor_right(col - screen_cur_col);
8849 else
8850 term_windgoto(row, col);
8851 }
8852 screen_cur_row = row;
8853 screen_cur_col = col;
8854 }
8855}
8856
8857/*
8858 * Set cursor to its position in the current window.
8859 */
8860 void
8861setcursor()
8862{
8863 if (redrawing())
8864 {
8865 validate_cursor();
8866 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8867 W_WINCOL(curwin) + (
8868#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008869 /* With 'rightleft' set and the cursor on a double-wide
8870 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8872# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008873 (has_mbyte
8874 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8875 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876# endif
8877 1)) :
8878#endif
8879 curwin->w_wcol));
8880 }
8881}
8882
8883
8884/*
8885 * insert 'line_count' lines at 'row' in window 'wp'
8886 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8887 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8888 * scrolling.
8889 * Returns FAIL if the lines are not inserted, OK for success.
8890 */
8891 int
8892win_ins_lines(wp, row, line_count, invalid, mayclear)
8893 win_T *wp;
8894 int row;
8895 int line_count;
8896 int invalid;
8897 int mayclear;
8898{
8899 int did_delete;
8900 int nextrow;
8901 int lastrow;
8902 int retval;
8903
8904 if (invalid)
8905 wp->w_lines_valid = 0;
8906
8907 if (wp->w_height < 5)
8908 return FAIL;
8909
8910 if (line_count > wp->w_height - row)
8911 line_count = wp->w_height - row;
8912
8913 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8914 if (retval != MAYBE)
8915 return retval;
8916
8917 /*
8918 * If there is a next window or a status line, we first try to delete the
8919 * lines at the bottom to avoid messing what is after the window.
8920 * If this fails and there are following windows, don't do anything to avoid
8921 * messing up those windows, better just redraw.
8922 */
8923 did_delete = FALSE;
8924#ifdef FEAT_WINDOWS
8925 if (wp->w_next != NULL || wp->w_status_height)
8926 {
8927 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8928 line_count, (int)Rows, FALSE, NULL) == OK)
8929 did_delete = TRUE;
8930 else if (wp->w_next)
8931 return FAIL;
8932 }
8933#endif
8934 /*
8935 * if no lines deleted, blank the lines that will end up below the window
8936 */
8937 if (!did_delete)
8938 {
8939#ifdef FEAT_WINDOWS
8940 wp->w_redr_status = TRUE;
8941#endif
8942 redraw_cmdline = TRUE;
8943 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8944 lastrow = nextrow + line_count;
8945 if (lastrow > Rows)
8946 lastrow = Rows;
8947 screen_fill(nextrow - line_count, lastrow - line_count,
8948 W_WINCOL(wp), (int)W_ENDCOL(wp),
8949 ' ', ' ', 0);
8950 }
8951
8952 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8953 == FAIL)
8954 {
8955 /* deletion will have messed up other windows */
8956 if (did_delete)
8957 {
8958#ifdef FEAT_WINDOWS
8959 wp->w_redr_status = TRUE;
8960#endif
8961 win_rest_invalid(W_NEXT(wp));
8962 }
8963 return FAIL;
8964 }
8965
8966 return OK;
8967}
8968
8969/*
8970 * delete "line_count" window lines at "row" in window "wp"
8971 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8972 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8973 * scrolling
8974 * Return OK for success, FAIL if the lines are not deleted.
8975 */
8976 int
8977win_del_lines(wp, row, line_count, invalid, mayclear)
8978 win_T *wp;
8979 int row;
8980 int line_count;
8981 int invalid;
8982 int mayclear;
8983{
8984 int retval;
8985
8986 if (invalid)
8987 wp->w_lines_valid = 0;
8988
8989 if (line_count > wp->w_height - row)
8990 line_count = wp->w_height - row;
8991
8992 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8993 if (retval != MAYBE)
8994 return retval;
8995
8996 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8997 (int)Rows, FALSE, NULL) == FAIL)
8998 return FAIL;
8999
9000#ifdef FEAT_WINDOWS
9001 /*
9002 * If there are windows or status lines below, try to put them at the
9003 * correct place. If we can't do that, they have to be redrawn.
9004 */
9005 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9006 {
9007 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9008 line_count, (int)Rows, NULL) == FAIL)
9009 {
9010 wp->w_redr_status = TRUE;
9011 win_rest_invalid(wp->w_next);
9012 }
9013 }
9014 /*
9015 * If this is the last window and there is no status line, redraw the
9016 * command line later.
9017 */
9018 else
9019#endif
9020 redraw_cmdline = TRUE;
9021 return OK;
9022}
9023
9024/*
9025 * Common code for win_ins_lines() and win_del_lines().
9026 * Returns OK or FAIL when the work has been done.
9027 * Returns MAYBE when not finished yet.
9028 */
9029 static int
9030win_do_lines(wp, row, line_count, mayclear, del)
9031 win_T *wp;
9032 int row;
9033 int line_count;
9034 int mayclear;
9035 int del;
9036{
9037 int retval;
9038
9039 if (!redrawing() || line_count <= 0)
9040 return FAIL;
9041
9042 /* only a few lines left: redraw is faster */
9043 if (mayclear && Rows - line_count < 5
9044#ifdef FEAT_VERTSPLIT
9045 && wp->w_width == Columns
9046#endif
9047 )
9048 {
9049 screenclear(); /* will set wp->w_lines_valid to 0 */
9050 return FAIL;
9051 }
9052
9053 /*
9054 * Delete all remaining lines
9055 */
9056 if (row + line_count >= wp->w_height)
9057 {
9058 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9059 W_WINCOL(wp), (int)W_ENDCOL(wp),
9060 ' ', ' ', 0);
9061 return OK;
9062 }
9063
9064 /*
9065 * when scrolling, the message on the command line should be cleared,
9066 * otherwise it will stay there forever.
9067 */
9068 clear_cmdline = TRUE;
9069
9070 /*
9071 * If the terminal can set a scroll region, use that.
9072 * Always do this in a vertically split window. This will redraw from
9073 * ScreenLines[] when t_CV isn't defined. That's faster than using
9074 * win_line().
9075 * Don't use a scroll region when we are going to redraw the text, writing
9076 * a character in the lower right corner of the scroll region causes a
9077 * scroll-up in the DJGPP version.
9078 */
9079 if (scroll_region
9080#ifdef FEAT_VERTSPLIT
9081 || W_WIDTH(wp) != Columns
9082#endif
9083 )
9084 {
9085#ifdef FEAT_VERTSPLIT
9086 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9087#endif
9088 scroll_region_set(wp, row);
9089 if (del)
9090 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9091 wp->w_height - row, FALSE, wp);
9092 else
9093 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9094 wp->w_height - row, wp);
9095#ifdef FEAT_VERTSPLIT
9096 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9097#endif
9098 scroll_region_reset();
9099 return retval;
9100 }
9101
9102#ifdef FEAT_WINDOWS
9103 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9104 return FAIL;
9105#endif
9106
9107 return MAYBE;
9108}
9109
9110/*
9111 * window 'wp' and everything after it is messed up, mark it for redraw
9112 */
9113 static void
9114win_rest_invalid(wp)
9115 win_T *wp;
9116{
9117#ifdef FEAT_WINDOWS
9118 while (wp != NULL)
9119#else
9120 if (wp != NULL)
9121#endif
9122 {
9123 redraw_win_later(wp, NOT_VALID);
9124#ifdef FEAT_WINDOWS
9125 wp->w_redr_status = TRUE;
9126 wp = wp->w_next;
9127#endif
9128 }
9129 redraw_cmdline = TRUE;
9130}
9131
9132/*
9133 * The rest of the routines in this file perform screen manipulations. The
9134 * given operation is performed physically on the screen. The corresponding
9135 * change is also made to the internal screen image. In this way, the editor
9136 * anticipates the effect of editing changes on the appearance of the screen.
9137 * That way, when we call screenupdate a complete redraw isn't usually
9138 * necessary. Another advantage is that we can keep adding code to anticipate
9139 * screen changes, and in the meantime, everything still works.
9140 */
9141
9142/*
9143 * types for inserting or deleting lines
9144 */
9145#define USE_T_CAL 1
9146#define USE_T_CDL 2
9147#define USE_T_AL 3
9148#define USE_T_CE 4
9149#define USE_T_DL 5
9150#define USE_T_SR 6
9151#define USE_NL 7
9152#define USE_T_CD 8
9153#define USE_REDRAW 9
9154
9155/*
9156 * insert lines on the screen and update ScreenLines[]
9157 * 'end' is the line after the scrolled part. Normally it is Rows.
9158 * When scrolling region used 'off' is the offset from the top for the region.
9159 * 'row' and 'end' are relative to the start of the region.
9160 *
9161 * return FAIL for failure, OK for success.
9162 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009163 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164screen_ins_lines(off, row, line_count, end, wp)
9165 int off;
9166 int row;
9167 int line_count;
9168 int end;
9169 win_T *wp; /* NULL or window to use width from */
9170{
9171 int i;
9172 int j;
9173 unsigned temp;
9174 int cursor_row;
9175 int type;
9176 int result_empty;
9177 int can_ce = can_clear(T_CE);
9178
9179 /*
9180 * FAIL if
9181 * - there is no valid screen
9182 * - the screen has to be redrawn completely
9183 * - the line count is less than one
9184 * - the line count is more than 'ttyscroll'
9185 */
9186 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9187 return FAIL;
9188
9189 /*
9190 * There are seven ways to insert lines:
9191 * 0. When in a vertically split window and t_CV isn't set, redraw the
9192 * characters from ScreenLines[].
9193 * 1. Use T_CD (clear to end of display) if it exists and the result of
9194 * the insert is just empty lines
9195 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9196 * present or line_count > 1. It looks better if we do all the inserts
9197 * at once.
9198 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9199 * insert is just empty lines and T_CE is not present or line_count >
9200 * 1.
9201 * 4. Use T_AL (insert line) if it exists.
9202 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9203 * just empty lines.
9204 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9205 * just empty lines.
9206 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9207 * the 'da' flag is not set or we have clear line capability.
9208 * 8. redraw the characters from ScreenLines[].
9209 *
9210 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9211 * the scrollbar for the window. It does have insert line, use that if it
9212 * exists.
9213 */
9214 result_empty = (row + line_count >= end);
9215#ifdef FEAT_VERTSPLIT
9216 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9217 type = USE_REDRAW;
9218 else
9219#endif
9220 if (can_clear(T_CD) && result_empty)
9221 type = USE_T_CD;
9222 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9223 type = USE_T_CAL;
9224 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9225 type = USE_T_CDL;
9226 else if (*T_AL != NUL)
9227 type = USE_T_AL;
9228 else if (can_ce && result_empty)
9229 type = USE_T_CE;
9230 else if (*T_DL != NUL && result_empty)
9231 type = USE_T_DL;
9232 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9233 type = USE_T_SR;
9234 else
9235 return FAIL;
9236
9237 /*
9238 * For clearing the lines screen_del_lines() is used. This will also take
9239 * care of t_db if necessary.
9240 */
9241 if (type == USE_T_CD || type == USE_T_CDL ||
9242 type == USE_T_CE || type == USE_T_DL)
9243 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9244
9245 /*
9246 * If text is retained below the screen, first clear or delete as many
9247 * lines at the bottom of the window as are about to be inserted so that
9248 * the deleted lines won't later surface during a screen_del_lines.
9249 */
9250 if (*T_DB)
9251 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9252
9253#ifdef FEAT_CLIPBOARD
9254 /* Remove a modeless selection when inserting lines halfway the screen
9255 * or not the full width of the screen. */
9256 if (off + row > 0
9257# ifdef FEAT_VERTSPLIT
9258 || (wp != NULL && wp->w_width != Columns)
9259# endif
9260 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009261 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 else
9263 clip_scroll_selection(-line_count);
9264#endif
9265
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266#ifdef FEAT_GUI
9267 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9268 * scrolling is actually carried out. */
9269 gui_dont_update_cursor();
9270#endif
9271
9272 if (*T_CCS != NUL) /* cursor relative to region */
9273 cursor_row = row;
9274 else
9275 cursor_row = row + off;
9276
9277 /*
9278 * Shift LineOffset[] line_count down to reflect the inserted lines.
9279 * Clear the inserted lines in ScreenLines[].
9280 */
9281 row += off;
9282 end += off;
9283 for (i = 0; i < line_count; ++i)
9284 {
9285#ifdef FEAT_VERTSPLIT
9286 if (wp != NULL && wp->w_width != Columns)
9287 {
9288 /* need to copy part of a line */
9289 j = end - 1 - i;
9290 while ((j -= line_count) >= row)
9291 linecopy(j + line_count, j, wp);
9292 j += line_count;
9293 if (can_clear((char_u *)" "))
9294 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9295 else
9296 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9297 LineWraps[j] = FALSE;
9298 }
9299 else
9300#endif
9301 {
9302 j = end - 1 - i;
9303 temp = LineOffset[j];
9304 while ((j -= line_count) >= row)
9305 {
9306 LineOffset[j + line_count] = LineOffset[j];
9307 LineWraps[j + line_count] = LineWraps[j];
9308 }
9309 LineOffset[j + line_count] = temp;
9310 LineWraps[j + line_count] = FALSE;
9311 if (can_clear((char_u *)" "))
9312 lineclear(temp, (int)Columns);
9313 else
9314 lineinvalid(temp, (int)Columns);
9315 }
9316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317
9318 screen_stop_highlight();
9319 windgoto(cursor_row, 0);
9320
9321#ifdef FEAT_VERTSPLIT
9322 /* redraw the characters */
9323 if (type == USE_REDRAW)
9324 redraw_block(row, end, wp);
9325 else
9326#endif
9327 if (type == USE_T_CAL)
9328 {
9329 term_append_lines(line_count);
9330 screen_start(); /* don't know where cursor is now */
9331 }
9332 else
9333 {
9334 for (i = 0; i < line_count; i++)
9335 {
9336 if (type == USE_T_AL)
9337 {
9338 if (i && cursor_row != 0)
9339 windgoto(cursor_row, 0);
9340 out_str(T_AL);
9341 }
9342 else /* type == USE_T_SR */
9343 out_str(T_SR);
9344 screen_start(); /* don't know where cursor is now */
9345 }
9346 }
9347
9348 /*
9349 * With scroll-reverse and 'da' flag set we need to clear the lines that
9350 * have been scrolled down into the region.
9351 */
9352 if (type == USE_T_SR && *T_DA)
9353 {
9354 for (i = 0; i < line_count; ++i)
9355 {
9356 windgoto(off + i, 0);
9357 out_str(T_CE);
9358 screen_start(); /* don't know where cursor is now */
9359 }
9360 }
9361
9362#ifdef FEAT_GUI
9363 gui_can_update_cursor();
9364 if (gui.in_use)
9365 out_flush(); /* always flush after a scroll */
9366#endif
9367 return OK;
9368}
9369
9370/*
9371 * delete lines on the screen and update ScreenLines[]
9372 * 'end' is the line after the scrolled part. Normally it is Rows.
9373 * When scrolling region used 'off' is the offset from the top for the region.
9374 * 'row' and 'end' are relative to the start of the region.
9375 *
9376 * Return OK for success, FAIL if the lines are not deleted.
9377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 int
9379screen_del_lines(off, row, line_count, end, force, wp)
9380 int off;
9381 int row;
9382 int line_count;
9383 int end;
9384 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009385 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 int j;
9388 int i;
9389 unsigned temp;
9390 int cursor_row;
9391 int cursor_end;
9392 int result_empty; /* result is empty until end of region */
9393 int can_delete; /* deleting line codes can be used */
9394 int type;
9395
9396 /*
9397 * FAIL if
9398 * - there is no valid screen
9399 * - the screen has to be redrawn completely
9400 * - the line count is less than one
9401 * - the line count is more than 'ttyscroll'
9402 */
9403 if (!screen_valid(TRUE) || line_count <= 0 ||
9404 (!force && line_count > p_ttyscroll))
9405 return FAIL;
9406
9407 /*
9408 * Check if the rest of the current region will become empty.
9409 */
9410 result_empty = row + line_count >= end;
9411
9412 /*
9413 * We can delete lines only when 'db' flag not set or when 'ce' option
9414 * available.
9415 */
9416 can_delete = (*T_DB == NUL || can_clear(T_CE));
9417
9418 /*
9419 * There are six ways to delete lines:
9420 * 0. When in a vertically split window and t_CV isn't set, redraw the
9421 * characters from ScreenLines[].
9422 * 1. Use T_CD if it exists and the result is empty.
9423 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9424 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9425 * none of the other ways work.
9426 * 4. Use T_CE (erase line) if the result is empty.
9427 * 5. Use T_DL (delete line) if it exists.
9428 * 6. redraw the characters from ScreenLines[].
9429 */
9430#ifdef FEAT_VERTSPLIT
9431 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9432 type = USE_REDRAW;
9433 else
9434#endif
9435 if (can_clear(T_CD) && result_empty)
9436 type = USE_T_CD;
9437#if defined(__BEOS__) && defined(BEOS_DR8)
9438 /*
9439 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9440 * its internal termcap... this works okay for tests which test *T_DB !=
9441 * NUL. It has the disadvantage that the user cannot use any :set t_*
9442 * command to get T_DB (back) to empty_option, only :set term=... will do
9443 * the trick...
9444 * Anyway, this hack will hopefully go away with the next OS release.
9445 * (Olaf Seibert)
9446 */
9447 else if (row == 0 && T_DB == empty_option
9448 && (line_count == 1 || *T_CDL == NUL))
9449#else
9450 else if (row == 0 && (
9451#ifndef AMIGA
9452 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9453 * up, so use delete-line command */
9454 line_count == 1 ||
9455#endif
9456 *T_CDL == NUL))
9457#endif
9458 type = USE_NL;
9459 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9460 type = USE_T_CDL;
9461 else if (can_clear(T_CE) && result_empty
9462#ifdef FEAT_VERTSPLIT
9463 && (wp == NULL || wp->w_width == Columns)
9464#endif
9465 )
9466 type = USE_T_CE;
9467 else if (*T_DL != NUL && can_delete)
9468 type = USE_T_DL;
9469 else if (*T_CDL != NUL && can_delete)
9470 type = USE_T_CDL;
9471 else
9472 return FAIL;
9473
9474#ifdef FEAT_CLIPBOARD
9475 /* Remove a modeless selection when deleting lines halfway the screen or
9476 * not the full width of the screen. */
9477 if (off + row > 0
9478# ifdef FEAT_VERTSPLIT
9479 || (wp != NULL && wp->w_width != Columns)
9480# endif
9481 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009482 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 else
9484 clip_scroll_selection(line_count);
9485#endif
9486
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487#ifdef FEAT_GUI
9488 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9489 * scrolling is actually carried out. */
9490 gui_dont_update_cursor();
9491#endif
9492
9493 if (*T_CCS != NUL) /* cursor relative to region */
9494 {
9495 cursor_row = row;
9496 cursor_end = end;
9497 }
9498 else
9499 {
9500 cursor_row = row + off;
9501 cursor_end = end + off;
9502 }
9503
9504 /*
9505 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9506 * Clear the inserted lines in ScreenLines[].
9507 */
9508 row += off;
9509 end += off;
9510 for (i = 0; i < line_count; ++i)
9511 {
9512#ifdef FEAT_VERTSPLIT
9513 if (wp != NULL && wp->w_width != Columns)
9514 {
9515 /* need to copy part of a line */
9516 j = row + i;
9517 while ((j += line_count) <= end - 1)
9518 linecopy(j - line_count, j, wp);
9519 j -= line_count;
9520 if (can_clear((char_u *)" "))
9521 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9522 else
9523 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9524 LineWraps[j] = FALSE;
9525 }
9526 else
9527#endif
9528 {
9529 /* whole width, moving the line pointers is faster */
9530 j = row + i;
9531 temp = LineOffset[j];
9532 while ((j += line_count) <= end - 1)
9533 {
9534 LineOffset[j - line_count] = LineOffset[j];
9535 LineWraps[j - line_count] = LineWraps[j];
9536 }
9537 LineOffset[j - line_count] = temp;
9538 LineWraps[j - line_count] = FALSE;
9539 if (can_clear((char_u *)" "))
9540 lineclear(temp, (int)Columns);
9541 else
9542 lineinvalid(temp, (int)Columns);
9543 }
9544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
9546 screen_stop_highlight();
9547
9548#ifdef FEAT_VERTSPLIT
9549 /* redraw the characters */
9550 if (type == USE_REDRAW)
9551 redraw_block(row, end, wp);
9552 else
9553#endif
9554 if (type == USE_T_CD) /* delete the lines */
9555 {
9556 windgoto(cursor_row, 0);
9557 out_str(T_CD);
9558 screen_start(); /* don't know where cursor is now */
9559 }
9560 else if (type == USE_T_CDL)
9561 {
9562 windgoto(cursor_row, 0);
9563 term_delete_lines(line_count);
9564 screen_start(); /* don't know where cursor is now */
9565 }
9566 /*
9567 * Deleting lines at top of the screen or scroll region: Just scroll
9568 * the whole screen (scroll region) up by outputting newlines on the
9569 * last line.
9570 */
9571 else if (type == USE_NL)
9572 {
9573 windgoto(cursor_end - 1, 0);
9574 for (i = line_count; --i >= 0; )
9575 out_char('\n'); /* cursor will remain on same line */
9576 }
9577 else
9578 {
9579 for (i = line_count; --i >= 0; )
9580 {
9581 if (type == USE_T_DL)
9582 {
9583 windgoto(cursor_row, 0);
9584 out_str(T_DL); /* delete a line */
9585 }
9586 else /* type == USE_T_CE */
9587 {
9588 windgoto(cursor_row + i, 0);
9589 out_str(T_CE); /* erase a line */
9590 }
9591 screen_start(); /* don't know where cursor is now */
9592 }
9593 }
9594
9595 /*
9596 * If the 'db' flag is set, we need to clear the lines that have been
9597 * scrolled up at the bottom of the region.
9598 */
9599 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9600 {
9601 for (i = line_count; i > 0; --i)
9602 {
9603 windgoto(cursor_end - i, 0);
9604 out_str(T_CE); /* erase a line */
9605 screen_start(); /* don't know where cursor is now */
9606 }
9607 }
9608
9609#ifdef FEAT_GUI
9610 gui_can_update_cursor();
9611 if (gui.in_use)
9612 out_flush(); /* always flush after a scroll */
9613#endif
9614
9615 return OK;
9616}
9617
9618/*
9619 * show the current mode and ruler
9620 *
9621 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9622 * If clear_cmdline is FALSE there may be a message there that needs to be
9623 * cleared only if a mode is shown.
9624 * Return the length of the message (0 if no message).
9625 */
9626 int
9627showmode()
9628{
9629 int need_clear;
9630 int length = 0;
9631 int do_mode;
9632 int attr;
9633 int nwr_save;
9634#ifdef FEAT_INS_EXPAND
9635 int sub_attr;
9636#endif
9637
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009638 do_mode = ((p_smd && msg_silent == 0)
9639 && ((State & INSERT)
9640 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641#ifdef FEAT_VISUAL
9642 || VIsual_active
9643#endif
9644 ));
9645 if (do_mode || Recording)
9646 {
9647 /*
9648 * Don't show mode right now, when not redrawing or inside a mapping.
9649 * Call char_avail() only when we are going to show something, because
9650 * it takes a bit of time.
9651 */
9652 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9653 {
9654 redraw_cmdline = TRUE; /* show mode later */
9655 return 0;
9656 }
9657
9658 nwr_save = need_wait_return;
9659
9660 /* wait a bit before overwriting an important message */
9661 check_for_delay(FALSE);
9662
9663 /* if the cmdline is more than one line high, erase top lines */
9664 need_clear = clear_cmdline;
9665 if (clear_cmdline && cmdline_row < Rows - 1)
9666 msg_clr_cmdline(); /* will reset clear_cmdline */
9667
9668 /* Position on the last line in the window, column 0 */
9669 msg_pos_mode();
9670 cursor_off();
9671 attr = hl_attr(HLF_CM); /* Highlight mode */
9672 if (do_mode)
9673 {
9674 MSG_PUTS_ATTR("--", attr);
9675#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009676 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009677# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009678 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009679# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009680 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009681# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009682 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009683# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 MSG_PUTS_ATTR(" IM", attr);
9685# else
9686 MSG_PUTS_ATTR(" XIM", attr);
9687# endif
9688#endif
9689#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9690 if (gui.in_use)
9691 {
9692 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009693 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 }
9695#endif
9696#ifdef FEAT_INS_EXPAND
9697 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9698 {
9699 /* These messages can get long, avoid a wrap in a narrow
9700 * window. Prefer showing edit_submode_extra. */
9701 length = (Rows - msg_row) * Columns - 3;
9702 if (edit_submode_extra != NULL)
9703 length -= vim_strsize(edit_submode_extra);
9704 if (length > 0)
9705 {
9706 if (edit_submode_pre != NULL)
9707 length -= vim_strsize(edit_submode_pre);
9708 if (length - vim_strsize(edit_submode) > 0)
9709 {
9710 if (edit_submode_pre != NULL)
9711 msg_puts_attr(edit_submode_pre, attr);
9712 msg_puts_attr(edit_submode, attr);
9713 }
9714 if (edit_submode_extra != NULL)
9715 {
9716 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9717 if ((int)edit_submode_highl < (int)HLF_COUNT)
9718 sub_attr = hl_attr(edit_submode_highl);
9719 else
9720 sub_attr = attr;
9721 msg_puts_attr(edit_submode_extra, sub_attr);
9722 }
9723 }
9724 length = 0;
9725 }
9726 else
9727#endif
9728 {
9729#ifdef FEAT_VREPLACE
9730 if (State & VREPLACE_FLAG)
9731 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9732 else
9733#endif
9734 if (State & REPLACE_FLAG)
9735 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9736 else if (State & INSERT)
9737 {
9738#ifdef FEAT_RIGHTLEFT
9739 if (p_ri)
9740 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9741#endif
9742 MSG_PUTS_ATTR(_(" INSERT"), attr);
9743 }
9744 else if (restart_edit == 'I')
9745 MSG_PUTS_ATTR(_(" (insert)"), attr);
9746 else if (restart_edit == 'R')
9747 MSG_PUTS_ATTR(_(" (replace)"), attr);
9748 else if (restart_edit == 'V')
9749 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9750#ifdef FEAT_RIGHTLEFT
9751 if (p_hkmap)
9752 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9753# ifdef FEAT_FKMAP
9754 if (p_fkmap)
9755 MSG_PUTS_ATTR(farsi_text_5, attr);
9756# endif
9757#endif
9758#ifdef FEAT_KEYMAP
9759 if (State & LANGMAP)
9760 {
9761# ifdef FEAT_ARABIC
9762 if (curwin->w_p_arab)
9763 MSG_PUTS_ATTR(_(" Arabic"), attr);
9764 else
9765# endif
9766 MSG_PUTS_ATTR(_(" (lang)"), attr);
9767 }
9768#endif
9769 if ((State & INSERT) && p_paste)
9770 MSG_PUTS_ATTR(_(" (paste)"), attr);
9771
9772#ifdef FEAT_VISUAL
9773 if (VIsual_active)
9774 {
9775 char *p;
9776
9777 /* Don't concatenate separate words to avoid translation
9778 * problems. */
9779 switch ((VIsual_select ? 4 : 0)
9780 + (VIsual_mode == Ctrl_V) * 2
9781 + (VIsual_mode == 'V'))
9782 {
9783 case 0: p = N_(" VISUAL"); break;
9784 case 1: p = N_(" VISUAL LINE"); break;
9785 case 2: p = N_(" VISUAL BLOCK"); break;
9786 case 4: p = N_(" SELECT"); break;
9787 case 5: p = N_(" SELECT LINE"); break;
9788 default: p = N_(" SELECT BLOCK"); break;
9789 }
9790 MSG_PUTS_ATTR(_(p), attr);
9791 }
9792#endif
9793 MSG_PUTS_ATTR(" --", attr);
9794 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009795
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 need_clear = TRUE;
9797 }
9798 if (Recording
9799#ifdef FEAT_INS_EXPAND
9800 && edit_submode == NULL /* otherwise it gets too long */
9801#endif
9802 )
9803 {
9804 MSG_PUTS_ATTR(_("recording"), attr);
9805 need_clear = TRUE;
9806 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009807
9808 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 if (need_clear || clear_cmdline)
9810 msg_clr_eos();
9811 msg_didout = FALSE; /* overwrite this message */
9812 length = msg_col;
9813 msg_col = 0;
9814 need_wait_return = nwr_save; /* never ask for hit-return for this */
9815 }
9816 else if (clear_cmdline && msg_silent == 0)
9817 /* Clear the whole command line. Will reset "clear_cmdline". */
9818 msg_clr_cmdline();
9819
9820#ifdef FEAT_CMDL_INFO
9821# ifdef FEAT_VISUAL
9822 /* In Visual mode the size of the selected area must be redrawn. */
9823 if (VIsual_active)
9824 clear_showcmd();
9825# endif
9826
9827 /* If the last window has no status line, the ruler is after the mode
9828 * message and must be redrawn */
9829 if (redrawing()
9830# ifdef FEAT_WINDOWS
9831 && lastwin->w_status_height == 0
9832# endif
9833 )
9834 win_redr_ruler(lastwin, TRUE);
9835#endif
9836 redraw_cmdline = FALSE;
9837 clear_cmdline = FALSE;
9838
9839 return length;
9840}
9841
9842/*
9843 * Position for a mode message.
9844 */
9845 static void
9846msg_pos_mode()
9847{
9848 msg_col = 0;
9849 msg_row = Rows - 1;
9850}
9851
9852/*
9853 * Delete mode message. Used when ESC is typed which is expected to end
9854 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009855 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 */
9857 void
9858unshowmode(force)
9859 int force;
9860{
9861 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009862 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 */
9864 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9865 redraw_cmdline = TRUE; /* delete mode later */
9866 else
9867 {
9868 msg_pos_mode();
9869 if (Recording)
9870 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9871 msg_clr_eos();
9872 }
9873}
9874
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009875#if defined(FEAT_WINDOWS)
9876/*
9877 * Draw the tab pages line at the top of the Vim window.
9878 */
9879 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009880draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009881{
9882 int tabcount = 0;
9883 tabpage_T *tp;
9884 int tabwidth;
9885 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009886 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009887 int attr;
9888 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009889 win_T *cwp;
9890 int wincount;
9891 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009892 int c;
9893 int len;
9894 int attr_sel = hl_attr(HLF_TPS);
9895 int attr_nosel = hl_attr(HLF_TP);
9896 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009897 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009898 int room;
9899 int use_sep_chars = (t_colors < 8
9900#ifdef FEAT_GUI
9901 && !gui.in_use
9902#endif
9903 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009904
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009905 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009906
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009907#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009908 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009909 if (gui_use_tabline())
9910 {
9911 gui_update_tabline();
9912 return;
9913 }
9914#endif
9915
9916 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009917 return;
9918
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009919#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009920
9921 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9922 for (scol = 0; scol < Columns; ++scol)
9923 TabPageIdxs[scol] = 0;
9924
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009925 /* Use the 'tabline' option if it's set. */
9926 if (*p_tal != NUL)
9927 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009928 int save_called_emsg = called_emsg;
9929
9930 /* Check for an error. If there is one we would loop in redrawing the
9931 * screen. Avoid that by making 'tabline' empty. */
9932 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009933 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009934 if (called_emsg)
9935 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009936 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009937 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009938 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009939 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009940#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009941 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009942 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9943 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009944
Bram Moolenaar238a5642006-02-21 22:12:05 +00009945 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9946 if (tabwidth < 6)
9947 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009948
Bram Moolenaar238a5642006-02-21 22:12:05 +00009949 attr = attr_nosel;
9950 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009951 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009952 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9953 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009954 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009955 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009956
Bram Moolenaar238a5642006-02-21 22:12:05 +00009957 if (tp->tp_topframe == topframe)
9958 attr = attr_sel;
9959 if (use_sep_chars && col > 0)
9960 screen_putchar('|', 0, col++, attr);
9961
9962 if (tp->tp_topframe != topframe)
9963 attr = attr_nosel;
9964
9965 screen_putchar(' ', 0, col++, attr);
9966
9967 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009968 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009969 cwp = curwin;
9970 wp = firstwin;
9971 }
9972 else
9973 {
9974 cwp = tp->tp_curwin;
9975 wp = tp->tp_firstwin;
9976 }
9977
9978 modified = FALSE;
9979 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9980 if (bufIsChanged(wp->w_buffer))
9981 modified = TRUE;
9982 if (modified || wincount > 1)
9983 {
9984 if (wincount > 1)
9985 {
9986 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009987 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009988 if (col + len >= Columns - 3)
9989 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009990 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009991#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009992 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009993#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009994 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009995#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009996 );
9997 col += len;
9998 }
9999 if (modified)
10000 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10001 screen_putchar(' ', 0, col++, attr);
10002 }
10003
10004 room = scol - col + tabwidth - 1;
10005 if (room > 0)
10006 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010007 /* Get buffer name in NameBuff[] */
10008 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010009 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010010 len = vim_strsize(NameBuff);
10011 p = NameBuff;
10012#ifdef FEAT_MBYTE
10013 if (has_mbyte)
10014 while (len > room)
10015 {
10016 len -= ptr2cells(p);
10017 mb_ptr_adv(p);
10018 }
10019 else
10020#endif
10021 if (len > room)
10022 {
10023 p += len - room;
10024 len = room;
10025 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010026 if (len > Columns - col - 1)
10027 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010028
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010029 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010030 col += len;
10031 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010032 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010033
10034 /* Store the tab page number in TabPageIdxs[], so that
10035 * jump_to_mouse() knows where each one is. */
10036 ++tabcount;
10037 while (scol < col)
10038 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010039 }
10040
Bram Moolenaar238a5642006-02-21 22:12:05 +000010041 if (use_sep_chars)
10042 c = '_';
10043 else
10044 c = ' ';
10045 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010046
10047 /* Put an "X" for closing the current tab if there are several. */
10048 if (first_tabpage->tp_next != NULL)
10049 {
10050 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10051 TabPageIdxs[Columns - 1] = -999;
10052 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010053 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010054
10055 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10056 * set. */
10057 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010058}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010059
10060/*
10061 * Get buffer name for "buf" into NameBuff[].
10062 * Takes care of special buffer names and translates special characters.
10063 */
10064 void
10065get_trans_bufname(buf)
10066 buf_T *buf;
10067{
10068 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010069 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010070 else
10071 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10072 trans_characters(NameBuff, MAXPATHL);
10073}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010074#endif
10075
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10077/*
10078 * Get the character to use in a status line. Get its attributes in "*attr".
10079 */
10080 static int
10081fillchar_status(attr, is_curwin)
10082 int *attr;
10083 int is_curwin;
10084{
10085 int fill;
10086 if (is_curwin)
10087 {
10088 *attr = hl_attr(HLF_S);
10089 fill = fill_stl;
10090 }
10091 else
10092 {
10093 *attr = hl_attr(HLF_SNC);
10094 fill = fill_stlnc;
10095 }
10096 /* Use fill when there is highlighting, and highlighting of current
10097 * window differs, or the fillchars differ, or this is not the
10098 * current window */
10099 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10100 || !is_curwin || firstwin == lastwin)
10101 || (fill_stl != fill_stlnc)))
10102 return fill;
10103 if (is_curwin)
10104 return '^';
10105 return '=';
10106}
10107#endif
10108
10109#ifdef FEAT_VERTSPLIT
10110/*
10111 * Get the character to use in a separator between vertically split windows.
10112 * Get its attributes in "*attr".
10113 */
10114 static int
10115fillchar_vsep(attr)
10116 int *attr;
10117{
10118 *attr = hl_attr(HLF_C);
10119 if (*attr == 0 && fill_vert == ' ')
10120 return '|';
10121 else
10122 return fill_vert;
10123}
10124#endif
10125
10126/*
10127 * Return TRUE if redrawing should currently be done.
10128 */
10129 int
10130redrawing()
10131{
10132 return (!RedrawingDisabled
10133 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10134}
10135
10136/*
10137 * Return TRUE if printing messages should currently be done.
10138 */
10139 int
10140messaging()
10141{
10142 return (!(p_lz && char_avail() && !KeyTyped));
10143}
10144
10145/*
10146 * Show current status info in ruler and various other places
10147 * If always is FALSE, only show ruler if position has changed.
10148 */
10149 void
10150showruler(always)
10151 int always;
10152{
10153 if (!always && !redrawing())
10154 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010155#ifdef FEAT_INS_EXPAND
10156 if (pum_visible())
10157 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010158# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010159 /* Don't redraw right now, do it later. */
10160 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010161# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010162 return;
10163 }
10164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010166 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010167 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010168 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 else
10171#endif
10172#ifdef FEAT_CMDL_INFO
10173 win_redr_ruler(curwin, always);
10174#endif
10175
10176#ifdef FEAT_TITLE
10177 if (need_maketitle
10178# ifdef FEAT_STL_OPT
10179 || (p_icon && (stl_syntax & STL_IN_ICON))
10180 || (p_title && (stl_syntax & STL_IN_TITLE))
10181# endif
10182 )
10183 maketitle();
10184#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010185#ifdef FEAT_WINDOWS
10186 /* Redraw the tab pages line if needed. */
10187 if (redraw_tabline)
10188 draw_tabline();
10189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190}
10191
10192#ifdef FEAT_CMDL_INFO
10193 static void
10194win_redr_ruler(wp, always)
10195 win_T *wp;
10196 int always;
10197{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010198#define RULER_BUF_LEN 70
10199 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 int row;
10201 int fillchar;
10202 int attr;
10203 int empty_line = FALSE;
10204 colnr_T virtcol;
10205 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010206 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 int o;
10208#ifdef FEAT_VERTSPLIT
10209 int this_ru_col;
10210 int off = 0;
10211 int width = Columns;
10212# define WITH_OFF(x) x
10213# define WITH_WIDTH(x) x
10214#else
10215# define WITH_OFF(x) 0
10216# define WITH_WIDTH(x) Columns
10217# define this_ru_col ru_col
10218#endif
10219
10220 /* If 'ruler' off or redrawing disabled, don't do anything */
10221 if (!p_ru)
10222 return;
10223
10224 /*
10225 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10226 * after deleting lines, before cursor.lnum is corrected.
10227 */
10228 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10229 return;
10230
10231#ifdef FEAT_INS_EXPAND
10232 /* Don't draw the ruler while doing insert-completion, it might overwrite
10233 * the (long) mode message. */
10234# ifdef FEAT_WINDOWS
10235 if (wp == lastwin && lastwin->w_status_height == 0)
10236# endif
10237 if (edit_submode != NULL)
10238 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010239 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10240 if (pum_visible())
10241 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#endif
10243
10244#ifdef FEAT_STL_OPT
10245 if (*p_ruf)
10246 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010247 int save_called_emsg = called_emsg;
10248
10249 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010251 if (called_emsg)
10252 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010253 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010254 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 return;
10256 }
10257#endif
10258
10259 /*
10260 * Check if not in Insert mode and the line is empty (will show "0-1").
10261 */
10262 if (!(State & INSERT)
10263 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10264 empty_line = TRUE;
10265
10266 /*
10267 * Only draw the ruler when something changed.
10268 */
10269 validate_virtcol_win(wp);
10270 if ( redraw_cmdline
10271 || always
10272 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10273 || wp->w_cursor.col != wp->w_ru_cursor.col
10274 || wp->w_virtcol != wp->w_ru_virtcol
10275#ifdef FEAT_VIRTUALEDIT
10276 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10277#endif
10278 || wp->w_topline != wp->w_ru_topline
10279 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10280#ifdef FEAT_DIFF
10281 || wp->w_topfill != wp->w_ru_topfill
10282#endif
10283 || empty_line != wp->w_ru_empty)
10284 {
10285 cursor_off();
10286#ifdef FEAT_WINDOWS
10287 if (wp->w_status_height)
10288 {
10289 row = W_WINROW(wp) + wp->w_height;
10290 fillchar = fillchar_status(&attr, wp == curwin);
10291# ifdef FEAT_VERTSPLIT
10292 off = W_WINCOL(wp);
10293 width = W_WIDTH(wp);
10294# endif
10295 }
10296 else
10297#endif
10298 {
10299 row = Rows - 1;
10300 fillchar = ' ';
10301 attr = 0;
10302#ifdef FEAT_VERTSPLIT
10303 width = Columns;
10304 off = 0;
10305#endif
10306 }
10307
10308 /* In list mode virtcol needs to be recomputed */
10309 virtcol = wp->w_virtcol;
10310 if (wp->w_p_list && lcs_tab1 == NUL)
10311 {
10312 wp->w_p_list = FALSE;
10313 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10314 wp->w_p_list = TRUE;
10315 }
10316
10317 /*
10318 * Some sprintfs return the length, some return a pointer.
10319 * To avoid portability problems we use strlen() here.
10320 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010321 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10323 ? 0L
10324 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010325 len = STRLEN(buffer);
10326 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10328 (int)virtcol + 1);
10329
10330 /*
10331 * Add a "50%" if there is room for it.
10332 * On the last line, don't print in the last column (scrolls the
10333 * screen up on some terminals).
10334 */
10335 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010336 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 o = i + vim_strsize(buffer + i + 1);
10338#ifdef FEAT_WINDOWS
10339 if (wp->w_status_height == 0) /* can't use last char of screen */
10340#endif
10341 ++o;
10342#ifdef FEAT_VERTSPLIT
10343 this_ru_col = ru_col - (Columns - width);
10344 if (this_ru_col < 0)
10345 this_ru_col = 0;
10346#endif
10347 /* Never use more than half the window/screen width, leave the other
10348 * half for the filename. */
10349 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10350 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10351 if (this_ru_col + o < WITH_WIDTH(width))
10352 {
10353 while (this_ru_col + o < WITH_WIDTH(width))
10354 {
10355#ifdef FEAT_MBYTE
10356 if (has_mbyte)
10357 i += (*mb_char2bytes)(fillchar, buffer + i);
10358 else
10359#endif
10360 buffer[i++] = fillchar;
10361 ++o;
10362 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010363 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 }
10365 /* Truncate at window boundary. */
10366#ifdef FEAT_MBYTE
10367 if (has_mbyte)
10368 {
10369 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010370 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 {
10372 o += (*mb_ptr2cells)(buffer + i);
10373 if (this_ru_col + o > WITH_WIDTH(width))
10374 {
10375 buffer[i] = NUL;
10376 break;
10377 }
10378 }
10379 }
10380 else
10381#endif
10382 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10383 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10384
10385 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10386 i = redraw_cmdline;
10387 screen_fill(row, row + 1,
10388 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10389 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10390 fillchar, fillchar, attr);
10391 /* don't redraw the cmdline because of showing the ruler */
10392 redraw_cmdline = i;
10393 wp->w_ru_cursor = wp->w_cursor;
10394 wp->w_ru_virtcol = wp->w_virtcol;
10395 wp->w_ru_empty = empty_line;
10396 wp->w_ru_topline = wp->w_topline;
10397 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10398#ifdef FEAT_DIFF
10399 wp->w_ru_topfill = wp->w_topfill;
10400#endif
10401 }
10402}
10403#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010404
10405#if defined(FEAT_LINEBREAK) || defined(PROTO)
10406/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010407 * Return the width of the 'number' and 'relativenumber' column.
10408 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010409 * Otherwise it depends on 'numberwidth' and the line count.
10410 */
10411 int
10412number_width(wp)
10413 win_T *wp;
10414{
10415 int n;
10416 linenr_T lnum;
10417
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010418 if (wp->w_p_rnu && !wp->w_p_nu)
10419 /* cursor line shows "0" */
10420 lnum = wp->w_height;
10421 else
10422 /* cursor line shows absolute line number */
10423 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010424
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010425 if (lnum == wp->w_nrwidth_line_count)
10426 return wp->w_nrwidth_width;
10427 wp->w_nrwidth_line_count = lnum;
10428
10429 n = 0;
10430 do
10431 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010432 lnum /= 10;
10433 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010434 } while (lnum > 0);
10435
10436 /* 'numberwidth' gives the minimal width plus one */
10437 if (n < wp->w_p_nuw - 1)
10438 n = wp->w_p_nuw - 1;
10439
10440 wp->w_nrwidth_width = n;
10441 return n;
10442}
10443#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010444
10445/*
10446 * Return the current cursor column. This is the actual position on the
10447 * screen. First column is 0.
10448 */
10449 int
10450screen_screencol()
10451{
10452 return screen_cur_col;
10453}
10454
10455/*
10456 * Return the current cursor row. This is the actual position on the screen.
10457 * First row is 0.
10458 */
10459 int
10460screen_screenrow()
10461{
10462 return screen_cur_row;
10463}