blob: 7e11fa4d8cadc0e437196c828bcaf4e4156486a1 [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
551
552#ifdef FEAT_WINDOWS
553 /* Reset b_mod_set flags. Going through all windows is probably faster
554 * than going through all buffers (there could be many buffers). */
555 for (wp = firstwin; wp != NULL; wp = wp->w_next)
556 wp->w_buffer->b_mod_set = FALSE;
557#else
558 curbuf->b_mod_set = FALSE;
559#endif
560
561 updating_screen = FALSE;
562#ifdef FEAT_GUI
563 gui_may_resize_shell();
564#endif
565
566 /* Clear or redraw the command line. Done last, because scrolling may
567 * mess up the command line. */
568 if (clear_cmdline || redraw_cmdline)
569 showmode();
570
571 /* May put up an introductory message when not editing a file */
572 if (!did_intro && bufempty()
573 && curbuf->b_fname == NULL
574#ifdef FEAT_WINDOWS
575 && firstwin->w_next == NULL
576#endif
577 && vim_strchr(p_shm, SHM_INTRO) == NULL)
578 intro_message(FALSE);
579 did_intro = TRUE;
580
581#ifdef FEAT_GUI
582 /* Redraw the cursor and update the scrollbars when all screen updating is
583 * done. */
584 if (gui.in_use)
585 {
586 out_flush(); /* required before updating the cursor */
587 if (did_one)
588 gui_update_cursor(FALSE, FALSE);
589 gui_update_scrollbars(FALSE);
590 }
591#endif
592}
593
Bram Moolenaar860cae12010-06-05 23:22:07 +0200594#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200595/*
596 * Return TRUE if the cursor line in window "wp" may be concealed, according
597 * to the 'concealcursor' option.
598 */
599 int
600conceal_cursor_line(wp)
601 win_T *wp;
602{
603 int c;
604
605 if (*wp->w_p_cocu == NUL)
606 return FALSE;
607 if (get_real_state() & VISUAL)
608 c = 'v';
609 else if (State & INSERT)
610 c = 'i';
611 else if (State & NORMAL)
612 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200613 else if (State & CMDLINE)
614 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200615 else
616 return FALSE;
617 return vim_strchr(wp->w_p_cocu, c) != NULL;
618}
619
620/*
621 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
622 */
623 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200624conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200625{
626 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
627 {
628 need_cursor_line_redraw = TRUE;
629 /* Need to recompute cursor column, e.g., when starting Visual mode
630 * without concealing. */
631 curs_columns(TRUE);
632 }
633}
634
Bram Moolenaar860cae12010-06-05 23:22:07 +0200635 void
636update_single_line(wp, lnum)
637 win_T *wp;
638 linenr_T lnum;
639{
640 int row;
641 int j;
642
643 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200644 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200645 {
646# ifdef FEAT_GUI
647 /* Remove the cursor before starting to do anything, because scrolling
648 * may make it difficult to redraw the text under it. */
649 if (gui.in_use)
650 gui_undraw_cursor();
651# endif
652 row = 0;
653 for (j = 0; j < wp->w_lines_valid; ++j)
654 {
655 if (lnum == wp->w_lines[j].wl_lnum)
656 {
657 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200658# ifdef FEAT_SEARCH_EXTRA
659 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200660 start_search_hl();
661 prepare_search_hl(wp, lnum);
662# endif
663 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
664# if defined(FEAT_SEARCH_EXTRA)
665 end_search_hl();
666# endif
667 break;
668 }
669 row += wp->w_lines[j].wl_size;
670 }
671# ifdef FEAT_GUI
672 /* Redraw the cursor */
673 if (gui.in_use)
674 {
675 out_flush(); /* required before updating the cursor */
676 gui_update_cursor(FALSE, FALSE);
677 }
678# endif
679 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200680 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200681}
682#endif
683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
685static void update_prepare __ARGS((void));
686static void update_finish __ARGS((void));
687
688/*
689 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000690 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 */
692 static void
693update_prepare()
694{
695 cursor_off();
696 updating_screen = TRUE;
697#ifdef FEAT_GUI
698 /* Remove the cursor before starting to do anything, because scrolling may
699 * make it difficult to redraw the text under it. */
700 if (gui.in_use)
701 gui_undraw_cursor();
702#endif
703#ifdef FEAT_SEARCH_EXTRA
704 start_search_hl();
705#endif
706}
707
708/*
709 * Finish updating one or more windows.
710 */
711 static void
712update_finish()
713{
714 if (redraw_cmdline)
715 showmode();
716
717# ifdef FEAT_SEARCH_EXTRA
718 end_search_hl();
719# endif
720
721 updating_screen = FALSE;
722
723# ifdef FEAT_GUI
724 gui_may_resize_shell();
725
726 /* Redraw the cursor and update the scrollbars when all screen updating is
727 * done. */
728 if (gui.in_use)
729 {
730 out_flush(); /* required before updating the cursor */
731 gui_update_cursor(FALSE, FALSE);
732 gui_update_scrollbars(FALSE);
733 }
734# endif
735}
736#endif
737
738#if defined(FEAT_SIGNS) || defined(PROTO)
739 void
740update_debug_sign(buf, lnum)
741 buf_T *buf;
742 linenr_T lnum;
743{
744 win_T *wp;
745 int doit = FALSE;
746
747# ifdef FEAT_FOLDING
748 win_foldinfo.fi_level = 0;
749# endif
750
751 /* update/delete a specific mark */
752 FOR_ALL_WINDOWS(wp)
753 {
754 if (buf != NULL && lnum > 0)
755 {
756 if (wp->w_buffer == buf && lnum >= wp->w_topline
757 && lnum < wp->w_botline)
758 {
759 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
760 wp->w_redraw_top = lnum;
761 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
762 wp->w_redraw_bot = lnum;
763 redraw_win_later(wp, VALID);
764 }
765 }
766 else
767 redraw_win_later(wp, VALID);
768 if (wp->w_redr_type != 0)
769 doit = TRUE;
770 }
771
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100772 /* Return when there is nothing to do, screen updating is already
773 * happening (recursive call) or still starting up. */
774 if (!doit || updating_screen
775#ifdef FEAT_GUI
776 || gui.starting
777#endif
778 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 return;
780
781 /* update all windows that need updating */
782 update_prepare();
783
784# ifdef FEAT_WINDOWS
785 for (wp = firstwin; wp; wp = wp->w_next)
786 {
787 if (wp->w_redr_type != 0)
788 win_update(wp);
789 if (wp->w_redr_status)
790 win_redr_status(wp);
791 }
792# else
793 if (curwin->w_redr_type != 0)
794 win_update(curwin);
795# endif
796
797 update_finish();
798}
799#endif
800
801
802#if defined(FEAT_GUI) || defined(PROTO)
803/*
804 * Update a single window, its status line and maybe the command line msg.
805 * Used for the GUI scrollbar.
806 */
807 void
808updateWindow(wp)
809 win_T *wp;
810{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000811 /* return if already busy updating */
812 if (updating_screen)
813 return;
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 update_prepare();
816
817#ifdef FEAT_CLIPBOARD
818 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200819 if (clip_star.available && clip_isautosel_star())
820 clip_update_selection(&clip_star);
821 if (clip_plus.available && clip_isautosel_plus())
822 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000828 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000829 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000830 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (wp->w_redr_status
833# ifdef FEAT_CMDL_INFO
834 || p_ru
835# endif
836# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000837 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838# endif
839 )
840 win_redr_status(wp);
841#endif
842
843 update_finish();
844}
845#endif
846
847/*
848 * Update a single window.
849 *
850 * This may cause the windows below it also to be redrawn (when clearing the
851 * screen or scrolling lines).
852 *
853 * How the window is redrawn depends on wp->w_redr_type. Each type also
854 * implies the one below it.
855 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000856 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
858 * INVERTED redraw the changed part of the Visual area
859 * INVERTED_ALL redraw the whole Visual area
860 * VALID 1. scroll up/down to adjust for a changed w_topline
861 * 2. update lines at the top when scrolled down
862 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000863 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * b_mod_top and b_mod_bot.
865 * - if wp->w_redraw_top non-zero, redraw lines between
866 * wp->w_redraw_top and wp->w_redr_bot.
867 * - continue redrawing when syntax status is invalid.
868 * 4. if scrolled up, update lines at the bottom.
869 * This results in three areas that may need updating:
870 * top: from first row to top_end (when scrolled down)
871 * mid: from mid_start to mid_end (update inversion or changed text)
872 * bot: from bot_start to last row (when scrolled up)
873 */
874 static void
875win_update(wp)
876 win_T *wp;
877{
878 buf_T *buf = wp->w_buffer;
879 int type;
880 int top_end = 0; /* Below last row of the top area that needs
881 updating. 0 when no top area updating. */
882 int mid_start = 999;/* first row of the mid area that needs
883 updating. 999 when no mid area updating. */
884 int mid_end = 0; /* Below last row of the mid area that needs
885 updating. 0 when no mid area updating. */
886 int bot_start = 999;/* first row of the bot area that needs
887 updating. 999 when no bot area updating */
888#ifdef FEAT_VISUAL
889 int scrolled_down = FALSE; /* TRUE when scrolled down when
890 w_topline got smaller a bit */
891#endif
892#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000893 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 int top_to_mod = FALSE; /* redraw above mod_top */
895#endif
896
897 int row; /* current window row to display */
898 linenr_T lnum; /* current buffer lnum to display */
899 int idx; /* current index in w_lines[] */
900 int srow; /* starting row of the current line */
901
902 int eof = FALSE; /* if TRUE, we hit the end of the file */
903 int didline = FALSE; /* if TRUE, we finished the last line */
904 int i;
905 long j;
906 static int recursive = FALSE; /* being called recursively */
907 int old_botline = wp->w_botline;
908#ifdef FEAT_FOLDING
909 long fold_count;
910#endif
911#ifdef FEAT_SYN_HL
912 /* remember what happened to the previous line, to know if
913 * check_visual_highlight() can be used */
914#define DID_NONE 1 /* didn't update a line */
915#define DID_LINE 2 /* updated a normal line */
916#define DID_FOLD 3 /* updated a folded line */
917 int did_update = DID_NONE;
918 linenr_T syntax_last_parsed = 0; /* last parsed text line */
919#endif
920 linenr_T mod_top = 0;
921 linenr_T mod_bot = 0;
922#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
923 int save_got_int;
924#endif
925
926 type = wp->w_redr_type;
927
928 if (type == NOT_VALID)
929 {
930#ifdef FEAT_WINDOWS
931 wp->w_redr_status = TRUE;
932#endif
933 wp->w_lines_valid = 0;
934 }
935
936 /* Window is zero-height: nothing to draw. */
937 if (wp->w_height == 0)
938 {
939 wp->w_redr_type = 0;
940 return;
941 }
942
943#ifdef FEAT_VERTSPLIT
944 /* Window is zero-width: Only need to draw the separator. */
945 if (wp->w_width == 0)
946 {
947 /* draw the vertical separator right of this window */
948 draw_vsep_win(wp, 0);
949 wp->w_redr_type = 0;
950 return;
951 }
952#endif
953
954#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200955 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956#endif
957
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000958#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200959 /* Force redraw when width of 'number' or 'relativenumber' column
960 * changes. */
961 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000962 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000963 {
964 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000965 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000966 }
967 else
968#endif
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
971 {
972 /*
973 * When there are both inserted/deleted lines and specific lines to be
974 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
975 * everything (only happens when redrawing is off for while).
976 */
977 type = NOT_VALID;
978 }
979 else
980 {
981 /*
982 * Set mod_top to the first line that needs displaying because of
983 * changes. Set mod_bot to the first line after the changes.
984 */
985 mod_top = wp->w_redraw_top;
986 if (wp->w_redraw_bot != 0)
987 mod_bot = wp->w_redraw_bot + 1;
988 else
989 mod_bot = 0;
990 wp->w_redraw_top = 0; /* reset for next time */
991 wp->w_redraw_bot = 0;
992 if (buf->b_mod_set)
993 {
994 if (mod_top == 0 || mod_top > buf->b_mod_top)
995 {
996 mod_top = buf->b_mod_top;
997#ifdef FEAT_SYN_HL
998 /* Need to redraw lines above the change that may be included
999 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001000 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001002 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (mod_top < 1)
1004 mod_top = 1;
1005 }
1006#endif
1007 }
1008 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1009 mod_bot = buf->b_mod_bot;
1010
1011#ifdef FEAT_SEARCH_EXTRA
1012 /* When 'hlsearch' is on and using a multi-line search pattern, a
1013 * change in one line may make the Search highlighting in a
1014 * previous line invalid. Simple solution: redraw all visible
1015 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001016 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001018 if (search_hl.rm.regprog != NULL
1019 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001021 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001022 {
1023 cur = wp->w_match_head;
1024 while (cur != NULL)
1025 {
1026 if (cur->match.regprog != NULL
1027 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001028 {
1029 top_to_mod = TRUE;
1030 break;
1031 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001032 cur = cur->next;
1033 }
1034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#endif
1036 }
1037#ifdef FEAT_FOLDING
1038 if (mod_top != 0 && hasAnyFolding(wp))
1039 {
1040 linenr_T lnumt, lnumb;
1041
1042 /*
1043 * A change in a line can cause lines above it to become folded or
1044 * unfolded. Find the top most buffer line that may be affected.
1045 * If the line was previously folded and displayed, get the first
1046 * line of that fold. If the line is folded now, get the first
1047 * folded line. Use the minimum of these two.
1048 */
1049
1050 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1051 * the line below it. If there is no valid entry, use w_topline.
1052 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1053 * to this line. If there is no valid entry, use MAXLNUM. */
1054 lnumt = wp->w_topline;
1055 lnumb = MAXLNUM;
1056 for (i = 0; i < wp->w_lines_valid; ++i)
1057 if (wp->w_lines[i].wl_valid)
1058 {
1059 if (wp->w_lines[i].wl_lastlnum < mod_top)
1060 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1061 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1062 {
1063 lnumb = wp->w_lines[i].wl_lnum;
1064 /* When there is a fold column it might need updating
1065 * in the next line ("J" just above an open fold). */
1066 if (wp->w_p_fdc > 0)
1067 ++lnumb;
1068 }
1069 }
1070
1071 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1072 if (mod_top > lnumt)
1073 mod_top = lnumt;
1074
1075 /* Now do the same for the bottom line (one above mod_bot). */
1076 --mod_bot;
1077 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1078 ++mod_bot;
1079 if (mod_bot < lnumb)
1080 mod_bot = lnumb;
1081 }
1082#endif
1083
1084 /* When a change starts above w_topline and the end is below
1085 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001086 * If the end of the change is above w_topline: do like no change was
1087 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (mod_top != 0 && mod_top < wp->w_topline)
1089 {
1090 if (mod_bot > wp->w_topline)
1091 mod_top = wp->w_topline;
1092#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001093 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 top_end = 1;
1095#endif
1096 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001097
1098 /* When line numbers are displayed need to redraw all lines below
1099 * inserted/deleted lines. */
1100 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1101 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 }
1103
1104 /*
1105 * When only displaying the lines at the top, set top_end. Used when
1106 * window has scrolled down for msg_scrolled.
1107 */
1108 if (type == REDRAW_TOP)
1109 {
1110 j = 0;
1111 for (i = 0; i < wp->w_lines_valid; ++i)
1112 {
1113 j += wp->w_lines[i].wl_size;
1114 if (j >= wp->w_upd_rows)
1115 {
1116 top_end = j;
1117 break;
1118 }
1119 }
1120 if (top_end == 0)
1121 /* not found (cannot happen?): redraw everything */
1122 type = NOT_VALID;
1123 else
1124 /* top area defined, the rest is VALID */
1125 type = VALID;
1126 }
1127
Bram Moolenaar367329b2007-08-30 11:53:22 +00001128 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001129 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1130 * non-zero and thus not FALSE) will indicate that screenclear() was not
1131 * called. */
1132 if (screen_cleared)
1133 screen_cleared = MAYBE;
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 /*
1136 * If there are no changes on the screen that require a complete redraw,
1137 * handle three cases:
1138 * 1: we are off the top of the screen by a few lines: scroll down
1139 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1140 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1141 * w_lines[] that needs updating.
1142 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001143 if ((type == VALID || type == SOME_VALID
1144 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#ifdef FEAT_DIFF
1146 && !wp->w_botfill && !wp->w_old_botfill
1147#endif
1148 )
1149 {
1150 if (mod_top != 0 && wp->w_topline == mod_top)
1151 {
1152 /*
1153 * w_topline is the first changed line, the scrolling will be done
1154 * further down.
1155 */
1156 }
1157 else if (wp->w_lines[0].wl_valid
1158 && (wp->w_topline < wp->w_lines[0].wl_lnum
1159#ifdef FEAT_DIFF
1160 || (wp->w_topline == wp->w_lines[0].wl_lnum
1161 && wp->w_topfill > wp->w_old_topfill)
1162#endif
1163 ))
1164 {
1165 /*
1166 * New topline is above old topline: May scroll down.
1167 */
1168#ifdef FEAT_FOLDING
1169 if (hasAnyFolding(wp))
1170 {
1171 linenr_T ln;
1172
1173 /* count the number of lines we are off, counting a sequence
1174 * of folded lines as one */
1175 j = 0;
1176 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1177 {
1178 ++j;
1179 if (j >= wp->w_height - 2)
1180 break;
1181 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1182 }
1183 }
1184 else
1185#endif
1186 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1187 if (j < wp->w_height - 2) /* not too far off */
1188 {
1189 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1190#ifdef FEAT_DIFF
1191 /* insert extra lines for previously invisible filler lines */
1192 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1193 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1194 - wp->w_old_topfill;
1195#endif
1196 if (i < wp->w_height - 2) /* less than a screen off */
1197 {
1198 /*
1199 * Try to insert the correct number of lines.
1200 * If not the last window, delete the lines at the bottom.
1201 * win_ins_lines may fail when the terminal can't do it.
1202 */
1203 if (i > 0)
1204 check_for_delay(FALSE);
1205 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1206 {
1207 if (wp->w_lines_valid != 0)
1208 {
1209 /* Need to update rows that are new, stop at the
1210 * first one that scrolled down. */
1211 top_end = i;
1212#ifdef FEAT_VISUAL
1213 scrolled_down = TRUE;
1214#endif
1215
1216 /* Move the entries that were scrolled, disable
1217 * the entries for the lines to be redrawn. */
1218 if ((wp->w_lines_valid += j) > wp->w_height)
1219 wp->w_lines_valid = wp->w_height;
1220 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1221 wp->w_lines[idx] = wp->w_lines[idx - j];
1222 while (idx >= 0)
1223 wp->w_lines[idx--].wl_valid = FALSE;
1224 }
1225 }
1226 else
1227 mid_start = 0; /* redraw all lines */
1228 }
1229 else
1230 mid_start = 0; /* redraw all lines */
1231 }
1232 else
1233 mid_start = 0; /* redraw all lines */
1234 }
1235 else
1236 {
1237 /*
1238 * New topline is at or below old topline: May scroll up.
1239 * When topline didn't change, find first entry in w_lines[] that
1240 * needs updating.
1241 */
1242
1243 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1244 j = -1;
1245 row = 0;
1246 for (i = 0; i < wp->w_lines_valid; i++)
1247 {
1248 if (wp->w_lines[i].wl_valid
1249 && wp->w_lines[i].wl_lnum == wp->w_topline)
1250 {
1251 j = i;
1252 break;
1253 }
1254 row += wp->w_lines[i].wl_size;
1255 }
1256 if (j == -1)
1257 {
1258 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1259 * lines */
1260 mid_start = 0;
1261 }
1262 else
1263 {
1264 /*
1265 * Try to delete the correct number of lines.
1266 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1267 */
1268#ifdef FEAT_DIFF
1269 /* If the topline didn't change, delete old filler lines,
1270 * otherwise delete filler lines of the new topline... */
1271 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1272 row += wp->w_old_topfill;
1273 else
1274 row += diff_check_fill(wp, wp->w_topline);
1275 /* ... but don't delete new filler lines. */
1276 row -= wp->w_topfill;
1277#endif
1278 if (row > 0)
1279 {
1280 check_for_delay(FALSE);
1281 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1282 bot_start = wp->w_height - row;
1283 else
1284 mid_start = 0; /* redraw all lines */
1285 }
1286 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1287 {
1288 /*
1289 * Skip the lines (below the deleted lines) that are still
1290 * valid and don't need redrawing. Copy their info
1291 * upwards, to compensate for the deleted lines. Set
1292 * bot_start to the first row that needs redrawing.
1293 */
1294 bot_start = 0;
1295 idx = 0;
1296 for (;;)
1297 {
1298 wp->w_lines[idx] = wp->w_lines[j];
1299 /* stop at line that didn't fit, unless it is still
1300 * valid (no lines deleted) */
1301 if (row > 0 && bot_start + row
1302 + (int)wp->w_lines[j].wl_size > wp->w_height)
1303 {
1304 wp->w_lines_valid = idx + 1;
1305 break;
1306 }
1307 bot_start += wp->w_lines[idx++].wl_size;
1308
1309 /* stop at the last valid entry in w_lines[].wl_size */
1310 if (++j >= wp->w_lines_valid)
1311 {
1312 wp->w_lines_valid = idx;
1313 break;
1314 }
1315 }
1316#ifdef FEAT_DIFF
1317 /* Correct the first entry for filler lines at the top
1318 * when it won't get updated below. */
1319 if (wp->w_p_diff && bot_start > 0)
1320 wp->w_lines[0].wl_size =
1321 plines_win_nofill(wp, wp->w_topline, TRUE)
1322 + wp->w_topfill;
1323#endif
1324 }
1325 }
1326 }
1327
1328 /* When starting redraw in the first line, redraw all lines. When
1329 * there is only one window it's probably faster to clear the screen
1330 * first. */
1331 if (mid_start == 0)
1332 {
1333 mid_end = wp->w_height;
1334 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001335 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001336 /* Clear the screen when it was not done by win_del_lines() or
1337 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1338 * then. */
1339 if (screen_cleared != TRUE)
1340 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001341#ifdef FEAT_WINDOWS
1342 /* The screen was cleared, redraw the tab pages line. */
1343 if (redraw_tabline)
1344 draw_tabline();
1345#endif
1346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001348
1349 /* When win_del_lines() or win_ins_lines() caused the screen to be
1350 * cleared (only happens for the first window) or when screenclear()
1351 * was called directly above, "must_redraw" will have been set to
1352 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1353 if (screen_cleared == TRUE)
1354 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 else
1357 {
1358 /* Not VALID or INVERTED: redraw all lines. */
1359 mid_start = 0;
1360 mid_end = wp->w_height;
1361 }
1362
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001363 if (type == SOME_VALID)
1364 {
1365 /* SOME_VALID: redraw all lines. */
1366 mid_start = 0;
1367 mid_end = wp->w_height;
1368 type = NOT_VALID;
1369 }
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371#ifdef FEAT_VISUAL
1372 /* check if we are updating or removing the inverted part */
1373 if ((VIsual_active && buf == curwin->w_buffer)
1374 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1375 {
1376 linenr_T from, to;
1377
1378 if (VIsual_active)
1379 {
1380 if (VIsual_active
1381 && (VIsual_mode != wp->w_old_visual_mode
1382 || type == INVERTED_ALL))
1383 {
1384 /*
1385 * If the type of Visual selection changed, redraw the whole
1386 * selection. Also when the ownership of the X selection is
1387 * gained or lost.
1388 */
1389 if (curwin->w_cursor.lnum < VIsual.lnum)
1390 {
1391 from = curwin->w_cursor.lnum;
1392 to = VIsual.lnum;
1393 }
1394 else
1395 {
1396 from = VIsual.lnum;
1397 to = curwin->w_cursor.lnum;
1398 }
1399 /* redraw more when the cursor moved as well */
1400 if (wp->w_old_cursor_lnum < from)
1401 from = wp->w_old_cursor_lnum;
1402 if (wp->w_old_cursor_lnum > to)
1403 to = wp->w_old_cursor_lnum;
1404 if (wp->w_old_visual_lnum < from)
1405 from = wp->w_old_visual_lnum;
1406 if (wp->w_old_visual_lnum > to)
1407 to = wp->w_old_visual_lnum;
1408 }
1409 else
1410 {
1411 /*
1412 * Find the line numbers that need to be updated: The lines
1413 * between the old cursor position and the current cursor
1414 * position. Also check if the Visual position changed.
1415 */
1416 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1417 {
1418 from = curwin->w_cursor.lnum;
1419 to = wp->w_old_cursor_lnum;
1420 }
1421 else
1422 {
1423 from = wp->w_old_cursor_lnum;
1424 to = curwin->w_cursor.lnum;
1425 if (from == 0) /* Visual mode just started */
1426 from = to;
1427 }
1428
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001429 if (VIsual.lnum != wp->w_old_visual_lnum
1430 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 {
1432 if (wp->w_old_visual_lnum < from
1433 && wp->w_old_visual_lnum != 0)
1434 from = wp->w_old_visual_lnum;
1435 if (wp->w_old_visual_lnum > to)
1436 to = wp->w_old_visual_lnum;
1437 if (VIsual.lnum < from)
1438 from = VIsual.lnum;
1439 if (VIsual.lnum > to)
1440 to = VIsual.lnum;
1441 }
1442 }
1443
1444 /*
1445 * If in block mode and changed column or curwin->w_curswant:
1446 * update all lines.
1447 * First compute the actual start and end column.
1448 */
1449 if (VIsual_mode == Ctrl_V)
1450 {
1451 colnr_T fromc, toc;
1452
1453 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1454 ++toc;
1455 if (curwin->w_curswant == MAXCOL)
1456 toc = MAXCOL;
1457
1458 if (fromc != wp->w_old_cursor_fcol
1459 || toc != wp->w_old_cursor_lcol)
1460 {
1461 if (from > VIsual.lnum)
1462 from = VIsual.lnum;
1463 if (to < VIsual.lnum)
1464 to = VIsual.lnum;
1465 }
1466 wp->w_old_cursor_fcol = fromc;
1467 wp->w_old_cursor_lcol = toc;
1468 }
1469 }
1470 else
1471 {
1472 /* Use the line numbers of the old Visual area. */
1473 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1474 {
1475 from = wp->w_old_cursor_lnum;
1476 to = wp->w_old_visual_lnum;
1477 }
1478 else
1479 {
1480 from = wp->w_old_visual_lnum;
1481 to = wp->w_old_cursor_lnum;
1482 }
1483 }
1484
1485 /*
1486 * There is no need to update lines above the top of the window.
1487 */
1488 if (from < wp->w_topline)
1489 from = wp->w_topline;
1490
1491 /*
1492 * If we know the value of w_botline, use it to restrict the update to
1493 * the lines that are visible in the window.
1494 */
1495 if (wp->w_valid & VALID_BOTLINE)
1496 {
1497 if (from >= wp->w_botline)
1498 from = wp->w_botline - 1;
1499 if (to >= wp->w_botline)
1500 to = wp->w_botline - 1;
1501 }
1502
1503 /*
1504 * Find the minimal part to be updated.
1505 * Watch out for scrolling that made entries in w_lines[] invalid.
1506 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1507 * top_end; need to redraw from top_end to the "to" line.
1508 * A middle mouse click with a Visual selection may change the text
1509 * above the Visual area and reset wl_valid, do count these for
1510 * mid_end (in srow).
1511 */
1512 if (mid_start > 0)
1513 {
1514 lnum = wp->w_topline;
1515 idx = 0;
1516 srow = 0;
1517 if (scrolled_down)
1518 mid_start = top_end;
1519 else
1520 mid_start = 0;
1521 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1522 {
1523 if (wp->w_lines[idx].wl_valid)
1524 mid_start += wp->w_lines[idx].wl_size;
1525 else if (!scrolled_down)
1526 srow += wp->w_lines[idx].wl_size;
1527 ++idx;
1528# ifdef FEAT_FOLDING
1529 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1530 lnum = wp->w_lines[idx].wl_lnum;
1531 else
1532# endif
1533 ++lnum;
1534 }
1535 srow += mid_start;
1536 mid_end = wp->w_height;
1537 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1538 {
1539 if (wp->w_lines[idx].wl_valid
1540 && wp->w_lines[idx].wl_lnum >= to + 1)
1541 {
1542 /* Only update until first row of this line */
1543 mid_end = srow;
1544 break;
1545 }
1546 srow += wp->w_lines[idx].wl_size;
1547 }
1548 }
1549 }
1550
1551 if (VIsual_active && buf == curwin->w_buffer)
1552 {
1553 wp->w_old_visual_mode = VIsual_mode;
1554 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1555 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001556 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 wp->w_old_curswant = curwin->w_curswant;
1558 }
1559 else
1560 {
1561 wp->w_old_visual_mode = 0;
1562 wp->w_old_cursor_lnum = 0;
1563 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001564 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566#endif /* FEAT_VISUAL */
1567
1568#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1569 /* reset got_int, otherwise regexp won't work */
1570 save_got_int = got_int;
1571 got_int = 0;
1572#endif
1573#ifdef FEAT_FOLDING
1574 win_foldinfo.fi_level = 0;
1575#endif
1576
1577 /*
1578 * Update all the window rows.
1579 */
1580 idx = 0; /* first entry in w_lines[].wl_size */
1581 row = 0;
1582 srow = 0;
1583 lnum = wp->w_topline; /* first line shown in window */
1584 for (;;)
1585 {
1586 /* stop updating when reached the end of the window (check for _past_
1587 * the end of the window is at the end of the loop) */
1588 if (row == wp->w_height)
1589 {
1590 didline = TRUE;
1591 break;
1592 }
1593
1594 /* stop updating when hit the end of the file */
1595 if (lnum > buf->b_ml.ml_line_count)
1596 {
1597 eof = TRUE;
1598 break;
1599 }
1600
1601 /* Remember the starting row of the line that is going to be dealt
1602 * with. It is used further down when the line doesn't fit. */
1603 srow = row;
1604
1605 /*
1606 * Update a line when it is in an area that needs updating, when it
1607 * has changes or w_lines[idx] is invalid.
1608 * bot_start may be halfway a wrapped line after using
1609 * win_del_lines(), check if the current line includes it.
1610 * When syntax folding is being used, the saved syntax states will
1611 * already have been updated, we can't see where the syntax state is
1612 * the same again, just update until the end of the window.
1613 */
1614 if (row < top_end
1615 || (row >= mid_start && row < mid_end)
1616#ifdef FEAT_SEARCH_EXTRA
1617 || top_to_mod
1618#endif
1619 || idx >= wp->w_lines_valid
1620 || (row + wp->w_lines[idx].wl_size > bot_start)
1621 || (mod_top != 0
1622 && (lnum == mod_top
1623 || (lnum >= mod_top
1624 && (lnum < mod_bot
1625#ifdef FEAT_SYN_HL
1626 || did_update == DID_FOLD
1627 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001628 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 && (
1630# ifdef FEAT_FOLDING
1631 (foldmethodIsSyntax(wp)
1632 && hasAnyFolding(wp)) ||
1633# endif
1634 syntax_check_changed(lnum)))
1635#endif
1636 )))))
1637 {
1638#ifdef FEAT_SEARCH_EXTRA
1639 if (lnum == mod_top)
1640 top_to_mod = FALSE;
1641#endif
1642
1643 /*
1644 * When at start of changed lines: May scroll following lines
1645 * up or down to minimize redrawing.
1646 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001647 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 */
1649 if (lnum == mod_top
1650 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001651 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
1653 int old_rows = 0;
1654 int new_rows = 0;
1655 int xtra_rows;
1656 linenr_T l;
1657
1658 /* Count the old number of window rows, using w_lines[], which
1659 * should still contain the sizes for the lines as they are
1660 * currently displayed. */
1661 for (i = idx; i < wp->w_lines_valid; ++i)
1662 {
1663 /* Only valid lines have a meaningful wl_lnum. Invalid
1664 * lines are part of the changed area. */
1665 if (wp->w_lines[i].wl_valid
1666 && wp->w_lines[i].wl_lnum == mod_bot)
1667 break;
1668 old_rows += wp->w_lines[i].wl_size;
1669#ifdef FEAT_FOLDING
1670 if (wp->w_lines[i].wl_valid
1671 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1672 {
1673 /* Must have found the last valid entry above mod_bot.
1674 * Add following invalid entries. */
1675 ++i;
1676 while (i < wp->w_lines_valid
1677 && !wp->w_lines[i].wl_valid)
1678 old_rows += wp->w_lines[i++].wl_size;
1679 break;
1680 }
1681#endif
1682 }
1683
1684 if (i >= wp->w_lines_valid)
1685 {
1686 /* We can't find a valid line below the changed lines,
1687 * need to redraw until the end of the window.
1688 * Inserting/deleting lines has no use. */
1689 bot_start = 0;
1690 }
1691 else
1692 {
1693 /* Able to count old number of rows: Count new window
1694 * rows, and may insert/delete lines */
1695 j = idx;
1696 for (l = lnum; l < mod_bot; ++l)
1697 {
1698#ifdef FEAT_FOLDING
1699 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1700 ++new_rows;
1701 else
1702#endif
1703#ifdef FEAT_DIFF
1704 if (l == wp->w_topline)
1705 new_rows += plines_win_nofill(wp, l, TRUE)
1706 + wp->w_topfill;
1707 else
1708#endif
1709 new_rows += plines_win(wp, l, TRUE);
1710 ++j;
1711 if (new_rows > wp->w_height - row - 2)
1712 {
1713 /* it's getting too much, must redraw the rest */
1714 new_rows = 9999;
1715 break;
1716 }
1717 }
1718 xtra_rows = new_rows - old_rows;
1719 if (xtra_rows < 0)
1720 {
1721 /* May scroll text up. If there is not enough
1722 * remaining text or scrolling fails, must redraw the
1723 * rest. If scrolling works, must redraw the text
1724 * below the scrolled text. */
1725 if (row - xtra_rows >= wp->w_height - 2)
1726 mod_bot = MAXLNUM;
1727 else
1728 {
1729 check_for_delay(FALSE);
1730 if (win_del_lines(wp, row,
1731 -xtra_rows, FALSE, FALSE) == FAIL)
1732 mod_bot = MAXLNUM;
1733 else
1734 bot_start = wp->w_height + xtra_rows;
1735 }
1736 }
1737 else if (xtra_rows > 0)
1738 {
1739 /* May scroll text down. If there is not enough
1740 * remaining text of scrolling fails, must redraw the
1741 * rest. */
1742 if (row + xtra_rows >= wp->w_height - 2)
1743 mod_bot = MAXLNUM;
1744 else
1745 {
1746 check_for_delay(FALSE);
1747 if (win_ins_lines(wp, row + old_rows,
1748 xtra_rows, FALSE, FALSE) == FAIL)
1749 mod_bot = MAXLNUM;
1750 else if (top_end > row + old_rows)
1751 /* Scrolled the part at the top that requires
1752 * updating down. */
1753 top_end += xtra_rows;
1754 }
1755 }
1756
1757 /* When not updating the rest, may need to move w_lines[]
1758 * entries. */
1759 if (mod_bot != MAXLNUM && i != j)
1760 {
1761 if (j < i)
1762 {
1763 int x = row + new_rows;
1764
1765 /* move entries in w_lines[] upwards */
1766 for (;;)
1767 {
1768 /* stop at last valid entry in w_lines[] */
1769 if (i >= wp->w_lines_valid)
1770 {
1771 wp->w_lines_valid = j;
1772 break;
1773 }
1774 wp->w_lines[j] = wp->w_lines[i];
1775 /* stop at a line that won't fit */
1776 if (x + (int)wp->w_lines[j].wl_size
1777 > wp->w_height)
1778 {
1779 wp->w_lines_valid = j + 1;
1780 break;
1781 }
1782 x += wp->w_lines[j++].wl_size;
1783 ++i;
1784 }
1785 if (bot_start > x)
1786 bot_start = x;
1787 }
1788 else /* j > i */
1789 {
1790 /* move entries in w_lines[] downwards */
1791 j -= i;
1792 wp->w_lines_valid += j;
1793 if (wp->w_lines_valid > wp->w_height)
1794 wp->w_lines_valid = wp->w_height;
1795 for (i = wp->w_lines_valid; i - j >= idx; --i)
1796 wp->w_lines[i] = wp->w_lines[i - j];
1797
1798 /* The w_lines[] entries for inserted lines are
1799 * now invalid, but wl_size may be used above.
1800 * Reset to zero. */
1801 while (i >= idx)
1802 {
1803 wp->w_lines[i].wl_size = 0;
1804 wp->w_lines[i--].wl_valid = FALSE;
1805 }
1806 }
1807 }
1808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
1810
1811#ifdef FEAT_FOLDING
1812 /*
1813 * When lines are folded, display one line for all of them.
1814 * Otherwise, display normally (can be several display lines when
1815 * 'wrap' is on).
1816 */
1817 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1818 if (fold_count != 0)
1819 {
1820 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1821 ++row;
1822 --fold_count;
1823 wp->w_lines[idx].wl_folded = TRUE;
1824 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1825# ifdef FEAT_SYN_HL
1826 did_update = DID_FOLD;
1827# endif
1828 }
1829 else
1830#endif
1831 if (idx < wp->w_lines_valid
1832 && wp->w_lines[idx].wl_valid
1833 && wp->w_lines[idx].wl_lnum == lnum
1834 && lnum > wp->w_topline
1835 && !(dy_flags & DY_LASTLINE)
1836 && srow + wp->w_lines[idx].wl_size > wp->w_height
1837#ifdef FEAT_DIFF
1838 && diff_check_fill(wp, lnum) == 0
1839#endif
1840 )
1841 {
1842 /* This line is not going to fit. Don't draw anything here,
1843 * will draw "@ " lines below. */
1844 row = wp->w_height + 1;
1845 }
1846 else
1847 {
1848#ifdef FEAT_SEARCH_EXTRA
1849 prepare_search_hl(wp, lnum);
1850#endif
1851#ifdef FEAT_SYN_HL
1852 /* Let the syntax stuff know we skipped a few lines. */
1853 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001854 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 syntax_end_parsing(syntax_last_parsed + 1);
1856#endif
1857
1858 /*
1859 * Display one line.
1860 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001861 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863#ifdef FEAT_FOLDING
1864 wp->w_lines[idx].wl_folded = FALSE;
1865 wp->w_lines[idx].wl_lastlnum = lnum;
1866#endif
1867#ifdef FEAT_SYN_HL
1868 did_update = DID_LINE;
1869 syntax_last_parsed = lnum;
1870#endif
1871 }
1872
1873 wp->w_lines[idx].wl_lnum = lnum;
1874 wp->w_lines[idx].wl_valid = TRUE;
1875 if (row > wp->w_height) /* past end of screen */
1876 {
1877 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001878 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1880 ++idx;
1881 break;
1882 }
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 = row - srow;
1885 ++idx;
1886#ifdef FEAT_FOLDING
1887 lnum += fold_count + 1;
1888#else
1889 ++lnum;
1890#endif
1891 }
1892 else
1893 {
1894 /* This line does not need updating, advance to the next one */
1895 row += wp->w_lines[idx++].wl_size;
1896 if (row > wp->w_height) /* past end of screen */
1897 break;
1898#ifdef FEAT_FOLDING
1899 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1900#else
1901 ++lnum;
1902#endif
1903#ifdef FEAT_SYN_HL
1904 did_update = DID_NONE;
1905#endif
1906 }
1907
1908 if (lnum > buf->b_ml.ml_line_count)
1909 {
1910 eof = TRUE;
1911 break;
1912 }
1913 }
1914 /*
1915 * End of loop over all window lines.
1916 */
1917
1918
1919 if (idx > wp->w_lines_valid)
1920 wp->w_lines_valid = idx;
1921
1922#ifdef FEAT_SYN_HL
1923 /*
1924 * Let the syntax stuff know we stop parsing here.
1925 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001926 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 syntax_end_parsing(syntax_last_parsed + 1);
1928#endif
1929
1930 /*
1931 * If we didn't hit the end of the file, and we didn't finish the last
1932 * line we were working on, then the line didn't fit.
1933 */
1934 wp->w_empty_rows = 0;
1935#ifdef FEAT_DIFF
1936 wp->w_filler_rows = 0;
1937#endif
1938 if (!eof && !didline)
1939 {
1940 if (lnum == wp->w_topline)
1941 {
1942 /*
1943 * Single line that does not fit!
1944 * Don't overwrite it, it can be edited.
1945 */
1946 wp->w_botline = lnum + 1;
1947 }
1948#ifdef FEAT_DIFF
1949 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1950 {
1951 /* Window ends in filler lines. */
1952 wp->w_botline = lnum;
1953 wp->w_filler_rows = wp->w_height - srow;
1954 }
1955#endif
1956 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1957 {
1958 /*
1959 * Last line isn't finished: Display "@@@" at the end.
1960 */
1961 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1962 W_WINROW(wp) + wp->w_height,
1963 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1964 '@', '@', hl_attr(HLF_AT));
1965 set_empty_rows(wp, srow);
1966 wp->w_botline = lnum;
1967 }
1968 else
1969 {
1970 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1971 wp->w_botline = lnum;
1972 }
1973 }
1974 else
1975 {
1976#ifdef FEAT_VERTSPLIT
1977 draw_vsep_win(wp, row);
1978#endif
1979 if (eof) /* we hit the end of the file */
1980 {
1981 wp->w_botline = buf->b_ml.ml_line_count + 1;
1982#ifdef FEAT_DIFF
1983 j = diff_check_fill(wp, wp->w_botline);
1984 if (j > 0 && !wp->w_botfill)
1985 {
1986 /*
1987 * Display filler lines at the end of the file
1988 */
1989 if (char2cells(fill_diff) > 1)
1990 i = '-';
1991 else
1992 i = fill_diff;
1993 if (row + j > wp->w_height)
1994 j = wp->w_height - row;
1995 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1996 row += j;
1997 }
1998#endif
1999 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002000 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 wp->w_botline = lnum;
2002
2003 /* make sure the rest of the screen is blank */
2004 /* put '~'s on rows that aren't part of the file. */
2005 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2006 }
2007
2008 /* Reset the type of redrawing required, the window has been updated. */
2009 wp->w_redr_type = 0;
2010#ifdef FEAT_DIFF
2011 wp->w_old_topfill = wp->w_topfill;
2012 wp->w_old_botfill = wp->w_botfill;
2013#endif
2014
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002015 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
2017 /*
2018 * There is a trick with w_botline. If we invalidate it on each
2019 * change that might modify it, this will cause a lot of expensive
2020 * calls to plines() in update_topline() each time. Therefore the
2021 * value of w_botline is often approximated, and this value is used to
2022 * compute the value of w_topline. If the value of w_botline was
2023 * wrong, check that the value of w_topline is correct (cursor is on
2024 * the visible part of the text). If it's not, we need to redraw
2025 * again. Mostly this just means scrolling up a few lines, so it
2026 * doesn't look too bad. Only do this for the current window (where
2027 * changes are relevant).
2028 */
2029 wp->w_valid |= VALID_BOTLINE;
2030 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2031 {
2032 recursive = TRUE;
2033 curwin->w_valid &= ~VALID_TOPLINE;
2034 update_topline(); /* may invalidate w_botline again */
2035 if (must_redraw != 0)
2036 {
2037 /* Don't update for changes in buffer again. */
2038 i = curbuf->b_mod_set;
2039 curbuf->b_mod_set = FALSE;
2040 win_update(curwin);
2041 must_redraw = 0;
2042 curbuf->b_mod_set = i;
2043 }
2044 recursive = FALSE;
2045 }
2046 }
2047
2048#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2049 /* restore got_int, unless CTRL-C was hit while redrawing */
2050 if (!got_int)
2051 got_int = save_got_int;
2052#endif
2053}
2054
2055#ifdef FEAT_SIGNS
2056static int draw_signcolumn __ARGS((win_T *wp));
2057
2058/*
2059 * Return TRUE when window "wp" has a column to draw signs in.
2060 */
2061 static int
2062draw_signcolumn(wp)
2063 win_T *wp;
2064{
2065 return (wp->w_buffer->b_signlist != NULL
2066# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002067 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068# endif
2069 );
2070}
2071#endif
2072
2073/*
2074 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2075 * as the filler character.
2076 */
2077 static void
2078win_draw_end(wp, c1, c2, row, endrow, hl)
2079 win_T *wp;
2080 int c1;
2081 int c2;
2082 int row;
2083 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002084 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085{
2086#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2087 int n = 0;
2088# define FDC_OFF n
2089#else
2090# define FDC_OFF 0
2091#endif
2092
2093#ifdef FEAT_RIGHTLEFT
2094 if (wp->w_p_rl)
2095 {
2096 /* No check for cmdline window: should never be right-left. */
2097# ifdef FEAT_FOLDING
2098 n = wp->w_p_fdc;
2099
2100 if (n > 0)
2101 {
2102 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002103 if (n > W_WIDTH(wp))
2104 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2106 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2107 ' ', ' ', hl_attr(HLF_FC));
2108 }
2109# endif
2110# ifdef FEAT_SIGNS
2111 if (draw_signcolumn(wp))
2112 {
2113 int nn = n + 2;
2114
2115 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002116 if (nn > W_WIDTH(wp))
2117 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2119 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2120 ' ', ' ', hl_attr(HLF_SC));
2121 n = nn;
2122 }
2123# endif
2124 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2125 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2126 c2, c2, hl_attr(hl));
2127 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2128 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2129 c1, c2, hl_attr(hl));
2130 }
2131 else
2132#endif
2133 {
2134#ifdef FEAT_CMDWIN
2135 if (cmdwin_type != 0 && wp == curwin)
2136 {
2137 /* draw the cmdline character in the leftmost column */
2138 n = 1;
2139 if (n > wp->w_width)
2140 n = wp->w_width;
2141 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2142 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2143 cmdwin_type, ' ', hl_attr(HLF_AT));
2144 }
2145#endif
2146#ifdef FEAT_FOLDING
2147 if (wp->w_p_fdc > 0)
2148 {
2149 int nn = n + wp->w_p_fdc;
2150
2151 /* draw the fold column at the left */
2152 if (nn > W_WIDTH(wp))
2153 nn = W_WIDTH(wp);
2154 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2155 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2156 ' ', ' ', hl_attr(HLF_FC));
2157 n = nn;
2158 }
2159#endif
2160#ifdef FEAT_SIGNS
2161 if (draw_signcolumn(wp))
2162 {
2163 int nn = n + 2;
2164
2165 /* draw the sign column after the fold column */
2166 if (nn > W_WIDTH(wp))
2167 nn = W_WIDTH(wp);
2168 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2169 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2170 ' ', ' ', hl_attr(HLF_SC));
2171 n = nn;
2172 }
2173#endif
2174 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2175 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2176 c1, c2, hl_attr(hl));
2177 }
2178 set_empty_rows(wp, row);
2179}
2180
Bram Moolenaar1a384422010-07-14 19:53:30 +02002181#ifdef FEAT_SYN_HL
2182static int advance_color_col __ARGS((int vcol, int **color_cols));
2183
2184/*
2185 * Advance **color_cols and return TRUE when there are columns to draw.
2186 */
2187 static int
2188advance_color_col(vcol, color_cols)
2189 int vcol;
2190 int **color_cols;
2191{
2192 while (**color_cols >= 0 && vcol > **color_cols)
2193 ++*color_cols;
2194 return (**color_cols >= 0);
2195}
2196#endif
2197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#ifdef FEAT_FOLDING
2199/*
2200 * Display one folded line.
2201 */
2202 static void
2203fold_line(wp, fold_count, foldinfo, lnum, row)
2204 win_T *wp;
2205 long fold_count;
2206 foldinfo_T *foldinfo;
2207 linenr_T lnum;
2208 int row;
2209{
2210 char_u buf[51];
2211 pos_T *top, *bot;
2212 linenr_T lnume = lnum + fold_count - 1;
2213 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002214 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 int col;
2217 int txtcol;
2218 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002219 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
2221 /* Build the fold line:
2222 * 1. Add the cmdwin_type for the command-line window
2223 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002224 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 * 4. Compose the text
2226 * 5. Add the text
2227 * 6. set highlighting for the Visual area an other text
2228 */
2229 col = 0;
2230
2231 /*
2232 * 1. Add the cmdwin_type for the command-line window
2233 * Ignores 'rightleft', this window is never right-left.
2234 */
2235#ifdef FEAT_CMDWIN
2236 if (cmdwin_type != 0 && wp == curwin)
2237 {
2238 ScreenLines[off] = cmdwin_type;
2239 ScreenAttrs[off] = hl_attr(HLF_AT);
2240#ifdef FEAT_MBYTE
2241 if (enc_utf8)
2242 ScreenLinesUC[off] = 0;
2243#endif
2244 ++col;
2245 }
2246#endif
2247
2248 /*
2249 * 2. Add the 'foldcolumn'
2250 */
2251 fdc = wp->w_p_fdc;
2252 if (fdc > W_WIDTH(wp) - col)
2253 fdc = W_WIDTH(wp) - col;
2254 if (fdc > 0)
2255 {
2256 fill_foldcolumn(buf, wp, TRUE, lnum);
2257#ifdef FEAT_RIGHTLEFT
2258 if (wp->w_p_rl)
2259 {
2260 int i;
2261
2262 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2263 hl_attr(HLF_FC));
2264 /* reverse the fold column */
2265 for (i = 0; i < fdc; ++i)
2266 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2267 }
2268 else
2269#endif
2270 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2271 col += fdc;
2272 }
2273
2274#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002275# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2276 for (ri = 0; ri < l; ++ri) \
2277 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2278 else \
2279 for (ri = 0; ri < l; ++ri) \
2280 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002282# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2283 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#endif
2285
Bram Moolenaar64486672010-05-16 15:46:46 +02002286 /* Set all attributes of the 'number' or 'relativenumber' column and the
2287 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002288 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290#ifdef FEAT_SIGNS
2291 /* If signs are being displayed, add two spaces. */
2292 if (draw_signcolumn(wp))
2293 {
2294 len = W_WIDTH(wp) - col;
2295 if (len > 0)
2296 {
2297 if (len > 2)
2298 len = 2;
2299# ifdef FEAT_RIGHTLEFT
2300 if (wp->w_p_rl)
2301 /* the line number isn't reversed */
2302 copy_text_attr(off + W_WIDTH(wp) - len - col,
2303 (char_u *)" ", len, hl_attr(HLF_FL));
2304 else
2305# endif
2306 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2307 col += len;
2308 }
2309 }
2310#endif
2311
2312 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002313 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002315 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 len = W_WIDTH(wp) - col;
2318 if (len > 0)
2319 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002320 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002321 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002322
2323 if (len > w + 1)
2324 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002325
2326 if (wp->w_p_nu)
2327 /* 'number' */
2328 num = (long)lnum;
2329 else
2330 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002331 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02002332
2333 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#ifdef FEAT_RIGHTLEFT
2335 if (wp->w_p_rl)
2336 /* the line number isn't reversed */
2337 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2338 hl_attr(HLF_FL));
2339 else
2340#endif
2341 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2342 col += len;
2343 }
2344 }
2345
2346 /*
2347 * 4. Compose the folded-line string with 'foldtext', if set.
2348 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002349 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 txtcol = col; /* remember where text starts */
2352
2353 /*
2354 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2355 * Right-left text is put in columns 0 - number-col, normal text is put
2356 * in columns number-col - window-width.
2357 */
2358#ifdef FEAT_MBYTE
2359 if (has_mbyte)
2360 {
2361 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002362 int u8c, u8cc[MAX_MCO];
2363 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 int idx;
2365 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002366 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367# ifdef FEAT_ARABIC
2368 int prev_c = 0; /* previous Arabic character */
2369 int prev_c1 = 0; /* first composing char for prev_c */
2370# endif
2371
2372# ifdef FEAT_RIGHTLEFT
2373 if (wp->w_p_rl)
2374 idx = off;
2375 else
2376# endif
2377 idx = off + col;
2378
2379 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2380 for (p = text; *p != NUL; )
2381 {
2382 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002383 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (col + cells > W_WIDTH(wp)
2385# ifdef FEAT_RIGHTLEFT
2386 - (wp->w_p_rl ? col : 0)
2387# endif
2388 )
2389 break;
2390 ScreenLines[idx] = *p;
2391 if (enc_utf8)
2392 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002393 u8c = utfc_ptr2char(p, u8cc);
2394 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 ScreenLinesUC[idx] = 0;
2397#ifdef FEAT_ARABIC
2398 prev_c = u8c;
2399#endif
2400 }
2401 else
2402 {
2403#ifdef FEAT_ARABIC
2404 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2405 {
2406 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002407 int pc, pc1, nc;
2408 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 int firstbyte = *p;
2410
2411 /* The idea of what is the previous and next
2412 * character depends on 'rightleft'. */
2413 if (wp->w_p_rl)
2414 {
2415 pc = prev_c;
2416 pc1 = prev_c1;
2417 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002418 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420 else
2421 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002422 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002424 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426 prev_c = u8c;
2427
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002428 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 pc, pc1, nc);
2430 ScreenLines[idx] = firstbyte;
2431 }
2432 else
2433 prev_c = u8c;
2434#endif
2435 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002436#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 if (u8c >= 0x10000)
2438 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2439 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002442 for (i = 0; i < Screen_mco; ++i)
2443 {
2444 ScreenLinesC[i][idx] = u8cc[i];
2445 if (u8cc[i] == 0)
2446 break;
2447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
2449 if (cells > 1)
2450 ScreenLines[idx + 1] = 0;
2451 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002452 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2453 /* double-byte single width character */
2454 ScreenLines2[idx] = p[1];
2455 else if (cells > 1)
2456 /* double-width character */
2457 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 col += cells;
2459 idx += cells;
2460 p += c_len;
2461 }
2462 }
2463 else
2464#endif
2465 {
2466 len = (int)STRLEN(text);
2467 if (len > W_WIDTH(wp) - col)
2468 len = W_WIDTH(wp) - col;
2469 if (len > 0)
2470 {
2471#ifdef FEAT_RIGHTLEFT
2472 if (wp->w_p_rl)
2473 STRNCPY(current_ScreenLine, text, len);
2474 else
2475#endif
2476 STRNCPY(current_ScreenLine + col, text, len);
2477 col += len;
2478 }
2479 }
2480
2481 /* Fill the rest of the line with the fold filler */
2482#ifdef FEAT_RIGHTLEFT
2483 if (wp->w_p_rl)
2484 col -= txtcol;
2485#endif
2486 while (col < W_WIDTH(wp)
2487#ifdef FEAT_RIGHTLEFT
2488 - (wp->w_p_rl ? txtcol : 0)
2489#endif
2490 )
2491 {
2492#ifdef FEAT_MBYTE
2493 if (enc_utf8)
2494 {
2495 if (fill_fold >= 0x80)
2496 {
2497 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002498 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 else
2501 ScreenLinesUC[off + col] = 0;
2502 }
2503#endif
2504 ScreenLines[off + col++] = fill_fold;
2505 }
2506
2507 if (text != buf)
2508 vim_free(text);
2509
2510 /*
2511 * 6. set highlighting for the Visual area an other text.
2512 * If all folded lines are in the Visual area, highlight the line.
2513 */
2514#ifdef FEAT_VISUAL
2515 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2516 {
2517 if (ltoreq(curwin->w_cursor, VIsual))
2518 {
2519 /* Visual is after curwin->w_cursor */
2520 top = &curwin->w_cursor;
2521 bot = &VIsual;
2522 }
2523 else
2524 {
2525 /* Visual is before curwin->w_cursor */
2526 top = &VIsual;
2527 bot = &curwin->w_cursor;
2528 }
2529 if (lnum >= top->lnum
2530 && lnume <= bot->lnum
2531 && (VIsual_mode != 'v'
2532 || ((lnum > top->lnum
2533 || (lnum == top->lnum
2534 && top->col == 0))
2535 && (lnume < bot->lnum
2536 || (lnume == bot->lnum
2537 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002538 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
2540 if (VIsual_mode == Ctrl_V)
2541 {
2542 /* Visual block mode: highlight the chars part of the block */
2543 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2544 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002545 if (wp->w_old_cursor_lcol != MAXCOL
2546 && wp->w_old_cursor_lcol + txtcol
2547 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 len = wp->w_old_cursor_lcol;
2549 else
2550 len = W_WIDTH(wp) - txtcol;
2551 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002552 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554 }
2555 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002558 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561 }
2562#endif
2563
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002564#ifdef FEAT_SYN_HL
2565 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002566 if (wp->w_p_cuc)
2567 {
2568 txtcol += wp->w_virtcol;
2569 if (wp->w_p_wrap)
2570 txtcol -= wp->w_skipcol;
2571 else
2572 txtcol -= wp->w_leftcol;
2573 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2574 ScreenAttrs[off + txtcol] = hl_combine_attr(
2575 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2576 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2580 (int)W_WIDTH(wp), FALSE);
2581
2582 /*
2583 * Update w_cline_height and w_cline_folded if the cursor line was
2584 * updated (saves a call to plines() later).
2585 */
2586 if (wp == curwin
2587 && lnum <= curwin->w_cursor.lnum
2588 && lnume >= curwin->w_cursor.lnum)
2589 {
2590 curwin->w_cline_row = row;
2591 curwin->w_cline_height = 1;
2592 curwin->w_cline_folded = TRUE;
2593 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2594 }
2595}
2596
2597/*
2598 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2599 */
2600 static void
2601copy_text_attr(off, buf, len, attr)
2602 int off;
2603 char_u *buf;
2604 int len;
2605 int attr;
2606{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002607 int i;
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 mch_memmove(ScreenLines + off, buf, (size_t)len);
2610# ifdef FEAT_MBYTE
2611 if (enc_utf8)
2612 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2613# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002614 for (i = 0; i < len; ++i)
2615 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616}
2617
2618/*
2619 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002620 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 */
2622 static void
2623fill_foldcolumn(p, wp, closed, lnum)
2624 char_u *p;
2625 win_T *wp;
2626 int closed; /* TRUE of FALSE */
2627 linenr_T lnum; /* current line number */
2628{
2629 int i = 0;
2630 int level;
2631 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002632 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634 /* Init to all spaces. */
2635 copy_spaces(p, (size_t)wp->w_p_fdc);
2636
2637 level = win_foldinfo.fi_level;
2638 if (level > 0)
2639 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002640 /* If there is only one column put more info in it. */
2641 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2642
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 /* If the column is too narrow, we start at the lowest level that
2644 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002645 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (first_level < 1)
2647 first_level = 1;
2648
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002649 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
2651 if (win_foldinfo.fi_lnum == lnum
2652 && first_level + i >= win_foldinfo.fi_low_level)
2653 p[i] = '-';
2654 else if (first_level == 1)
2655 p[i] = '|';
2656 else if (first_level + i <= 9)
2657 p[i] = '0' + first_level + i;
2658 else
2659 p[i] = '>';
2660 if (first_level + i == level)
2661 break;
2662 }
2663 }
2664 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002665 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666}
2667#endif /* FEAT_FOLDING */
2668
2669/*
2670 * Display line "lnum" of window 'wp' on the screen.
2671 * Start at row "startrow", stop when "endrow" is reached.
2672 * wp->w_virtcol needs to be valid.
2673 *
2674 * Return the number of last row the line occupies.
2675 */
2676 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002677win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 win_T *wp;
2679 linenr_T lnum;
2680 int startrow;
2681 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002682 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
2684 int col; /* visual column on screen */
2685 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2686 int c = 0; /* init for GCC */
2687 long vcol = 0; /* virtual column (for tabs) */
2688 long vcol_prev = -1; /* "vcol" of previous character */
2689 char_u *line; /* current line */
2690 char_u *ptr; /* current position in "line" */
2691 int row; /* row in the window, excl w_winrow */
2692 int screen_row; /* row on the screen, incl w_winrow */
2693
2694 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2695 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002696 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 int c_extra = NUL; /* extra chars, all the same */
2698 int extra_attr = 0; /* attributes when n_extra != 0 */
2699 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2700 displaying lcs_eol at end-of-line */
2701 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2702 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2703
2704 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2705 int saved_n_extra = 0;
2706 char_u *saved_p_extra = NULL;
2707 int saved_c_extra = 0;
2708 int saved_char_attr = 0;
2709
2710 int n_attr = 0; /* chars with special attr */
2711 int saved_attr2 = 0; /* char_attr saved for n_attr */
2712 int n_attr3 = 0; /* chars with overruling special attr */
2713 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2714
2715 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2716
2717 int fromcol, tocol; /* start/end of inverting */
2718 int fromcol_prev = -2; /* start of inverting after cursor */
2719 int noinvcur = FALSE; /* don't invert the cursor */
2720#ifdef FEAT_VISUAL
2721 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002722 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723#endif
2724 pos_T pos;
2725 long v;
2726
2727 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002728 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2730 in this line */
2731 int attr = 0; /* attributes for area highlighting */
2732 int area_attr = 0; /* attributes desired by highlighting */
2733 int search_attr = 0; /* attributes desired by 'hlsearch' */
2734#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002735 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 int syntax_attr = 0; /* attributes desired by syntax */
2737 int has_syntax = FALSE; /* this buffer has syntax highl. */
2738 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002739 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002740 int draw_color_col = FALSE; /* highlight colorcolumn */
2741 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002742#endif
2743#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002744 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002745# define SPWORDLEN 150
2746 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002747 int nextlinecol = 0; /* column where nextline[] starts */
2748 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002749 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002750 int spell_attr = 0; /* attributes desired by spelling */
2751 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002752 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2753 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002754 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002755 static int cap_col = -1; /* column to check for Cap word */
2756 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002757 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#endif
2759 int extra_check; /* has syntax or linebreak */
2760#ifdef FEAT_MBYTE
2761 int multi_attr = 0; /* attributes desired by multibyte */
2762 int mb_l = 1; /* multi-byte byte length */
2763 int mb_c = 0; /* decoded multi-byte character */
2764 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002765 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766#endif
2767#ifdef FEAT_DIFF
2768 int filler_lines; /* nr of filler lines to be drawn */
2769 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002770 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 int change_start = MAXCOL; /* first col of changed area */
2772 int change_end = -1; /* last col of changed area */
2773#endif
2774 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2775#ifdef FEAT_LINEBREAK
2776 int need_showbreak = FALSE;
2777#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002778#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2779 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002781 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782#endif
2783#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002784 matchitem_T *cur; /* points to the match list */
2785 match_T *shl; /* points to search_hl or a match */
2786 int shl_flag; /* flag to indicate whether search_hl
2787 has been processed or not */
2788 int prevcol_hl_flag; /* flag to indicate whether prevcol
2789 equals startcol of search_hl or one
2790 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791#endif
2792#ifdef FEAT_ARABIC
2793 int prev_c = 0; /* previous Arabic character */
2794 int prev_c1 = 0; /* first composing char for prev_c */
2795#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002796#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002797 int did_line_attr = 0;
2798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 /* draw_state: items that are drawn in sequence: */
2801#define WL_START 0 /* nothing done yet */
2802#ifdef FEAT_CMDWIN
2803# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2804#else
2805# define WL_CMDLINE WL_START
2806#endif
2807#ifdef FEAT_FOLDING
2808# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2809#else
2810# define WL_FOLD WL_CMDLINE
2811#endif
2812#ifdef FEAT_SIGNS
2813# define WL_SIGN WL_FOLD + 1 /* column for signs */
2814#else
2815# define WL_SIGN WL_FOLD /* column for signs */
2816#endif
2817#define WL_NR WL_SIGN + 1 /* line number */
2818#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2819# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2820#else
2821# define WL_SBR WL_NR
2822#endif
2823#define WL_LINE WL_SBR + 1 /* text in the line */
2824 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002825#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 int feedback_col = 0;
2827 int feedback_old_attr = -1;
2828#endif
2829
Bram Moolenaar860cae12010-06-05 23:22:07 +02002830#ifdef FEAT_CONCEAL
2831 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002832 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002833 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002834 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002835 int is_concealing = FALSE;
2836 int boguscols = 0; /* nonexistent columns added to force
2837 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002838 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002839 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002840# define VCOL_HLC (vcol - vcol_off)
2841#else
2842# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
2845 if (startrow > endrow) /* past the end already! */
2846 return startrow;
2847
2848 row = startrow;
2849 screen_row = row + W_WINROW(wp);
2850
2851 /*
2852 * To speed up the loop below, set extra_check when there is linebreak,
2853 * trailing white space and/or syntax processing to be done.
2854 */
2855#ifdef FEAT_LINEBREAK
2856 extra_check = wp->w_p_lbr;
2857#else
2858 extra_check = 0;
2859#endif
2860#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002861 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
2863 /* Prepare for syntax highlighting in this line. When there is an
2864 * error, stop syntax highlighting. */
2865 save_did_emsg = did_emsg;
2866 did_emsg = FALSE;
2867 syntax_start(wp, lnum);
2868 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002869 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 else
2871 {
2872 did_emsg = save_did_emsg;
2873 has_syntax = TRUE;
2874 extra_check = TRUE;
2875 }
2876 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002877
2878 /* Check for columns to display for 'colorcolumn'. */
2879 color_cols = wp->w_p_cc_cols;
2880 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002881 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002882#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002883
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002884#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002885 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002886 && *wp->w_s->b_p_spl != NUL
2887 && wp->w_s->b_langp.ga_len > 0
2888 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002889 {
2890 /* Prepare for spell checking. */
2891 has_spell = TRUE;
2892 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002893
2894 /* Get the start of the next line, so that words that wrap to the next
2895 * line are found too: "et<line-break>al.".
2896 * Trick: skip a few chars for C/shell/Vim comments */
2897 nextline[SPWORDLEN] = NUL;
2898 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2899 {
2900 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2901 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2902 }
2903
2904 /* When a word wrapped from the previous line the start of the current
2905 * line is valid. */
2906 if (lnum == checked_lnum)
2907 cur_checked_col = checked_col;
2908 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002909
2910 /* When there was a sentence end in the previous line may require a
2911 * word starting with capital in this line. In line 1 always check
2912 * the first word. */
2913 if (lnum != capcol_lnum)
2914 cap_col = -1;
2915 if (lnum == 1)
2916 cap_col = 0;
2917 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919#endif
2920
2921 /*
2922 * handle visual active in this window
2923 */
2924 fromcol = -10;
2925 tocol = MAXCOL;
2926#ifdef FEAT_VISUAL
2927 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2928 {
2929 /* Visual is after curwin->w_cursor */
2930 if (ltoreq(curwin->w_cursor, VIsual))
2931 {
2932 top = &curwin->w_cursor;
2933 bot = &VIsual;
2934 }
2935 else /* Visual is before curwin->w_cursor */
2936 {
2937 top = &VIsual;
2938 bot = &curwin->w_cursor;
2939 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002940 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if (VIsual_mode == Ctrl_V) /* block mode */
2942 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002943 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {
2945 fromcol = wp->w_old_cursor_fcol;
2946 tocol = wp->w_old_cursor_lcol;
2947 }
2948 }
2949 else /* non-block mode */
2950 {
2951 if (lnum > top->lnum && lnum <= bot->lnum)
2952 fromcol = 0;
2953 else if (lnum == top->lnum)
2954 {
2955 if (VIsual_mode == 'V') /* linewise */
2956 fromcol = 0;
2957 else
2958 {
2959 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2960 if (gchar_pos(top) == NUL)
2961 tocol = fromcol + 1;
2962 }
2963 }
2964 if (VIsual_mode != 'V' && lnum == bot->lnum)
2965 {
2966 if (*p_sel == 'e' && bot->col == 0
2967#ifdef FEAT_VIRTUALEDIT
2968 && bot->coladd == 0
2969#endif
2970 )
2971 {
2972 fromcol = -10;
2973 tocol = MAXCOL;
2974 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002975 else if (bot->col == MAXCOL)
2976 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 else
2978 {
2979 pos = *bot;
2980 if (*p_sel == 'e')
2981 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2982 else
2983 {
2984 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2985 ++tocol;
2986 }
2987 }
2988 }
2989 }
2990
2991#ifndef MSDOS
2992 /* Check if the character under the cursor should not be inverted */
2993 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2994# ifdef FEAT_GUI
2995 && !gui.in_use
2996# endif
2997 )
2998 noinvcur = TRUE;
2999#endif
3000
3001 /* if inverting in this line set area_highlighting */
3002 if (fromcol >= 0)
3003 {
3004 area_highlighting = TRUE;
3005 attr = hl_attr(HLF_V);
3006#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003007 if ((clip_star.available && !clip_star.owned
3008 && clip_isautosel_star())
3009 || (clip_plus.available && !clip_plus.owned
3010 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 attr = hl_attr(HLF_VNC);
3012#endif
3013 }
3014 }
3015
3016 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003017 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 */
3019 else
3020#endif /* FEAT_VISUAL */
3021 if (highlight_match
3022 && wp == curwin
3023 && lnum >= curwin->w_cursor.lnum
3024 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3025 {
3026 if (lnum == curwin->w_cursor.lnum)
3027 getvcol(curwin, &(curwin->w_cursor),
3028 (colnr_T *)&fromcol, NULL, NULL);
3029 else
3030 fromcol = 0;
3031 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3032 {
3033 pos.lnum = lnum;
3034 pos.col = search_match_endcol;
3035 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3036 }
3037 else
3038 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003039 /* do at least one character; happens when past end of line */
3040 if (fromcol == tocol)
3041 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 area_highlighting = TRUE;
3043 attr = hl_attr(HLF_I);
3044 }
3045
3046#ifdef FEAT_DIFF
3047 filler_lines = diff_check(wp, lnum);
3048 if (filler_lines < 0)
3049 {
3050 if (filler_lines == -1)
3051 {
3052 if (diff_find_change(wp, lnum, &change_start, &change_end))
3053 diff_hlf = HLF_ADD; /* added line */
3054 else if (change_start == 0)
3055 diff_hlf = HLF_TXD; /* changed text */
3056 else
3057 diff_hlf = HLF_CHD; /* changed line */
3058 }
3059 else
3060 diff_hlf = HLF_ADD; /* added line */
3061 filler_lines = 0;
3062 area_highlighting = TRUE;
3063 }
3064 if (lnum == wp->w_topline)
3065 filler_lines = wp->w_topfill;
3066 filler_todo = filler_lines;
3067#endif
3068
3069#ifdef LINE_ATTR
3070# ifdef FEAT_SIGNS
3071 /* If this line has a sign with line highlighting set line_attr. */
3072 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3073 if (v != 0)
3074 line_attr = sign_get_attr((int)v, TRUE);
3075# endif
3076# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3077 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003078 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 line_attr = hl_attr(HLF_L);
3080# endif
3081 if (line_attr != 0)
3082 area_highlighting = TRUE;
3083#endif
3084
3085 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3086 ptr = line;
3087
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003088#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003089 if (has_spell)
3090 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003091 /* For checking first word with a capital skip white space. */
3092 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003093 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003094
Bram Moolenaar30abd282005-06-22 22:35:10 +00003095 /* To be able to spell-check over line boundaries copy the end of the
3096 * current line into nextline[]. Above the start of the next line was
3097 * copied to nextline[SPWORDLEN]. */
3098 if (nextline[SPWORDLEN] == NUL)
3099 {
3100 /* No next line or it is empty. */
3101 nextlinecol = MAXCOL;
3102 nextline_idx = 0;
3103 }
3104 else
3105 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003106 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003107 if (v < SPWORDLEN)
3108 {
3109 /* Short line, use it completely and append the start of the
3110 * next line. */
3111 nextlinecol = 0;
3112 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003113 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003114 nextline_idx = v + 1;
3115 }
3116 else
3117 {
3118 /* Long line, use only the last SPWORDLEN bytes. */
3119 nextlinecol = v - SPWORDLEN;
3120 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3121 nextline_idx = SPWORDLEN + 1;
3122 }
3123 }
3124 }
3125#endif
3126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 /* find start of trailing whitespace */
3128 if (wp->w_p_list && lcs_trail)
3129 {
3130 trailcol = (colnr_T)STRLEN(ptr);
3131 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3132 --trailcol;
3133 trailcol += (colnr_T) (ptr - line);
3134 extra_check = TRUE;
3135 }
3136
3137 /*
3138 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3139 * first character to be displayed.
3140 */
3141 if (wp->w_p_wrap)
3142 v = wp->w_skipcol;
3143 else
3144 v = wp->w_leftcol;
3145 if (v > 0)
3146 {
3147#ifdef FEAT_MBYTE
3148 char_u *prev_ptr = ptr;
3149#endif
3150 while (vcol < v && *ptr != NUL)
3151 {
3152 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3153 vcol += c;
3154#ifdef FEAT_MBYTE
3155 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003157 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
3159
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003160#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3161 /* When:
3162 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003163 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003164 * - 'virtualedit' is set, or
3165 * - the visual mode is active,
3166 * the end of the line may be before the start of the displayed part.
3167 */
3168 if (vcol < v && (
3169# ifdef FEAT_SYN_HL
3170 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003171 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003172# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3173 ||
3174# endif
3175# endif
3176# ifdef FEAT_VIRTUALEDIT
3177 virtual_active()
3178# ifdef FEAT_VISUAL
3179 ||
3180# endif
3181# endif
3182# ifdef FEAT_VISUAL
3183 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3184# endif
3185 ))
3186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189#endif
3190
3191 /* Handle a character that's not completely on the screen: Put ptr at
3192 * that character but skip the first few screen characters. */
3193 if (vcol > v)
3194 {
3195 vcol -= c;
3196#ifdef FEAT_MBYTE
3197 ptr = prev_ptr;
3198#else
3199 --ptr;
3200#endif
3201 n_skip = v - vcol;
3202 }
3203
3204 /*
3205 * Adjust for when the inverted text is before the screen,
3206 * and when the start of the inverted text is before the screen.
3207 */
3208 if (tocol <= vcol)
3209 fromcol = 0;
3210 else if (fromcol >= 0 && fromcol < vcol)
3211 fromcol = vcol;
3212
3213#ifdef FEAT_LINEBREAK
3214 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3215 if (wp->w_p_wrap)
3216 need_showbreak = TRUE;
3217#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003218#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003219 /* When spell checking a word we need to figure out the start of the
3220 * word and if it's badly spelled or not. */
3221 if (has_spell)
3222 {
3223 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003224 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003225 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003226
3227 pos = wp->w_cursor;
3228 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003229 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003230 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003231
3232 /* spell_move_to() may call ml_get() and make "line" invalid */
3233 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3234 ptr = line + linecol;
3235
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003236 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003237 {
3238 /* no bad word found at line start, don't check until end of a
3239 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003240 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003241 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003242 }
3243 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003244 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003245 /* bad word found, use attributes until end of word */
3246 word_end = wp->w_cursor.col + len + 1;
3247
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003248 /* Turn index into actual attributes. */
3249 if (spell_hlf != HLF_COUNT)
3250 spell_attr = highlight_attr[spell_hlf];
3251 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003252 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003253
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003254# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003255 /* Need to restart syntax highlighting for this line. */
3256 if (has_syntax)
3257 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003258# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003259 }
3260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262
3263 /*
3264 * Correct highlighting for cursor that can't be disabled.
3265 * Avoids having to check this for each character.
3266 */
3267 if (fromcol >= 0)
3268 {
3269 if (noinvcur)
3270 {
3271 if ((colnr_T)fromcol == wp->w_virtcol)
3272 {
3273 /* highlighting starts at cursor, let it start just after the
3274 * cursor */
3275 fromcol_prev = fromcol;
3276 fromcol = -1;
3277 }
3278 else if ((colnr_T)fromcol < wp->w_virtcol)
3279 /* restart highlighting after the cursor */
3280 fromcol_prev = wp->w_virtcol;
3281 }
3282 if (fromcol >= tocol)
3283 fromcol = -1;
3284 }
3285
3286#ifdef FEAT_SEARCH_EXTRA
3287 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003288 * Handle highlighting the last used search pattern and matches.
3289 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003291 cur = wp->w_match_head;
3292 shl_flag = FALSE;
3293 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003295 if (shl_flag == FALSE)
3296 {
3297 shl = &search_hl;
3298 shl_flag = TRUE;
3299 }
3300 else
3301 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003302 shl->startcol = MAXCOL;
3303 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 shl->attr_cur = 0;
3305 if (shl->rm.regprog != NULL)
3306 {
3307 v = (long)(ptr - line);
3308 next_search_hl(wp, shl, lnum, (colnr_T)v);
3309
3310 /* Need to get the line again, a multi-line regexp may have made it
3311 * invalid. */
3312 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3313 ptr = line + v;
3314
3315 if (shl->lnum != 0 && shl->lnum <= lnum)
3316 {
3317 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003318 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003320 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3322 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003323 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003325 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003327 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
3329#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003330 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003331 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
3333#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003334 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003336 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
3338 shl->attr_cur = shl->attr;
3339 search_attr = shl->attr;
3340 }
3341 area_highlighting = TRUE;
3342 }
3343 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003344 if (shl != &search_hl && cur != NULL)
3345 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
3347#endif
3348
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003349#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003350 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3351 * active, because it's not clear what is selected then. */
3352 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003353 {
3354 line_attr = hl_attr(HLF_CUL);
3355 area_highlighting = TRUE;
3356 }
3357#endif
3358
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003359 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 col = 0;
3361#ifdef FEAT_RIGHTLEFT
3362 if (wp->w_p_rl)
3363 {
3364 /* Rightleft window: process the text in the normal direction, but put
3365 * it in current_ScreenLine[] from right to left. Start at the
3366 * rightmost column of the window. */
3367 col = W_WIDTH(wp) - 1;
3368 off += col;
3369 }
3370#endif
3371
3372 /*
3373 * Repeat for the whole displayed line.
3374 */
3375 for (;;)
3376 {
3377 /* Skip this quickly when working on the text. */
3378 if (draw_state != WL_LINE)
3379 {
3380#ifdef FEAT_CMDWIN
3381 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3382 {
3383 draw_state = WL_CMDLINE;
3384 if (cmdwin_type != 0 && wp == curwin)
3385 {
3386 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003388 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 char_attr = hl_attr(HLF_AT);
3390 }
3391 }
3392#endif
3393
3394#ifdef FEAT_FOLDING
3395 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3396 {
3397 draw_state = WL_FOLD;
3398 if (wp->w_p_fdc > 0)
3399 {
3400 /* Draw the 'foldcolumn'. */
3401 fill_foldcolumn(extra, wp, FALSE, lnum);
3402 n_extra = wp->w_p_fdc;
3403 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003404 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 c_extra = NUL;
3406 char_attr = hl_attr(HLF_FC);
3407 }
3408 }
3409#endif
3410
3411#ifdef FEAT_SIGNS
3412 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3413 {
3414 draw_state = WL_SIGN;
3415 /* Show the sign column when there are any signs in this
3416 * buffer or when using Netbeans. */
3417 if (draw_signcolumn(wp)
3418# ifdef FEAT_DIFF
3419 && filler_todo <= 0
3420# endif
3421 )
3422 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003423 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003425 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426# endif
3427
3428 /* Draw two cells with the sign value or blank. */
3429 c_extra = ' ';
3430 char_attr = hl_attr(HLF_SC);
3431 n_extra = 2;
3432
3433 if (row == startrow)
3434 {
3435 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3436 SIGN_TEXT);
3437# ifdef FEAT_SIGN_ICONS
3438 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3439 SIGN_ICON);
3440 if (gui.in_use && icon_sign != 0)
3441 {
3442 /* Use the image in this position. */
3443 c_extra = SIGN_BYTE;
3444# ifdef FEAT_NETBEANS_INTG
3445 if (buf_signcount(wp->w_buffer, lnum) > 1)
3446 c_extra = MULTISIGN_BYTE;
3447# endif
3448 char_attr = icon_sign;
3449 }
3450 else
3451# endif
3452 if (text_sign != 0)
3453 {
3454 p_extra = sign_get_text(text_sign);
3455 if (p_extra != NULL)
3456 {
3457 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003458 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 char_attr = sign_get_attr(text_sign, FALSE);
3461 }
3462 }
3463 }
3464 }
3465#endif
3466
3467 if (draw_state == WL_NR - 1 && n_extra == 0)
3468 {
3469 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003470 /* Display the absolute or relative line number. After the
3471 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3472 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 && (row == startrow
3474#ifdef FEAT_DIFF
3475 + filler_lines
3476#endif
3477 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3478 {
3479 /* Draw the line number (empty space after wrapping). */
3480 if (row == startrow
3481#ifdef FEAT_DIFF
3482 + filler_lines
3483#endif
3484 )
3485 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003486 long num;
3487
3488 if (wp->w_p_nu)
3489 /* 'number' */
3490 num = (long)lnum;
3491 else
3492 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003493 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02003494
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003495 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003496 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (wp->w_skipcol > 0)
3498 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3499 *p_extra = '-';
3500#ifdef FEAT_RIGHTLEFT
3501 if (wp->w_p_rl) /* reverse line numbers */
3502 rl_mirror(extra);
3503#endif
3504 p_extra = extra;
3505 c_extra = NUL;
3506 }
3507 else
3508 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003509 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003511#ifdef FEAT_SYN_HL
3512 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003513 * the current line differently.
3514 * TODO: Can we use CursorLine instead of CursorLineNr
3515 * when CursorLineNr isn't set? */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003516 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003517 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520 }
3521
3522#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3523 if (draw_state == WL_SBR - 1 && n_extra == 0)
3524 {
3525 draw_state = WL_SBR;
3526# ifdef FEAT_DIFF
3527 if (filler_todo > 0)
3528 {
3529 /* Draw "deleted" diff line(s). */
3530 if (char2cells(fill_diff) > 1)
3531 c_extra = '-';
3532 else
3533 c_extra = fill_diff;
3534# ifdef FEAT_RIGHTLEFT
3535 if (wp->w_p_rl)
3536 n_extra = col + 1;
3537 else
3538# endif
3539 n_extra = W_WIDTH(wp) - col;
3540 char_attr = hl_attr(HLF_DED);
3541 }
3542# endif
3543# ifdef FEAT_LINEBREAK
3544 if (*p_sbr != NUL && need_showbreak)
3545 {
3546 /* Draw 'showbreak' at the start of each broken line. */
3547 p_extra = p_sbr;
3548 c_extra = NUL;
3549 n_extra = (int)STRLEN(p_sbr);
3550 char_attr = hl_attr(HLF_AT);
3551 need_showbreak = FALSE;
3552 /* Correct end of highlighted area for 'showbreak',
3553 * required when 'linebreak' is also set. */
3554 if (tocol == vcol)
3555 tocol += n_extra;
3556 }
3557# endif
3558 }
3559#endif
3560
3561 if (draw_state == WL_LINE - 1 && n_extra == 0)
3562 {
3563 draw_state = WL_LINE;
3564 if (saved_n_extra)
3565 {
3566 /* Continue item from end of wrapped line. */
3567 n_extra = saved_n_extra;
3568 c_extra = saved_c_extra;
3569 p_extra = saved_p_extra;
3570 char_attr = saved_char_attr;
3571 }
3572 else
3573 char_attr = 0;
3574 }
3575 }
3576
3577 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003578 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003579 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580#ifdef FEAT_DIFF
3581 && filler_todo <= 0
3582#endif
3583 )
3584 {
3585 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3586 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003587 /* Pretend we have finished updating the window. Except when
3588 * 'cursorcolumn' is set. */
3589#ifdef FEAT_SYN_HL
3590 if (wp->w_p_cuc)
3591 row = wp->w_cline_row + wp->w_cline_height;
3592 else
3593#endif
3594 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 break;
3596 }
3597
3598 if (draw_state == WL_LINE && area_highlighting)
3599 {
3600 /* handle Visual or match highlighting in this line */
3601 if (vcol == fromcol
3602#ifdef FEAT_MBYTE
3603 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3604 && (*mb_ptr2cells)(ptr) > 1)
3605#endif
3606 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003607 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 && vcol < tocol))
3609 area_attr = attr; /* start highlighting */
3610 else if (area_attr != 0
3611 && (vcol == tocol
3612 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
3615#ifdef FEAT_SEARCH_EXTRA
3616 if (!n_extra)
3617 {
3618 /*
3619 * Check for start/end of search pattern match.
3620 * After end, check for start/end of next match.
3621 * When another match, have to check for start again.
3622 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003623 * Do this for 'search_hl' and the match list (ordered by
3624 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003626 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003627 cur = wp->w_match_head;
3628 shl_flag = FALSE;
3629 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003631 if (shl_flag == FALSE
3632 && ((cur != NULL
3633 && cur->priority > SEARCH_HL_PRIORITY)
3634 || cur == NULL))
3635 {
3636 shl = &search_hl;
3637 shl_flag = TRUE;
3638 }
3639 else
3640 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 while (shl->rm.regprog != NULL)
3642 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003643 if (shl->startcol != MAXCOL
3644 && v >= (long)shl->startcol
3645 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
3647 shl->attr_cur = shl->attr;
3648 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003649 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
3651 shl->attr_cur = 0;
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 next_search_hl(wp, shl, lnum, (colnr_T)v);
3654
3655 /* Need to get the line again, a multi-line regexp
3656 * may have made it invalid. */
3657 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3658 ptr = line + v;
3659
3660 if (shl->lnum == lnum)
3661 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003662 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003664 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003666 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003668 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 {
3670 /* highlight empty match, try again after
3671 * it */
3672#ifdef FEAT_MBYTE
3673 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003674 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003675 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 else
3677#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003678 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680
3681 /* Loop to check if the match starts at the
3682 * current position */
3683 continue;
3684 }
3685 }
3686 break;
3687 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003688 if (shl != &search_hl && cur != NULL)
3689 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003691
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003692 /* Use attributes from match with highest priority among
3693 * 'search_hl' and the match list. */
3694 search_attr = search_hl.attr_cur;
3695 cur = wp->w_match_head;
3696 shl_flag = FALSE;
3697 while (cur != NULL || shl_flag == FALSE)
3698 {
3699 if (shl_flag == FALSE
3700 && ((cur != NULL
3701 && cur->priority > SEARCH_HL_PRIORITY)
3702 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003703 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003704 shl = &search_hl;
3705 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003706 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003707 else
3708 shl = &cur->hl;
3709 if (shl->attr_cur != 0)
3710 search_attr = shl->attr_cur;
3711 if (shl != &search_hl && cur != NULL)
3712 cur = cur->next;
3713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715#endif
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003718 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003720 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3721 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003723 if (diff_hlf == HLF_TXD && ptr - line > change_end
3724 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003726 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
3728#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003729
3730 /* Decide which of the highlight attributes to use. */
3731 attr_pri = TRUE;
3732 if (area_attr != 0)
3733 char_attr = area_attr;
3734 else if (search_attr != 0)
3735 char_attr = search_attr;
3736#ifdef LINE_ATTR
3737 /* Use line_attr when not in the Visual or 'incsearch' area
3738 * (area_attr may be 0 when "noinvcur" is set). */
3739 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003740 || vcol < fromcol || vcol_prev < fromcol_prev
3741 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003742 char_attr = line_attr;
3743#endif
3744 else
3745 {
3746 attr_pri = FALSE;
3747#ifdef FEAT_SYN_HL
3748 if (has_syntax)
3749 char_attr = syntax_attr;
3750 else
3751#endif
3752 char_attr = 0;
3753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755
3756 /*
3757 * Get the next character to put on the screen.
3758 */
3759 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003760 * The "p_extra" points to the extra stuff that is inserted to
3761 * represent special characters (non-printable stuff) and other
3762 * things. When all characters are the same, c_extra is used.
3763 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3764 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3766 */
3767 if (n_extra > 0)
3768 {
3769 if (c_extra != NUL)
3770 {
3771 c = c_extra;
3772#ifdef FEAT_MBYTE
3773 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3774 if (enc_utf8 && (*mb_char2len)(c) > 1)
3775 {
3776 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003777 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003778 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 else
3781 mb_utf8 = FALSE;
3782#endif
3783 }
3784 else
3785 {
3786 c = *p_extra;
3787#ifdef FEAT_MBYTE
3788 if (has_mbyte)
3789 {
3790 mb_c = c;
3791 if (enc_utf8)
3792 {
3793 /* If the UTF-8 character is more than one byte:
3794 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003795 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 mb_utf8 = FALSE;
3797 if (mb_l > n_extra)
3798 mb_l = 1;
3799 else if (mb_l > 1)
3800 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003801 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003803 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 }
3806 else
3807 {
3808 /* if this is a DBCS character, put it in "mb_c" */
3809 mb_l = MB_BYTE2LEN(c);
3810 if (mb_l >= n_extra)
3811 mb_l = 1;
3812 else if (mb_l > 1)
3813 mb_c = (c << 8) + p_extra[1];
3814 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003815 if (mb_l == 0) /* at the NUL at end-of-line */
3816 mb_l = 1;
3817
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 /* If a double-width char doesn't fit display a '>' in the
3819 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003820 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821# ifdef FEAT_RIGHTLEFT
3822 wp->w_p_rl ? (col <= 0) :
3823# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003824 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 && (*mb_char2cells)(mb_c) == 2)
3826 {
3827 c = '>';
3828 mb_c = c;
3829 mb_l = 1;
3830 mb_utf8 = FALSE;
3831 multi_attr = hl_attr(HLF_AT);
3832 /* put the pointer back to output the double-width
3833 * character at the start of the next line. */
3834 ++n_extra;
3835 --p_extra;
3836 }
3837 else
3838 {
3839 n_extra -= mb_l - 1;
3840 p_extra += mb_l - 1;
3841 }
3842 }
3843#endif
3844 ++p_extra;
3845 }
3846 --n_extra;
3847 }
3848 else
3849 {
3850 /*
3851 * Get a character from the line itself.
3852 */
3853 c = *ptr;
3854#ifdef FEAT_MBYTE
3855 if (has_mbyte)
3856 {
3857 mb_c = c;
3858 if (enc_utf8)
3859 {
3860 /* If the UTF-8 character is more than one byte: Decode it
3861 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003862 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 mb_utf8 = FALSE;
3864 if (mb_l > 1)
3865 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003866 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 /* Overlong encoded ASCII or ASCII with composing char
3868 * is displayed normally, except a NUL. */
3869 if (mb_c < 0x80)
3870 c = mb_c;
3871 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003872
3873 /* At start of the line we can have a composing char.
3874 * Draw it as a space with a composing char. */
3875 if (utf_iscomposing(mb_c))
3876 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003877 int i;
3878
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003879 for (i = Screen_mco - 1; i > 0; --i)
3880 u8cc[i] = u8cc[i - 1];
3881 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003882 mb_c = ' ';
3883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885
3886 if ((mb_l == 1 && c >= 0x80)
3887 || (mb_l >= 1 && mb_c == 0)
3888 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003889# ifdef UNICODE16
3890 || mb_c >= 0x10000
3891# endif
3892 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
3894 /*
3895 * Illegal UTF-8 byte: display as <xx>.
3896 * Non-BMP character : display as ? or fullwidth ?.
3897 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003898# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
3902 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003903# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if (wp->w_p_rl) /* reverse */
3905 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003908# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 else if (utf_char2cells(mb_c) != 2)
3910 STRCPY(extra, "?");
3911 else
3912 /* 0xff1f in UTF-8: full-width '?' */
3913 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916 p_extra = extra;
3917 c = *p_extra;
3918 mb_c = mb_ptr2char_adv(&p_extra);
3919 mb_utf8 = (c >= 0x80);
3920 n_extra = (int)STRLEN(p_extra);
3921 c_extra = NUL;
3922 if (area_attr == 0 && search_attr == 0)
3923 {
3924 n_attr = n_extra + 1;
3925 extra_attr = hl_attr(HLF_8);
3926 saved_attr2 = char_attr; /* save current attr */
3927 }
3928 }
3929 else if (mb_l == 0) /* at the NUL at end-of-line */
3930 mb_l = 1;
3931#ifdef FEAT_ARABIC
3932 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3933 {
3934 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003935 int pc, pc1, nc;
3936 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 /* The idea of what is the previous and next
3939 * character depends on 'rightleft'. */
3940 if (wp->w_p_rl)
3941 {
3942 pc = prev_c;
3943 pc1 = prev_c1;
3944 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003945 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947 else
3948 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003949 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003951 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953 prev_c = mb_c;
3954
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003955 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
3957 else
3958 prev_c = mb_c;
3959#endif
3960 }
3961 else /* enc_dbcs */
3962 {
3963 mb_l = MB_BYTE2LEN(c);
3964 if (mb_l == 0) /* at the NUL at end-of-line */
3965 mb_l = 1;
3966 else if (mb_l > 1)
3967 {
3968 /* We assume a second byte below 32 is illegal.
3969 * Hopefully this is OK for all double-byte encodings!
3970 */
3971 if (ptr[1] >= 32)
3972 mb_c = (c << 8) + ptr[1];
3973 else
3974 {
3975 if (ptr[1] == NUL)
3976 {
3977 /* head byte at end of line */
3978 mb_l = 1;
3979 transchar_nonprint(extra, c);
3980 }
3981 else
3982 {
3983 /* illegal tail byte */
3984 mb_l = 2;
3985 STRCPY(extra, "XX");
3986 }
3987 p_extra = extra;
3988 n_extra = (int)STRLEN(extra) - 1;
3989 c_extra = NUL;
3990 c = *p_extra++;
3991 if (area_attr == 0 && search_attr == 0)
3992 {
3993 n_attr = n_extra + 1;
3994 extra_attr = hl_attr(HLF_8);
3995 saved_attr2 = char_attr; /* save current attr */
3996 }
3997 mb_c = c;
3998 }
3999 }
4000 }
4001 /* If a double-width char doesn't fit display a '>' in the
4002 * last column; the character is displayed at the start of the
4003 * next line. */
4004 if ((
4005# ifdef FEAT_RIGHTLEFT
4006 wp->w_p_rl ? (col <= 0) :
4007# endif
4008 (col >= W_WIDTH(wp) - 1))
4009 && (*mb_char2cells)(mb_c) == 2)
4010 {
4011 c = '>';
4012 mb_c = c;
4013 mb_utf8 = FALSE;
4014 mb_l = 1;
4015 multi_attr = hl_attr(HLF_AT);
4016 /* Put pointer back so that the character will be
4017 * displayed at the start of the next line. */
4018 --ptr;
4019 }
4020 else if (*ptr != NUL)
4021 ptr += mb_l - 1;
4022
4023 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004024 * a '<' in the first column. Don't do this for unprintable
4025 * charactes. */
4026 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004029 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 c = ' ';
4031 if (area_attr == 0 && search_attr == 0)
4032 {
4033 n_attr = n_extra + 1;
4034 extra_attr = hl_attr(HLF_AT);
4035 saved_attr2 = char_attr; /* save current attr */
4036 }
4037 mb_c = c;
4038 mb_utf8 = FALSE;
4039 mb_l = 1;
4040 }
4041
4042 }
4043#endif
4044 ++ptr;
4045
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004046 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004047 if (wp->w_p_list && (c == 160
4048#ifdef FEAT_MBYTE
4049 || (mb_utf8 && mb_c == 160)
4050#endif
4051 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004052 {
4053 c = lcs_nbsp;
4054 if (area_attr == 0 && search_attr == 0)
4055 {
4056 n_attr = 1;
4057 extra_attr = hl_attr(HLF_8);
4058 saved_attr2 = char_attr; /* save current attr */
4059 }
4060#ifdef FEAT_MBYTE
4061 mb_c = c;
4062 if (enc_utf8 && (*mb_char2len)(c) > 1)
4063 {
4064 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004065 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004067 }
4068 else
4069 mb_utf8 = FALSE;
4070#endif
4071 }
4072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 if (extra_check)
4074 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004075#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004076 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004077#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004078
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004079#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 /* Get syntax attribute, unless still at the start of the line
4081 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004082 v = (long)(ptr - line);
4083 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
4085 /* Get the syntax attribute for the character. If there
4086 * is an error, disable syntax highlighting. */
4087 save_did_emsg = did_emsg;
4088 did_emsg = FALSE;
4089
Bram Moolenaar217ad922005-03-20 22:37:15 +00004090 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004091# ifdef FEAT_SPELL
4092 has_spell ? &can_spell :
4093# endif
4094 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004097 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004098 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004099 has_syntax = FALSE;
4100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 else
4102 did_emsg = save_did_emsg;
4103
4104 /* Need to get the line again, a multi-line regexp may
4105 * have made it invalid. */
4106 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4107 ptr = line + v;
4108
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004109 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004111 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004112 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004113# ifdef FEAT_CONCEAL
4114 /* no concealing past the end of the line, it interferes
4115 * with line highlighting */
4116 if (c == NUL)
4117 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004118 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004119 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004120# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004122#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004123
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004124#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004125 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004126 * Only do this when there is no syntax highlighting, the
4127 * @Spell cluster is not used or the current syntax item
4128 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004129 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004130 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004131 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004132# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004133 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004134 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004135# endif
4136 if (c != 0 && (
4137# ifdef FEAT_SYN_HL
4138 !has_syntax ||
4139# endif
4140 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004141 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004142 char_u *prev_ptr, *p;
4143 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004144 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004145# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004146 if (has_mbyte)
4147 {
4148 prev_ptr = ptr - mb_l;
4149 v -= mb_l - 1;
4150 }
4151 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004152# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004153 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004154
4155 /* Use nextline[] if possible, it has the start of the
4156 * next line concatenated. */
4157 if ((prev_ptr - line) - nextlinecol >= 0)
4158 p = nextline + (prev_ptr - line) - nextlinecol;
4159 else
4160 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004161 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004162 len = spell_check(wp, p, &spell_hlf, &cap_col,
4163 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004164 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004165
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004166 /* In Insert mode only highlight a word that
4167 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004168 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004169 && (State & INSERT) != 0
4170 && wp->w_cursor.lnum == lnum
4171 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004172 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004173 && wp->w_cursor.col < (colnr_T)word_end)
4174 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004175 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004176 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004177 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004178
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004179 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004180 && (p - nextline) + len > nextline_idx)
4181 {
4182 /* Remember that the good word continues at the
4183 * start of the next line. */
4184 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004185 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004186 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004187
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004188 /* Turn index into actual attributes. */
4189 if (spell_hlf != HLF_COUNT)
4190 spell_attr = highlight_attr[spell_hlf];
4191
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004192 if (cap_col > 0)
4193 {
4194 if (p != prev_ptr
4195 && (p - nextline) + cap_col >= nextline_idx)
4196 {
4197 /* Remember that the word in the next line
4198 * must start with a capital. */
4199 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004200 cap_col = (int)((p - nextline) + cap_col
4201 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004202 }
4203 else
4204 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004205 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004206 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004207 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004208 }
4209 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004210 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004211 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004212 char_attr = hl_combine_attr(char_attr, spell_attr);
4213 else
4214 char_attr = hl_combine_attr(spell_attr, char_attr);
4215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216#endif
4217#ifdef FEAT_LINEBREAK
4218 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004219 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 */
4221 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004222 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
4224 n_extra = win_lbr_chartabsize(wp, ptr - (
4225# ifdef FEAT_MBYTE
4226 has_mbyte ? mb_l :
4227# endif
4228 1), (colnr_T)vcol, NULL) - 1;
4229 c_extra = ' ';
4230 if (vim_iswhite(c))
4231 c = ' ';
4232 }
4233#endif
4234
4235 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4236 {
4237 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004238 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
4240 n_attr = 1;
4241 extra_attr = hl_attr(HLF_8);
4242 saved_attr2 = char_attr; /* save current attr */
4243 }
4244#ifdef FEAT_MBYTE
4245 mb_c = c;
4246 if (enc_utf8 && (*mb_char2len)(c) > 1)
4247 {
4248 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004250 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 else
4253 mb_utf8 = FALSE;
4254#endif
4255 }
4256 }
4257
4258 /*
4259 * Handling of non-printable characters.
4260 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004261 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 {
4263 /*
4264 * when getting a character from the file, we may have to
4265 * turn it into something else on the way to putting it
4266 * into "ScreenLines".
4267 */
4268 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4269 {
4270 /* tab amount depends on current column */
4271 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004272 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4273#ifdef FEAT_CONCEAL
4274 /* Tab alignment should be identical regardless of
4275 * 'conceallevel' value. So tab compensates of all
4276 * previous concealed characters, and thus resets vcol_off
4277 * and boguscols accumulated so far in the line. Note that
4278 * the tab can be longer than 'tabstop' when there
4279 * are concealed characters. */
4280 n_extra += vcol_off;
4281 vcol -= vcol_off;
4282 vcol_off = 0;
4283 col -= boguscols;
4284 boguscols = 0;
4285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#ifdef FEAT_MBYTE
4287 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4288#endif
4289 if (wp->w_p_list)
4290 {
4291 c = lcs_tab1;
4292 c_extra = lcs_tab2;
4293 n_attr = n_extra + 1;
4294 extra_attr = hl_attr(HLF_8);
4295 saved_attr2 = char_attr; /* save current attr */
4296#ifdef FEAT_MBYTE
4297 mb_c = c;
4298 if (enc_utf8 && (*mb_char2len)(c) > 1)
4299 {
4300 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004301 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004302 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304#endif
4305 }
4306 else
4307 {
4308 c_extra = ' ';
4309 c = ' ';
4310 }
4311 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004312 else if (c == NUL
4313 && ((wp->w_p_list && lcs_eol > 0)
4314 || ((fromcol >= 0 || fromcol_prev >= 0)
4315 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004316#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004317 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004318#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004319 && (
4320# ifdef FEAT_RIGHTLEFT
4321 wp->w_p_rl ? (col >= 0) :
4322# endif
4323 (col < W_WIDTH(wp)))
4324 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004325 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004326 && (colnr_T)vcol == wp->w_virtcol)))
4327 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004329 /* Display a '$' after the line or highlight an extra
4330 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4332 /* For a diff line the highlighting continues after the
4333 * "$". */
4334 if (
4335# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004336 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337# ifdef LINE_ATTR
4338 &&
4339# endif
4340# endif
4341# ifdef LINE_ATTR
4342 line_attr == 0
4343# endif
4344 )
4345#endif
4346 {
4347#ifdef FEAT_VIRTUALEDIT
4348 /* In virtualedit, visual selections may extend
4349 * beyond end of line. */
4350 if (area_highlighting && virtual_active()
4351 && tocol != MAXCOL && vcol < tocol)
4352 n_extra = 0;
4353 else
4354#endif
4355 {
4356 p_extra = at_end_str;
4357 n_extra = 1;
4358 c_extra = NUL;
4359 }
4360 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004361 if (wp->w_p_list)
4362 c = lcs_eol;
4363 else
4364 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 lcs_eol_one = -1;
4366 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004367 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
4369 extra_attr = hl_attr(HLF_AT);
4370 n_attr = 1;
4371 }
4372#ifdef FEAT_MBYTE
4373 mb_c = c;
4374 if (enc_utf8 && (*mb_char2len)(c) > 1)
4375 {
4376 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004377 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004378 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 else
4381 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4382#endif
4383 }
4384 else if (c != NUL)
4385 {
4386 p_extra = transchar(c);
4387#ifdef FEAT_RIGHTLEFT
4388 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4389 rl_mirror(p_extra); /* reverse "<12>" */
4390#endif
4391 n_extra = byte2cells(c) - 1;
4392 c_extra = NUL;
4393 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004394 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
4396 n_attr = n_extra + 1;
4397 extra_attr = hl_attr(HLF_8);
4398 saved_attr2 = char_attr; /* save current attr */
4399 }
4400#ifdef FEAT_MBYTE
4401 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4402#endif
4403 }
4404#ifdef FEAT_VIRTUALEDIT
4405 else if (VIsual_active
4406 && (VIsual_mode == Ctrl_V
4407 || VIsual_mode == 'v')
4408 && virtual_active()
4409 && tocol != MAXCOL
4410 && vcol < tocol
4411 && (
4412# ifdef FEAT_RIGHTLEFT
4413 wp->w_p_rl ? (col >= 0) :
4414# endif
4415 (col < W_WIDTH(wp))))
4416 {
4417 c = ' ';
4418 --ptr; /* put it back at the NUL */
4419 }
4420#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004421#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 else if ((
4423# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004424 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 ) && (
4428# ifdef FEAT_RIGHTLEFT
4429 wp->w_p_rl ? (col >= 0) :
4430# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004431 (col
4432# ifdef FEAT_CONCEAL
4433 - boguscols
4434# endif
4435 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
4437 /* Highlight until the right side of the window */
4438 c = ' ';
4439 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004440
4441 /* Remember we do the char for line highlighting. */
4442 ++did_line_attr;
4443
4444 /* don't do search HL for the rest of the line */
4445 if (line_attr != 0 && char_attr == search_attr && col > 0)
4446 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447# ifdef FEAT_DIFF
4448 if (diff_hlf == HLF_TXD)
4449 {
4450 diff_hlf = HLF_CHD;
4451 if (attr == 0 || char_attr != attr)
4452 char_attr = hl_attr(diff_hlf);
4453 }
4454# endif
4455 }
4456#endif
4457 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004458
4459#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004460 if ( wp->w_p_cole > 0
4461 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4462 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004463 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004464 && !(lnum_in_visual_area
4465 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004466 {
4467 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004468 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004469 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4470 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004471 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004472 /* First time at this concealed item: display one
4473 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004474 if (syn_get_sub_char() != NUL)
4475 c = syn_get_sub_char();
4476 else if (lcs_conceal != NUL)
4477 c = lcs_conceal;
4478 else
4479 c = ' ';
4480
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004481 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004482
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004483 if (n_extra > 0)
4484 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004485 vcol += n_extra;
4486 if (wp->w_p_wrap && n_extra > 0)
4487 {
4488# ifdef FEAT_RIGHTLEFT
4489 if (wp->w_p_rl)
4490 {
4491 col -= n_extra;
4492 boguscols -= n_extra;
4493 }
4494 else
4495# endif
4496 {
4497 boguscols += n_extra;
4498 col += n_extra;
4499 }
4500 }
4501 n_extra = 0;
4502 n_attr = 0;
4503 }
4504 else if (n_skip == 0)
4505 {
4506 is_concealing = TRUE;
4507 n_skip = 1;
4508 }
4509# ifdef FEAT_MBYTE
4510 mb_c = c;
4511 if (enc_utf8 && (*mb_char2len)(c) > 1)
4512 {
4513 mb_utf8 = TRUE;
4514 u8cc[0] = 0;
4515 c = 0xc0;
4516 }
4517 else
4518 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4519# endif
4520 }
4521 else
4522 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004523 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004524 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004525 }
4526#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004529#ifdef FEAT_CONCEAL
4530 /* In the cursor line and we may be concealing characters: correct
4531 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004532 if (!did_wcol && draw_state == WL_LINE
4533 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004534 && conceal_cursor_line(wp)
4535 && (int)wp->w_virtcol <= vcol + n_skip)
4536 {
4537 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004538 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004539 did_wcol = TRUE;
4540 }
4541#endif
4542
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 /* Don't override visual selection highlighting. */
4544 if (n_attr > 0
4545 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004546 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 char_attr = extra_attr;
4548
Bram Moolenaar81695252004-12-29 20:58:21 +00004549#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 /* XIM don't send preedit_start and preedit_end, but they send
4551 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4552 * im_is_preediting() here. */
4553 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004554 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 && (State & INSERT)
4556 && !p_imdisable
4557 && im_is_preediting()
4558 && draw_state == WL_LINE)
4559 {
4560 colnr_T tcol;
4561
4562 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004563 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 else
4565 tcol = preedit_end_col;
4566 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4567 {
4568 if (feedback_old_attr < 0)
4569 {
4570 feedback_col = 0;
4571 feedback_old_attr = char_attr;
4572 }
4573 char_attr = im_get_feedback_attr(feedback_col);
4574 if (char_attr < 0)
4575 char_attr = feedback_old_attr;
4576 feedback_col++;
4577 }
4578 else if (feedback_old_attr >= 0)
4579 {
4580 char_attr = feedback_old_attr;
4581 feedback_old_attr = -1;
4582 feedback_col = 0;
4583 }
4584 }
4585#endif
4586 /*
4587 * Handle the case where we are in column 0 but not on the first
4588 * character of the line and the user wants us to show us a
4589 * special character (via 'listchars' option "precedes:<char>".
4590 */
4591 if (lcs_prec_todo != NUL
4592 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4593#ifdef FEAT_DIFF
4594 && filler_todo <= 0
4595#endif
4596 && draw_state > WL_NR
4597 && c != NUL)
4598 {
4599 c = lcs_prec;
4600 lcs_prec_todo = NUL;
4601#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004602 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4603 {
4604 /* Double-width character being overwritten by the "precedes"
4605 * character, need to fill up half the character. */
4606 c_extra = MB_FILLER_CHAR;
4607 n_extra = 1;
4608 n_attr = 2;
4609 extra_attr = hl_attr(HLF_AT);
4610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 mb_c = c;
4612 if (enc_utf8 && (*mb_char2len)(c) > 1)
4613 {
4614 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004615 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004616 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 else
4619 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4620#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004621 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
4623 saved_attr3 = char_attr; /* save current attr */
4624 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4625 n_attr3 = 1;
4626 }
4627 }
4628
4629 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004630 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004632 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004633#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004634 || did_line_attr == 1
4635#endif
4636 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004638#ifdef FEAT_SEARCH_EXTRA
4639 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004640
4641 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004642 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004643 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004644#endif
4645
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004646 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 * highlight match at end of line. If it's beyond the last
4648 * char on the screen, just overwrite that one (tricky!) Not
4649 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004650#ifdef FEAT_SEARCH_EXTRA
4651 prevcol_hl_flag = FALSE;
4652 if (prevcol == (long)search_hl.startcol)
4653 prevcol_hl_flag = TRUE;
4654 else
4655 {
4656 cur = wp->w_match_head;
4657 while (cur != NULL)
4658 {
4659 if (prevcol == (long)cur->hl.startcol)
4660 {
4661 prevcol_hl_flag = TRUE;
4662 break;
4663 }
4664 cur = cur->next;
4665 }
4666 }
4667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004669 && ((area_attr != 0 && vcol == fromcol
4670#ifdef FEAT_VISUAL
4671 && (VIsual_mode != Ctrl_V
4672 || lnum == VIsual.lnum
4673 || lnum == curwin->w_cursor.lnum)
4674#endif
4675 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676#ifdef FEAT_SEARCH_EXTRA
4677 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004678 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004679# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004680 && did_line_attr <= 1
4681# endif
4682 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683#endif
4684 ))
4685 {
4686 int n = 0;
4687
4688#ifdef FEAT_RIGHTLEFT
4689 if (wp->w_p_rl)
4690 {
4691 if (col < 0)
4692 n = 1;
4693 }
4694 else
4695#endif
4696 {
4697 if (col >= W_WIDTH(wp))
4698 n = -1;
4699 }
4700 if (n != 0)
4701 {
4702 /* At the window boundary, highlight the last character
4703 * instead (better than nothing). */
4704 off += n;
4705 col += n;
4706 }
4707 else
4708 {
4709 /* Add a blank character to highlight. */
4710 ScreenLines[off] = ' ';
4711#ifdef FEAT_MBYTE
4712 if (enc_utf8)
4713 ScreenLinesUC[off] = 0;
4714#endif
4715 }
4716#ifdef FEAT_SEARCH_EXTRA
4717 if (area_attr == 0)
4718 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004719 /* Use attributes from match with highest priority among
4720 * 'search_hl' and the match list. */
4721 char_attr = search_hl.attr;
4722 cur = wp->w_match_head;
4723 shl_flag = FALSE;
4724 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004725 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004726 if (shl_flag == FALSE
4727 && ((cur != NULL
4728 && cur->priority > SEARCH_HL_PRIORITY)
4729 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004730 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004731 shl = &search_hl;
4732 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004733 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004734 else
4735 shl = &cur->hl;
4736 if ((ptr - line) - 1 == (long)shl->startcol)
4737 char_attr = shl->attr;
4738 if (shl != &search_hl && cur != NULL)
4739 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
4742#endif
4743 ScreenAttrs[off] = char_attr;
4744#ifdef FEAT_RIGHTLEFT
4745 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004746 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004748 --off;
4749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 else
4751#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004754 ++off;
4755 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004756 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004757#ifdef FEAT_SYN_HL
4758 eol_hl_off = 1;
4759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar91170f82006-05-05 21:15:17 +00004763 /*
4764 * At end of the text line.
4765 */
4766 if (c == NUL)
4767 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004768#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004769 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4770 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004771 {
4772 /* highlight last char after line */
4773 --col;
4774 --off;
4775 --vcol;
4776 }
4777
Bram Moolenaar1a384422010-07-14 19:53:30 +02004778 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004779 if (wp->w_p_wrap)
4780 v = wp->w_skipcol;
4781 else
4782 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004783
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004784 /* check if line ends before left margin */
4785 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004786 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004787#ifdef FEAT_CONCEAL
4788 /* Get rid of the boguscols now, we want to draw until the right
4789 * edge for 'cursorcolumn'. */
4790 col -= boguscols;
4791 boguscols = 0;
4792#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004793
4794 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004795 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004796
4797 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004798 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4799 && (int)wp->w_virtcol <
4800 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004801 && lnum != wp->w_cursor.lnum)
4802 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004803# ifdef FEAT_RIGHTLEFT
4804 && !wp->w_p_rl
4805# endif
4806 )
4807 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004808 int rightmost_vcol = 0;
4809 int i;
4810
4811 if (wp->w_p_cuc)
4812 rightmost_vcol = wp->w_virtcol;
4813 if (draw_color_col)
4814 /* determine rightmost colorcolumn to possibly draw */
4815 for (i = 0; color_cols[i] >= 0; ++i)
4816 if (rightmost_vcol < color_cols[i])
4817 rightmost_vcol = color_cols[i];
4818
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004819 while (col < W_WIDTH(wp))
4820 {
4821 ScreenLines[off] = ' ';
4822#ifdef FEAT_MBYTE
4823 if (enc_utf8)
4824 ScreenLinesUC[off] = 0;
4825#endif
4826 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004827 if (draw_color_col)
4828 draw_color_col = advance_color_col(VCOL_HLC,
4829 &color_cols);
4830
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004831 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004832 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004833 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004834 ScreenAttrs[off++] = hl_attr(HLF_MC);
4835 else
4836 ScreenAttrs[off++] = 0;
4837
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004838 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004839 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004840
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004841 ++vcol;
4842 }
4843 }
4844#endif
4845
Bram Moolenaar860cae12010-06-05 23:22:07 +02004846 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4847 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 row++;
4849
4850 /*
4851 * Update w_cline_height and w_cline_folded if the cursor line was
4852 * updated (saves a call to plines() later).
4853 */
4854 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4855 {
4856 curwin->w_cline_row = startrow;
4857 curwin->w_cline_height = row - startrow;
4858#ifdef FEAT_FOLDING
4859 curwin->w_cline_folded = FALSE;
4860#endif
4861 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4862 }
4863
4864 break;
4865 }
4866
4867 /* line continues beyond line end */
4868 if (lcs_ext
4869 && !wp->w_p_wrap
4870#ifdef FEAT_DIFF
4871 && filler_todo <= 0
4872#endif
4873 && (
4874#ifdef FEAT_RIGHTLEFT
4875 wp->w_p_rl ? col == 0 :
4876#endif
4877 col == W_WIDTH(wp) - 1)
4878 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004879 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4881 {
4882 c = lcs_ext;
4883 char_attr = hl_attr(HLF_AT);
4884#ifdef FEAT_MBYTE
4885 mb_c = c;
4886 if (enc_utf8 && (*mb_char2len)(c) > 1)
4887 {
4888 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004889 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004890 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 else
4893 mb_utf8 = FALSE;
4894#endif
4895 }
4896
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004897#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004898 /* advance to the next 'colorcolumn' */
4899 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004900 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004901
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004902 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004903 * highlight the cursor position itself.
4904 * Also highlight the 'colorcolumn' if it is different than
4905 * 'cursorcolumn' */
4906 vcol_save_attr = -1;
4907 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004908 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004909 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004910 && lnum != wp->w_cursor.lnum)
4911 {
4912 vcol_save_attr = char_attr;
4913 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4914 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004915 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004916 {
4917 vcol_save_attr = char_attr;
4918 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4919 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004920 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004921#endif
4922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 /*
4924 * Store character to be displayed.
4925 * Skip characters that are left of the screen for 'nowrap'.
4926 */
4927 vcol_prev = vcol;
4928 if (draw_state < WL_LINE || n_skip <= 0)
4929 {
4930 /*
4931 * Store the character.
4932 */
4933#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4934 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4935 {
4936 /* A double-wide character is: put first halve in left cell. */
4937 --off;
4938 --col;
4939 }
4940#endif
4941 ScreenLines[off] = c;
4942#ifdef FEAT_MBYTE
4943 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004944 {
4945 if ((mb_c & 0xff00) == 0x8e00)
4946 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 else if (enc_utf8)
4950 {
4951 if (mb_utf8)
4952 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004953 int i;
4954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004956 if ((c & 0xff) == 0)
4957 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004958 for (i = 0; i < Screen_mco; ++i)
4959 {
4960 ScreenLinesC[i][off] = u8cc[i];
4961 if (u8cc[i] == 0)
4962 break;
4963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 else
4966 ScreenLinesUC[off] = 0;
4967 }
4968 if (multi_attr)
4969 {
4970 ScreenAttrs[off] = multi_attr;
4971 multi_attr = 0;
4972 }
4973 else
4974#endif
4975 ScreenAttrs[off] = char_attr;
4976
4977#ifdef FEAT_MBYTE
4978 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4979 {
4980 /* Need to fill two screen columns. */
4981 ++off;
4982 ++col;
4983 if (enc_utf8)
4984 /* UTF-8: Put a 0 in the second screen char. */
4985 ScreenLines[off] = 0;
4986 else
4987 /* DBCS: Put second byte in the second screen char. */
4988 ScreenLines[off] = mb_c & 0xff;
4989 ++vcol;
4990 /* When "tocol" is halfway a character, set it to the end of
4991 * the character, otherwise highlighting won't stop. */
4992 if (tocol == vcol)
4993 ++tocol;
4994#ifdef FEAT_RIGHTLEFT
4995 if (wp->w_p_rl)
4996 {
4997 /* now it's time to backup one cell */
4998 --off;
4999 --col;
5000 }
5001#endif
5002 }
5003#endif
5004#ifdef FEAT_RIGHTLEFT
5005 if (wp->w_p_rl)
5006 {
5007 --off;
5008 --col;
5009 }
5010 else
5011#endif
5012 {
5013 ++off;
5014 ++col;
5015 }
5016 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005017#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005018 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005019 {
5020 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005021 ++vcol_off;
5022 if (n_extra > 0)
5023 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005024 if (wp->w_p_wrap)
5025 {
5026 /*
5027 * Special voodoo required if 'wrap' is on.
5028 *
5029 * Advance the column indicator to force the line
5030 * drawing to wrap early. This will make the line
5031 * take up the same screen space when parts are concealed,
5032 * so that cursor line computations aren't messed up.
5033 *
5034 * To avoid the fictitious advance of 'col' causing
5035 * trailing junk to be written out of the screen line
5036 * we are building, 'boguscols' keeps track of the number
5037 * of bad columns we have advanced.
5038 */
5039 if (n_extra > 0)
5040 {
5041 vcol += n_extra;
5042# ifdef FEAT_RIGHTLEFT
5043 if (wp->w_p_rl)
5044 {
5045 col -= n_extra;
5046 boguscols -= n_extra;
5047 }
5048 else
5049# endif
5050 {
5051 col += n_extra;
5052 boguscols += n_extra;
5053 }
5054 n_extra = 0;
5055 n_attr = 0;
5056 }
5057
5058
5059# ifdef FEAT_MBYTE
5060 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5061 {
5062 /* Need to fill two screen columns. */
5063# ifdef FEAT_RIGHTLEFT
5064 if (wp->w_p_rl)
5065 {
5066 --boguscols;
5067 --col;
5068 }
5069 else
5070# endif
5071 {
5072 ++boguscols;
5073 ++col;
5074 }
5075 }
5076# endif
5077
5078# ifdef FEAT_RIGHTLEFT
5079 if (wp->w_p_rl)
5080 {
5081 --boguscols;
5082 --col;
5083 }
5084 else
5085# endif
5086 {
5087 ++boguscols;
5088 ++col;
5089 }
5090 }
5091 else
5092 {
5093 if (n_extra > 0)
5094 {
5095 vcol += n_extra;
5096 n_extra = 0;
5097 n_attr = 0;
5098 }
5099 }
5100
5101 }
5102#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 else
5104 --n_skip;
5105
Bram Moolenaar64486672010-05-16 15:46:46 +02005106 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5107 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005108 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109#ifdef FEAT_DIFF
5110 && filler_todo <= 0
5111#endif
5112 )
5113 ++vcol;
5114
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005115#ifdef FEAT_SYN_HL
5116 if (vcol_save_attr >= 0)
5117 char_attr = vcol_save_attr;
5118#endif
5119
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 /* restore attributes after "predeces" in 'listchars' */
5121 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5122 char_attr = saved_attr3;
5123
5124 /* restore attributes after last 'listchars' or 'number' char */
5125 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5126 char_attr = saved_attr2;
5127
5128 /*
5129 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005130 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 */
5132 if ((
5133#ifdef FEAT_RIGHTLEFT
5134 wp->w_p_rl ? (col < 0) :
5135#endif
5136 (col >= W_WIDTH(wp)))
5137 && (*ptr != NUL
5138#ifdef FEAT_DIFF
5139 || filler_todo > 0
5140#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005141 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5143 )
5144 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005145#ifdef FEAT_CONCEAL
5146 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5147 (int)W_WIDTH(wp), wp->w_p_rl);
5148 boguscols = 0;
5149#else
5150 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5151 (int)W_WIDTH(wp), wp->w_p_rl);
5152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 ++row;
5154 ++screen_row;
5155
5156 /* When not wrapping and finished diff lines, or when displayed
5157 * '$' and highlighting until last column, break here. */
5158 if ((!wp->w_p_wrap
5159#ifdef FEAT_DIFF
5160 && filler_todo <= 0
5161#endif
5162 ) || lcs_eol_one == -1)
5163 break;
5164
5165 /* When the window is too narrow draw all "@" lines. */
5166 if (draw_state != WL_LINE
5167#ifdef FEAT_DIFF
5168 && filler_todo <= 0
5169#endif
5170 )
5171 {
5172 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5173#ifdef FEAT_VERTSPLIT
5174 draw_vsep_win(wp, row);
5175#endif
5176 row = endrow;
5177 }
5178
5179 /* When line got too long for screen break here. */
5180 if (row == endrow)
5181 {
5182 ++row;
5183 break;
5184 }
5185
5186 if (screen_cur_row == screen_row - 1
5187#ifdef FEAT_DIFF
5188 && filler_todo <= 0
5189#endif
5190 && W_WIDTH(wp) == Columns)
5191 {
5192 /* Remember that the line wraps, used for modeless copy. */
5193 LineWraps[screen_row - 1] = TRUE;
5194
5195 /*
5196 * Special trick to make copy/paste of wrapped lines work with
5197 * xterm/screen: write an extra character beyond the end of
5198 * the line. This will work with all terminal types
5199 * (regardless of the xn,am settings).
5200 * Only do this on a fast tty.
5201 * Only do this if the cursor is on the current line
5202 * (something has been written in it).
5203 * Don't do this for the GUI.
5204 * Don't do this for double-width characters.
5205 * Don't do this for a window not at the right screen border.
5206 */
5207 if (p_tf
5208#ifdef FEAT_GUI
5209 && !gui.in_use
5210#endif
5211#ifdef FEAT_MBYTE
5212 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005213 && ((*mb_off2cells)(LineOffset[screen_row],
5214 LineOffset[screen_row] + screen_Columns)
5215 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005217 + (int)Columns - 2,
5218 LineOffset[screen_row] + screen_Columns)
5219 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#endif
5221 )
5222 {
5223 /* First make sure we are at the end of the screen line,
5224 * then output the same character again to let the
5225 * terminal know about the wrap. If the terminal doesn't
5226 * auto-wrap, we overwrite the character. */
5227 if (screen_cur_col != W_WIDTH(wp))
5228 screen_char(LineOffset[screen_row - 1]
5229 + (unsigned)Columns - 1,
5230 screen_row - 1, (int)(Columns - 1));
5231
5232#ifdef FEAT_MBYTE
5233 /* When there is a multi-byte character, just output a
5234 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005235 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5236 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 out_char(' ');
5238 else
5239#endif
5240 out_char(ScreenLines[LineOffset[screen_row - 1]
5241 + (Columns - 1)]);
5242 /* force a redraw of the first char on the next line */
5243 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5244 screen_start(); /* don't know where cursor is now */
5245 }
5246 }
5247
5248 col = 0;
5249 off = (unsigned)(current_ScreenLine - ScreenLines);
5250#ifdef FEAT_RIGHTLEFT
5251 if (wp->w_p_rl)
5252 {
5253 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5254 off += col;
5255 }
5256#endif
5257
5258 /* reset the drawing state for the start of a wrapped line */
5259 draw_state = WL_START;
5260 saved_n_extra = n_extra;
5261 saved_p_extra = p_extra;
5262 saved_c_extra = c_extra;
5263 saved_char_attr = char_attr;
5264 n_extra = 0;
5265 lcs_prec_todo = lcs_prec;
5266#ifdef FEAT_LINEBREAK
5267# ifdef FEAT_DIFF
5268 if (filler_todo <= 0)
5269# endif
5270 need_showbreak = TRUE;
5271#endif
5272#ifdef FEAT_DIFF
5273 --filler_todo;
5274 /* When the filler lines are actually below the last line of the
5275 * file, don't draw the line itself, break here. */
5276 if (filler_todo == 0 && wp->w_botfill)
5277 break;
5278#endif
5279 }
5280
5281 } /* for every character in the line */
5282
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005283#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005284 /* After an empty line check first word for capital. */
5285 if (*skipwhite(line) == NUL)
5286 {
5287 capcol_lnum = lnum + 1;
5288 cap_col = 0;
5289 }
5290#endif
5291
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 return row;
5293}
5294
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005295#ifdef FEAT_MBYTE
5296static int comp_char_differs __ARGS((int, int));
5297
5298/*
5299 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005300 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005301 */
5302 static int
5303comp_char_differs(off_from, off_to)
5304 int off_from;
5305 int off_to;
5306{
5307 int i;
5308
5309 for (i = 0; i < Screen_mco; ++i)
5310 {
5311 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5312 return TRUE;
5313 if (ScreenLinesC[i][off_from] == 0)
5314 break;
5315 }
5316 return FALSE;
5317}
5318#endif
5319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320/*
5321 * Check whether the given character needs redrawing:
5322 * - the (first byte of the) character is different
5323 * - the attributes are different
5324 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005325 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 */
5327 static int
5328char_needs_redraw(off_from, off_to, cols)
5329 int off_from;
5330 int off_to;
5331 int cols;
5332{
5333 if (cols > 0
5334 && ((ScreenLines[off_from] != ScreenLines[off_to]
5335 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5336
5337#ifdef FEAT_MBYTE
5338 || (enc_dbcs != 0
5339 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5340 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5341 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5342 : (cols > 1 && ScreenLines[off_from + 1]
5343 != ScreenLines[off_to + 1])))
5344 || (enc_utf8
5345 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5346 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005347 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005348 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5349 && ScreenLines[off_from + 1]
5350 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351#endif
5352 ))
5353 return TRUE;
5354 return FALSE;
5355}
5356
5357/*
5358 * Move one "cooked" screen line to the screen, but only the characters that
5359 * have actually changed. Handle insert/delete character.
5360 * "coloff" gives the first column on the screen for this line.
5361 * "endcol" gives the columns where valid characters are.
5362 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5363 * needs to be cleared, negative otherwise.
5364 * "rlflag" is TRUE in a rightleft window:
5365 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5366 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5367 */
5368 static void
5369screen_line(row, coloff, endcol, clear_width
5370#ifdef FEAT_RIGHTLEFT
5371 , rlflag
5372#endif
5373 )
5374 int row;
5375 int coloff;
5376 int endcol;
5377 int clear_width;
5378#ifdef FEAT_RIGHTLEFT
5379 int rlflag;
5380#endif
5381{
5382 unsigned off_from;
5383 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005384#ifdef FEAT_MBYTE
5385 unsigned max_off_from;
5386 unsigned max_off_to;
5387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 int col = 0;
5389#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5390 int hl;
5391#endif
5392 int force = FALSE; /* force update rest of the line */
5393 int redraw_this /* bool: does character need redraw? */
5394#ifdef FEAT_GUI
5395 = TRUE /* For GUI when while-loop empty */
5396#endif
5397 ;
5398 int redraw_next; /* redraw_this for next character */
5399#ifdef FEAT_MBYTE
5400 int clear_next = FALSE;
5401 int char_cells; /* 1: normal char */
5402 /* 2: occupies two display cells */
5403# define CHAR_CELLS char_cells
5404#else
5405# define CHAR_CELLS 1
5406#endif
5407
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005408 /* Check for illegal row and col, just in case. */
5409 if (row >= Rows)
5410 row = Rows - 1;
5411 if (endcol > Columns)
5412 endcol = Columns;
5413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414# ifdef FEAT_CLIPBOARD
5415 clip_may_clear_selection(row, row);
5416# endif
5417
5418 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5419 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005420#ifdef FEAT_MBYTE
5421 max_off_from = off_from + screen_Columns;
5422 max_off_to = LineOffset[row] + screen_Columns;
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
5425#ifdef FEAT_RIGHTLEFT
5426 if (rlflag)
5427 {
5428 /* Clear rest first, because it's left of the text. */
5429 if (clear_width > 0)
5430 {
5431 while (col <= endcol && ScreenLines[off_to] == ' '
5432 && ScreenAttrs[off_to] == 0
5433# ifdef FEAT_MBYTE
5434 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5435# endif
5436 )
5437 {
5438 ++off_to;
5439 ++col;
5440 }
5441 if (col <= endcol)
5442 screen_fill(row, row + 1, col + coloff,
5443 endcol + coloff + 1, ' ', ' ', 0);
5444 }
5445 col = endcol + 1;
5446 off_to = LineOffset[row] + col + coloff;
5447 off_from += col;
5448 endcol = (clear_width > 0 ? clear_width : -clear_width);
5449 }
5450#endif /* FEAT_RIGHTLEFT */
5451
5452 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5453
5454 while (col < endcol)
5455 {
5456#ifdef FEAT_MBYTE
5457 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005458 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 else
5460 char_cells = 1;
5461#endif
5462
5463 redraw_this = redraw_next;
5464 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5465 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5466
5467#ifdef FEAT_GUI
5468 /* If the next character was bold, then redraw the current character to
5469 * remove any pixels that might have spilt over into us. This only
5470 * happens in the GUI.
5471 */
5472 if (redraw_next && gui.in_use)
5473 {
5474 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005475 if (hl > HL_ALL)
5476 hl = syn_attr2attr(hl);
5477 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 redraw_this = TRUE;
5479 }
5480#endif
5481
5482 if (redraw_this)
5483 {
5484 /*
5485 * Special handling when 'xs' termcap flag set (hpterm):
5486 * Attributes for characters are stored at the position where the
5487 * cursor is when writing the highlighting code. The
5488 * start-highlighting code must be written with the cursor on the
5489 * first highlighted character. The stop-highlighting code must
5490 * be written with the cursor just after the last highlighted
5491 * character.
5492 * Overwriting a character doesn't remove it's highlighting. Need
5493 * to clear the rest of the line, and force redrawing it
5494 * completely.
5495 */
5496 if ( p_wiv
5497 && !force
5498#ifdef FEAT_GUI
5499 && !gui.in_use
5500#endif
5501 && ScreenAttrs[off_to] != 0
5502 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5503 {
5504 /*
5505 * Need to remove highlighting attributes here.
5506 */
5507 windgoto(row, col + coloff);
5508 out_str(T_CE); /* clear rest of this screen line */
5509 screen_start(); /* don't know where cursor is now */
5510 force = TRUE; /* force redraw of rest of the line */
5511 redraw_next = TRUE; /* or else next char would miss out */
5512
5513 /*
5514 * If the previous character was highlighted, need to stop
5515 * highlighting at this character.
5516 */
5517 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5518 {
5519 screen_attr = ScreenAttrs[off_to - 1];
5520 term_windgoto(row, col + coloff);
5521 screen_stop_highlight();
5522 }
5523 else
5524 screen_attr = 0; /* highlighting has stopped */
5525 }
5526#ifdef FEAT_MBYTE
5527 if (enc_dbcs != 0)
5528 {
5529 /* Check if overwriting a double-byte with a single-byte or
5530 * the other way around requires another character to be
5531 * redrawn. For UTF-8 this isn't needed, because comparing
5532 * ScreenLinesUC[] is sufficient. */
5533 if (char_cells == 1
5534 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005535 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 /* Writing a single-cell character over a double-cell
5538 * character: need to redraw the next cell. */
5539 ScreenLines[off_to + 1] = 0;
5540 redraw_next = TRUE;
5541 }
5542 else if (char_cells == 2
5543 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005544 && (*mb_off2cells)(off_to, max_off_to) == 1
5545 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 {
5547 /* Writing the second half of a double-cell character over
5548 * a double-cell character: need to redraw the second
5549 * cell. */
5550 ScreenLines[off_to + 2] = 0;
5551 redraw_next = TRUE;
5552 }
5553
5554 if (enc_dbcs == DBCS_JPNU)
5555 ScreenLines2[off_to] = ScreenLines2[off_from];
5556 }
5557 /* When writing a single-width character over a double-width
5558 * character and at the end of the redrawn text, need to clear out
5559 * the right halve of the old character.
5560 * Also required when writing the right halve of a double-width
5561 * char over the left halve of an existing one. */
5562 if (has_mbyte && col + char_cells == endcol
5563 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005564 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005566 && (*mb_off2cells)(off_to, max_off_to) == 1
5567 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 clear_next = TRUE;
5569#endif
5570
5571 ScreenLines[off_to] = ScreenLines[off_from];
5572#ifdef FEAT_MBYTE
5573 if (enc_utf8)
5574 {
5575 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5576 if (ScreenLinesUC[off_from] != 0)
5577 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005578 int i;
5579
5580 for (i = 0; i < Screen_mco; ++i)
5581 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583 }
5584 if (char_cells == 2)
5585 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5586#endif
5587
5588#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005589 /* The bold trick makes a single column of pixels appear in the
5590 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 * character should be redrawn too. This happens for our own GUI
5592 * and for some xterms. */
5593 if (
5594# ifdef FEAT_GUI
5595 gui.in_use
5596# endif
5597# if defined(FEAT_GUI) && defined(UNIX)
5598 ||
5599# endif
5600# ifdef UNIX
5601 term_is_xterm
5602# endif
5603 )
5604 {
5605 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005606 if (hl > HL_ALL)
5607 hl = syn_attr2attr(hl);
5608 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 redraw_next = TRUE;
5610 }
5611#endif
5612 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5613#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005614 /* For simplicity set the attributes of second half of a
5615 * double-wide character equal to the first half. */
5616 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005618
5619 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 else
5622#endif
5623 screen_char(off_to, row, col + coloff);
5624 }
5625 else if ( p_wiv
5626#ifdef FEAT_GUI
5627 && !gui.in_use
5628#endif
5629 && col + coloff > 0)
5630 {
5631 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5632 {
5633 /*
5634 * Don't output stop-highlight when moving the cursor, it will
5635 * stop the highlighting when it should continue.
5636 */
5637 screen_attr = 0;
5638 }
5639 else if (screen_attr != 0)
5640 screen_stop_highlight();
5641 }
5642
5643 off_to += CHAR_CELLS;
5644 off_from += CHAR_CELLS;
5645 col += CHAR_CELLS;
5646 }
5647
5648#ifdef FEAT_MBYTE
5649 if (clear_next)
5650 {
5651 /* Clear the second half of a double-wide character of which the left
5652 * half was overwritten with a single-wide character. */
5653 ScreenLines[off_to] = ' ';
5654 if (enc_utf8)
5655 ScreenLinesUC[off_to] = 0;
5656 screen_char(off_to, row, col + coloff);
5657 }
5658#endif
5659
5660 if (clear_width > 0
5661#ifdef FEAT_RIGHTLEFT
5662 && !rlflag
5663#endif
5664 )
5665 {
5666#ifdef FEAT_GUI
5667 int startCol = col;
5668#endif
5669
5670 /* blank out the rest of the line */
5671 while (col < clear_width && ScreenLines[off_to] == ' '
5672 && ScreenAttrs[off_to] == 0
5673#ifdef FEAT_MBYTE
5674 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5675#endif
5676 )
5677 {
5678 ++off_to;
5679 ++col;
5680 }
5681 if (col < clear_width)
5682 {
5683#ifdef FEAT_GUI
5684 /*
5685 * In the GUI, clearing the rest of the line may leave pixels
5686 * behind if the first character cleared was bold. Some bold
5687 * fonts spill over the left. In this case we redraw the previous
5688 * character too. If we didn't skip any blanks above, then we
5689 * only redraw if the character wasn't already redrawn anyway.
5690 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005691 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
5693 hl = ScreenAttrs[off_to];
5694 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005695 {
5696 int prev_cells = 1;
5697# ifdef FEAT_MBYTE
5698 if (enc_utf8)
5699 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5700 * that its width is 2. */
5701 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5702 else if (enc_dbcs != 0)
5703 {
5704 /* find previous character by counting from first
5705 * column and get its width. */
5706 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005707 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005708
5709 while (off < off_to)
5710 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005711 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005712 off += prev_cells;
5713 }
5714 }
5715
5716 if (enc_dbcs != 0 && prev_cells > 1)
5717 screen_char_2(off_to - prev_cells, row,
5718 col + coloff - prev_cells);
5719 else
5720# endif
5721 screen_char(off_to - prev_cells, row,
5722 col + coloff - prev_cells);
5723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725#endif
5726 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5727 ' ', ' ', 0);
5728#ifdef FEAT_VERTSPLIT
5729 off_to += clear_width - col;
5730 col = clear_width;
5731#endif
5732 }
5733 }
5734
5735 if (clear_width > 0)
5736 {
5737#ifdef FEAT_VERTSPLIT
5738 /* For a window that's left of another, draw the separator char. */
5739 if (col + coloff < Columns)
5740 {
5741 int c;
5742
5743 c = fillchar_vsep(&hl);
5744 if (ScreenLines[off_to] != c
5745# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005746 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5747 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748# endif
5749 || ScreenAttrs[off_to] != hl)
5750 {
5751 ScreenLines[off_to] = c;
5752 ScreenAttrs[off_to] = hl;
5753# ifdef FEAT_MBYTE
5754 if (enc_utf8)
5755 {
5756 if (c >= 0x80)
5757 {
5758 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005759 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761 else
5762 ScreenLinesUC[off_to] = 0;
5763 }
5764# endif
5765 screen_char(off_to, row, col + coloff);
5766 }
5767 }
5768 else
5769#endif
5770 LineWraps[row] = FALSE;
5771 }
5772}
5773
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005774#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005776 * Mirror text "str" for right-left displaying.
5777 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005779 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780rl_mirror(str)
5781 char_u *str;
5782{
5783 char_u *p1, *p2;
5784 int t;
5785
5786 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5787 {
5788 t = *p1;
5789 *p1 = *p2;
5790 *p2 = t;
5791 }
5792}
5793#endif
5794
5795#if defined(FEAT_WINDOWS) || defined(PROTO)
5796/*
5797 * mark all status lines for redraw; used after first :cd
5798 */
5799 void
5800status_redraw_all()
5801{
5802 win_T *wp;
5803
5804 for (wp = firstwin; wp; wp = wp->w_next)
5805 if (wp->w_status_height)
5806 {
5807 wp->w_redr_status = TRUE;
5808 redraw_later(VALID);
5809 }
5810}
5811
5812/*
5813 * mark all status lines of the current buffer for redraw
5814 */
5815 void
5816status_redraw_curbuf()
5817{
5818 win_T *wp;
5819
5820 for (wp = firstwin; wp; wp = wp->w_next)
5821 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5822 {
5823 wp->w_redr_status = TRUE;
5824 redraw_later(VALID);
5825 }
5826}
5827
5828/*
5829 * Redraw all status lines that need to be redrawn.
5830 */
5831 void
5832redraw_statuslines()
5833{
5834 win_T *wp;
5835
5836 for (wp = firstwin; wp; wp = wp->w_next)
5837 if (wp->w_redr_status)
5838 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005839 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005840 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841}
5842#endif
5843
5844#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5845/*
5846 * Redraw all status lines at the bottom of frame "frp".
5847 */
5848 void
5849win_redraw_last_status(frp)
5850 frame_T *frp;
5851{
5852 if (frp->fr_layout == FR_LEAF)
5853 frp->fr_win->w_redr_status = TRUE;
5854 else if (frp->fr_layout == FR_ROW)
5855 {
5856 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5857 win_redraw_last_status(frp);
5858 }
5859 else /* frp->fr_layout == FR_COL */
5860 {
5861 frp = frp->fr_child;
5862 while (frp->fr_next != NULL)
5863 frp = frp->fr_next;
5864 win_redraw_last_status(frp);
5865 }
5866}
5867#endif
5868
5869#ifdef FEAT_VERTSPLIT
5870/*
5871 * Draw the verticap separator right of window "wp" starting with line "row".
5872 */
5873 static void
5874draw_vsep_win(wp, row)
5875 win_T *wp;
5876 int row;
5877{
5878 int hl;
5879 int c;
5880
5881 if (wp->w_vsep_width)
5882 {
5883 /* draw the vertical separator right of this window */
5884 c = fillchar_vsep(&hl);
5885 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5886 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5887 c, ' ', hl);
5888 }
5889}
5890#endif
5891
5892#ifdef FEAT_WILDMENU
5893static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005894static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
5896/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005897 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 */
5899 static int
5900status_match_len(xp, s)
5901 expand_T *xp;
5902 char_u *s;
5903{
5904 int len = 0;
5905
5906#ifdef FEAT_MENU
5907 int emenu = (xp->xp_context == EXPAND_MENUS
5908 || xp->xp_context == EXPAND_MENUNAMES);
5909
5910 /* Check for menu separators - replace with '|'. */
5911 if (emenu && menu_is_separator(s))
5912 return 1;
5913#endif
5914
5915 while (*s != NUL)
5916 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005917 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005918 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005919 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 }
5921
5922 return len;
5923}
5924
5925/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005926 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005927 * These are backslashes used for escaping. Do show backslashes in help tags.
5928 */
5929 static int
5930skip_status_match_char(xp, s)
5931 expand_T *xp;
5932 char_u *s;
5933{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005934 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005935#ifdef FEAT_MENU
5936 || ((xp->xp_context == EXPAND_MENUS
5937 || xp->xp_context == EXPAND_MENUNAMES)
5938 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5939#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005940 )
5941 {
5942#ifndef BACKSLASH_IN_FILENAME
5943 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5944 return 2;
5945#endif
5946 return 1;
5947 }
5948 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005949}
5950
5951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 * Show wildchar matches in the status line.
5953 * Show at least the "match" item.
5954 * We start at item 'first_match' in the list and show all matches that fit.
5955 *
5956 * If inversion is possible we use it. Else '=' characters are used.
5957 */
5958 void
5959win_redr_status_matches(xp, num_matches, matches, match, showtail)
5960 expand_T *xp;
5961 int num_matches;
5962 char_u **matches; /* list of matches */
5963 int match;
5964 int showtail;
5965{
5966#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5967 int row;
5968 char_u *buf;
5969 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005970 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 int fillchar;
5972 int attr;
5973 int i;
5974 int highlight = TRUE;
5975 char_u *selstart = NULL;
5976 int selstart_col = 0;
5977 char_u *selend = NULL;
5978 static int first_match = 0;
5979 int add_left = FALSE;
5980 char_u *s;
5981#ifdef FEAT_MENU
5982 int emenu;
5983#endif
5984#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5985 int l;
5986#endif
5987
5988 if (matches == NULL) /* interrupted completion? */
5989 return;
5990
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005991#ifdef FEAT_MBYTE
5992 if (has_mbyte)
5993 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5994 else
5995#endif
5996 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 if (buf == NULL)
5998 return;
5999
6000 if (match == -1) /* don't show match but original text */
6001 {
6002 match = 0;
6003 highlight = FALSE;
6004 }
6005 /* count 1 for the ending ">" */
6006 clen = status_match_len(xp, L_MATCH(match)) + 3;
6007 if (match == 0)
6008 first_match = 0;
6009 else if (match < first_match)
6010 {
6011 /* jumping left, as far as we can go */
6012 first_match = match;
6013 add_left = TRUE;
6014 }
6015 else
6016 {
6017 /* check if match fits on the screen */
6018 for (i = first_match; i < match; ++i)
6019 clen += status_match_len(xp, L_MATCH(i)) + 2;
6020 if (first_match > 0)
6021 clen += 2;
6022 /* jumping right, put match at the left */
6023 if ((long)clen > Columns)
6024 {
6025 first_match = match;
6026 /* if showing the last match, we can add some on the left */
6027 clen = 2;
6028 for (i = match; i < num_matches; ++i)
6029 {
6030 clen += status_match_len(xp, L_MATCH(i)) + 2;
6031 if ((long)clen >= Columns)
6032 break;
6033 }
6034 if (i == num_matches)
6035 add_left = TRUE;
6036 }
6037 }
6038 if (add_left)
6039 while (first_match > 0)
6040 {
6041 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6042 if ((long)clen >= Columns)
6043 break;
6044 --first_match;
6045 }
6046
6047 fillchar = fillchar_status(&attr, TRUE);
6048
6049 if (first_match == 0)
6050 {
6051 *buf = NUL;
6052 len = 0;
6053 }
6054 else
6055 {
6056 STRCPY(buf, "< ");
6057 len = 2;
6058 }
6059 clen = len;
6060
6061 i = first_match;
6062 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6063 {
6064 if (i == match)
6065 {
6066 selstart = buf + len;
6067 selstart_col = clen;
6068 }
6069
6070 s = L_MATCH(i);
6071 /* Check for menu separators - replace with '|' */
6072#ifdef FEAT_MENU
6073 emenu = (xp->xp_context == EXPAND_MENUS
6074 || xp->xp_context == EXPAND_MENUNAMES);
6075 if (emenu && menu_is_separator(s))
6076 {
6077 STRCPY(buf + len, transchar('|'));
6078 l = (int)STRLEN(buf + len);
6079 len += l;
6080 clen += l;
6081 }
6082 else
6083#endif
6084 for ( ; *s != NUL; ++s)
6085 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006086 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 clen += ptr2cells(s);
6088#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006089 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 {
6091 STRNCPY(buf + len, s, l);
6092 s += l - 1;
6093 len += l;
6094 }
6095 else
6096#endif
6097 {
6098 STRCPY(buf + len, transchar_byte(*s));
6099 len += (int)STRLEN(buf + len);
6100 }
6101 }
6102 if (i == match)
6103 selend = buf + len;
6104
6105 *(buf + len++) = ' ';
6106 *(buf + len++) = ' ';
6107 clen += 2;
6108 if (++i == num_matches)
6109 break;
6110 }
6111
6112 if (i != num_matches)
6113 {
6114 *(buf + len++) = '>';
6115 ++clen;
6116 }
6117
6118 buf[len] = NUL;
6119
6120 row = cmdline_row - 1;
6121 if (row >= 0)
6122 {
6123 if (wild_menu_showing == 0)
6124 {
6125 if (msg_scrolled > 0)
6126 {
6127 /* Put the wildmenu just above the command line. If there is
6128 * no room, scroll the screen one line up. */
6129 if (cmdline_row == Rows - 1)
6130 {
6131 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6132 ++msg_scrolled;
6133 }
6134 else
6135 {
6136 ++cmdline_row;
6137 ++row;
6138 }
6139 wild_menu_showing = WM_SCROLLED;
6140 }
6141 else
6142 {
6143 /* Create status line if needed by setting 'laststatus' to 2.
6144 * Set 'winminheight' to zero to avoid that the window is
6145 * resized. */
6146 if (lastwin->w_status_height == 0)
6147 {
6148 save_p_ls = p_ls;
6149 save_p_wmh = p_wmh;
6150 p_ls = 2;
6151 p_wmh = 0;
6152 last_status(FALSE);
6153 }
6154 wild_menu_showing = WM_SHOWN;
6155 }
6156 }
6157
6158 screen_puts(buf, row, 0, attr);
6159 if (selstart != NULL && highlight)
6160 {
6161 *selend = NUL;
6162 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6163 }
6164
6165 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6166 }
6167
6168#ifdef FEAT_VERTSPLIT
6169 win_redraw_last_status(topframe);
6170#else
6171 lastwin->w_redr_status = TRUE;
6172#endif
6173 vim_free(buf);
6174}
6175#endif
6176
6177#if defined(FEAT_WINDOWS) || defined(PROTO)
6178/*
6179 * Redraw the status line of window wp.
6180 *
6181 * If inversion is possible we use it. Else '=' characters are used.
6182 */
6183 void
6184win_redr_status(wp)
6185 win_T *wp;
6186{
6187 int row;
6188 char_u *p;
6189 int len;
6190 int fillchar;
6191 int attr;
6192 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006193 static int busy = FALSE;
6194
6195 /* It's possible to get here recursively when 'statusline' (indirectly)
6196 * invokes ":redrawstatus". Simply ignore the call then. */
6197 if (busy)
6198 return;
6199 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 wp->w_redr_status = FALSE;
6202 if (wp->w_status_height == 0)
6203 {
6204 /* no status line, can only be last window */
6205 redraw_cmdline = TRUE;
6206 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006207 else if (!redrawing()
6208#ifdef FEAT_INS_EXPAND
6209 /* don't update status line when popup menu is visible and may be
6210 * drawn over it */
6211 || pum_visible()
6212#endif
6213 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
6215 /* Don't redraw right now, do it later. */
6216 wp->w_redr_status = TRUE;
6217 }
6218#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006219 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 {
6221 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006222 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
6224#endif
6225 else
6226 {
6227 fillchar = fillchar_status(&attr, wp == curwin);
6228
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006229 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 p = NameBuff;
6231 len = (int)STRLEN(p);
6232
6233 if (wp->w_buffer->b_help
6234#ifdef FEAT_QUICKFIX
6235 || wp->w_p_pvw
6236#endif
6237 || bufIsChanged(wp->w_buffer)
6238 || wp->w_buffer->b_p_ro)
6239 *(p + len++) = ' ';
6240 if (wp->w_buffer->b_help)
6241 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006242 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 len += (int)STRLEN(p + len);
6244 }
6245#ifdef FEAT_QUICKFIX
6246 if (wp->w_p_pvw)
6247 {
6248 STRCPY(p + len, _("[Preview]"));
6249 len += (int)STRLEN(p + len);
6250 }
6251#endif
6252 if (bufIsChanged(wp->w_buffer))
6253 {
6254 STRCPY(p + len, "[+]");
6255 len += 3;
6256 }
6257 if (wp->w_buffer->b_p_ro)
6258 {
6259 STRCPY(p + len, "[RO]");
6260 len += 4;
6261 }
6262
6263#ifndef FEAT_VERTSPLIT
6264 this_ru_col = ru_col;
6265 if (this_ru_col < (Columns + 1) / 2)
6266 this_ru_col = (Columns + 1) / 2;
6267#else
6268 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6269 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6270 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6271 if (this_ru_col <= 1)
6272 {
6273 p = (char_u *)"<"; /* No room for file name! */
6274 len = 1;
6275 }
6276 else
6277#endif
6278#ifdef FEAT_MBYTE
6279 if (has_mbyte)
6280 {
6281 int clen = 0, i;
6282
6283 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006284 clen = mb_string2cells(p, -1);
6285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 /* Find first character that will fit.
6287 * Going from start to end is much faster for DBCS. */
6288 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006289 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 clen -= (*mb_ptr2cells)(p + i);
6291 len = clen;
6292 if (i > 0)
6293 {
6294 p = p + i - 1;
6295 *p = '<';
6296 ++len;
6297 }
6298
6299 }
6300 else
6301#endif
6302 if (len > this_ru_col - 1)
6303 {
6304 p += len - (this_ru_col - 1);
6305 *p = '<';
6306 len = this_ru_col - 1;
6307 }
6308
6309 row = W_WINROW(wp) + wp->w_height;
6310 screen_puts(p, row, W_WINCOL(wp), attr);
6311 screen_fill(row, row + 1, len + W_WINCOL(wp),
6312 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6313
6314 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6315 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6316 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6317 - 1 + W_WINCOL(wp)), attr);
6318
6319#ifdef FEAT_CMDL_INFO
6320 win_redr_ruler(wp, TRUE);
6321#endif
6322 }
6323
6324#ifdef FEAT_VERTSPLIT
6325 /*
6326 * May need to draw the character below the vertical separator.
6327 */
6328 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6329 {
6330 if (stl_connected(wp))
6331 fillchar = fillchar_status(&attr, wp == curwin);
6332 else
6333 fillchar = fillchar_vsep(&attr);
6334 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6335 attr);
6336 }
6337#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006338 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339}
6340
Bram Moolenaar238a5642006-02-21 22:12:05 +00006341#ifdef FEAT_STL_OPT
6342/*
6343 * Redraw the status line according to 'statusline' and take care of any
6344 * errors encountered.
6345 */
6346 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006347redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006348 win_T *wp;
6349{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006350 static int entered = FALSE;
6351 int save_called_emsg = called_emsg;
6352
6353 /* When called recursively return. This can happen when the statusline
6354 * contains an expression that triggers a redraw. */
6355 if (entered)
6356 return;
6357 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006358
6359 called_emsg = FALSE;
6360 win_redr_custom(wp, FALSE);
6361 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006362 {
6363 /* When there is an error disable the statusline, otherwise the
6364 * display is messed up with errors and a redraw triggers the problem
6365 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006366 set_string_option_direct((char_u *)"statusline", -1,
6367 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006368 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006369 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006370 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006371 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006372}
6373#endif
6374
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375# ifdef FEAT_VERTSPLIT
6376/*
6377 * Return TRUE if the status line of window "wp" is connected to the status
6378 * line of the window right of it. If not, then it's a vertical separator.
6379 * Only call if (wp->w_vsep_width != 0).
6380 */
6381 int
6382stl_connected(wp)
6383 win_T *wp;
6384{
6385 frame_T *fr;
6386
6387 fr = wp->w_frame;
6388 while (fr->fr_parent != NULL)
6389 {
6390 if (fr->fr_parent->fr_layout == FR_COL)
6391 {
6392 if (fr->fr_next != NULL)
6393 break;
6394 }
6395 else
6396 {
6397 if (fr->fr_next != NULL)
6398 return TRUE;
6399 }
6400 fr = fr->fr_parent;
6401 }
6402 return FALSE;
6403}
6404# endif
6405
6406#endif /* FEAT_WINDOWS */
6407
6408#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6409/*
6410 * Get the value to show for the language mappings, active 'keymap'.
6411 */
6412 int
6413get_keymap_str(wp, buf, len)
6414 win_T *wp;
6415 char_u *buf; /* buffer for the result */
6416 int len; /* length of buffer */
6417{
6418 char_u *p;
6419
6420 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6421 return FALSE;
6422
6423 {
6424#ifdef FEAT_EVAL
6425 buf_T *old_curbuf = curbuf;
6426 win_T *old_curwin = curwin;
6427 char_u *s;
6428
6429 curbuf = wp->w_buffer;
6430 curwin = wp;
6431 STRCPY(buf, "b:keymap_name"); /* must be writable */
6432 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006433 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 --emsg_skip;
6435 curbuf = old_curbuf;
6436 curwin = old_curwin;
6437 if (p == NULL || *p == NUL)
6438#endif
6439 {
6440#ifdef FEAT_KEYMAP
6441 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6442 p = wp->w_buffer->b_p_keymap;
6443 else
6444#endif
6445 p = (char_u *)"lang";
6446 }
6447 if ((int)(STRLEN(p) + 3) < len)
6448 sprintf((char *)buf, "<%s>", p);
6449 else
6450 buf[0] = NUL;
6451#ifdef FEAT_EVAL
6452 vim_free(s);
6453#endif
6454 }
6455 return buf[0] != NUL;
6456}
6457#endif
6458
6459#if defined(FEAT_STL_OPT) || defined(PROTO)
6460/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006461 * Redraw the status line or ruler of window "wp".
6462 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 */
6464 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006465win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006467 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 int attr;
6470 int curattr;
6471 int row;
6472 int col = 0;
6473 int maxwidth;
6474 int width;
6475 int n;
6476 int len;
6477 int fillchar;
6478 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006479 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006481 struct stl_hlrec hltab[STL_MAX_ITEM];
6482 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006483 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006484 win_T *ewp;
6485 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006488 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006490 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006491 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006492 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006493 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006494 attr = hl_attr(HLF_TPF);
6495 maxwidth = Columns;
6496# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006497 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006500 else
6501 {
6502 row = W_WINROW(wp) + wp->w_height;
6503 fillchar = fillchar_status(&attr, wp == curwin);
6504 maxwidth = W_WIDTH(wp);
6505
6506 if (draw_ruler)
6507 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006508 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006509 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006510 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006511 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006512 if (*++stl == '-')
6513 stl++;
6514 if (atoi((char *)stl))
6515 while (VIM_ISDIGIT(*stl))
6516 stl++;
6517 if (*stl++ != '(')
6518 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006519 }
6520#ifdef FEAT_VERTSPLIT
6521 col = ru_col - (Columns - W_WIDTH(wp));
6522 if (col < (W_WIDTH(wp) + 1) / 2)
6523 col = (W_WIDTH(wp) + 1) / 2;
6524#else
6525 col = ru_col;
6526 if (col > (Columns + 1) / 2)
6527 col = (Columns + 1) / 2;
6528#endif
6529 maxwidth = W_WIDTH(wp) - col;
6530#ifdef FEAT_WINDOWS
6531 if (!wp->w_status_height)
6532#endif
6533 {
6534 row = Rows - 1;
6535 --maxwidth; /* writing in last column may cause scrolling */
6536 fillchar = ' ';
6537 attr = 0;
6538 }
6539
6540# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006541 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006542# endif
6543 }
6544 else
6545 {
6546 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006547 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006548 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006549 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006550# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006551 use_sandbox = was_set_insecurely((char_u *)"statusline",
6552 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006553# endif
6554 }
6555
6556#ifdef FEAT_VERTSPLIT
6557 col += W_WINCOL(wp);
6558#endif
6559 }
6560
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 if (maxwidth <= 0)
6562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
Bram Moolenaar61452852011-02-01 18:01:11 +01006564 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6565 * the cursor away and back. */
6566 ewp = wp == NULL ? curwin : wp;
6567 p_crb_save = ewp->w_p_crb;
6568 ewp->w_p_crb = FALSE;
6569
Bram Moolenaar362f3562009-11-03 16:20:34 +00006570 /* Make a copy, because the statusline may include a function call that
6571 * might change the option value and free the memory. */
6572 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006573 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006574 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006575 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006576 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006577 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006579 /* Make all characters printable. */
6580 p = transstr(buf);
6581 if (p != NULL)
6582 {
6583 vim_strncpy(buf, p, sizeof(buf) - 1);
6584 vim_free(p);
6585 }
6586
6587 /* fill up with "fillchar" */
6588 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006589 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 {
6591#ifdef FEAT_MBYTE
6592 len += (*mb_char2bytes)(fillchar, buf + len);
6593#else
6594 buf[len++] = fillchar;
6595#endif
6596 ++width;
6597 }
6598 buf[len] = NUL;
6599
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006600 /*
6601 * Draw each snippet with the specified highlighting.
6602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 curattr = attr;
6604 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006605 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006607 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 screen_puts_len(p, len, row, col, curattr);
6609 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006610 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006612 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006614 else if (hltab[n].userhl < 0)
6615 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006617 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006618 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619#endif
6620 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006621 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 }
6623 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006624
6625 if (wp == NULL)
6626 {
6627 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6628 col = 0;
6629 len = 0;
6630 p = buf;
6631 fillchar = 0;
6632 for (n = 0; tabtab[n].start != NULL; n++)
6633 {
6634 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6635 while (col < len)
6636 TabPageIdxs[col++] = fillchar;
6637 p = tabtab[n].start;
6638 fillchar = tabtab[n].userhl;
6639 }
6640 while (col < Columns)
6641 TabPageIdxs[col++] = fillchar;
6642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643}
6644
6645#endif /* FEAT_STL_OPT */
6646
6647/*
6648 * Output a single character directly to the screen and update ScreenLines.
6649 */
6650 void
6651screen_putchar(c, row, col, attr)
6652 int c;
6653 int row, col;
6654 int attr;
6655{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 char_u buf[MB_MAXBYTES + 1];
6657
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006658#ifdef FEAT_MBYTE
6659 if (has_mbyte)
6660 buf[(*mb_char2bytes)(c, buf)] = NUL;
6661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006663 {
6664 buf[0] = c;
6665 buf[1] = NUL;
6666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 screen_puts(buf, row, col, attr);
6668}
6669
6670/*
6671 * Get a single character directly from ScreenLines into "bytes[]".
6672 * Also return its attribute in *attrp;
6673 */
6674 void
6675screen_getbytes(row, col, bytes, attrp)
6676 int row, col;
6677 char_u *bytes;
6678 int *attrp;
6679{
6680 unsigned off;
6681
6682 /* safety check */
6683 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6684 {
6685 off = LineOffset[row] + col;
6686 *attrp = ScreenAttrs[off];
6687 bytes[0] = ScreenLines[off];
6688 bytes[1] = NUL;
6689
6690#ifdef FEAT_MBYTE
6691 if (enc_utf8 && ScreenLinesUC[off] != 0)
6692 bytes[utfc_char2bytes(off, bytes)] = NUL;
6693 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6694 {
6695 bytes[0] = ScreenLines[off];
6696 bytes[1] = ScreenLines2[off];
6697 bytes[2] = NUL;
6698 }
6699 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6700 {
6701 bytes[1] = ScreenLines[off + 1];
6702 bytes[2] = NUL;
6703 }
6704#endif
6705 }
6706}
6707
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006708#ifdef FEAT_MBYTE
6709static int screen_comp_differs __ARGS((int, int*));
6710
6711/*
6712 * Return TRUE if composing characters for screen posn "off" differs from
6713 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006714 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006715 */
6716 static int
6717screen_comp_differs(off, u8cc)
6718 int off;
6719 int *u8cc;
6720{
6721 int i;
6722
6723 for (i = 0; i < Screen_mco; ++i)
6724 {
6725 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6726 return TRUE;
6727 if (u8cc[i] == 0)
6728 break;
6729 }
6730 return FALSE;
6731}
6732#endif
6733
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734/*
6735 * Put string '*text' on the screen at position 'row' and 'col', with
6736 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6737 * Note: only outputs within one row, message is truncated at screen boundary!
6738 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6739 */
6740 void
6741screen_puts(text, row, col, attr)
6742 char_u *text;
6743 int row;
6744 int col;
6745 int attr;
6746{
6747 screen_puts_len(text, -1, row, col, attr);
6748}
6749
6750/*
6751 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6752 * a NUL.
6753 */
6754 void
6755screen_puts_len(text, len, row, col, attr)
6756 char_u *text;
6757 int len;
6758 int row;
6759 int col;
6760 int attr;
6761{
6762 unsigned off;
6763 char_u *ptr = text;
6764 int c;
6765#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006766 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 int mbyte_blen = 1;
6768 int mbyte_cells = 1;
6769 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006770 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 int clear_next_cell = FALSE;
6772# ifdef FEAT_ARABIC
6773 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006774 int pc, nc, nc1;
6775 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776# endif
6777#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006778#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6779 int force_redraw_this;
6780 int force_redraw_next = FALSE;
6781#endif
6782 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783
6784 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6785 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006786 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
Bram Moolenaarc236c162008-07-13 17:41:49 +00006788#ifdef FEAT_MBYTE
6789 /* When drawing over the right halve of a double-wide char clear out the
6790 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006791 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006792# ifdef FEAT_GUI
6793 && !gui.in_use
6794# endif
6795 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006796 {
6797 ScreenLines[off - 1] = ' ';
6798 ScreenAttrs[off - 1] = 0;
6799 if (enc_utf8)
6800 {
6801 ScreenLinesUC[off - 1] = 0;
6802 ScreenLinesC[0][off - 1] = 0;
6803 }
6804 /* redraw the previous cell, make it empty */
6805 screen_char(off - 1, row, col - 1);
6806 /* force the cell at "col" to be redrawn */
6807 force_redraw_next = TRUE;
6808 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006809#endif
6810
Bram Moolenaar367329b2007-08-30 11:53:22 +00006811#ifdef FEAT_MBYTE
6812 max_off = LineOffset[row] + screen_Columns;
6813#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006814 while (col < screen_Columns
6815 && (len < 0 || (int)(ptr - text) < len)
6816 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 {
6818 c = *ptr;
6819#ifdef FEAT_MBYTE
6820 /* check if this is the first byte of a multibyte */
6821 if (has_mbyte)
6822 {
6823 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006824 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006826 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6828 mbyte_cells = 1;
6829 else if (enc_dbcs != 0)
6830 mbyte_cells = mbyte_blen;
6831 else /* enc_utf8 */
6832 {
6833 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006834 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 (int)((text + len) - ptr));
6836 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006837 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006839# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 /* Non-BMP character: display as ? or fullwidth ?. */
6841 if (u8c >= 0x10000)
6842 {
6843 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6844 if (attr == 0)
6845 attr = hl_attr(HLF_8);
6846 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848# ifdef FEAT_ARABIC
6849 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6850 {
6851 /* Do Arabic shaping. */
6852 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6853 {
6854 /* Past end of string to be displayed. */
6855 nc = NUL;
6856 nc1 = NUL;
6857 }
6858 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006859 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006860 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6861 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006862 nc1 = pcc[0];
6863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 pc = prev_c;
6865 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006866 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 }
6868 else
6869 prev_c = u8c;
6870# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006871 if (col + mbyte_cells > screen_Columns)
6872 {
6873 /* Only 1 cell left, but character requires 2 cells:
6874 * display a '>' in the last column to avoid wrapping. */
6875 c = '>';
6876 mbyte_cells = 1;
6877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 }
6879 }
6880#endif
6881
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006882#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6883 force_redraw_this = force_redraw_next;
6884 force_redraw_next = FALSE;
6885#endif
6886
6887 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888#ifdef FEAT_MBYTE
6889 || (mbyte_cells == 2
6890 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6891 || (enc_dbcs == DBCS_JPNU
6892 && c == 0x8e
6893 && ScreenLines2[off] != ptr[1])
6894 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006895 && (ScreenLinesUC[off] !=
6896 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6897 || (ScreenLinesUC[off] != 0
6898 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899#endif
6900 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006901 || exmode_active;
6902
6903 if (need_redraw
6904#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6905 || force_redraw_this
6906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 )
6908 {
6909#if defined(FEAT_GUI) || defined(UNIX)
6910 /* The bold trick makes a single row of pixels appear in the next
6911 * character. When a bold character is removed, the next
6912 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006913 * and for some xterms. */
6914 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915# ifdef FEAT_GUI
6916 gui.in_use
6917# endif
6918# if defined(FEAT_GUI) && defined(UNIX)
6919 ||
6920# endif
6921# ifdef UNIX
6922 term_is_xterm
6923# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006924 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006926 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006928 if (n > HL_ALL)
6929 n = syn_attr2attr(n);
6930 if (n & HL_BOLD)
6931 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
6933#endif
6934#ifdef FEAT_MBYTE
6935 /* When at the end of the text and overwriting a two-cell
6936 * character with a one-cell character, need to clear the next
6937 * cell. Also when overwriting the left halve of a two-cell char
6938 * with the right halve of a two-cell char. Do this only once
6939 * (mb_off2cells() may return 2 on the right halve). */
6940 if (clear_next_cell)
6941 clear_next_cell = FALSE;
6942 else if (has_mbyte
6943 && (len < 0 ? ptr[mbyte_blen] == NUL
6944 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006945 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006947 && (*mb_off2cells)(off, max_off) == 1
6948 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 clear_next_cell = TRUE;
6950
6951 /* Make sure we never leave a second byte of a double-byte behind,
6952 * it confuses mb_off2cells(). */
6953 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006954 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006956 && (*mb_off2cells)(off, max_off) == 1
6957 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 ScreenLines[off + mbyte_blen] = 0;
6959#endif
6960 ScreenLines[off] = c;
6961 ScreenAttrs[off] = attr;
6962#ifdef FEAT_MBYTE
6963 if (enc_utf8)
6964 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006965 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 ScreenLinesUC[off] = 0;
6967 else
6968 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006969 int i;
6970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006972 for (i = 0; i < Screen_mco; ++i)
6973 {
6974 ScreenLinesC[i][off] = u8cc[i];
6975 if (u8cc[i] == 0)
6976 break;
6977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 }
6979 if (mbyte_cells == 2)
6980 {
6981 ScreenLines[off + 1] = 0;
6982 ScreenAttrs[off + 1] = attr;
6983 }
6984 screen_char(off, row, col);
6985 }
6986 else if (mbyte_cells == 2)
6987 {
6988 ScreenLines[off + 1] = ptr[1];
6989 ScreenAttrs[off + 1] = attr;
6990 screen_char_2(off, row, col);
6991 }
6992 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6993 {
6994 ScreenLines2[off] = ptr[1];
6995 screen_char(off, row, col);
6996 }
6997 else
6998#endif
6999 screen_char(off, row, col);
7000 }
7001#ifdef FEAT_MBYTE
7002 if (has_mbyte)
7003 {
7004 off += mbyte_cells;
7005 col += mbyte_cells;
7006 ptr += mbyte_blen;
7007 if (clear_next_cell)
7008 ptr = (char_u *)" ";
7009 }
7010 else
7011#endif
7012 {
7013 ++off;
7014 ++col;
7015 ++ptr;
7016 }
7017 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007018
7019#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7020 /* If we detected the next character needs to be redrawn, but the text
7021 * doesn't extend up to there, update the character here. */
7022 if (force_redraw_next && col < screen_Columns)
7023 {
7024# ifdef FEAT_MBYTE
7025 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7026 screen_char_2(off, row, col);
7027 else
7028# endif
7029 screen_char(off, row, col);
7030 }
7031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032}
7033
7034#ifdef FEAT_SEARCH_EXTRA
7035/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007036 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 */
7038 static void
7039start_search_hl()
7040{
7041 if (p_hls && !no_hlsearch)
7042 {
7043 last_pat_prog(&search_hl.rm);
7044 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007045# ifdef FEAT_RELTIME
7046 /* Set the time limit to 'redrawtime'. */
7047 profile_setlimit(p_rdt, &search_hl.tm);
7048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 }
7050}
7051
7052/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007053 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 */
7055 static void
7056end_search_hl()
7057{
7058 if (search_hl.rm.regprog != NULL)
7059 {
7060 vim_free(search_hl.rm.regprog);
7061 search_hl.rm.regprog = NULL;
7062 }
7063}
7064
7065/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007066 * Init for calling prepare_search_hl().
7067 */
7068 static void
7069init_search_hl(wp)
7070 win_T *wp;
7071{
7072 matchitem_T *cur;
7073
7074 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7075 * match */
7076 cur = wp->w_match_head;
7077 while (cur != NULL)
7078 {
7079 cur->hl.rm = cur->match;
7080 if (cur->hlg_id == 0)
7081 cur->hl.attr = 0;
7082 else
7083 cur->hl.attr = syn_id2attr(cur->hlg_id);
7084 cur->hl.buf = wp->w_buffer;
7085 cur->hl.lnum = 0;
7086 cur->hl.first_lnum = 0;
7087# ifdef FEAT_RELTIME
7088 /* Set the time limit to 'redrawtime'. */
7089 profile_setlimit(p_rdt, &(cur->hl.tm));
7090# endif
7091 cur = cur->next;
7092 }
7093 search_hl.buf = wp->w_buffer;
7094 search_hl.lnum = 0;
7095 search_hl.first_lnum = 0;
7096 /* time limit is set at the toplevel, for all windows */
7097}
7098
7099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 * Advance to the match in window "wp" line "lnum" or past it.
7101 */
7102 static void
7103prepare_search_hl(wp, lnum)
7104 win_T *wp;
7105 linenr_T lnum;
7106{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007107 matchitem_T *cur; /* points to the match list */
7108 match_T *shl; /* points to search_hl or a match */
7109 int shl_flag; /* flag to indicate whether search_hl
7110 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 int n;
7112
7113 /*
7114 * When using a multi-line pattern, start searching at the top
7115 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007116 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007118 cur = wp->w_match_head;
7119 shl_flag = FALSE;
7120 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007122 if (shl_flag == FALSE)
7123 {
7124 shl = &search_hl;
7125 shl_flag = TRUE;
7126 }
7127 else
7128 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (shl->rm.regprog != NULL
7130 && shl->lnum == 0
7131 && re_multiline(shl->rm.regprog))
7132 {
7133 if (shl->first_lnum == 0)
7134 {
7135# ifdef FEAT_FOLDING
7136 for (shl->first_lnum = lnum;
7137 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7138 if (hasFoldingWin(wp, shl->first_lnum - 1,
7139 NULL, NULL, TRUE, NULL))
7140 break;
7141# else
7142 shl->first_lnum = wp->w_topline;
7143# endif
7144 }
7145 n = 0;
7146 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7147 {
7148 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7149 if (shl->lnum != 0)
7150 {
7151 shl->first_lnum = shl->lnum
7152 + shl->rm.endpos[0].lnum
7153 - shl->rm.startpos[0].lnum;
7154 n = shl->rm.endpos[0].col;
7155 }
7156 else
7157 {
7158 ++shl->first_lnum;
7159 n = 0;
7160 }
7161 }
7162 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007163 if (shl != &search_hl && cur != NULL)
7164 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 }
7166}
7167
7168/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007169 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 * Uses shl->buf.
7171 * Sets shl->lnum and shl->rm contents.
7172 * Note: Assumes a previous match is always before "lnum", unless
7173 * shl->lnum is zero.
7174 * Careful: Any pointers for buffer lines will become invalid.
7175 */
7176 static void
7177next_search_hl(win, shl, lnum, mincol)
7178 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007179 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 linenr_T lnum;
7181 colnr_T mincol; /* minimal column for a match */
7182{
7183 linenr_T l;
7184 colnr_T matchcol;
7185 long nmatched;
7186
7187 if (shl->lnum != 0)
7188 {
7189 /* Check for three situations:
7190 * 1. If the "lnum" is below a previous match, start a new search.
7191 * 2. If the previous match includes "mincol", use it.
7192 * 3. Continue after the previous match.
7193 */
7194 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7195 if (lnum > l)
7196 shl->lnum = 0;
7197 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7198 return;
7199 }
7200
7201 /*
7202 * Repeat searching for a match until one is found that includes "mincol"
7203 * or none is found in this line.
7204 */
7205 called_emsg = FALSE;
7206 for (;;)
7207 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007208#ifdef FEAT_RELTIME
7209 /* Stop searching after passing the time limit. */
7210 if (profile_passed_limit(&(shl->tm)))
7211 {
7212 shl->lnum = 0; /* no match found in time */
7213 break;
7214 }
7215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 /* Three situations:
7217 * 1. No useful previous match: search from start of line.
7218 * 2. Not Vi compatible or empty match: continue at next character.
7219 * Break the loop if this is beyond the end of the line.
7220 * 3. Vi compatible searching: continue at end of previous match.
7221 */
7222 if (shl->lnum == 0)
7223 matchcol = 0;
7224 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7225 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007226 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007228 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007229
7230 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007231 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007232 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007234 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 shl->lnum = 0;
7236 break;
7237 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007238#ifdef FEAT_MBYTE
7239 if (has_mbyte)
7240 matchcol += mb_ptr2len(ml);
7241 else
7242#endif
7243 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245 else
7246 matchcol = shl->rm.endpos[0].col;
7247
7248 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007249 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7250#ifdef FEAT_RELTIME
7251 &(shl->tm)
7252#else
7253 NULL
7254#endif
7255 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007256 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
7258 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007259 if (shl == &search_hl)
7260 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007261 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007262 vim_free(shl->rm.regprog);
7263 no_hlsearch = TRUE;
7264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007266 shl->lnum = 0;
7267 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 break;
7269 }
7270 if (nmatched == 0)
7271 {
7272 shl->lnum = 0; /* no match found */
7273 break;
7274 }
7275 if (shl->rm.startpos[0].lnum > 0
7276 || shl->rm.startpos[0].col >= mincol
7277 || nmatched > 1
7278 || shl->rm.endpos[0].col > mincol)
7279 {
7280 shl->lnum += shl->rm.startpos[0].lnum;
7281 break; /* useful match found */
7282 }
7283 }
7284}
7285#endif
7286
7287 static void
7288screen_start_highlight(attr)
7289 int attr;
7290{
7291 attrentry_T *aep = NULL;
7292
7293 screen_attr = attr;
7294 if (full_screen
7295#ifdef WIN3264
7296 && termcap_active
7297#endif
7298 )
7299 {
7300#ifdef FEAT_GUI
7301 if (gui.in_use)
7302 {
7303 char buf[20];
7304
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007305 /* The GUI handles this internally. */
7306 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 OUT_STR(buf);
7308 }
7309 else
7310#endif
7311 {
7312 if (attr > HL_ALL) /* special HL attr. */
7313 {
7314 if (t_colors > 1)
7315 aep = syn_cterm_attr2entry(attr);
7316 else
7317 aep = syn_term_attr2entry(attr);
7318 if (aep == NULL) /* did ":syntax clear" */
7319 attr = 0;
7320 else
7321 attr = aep->ae_attr;
7322 }
7323 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7324 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007325 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7326 && cterm_normal_fg_bold)
7327 /* If the Normal FG color has BOLD attribute and the new HL
7328 * has a FG color defined, clear BOLD. */
7329 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7331 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007332 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7333 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 out_str(T_US);
7335 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7336 out_str(T_CZH);
7337 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7338 out_str(T_MR);
7339
7340 /*
7341 * Output the color or start string after bold etc., in case the
7342 * bold etc. override the color setting.
7343 */
7344 if (aep != NULL)
7345 {
7346 if (t_colors > 1)
7347 {
7348 if (aep->ae_u.cterm.fg_color)
7349 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7350 if (aep->ae_u.cterm.bg_color)
7351 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7352 }
7353 else
7354 {
7355 if (aep->ae_u.term.start != NULL)
7356 out_str(aep->ae_u.term.start);
7357 }
7358 }
7359 }
7360 }
7361}
7362
7363 void
7364screen_stop_highlight()
7365{
7366 int do_ME = FALSE; /* output T_ME code */
7367
7368 if (screen_attr != 0
7369#ifdef WIN3264
7370 && termcap_active
7371#endif
7372 )
7373 {
7374#ifdef FEAT_GUI
7375 if (gui.in_use)
7376 {
7377 char buf[20];
7378
7379 /* use internal GUI code */
7380 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7381 OUT_STR(buf);
7382 }
7383 else
7384#endif
7385 {
7386 if (screen_attr > HL_ALL) /* special HL attr. */
7387 {
7388 attrentry_T *aep;
7389
7390 if (t_colors > 1)
7391 {
7392 /*
7393 * Assume that t_me restores the original colors!
7394 */
7395 aep = syn_cterm_attr2entry(screen_attr);
7396 if (aep != NULL && (aep->ae_u.cterm.fg_color
7397 || aep->ae_u.cterm.bg_color))
7398 do_ME = TRUE;
7399 }
7400 else
7401 {
7402 aep = syn_term_attr2entry(screen_attr);
7403 if (aep != NULL && aep->ae_u.term.stop != NULL)
7404 {
7405 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7406 do_ME = TRUE;
7407 else
7408 out_str(aep->ae_u.term.stop);
7409 }
7410 }
7411 if (aep == NULL) /* did ":syntax clear" */
7412 screen_attr = 0;
7413 else
7414 screen_attr = aep->ae_attr;
7415 }
7416
7417 /*
7418 * Often all ending-codes are equal to T_ME. Avoid outputting the
7419 * same sequence several times.
7420 */
7421 if (screen_attr & HL_STANDOUT)
7422 {
7423 if (STRCMP(T_SE, T_ME) == 0)
7424 do_ME = TRUE;
7425 else
7426 out_str(T_SE);
7427 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007428 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {
7430 if (STRCMP(T_UE, T_ME) == 0)
7431 do_ME = TRUE;
7432 else
7433 out_str(T_UE);
7434 }
7435 if (screen_attr & HL_ITALIC)
7436 {
7437 if (STRCMP(T_CZR, T_ME) == 0)
7438 do_ME = TRUE;
7439 else
7440 out_str(T_CZR);
7441 }
7442 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7443 out_str(T_ME);
7444
7445 if (t_colors > 1)
7446 {
7447 /* set Normal cterm colors */
7448 if (cterm_normal_fg_color != 0)
7449 term_fg_color(cterm_normal_fg_color - 1);
7450 if (cterm_normal_bg_color != 0)
7451 term_bg_color(cterm_normal_bg_color - 1);
7452 if (cterm_normal_fg_bold)
7453 out_str(T_MD);
7454 }
7455 }
7456 }
7457 screen_attr = 0;
7458}
7459
7460/*
7461 * Reset the colors for a cterm. Used when leaving Vim.
7462 * The machine specific code may override this again.
7463 */
7464 void
7465reset_cterm_colors()
7466{
7467 if (t_colors > 1)
7468 {
7469 /* set Normal cterm colors */
7470 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7471 {
7472 out_str(T_OP);
7473 screen_attr = -1;
7474 }
7475 if (cterm_normal_fg_bold)
7476 {
7477 out_str(T_ME);
7478 screen_attr = -1;
7479 }
7480 }
7481}
7482
7483/*
7484 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7485 * using the attributes from ScreenAttrs["off"].
7486 */
7487 static void
7488screen_char(off, row, col)
7489 unsigned off;
7490 int row;
7491 int col;
7492{
7493 int attr;
7494
7495 /* Check for illegal values, just in case (could happen just after
7496 * resizing). */
7497 if (row >= screen_Rows || col >= screen_Columns)
7498 return;
7499
7500 /* Outputting the last character on the screen may scrollup the screen.
7501 * Don't to it! Mark the character invalid (update it when scrolled up) */
7502 if (row == screen_Rows - 1 && col == screen_Columns - 1
7503#ifdef FEAT_RIGHTLEFT
7504 /* account for first command-line character in rightleft mode */
7505 && !cmdmsg_rl
7506#endif
7507 )
7508 {
7509 ScreenAttrs[off] = (sattr_T)-1;
7510 return;
7511 }
7512
7513 /*
7514 * Stop highlighting first, so it's easier to move the cursor.
7515 */
7516#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7517 if (screen_char_attr != 0)
7518 attr = screen_char_attr;
7519 else
7520#endif
7521 attr = ScreenAttrs[off];
7522 if (screen_attr != attr)
7523 screen_stop_highlight();
7524
7525 windgoto(row, col);
7526
7527 if (screen_attr != attr)
7528 screen_start_highlight(attr);
7529
7530#ifdef FEAT_MBYTE
7531 if (enc_utf8 && ScreenLinesUC[off] != 0)
7532 {
7533 char_u buf[MB_MAXBYTES + 1];
7534
7535 /* Convert UTF-8 character to bytes and write it. */
7536
7537 buf[utfc_char2bytes(off, buf)] = NUL;
7538
7539 out_str(buf);
7540 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7541 ++screen_cur_col;
7542 }
7543 else
7544#endif
7545 {
7546#ifdef FEAT_MBYTE
7547 out_flush_check();
7548#endif
7549 out_char(ScreenLines[off]);
7550#ifdef FEAT_MBYTE
7551 /* double-byte character in single-width cell */
7552 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7553 out_char(ScreenLines2[off]);
7554#endif
7555 }
7556
7557 screen_cur_col++;
7558}
7559
7560#ifdef FEAT_MBYTE
7561
7562/*
7563 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7564 * on the screen at position 'row' and 'col'.
7565 * The attributes of the first byte is used for all. This is required to
7566 * output the two bytes of a double-byte character with nothing in between.
7567 */
7568 static void
7569screen_char_2(off, row, col)
7570 unsigned off;
7571 int row;
7572 int col;
7573{
7574 /* Check for illegal values (could be wrong when screen was resized). */
7575 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7576 return;
7577
7578 /* Outputting the last character on the screen may scrollup the screen.
7579 * Don't to it! Mark the character invalid (update it when scrolled up) */
7580 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7581 {
7582 ScreenAttrs[off] = (sattr_T)-1;
7583 return;
7584 }
7585
7586 /* Output the first byte normally (positions the cursor), then write the
7587 * second byte directly. */
7588 screen_char(off, row, col);
7589 out_char(ScreenLines[off + 1]);
7590 ++screen_cur_col;
7591}
7592#endif
7593
7594#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7595/*
7596 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7597 * This uses the contents of ScreenLines[] and doesn't change it.
7598 */
7599 void
7600screen_draw_rectangle(row, col, height, width, invert)
7601 int row;
7602 int col;
7603 int height;
7604 int width;
7605 int invert;
7606{
7607 int r, c;
7608 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007609#ifdef FEAT_MBYTE
7610 int max_off;
7611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007613 /* Can't use ScreenLines unless initialized */
7614 if (ScreenLines == NULL)
7615 return;
7616
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 if (invert)
7618 screen_char_attr = HL_INVERSE;
7619 for (r = row; r < row + height; ++r)
7620 {
7621 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007622#ifdef FEAT_MBYTE
7623 max_off = off + screen_Columns;
7624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 for (c = col; c < col + width; ++c)
7626 {
7627#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007628 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
7630 screen_char_2(off + c, r, c);
7631 ++c;
7632 }
7633 else
7634#endif
7635 {
7636 screen_char(off + c, r, c);
7637#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007638 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 ++c;
7640#endif
7641 }
7642 }
7643 }
7644 screen_char_attr = 0;
7645}
7646#endif
7647
7648#ifdef FEAT_VERTSPLIT
7649/*
7650 * Redraw the characters for a vertically split window.
7651 */
7652 static void
7653redraw_block(row, end, wp)
7654 int row;
7655 int end;
7656 win_T *wp;
7657{
7658 int col;
7659 int width;
7660
7661# ifdef FEAT_CLIPBOARD
7662 clip_may_clear_selection(row, end - 1);
7663# endif
7664
7665 if (wp == NULL)
7666 {
7667 col = 0;
7668 width = Columns;
7669 }
7670 else
7671 {
7672 col = wp->w_wincol;
7673 width = wp->w_width;
7674 }
7675 screen_draw_rectangle(row, col, end - row, width, FALSE);
7676}
7677#endif
7678
7679/*
7680 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7681 * with character 'c1' in first column followed by 'c2' in the other columns.
7682 * Use attributes 'attr'.
7683 */
7684 void
7685screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7686 int start_row, end_row;
7687 int start_col, end_col;
7688 int c1, c2;
7689 int attr;
7690{
7691 int row;
7692 int col;
7693 int off;
7694 int end_off;
7695 int did_delete;
7696 int c;
7697 int norm_term;
7698#if defined(FEAT_GUI) || defined(UNIX)
7699 int force_next = FALSE;
7700#endif
7701
7702 if (end_row > screen_Rows) /* safety check */
7703 end_row = screen_Rows;
7704 if (end_col > screen_Columns) /* safety check */
7705 end_col = screen_Columns;
7706 if (ScreenLines == NULL
7707 || start_row >= end_row
7708 || start_col >= end_col) /* nothing to do */
7709 return;
7710
7711 /* it's a "normal" terminal when not in a GUI or cterm */
7712 norm_term = (
7713#ifdef FEAT_GUI
7714 !gui.in_use &&
7715#endif
7716 t_colors <= 1);
7717 for (row = start_row; row < end_row; ++row)
7718 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007719#ifdef FEAT_MBYTE
7720 if (has_mbyte
7721# ifdef FEAT_GUI
7722 && !gui.in_use
7723# endif
7724 )
7725 {
7726 /* When drawing over the right halve of a double-wide char clear
7727 * out the left halve. When drawing over the left halve of a
7728 * double wide-char clear out the right halve. Only needed in a
7729 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007730 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007731 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007732 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007733 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007734 }
7735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 /*
7737 * Try to use delete-line termcap code, when no attributes or in a
7738 * "normal" terminal, where a bold/italic space is just a
7739 * space.
7740 */
7741 did_delete = FALSE;
7742 if (c2 == ' '
7743 && end_col == Columns
7744 && can_clear(T_CE)
7745 && (attr == 0
7746 || (norm_term
7747 && attr <= HL_ALL
7748 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7749 {
7750 /*
7751 * check if we really need to clear something
7752 */
7753 col = start_col;
7754 if (c1 != ' ') /* don't clear first char */
7755 ++col;
7756
7757 off = LineOffset[row] + col;
7758 end_off = LineOffset[row] + end_col;
7759
7760 /* skip blanks (used often, keep it fast!) */
7761#ifdef FEAT_MBYTE
7762 if (enc_utf8)
7763 while (off < end_off && ScreenLines[off] == ' '
7764 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7765 ++off;
7766 else
7767#endif
7768 while (off < end_off && ScreenLines[off] == ' '
7769 && ScreenAttrs[off] == 0)
7770 ++off;
7771 if (off < end_off) /* something to be cleared */
7772 {
7773 col = off - LineOffset[row];
7774 screen_stop_highlight();
7775 term_windgoto(row, col);/* clear rest of this screen line */
7776 out_str(T_CE);
7777 screen_start(); /* don't know where cursor is now */
7778 col = end_col - col;
7779 while (col--) /* clear chars in ScreenLines */
7780 {
7781 ScreenLines[off] = ' ';
7782#ifdef FEAT_MBYTE
7783 if (enc_utf8)
7784 ScreenLinesUC[off] = 0;
7785#endif
7786 ScreenAttrs[off] = 0;
7787 ++off;
7788 }
7789 }
7790 did_delete = TRUE; /* the chars are cleared now */
7791 }
7792
7793 off = LineOffset[row] + start_col;
7794 c = c1;
7795 for (col = start_col; col < end_col; ++col)
7796 {
7797 if (ScreenLines[off] != c
7798#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007799 || (enc_utf8 && (int)ScreenLinesUC[off]
7800 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801#endif
7802 || ScreenAttrs[off] != attr
7803#if defined(FEAT_GUI) || defined(UNIX)
7804 || force_next
7805#endif
7806 )
7807 {
7808#if defined(FEAT_GUI) || defined(UNIX)
7809 /* The bold trick may make a single row of pixels appear in
7810 * the next character. When a bold character is removed, the
7811 * next character should be redrawn too. This happens for our
7812 * own GUI and for some xterms. */
7813 if (
7814# ifdef FEAT_GUI
7815 gui.in_use
7816# endif
7817# if defined(FEAT_GUI) && defined(UNIX)
7818 ||
7819# endif
7820# ifdef UNIX
7821 term_is_xterm
7822# endif
7823 )
7824 {
7825 if (ScreenLines[off] != ' '
7826 && (ScreenAttrs[off] > HL_ALL
7827 || ScreenAttrs[off] & HL_BOLD))
7828 force_next = TRUE;
7829 else
7830 force_next = FALSE;
7831 }
7832#endif
7833 ScreenLines[off] = c;
7834#ifdef FEAT_MBYTE
7835 if (enc_utf8)
7836 {
7837 if (c >= 0x80)
7838 {
7839 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007840 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842 else
7843 ScreenLinesUC[off] = 0;
7844 }
7845#endif
7846 ScreenAttrs[off] = attr;
7847 if (!did_delete || c != ' ')
7848 screen_char(off, row, col);
7849 }
7850 ++off;
7851 if (col == start_col)
7852 {
7853 if (did_delete)
7854 break;
7855 c = c2;
7856 }
7857 }
7858 if (end_col == Columns)
7859 LineWraps[row] = FALSE;
7860 if (row == Rows - 1) /* overwritten the command line */
7861 {
7862 redraw_cmdline = TRUE;
7863 if (c1 == ' ' && c2 == ' ')
7864 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007865 if (start_col == 0)
7866 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 }
7868 }
7869}
7870
7871/*
7872 * Check if there should be a delay. Used before clearing or redrawing the
7873 * screen or the command line.
7874 */
7875 void
7876check_for_delay(check_msg_scroll)
7877 int check_msg_scroll;
7878{
7879 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7880 && !did_wait_return
7881 && emsg_silent == 0)
7882 {
7883 out_flush();
7884 ui_delay(1000L, TRUE);
7885 emsg_on_display = FALSE;
7886 if (check_msg_scroll)
7887 msg_scroll = FALSE;
7888 }
7889}
7890
7891/*
7892 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007893 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 * Returns TRUE if there is a valid screen to write to.
7895 * Returns FALSE when starting up and screen not initialized yet.
7896 */
7897 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007898screen_valid(doclear)
7899 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007901 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 return (ScreenLines != NULL);
7903}
7904
7905/*
7906 * Resize the shell to Rows and Columns.
7907 * Allocate ScreenLines[] and associated items.
7908 *
7909 * There may be some time between setting Rows and Columns and (re)allocating
7910 * ScreenLines[]. This happens when starting up and when (manually) changing
7911 * the shell size. Always use screen_Rows and screen_Columns to access items
7912 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7913 * final size of the shell is needed.
7914 */
7915 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007916screenalloc(doclear)
7917 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 int new_row, old_row;
7920#ifdef FEAT_GUI
7921 int old_Rows;
7922#endif
7923 win_T *wp;
7924 int outofmem = FALSE;
7925 int len;
7926 schar_T *new_ScreenLines;
7927#ifdef FEAT_MBYTE
7928 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007929 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007931 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932#endif
7933 sattr_T *new_ScreenAttrs;
7934 unsigned *new_LineOffset;
7935 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007936#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007937 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007938 tabpage_T *tp;
7939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007941 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007942#ifdef FEAT_AUTOCMD
7943 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007945retry:
7946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 /*
7948 * Allocation of the screen buffers is done only when the size changes and
7949 * when Rows and Columns have been set and we have started doing full
7950 * screen stuff.
7951 */
7952 if ((ScreenLines != NULL
7953 && Rows == screen_Rows
7954 && Columns == screen_Columns
7955#ifdef FEAT_MBYTE
7956 && enc_utf8 == (ScreenLinesUC != NULL)
7957 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007958 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959#endif
7960 )
7961 || Rows == 0
7962 || Columns == 0
7963 || (!full_screen && ScreenLines == NULL))
7964 return;
7965
7966 /*
7967 * It's possible that we produce an out-of-memory message below, which
7968 * will cause this function to be called again. To break the loop, just
7969 * return here.
7970 */
7971 if (entered)
7972 return;
7973 entered = TRUE;
7974
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007975 /*
7976 * Note that the window sizes are updated before reallocating the arrays,
7977 * thus we must not redraw here!
7978 */
7979 ++RedrawingDisabled;
7980
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 win_new_shellsize(); /* fit the windows in the new sized shell */
7982
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 comp_col(); /* recompute columns for shown command and ruler */
7984
7985 /*
7986 * We're changing the size of the screen.
7987 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7988 * - Move lines from the old arrays into the new arrays, clear extra
7989 * lines (unless the screen is going to be cleared).
7990 * - Free the old arrays.
7991 *
7992 * If anything fails, make ScreenLines NULL, so we don't do anything!
7993 * Continuing with the old ScreenLines may result in a crash, because the
7994 * size is wrong.
7995 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007996 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007998#ifdef FEAT_AUTOCMD
7999 if (aucmd_win != NULL)
8000 win_free_lsize(aucmd_win);
8001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002
8003 new_ScreenLines = (schar_T *)lalloc((long_u)(
8004 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8005#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008006 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (enc_utf8)
8008 {
8009 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8010 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008011 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008012 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8014 }
8015 if (enc_dbcs == DBCS_JPNU)
8016 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8017 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8018#endif
8019 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8020 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8021 new_LineOffset = (unsigned *)lalloc((long_u)(
8022 Rows * sizeof(unsigned)), FALSE);
8023 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008024#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008025 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008028 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {
8030 if (win_alloc_lines(wp) == FAIL)
8031 {
8032 outofmem = TRUE;
8033#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008034 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035#endif
8036 }
8037 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008038#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008039 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8040 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008041 outofmem = TRUE;
8042#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008043#ifdef FEAT_WINDOWS
8044give_up:
8045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008047#ifdef FEAT_MBYTE
8048 for (i = 0; i < p_mco; ++i)
8049 if (new_ScreenLinesC[i] == NULL)
8050 break;
8051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 if (new_ScreenLines == NULL
8053#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008054 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8056#endif
8057 || new_ScreenAttrs == NULL
8058 || new_LineOffset == NULL
8059 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008060#ifdef FEAT_WINDOWS
8061 || new_TabPageIdxs == NULL
8062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 || outofmem)
8064 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008065 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008066 {
8067 /* guess the size */
8068 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8069
8070 /* Remember we did this to avoid getting outofmem messages over
8071 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008072 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 vim_free(new_ScreenLines);
8075 new_ScreenLines = NULL;
8076#ifdef FEAT_MBYTE
8077 vim_free(new_ScreenLinesUC);
8078 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008079 for (i = 0; i < p_mco; ++i)
8080 {
8081 vim_free(new_ScreenLinesC[i]);
8082 new_ScreenLinesC[i] = NULL;
8083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 vim_free(new_ScreenLines2);
8085 new_ScreenLines2 = NULL;
8086#endif
8087 vim_free(new_ScreenAttrs);
8088 new_ScreenAttrs = NULL;
8089 vim_free(new_LineOffset);
8090 new_LineOffset = NULL;
8091 vim_free(new_LineWraps);
8092 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008093#ifdef FEAT_WINDOWS
8094 vim_free(new_TabPageIdxs);
8095 new_TabPageIdxs = NULL;
8096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 }
8098 else
8099 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008100 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008101
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 for (new_row = 0; new_row < Rows; ++new_row)
8103 {
8104 new_LineOffset[new_row] = new_row * Columns;
8105 new_LineWraps[new_row] = FALSE;
8106
8107 /*
8108 * If the screen is not going to be cleared, copy as much as
8109 * possible from the old screen to the new one and clear the rest
8110 * (used when resizing the window at the "--more--" prompt or when
8111 * executing an external command, for the GUI).
8112 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008113 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
8115 (void)vim_memset(new_ScreenLines + new_row * Columns,
8116 ' ', (size_t)Columns * sizeof(schar_T));
8117#ifdef FEAT_MBYTE
8118 if (enc_utf8)
8119 {
8120 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8121 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008122 for (i = 0; i < p_mco; ++i)
8123 (void)vim_memset(new_ScreenLinesC[i]
8124 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 0, (size_t)Columns * sizeof(u8char_T));
8126 }
8127 if (enc_dbcs == DBCS_JPNU)
8128 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8129 0, (size_t)Columns * sizeof(schar_T));
8130#endif
8131 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8132 0, (size_t)Columns * sizeof(sattr_T));
8133 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008134 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {
8136 if (screen_Columns < Columns)
8137 len = screen_Columns;
8138 else
8139 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008140#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008141 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008142 * may be invalid now. Also when p_mco changes. */
8143 if (!(enc_utf8 && ScreenLinesUC == NULL)
8144 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008145#endif
8146 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8147 ScreenLines + LineOffset[old_row],
8148 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008150 if (enc_utf8 && ScreenLinesUC != NULL
8151 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {
8153 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8154 ScreenLinesUC + LineOffset[old_row],
8155 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008156 for (i = 0; i < p_mco; ++i)
8157 mch_memmove(new_ScreenLinesC[i]
8158 + new_LineOffset[new_row],
8159 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 (size_t)len * sizeof(u8char_T));
8161 }
8162 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8163 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8164 ScreenLines2 + LineOffset[old_row],
8165 (size_t)len * sizeof(schar_T));
8166#endif
8167 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8168 ScreenAttrs + LineOffset[old_row],
8169 (size_t)len * sizeof(sattr_T));
8170 }
8171 }
8172 }
8173 /* Use the last line of the screen for the current line. */
8174 current_ScreenLine = new_ScreenLines + Rows * Columns;
8175 }
8176
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008177 free_screenlines();
8178
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 ScreenLines = new_ScreenLines;
8180#ifdef FEAT_MBYTE
8181 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008182 for (i = 0; i < p_mco; ++i)
8183 ScreenLinesC[i] = new_ScreenLinesC[i];
8184 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 ScreenLines2 = new_ScreenLines2;
8186#endif
8187 ScreenAttrs = new_ScreenAttrs;
8188 LineOffset = new_LineOffset;
8189 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008190#ifdef FEAT_WINDOWS
8191 TabPageIdxs = new_TabPageIdxs;
8192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193
8194 /* It's important that screen_Rows and screen_Columns reflect the actual
8195 * size of ScreenLines[]. Set them before calling anything. */
8196#ifdef FEAT_GUI
8197 old_Rows = screen_Rows;
8198#endif
8199 screen_Rows = Rows;
8200 screen_Columns = Columns;
8201
8202 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008203 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 screenclear2();
8205
8206#ifdef FEAT_GUI
8207 else if (gui.in_use
8208 && !gui.starting
8209 && ScreenLines != NULL
8210 && old_Rows != Rows)
8211 {
8212 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8213 /*
8214 * Adjust the position of the cursor, for when executing an external
8215 * command.
8216 */
8217 if (msg_row >= Rows) /* Rows got smaller */
8218 msg_row = Rows - 1; /* put cursor at last row */
8219 else if (Rows > old_Rows) /* Rows got bigger */
8220 msg_row += Rows - old_Rows; /* put cursor in same place */
8221 if (msg_col >= Columns) /* Columns got smaller */
8222 msg_col = Columns - 1; /* put cursor at last column */
8223 }
8224#endif
8225
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008227 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008228
8229#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008230 /*
8231 * Do not apply autocommands more than 3 times to avoid an endless loop
8232 * in case applying autocommands always changes Rows or Columns.
8233 */
8234 if (starting == 0 && ++retry_count <= 3)
8235 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008236 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008237 /* In rare cases, autocommands may have altered Rows or Columns,
8238 * jump back to check if we need to allocate the screen again. */
8239 goto retry;
8240 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242}
8243
8244 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008245free_screenlines()
8246{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008247#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008248 int i;
8249
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008250 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008251 for (i = 0; i < Screen_mco; ++i)
8252 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008253 vim_free(ScreenLines2);
8254#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008255 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008256 vim_free(ScreenAttrs);
8257 vim_free(LineOffset);
8258 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008259#ifdef FEAT_WINDOWS
8260 vim_free(TabPageIdxs);
8261#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008262}
8263
8264 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265screenclear()
8266{
8267 check_for_delay(FALSE);
8268 screenalloc(FALSE); /* allocate screen buffers if size changed */
8269 screenclear2(); /* clear the screen */
8270}
8271
8272 static void
8273screenclear2()
8274{
8275 int i;
8276
8277 if (starting == NO_SCREEN || ScreenLines == NULL
8278#ifdef FEAT_GUI
8279 || (gui.in_use && gui.starting)
8280#endif
8281 )
8282 return;
8283
8284#ifdef FEAT_GUI
8285 if (!gui.in_use)
8286#endif
8287 screen_attr = -1; /* force setting the Normal colors */
8288 screen_stop_highlight(); /* don't want highlighting here */
8289
8290#ifdef FEAT_CLIPBOARD
8291 /* disable selection without redrawing it */
8292 clip_scroll_selection(9999);
8293#endif
8294
8295 /* blank out ScreenLines */
8296 for (i = 0; i < Rows; ++i)
8297 {
8298 lineclear(LineOffset[i], (int)Columns);
8299 LineWraps[i] = FALSE;
8300 }
8301
8302 if (can_clear(T_CL))
8303 {
8304 out_str(T_CL); /* clear the display */
8305 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008306 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 else
8309 {
8310 /* can't clear the screen, mark all chars with invalid attributes */
8311 for (i = 0; i < Rows; ++i)
8312 lineinvalid(LineOffset[i], (int)Columns);
8313 clear_cmdline = TRUE;
8314 }
8315
8316 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8317
8318 win_rest_invalid(firstwin);
8319 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008320#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008321 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 if (must_redraw == CLEAR) /* no need to clear again */
8324 must_redraw = NOT_VALID;
8325 compute_cmdrow();
8326 msg_row = cmdline_row; /* put cursor on last line for messages */
8327 msg_col = 0;
8328 screen_start(); /* don't know where cursor is now */
8329 msg_scrolled = 0; /* can't scroll back */
8330 msg_didany = FALSE;
8331 msg_didout = FALSE;
8332}
8333
8334/*
8335 * Clear one line in ScreenLines.
8336 */
8337 static void
8338lineclear(off, width)
8339 unsigned off;
8340 int width;
8341{
8342 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8343#ifdef FEAT_MBYTE
8344 if (enc_utf8)
8345 (void)vim_memset(ScreenLinesUC + off, 0,
8346 (size_t)width * sizeof(u8char_T));
8347#endif
8348 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8349}
8350
8351/*
8352 * Mark one line in ScreenLines invalid by setting the attributes to an
8353 * invalid value.
8354 */
8355 static void
8356lineinvalid(off, width)
8357 unsigned off;
8358 int width;
8359{
8360 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8361}
8362
8363#ifdef FEAT_VERTSPLIT
8364/*
8365 * Copy part of a Screenline for vertically split window "wp".
8366 */
8367 static void
8368linecopy(to, from, wp)
8369 int to;
8370 int from;
8371 win_T *wp;
8372{
8373 unsigned off_to = LineOffset[to] + wp->w_wincol;
8374 unsigned off_from = LineOffset[from] + wp->w_wincol;
8375
8376 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8377 wp->w_width * sizeof(schar_T));
8378# ifdef FEAT_MBYTE
8379 if (enc_utf8)
8380 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008381 int i;
8382
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8384 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008385 for (i = 0; i < p_mco; ++i)
8386 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8387 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 }
8389 if (enc_dbcs == DBCS_JPNU)
8390 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8391 wp->w_width * sizeof(schar_T));
8392# endif
8393 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8394 wp->w_width * sizeof(sattr_T));
8395}
8396#endif
8397
8398/*
8399 * Return TRUE if clearing with term string "p" would work.
8400 * It can't work when the string is empty or it won't set the right background.
8401 */
8402 int
8403can_clear(p)
8404 char_u *p;
8405{
8406 return (*p != NUL && (t_colors <= 1
8407#ifdef FEAT_GUI
8408 || gui.in_use
8409#endif
8410 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8411}
8412
8413/*
8414 * Reset cursor position. Use whenever cursor was moved because of outputting
8415 * something directly to the screen (shell commands) or a terminal control
8416 * code.
8417 */
8418 void
8419screen_start()
8420{
8421 screen_cur_row = screen_cur_col = 9999;
8422}
8423
8424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 * Move the cursor to position "row","col" in the screen.
8426 * This tries to find the most efficient way to move, minimizing the number of
8427 * characters sent to the terminal.
8428 */
8429 void
8430windgoto(row, col)
8431 int row;
8432 int col;
8433{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008434 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 int i;
8436 int plan;
8437 int cost;
8438 int wouldbe_col;
8439 int noinvcurs;
8440 char_u *bs;
8441 int goto_cost;
8442 int attr;
8443
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008444#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8446
8447#define PLAN_LE 1
8448#define PLAN_CR 2
8449#define PLAN_NL 3
8450#define PLAN_WRITE 4
8451 /* Can't use ScreenLines unless initialized */
8452 if (ScreenLines == NULL)
8453 return;
8454
8455 if (col != screen_cur_col || row != screen_cur_row)
8456 {
8457 /* Check for valid position. */
8458 if (row < 0) /* window without text lines? */
8459 row = 0;
8460 if (row >= screen_Rows)
8461 row = screen_Rows - 1;
8462 if (col >= screen_Columns)
8463 col = screen_Columns - 1;
8464
8465 /* check if no cursor movement is allowed in highlight mode */
8466 if (screen_attr && *T_MS == NUL)
8467 noinvcurs = HIGHL_COST;
8468 else
8469 noinvcurs = 0;
8470 goto_cost = GOTO_COST + noinvcurs;
8471
8472 /*
8473 * Plan how to do the positioning:
8474 * 1. Use CR to move it to column 0, same row.
8475 * 2. Use T_LE to move it a few columns to the left.
8476 * 3. Use NL to move a few lines down, column 0.
8477 * 4. Move a few columns to the right with T_ND or by writing chars.
8478 *
8479 * Don't do this if the cursor went beyond the last column, the cursor
8480 * position is unknown then (some terminals wrap, some don't )
8481 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008482 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 * characters to move the cursor to the right.
8484 */
8485 if (row >= screen_cur_row && screen_cur_col < Columns)
8486 {
8487 /*
8488 * If the cursor is in the same row, bigger col, we can use CR
8489 * or T_LE.
8490 */
8491 bs = NULL; /* init for GCC */
8492 attr = screen_attr;
8493 if (row == screen_cur_row && col < screen_cur_col)
8494 {
8495 /* "le" is preferred over "bc", because "bc" is obsolete */
8496 if (*T_LE)
8497 bs = T_LE; /* "cursor left" */
8498 else
8499 bs = T_BC; /* "backspace character (old) */
8500 if (*bs)
8501 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8502 else
8503 cost = 999;
8504 if (col + 1 < cost) /* using CR is less characters */
8505 {
8506 plan = PLAN_CR;
8507 wouldbe_col = 0;
8508 cost = 1; /* CR is just one character */
8509 }
8510 else
8511 {
8512 plan = PLAN_LE;
8513 wouldbe_col = col;
8514 }
8515 if (noinvcurs) /* will stop highlighting */
8516 {
8517 cost += noinvcurs;
8518 attr = 0;
8519 }
8520 }
8521
8522 /*
8523 * If the cursor is above where we want to be, we can use CR LF.
8524 */
8525 else if (row > screen_cur_row)
8526 {
8527 plan = PLAN_NL;
8528 wouldbe_col = 0;
8529 cost = (row - screen_cur_row) * 2; /* CR LF */
8530 if (noinvcurs) /* will stop highlighting */
8531 {
8532 cost += noinvcurs;
8533 attr = 0;
8534 }
8535 }
8536
8537 /*
8538 * If the cursor is in the same row, smaller col, just use write.
8539 */
8540 else
8541 {
8542 plan = PLAN_WRITE;
8543 wouldbe_col = screen_cur_col;
8544 cost = 0;
8545 }
8546
8547 /*
8548 * Check if any characters that need to be written have the
8549 * correct attributes. Also avoid UTF-8 characters.
8550 */
8551 i = col - wouldbe_col;
8552 if (i > 0)
8553 cost += i;
8554 if (cost < goto_cost && i > 0)
8555 {
8556 /*
8557 * Check if the attributes are correct without additionally
8558 * stopping highlighting.
8559 */
8560 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8561 while (i && *p++ == attr)
8562 --i;
8563 if (i != 0)
8564 {
8565 /*
8566 * Try if it works when highlighting is stopped here.
8567 */
8568 if (*--p == 0)
8569 {
8570 cost += noinvcurs;
8571 while (i && *p++ == 0)
8572 --i;
8573 }
8574 if (i != 0)
8575 cost = 999; /* different attributes, don't do it */
8576 }
8577#ifdef FEAT_MBYTE
8578 if (enc_utf8)
8579 {
8580 /* Don't use an UTF-8 char for positioning, it's slow. */
8581 for (i = wouldbe_col; i < col; ++i)
8582 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8583 {
8584 cost = 999;
8585 break;
8586 }
8587 }
8588#endif
8589 }
8590
8591 /*
8592 * We can do it without term_windgoto()!
8593 */
8594 if (cost < goto_cost)
8595 {
8596 if (plan == PLAN_LE)
8597 {
8598 if (noinvcurs)
8599 screen_stop_highlight();
8600 while (screen_cur_col > col)
8601 {
8602 out_str(bs);
8603 --screen_cur_col;
8604 }
8605 }
8606 else if (plan == PLAN_CR)
8607 {
8608 if (noinvcurs)
8609 screen_stop_highlight();
8610 out_char('\r');
8611 screen_cur_col = 0;
8612 }
8613 else if (plan == PLAN_NL)
8614 {
8615 if (noinvcurs)
8616 screen_stop_highlight();
8617 while (screen_cur_row < row)
8618 {
8619 out_char('\n');
8620 ++screen_cur_row;
8621 }
8622 screen_cur_col = 0;
8623 }
8624
8625 i = col - screen_cur_col;
8626 if (i > 0)
8627 {
8628 /*
8629 * Use cursor-right if it's one character only. Avoids
8630 * removing a line of pixels from the last bold char, when
8631 * using the bold trick in the GUI.
8632 */
8633 if (T_ND[0] != NUL && T_ND[1] == NUL)
8634 {
8635 while (i-- > 0)
8636 out_char(*T_ND);
8637 }
8638 else
8639 {
8640 int off;
8641
8642 off = LineOffset[row] + screen_cur_col;
8643 while (i-- > 0)
8644 {
8645 if (ScreenAttrs[off] != screen_attr)
8646 screen_stop_highlight();
8647#ifdef FEAT_MBYTE
8648 out_flush_check();
8649#endif
8650 out_char(ScreenLines[off]);
8651#ifdef FEAT_MBYTE
8652 if (enc_dbcs == DBCS_JPNU
8653 && ScreenLines[off] == 0x8e)
8654 out_char(ScreenLines2[off]);
8655#endif
8656 ++off;
8657 }
8658 }
8659 }
8660 }
8661 }
8662 else
8663 cost = 999;
8664
8665 if (cost >= goto_cost)
8666 {
8667 if (noinvcurs)
8668 screen_stop_highlight();
8669 if (row == screen_cur_row && (col > screen_cur_col) &&
8670 *T_CRI != NUL)
8671 term_cursor_right(col - screen_cur_col);
8672 else
8673 term_windgoto(row, col);
8674 }
8675 screen_cur_row = row;
8676 screen_cur_col = col;
8677 }
8678}
8679
8680/*
8681 * Set cursor to its position in the current window.
8682 */
8683 void
8684setcursor()
8685{
8686 if (redrawing())
8687 {
8688 validate_cursor();
8689 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8690 W_WINCOL(curwin) + (
8691#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008692 /* With 'rightleft' set and the cursor on a double-wide
8693 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8695# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008696 (has_mbyte
8697 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8698 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699# endif
8700 1)) :
8701#endif
8702 curwin->w_wcol));
8703 }
8704}
8705
8706
8707/*
8708 * insert 'line_count' lines at 'row' in window 'wp'
8709 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8710 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8711 * scrolling.
8712 * Returns FAIL if the lines are not inserted, OK for success.
8713 */
8714 int
8715win_ins_lines(wp, row, line_count, invalid, mayclear)
8716 win_T *wp;
8717 int row;
8718 int line_count;
8719 int invalid;
8720 int mayclear;
8721{
8722 int did_delete;
8723 int nextrow;
8724 int lastrow;
8725 int retval;
8726
8727 if (invalid)
8728 wp->w_lines_valid = 0;
8729
8730 if (wp->w_height < 5)
8731 return FAIL;
8732
8733 if (line_count > wp->w_height - row)
8734 line_count = wp->w_height - row;
8735
8736 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8737 if (retval != MAYBE)
8738 return retval;
8739
8740 /*
8741 * If there is a next window or a status line, we first try to delete the
8742 * lines at the bottom to avoid messing what is after the window.
8743 * If this fails and there are following windows, don't do anything to avoid
8744 * messing up those windows, better just redraw.
8745 */
8746 did_delete = FALSE;
8747#ifdef FEAT_WINDOWS
8748 if (wp->w_next != NULL || wp->w_status_height)
8749 {
8750 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8751 line_count, (int)Rows, FALSE, NULL) == OK)
8752 did_delete = TRUE;
8753 else if (wp->w_next)
8754 return FAIL;
8755 }
8756#endif
8757 /*
8758 * if no lines deleted, blank the lines that will end up below the window
8759 */
8760 if (!did_delete)
8761 {
8762#ifdef FEAT_WINDOWS
8763 wp->w_redr_status = TRUE;
8764#endif
8765 redraw_cmdline = TRUE;
8766 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8767 lastrow = nextrow + line_count;
8768 if (lastrow > Rows)
8769 lastrow = Rows;
8770 screen_fill(nextrow - line_count, lastrow - line_count,
8771 W_WINCOL(wp), (int)W_ENDCOL(wp),
8772 ' ', ' ', 0);
8773 }
8774
8775 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8776 == FAIL)
8777 {
8778 /* deletion will have messed up other windows */
8779 if (did_delete)
8780 {
8781#ifdef FEAT_WINDOWS
8782 wp->w_redr_status = TRUE;
8783#endif
8784 win_rest_invalid(W_NEXT(wp));
8785 }
8786 return FAIL;
8787 }
8788
8789 return OK;
8790}
8791
8792/*
8793 * delete "line_count" window lines at "row" in window "wp"
8794 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8795 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8796 * scrolling
8797 * Return OK for success, FAIL if the lines are not deleted.
8798 */
8799 int
8800win_del_lines(wp, row, line_count, invalid, mayclear)
8801 win_T *wp;
8802 int row;
8803 int line_count;
8804 int invalid;
8805 int mayclear;
8806{
8807 int retval;
8808
8809 if (invalid)
8810 wp->w_lines_valid = 0;
8811
8812 if (line_count > wp->w_height - row)
8813 line_count = wp->w_height - row;
8814
8815 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8816 if (retval != MAYBE)
8817 return retval;
8818
8819 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8820 (int)Rows, FALSE, NULL) == FAIL)
8821 return FAIL;
8822
8823#ifdef FEAT_WINDOWS
8824 /*
8825 * If there are windows or status lines below, try to put them at the
8826 * correct place. If we can't do that, they have to be redrawn.
8827 */
8828 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8829 {
8830 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8831 line_count, (int)Rows, NULL) == FAIL)
8832 {
8833 wp->w_redr_status = TRUE;
8834 win_rest_invalid(wp->w_next);
8835 }
8836 }
8837 /*
8838 * If this is the last window and there is no status line, redraw the
8839 * command line later.
8840 */
8841 else
8842#endif
8843 redraw_cmdline = TRUE;
8844 return OK;
8845}
8846
8847/*
8848 * Common code for win_ins_lines() and win_del_lines().
8849 * Returns OK or FAIL when the work has been done.
8850 * Returns MAYBE when not finished yet.
8851 */
8852 static int
8853win_do_lines(wp, row, line_count, mayclear, del)
8854 win_T *wp;
8855 int row;
8856 int line_count;
8857 int mayclear;
8858 int del;
8859{
8860 int retval;
8861
8862 if (!redrawing() || line_count <= 0)
8863 return FAIL;
8864
8865 /* only a few lines left: redraw is faster */
8866 if (mayclear && Rows - line_count < 5
8867#ifdef FEAT_VERTSPLIT
8868 && wp->w_width == Columns
8869#endif
8870 )
8871 {
8872 screenclear(); /* will set wp->w_lines_valid to 0 */
8873 return FAIL;
8874 }
8875
8876 /*
8877 * Delete all remaining lines
8878 */
8879 if (row + line_count >= wp->w_height)
8880 {
8881 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8882 W_WINCOL(wp), (int)W_ENDCOL(wp),
8883 ' ', ' ', 0);
8884 return OK;
8885 }
8886
8887 /*
8888 * when scrolling, the message on the command line should be cleared,
8889 * otherwise it will stay there forever.
8890 */
8891 clear_cmdline = TRUE;
8892
8893 /*
8894 * If the terminal can set a scroll region, use that.
8895 * Always do this in a vertically split window. This will redraw from
8896 * ScreenLines[] when t_CV isn't defined. That's faster than using
8897 * win_line().
8898 * Don't use a scroll region when we are going to redraw the text, writing
8899 * a character in the lower right corner of the scroll region causes a
8900 * scroll-up in the DJGPP version.
8901 */
8902 if (scroll_region
8903#ifdef FEAT_VERTSPLIT
8904 || W_WIDTH(wp) != Columns
8905#endif
8906 )
8907 {
8908#ifdef FEAT_VERTSPLIT
8909 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8910#endif
8911 scroll_region_set(wp, row);
8912 if (del)
8913 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8914 wp->w_height - row, FALSE, wp);
8915 else
8916 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8917 wp->w_height - row, wp);
8918#ifdef FEAT_VERTSPLIT
8919 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8920#endif
8921 scroll_region_reset();
8922 return retval;
8923 }
8924
8925#ifdef FEAT_WINDOWS
8926 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8927 return FAIL;
8928#endif
8929
8930 return MAYBE;
8931}
8932
8933/*
8934 * window 'wp' and everything after it is messed up, mark it for redraw
8935 */
8936 static void
8937win_rest_invalid(wp)
8938 win_T *wp;
8939{
8940#ifdef FEAT_WINDOWS
8941 while (wp != NULL)
8942#else
8943 if (wp != NULL)
8944#endif
8945 {
8946 redraw_win_later(wp, NOT_VALID);
8947#ifdef FEAT_WINDOWS
8948 wp->w_redr_status = TRUE;
8949 wp = wp->w_next;
8950#endif
8951 }
8952 redraw_cmdline = TRUE;
8953}
8954
8955/*
8956 * The rest of the routines in this file perform screen manipulations. The
8957 * given operation is performed physically on the screen. The corresponding
8958 * change is also made to the internal screen image. In this way, the editor
8959 * anticipates the effect of editing changes on the appearance of the screen.
8960 * That way, when we call screenupdate a complete redraw isn't usually
8961 * necessary. Another advantage is that we can keep adding code to anticipate
8962 * screen changes, and in the meantime, everything still works.
8963 */
8964
8965/*
8966 * types for inserting or deleting lines
8967 */
8968#define USE_T_CAL 1
8969#define USE_T_CDL 2
8970#define USE_T_AL 3
8971#define USE_T_CE 4
8972#define USE_T_DL 5
8973#define USE_T_SR 6
8974#define USE_NL 7
8975#define USE_T_CD 8
8976#define USE_REDRAW 9
8977
8978/*
8979 * insert lines on the screen and update ScreenLines[]
8980 * 'end' is the line after the scrolled part. Normally it is Rows.
8981 * When scrolling region used 'off' is the offset from the top for the region.
8982 * 'row' and 'end' are relative to the start of the region.
8983 *
8984 * return FAIL for failure, OK for success.
8985 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008986 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987screen_ins_lines(off, row, line_count, end, wp)
8988 int off;
8989 int row;
8990 int line_count;
8991 int end;
8992 win_T *wp; /* NULL or window to use width from */
8993{
8994 int i;
8995 int j;
8996 unsigned temp;
8997 int cursor_row;
8998 int type;
8999 int result_empty;
9000 int can_ce = can_clear(T_CE);
9001
9002 /*
9003 * FAIL if
9004 * - there is no valid screen
9005 * - the screen has to be redrawn completely
9006 * - the line count is less than one
9007 * - the line count is more than 'ttyscroll'
9008 */
9009 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9010 return FAIL;
9011
9012 /*
9013 * There are seven ways to insert lines:
9014 * 0. When in a vertically split window and t_CV isn't set, redraw the
9015 * characters from ScreenLines[].
9016 * 1. Use T_CD (clear to end of display) if it exists and the result of
9017 * the insert is just empty lines
9018 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9019 * present or line_count > 1. It looks better if we do all the inserts
9020 * at once.
9021 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9022 * insert is just empty lines and T_CE is not present or line_count >
9023 * 1.
9024 * 4. Use T_AL (insert line) if it exists.
9025 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9026 * just empty lines.
9027 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9028 * just empty lines.
9029 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9030 * the 'da' flag is not set or we have clear line capability.
9031 * 8. redraw the characters from ScreenLines[].
9032 *
9033 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9034 * the scrollbar for the window. It does have insert line, use that if it
9035 * exists.
9036 */
9037 result_empty = (row + line_count >= end);
9038#ifdef FEAT_VERTSPLIT
9039 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9040 type = USE_REDRAW;
9041 else
9042#endif
9043 if (can_clear(T_CD) && result_empty)
9044 type = USE_T_CD;
9045 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9046 type = USE_T_CAL;
9047 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9048 type = USE_T_CDL;
9049 else if (*T_AL != NUL)
9050 type = USE_T_AL;
9051 else if (can_ce && result_empty)
9052 type = USE_T_CE;
9053 else if (*T_DL != NUL && result_empty)
9054 type = USE_T_DL;
9055 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9056 type = USE_T_SR;
9057 else
9058 return FAIL;
9059
9060 /*
9061 * For clearing the lines screen_del_lines() is used. This will also take
9062 * care of t_db if necessary.
9063 */
9064 if (type == USE_T_CD || type == USE_T_CDL ||
9065 type == USE_T_CE || type == USE_T_DL)
9066 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9067
9068 /*
9069 * If text is retained below the screen, first clear or delete as many
9070 * lines at the bottom of the window as are about to be inserted so that
9071 * the deleted lines won't later surface during a screen_del_lines.
9072 */
9073 if (*T_DB)
9074 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9075
9076#ifdef FEAT_CLIPBOARD
9077 /* Remove a modeless selection when inserting lines halfway the screen
9078 * or not the full width of the screen. */
9079 if (off + row > 0
9080# ifdef FEAT_VERTSPLIT
9081 || (wp != NULL && wp->w_width != Columns)
9082# endif
9083 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009084 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 else
9086 clip_scroll_selection(-line_count);
9087#endif
9088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089#ifdef FEAT_GUI
9090 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9091 * scrolling is actually carried out. */
9092 gui_dont_update_cursor();
9093#endif
9094
9095 if (*T_CCS != NUL) /* cursor relative to region */
9096 cursor_row = row;
9097 else
9098 cursor_row = row + off;
9099
9100 /*
9101 * Shift LineOffset[] line_count down to reflect the inserted lines.
9102 * Clear the inserted lines in ScreenLines[].
9103 */
9104 row += off;
9105 end += off;
9106 for (i = 0; i < line_count; ++i)
9107 {
9108#ifdef FEAT_VERTSPLIT
9109 if (wp != NULL && wp->w_width != Columns)
9110 {
9111 /* need to copy part of a line */
9112 j = end - 1 - i;
9113 while ((j -= line_count) >= row)
9114 linecopy(j + line_count, j, wp);
9115 j += line_count;
9116 if (can_clear((char_u *)" "))
9117 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9118 else
9119 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9120 LineWraps[j] = FALSE;
9121 }
9122 else
9123#endif
9124 {
9125 j = end - 1 - i;
9126 temp = LineOffset[j];
9127 while ((j -= line_count) >= row)
9128 {
9129 LineOffset[j + line_count] = LineOffset[j];
9130 LineWraps[j + line_count] = LineWraps[j];
9131 }
9132 LineOffset[j + line_count] = temp;
9133 LineWraps[j + line_count] = FALSE;
9134 if (can_clear((char_u *)" "))
9135 lineclear(temp, (int)Columns);
9136 else
9137 lineinvalid(temp, (int)Columns);
9138 }
9139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140
9141 screen_stop_highlight();
9142 windgoto(cursor_row, 0);
9143
9144#ifdef FEAT_VERTSPLIT
9145 /* redraw the characters */
9146 if (type == USE_REDRAW)
9147 redraw_block(row, end, wp);
9148 else
9149#endif
9150 if (type == USE_T_CAL)
9151 {
9152 term_append_lines(line_count);
9153 screen_start(); /* don't know where cursor is now */
9154 }
9155 else
9156 {
9157 for (i = 0; i < line_count; i++)
9158 {
9159 if (type == USE_T_AL)
9160 {
9161 if (i && cursor_row != 0)
9162 windgoto(cursor_row, 0);
9163 out_str(T_AL);
9164 }
9165 else /* type == USE_T_SR */
9166 out_str(T_SR);
9167 screen_start(); /* don't know where cursor is now */
9168 }
9169 }
9170
9171 /*
9172 * With scroll-reverse and 'da' flag set we need to clear the lines that
9173 * have been scrolled down into the region.
9174 */
9175 if (type == USE_T_SR && *T_DA)
9176 {
9177 for (i = 0; i < line_count; ++i)
9178 {
9179 windgoto(off + i, 0);
9180 out_str(T_CE);
9181 screen_start(); /* don't know where cursor is now */
9182 }
9183 }
9184
9185#ifdef FEAT_GUI
9186 gui_can_update_cursor();
9187 if (gui.in_use)
9188 out_flush(); /* always flush after a scroll */
9189#endif
9190 return OK;
9191}
9192
9193/*
9194 * delete lines on the screen and update ScreenLines[]
9195 * 'end' is the line after the scrolled part. Normally it is Rows.
9196 * When scrolling region used 'off' is the offset from the top for the region.
9197 * 'row' and 'end' are relative to the start of the region.
9198 *
9199 * Return OK for success, FAIL if the lines are not deleted.
9200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 int
9202screen_del_lines(off, row, line_count, end, force, wp)
9203 int off;
9204 int row;
9205 int line_count;
9206 int end;
9207 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009208 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210 int j;
9211 int i;
9212 unsigned temp;
9213 int cursor_row;
9214 int cursor_end;
9215 int result_empty; /* result is empty until end of region */
9216 int can_delete; /* deleting line codes can be used */
9217 int type;
9218
9219 /*
9220 * FAIL if
9221 * - there is no valid screen
9222 * - the screen has to be redrawn completely
9223 * - the line count is less than one
9224 * - the line count is more than 'ttyscroll'
9225 */
9226 if (!screen_valid(TRUE) || line_count <= 0 ||
9227 (!force && line_count > p_ttyscroll))
9228 return FAIL;
9229
9230 /*
9231 * Check if the rest of the current region will become empty.
9232 */
9233 result_empty = row + line_count >= end;
9234
9235 /*
9236 * We can delete lines only when 'db' flag not set or when 'ce' option
9237 * available.
9238 */
9239 can_delete = (*T_DB == NUL || can_clear(T_CE));
9240
9241 /*
9242 * There are six ways to delete lines:
9243 * 0. When in a vertically split window and t_CV isn't set, redraw the
9244 * characters from ScreenLines[].
9245 * 1. Use T_CD if it exists and the result is empty.
9246 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9247 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9248 * none of the other ways work.
9249 * 4. Use T_CE (erase line) if the result is empty.
9250 * 5. Use T_DL (delete line) if it exists.
9251 * 6. redraw the characters from ScreenLines[].
9252 */
9253#ifdef FEAT_VERTSPLIT
9254 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9255 type = USE_REDRAW;
9256 else
9257#endif
9258 if (can_clear(T_CD) && result_empty)
9259 type = USE_T_CD;
9260#if defined(__BEOS__) && defined(BEOS_DR8)
9261 /*
9262 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9263 * its internal termcap... this works okay for tests which test *T_DB !=
9264 * NUL. It has the disadvantage that the user cannot use any :set t_*
9265 * command to get T_DB (back) to empty_option, only :set term=... will do
9266 * the trick...
9267 * Anyway, this hack will hopefully go away with the next OS release.
9268 * (Olaf Seibert)
9269 */
9270 else if (row == 0 && T_DB == empty_option
9271 && (line_count == 1 || *T_CDL == NUL))
9272#else
9273 else if (row == 0 && (
9274#ifndef AMIGA
9275 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9276 * up, so use delete-line command */
9277 line_count == 1 ||
9278#endif
9279 *T_CDL == NUL))
9280#endif
9281 type = USE_NL;
9282 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9283 type = USE_T_CDL;
9284 else if (can_clear(T_CE) && result_empty
9285#ifdef FEAT_VERTSPLIT
9286 && (wp == NULL || wp->w_width == Columns)
9287#endif
9288 )
9289 type = USE_T_CE;
9290 else if (*T_DL != NUL && can_delete)
9291 type = USE_T_DL;
9292 else if (*T_CDL != NUL && can_delete)
9293 type = USE_T_CDL;
9294 else
9295 return FAIL;
9296
9297#ifdef FEAT_CLIPBOARD
9298 /* Remove a modeless selection when deleting lines halfway the screen or
9299 * not the full width of the screen. */
9300 if (off + row > 0
9301# ifdef FEAT_VERTSPLIT
9302 || (wp != NULL && wp->w_width != Columns)
9303# endif
9304 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009305 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 else
9307 clip_scroll_selection(line_count);
9308#endif
9309
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310#ifdef FEAT_GUI
9311 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9312 * scrolling is actually carried out. */
9313 gui_dont_update_cursor();
9314#endif
9315
9316 if (*T_CCS != NUL) /* cursor relative to region */
9317 {
9318 cursor_row = row;
9319 cursor_end = end;
9320 }
9321 else
9322 {
9323 cursor_row = row + off;
9324 cursor_end = end + off;
9325 }
9326
9327 /*
9328 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9329 * Clear the inserted lines in ScreenLines[].
9330 */
9331 row += off;
9332 end += off;
9333 for (i = 0; i < line_count; ++i)
9334 {
9335#ifdef FEAT_VERTSPLIT
9336 if (wp != NULL && wp->w_width != Columns)
9337 {
9338 /* need to copy part of a line */
9339 j = row + i;
9340 while ((j += line_count) <= end - 1)
9341 linecopy(j - line_count, j, wp);
9342 j -= line_count;
9343 if (can_clear((char_u *)" "))
9344 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9345 else
9346 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9347 LineWraps[j] = FALSE;
9348 }
9349 else
9350#endif
9351 {
9352 /* whole width, moving the line pointers is faster */
9353 j = row + i;
9354 temp = LineOffset[j];
9355 while ((j += line_count) <= end - 1)
9356 {
9357 LineOffset[j - line_count] = LineOffset[j];
9358 LineWraps[j - line_count] = LineWraps[j];
9359 }
9360 LineOffset[j - line_count] = temp;
9361 LineWraps[j - line_count] = FALSE;
9362 if (can_clear((char_u *)" "))
9363 lineclear(temp, (int)Columns);
9364 else
9365 lineinvalid(temp, (int)Columns);
9366 }
9367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368
9369 screen_stop_highlight();
9370
9371#ifdef FEAT_VERTSPLIT
9372 /* redraw the characters */
9373 if (type == USE_REDRAW)
9374 redraw_block(row, end, wp);
9375 else
9376#endif
9377 if (type == USE_T_CD) /* delete the lines */
9378 {
9379 windgoto(cursor_row, 0);
9380 out_str(T_CD);
9381 screen_start(); /* don't know where cursor is now */
9382 }
9383 else if (type == USE_T_CDL)
9384 {
9385 windgoto(cursor_row, 0);
9386 term_delete_lines(line_count);
9387 screen_start(); /* don't know where cursor is now */
9388 }
9389 /*
9390 * Deleting lines at top of the screen or scroll region: Just scroll
9391 * the whole screen (scroll region) up by outputting newlines on the
9392 * last line.
9393 */
9394 else if (type == USE_NL)
9395 {
9396 windgoto(cursor_end - 1, 0);
9397 for (i = line_count; --i >= 0; )
9398 out_char('\n'); /* cursor will remain on same line */
9399 }
9400 else
9401 {
9402 for (i = line_count; --i >= 0; )
9403 {
9404 if (type == USE_T_DL)
9405 {
9406 windgoto(cursor_row, 0);
9407 out_str(T_DL); /* delete a line */
9408 }
9409 else /* type == USE_T_CE */
9410 {
9411 windgoto(cursor_row + i, 0);
9412 out_str(T_CE); /* erase a line */
9413 }
9414 screen_start(); /* don't know where cursor is now */
9415 }
9416 }
9417
9418 /*
9419 * If the 'db' flag is set, we need to clear the lines that have been
9420 * scrolled up at the bottom of the region.
9421 */
9422 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9423 {
9424 for (i = line_count; i > 0; --i)
9425 {
9426 windgoto(cursor_end - i, 0);
9427 out_str(T_CE); /* erase a line */
9428 screen_start(); /* don't know where cursor is now */
9429 }
9430 }
9431
9432#ifdef FEAT_GUI
9433 gui_can_update_cursor();
9434 if (gui.in_use)
9435 out_flush(); /* always flush after a scroll */
9436#endif
9437
9438 return OK;
9439}
9440
9441/*
9442 * show the current mode and ruler
9443 *
9444 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9445 * If clear_cmdline is FALSE there may be a message there that needs to be
9446 * cleared only if a mode is shown.
9447 * Return the length of the message (0 if no message).
9448 */
9449 int
9450showmode()
9451{
9452 int need_clear;
9453 int length = 0;
9454 int do_mode;
9455 int attr;
9456 int nwr_save;
9457#ifdef FEAT_INS_EXPAND
9458 int sub_attr;
9459#endif
9460
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009461 do_mode = ((p_smd && msg_silent == 0)
9462 && ((State & INSERT)
9463 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464#ifdef FEAT_VISUAL
9465 || VIsual_active
9466#endif
9467 ));
9468 if (do_mode || Recording)
9469 {
9470 /*
9471 * Don't show mode right now, when not redrawing or inside a mapping.
9472 * Call char_avail() only when we are going to show something, because
9473 * it takes a bit of time.
9474 */
9475 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9476 {
9477 redraw_cmdline = TRUE; /* show mode later */
9478 return 0;
9479 }
9480
9481 nwr_save = need_wait_return;
9482
9483 /* wait a bit before overwriting an important message */
9484 check_for_delay(FALSE);
9485
9486 /* if the cmdline is more than one line high, erase top lines */
9487 need_clear = clear_cmdline;
9488 if (clear_cmdline && cmdline_row < Rows - 1)
9489 msg_clr_cmdline(); /* will reset clear_cmdline */
9490
9491 /* Position on the last line in the window, column 0 */
9492 msg_pos_mode();
9493 cursor_off();
9494 attr = hl_attr(HLF_CM); /* Highlight mode */
9495 if (do_mode)
9496 {
9497 MSG_PUTS_ATTR("--", attr);
9498#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009499 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009500# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009501 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009502# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009503 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009504# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009505 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009506# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 MSG_PUTS_ATTR(" IM", attr);
9508# else
9509 MSG_PUTS_ATTR(" XIM", attr);
9510# endif
9511#endif
9512#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9513 if (gui.in_use)
9514 {
9515 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009516 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 }
9518#endif
9519#ifdef FEAT_INS_EXPAND
9520 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9521 {
9522 /* These messages can get long, avoid a wrap in a narrow
9523 * window. Prefer showing edit_submode_extra. */
9524 length = (Rows - msg_row) * Columns - 3;
9525 if (edit_submode_extra != NULL)
9526 length -= vim_strsize(edit_submode_extra);
9527 if (length > 0)
9528 {
9529 if (edit_submode_pre != NULL)
9530 length -= vim_strsize(edit_submode_pre);
9531 if (length - vim_strsize(edit_submode) > 0)
9532 {
9533 if (edit_submode_pre != NULL)
9534 msg_puts_attr(edit_submode_pre, attr);
9535 msg_puts_attr(edit_submode, attr);
9536 }
9537 if (edit_submode_extra != NULL)
9538 {
9539 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9540 if ((int)edit_submode_highl < (int)HLF_COUNT)
9541 sub_attr = hl_attr(edit_submode_highl);
9542 else
9543 sub_attr = attr;
9544 msg_puts_attr(edit_submode_extra, sub_attr);
9545 }
9546 }
9547 length = 0;
9548 }
9549 else
9550#endif
9551 {
9552#ifdef FEAT_VREPLACE
9553 if (State & VREPLACE_FLAG)
9554 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9555 else
9556#endif
9557 if (State & REPLACE_FLAG)
9558 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9559 else if (State & INSERT)
9560 {
9561#ifdef FEAT_RIGHTLEFT
9562 if (p_ri)
9563 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9564#endif
9565 MSG_PUTS_ATTR(_(" INSERT"), attr);
9566 }
9567 else if (restart_edit == 'I')
9568 MSG_PUTS_ATTR(_(" (insert)"), attr);
9569 else if (restart_edit == 'R')
9570 MSG_PUTS_ATTR(_(" (replace)"), attr);
9571 else if (restart_edit == 'V')
9572 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9573#ifdef FEAT_RIGHTLEFT
9574 if (p_hkmap)
9575 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9576# ifdef FEAT_FKMAP
9577 if (p_fkmap)
9578 MSG_PUTS_ATTR(farsi_text_5, attr);
9579# endif
9580#endif
9581#ifdef FEAT_KEYMAP
9582 if (State & LANGMAP)
9583 {
9584# ifdef FEAT_ARABIC
9585 if (curwin->w_p_arab)
9586 MSG_PUTS_ATTR(_(" Arabic"), attr);
9587 else
9588# endif
9589 MSG_PUTS_ATTR(_(" (lang)"), attr);
9590 }
9591#endif
9592 if ((State & INSERT) && p_paste)
9593 MSG_PUTS_ATTR(_(" (paste)"), attr);
9594
9595#ifdef FEAT_VISUAL
9596 if (VIsual_active)
9597 {
9598 char *p;
9599
9600 /* Don't concatenate separate words to avoid translation
9601 * problems. */
9602 switch ((VIsual_select ? 4 : 0)
9603 + (VIsual_mode == Ctrl_V) * 2
9604 + (VIsual_mode == 'V'))
9605 {
9606 case 0: p = N_(" VISUAL"); break;
9607 case 1: p = N_(" VISUAL LINE"); break;
9608 case 2: p = N_(" VISUAL BLOCK"); break;
9609 case 4: p = N_(" SELECT"); break;
9610 case 5: p = N_(" SELECT LINE"); break;
9611 default: p = N_(" SELECT BLOCK"); break;
9612 }
9613 MSG_PUTS_ATTR(_(p), attr);
9614 }
9615#endif
9616 MSG_PUTS_ATTR(" --", attr);
9617 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009618
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 need_clear = TRUE;
9620 }
9621 if (Recording
9622#ifdef FEAT_INS_EXPAND
9623 && edit_submode == NULL /* otherwise it gets too long */
9624#endif
9625 )
9626 {
9627 MSG_PUTS_ATTR(_("recording"), attr);
9628 need_clear = TRUE;
9629 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009630
9631 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 if (need_clear || clear_cmdline)
9633 msg_clr_eos();
9634 msg_didout = FALSE; /* overwrite this message */
9635 length = msg_col;
9636 msg_col = 0;
9637 need_wait_return = nwr_save; /* never ask for hit-return for this */
9638 }
9639 else if (clear_cmdline && msg_silent == 0)
9640 /* Clear the whole command line. Will reset "clear_cmdline". */
9641 msg_clr_cmdline();
9642
9643#ifdef FEAT_CMDL_INFO
9644# ifdef FEAT_VISUAL
9645 /* In Visual mode the size of the selected area must be redrawn. */
9646 if (VIsual_active)
9647 clear_showcmd();
9648# endif
9649
9650 /* If the last window has no status line, the ruler is after the mode
9651 * message and must be redrawn */
9652 if (redrawing()
9653# ifdef FEAT_WINDOWS
9654 && lastwin->w_status_height == 0
9655# endif
9656 )
9657 win_redr_ruler(lastwin, TRUE);
9658#endif
9659 redraw_cmdline = FALSE;
9660 clear_cmdline = FALSE;
9661
9662 return length;
9663}
9664
9665/*
9666 * Position for a mode message.
9667 */
9668 static void
9669msg_pos_mode()
9670{
9671 msg_col = 0;
9672 msg_row = Rows - 1;
9673}
9674
9675/*
9676 * Delete mode message. Used when ESC is typed which is expected to end
9677 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009678 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 */
9680 void
9681unshowmode(force)
9682 int force;
9683{
9684 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009685 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 */
9687 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9688 redraw_cmdline = TRUE; /* delete mode later */
9689 else
9690 {
9691 msg_pos_mode();
9692 if (Recording)
9693 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9694 msg_clr_eos();
9695 }
9696}
9697
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009698#if defined(FEAT_WINDOWS)
9699/*
9700 * Draw the tab pages line at the top of the Vim window.
9701 */
9702 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009703draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009704{
9705 int tabcount = 0;
9706 tabpage_T *tp;
9707 int tabwidth;
9708 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009709 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009710 int attr;
9711 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009712 win_T *cwp;
9713 int wincount;
9714 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009715 int c;
9716 int len;
9717 int attr_sel = hl_attr(HLF_TPS);
9718 int attr_nosel = hl_attr(HLF_TP);
9719 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009720 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009721 int room;
9722 int use_sep_chars = (t_colors < 8
9723#ifdef FEAT_GUI
9724 && !gui.in_use
9725#endif
9726 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009727
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009728 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009729
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009730#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009731 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009732 if (gui_use_tabline())
9733 {
9734 gui_update_tabline();
9735 return;
9736 }
9737#endif
9738
9739 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009740 return;
9741
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009742#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009743
9744 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9745 for (scol = 0; scol < Columns; ++scol)
9746 TabPageIdxs[scol] = 0;
9747
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009748 /* Use the 'tabline' option if it's set. */
9749 if (*p_tal != NUL)
9750 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009751 int save_called_emsg = called_emsg;
9752
9753 /* Check for an error. If there is one we would loop in redrawing the
9754 * screen. Avoid that by making 'tabline' empty. */
9755 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009756 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009757 if (called_emsg)
9758 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009759 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009760 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009761 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009762 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009763#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009764 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009765 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9766 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009767
Bram Moolenaar238a5642006-02-21 22:12:05 +00009768 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9769 if (tabwidth < 6)
9770 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009771
Bram Moolenaar238a5642006-02-21 22:12:05 +00009772 attr = attr_nosel;
9773 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009774 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009775 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9776 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009777 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009778 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009779
Bram Moolenaar238a5642006-02-21 22:12:05 +00009780 if (tp->tp_topframe == topframe)
9781 attr = attr_sel;
9782 if (use_sep_chars && col > 0)
9783 screen_putchar('|', 0, col++, attr);
9784
9785 if (tp->tp_topframe != topframe)
9786 attr = attr_nosel;
9787
9788 screen_putchar(' ', 0, col++, attr);
9789
9790 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009791 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009792 cwp = curwin;
9793 wp = firstwin;
9794 }
9795 else
9796 {
9797 cwp = tp->tp_curwin;
9798 wp = tp->tp_firstwin;
9799 }
9800
9801 modified = FALSE;
9802 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9803 if (bufIsChanged(wp->w_buffer))
9804 modified = TRUE;
9805 if (modified || wincount > 1)
9806 {
9807 if (wincount > 1)
9808 {
9809 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009810 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009811 if (col + len >= Columns - 3)
9812 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009813 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009814#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009815 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009816#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009817 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009818#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009819 );
9820 col += len;
9821 }
9822 if (modified)
9823 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9824 screen_putchar(' ', 0, col++, attr);
9825 }
9826
9827 room = scol - col + tabwidth - 1;
9828 if (room > 0)
9829 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009830 /* Get buffer name in NameBuff[] */
9831 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009832 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009833 len = vim_strsize(NameBuff);
9834 p = NameBuff;
9835#ifdef FEAT_MBYTE
9836 if (has_mbyte)
9837 while (len > room)
9838 {
9839 len -= ptr2cells(p);
9840 mb_ptr_adv(p);
9841 }
9842 else
9843#endif
9844 if (len > room)
9845 {
9846 p += len - room;
9847 len = room;
9848 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009849 if (len > Columns - col - 1)
9850 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009851
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009852 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009853 col += len;
9854 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009855 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009856
9857 /* Store the tab page number in TabPageIdxs[], so that
9858 * jump_to_mouse() knows where each one is. */
9859 ++tabcount;
9860 while (scol < col)
9861 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009862 }
9863
Bram Moolenaar238a5642006-02-21 22:12:05 +00009864 if (use_sep_chars)
9865 c = '_';
9866 else
9867 c = ' ';
9868 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009869
9870 /* Put an "X" for closing the current tab if there are several. */
9871 if (first_tabpage->tp_next != NULL)
9872 {
9873 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9874 TabPageIdxs[Columns - 1] = -999;
9875 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009876 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009877
9878 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9879 * set. */
9880 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009881}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009882
9883/*
9884 * Get buffer name for "buf" into NameBuff[].
9885 * Takes care of special buffer names and translates special characters.
9886 */
9887 void
9888get_trans_bufname(buf)
9889 buf_T *buf;
9890{
9891 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02009892 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009893 else
9894 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9895 trans_characters(NameBuff, MAXPATHL);
9896}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009897#endif
9898
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9900/*
9901 * Get the character to use in a status line. Get its attributes in "*attr".
9902 */
9903 static int
9904fillchar_status(attr, is_curwin)
9905 int *attr;
9906 int is_curwin;
9907{
9908 int fill;
9909 if (is_curwin)
9910 {
9911 *attr = hl_attr(HLF_S);
9912 fill = fill_stl;
9913 }
9914 else
9915 {
9916 *attr = hl_attr(HLF_SNC);
9917 fill = fill_stlnc;
9918 }
9919 /* Use fill when there is highlighting, and highlighting of current
9920 * window differs, or the fillchars differ, or this is not the
9921 * current window */
9922 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9923 || !is_curwin || firstwin == lastwin)
9924 || (fill_stl != fill_stlnc)))
9925 return fill;
9926 if (is_curwin)
9927 return '^';
9928 return '=';
9929}
9930#endif
9931
9932#ifdef FEAT_VERTSPLIT
9933/*
9934 * Get the character to use in a separator between vertically split windows.
9935 * Get its attributes in "*attr".
9936 */
9937 static int
9938fillchar_vsep(attr)
9939 int *attr;
9940{
9941 *attr = hl_attr(HLF_C);
9942 if (*attr == 0 && fill_vert == ' ')
9943 return '|';
9944 else
9945 return fill_vert;
9946}
9947#endif
9948
9949/*
9950 * Return TRUE if redrawing should currently be done.
9951 */
9952 int
9953redrawing()
9954{
9955 return (!RedrawingDisabled
9956 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9957}
9958
9959/*
9960 * Return TRUE if printing messages should currently be done.
9961 */
9962 int
9963messaging()
9964{
9965 return (!(p_lz && char_avail() && !KeyTyped));
9966}
9967
9968/*
9969 * Show current status info in ruler and various other places
9970 * If always is FALSE, only show ruler if position has changed.
9971 */
9972 void
9973showruler(always)
9974 int always;
9975{
9976 if (!always && !redrawing())
9977 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009978#ifdef FEAT_INS_EXPAND
9979 if (pum_visible())
9980 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009981# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009982 /* Don't redraw right now, do it later. */
9983 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009984# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009985 return;
9986 }
9987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009989 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009990 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009991 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 else
9994#endif
9995#ifdef FEAT_CMDL_INFO
9996 win_redr_ruler(curwin, always);
9997#endif
9998
9999#ifdef FEAT_TITLE
10000 if (need_maketitle
10001# ifdef FEAT_STL_OPT
10002 || (p_icon && (stl_syntax & STL_IN_ICON))
10003 || (p_title && (stl_syntax & STL_IN_TITLE))
10004# endif
10005 )
10006 maketitle();
10007#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010008#ifdef FEAT_WINDOWS
10009 /* Redraw the tab pages line if needed. */
10010 if (redraw_tabline)
10011 draw_tabline();
10012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013}
10014
10015#ifdef FEAT_CMDL_INFO
10016 static void
10017win_redr_ruler(wp, always)
10018 win_T *wp;
10019 int always;
10020{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010021#define RULER_BUF_LEN 70
10022 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 int row;
10024 int fillchar;
10025 int attr;
10026 int empty_line = FALSE;
10027 colnr_T virtcol;
10028 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010029 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 int o;
10031#ifdef FEAT_VERTSPLIT
10032 int this_ru_col;
10033 int off = 0;
10034 int width = Columns;
10035# define WITH_OFF(x) x
10036# define WITH_WIDTH(x) x
10037#else
10038# define WITH_OFF(x) 0
10039# define WITH_WIDTH(x) Columns
10040# define this_ru_col ru_col
10041#endif
10042
10043 /* If 'ruler' off or redrawing disabled, don't do anything */
10044 if (!p_ru)
10045 return;
10046
10047 /*
10048 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10049 * after deleting lines, before cursor.lnum is corrected.
10050 */
10051 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10052 return;
10053
10054#ifdef FEAT_INS_EXPAND
10055 /* Don't draw the ruler while doing insert-completion, it might overwrite
10056 * the (long) mode message. */
10057# ifdef FEAT_WINDOWS
10058 if (wp == lastwin && lastwin->w_status_height == 0)
10059# endif
10060 if (edit_submode != NULL)
10061 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010062 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10063 if (pum_visible())
10064 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065#endif
10066
10067#ifdef FEAT_STL_OPT
10068 if (*p_ruf)
10069 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010070 int save_called_emsg = called_emsg;
10071
10072 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010074 if (called_emsg)
10075 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010076 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010077 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 return;
10079 }
10080#endif
10081
10082 /*
10083 * Check if not in Insert mode and the line is empty (will show "0-1").
10084 */
10085 if (!(State & INSERT)
10086 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10087 empty_line = TRUE;
10088
10089 /*
10090 * Only draw the ruler when something changed.
10091 */
10092 validate_virtcol_win(wp);
10093 if ( redraw_cmdline
10094 || always
10095 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10096 || wp->w_cursor.col != wp->w_ru_cursor.col
10097 || wp->w_virtcol != wp->w_ru_virtcol
10098#ifdef FEAT_VIRTUALEDIT
10099 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10100#endif
10101 || wp->w_topline != wp->w_ru_topline
10102 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10103#ifdef FEAT_DIFF
10104 || wp->w_topfill != wp->w_ru_topfill
10105#endif
10106 || empty_line != wp->w_ru_empty)
10107 {
10108 cursor_off();
10109#ifdef FEAT_WINDOWS
10110 if (wp->w_status_height)
10111 {
10112 row = W_WINROW(wp) + wp->w_height;
10113 fillchar = fillchar_status(&attr, wp == curwin);
10114# ifdef FEAT_VERTSPLIT
10115 off = W_WINCOL(wp);
10116 width = W_WIDTH(wp);
10117# endif
10118 }
10119 else
10120#endif
10121 {
10122 row = Rows - 1;
10123 fillchar = ' ';
10124 attr = 0;
10125#ifdef FEAT_VERTSPLIT
10126 width = Columns;
10127 off = 0;
10128#endif
10129 }
10130
10131 /* In list mode virtcol needs to be recomputed */
10132 virtcol = wp->w_virtcol;
10133 if (wp->w_p_list && lcs_tab1 == NUL)
10134 {
10135 wp->w_p_list = FALSE;
10136 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10137 wp->w_p_list = TRUE;
10138 }
10139
10140 /*
10141 * Some sprintfs return the length, some return a pointer.
10142 * To avoid portability problems we use strlen() here.
10143 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010144 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10146 ? 0L
10147 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010148 len = STRLEN(buffer);
10149 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10151 (int)virtcol + 1);
10152
10153 /*
10154 * Add a "50%" if there is room for it.
10155 * On the last line, don't print in the last column (scrolls the
10156 * screen up on some terminals).
10157 */
10158 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010159 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 o = i + vim_strsize(buffer + i + 1);
10161#ifdef FEAT_WINDOWS
10162 if (wp->w_status_height == 0) /* can't use last char of screen */
10163#endif
10164 ++o;
10165#ifdef FEAT_VERTSPLIT
10166 this_ru_col = ru_col - (Columns - width);
10167 if (this_ru_col < 0)
10168 this_ru_col = 0;
10169#endif
10170 /* Never use more than half the window/screen width, leave the other
10171 * half for the filename. */
10172 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10173 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10174 if (this_ru_col + o < WITH_WIDTH(width))
10175 {
10176 while (this_ru_col + o < WITH_WIDTH(width))
10177 {
10178#ifdef FEAT_MBYTE
10179 if (has_mbyte)
10180 i += (*mb_char2bytes)(fillchar, buffer + i);
10181 else
10182#endif
10183 buffer[i++] = fillchar;
10184 ++o;
10185 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010186 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 }
10188 /* Truncate at window boundary. */
10189#ifdef FEAT_MBYTE
10190 if (has_mbyte)
10191 {
10192 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010193 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 {
10195 o += (*mb_ptr2cells)(buffer + i);
10196 if (this_ru_col + o > WITH_WIDTH(width))
10197 {
10198 buffer[i] = NUL;
10199 break;
10200 }
10201 }
10202 }
10203 else
10204#endif
10205 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10206 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10207
10208 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10209 i = redraw_cmdline;
10210 screen_fill(row, row + 1,
10211 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10212 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10213 fillchar, fillchar, attr);
10214 /* don't redraw the cmdline because of showing the ruler */
10215 redraw_cmdline = i;
10216 wp->w_ru_cursor = wp->w_cursor;
10217 wp->w_ru_virtcol = wp->w_virtcol;
10218 wp->w_ru_empty = empty_line;
10219 wp->w_ru_topline = wp->w_topline;
10220 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10221#ifdef FEAT_DIFF
10222 wp->w_ru_topfill = wp->w_topfill;
10223#endif
10224 }
10225}
10226#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010227
10228#if defined(FEAT_LINEBREAK) || defined(PROTO)
10229/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010230 * Return the width of the 'number' and 'relativenumber' column.
10231 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010232 * Otherwise it depends on 'numberwidth' and the line count.
10233 */
10234 int
10235number_width(wp)
10236 win_T *wp;
10237{
10238 int n;
10239 linenr_T lnum;
10240
Bram Moolenaar64486672010-05-16 15:46:46 +020010241 if (wp->w_p_nu)
10242 /* 'number' */
10243 lnum = wp->w_buffer->b_ml.ml_line_count;
10244 else
10245 /* 'relativenumber' */
10246 lnum = wp->w_height;
10247
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010248 if (lnum == wp->w_nrwidth_line_count)
10249 return wp->w_nrwidth_width;
10250 wp->w_nrwidth_line_count = lnum;
10251
10252 n = 0;
10253 do
10254 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010255 lnum /= 10;
10256 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010257 } while (lnum > 0);
10258
10259 /* 'numberwidth' gives the minimal width plus one */
10260 if (n < wp->w_p_nuw - 1)
10261 n = wp->w_p_nuw - 1;
10262
10263 wp->w_nrwidth_width = n;
10264 return n;
10265}
10266#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010267
10268/*
10269 * Return the current cursor column. This is the actual position on the
10270 * screen. First column is 0.
10271 */
10272 int
10273screen_screencol()
10274{
10275 return screen_cur_col;
10276}
10277
10278/*
10279 * Return the current cursor row. This is the actual position on the screen.
10280 * First row is 0.
10281 */
10282 int
10283screen_screenrow()
10284{
10285 return screen_cur_row;
10286}