blob: 86d43cb1b290176213e24e063ecc2e44931a5d44 [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/*
271 * Changed something in the current window, at buffer line "lnum", that
272 * requires that line and possibly other lines to be redrawn.
273 * Used when entering/leaving Insert mode with the cursor on a folded line.
274 * Used to remove the "$" from a change command.
275 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
276 * may become invalid and the whole window will have to be redrawn.
277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 void
279redrawWinline(lnum, invalid)
280 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000281 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282{
283#ifdef FEAT_FOLDING
284 int i;
285#endif
286
287 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
288 curwin->w_redraw_top = lnum;
289 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
290 curwin->w_redraw_bot = lnum;
291 redraw_later(VALID);
292
293#ifdef FEAT_FOLDING
294 if (invalid)
295 {
296 /* A w_lines[] entry for this lnum has become invalid. */
297 i = find_wl_entry(curwin, lnum);
298 if (i >= 0)
299 curwin->w_lines[i].wl_valid = FALSE;
300 }
301#endif
302}
303
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200304#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200305 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
307 * update all windows that are editing the current buffer
308 */
309 void
310update_curbuf(type)
311 int type;
312{
313 redraw_curbuf_later(type);
314 update_screen(type);
315}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
318/*
319 * update_screen()
320 *
321 * Based on the current value of curwin->w_topline, transfer a screenfull
322 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
323 */
324 void
325update_screen(type)
326 int type;
327{
328 win_T *wp;
329 static int did_intro = FALSE;
330#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
331 int did_one;
332#endif
333
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000334 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 if (!screen_valid(TRUE))
336 return;
337
338 if (must_redraw)
339 {
340 if (type < must_redraw) /* use maximal type */
341 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000342
343 /* must_redraw is reset here, so that when we run into some weird
344 * reason to redraw while busy redrawing (e.g., asynchronous
345 * scrolling), or update_topline() in win_update() will cause a
346 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 must_redraw = 0;
348 }
349
350 /* Need to update w_lines[]. */
351 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
352 type = NOT_VALID;
353
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000354 /* Postpone the redrawing when it's not needed and when being called
355 * recursively. */
356 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
358 redraw_later(type); /* remember type for next time */
359 must_redraw = type;
360 if (type > INVERTED_ALL)
361 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
362 return;
363 }
364
365 updating_screen = TRUE;
366#ifdef FEAT_SYN_HL
367 ++display_tick; /* let syntax code know we're in a next round of
368 * display updating */
369#endif
370
371 /*
372 * if the screen was scrolled up when displaying a message, scroll it down
373 */
374 if (msg_scrolled)
375 {
376 clear_cmdline = TRUE;
377 if (msg_scrolled > Rows - 5) /* clearing is faster */
378 type = CLEAR;
379 else if (type != CLEAR)
380 {
381 check_for_delay(FALSE);
382 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
383 type = CLEAR;
384 FOR_ALL_WINDOWS(wp)
385 {
386 if (W_WINROW(wp) < msg_scrolled)
387 {
388 if (W_WINROW(wp) + wp->w_height > msg_scrolled
389 && wp->w_redr_type < REDRAW_TOP
390 && wp->w_lines_valid > 0
391 && wp->w_topline == wp->w_lines[0].wl_lnum)
392 {
393 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
394 wp->w_redr_type = REDRAW_TOP;
395 }
396 else
397 {
398 wp->w_redr_type = NOT_VALID;
399#ifdef FEAT_WINDOWS
400 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
401 <= msg_scrolled)
402 wp->w_redr_status = TRUE;
403#endif
404 }
405 }
406 }
407 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000408#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000409 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 }
412 msg_scrolled = 0;
413 need_wait_return = FALSE;
414 }
415
416 /* reset cmdline_row now (may have been changed temporarily) */
417 compute_cmdrow();
418
419 /* Check for changed highlighting */
420 if (need_highlight_changed)
421 highlight_changed();
422
423 if (type == CLEAR) /* first clear screen */
424 {
425 screenclear(); /* will reset clear_cmdline */
426 type = NOT_VALID;
427 }
428
429 if (clear_cmdline) /* going to clear cmdline (done below) */
430 check_for_delay(FALSE);
431
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 /* Force redraw when width of 'number' or 'relativenumber' column
434 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200436 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
437 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000438 curwin->w_redr_type = NOT_VALID;
439#endif
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 /*
442 * Only start redrawing if there is really something to do.
443 */
444 if (type == INVERTED)
445 update_curswant();
446 if (curwin->w_redr_type < type
447 && !((type == VALID
448 && curwin->w_lines[0].wl_valid
449#ifdef FEAT_DIFF
450 && curwin->w_topfill == curwin->w_old_topfill
451 && curwin->w_botfill == curwin->w_old_botfill
452#endif
453 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
454#ifdef FEAT_VISUAL
455 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000456 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
458 && curwin->w_old_visual_mode == VIsual_mode
459 && (curwin->w_valid & VALID_VIRTCOL)
460 && curwin->w_old_curswant == curwin->w_curswant)
461#endif
462 ))
463 curwin->w_redr_type = type;
464
Bram Moolenaar5a305422006-04-28 22:38:25 +0000465#ifdef FEAT_WINDOWS
466 /* Redraw the tab pages line if needed. */
467 if (redraw_tabline || type >= NOT_VALID)
468 draw_tabline();
469#endif
470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471#ifdef FEAT_SYN_HL
472 /*
473 * Correct stored syntax highlighting info for changes in each displayed
474 * buffer. Each buffer must only be done once.
475 */
476 FOR_ALL_WINDOWS(wp)
477 {
478 if (wp->w_buffer->b_mod_set)
479 {
480# ifdef FEAT_WINDOWS
481 win_T *wwp;
482
483 /* Check if we already did this buffer. */
484 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
485 if (wwp->w_buffer == wp->w_buffer)
486 break;
487# endif
488 if (
489# ifdef FEAT_WINDOWS
490 wwp == wp &&
491# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200492 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 syn_stack_apply_changes(wp->w_buffer);
494 }
495 }
496#endif
497
498 /*
499 * Go from top to bottom through the windows, redrawing the ones that need
500 * it.
501 */
502#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
503 did_one = FALSE;
504#endif
505#ifdef FEAT_SEARCH_EXTRA
506 search_hl.rm.regprog = NULL;
507#endif
508 FOR_ALL_WINDOWS(wp)
509 {
510 if (wp->w_redr_type != 0)
511 {
512 cursor_off();
513#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
514 if (!did_one)
515 {
516 did_one = TRUE;
517# ifdef FEAT_SEARCH_EXTRA
518 start_search_hl();
519# endif
520# ifdef FEAT_CLIPBOARD
521 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200522 if (clip_star.available && clip_isautosel_star())
523 clip_update_selection(&clip_star);
524 if (clip_plus.available && clip_isautosel_plus())
525 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# endif
527#ifdef FEAT_GUI
528 /* Remove the cursor before starting to do anything, because
529 * scrolling may make it difficult to redraw the text under
530 * it. */
531 if (gui.in_use)
532 gui_undraw_cursor();
533#endif
534 }
535#endif
536 win_update(wp);
537 }
538
539#ifdef FEAT_WINDOWS
540 /* redraw status line after the window to minimize cursor movement */
541 if (wp->w_redr_status)
542 {
543 cursor_off();
544 win_redr_status(wp);
545 }
546#endif
547 }
548#if defined(FEAT_SEARCH_EXTRA)
549 end_search_hl();
550#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100551#ifdef FEAT_INS_EXPAND
552 /* May need to redraw the popup menu. */
553 if (pum_visible())
554 pum_redraw();
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557#ifdef FEAT_WINDOWS
558 /* Reset b_mod_set flags. Going through all windows is probably faster
559 * than going through all buffers (there could be many buffers). */
560 for (wp = firstwin; wp != NULL; wp = wp->w_next)
561 wp->w_buffer->b_mod_set = FALSE;
562#else
563 curbuf->b_mod_set = FALSE;
564#endif
565
566 updating_screen = FALSE;
567#ifdef FEAT_GUI
568 gui_may_resize_shell();
569#endif
570
571 /* Clear or redraw the command line. Done last, because scrolling may
572 * mess up the command line. */
573 if (clear_cmdline || redraw_cmdline)
574 showmode();
575
576 /* May put up an introductory message when not editing a file */
577 if (!did_intro && bufempty()
578 && curbuf->b_fname == NULL
579#ifdef FEAT_WINDOWS
580 && firstwin->w_next == NULL
581#endif
582 && vim_strchr(p_shm, SHM_INTRO) == NULL)
583 intro_message(FALSE);
584 did_intro = TRUE;
585
586#ifdef FEAT_GUI
587 /* Redraw the cursor and update the scrollbars when all screen updating is
588 * done. */
589 if (gui.in_use)
590 {
591 out_flush(); /* required before updating the cursor */
592 if (did_one)
593 gui_update_cursor(FALSE, FALSE);
594 gui_update_scrollbars(FALSE);
595 }
596#endif
597}
598
Bram Moolenaar860cae12010-06-05 23:22:07 +0200599#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200600/*
601 * Return TRUE if the cursor line in window "wp" may be concealed, according
602 * to the 'concealcursor' option.
603 */
604 int
605conceal_cursor_line(wp)
606 win_T *wp;
607{
608 int c;
609
610 if (*wp->w_p_cocu == NUL)
611 return FALSE;
612 if (get_real_state() & VISUAL)
613 c = 'v';
614 else if (State & INSERT)
615 c = 'i';
616 else if (State & NORMAL)
617 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200618 else if (State & CMDLINE)
619 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200620 else
621 return FALSE;
622 return vim_strchr(wp->w_p_cocu, c) != NULL;
623}
624
625/*
626 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
627 */
628 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200629conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200630{
631 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
632 {
633 need_cursor_line_redraw = TRUE;
634 /* Need to recompute cursor column, e.g., when starting Visual mode
635 * without concealing. */
636 curs_columns(TRUE);
637 }
638}
639
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 void
641update_single_line(wp, lnum)
642 win_T *wp;
643 linenr_T lnum;
644{
645 int row;
646 int j;
647
648 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200649 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200650 {
651# ifdef FEAT_GUI
652 /* Remove the cursor before starting to do anything, because scrolling
653 * may make it difficult to redraw the text under it. */
654 if (gui.in_use)
655 gui_undraw_cursor();
656# endif
657 row = 0;
658 for (j = 0; j < wp->w_lines_valid; ++j)
659 {
660 if (lnum == wp->w_lines[j].wl_lnum)
661 {
662 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200663# ifdef FEAT_SEARCH_EXTRA
664 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200665 start_search_hl();
666 prepare_search_hl(wp, lnum);
667# endif
668 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
669# if defined(FEAT_SEARCH_EXTRA)
670 end_search_hl();
671# endif
672 break;
673 }
674 row += wp->w_lines[j].wl_size;
675 }
676# ifdef FEAT_GUI
677 /* Redraw the cursor */
678 if (gui.in_use)
679 {
680 out_flush(); /* required before updating the cursor */
681 gui_update_cursor(FALSE, FALSE);
682 }
683# endif
684 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200685 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200686}
687#endif
688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
690static void update_prepare __ARGS((void));
691static void update_finish __ARGS((void));
692
693/*
694 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000695 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 */
697 static void
698update_prepare()
699{
700 cursor_off();
701 updating_screen = TRUE;
702#ifdef FEAT_GUI
703 /* Remove the cursor before starting to do anything, because scrolling may
704 * make it difficult to redraw the text under it. */
705 if (gui.in_use)
706 gui_undraw_cursor();
707#endif
708#ifdef FEAT_SEARCH_EXTRA
709 start_search_hl();
710#endif
711}
712
713/*
714 * Finish updating one or more windows.
715 */
716 static void
717update_finish()
718{
719 if (redraw_cmdline)
720 showmode();
721
722# ifdef FEAT_SEARCH_EXTRA
723 end_search_hl();
724# endif
725
726 updating_screen = FALSE;
727
728# ifdef FEAT_GUI
729 gui_may_resize_shell();
730
731 /* Redraw the cursor and update the scrollbars when all screen updating is
732 * done. */
733 if (gui.in_use)
734 {
735 out_flush(); /* required before updating the cursor */
736 gui_update_cursor(FALSE, FALSE);
737 gui_update_scrollbars(FALSE);
738 }
739# endif
740}
741#endif
742
743#if defined(FEAT_SIGNS) || defined(PROTO)
744 void
745update_debug_sign(buf, lnum)
746 buf_T *buf;
747 linenr_T lnum;
748{
749 win_T *wp;
750 int doit = FALSE;
751
752# ifdef FEAT_FOLDING
753 win_foldinfo.fi_level = 0;
754# endif
755
756 /* update/delete a specific mark */
757 FOR_ALL_WINDOWS(wp)
758 {
759 if (buf != NULL && lnum > 0)
760 {
761 if (wp->w_buffer == buf && lnum >= wp->w_topline
762 && lnum < wp->w_botline)
763 {
764 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
765 wp->w_redraw_top = lnum;
766 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
767 wp->w_redraw_bot = lnum;
768 redraw_win_later(wp, VALID);
769 }
770 }
771 else
772 redraw_win_later(wp, VALID);
773 if (wp->w_redr_type != 0)
774 doit = TRUE;
775 }
776
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100777 /* Return when there is nothing to do, screen updating is already
778 * happening (recursive call) or still starting up. */
779 if (!doit || updating_screen
780#ifdef FEAT_GUI
781 || gui.starting
782#endif
783 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 return;
785
786 /* update all windows that need updating */
787 update_prepare();
788
789# ifdef FEAT_WINDOWS
790 for (wp = firstwin; wp; wp = wp->w_next)
791 {
792 if (wp->w_redr_type != 0)
793 win_update(wp);
794 if (wp->w_redr_status)
795 win_redr_status(wp);
796 }
797# else
798 if (curwin->w_redr_type != 0)
799 win_update(curwin);
800# endif
801
802 update_finish();
803}
804#endif
805
806
807#if defined(FEAT_GUI) || defined(PROTO)
808/*
809 * Update a single window, its status line and maybe the command line msg.
810 * Used for the GUI scrollbar.
811 */
812 void
813updateWindow(wp)
814 win_T *wp;
815{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000816 /* return if already busy updating */
817 if (updating_screen)
818 return;
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 update_prepare();
821
822#ifdef FEAT_CLIPBOARD
823 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200824 if (clip_star.available && clip_isautosel_star())
825 clip_update_selection(&clip_star);
826 if (clip_plus.available && clip_isautosel_plus())
827 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000833 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000834 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000835 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (wp->w_redr_status
838# ifdef FEAT_CMDL_INFO
839 || p_ru
840# endif
841# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000842 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843# endif
844 )
845 win_redr_status(wp);
846#endif
847
848 update_finish();
849}
850#endif
851
852/*
853 * Update a single window.
854 *
855 * This may cause the windows below it also to be redrawn (when clearing the
856 * screen or scrolling lines).
857 *
858 * How the window is redrawn depends on wp->w_redr_type. Each type also
859 * implies the one below it.
860 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000861 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
863 * INVERTED redraw the changed part of the Visual area
864 * INVERTED_ALL redraw the whole Visual area
865 * VALID 1. scroll up/down to adjust for a changed w_topline
866 * 2. update lines at the top when scrolled down
867 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000868 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 * b_mod_top and b_mod_bot.
870 * - if wp->w_redraw_top non-zero, redraw lines between
871 * wp->w_redraw_top and wp->w_redr_bot.
872 * - continue redrawing when syntax status is invalid.
873 * 4. if scrolled up, update lines at the bottom.
874 * This results in three areas that may need updating:
875 * top: from first row to top_end (when scrolled down)
876 * mid: from mid_start to mid_end (update inversion or changed text)
877 * bot: from bot_start to last row (when scrolled up)
878 */
879 static void
880win_update(wp)
881 win_T *wp;
882{
883 buf_T *buf = wp->w_buffer;
884 int type;
885 int top_end = 0; /* Below last row of the top area that needs
886 updating. 0 when no top area updating. */
887 int mid_start = 999;/* first row of the mid area that needs
888 updating. 999 when no mid area updating. */
889 int mid_end = 0; /* Below last row of the mid area that needs
890 updating. 0 when no mid area updating. */
891 int bot_start = 999;/* first row of the bot area that needs
892 updating. 999 when no bot area updating */
893#ifdef FEAT_VISUAL
894 int scrolled_down = FALSE; /* TRUE when scrolled down when
895 w_topline got smaller a bit */
896#endif
897#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000898 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 int top_to_mod = FALSE; /* redraw above mod_top */
900#endif
901
902 int row; /* current window row to display */
903 linenr_T lnum; /* current buffer lnum to display */
904 int idx; /* current index in w_lines[] */
905 int srow; /* starting row of the current line */
906
907 int eof = FALSE; /* if TRUE, we hit the end of the file */
908 int didline = FALSE; /* if TRUE, we finished the last line */
909 int i;
910 long j;
911 static int recursive = FALSE; /* being called recursively */
912 int old_botline = wp->w_botline;
913#ifdef FEAT_FOLDING
914 long fold_count;
915#endif
916#ifdef FEAT_SYN_HL
917 /* remember what happened to the previous line, to know if
918 * check_visual_highlight() can be used */
919#define DID_NONE 1 /* didn't update a line */
920#define DID_LINE 2 /* updated a normal line */
921#define DID_FOLD 3 /* updated a folded line */
922 int did_update = DID_NONE;
923 linenr_T syntax_last_parsed = 0; /* last parsed text line */
924#endif
925 linenr_T mod_top = 0;
926 linenr_T mod_bot = 0;
927#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
928 int save_got_int;
929#endif
930
931 type = wp->w_redr_type;
932
933 if (type == NOT_VALID)
934 {
935#ifdef FEAT_WINDOWS
936 wp->w_redr_status = TRUE;
937#endif
938 wp->w_lines_valid = 0;
939 }
940
941 /* Window is zero-height: nothing to draw. */
942 if (wp->w_height == 0)
943 {
944 wp->w_redr_type = 0;
945 return;
946 }
947
948#ifdef FEAT_VERTSPLIT
949 /* Window is zero-width: Only need to draw the separator. */
950 if (wp->w_width == 0)
951 {
952 /* draw the vertical separator right of this window */
953 draw_vsep_win(wp, 0);
954 wp->w_redr_type = 0;
955 return;
956 }
957#endif
958
959#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200960 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#endif
962
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000963#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200964 /* Force redraw when width of 'number' or 'relativenumber' column
965 * changes. */
966 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000967 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000968 {
969 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000970 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000971 }
972 else
973#endif
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
976 {
977 /*
978 * When there are both inserted/deleted lines and specific lines to be
979 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
980 * everything (only happens when redrawing is off for while).
981 */
982 type = NOT_VALID;
983 }
984 else
985 {
986 /*
987 * Set mod_top to the first line that needs displaying because of
988 * changes. Set mod_bot to the first line after the changes.
989 */
990 mod_top = wp->w_redraw_top;
991 if (wp->w_redraw_bot != 0)
992 mod_bot = wp->w_redraw_bot + 1;
993 else
994 mod_bot = 0;
995 wp->w_redraw_top = 0; /* reset for next time */
996 wp->w_redraw_bot = 0;
997 if (buf->b_mod_set)
998 {
999 if (mod_top == 0 || mod_top > buf->b_mod_top)
1000 {
1001 mod_top = buf->b_mod_top;
1002#ifdef FEAT_SYN_HL
1003 /* Need to redraw lines above the change that may be included
1004 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001005 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001007 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (mod_top < 1)
1009 mod_top = 1;
1010 }
1011#endif
1012 }
1013 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1014 mod_bot = buf->b_mod_bot;
1015
1016#ifdef FEAT_SEARCH_EXTRA
1017 /* When 'hlsearch' is on and using a multi-line search pattern, a
1018 * change in one line may make the Search highlighting in a
1019 * previous line invalid. Simple solution: redraw all visible
1020 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001021 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001023 if (search_hl.rm.regprog != NULL
1024 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001026 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001027 {
1028 cur = wp->w_match_head;
1029 while (cur != NULL)
1030 {
1031 if (cur->match.regprog != NULL
1032 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001033 {
1034 top_to_mod = TRUE;
1035 break;
1036 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001037 cur = cur->next;
1038 }
1039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#endif
1041 }
1042#ifdef FEAT_FOLDING
1043 if (mod_top != 0 && hasAnyFolding(wp))
1044 {
1045 linenr_T lnumt, lnumb;
1046
1047 /*
1048 * A change in a line can cause lines above it to become folded or
1049 * unfolded. Find the top most buffer line that may be affected.
1050 * If the line was previously folded and displayed, get the first
1051 * line of that fold. If the line is folded now, get the first
1052 * folded line. Use the minimum of these two.
1053 */
1054
1055 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1056 * the line below it. If there is no valid entry, use w_topline.
1057 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1058 * to this line. If there is no valid entry, use MAXLNUM. */
1059 lnumt = wp->w_topline;
1060 lnumb = MAXLNUM;
1061 for (i = 0; i < wp->w_lines_valid; ++i)
1062 if (wp->w_lines[i].wl_valid)
1063 {
1064 if (wp->w_lines[i].wl_lastlnum < mod_top)
1065 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1066 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1067 {
1068 lnumb = wp->w_lines[i].wl_lnum;
1069 /* When there is a fold column it might need updating
1070 * in the next line ("J" just above an open fold). */
1071 if (wp->w_p_fdc > 0)
1072 ++lnumb;
1073 }
1074 }
1075
1076 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1077 if (mod_top > lnumt)
1078 mod_top = lnumt;
1079
1080 /* Now do the same for the bottom line (one above mod_bot). */
1081 --mod_bot;
1082 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1083 ++mod_bot;
1084 if (mod_bot < lnumb)
1085 mod_bot = lnumb;
1086 }
1087#endif
1088
1089 /* When a change starts above w_topline and the end is below
1090 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001091 * If the end of the change is above w_topline: do like no change was
1092 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (mod_top != 0 && mod_top < wp->w_topline)
1094 {
1095 if (mod_bot > wp->w_topline)
1096 mod_top = wp->w_topline;
1097#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001098 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 top_end = 1;
1100#endif
1101 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001102
1103 /* When line numbers are displayed need to redraw all lines below
1104 * inserted/deleted lines. */
1105 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1106 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108
1109 /*
1110 * When only displaying the lines at the top, set top_end. Used when
1111 * window has scrolled down for msg_scrolled.
1112 */
1113 if (type == REDRAW_TOP)
1114 {
1115 j = 0;
1116 for (i = 0; i < wp->w_lines_valid; ++i)
1117 {
1118 j += wp->w_lines[i].wl_size;
1119 if (j >= wp->w_upd_rows)
1120 {
1121 top_end = j;
1122 break;
1123 }
1124 }
1125 if (top_end == 0)
1126 /* not found (cannot happen?): redraw everything */
1127 type = NOT_VALID;
1128 else
1129 /* top area defined, the rest is VALID */
1130 type = VALID;
1131 }
1132
Bram Moolenaar367329b2007-08-30 11:53:22 +00001133 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001134 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1135 * non-zero and thus not FALSE) will indicate that screenclear() was not
1136 * called. */
1137 if (screen_cleared)
1138 screen_cleared = MAYBE;
1139
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 /*
1141 * If there are no changes on the screen that require a complete redraw,
1142 * handle three cases:
1143 * 1: we are off the top of the screen by a few lines: scroll down
1144 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1145 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1146 * w_lines[] that needs updating.
1147 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001148 if ((type == VALID || type == SOME_VALID
1149 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150#ifdef FEAT_DIFF
1151 && !wp->w_botfill && !wp->w_old_botfill
1152#endif
1153 )
1154 {
1155 if (mod_top != 0 && wp->w_topline == mod_top)
1156 {
1157 /*
1158 * w_topline is the first changed line, the scrolling will be done
1159 * further down.
1160 */
1161 }
1162 else if (wp->w_lines[0].wl_valid
1163 && (wp->w_topline < wp->w_lines[0].wl_lnum
1164#ifdef FEAT_DIFF
1165 || (wp->w_topline == wp->w_lines[0].wl_lnum
1166 && wp->w_topfill > wp->w_old_topfill)
1167#endif
1168 ))
1169 {
1170 /*
1171 * New topline is above old topline: May scroll down.
1172 */
1173#ifdef FEAT_FOLDING
1174 if (hasAnyFolding(wp))
1175 {
1176 linenr_T ln;
1177
1178 /* count the number of lines we are off, counting a sequence
1179 * of folded lines as one */
1180 j = 0;
1181 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1182 {
1183 ++j;
1184 if (j >= wp->w_height - 2)
1185 break;
1186 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1187 }
1188 }
1189 else
1190#endif
1191 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1192 if (j < wp->w_height - 2) /* not too far off */
1193 {
1194 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1195#ifdef FEAT_DIFF
1196 /* insert extra lines for previously invisible filler lines */
1197 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1198 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1199 - wp->w_old_topfill;
1200#endif
1201 if (i < wp->w_height - 2) /* less than a screen off */
1202 {
1203 /*
1204 * Try to insert the correct number of lines.
1205 * If not the last window, delete the lines at the bottom.
1206 * win_ins_lines may fail when the terminal can't do it.
1207 */
1208 if (i > 0)
1209 check_for_delay(FALSE);
1210 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1211 {
1212 if (wp->w_lines_valid != 0)
1213 {
1214 /* Need to update rows that are new, stop at the
1215 * first one that scrolled down. */
1216 top_end = i;
1217#ifdef FEAT_VISUAL
1218 scrolled_down = TRUE;
1219#endif
1220
1221 /* Move the entries that were scrolled, disable
1222 * the entries for the lines to be redrawn. */
1223 if ((wp->w_lines_valid += j) > wp->w_height)
1224 wp->w_lines_valid = wp->w_height;
1225 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1226 wp->w_lines[idx] = wp->w_lines[idx - j];
1227 while (idx >= 0)
1228 wp->w_lines[idx--].wl_valid = FALSE;
1229 }
1230 }
1231 else
1232 mid_start = 0; /* redraw all lines */
1233 }
1234 else
1235 mid_start = 0; /* redraw all lines */
1236 }
1237 else
1238 mid_start = 0; /* redraw all lines */
1239 }
1240 else
1241 {
1242 /*
1243 * New topline is at or below old topline: May scroll up.
1244 * When topline didn't change, find first entry in w_lines[] that
1245 * needs updating.
1246 */
1247
1248 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1249 j = -1;
1250 row = 0;
1251 for (i = 0; i < wp->w_lines_valid; i++)
1252 {
1253 if (wp->w_lines[i].wl_valid
1254 && wp->w_lines[i].wl_lnum == wp->w_topline)
1255 {
1256 j = i;
1257 break;
1258 }
1259 row += wp->w_lines[i].wl_size;
1260 }
1261 if (j == -1)
1262 {
1263 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1264 * lines */
1265 mid_start = 0;
1266 }
1267 else
1268 {
1269 /*
1270 * Try to delete the correct number of lines.
1271 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1272 */
1273#ifdef FEAT_DIFF
1274 /* If the topline didn't change, delete old filler lines,
1275 * otherwise delete filler lines of the new topline... */
1276 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1277 row += wp->w_old_topfill;
1278 else
1279 row += diff_check_fill(wp, wp->w_topline);
1280 /* ... but don't delete new filler lines. */
1281 row -= wp->w_topfill;
1282#endif
1283 if (row > 0)
1284 {
1285 check_for_delay(FALSE);
1286 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1287 bot_start = wp->w_height - row;
1288 else
1289 mid_start = 0; /* redraw all lines */
1290 }
1291 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1292 {
1293 /*
1294 * Skip the lines (below the deleted lines) that are still
1295 * valid and don't need redrawing. Copy their info
1296 * upwards, to compensate for the deleted lines. Set
1297 * bot_start to the first row that needs redrawing.
1298 */
1299 bot_start = 0;
1300 idx = 0;
1301 for (;;)
1302 {
1303 wp->w_lines[idx] = wp->w_lines[j];
1304 /* stop at line that didn't fit, unless it is still
1305 * valid (no lines deleted) */
1306 if (row > 0 && bot_start + row
1307 + (int)wp->w_lines[j].wl_size > wp->w_height)
1308 {
1309 wp->w_lines_valid = idx + 1;
1310 break;
1311 }
1312 bot_start += wp->w_lines[idx++].wl_size;
1313
1314 /* stop at the last valid entry in w_lines[].wl_size */
1315 if (++j >= wp->w_lines_valid)
1316 {
1317 wp->w_lines_valid = idx;
1318 break;
1319 }
1320 }
1321#ifdef FEAT_DIFF
1322 /* Correct the first entry for filler lines at the top
1323 * when it won't get updated below. */
1324 if (wp->w_p_diff && bot_start > 0)
1325 wp->w_lines[0].wl_size =
1326 plines_win_nofill(wp, wp->w_topline, TRUE)
1327 + wp->w_topfill;
1328#endif
1329 }
1330 }
1331 }
1332
1333 /* When starting redraw in the first line, redraw all lines. When
1334 * there is only one window it's probably faster to clear the screen
1335 * first. */
1336 if (mid_start == 0)
1337 {
1338 mid_end = wp->w_height;
1339 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001340 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001341 /* Clear the screen when it was not done by win_del_lines() or
1342 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1343 * then. */
1344 if (screen_cleared != TRUE)
1345 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001346#ifdef FEAT_WINDOWS
1347 /* The screen was cleared, redraw the tab pages line. */
1348 if (redraw_tabline)
1349 draw_tabline();
1350#endif
1351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001353
1354 /* When win_del_lines() or win_ins_lines() caused the screen to be
1355 * cleared (only happens for the first window) or when screenclear()
1356 * was called directly above, "must_redraw" will have been set to
1357 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1358 if (screen_cleared == TRUE)
1359 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 else
1362 {
1363 /* Not VALID or INVERTED: redraw all lines. */
1364 mid_start = 0;
1365 mid_end = wp->w_height;
1366 }
1367
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001368 if (type == SOME_VALID)
1369 {
1370 /* SOME_VALID: redraw all lines. */
1371 mid_start = 0;
1372 mid_end = wp->w_height;
1373 type = NOT_VALID;
1374 }
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#ifdef FEAT_VISUAL
1377 /* check if we are updating or removing the inverted part */
1378 if ((VIsual_active && buf == curwin->w_buffer)
1379 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1380 {
1381 linenr_T from, to;
1382
1383 if (VIsual_active)
1384 {
1385 if (VIsual_active
1386 && (VIsual_mode != wp->w_old_visual_mode
1387 || type == INVERTED_ALL))
1388 {
1389 /*
1390 * If the type of Visual selection changed, redraw the whole
1391 * selection. Also when the ownership of the X selection is
1392 * gained or lost.
1393 */
1394 if (curwin->w_cursor.lnum < VIsual.lnum)
1395 {
1396 from = curwin->w_cursor.lnum;
1397 to = VIsual.lnum;
1398 }
1399 else
1400 {
1401 from = VIsual.lnum;
1402 to = curwin->w_cursor.lnum;
1403 }
1404 /* redraw more when the cursor moved as well */
1405 if (wp->w_old_cursor_lnum < from)
1406 from = wp->w_old_cursor_lnum;
1407 if (wp->w_old_cursor_lnum > to)
1408 to = wp->w_old_cursor_lnum;
1409 if (wp->w_old_visual_lnum < from)
1410 from = wp->w_old_visual_lnum;
1411 if (wp->w_old_visual_lnum > to)
1412 to = wp->w_old_visual_lnum;
1413 }
1414 else
1415 {
1416 /*
1417 * Find the line numbers that need to be updated: The lines
1418 * between the old cursor position and the current cursor
1419 * position. Also check if the Visual position changed.
1420 */
1421 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1422 {
1423 from = curwin->w_cursor.lnum;
1424 to = wp->w_old_cursor_lnum;
1425 }
1426 else
1427 {
1428 from = wp->w_old_cursor_lnum;
1429 to = curwin->w_cursor.lnum;
1430 if (from == 0) /* Visual mode just started */
1431 from = to;
1432 }
1433
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001434 if (VIsual.lnum != wp->w_old_visual_lnum
1435 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 {
1437 if (wp->w_old_visual_lnum < from
1438 && wp->w_old_visual_lnum != 0)
1439 from = wp->w_old_visual_lnum;
1440 if (wp->w_old_visual_lnum > to)
1441 to = wp->w_old_visual_lnum;
1442 if (VIsual.lnum < from)
1443 from = VIsual.lnum;
1444 if (VIsual.lnum > to)
1445 to = VIsual.lnum;
1446 }
1447 }
1448
1449 /*
1450 * If in block mode and changed column or curwin->w_curswant:
1451 * update all lines.
1452 * First compute the actual start and end column.
1453 */
1454 if (VIsual_mode == Ctrl_V)
1455 {
1456 colnr_T fromc, toc;
1457
1458 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1459 ++toc;
1460 if (curwin->w_curswant == MAXCOL)
1461 toc = MAXCOL;
1462
1463 if (fromc != wp->w_old_cursor_fcol
1464 || toc != wp->w_old_cursor_lcol)
1465 {
1466 if (from > VIsual.lnum)
1467 from = VIsual.lnum;
1468 if (to < VIsual.lnum)
1469 to = VIsual.lnum;
1470 }
1471 wp->w_old_cursor_fcol = fromc;
1472 wp->w_old_cursor_lcol = toc;
1473 }
1474 }
1475 else
1476 {
1477 /* Use the line numbers of the old Visual area. */
1478 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1479 {
1480 from = wp->w_old_cursor_lnum;
1481 to = wp->w_old_visual_lnum;
1482 }
1483 else
1484 {
1485 from = wp->w_old_visual_lnum;
1486 to = wp->w_old_cursor_lnum;
1487 }
1488 }
1489
1490 /*
1491 * There is no need to update lines above the top of the window.
1492 */
1493 if (from < wp->w_topline)
1494 from = wp->w_topline;
1495
1496 /*
1497 * If we know the value of w_botline, use it to restrict the update to
1498 * the lines that are visible in the window.
1499 */
1500 if (wp->w_valid & VALID_BOTLINE)
1501 {
1502 if (from >= wp->w_botline)
1503 from = wp->w_botline - 1;
1504 if (to >= wp->w_botline)
1505 to = wp->w_botline - 1;
1506 }
1507
1508 /*
1509 * Find the minimal part to be updated.
1510 * Watch out for scrolling that made entries in w_lines[] invalid.
1511 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1512 * top_end; need to redraw from top_end to the "to" line.
1513 * A middle mouse click with a Visual selection may change the text
1514 * above the Visual area and reset wl_valid, do count these for
1515 * mid_end (in srow).
1516 */
1517 if (mid_start > 0)
1518 {
1519 lnum = wp->w_topline;
1520 idx = 0;
1521 srow = 0;
1522 if (scrolled_down)
1523 mid_start = top_end;
1524 else
1525 mid_start = 0;
1526 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1527 {
1528 if (wp->w_lines[idx].wl_valid)
1529 mid_start += wp->w_lines[idx].wl_size;
1530 else if (!scrolled_down)
1531 srow += wp->w_lines[idx].wl_size;
1532 ++idx;
1533# ifdef FEAT_FOLDING
1534 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1535 lnum = wp->w_lines[idx].wl_lnum;
1536 else
1537# endif
1538 ++lnum;
1539 }
1540 srow += mid_start;
1541 mid_end = wp->w_height;
1542 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1543 {
1544 if (wp->w_lines[idx].wl_valid
1545 && wp->w_lines[idx].wl_lnum >= to + 1)
1546 {
1547 /* Only update until first row of this line */
1548 mid_end = srow;
1549 break;
1550 }
1551 srow += wp->w_lines[idx].wl_size;
1552 }
1553 }
1554 }
1555
1556 if (VIsual_active && buf == curwin->w_buffer)
1557 {
1558 wp->w_old_visual_mode = VIsual_mode;
1559 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1560 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001561 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 wp->w_old_curswant = curwin->w_curswant;
1563 }
1564 else
1565 {
1566 wp->w_old_visual_mode = 0;
1567 wp->w_old_cursor_lnum = 0;
1568 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001569 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
1571#endif /* FEAT_VISUAL */
1572
1573#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1574 /* reset got_int, otherwise regexp won't work */
1575 save_got_int = got_int;
1576 got_int = 0;
1577#endif
1578#ifdef FEAT_FOLDING
1579 win_foldinfo.fi_level = 0;
1580#endif
1581
1582 /*
1583 * Update all the window rows.
1584 */
1585 idx = 0; /* first entry in w_lines[].wl_size */
1586 row = 0;
1587 srow = 0;
1588 lnum = wp->w_topline; /* first line shown in window */
1589 for (;;)
1590 {
1591 /* stop updating when reached the end of the window (check for _past_
1592 * the end of the window is at the end of the loop) */
1593 if (row == wp->w_height)
1594 {
1595 didline = TRUE;
1596 break;
1597 }
1598
1599 /* stop updating when hit the end of the file */
1600 if (lnum > buf->b_ml.ml_line_count)
1601 {
1602 eof = TRUE;
1603 break;
1604 }
1605
1606 /* Remember the starting row of the line that is going to be dealt
1607 * with. It is used further down when the line doesn't fit. */
1608 srow = row;
1609
1610 /*
1611 * Update a line when it is in an area that needs updating, when it
1612 * has changes or w_lines[idx] is invalid.
1613 * bot_start may be halfway a wrapped line after using
1614 * win_del_lines(), check if the current line includes it.
1615 * When syntax folding is being used, the saved syntax states will
1616 * already have been updated, we can't see where the syntax state is
1617 * the same again, just update until the end of the window.
1618 */
1619 if (row < top_end
1620 || (row >= mid_start && row < mid_end)
1621#ifdef FEAT_SEARCH_EXTRA
1622 || top_to_mod
1623#endif
1624 || idx >= wp->w_lines_valid
1625 || (row + wp->w_lines[idx].wl_size > bot_start)
1626 || (mod_top != 0
1627 && (lnum == mod_top
1628 || (lnum >= mod_top
1629 && (lnum < mod_bot
1630#ifdef FEAT_SYN_HL
1631 || did_update == DID_FOLD
1632 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001633 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 && (
1635# ifdef FEAT_FOLDING
1636 (foldmethodIsSyntax(wp)
1637 && hasAnyFolding(wp)) ||
1638# endif
1639 syntax_check_changed(lnum)))
1640#endif
1641 )))))
1642 {
1643#ifdef FEAT_SEARCH_EXTRA
1644 if (lnum == mod_top)
1645 top_to_mod = FALSE;
1646#endif
1647
1648 /*
1649 * When at start of changed lines: May scroll following lines
1650 * up or down to minimize redrawing.
1651 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001652 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 */
1654 if (lnum == mod_top
1655 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001656 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658 int old_rows = 0;
1659 int new_rows = 0;
1660 int xtra_rows;
1661 linenr_T l;
1662
1663 /* Count the old number of window rows, using w_lines[], which
1664 * should still contain the sizes for the lines as they are
1665 * currently displayed. */
1666 for (i = idx; i < wp->w_lines_valid; ++i)
1667 {
1668 /* Only valid lines have a meaningful wl_lnum. Invalid
1669 * lines are part of the changed area. */
1670 if (wp->w_lines[i].wl_valid
1671 && wp->w_lines[i].wl_lnum == mod_bot)
1672 break;
1673 old_rows += wp->w_lines[i].wl_size;
1674#ifdef FEAT_FOLDING
1675 if (wp->w_lines[i].wl_valid
1676 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1677 {
1678 /* Must have found the last valid entry above mod_bot.
1679 * Add following invalid entries. */
1680 ++i;
1681 while (i < wp->w_lines_valid
1682 && !wp->w_lines[i].wl_valid)
1683 old_rows += wp->w_lines[i++].wl_size;
1684 break;
1685 }
1686#endif
1687 }
1688
1689 if (i >= wp->w_lines_valid)
1690 {
1691 /* We can't find a valid line below the changed lines,
1692 * need to redraw until the end of the window.
1693 * Inserting/deleting lines has no use. */
1694 bot_start = 0;
1695 }
1696 else
1697 {
1698 /* Able to count old number of rows: Count new window
1699 * rows, and may insert/delete lines */
1700 j = idx;
1701 for (l = lnum; l < mod_bot; ++l)
1702 {
1703#ifdef FEAT_FOLDING
1704 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1705 ++new_rows;
1706 else
1707#endif
1708#ifdef FEAT_DIFF
1709 if (l == wp->w_topline)
1710 new_rows += plines_win_nofill(wp, l, TRUE)
1711 + wp->w_topfill;
1712 else
1713#endif
1714 new_rows += plines_win(wp, l, TRUE);
1715 ++j;
1716 if (new_rows > wp->w_height - row - 2)
1717 {
1718 /* it's getting too much, must redraw the rest */
1719 new_rows = 9999;
1720 break;
1721 }
1722 }
1723 xtra_rows = new_rows - old_rows;
1724 if (xtra_rows < 0)
1725 {
1726 /* May scroll text up. If there is not enough
1727 * remaining text or scrolling fails, must redraw the
1728 * rest. If scrolling works, must redraw the text
1729 * below the scrolled text. */
1730 if (row - xtra_rows >= wp->w_height - 2)
1731 mod_bot = MAXLNUM;
1732 else
1733 {
1734 check_for_delay(FALSE);
1735 if (win_del_lines(wp, row,
1736 -xtra_rows, FALSE, FALSE) == FAIL)
1737 mod_bot = MAXLNUM;
1738 else
1739 bot_start = wp->w_height + xtra_rows;
1740 }
1741 }
1742 else if (xtra_rows > 0)
1743 {
1744 /* May scroll text down. If there is not enough
1745 * remaining text of scrolling fails, must redraw the
1746 * rest. */
1747 if (row + xtra_rows >= wp->w_height - 2)
1748 mod_bot = MAXLNUM;
1749 else
1750 {
1751 check_for_delay(FALSE);
1752 if (win_ins_lines(wp, row + old_rows,
1753 xtra_rows, FALSE, FALSE) == FAIL)
1754 mod_bot = MAXLNUM;
1755 else if (top_end > row + old_rows)
1756 /* Scrolled the part at the top that requires
1757 * updating down. */
1758 top_end += xtra_rows;
1759 }
1760 }
1761
1762 /* When not updating the rest, may need to move w_lines[]
1763 * entries. */
1764 if (mod_bot != MAXLNUM && i != j)
1765 {
1766 if (j < i)
1767 {
1768 int x = row + new_rows;
1769
1770 /* move entries in w_lines[] upwards */
1771 for (;;)
1772 {
1773 /* stop at last valid entry in w_lines[] */
1774 if (i >= wp->w_lines_valid)
1775 {
1776 wp->w_lines_valid = j;
1777 break;
1778 }
1779 wp->w_lines[j] = wp->w_lines[i];
1780 /* stop at a line that won't fit */
1781 if (x + (int)wp->w_lines[j].wl_size
1782 > wp->w_height)
1783 {
1784 wp->w_lines_valid = j + 1;
1785 break;
1786 }
1787 x += wp->w_lines[j++].wl_size;
1788 ++i;
1789 }
1790 if (bot_start > x)
1791 bot_start = x;
1792 }
1793 else /* j > i */
1794 {
1795 /* move entries in w_lines[] downwards */
1796 j -= i;
1797 wp->w_lines_valid += j;
1798 if (wp->w_lines_valid > wp->w_height)
1799 wp->w_lines_valid = wp->w_height;
1800 for (i = wp->w_lines_valid; i - j >= idx; --i)
1801 wp->w_lines[i] = wp->w_lines[i - j];
1802
1803 /* The w_lines[] entries for inserted lines are
1804 * now invalid, but wl_size may be used above.
1805 * Reset to zero. */
1806 while (i >= idx)
1807 {
1808 wp->w_lines[i].wl_size = 0;
1809 wp->w_lines[i--].wl_valid = FALSE;
1810 }
1811 }
1812 }
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815
1816#ifdef FEAT_FOLDING
1817 /*
1818 * When lines are folded, display one line for all of them.
1819 * Otherwise, display normally (can be several display lines when
1820 * 'wrap' is on).
1821 */
1822 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1823 if (fold_count != 0)
1824 {
1825 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1826 ++row;
1827 --fold_count;
1828 wp->w_lines[idx].wl_folded = TRUE;
1829 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1830# ifdef FEAT_SYN_HL
1831 did_update = DID_FOLD;
1832# endif
1833 }
1834 else
1835#endif
1836 if (idx < wp->w_lines_valid
1837 && wp->w_lines[idx].wl_valid
1838 && wp->w_lines[idx].wl_lnum == lnum
1839 && lnum > wp->w_topline
1840 && !(dy_flags & DY_LASTLINE)
1841 && srow + wp->w_lines[idx].wl_size > wp->w_height
1842#ifdef FEAT_DIFF
1843 && diff_check_fill(wp, lnum) == 0
1844#endif
1845 )
1846 {
1847 /* This line is not going to fit. Don't draw anything here,
1848 * will draw "@ " lines below. */
1849 row = wp->w_height + 1;
1850 }
1851 else
1852 {
1853#ifdef FEAT_SEARCH_EXTRA
1854 prepare_search_hl(wp, lnum);
1855#endif
1856#ifdef FEAT_SYN_HL
1857 /* Let the syntax stuff know we skipped a few lines. */
1858 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001859 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 syntax_end_parsing(syntax_last_parsed + 1);
1861#endif
1862
1863 /*
1864 * Display one line.
1865 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001866 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868#ifdef FEAT_FOLDING
1869 wp->w_lines[idx].wl_folded = FALSE;
1870 wp->w_lines[idx].wl_lastlnum = lnum;
1871#endif
1872#ifdef FEAT_SYN_HL
1873 did_update = DID_LINE;
1874 syntax_last_parsed = lnum;
1875#endif
1876 }
1877
1878 wp->w_lines[idx].wl_lnum = lnum;
1879 wp->w_lines[idx].wl_valid = TRUE;
1880 if (row > wp->w_height) /* past end of screen */
1881 {
1882 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001883 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1885 ++idx;
1886 break;
1887 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001888 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 wp->w_lines[idx].wl_size = row - srow;
1890 ++idx;
1891#ifdef FEAT_FOLDING
1892 lnum += fold_count + 1;
1893#else
1894 ++lnum;
1895#endif
1896 }
1897 else
1898 {
1899 /* This line does not need updating, advance to the next one */
1900 row += wp->w_lines[idx++].wl_size;
1901 if (row > wp->w_height) /* past end of screen */
1902 break;
1903#ifdef FEAT_FOLDING
1904 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1905#else
1906 ++lnum;
1907#endif
1908#ifdef FEAT_SYN_HL
1909 did_update = DID_NONE;
1910#endif
1911 }
1912
1913 if (lnum > buf->b_ml.ml_line_count)
1914 {
1915 eof = TRUE;
1916 break;
1917 }
1918 }
1919 /*
1920 * End of loop over all window lines.
1921 */
1922
1923
1924 if (idx > wp->w_lines_valid)
1925 wp->w_lines_valid = idx;
1926
1927#ifdef FEAT_SYN_HL
1928 /*
1929 * Let the syntax stuff know we stop parsing here.
1930 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001931 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 syntax_end_parsing(syntax_last_parsed + 1);
1933#endif
1934
1935 /*
1936 * If we didn't hit the end of the file, and we didn't finish the last
1937 * line we were working on, then the line didn't fit.
1938 */
1939 wp->w_empty_rows = 0;
1940#ifdef FEAT_DIFF
1941 wp->w_filler_rows = 0;
1942#endif
1943 if (!eof && !didline)
1944 {
1945 if (lnum == wp->w_topline)
1946 {
1947 /*
1948 * Single line that does not fit!
1949 * Don't overwrite it, it can be edited.
1950 */
1951 wp->w_botline = lnum + 1;
1952 }
1953#ifdef FEAT_DIFF
1954 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1955 {
1956 /* Window ends in filler lines. */
1957 wp->w_botline = lnum;
1958 wp->w_filler_rows = wp->w_height - srow;
1959 }
1960#endif
1961 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1962 {
1963 /*
1964 * Last line isn't finished: Display "@@@" at the end.
1965 */
1966 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1967 W_WINROW(wp) + wp->w_height,
1968 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1969 '@', '@', hl_attr(HLF_AT));
1970 set_empty_rows(wp, srow);
1971 wp->w_botline = lnum;
1972 }
1973 else
1974 {
1975 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1976 wp->w_botline = lnum;
1977 }
1978 }
1979 else
1980 {
1981#ifdef FEAT_VERTSPLIT
1982 draw_vsep_win(wp, row);
1983#endif
1984 if (eof) /* we hit the end of the file */
1985 {
1986 wp->w_botline = buf->b_ml.ml_line_count + 1;
1987#ifdef FEAT_DIFF
1988 j = diff_check_fill(wp, wp->w_botline);
1989 if (j > 0 && !wp->w_botfill)
1990 {
1991 /*
1992 * Display filler lines at the end of the file
1993 */
1994 if (char2cells(fill_diff) > 1)
1995 i = '-';
1996 else
1997 i = fill_diff;
1998 if (row + j > wp->w_height)
1999 j = wp->w_height - row;
2000 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2001 row += j;
2002 }
2003#endif
2004 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002005 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 wp->w_botline = lnum;
2007
2008 /* make sure the rest of the screen is blank */
2009 /* put '~'s on rows that aren't part of the file. */
2010 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2011 }
2012
2013 /* Reset the type of redrawing required, the window has been updated. */
2014 wp->w_redr_type = 0;
2015#ifdef FEAT_DIFF
2016 wp->w_old_topfill = wp->w_topfill;
2017 wp->w_old_botfill = wp->w_botfill;
2018#endif
2019
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002020 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 /*
2023 * There is a trick with w_botline. If we invalidate it on each
2024 * change that might modify it, this will cause a lot of expensive
2025 * calls to plines() in update_topline() each time. Therefore the
2026 * value of w_botline is often approximated, and this value is used to
2027 * compute the value of w_topline. If the value of w_botline was
2028 * wrong, check that the value of w_topline is correct (cursor is on
2029 * the visible part of the text). If it's not, we need to redraw
2030 * again. Mostly this just means scrolling up a few lines, so it
2031 * doesn't look too bad. Only do this for the current window (where
2032 * changes are relevant).
2033 */
2034 wp->w_valid |= VALID_BOTLINE;
2035 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2036 {
2037 recursive = TRUE;
2038 curwin->w_valid &= ~VALID_TOPLINE;
2039 update_topline(); /* may invalidate w_botline again */
2040 if (must_redraw != 0)
2041 {
2042 /* Don't update for changes in buffer again. */
2043 i = curbuf->b_mod_set;
2044 curbuf->b_mod_set = FALSE;
2045 win_update(curwin);
2046 must_redraw = 0;
2047 curbuf->b_mod_set = i;
2048 }
2049 recursive = FALSE;
2050 }
2051 }
2052
2053#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2054 /* restore got_int, unless CTRL-C was hit while redrawing */
2055 if (!got_int)
2056 got_int = save_got_int;
2057#endif
2058}
2059
2060#ifdef FEAT_SIGNS
2061static int draw_signcolumn __ARGS((win_T *wp));
2062
2063/*
2064 * Return TRUE when window "wp" has a column to draw signs in.
2065 */
2066 static int
2067draw_signcolumn(wp)
2068 win_T *wp;
2069{
2070 return (wp->w_buffer->b_signlist != NULL
2071# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002072 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073# endif
2074 );
2075}
2076#endif
2077
2078/*
2079 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2080 * as the filler character.
2081 */
2082 static void
2083win_draw_end(wp, c1, c2, row, endrow, hl)
2084 win_T *wp;
2085 int c1;
2086 int c2;
2087 int row;
2088 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002089 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
2091#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2092 int n = 0;
2093# define FDC_OFF n
2094#else
2095# define FDC_OFF 0
2096#endif
2097
2098#ifdef FEAT_RIGHTLEFT
2099 if (wp->w_p_rl)
2100 {
2101 /* No check for cmdline window: should never be right-left. */
2102# ifdef FEAT_FOLDING
2103 n = wp->w_p_fdc;
2104
2105 if (n > 0)
2106 {
2107 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002108 if (n > W_WIDTH(wp))
2109 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2111 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2112 ' ', ' ', hl_attr(HLF_FC));
2113 }
2114# endif
2115# ifdef FEAT_SIGNS
2116 if (draw_signcolumn(wp))
2117 {
2118 int nn = n + 2;
2119
2120 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002121 if (nn > W_WIDTH(wp))
2122 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2124 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2125 ' ', ' ', hl_attr(HLF_SC));
2126 n = nn;
2127 }
2128# endif
2129 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2130 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2131 c2, c2, hl_attr(hl));
2132 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2133 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2134 c1, c2, hl_attr(hl));
2135 }
2136 else
2137#endif
2138 {
2139#ifdef FEAT_CMDWIN
2140 if (cmdwin_type != 0 && wp == curwin)
2141 {
2142 /* draw the cmdline character in the leftmost column */
2143 n = 1;
2144 if (n > wp->w_width)
2145 n = wp->w_width;
2146 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2147 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2148 cmdwin_type, ' ', hl_attr(HLF_AT));
2149 }
2150#endif
2151#ifdef FEAT_FOLDING
2152 if (wp->w_p_fdc > 0)
2153 {
2154 int nn = n + wp->w_p_fdc;
2155
2156 /* draw the fold column at the left */
2157 if (nn > W_WIDTH(wp))
2158 nn = W_WIDTH(wp);
2159 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2160 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2161 ' ', ' ', hl_attr(HLF_FC));
2162 n = nn;
2163 }
2164#endif
2165#ifdef FEAT_SIGNS
2166 if (draw_signcolumn(wp))
2167 {
2168 int nn = n + 2;
2169
2170 /* draw the sign column after the fold column */
2171 if (nn > W_WIDTH(wp))
2172 nn = W_WIDTH(wp);
2173 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2174 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2175 ' ', ' ', hl_attr(HLF_SC));
2176 n = nn;
2177 }
2178#endif
2179 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2180 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2181 c1, c2, hl_attr(hl));
2182 }
2183 set_empty_rows(wp, row);
2184}
2185
Bram Moolenaar1a384422010-07-14 19:53:30 +02002186#ifdef FEAT_SYN_HL
2187static int advance_color_col __ARGS((int vcol, int **color_cols));
2188
2189/*
2190 * Advance **color_cols and return TRUE when there are columns to draw.
2191 */
2192 static int
2193advance_color_col(vcol, color_cols)
2194 int vcol;
2195 int **color_cols;
2196{
2197 while (**color_cols >= 0 && vcol > **color_cols)
2198 ++*color_cols;
2199 return (**color_cols >= 0);
2200}
2201#endif
2202
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203#ifdef FEAT_FOLDING
2204/*
2205 * Display one folded line.
2206 */
2207 static void
2208fold_line(wp, fold_count, foldinfo, lnum, row)
2209 win_T *wp;
2210 long fold_count;
2211 foldinfo_T *foldinfo;
2212 linenr_T lnum;
2213 int row;
2214{
2215 char_u buf[51];
2216 pos_T *top, *bot;
2217 linenr_T lnume = lnum + fold_count - 1;
2218 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002219 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 int col;
2222 int txtcol;
2223 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002224 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 /* Build the fold line:
2227 * 1. Add the cmdwin_type for the command-line window
2228 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002229 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 * 4. Compose the text
2231 * 5. Add the text
2232 * 6. set highlighting for the Visual area an other text
2233 */
2234 col = 0;
2235
2236 /*
2237 * 1. Add the cmdwin_type for the command-line window
2238 * Ignores 'rightleft', this window is never right-left.
2239 */
2240#ifdef FEAT_CMDWIN
2241 if (cmdwin_type != 0 && wp == curwin)
2242 {
2243 ScreenLines[off] = cmdwin_type;
2244 ScreenAttrs[off] = hl_attr(HLF_AT);
2245#ifdef FEAT_MBYTE
2246 if (enc_utf8)
2247 ScreenLinesUC[off] = 0;
2248#endif
2249 ++col;
2250 }
2251#endif
2252
2253 /*
2254 * 2. Add the 'foldcolumn'
2255 */
2256 fdc = wp->w_p_fdc;
2257 if (fdc > W_WIDTH(wp) - col)
2258 fdc = W_WIDTH(wp) - col;
2259 if (fdc > 0)
2260 {
2261 fill_foldcolumn(buf, wp, TRUE, lnum);
2262#ifdef FEAT_RIGHTLEFT
2263 if (wp->w_p_rl)
2264 {
2265 int i;
2266
2267 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2268 hl_attr(HLF_FC));
2269 /* reverse the fold column */
2270 for (i = 0; i < fdc; ++i)
2271 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2272 }
2273 else
2274#endif
2275 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2276 col += fdc;
2277 }
2278
2279#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002280# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2281 for (ri = 0; ri < l; ++ri) \
2282 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2283 else \
2284 for (ri = 0; ri < l; ++ri) \
2285 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002287# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2288 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#endif
2290
Bram Moolenaar64486672010-05-16 15:46:46 +02002291 /* Set all attributes of the 'number' or 'relativenumber' column and the
2292 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002293 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
2295#ifdef FEAT_SIGNS
2296 /* If signs are being displayed, add two spaces. */
2297 if (draw_signcolumn(wp))
2298 {
2299 len = W_WIDTH(wp) - col;
2300 if (len > 0)
2301 {
2302 if (len > 2)
2303 len = 2;
2304# ifdef FEAT_RIGHTLEFT
2305 if (wp->w_p_rl)
2306 /* the line number isn't reversed */
2307 copy_text_attr(off + W_WIDTH(wp) - len - col,
2308 (char_u *)" ", len, hl_attr(HLF_FL));
2309 else
2310# endif
2311 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2312 col += len;
2313 }
2314 }
2315#endif
2316
2317 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002318 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002320 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 len = W_WIDTH(wp) - col;
2323 if (len > 0)
2324 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002325 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002326 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01002327 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002328
2329 if (len > w + 1)
2330 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002331
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002332 if (wp->w_p_nu && !wp->w_p_rnu)
2333 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002334 num = (long)lnum;
2335 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002336 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002337 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002338 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002339 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002340 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002341 /* 'number' + 'relativenumber': cursor line shows absolute
2342 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002343 num = lnum;
2344 fmt = "%-*ld ";
2345 }
2346 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002347
Bram Moolenaar700e7342013-01-30 12:31:36 +01002348 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#ifdef FEAT_RIGHTLEFT
2350 if (wp->w_p_rl)
2351 /* the line number isn't reversed */
2352 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2353 hl_attr(HLF_FL));
2354 else
2355#endif
2356 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2357 col += len;
2358 }
2359 }
2360
2361 /*
2362 * 4. Compose the folded-line string with 'foldtext', if set.
2363 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002364 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366 txtcol = col; /* remember where text starts */
2367
2368 /*
2369 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2370 * Right-left text is put in columns 0 - number-col, normal text is put
2371 * in columns number-col - window-width.
2372 */
2373#ifdef FEAT_MBYTE
2374 if (has_mbyte)
2375 {
2376 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002377 int u8c, u8cc[MAX_MCO];
2378 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 int idx;
2380 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002381 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382# ifdef FEAT_ARABIC
2383 int prev_c = 0; /* previous Arabic character */
2384 int prev_c1 = 0; /* first composing char for prev_c */
2385# endif
2386
2387# ifdef FEAT_RIGHTLEFT
2388 if (wp->w_p_rl)
2389 idx = off;
2390 else
2391# endif
2392 idx = off + col;
2393
2394 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2395 for (p = text; *p != NUL; )
2396 {
2397 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002398 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 if (col + cells > W_WIDTH(wp)
2400# ifdef FEAT_RIGHTLEFT
2401 - (wp->w_p_rl ? col : 0)
2402# endif
2403 )
2404 break;
2405 ScreenLines[idx] = *p;
2406 if (enc_utf8)
2407 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002408 u8c = utfc_ptr2char(p, u8cc);
2409 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
2411 ScreenLinesUC[idx] = 0;
2412#ifdef FEAT_ARABIC
2413 prev_c = u8c;
2414#endif
2415 }
2416 else
2417 {
2418#ifdef FEAT_ARABIC
2419 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2420 {
2421 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002422 int pc, pc1, nc;
2423 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 int firstbyte = *p;
2425
2426 /* The idea of what is the previous and next
2427 * character depends on 'rightleft'. */
2428 if (wp->w_p_rl)
2429 {
2430 pc = prev_c;
2431 pc1 = prev_c1;
2432 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002433 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 else
2436 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002437 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002439 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 }
2441 prev_c = u8c;
2442
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002443 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 pc, pc1, nc);
2445 ScreenLines[idx] = firstbyte;
2446 }
2447 else
2448 prev_c = u8c;
2449#endif
2450 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002451#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (u8c >= 0x10000)
2453 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2454 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002457 for (i = 0; i < Screen_mco; ++i)
2458 {
2459 ScreenLinesC[i][idx] = u8cc[i];
2460 if (u8cc[i] == 0)
2461 break;
2462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464 if (cells > 1)
2465 ScreenLines[idx + 1] = 0;
2466 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002467 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2468 /* double-byte single width character */
2469 ScreenLines2[idx] = p[1];
2470 else if (cells > 1)
2471 /* double-width character */
2472 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 col += cells;
2474 idx += cells;
2475 p += c_len;
2476 }
2477 }
2478 else
2479#endif
2480 {
2481 len = (int)STRLEN(text);
2482 if (len > W_WIDTH(wp) - col)
2483 len = W_WIDTH(wp) - col;
2484 if (len > 0)
2485 {
2486#ifdef FEAT_RIGHTLEFT
2487 if (wp->w_p_rl)
2488 STRNCPY(current_ScreenLine, text, len);
2489 else
2490#endif
2491 STRNCPY(current_ScreenLine + col, text, len);
2492 col += len;
2493 }
2494 }
2495
2496 /* Fill the rest of the line with the fold filler */
2497#ifdef FEAT_RIGHTLEFT
2498 if (wp->w_p_rl)
2499 col -= txtcol;
2500#endif
2501 while (col < W_WIDTH(wp)
2502#ifdef FEAT_RIGHTLEFT
2503 - (wp->w_p_rl ? txtcol : 0)
2504#endif
2505 )
2506 {
2507#ifdef FEAT_MBYTE
2508 if (enc_utf8)
2509 {
2510 if (fill_fold >= 0x80)
2511 {
2512 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002513 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
2515 else
2516 ScreenLinesUC[off + col] = 0;
2517 }
2518#endif
2519 ScreenLines[off + col++] = fill_fold;
2520 }
2521
2522 if (text != buf)
2523 vim_free(text);
2524
2525 /*
2526 * 6. set highlighting for the Visual area an other text.
2527 * If all folded lines are in the Visual area, highlight the line.
2528 */
2529#ifdef FEAT_VISUAL
2530 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2531 {
2532 if (ltoreq(curwin->w_cursor, VIsual))
2533 {
2534 /* Visual is after curwin->w_cursor */
2535 top = &curwin->w_cursor;
2536 bot = &VIsual;
2537 }
2538 else
2539 {
2540 /* Visual is before curwin->w_cursor */
2541 top = &VIsual;
2542 bot = &curwin->w_cursor;
2543 }
2544 if (lnum >= top->lnum
2545 && lnume <= bot->lnum
2546 && (VIsual_mode != 'v'
2547 || ((lnum > top->lnum
2548 || (lnum == top->lnum
2549 && top->col == 0))
2550 && (lnume < bot->lnum
2551 || (lnume == bot->lnum
2552 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002553 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 if (VIsual_mode == Ctrl_V)
2556 {
2557 /* Visual block mode: highlight the chars part of the block */
2558 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2559 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002560 if (wp->w_old_cursor_lcol != MAXCOL
2561 && wp->w_old_cursor_lcol + txtcol
2562 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 len = wp->w_old_cursor_lcol;
2564 else
2565 len = W_WIDTH(wp) - txtcol;
2566 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002567 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 }
2570 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002573 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 }
2576 }
2577#endif
2578
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002579#ifdef FEAT_SYN_HL
2580 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002581 if (wp->w_p_cuc)
2582 {
2583 txtcol += wp->w_virtcol;
2584 if (wp->w_p_wrap)
2585 txtcol -= wp->w_skipcol;
2586 else
2587 txtcol -= wp->w_leftcol;
2588 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2589 ScreenAttrs[off + txtcol] = hl_combine_attr(
2590 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2591 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2595 (int)W_WIDTH(wp), FALSE);
2596
2597 /*
2598 * Update w_cline_height and w_cline_folded if the cursor line was
2599 * updated (saves a call to plines() later).
2600 */
2601 if (wp == curwin
2602 && lnum <= curwin->w_cursor.lnum
2603 && lnume >= curwin->w_cursor.lnum)
2604 {
2605 curwin->w_cline_row = row;
2606 curwin->w_cline_height = 1;
2607 curwin->w_cline_folded = TRUE;
2608 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2609 }
2610}
2611
2612/*
2613 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2614 */
2615 static void
2616copy_text_attr(off, buf, len, attr)
2617 int off;
2618 char_u *buf;
2619 int len;
2620 int attr;
2621{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002622 int i;
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 mch_memmove(ScreenLines + off, buf, (size_t)len);
2625# ifdef FEAT_MBYTE
2626 if (enc_utf8)
2627 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2628# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002629 for (i = 0; i < len; ++i)
2630 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631}
2632
2633/*
2634 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002635 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 */
2637 static void
2638fill_foldcolumn(p, wp, closed, lnum)
2639 char_u *p;
2640 win_T *wp;
2641 int closed; /* TRUE of FALSE */
2642 linenr_T lnum; /* current line number */
2643{
2644 int i = 0;
2645 int level;
2646 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002647 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
2649 /* Init to all spaces. */
2650 copy_spaces(p, (size_t)wp->w_p_fdc);
2651
2652 level = win_foldinfo.fi_level;
2653 if (level > 0)
2654 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002655 /* If there is only one column put more info in it. */
2656 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 /* If the column is too narrow, we start at the lowest level that
2659 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002660 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (first_level < 1)
2662 first_level = 1;
2663
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002664 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 {
2666 if (win_foldinfo.fi_lnum == lnum
2667 && first_level + i >= win_foldinfo.fi_low_level)
2668 p[i] = '-';
2669 else if (first_level == 1)
2670 p[i] = '|';
2671 else if (first_level + i <= 9)
2672 p[i] = '0' + first_level + i;
2673 else
2674 p[i] = '>';
2675 if (first_level + i == level)
2676 break;
2677 }
2678 }
2679 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002680 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681}
2682#endif /* FEAT_FOLDING */
2683
2684/*
2685 * Display line "lnum" of window 'wp' on the screen.
2686 * Start at row "startrow", stop when "endrow" is reached.
2687 * wp->w_virtcol needs to be valid.
2688 *
2689 * Return the number of last row the line occupies.
2690 */
2691 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002692win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 win_T *wp;
2694 linenr_T lnum;
2695 int startrow;
2696 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002697 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 int col; /* visual column on screen */
2700 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2701 int c = 0; /* init for GCC */
2702 long vcol = 0; /* virtual column (for tabs) */
2703 long vcol_prev = -1; /* "vcol" of previous character */
2704 char_u *line; /* current line */
2705 char_u *ptr; /* current position in "line" */
2706 int row; /* row in the window, excl w_winrow */
2707 int screen_row; /* row on the screen, incl w_winrow */
2708
2709 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2710 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002711 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 int c_extra = NUL; /* extra chars, all the same */
2713 int extra_attr = 0; /* attributes when n_extra != 0 */
2714 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2715 displaying lcs_eol at end-of-line */
2716 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2717 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2718
2719 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2720 int saved_n_extra = 0;
2721 char_u *saved_p_extra = NULL;
2722 int saved_c_extra = 0;
2723 int saved_char_attr = 0;
2724
2725 int n_attr = 0; /* chars with special attr */
2726 int saved_attr2 = 0; /* char_attr saved for n_attr */
2727 int n_attr3 = 0; /* chars with overruling special attr */
2728 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2729
2730 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2731
2732 int fromcol, tocol; /* start/end of inverting */
2733 int fromcol_prev = -2; /* start of inverting after cursor */
2734 int noinvcur = FALSE; /* don't invert the cursor */
2735#ifdef FEAT_VISUAL
2736 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002737 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738#endif
2739 pos_T pos;
2740 long v;
2741
2742 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002743 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2745 in this line */
2746 int attr = 0; /* attributes for area highlighting */
2747 int area_attr = 0; /* attributes desired by highlighting */
2748 int search_attr = 0; /* attributes desired by 'hlsearch' */
2749#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002750 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 int syntax_attr = 0; /* attributes desired by syntax */
2752 int has_syntax = FALSE; /* this buffer has syntax highl. */
2753 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002754 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002755 int draw_color_col = FALSE; /* highlight colorcolumn */
2756 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002757#endif
2758#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002759 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002760# define SPWORDLEN 150
2761 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002762 int nextlinecol = 0; /* column where nextline[] starts */
2763 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002764 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002765 int spell_attr = 0; /* attributes desired by spelling */
2766 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002767 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2768 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002769 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002770 static int cap_col = -1; /* column to check for Cap word */
2771 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002772 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773#endif
2774 int extra_check; /* has syntax or linebreak */
2775#ifdef FEAT_MBYTE
2776 int multi_attr = 0; /* attributes desired by multibyte */
2777 int mb_l = 1; /* multi-byte byte length */
2778 int mb_c = 0; /* decoded multi-byte character */
2779 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002780 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781#endif
2782#ifdef FEAT_DIFF
2783 int filler_lines; /* nr of filler lines to be drawn */
2784 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002785 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 int change_start = MAXCOL; /* first col of changed area */
2787 int change_end = -1; /* last col of changed area */
2788#endif
2789 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2790#ifdef FEAT_LINEBREAK
2791 int need_showbreak = FALSE;
2792#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002793#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2794 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002796 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797#endif
2798#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002799 matchitem_T *cur; /* points to the match list */
2800 match_T *shl; /* points to search_hl or a match */
2801 int shl_flag; /* flag to indicate whether search_hl
2802 has been processed or not */
2803 int prevcol_hl_flag; /* flag to indicate whether prevcol
2804 equals startcol of search_hl or one
2805 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806#endif
2807#ifdef FEAT_ARABIC
2808 int prev_c = 0; /* previous Arabic character */
2809 int prev_c1 = 0; /* first composing char for prev_c */
2810#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002811#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002812 int did_line_attr = 0;
2813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815 /* draw_state: items that are drawn in sequence: */
2816#define WL_START 0 /* nothing done yet */
2817#ifdef FEAT_CMDWIN
2818# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2819#else
2820# define WL_CMDLINE WL_START
2821#endif
2822#ifdef FEAT_FOLDING
2823# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2824#else
2825# define WL_FOLD WL_CMDLINE
2826#endif
2827#ifdef FEAT_SIGNS
2828# define WL_SIGN WL_FOLD + 1 /* column for signs */
2829#else
2830# define WL_SIGN WL_FOLD /* column for signs */
2831#endif
2832#define WL_NR WL_SIGN + 1 /* line number */
2833#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2834# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2835#else
2836# define WL_SBR WL_NR
2837#endif
2838#define WL_LINE WL_SBR + 1 /* text in the line */
2839 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002840#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 int feedback_col = 0;
2842 int feedback_old_attr = -1;
2843#endif
2844
Bram Moolenaar860cae12010-06-05 23:22:07 +02002845#ifdef FEAT_CONCEAL
2846 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002847 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002848 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002849 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002850 int is_concealing = FALSE;
2851 int boguscols = 0; /* nonexistent columns added to force
2852 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002853 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002854 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002855# define VCOL_HLC (vcol - vcol_off)
2856#else
2857# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
2860 if (startrow > endrow) /* past the end already! */
2861 return startrow;
2862
2863 row = startrow;
2864 screen_row = row + W_WINROW(wp);
2865
2866 /*
2867 * To speed up the loop below, set extra_check when there is linebreak,
2868 * trailing white space and/or syntax processing to be done.
2869 */
2870#ifdef FEAT_LINEBREAK
2871 extra_check = wp->w_p_lbr;
2872#else
2873 extra_check = 0;
2874#endif
2875#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002876 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 {
2878 /* Prepare for syntax highlighting in this line. When there is an
2879 * error, stop syntax highlighting. */
2880 save_did_emsg = did_emsg;
2881 did_emsg = FALSE;
2882 syntax_start(wp, lnum);
2883 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002884 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else
2886 {
2887 did_emsg = save_did_emsg;
2888 has_syntax = TRUE;
2889 extra_check = TRUE;
2890 }
2891 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002892
2893 /* Check for columns to display for 'colorcolumn'. */
2894 color_cols = wp->w_p_cc_cols;
2895 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002896 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002897#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002898
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002899#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002900 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002901 && *wp->w_s->b_p_spl != NUL
2902 && wp->w_s->b_langp.ga_len > 0
2903 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002904 {
2905 /* Prepare for spell checking. */
2906 has_spell = TRUE;
2907 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002908
2909 /* Get the start of the next line, so that words that wrap to the next
2910 * line are found too: "et<line-break>al.".
2911 * Trick: skip a few chars for C/shell/Vim comments */
2912 nextline[SPWORDLEN] = NUL;
2913 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2914 {
2915 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2916 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2917 }
2918
2919 /* When a word wrapped from the previous line the start of the current
2920 * line is valid. */
2921 if (lnum == checked_lnum)
2922 cur_checked_col = checked_col;
2923 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002924
2925 /* When there was a sentence end in the previous line may require a
2926 * word starting with capital in this line. In line 1 always check
2927 * the first word. */
2928 if (lnum != capcol_lnum)
2929 cap_col = -1;
2930 if (lnum == 1)
2931 cap_col = 0;
2932 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934#endif
2935
2936 /*
2937 * handle visual active in this window
2938 */
2939 fromcol = -10;
2940 tocol = MAXCOL;
2941#ifdef FEAT_VISUAL
2942 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2943 {
2944 /* Visual is after curwin->w_cursor */
2945 if (ltoreq(curwin->w_cursor, VIsual))
2946 {
2947 top = &curwin->w_cursor;
2948 bot = &VIsual;
2949 }
2950 else /* Visual is before curwin->w_cursor */
2951 {
2952 top = &VIsual;
2953 bot = &curwin->w_cursor;
2954 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002955 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (VIsual_mode == Ctrl_V) /* block mode */
2957 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002958 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
2960 fromcol = wp->w_old_cursor_fcol;
2961 tocol = wp->w_old_cursor_lcol;
2962 }
2963 }
2964 else /* non-block mode */
2965 {
2966 if (lnum > top->lnum && lnum <= bot->lnum)
2967 fromcol = 0;
2968 else if (lnum == top->lnum)
2969 {
2970 if (VIsual_mode == 'V') /* linewise */
2971 fromcol = 0;
2972 else
2973 {
2974 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2975 if (gchar_pos(top) == NUL)
2976 tocol = fromcol + 1;
2977 }
2978 }
2979 if (VIsual_mode != 'V' && lnum == bot->lnum)
2980 {
2981 if (*p_sel == 'e' && bot->col == 0
2982#ifdef FEAT_VIRTUALEDIT
2983 && bot->coladd == 0
2984#endif
2985 )
2986 {
2987 fromcol = -10;
2988 tocol = MAXCOL;
2989 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002990 else if (bot->col == MAXCOL)
2991 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 else
2993 {
2994 pos = *bot;
2995 if (*p_sel == 'e')
2996 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2997 else
2998 {
2999 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3000 ++tocol;
3001 }
3002 }
3003 }
3004 }
3005
3006#ifndef MSDOS
3007 /* Check if the character under the cursor should not be inverted */
3008 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3009# ifdef FEAT_GUI
3010 && !gui.in_use
3011# endif
3012 )
3013 noinvcur = TRUE;
3014#endif
3015
3016 /* if inverting in this line set area_highlighting */
3017 if (fromcol >= 0)
3018 {
3019 area_highlighting = TRUE;
3020 attr = hl_attr(HLF_V);
3021#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003022 if ((clip_star.available && !clip_star.owned
3023 && clip_isautosel_star())
3024 || (clip_plus.available && !clip_plus.owned
3025 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 attr = hl_attr(HLF_VNC);
3027#endif
3028 }
3029 }
3030
3031 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003032 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 */
3034 else
3035#endif /* FEAT_VISUAL */
3036 if (highlight_match
3037 && wp == curwin
3038 && lnum >= curwin->w_cursor.lnum
3039 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3040 {
3041 if (lnum == curwin->w_cursor.lnum)
3042 getvcol(curwin, &(curwin->w_cursor),
3043 (colnr_T *)&fromcol, NULL, NULL);
3044 else
3045 fromcol = 0;
3046 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3047 {
3048 pos.lnum = lnum;
3049 pos.col = search_match_endcol;
3050 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3051 }
3052 else
3053 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003054 /* do at least one character; happens when past end of line */
3055 if (fromcol == tocol)
3056 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 area_highlighting = TRUE;
3058 attr = hl_attr(HLF_I);
3059 }
3060
3061#ifdef FEAT_DIFF
3062 filler_lines = diff_check(wp, lnum);
3063 if (filler_lines < 0)
3064 {
3065 if (filler_lines == -1)
3066 {
3067 if (diff_find_change(wp, lnum, &change_start, &change_end))
3068 diff_hlf = HLF_ADD; /* added line */
3069 else if (change_start == 0)
3070 diff_hlf = HLF_TXD; /* changed text */
3071 else
3072 diff_hlf = HLF_CHD; /* changed line */
3073 }
3074 else
3075 diff_hlf = HLF_ADD; /* added line */
3076 filler_lines = 0;
3077 area_highlighting = TRUE;
3078 }
3079 if (lnum == wp->w_topline)
3080 filler_lines = wp->w_topfill;
3081 filler_todo = filler_lines;
3082#endif
3083
3084#ifdef LINE_ATTR
3085# ifdef FEAT_SIGNS
3086 /* If this line has a sign with line highlighting set line_attr. */
3087 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3088 if (v != 0)
3089 line_attr = sign_get_attr((int)v, TRUE);
3090# endif
3091# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3092 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003093 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 line_attr = hl_attr(HLF_L);
3095# endif
3096 if (line_attr != 0)
3097 area_highlighting = TRUE;
3098#endif
3099
3100 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3101 ptr = line;
3102
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003103#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003104 if (has_spell)
3105 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003106 /* For checking first word with a capital skip white space. */
3107 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003108 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003109
Bram Moolenaar30abd282005-06-22 22:35:10 +00003110 /* To be able to spell-check over line boundaries copy the end of the
3111 * current line into nextline[]. Above the start of the next line was
3112 * copied to nextline[SPWORDLEN]. */
3113 if (nextline[SPWORDLEN] == NUL)
3114 {
3115 /* No next line or it is empty. */
3116 nextlinecol = MAXCOL;
3117 nextline_idx = 0;
3118 }
3119 else
3120 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003121 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003122 if (v < SPWORDLEN)
3123 {
3124 /* Short line, use it completely and append the start of the
3125 * next line. */
3126 nextlinecol = 0;
3127 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003128 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003129 nextline_idx = v + 1;
3130 }
3131 else
3132 {
3133 /* Long line, use only the last SPWORDLEN bytes. */
3134 nextlinecol = v - SPWORDLEN;
3135 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3136 nextline_idx = SPWORDLEN + 1;
3137 }
3138 }
3139 }
3140#endif
3141
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 /* find start of trailing whitespace */
3143 if (wp->w_p_list && lcs_trail)
3144 {
3145 trailcol = (colnr_T)STRLEN(ptr);
3146 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3147 --trailcol;
3148 trailcol += (colnr_T) (ptr - line);
3149 extra_check = TRUE;
3150 }
3151
3152 /*
3153 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3154 * first character to be displayed.
3155 */
3156 if (wp->w_p_wrap)
3157 v = wp->w_skipcol;
3158 else
3159 v = wp->w_leftcol;
3160 if (v > 0)
3161 {
3162#ifdef FEAT_MBYTE
3163 char_u *prev_ptr = ptr;
3164#endif
3165 while (vcol < v && *ptr != NUL)
3166 {
3167 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3168 vcol += c;
3169#ifdef FEAT_MBYTE
3170 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003172 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
3174
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003175#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3176 /* When:
3177 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003178 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003179 * - 'virtualedit' is set, or
3180 * - the visual mode is active,
3181 * the end of the line may be before the start of the displayed part.
3182 */
3183 if (vcol < v && (
3184# ifdef FEAT_SYN_HL
3185 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003186 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003187# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3188 ||
3189# endif
3190# endif
3191# ifdef FEAT_VIRTUALEDIT
3192 virtual_active()
3193# ifdef FEAT_VISUAL
3194 ||
3195# endif
3196# endif
3197# ifdef FEAT_VISUAL
3198 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3199# endif
3200 ))
3201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#endif
3205
3206 /* Handle a character that's not completely on the screen: Put ptr at
3207 * that character but skip the first few screen characters. */
3208 if (vcol > v)
3209 {
3210 vcol -= c;
3211#ifdef FEAT_MBYTE
3212 ptr = prev_ptr;
3213#else
3214 --ptr;
3215#endif
3216 n_skip = v - vcol;
3217 }
3218
3219 /*
3220 * Adjust for when the inverted text is before the screen,
3221 * and when the start of the inverted text is before the screen.
3222 */
3223 if (tocol <= vcol)
3224 fromcol = 0;
3225 else if (fromcol >= 0 && fromcol < vcol)
3226 fromcol = vcol;
3227
3228#ifdef FEAT_LINEBREAK
3229 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3230 if (wp->w_p_wrap)
3231 need_showbreak = TRUE;
3232#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003233#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003234 /* When spell checking a word we need to figure out the start of the
3235 * word and if it's badly spelled or not. */
3236 if (has_spell)
3237 {
3238 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003239 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003240 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003241
3242 pos = wp->w_cursor;
3243 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003244 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003245 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003246
3247 /* spell_move_to() may call ml_get() and make "line" invalid */
3248 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3249 ptr = line + linecol;
3250
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003251 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003252 {
3253 /* no bad word found at line start, don't check until end of a
3254 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003255 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003256 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003257 }
3258 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003259 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003260 /* bad word found, use attributes until end of word */
3261 word_end = wp->w_cursor.col + len + 1;
3262
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003263 /* Turn index into actual attributes. */
3264 if (spell_hlf != HLF_COUNT)
3265 spell_attr = highlight_attr[spell_hlf];
3266 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003267 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003268
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003269# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003270 /* Need to restart syntax highlighting for this line. */
3271 if (has_syntax)
3272 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003273# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003274 }
3275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277
3278 /*
3279 * Correct highlighting for cursor that can't be disabled.
3280 * Avoids having to check this for each character.
3281 */
3282 if (fromcol >= 0)
3283 {
3284 if (noinvcur)
3285 {
3286 if ((colnr_T)fromcol == wp->w_virtcol)
3287 {
3288 /* highlighting starts at cursor, let it start just after the
3289 * cursor */
3290 fromcol_prev = fromcol;
3291 fromcol = -1;
3292 }
3293 else if ((colnr_T)fromcol < wp->w_virtcol)
3294 /* restart highlighting after the cursor */
3295 fromcol_prev = wp->w_virtcol;
3296 }
3297 if (fromcol >= tocol)
3298 fromcol = -1;
3299 }
3300
3301#ifdef FEAT_SEARCH_EXTRA
3302 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003303 * Handle highlighting the last used search pattern and matches.
3304 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003306 cur = wp->w_match_head;
3307 shl_flag = FALSE;
3308 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003310 if (shl_flag == FALSE)
3311 {
3312 shl = &search_hl;
3313 shl_flag = TRUE;
3314 }
3315 else
3316 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003317 shl->startcol = MAXCOL;
3318 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 shl->attr_cur = 0;
3320 if (shl->rm.regprog != NULL)
3321 {
3322 v = (long)(ptr - line);
3323 next_search_hl(wp, shl, lnum, (colnr_T)v);
3324
3325 /* Need to get the line again, a multi-line regexp may have made it
3326 * invalid. */
3327 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3328 ptr = line + v;
3329
3330 if (shl->lnum != 0 && shl->lnum <= lnum)
3331 {
3332 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003333 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003335 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3337 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003338 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003340 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003342 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
3344#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003345 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003346 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 else
3348#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003349 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003351 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
3353 shl->attr_cur = shl->attr;
3354 search_attr = shl->attr;
3355 }
3356 area_highlighting = TRUE;
3357 }
3358 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003359 if (shl != &search_hl && cur != NULL)
3360 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
3362#endif
3363
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003364#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003365 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3366 * active, because it's not clear what is selected then. */
3367 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003368 {
3369 line_attr = hl_attr(HLF_CUL);
3370 area_highlighting = TRUE;
3371 }
3372#endif
3373
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003374 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 col = 0;
3376#ifdef FEAT_RIGHTLEFT
3377 if (wp->w_p_rl)
3378 {
3379 /* Rightleft window: process the text in the normal direction, but put
3380 * it in current_ScreenLine[] from right to left. Start at the
3381 * rightmost column of the window. */
3382 col = W_WIDTH(wp) - 1;
3383 off += col;
3384 }
3385#endif
3386
3387 /*
3388 * Repeat for the whole displayed line.
3389 */
3390 for (;;)
3391 {
3392 /* Skip this quickly when working on the text. */
3393 if (draw_state != WL_LINE)
3394 {
3395#ifdef FEAT_CMDWIN
3396 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3397 {
3398 draw_state = WL_CMDLINE;
3399 if (cmdwin_type != 0 && wp == curwin)
3400 {
3401 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003403 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 char_attr = hl_attr(HLF_AT);
3405 }
3406 }
3407#endif
3408
3409#ifdef FEAT_FOLDING
3410 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3411 {
3412 draw_state = WL_FOLD;
3413 if (wp->w_p_fdc > 0)
3414 {
3415 /* Draw the 'foldcolumn'. */
3416 fill_foldcolumn(extra, wp, FALSE, lnum);
3417 n_extra = wp->w_p_fdc;
3418 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003419 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 c_extra = NUL;
3421 char_attr = hl_attr(HLF_FC);
3422 }
3423 }
3424#endif
3425
3426#ifdef FEAT_SIGNS
3427 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3428 {
3429 draw_state = WL_SIGN;
3430 /* Show the sign column when there are any signs in this
3431 * buffer or when using Netbeans. */
3432 if (draw_signcolumn(wp)
3433# ifdef FEAT_DIFF
3434 && filler_todo <= 0
3435# endif
3436 )
3437 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003438 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003440 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441# endif
3442
3443 /* Draw two cells with the sign value or blank. */
3444 c_extra = ' ';
3445 char_attr = hl_attr(HLF_SC);
3446 n_extra = 2;
3447
3448 if (row == startrow)
3449 {
3450 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3451 SIGN_TEXT);
3452# ifdef FEAT_SIGN_ICONS
3453 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3454 SIGN_ICON);
3455 if (gui.in_use && icon_sign != 0)
3456 {
3457 /* Use the image in this position. */
3458 c_extra = SIGN_BYTE;
3459# ifdef FEAT_NETBEANS_INTG
3460 if (buf_signcount(wp->w_buffer, lnum) > 1)
3461 c_extra = MULTISIGN_BYTE;
3462# endif
3463 char_attr = icon_sign;
3464 }
3465 else
3466# endif
3467 if (text_sign != 0)
3468 {
3469 p_extra = sign_get_text(text_sign);
3470 if (p_extra != NULL)
3471 {
3472 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003473 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
3475 char_attr = sign_get_attr(text_sign, FALSE);
3476 }
3477 }
3478 }
3479 }
3480#endif
3481
3482 if (draw_state == WL_NR - 1 && n_extra == 0)
3483 {
3484 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003485 /* Display the absolute or relative line number. After the
3486 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3487 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 && (row == startrow
3489#ifdef FEAT_DIFF
3490 + filler_lines
3491#endif
3492 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3493 {
3494 /* Draw the line number (empty space after wrapping). */
3495 if (row == startrow
3496#ifdef FEAT_DIFF
3497 + filler_lines
3498#endif
3499 )
3500 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003501 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003502 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003503
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003504 if (wp->w_p_nu && !wp->w_p_rnu)
3505 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003506 num = (long)lnum;
3507 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003508 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003509 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003510 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003511 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003512 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003513 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003514 num = lnum;
3515 fmt = "%-*ld ";
3516 }
3517 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003518
Bram Moolenaar700e7342013-01-30 12:31:36 +01003519 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003520 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (wp->w_skipcol > 0)
3522 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3523 *p_extra = '-';
3524#ifdef FEAT_RIGHTLEFT
3525 if (wp->w_p_rl) /* reverse line numbers */
3526 rl_mirror(extra);
3527#endif
3528 p_extra = extra;
3529 c_extra = NUL;
3530 }
3531 else
3532 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003533 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003535#ifdef FEAT_SYN_HL
3536 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003537 * the current line differently.
3538 * TODO: Can we use CursorLine instead of CursorLineNr
3539 * when CursorLineNr isn't set? */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003540 if ((wp->w_p_cul || wp->w_p_rnu)
3541 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003542 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545 }
3546
3547#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3548 if (draw_state == WL_SBR - 1 && n_extra == 0)
3549 {
3550 draw_state = WL_SBR;
3551# ifdef FEAT_DIFF
3552 if (filler_todo > 0)
3553 {
3554 /* Draw "deleted" diff line(s). */
3555 if (char2cells(fill_diff) > 1)
3556 c_extra = '-';
3557 else
3558 c_extra = fill_diff;
3559# ifdef FEAT_RIGHTLEFT
3560 if (wp->w_p_rl)
3561 n_extra = col + 1;
3562 else
3563# endif
3564 n_extra = W_WIDTH(wp) - col;
3565 char_attr = hl_attr(HLF_DED);
3566 }
3567# endif
3568# ifdef FEAT_LINEBREAK
3569 if (*p_sbr != NUL && need_showbreak)
3570 {
3571 /* Draw 'showbreak' at the start of each broken line. */
3572 p_extra = p_sbr;
3573 c_extra = NUL;
3574 n_extra = (int)STRLEN(p_sbr);
3575 char_attr = hl_attr(HLF_AT);
3576 need_showbreak = FALSE;
3577 /* Correct end of highlighted area for 'showbreak',
3578 * required when 'linebreak' is also set. */
3579 if (tocol == vcol)
3580 tocol += n_extra;
3581 }
3582# endif
3583 }
3584#endif
3585
3586 if (draw_state == WL_LINE - 1 && n_extra == 0)
3587 {
3588 draw_state = WL_LINE;
3589 if (saved_n_extra)
3590 {
3591 /* Continue item from end of wrapped line. */
3592 n_extra = saved_n_extra;
3593 c_extra = saved_c_extra;
3594 p_extra = saved_p_extra;
3595 char_attr = saved_char_attr;
3596 }
3597 else
3598 char_attr = 0;
3599 }
3600 }
3601
3602 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003603 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003604 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605#ifdef FEAT_DIFF
3606 && filler_todo <= 0
3607#endif
3608 )
3609 {
3610 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3611 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003612 /* Pretend we have finished updating the window. Except when
3613 * 'cursorcolumn' is set. */
3614#ifdef FEAT_SYN_HL
3615 if (wp->w_p_cuc)
3616 row = wp->w_cline_row + wp->w_cline_height;
3617 else
3618#endif
3619 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 break;
3621 }
3622
3623 if (draw_state == WL_LINE && area_highlighting)
3624 {
3625 /* handle Visual or match highlighting in this line */
3626 if (vcol == fromcol
3627#ifdef FEAT_MBYTE
3628 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3629 && (*mb_ptr2cells)(ptr) > 1)
3630#endif
3631 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003632 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 && vcol < tocol))
3634 area_attr = attr; /* start highlighting */
3635 else if (area_attr != 0
3636 && (vcol == tocol
3637 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640#ifdef FEAT_SEARCH_EXTRA
3641 if (!n_extra)
3642 {
3643 /*
3644 * Check for start/end of search pattern match.
3645 * After end, check for start/end of next match.
3646 * When another match, have to check for start again.
3647 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003648 * Do this for 'search_hl' and the match list (ordered by
3649 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003651 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003652 cur = wp->w_match_head;
3653 shl_flag = FALSE;
3654 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003656 if (shl_flag == FALSE
3657 && ((cur != NULL
3658 && cur->priority > SEARCH_HL_PRIORITY)
3659 || cur == NULL))
3660 {
3661 shl = &search_hl;
3662 shl_flag = TRUE;
3663 }
3664 else
3665 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 while (shl->rm.regprog != NULL)
3667 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003668 if (shl->startcol != MAXCOL
3669 && v >= (long)shl->startcol
3670 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
3672 shl->attr_cur = shl->attr;
3673 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003674 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
3676 shl->attr_cur = 0;
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 next_search_hl(wp, shl, lnum, (colnr_T)v);
3679
3680 /* Need to get the line again, a multi-line regexp
3681 * may have made it invalid. */
3682 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3683 ptr = line + v;
3684
3685 if (shl->lnum == lnum)
3686 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003687 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003689 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003691 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003693 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
3695 /* highlight empty match, try again after
3696 * it */
3697#ifdef FEAT_MBYTE
3698 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003699 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003700 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 else
3702#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003703 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705
3706 /* Loop to check if the match starts at the
3707 * current position */
3708 continue;
3709 }
3710 }
3711 break;
3712 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003713 if (shl != &search_hl && cur != NULL)
3714 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003716
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003717 /* Use attributes from match with highest priority among
3718 * 'search_hl' and the match list. */
3719 search_attr = search_hl.attr_cur;
3720 cur = wp->w_match_head;
3721 shl_flag = FALSE;
3722 while (cur != NULL || shl_flag == FALSE)
3723 {
3724 if (shl_flag == FALSE
3725 && ((cur != NULL
3726 && cur->priority > SEARCH_HL_PRIORITY)
3727 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003728 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003729 shl = &search_hl;
3730 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003731 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003732 else
3733 shl = &cur->hl;
3734 if (shl->attr_cur != 0)
3735 search_attr = shl->attr_cur;
3736 if (shl != &search_hl && cur != NULL)
3737 cur = cur->next;
3738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740#endif
3741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003743 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003745 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3746 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003748 if (diff_hlf == HLF_TXD && ptr - line > change_end
3749 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003751 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003754
3755 /* Decide which of the highlight attributes to use. */
3756 attr_pri = TRUE;
3757 if (area_attr != 0)
3758 char_attr = area_attr;
3759 else if (search_attr != 0)
3760 char_attr = search_attr;
3761#ifdef LINE_ATTR
3762 /* Use line_attr when not in the Visual or 'incsearch' area
3763 * (area_attr may be 0 when "noinvcur" is set). */
3764 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003765 || vcol < fromcol || vcol_prev < fromcol_prev
3766 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003767 char_attr = line_attr;
3768#endif
3769 else
3770 {
3771 attr_pri = FALSE;
3772#ifdef FEAT_SYN_HL
3773 if (has_syntax)
3774 char_attr = syntax_attr;
3775 else
3776#endif
3777 char_attr = 0;
3778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780
3781 /*
3782 * Get the next character to put on the screen.
3783 */
3784 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003785 * The "p_extra" points to the extra stuff that is inserted to
3786 * represent special characters (non-printable stuff) and other
3787 * things. When all characters are the same, c_extra is used.
3788 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3789 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3791 */
3792 if (n_extra > 0)
3793 {
3794 if (c_extra != NUL)
3795 {
3796 c = c_extra;
3797#ifdef FEAT_MBYTE
3798 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3799 if (enc_utf8 && (*mb_char2len)(c) > 1)
3800 {
3801 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003802 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003803 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 else
3806 mb_utf8 = FALSE;
3807#endif
3808 }
3809 else
3810 {
3811 c = *p_extra;
3812#ifdef FEAT_MBYTE
3813 if (has_mbyte)
3814 {
3815 mb_c = c;
3816 if (enc_utf8)
3817 {
3818 /* If the UTF-8 character is more than one byte:
3819 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003820 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 mb_utf8 = FALSE;
3822 if (mb_l > n_extra)
3823 mb_l = 1;
3824 else if (mb_l > 1)
3825 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003826 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003828 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 }
3831 else
3832 {
3833 /* if this is a DBCS character, put it in "mb_c" */
3834 mb_l = MB_BYTE2LEN(c);
3835 if (mb_l >= n_extra)
3836 mb_l = 1;
3837 else if (mb_l > 1)
3838 mb_c = (c << 8) + p_extra[1];
3839 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003840 if (mb_l == 0) /* at the NUL at end-of-line */
3841 mb_l = 1;
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 /* If a double-width char doesn't fit display a '>' in the
3844 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003845 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846# ifdef FEAT_RIGHTLEFT
3847 wp->w_p_rl ? (col <= 0) :
3848# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003849 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 && (*mb_char2cells)(mb_c) == 2)
3851 {
3852 c = '>';
3853 mb_c = c;
3854 mb_l = 1;
3855 mb_utf8 = FALSE;
3856 multi_attr = hl_attr(HLF_AT);
3857 /* put the pointer back to output the double-width
3858 * character at the start of the next line. */
3859 ++n_extra;
3860 --p_extra;
3861 }
3862 else
3863 {
3864 n_extra -= mb_l - 1;
3865 p_extra += mb_l - 1;
3866 }
3867 }
3868#endif
3869 ++p_extra;
3870 }
3871 --n_extra;
3872 }
3873 else
3874 {
3875 /*
3876 * Get a character from the line itself.
3877 */
3878 c = *ptr;
3879#ifdef FEAT_MBYTE
3880 if (has_mbyte)
3881 {
3882 mb_c = c;
3883 if (enc_utf8)
3884 {
3885 /* If the UTF-8 character is more than one byte: Decode it
3886 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003887 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 mb_utf8 = FALSE;
3889 if (mb_l > 1)
3890 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003891 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 /* Overlong encoded ASCII or ASCII with composing char
3893 * is displayed normally, except a NUL. */
3894 if (mb_c < 0x80)
3895 c = mb_c;
3896 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003897
3898 /* At start of the line we can have a composing char.
3899 * Draw it as a space with a composing char. */
3900 if (utf_iscomposing(mb_c))
3901 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003902 int i;
3903
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003904 for (i = Screen_mco - 1; i > 0; --i)
3905 u8cc[i] = u8cc[i - 1];
3906 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003907 mb_c = ' ';
3908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910
3911 if ((mb_l == 1 && c >= 0x80)
3912 || (mb_l >= 1 && mb_c == 0)
3913 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003914# ifdef UNICODE16
3915 || mb_c >= 0x10000
3916# endif
3917 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
3919 /*
3920 * Illegal UTF-8 byte: display as <xx>.
3921 * Non-BMP character : display as ? or fullwidth ?.
3922 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003923# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
3927 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003928# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (wp->w_p_rl) /* reverse */
3930 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003933# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 else if (utf_char2cells(mb_c) != 2)
3935 STRCPY(extra, "?");
3936 else
3937 /* 0xff1f in UTF-8: full-width '?' */
3938 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
3941 p_extra = extra;
3942 c = *p_extra;
3943 mb_c = mb_ptr2char_adv(&p_extra);
3944 mb_utf8 = (c >= 0x80);
3945 n_extra = (int)STRLEN(p_extra);
3946 c_extra = NUL;
3947 if (area_attr == 0 && search_attr == 0)
3948 {
3949 n_attr = n_extra + 1;
3950 extra_attr = hl_attr(HLF_8);
3951 saved_attr2 = char_attr; /* save current attr */
3952 }
3953 }
3954 else if (mb_l == 0) /* at the NUL at end-of-line */
3955 mb_l = 1;
3956#ifdef FEAT_ARABIC
3957 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3958 {
3959 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003960 int pc, pc1, nc;
3961 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
3963 /* The idea of what is the previous and next
3964 * character depends on 'rightleft'. */
3965 if (wp->w_p_rl)
3966 {
3967 pc = prev_c;
3968 pc1 = prev_c1;
3969 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003970 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972 else
3973 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003974 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003976 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 prev_c = mb_c;
3979
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003980 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982 else
3983 prev_c = mb_c;
3984#endif
3985 }
3986 else /* enc_dbcs */
3987 {
3988 mb_l = MB_BYTE2LEN(c);
3989 if (mb_l == 0) /* at the NUL at end-of-line */
3990 mb_l = 1;
3991 else if (mb_l > 1)
3992 {
3993 /* We assume a second byte below 32 is illegal.
3994 * Hopefully this is OK for all double-byte encodings!
3995 */
3996 if (ptr[1] >= 32)
3997 mb_c = (c << 8) + ptr[1];
3998 else
3999 {
4000 if (ptr[1] == NUL)
4001 {
4002 /* head byte at end of line */
4003 mb_l = 1;
4004 transchar_nonprint(extra, c);
4005 }
4006 else
4007 {
4008 /* illegal tail byte */
4009 mb_l = 2;
4010 STRCPY(extra, "XX");
4011 }
4012 p_extra = extra;
4013 n_extra = (int)STRLEN(extra) - 1;
4014 c_extra = NUL;
4015 c = *p_extra++;
4016 if (area_attr == 0 && search_attr == 0)
4017 {
4018 n_attr = n_extra + 1;
4019 extra_attr = hl_attr(HLF_8);
4020 saved_attr2 = char_attr; /* save current attr */
4021 }
4022 mb_c = c;
4023 }
4024 }
4025 }
4026 /* If a double-width char doesn't fit display a '>' in the
4027 * last column; the character is displayed at the start of the
4028 * next line. */
4029 if ((
4030# ifdef FEAT_RIGHTLEFT
4031 wp->w_p_rl ? (col <= 0) :
4032# endif
4033 (col >= W_WIDTH(wp) - 1))
4034 && (*mb_char2cells)(mb_c) == 2)
4035 {
4036 c = '>';
4037 mb_c = c;
4038 mb_utf8 = FALSE;
4039 mb_l = 1;
4040 multi_attr = hl_attr(HLF_AT);
4041 /* Put pointer back so that the character will be
4042 * displayed at the start of the next line. */
4043 --ptr;
4044 }
4045 else if (*ptr != NUL)
4046 ptr += mb_l - 1;
4047
4048 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004049 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004050 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004051 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004054 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 c = ' ';
4056 if (area_attr == 0 && search_attr == 0)
4057 {
4058 n_attr = n_extra + 1;
4059 extra_attr = hl_attr(HLF_AT);
4060 saved_attr2 = char_attr; /* save current attr */
4061 }
4062 mb_c = c;
4063 mb_utf8 = FALSE;
4064 mb_l = 1;
4065 }
4066
4067 }
4068#endif
4069 ++ptr;
4070
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004071 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072 if (wp->w_p_list && (c == 160
4073#ifdef FEAT_MBYTE
4074 || (mb_utf8 && mb_c == 160)
4075#endif
4076 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004077 {
4078 c = lcs_nbsp;
4079 if (area_attr == 0 && search_attr == 0)
4080 {
4081 n_attr = 1;
4082 extra_attr = hl_attr(HLF_8);
4083 saved_attr2 = char_attr; /* save current attr */
4084 }
4085#ifdef FEAT_MBYTE
4086 mb_c = c;
4087 if (enc_utf8 && (*mb_char2len)(c) > 1)
4088 {
4089 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004090 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004091 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004092 }
4093 else
4094 mb_utf8 = FALSE;
4095#endif
4096 }
4097
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (extra_check)
4099 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004100#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004101 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004102#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004103
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004104#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 /* Get syntax attribute, unless still at the start of the line
4106 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004107 v = (long)(ptr - line);
4108 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
4110 /* Get the syntax attribute for the character. If there
4111 * is an error, disable syntax highlighting. */
4112 save_did_emsg = did_emsg;
4113 did_emsg = FALSE;
4114
Bram Moolenaar217ad922005-03-20 22:37:15 +00004115 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004116# ifdef FEAT_SPELL
4117 has_spell ? &can_spell :
4118# endif
4119 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004122 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004123 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004124 has_syntax = FALSE;
4125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else
4127 did_emsg = save_did_emsg;
4128
4129 /* Need to get the line again, a multi-line regexp may
4130 * have made it invalid. */
4131 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4132 ptr = line + v;
4133
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004134 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004136 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004137 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004138# ifdef FEAT_CONCEAL
4139 /* no concealing past the end of the line, it interferes
4140 * with line highlighting */
4141 if (c == NUL)
4142 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004143 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004144 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004147#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004148
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004149#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004150 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004151 * Only do this when there is no syntax highlighting, the
4152 * @Spell cluster is not used or the current syntax item
4153 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004154 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004155 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004156 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004157# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004158 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004159 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004160# endif
4161 if (c != 0 && (
4162# ifdef FEAT_SYN_HL
4163 !has_syntax ||
4164# endif
4165 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004166 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004167 char_u *prev_ptr, *p;
4168 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004169 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004170# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004171 if (has_mbyte)
4172 {
4173 prev_ptr = ptr - mb_l;
4174 v -= mb_l - 1;
4175 }
4176 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004177# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004178 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004179
4180 /* Use nextline[] if possible, it has the start of the
4181 * next line concatenated. */
4182 if ((prev_ptr - line) - nextlinecol >= 0)
4183 p = nextline + (prev_ptr - line) - nextlinecol;
4184 else
4185 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004186 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004187 len = spell_check(wp, p, &spell_hlf, &cap_col,
4188 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004189 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004190
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004191 /* In Insert mode only highlight a word that
4192 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004193 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004194 && (State & INSERT) != 0
4195 && wp->w_cursor.lnum == lnum
4196 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004197 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004198 && wp->w_cursor.col < (colnr_T)word_end)
4199 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004200 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004201 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004202 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004203
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004204 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004205 && (p - nextline) + len > nextline_idx)
4206 {
4207 /* Remember that the good word continues at the
4208 * start of the next line. */
4209 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004210 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004211 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004212
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004213 /* Turn index into actual attributes. */
4214 if (spell_hlf != HLF_COUNT)
4215 spell_attr = highlight_attr[spell_hlf];
4216
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004217 if (cap_col > 0)
4218 {
4219 if (p != prev_ptr
4220 && (p - nextline) + cap_col >= nextline_idx)
4221 {
4222 /* Remember that the word in the next line
4223 * must start with a capital. */
4224 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004225 cap_col = (int)((p - nextline) + cap_col
4226 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004227 }
4228 else
4229 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004230 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004231 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004232 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004233 }
4234 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004235 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004236 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004237 char_attr = hl_combine_attr(char_attr, spell_attr);
4238 else
4239 char_attr = hl_combine_attr(spell_attr, char_attr);
4240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241#endif
4242#ifdef FEAT_LINEBREAK
4243 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004244 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 */
4246 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004247 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
4249 n_extra = win_lbr_chartabsize(wp, ptr - (
4250# ifdef FEAT_MBYTE
4251 has_mbyte ? mb_l :
4252# endif
4253 1), (colnr_T)vcol, NULL) - 1;
4254 c_extra = ' ';
4255 if (vim_iswhite(c))
4256 c = ' ';
4257 }
4258#endif
4259
4260 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4261 {
4262 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004263 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {
4265 n_attr = 1;
4266 extra_attr = hl_attr(HLF_8);
4267 saved_attr2 = char_attr; /* save current attr */
4268 }
4269#ifdef FEAT_MBYTE
4270 mb_c = c;
4271 if (enc_utf8 && (*mb_char2len)(c) > 1)
4272 {
4273 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004274 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004275 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 else
4278 mb_utf8 = FALSE;
4279#endif
4280 }
4281 }
4282
4283 /*
4284 * Handling of non-printable characters.
4285 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004286 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
4288 /*
4289 * when getting a character from the file, we may have to
4290 * turn it into something else on the way to putting it
4291 * into "ScreenLines".
4292 */
4293 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4294 {
4295 /* tab amount depends on current column */
4296 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004297 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4298#ifdef FEAT_CONCEAL
4299 /* Tab alignment should be identical regardless of
4300 * 'conceallevel' value. So tab compensates of all
4301 * previous concealed characters, and thus resets vcol_off
4302 * and boguscols accumulated so far in the line. Note that
4303 * the tab can be longer than 'tabstop' when there
4304 * are concealed characters. */
4305 n_extra += vcol_off;
4306 vcol -= vcol_off;
4307 vcol_off = 0;
4308 col -= boguscols;
4309 boguscols = 0;
4310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311#ifdef FEAT_MBYTE
4312 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4313#endif
4314 if (wp->w_p_list)
4315 {
4316 c = lcs_tab1;
4317 c_extra = lcs_tab2;
4318 n_attr = n_extra + 1;
4319 extra_attr = hl_attr(HLF_8);
4320 saved_attr2 = char_attr; /* save current attr */
4321#ifdef FEAT_MBYTE
4322 mb_c = c;
4323 if (enc_utf8 && (*mb_char2len)(c) > 1)
4324 {
4325 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004326 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004327 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
4329#endif
4330 }
4331 else
4332 {
4333 c_extra = ' ';
4334 c = ' ';
4335 }
4336 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004337 else if (c == NUL
4338 && ((wp->w_p_list && lcs_eol > 0)
4339 || ((fromcol >= 0 || fromcol_prev >= 0)
4340 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004341#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004342 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004343#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004344 && (
4345# ifdef FEAT_RIGHTLEFT
4346 wp->w_p_rl ? (col >= 0) :
4347# endif
4348 (col < W_WIDTH(wp)))
4349 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004350 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004351 && (colnr_T)vcol == wp->w_virtcol)))
4352 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004354 /* Display a '$' after the line or highlight an extra
4355 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4357 /* For a diff line the highlighting continues after the
4358 * "$". */
4359 if (
4360# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004361 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362# ifdef LINE_ATTR
4363 &&
4364# endif
4365# endif
4366# ifdef LINE_ATTR
4367 line_attr == 0
4368# endif
4369 )
4370#endif
4371 {
4372#ifdef FEAT_VIRTUALEDIT
4373 /* In virtualedit, visual selections may extend
4374 * beyond end of line. */
4375 if (area_highlighting && virtual_active()
4376 && tocol != MAXCOL && vcol < tocol)
4377 n_extra = 0;
4378 else
4379#endif
4380 {
4381 p_extra = at_end_str;
4382 n_extra = 1;
4383 c_extra = NUL;
4384 }
4385 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004386 if (wp->w_p_list)
4387 c = lcs_eol;
4388 else
4389 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 lcs_eol_one = -1;
4391 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004392 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
4394 extra_attr = hl_attr(HLF_AT);
4395 n_attr = 1;
4396 }
4397#ifdef FEAT_MBYTE
4398 mb_c = c;
4399 if (enc_utf8 && (*mb_char2len)(c) > 1)
4400 {
4401 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004402 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004403 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
4405 else
4406 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4407#endif
4408 }
4409 else if (c != NUL)
4410 {
4411 p_extra = transchar(c);
4412#ifdef FEAT_RIGHTLEFT
4413 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4414 rl_mirror(p_extra); /* reverse "<12>" */
4415#endif
4416 n_extra = byte2cells(c) - 1;
4417 c_extra = NUL;
4418 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004419 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 {
4421 n_attr = n_extra + 1;
4422 extra_attr = hl_attr(HLF_8);
4423 saved_attr2 = char_attr; /* save current attr */
4424 }
4425#ifdef FEAT_MBYTE
4426 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4427#endif
4428 }
4429#ifdef FEAT_VIRTUALEDIT
4430 else if (VIsual_active
4431 && (VIsual_mode == Ctrl_V
4432 || VIsual_mode == 'v')
4433 && virtual_active()
4434 && tocol != MAXCOL
4435 && vcol < tocol
4436 && (
4437# ifdef FEAT_RIGHTLEFT
4438 wp->w_p_rl ? (col >= 0) :
4439# endif
4440 (col < W_WIDTH(wp))))
4441 {
4442 c = ' ';
4443 --ptr; /* put it back at the NUL */
4444 }
4445#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004446#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 else if ((
4448# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004449 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 ) && (
4453# ifdef FEAT_RIGHTLEFT
4454 wp->w_p_rl ? (col >= 0) :
4455# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004456 (col
4457# ifdef FEAT_CONCEAL
4458 - boguscols
4459# endif
4460 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
4462 /* Highlight until the right side of the window */
4463 c = ' ';
4464 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004465
4466 /* Remember we do the char for line highlighting. */
4467 ++did_line_attr;
4468
4469 /* don't do search HL for the rest of the line */
4470 if (line_attr != 0 && char_attr == search_attr && col > 0)
4471 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472# ifdef FEAT_DIFF
4473 if (diff_hlf == HLF_TXD)
4474 {
4475 diff_hlf = HLF_CHD;
4476 if (attr == 0 || char_attr != attr)
4477 char_attr = hl_attr(diff_hlf);
4478 }
4479# endif
4480 }
4481#endif
4482 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004483
4484#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004485 if ( wp->w_p_cole > 0
4486 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4487 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004488 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004489 && !(lnum_in_visual_area
4490 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004491 {
4492 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004493 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004494 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4495 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004496 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004497 /* First time at this concealed item: display one
4498 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004499 if (syn_get_sub_char() != NUL)
4500 c = syn_get_sub_char();
4501 else if (lcs_conceal != NUL)
4502 c = lcs_conceal;
4503 else
4504 c = ' ';
4505
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004506 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004507
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004508 if (n_extra > 0)
4509 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004510 vcol += n_extra;
4511 if (wp->w_p_wrap && n_extra > 0)
4512 {
4513# ifdef FEAT_RIGHTLEFT
4514 if (wp->w_p_rl)
4515 {
4516 col -= n_extra;
4517 boguscols -= n_extra;
4518 }
4519 else
4520# endif
4521 {
4522 boguscols += n_extra;
4523 col += n_extra;
4524 }
4525 }
4526 n_extra = 0;
4527 n_attr = 0;
4528 }
4529 else if (n_skip == 0)
4530 {
4531 is_concealing = TRUE;
4532 n_skip = 1;
4533 }
4534# ifdef FEAT_MBYTE
4535 mb_c = c;
4536 if (enc_utf8 && (*mb_char2len)(c) > 1)
4537 {
4538 mb_utf8 = TRUE;
4539 u8cc[0] = 0;
4540 c = 0xc0;
4541 }
4542 else
4543 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4544# endif
4545 }
4546 else
4547 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004548 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004549 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004550 }
4551#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
4553
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004554#ifdef FEAT_CONCEAL
4555 /* In the cursor line and we may be concealing characters: correct
4556 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004557 if (!did_wcol && draw_state == WL_LINE
4558 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004559 && conceal_cursor_line(wp)
4560 && (int)wp->w_virtcol <= vcol + n_skip)
4561 {
4562 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004563 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004564 did_wcol = TRUE;
4565 }
4566#endif
4567
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 /* Don't override visual selection highlighting. */
4569 if (n_attr > 0
4570 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004571 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 char_attr = extra_attr;
4573
Bram Moolenaar81695252004-12-29 20:58:21 +00004574#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 /* XIM don't send preedit_start and preedit_end, but they send
4576 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4577 * im_is_preediting() here. */
4578 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004579 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 && (State & INSERT)
4581 && !p_imdisable
4582 && im_is_preediting()
4583 && draw_state == WL_LINE)
4584 {
4585 colnr_T tcol;
4586
4587 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004588 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 else
4590 tcol = preedit_end_col;
4591 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4592 {
4593 if (feedback_old_attr < 0)
4594 {
4595 feedback_col = 0;
4596 feedback_old_attr = char_attr;
4597 }
4598 char_attr = im_get_feedback_attr(feedback_col);
4599 if (char_attr < 0)
4600 char_attr = feedback_old_attr;
4601 feedback_col++;
4602 }
4603 else if (feedback_old_attr >= 0)
4604 {
4605 char_attr = feedback_old_attr;
4606 feedback_old_attr = -1;
4607 feedback_col = 0;
4608 }
4609 }
4610#endif
4611 /*
4612 * Handle the case where we are in column 0 but not on the first
4613 * character of the line and the user wants us to show us a
4614 * special character (via 'listchars' option "precedes:<char>".
4615 */
4616 if (lcs_prec_todo != NUL
4617 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4618#ifdef FEAT_DIFF
4619 && filler_todo <= 0
4620#endif
4621 && draw_state > WL_NR
4622 && c != NUL)
4623 {
4624 c = lcs_prec;
4625 lcs_prec_todo = NUL;
4626#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004627 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4628 {
4629 /* Double-width character being overwritten by the "precedes"
4630 * character, need to fill up half the character. */
4631 c_extra = MB_FILLER_CHAR;
4632 n_extra = 1;
4633 n_attr = 2;
4634 extra_attr = hl_attr(HLF_AT);
4635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 mb_c = c;
4637 if (enc_utf8 && (*mb_char2len)(c) > 1)
4638 {
4639 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004640 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004641 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643 else
4644 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4645#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004646 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
4648 saved_attr3 = char_attr; /* save current attr */
4649 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4650 n_attr3 = 1;
4651 }
4652 }
4653
4654 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004655 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004657 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004658#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004659 || did_line_attr == 1
4660#endif
4661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004663#ifdef FEAT_SEARCH_EXTRA
4664 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004665
4666 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004667 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004668 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004669#endif
4670
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004671 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 * highlight match at end of line. If it's beyond the last
4673 * char on the screen, just overwrite that one (tricky!) Not
4674 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004675#ifdef FEAT_SEARCH_EXTRA
4676 prevcol_hl_flag = FALSE;
4677 if (prevcol == (long)search_hl.startcol)
4678 prevcol_hl_flag = TRUE;
4679 else
4680 {
4681 cur = wp->w_match_head;
4682 while (cur != NULL)
4683 {
4684 if (prevcol == (long)cur->hl.startcol)
4685 {
4686 prevcol_hl_flag = TRUE;
4687 break;
4688 }
4689 cur = cur->next;
4690 }
4691 }
4692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004694 && ((area_attr != 0 && vcol == fromcol
4695#ifdef FEAT_VISUAL
4696 && (VIsual_mode != Ctrl_V
4697 || lnum == VIsual.lnum
4698 || lnum == curwin->w_cursor.lnum)
4699#endif
4700 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701#ifdef FEAT_SEARCH_EXTRA
4702 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004703 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004704# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004705 && did_line_attr <= 1
4706# endif
4707 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708#endif
4709 ))
4710 {
4711 int n = 0;
4712
4713#ifdef FEAT_RIGHTLEFT
4714 if (wp->w_p_rl)
4715 {
4716 if (col < 0)
4717 n = 1;
4718 }
4719 else
4720#endif
4721 {
4722 if (col >= W_WIDTH(wp))
4723 n = -1;
4724 }
4725 if (n != 0)
4726 {
4727 /* At the window boundary, highlight the last character
4728 * instead (better than nothing). */
4729 off += n;
4730 col += n;
4731 }
4732 else
4733 {
4734 /* Add a blank character to highlight. */
4735 ScreenLines[off] = ' ';
4736#ifdef FEAT_MBYTE
4737 if (enc_utf8)
4738 ScreenLinesUC[off] = 0;
4739#endif
4740 }
4741#ifdef FEAT_SEARCH_EXTRA
4742 if (area_attr == 0)
4743 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004744 /* Use attributes from match with highest priority among
4745 * 'search_hl' and the match list. */
4746 char_attr = search_hl.attr;
4747 cur = wp->w_match_head;
4748 shl_flag = FALSE;
4749 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004750 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004751 if (shl_flag == FALSE
4752 && ((cur != NULL
4753 && cur->priority > SEARCH_HL_PRIORITY)
4754 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004755 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004756 shl = &search_hl;
4757 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004758 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004759 else
4760 shl = &cur->hl;
4761 if ((ptr - line) - 1 == (long)shl->startcol)
4762 char_attr = shl->attr;
4763 if (shl != &search_hl && cur != NULL)
4764 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767#endif
4768 ScreenAttrs[off] = char_attr;
4769#ifdef FEAT_RIGHTLEFT
4770 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004771 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004773 --off;
4774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 else
4776#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004779 ++off;
4780 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004781 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004782#ifdef FEAT_SYN_HL
4783 eol_hl_off = 1;
4784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaar91170f82006-05-05 21:15:17 +00004788 /*
4789 * At end of the text line.
4790 */
4791 if (c == NUL)
4792 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004793#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004794 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4795 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004796 {
4797 /* highlight last char after line */
4798 --col;
4799 --off;
4800 --vcol;
4801 }
4802
Bram Moolenaar1a384422010-07-14 19:53:30 +02004803 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004804 if (wp->w_p_wrap)
4805 v = wp->w_skipcol;
4806 else
4807 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004808
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004809 /* check if line ends before left margin */
4810 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004811 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004812#ifdef FEAT_CONCEAL
4813 /* Get rid of the boguscols now, we want to draw until the right
4814 * edge for 'cursorcolumn'. */
4815 col -= boguscols;
4816 boguscols = 0;
4817#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004818
4819 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004820 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004821
4822 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004823 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4824 && (int)wp->w_virtcol <
4825 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004826 && lnum != wp->w_cursor.lnum)
4827 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004828# ifdef FEAT_RIGHTLEFT
4829 && !wp->w_p_rl
4830# endif
4831 )
4832 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004833 int rightmost_vcol = 0;
4834 int i;
4835
4836 if (wp->w_p_cuc)
4837 rightmost_vcol = wp->w_virtcol;
4838 if (draw_color_col)
4839 /* determine rightmost colorcolumn to possibly draw */
4840 for (i = 0; color_cols[i] >= 0; ++i)
4841 if (rightmost_vcol < color_cols[i])
4842 rightmost_vcol = color_cols[i];
4843
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004844 while (col < W_WIDTH(wp))
4845 {
4846 ScreenLines[off] = ' ';
4847#ifdef FEAT_MBYTE
4848 if (enc_utf8)
4849 ScreenLinesUC[off] = 0;
4850#endif
4851 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004852 if (draw_color_col)
4853 draw_color_col = advance_color_col(VCOL_HLC,
4854 &color_cols);
4855
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004856 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004857 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004858 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004859 ScreenAttrs[off++] = hl_attr(HLF_MC);
4860 else
4861 ScreenAttrs[off++] = 0;
4862
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004863 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004864 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004865
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004866 ++vcol;
4867 }
4868 }
4869#endif
4870
Bram Moolenaar860cae12010-06-05 23:22:07 +02004871 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4872 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 row++;
4874
4875 /*
4876 * Update w_cline_height and w_cline_folded if the cursor line was
4877 * updated (saves a call to plines() later).
4878 */
4879 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4880 {
4881 curwin->w_cline_row = startrow;
4882 curwin->w_cline_height = row - startrow;
4883#ifdef FEAT_FOLDING
4884 curwin->w_cline_folded = FALSE;
4885#endif
4886 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4887 }
4888
4889 break;
4890 }
4891
4892 /* line continues beyond line end */
4893 if (lcs_ext
4894 && !wp->w_p_wrap
4895#ifdef FEAT_DIFF
4896 && filler_todo <= 0
4897#endif
4898 && (
4899#ifdef FEAT_RIGHTLEFT
4900 wp->w_p_rl ? col == 0 :
4901#endif
4902 col == W_WIDTH(wp) - 1)
4903 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004904 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4906 {
4907 c = lcs_ext;
4908 char_attr = hl_attr(HLF_AT);
4909#ifdef FEAT_MBYTE
4910 mb_c = c;
4911 if (enc_utf8 && (*mb_char2len)(c) > 1)
4912 {
4913 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004914 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004915 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else
4918 mb_utf8 = FALSE;
4919#endif
4920 }
4921
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004922#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004923 /* advance to the next 'colorcolumn' */
4924 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004925 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004926
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004927 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004928 * highlight the cursor position itself.
4929 * Also highlight the 'colorcolumn' if it is different than
4930 * 'cursorcolumn' */
4931 vcol_save_attr = -1;
4932 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004933 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004934 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004935 && lnum != wp->w_cursor.lnum)
4936 {
4937 vcol_save_attr = char_attr;
4938 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4939 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004940 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004941 {
4942 vcol_save_attr = char_attr;
4943 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4944 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004945 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004946#endif
4947
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 /*
4949 * Store character to be displayed.
4950 * Skip characters that are left of the screen for 'nowrap'.
4951 */
4952 vcol_prev = vcol;
4953 if (draw_state < WL_LINE || n_skip <= 0)
4954 {
4955 /*
4956 * Store the character.
4957 */
4958#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4959 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4960 {
4961 /* A double-wide character is: put first halve in left cell. */
4962 --off;
4963 --col;
4964 }
4965#endif
4966 ScreenLines[off] = c;
4967#ifdef FEAT_MBYTE
4968 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004969 {
4970 if ((mb_c & 0xff00) == 0x8e00)
4971 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 else if (enc_utf8)
4975 {
4976 if (mb_utf8)
4977 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004978 int i;
4979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004981 if ((c & 0xff) == 0)
4982 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004983 for (i = 0; i < Screen_mco; ++i)
4984 {
4985 ScreenLinesC[i][off] = u8cc[i];
4986 if (u8cc[i] == 0)
4987 break;
4988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 else
4991 ScreenLinesUC[off] = 0;
4992 }
4993 if (multi_attr)
4994 {
4995 ScreenAttrs[off] = multi_attr;
4996 multi_attr = 0;
4997 }
4998 else
4999#endif
5000 ScreenAttrs[off] = char_attr;
5001
5002#ifdef FEAT_MBYTE
5003 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5004 {
5005 /* Need to fill two screen columns. */
5006 ++off;
5007 ++col;
5008 if (enc_utf8)
5009 /* UTF-8: Put a 0 in the second screen char. */
5010 ScreenLines[off] = 0;
5011 else
5012 /* DBCS: Put second byte in the second screen char. */
5013 ScreenLines[off] = mb_c & 0xff;
5014 ++vcol;
5015 /* When "tocol" is halfway a character, set it to the end of
5016 * the character, otherwise highlighting won't stop. */
5017 if (tocol == vcol)
5018 ++tocol;
5019#ifdef FEAT_RIGHTLEFT
5020 if (wp->w_p_rl)
5021 {
5022 /* now it's time to backup one cell */
5023 --off;
5024 --col;
5025 }
5026#endif
5027 }
5028#endif
5029#ifdef FEAT_RIGHTLEFT
5030 if (wp->w_p_rl)
5031 {
5032 --off;
5033 --col;
5034 }
5035 else
5036#endif
5037 {
5038 ++off;
5039 ++col;
5040 }
5041 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005042#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005043 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005044 {
5045 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005046 ++vcol_off;
5047 if (n_extra > 0)
5048 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005049 if (wp->w_p_wrap)
5050 {
5051 /*
5052 * Special voodoo required if 'wrap' is on.
5053 *
5054 * Advance the column indicator to force the line
5055 * drawing to wrap early. This will make the line
5056 * take up the same screen space when parts are concealed,
5057 * so that cursor line computations aren't messed up.
5058 *
5059 * To avoid the fictitious advance of 'col' causing
5060 * trailing junk to be written out of the screen line
5061 * we are building, 'boguscols' keeps track of the number
5062 * of bad columns we have advanced.
5063 */
5064 if (n_extra > 0)
5065 {
5066 vcol += n_extra;
5067# ifdef FEAT_RIGHTLEFT
5068 if (wp->w_p_rl)
5069 {
5070 col -= n_extra;
5071 boguscols -= n_extra;
5072 }
5073 else
5074# endif
5075 {
5076 col += n_extra;
5077 boguscols += n_extra;
5078 }
5079 n_extra = 0;
5080 n_attr = 0;
5081 }
5082
5083
5084# ifdef FEAT_MBYTE
5085 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5086 {
5087 /* Need to fill two screen columns. */
5088# ifdef FEAT_RIGHTLEFT
5089 if (wp->w_p_rl)
5090 {
5091 --boguscols;
5092 --col;
5093 }
5094 else
5095# endif
5096 {
5097 ++boguscols;
5098 ++col;
5099 }
5100 }
5101# endif
5102
5103# ifdef FEAT_RIGHTLEFT
5104 if (wp->w_p_rl)
5105 {
5106 --boguscols;
5107 --col;
5108 }
5109 else
5110# endif
5111 {
5112 ++boguscols;
5113 ++col;
5114 }
5115 }
5116 else
5117 {
5118 if (n_extra > 0)
5119 {
5120 vcol += n_extra;
5121 n_extra = 0;
5122 n_attr = 0;
5123 }
5124 }
5125
5126 }
5127#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else
5129 --n_skip;
5130
Bram Moolenaar64486672010-05-16 15:46:46 +02005131 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5132 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005133 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134#ifdef FEAT_DIFF
5135 && filler_todo <= 0
5136#endif
5137 )
5138 ++vcol;
5139
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005140#ifdef FEAT_SYN_HL
5141 if (vcol_save_attr >= 0)
5142 char_attr = vcol_save_attr;
5143#endif
5144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 /* restore attributes after "predeces" in 'listchars' */
5146 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5147 char_attr = saved_attr3;
5148
5149 /* restore attributes after last 'listchars' or 'number' char */
5150 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5151 char_attr = saved_attr2;
5152
5153 /*
5154 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005155 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
5157 if ((
5158#ifdef FEAT_RIGHTLEFT
5159 wp->w_p_rl ? (col < 0) :
5160#endif
5161 (col >= W_WIDTH(wp)))
5162 && (*ptr != NUL
5163#ifdef FEAT_DIFF
5164 || filler_todo > 0
5165#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005166 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5168 )
5169 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005170#ifdef FEAT_CONCEAL
5171 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5172 (int)W_WIDTH(wp), wp->w_p_rl);
5173 boguscols = 0;
5174#else
5175 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5176 (int)W_WIDTH(wp), wp->w_p_rl);
5177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 ++row;
5179 ++screen_row;
5180
5181 /* When not wrapping and finished diff lines, or when displayed
5182 * '$' and highlighting until last column, break here. */
5183 if ((!wp->w_p_wrap
5184#ifdef FEAT_DIFF
5185 && filler_todo <= 0
5186#endif
5187 ) || lcs_eol_one == -1)
5188 break;
5189
5190 /* When the window is too narrow draw all "@" lines. */
5191 if (draw_state != WL_LINE
5192#ifdef FEAT_DIFF
5193 && filler_todo <= 0
5194#endif
5195 )
5196 {
5197 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5198#ifdef FEAT_VERTSPLIT
5199 draw_vsep_win(wp, row);
5200#endif
5201 row = endrow;
5202 }
5203
5204 /* When line got too long for screen break here. */
5205 if (row == endrow)
5206 {
5207 ++row;
5208 break;
5209 }
5210
5211 if (screen_cur_row == screen_row - 1
5212#ifdef FEAT_DIFF
5213 && filler_todo <= 0
5214#endif
5215 && W_WIDTH(wp) == Columns)
5216 {
5217 /* Remember that the line wraps, used for modeless copy. */
5218 LineWraps[screen_row - 1] = TRUE;
5219
5220 /*
5221 * Special trick to make copy/paste of wrapped lines work with
5222 * xterm/screen: write an extra character beyond the end of
5223 * the line. This will work with all terminal types
5224 * (regardless of the xn,am settings).
5225 * Only do this on a fast tty.
5226 * Only do this if the cursor is on the current line
5227 * (something has been written in it).
5228 * Don't do this for the GUI.
5229 * Don't do this for double-width characters.
5230 * Don't do this for a window not at the right screen border.
5231 */
5232 if (p_tf
5233#ifdef FEAT_GUI
5234 && !gui.in_use
5235#endif
5236#ifdef FEAT_MBYTE
5237 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005238 && ((*mb_off2cells)(LineOffset[screen_row],
5239 LineOffset[screen_row] + screen_Columns)
5240 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005242 + (int)Columns - 2,
5243 LineOffset[screen_row] + screen_Columns)
5244 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#endif
5246 )
5247 {
5248 /* First make sure we are at the end of the screen line,
5249 * then output the same character again to let the
5250 * terminal know about the wrap. If the terminal doesn't
5251 * auto-wrap, we overwrite the character. */
5252 if (screen_cur_col != W_WIDTH(wp))
5253 screen_char(LineOffset[screen_row - 1]
5254 + (unsigned)Columns - 1,
5255 screen_row - 1, (int)(Columns - 1));
5256
5257#ifdef FEAT_MBYTE
5258 /* When there is a multi-byte character, just output a
5259 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005260 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5261 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 out_char(' ');
5263 else
5264#endif
5265 out_char(ScreenLines[LineOffset[screen_row - 1]
5266 + (Columns - 1)]);
5267 /* force a redraw of the first char on the next line */
5268 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5269 screen_start(); /* don't know where cursor is now */
5270 }
5271 }
5272
5273 col = 0;
5274 off = (unsigned)(current_ScreenLine - ScreenLines);
5275#ifdef FEAT_RIGHTLEFT
5276 if (wp->w_p_rl)
5277 {
5278 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5279 off += col;
5280 }
5281#endif
5282
5283 /* reset the drawing state for the start of a wrapped line */
5284 draw_state = WL_START;
5285 saved_n_extra = n_extra;
5286 saved_p_extra = p_extra;
5287 saved_c_extra = c_extra;
5288 saved_char_attr = char_attr;
5289 n_extra = 0;
5290 lcs_prec_todo = lcs_prec;
5291#ifdef FEAT_LINEBREAK
5292# ifdef FEAT_DIFF
5293 if (filler_todo <= 0)
5294# endif
5295 need_showbreak = TRUE;
5296#endif
5297#ifdef FEAT_DIFF
5298 --filler_todo;
5299 /* When the filler lines are actually below the last line of the
5300 * file, don't draw the line itself, break here. */
5301 if (filler_todo == 0 && wp->w_botfill)
5302 break;
5303#endif
5304 }
5305
5306 } /* for every character in the line */
5307
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005308#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005309 /* After an empty line check first word for capital. */
5310 if (*skipwhite(line) == NUL)
5311 {
5312 capcol_lnum = lnum + 1;
5313 cap_col = 0;
5314 }
5315#endif
5316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 return row;
5318}
5319
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005320#ifdef FEAT_MBYTE
5321static int comp_char_differs __ARGS((int, int));
5322
5323/*
5324 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005325 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005326 */
5327 static int
5328comp_char_differs(off_from, off_to)
5329 int off_from;
5330 int off_to;
5331{
5332 int i;
5333
5334 for (i = 0; i < Screen_mco; ++i)
5335 {
5336 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5337 return TRUE;
5338 if (ScreenLinesC[i][off_from] == 0)
5339 break;
5340 }
5341 return FALSE;
5342}
5343#endif
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345/*
5346 * Check whether the given character needs redrawing:
5347 * - the (first byte of the) character is different
5348 * - the attributes are different
5349 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005350 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 */
5352 static int
5353char_needs_redraw(off_from, off_to, cols)
5354 int off_from;
5355 int off_to;
5356 int cols;
5357{
5358 if (cols > 0
5359 && ((ScreenLines[off_from] != ScreenLines[off_to]
5360 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5361
5362#ifdef FEAT_MBYTE
5363 || (enc_dbcs != 0
5364 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5365 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5366 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5367 : (cols > 1 && ScreenLines[off_from + 1]
5368 != ScreenLines[off_to + 1])))
5369 || (enc_utf8
5370 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5371 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005372 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005373 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5374 && ScreenLines[off_from + 1]
5375 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376#endif
5377 ))
5378 return TRUE;
5379 return FALSE;
5380}
5381
5382/*
5383 * Move one "cooked" screen line to the screen, but only the characters that
5384 * have actually changed. Handle insert/delete character.
5385 * "coloff" gives the first column on the screen for this line.
5386 * "endcol" gives the columns where valid characters are.
5387 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5388 * needs to be cleared, negative otherwise.
5389 * "rlflag" is TRUE in a rightleft window:
5390 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5391 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5392 */
5393 static void
5394screen_line(row, coloff, endcol, clear_width
5395#ifdef FEAT_RIGHTLEFT
5396 , rlflag
5397#endif
5398 )
5399 int row;
5400 int coloff;
5401 int endcol;
5402 int clear_width;
5403#ifdef FEAT_RIGHTLEFT
5404 int rlflag;
5405#endif
5406{
5407 unsigned off_from;
5408 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005409#ifdef FEAT_MBYTE
5410 unsigned max_off_from;
5411 unsigned max_off_to;
5412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 int col = 0;
5414#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5415 int hl;
5416#endif
5417 int force = FALSE; /* force update rest of the line */
5418 int redraw_this /* bool: does character need redraw? */
5419#ifdef FEAT_GUI
5420 = TRUE /* For GUI when while-loop empty */
5421#endif
5422 ;
5423 int redraw_next; /* redraw_this for next character */
5424#ifdef FEAT_MBYTE
5425 int clear_next = FALSE;
5426 int char_cells; /* 1: normal char */
5427 /* 2: occupies two display cells */
5428# define CHAR_CELLS char_cells
5429#else
5430# define CHAR_CELLS 1
5431#endif
5432
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005433 /* Check for illegal row and col, just in case. */
5434 if (row >= Rows)
5435 row = Rows - 1;
5436 if (endcol > Columns)
5437 endcol = Columns;
5438
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439# ifdef FEAT_CLIPBOARD
5440 clip_may_clear_selection(row, row);
5441# endif
5442
5443 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5444 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005445#ifdef FEAT_MBYTE
5446 max_off_from = off_from + screen_Columns;
5447 max_off_to = LineOffset[row] + screen_Columns;
5448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450#ifdef FEAT_RIGHTLEFT
5451 if (rlflag)
5452 {
5453 /* Clear rest first, because it's left of the text. */
5454 if (clear_width > 0)
5455 {
5456 while (col <= endcol && ScreenLines[off_to] == ' '
5457 && ScreenAttrs[off_to] == 0
5458# ifdef FEAT_MBYTE
5459 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5460# endif
5461 )
5462 {
5463 ++off_to;
5464 ++col;
5465 }
5466 if (col <= endcol)
5467 screen_fill(row, row + 1, col + coloff,
5468 endcol + coloff + 1, ' ', ' ', 0);
5469 }
5470 col = endcol + 1;
5471 off_to = LineOffset[row] + col + coloff;
5472 off_from += col;
5473 endcol = (clear_width > 0 ? clear_width : -clear_width);
5474 }
5475#endif /* FEAT_RIGHTLEFT */
5476
5477 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5478
5479 while (col < endcol)
5480 {
5481#ifdef FEAT_MBYTE
5482 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005483 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 else
5485 char_cells = 1;
5486#endif
5487
5488 redraw_this = redraw_next;
5489 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5490 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5491
5492#ifdef FEAT_GUI
5493 /* If the next character was bold, then redraw the current character to
5494 * remove any pixels that might have spilt over into us. This only
5495 * happens in the GUI.
5496 */
5497 if (redraw_next && gui.in_use)
5498 {
5499 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005500 if (hl > HL_ALL)
5501 hl = syn_attr2attr(hl);
5502 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 redraw_this = TRUE;
5504 }
5505#endif
5506
5507 if (redraw_this)
5508 {
5509 /*
5510 * Special handling when 'xs' termcap flag set (hpterm):
5511 * Attributes for characters are stored at the position where the
5512 * cursor is when writing the highlighting code. The
5513 * start-highlighting code must be written with the cursor on the
5514 * first highlighted character. The stop-highlighting code must
5515 * be written with the cursor just after the last highlighted
5516 * character.
5517 * Overwriting a character doesn't remove it's highlighting. Need
5518 * to clear the rest of the line, and force redrawing it
5519 * completely.
5520 */
5521 if ( p_wiv
5522 && !force
5523#ifdef FEAT_GUI
5524 && !gui.in_use
5525#endif
5526 && ScreenAttrs[off_to] != 0
5527 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5528 {
5529 /*
5530 * Need to remove highlighting attributes here.
5531 */
5532 windgoto(row, col + coloff);
5533 out_str(T_CE); /* clear rest of this screen line */
5534 screen_start(); /* don't know where cursor is now */
5535 force = TRUE; /* force redraw of rest of the line */
5536 redraw_next = TRUE; /* or else next char would miss out */
5537
5538 /*
5539 * If the previous character was highlighted, need to stop
5540 * highlighting at this character.
5541 */
5542 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5543 {
5544 screen_attr = ScreenAttrs[off_to - 1];
5545 term_windgoto(row, col + coloff);
5546 screen_stop_highlight();
5547 }
5548 else
5549 screen_attr = 0; /* highlighting has stopped */
5550 }
5551#ifdef FEAT_MBYTE
5552 if (enc_dbcs != 0)
5553 {
5554 /* Check if overwriting a double-byte with a single-byte or
5555 * the other way around requires another character to be
5556 * redrawn. For UTF-8 this isn't needed, because comparing
5557 * ScreenLinesUC[] is sufficient. */
5558 if (char_cells == 1
5559 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005560 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
5562 /* Writing a single-cell character over a double-cell
5563 * character: need to redraw the next cell. */
5564 ScreenLines[off_to + 1] = 0;
5565 redraw_next = TRUE;
5566 }
5567 else if (char_cells == 2
5568 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005569 && (*mb_off2cells)(off_to, max_off_to) == 1
5570 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
5572 /* Writing the second half of a double-cell character over
5573 * a double-cell character: need to redraw the second
5574 * cell. */
5575 ScreenLines[off_to + 2] = 0;
5576 redraw_next = TRUE;
5577 }
5578
5579 if (enc_dbcs == DBCS_JPNU)
5580 ScreenLines2[off_to] = ScreenLines2[off_from];
5581 }
5582 /* When writing a single-width character over a double-width
5583 * character and at the end of the redrawn text, need to clear out
5584 * the right halve of the old character.
5585 * Also required when writing the right halve of a double-width
5586 * char over the left halve of an existing one. */
5587 if (has_mbyte && col + char_cells == endcol
5588 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005589 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005591 && (*mb_off2cells)(off_to, max_off_to) == 1
5592 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 clear_next = TRUE;
5594#endif
5595
5596 ScreenLines[off_to] = ScreenLines[off_from];
5597#ifdef FEAT_MBYTE
5598 if (enc_utf8)
5599 {
5600 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5601 if (ScreenLinesUC[off_from] != 0)
5602 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005603 int i;
5604
5605 for (i = 0; i < Screen_mco; ++i)
5606 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
5608 }
5609 if (char_cells == 2)
5610 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5611#endif
5612
5613#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005614 /* The bold trick makes a single column of pixels appear in the
5615 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 * character should be redrawn too. This happens for our own GUI
5617 * and for some xterms. */
5618 if (
5619# ifdef FEAT_GUI
5620 gui.in_use
5621# endif
5622# if defined(FEAT_GUI) && defined(UNIX)
5623 ||
5624# endif
5625# ifdef UNIX
5626 term_is_xterm
5627# endif
5628 )
5629 {
5630 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005631 if (hl > HL_ALL)
5632 hl = syn_attr2attr(hl);
5633 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 redraw_next = TRUE;
5635 }
5636#endif
5637 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5638#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005639 /* For simplicity set the attributes of second half of a
5640 * double-wide character equal to the first half. */
5641 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005643
5644 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 else
5647#endif
5648 screen_char(off_to, row, col + coloff);
5649 }
5650 else if ( p_wiv
5651#ifdef FEAT_GUI
5652 && !gui.in_use
5653#endif
5654 && col + coloff > 0)
5655 {
5656 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5657 {
5658 /*
5659 * Don't output stop-highlight when moving the cursor, it will
5660 * stop the highlighting when it should continue.
5661 */
5662 screen_attr = 0;
5663 }
5664 else if (screen_attr != 0)
5665 screen_stop_highlight();
5666 }
5667
5668 off_to += CHAR_CELLS;
5669 off_from += CHAR_CELLS;
5670 col += CHAR_CELLS;
5671 }
5672
5673#ifdef FEAT_MBYTE
5674 if (clear_next)
5675 {
5676 /* Clear the second half of a double-wide character of which the left
5677 * half was overwritten with a single-wide character. */
5678 ScreenLines[off_to] = ' ';
5679 if (enc_utf8)
5680 ScreenLinesUC[off_to] = 0;
5681 screen_char(off_to, row, col + coloff);
5682 }
5683#endif
5684
5685 if (clear_width > 0
5686#ifdef FEAT_RIGHTLEFT
5687 && !rlflag
5688#endif
5689 )
5690 {
5691#ifdef FEAT_GUI
5692 int startCol = col;
5693#endif
5694
5695 /* blank out the rest of the line */
5696 while (col < clear_width && ScreenLines[off_to] == ' '
5697 && ScreenAttrs[off_to] == 0
5698#ifdef FEAT_MBYTE
5699 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5700#endif
5701 )
5702 {
5703 ++off_to;
5704 ++col;
5705 }
5706 if (col < clear_width)
5707 {
5708#ifdef FEAT_GUI
5709 /*
5710 * In the GUI, clearing the rest of the line may leave pixels
5711 * behind if the first character cleared was bold. Some bold
5712 * fonts spill over the left. In this case we redraw the previous
5713 * character too. If we didn't skip any blanks above, then we
5714 * only redraw if the character wasn't already redrawn anyway.
5715 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005716 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
5718 hl = ScreenAttrs[off_to];
5719 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005720 {
5721 int prev_cells = 1;
5722# ifdef FEAT_MBYTE
5723 if (enc_utf8)
5724 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5725 * that its width is 2. */
5726 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5727 else if (enc_dbcs != 0)
5728 {
5729 /* find previous character by counting from first
5730 * column and get its width. */
5731 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005732 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005733
5734 while (off < off_to)
5735 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005736 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005737 off += prev_cells;
5738 }
5739 }
5740
5741 if (enc_dbcs != 0 && prev_cells > 1)
5742 screen_char_2(off_to - prev_cells, row,
5743 col + coloff - prev_cells);
5744 else
5745# endif
5746 screen_char(off_to - prev_cells, row,
5747 col + coloff - prev_cells);
5748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 }
5750#endif
5751 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5752 ' ', ' ', 0);
5753#ifdef FEAT_VERTSPLIT
5754 off_to += clear_width - col;
5755 col = clear_width;
5756#endif
5757 }
5758 }
5759
5760 if (clear_width > 0)
5761 {
5762#ifdef FEAT_VERTSPLIT
5763 /* For a window that's left of another, draw the separator char. */
5764 if (col + coloff < Columns)
5765 {
5766 int c;
5767
5768 c = fillchar_vsep(&hl);
5769 if (ScreenLines[off_to] != c
5770# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005771 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5772 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773# endif
5774 || ScreenAttrs[off_to] != hl)
5775 {
5776 ScreenLines[off_to] = c;
5777 ScreenAttrs[off_to] = hl;
5778# ifdef FEAT_MBYTE
5779 if (enc_utf8)
5780 {
5781 if (c >= 0x80)
5782 {
5783 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005784 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
5786 else
5787 ScreenLinesUC[off_to] = 0;
5788 }
5789# endif
5790 screen_char(off_to, row, col + coloff);
5791 }
5792 }
5793 else
5794#endif
5795 LineWraps[row] = FALSE;
5796 }
5797}
5798
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005799#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005801 * Mirror text "str" for right-left displaying.
5802 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005804 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805rl_mirror(str)
5806 char_u *str;
5807{
5808 char_u *p1, *p2;
5809 int t;
5810
5811 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5812 {
5813 t = *p1;
5814 *p1 = *p2;
5815 *p2 = t;
5816 }
5817}
5818#endif
5819
5820#if defined(FEAT_WINDOWS) || defined(PROTO)
5821/*
5822 * mark all status lines for redraw; used after first :cd
5823 */
5824 void
5825status_redraw_all()
5826{
5827 win_T *wp;
5828
5829 for (wp = firstwin; wp; wp = wp->w_next)
5830 if (wp->w_status_height)
5831 {
5832 wp->w_redr_status = TRUE;
5833 redraw_later(VALID);
5834 }
5835}
5836
5837/*
5838 * mark all status lines of the current buffer for redraw
5839 */
5840 void
5841status_redraw_curbuf()
5842{
5843 win_T *wp;
5844
5845 for (wp = firstwin; wp; wp = wp->w_next)
5846 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5847 {
5848 wp->w_redr_status = TRUE;
5849 redraw_later(VALID);
5850 }
5851}
5852
5853/*
5854 * Redraw all status lines that need to be redrawn.
5855 */
5856 void
5857redraw_statuslines()
5858{
5859 win_T *wp;
5860
5861 for (wp = firstwin; wp; wp = wp->w_next)
5862 if (wp->w_redr_status)
5863 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005864 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005865 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866}
5867#endif
5868
5869#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5870/*
5871 * Redraw all status lines at the bottom of frame "frp".
5872 */
5873 void
5874win_redraw_last_status(frp)
5875 frame_T *frp;
5876{
5877 if (frp->fr_layout == FR_LEAF)
5878 frp->fr_win->w_redr_status = TRUE;
5879 else if (frp->fr_layout == FR_ROW)
5880 {
5881 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5882 win_redraw_last_status(frp);
5883 }
5884 else /* frp->fr_layout == FR_COL */
5885 {
5886 frp = frp->fr_child;
5887 while (frp->fr_next != NULL)
5888 frp = frp->fr_next;
5889 win_redraw_last_status(frp);
5890 }
5891}
5892#endif
5893
5894#ifdef FEAT_VERTSPLIT
5895/*
5896 * Draw the verticap separator right of window "wp" starting with line "row".
5897 */
5898 static void
5899draw_vsep_win(wp, row)
5900 win_T *wp;
5901 int row;
5902{
5903 int hl;
5904 int c;
5905
5906 if (wp->w_vsep_width)
5907 {
5908 /* draw the vertical separator right of this window */
5909 c = fillchar_vsep(&hl);
5910 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5911 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5912 c, ' ', hl);
5913 }
5914}
5915#endif
5916
5917#ifdef FEAT_WILDMENU
5918static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005919static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005922 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 */
5924 static int
5925status_match_len(xp, s)
5926 expand_T *xp;
5927 char_u *s;
5928{
5929 int len = 0;
5930
5931#ifdef FEAT_MENU
5932 int emenu = (xp->xp_context == EXPAND_MENUS
5933 || xp->xp_context == EXPAND_MENUNAMES);
5934
5935 /* Check for menu separators - replace with '|'. */
5936 if (emenu && menu_is_separator(s))
5937 return 1;
5938#endif
5939
5940 while (*s != NUL)
5941 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005942 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005943 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005944 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 }
5946
5947 return len;
5948}
5949
5950/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005951 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005952 * These are backslashes used for escaping. Do show backslashes in help tags.
5953 */
5954 static int
5955skip_status_match_char(xp, s)
5956 expand_T *xp;
5957 char_u *s;
5958{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005959 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005960#ifdef FEAT_MENU
5961 || ((xp->xp_context == EXPAND_MENUS
5962 || xp->xp_context == EXPAND_MENUNAMES)
5963 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5964#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005965 )
5966 {
5967#ifndef BACKSLASH_IN_FILENAME
5968 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5969 return 2;
5970#endif
5971 return 1;
5972 }
5973 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005974}
5975
5976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 * Show wildchar matches in the status line.
5978 * Show at least the "match" item.
5979 * We start at item 'first_match' in the list and show all matches that fit.
5980 *
5981 * If inversion is possible we use it. Else '=' characters are used.
5982 */
5983 void
5984win_redr_status_matches(xp, num_matches, matches, match, showtail)
5985 expand_T *xp;
5986 int num_matches;
5987 char_u **matches; /* list of matches */
5988 int match;
5989 int showtail;
5990{
5991#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5992 int row;
5993 char_u *buf;
5994 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005995 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 int fillchar;
5997 int attr;
5998 int i;
5999 int highlight = TRUE;
6000 char_u *selstart = NULL;
6001 int selstart_col = 0;
6002 char_u *selend = NULL;
6003 static int first_match = 0;
6004 int add_left = FALSE;
6005 char_u *s;
6006#ifdef FEAT_MENU
6007 int emenu;
6008#endif
6009#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6010 int l;
6011#endif
6012
6013 if (matches == NULL) /* interrupted completion? */
6014 return;
6015
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006016#ifdef FEAT_MBYTE
6017 if (has_mbyte)
6018 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6019 else
6020#endif
6021 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 if (buf == NULL)
6023 return;
6024
6025 if (match == -1) /* don't show match but original text */
6026 {
6027 match = 0;
6028 highlight = FALSE;
6029 }
6030 /* count 1 for the ending ">" */
6031 clen = status_match_len(xp, L_MATCH(match)) + 3;
6032 if (match == 0)
6033 first_match = 0;
6034 else if (match < first_match)
6035 {
6036 /* jumping left, as far as we can go */
6037 first_match = match;
6038 add_left = TRUE;
6039 }
6040 else
6041 {
6042 /* check if match fits on the screen */
6043 for (i = first_match; i < match; ++i)
6044 clen += status_match_len(xp, L_MATCH(i)) + 2;
6045 if (first_match > 0)
6046 clen += 2;
6047 /* jumping right, put match at the left */
6048 if ((long)clen > Columns)
6049 {
6050 first_match = match;
6051 /* if showing the last match, we can add some on the left */
6052 clen = 2;
6053 for (i = match; i < num_matches; ++i)
6054 {
6055 clen += status_match_len(xp, L_MATCH(i)) + 2;
6056 if ((long)clen >= Columns)
6057 break;
6058 }
6059 if (i == num_matches)
6060 add_left = TRUE;
6061 }
6062 }
6063 if (add_left)
6064 while (first_match > 0)
6065 {
6066 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6067 if ((long)clen >= Columns)
6068 break;
6069 --first_match;
6070 }
6071
6072 fillchar = fillchar_status(&attr, TRUE);
6073
6074 if (first_match == 0)
6075 {
6076 *buf = NUL;
6077 len = 0;
6078 }
6079 else
6080 {
6081 STRCPY(buf, "< ");
6082 len = 2;
6083 }
6084 clen = len;
6085
6086 i = first_match;
6087 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6088 {
6089 if (i == match)
6090 {
6091 selstart = buf + len;
6092 selstart_col = clen;
6093 }
6094
6095 s = L_MATCH(i);
6096 /* Check for menu separators - replace with '|' */
6097#ifdef FEAT_MENU
6098 emenu = (xp->xp_context == EXPAND_MENUS
6099 || xp->xp_context == EXPAND_MENUNAMES);
6100 if (emenu && menu_is_separator(s))
6101 {
6102 STRCPY(buf + len, transchar('|'));
6103 l = (int)STRLEN(buf + len);
6104 len += l;
6105 clen += l;
6106 }
6107 else
6108#endif
6109 for ( ; *s != NUL; ++s)
6110 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006111 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 clen += ptr2cells(s);
6113#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006114 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
6116 STRNCPY(buf + len, s, l);
6117 s += l - 1;
6118 len += l;
6119 }
6120 else
6121#endif
6122 {
6123 STRCPY(buf + len, transchar_byte(*s));
6124 len += (int)STRLEN(buf + len);
6125 }
6126 }
6127 if (i == match)
6128 selend = buf + len;
6129
6130 *(buf + len++) = ' ';
6131 *(buf + len++) = ' ';
6132 clen += 2;
6133 if (++i == num_matches)
6134 break;
6135 }
6136
6137 if (i != num_matches)
6138 {
6139 *(buf + len++) = '>';
6140 ++clen;
6141 }
6142
6143 buf[len] = NUL;
6144
6145 row = cmdline_row - 1;
6146 if (row >= 0)
6147 {
6148 if (wild_menu_showing == 0)
6149 {
6150 if (msg_scrolled > 0)
6151 {
6152 /* Put the wildmenu just above the command line. If there is
6153 * no room, scroll the screen one line up. */
6154 if (cmdline_row == Rows - 1)
6155 {
6156 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6157 ++msg_scrolled;
6158 }
6159 else
6160 {
6161 ++cmdline_row;
6162 ++row;
6163 }
6164 wild_menu_showing = WM_SCROLLED;
6165 }
6166 else
6167 {
6168 /* Create status line if needed by setting 'laststatus' to 2.
6169 * Set 'winminheight' to zero to avoid that the window is
6170 * resized. */
6171 if (lastwin->w_status_height == 0)
6172 {
6173 save_p_ls = p_ls;
6174 save_p_wmh = p_wmh;
6175 p_ls = 2;
6176 p_wmh = 0;
6177 last_status(FALSE);
6178 }
6179 wild_menu_showing = WM_SHOWN;
6180 }
6181 }
6182
6183 screen_puts(buf, row, 0, attr);
6184 if (selstart != NULL && highlight)
6185 {
6186 *selend = NUL;
6187 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6188 }
6189
6190 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6191 }
6192
6193#ifdef FEAT_VERTSPLIT
6194 win_redraw_last_status(topframe);
6195#else
6196 lastwin->w_redr_status = TRUE;
6197#endif
6198 vim_free(buf);
6199}
6200#endif
6201
6202#if defined(FEAT_WINDOWS) || defined(PROTO)
6203/*
6204 * Redraw the status line of window wp.
6205 *
6206 * If inversion is possible we use it. Else '=' characters are used.
6207 */
6208 void
6209win_redr_status(wp)
6210 win_T *wp;
6211{
6212 int row;
6213 char_u *p;
6214 int len;
6215 int fillchar;
6216 int attr;
6217 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006218 static int busy = FALSE;
6219
6220 /* It's possible to get here recursively when 'statusline' (indirectly)
6221 * invokes ":redrawstatus". Simply ignore the call then. */
6222 if (busy)
6223 return;
6224 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
6226 wp->w_redr_status = FALSE;
6227 if (wp->w_status_height == 0)
6228 {
6229 /* no status line, can only be last window */
6230 redraw_cmdline = TRUE;
6231 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006232 else if (!redrawing()
6233#ifdef FEAT_INS_EXPAND
6234 /* don't update status line when popup menu is visible and may be
6235 * drawn over it */
6236 || pum_visible()
6237#endif
6238 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {
6240 /* Don't redraw right now, do it later. */
6241 wp->w_redr_status = TRUE;
6242 }
6243#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006244 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 {
6246 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006247 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
6249#endif
6250 else
6251 {
6252 fillchar = fillchar_status(&attr, wp == curwin);
6253
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006254 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 p = NameBuff;
6256 len = (int)STRLEN(p);
6257
6258 if (wp->w_buffer->b_help
6259#ifdef FEAT_QUICKFIX
6260 || wp->w_p_pvw
6261#endif
6262 || bufIsChanged(wp->w_buffer)
6263 || wp->w_buffer->b_p_ro)
6264 *(p + len++) = ' ';
6265 if (wp->w_buffer->b_help)
6266 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006267 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 len += (int)STRLEN(p + len);
6269 }
6270#ifdef FEAT_QUICKFIX
6271 if (wp->w_p_pvw)
6272 {
6273 STRCPY(p + len, _("[Preview]"));
6274 len += (int)STRLEN(p + len);
6275 }
6276#endif
6277 if (bufIsChanged(wp->w_buffer))
6278 {
6279 STRCPY(p + len, "[+]");
6280 len += 3;
6281 }
6282 if (wp->w_buffer->b_p_ro)
6283 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006284 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 len += 4;
6286 }
6287
6288#ifndef FEAT_VERTSPLIT
6289 this_ru_col = ru_col;
6290 if (this_ru_col < (Columns + 1) / 2)
6291 this_ru_col = (Columns + 1) / 2;
6292#else
6293 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6294 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6295 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6296 if (this_ru_col <= 1)
6297 {
6298 p = (char_u *)"<"; /* No room for file name! */
6299 len = 1;
6300 }
6301 else
6302#endif
6303#ifdef FEAT_MBYTE
6304 if (has_mbyte)
6305 {
6306 int clen = 0, i;
6307
6308 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006309 clen = mb_string2cells(p, -1);
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 /* Find first character that will fit.
6312 * Going from start to end is much faster for DBCS. */
6313 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006314 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 clen -= (*mb_ptr2cells)(p + i);
6316 len = clen;
6317 if (i > 0)
6318 {
6319 p = p + i - 1;
6320 *p = '<';
6321 ++len;
6322 }
6323
6324 }
6325 else
6326#endif
6327 if (len > this_ru_col - 1)
6328 {
6329 p += len - (this_ru_col - 1);
6330 *p = '<';
6331 len = this_ru_col - 1;
6332 }
6333
6334 row = W_WINROW(wp) + wp->w_height;
6335 screen_puts(p, row, W_WINCOL(wp), attr);
6336 screen_fill(row, row + 1, len + W_WINCOL(wp),
6337 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6338
6339 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6340 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6341 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6342 - 1 + W_WINCOL(wp)), attr);
6343
6344#ifdef FEAT_CMDL_INFO
6345 win_redr_ruler(wp, TRUE);
6346#endif
6347 }
6348
6349#ifdef FEAT_VERTSPLIT
6350 /*
6351 * May need to draw the character below the vertical separator.
6352 */
6353 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6354 {
6355 if (stl_connected(wp))
6356 fillchar = fillchar_status(&attr, wp == curwin);
6357 else
6358 fillchar = fillchar_vsep(&attr);
6359 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6360 attr);
6361 }
6362#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006363 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364}
6365
Bram Moolenaar238a5642006-02-21 22:12:05 +00006366#ifdef FEAT_STL_OPT
6367/*
6368 * Redraw the status line according to 'statusline' and take care of any
6369 * errors encountered.
6370 */
6371 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006372redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006373 win_T *wp;
6374{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006375 static int entered = FALSE;
6376 int save_called_emsg = called_emsg;
6377
6378 /* When called recursively return. This can happen when the statusline
6379 * contains an expression that triggers a redraw. */
6380 if (entered)
6381 return;
6382 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006383
6384 called_emsg = FALSE;
6385 win_redr_custom(wp, FALSE);
6386 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006387 {
6388 /* When there is an error disable the statusline, otherwise the
6389 * display is messed up with errors and a redraw triggers the problem
6390 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006391 set_string_option_direct((char_u *)"statusline", -1,
6392 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006393 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006394 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006395 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006396 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006397}
6398#endif
6399
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400# ifdef FEAT_VERTSPLIT
6401/*
6402 * Return TRUE if the status line of window "wp" is connected to the status
6403 * line of the window right of it. If not, then it's a vertical separator.
6404 * Only call if (wp->w_vsep_width != 0).
6405 */
6406 int
6407stl_connected(wp)
6408 win_T *wp;
6409{
6410 frame_T *fr;
6411
6412 fr = wp->w_frame;
6413 while (fr->fr_parent != NULL)
6414 {
6415 if (fr->fr_parent->fr_layout == FR_COL)
6416 {
6417 if (fr->fr_next != NULL)
6418 break;
6419 }
6420 else
6421 {
6422 if (fr->fr_next != NULL)
6423 return TRUE;
6424 }
6425 fr = fr->fr_parent;
6426 }
6427 return FALSE;
6428}
6429# endif
6430
6431#endif /* FEAT_WINDOWS */
6432
6433#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6434/*
6435 * Get the value to show for the language mappings, active 'keymap'.
6436 */
6437 int
6438get_keymap_str(wp, buf, len)
6439 win_T *wp;
6440 char_u *buf; /* buffer for the result */
6441 int len; /* length of buffer */
6442{
6443 char_u *p;
6444
6445 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6446 return FALSE;
6447
6448 {
6449#ifdef FEAT_EVAL
6450 buf_T *old_curbuf = curbuf;
6451 win_T *old_curwin = curwin;
6452 char_u *s;
6453
6454 curbuf = wp->w_buffer;
6455 curwin = wp;
6456 STRCPY(buf, "b:keymap_name"); /* must be writable */
6457 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006458 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 --emsg_skip;
6460 curbuf = old_curbuf;
6461 curwin = old_curwin;
6462 if (p == NULL || *p == NUL)
6463#endif
6464 {
6465#ifdef FEAT_KEYMAP
6466 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6467 p = wp->w_buffer->b_p_keymap;
6468 else
6469#endif
6470 p = (char_u *)"lang";
6471 }
6472 if ((int)(STRLEN(p) + 3) < len)
6473 sprintf((char *)buf, "<%s>", p);
6474 else
6475 buf[0] = NUL;
6476#ifdef FEAT_EVAL
6477 vim_free(s);
6478#endif
6479 }
6480 return buf[0] != NUL;
6481}
6482#endif
6483
6484#if defined(FEAT_STL_OPT) || defined(PROTO)
6485/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006486 * Redraw the status line or ruler of window "wp".
6487 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 */
6489 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006490win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006492 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 int attr;
6495 int curattr;
6496 int row;
6497 int col = 0;
6498 int maxwidth;
6499 int width;
6500 int n;
6501 int len;
6502 int fillchar;
6503 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006504 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006506 struct stl_hlrec hltab[STL_MAX_ITEM];
6507 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006508 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006509 win_T *ewp;
6510 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
6512 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006513 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006515 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006516 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006517 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006518 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006519 attr = hl_attr(HLF_TPF);
6520 maxwidth = Columns;
6521# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006522 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006525 else
6526 {
6527 row = W_WINROW(wp) + wp->w_height;
6528 fillchar = fillchar_status(&attr, wp == curwin);
6529 maxwidth = W_WIDTH(wp);
6530
6531 if (draw_ruler)
6532 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006533 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006534 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006535 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006536 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006537 if (*++stl == '-')
6538 stl++;
6539 if (atoi((char *)stl))
6540 while (VIM_ISDIGIT(*stl))
6541 stl++;
6542 if (*stl++ != '(')
6543 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006544 }
6545#ifdef FEAT_VERTSPLIT
6546 col = ru_col - (Columns - W_WIDTH(wp));
6547 if (col < (W_WIDTH(wp) + 1) / 2)
6548 col = (W_WIDTH(wp) + 1) / 2;
6549#else
6550 col = ru_col;
6551 if (col > (Columns + 1) / 2)
6552 col = (Columns + 1) / 2;
6553#endif
6554 maxwidth = W_WIDTH(wp) - col;
6555#ifdef FEAT_WINDOWS
6556 if (!wp->w_status_height)
6557#endif
6558 {
6559 row = Rows - 1;
6560 --maxwidth; /* writing in last column may cause scrolling */
6561 fillchar = ' ';
6562 attr = 0;
6563 }
6564
6565# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006566 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006567# endif
6568 }
6569 else
6570 {
6571 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006572 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006573 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006574 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006575# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006576 use_sandbox = was_set_insecurely((char_u *)"statusline",
6577 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006578# endif
6579 }
6580
6581#ifdef FEAT_VERTSPLIT
6582 col += W_WINCOL(wp);
6583#endif
6584 }
6585
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 if (maxwidth <= 0)
6587 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
Bram Moolenaar61452852011-02-01 18:01:11 +01006589 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6590 * the cursor away and back. */
6591 ewp = wp == NULL ? curwin : wp;
6592 p_crb_save = ewp->w_p_crb;
6593 ewp->w_p_crb = FALSE;
6594
Bram Moolenaar362f3562009-11-03 16:20:34 +00006595 /* Make a copy, because the statusline may include a function call that
6596 * might change the option value and free the memory. */
6597 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006598 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006599 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006600 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006601 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006602 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006604 /* Make all characters printable. */
6605 p = transstr(buf);
6606 if (p != NULL)
6607 {
6608 vim_strncpy(buf, p, sizeof(buf) - 1);
6609 vim_free(p);
6610 }
6611
6612 /* fill up with "fillchar" */
6613 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006614 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 {
6616#ifdef FEAT_MBYTE
6617 len += (*mb_char2bytes)(fillchar, buf + len);
6618#else
6619 buf[len++] = fillchar;
6620#endif
6621 ++width;
6622 }
6623 buf[len] = NUL;
6624
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006625 /*
6626 * Draw each snippet with the specified highlighting.
6627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 curattr = attr;
6629 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006630 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006632 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 screen_puts_len(p, len, row, col, curattr);
6634 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006635 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006637 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006639 else if (hltab[n].userhl < 0)
6640 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006642 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006643 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644#endif
6645 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006646 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 }
6648 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006649
6650 if (wp == NULL)
6651 {
6652 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6653 col = 0;
6654 len = 0;
6655 p = buf;
6656 fillchar = 0;
6657 for (n = 0; tabtab[n].start != NULL; n++)
6658 {
6659 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6660 while (col < len)
6661 TabPageIdxs[col++] = fillchar;
6662 p = tabtab[n].start;
6663 fillchar = tabtab[n].userhl;
6664 }
6665 while (col < Columns)
6666 TabPageIdxs[col++] = fillchar;
6667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668}
6669
6670#endif /* FEAT_STL_OPT */
6671
6672/*
6673 * Output a single character directly to the screen and update ScreenLines.
6674 */
6675 void
6676screen_putchar(c, row, col, attr)
6677 int c;
6678 int row, col;
6679 int attr;
6680{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 char_u buf[MB_MAXBYTES + 1];
6682
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006683#ifdef FEAT_MBYTE
6684 if (has_mbyte)
6685 buf[(*mb_char2bytes)(c, buf)] = NUL;
6686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006688 {
6689 buf[0] = c;
6690 buf[1] = NUL;
6691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 screen_puts(buf, row, col, attr);
6693}
6694
6695/*
6696 * Get a single character directly from ScreenLines into "bytes[]".
6697 * Also return its attribute in *attrp;
6698 */
6699 void
6700screen_getbytes(row, col, bytes, attrp)
6701 int row, col;
6702 char_u *bytes;
6703 int *attrp;
6704{
6705 unsigned off;
6706
6707 /* safety check */
6708 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6709 {
6710 off = LineOffset[row] + col;
6711 *attrp = ScreenAttrs[off];
6712 bytes[0] = ScreenLines[off];
6713 bytes[1] = NUL;
6714
6715#ifdef FEAT_MBYTE
6716 if (enc_utf8 && ScreenLinesUC[off] != 0)
6717 bytes[utfc_char2bytes(off, bytes)] = NUL;
6718 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6719 {
6720 bytes[0] = ScreenLines[off];
6721 bytes[1] = ScreenLines2[off];
6722 bytes[2] = NUL;
6723 }
6724 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6725 {
6726 bytes[1] = ScreenLines[off + 1];
6727 bytes[2] = NUL;
6728 }
6729#endif
6730 }
6731}
6732
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006733#ifdef FEAT_MBYTE
6734static int screen_comp_differs __ARGS((int, int*));
6735
6736/*
6737 * Return TRUE if composing characters for screen posn "off" differs from
6738 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006739 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006740 */
6741 static int
6742screen_comp_differs(off, u8cc)
6743 int off;
6744 int *u8cc;
6745{
6746 int i;
6747
6748 for (i = 0; i < Screen_mco; ++i)
6749 {
6750 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6751 return TRUE;
6752 if (u8cc[i] == 0)
6753 break;
6754 }
6755 return FALSE;
6756}
6757#endif
6758
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759/*
6760 * Put string '*text' on the screen at position 'row' and 'col', with
6761 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6762 * Note: only outputs within one row, message is truncated at screen boundary!
6763 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6764 */
6765 void
6766screen_puts(text, row, col, attr)
6767 char_u *text;
6768 int row;
6769 int col;
6770 int attr;
6771{
6772 screen_puts_len(text, -1, row, col, attr);
6773}
6774
6775/*
6776 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6777 * a NUL.
6778 */
6779 void
6780screen_puts_len(text, len, row, col, attr)
6781 char_u *text;
6782 int len;
6783 int row;
6784 int col;
6785 int attr;
6786{
6787 unsigned off;
6788 char_u *ptr = text;
6789 int c;
6790#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006791 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 int mbyte_blen = 1;
6793 int mbyte_cells = 1;
6794 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006795 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 int clear_next_cell = FALSE;
6797# ifdef FEAT_ARABIC
6798 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006799 int pc, nc, nc1;
6800 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801# endif
6802#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006803#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6804 int force_redraw_this;
6805 int force_redraw_next = FALSE;
6806#endif
6807 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
6809 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6810 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006811 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
Bram Moolenaarc236c162008-07-13 17:41:49 +00006813#ifdef FEAT_MBYTE
6814 /* When drawing over the right halve of a double-wide char clear out the
6815 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006816 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006817# ifdef FEAT_GUI
6818 && !gui.in_use
6819# endif
6820 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006821 {
6822 ScreenLines[off - 1] = ' ';
6823 ScreenAttrs[off - 1] = 0;
6824 if (enc_utf8)
6825 {
6826 ScreenLinesUC[off - 1] = 0;
6827 ScreenLinesC[0][off - 1] = 0;
6828 }
6829 /* redraw the previous cell, make it empty */
6830 screen_char(off - 1, row, col - 1);
6831 /* force the cell at "col" to be redrawn */
6832 force_redraw_next = TRUE;
6833 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006834#endif
6835
Bram Moolenaar367329b2007-08-30 11:53:22 +00006836#ifdef FEAT_MBYTE
6837 max_off = LineOffset[row] + screen_Columns;
6838#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006839 while (col < screen_Columns
6840 && (len < 0 || (int)(ptr - text) < len)
6841 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 {
6843 c = *ptr;
6844#ifdef FEAT_MBYTE
6845 /* check if this is the first byte of a multibyte */
6846 if (has_mbyte)
6847 {
6848 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006849 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006851 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6853 mbyte_cells = 1;
6854 else if (enc_dbcs != 0)
6855 mbyte_cells = mbyte_blen;
6856 else /* enc_utf8 */
6857 {
6858 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006859 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 (int)((text + len) - ptr));
6861 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006862 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006864# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 /* Non-BMP character: display as ? or fullwidth ?. */
6866 if (u8c >= 0x10000)
6867 {
6868 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6869 if (attr == 0)
6870 attr = hl_attr(HLF_8);
6871 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873# ifdef FEAT_ARABIC
6874 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6875 {
6876 /* Do Arabic shaping. */
6877 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6878 {
6879 /* Past end of string to be displayed. */
6880 nc = NUL;
6881 nc1 = NUL;
6882 }
6883 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006884 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006885 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6886 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006887 nc1 = pcc[0];
6888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 pc = prev_c;
6890 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006891 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
6893 else
6894 prev_c = u8c;
6895# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006896 if (col + mbyte_cells > screen_Columns)
6897 {
6898 /* Only 1 cell left, but character requires 2 cells:
6899 * display a '>' in the last column to avoid wrapping. */
6900 c = '>';
6901 mbyte_cells = 1;
6902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904 }
6905#endif
6906
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006907#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6908 force_redraw_this = force_redraw_next;
6909 force_redraw_next = FALSE;
6910#endif
6911
6912 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913#ifdef FEAT_MBYTE
6914 || (mbyte_cells == 2
6915 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6916 || (enc_dbcs == DBCS_JPNU
6917 && c == 0x8e
6918 && ScreenLines2[off] != ptr[1])
6919 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006920 && (ScreenLinesUC[off] !=
6921 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6922 || (ScreenLinesUC[off] != 0
6923 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924#endif
6925 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006926 || exmode_active;
6927
6928 if (need_redraw
6929#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6930 || force_redraw_this
6931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 )
6933 {
6934#if defined(FEAT_GUI) || defined(UNIX)
6935 /* The bold trick makes a single row of pixels appear in the next
6936 * character. When a bold character is removed, the next
6937 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006938 * and for some xterms. */
6939 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940# ifdef FEAT_GUI
6941 gui.in_use
6942# endif
6943# if defined(FEAT_GUI) && defined(UNIX)
6944 ||
6945# endif
6946# ifdef UNIX
6947 term_is_xterm
6948# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006949 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006951 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006953 if (n > HL_ALL)
6954 n = syn_attr2attr(n);
6955 if (n & HL_BOLD)
6956 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958#endif
6959#ifdef FEAT_MBYTE
6960 /* When at the end of the text and overwriting a two-cell
6961 * character with a one-cell character, need to clear the next
6962 * cell. Also when overwriting the left halve of a two-cell char
6963 * with the right halve of a two-cell char. Do this only once
6964 * (mb_off2cells() may return 2 on the right halve). */
6965 if (clear_next_cell)
6966 clear_next_cell = FALSE;
6967 else if (has_mbyte
6968 && (len < 0 ? ptr[mbyte_blen] == NUL
6969 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006970 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006972 && (*mb_off2cells)(off, max_off) == 1
6973 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 clear_next_cell = TRUE;
6975
6976 /* Make sure we never leave a second byte of a double-byte behind,
6977 * it confuses mb_off2cells(). */
6978 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006979 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006981 && (*mb_off2cells)(off, max_off) == 1
6982 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 ScreenLines[off + mbyte_blen] = 0;
6984#endif
6985 ScreenLines[off] = c;
6986 ScreenAttrs[off] = attr;
6987#ifdef FEAT_MBYTE
6988 if (enc_utf8)
6989 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006990 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 ScreenLinesUC[off] = 0;
6992 else
6993 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006994 int i;
6995
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006997 for (i = 0; i < Screen_mco; ++i)
6998 {
6999 ScreenLinesC[i][off] = u8cc[i];
7000 if (u8cc[i] == 0)
7001 break;
7002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 }
7004 if (mbyte_cells == 2)
7005 {
7006 ScreenLines[off + 1] = 0;
7007 ScreenAttrs[off + 1] = attr;
7008 }
7009 screen_char(off, row, col);
7010 }
7011 else if (mbyte_cells == 2)
7012 {
7013 ScreenLines[off + 1] = ptr[1];
7014 ScreenAttrs[off + 1] = attr;
7015 screen_char_2(off, row, col);
7016 }
7017 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7018 {
7019 ScreenLines2[off] = ptr[1];
7020 screen_char(off, row, col);
7021 }
7022 else
7023#endif
7024 screen_char(off, row, col);
7025 }
7026#ifdef FEAT_MBYTE
7027 if (has_mbyte)
7028 {
7029 off += mbyte_cells;
7030 col += mbyte_cells;
7031 ptr += mbyte_blen;
7032 if (clear_next_cell)
7033 ptr = (char_u *)" ";
7034 }
7035 else
7036#endif
7037 {
7038 ++off;
7039 ++col;
7040 ++ptr;
7041 }
7042 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007043
7044#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7045 /* If we detected the next character needs to be redrawn, but the text
7046 * doesn't extend up to there, update the character here. */
7047 if (force_redraw_next && col < screen_Columns)
7048 {
7049# ifdef FEAT_MBYTE
7050 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7051 screen_char_2(off, row, col);
7052 else
7053# endif
7054 screen_char(off, row, col);
7055 }
7056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057}
7058
7059#ifdef FEAT_SEARCH_EXTRA
7060/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007061 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 */
7063 static void
7064start_search_hl()
7065{
7066 if (p_hls && !no_hlsearch)
7067 {
7068 last_pat_prog(&search_hl.rm);
7069 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007070# ifdef FEAT_RELTIME
7071 /* Set the time limit to 'redrawtime'. */
7072 profile_setlimit(p_rdt, &search_hl.tm);
7073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 }
7075}
7076
7077/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007078 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 */
7080 static void
7081end_search_hl()
7082{
7083 if (search_hl.rm.regprog != NULL)
7084 {
7085 vim_free(search_hl.rm.regprog);
7086 search_hl.rm.regprog = NULL;
7087 }
7088}
7089
7090/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007091 * Init for calling prepare_search_hl().
7092 */
7093 static void
7094init_search_hl(wp)
7095 win_T *wp;
7096{
7097 matchitem_T *cur;
7098
7099 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7100 * match */
7101 cur = wp->w_match_head;
7102 while (cur != NULL)
7103 {
7104 cur->hl.rm = cur->match;
7105 if (cur->hlg_id == 0)
7106 cur->hl.attr = 0;
7107 else
7108 cur->hl.attr = syn_id2attr(cur->hlg_id);
7109 cur->hl.buf = wp->w_buffer;
7110 cur->hl.lnum = 0;
7111 cur->hl.first_lnum = 0;
7112# ifdef FEAT_RELTIME
7113 /* Set the time limit to 'redrawtime'. */
7114 profile_setlimit(p_rdt, &(cur->hl.tm));
7115# endif
7116 cur = cur->next;
7117 }
7118 search_hl.buf = wp->w_buffer;
7119 search_hl.lnum = 0;
7120 search_hl.first_lnum = 0;
7121 /* time limit is set at the toplevel, for all windows */
7122}
7123
7124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 * Advance to the match in window "wp" line "lnum" or past it.
7126 */
7127 static void
7128prepare_search_hl(wp, lnum)
7129 win_T *wp;
7130 linenr_T lnum;
7131{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007132 matchitem_T *cur; /* points to the match list */
7133 match_T *shl; /* points to search_hl or a match */
7134 int shl_flag; /* flag to indicate whether search_hl
7135 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 int n;
7137
7138 /*
7139 * When using a multi-line pattern, start searching at the top
7140 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007141 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007143 cur = wp->w_match_head;
7144 shl_flag = FALSE;
7145 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007147 if (shl_flag == FALSE)
7148 {
7149 shl = &search_hl;
7150 shl_flag = TRUE;
7151 }
7152 else
7153 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 if (shl->rm.regprog != NULL
7155 && shl->lnum == 0
7156 && re_multiline(shl->rm.regprog))
7157 {
7158 if (shl->first_lnum == 0)
7159 {
7160# ifdef FEAT_FOLDING
7161 for (shl->first_lnum = lnum;
7162 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7163 if (hasFoldingWin(wp, shl->first_lnum - 1,
7164 NULL, NULL, TRUE, NULL))
7165 break;
7166# else
7167 shl->first_lnum = wp->w_topline;
7168# endif
7169 }
7170 n = 0;
7171 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7172 {
7173 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7174 if (shl->lnum != 0)
7175 {
7176 shl->first_lnum = shl->lnum
7177 + shl->rm.endpos[0].lnum
7178 - shl->rm.startpos[0].lnum;
7179 n = shl->rm.endpos[0].col;
7180 }
7181 else
7182 {
7183 ++shl->first_lnum;
7184 n = 0;
7185 }
7186 }
7187 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007188 if (shl != &search_hl && cur != NULL)
7189 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
7191}
7192
7193/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007194 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 * Uses shl->buf.
7196 * Sets shl->lnum and shl->rm contents.
7197 * Note: Assumes a previous match is always before "lnum", unless
7198 * shl->lnum is zero.
7199 * Careful: Any pointers for buffer lines will become invalid.
7200 */
7201 static void
7202next_search_hl(win, shl, lnum, mincol)
7203 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007204 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 linenr_T lnum;
7206 colnr_T mincol; /* minimal column for a match */
7207{
7208 linenr_T l;
7209 colnr_T matchcol;
7210 long nmatched;
7211
7212 if (shl->lnum != 0)
7213 {
7214 /* Check for three situations:
7215 * 1. If the "lnum" is below a previous match, start a new search.
7216 * 2. If the previous match includes "mincol", use it.
7217 * 3. Continue after the previous match.
7218 */
7219 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7220 if (lnum > l)
7221 shl->lnum = 0;
7222 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7223 return;
7224 }
7225
7226 /*
7227 * Repeat searching for a match until one is found that includes "mincol"
7228 * or none is found in this line.
7229 */
7230 called_emsg = FALSE;
7231 for (;;)
7232 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007233#ifdef FEAT_RELTIME
7234 /* Stop searching after passing the time limit. */
7235 if (profile_passed_limit(&(shl->tm)))
7236 {
7237 shl->lnum = 0; /* no match found in time */
7238 break;
7239 }
7240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 /* Three situations:
7242 * 1. No useful previous match: search from start of line.
7243 * 2. Not Vi compatible or empty match: continue at next character.
7244 * Break the loop if this is beyond the end of the line.
7245 * 3. Vi compatible searching: continue at end of previous match.
7246 */
7247 if (shl->lnum == 0)
7248 matchcol = 0;
7249 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7250 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007251 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007253 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007254
7255 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007256 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007257 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007259 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 shl->lnum = 0;
7261 break;
7262 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007263#ifdef FEAT_MBYTE
7264 if (has_mbyte)
7265 matchcol += mb_ptr2len(ml);
7266 else
7267#endif
7268 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270 else
7271 matchcol = shl->rm.endpos[0].col;
7272
7273 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007274 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7275#ifdef FEAT_RELTIME
7276 &(shl->tm)
7277#else
7278 NULL
7279#endif
7280 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007281 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 {
7283 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007284 if (shl == &search_hl)
7285 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007286 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007287 vim_free(shl->rm.regprog);
7288 no_hlsearch = TRUE;
7289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007291 shl->lnum = 0;
7292 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 break;
7294 }
7295 if (nmatched == 0)
7296 {
7297 shl->lnum = 0; /* no match found */
7298 break;
7299 }
7300 if (shl->rm.startpos[0].lnum > 0
7301 || shl->rm.startpos[0].col >= mincol
7302 || nmatched > 1
7303 || shl->rm.endpos[0].col > mincol)
7304 {
7305 shl->lnum += shl->rm.startpos[0].lnum;
7306 break; /* useful match found */
7307 }
7308 }
7309}
7310#endif
7311
7312 static void
7313screen_start_highlight(attr)
7314 int attr;
7315{
7316 attrentry_T *aep = NULL;
7317
7318 screen_attr = attr;
7319 if (full_screen
7320#ifdef WIN3264
7321 && termcap_active
7322#endif
7323 )
7324 {
7325#ifdef FEAT_GUI
7326 if (gui.in_use)
7327 {
7328 char buf[20];
7329
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007330 /* The GUI handles this internally. */
7331 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 OUT_STR(buf);
7333 }
7334 else
7335#endif
7336 {
7337 if (attr > HL_ALL) /* special HL attr. */
7338 {
7339 if (t_colors > 1)
7340 aep = syn_cterm_attr2entry(attr);
7341 else
7342 aep = syn_term_attr2entry(attr);
7343 if (aep == NULL) /* did ":syntax clear" */
7344 attr = 0;
7345 else
7346 attr = aep->ae_attr;
7347 }
7348 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7349 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007350 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7351 && cterm_normal_fg_bold)
7352 /* If the Normal FG color has BOLD attribute and the new HL
7353 * has a FG color defined, clear BOLD. */
7354 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7356 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007357 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7358 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 out_str(T_US);
7360 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7361 out_str(T_CZH);
7362 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7363 out_str(T_MR);
7364
7365 /*
7366 * Output the color or start string after bold etc., in case the
7367 * bold etc. override the color setting.
7368 */
7369 if (aep != NULL)
7370 {
7371 if (t_colors > 1)
7372 {
7373 if (aep->ae_u.cterm.fg_color)
7374 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7375 if (aep->ae_u.cterm.bg_color)
7376 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7377 }
7378 else
7379 {
7380 if (aep->ae_u.term.start != NULL)
7381 out_str(aep->ae_u.term.start);
7382 }
7383 }
7384 }
7385 }
7386}
7387
7388 void
7389screen_stop_highlight()
7390{
7391 int do_ME = FALSE; /* output T_ME code */
7392
7393 if (screen_attr != 0
7394#ifdef WIN3264
7395 && termcap_active
7396#endif
7397 )
7398 {
7399#ifdef FEAT_GUI
7400 if (gui.in_use)
7401 {
7402 char buf[20];
7403
7404 /* use internal GUI code */
7405 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7406 OUT_STR(buf);
7407 }
7408 else
7409#endif
7410 {
7411 if (screen_attr > HL_ALL) /* special HL attr. */
7412 {
7413 attrentry_T *aep;
7414
7415 if (t_colors > 1)
7416 {
7417 /*
7418 * Assume that t_me restores the original colors!
7419 */
7420 aep = syn_cterm_attr2entry(screen_attr);
7421 if (aep != NULL && (aep->ae_u.cterm.fg_color
7422 || aep->ae_u.cterm.bg_color))
7423 do_ME = TRUE;
7424 }
7425 else
7426 {
7427 aep = syn_term_attr2entry(screen_attr);
7428 if (aep != NULL && aep->ae_u.term.stop != NULL)
7429 {
7430 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7431 do_ME = TRUE;
7432 else
7433 out_str(aep->ae_u.term.stop);
7434 }
7435 }
7436 if (aep == NULL) /* did ":syntax clear" */
7437 screen_attr = 0;
7438 else
7439 screen_attr = aep->ae_attr;
7440 }
7441
7442 /*
7443 * Often all ending-codes are equal to T_ME. Avoid outputting the
7444 * same sequence several times.
7445 */
7446 if (screen_attr & HL_STANDOUT)
7447 {
7448 if (STRCMP(T_SE, T_ME) == 0)
7449 do_ME = TRUE;
7450 else
7451 out_str(T_SE);
7452 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007453 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 {
7455 if (STRCMP(T_UE, T_ME) == 0)
7456 do_ME = TRUE;
7457 else
7458 out_str(T_UE);
7459 }
7460 if (screen_attr & HL_ITALIC)
7461 {
7462 if (STRCMP(T_CZR, T_ME) == 0)
7463 do_ME = TRUE;
7464 else
7465 out_str(T_CZR);
7466 }
7467 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7468 out_str(T_ME);
7469
7470 if (t_colors > 1)
7471 {
7472 /* set Normal cterm colors */
7473 if (cterm_normal_fg_color != 0)
7474 term_fg_color(cterm_normal_fg_color - 1);
7475 if (cterm_normal_bg_color != 0)
7476 term_bg_color(cterm_normal_bg_color - 1);
7477 if (cterm_normal_fg_bold)
7478 out_str(T_MD);
7479 }
7480 }
7481 }
7482 screen_attr = 0;
7483}
7484
7485/*
7486 * Reset the colors for a cterm. Used when leaving Vim.
7487 * The machine specific code may override this again.
7488 */
7489 void
7490reset_cterm_colors()
7491{
7492 if (t_colors > 1)
7493 {
7494 /* set Normal cterm colors */
7495 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7496 {
7497 out_str(T_OP);
7498 screen_attr = -1;
7499 }
7500 if (cterm_normal_fg_bold)
7501 {
7502 out_str(T_ME);
7503 screen_attr = -1;
7504 }
7505 }
7506}
7507
7508/*
7509 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7510 * using the attributes from ScreenAttrs["off"].
7511 */
7512 static void
7513screen_char(off, row, col)
7514 unsigned off;
7515 int row;
7516 int col;
7517{
7518 int attr;
7519
7520 /* Check for illegal values, just in case (could happen just after
7521 * resizing). */
7522 if (row >= screen_Rows || col >= screen_Columns)
7523 return;
7524
7525 /* Outputting the last character on the screen may scrollup the screen.
7526 * Don't to it! Mark the character invalid (update it when scrolled up) */
7527 if (row == screen_Rows - 1 && col == screen_Columns - 1
7528#ifdef FEAT_RIGHTLEFT
7529 /* account for first command-line character in rightleft mode */
7530 && !cmdmsg_rl
7531#endif
7532 )
7533 {
7534 ScreenAttrs[off] = (sattr_T)-1;
7535 return;
7536 }
7537
7538 /*
7539 * Stop highlighting first, so it's easier to move the cursor.
7540 */
7541#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7542 if (screen_char_attr != 0)
7543 attr = screen_char_attr;
7544 else
7545#endif
7546 attr = ScreenAttrs[off];
7547 if (screen_attr != attr)
7548 screen_stop_highlight();
7549
7550 windgoto(row, col);
7551
7552 if (screen_attr != attr)
7553 screen_start_highlight(attr);
7554
7555#ifdef FEAT_MBYTE
7556 if (enc_utf8 && ScreenLinesUC[off] != 0)
7557 {
7558 char_u buf[MB_MAXBYTES + 1];
7559
7560 /* Convert UTF-8 character to bytes and write it. */
7561
7562 buf[utfc_char2bytes(off, buf)] = NUL;
7563
7564 out_str(buf);
7565 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7566 ++screen_cur_col;
7567 }
7568 else
7569#endif
7570 {
7571#ifdef FEAT_MBYTE
7572 out_flush_check();
7573#endif
7574 out_char(ScreenLines[off]);
7575#ifdef FEAT_MBYTE
7576 /* double-byte character in single-width cell */
7577 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7578 out_char(ScreenLines2[off]);
7579#endif
7580 }
7581
7582 screen_cur_col++;
7583}
7584
7585#ifdef FEAT_MBYTE
7586
7587/*
7588 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7589 * on the screen at position 'row' and 'col'.
7590 * The attributes of the first byte is used for all. This is required to
7591 * output the two bytes of a double-byte character with nothing in between.
7592 */
7593 static void
7594screen_char_2(off, row, col)
7595 unsigned off;
7596 int row;
7597 int col;
7598{
7599 /* Check for illegal values (could be wrong when screen was resized). */
7600 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7601 return;
7602
7603 /* Outputting the last character on the screen may scrollup the screen.
7604 * Don't to it! Mark the character invalid (update it when scrolled up) */
7605 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7606 {
7607 ScreenAttrs[off] = (sattr_T)-1;
7608 return;
7609 }
7610
7611 /* Output the first byte normally (positions the cursor), then write the
7612 * second byte directly. */
7613 screen_char(off, row, col);
7614 out_char(ScreenLines[off + 1]);
7615 ++screen_cur_col;
7616}
7617#endif
7618
7619#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7620/*
7621 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7622 * This uses the contents of ScreenLines[] and doesn't change it.
7623 */
7624 void
7625screen_draw_rectangle(row, col, height, width, invert)
7626 int row;
7627 int col;
7628 int height;
7629 int width;
7630 int invert;
7631{
7632 int r, c;
7633 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007634#ifdef FEAT_MBYTE
7635 int max_off;
7636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007638 /* Can't use ScreenLines unless initialized */
7639 if (ScreenLines == NULL)
7640 return;
7641
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (invert)
7643 screen_char_attr = HL_INVERSE;
7644 for (r = row; r < row + height; ++r)
7645 {
7646 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007647#ifdef FEAT_MBYTE
7648 max_off = off + screen_Columns;
7649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 for (c = col; c < col + width; ++c)
7651 {
7652#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007653 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {
7655 screen_char_2(off + c, r, c);
7656 ++c;
7657 }
7658 else
7659#endif
7660 {
7661 screen_char(off + c, r, c);
7662#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007663 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 ++c;
7665#endif
7666 }
7667 }
7668 }
7669 screen_char_attr = 0;
7670}
7671#endif
7672
7673#ifdef FEAT_VERTSPLIT
7674/*
7675 * Redraw the characters for a vertically split window.
7676 */
7677 static void
7678redraw_block(row, end, wp)
7679 int row;
7680 int end;
7681 win_T *wp;
7682{
7683 int col;
7684 int width;
7685
7686# ifdef FEAT_CLIPBOARD
7687 clip_may_clear_selection(row, end - 1);
7688# endif
7689
7690 if (wp == NULL)
7691 {
7692 col = 0;
7693 width = Columns;
7694 }
7695 else
7696 {
7697 col = wp->w_wincol;
7698 width = wp->w_width;
7699 }
7700 screen_draw_rectangle(row, col, end - row, width, FALSE);
7701}
7702#endif
7703
7704/*
7705 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7706 * with character 'c1' in first column followed by 'c2' in the other columns.
7707 * Use attributes 'attr'.
7708 */
7709 void
7710screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7711 int start_row, end_row;
7712 int start_col, end_col;
7713 int c1, c2;
7714 int attr;
7715{
7716 int row;
7717 int col;
7718 int off;
7719 int end_off;
7720 int did_delete;
7721 int c;
7722 int norm_term;
7723#if defined(FEAT_GUI) || defined(UNIX)
7724 int force_next = FALSE;
7725#endif
7726
7727 if (end_row > screen_Rows) /* safety check */
7728 end_row = screen_Rows;
7729 if (end_col > screen_Columns) /* safety check */
7730 end_col = screen_Columns;
7731 if (ScreenLines == NULL
7732 || start_row >= end_row
7733 || start_col >= end_col) /* nothing to do */
7734 return;
7735
7736 /* it's a "normal" terminal when not in a GUI or cterm */
7737 norm_term = (
7738#ifdef FEAT_GUI
7739 !gui.in_use &&
7740#endif
7741 t_colors <= 1);
7742 for (row = start_row; row < end_row; ++row)
7743 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007744#ifdef FEAT_MBYTE
7745 if (has_mbyte
7746# ifdef FEAT_GUI
7747 && !gui.in_use
7748# endif
7749 )
7750 {
7751 /* When drawing over the right halve of a double-wide char clear
7752 * out the left halve. When drawing over the left halve of a
7753 * double wide-char clear out the right halve. Only needed in a
7754 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007755 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007756 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007757 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007758 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007759 }
7760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 /*
7762 * Try to use delete-line termcap code, when no attributes or in a
7763 * "normal" terminal, where a bold/italic space is just a
7764 * space.
7765 */
7766 did_delete = FALSE;
7767 if (c2 == ' '
7768 && end_col == Columns
7769 && can_clear(T_CE)
7770 && (attr == 0
7771 || (norm_term
7772 && attr <= HL_ALL
7773 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7774 {
7775 /*
7776 * check if we really need to clear something
7777 */
7778 col = start_col;
7779 if (c1 != ' ') /* don't clear first char */
7780 ++col;
7781
7782 off = LineOffset[row] + col;
7783 end_off = LineOffset[row] + end_col;
7784
7785 /* skip blanks (used often, keep it fast!) */
7786#ifdef FEAT_MBYTE
7787 if (enc_utf8)
7788 while (off < end_off && ScreenLines[off] == ' '
7789 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7790 ++off;
7791 else
7792#endif
7793 while (off < end_off && ScreenLines[off] == ' '
7794 && ScreenAttrs[off] == 0)
7795 ++off;
7796 if (off < end_off) /* something to be cleared */
7797 {
7798 col = off - LineOffset[row];
7799 screen_stop_highlight();
7800 term_windgoto(row, col);/* clear rest of this screen line */
7801 out_str(T_CE);
7802 screen_start(); /* don't know where cursor is now */
7803 col = end_col - col;
7804 while (col--) /* clear chars in ScreenLines */
7805 {
7806 ScreenLines[off] = ' ';
7807#ifdef FEAT_MBYTE
7808 if (enc_utf8)
7809 ScreenLinesUC[off] = 0;
7810#endif
7811 ScreenAttrs[off] = 0;
7812 ++off;
7813 }
7814 }
7815 did_delete = TRUE; /* the chars are cleared now */
7816 }
7817
7818 off = LineOffset[row] + start_col;
7819 c = c1;
7820 for (col = start_col; col < end_col; ++col)
7821 {
7822 if (ScreenLines[off] != c
7823#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007824 || (enc_utf8 && (int)ScreenLinesUC[off]
7825 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826#endif
7827 || ScreenAttrs[off] != attr
7828#if defined(FEAT_GUI) || defined(UNIX)
7829 || force_next
7830#endif
7831 )
7832 {
7833#if defined(FEAT_GUI) || defined(UNIX)
7834 /* The bold trick may make a single row of pixels appear in
7835 * the next character. When a bold character is removed, the
7836 * next character should be redrawn too. This happens for our
7837 * own GUI and for some xterms. */
7838 if (
7839# ifdef FEAT_GUI
7840 gui.in_use
7841# endif
7842# if defined(FEAT_GUI) && defined(UNIX)
7843 ||
7844# endif
7845# ifdef UNIX
7846 term_is_xterm
7847# endif
7848 )
7849 {
7850 if (ScreenLines[off] != ' '
7851 && (ScreenAttrs[off] > HL_ALL
7852 || ScreenAttrs[off] & HL_BOLD))
7853 force_next = TRUE;
7854 else
7855 force_next = FALSE;
7856 }
7857#endif
7858 ScreenLines[off] = c;
7859#ifdef FEAT_MBYTE
7860 if (enc_utf8)
7861 {
7862 if (c >= 0x80)
7863 {
7864 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007865 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 }
7867 else
7868 ScreenLinesUC[off] = 0;
7869 }
7870#endif
7871 ScreenAttrs[off] = attr;
7872 if (!did_delete || c != ' ')
7873 screen_char(off, row, col);
7874 }
7875 ++off;
7876 if (col == start_col)
7877 {
7878 if (did_delete)
7879 break;
7880 c = c2;
7881 }
7882 }
7883 if (end_col == Columns)
7884 LineWraps[row] = FALSE;
7885 if (row == Rows - 1) /* overwritten the command line */
7886 {
7887 redraw_cmdline = TRUE;
7888 if (c1 == ' ' && c2 == ' ')
7889 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007890 if (start_col == 0)
7891 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 }
7893 }
7894}
7895
7896/*
7897 * Check if there should be a delay. Used before clearing or redrawing the
7898 * screen or the command line.
7899 */
7900 void
7901check_for_delay(check_msg_scroll)
7902 int check_msg_scroll;
7903{
7904 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7905 && !did_wait_return
7906 && emsg_silent == 0)
7907 {
7908 out_flush();
7909 ui_delay(1000L, TRUE);
7910 emsg_on_display = FALSE;
7911 if (check_msg_scroll)
7912 msg_scroll = FALSE;
7913 }
7914}
7915
7916/*
7917 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007918 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 * Returns TRUE if there is a valid screen to write to.
7920 * Returns FALSE when starting up and screen not initialized yet.
7921 */
7922 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007923screen_valid(doclear)
7924 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007926 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 return (ScreenLines != NULL);
7928}
7929
7930/*
7931 * Resize the shell to Rows and Columns.
7932 * Allocate ScreenLines[] and associated items.
7933 *
7934 * There may be some time between setting Rows and Columns and (re)allocating
7935 * ScreenLines[]. This happens when starting up and when (manually) changing
7936 * the shell size. Always use screen_Rows and screen_Columns to access items
7937 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7938 * final size of the shell is needed.
7939 */
7940 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007941screenalloc(doclear)
7942 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944 int new_row, old_row;
7945#ifdef FEAT_GUI
7946 int old_Rows;
7947#endif
7948 win_T *wp;
7949 int outofmem = FALSE;
7950 int len;
7951 schar_T *new_ScreenLines;
7952#ifdef FEAT_MBYTE
7953 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007954 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007956 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957#endif
7958 sattr_T *new_ScreenAttrs;
7959 unsigned *new_LineOffset;
7960 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007961#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007962 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007963 tabpage_T *tp;
7964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007966 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007967#ifdef FEAT_AUTOCMD
7968 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007970retry:
7971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 /*
7973 * Allocation of the screen buffers is done only when the size changes and
7974 * when Rows and Columns have been set and we have started doing full
7975 * screen stuff.
7976 */
7977 if ((ScreenLines != NULL
7978 && Rows == screen_Rows
7979 && Columns == screen_Columns
7980#ifdef FEAT_MBYTE
7981 && enc_utf8 == (ScreenLinesUC != NULL)
7982 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007983 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984#endif
7985 )
7986 || Rows == 0
7987 || Columns == 0
7988 || (!full_screen && ScreenLines == NULL))
7989 return;
7990
7991 /*
7992 * It's possible that we produce an out-of-memory message below, which
7993 * will cause this function to be called again. To break the loop, just
7994 * return here.
7995 */
7996 if (entered)
7997 return;
7998 entered = TRUE;
7999
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008000 /*
8001 * Note that the window sizes are updated before reallocating the arrays,
8002 * thus we must not redraw here!
8003 */
8004 ++RedrawingDisabled;
8005
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 win_new_shellsize(); /* fit the windows in the new sized shell */
8007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 comp_col(); /* recompute columns for shown command and ruler */
8009
8010 /*
8011 * We're changing the size of the screen.
8012 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8013 * - Move lines from the old arrays into the new arrays, clear extra
8014 * lines (unless the screen is going to be cleared).
8015 * - Free the old arrays.
8016 *
8017 * If anything fails, make ScreenLines NULL, so we don't do anything!
8018 * Continuing with the old ScreenLines may result in a crash, because the
8019 * size is wrong.
8020 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008021 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008023#ifdef FEAT_AUTOCMD
8024 if (aucmd_win != NULL)
8025 win_free_lsize(aucmd_win);
8026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027
8028 new_ScreenLines = (schar_T *)lalloc((long_u)(
8029 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8030#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008031 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (enc_utf8)
8033 {
8034 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8035 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008036 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008037 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8039 }
8040 if (enc_dbcs == DBCS_JPNU)
8041 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8042 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8043#endif
8044 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8045 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8046 new_LineOffset = (unsigned *)lalloc((long_u)(
8047 Rows * sizeof(unsigned)), FALSE);
8048 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008049#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008050 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008053 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {
8055 if (win_alloc_lines(wp) == FAIL)
8056 {
8057 outofmem = TRUE;
8058#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008059 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060#endif
8061 }
8062 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008063#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008064 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8065 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008066 outofmem = TRUE;
8067#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008068#ifdef FEAT_WINDOWS
8069give_up:
8070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008072#ifdef FEAT_MBYTE
8073 for (i = 0; i < p_mco; ++i)
8074 if (new_ScreenLinesC[i] == NULL)
8075 break;
8076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 if (new_ScreenLines == NULL
8078#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008079 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8081#endif
8082 || new_ScreenAttrs == NULL
8083 || new_LineOffset == NULL
8084 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008085#ifdef FEAT_WINDOWS
8086 || new_TabPageIdxs == NULL
8087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 || outofmem)
8089 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008090 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008091 {
8092 /* guess the size */
8093 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8094
8095 /* Remember we did this to avoid getting outofmem messages over
8096 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008097 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 vim_free(new_ScreenLines);
8100 new_ScreenLines = NULL;
8101#ifdef FEAT_MBYTE
8102 vim_free(new_ScreenLinesUC);
8103 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008104 for (i = 0; i < p_mco; ++i)
8105 {
8106 vim_free(new_ScreenLinesC[i]);
8107 new_ScreenLinesC[i] = NULL;
8108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 vim_free(new_ScreenLines2);
8110 new_ScreenLines2 = NULL;
8111#endif
8112 vim_free(new_ScreenAttrs);
8113 new_ScreenAttrs = NULL;
8114 vim_free(new_LineOffset);
8115 new_LineOffset = NULL;
8116 vim_free(new_LineWraps);
8117 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008118#ifdef FEAT_WINDOWS
8119 vim_free(new_TabPageIdxs);
8120 new_TabPageIdxs = NULL;
8121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 }
8123 else
8124 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008125 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008126
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 for (new_row = 0; new_row < Rows; ++new_row)
8128 {
8129 new_LineOffset[new_row] = new_row * Columns;
8130 new_LineWraps[new_row] = FALSE;
8131
8132 /*
8133 * If the screen is not going to be cleared, copy as much as
8134 * possible from the old screen to the new one and clear the rest
8135 * (used when resizing the window at the "--more--" prompt or when
8136 * executing an external command, for the GUI).
8137 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008138 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {
8140 (void)vim_memset(new_ScreenLines + new_row * Columns,
8141 ' ', (size_t)Columns * sizeof(schar_T));
8142#ifdef FEAT_MBYTE
8143 if (enc_utf8)
8144 {
8145 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8146 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008147 for (i = 0; i < p_mco; ++i)
8148 (void)vim_memset(new_ScreenLinesC[i]
8149 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 0, (size_t)Columns * sizeof(u8char_T));
8151 }
8152 if (enc_dbcs == DBCS_JPNU)
8153 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8154 0, (size_t)Columns * sizeof(schar_T));
8155#endif
8156 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8157 0, (size_t)Columns * sizeof(sattr_T));
8158 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008159 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {
8161 if (screen_Columns < Columns)
8162 len = screen_Columns;
8163 else
8164 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008165#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008166 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008167 * may be invalid now. Also when p_mco changes. */
8168 if (!(enc_utf8 && ScreenLinesUC == NULL)
8169 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008170#endif
8171 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8172 ScreenLines + LineOffset[old_row],
8173 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008175 if (enc_utf8 && ScreenLinesUC != NULL
8176 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {
8178 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8179 ScreenLinesUC + LineOffset[old_row],
8180 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008181 for (i = 0; i < p_mco; ++i)
8182 mch_memmove(new_ScreenLinesC[i]
8183 + new_LineOffset[new_row],
8184 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 (size_t)len * sizeof(u8char_T));
8186 }
8187 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8188 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8189 ScreenLines2 + LineOffset[old_row],
8190 (size_t)len * sizeof(schar_T));
8191#endif
8192 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8193 ScreenAttrs + LineOffset[old_row],
8194 (size_t)len * sizeof(sattr_T));
8195 }
8196 }
8197 }
8198 /* Use the last line of the screen for the current line. */
8199 current_ScreenLine = new_ScreenLines + Rows * Columns;
8200 }
8201
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008202 free_screenlines();
8203
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 ScreenLines = new_ScreenLines;
8205#ifdef FEAT_MBYTE
8206 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008207 for (i = 0; i < p_mco; ++i)
8208 ScreenLinesC[i] = new_ScreenLinesC[i];
8209 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 ScreenLines2 = new_ScreenLines2;
8211#endif
8212 ScreenAttrs = new_ScreenAttrs;
8213 LineOffset = new_LineOffset;
8214 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008215#ifdef FEAT_WINDOWS
8216 TabPageIdxs = new_TabPageIdxs;
8217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218
8219 /* It's important that screen_Rows and screen_Columns reflect the actual
8220 * size of ScreenLines[]. Set them before calling anything. */
8221#ifdef FEAT_GUI
8222 old_Rows = screen_Rows;
8223#endif
8224 screen_Rows = Rows;
8225 screen_Columns = Columns;
8226
8227 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008228 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 screenclear2();
8230
8231#ifdef FEAT_GUI
8232 else if (gui.in_use
8233 && !gui.starting
8234 && ScreenLines != NULL
8235 && old_Rows != Rows)
8236 {
8237 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8238 /*
8239 * Adjust the position of the cursor, for when executing an external
8240 * command.
8241 */
8242 if (msg_row >= Rows) /* Rows got smaller */
8243 msg_row = Rows - 1; /* put cursor at last row */
8244 else if (Rows > old_Rows) /* Rows got bigger */
8245 msg_row += Rows - old_Rows; /* put cursor in same place */
8246 if (msg_col >= Columns) /* Columns got smaller */
8247 msg_col = Columns - 1; /* put cursor at last column */
8248 }
8249#endif
8250
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008252 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008253
8254#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008255 /*
8256 * Do not apply autocommands more than 3 times to avoid an endless loop
8257 * in case applying autocommands always changes Rows or Columns.
8258 */
8259 if (starting == 0 && ++retry_count <= 3)
8260 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008261 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008262 /* In rare cases, autocommands may have altered Rows or Columns,
8263 * jump back to check if we need to allocate the screen again. */
8264 goto retry;
8265 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267}
8268
8269 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008270free_screenlines()
8271{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008272#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008273 int i;
8274
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008275 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008276 for (i = 0; i < Screen_mco; ++i)
8277 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008278 vim_free(ScreenLines2);
8279#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008280 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008281 vim_free(ScreenAttrs);
8282 vim_free(LineOffset);
8283 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008284#ifdef FEAT_WINDOWS
8285 vim_free(TabPageIdxs);
8286#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008287}
8288
8289 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290screenclear()
8291{
8292 check_for_delay(FALSE);
8293 screenalloc(FALSE); /* allocate screen buffers if size changed */
8294 screenclear2(); /* clear the screen */
8295}
8296
8297 static void
8298screenclear2()
8299{
8300 int i;
8301
8302 if (starting == NO_SCREEN || ScreenLines == NULL
8303#ifdef FEAT_GUI
8304 || (gui.in_use && gui.starting)
8305#endif
8306 )
8307 return;
8308
8309#ifdef FEAT_GUI
8310 if (!gui.in_use)
8311#endif
8312 screen_attr = -1; /* force setting the Normal colors */
8313 screen_stop_highlight(); /* don't want highlighting here */
8314
8315#ifdef FEAT_CLIPBOARD
8316 /* disable selection without redrawing it */
8317 clip_scroll_selection(9999);
8318#endif
8319
8320 /* blank out ScreenLines */
8321 for (i = 0; i < Rows; ++i)
8322 {
8323 lineclear(LineOffset[i], (int)Columns);
8324 LineWraps[i] = FALSE;
8325 }
8326
8327 if (can_clear(T_CL))
8328 {
8329 out_str(T_CL); /* clear the display */
8330 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008331 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 }
8333 else
8334 {
8335 /* can't clear the screen, mark all chars with invalid attributes */
8336 for (i = 0; i < Rows; ++i)
8337 lineinvalid(LineOffset[i], (int)Columns);
8338 clear_cmdline = TRUE;
8339 }
8340
8341 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8342
8343 win_rest_invalid(firstwin);
8344 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008345#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008346 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 if (must_redraw == CLEAR) /* no need to clear again */
8349 must_redraw = NOT_VALID;
8350 compute_cmdrow();
8351 msg_row = cmdline_row; /* put cursor on last line for messages */
8352 msg_col = 0;
8353 screen_start(); /* don't know where cursor is now */
8354 msg_scrolled = 0; /* can't scroll back */
8355 msg_didany = FALSE;
8356 msg_didout = FALSE;
8357}
8358
8359/*
8360 * Clear one line in ScreenLines.
8361 */
8362 static void
8363lineclear(off, width)
8364 unsigned off;
8365 int width;
8366{
8367 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8368#ifdef FEAT_MBYTE
8369 if (enc_utf8)
8370 (void)vim_memset(ScreenLinesUC + off, 0,
8371 (size_t)width * sizeof(u8char_T));
8372#endif
8373 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8374}
8375
8376/*
8377 * Mark one line in ScreenLines invalid by setting the attributes to an
8378 * invalid value.
8379 */
8380 static void
8381lineinvalid(off, width)
8382 unsigned off;
8383 int width;
8384{
8385 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8386}
8387
8388#ifdef FEAT_VERTSPLIT
8389/*
8390 * Copy part of a Screenline for vertically split window "wp".
8391 */
8392 static void
8393linecopy(to, from, wp)
8394 int to;
8395 int from;
8396 win_T *wp;
8397{
8398 unsigned off_to = LineOffset[to] + wp->w_wincol;
8399 unsigned off_from = LineOffset[from] + wp->w_wincol;
8400
8401 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8402 wp->w_width * sizeof(schar_T));
8403# ifdef FEAT_MBYTE
8404 if (enc_utf8)
8405 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008406 int i;
8407
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8409 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008410 for (i = 0; i < p_mco; ++i)
8411 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8412 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 }
8414 if (enc_dbcs == DBCS_JPNU)
8415 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8416 wp->w_width * sizeof(schar_T));
8417# endif
8418 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8419 wp->w_width * sizeof(sattr_T));
8420}
8421#endif
8422
8423/*
8424 * Return TRUE if clearing with term string "p" would work.
8425 * It can't work when the string is empty or it won't set the right background.
8426 */
8427 int
8428can_clear(p)
8429 char_u *p;
8430{
8431 return (*p != NUL && (t_colors <= 1
8432#ifdef FEAT_GUI
8433 || gui.in_use
8434#endif
8435 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8436}
8437
8438/*
8439 * Reset cursor position. Use whenever cursor was moved because of outputting
8440 * something directly to the screen (shell commands) or a terminal control
8441 * code.
8442 */
8443 void
8444screen_start()
8445{
8446 screen_cur_row = screen_cur_col = 9999;
8447}
8448
8449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 * Move the cursor to position "row","col" in the screen.
8451 * This tries to find the most efficient way to move, minimizing the number of
8452 * characters sent to the terminal.
8453 */
8454 void
8455windgoto(row, col)
8456 int row;
8457 int col;
8458{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008459 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 int i;
8461 int plan;
8462 int cost;
8463 int wouldbe_col;
8464 int noinvcurs;
8465 char_u *bs;
8466 int goto_cost;
8467 int attr;
8468
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008469#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8471
8472#define PLAN_LE 1
8473#define PLAN_CR 2
8474#define PLAN_NL 3
8475#define PLAN_WRITE 4
8476 /* Can't use ScreenLines unless initialized */
8477 if (ScreenLines == NULL)
8478 return;
8479
8480 if (col != screen_cur_col || row != screen_cur_row)
8481 {
8482 /* Check for valid position. */
8483 if (row < 0) /* window without text lines? */
8484 row = 0;
8485 if (row >= screen_Rows)
8486 row = screen_Rows - 1;
8487 if (col >= screen_Columns)
8488 col = screen_Columns - 1;
8489
8490 /* check if no cursor movement is allowed in highlight mode */
8491 if (screen_attr && *T_MS == NUL)
8492 noinvcurs = HIGHL_COST;
8493 else
8494 noinvcurs = 0;
8495 goto_cost = GOTO_COST + noinvcurs;
8496
8497 /*
8498 * Plan how to do the positioning:
8499 * 1. Use CR to move it to column 0, same row.
8500 * 2. Use T_LE to move it a few columns to the left.
8501 * 3. Use NL to move a few lines down, column 0.
8502 * 4. Move a few columns to the right with T_ND or by writing chars.
8503 *
8504 * Don't do this if the cursor went beyond the last column, the cursor
8505 * position is unknown then (some terminals wrap, some don't )
8506 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008507 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 * characters to move the cursor to the right.
8509 */
8510 if (row >= screen_cur_row && screen_cur_col < Columns)
8511 {
8512 /*
8513 * If the cursor is in the same row, bigger col, we can use CR
8514 * or T_LE.
8515 */
8516 bs = NULL; /* init for GCC */
8517 attr = screen_attr;
8518 if (row == screen_cur_row && col < screen_cur_col)
8519 {
8520 /* "le" is preferred over "bc", because "bc" is obsolete */
8521 if (*T_LE)
8522 bs = T_LE; /* "cursor left" */
8523 else
8524 bs = T_BC; /* "backspace character (old) */
8525 if (*bs)
8526 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8527 else
8528 cost = 999;
8529 if (col + 1 < cost) /* using CR is less characters */
8530 {
8531 plan = PLAN_CR;
8532 wouldbe_col = 0;
8533 cost = 1; /* CR is just one character */
8534 }
8535 else
8536 {
8537 plan = PLAN_LE;
8538 wouldbe_col = col;
8539 }
8540 if (noinvcurs) /* will stop highlighting */
8541 {
8542 cost += noinvcurs;
8543 attr = 0;
8544 }
8545 }
8546
8547 /*
8548 * If the cursor is above where we want to be, we can use CR LF.
8549 */
8550 else if (row > screen_cur_row)
8551 {
8552 plan = PLAN_NL;
8553 wouldbe_col = 0;
8554 cost = (row - screen_cur_row) * 2; /* CR LF */
8555 if (noinvcurs) /* will stop highlighting */
8556 {
8557 cost += noinvcurs;
8558 attr = 0;
8559 }
8560 }
8561
8562 /*
8563 * If the cursor is in the same row, smaller col, just use write.
8564 */
8565 else
8566 {
8567 plan = PLAN_WRITE;
8568 wouldbe_col = screen_cur_col;
8569 cost = 0;
8570 }
8571
8572 /*
8573 * Check if any characters that need to be written have the
8574 * correct attributes. Also avoid UTF-8 characters.
8575 */
8576 i = col - wouldbe_col;
8577 if (i > 0)
8578 cost += i;
8579 if (cost < goto_cost && i > 0)
8580 {
8581 /*
8582 * Check if the attributes are correct without additionally
8583 * stopping highlighting.
8584 */
8585 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8586 while (i && *p++ == attr)
8587 --i;
8588 if (i != 0)
8589 {
8590 /*
8591 * Try if it works when highlighting is stopped here.
8592 */
8593 if (*--p == 0)
8594 {
8595 cost += noinvcurs;
8596 while (i && *p++ == 0)
8597 --i;
8598 }
8599 if (i != 0)
8600 cost = 999; /* different attributes, don't do it */
8601 }
8602#ifdef FEAT_MBYTE
8603 if (enc_utf8)
8604 {
8605 /* Don't use an UTF-8 char for positioning, it's slow. */
8606 for (i = wouldbe_col; i < col; ++i)
8607 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8608 {
8609 cost = 999;
8610 break;
8611 }
8612 }
8613#endif
8614 }
8615
8616 /*
8617 * We can do it without term_windgoto()!
8618 */
8619 if (cost < goto_cost)
8620 {
8621 if (plan == PLAN_LE)
8622 {
8623 if (noinvcurs)
8624 screen_stop_highlight();
8625 while (screen_cur_col > col)
8626 {
8627 out_str(bs);
8628 --screen_cur_col;
8629 }
8630 }
8631 else if (plan == PLAN_CR)
8632 {
8633 if (noinvcurs)
8634 screen_stop_highlight();
8635 out_char('\r');
8636 screen_cur_col = 0;
8637 }
8638 else if (plan == PLAN_NL)
8639 {
8640 if (noinvcurs)
8641 screen_stop_highlight();
8642 while (screen_cur_row < row)
8643 {
8644 out_char('\n');
8645 ++screen_cur_row;
8646 }
8647 screen_cur_col = 0;
8648 }
8649
8650 i = col - screen_cur_col;
8651 if (i > 0)
8652 {
8653 /*
8654 * Use cursor-right if it's one character only. Avoids
8655 * removing a line of pixels from the last bold char, when
8656 * using the bold trick in the GUI.
8657 */
8658 if (T_ND[0] != NUL && T_ND[1] == NUL)
8659 {
8660 while (i-- > 0)
8661 out_char(*T_ND);
8662 }
8663 else
8664 {
8665 int off;
8666
8667 off = LineOffset[row] + screen_cur_col;
8668 while (i-- > 0)
8669 {
8670 if (ScreenAttrs[off] != screen_attr)
8671 screen_stop_highlight();
8672#ifdef FEAT_MBYTE
8673 out_flush_check();
8674#endif
8675 out_char(ScreenLines[off]);
8676#ifdef FEAT_MBYTE
8677 if (enc_dbcs == DBCS_JPNU
8678 && ScreenLines[off] == 0x8e)
8679 out_char(ScreenLines2[off]);
8680#endif
8681 ++off;
8682 }
8683 }
8684 }
8685 }
8686 }
8687 else
8688 cost = 999;
8689
8690 if (cost >= goto_cost)
8691 {
8692 if (noinvcurs)
8693 screen_stop_highlight();
8694 if (row == screen_cur_row && (col > screen_cur_col) &&
8695 *T_CRI != NUL)
8696 term_cursor_right(col - screen_cur_col);
8697 else
8698 term_windgoto(row, col);
8699 }
8700 screen_cur_row = row;
8701 screen_cur_col = col;
8702 }
8703}
8704
8705/*
8706 * Set cursor to its position in the current window.
8707 */
8708 void
8709setcursor()
8710{
8711 if (redrawing())
8712 {
8713 validate_cursor();
8714 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8715 W_WINCOL(curwin) + (
8716#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008717 /* With 'rightleft' set and the cursor on a double-wide
8718 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8720# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008721 (has_mbyte
8722 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8723 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724# endif
8725 1)) :
8726#endif
8727 curwin->w_wcol));
8728 }
8729}
8730
8731
8732/*
8733 * insert 'line_count' lines at 'row' in window 'wp'
8734 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8735 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8736 * scrolling.
8737 * Returns FAIL if the lines are not inserted, OK for success.
8738 */
8739 int
8740win_ins_lines(wp, row, line_count, invalid, mayclear)
8741 win_T *wp;
8742 int row;
8743 int line_count;
8744 int invalid;
8745 int mayclear;
8746{
8747 int did_delete;
8748 int nextrow;
8749 int lastrow;
8750 int retval;
8751
8752 if (invalid)
8753 wp->w_lines_valid = 0;
8754
8755 if (wp->w_height < 5)
8756 return FAIL;
8757
8758 if (line_count > wp->w_height - row)
8759 line_count = wp->w_height - row;
8760
8761 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8762 if (retval != MAYBE)
8763 return retval;
8764
8765 /*
8766 * If there is a next window or a status line, we first try to delete the
8767 * lines at the bottom to avoid messing what is after the window.
8768 * If this fails and there are following windows, don't do anything to avoid
8769 * messing up those windows, better just redraw.
8770 */
8771 did_delete = FALSE;
8772#ifdef FEAT_WINDOWS
8773 if (wp->w_next != NULL || wp->w_status_height)
8774 {
8775 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8776 line_count, (int)Rows, FALSE, NULL) == OK)
8777 did_delete = TRUE;
8778 else if (wp->w_next)
8779 return FAIL;
8780 }
8781#endif
8782 /*
8783 * if no lines deleted, blank the lines that will end up below the window
8784 */
8785 if (!did_delete)
8786 {
8787#ifdef FEAT_WINDOWS
8788 wp->w_redr_status = TRUE;
8789#endif
8790 redraw_cmdline = TRUE;
8791 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8792 lastrow = nextrow + line_count;
8793 if (lastrow > Rows)
8794 lastrow = Rows;
8795 screen_fill(nextrow - line_count, lastrow - line_count,
8796 W_WINCOL(wp), (int)W_ENDCOL(wp),
8797 ' ', ' ', 0);
8798 }
8799
8800 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8801 == FAIL)
8802 {
8803 /* deletion will have messed up other windows */
8804 if (did_delete)
8805 {
8806#ifdef FEAT_WINDOWS
8807 wp->w_redr_status = TRUE;
8808#endif
8809 win_rest_invalid(W_NEXT(wp));
8810 }
8811 return FAIL;
8812 }
8813
8814 return OK;
8815}
8816
8817/*
8818 * delete "line_count" window lines at "row" in window "wp"
8819 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8820 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8821 * scrolling
8822 * Return OK for success, FAIL if the lines are not deleted.
8823 */
8824 int
8825win_del_lines(wp, row, line_count, invalid, mayclear)
8826 win_T *wp;
8827 int row;
8828 int line_count;
8829 int invalid;
8830 int mayclear;
8831{
8832 int retval;
8833
8834 if (invalid)
8835 wp->w_lines_valid = 0;
8836
8837 if (line_count > wp->w_height - row)
8838 line_count = wp->w_height - row;
8839
8840 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8841 if (retval != MAYBE)
8842 return retval;
8843
8844 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8845 (int)Rows, FALSE, NULL) == FAIL)
8846 return FAIL;
8847
8848#ifdef FEAT_WINDOWS
8849 /*
8850 * If there are windows or status lines below, try to put them at the
8851 * correct place. If we can't do that, they have to be redrawn.
8852 */
8853 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8854 {
8855 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8856 line_count, (int)Rows, NULL) == FAIL)
8857 {
8858 wp->w_redr_status = TRUE;
8859 win_rest_invalid(wp->w_next);
8860 }
8861 }
8862 /*
8863 * If this is the last window and there is no status line, redraw the
8864 * command line later.
8865 */
8866 else
8867#endif
8868 redraw_cmdline = TRUE;
8869 return OK;
8870}
8871
8872/*
8873 * Common code for win_ins_lines() and win_del_lines().
8874 * Returns OK or FAIL when the work has been done.
8875 * Returns MAYBE when not finished yet.
8876 */
8877 static int
8878win_do_lines(wp, row, line_count, mayclear, del)
8879 win_T *wp;
8880 int row;
8881 int line_count;
8882 int mayclear;
8883 int del;
8884{
8885 int retval;
8886
8887 if (!redrawing() || line_count <= 0)
8888 return FAIL;
8889
8890 /* only a few lines left: redraw is faster */
8891 if (mayclear && Rows - line_count < 5
8892#ifdef FEAT_VERTSPLIT
8893 && wp->w_width == Columns
8894#endif
8895 )
8896 {
8897 screenclear(); /* will set wp->w_lines_valid to 0 */
8898 return FAIL;
8899 }
8900
8901 /*
8902 * Delete all remaining lines
8903 */
8904 if (row + line_count >= wp->w_height)
8905 {
8906 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8907 W_WINCOL(wp), (int)W_ENDCOL(wp),
8908 ' ', ' ', 0);
8909 return OK;
8910 }
8911
8912 /*
8913 * when scrolling, the message on the command line should be cleared,
8914 * otherwise it will stay there forever.
8915 */
8916 clear_cmdline = TRUE;
8917
8918 /*
8919 * If the terminal can set a scroll region, use that.
8920 * Always do this in a vertically split window. This will redraw from
8921 * ScreenLines[] when t_CV isn't defined. That's faster than using
8922 * win_line().
8923 * Don't use a scroll region when we are going to redraw the text, writing
8924 * a character in the lower right corner of the scroll region causes a
8925 * scroll-up in the DJGPP version.
8926 */
8927 if (scroll_region
8928#ifdef FEAT_VERTSPLIT
8929 || W_WIDTH(wp) != Columns
8930#endif
8931 )
8932 {
8933#ifdef FEAT_VERTSPLIT
8934 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8935#endif
8936 scroll_region_set(wp, row);
8937 if (del)
8938 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8939 wp->w_height - row, FALSE, wp);
8940 else
8941 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8942 wp->w_height - row, wp);
8943#ifdef FEAT_VERTSPLIT
8944 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8945#endif
8946 scroll_region_reset();
8947 return retval;
8948 }
8949
8950#ifdef FEAT_WINDOWS
8951 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8952 return FAIL;
8953#endif
8954
8955 return MAYBE;
8956}
8957
8958/*
8959 * window 'wp' and everything after it is messed up, mark it for redraw
8960 */
8961 static void
8962win_rest_invalid(wp)
8963 win_T *wp;
8964{
8965#ifdef FEAT_WINDOWS
8966 while (wp != NULL)
8967#else
8968 if (wp != NULL)
8969#endif
8970 {
8971 redraw_win_later(wp, NOT_VALID);
8972#ifdef FEAT_WINDOWS
8973 wp->w_redr_status = TRUE;
8974 wp = wp->w_next;
8975#endif
8976 }
8977 redraw_cmdline = TRUE;
8978}
8979
8980/*
8981 * The rest of the routines in this file perform screen manipulations. The
8982 * given operation is performed physically on the screen. The corresponding
8983 * change is also made to the internal screen image. In this way, the editor
8984 * anticipates the effect of editing changes on the appearance of the screen.
8985 * That way, when we call screenupdate a complete redraw isn't usually
8986 * necessary. Another advantage is that we can keep adding code to anticipate
8987 * screen changes, and in the meantime, everything still works.
8988 */
8989
8990/*
8991 * types for inserting or deleting lines
8992 */
8993#define USE_T_CAL 1
8994#define USE_T_CDL 2
8995#define USE_T_AL 3
8996#define USE_T_CE 4
8997#define USE_T_DL 5
8998#define USE_T_SR 6
8999#define USE_NL 7
9000#define USE_T_CD 8
9001#define USE_REDRAW 9
9002
9003/*
9004 * insert lines on the screen and update ScreenLines[]
9005 * 'end' is the line after the scrolled part. Normally it is Rows.
9006 * When scrolling region used 'off' is the offset from the top for the region.
9007 * 'row' and 'end' are relative to the start of the region.
9008 *
9009 * return FAIL for failure, OK for success.
9010 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009011 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012screen_ins_lines(off, row, line_count, end, wp)
9013 int off;
9014 int row;
9015 int line_count;
9016 int end;
9017 win_T *wp; /* NULL or window to use width from */
9018{
9019 int i;
9020 int j;
9021 unsigned temp;
9022 int cursor_row;
9023 int type;
9024 int result_empty;
9025 int can_ce = can_clear(T_CE);
9026
9027 /*
9028 * FAIL if
9029 * - there is no valid screen
9030 * - the screen has to be redrawn completely
9031 * - the line count is less than one
9032 * - the line count is more than 'ttyscroll'
9033 */
9034 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9035 return FAIL;
9036
9037 /*
9038 * There are seven ways to insert lines:
9039 * 0. When in a vertically split window and t_CV isn't set, redraw the
9040 * characters from ScreenLines[].
9041 * 1. Use T_CD (clear to end of display) if it exists and the result of
9042 * the insert is just empty lines
9043 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9044 * present or line_count > 1. It looks better if we do all the inserts
9045 * at once.
9046 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9047 * insert is just empty lines and T_CE is not present or line_count >
9048 * 1.
9049 * 4. Use T_AL (insert line) if it exists.
9050 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9051 * just empty lines.
9052 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9053 * just empty lines.
9054 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9055 * the 'da' flag is not set or we have clear line capability.
9056 * 8. redraw the characters from ScreenLines[].
9057 *
9058 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9059 * the scrollbar for the window. It does have insert line, use that if it
9060 * exists.
9061 */
9062 result_empty = (row + line_count >= end);
9063#ifdef FEAT_VERTSPLIT
9064 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9065 type = USE_REDRAW;
9066 else
9067#endif
9068 if (can_clear(T_CD) && result_empty)
9069 type = USE_T_CD;
9070 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9071 type = USE_T_CAL;
9072 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9073 type = USE_T_CDL;
9074 else if (*T_AL != NUL)
9075 type = USE_T_AL;
9076 else if (can_ce && result_empty)
9077 type = USE_T_CE;
9078 else if (*T_DL != NUL && result_empty)
9079 type = USE_T_DL;
9080 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9081 type = USE_T_SR;
9082 else
9083 return FAIL;
9084
9085 /*
9086 * For clearing the lines screen_del_lines() is used. This will also take
9087 * care of t_db if necessary.
9088 */
9089 if (type == USE_T_CD || type == USE_T_CDL ||
9090 type == USE_T_CE || type == USE_T_DL)
9091 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9092
9093 /*
9094 * If text is retained below the screen, first clear or delete as many
9095 * lines at the bottom of the window as are about to be inserted so that
9096 * the deleted lines won't later surface during a screen_del_lines.
9097 */
9098 if (*T_DB)
9099 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9100
9101#ifdef FEAT_CLIPBOARD
9102 /* Remove a modeless selection when inserting lines halfway the screen
9103 * or not the full width of the screen. */
9104 if (off + row > 0
9105# ifdef FEAT_VERTSPLIT
9106 || (wp != NULL && wp->w_width != Columns)
9107# endif
9108 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009109 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 else
9111 clip_scroll_selection(-line_count);
9112#endif
9113
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114#ifdef FEAT_GUI
9115 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9116 * scrolling is actually carried out. */
9117 gui_dont_update_cursor();
9118#endif
9119
9120 if (*T_CCS != NUL) /* cursor relative to region */
9121 cursor_row = row;
9122 else
9123 cursor_row = row + off;
9124
9125 /*
9126 * Shift LineOffset[] line_count down to reflect the inserted lines.
9127 * Clear the inserted lines in ScreenLines[].
9128 */
9129 row += off;
9130 end += off;
9131 for (i = 0; i < line_count; ++i)
9132 {
9133#ifdef FEAT_VERTSPLIT
9134 if (wp != NULL && wp->w_width != Columns)
9135 {
9136 /* need to copy part of a line */
9137 j = end - 1 - i;
9138 while ((j -= line_count) >= row)
9139 linecopy(j + line_count, j, wp);
9140 j += line_count;
9141 if (can_clear((char_u *)" "))
9142 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9143 else
9144 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9145 LineWraps[j] = FALSE;
9146 }
9147 else
9148#endif
9149 {
9150 j = end - 1 - i;
9151 temp = LineOffset[j];
9152 while ((j -= line_count) >= row)
9153 {
9154 LineOffset[j + line_count] = LineOffset[j];
9155 LineWraps[j + line_count] = LineWraps[j];
9156 }
9157 LineOffset[j + line_count] = temp;
9158 LineWraps[j + line_count] = FALSE;
9159 if (can_clear((char_u *)" "))
9160 lineclear(temp, (int)Columns);
9161 else
9162 lineinvalid(temp, (int)Columns);
9163 }
9164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165
9166 screen_stop_highlight();
9167 windgoto(cursor_row, 0);
9168
9169#ifdef FEAT_VERTSPLIT
9170 /* redraw the characters */
9171 if (type == USE_REDRAW)
9172 redraw_block(row, end, wp);
9173 else
9174#endif
9175 if (type == USE_T_CAL)
9176 {
9177 term_append_lines(line_count);
9178 screen_start(); /* don't know where cursor is now */
9179 }
9180 else
9181 {
9182 for (i = 0; i < line_count; i++)
9183 {
9184 if (type == USE_T_AL)
9185 {
9186 if (i && cursor_row != 0)
9187 windgoto(cursor_row, 0);
9188 out_str(T_AL);
9189 }
9190 else /* type == USE_T_SR */
9191 out_str(T_SR);
9192 screen_start(); /* don't know where cursor is now */
9193 }
9194 }
9195
9196 /*
9197 * With scroll-reverse and 'da' flag set we need to clear the lines that
9198 * have been scrolled down into the region.
9199 */
9200 if (type == USE_T_SR && *T_DA)
9201 {
9202 for (i = 0; i < line_count; ++i)
9203 {
9204 windgoto(off + i, 0);
9205 out_str(T_CE);
9206 screen_start(); /* don't know where cursor is now */
9207 }
9208 }
9209
9210#ifdef FEAT_GUI
9211 gui_can_update_cursor();
9212 if (gui.in_use)
9213 out_flush(); /* always flush after a scroll */
9214#endif
9215 return OK;
9216}
9217
9218/*
9219 * delete lines on the screen and update ScreenLines[]
9220 * 'end' is the line after the scrolled part. Normally it is Rows.
9221 * When scrolling region used 'off' is the offset from the top for the region.
9222 * 'row' and 'end' are relative to the start of the region.
9223 *
9224 * Return OK for success, FAIL if the lines are not deleted.
9225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 int
9227screen_del_lines(off, row, line_count, end, force, wp)
9228 int off;
9229 int row;
9230 int line_count;
9231 int end;
9232 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009233 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235 int j;
9236 int i;
9237 unsigned temp;
9238 int cursor_row;
9239 int cursor_end;
9240 int result_empty; /* result is empty until end of region */
9241 int can_delete; /* deleting line codes can be used */
9242 int type;
9243
9244 /*
9245 * FAIL if
9246 * - there is no valid screen
9247 * - the screen has to be redrawn completely
9248 * - the line count is less than one
9249 * - the line count is more than 'ttyscroll'
9250 */
9251 if (!screen_valid(TRUE) || line_count <= 0 ||
9252 (!force && line_count > p_ttyscroll))
9253 return FAIL;
9254
9255 /*
9256 * Check if the rest of the current region will become empty.
9257 */
9258 result_empty = row + line_count >= end;
9259
9260 /*
9261 * We can delete lines only when 'db' flag not set or when 'ce' option
9262 * available.
9263 */
9264 can_delete = (*T_DB == NUL || can_clear(T_CE));
9265
9266 /*
9267 * There are six ways to delete lines:
9268 * 0. When in a vertically split window and t_CV isn't set, redraw the
9269 * characters from ScreenLines[].
9270 * 1. Use T_CD if it exists and the result is empty.
9271 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9272 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9273 * none of the other ways work.
9274 * 4. Use T_CE (erase line) if the result is empty.
9275 * 5. Use T_DL (delete line) if it exists.
9276 * 6. redraw the characters from ScreenLines[].
9277 */
9278#ifdef FEAT_VERTSPLIT
9279 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9280 type = USE_REDRAW;
9281 else
9282#endif
9283 if (can_clear(T_CD) && result_empty)
9284 type = USE_T_CD;
9285#if defined(__BEOS__) && defined(BEOS_DR8)
9286 /*
9287 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9288 * its internal termcap... this works okay for tests which test *T_DB !=
9289 * NUL. It has the disadvantage that the user cannot use any :set t_*
9290 * command to get T_DB (back) to empty_option, only :set term=... will do
9291 * the trick...
9292 * Anyway, this hack will hopefully go away with the next OS release.
9293 * (Olaf Seibert)
9294 */
9295 else if (row == 0 && T_DB == empty_option
9296 && (line_count == 1 || *T_CDL == NUL))
9297#else
9298 else if (row == 0 && (
9299#ifndef AMIGA
9300 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9301 * up, so use delete-line command */
9302 line_count == 1 ||
9303#endif
9304 *T_CDL == NUL))
9305#endif
9306 type = USE_NL;
9307 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9308 type = USE_T_CDL;
9309 else if (can_clear(T_CE) && result_empty
9310#ifdef FEAT_VERTSPLIT
9311 && (wp == NULL || wp->w_width == Columns)
9312#endif
9313 )
9314 type = USE_T_CE;
9315 else if (*T_DL != NUL && can_delete)
9316 type = USE_T_DL;
9317 else if (*T_CDL != NUL && can_delete)
9318 type = USE_T_CDL;
9319 else
9320 return FAIL;
9321
9322#ifdef FEAT_CLIPBOARD
9323 /* Remove a modeless selection when deleting lines halfway the screen or
9324 * not the full width of the screen. */
9325 if (off + row > 0
9326# ifdef FEAT_VERTSPLIT
9327 || (wp != NULL && wp->w_width != Columns)
9328# endif
9329 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009330 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 else
9332 clip_scroll_selection(line_count);
9333#endif
9334
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335#ifdef FEAT_GUI
9336 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9337 * scrolling is actually carried out. */
9338 gui_dont_update_cursor();
9339#endif
9340
9341 if (*T_CCS != NUL) /* cursor relative to region */
9342 {
9343 cursor_row = row;
9344 cursor_end = end;
9345 }
9346 else
9347 {
9348 cursor_row = row + off;
9349 cursor_end = end + off;
9350 }
9351
9352 /*
9353 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9354 * Clear the inserted lines in ScreenLines[].
9355 */
9356 row += off;
9357 end += off;
9358 for (i = 0; i < line_count; ++i)
9359 {
9360#ifdef FEAT_VERTSPLIT
9361 if (wp != NULL && wp->w_width != Columns)
9362 {
9363 /* need to copy part of a line */
9364 j = row + i;
9365 while ((j += line_count) <= end - 1)
9366 linecopy(j - line_count, j, wp);
9367 j -= line_count;
9368 if (can_clear((char_u *)" "))
9369 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9370 else
9371 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9372 LineWraps[j] = FALSE;
9373 }
9374 else
9375#endif
9376 {
9377 /* whole width, moving the line pointers is faster */
9378 j = row + i;
9379 temp = LineOffset[j];
9380 while ((j += line_count) <= end - 1)
9381 {
9382 LineOffset[j - line_count] = LineOffset[j];
9383 LineWraps[j - line_count] = LineWraps[j];
9384 }
9385 LineOffset[j - line_count] = temp;
9386 LineWraps[j - line_count] = FALSE;
9387 if (can_clear((char_u *)" "))
9388 lineclear(temp, (int)Columns);
9389 else
9390 lineinvalid(temp, (int)Columns);
9391 }
9392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393
9394 screen_stop_highlight();
9395
9396#ifdef FEAT_VERTSPLIT
9397 /* redraw the characters */
9398 if (type == USE_REDRAW)
9399 redraw_block(row, end, wp);
9400 else
9401#endif
9402 if (type == USE_T_CD) /* delete the lines */
9403 {
9404 windgoto(cursor_row, 0);
9405 out_str(T_CD);
9406 screen_start(); /* don't know where cursor is now */
9407 }
9408 else if (type == USE_T_CDL)
9409 {
9410 windgoto(cursor_row, 0);
9411 term_delete_lines(line_count);
9412 screen_start(); /* don't know where cursor is now */
9413 }
9414 /*
9415 * Deleting lines at top of the screen or scroll region: Just scroll
9416 * the whole screen (scroll region) up by outputting newlines on the
9417 * last line.
9418 */
9419 else if (type == USE_NL)
9420 {
9421 windgoto(cursor_end - 1, 0);
9422 for (i = line_count; --i >= 0; )
9423 out_char('\n'); /* cursor will remain on same line */
9424 }
9425 else
9426 {
9427 for (i = line_count; --i >= 0; )
9428 {
9429 if (type == USE_T_DL)
9430 {
9431 windgoto(cursor_row, 0);
9432 out_str(T_DL); /* delete a line */
9433 }
9434 else /* type == USE_T_CE */
9435 {
9436 windgoto(cursor_row + i, 0);
9437 out_str(T_CE); /* erase a line */
9438 }
9439 screen_start(); /* don't know where cursor is now */
9440 }
9441 }
9442
9443 /*
9444 * If the 'db' flag is set, we need to clear the lines that have been
9445 * scrolled up at the bottom of the region.
9446 */
9447 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9448 {
9449 for (i = line_count; i > 0; --i)
9450 {
9451 windgoto(cursor_end - i, 0);
9452 out_str(T_CE); /* erase a line */
9453 screen_start(); /* don't know where cursor is now */
9454 }
9455 }
9456
9457#ifdef FEAT_GUI
9458 gui_can_update_cursor();
9459 if (gui.in_use)
9460 out_flush(); /* always flush after a scroll */
9461#endif
9462
9463 return OK;
9464}
9465
9466/*
9467 * show the current mode and ruler
9468 *
9469 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9470 * If clear_cmdline is FALSE there may be a message there that needs to be
9471 * cleared only if a mode is shown.
9472 * Return the length of the message (0 if no message).
9473 */
9474 int
9475showmode()
9476{
9477 int need_clear;
9478 int length = 0;
9479 int do_mode;
9480 int attr;
9481 int nwr_save;
9482#ifdef FEAT_INS_EXPAND
9483 int sub_attr;
9484#endif
9485
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009486 do_mode = ((p_smd && msg_silent == 0)
9487 && ((State & INSERT)
9488 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489#ifdef FEAT_VISUAL
9490 || VIsual_active
9491#endif
9492 ));
9493 if (do_mode || Recording)
9494 {
9495 /*
9496 * Don't show mode right now, when not redrawing or inside a mapping.
9497 * Call char_avail() only when we are going to show something, because
9498 * it takes a bit of time.
9499 */
9500 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9501 {
9502 redraw_cmdline = TRUE; /* show mode later */
9503 return 0;
9504 }
9505
9506 nwr_save = need_wait_return;
9507
9508 /* wait a bit before overwriting an important message */
9509 check_for_delay(FALSE);
9510
9511 /* if the cmdline is more than one line high, erase top lines */
9512 need_clear = clear_cmdline;
9513 if (clear_cmdline && cmdline_row < Rows - 1)
9514 msg_clr_cmdline(); /* will reset clear_cmdline */
9515
9516 /* Position on the last line in the window, column 0 */
9517 msg_pos_mode();
9518 cursor_off();
9519 attr = hl_attr(HLF_CM); /* Highlight mode */
9520 if (do_mode)
9521 {
9522 MSG_PUTS_ATTR("--", attr);
9523#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009524 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009525# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009526 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009527# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009528 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009529# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009530 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009531# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 MSG_PUTS_ATTR(" IM", attr);
9533# else
9534 MSG_PUTS_ATTR(" XIM", attr);
9535# endif
9536#endif
9537#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9538 if (gui.in_use)
9539 {
9540 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009541 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 }
9543#endif
9544#ifdef FEAT_INS_EXPAND
9545 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9546 {
9547 /* These messages can get long, avoid a wrap in a narrow
9548 * window. Prefer showing edit_submode_extra. */
9549 length = (Rows - msg_row) * Columns - 3;
9550 if (edit_submode_extra != NULL)
9551 length -= vim_strsize(edit_submode_extra);
9552 if (length > 0)
9553 {
9554 if (edit_submode_pre != NULL)
9555 length -= vim_strsize(edit_submode_pre);
9556 if (length - vim_strsize(edit_submode) > 0)
9557 {
9558 if (edit_submode_pre != NULL)
9559 msg_puts_attr(edit_submode_pre, attr);
9560 msg_puts_attr(edit_submode, attr);
9561 }
9562 if (edit_submode_extra != NULL)
9563 {
9564 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9565 if ((int)edit_submode_highl < (int)HLF_COUNT)
9566 sub_attr = hl_attr(edit_submode_highl);
9567 else
9568 sub_attr = attr;
9569 msg_puts_attr(edit_submode_extra, sub_attr);
9570 }
9571 }
9572 length = 0;
9573 }
9574 else
9575#endif
9576 {
9577#ifdef FEAT_VREPLACE
9578 if (State & VREPLACE_FLAG)
9579 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9580 else
9581#endif
9582 if (State & REPLACE_FLAG)
9583 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9584 else if (State & INSERT)
9585 {
9586#ifdef FEAT_RIGHTLEFT
9587 if (p_ri)
9588 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9589#endif
9590 MSG_PUTS_ATTR(_(" INSERT"), attr);
9591 }
9592 else if (restart_edit == 'I')
9593 MSG_PUTS_ATTR(_(" (insert)"), attr);
9594 else if (restart_edit == 'R')
9595 MSG_PUTS_ATTR(_(" (replace)"), attr);
9596 else if (restart_edit == 'V')
9597 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9598#ifdef FEAT_RIGHTLEFT
9599 if (p_hkmap)
9600 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9601# ifdef FEAT_FKMAP
9602 if (p_fkmap)
9603 MSG_PUTS_ATTR(farsi_text_5, attr);
9604# endif
9605#endif
9606#ifdef FEAT_KEYMAP
9607 if (State & LANGMAP)
9608 {
9609# ifdef FEAT_ARABIC
9610 if (curwin->w_p_arab)
9611 MSG_PUTS_ATTR(_(" Arabic"), attr);
9612 else
9613# endif
9614 MSG_PUTS_ATTR(_(" (lang)"), attr);
9615 }
9616#endif
9617 if ((State & INSERT) && p_paste)
9618 MSG_PUTS_ATTR(_(" (paste)"), attr);
9619
9620#ifdef FEAT_VISUAL
9621 if (VIsual_active)
9622 {
9623 char *p;
9624
9625 /* Don't concatenate separate words to avoid translation
9626 * problems. */
9627 switch ((VIsual_select ? 4 : 0)
9628 + (VIsual_mode == Ctrl_V) * 2
9629 + (VIsual_mode == 'V'))
9630 {
9631 case 0: p = N_(" VISUAL"); break;
9632 case 1: p = N_(" VISUAL LINE"); break;
9633 case 2: p = N_(" VISUAL BLOCK"); break;
9634 case 4: p = N_(" SELECT"); break;
9635 case 5: p = N_(" SELECT LINE"); break;
9636 default: p = N_(" SELECT BLOCK"); break;
9637 }
9638 MSG_PUTS_ATTR(_(p), attr);
9639 }
9640#endif
9641 MSG_PUTS_ATTR(" --", attr);
9642 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009643
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 need_clear = TRUE;
9645 }
9646 if (Recording
9647#ifdef FEAT_INS_EXPAND
9648 && edit_submode == NULL /* otherwise it gets too long */
9649#endif
9650 )
9651 {
9652 MSG_PUTS_ATTR(_("recording"), attr);
9653 need_clear = TRUE;
9654 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009655
9656 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 if (need_clear || clear_cmdline)
9658 msg_clr_eos();
9659 msg_didout = FALSE; /* overwrite this message */
9660 length = msg_col;
9661 msg_col = 0;
9662 need_wait_return = nwr_save; /* never ask for hit-return for this */
9663 }
9664 else if (clear_cmdline && msg_silent == 0)
9665 /* Clear the whole command line. Will reset "clear_cmdline". */
9666 msg_clr_cmdline();
9667
9668#ifdef FEAT_CMDL_INFO
9669# ifdef FEAT_VISUAL
9670 /* In Visual mode the size of the selected area must be redrawn. */
9671 if (VIsual_active)
9672 clear_showcmd();
9673# endif
9674
9675 /* If the last window has no status line, the ruler is after the mode
9676 * message and must be redrawn */
9677 if (redrawing()
9678# ifdef FEAT_WINDOWS
9679 && lastwin->w_status_height == 0
9680# endif
9681 )
9682 win_redr_ruler(lastwin, TRUE);
9683#endif
9684 redraw_cmdline = FALSE;
9685 clear_cmdline = FALSE;
9686
9687 return length;
9688}
9689
9690/*
9691 * Position for a mode message.
9692 */
9693 static void
9694msg_pos_mode()
9695{
9696 msg_col = 0;
9697 msg_row = Rows - 1;
9698}
9699
9700/*
9701 * Delete mode message. Used when ESC is typed which is expected to end
9702 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009703 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 */
9705 void
9706unshowmode(force)
9707 int force;
9708{
9709 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009710 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 */
9712 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9713 redraw_cmdline = TRUE; /* delete mode later */
9714 else
9715 {
9716 msg_pos_mode();
9717 if (Recording)
9718 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9719 msg_clr_eos();
9720 }
9721}
9722
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009723#if defined(FEAT_WINDOWS)
9724/*
9725 * Draw the tab pages line at the top of the Vim window.
9726 */
9727 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009728draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009729{
9730 int tabcount = 0;
9731 tabpage_T *tp;
9732 int tabwidth;
9733 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009734 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009735 int attr;
9736 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009737 win_T *cwp;
9738 int wincount;
9739 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009740 int c;
9741 int len;
9742 int attr_sel = hl_attr(HLF_TPS);
9743 int attr_nosel = hl_attr(HLF_TP);
9744 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009745 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009746 int room;
9747 int use_sep_chars = (t_colors < 8
9748#ifdef FEAT_GUI
9749 && !gui.in_use
9750#endif
9751 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009752
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009753 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009754
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009755#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009756 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009757 if (gui_use_tabline())
9758 {
9759 gui_update_tabline();
9760 return;
9761 }
9762#endif
9763
9764 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009765 return;
9766
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009767#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009768
9769 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9770 for (scol = 0; scol < Columns; ++scol)
9771 TabPageIdxs[scol] = 0;
9772
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009773 /* Use the 'tabline' option if it's set. */
9774 if (*p_tal != NUL)
9775 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009776 int save_called_emsg = called_emsg;
9777
9778 /* Check for an error. If there is one we would loop in redrawing the
9779 * screen. Avoid that by making 'tabline' empty. */
9780 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009781 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009782 if (called_emsg)
9783 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009784 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009785 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009786 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009787 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009788#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009789 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009790 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9791 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009792
Bram Moolenaar238a5642006-02-21 22:12:05 +00009793 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9794 if (tabwidth < 6)
9795 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009796
Bram Moolenaar238a5642006-02-21 22:12:05 +00009797 attr = attr_nosel;
9798 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009799 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009800 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9801 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009802 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009803 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009804
Bram Moolenaar238a5642006-02-21 22:12:05 +00009805 if (tp->tp_topframe == topframe)
9806 attr = attr_sel;
9807 if (use_sep_chars && col > 0)
9808 screen_putchar('|', 0, col++, attr);
9809
9810 if (tp->tp_topframe != topframe)
9811 attr = attr_nosel;
9812
9813 screen_putchar(' ', 0, col++, attr);
9814
9815 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009816 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009817 cwp = curwin;
9818 wp = firstwin;
9819 }
9820 else
9821 {
9822 cwp = tp->tp_curwin;
9823 wp = tp->tp_firstwin;
9824 }
9825
9826 modified = FALSE;
9827 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9828 if (bufIsChanged(wp->w_buffer))
9829 modified = TRUE;
9830 if (modified || wincount > 1)
9831 {
9832 if (wincount > 1)
9833 {
9834 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009835 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009836 if (col + len >= Columns - 3)
9837 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009838 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009839#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009840 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009841#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009842 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009843#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009844 );
9845 col += len;
9846 }
9847 if (modified)
9848 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9849 screen_putchar(' ', 0, col++, attr);
9850 }
9851
9852 room = scol - col + tabwidth - 1;
9853 if (room > 0)
9854 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009855 /* Get buffer name in NameBuff[] */
9856 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009857 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009858 len = vim_strsize(NameBuff);
9859 p = NameBuff;
9860#ifdef FEAT_MBYTE
9861 if (has_mbyte)
9862 while (len > room)
9863 {
9864 len -= ptr2cells(p);
9865 mb_ptr_adv(p);
9866 }
9867 else
9868#endif
9869 if (len > room)
9870 {
9871 p += len - room;
9872 len = room;
9873 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009874 if (len > Columns - col - 1)
9875 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009876
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009877 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009878 col += len;
9879 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009880 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009881
9882 /* Store the tab page number in TabPageIdxs[], so that
9883 * jump_to_mouse() knows where each one is. */
9884 ++tabcount;
9885 while (scol < col)
9886 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009887 }
9888
Bram Moolenaar238a5642006-02-21 22:12:05 +00009889 if (use_sep_chars)
9890 c = '_';
9891 else
9892 c = ' ';
9893 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009894
9895 /* Put an "X" for closing the current tab if there are several. */
9896 if (first_tabpage->tp_next != NULL)
9897 {
9898 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9899 TabPageIdxs[Columns - 1] = -999;
9900 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009901 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009902
9903 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9904 * set. */
9905 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009906}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009907
9908/*
9909 * Get buffer name for "buf" into NameBuff[].
9910 * Takes care of special buffer names and translates special characters.
9911 */
9912 void
9913get_trans_bufname(buf)
9914 buf_T *buf;
9915{
9916 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02009917 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009918 else
9919 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9920 trans_characters(NameBuff, MAXPATHL);
9921}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009922#endif
9923
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9925/*
9926 * Get the character to use in a status line. Get its attributes in "*attr".
9927 */
9928 static int
9929fillchar_status(attr, is_curwin)
9930 int *attr;
9931 int is_curwin;
9932{
9933 int fill;
9934 if (is_curwin)
9935 {
9936 *attr = hl_attr(HLF_S);
9937 fill = fill_stl;
9938 }
9939 else
9940 {
9941 *attr = hl_attr(HLF_SNC);
9942 fill = fill_stlnc;
9943 }
9944 /* Use fill when there is highlighting, and highlighting of current
9945 * window differs, or the fillchars differ, or this is not the
9946 * current window */
9947 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9948 || !is_curwin || firstwin == lastwin)
9949 || (fill_stl != fill_stlnc)))
9950 return fill;
9951 if (is_curwin)
9952 return '^';
9953 return '=';
9954}
9955#endif
9956
9957#ifdef FEAT_VERTSPLIT
9958/*
9959 * Get the character to use in a separator between vertically split windows.
9960 * Get its attributes in "*attr".
9961 */
9962 static int
9963fillchar_vsep(attr)
9964 int *attr;
9965{
9966 *attr = hl_attr(HLF_C);
9967 if (*attr == 0 && fill_vert == ' ')
9968 return '|';
9969 else
9970 return fill_vert;
9971}
9972#endif
9973
9974/*
9975 * Return TRUE if redrawing should currently be done.
9976 */
9977 int
9978redrawing()
9979{
9980 return (!RedrawingDisabled
9981 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9982}
9983
9984/*
9985 * Return TRUE if printing messages should currently be done.
9986 */
9987 int
9988messaging()
9989{
9990 return (!(p_lz && char_avail() && !KeyTyped));
9991}
9992
9993/*
9994 * Show current status info in ruler and various other places
9995 * If always is FALSE, only show ruler if position has changed.
9996 */
9997 void
9998showruler(always)
9999 int always;
10000{
10001 if (!always && !redrawing())
10002 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010003#ifdef FEAT_INS_EXPAND
10004 if (pum_visible())
10005 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010006# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010007 /* Don't redraw right now, do it later. */
10008 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010009# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010010 return;
10011 }
10012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010014 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010015 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010016 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 else
10019#endif
10020#ifdef FEAT_CMDL_INFO
10021 win_redr_ruler(curwin, always);
10022#endif
10023
10024#ifdef FEAT_TITLE
10025 if (need_maketitle
10026# ifdef FEAT_STL_OPT
10027 || (p_icon && (stl_syntax & STL_IN_ICON))
10028 || (p_title && (stl_syntax & STL_IN_TITLE))
10029# endif
10030 )
10031 maketitle();
10032#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010033#ifdef FEAT_WINDOWS
10034 /* Redraw the tab pages line if needed. */
10035 if (redraw_tabline)
10036 draw_tabline();
10037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038}
10039
10040#ifdef FEAT_CMDL_INFO
10041 static void
10042win_redr_ruler(wp, always)
10043 win_T *wp;
10044 int always;
10045{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010046#define RULER_BUF_LEN 70
10047 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 int row;
10049 int fillchar;
10050 int attr;
10051 int empty_line = FALSE;
10052 colnr_T virtcol;
10053 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010054 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 int o;
10056#ifdef FEAT_VERTSPLIT
10057 int this_ru_col;
10058 int off = 0;
10059 int width = Columns;
10060# define WITH_OFF(x) x
10061# define WITH_WIDTH(x) x
10062#else
10063# define WITH_OFF(x) 0
10064# define WITH_WIDTH(x) Columns
10065# define this_ru_col ru_col
10066#endif
10067
10068 /* If 'ruler' off or redrawing disabled, don't do anything */
10069 if (!p_ru)
10070 return;
10071
10072 /*
10073 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10074 * after deleting lines, before cursor.lnum is corrected.
10075 */
10076 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10077 return;
10078
10079#ifdef FEAT_INS_EXPAND
10080 /* Don't draw the ruler while doing insert-completion, it might overwrite
10081 * the (long) mode message. */
10082# ifdef FEAT_WINDOWS
10083 if (wp == lastwin && lastwin->w_status_height == 0)
10084# endif
10085 if (edit_submode != NULL)
10086 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010087 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10088 if (pum_visible())
10089 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090#endif
10091
10092#ifdef FEAT_STL_OPT
10093 if (*p_ruf)
10094 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010095 int save_called_emsg = called_emsg;
10096
10097 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010099 if (called_emsg)
10100 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010101 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010102 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 return;
10104 }
10105#endif
10106
10107 /*
10108 * Check if not in Insert mode and the line is empty (will show "0-1").
10109 */
10110 if (!(State & INSERT)
10111 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10112 empty_line = TRUE;
10113
10114 /*
10115 * Only draw the ruler when something changed.
10116 */
10117 validate_virtcol_win(wp);
10118 if ( redraw_cmdline
10119 || always
10120 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10121 || wp->w_cursor.col != wp->w_ru_cursor.col
10122 || wp->w_virtcol != wp->w_ru_virtcol
10123#ifdef FEAT_VIRTUALEDIT
10124 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10125#endif
10126 || wp->w_topline != wp->w_ru_topline
10127 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10128#ifdef FEAT_DIFF
10129 || wp->w_topfill != wp->w_ru_topfill
10130#endif
10131 || empty_line != wp->w_ru_empty)
10132 {
10133 cursor_off();
10134#ifdef FEAT_WINDOWS
10135 if (wp->w_status_height)
10136 {
10137 row = W_WINROW(wp) + wp->w_height;
10138 fillchar = fillchar_status(&attr, wp == curwin);
10139# ifdef FEAT_VERTSPLIT
10140 off = W_WINCOL(wp);
10141 width = W_WIDTH(wp);
10142# endif
10143 }
10144 else
10145#endif
10146 {
10147 row = Rows - 1;
10148 fillchar = ' ';
10149 attr = 0;
10150#ifdef FEAT_VERTSPLIT
10151 width = Columns;
10152 off = 0;
10153#endif
10154 }
10155
10156 /* In list mode virtcol needs to be recomputed */
10157 virtcol = wp->w_virtcol;
10158 if (wp->w_p_list && lcs_tab1 == NUL)
10159 {
10160 wp->w_p_list = FALSE;
10161 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10162 wp->w_p_list = TRUE;
10163 }
10164
10165 /*
10166 * Some sprintfs return the length, some return a pointer.
10167 * To avoid portability problems we use strlen() here.
10168 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010169 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10171 ? 0L
10172 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010173 len = STRLEN(buffer);
10174 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10176 (int)virtcol + 1);
10177
10178 /*
10179 * Add a "50%" if there is room for it.
10180 * On the last line, don't print in the last column (scrolls the
10181 * screen up on some terminals).
10182 */
10183 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010184 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 o = i + vim_strsize(buffer + i + 1);
10186#ifdef FEAT_WINDOWS
10187 if (wp->w_status_height == 0) /* can't use last char of screen */
10188#endif
10189 ++o;
10190#ifdef FEAT_VERTSPLIT
10191 this_ru_col = ru_col - (Columns - width);
10192 if (this_ru_col < 0)
10193 this_ru_col = 0;
10194#endif
10195 /* Never use more than half the window/screen width, leave the other
10196 * half for the filename. */
10197 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10198 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10199 if (this_ru_col + o < WITH_WIDTH(width))
10200 {
10201 while (this_ru_col + o < WITH_WIDTH(width))
10202 {
10203#ifdef FEAT_MBYTE
10204 if (has_mbyte)
10205 i += (*mb_char2bytes)(fillchar, buffer + i);
10206 else
10207#endif
10208 buffer[i++] = fillchar;
10209 ++o;
10210 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010211 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 }
10213 /* Truncate at window boundary. */
10214#ifdef FEAT_MBYTE
10215 if (has_mbyte)
10216 {
10217 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010218 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 {
10220 o += (*mb_ptr2cells)(buffer + i);
10221 if (this_ru_col + o > WITH_WIDTH(width))
10222 {
10223 buffer[i] = NUL;
10224 break;
10225 }
10226 }
10227 }
10228 else
10229#endif
10230 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10231 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10232
10233 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10234 i = redraw_cmdline;
10235 screen_fill(row, row + 1,
10236 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10237 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10238 fillchar, fillchar, attr);
10239 /* don't redraw the cmdline because of showing the ruler */
10240 redraw_cmdline = i;
10241 wp->w_ru_cursor = wp->w_cursor;
10242 wp->w_ru_virtcol = wp->w_virtcol;
10243 wp->w_ru_empty = empty_line;
10244 wp->w_ru_topline = wp->w_topline;
10245 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10246#ifdef FEAT_DIFF
10247 wp->w_ru_topfill = wp->w_topfill;
10248#endif
10249 }
10250}
10251#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010252
10253#if defined(FEAT_LINEBREAK) || defined(PROTO)
10254/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010255 * Return the width of the 'number' and 'relativenumber' column.
10256 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010257 * Otherwise it depends on 'numberwidth' and the line count.
10258 */
10259 int
10260number_width(wp)
10261 win_T *wp;
10262{
10263 int n;
10264 linenr_T lnum;
10265
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010266 if (wp->w_p_rnu && !wp->w_p_nu)
10267 /* cursor line shows "0" */
10268 lnum = wp->w_height;
10269 else
10270 /* cursor line shows absolute line number */
10271 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010272
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010273 if (lnum == wp->w_nrwidth_line_count)
10274 return wp->w_nrwidth_width;
10275 wp->w_nrwidth_line_count = lnum;
10276
10277 n = 0;
10278 do
10279 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010280 lnum /= 10;
10281 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010282 } while (lnum > 0);
10283
10284 /* 'numberwidth' gives the minimal width plus one */
10285 if (n < wp->w_p_nuw - 1)
10286 n = wp->w_p_nuw - 1;
10287
10288 wp->w_nrwidth_width = n;
10289 return n;
10290}
10291#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010292
10293/*
10294 * Return the current cursor column. This is the actual position on the
10295 * screen. First column is 0.
10296 */
10297 int
10298screen_screencol()
10299{
10300 return screen_cur_col;
10301}
10302
10303/*
10304 * Return the current cursor row. This is the actual position on the screen.
10305 * First row is 0.
10306 */
10307 int
10308screen_screenrow()
10309{
10310 return screen_cur_row;
10311}