blob: c9f447c9ca7eb617cfc58290ee9be0bcd1288369 [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 Moolenaar8a20b0f2011-08-10 14:32:39 +02004272 - VCOL_HLC % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273#ifdef FEAT_MBYTE
4274 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4275#endif
4276 if (wp->w_p_list)
4277 {
4278 c = lcs_tab1;
4279 c_extra = lcs_tab2;
4280 n_attr = n_extra + 1;
4281 extra_attr = hl_attr(HLF_8);
4282 saved_attr2 = char_attr; /* save current attr */
4283#ifdef FEAT_MBYTE
4284 mb_c = c;
4285 if (enc_utf8 && (*mb_char2len)(c) > 1)
4286 {
4287 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004288 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004289 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291#endif
4292 }
4293 else
4294 {
4295 c_extra = ' ';
4296 c = ' ';
4297 }
4298 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004299 else if (c == NUL
4300 && ((wp->w_p_list && lcs_eol > 0)
4301 || ((fromcol >= 0 || fromcol_prev >= 0)
4302 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004303#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004304 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004305#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004306 && (
4307# ifdef FEAT_RIGHTLEFT
4308 wp->w_p_rl ? (col >= 0) :
4309# endif
4310 (col < W_WIDTH(wp)))
4311 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004312 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004313 && (colnr_T)vcol == wp->w_virtcol)))
4314 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004316 /* Display a '$' after the line or highlight an extra
4317 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4319 /* For a diff line the highlighting continues after the
4320 * "$". */
4321 if (
4322# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004323 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324# ifdef LINE_ATTR
4325 &&
4326# endif
4327# endif
4328# ifdef LINE_ATTR
4329 line_attr == 0
4330# endif
4331 )
4332#endif
4333 {
4334#ifdef FEAT_VIRTUALEDIT
4335 /* In virtualedit, visual selections may extend
4336 * beyond end of line. */
4337 if (area_highlighting && virtual_active()
4338 && tocol != MAXCOL && vcol < tocol)
4339 n_extra = 0;
4340 else
4341#endif
4342 {
4343 p_extra = at_end_str;
4344 n_extra = 1;
4345 c_extra = NUL;
4346 }
4347 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004348 if (wp->w_p_list)
4349 c = lcs_eol;
4350 else
4351 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 lcs_eol_one = -1;
4353 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004354 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {
4356 extra_attr = hl_attr(HLF_AT);
4357 n_attr = 1;
4358 }
4359#ifdef FEAT_MBYTE
4360 mb_c = c;
4361 if (enc_utf8 && (*mb_char2len)(c) > 1)
4362 {
4363 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004364 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004365 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 else
4368 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4369#endif
4370 }
4371 else if (c != NUL)
4372 {
4373 p_extra = transchar(c);
4374#ifdef FEAT_RIGHTLEFT
4375 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4376 rl_mirror(p_extra); /* reverse "<12>" */
4377#endif
4378 n_extra = byte2cells(c) - 1;
4379 c_extra = NUL;
4380 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004381 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
4383 n_attr = n_extra + 1;
4384 extra_attr = hl_attr(HLF_8);
4385 saved_attr2 = char_attr; /* save current attr */
4386 }
4387#ifdef FEAT_MBYTE
4388 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4389#endif
4390 }
4391#ifdef FEAT_VIRTUALEDIT
4392 else if (VIsual_active
4393 && (VIsual_mode == Ctrl_V
4394 || VIsual_mode == 'v')
4395 && virtual_active()
4396 && tocol != MAXCOL
4397 && vcol < tocol
4398 && (
4399# ifdef FEAT_RIGHTLEFT
4400 wp->w_p_rl ? (col >= 0) :
4401# endif
4402 (col < W_WIDTH(wp))))
4403 {
4404 c = ' ';
4405 --ptr; /* put it back at the NUL */
4406 }
4407#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004408#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 else if ((
4410# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004411 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 ) && (
4415# ifdef FEAT_RIGHTLEFT
4416 wp->w_p_rl ? (col >= 0) :
4417# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004418 (col
4419# ifdef FEAT_CONCEAL
4420 - boguscols
4421# endif
4422 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 /* Highlight until the right side of the window */
4425 c = ' ';
4426 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004427
4428 /* Remember we do the char for line highlighting. */
4429 ++did_line_attr;
4430
4431 /* don't do search HL for the rest of the line */
4432 if (line_attr != 0 && char_attr == search_attr && col > 0)
4433 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434# ifdef FEAT_DIFF
4435 if (diff_hlf == HLF_TXD)
4436 {
4437 diff_hlf = HLF_CHD;
4438 if (attr == 0 || char_attr != attr)
4439 char_attr = hl_attr(diff_hlf);
4440 }
4441# endif
4442 }
4443#endif
4444 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004445
4446#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004447 if ( wp->w_p_cole > 0
4448 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4449 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004450 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004451 && !(lnum_in_visual_area
4452 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004453 {
4454 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004455 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004456 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4457 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004458 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004459 /* First time at this concealed item: display one
4460 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004461 if (syn_get_sub_char() != NUL)
4462 c = syn_get_sub_char();
4463 else if (lcs_conceal != NUL)
4464 c = lcs_conceal;
4465 else
4466 c = ' ';
4467
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004468 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004469
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004470 if (n_extra > 0)
4471 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004472 vcol += n_extra;
4473 if (wp->w_p_wrap && n_extra > 0)
4474 {
4475# ifdef FEAT_RIGHTLEFT
4476 if (wp->w_p_rl)
4477 {
4478 col -= n_extra;
4479 boguscols -= n_extra;
4480 }
4481 else
4482# endif
4483 {
4484 boguscols += n_extra;
4485 col += n_extra;
4486 }
4487 }
4488 n_extra = 0;
4489 n_attr = 0;
4490 }
4491 else if (n_skip == 0)
4492 {
4493 is_concealing = TRUE;
4494 n_skip = 1;
4495 }
4496# ifdef FEAT_MBYTE
4497 mb_c = c;
4498 if (enc_utf8 && (*mb_char2len)(c) > 1)
4499 {
4500 mb_utf8 = TRUE;
4501 u8cc[0] = 0;
4502 c = 0xc0;
4503 }
4504 else
4505 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4506# endif
4507 }
4508 else
4509 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004510 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004511 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004512 }
4513#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004516#ifdef FEAT_CONCEAL
4517 /* In the cursor line and we may be concealing characters: correct
4518 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004519 if (!did_wcol && draw_state == WL_LINE
4520 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004521 && conceal_cursor_line(wp)
4522 && (int)wp->w_virtcol <= vcol + n_skip)
4523 {
4524 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004525 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004526 did_wcol = TRUE;
4527 }
4528#endif
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /* Don't override visual selection highlighting. */
4531 if (n_attr > 0
4532 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004533 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 char_attr = extra_attr;
4535
Bram Moolenaar81695252004-12-29 20:58:21 +00004536#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 /* XIM don't send preedit_start and preedit_end, but they send
4538 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4539 * im_is_preediting() here. */
4540 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004541 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 && (State & INSERT)
4543 && !p_imdisable
4544 && im_is_preediting()
4545 && draw_state == WL_LINE)
4546 {
4547 colnr_T tcol;
4548
4549 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004550 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 else
4552 tcol = preedit_end_col;
4553 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4554 {
4555 if (feedback_old_attr < 0)
4556 {
4557 feedback_col = 0;
4558 feedback_old_attr = char_attr;
4559 }
4560 char_attr = im_get_feedback_attr(feedback_col);
4561 if (char_attr < 0)
4562 char_attr = feedback_old_attr;
4563 feedback_col++;
4564 }
4565 else if (feedback_old_attr >= 0)
4566 {
4567 char_attr = feedback_old_attr;
4568 feedback_old_attr = -1;
4569 feedback_col = 0;
4570 }
4571 }
4572#endif
4573 /*
4574 * Handle the case where we are in column 0 but not on the first
4575 * character of the line and the user wants us to show us a
4576 * special character (via 'listchars' option "precedes:<char>".
4577 */
4578 if (lcs_prec_todo != NUL
4579 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4580#ifdef FEAT_DIFF
4581 && filler_todo <= 0
4582#endif
4583 && draw_state > WL_NR
4584 && c != NUL)
4585 {
4586 c = lcs_prec;
4587 lcs_prec_todo = NUL;
4588#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004589 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4590 {
4591 /* Double-width character being overwritten by the "precedes"
4592 * character, need to fill up half the character. */
4593 c_extra = MB_FILLER_CHAR;
4594 n_extra = 1;
4595 n_attr = 2;
4596 extra_attr = hl_attr(HLF_AT);
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 mb_c = c;
4599 if (enc_utf8 && (*mb_char2len)(c) > 1)
4600 {
4601 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004602 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004603 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605 else
4606 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4607#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004608 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
4610 saved_attr3 = char_attr; /* save current attr */
4611 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4612 n_attr3 = 1;
4613 }
4614 }
4615
4616 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004617 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004619 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004620#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004621 || did_line_attr == 1
4622#endif
4623 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004625#ifdef FEAT_SEARCH_EXTRA
4626 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004627
4628 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004629 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004630 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004631#endif
4632
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004633 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 * highlight match at end of line. If it's beyond the last
4635 * char on the screen, just overwrite that one (tricky!) Not
4636 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004637#ifdef FEAT_SEARCH_EXTRA
4638 prevcol_hl_flag = FALSE;
4639 if (prevcol == (long)search_hl.startcol)
4640 prevcol_hl_flag = TRUE;
4641 else
4642 {
4643 cur = wp->w_match_head;
4644 while (cur != NULL)
4645 {
4646 if (prevcol == (long)cur->hl.startcol)
4647 {
4648 prevcol_hl_flag = TRUE;
4649 break;
4650 }
4651 cur = cur->next;
4652 }
4653 }
4654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004656 && ((area_attr != 0 && vcol == fromcol
4657#ifdef FEAT_VISUAL
4658 && (VIsual_mode != Ctrl_V
4659 || lnum == VIsual.lnum
4660 || lnum == curwin->w_cursor.lnum)
4661#endif
4662 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#ifdef FEAT_SEARCH_EXTRA
4664 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004665 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004666# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004667 && did_line_attr <= 1
4668# endif
4669 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670#endif
4671 ))
4672 {
4673 int n = 0;
4674
4675#ifdef FEAT_RIGHTLEFT
4676 if (wp->w_p_rl)
4677 {
4678 if (col < 0)
4679 n = 1;
4680 }
4681 else
4682#endif
4683 {
4684 if (col >= W_WIDTH(wp))
4685 n = -1;
4686 }
4687 if (n != 0)
4688 {
4689 /* At the window boundary, highlight the last character
4690 * instead (better than nothing). */
4691 off += n;
4692 col += n;
4693 }
4694 else
4695 {
4696 /* Add a blank character to highlight. */
4697 ScreenLines[off] = ' ';
4698#ifdef FEAT_MBYTE
4699 if (enc_utf8)
4700 ScreenLinesUC[off] = 0;
4701#endif
4702 }
4703#ifdef FEAT_SEARCH_EXTRA
4704 if (area_attr == 0)
4705 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004706 /* Use attributes from match with highest priority among
4707 * 'search_hl' and the match list. */
4708 char_attr = search_hl.attr;
4709 cur = wp->w_match_head;
4710 shl_flag = FALSE;
4711 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004712 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004713 if (shl_flag == FALSE
4714 && ((cur != NULL
4715 && cur->priority > SEARCH_HL_PRIORITY)
4716 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004717 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004718 shl = &search_hl;
4719 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004720 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004721 else
4722 shl = &cur->hl;
4723 if ((ptr - line) - 1 == (long)shl->startcol)
4724 char_attr = shl->attr;
4725 if (shl != &search_hl && cur != NULL)
4726 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
4729#endif
4730 ScreenAttrs[off] = char_attr;
4731#ifdef FEAT_RIGHTLEFT
4732 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004735 --off;
4736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 else
4738#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004741 ++off;
4742 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004743 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004744#ifdef FEAT_SYN_HL
4745 eol_hl_off = 1;
4746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
Bram Moolenaar91170f82006-05-05 21:15:17 +00004750 /*
4751 * At end of the text line.
4752 */
4753 if (c == NUL)
4754 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004755#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004756 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4757 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004758 {
4759 /* highlight last char after line */
4760 --col;
4761 --off;
4762 --vcol;
4763 }
4764
Bram Moolenaar1a384422010-07-14 19:53:30 +02004765 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004766 if (wp->w_p_wrap)
4767 v = wp->w_skipcol;
4768 else
4769 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004770
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004771 /* check if line ends before left margin */
4772 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004773 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004774#ifdef FEAT_CONCEAL
4775 /* Get rid of the boguscols now, we want to draw until the right
4776 * edge for 'cursorcolumn'. */
4777 col -= boguscols;
4778 boguscols = 0;
4779#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004780
4781 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004782 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004783
4784 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004785 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4786 && (int)wp->w_virtcol <
4787 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004788 && lnum != wp->w_cursor.lnum)
4789 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004790# ifdef FEAT_RIGHTLEFT
4791 && !wp->w_p_rl
4792# endif
4793 )
4794 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004795 int rightmost_vcol = 0;
4796 int i;
4797
4798 if (wp->w_p_cuc)
4799 rightmost_vcol = wp->w_virtcol;
4800 if (draw_color_col)
4801 /* determine rightmost colorcolumn to possibly draw */
4802 for (i = 0; color_cols[i] >= 0; ++i)
4803 if (rightmost_vcol < color_cols[i])
4804 rightmost_vcol = color_cols[i];
4805
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004806 while (col < W_WIDTH(wp))
4807 {
4808 ScreenLines[off] = ' ';
4809#ifdef FEAT_MBYTE
4810 if (enc_utf8)
4811 ScreenLinesUC[off] = 0;
4812#endif
4813 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004814 if (draw_color_col)
4815 draw_color_col = advance_color_col(VCOL_HLC,
4816 &color_cols);
4817
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004818 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004819 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004820 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004821 ScreenAttrs[off++] = hl_attr(HLF_MC);
4822 else
4823 ScreenAttrs[off++] = 0;
4824
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004825 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004826 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004827
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004828 ++vcol;
4829 }
4830 }
4831#endif
4832
Bram Moolenaar860cae12010-06-05 23:22:07 +02004833 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4834 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 row++;
4836
4837 /*
4838 * Update w_cline_height and w_cline_folded if the cursor line was
4839 * updated (saves a call to plines() later).
4840 */
4841 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4842 {
4843 curwin->w_cline_row = startrow;
4844 curwin->w_cline_height = row - startrow;
4845#ifdef FEAT_FOLDING
4846 curwin->w_cline_folded = FALSE;
4847#endif
4848 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4849 }
4850
4851 break;
4852 }
4853
4854 /* line continues beyond line end */
4855 if (lcs_ext
4856 && !wp->w_p_wrap
4857#ifdef FEAT_DIFF
4858 && filler_todo <= 0
4859#endif
4860 && (
4861#ifdef FEAT_RIGHTLEFT
4862 wp->w_p_rl ? col == 0 :
4863#endif
4864 col == W_WIDTH(wp) - 1)
4865 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004866 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4868 {
4869 c = lcs_ext;
4870 char_attr = hl_attr(HLF_AT);
4871#ifdef FEAT_MBYTE
4872 mb_c = c;
4873 if (enc_utf8 && (*mb_char2len)(c) > 1)
4874 {
4875 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004876 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004877 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 else
4880 mb_utf8 = FALSE;
4881#endif
4882 }
4883
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004884#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004885 /* advance to the next 'colorcolumn' */
4886 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004887 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004888
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004889 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004890 * highlight the cursor position itself.
4891 * Also highlight the 'colorcolumn' if it is different than
4892 * 'cursorcolumn' */
4893 vcol_save_attr = -1;
4894 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004895 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004896 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004897 && lnum != wp->w_cursor.lnum)
4898 {
4899 vcol_save_attr = char_attr;
4900 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4901 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004902 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004903 {
4904 vcol_save_attr = char_attr;
4905 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4906 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004907 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004908#endif
4909
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 /*
4911 * Store character to be displayed.
4912 * Skip characters that are left of the screen for 'nowrap'.
4913 */
4914 vcol_prev = vcol;
4915 if (draw_state < WL_LINE || n_skip <= 0)
4916 {
4917 /*
4918 * Store the character.
4919 */
4920#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4921 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4922 {
4923 /* A double-wide character is: put first halve in left cell. */
4924 --off;
4925 --col;
4926 }
4927#endif
4928 ScreenLines[off] = c;
4929#ifdef FEAT_MBYTE
4930 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004931 {
4932 if ((mb_c & 0xff00) == 0x8e00)
4933 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 else if (enc_utf8)
4937 {
4938 if (mb_utf8)
4939 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004940 int i;
4941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004943 if ((c & 0xff) == 0)
4944 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004945 for (i = 0; i < Screen_mco; ++i)
4946 {
4947 ScreenLinesC[i][off] = u8cc[i];
4948 if (u8cc[i] == 0)
4949 break;
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else
4953 ScreenLinesUC[off] = 0;
4954 }
4955 if (multi_attr)
4956 {
4957 ScreenAttrs[off] = multi_attr;
4958 multi_attr = 0;
4959 }
4960 else
4961#endif
4962 ScreenAttrs[off] = char_attr;
4963
4964#ifdef FEAT_MBYTE
4965 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4966 {
4967 /* Need to fill two screen columns. */
4968 ++off;
4969 ++col;
4970 if (enc_utf8)
4971 /* UTF-8: Put a 0 in the second screen char. */
4972 ScreenLines[off] = 0;
4973 else
4974 /* DBCS: Put second byte in the second screen char. */
4975 ScreenLines[off] = mb_c & 0xff;
4976 ++vcol;
4977 /* When "tocol" is halfway a character, set it to the end of
4978 * the character, otherwise highlighting won't stop. */
4979 if (tocol == vcol)
4980 ++tocol;
4981#ifdef FEAT_RIGHTLEFT
4982 if (wp->w_p_rl)
4983 {
4984 /* now it's time to backup one cell */
4985 --off;
4986 --col;
4987 }
4988#endif
4989 }
4990#endif
4991#ifdef FEAT_RIGHTLEFT
4992 if (wp->w_p_rl)
4993 {
4994 --off;
4995 --col;
4996 }
4997 else
4998#endif
4999 {
5000 ++off;
5001 ++col;
5002 }
5003 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005004#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005005 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005006 {
5007 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005008 ++vcol_off;
5009 if (n_extra > 0)
5010 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005011 if (wp->w_p_wrap)
5012 {
5013 /*
5014 * Special voodoo required if 'wrap' is on.
5015 *
5016 * Advance the column indicator to force the line
5017 * drawing to wrap early. This will make the line
5018 * take up the same screen space when parts are concealed,
5019 * so that cursor line computations aren't messed up.
5020 *
5021 * To avoid the fictitious advance of 'col' causing
5022 * trailing junk to be written out of the screen line
5023 * we are building, 'boguscols' keeps track of the number
5024 * of bad columns we have advanced.
5025 */
5026 if (n_extra > 0)
5027 {
5028 vcol += n_extra;
5029# ifdef FEAT_RIGHTLEFT
5030 if (wp->w_p_rl)
5031 {
5032 col -= n_extra;
5033 boguscols -= n_extra;
5034 }
5035 else
5036# endif
5037 {
5038 col += n_extra;
5039 boguscols += n_extra;
5040 }
5041 n_extra = 0;
5042 n_attr = 0;
5043 }
5044
5045
5046# ifdef FEAT_MBYTE
5047 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5048 {
5049 /* Need to fill two screen columns. */
5050# ifdef FEAT_RIGHTLEFT
5051 if (wp->w_p_rl)
5052 {
5053 --boguscols;
5054 --col;
5055 }
5056 else
5057# endif
5058 {
5059 ++boguscols;
5060 ++col;
5061 }
5062 }
5063# endif
5064
5065# ifdef FEAT_RIGHTLEFT
5066 if (wp->w_p_rl)
5067 {
5068 --boguscols;
5069 --col;
5070 }
5071 else
5072# endif
5073 {
5074 ++boguscols;
5075 ++col;
5076 }
5077 }
5078 else
5079 {
5080 if (n_extra > 0)
5081 {
5082 vcol += n_extra;
5083 n_extra = 0;
5084 n_attr = 0;
5085 }
5086 }
5087
5088 }
5089#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 else
5091 --n_skip;
5092
Bram Moolenaar64486672010-05-16 15:46:46 +02005093 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5094 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005095 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096#ifdef FEAT_DIFF
5097 && filler_todo <= 0
5098#endif
5099 )
5100 ++vcol;
5101
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005102#ifdef FEAT_SYN_HL
5103 if (vcol_save_attr >= 0)
5104 char_attr = vcol_save_attr;
5105#endif
5106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 /* restore attributes after "predeces" in 'listchars' */
5108 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5109 char_attr = saved_attr3;
5110
5111 /* restore attributes after last 'listchars' or 'number' char */
5112 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5113 char_attr = saved_attr2;
5114
5115 /*
5116 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005117 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 */
5119 if ((
5120#ifdef FEAT_RIGHTLEFT
5121 wp->w_p_rl ? (col < 0) :
5122#endif
5123 (col >= W_WIDTH(wp)))
5124 && (*ptr != NUL
5125#ifdef FEAT_DIFF
5126 || filler_todo > 0
5127#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005128 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5130 )
5131 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005132#ifdef FEAT_CONCEAL
5133 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5134 (int)W_WIDTH(wp), wp->w_p_rl);
5135 boguscols = 0;
5136#else
5137 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5138 (int)W_WIDTH(wp), wp->w_p_rl);
5139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 ++row;
5141 ++screen_row;
5142
5143 /* When not wrapping and finished diff lines, or when displayed
5144 * '$' and highlighting until last column, break here. */
5145 if ((!wp->w_p_wrap
5146#ifdef FEAT_DIFF
5147 && filler_todo <= 0
5148#endif
5149 ) || lcs_eol_one == -1)
5150 break;
5151
5152 /* When the window is too narrow draw all "@" lines. */
5153 if (draw_state != WL_LINE
5154#ifdef FEAT_DIFF
5155 && filler_todo <= 0
5156#endif
5157 )
5158 {
5159 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5160#ifdef FEAT_VERTSPLIT
5161 draw_vsep_win(wp, row);
5162#endif
5163 row = endrow;
5164 }
5165
5166 /* When line got too long for screen break here. */
5167 if (row == endrow)
5168 {
5169 ++row;
5170 break;
5171 }
5172
5173 if (screen_cur_row == screen_row - 1
5174#ifdef FEAT_DIFF
5175 && filler_todo <= 0
5176#endif
5177 && W_WIDTH(wp) == Columns)
5178 {
5179 /* Remember that the line wraps, used for modeless copy. */
5180 LineWraps[screen_row - 1] = TRUE;
5181
5182 /*
5183 * Special trick to make copy/paste of wrapped lines work with
5184 * xterm/screen: write an extra character beyond the end of
5185 * the line. This will work with all terminal types
5186 * (regardless of the xn,am settings).
5187 * Only do this on a fast tty.
5188 * Only do this if the cursor is on the current line
5189 * (something has been written in it).
5190 * Don't do this for the GUI.
5191 * Don't do this for double-width characters.
5192 * Don't do this for a window not at the right screen border.
5193 */
5194 if (p_tf
5195#ifdef FEAT_GUI
5196 && !gui.in_use
5197#endif
5198#ifdef FEAT_MBYTE
5199 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005200 && ((*mb_off2cells)(LineOffset[screen_row],
5201 LineOffset[screen_row] + screen_Columns)
5202 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005204 + (int)Columns - 2,
5205 LineOffset[screen_row] + screen_Columns)
5206 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#endif
5208 )
5209 {
5210 /* First make sure we are at the end of the screen line,
5211 * then output the same character again to let the
5212 * terminal know about the wrap. If the terminal doesn't
5213 * auto-wrap, we overwrite the character. */
5214 if (screen_cur_col != W_WIDTH(wp))
5215 screen_char(LineOffset[screen_row - 1]
5216 + (unsigned)Columns - 1,
5217 screen_row - 1, (int)(Columns - 1));
5218
5219#ifdef FEAT_MBYTE
5220 /* When there is a multi-byte character, just output a
5221 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005222 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5223 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 out_char(' ');
5225 else
5226#endif
5227 out_char(ScreenLines[LineOffset[screen_row - 1]
5228 + (Columns - 1)]);
5229 /* force a redraw of the first char on the next line */
5230 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5231 screen_start(); /* don't know where cursor is now */
5232 }
5233 }
5234
5235 col = 0;
5236 off = (unsigned)(current_ScreenLine - ScreenLines);
5237#ifdef FEAT_RIGHTLEFT
5238 if (wp->w_p_rl)
5239 {
5240 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5241 off += col;
5242 }
5243#endif
5244
5245 /* reset the drawing state for the start of a wrapped line */
5246 draw_state = WL_START;
5247 saved_n_extra = n_extra;
5248 saved_p_extra = p_extra;
5249 saved_c_extra = c_extra;
5250 saved_char_attr = char_attr;
5251 n_extra = 0;
5252 lcs_prec_todo = lcs_prec;
5253#ifdef FEAT_LINEBREAK
5254# ifdef FEAT_DIFF
5255 if (filler_todo <= 0)
5256# endif
5257 need_showbreak = TRUE;
5258#endif
5259#ifdef FEAT_DIFF
5260 --filler_todo;
5261 /* When the filler lines are actually below the last line of the
5262 * file, don't draw the line itself, break here. */
5263 if (filler_todo == 0 && wp->w_botfill)
5264 break;
5265#endif
5266 }
5267
5268 } /* for every character in the line */
5269
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005270#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005271 /* After an empty line check first word for capital. */
5272 if (*skipwhite(line) == NUL)
5273 {
5274 capcol_lnum = lnum + 1;
5275 cap_col = 0;
5276 }
5277#endif
5278
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 return row;
5280}
5281
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005282#ifdef FEAT_MBYTE
5283static int comp_char_differs __ARGS((int, int));
5284
5285/*
5286 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005287 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005288 */
5289 static int
5290comp_char_differs(off_from, off_to)
5291 int off_from;
5292 int off_to;
5293{
5294 int i;
5295
5296 for (i = 0; i < Screen_mco; ++i)
5297 {
5298 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5299 return TRUE;
5300 if (ScreenLinesC[i][off_from] == 0)
5301 break;
5302 }
5303 return FALSE;
5304}
5305#endif
5306
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307/*
5308 * Check whether the given character needs redrawing:
5309 * - the (first byte of the) character is different
5310 * - the attributes are different
5311 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005312 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 */
5314 static int
5315char_needs_redraw(off_from, off_to, cols)
5316 int off_from;
5317 int off_to;
5318 int cols;
5319{
5320 if (cols > 0
5321 && ((ScreenLines[off_from] != ScreenLines[off_to]
5322 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5323
5324#ifdef FEAT_MBYTE
5325 || (enc_dbcs != 0
5326 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5327 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5328 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5329 : (cols > 1 && ScreenLines[off_from + 1]
5330 != ScreenLines[off_to + 1])))
5331 || (enc_utf8
5332 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5333 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005334 && comp_char_differs(off_from, off_to))
5335 || (cols > 1 && ScreenLines[off_from + 1]
5336 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337#endif
5338 ))
5339 return TRUE;
5340 return FALSE;
5341}
5342
5343/*
5344 * Move one "cooked" screen line to the screen, but only the characters that
5345 * have actually changed. Handle insert/delete character.
5346 * "coloff" gives the first column on the screen for this line.
5347 * "endcol" gives the columns where valid characters are.
5348 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5349 * needs to be cleared, negative otherwise.
5350 * "rlflag" is TRUE in a rightleft window:
5351 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5352 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5353 */
5354 static void
5355screen_line(row, coloff, endcol, clear_width
5356#ifdef FEAT_RIGHTLEFT
5357 , rlflag
5358#endif
5359 )
5360 int row;
5361 int coloff;
5362 int endcol;
5363 int clear_width;
5364#ifdef FEAT_RIGHTLEFT
5365 int rlflag;
5366#endif
5367{
5368 unsigned off_from;
5369 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005370#ifdef FEAT_MBYTE
5371 unsigned max_off_from;
5372 unsigned max_off_to;
5373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 int col = 0;
5375#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5376 int hl;
5377#endif
5378 int force = FALSE; /* force update rest of the line */
5379 int redraw_this /* bool: does character need redraw? */
5380#ifdef FEAT_GUI
5381 = TRUE /* For GUI when while-loop empty */
5382#endif
5383 ;
5384 int redraw_next; /* redraw_this for next character */
5385#ifdef FEAT_MBYTE
5386 int clear_next = FALSE;
5387 int char_cells; /* 1: normal char */
5388 /* 2: occupies two display cells */
5389# define CHAR_CELLS char_cells
5390#else
5391# define CHAR_CELLS 1
5392#endif
5393
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005394 /* Check for illegal row and col, just in case. */
5395 if (row >= Rows)
5396 row = Rows - 1;
5397 if (endcol > Columns)
5398 endcol = Columns;
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400# ifdef FEAT_CLIPBOARD
5401 clip_may_clear_selection(row, row);
5402# endif
5403
5404 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5405 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005406#ifdef FEAT_MBYTE
5407 max_off_from = off_from + screen_Columns;
5408 max_off_to = LineOffset[row] + screen_Columns;
5409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411#ifdef FEAT_RIGHTLEFT
5412 if (rlflag)
5413 {
5414 /* Clear rest first, because it's left of the text. */
5415 if (clear_width > 0)
5416 {
5417 while (col <= endcol && ScreenLines[off_to] == ' '
5418 && ScreenAttrs[off_to] == 0
5419# ifdef FEAT_MBYTE
5420 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5421# endif
5422 )
5423 {
5424 ++off_to;
5425 ++col;
5426 }
5427 if (col <= endcol)
5428 screen_fill(row, row + 1, col + coloff,
5429 endcol + coloff + 1, ' ', ' ', 0);
5430 }
5431 col = endcol + 1;
5432 off_to = LineOffset[row] + col + coloff;
5433 off_from += col;
5434 endcol = (clear_width > 0 ? clear_width : -clear_width);
5435 }
5436#endif /* FEAT_RIGHTLEFT */
5437
5438 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5439
5440 while (col < endcol)
5441 {
5442#ifdef FEAT_MBYTE
5443 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005444 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 else
5446 char_cells = 1;
5447#endif
5448
5449 redraw_this = redraw_next;
5450 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5451 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5452
5453#ifdef FEAT_GUI
5454 /* If the next character was bold, then redraw the current character to
5455 * remove any pixels that might have spilt over into us. This only
5456 * happens in the GUI.
5457 */
5458 if (redraw_next && gui.in_use)
5459 {
5460 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005461 if (hl > HL_ALL)
5462 hl = syn_attr2attr(hl);
5463 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 redraw_this = TRUE;
5465 }
5466#endif
5467
5468 if (redraw_this)
5469 {
5470 /*
5471 * Special handling when 'xs' termcap flag set (hpterm):
5472 * Attributes for characters are stored at the position where the
5473 * cursor is when writing the highlighting code. The
5474 * start-highlighting code must be written with the cursor on the
5475 * first highlighted character. The stop-highlighting code must
5476 * be written with the cursor just after the last highlighted
5477 * character.
5478 * Overwriting a character doesn't remove it's highlighting. Need
5479 * to clear the rest of the line, and force redrawing it
5480 * completely.
5481 */
5482 if ( p_wiv
5483 && !force
5484#ifdef FEAT_GUI
5485 && !gui.in_use
5486#endif
5487 && ScreenAttrs[off_to] != 0
5488 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5489 {
5490 /*
5491 * Need to remove highlighting attributes here.
5492 */
5493 windgoto(row, col + coloff);
5494 out_str(T_CE); /* clear rest of this screen line */
5495 screen_start(); /* don't know where cursor is now */
5496 force = TRUE; /* force redraw of rest of the line */
5497 redraw_next = TRUE; /* or else next char would miss out */
5498
5499 /*
5500 * If the previous character was highlighted, need to stop
5501 * highlighting at this character.
5502 */
5503 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5504 {
5505 screen_attr = ScreenAttrs[off_to - 1];
5506 term_windgoto(row, col + coloff);
5507 screen_stop_highlight();
5508 }
5509 else
5510 screen_attr = 0; /* highlighting has stopped */
5511 }
5512#ifdef FEAT_MBYTE
5513 if (enc_dbcs != 0)
5514 {
5515 /* Check if overwriting a double-byte with a single-byte or
5516 * the other way around requires another character to be
5517 * redrawn. For UTF-8 this isn't needed, because comparing
5518 * ScreenLinesUC[] is sufficient. */
5519 if (char_cells == 1
5520 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005521 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
5523 /* Writing a single-cell character over a double-cell
5524 * character: need to redraw the next cell. */
5525 ScreenLines[off_to + 1] = 0;
5526 redraw_next = TRUE;
5527 }
5528 else if (char_cells == 2
5529 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005530 && (*mb_off2cells)(off_to, max_off_to) == 1
5531 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 {
5533 /* Writing the second half of a double-cell character over
5534 * a double-cell character: need to redraw the second
5535 * cell. */
5536 ScreenLines[off_to + 2] = 0;
5537 redraw_next = TRUE;
5538 }
5539
5540 if (enc_dbcs == DBCS_JPNU)
5541 ScreenLines2[off_to] = ScreenLines2[off_from];
5542 }
5543 /* When writing a single-width character over a double-width
5544 * character and at the end of the redrawn text, need to clear out
5545 * the right halve of the old character.
5546 * Also required when writing the right halve of a double-width
5547 * char over the left halve of an existing one. */
5548 if (has_mbyte && col + char_cells == endcol
5549 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005550 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005552 && (*mb_off2cells)(off_to, max_off_to) == 1
5553 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 clear_next = TRUE;
5555#endif
5556
5557 ScreenLines[off_to] = ScreenLines[off_from];
5558#ifdef FEAT_MBYTE
5559 if (enc_utf8)
5560 {
5561 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5562 if (ScreenLinesUC[off_from] != 0)
5563 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005564 int i;
5565
5566 for (i = 0; i < Screen_mco; ++i)
5567 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 }
5570 if (char_cells == 2)
5571 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5572#endif
5573
5574#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005575 /* The bold trick makes a single column of pixels appear in the
5576 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 * character should be redrawn too. This happens for our own GUI
5578 * and for some xterms. */
5579 if (
5580# ifdef FEAT_GUI
5581 gui.in_use
5582# endif
5583# if defined(FEAT_GUI) && defined(UNIX)
5584 ||
5585# endif
5586# ifdef UNIX
5587 term_is_xterm
5588# endif
5589 )
5590 {
5591 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005592 if (hl > HL_ALL)
5593 hl = syn_attr2attr(hl);
5594 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 redraw_next = TRUE;
5596 }
5597#endif
5598 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5599#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005600 /* For simplicity set the attributes of second half of a
5601 * double-wide character equal to the first half. */
5602 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005604
5605 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 else
5608#endif
5609 screen_char(off_to, row, col + coloff);
5610 }
5611 else if ( p_wiv
5612#ifdef FEAT_GUI
5613 && !gui.in_use
5614#endif
5615 && col + coloff > 0)
5616 {
5617 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5618 {
5619 /*
5620 * Don't output stop-highlight when moving the cursor, it will
5621 * stop the highlighting when it should continue.
5622 */
5623 screen_attr = 0;
5624 }
5625 else if (screen_attr != 0)
5626 screen_stop_highlight();
5627 }
5628
5629 off_to += CHAR_CELLS;
5630 off_from += CHAR_CELLS;
5631 col += CHAR_CELLS;
5632 }
5633
5634#ifdef FEAT_MBYTE
5635 if (clear_next)
5636 {
5637 /* Clear the second half of a double-wide character of which the left
5638 * half was overwritten with a single-wide character. */
5639 ScreenLines[off_to] = ' ';
5640 if (enc_utf8)
5641 ScreenLinesUC[off_to] = 0;
5642 screen_char(off_to, row, col + coloff);
5643 }
5644#endif
5645
5646 if (clear_width > 0
5647#ifdef FEAT_RIGHTLEFT
5648 && !rlflag
5649#endif
5650 )
5651 {
5652#ifdef FEAT_GUI
5653 int startCol = col;
5654#endif
5655
5656 /* blank out the rest of the line */
5657 while (col < clear_width && ScreenLines[off_to] == ' '
5658 && ScreenAttrs[off_to] == 0
5659#ifdef FEAT_MBYTE
5660 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5661#endif
5662 )
5663 {
5664 ++off_to;
5665 ++col;
5666 }
5667 if (col < clear_width)
5668 {
5669#ifdef FEAT_GUI
5670 /*
5671 * In the GUI, clearing the rest of the line may leave pixels
5672 * behind if the first character cleared was bold. Some bold
5673 * fonts spill over the left. In this case we redraw the previous
5674 * character too. If we didn't skip any blanks above, then we
5675 * only redraw if the character wasn't already redrawn anyway.
5676 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005677 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {
5679 hl = ScreenAttrs[off_to];
5680 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005681 {
5682 int prev_cells = 1;
5683# ifdef FEAT_MBYTE
5684 if (enc_utf8)
5685 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5686 * that its width is 2. */
5687 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5688 else if (enc_dbcs != 0)
5689 {
5690 /* find previous character by counting from first
5691 * column and get its width. */
5692 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005693 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005694
5695 while (off < off_to)
5696 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005697 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005698 off += prev_cells;
5699 }
5700 }
5701
5702 if (enc_dbcs != 0 && prev_cells > 1)
5703 screen_char_2(off_to - prev_cells, row,
5704 col + coloff - prev_cells);
5705 else
5706# endif
5707 screen_char(off_to - prev_cells, row,
5708 col + coloff - prev_cells);
5709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711#endif
5712 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5713 ' ', ' ', 0);
5714#ifdef FEAT_VERTSPLIT
5715 off_to += clear_width - col;
5716 col = clear_width;
5717#endif
5718 }
5719 }
5720
5721 if (clear_width > 0)
5722 {
5723#ifdef FEAT_VERTSPLIT
5724 /* For a window that's left of another, draw the separator char. */
5725 if (col + coloff < Columns)
5726 {
5727 int c;
5728
5729 c = fillchar_vsep(&hl);
5730 if (ScreenLines[off_to] != c
5731# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005732 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5733 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734# endif
5735 || ScreenAttrs[off_to] != hl)
5736 {
5737 ScreenLines[off_to] = c;
5738 ScreenAttrs[off_to] = hl;
5739# ifdef FEAT_MBYTE
5740 if (enc_utf8)
5741 {
5742 if (c >= 0x80)
5743 {
5744 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005745 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
5747 else
5748 ScreenLinesUC[off_to] = 0;
5749 }
5750# endif
5751 screen_char(off_to, row, col + coloff);
5752 }
5753 }
5754 else
5755#endif
5756 LineWraps[row] = FALSE;
5757 }
5758}
5759
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005760#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005762 * Mirror text "str" for right-left displaying.
5763 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005765 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766rl_mirror(str)
5767 char_u *str;
5768{
5769 char_u *p1, *p2;
5770 int t;
5771
5772 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5773 {
5774 t = *p1;
5775 *p1 = *p2;
5776 *p2 = t;
5777 }
5778}
5779#endif
5780
5781#if defined(FEAT_WINDOWS) || defined(PROTO)
5782/*
5783 * mark all status lines for redraw; used after first :cd
5784 */
5785 void
5786status_redraw_all()
5787{
5788 win_T *wp;
5789
5790 for (wp = firstwin; wp; wp = wp->w_next)
5791 if (wp->w_status_height)
5792 {
5793 wp->w_redr_status = TRUE;
5794 redraw_later(VALID);
5795 }
5796}
5797
5798/*
5799 * mark all status lines of the current buffer for redraw
5800 */
5801 void
5802status_redraw_curbuf()
5803{
5804 win_T *wp;
5805
5806 for (wp = firstwin; wp; wp = wp->w_next)
5807 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5808 {
5809 wp->w_redr_status = TRUE;
5810 redraw_later(VALID);
5811 }
5812}
5813
5814/*
5815 * Redraw all status lines that need to be redrawn.
5816 */
5817 void
5818redraw_statuslines()
5819{
5820 win_T *wp;
5821
5822 for (wp = firstwin; wp; wp = wp->w_next)
5823 if (wp->w_redr_status)
5824 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005825 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005826 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827}
5828#endif
5829
5830#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5831/*
5832 * Redraw all status lines at the bottom of frame "frp".
5833 */
5834 void
5835win_redraw_last_status(frp)
5836 frame_T *frp;
5837{
5838 if (frp->fr_layout == FR_LEAF)
5839 frp->fr_win->w_redr_status = TRUE;
5840 else if (frp->fr_layout == FR_ROW)
5841 {
5842 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5843 win_redraw_last_status(frp);
5844 }
5845 else /* frp->fr_layout == FR_COL */
5846 {
5847 frp = frp->fr_child;
5848 while (frp->fr_next != NULL)
5849 frp = frp->fr_next;
5850 win_redraw_last_status(frp);
5851 }
5852}
5853#endif
5854
5855#ifdef FEAT_VERTSPLIT
5856/*
5857 * Draw the verticap separator right of window "wp" starting with line "row".
5858 */
5859 static void
5860draw_vsep_win(wp, row)
5861 win_T *wp;
5862 int row;
5863{
5864 int hl;
5865 int c;
5866
5867 if (wp->w_vsep_width)
5868 {
5869 /* draw the vertical separator right of this window */
5870 c = fillchar_vsep(&hl);
5871 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5872 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5873 c, ' ', hl);
5874 }
5875}
5876#endif
5877
5878#ifdef FEAT_WILDMENU
5879static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005880static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
5882/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005883 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 */
5885 static int
5886status_match_len(xp, s)
5887 expand_T *xp;
5888 char_u *s;
5889{
5890 int len = 0;
5891
5892#ifdef FEAT_MENU
5893 int emenu = (xp->xp_context == EXPAND_MENUS
5894 || xp->xp_context == EXPAND_MENUNAMES);
5895
5896 /* Check for menu separators - replace with '|'. */
5897 if (emenu && menu_is_separator(s))
5898 return 1;
5899#endif
5900
5901 while (*s != NUL)
5902 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005903 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005904 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005905 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
5907
5908 return len;
5909}
5910
5911/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005912 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005913 * These are backslashes used for escaping. Do show backslashes in help tags.
5914 */
5915 static int
5916skip_status_match_char(xp, s)
5917 expand_T *xp;
5918 char_u *s;
5919{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005920 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005921#ifdef FEAT_MENU
5922 || ((xp->xp_context == EXPAND_MENUS
5923 || xp->xp_context == EXPAND_MENUNAMES)
5924 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5925#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005926 )
5927 {
5928#ifndef BACKSLASH_IN_FILENAME
5929 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5930 return 2;
5931#endif
5932 return 1;
5933 }
5934 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005935}
5936
5937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 * Show wildchar matches in the status line.
5939 * Show at least the "match" item.
5940 * We start at item 'first_match' in the list and show all matches that fit.
5941 *
5942 * If inversion is possible we use it. Else '=' characters are used.
5943 */
5944 void
5945win_redr_status_matches(xp, num_matches, matches, match, showtail)
5946 expand_T *xp;
5947 int num_matches;
5948 char_u **matches; /* list of matches */
5949 int match;
5950 int showtail;
5951{
5952#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5953 int row;
5954 char_u *buf;
5955 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005956 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 int fillchar;
5958 int attr;
5959 int i;
5960 int highlight = TRUE;
5961 char_u *selstart = NULL;
5962 int selstart_col = 0;
5963 char_u *selend = NULL;
5964 static int first_match = 0;
5965 int add_left = FALSE;
5966 char_u *s;
5967#ifdef FEAT_MENU
5968 int emenu;
5969#endif
5970#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5971 int l;
5972#endif
5973
5974 if (matches == NULL) /* interrupted completion? */
5975 return;
5976
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005977#ifdef FEAT_MBYTE
5978 if (has_mbyte)
5979 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5980 else
5981#endif
5982 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 if (buf == NULL)
5984 return;
5985
5986 if (match == -1) /* don't show match but original text */
5987 {
5988 match = 0;
5989 highlight = FALSE;
5990 }
5991 /* count 1 for the ending ">" */
5992 clen = status_match_len(xp, L_MATCH(match)) + 3;
5993 if (match == 0)
5994 first_match = 0;
5995 else if (match < first_match)
5996 {
5997 /* jumping left, as far as we can go */
5998 first_match = match;
5999 add_left = TRUE;
6000 }
6001 else
6002 {
6003 /* check if match fits on the screen */
6004 for (i = first_match; i < match; ++i)
6005 clen += status_match_len(xp, L_MATCH(i)) + 2;
6006 if (first_match > 0)
6007 clen += 2;
6008 /* jumping right, put match at the left */
6009 if ((long)clen > Columns)
6010 {
6011 first_match = match;
6012 /* if showing the last match, we can add some on the left */
6013 clen = 2;
6014 for (i = match; i < num_matches; ++i)
6015 {
6016 clen += status_match_len(xp, L_MATCH(i)) + 2;
6017 if ((long)clen >= Columns)
6018 break;
6019 }
6020 if (i == num_matches)
6021 add_left = TRUE;
6022 }
6023 }
6024 if (add_left)
6025 while (first_match > 0)
6026 {
6027 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6028 if ((long)clen >= Columns)
6029 break;
6030 --first_match;
6031 }
6032
6033 fillchar = fillchar_status(&attr, TRUE);
6034
6035 if (first_match == 0)
6036 {
6037 *buf = NUL;
6038 len = 0;
6039 }
6040 else
6041 {
6042 STRCPY(buf, "< ");
6043 len = 2;
6044 }
6045 clen = len;
6046
6047 i = first_match;
6048 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6049 {
6050 if (i == match)
6051 {
6052 selstart = buf + len;
6053 selstart_col = clen;
6054 }
6055
6056 s = L_MATCH(i);
6057 /* Check for menu separators - replace with '|' */
6058#ifdef FEAT_MENU
6059 emenu = (xp->xp_context == EXPAND_MENUS
6060 || xp->xp_context == EXPAND_MENUNAMES);
6061 if (emenu && menu_is_separator(s))
6062 {
6063 STRCPY(buf + len, transchar('|'));
6064 l = (int)STRLEN(buf + len);
6065 len += l;
6066 clen += l;
6067 }
6068 else
6069#endif
6070 for ( ; *s != NUL; ++s)
6071 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006072 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 clen += ptr2cells(s);
6074#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006075 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
6077 STRNCPY(buf + len, s, l);
6078 s += l - 1;
6079 len += l;
6080 }
6081 else
6082#endif
6083 {
6084 STRCPY(buf + len, transchar_byte(*s));
6085 len += (int)STRLEN(buf + len);
6086 }
6087 }
6088 if (i == match)
6089 selend = buf + len;
6090
6091 *(buf + len++) = ' ';
6092 *(buf + len++) = ' ';
6093 clen += 2;
6094 if (++i == num_matches)
6095 break;
6096 }
6097
6098 if (i != num_matches)
6099 {
6100 *(buf + len++) = '>';
6101 ++clen;
6102 }
6103
6104 buf[len] = NUL;
6105
6106 row = cmdline_row - 1;
6107 if (row >= 0)
6108 {
6109 if (wild_menu_showing == 0)
6110 {
6111 if (msg_scrolled > 0)
6112 {
6113 /* Put the wildmenu just above the command line. If there is
6114 * no room, scroll the screen one line up. */
6115 if (cmdline_row == Rows - 1)
6116 {
6117 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6118 ++msg_scrolled;
6119 }
6120 else
6121 {
6122 ++cmdline_row;
6123 ++row;
6124 }
6125 wild_menu_showing = WM_SCROLLED;
6126 }
6127 else
6128 {
6129 /* Create status line if needed by setting 'laststatus' to 2.
6130 * Set 'winminheight' to zero to avoid that the window is
6131 * resized. */
6132 if (lastwin->w_status_height == 0)
6133 {
6134 save_p_ls = p_ls;
6135 save_p_wmh = p_wmh;
6136 p_ls = 2;
6137 p_wmh = 0;
6138 last_status(FALSE);
6139 }
6140 wild_menu_showing = WM_SHOWN;
6141 }
6142 }
6143
6144 screen_puts(buf, row, 0, attr);
6145 if (selstart != NULL && highlight)
6146 {
6147 *selend = NUL;
6148 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6149 }
6150
6151 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6152 }
6153
6154#ifdef FEAT_VERTSPLIT
6155 win_redraw_last_status(topframe);
6156#else
6157 lastwin->w_redr_status = TRUE;
6158#endif
6159 vim_free(buf);
6160}
6161#endif
6162
6163#if defined(FEAT_WINDOWS) || defined(PROTO)
6164/*
6165 * Redraw the status line of window wp.
6166 *
6167 * If inversion is possible we use it. Else '=' characters are used.
6168 */
6169 void
6170win_redr_status(wp)
6171 win_T *wp;
6172{
6173 int row;
6174 char_u *p;
6175 int len;
6176 int fillchar;
6177 int attr;
6178 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006179 static int busy = FALSE;
6180
6181 /* It's possible to get here recursively when 'statusline' (indirectly)
6182 * invokes ":redrawstatus". Simply ignore the call then. */
6183 if (busy)
6184 return;
6185 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186
6187 wp->w_redr_status = FALSE;
6188 if (wp->w_status_height == 0)
6189 {
6190 /* no status line, can only be last window */
6191 redraw_cmdline = TRUE;
6192 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006193 else if (!redrawing()
6194#ifdef FEAT_INS_EXPAND
6195 /* don't update status line when popup menu is visible and may be
6196 * drawn over it */
6197 || pum_visible()
6198#endif
6199 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
6201 /* Don't redraw right now, do it later. */
6202 wp->w_redr_status = TRUE;
6203 }
6204#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006205 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
6207 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006208 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210#endif
6211 else
6212 {
6213 fillchar = fillchar_status(&attr, wp == curwin);
6214
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006215 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 p = NameBuff;
6217 len = (int)STRLEN(p);
6218
6219 if (wp->w_buffer->b_help
6220#ifdef FEAT_QUICKFIX
6221 || wp->w_p_pvw
6222#endif
6223 || bufIsChanged(wp->w_buffer)
6224 || wp->w_buffer->b_p_ro)
6225 *(p + len++) = ' ';
6226 if (wp->w_buffer->b_help)
6227 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006228 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 len += (int)STRLEN(p + len);
6230 }
6231#ifdef FEAT_QUICKFIX
6232 if (wp->w_p_pvw)
6233 {
6234 STRCPY(p + len, _("[Preview]"));
6235 len += (int)STRLEN(p + len);
6236 }
6237#endif
6238 if (bufIsChanged(wp->w_buffer))
6239 {
6240 STRCPY(p + len, "[+]");
6241 len += 3;
6242 }
6243 if (wp->w_buffer->b_p_ro)
6244 {
6245 STRCPY(p + len, "[RO]");
6246 len += 4;
6247 }
6248
6249#ifndef FEAT_VERTSPLIT
6250 this_ru_col = ru_col;
6251 if (this_ru_col < (Columns + 1) / 2)
6252 this_ru_col = (Columns + 1) / 2;
6253#else
6254 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6255 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6256 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6257 if (this_ru_col <= 1)
6258 {
6259 p = (char_u *)"<"; /* No room for file name! */
6260 len = 1;
6261 }
6262 else
6263#endif
6264#ifdef FEAT_MBYTE
6265 if (has_mbyte)
6266 {
6267 int clen = 0, i;
6268
6269 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006270 clen = mb_string2cells(p, -1);
6271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 /* Find first character that will fit.
6273 * Going from start to end is much faster for DBCS. */
6274 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006275 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 clen -= (*mb_ptr2cells)(p + i);
6277 len = clen;
6278 if (i > 0)
6279 {
6280 p = p + i - 1;
6281 *p = '<';
6282 ++len;
6283 }
6284
6285 }
6286 else
6287#endif
6288 if (len > this_ru_col - 1)
6289 {
6290 p += len - (this_ru_col - 1);
6291 *p = '<';
6292 len = this_ru_col - 1;
6293 }
6294
6295 row = W_WINROW(wp) + wp->w_height;
6296 screen_puts(p, row, W_WINCOL(wp), attr);
6297 screen_fill(row, row + 1, len + W_WINCOL(wp),
6298 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6299
6300 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6301 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6302 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6303 - 1 + W_WINCOL(wp)), attr);
6304
6305#ifdef FEAT_CMDL_INFO
6306 win_redr_ruler(wp, TRUE);
6307#endif
6308 }
6309
6310#ifdef FEAT_VERTSPLIT
6311 /*
6312 * May need to draw the character below the vertical separator.
6313 */
6314 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6315 {
6316 if (stl_connected(wp))
6317 fillchar = fillchar_status(&attr, wp == curwin);
6318 else
6319 fillchar = fillchar_vsep(&attr);
6320 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6321 attr);
6322 }
6323#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006324 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325}
6326
Bram Moolenaar238a5642006-02-21 22:12:05 +00006327#ifdef FEAT_STL_OPT
6328/*
6329 * Redraw the status line according to 'statusline' and take care of any
6330 * errors encountered.
6331 */
6332 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006333redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006334 win_T *wp;
6335{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006336 static int entered = FALSE;
6337 int save_called_emsg = called_emsg;
6338
6339 /* When called recursively return. This can happen when the statusline
6340 * contains an expression that triggers a redraw. */
6341 if (entered)
6342 return;
6343 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006344
6345 called_emsg = FALSE;
6346 win_redr_custom(wp, FALSE);
6347 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006348 {
6349 /* When there is an error disable the statusline, otherwise the
6350 * display is messed up with errors and a redraw triggers the problem
6351 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006352 set_string_option_direct((char_u *)"statusline", -1,
6353 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006354 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006355 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006356 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006357 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006358}
6359#endif
6360
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361# ifdef FEAT_VERTSPLIT
6362/*
6363 * Return TRUE if the status line of window "wp" is connected to the status
6364 * line of the window right of it. If not, then it's a vertical separator.
6365 * Only call if (wp->w_vsep_width != 0).
6366 */
6367 int
6368stl_connected(wp)
6369 win_T *wp;
6370{
6371 frame_T *fr;
6372
6373 fr = wp->w_frame;
6374 while (fr->fr_parent != NULL)
6375 {
6376 if (fr->fr_parent->fr_layout == FR_COL)
6377 {
6378 if (fr->fr_next != NULL)
6379 break;
6380 }
6381 else
6382 {
6383 if (fr->fr_next != NULL)
6384 return TRUE;
6385 }
6386 fr = fr->fr_parent;
6387 }
6388 return FALSE;
6389}
6390# endif
6391
6392#endif /* FEAT_WINDOWS */
6393
6394#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6395/*
6396 * Get the value to show for the language mappings, active 'keymap'.
6397 */
6398 int
6399get_keymap_str(wp, buf, len)
6400 win_T *wp;
6401 char_u *buf; /* buffer for the result */
6402 int len; /* length of buffer */
6403{
6404 char_u *p;
6405
6406 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6407 return FALSE;
6408
6409 {
6410#ifdef FEAT_EVAL
6411 buf_T *old_curbuf = curbuf;
6412 win_T *old_curwin = curwin;
6413 char_u *s;
6414
6415 curbuf = wp->w_buffer;
6416 curwin = wp;
6417 STRCPY(buf, "b:keymap_name"); /* must be writable */
6418 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006419 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 --emsg_skip;
6421 curbuf = old_curbuf;
6422 curwin = old_curwin;
6423 if (p == NULL || *p == NUL)
6424#endif
6425 {
6426#ifdef FEAT_KEYMAP
6427 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6428 p = wp->w_buffer->b_p_keymap;
6429 else
6430#endif
6431 p = (char_u *)"lang";
6432 }
6433 if ((int)(STRLEN(p) + 3) < len)
6434 sprintf((char *)buf, "<%s>", p);
6435 else
6436 buf[0] = NUL;
6437#ifdef FEAT_EVAL
6438 vim_free(s);
6439#endif
6440 }
6441 return buf[0] != NUL;
6442}
6443#endif
6444
6445#if defined(FEAT_STL_OPT) || defined(PROTO)
6446/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006447 * Redraw the status line or ruler of window "wp".
6448 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 */
6450 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006451win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006453 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 int attr;
6456 int curattr;
6457 int row;
6458 int col = 0;
6459 int maxwidth;
6460 int width;
6461 int n;
6462 int len;
6463 int fillchar;
6464 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006465 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006467 struct stl_hlrec hltab[STL_MAX_ITEM];
6468 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006469 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006470 win_T *ewp;
6471 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472
6473 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006474 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006476 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006477 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006478 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006479 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006480 attr = hl_attr(HLF_TPF);
6481 maxwidth = Columns;
6482# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006483 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006486 else
6487 {
6488 row = W_WINROW(wp) + wp->w_height;
6489 fillchar = fillchar_status(&attr, wp == curwin);
6490 maxwidth = W_WIDTH(wp);
6491
6492 if (draw_ruler)
6493 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006494 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006495 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006496 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006497 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006498 if (*++stl == '-')
6499 stl++;
6500 if (atoi((char *)stl))
6501 while (VIM_ISDIGIT(*stl))
6502 stl++;
6503 if (*stl++ != '(')
6504 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006505 }
6506#ifdef FEAT_VERTSPLIT
6507 col = ru_col - (Columns - W_WIDTH(wp));
6508 if (col < (W_WIDTH(wp) + 1) / 2)
6509 col = (W_WIDTH(wp) + 1) / 2;
6510#else
6511 col = ru_col;
6512 if (col > (Columns + 1) / 2)
6513 col = (Columns + 1) / 2;
6514#endif
6515 maxwidth = W_WIDTH(wp) - col;
6516#ifdef FEAT_WINDOWS
6517 if (!wp->w_status_height)
6518#endif
6519 {
6520 row = Rows - 1;
6521 --maxwidth; /* writing in last column may cause scrolling */
6522 fillchar = ' ';
6523 attr = 0;
6524 }
6525
6526# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006527 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006528# endif
6529 }
6530 else
6531 {
6532 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006533 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006534 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006535 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006536# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006537 use_sandbox = was_set_insecurely((char_u *)"statusline",
6538 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006539# endif
6540 }
6541
6542#ifdef FEAT_VERTSPLIT
6543 col += W_WINCOL(wp);
6544#endif
6545 }
6546
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 if (maxwidth <= 0)
6548 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaar61452852011-02-01 18:01:11 +01006550 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6551 * the cursor away and back. */
6552 ewp = wp == NULL ? curwin : wp;
6553 p_crb_save = ewp->w_p_crb;
6554 ewp->w_p_crb = FALSE;
6555
Bram Moolenaar362f3562009-11-03 16:20:34 +00006556 /* Make a copy, because the statusline may include a function call that
6557 * might change the option value and free the memory. */
6558 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006559 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006560 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006561 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006562 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006563 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006565 /* Make all characters printable. */
6566 p = transstr(buf);
6567 if (p != NULL)
6568 {
6569 vim_strncpy(buf, p, sizeof(buf) - 1);
6570 vim_free(p);
6571 }
6572
6573 /* fill up with "fillchar" */
6574 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006575 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 {
6577#ifdef FEAT_MBYTE
6578 len += (*mb_char2bytes)(fillchar, buf + len);
6579#else
6580 buf[len++] = fillchar;
6581#endif
6582 ++width;
6583 }
6584 buf[len] = NUL;
6585
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006586 /*
6587 * Draw each snippet with the specified highlighting.
6588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 curattr = attr;
6590 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006591 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006593 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 screen_puts_len(p, len, row, col, curattr);
6595 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006596 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006598 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006600 else if (hltab[n].userhl < 0)
6601 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006603 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006604 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605#endif
6606 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006607 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006610
6611 if (wp == NULL)
6612 {
6613 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6614 col = 0;
6615 len = 0;
6616 p = buf;
6617 fillchar = 0;
6618 for (n = 0; tabtab[n].start != NULL; n++)
6619 {
6620 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6621 while (col < len)
6622 TabPageIdxs[col++] = fillchar;
6623 p = tabtab[n].start;
6624 fillchar = tabtab[n].userhl;
6625 }
6626 while (col < Columns)
6627 TabPageIdxs[col++] = fillchar;
6628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629}
6630
6631#endif /* FEAT_STL_OPT */
6632
6633/*
6634 * Output a single character directly to the screen and update ScreenLines.
6635 */
6636 void
6637screen_putchar(c, row, col, attr)
6638 int c;
6639 int row, col;
6640 int attr;
6641{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 char_u buf[MB_MAXBYTES + 1];
6643
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006644#ifdef FEAT_MBYTE
6645 if (has_mbyte)
6646 buf[(*mb_char2bytes)(c, buf)] = NUL;
6647 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006649 {
6650 buf[0] = c;
6651 buf[1] = NUL;
6652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 screen_puts(buf, row, col, attr);
6654}
6655
6656/*
6657 * Get a single character directly from ScreenLines into "bytes[]".
6658 * Also return its attribute in *attrp;
6659 */
6660 void
6661screen_getbytes(row, col, bytes, attrp)
6662 int row, col;
6663 char_u *bytes;
6664 int *attrp;
6665{
6666 unsigned off;
6667
6668 /* safety check */
6669 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6670 {
6671 off = LineOffset[row] + col;
6672 *attrp = ScreenAttrs[off];
6673 bytes[0] = ScreenLines[off];
6674 bytes[1] = NUL;
6675
6676#ifdef FEAT_MBYTE
6677 if (enc_utf8 && ScreenLinesUC[off] != 0)
6678 bytes[utfc_char2bytes(off, bytes)] = NUL;
6679 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6680 {
6681 bytes[0] = ScreenLines[off];
6682 bytes[1] = ScreenLines2[off];
6683 bytes[2] = NUL;
6684 }
6685 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6686 {
6687 bytes[1] = ScreenLines[off + 1];
6688 bytes[2] = NUL;
6689 }
6690#endif
6691 }
6692}
6693
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006694#ifdef FEAT_MBYTE
6695static int screen_comp_differs __ARGS((int, int*));
6696
6697/*
6698 * Return TRUE if composing characters for screen posn "off" differs from
6699 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006700 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006701 */
6702 static int
6703screen_comp_differs(off, u8cc)
6704 int off;
6705 int *u8cc;
6706{
6707 int i;
6708
6709 for (i = 0; i < Screen_mco; ++i)
6710 {
6711 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6712 return TRUE;
6713 if (u8cc[i] == 0)
6714 break;
6715 }
6716 return FALSE;
6717}
6718#endif
6719
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720/*
6721 * Put string '*text' on the screen at position 'row' and 'col', with
6722 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6723 * Note: only outputs within one row, message is truncated at screen boundary!
6724 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6725 */
6726 void
6727screen_puts(text, row, col, attr)
6728 char_u *text;
6729 int row;
6730 int col;
6731 int attr;
6732{
6733 screen_puts_len(text, -1, row, col, attr);
6734}
6735
6736/*
6737 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6738 * a NUL.
6739 */
6740 void
6741screen_puts_len(text, len, row, col, attr)
6742 char_u *text;
6743 int len;
6744 int row;
6745 int col;
6746 int attr;
6747{
6748 unsigned off;
6749 char_u *ptr = text;
6750 int c;
6751#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006752 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 int mbyte_blen = 1;
6754 int mbyte_cells = 1;
6755 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006756 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 int clear_next_cell = FALSE;
6758# ifdef FEAT_ARABIC
6759 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006760 int pc, nc, nc1;
6761 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762# endif
6763#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006764#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6765 int force_redraw_this;
6766 int force_redraw_next = FALSE;
6767#endif
6768 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
6770 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6771 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006772 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
Bram Moolenaarc236c162008-07-13 17:41:49 +00006774#ifdef FEAT_MBYTE
6775 /* When drawing over the right halve of a double-wide char clear out the
6776 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006777 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006778# ifdef FEAT_GUI
6779 && !gui.in_use
6780# endif
6781 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006782 {
6783 ScreenLines[off - 1] = ' ';
6784 ScreenAttrs[off - 1] = 0;
6785 if (enc_utf8)
6786 {
6787 ScreenLinesUC[off - 1] = 0;
6788 ScreenLinesC[0][off - 1] = 0;
6789 }
6790 /* redraw the previous cell, make it empty */
6791 screen_char(off - 1, row, col - 1);
6792 /* force the cell at "col" to be redrawn */
6793 force_redraw_next = TRUE;
6794 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006795#endif
6796
Bram Moolenaar367329b2007-08-30 11:53:22 +00006797#ifdef FEAT_MBYTE
6798 max_off = LineOffset[row] + screen_Columns;
6799#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006800 while (col < screen_Columns
6801 && (len < 0 || (int)(ptr - text) < len)
6802 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
6804 c = *ptr;
6805#ifdef FEAT_MBYTE
6806 /* check if this is the first byte of a multibyte */
6807 if (has_mbyte)
6808 {
6809 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006810 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006812 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6814 mbyte_cells = 1;
6815 else if (enc_dbcs != 0)
6816 mbyte_cells = mbyte_blen;
6817 else /* enc_utf8 */
6818 {
6819 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006820 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 (int)((text + len) - ptr));
6822 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006823 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006825# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 /* Non-BMP character: display as ? or fullwidth ?. */
6827 if (u8c >= 0x10000)
6828 {
6829 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6830 if (attr == 0)
6831 attr = hl_attr(HLF_8);
6832 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834# ifdef FEAT_ARABIC
6835 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6836 {
6837 /* Do Arabic shaping. */
6838 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6839 {
6840 /* Past end of string to be displayed. */
6841 nc = NUL;
6842 nc1 = NUL;
6843 }
6844 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006845 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006846 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6847 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006848 nc1 = pcc[0];
6849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 pc = prev_c;
6851 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006852 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 }
6854 else
6855 prev_c = u8c;
6856# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006857 if (col + mbyte_cells > screen_Columns)
6858 {
6859 /* Only 1 cell left, but character requires 2 cells:
6860 * display a '>' in the last column to avoid wrapping. */
6861 c = '>';
6862 mbyte_cells = 1;
6863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 }
6865 }
6866#endif
6867
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006868#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6869 force_redraw_this = force_redraw_next;
6870 force_redraw_next = FALSE;
6871#endif
6872
6873 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874#ifdef FEAT_MBYTE
6875 || (mbyte_cells == 2
6876 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6877 || (enc_dbcs == DBCS_JPNU
6878 && c == 0x8e
6879 && ScreenLines2[off] != ptr[1])
6880 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006881 && (ScreenLinesUC[off] !=
6882 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6883 || (ScreenLinesUC[off] != 0
6884 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885#endif
6886 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006887 || exmode_active;
6888
6889 if (need_redraw
6890#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6891 || force_redraw_this
6892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 )
6894 {
6895#if defined(FEAT_GUI) || defined(UNIX)
6896 /* The bold trick makes a single row of pixels appear in the next
6897 * character. When a bold character is removed, the next
6898 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006899 * and for some xterms. */
6900 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901# ifdef FEAT_GUI
6902 gui.in_use
6903# endif
6904# if defined(FEAT_GUI) && defined(UNIX)
6905 ||
6906# endif
6907# ifdef UNIX
6908 term_is_xterm
6909# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006910 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006912 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006914 if (n > HL_ALL)
6915 n = syn_attr2attr(n);
6916 if (n & HL_BOLD)
6917 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 }
6919#endif
6920#ifdef FEAT_MBYTE
6921 /* When at the end of the text and overwriting a two-cell
6922 * character with a one-cell character, need to clear the next
6923 * cell. Also when overwriting the left halve of a two-cell char
6924 * with the right halve of a two-cell char. Do this only once
6925 * (mb_off2cells() may return 2 on the right halve). */
6926 if (clear_next_cell)
6927 clear_next_cell = FALSE;
6928 else if (has_mbyte
6929 && (len < 0 ? ptr[mbyte_blen] == NUL
6930 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006931 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006933 && (*mb_off2cells)(off, max_off) == 1
6934 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 clear_next_cell = TRUE;
6936
6937 /* Make sure we never leave a second byte of a double-byte behind,
6938 * it confuses mb_off2cells(). */
6939 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006940 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006942 && (*mb_off2cells)(off, max_off) == 1
6943 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 ScreenLines[off + mbyte_blen] = 0;
6945#endif
6946 ScreenLines[off] = c;
6947 ScreenAttrs[off] = attr;
6948#ifdef FEAT_MBYTE
6949 if (enc_utf8)
6950 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006951 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 ScreenLinesUC[off] = 0;
6953 else
6954 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006955 int i;
6956
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006958 for (i = 0; i < Screen_mco; ++i)
6959 {
6960 ScreenLinesC[i][off] = u8cc[i];
6961 if (u8cc[i] == 0)
6962 break;
6963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 }
6965 if (mbyte_cells == 2)
6966 {
6967 ScreenLines[off + 1] = 0;
6968 ScreenAttrs[off + 1] = attr;
6969 }
6970 screen_char(off, row, col);
6971 }
6972 else if (mbyte_cells == 2)
6973 {
6974 ScreenLines[off + 1] = ptr[1];
6975 ScreenAttrs[off + 1] = attr;
6976 screen_char_2(off, row, col);
6977 }
6978 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6979 {
6980 ScreenLines2[off] = ptr[1];
6981 screen_char(off, row, col);
6982 }
6983 else
6984#endif
6985 screen_char(off, row, col);
6986 }
6987#ifdef FEAT_MBYTE
6988 if (has_mbyte)
6989 {
6990 off += mbyte_cells;
6991 col += mbyte_cells;
6992 ptr += mbyte_blen;
6993 if (clear_next_cell)
6994 ptr = (char_u *)" ";
6995 }
6996 else
6997#endif
6998 {
6999 ++off;
7000 ++col;
7001 ++ptr;
7002 }
7003 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007004
7005#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7006 /* If we detected the next character needs to be redrawn, but the text
7007 * doesn't extend up to there, update the character here. */
7008 if (force_redraw_next && col < screen_Columns)
7009 {
7010# ifdef FEAT_MBYTE
7011 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7012 screen_char_2(off, row, col);
7013 else
7014# endif
7015 screen_char(off, row, col);
7016 }
7017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018}
7019
7020#ifdef FEAT_SEARCH_EXTRA
7021/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007022 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 */
7024 static void
7025start_search_hl()
7026{
7027 if (p_hls && !no_hlsearch)
7028 {
7029 last_pat_prog(&search_hl.rm);
7030 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007031# ifdef FEAT_RELTIME
7032 /* Set the time limit to 'redrawtime'. */
7033 profile_setlimit(p_rdt, &search_hl.tm);
7034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 }
7036}
7037
7038/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007039 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 */
7041 static void
7042end_search_hl()
7043{
7044 if (search_hl.rm.regprog != NULL)
7045 {
7046 vim_free(search_hl.rm.regprog);
7047 search_hl.rm.regprog = NULL;
7048 }
7049}
7050
7051/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007052 * Init for calling prepare_search_hl().
7053 */
7054 static void
7055init_search_hl(wp)
7056 win_T *wp;
7057{
7058 matchitem_T *cur;
7059
7060 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7061 * match */
7062 cur = wp->w_match_head;
7063 while (cur != NULL)
7064 {
7065 cur->hl.rm = cur->match;
7066 if (cur->hlg_id == 0)
7067 cur->hl.attr = 0;
7068 else
7069 cur->hl.attr = syn_id2attr(cur->hlg_id);
7070 cur->hl.buf = wp->w_buffer;
7071 cur->hl.lnum = 0;
7072 cur->hl.first_lnum = 0;
7073# ifdef FEAT_RELTIME
7074 /* Set the time limit to 'redrawtime'. */
7075 profile_setlimit(p_rdt, &(cur->hl.tm));
7076# endif
7077 cur = cur->next;
7078 }
7079 search_hl.buf = wp->w_buffer;
7080 search_hl.lnum = 0;
7081 search_hl.first_lnum = 0;
7082 /* time limit is set at the toplevel, for all windows */
7083}
7084
7085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 * Advance to the match in window "wp" line "lnum" or past it.
7087 */
7088 static void
7089prepare_search_hl(wp, lnum)
7090 win_T *wp;
7091 linenr_T lnum;
7092{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007093 matchitem_T *cur; /* points to the match list */
7094 match_T *shl; /* points to search_hl or a match */
7095 int shl_flag; /* flag to indicate whether search_hl
7096 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 int n;
7098
7099 /*
7100 * When using a multi-line pattern, start searching at the top
7101 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007102 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007104 cur = wp->w_match_head;
7105 shl_flag = FALSE;
7106 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007108 if (shl_flag == FALSE)
7109 {
7110 shl = &search_hl;
7111 shl_flag = TRUE;
7112 }
7113 else
7114 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 if (shl->rm.regprog != NULL
7116 && shl->lnum == 0
7117 && re_multiline(shl->rm.regprog))
7118 {
7119 if (shl->first_lnum == 0)
7120 {
7121# ifdef FEAT_FOLDING
7122 for (shl->first_lnum = lnum;
7123 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7124 if (hasFoldingWin(wp, shl->first_lnum - 1,
7125 NULL, NULL, TRUE, NULL))
7126 break;
7127# else
7128 shl->first_lnum = wp->w_topline;
7129# endif
7130 }
7131 n = 0;
7132 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7133 {
7134 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7135 if (shl->lnum != 0)
7136 {
7137 shl->first_lnum = shl->lnum
7138 + shl->rm.endpos[0].lnum
7139 - shl->rm.startpos[0].lnum;
7140 n = shl->rm.endpos[0].col;
7141 }
7142 else
7143 {
7144 ++shl->first_lnum;
7145 n = 0;
7146 }
7147 }
7148 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007149 if (shl != &search_hl && cur != NULL)
7150 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
7152}
7153
7154/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007155 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 * Uses shl->buf.
7157 * Sets shl->lnum and shl->rm contents.
7158 * Note: Assumes a previous match is always before "lnum", unless
7159 * shl->lnum is zero.
7160 * Careful: Any pointers for buffer lines will become invalid.
7161 */
7162 static void
7163next_search_hl(win, shl, lnum, mincol)
7164 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007165 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 linenr_T lnum;
7167 colnr_T mincol; /* minimal column for a match */
7168{
7169 linenr_T l;
7170 colnr_T matchcol;
7171 long nmatched;
7172
7173 if (shl->lnum != 0)
7174 {
7175 /* Check for three situations:
7176 * 1. If the "lnum" is below a previous match, start a new search.
7177 * 2. If the previous match includes "mincol", use it.
7178 * 3. Continue after the previous match.
7179 */
7180 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7181 if (lnum > l)
7182 shl->lnum = 0;
7183 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7184 return;
7185 }
7186
7187 /*
7188 * Repeat searching for a match until one is found that includes "mincol"
7189 * or none is found in this line.
7190 */
7191 called_emsg = FALSE;
7192 for (;;)
7193 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007194#ifdef FEAT_RELTIME
7195 /* Stop searching after passing the time limit. */
7196 if (profile_passed_limit(&(shl->tm)))
7197 {
7198 shl->lnum = 0; /* no match found in time */
7199 break;
7200 }
7201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 /* Three situations:
7203 * 1. No useful previous match: search from start of line.
7204 * 2. Not Vi compatible or empty match: continue at next character.
7205 * Break the loop if this is beyond the end of the line.
7206 * 3. Vi compatible searching: continue at end of previous match.
7207 */
7208 if (shl->lnum == 0)
7209 matchcol = 0;
7210 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7211 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007212 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007214 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007215
7216 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007217 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007218 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007220 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 shl->lnum = 0;
7222 break;
7223 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007224#ifdef FEAT_MBYTE
7225 if (has_mbyte)
7226 matchcol += mb_ptr2len(ml);
7227 else
7228#endif
7229 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 }
7231 else
7232 matchcol = shl->rm.endpos[0].col;
7233
7234 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007235 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7236#ifdef FEAT_RELTIME
7237 &(shl->tm)
7238#else
7239 NULL
7240#endif
7241 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007242 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {
7244 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007245 if (shl == &search_hl)
7246 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007247 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007248 vim_free(shl->rm.regprog);
7249 no_hlsearch = TRUE;
7250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007252 shl->lnum = 0;
7253 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 break;
7255 }
7256 if (nmatched == 0)
7257 {
7258 shl->lnum = 0; /* no match found */
7259 break;
7260 }
7261 if (shl->rm.startpos[0].lnum > 0
7262 || shl->rm.startpos[0].col >= mincol
7263 || nmatched > 1
7264 || shl->rm.endpos[0].col > mincol)
7265 {
7266 shl->lnum += shl->rm.startpos[0].lnum;
7267 break; /* useful match found */
7268 }
7269 }
7270}
7271#endif
7272
7273 static void
7274screen_start_highlight(attr)
7275 int attr;
7276{
7277 attrentry_T *aep = NULL;
7278
7279 screen_attr = attr;
7280 if (full_screen
7281#ifdef WIN3264
7282 && termcap_active
7283#endif
7284 )
7285 {
7286#ifdef FEAT_GUI
7287 if (gui.in_use)
7288 {
7289 char buf[20];
7290
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007291 /* The GUI handles this internally. */
7292 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 OUT_STR(buf);
7294 }
7295 else
7296#endif
7297 {
7298 if (attr > HL_ALL) /* special HL attr. */
7299 {
7300 if (t_colors > 1)
7301 aep = syn_cterm_attr2entry(attr);
7302 else
7303 aep = syn_term_attr2entry(attr);
7304 if (aep == NULL) /* did ":syntax clear" */
7305 attr = 0;
7306 else
7307 attr = aep->ae_attr;
7308 }
7309 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7310 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007311 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7312 && cterm_normal_fg_bold)
7313 /* If the Normal FG color has BOLD attribute and the new HL
7314 * has a FG color defined, clear BOLD. */
7315 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7317 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007318 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7319 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 out_str(T_US);
7321 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7322 out_str(T_CZH);
7323 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7324 out_str(T_MR);
7325
7326 /*
7327 * Output the color or start string after bold etc., in case the
7328 * bold etc. override the color setting.
7329 */
7330 if (aep != NULL)
7331 {
7332 if (t_colors > 1)
7333 {
7334 if (aep->ae_u.cterm.fg_color)
7335 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7336 if (aep->ae_u.cterm.bg_color)
7337 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7338 }
7339 else
7340 {
7341 if (aep->ae_u.term.start != NULL)
7342 out_str(aep->ae_u.term.start);
7343 }
7344 }
7345 }
7346 }
7347}
7348
7349 void
7350screen_stop_highlight()
7351{
7352 int do_ME = FALSE; /* output T_ME code */
7353
7354 if (screen_attr != 0
7355#ifdef WIN3264
7356 && termcap_active
7357#endif
7358 )
7359 {
7360#ifdef FEAT_GUI
7361 if (gui.in_use)
7362 {
7363 char buf[20];
7364
7365 /* use internal GUI code */
7366 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7367 OUT_STR(buf);
7368 }
7369 else
7370#endif
7371 {
7372 if (screen_attr > HL_ALL) /* special HL attr. */
7373 {
7374 attrentry_T *aep;
7375
7376 if (t_colors > 1)
7377 {
7378 /*
7379 * Assume that t_me restores the original colors!
7380 */
7381 aep = syn_cterm_attr2entry(screen_attr);
7382 if (aep != NULL && (aep->ae_u.cterm.fg_color
7383 || aep->ae_u.cterm.bg_color))
7384 do_ME = TRUE;
7385 }
7386 else
7387 {
7388 aep = syn_term_attr2entry(screen_attr);
7389 if (aep != NULL && aep->ae_u.term.stop != NULL)
7390 {
7391 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7392 do_ME = TRUE;
7393 else
7394 out_str(aep->ae_u.term.stop);
7395 }
7396 }
7397 if (aep == NULL) /* did ":syntax clear" */
7398 screen_attr = 0;
7399 else
7400 screen_attr = aep->ae_attr;
7401 }
7402
7403 /*
7404 * Often all ending-codes are equal to T_ME. Avoid outputting the
7405 * same sequence several times.
7406 */
7407 if (screen_attr & HL_STANDOUT)
7408 {
7409 if (STRCMP(T_SE, T_ME) == 0)
7410 do_ME = TRUE;
7411 else
7412 out_str(T_SE);
7413 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007414 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 {
7416 if (STRCMP(T_UE, T_ME) == 0)
7417 do_ME = TRUE;
7418 else
7419 out_str(T_UE);
7420 }
7421 if (screen_attr & HL_ITALIC)
7422 {
7423 if (STRCMP(T_CZR, T_ME) == 0)
7424 do_ME = TRUE;
7425 else
7426 out_str(T_CZR);
7427 }
7428 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7429 out_str(T_ME);
7430
7431 if (t_colors > 1)
7432 {
7433 /* set Normal cterm colors */
7434 if (cterm_normal_fg_color != 0)
7435 term_fg_color(cterm_normal_fg_color - 1);
7436 if (cterm_normal_bg_color != 0)
7437 term_bg_color(cterm_normal_bg_color - 1);
7438 if (cterm_normal_fg_bold)
7439 out_str(T_MD);
7440 }
7441 }
7442 }
7443 screen_attr = 0;
7444}
7445
7446/*
7447 * Reset the colors for a cterm. Used when leaving Vim.
7448 * The machine specific code may override this again.
7449 */
7450 void
7451reset_cterm_colors()
7452{
7453 if (t_colors > 1)
7454 {
7455 /* set Normal cterm colors */
7456 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7457 {
7458 out_str(T_OP);
7459 screen_attr = -1;
7460 }
7461 if (cterm_normal_fg_bold)
7462 {
7463 out_str(T_ME);
7464 screen_attr = -1;
7465 }
7466 }
7467}
7468
7469/*
7470 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7471 * using the attributes from ScreenAttrs["off"].
7472 */
7473 static void
7474screen_char(off, row, col)
7475 unsigned off;
7476 int row;
7477 int col;
7478{
7479 int attr;
7480
7481 /* Check for illegal values, just in case (could happen just after
7482 * resizing). */
7483 if (row >= screen_Rows || col >= screen_Columns)
7484 return;
7485
7486 /* Outputting the last character on the screen may scrollup the screen.
7487 * Don't to it! Mark the character invalid (update it when scrolled up) */
7488 if (row == screen_Rows - 1 && col == screen_Columns - 1
7489#ifdef FEAT_RIGHTLEFT
7490 /* account for first command-line character in rightleft mode */
7491 && !cmdmsg_rl
7492#endif
7493 )
7494 {
7495 ScreenAttrs[off] = (sattr_T)-1;
7496 return;
7497 }
7498
7499 /*
7500 * Stop highlighting first, so it's easier to move the cursor.
7501 */
7502#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7503 if (screen_char_attr != 0)
7504 attr = screen_char_attr;
7505 else
7506#endif
7507 attr = ScreenAttrs[off];
7508 if (screen_attr != attr)
7509 screen_stop_highlight();
7510
7511 windgoto(row, col);
7512
7513 if (screen_attr != attr)
7514 screen_start_highlight(attr);
7515
7516#ifdef FEAT_MBYTE
7517 if (enc_utf8 && ScreenLinesUC[off] != 0)
7518 {
7519 char_u buf[MB_MAXBYTES + 1];
7520
7521 /* Convert UTF-8 character to bytes and write it. */
7522
7523 buf[utfc_char2bytes(off, buf)] = NUL;
7524
7525 out_str(buf);
7526 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7527 ++screen_cur_col;
7528 }
7529 else
7530#endif
7531 {
7532#ifdef FEAT_MBYTE
7533 out_flush_check();
7534#endif
7535 out_char(ScreenLines[off]);
7536#ifdef FEAT_MBYTE
7537 /* double-byte character in single-width cell */
7538 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7539 out_char(ScreenLines2[off]);
7540#endif
7541 }
7542
7543 screen_cur_col++;
7544}
7545
7546#ifdef FEAT_MBYTE
7547
7548/*
7549 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7550 * on the screen at position 'row' and 'col'.
7551 * The attributes of the first byte is used for all. This is required to
7552 * output the two bytes of a double-byte character with nothing in between.
7553 */
7554 static void
7555screen_char_2(off, row, col)
7556 unsigned off;
7557 int row;
7558 int col;
7559{
7560 /* Check for illegal values (could be wrong when screen was resized). */
7561 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7562 return;
7563
7564 /* Outputting the last character on the screen may scrollup the screen.
7565 * Don't to it! Mark the character invalid (update it when scrolled up) */
7566 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7567 {
7568 ScreenAttrs[off] = (sattr_T)-1;
7569 return;
7570 }
7571
7572 /* Output the first byte normally (positions the cursor), then write the
7573 * second byte directly. */
7574 screen_char(off, row, col);
7575 out_char(ScreenLines[off + 1]);
7576 ++screen_cur_col;
7577}
7578#endif
7579
7580#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7581/*
7582 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7583 * This uses the contents of ScreenLines[] and doesn't change it.
7584 */
7585 void
7586screen_draw_rectangle(row, col, height, width, invert)
7587 int row;
7588 int col;
7589 int height;
7590 int width;
7591 int invert;
7592{
7593 int r, c;
7594 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007595#ifdef FEAT_MBYTE
7596 int max_off;
7597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007599 /* Can't use ScreenLines unless initialized */
7600 if (ScreenLines == NULL)
7601 return;
7602
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 if (invert)
7604 screen_char_attr = HL_INVERSE;
7605 for (r = row; r < row + height; ++r)
7606 {
7607 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007608#ifdef FEAT_MBYTE
7609 max_off = off + screen_Columns;
7610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 for (c = col; c < col + width; ++c)
7612 {
7613#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007614 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {
7616 screen_char_2(off + c, r, c);
7617 ++c;
7618 }
7619 else
7620#endif
7621 {
7622 screen_char(off + c, r, c);
7623#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007624 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 ++c;
7626#endif
7627 }
7628 }
7629 }
7630 screen_char_attr = 0;
7631}
7632#endif
7633
7634#ifdef FEAT_VERTSPLIT
7635/*
7636 * Redraw the characters for a vertically split window.
7637 */
7638 static void
7639redraw_block(row, end, wp)
7640 int row;
7641 int end;
7642 win_T *wp;
7643{
7644 int col;
7645 int width;
7646
7647# ifdef FEAT_CLIPBOARD
7648 clip_may_clear_selection(row, end - 1);
7649# endif
7650
7651 if (wp == NULL)
7652 {
7653 col = 0;
7654 width = Columns;
7655 }
7656 else
7657 {
7658 col = wp->w_wincol;
7659 width = wp->w_width;
7660 }
7661 screen_draw_rectangle(row, col, end - row, width, FALSE);
7662}
7663#endif
7664
7665/*
7666 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7667 * with character 'c1' in first column followed by 'c2' in the other columns.
7668 * Use attributes 'attr'.
7669 */
7670 void
7671screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7672 int start_row, end_row;
7673 int start_col, end_col;
7674 int c1, c2;
7675 int attr;
7676{
7677 int row;
7678 int col;
7679 int off;
7680 int end_off;
7681 int did_delete;
7682 int c;
7683 int norm_term;
7684#if defined(FEAT_GUI) || defined(UNIX)
7685 int force_next = FALSE;
7686#endif
7687
7688 if (end_row > screen_Rows) /* safety check */
7689 end_row = screen_Rows;
7690 if (end_col > screen_Columns) /* safety check */
7691 end_col = screen_Columns;
7692 if (ScreenLines == NULL
7693 || start_row >= end_row
7694 || start_col >= end_col) /* nothing to do */
7695 return;
7696
7697 /* it's a "normal" terminal when not in a GUI or cterm */
7698 norm_term = (
7699#ifdef FEAT_GUI
7700 !gui.in_use &&
7701#endif
7702 t_colors <= 1);
7703 for (row = start_row; row < end_row; ++row)
7704 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007705#ifdef FEAT_MBYTE
7706 if (has_mbyte
7707# ifdef FEAT_GUI
7708 && !gui.in_use
7709# endif
7710 )
7711 {
7712 /* When drawing over the right halve of a double-wide char clear
7713 * out the left halve. When drawing over the left halve of a
7714 * double wide-char clear out the right halve. Only needed in a
7715 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007716 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007717 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007718 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007719 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007720 }
7721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 /*
7723 * Try to use delete-line termcap code, when no attributes or in a
7724 * "normal" terminal, where a bold/italic space is just a
7725 * space.
7726 */
7727 did_delete = FALSE;
7728 if (c2 == ' '
7729 && end_col == Columns
7730 && can_clear(T_CE)
7731 && (attr == 0
7732 || (norm_term
7733 && attr <= HL_ALL
7734 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7735 {
7736 /*
7737 * check if we really need to clear something
7738 */
7739 col = start_col;
7740 if (c1 != ' ') /* don't clear first char */
7741 ++col;
7742
7743 off = LineOffset[row] + col;
7744 end_off = LineOffset[row] + end_col;
7745
7746 /* skip blanks (used often, keep it fast!) */
7747#ifdef FEAT_MBYTE
7748 if (enc_utf8)
7749 while (off < end_off && ScreenLines[off] == ' '
7750 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7751 ++off;
7752 else
7753#endif
7754 while (off < end_off && ScreenLines[off] == ' '
7755 && ScreenAttrs[off] == 0)
7756 ++off;
7757 if (off < end_off) /* something to be cleared */
7758 {
7759 col = off - LineOffset[row];
7760 screen_stop_highlight();
7761 term_windgoto(row, col);/* clear rest of this screen line */
7762 out_str(T_CE);
7763 screen_start(); /* don't know where cursor is now */
7764 col = end_col - col;
7765 while (col--) /* clear chars in ScreenLines */
7766 {
7767 ScreenLines[off] = ' ';
7768#ifdef FEAT_MBYTE
7769 if (enc_utf8)
7770 ScreenLinesUC[off] = 0;
7771#endif
7772 ScreenAttrs[off] = 0;
7773 ++off;
7774 }
7775 }
7776 did_delete = TRUE; /* the chars are cleared now */
7777 }
7778
7779 off = LineOffset[row] + start_col;
7780 c = c1;
7781 for (col = start_col; col < end_col; ++col)
7782 {
7783 if (ScreenLines[off] != c
7784#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007785 || (enc_utf8 && (int)ScreenLinesUC[off]
7786 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787#endif
7788 || ScreenAttrs[off] != attr
7789#if defined(FEAT_GUI) || defined(UNIX)
7790 || force_next
7791#endif
7792 )
7793 {
7794#if defined(FEAT_GUI) || defined(UNIX)
7795 /* The bold trick may make a single row of pixels appear in
7796 * the next character. When a bold character is removed, the
7797 * next character should be redrawn too. This happens for our
7798 * own GUI and for some xterms. */
7799 if (
7800# ifdef FEAT_GUI
7801 gui.in_use
7802# endif
7803# if defined(FEAT_GUI) && defined(UNIX)
7804 ||
7805# endif
7806# ifdef UNIX
7807 term_is_xterm
7808# endif
7809 )
7810 {
7811 if (ScreenLines[off] != ' '
7812 && (ScreenAttrs[off] > HL_ALL
7813 || ScreenAttrs[off] & HL_BOLD))
7814 force_next = TRUE;
7815 else
7816 force_next = FALSE;
7817 }
7818#endif
7819 ScreenLines[off] = c;
7820#ifdef FEAT_MBYTE
7821 if (enc_utf8)
7822 {
7823 if (c >= 0x80)
7824 {
7825 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007826 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 }
7828 else
7829 ScreenLinesUC[off] = 0;
7830 }
7831#endif
7832 ScreenAttrs[off] = attr;
7833 if (!did_delete || c != ' ')
7834 screen_char(off, row, col);
7835 }
7836 ++off;
7837 if (col == start_col)
7838 {
7839 if (did_delete)
7840 break;
7841 c = c2;
7842 }
7843 }
7844 if (end_col == Columns)
7845 LineWraps[row] = FALSE;
7846 if (row == Rows - 1) /* overwritten the command line */
7847 {
7848 redraw_cmdline = TRUE;
7849 if (c1 == ' ' && c2 == ' ')
7850 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007851 if (start_col == 0)
7852 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 }
7854 }
7855}
7856
7857/*
7858 * Check if there should be a delay. Used before clearing or redrawing the
7859 * screen or the command line.
7860 */
7861 void
7862check_for_delay(check_msg_scroll)
7863 int check_msg_scroll;
7864{
7865 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7866 && !did_wait_return
7867 && emsg_silent == 0)
7868 {
7869 out_flush();
7870 ui_delay(1000L, TRUE);
7871 emsg_on_display = FALSE;
7872 if (check_msg_scroll)
7873 msg_scroll = FALSE;
7874 }
7875}
7876
7877/*
7878 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007879 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 * Returns TRUE if there is a valid screen to write to.
7881 * Returns FALSE when starting up and screen not initialized yet.
7882 */
7883 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007884screen_valid(doclear)
7885 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007887 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 return (ScreenLines != NULL);
7889}
7890
7891/*
7892 * Resize the shell to Rows and Columns.
7893 * Allocate ScreenLines[] and associated items.
7894 *
7895 * There may be some time between setting Rows and Columns and (re)allocating
7896 * ScreenLines[]. This happens when starting up and when (manually) changing
7897 * the shell size. Always use screen_Rows and screen_Columns to access items
7898 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7899 * final size of the shell is needed.
7900 */
7901 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007902screenalloc(doclear)
7903 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
7905 int new_row, old_row;
7906#ifdef FEAT_GUI
7907 int old_Rows;
7908#endif
7909 win_T *wp;
7910 int outofmem = FALSE;
7911 int len;
7912 schar_T *new_ScreenLines;
7913#ifdef FEAT_MBYTE
7914 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007915 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007917 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918#endif
7919 sattr_T *new_ScreenAttrs;
7920 unsigned *new_LineOffset;
7921 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007922#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007923 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007924 tabpage_T *tp;
7925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007927 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007928#ifdef FEAT_AUTOCMD
7929 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007931retry:
7932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 /*
7934 * Allocation of the screen buffers is done only when the size changes and
7935 * when Rows and Columns have been set and we have started doing full
7936 * screen stuff.
7937 */
7938 if ((ScreenLines != NULL
7939 && Rows == screen_Rows
7940 && Columns == screen_Columns
7941#ifdef FEAT_MBYTE
7942 && enc_utf8 == (ScreenLinesUC != NULL)
7943 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007944 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945#endif
7946 )
7947 || Rows == 0
7948 || Columns == 0
7949 || (!full_screen && ScreenLines == NULL))
7950 return;
7951
7952 /*
7953 * It's possible that we produce an out-of-memory message below, which
7954 * will cause this function to be called again. To break the loop, just
7955 * return here.
7956 */
7957 if (entered)
7958 return;
7959 entered = TRUE;
7960
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007961 /*
7962 * Note that the window sizes are updated before reallocating the arrays,
7963 * thus we must not redraw here!
7964 */
7965 ++RedrawingDisabled;
7966
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 win_new_shellsize(); /* fit the windows in the new sized shell */
7968
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 comp_col(); /* recompute columns for shown command and ruler */
7970
7971 /*
7972 * We're changing the size of the screen.
7973 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7974 * - Move lines from the old arrays into the new arrays, clear extra
7975 * lines (unless the screen is going to be cleared).
7976 * - Free the old arrays.
7977 *
7978 * If anything fails, make ScreenLines NULL, so we don't do anything!
7979 * Continuing with the old ScreenLines may result in a crash, because the
7980 * size is wrong.
7981 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007982 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007984#ifdef FEAT_AUTOCMD
7985 if (aucmd_win != NULL)
7986 win_free_lsize(aucmd_win);
7987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988
7989 new_ScreenLines = (schar_T *)lalloc((long_u)(
7990 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7991#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007992 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (enc_utf8)
7994 {
7995 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7996 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007997 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007998 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8000 }
8001 if (enc_dbcs == DBCS_JPNU)
8002 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8003 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8004#endif
8005 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8006 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8007 new_LineOffset = (unsigned *)lalloc((long_u)(
8008 Rows * sizeof(unsigned)), FALSE);
8009 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008010#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008011 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008014 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {
8016 if (win_alloc_lines(wp) == FAIL)
8017 {
8018 outofmem = TRUE;
8019#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008020 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021#endif
8022 }
8023 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008024#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008025 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8026 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008027 outofmem = TRUE;
8028#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008029#ifdef FEAT_WINDOWS
8030give_up:
8031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008033#ifdef FEAT_MBYTE
8034 for (i = 0; i < p_mco; ++i)
8035 if (new_ScreenLinesC[i] == NULL)
8036 break;
8037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 if (new_ScreenLines == NULL
8039#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008040 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8042#endif
8043 || new_ScreenAttrs == NULL
8044 || new_LineOffset == NULL
8045 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008046#ifdef FEAT_WINDOWS
8047 || new_TabPageIdxs == NULL
8048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 || outofmem)
8050 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008051 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008052 {
8053 /* guess the size */
8054 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8055
8056 /* Remember we did this to avoid getting outofmem messages over
8057 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008058 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 vim_free(new_ScreenLines);
8061 new_ScreenLines = NULL;
8062#ifdef FEAT_MBYTE
8063 vim_free(new_ScreenLinesUC);
8064 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008065 for (i = 0; i < p_mco; ++i)
8066 {
8067 vim_free(new_ScreenLinesC[i]);
8068 new_ScreenLinesC[i] = NULL;
8069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 vim_free(new_ScreenLines2);
8071 new_ScreenLines2 = NULL;
8072#endif
8073 vim_free(new_ScreenAttrs);
8074 new_ScreenAttrs = NULL;
8075 vim_free(new_LineOffset);
8076 new_LineOffset = NULL;
8077 vim_free(new_LineWraps);
8078 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008079#ifdef FEAT_WINDOWS
8080 vim_free(new_TabPageIdxs);
8081 new_TabPageIdxs = NULL;
8082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 }
8084 else
8085 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008086 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008087
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 for (new_row = 0; new_row < Rows; ++new_row)
8089 {
8090 new_LineOffset[new_row] = new_row * Columns;
8091 new_LineWraps[new_row] = FALSE;
8092
8093 /*
8094 * If the screen is not going to be cleared, copy as much as
8095 * possible from the old screen to the new one and clear the rest
8096 * (used when resizing the window at the "--more--" prompt or when
8097 * executing an external command, for the GUI).
8098 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008099 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {
8101 (void)vim_memset(new_ScreenLines + new_row * Columns,
8102 ' ', (size_t)Columns * sizeof(schar_T));
8103#ifdef FEAT_MBYTE
8104 if (enc_utf8)
8105 {
8106 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8107 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008108 for (i = 0; i < p_mco; ++i)
8109 (void)vim_memset(new_ScreenLinesC[i]
8110 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 0, (size_t)Columns * sizeof(u8char_T));
8112 }
8113 if (enc_dbcs == DBCS_JPNU)
8114 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8115 0, (size_t)Columns * sizeof(schar_T));
8116#endif
8117 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8118 0, (size_t)Columns * sizeof(sattr_T));
8119 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008120 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {
8122 if (screen_Columns < Columns)
8123 len = screen_Columns;
8124 else
8125 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008126#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008127 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008128 * may be invalid now. Also when p_mco changes. */
8129 if (!(enc_utf8 && ScreenLinesUC == NULL)
8130 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008131#endif
8132 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8133 ScreenLines + LineOffset[old_row],
8134 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008136 if (enc_utf8 && ScreenLinesUC != NULL
8137 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
8139 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8140 ScreenLinesUC + LineOffset[old_row],
8141 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008142 for (i = 0; i < p_mco; ++i)
8143 mch_memmove(new_ScreenLinesC[i]
8144 + new_LineOffset[new_row],
8145 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 (size_t)len * sizeof(u8char_T));
8147 }
8148 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8149 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8150 ScreenLines2 + LineOffset[old_row],
8151 (size_t)len * sizeof(schar_T));
8152#endif
8153 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8154 ScreenAttrs + LineOffset[old_row],
8155 (size_t)len * sizeof(sattr_T));
8156 }
8157 }
8158 }
8159 /* Use the last line of the screen for the current line. */
8160 current_ScreenLine = new_ScreenLines + Rows * Columns;
8161 }
8162
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008163 free_screenlines();
8164
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 ScreenLines = new_ScreenLines;
8166#ifdef FEAT_MBYTE
8167 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008168 for (i = 0; i < p_mco; ++i)
8169 ScreenLinesC[i] = new_ScreenLinesC[i];
8170 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 ScreenLines2 = new_ScreenLines2;
8172#endif
8173 ScreenAttrs = new_ScreenAttrs;
8174 LineOffset = new_LineOffset;
8175 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008176#ifdef FEAT_WINDOWS
8177 TabPageIdxs = new_TabPageIdxs;
8178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
8180 /* It's important that screen_Rows and screen_Columns reflect the actual
8181 * size of ScreenLines[]. Set them before calling anything. */
8182#ifdef FEAT_GUI
8183 old_Rows = screen_Rows;
8184#endif
8185 screen_Rows = Rows;
8186 screen_Columns = Columns;
8187
8188 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008189 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 screenclear2();
8191
8192#ifdef FEAT_GUI
8193 else if (gui.in_use
8194 && !gui.starting
8195 && ScreenLines != NULL
8196 && old_Rows != Rows)
8197 {
8198 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8199 /*
8200 * Adjust the position of the cursor, for when executing an external
8201 * command.
8202 */
8203 if (msg_row >= Rows) /* Rows got smaller */
8204 msg_row = Rows - 1; /* put cursor at last row */
8205 else if (Rows > old_Rows) /* Rows got bigger */
8206 msg_row += Rows - old_Rows; /* put cursor in same place */
8207 if (msg_col >= Columns) /* Columns got smaller */
8208 msg_col = Columns - 1; /* put cursor at last column */
8209 }
8210#endif
8211
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008213 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008214
8215#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008216 /*
8217 * Do not apply autocommands more than 3 times to avoid an endless loop
8218 * in case applying autocommands always changes Rows or Columns.
8219 */
8220 if (starting == 0 && ++retry_count <= 3)
8221 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008222 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008223 /* In rare cases, autocommands may have altered Rows or Columns,
8224 * jump back to check if we need to allocate the screen again. */
8225 goto retry;
8226 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
8230 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008231free_screenlines()
8232{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008233#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008234 int i;
8235
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008236 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008237 for (i = 0; i < Screen_mco; ++i)
8238 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008239 vim_free(ScreenLines2);
8240#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008241 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008242 vim_free(ScreenAttrs);
8243 vim_free(LineOffset);
8244 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008245#ifdef FEAT_WINDOWS
8246 vim_free(TabPageIdxs);
8247#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008248}
8249
8250 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251screenclear()
8252{
8253 check_for_delay(FALSE);
8254 screenalloc(FALSE); /* allocate screen buffers if size changed */
8255 screenclear2(); /* clear the screen */
8256}
8257
8258 static void
8259screenclear2()
8260{
8261 int i;
8262
8263 if (starting == NO_SCREEN || ScreenLines == NULL
8264#ifdef FEAT_GUI
8265 || (gui.in_use && gui.starting)
8266#endif
8267 )
8268 return;
8269
8270#ifdef FEAT_GUI
8271 if (!gui.in_use)
8272#endif
8273 screen_attr = -1; /* force setting the Normal colors */
8274 screen_stop_highlight(); /* don't want highlighting here */
8275
8276#ifdef FEAT_CLIPBOARD
8277 /* disable selection without redrawing it */
8278 clip_scroll_selection(9999);
8279#endif
8280
8281 /* blank out ScreenLines */
8282 for (i = 0; i < Rows; ++i)
8283 {
8284 lineclear(LineOffset[i], (int)Columns);
8285 LineWraps[i] = FALSE;
8286 }
8287
8288 if (can_clear(T_CL))
8289 {
8290 out_str(T_CL); /* clear the display */
8291 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008292 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 }
8294 else
8295 {
8296 /* can't clear the screen, mark all chars with invalid attributes */
8297 for (i = 0; i < Rows; ++i)
8298 lineinvalid(LineOffset[i], (int)Columns);
8299 clear_cmdline = TRUE;
8300 }
8301
8302 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8303
8304 win_rest_invalid(firstwin);
8305 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008306#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008307 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 if (must_redraw == CLEAR) /* no need to clear again */
8310 must_redraw = NOT_VALID;
8311 compute_cmdrow();
8312 msg_row = cmdline_row; /* put cursor on last line for messages */
8313 msg_col = 0;
8314 screen_start(); /* don't know where cursor is now */
8315 msg_scrolled = 0; /* can't scroll back */
8316 msg_didany = FALSE;
8317 msg_didout = FALSE;
8318}
8319
8320/*
8321 * Clear one line in ScreenLines.
8322 */
8323 static void
8324lineclear(off, width)
8325 unsigned off;
8326 int width;
8327{
8328 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8329#ifdef FEAT_MBYTE
8330 if (enc_utf8)
8331 (void)vim_memset(ScreenLinesUC + off, 0,
8332 (size_t)width * sizeof(u8char_T));
8333#endif
8334 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8335}
8336
8337/*
8338 * Mark one line in ScreenLines invalid by setting the attributes to an
8339 * invalid value.
8340 */
8341 static void
8342lineinvalid(off, width)
8343 unsigned off;
8344 int width;
8345{
8346 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8347}
8348
8349#ifdef FEAT_VERTSPLIT
8350/*
8351 * Copy part of a Screenline for vertically split window "wp".
8352 */
8353 static void
8354linecopy(to, from, wp)
8355 int to;
8356 int from;
8357 win_T *wp;
8358{
8359 unsigned off_to = LineOffset[to] + wp->w_wincol;
8360 unsigned off_from = LineOffset[from] + wp->w_wincol;
8361
8362 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8363 wp->w_width * sizeof(schar_T));
8364# ifdef FEAT_MBYTE
8365 if (enc_utf8)
8366 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008367 int i;
8368
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8370 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008371 for (i = 0; i < p_mco; ++i)
8372 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8373 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 }
8375 if (enc_dbcs == DBCS_JPNU)
8376 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8377 wp->w_width * sizeof(schar_T));
8378# endif
8379 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8380 wp->w_width * sizeof(sattr_T));
8381}
8382#endif
8383
8384/*
8385 * Return TRUE if clearing with term string "p" would work.
8386 * It can't work when the string is empty or it won't set the right background.
8387 */
8388 int
8389can_clear(p)
8390 char_u *p;
8391{
8392 return (*p != NUL && (t_colors <= 1
8393#ifdef FEAT_GUI
8394 || gui.in_use
8395#endif
8396 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8397}
8398
8399/*
8400 * Reset cursor position. Use whenever cursor was moved because of outputting
8401 * something directly to the screen (shell commands) or a terminal control
8402 * code.
8403 */
8404 void
8405screen_start()
8406{
8407 screen_cur_row = screen_cur_col = 9999;
8408}
8409
8410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 * Move the cursor to position "row","col" in the screen.
8412 * This tries to find the most efficient way to move, minimizing the number of
8413 * characters sent to the terminal.
8414 */
8415 void
8416windgoto(row, col)
8417 int row;
8418 int col;
8419{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008420 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 int i;
8422 int plan;
8423 int cost;
8424 int wouldbe_col;
8425 int noinvcurs;
8426 char_u *bs;
8427 int goto_cost;
8428 int attr;
8429
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008430#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8432
8433#define PLAN_LE 1
8434#define PLAN_CR 2
8435#define PLAN_NL 3
8436#define PLAN_WRITE 4
8437 /* Can't use ScreenLines unless initialized */
8438 if (ScreenLines == NULL)
8439 return;
8440
8441 if (col != screen_cur_col || row != screen_cur_row)
8442 {
8443 /* Check for valid position. */
8444 if (row < 0) /* window without text lines? */
8445 row = 0;
8446 if (row >= screen_Rows)
8447 row = screen_Rows - 1;
8448 if (col >= screen_Columns)
8449 col = screen_Columns - 1;
8450
8451 /* check if no cursor movement is allowed in highlight mode */
8452 if (screen_attr && *T_MS == NUL)
8453 noinvcurs = HIGHL_COST;
8454 else
8455 noinvcurs = 0;
8456 goto_cost = GOTO_COST + noinvcurs;
8457
8458 /*
8459 * Plan how to do the positioning:
8460 * 1. Use CR to move it to column 0, same row.
8461 * 2. Use T_LE to move it a few columns to the left.
8462 * 3. Use NL to move a few lines down, column 0.
8463 * 4. Move a few columns to the right with T_ND or by writing chars.
8464 *
8465 * Don't do this if the cursor went beyond the last column, the cursor
8466 * position is unknown then (some terminals wrap, some don't )
8467 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008468 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 * characters to move the cursor to the right.
8470 */
8471 if (row >= screen_cur_row && screen_cur_col < Columns)
8472 {
8473 /*
8474 * If the cursor is in the same row, bigger col, we can use CR
8475 * or T_LE.
8476 */
8477 bs = NULL; /* init for GCC */
8478 attr = screen_attr;
8479 if (row == screen_cur_row && col < screen_cur_col)
8480 {
8481 /* "le" is preferred over "bc", because "bc" is obsolete */
8482 if (*T_LE)
8483 bs = T_LE; /* "cursor left" */
8484 else
8485 bs = T_BC; /* "backspace character (old) */
8486 if (*bs)
8487 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8488 else
8489 cost = 999;
8490 if (col + 1 < cost) /* using CR is less characters */
8491 {
8492 plan = PLAN_CR;
8493 wouldbe_col = 0;
8494 cost = 1; /* CR is just one character */
8495 }
8496 else
8497 {
8498 plan = PLAN_LE;
8499 wouldbe_col = col;
8500 }
8501 if (noinvcurs) /* will stop highlighting */
8502 {
8503 cost += noinvcurs;
8504 attr = 0;
8505 }
8506 }
8507
8508 /*
8509 * If the cursor is above where we want to be, we can use CR LF.
8510 */
8511 else if (row > screen_cur_row)
8512 {
8513 plan = PLAN_NL;
8514 wouldbe_col = 0;
8515 cost = (row - screen_cur_row) * 2; /* CR LF */
8516 if (noinvcurs) /* will stop highlighting */
8517 {
8518 cost += noinvcurs;
8519 attr = 0;
8520 }
8521 }
8522
8523 /*
8524 * If the cursor is in the same row, smaller col, just use write.
8525 */
8526 else
8527 {
8528 plan = PLAN_WRITE;
8529 wouldbe_col = screen_cur_col;
8530 cost = 0;
8531 }
8532
8533 /*
8534 * Check if any characters that need to be written have the
8535 * correct attributes. Also avoid UTF-8 characters.
8536 */
8537 i = col - wouldbe_col;
8538 if (i > 0)
8539 cost += i;
8540 if (cost < goto_cost && i > 0)
8541 {
8542 /*
8543 * Check if the attributes are correct without additionally
8544 * stopping highlighting.
8545 */
8546 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8547 while (i && *p++ == attr)
8548 --i;
8549 if (i != 0)
8550 {
8551 /*
8552 * Try if it works when highlighting is stopped here.
8553 */
8554 if (*--p == 0)
8555 {
8556 cost += noinvcurs;
8557 while (i && *p++ == 0)
8558 --i;
8559 }
8560 if (i != 0)
8561 cost = 999; /* different attributes, don't do it */
8562 }
8563#ifdef FEAT_MBYTE
8564 if (enc_utf8)
8565 {
8566 /* Don't use an UTF-8 char for positioning, it's slow. */
8567 for (i = wouldbe_col; i < col; ++i)
8568 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8569 {
8570 cost = 999;
8571 break;
8572 }
8573 }
8574#endif
8575 }
8576
8577 /*
8578 * We can do it without term_windgoto()!
8579 */
8580 if (cost < goto_cost)
8581 {
8582 if (plan == PLAN_LE)
8583 {
8584 if (noinvcurs)
8585 screen_stop_highlight();
8586 while (screen_cur_col > col)
8587 {
8588 out_str(bs);
8589 --screen_cur_col;
8590 }
8591 }
8592 else if (plan == PLAN_CR)
8593 {
8594 if (noinvcurs)
8595 screen_stop_highlight();
8596 out_char('\r');
8597 screen_cur_col = 0;
8598 }
8599 else if (plan == PLAN_NL)
8600 {
8601 if (noinvcurs)
8602 screen_stop_highlight();
8603 while (screen_cur_row < row)
8604 {
8605 out_char('\n');
8606 ++screen_cur_row;
8607 }
8608 screen_cur_col = 0;
8609 }
8610
8611 i = col - screen_cur_col;
8612 if (i > 0)
8613 {
8614 /*
8615 * Use cursor-right if it's one character only. Avoids
8616 * removing a line of pixels from the last bold char, when
8617 * using the bold trick in the GUI.
8618 */
8619 if (T_ND[0] != NUL && T_ND[1] == NUL)
8620 {
8621 while (i-- > 0)
8622 out_char(*T_ND);
8623 }
8624 else
8625 {
8626 int off;
8627
8628 off = LineOffset[row] + screen_cur_col;
8629 while (i-- > 0)
8630 {
8631 if (ScreenAttrs[off] != screen_attr)
8632 screen_stop_highlight();
8633#ifdef FEAT_MBYTE
8634 out_flush_check();
8635#endif
8636 out_char(ScreenLines[off]);
8637#ifdef FEAT_MBYTE
8638 if (enc_dbcs == DBCS_JPNU
8639 && ScreenLines[off] == 0x8e)
8640 out_char(ScreenLines2[off]);
8641#endif
8642 ++off;
8643 }
8644 }
8645 }
8646 }
8647 }
8648 else
8649 cost = 999;
8650
8651 if (cost >= goto_cost)
8652 {
8653 if (noinvcurs)
8654 screen_stop_highlight();
8655 if (row == screen_cur_row && (col > screen_cur_col) &&
8656 *T_CRI != NUL)
8657 term_cursor_right(col - screen_cur_col);
8658 else
8659 term_windgoto(row, col);
8660 }
8661 screen_cur_row = row;
8662 screen_cur_col = col;
8663 }
8664}
8665
8666/*
8667 * Set cursor to its position in the current window.
8668 */
8669 void
8670setcursor()
8671{
8672 if (redrawing())
8673 {
8674 validate_cursor();
8675 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8676 W_WINCOL(curwin) + (
8677#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008678 /* With 'rightleft' set and the cursor on a double-wide
8679 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8681# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008682 (has_mbyte
8683 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8684 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685# endif
8686 1)) :
8687#endif
8688 curwin->w_wcol));
8689 }
8690}
8691
8692
8693/*
8694 * insert 'line_count' lines at 'row' in window 'wp'
8695 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8696 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8697 * scrolling.
8698 * Returns FAIL if the lines are not inserted, OK for success.
8699 */
8700 int
8701win_ins_lines(wp, row, line_count, invalid, mayclear)
8702 win_T *wp;
8703 int row;
8704 int line_count;
8705 int invalid;
8706 int mayclear;
8707{
8708 int did_delete;
8709 int nextrow;
8710 int lastrow;
8711 int retval;
8712
8713 if (invalid)
8714 wp->w_lines_valid = 0;
8715
8716 if (wp->w_height < 5)
8717 return FAIL;
8718
8719 if (line_count > wp->w_height - row)
8720 line_count = wp->w_height - row;
8721
8722 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8723 if (retval != MAYBE)
8724 return retval;
8725
8726 /*
8727 * If there is a next window or a status line, we first try to delete the
8728 * lines at the bottom to avoid messing what is after the window.
8729 * If this fails and there are following windows, don't do anything to avoid
8730 * messing up those windows, better just redraw.
8731 */
8732 did_delete = FALSE;
8733#ifdef FEAT_WINDOWS
8734 if (wp->w_next != NULL || wp->w_status_height)
8735 {
8736 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8737 line_count, (int)Rows, FALSE, NULL) == OK)
8738 did_delete = TRUE;
8739 else if (wp->w_next)
8740 return FAIL;
8741 }
8742#endif
8743 /*
8744 * if no lines deleted, blank the lines that will end up below the window
8745 */
8746 if (!did_delete)
8747 {
8748#ifdef FEAT_WINDOWS
8749 wp->w_redr_status = TRUE;
8750#endif
8751 redraw_cmdline = TRUE;
8752 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8753 lastrow = nextrow + line_count;
8754 if (lastrow > Rows)
8755 lastrow = Rows;
8756 screen_fill(nextrow - line_count, lastrow - line_count,
8757 W_WINCOL(wp), (int)W_ENDCOL(wp),
8758 ' ', ' ', 0);
8759 }
8760
8761 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8762 == FAIL)
8763 {
8764 /* deletion will have messed up other windows */
8765 if (did_delete)
8766 {
8767#ifdef FEAT_WINDOWS
8768 wp->w_redr_status = TRUE;
8769#endif
8770 win_rest_invalid(W_NEXT(wp));
8771 }
8772 return FAIL;
8773 }
8774
8775 return OK;
8776}
8777
8778/*
8779 * delete "line_count" window lines at "row" in window "wp"
8780 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8781 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8782 * scrolling
8783 * Return OK for success, FAIL if the lines are not deleted.
8784 */
8785 int
8786win_del_lines(wp, row, line_count, invalid, mayclear)
8787 win_T *wp;
8788 int row;
8789 int line_count;
8790 int invalid;
8791 int mayclear;
8792{
8793 int retval;
8794
8795 if (invalid)
8796 wp->w_lines_valid = 0;
8797
8798 if (line_count > wp->w_height - row)
8799 line_count = wp->w_height - row;
8800
8801 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8802 if (retval != MAYBE)
8803 return retval;
8804
8805 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8806 (int)Rows, FALSE, NULL) == FAIL)
8807 return FAIL;
8808
8809#ifdef FEAT_WINDOWS
8810 /*
8811 * If there are windows or status lines below, try to put them at the
8812 * correct place. If we can't do that, they have to be redrawn.
8813 */
8814 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8815 {
8816 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8817 line_count, (int)Rows, NULL) == FAIL)
8818 {
8819 wp->w_redr_status = TRUE;
8820 win_rest_invalid(wp->w_next);
8821 }
8822 }
8823 /*
8824 * If this is the last window and there is no status line, redraw the
8825 * command line later.
8826 */
8827 else
8828#endif
8829 redraw_cmdline = TRUE;
8830 return OK;
8831}
8832
8833/*
8834 * Common code for win_ins_lines() and win_del_lines().
8835 * Returns OK or FAIL when the work has been done.
8836 * Returns MAYBE when not finished yet.
8837 */
8838 static int
8839win_do_lines(wp, row, line_count, mayclear, del)
8840 win_T *wp;
8841 int row;
8842 int line_count;
8843 int mayclear;
8844 int del;
8845{
8846 int retval;
8847
8848 if (!redrawing() || line_count <= 0)
8849 return FAIL;
8850
8851 /* only a few lines left: redraw is faster */
8852 if (mayclear && Rows - line_count < 5
8853#ifdef FEAT_VERTSPLIT
8854 && wp->w_width == Columns
8855#endif
8856 )
8857 {
8858 screenclear(); /* will set wp->w_lines_valid to 0 */
8859 return FAIL;
8860 }
8861
8862 /*
8863 * Delete all remaining lines
8864 */
8865 if (row + line_count >= wp->w_height)
8866 {
8867 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8868 W_WINCOL(wp), (int)W_ENDCOL(wp),
8869 ' ', ' ', 0);
8870 return OK;
8871 }
8872
8873 /*
8874 * when scrolling, the message on the command line should be cleared,
8875 * otherwise it will stay there forever.
8876 */
8877 clear_cmdline = TRUE;
8878
8879 /*
8880 * If the terminal can set a scroll region, use that.
8881 * Always do this in a vertically split window. This will redraw from
8882 * ScreenLines[] when t_CV isn't defined. That's faster than using
8883 * win_line().
8884 * Don't use a scroll region when we are going to redraw the text, writing
8885 * a character in the lower right corner of the scroll region causes a
8886 * scroll-up in the DJGPP version.
8887 */
8888 if (scroll_region
8889#ifdef FEAT_VERTSPLIT
8890 || W_WIDTH(wp) != Columns
8891#endif
8892 )
8893 {
8894#ifdef FEAT_VERTSPLIT
8895 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8896#endif
8897 scroll_region_set(wp, row);
8898 if (del)
8899 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8900 wp->w_height - row, FALSE, wp);
8901 else
8902 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8903 wp->w_height - row, wp);
8904#ifdef FEAT_VERTSPLIT
8905 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8906#endif
8907 scroll_region_reset();
8908 return retval;
8909 }
8910
8911#ifdef FEAT_WINDOWS
8912 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8913 return FAIL;
8914#endif
8915
8916 return MAYBE;
8917}
8918
8919/*
8920 * window 'wp' and everything after it is messed up, mark it for redraw
8921 */
8922 static void
8923win_rest_invalid(wp)
8924 win_T *wp;
8925{
8926#ifdef FEAT_WINDOWS
8927 while (wp != NULL)
8928#else
8929 if (wp != NULL)
8930#endif
8931 {
8932 redraw_win_later(wp, NOT_VALID);
8933#ifdef FEAT_WINDOWS
8934 wp->w_redr_status = TRUE;
8935 wp = wp->w_next;
8936#endif
8937 }
8938 redraw_cmdline = TRUE;
8939}
8940
8941/*
8942 * The rest of the routines in this file perform screen manipulations. The
8943 * given operation is performed physically on the screen. The corresponding
8944 * change is also made to the internal screen image. In this way, the editor
8945 * anticipates the effect of editing changes on the appearance of the screen.
8946 * That way, when we call screenupdate a complete redraw isn't usually
8947 * necessary. Another advantage is that we can keep adding code to anticipate
8948 * screen changes, and in the meantime, everything still works.
8949 */
8950
8951/*
8952 * types for inserting or deleting lines
8953 */
8954#define USE_T_CAL 1
8955#define USE_T_CDL 2
8956#define USE_T_AL 3
8957#define USE_T_CE 4
8958#define USE_T_DL 5
8959#define USE_T_SR 6
8960#define USE_NL 7
8961#define USE_T_CD 8
8962#define USE_REDRAW 9
8963
8964/*
8965 * insert lines on the screen and update ScreenLines[]
8966 * 'end' is the line after the scrolled part. Normally it is Rows.
8967 * When scrolling region used 'off' is the offset from the top for the region.
8968 * 'row' and 'end' are relative to the start of the region.
8969 *
8970 * return FAIL for failure, OK for success.
8971 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008972 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973screen_ins_lines(off, row, line_count, end, wp)
8974 int off;
8975 int row;
8976 int line_count;
8977 int end;
8978 win_T *wp; /* NULL or window to use width from */
8979{
8980 int i;
8981 int j;
8982 unsigned temp;
8983 int cursor_row;
8984 int type;
8985 int result_empty;
8986 int can_ce = can_clear(T_CE);
8987
8988 /*
8989 * FAIL if
8990 * - there is no valid screen
8991 * - the screen has to be redrawn completely
8992 * - the line count is less than one
8993 * - the line count is more than 'ttyscroll'
8994 */
8995 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8996 return FAIL;
8997
8998 /*
8999 * There are seven ways to insert lines:
9000 * 0. When in a vertically split window and t_CV isn't set, redraw the
9001 * characters from ScreenLines[].
9002 * 1. Use T_CD (clear to end of display) if it exists and the result of
9003 * the insert is just empty lines
9004 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9005 * present or line_count > 1. It looks better if we do all the inserts
9006 * at once.
9007 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9008 * insert is just empty lines and T_CE is not present or line_count >
9009 * 1.
9010 * 4. Use T_AL (insert line) if it exists.
9011 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9012 * just empty lines.
9013 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9014 * just empty lines.
9015 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9016 * the 'da' flag is not set or we have clear line capability.
9017 * 8. redraw the characters from ScreenLines[].
9018 *
9019 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9020 * the scrollbar for the window. It does have insert line, use that if it
9021 * exists.
9022 */
9023 result_empty = (row + line_count >= end);
9024#ifdef FEAT_VERTSPLIT
9025 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9026 type = USE_REDRAW;
9027 else
9028#endif
9029 if (can_clear(T_CD) && result_empty)
9030 type = USE_T_CD;
9031 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9032 type = USE_T_CAL;
9033 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9034 type = USE_T_CDL;
9035 else if (*T_AL != NUL)
9036 type = USE_T_AL;
9037 else if (can_ce && result_empty)
9038 type = USE_T_CE;
9039 else if (*T_DL != NUL && result_empty)
9040 type = USE_T_DL;
9041 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9042 type = USE_T_SR;
9043 else
9044 return FAIL;
9045
9046 /*
9047 * For clearing the lines screen_del_lines() is used. This will also take
9048 * care of t_db if necessary.
9049 */
9050 if (type == USE_T_CD || type == USE_T_CDL ||
9051 type == USE_T_CE || type == USE_T_DL)
9052 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9053
9054 /*
9055 * If text is retained below the screen, first clear or delete as many
9056 * lines at the bottom of the window as are about to be inserted so that
9057 * the deleted lines won't later surface during a screen_del_lines.
9058 */
9059 if (*T_DB)
9060 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9061
9062#ifdef FEAT_CLIPBOARD
9063 /* Remove a modeless selection when inserting lines halfway the screen
9064 * or not the full width of the screen. */
9065 if (off + row > 0
9066# ifdef FEAT_VERTSPLIT
9067 || (wp != NULL && wp->w_width != Columns)
9068# endif
9069 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009070 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 else
9072 clip_scroll_selection(-line_count);
9073#endif
9074
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075#ifdef FEAT_GUI
9076 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9077 * scrolling is actually carried out. */
9078 gui_dont_update_cursor();
9079#endif
9080
9081 if (*T_CCS != NUL) /* cursor relative to region */
9082 cursor_row = row;
9083 else
9084 cursor_row = row + off;
9085
9086 /*
9087 * Shift LineOffset[] line_count down to reflect the inserted lines.
9088 * Clear the inserted lines in ScreenLines[].
9089 */
9090 row += off;
9091 end += off;
9092 for (i = 0; i < line_count; ++i)
9093 {
9094#ifdef FEAT_VERTSPLIT
9095 if (wp != NULL && wp->w_width != Columns)
9096 {
9097 /* need to copy part of a line */
9098 j = end - 1 - i;
9099 while ((j -= line_count) >= row)
9100 linecopy(j + line_count, j, wp);
9101 j += line_count;
9102 if (can_clear((char_u *)" "))
9103 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9104 else
9105 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9106 LineWraps[j] = FALSE;
9107 }
9108 else
9109#endif
9110 {
9111 j = end - 1 - i;
9112 temp = LineOffset[j];
9113 while ((j -= line_count) >= row)
9114 {
9115 LineOffset[j + line_count] = LineOffset[j];
9116 LineWraps[j + line_count] = LineWraps[j];
9117 }
9118 LineOffset[j + line_count] = temp;
9119 LineWraps[j + line_count] = FALSE;
9120 if (can_clear((char_u *)" "))
9121 lineclear(temp, (int)Columns);
9122 else
9123 lineinvalid(temp, (int)Columns);
9124 }
9125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126
9127 screen_stop_highlight();
9128 windgoto(cursor_row, 0);
9129
9130#ifdef FEAT_VERTSPLIT
9131 /* redraw the characters */
9132 if (type == USE_REDRAW)
9133 redraw_block(row, end, wp);
9134 else
9135#endif
9136 if (type == USE_T_CAL)
9137 {
9138 term_append_lines(line_count);
9139 screen_start(); /* don't know where cursor is now */
9140 }
9141 else
9142 {
9143 for (i = 0; i < line_count; i++)
9144 {
9145 if (type == USE_T_AL)
9146 {
9147 if (i && cursor_row != 0)
9148 windgoto(cursor_row, 0);
9149 out_str(T_AL);
9150 }
9151 else /* type == USE_T_SR */
9152 out_str(T_SR);
9153 screen_start(); /* don't know where cursor is now */
9154 }
9155 }
9156
9157 /*
9158 * With scroll-reverse and 'da' flag set we need to clear the lines that
9159 * have been scrolled down into the region.
9160 */
9161 if (type == USE_T_SR && *T_DA)
9162 {
9163 for (i = 0; i < line_count; ++i)
9164 {
9165 windgoto(off + i, 0);
9166 out_str(T_CE);
9167 screen_start(); /* don't know where cursor is now */
9168 }
9169 }
9170
9171#ifdef FEAT_GUI
9172 gui_can_update_cursor();
9173 if (gui.in_use)
9174 out_flush(); /* always flush after a scroll */
9175#endif
9176 return OK;
9177}
9178
9179/*
9180 * delete lines on the screen and update ScreenLines[]
9181 * 'end' is the line after the scrolled part. Normally it is Rows.
9182 * When scrolling region used 'off' is the offset from the top for the region.
9183 * 'row' and 'end' are relative to the start of the region.
9184 *
9185 * Return OK for success, FAIL if the lines are not deleted.
9186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 int
9188screen_del_lines(off, row, line_count, end, force, wp)
9189 int off;
9190 int row;
9191 int line_count;
9192 int end;
9193 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009194 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 int j;
9197 int i;
9198 unsigned temp;
9199 int cursor_row;
9200 int cursor_end;
9201 int result_empty; /* result is empty until end of region */
9202 int can_delete; /* deleting line codes can be used */
9203 int type;
9204
9205 /*
9206 * FAIL if
9207 * - there is no valid screen
9208 * - the screen has to be redrawn completely
9209 * - the line count is less than one
9210 * - the line count is more than 'ttyscroll'
9211 */
9212 if (!screen_valid(TRUE) || line_count <= 0 ||
9213 (!force && line_count > p_ttyscroll))
9214 return FAIL;
9215
9216 /*
9217 * Check if the rest of the current region will become empty.
9218 */
9219 result_empty = row + line_count >= end;
9220
9221 /*
9222 * We can delete lines only when 'db' flag not set or when 'ce' option
9223 * available.
9224 */
9225 can_delete = (*T_DB == NUL || can_clear(T_CE));
9226
9227 /*
9228 * There are six ways to delete lines:
9229 * 0. When in a vertically split window and t_CV isn't set, redraw the
9230 * characters from ScreenLines[].
9231 * 1. Use T_CD if it exists and the result is empty.
9232 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9233 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9234 * none of the other ways work.
9235 * 4. Use T_CE (erase line) if the result is empty.
9236 * 5. Use T_DL (delete line) if it exists.
9237 * 6. redraw the characters from ScreenLines[].
9238 */
9239#ifdef FEAT_VERTSPLIT
9240 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9241 type = USE_REDRAW;
9242 else
9243#endif
9244 if (can_clear(T_CD) && result_empty)
9245 type = USE_T_CD;
9246#if defined(__BEOS__) && defined(BEOS_DR8)
9247 /*
9248 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9249 * its internal termcap... this works okay for tests which test *T_DB !=
9250 * NUL. It has the disadvantage that the user cannot use any :set t_*
9251 * command to get T_DB (back) to empty_option, only :set term=... will do
9252 * the trick...
9253 * Anyway, this hack will hopefully go away with the next OS release.
9254 * (Olaf Seibert)
9255 */
9256 else if (row == 0 && T_DB == empty_option
9257 && (line_count == 1 || *T_CDL == NUL))
9258#else
9259 else if (row == 0 && (
9260#ifndef AMIGA
9261 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9262 * up, so use delete-line command */
9263 line_count == 1 ||
9264#endif
9265 *T_CDL == NUL))
9266#endif
9267 type = USE_NL;
9268 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9269 type = USE_T_CDL;
9270 else if (can_clear(T_CE) && result_empty
9271#ifdef FEAT_VERTSPLIT
9272 && (wp == NULL || wp->w_width == Columns)
9273#endif
9274 )
9275 type = USE_T_CE;
9276 else if (*T_DL != NUL && can_delete)
9277 type = USE_T_DL;
9278 else if (*T_CDL != NUL && can_delete)
9279 type = USE_T_CDL;
9280 else
9281 return FAIL;
9282
9283#ifdef FEAT_CLIPBOARD
9284 /* Remove a modeless selection when deleting lines halfway the screen or
9285 * not the full width of the screen. */
9286 if (off + row > 0
9287# ifdef FEAT_VERTSPLIT
9288 || (wp != NULL && wp->w_width != Columns)
9289# endif
9290 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009291 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 else
9293 clip_scroll_selection(line_count);
9294#endif
9295
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296#ifdef FEAT_GUI
9297 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9298 * scrolling is actually carried out. */
9299 gui_dont_update_cursor();
9300#endif
9301
9302 if (*T_CCS != NUL) /* cursor relative to region */
9303 {
9304 cursor_row = row;
9305 cursor_end = end;
9306 }
9307 else
9308 {
9309 cursor_row = row + off;
9310 cursor_end = end + off;
9311 }
9312
9313 /*
9314 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9315 * Clear the inserted lines in ScreenLines[].
9316 */
9317 row += off;
9318 end += off;
9319 for (i = 0; i < line_count; ++i)
9320 {
9321#ifdef FEAT_VERTSPLIT
9322 if (wp != NULL && wp->w_width != Columns)
9323 {
9324 /* need to copy part of a line */
9325 j = row + i;
9326 while ((j += line_count) <= end - 1)
9327 linecopy(j - line_count, j, wp);
9328 j -= line_count;
9329 if (can_clear((char_u *)" "))
9330 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9331 else
9332 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9333 LineWraps[j] = FALSE;
9334 }
9335 else
9336#endif
9337 {
9338 /* whole width, moving the line pointers is faster */
9339 j = row + i;
9340 temp = LineOffset[j];
9341 while ((j += line_count) <= end - 1)
9342 {
9343 LineOffset[j - line_count] = LineOffset[j];
9344 LineWraps[j - line_count] = LineWraps[j];
9345 }
9346 LineOffset[j - line_count] = temp;
9347 LineWraps[j - line_count] = FALSE;
9348 if (can_clear((char_u *)" "))
9349 lineclear(temp, (int)Columns);
9350 else
9351 lineinvalid(temp, (int)Columns);
9352 }
9353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
9355 screen_stop_highlight();
9356
9357#ifdef FEAT_VERTSPLIT
9358 /* redraw the characters */
9359 if (type == USE_REDRAW)
9360 redraw_block(row, end, wp);
9361 else
9362#endif
9363 if (type == USE_T_CD) /* delete the lines */
9364 {
9365 windgoto(cursor_row, 0);
9366 out_str(T_CD);
9367 screen_start(); /* don't know where cursor is now */
9368 }
9369 else if (type == USE_T_CDL)
9370 {
9371 windgoto(cursor_row, 0);
9372 term_delete_lines(line_count);
9373 screen_start(); /* don't know where cursor is now */
9374 }
9375 /*
9376 * Deleting lines at top of the screen or scroll region: Just scroll
9377 * the whole screen (scroll region) up by outputting newlines on the
9378 * last line.
9379 */
9380 else if (type == USE_NL)
9381 {
9382 windgoto(cursor_end - 1, 0);
9383 for (i = line_count; --i >= 0; )
9384 out_char('\n'); /* cursor will remain on same line */
9385 }
9386 else
9387 {
9388 for (i = line_count; --i >= 0; )
9389 {
9390 if (type == USE_T_DL)
9391 {
9392 windgoto(cursor_row, 0);
9393 out_str(T_DL); /* delete a line */
9394 }
9395 else /* type == USE_T_CE */
9396 {
9397 windgoto(cursor_row + i, 0);
9398 out_str(T_CE); /* erase a line */
9399 }
9400 screen_start(); /* don't know where cursor is now */
9401 }
9402 }
9403
9404 /*
9405 * If the 'db' flag is set, we need to clear the lines that have been
9406 * scrolled up at the bottom of the region.
9407 */
9408 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9409 {
9410 for (i = line_count; i > 0; --i)
9411 {
9412 windgoto(cursor_end - i, 0);
9413 out_str(T_CE); /* erase a line */
9414 screen_start(); /* don't know where cursor is now */
9415 }
9416 }
9417
9418#ifdef FEAT_GUI
9419 gui_can_update_cursor();
9420 if (gui.in_use)
9421 out_flush(); /* always flush after a scroll */
9422#endif
9423
9424 return OK;
9425}
9426
9427/*
9428 * show the current mode and ruler
9429 *
9430 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9431 * If clear_cmdline is FALSE there may be a message there that needs to be
9432 * cleared only if a mode is shown.
9433 * Return the length of the message (0 if no message).
9434 */
9435 int
9436showmode()
9437{
9438 int need_clear;
9439 int length = 0;
9440 int do_mode;
9441 int attr;
9442 int nwr_save;
9443#ifdef FEAT_INS_EXPAND
9444 int sub_attr;
9445#endif
9446
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009447 do_mode = ((p_smd && msg_silent == 0)
9448 && ((State & INSERT)
9449 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450#ifdef FEAT_VISUAL
9451 || VIsual_active
9452#endif
9453 ));
9454 if (do_mode || Recording)
9455 {
9456 /*
9457 * Don't show mode right now, when not redrawing or inside a mapping.
9458 * Call char_avail() only when we are going to show something, because
9459 * it takes a bit of time.
9460 */
9461 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9462 {
9463 redraw_cmdline = TRUE; /* show mode later */
9464 return 0;
9465 }
9466
9467 nwr_save = need_wait_return;
9468
9469 /* wait a bit before overwriting an important message */
9470 check_for_delay(FALSE);
9471
9472 /* if the cmdline is more than one line high, erase top lines */
9473 need_clear = clear_cmdline;
9474 if (clear_cmdline && cmdline_row < Rows - 1)
9475 msg_clr_cmdline(); /* will reset clear_cmdline */
9476
9477 /* Position on the last line in the window, column 0 */
9478 msg_pos_mode();
9479 cursor_off();
9480 attr = hl_attr(HLF_CM); /* Highlight mode */
9481 if (do_mode)
9482 {
9483 MSG_PUTS_ATTR("--", attr);
9484#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009485 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009486# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009487 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009488# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009489 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009490# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009491 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009492# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 MSG_PUTS_ATTR(" IM", attr);
9494# else
9495 MSG_PUTS_ATTR(" XIM", attr);
9496# endif
9497#endif
9498#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9499 if (gui.in_use)
9500 {
9501 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009502 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 }
9504#endif
9505#ifdef FEAT_INS_EXPAND
9506 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9507 {
9508 /* These messages can get long, avoid a wrap in a narrow
9509 * window. Prefer showing edit_submode_extra. */
9510 length = (Rows - msg_row) * Columns - 3;
9511 if (edit_submode_extra != NULL)
9512 length -= vim_strsize(edit_submode_extra);
9513 if (length > 0)
9514 {
9515 if (edit_submode_pre != NULL)
9516 length -= vim_strsize(edit_submode_pre);
9517 if (length - vim_strsize(edit_submode) > 0)
9518 {
9519 if (edit_submode_pre != NULL)
9520 msg_puts_attr(edit_submode_pre, attr);
9521 msg_puts_attr(edit_submode, attr);
9522 }
9523 if (edit_submode_extra != NULL)
9524 {
9525 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9526 if ((int)edit_submode_highl < (int)HLF_COUNT)
9527 sub_attr = hl_attr(edit_submode_highl);
9528 else
9529 sub_attr = attr;
9530 msg_puts_attr(edit_submode_extra, sub_attr);
9531 }
9532 }
9533 length = 0;
9534 }
9535 else
9536#endif
9537 {
9538#ifdef FEAT_VREPLACE
9539 if (State & VREPLACE_FLAG)
9540 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9541 else
9542#endif
9543 if (State & REPLACE_FLAG)
9544 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9545 else if (State & INSERT)
9546 {
9547#ifdef FEAT_RIGHTLEFT
9548 if (p_ri)
9549 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9550#endif
9551 MSG_PUTS_ATTR(_(" INSERT"), attr);
9552 }
9553 else if (restart_edit == 'I')
9554 MSG_PUTS_ATTR(_(" (insert)"), attr);
9555 else if (restart_edit == 'R')
9556 MSG_PUTS_ATTR(_(" (replace)"), attr);
9557 else if (restart_edit == 'V')
9558 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9559#ifdef FEAT_RIGHTLEFT
9560 if (p_hkmap)
9561 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9562# ifdef FEAT_FKMAP
9563 if (p_fkmap)
9564 MSG_PUTS_ATTR(farsi_text_5, attr);
9565# endif
9566#endif
9567#ifdef FEAT_KEYMAP
9568 if (State & LANGMAP)
9569 {
9570# ifdef FEAT_ARABIC
9571 if (curwin->w_p_arab)
9572 MSG_PUTS_ATTR(_(" Arabic"), attr);
9573 else
9574# endif
9575 MSG_PUTS_ATTR(_(" (lang)"), attr);
9576 }
9577#endif
9578 if ((State & INSERT) && p_paste)
9579 MSG_PUTS_ATTR(_(" (paste)"), attr);
9580
9581#ifdef FEAT_VISUAL
9582 if (VIsual_active)
9583 {
9584 char *p;
9585
9586 /* Don't concatenate separate words to avoid translation
9587 * problems. */
9588 switch ((VIsual_select ? 4 : 0)
9589 + (VIsual_mode == Ctrl_V) * 2
9590 + (VIsual_mode == 'V'))
9591 {
9592 case 0: p = N_(" VISUAL"); break;
9593 case 1: p = N_(" VISUAL LINE"); break;
9594 case 2: p = N_(" VISUAL BLOCK"); break;
9595 case 4: p = N_(" SELECT"); break;
9596 case 5: p = N_(" SELECT LINE"); break;
9597 default: p = N_(" SELECT BLOCK"); break;
9598 }
9599 MSG_PUTS_ATTR(_(p), attr);
9600 }
9601#endif
9602 MSG_PUTS_ATTR(" --", attr);
9603 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009604
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 need_clear = TRUE;
9606 }
9607 if (Recording
9608#ifdef FEAT_INS_EXPAND
9609 && edit_submode == NULL /* otherwise it gets too long */
9610#endif
9611 )
9612 {
9613 MSG_PUTS_ATTR(_("recording"), attr);
9614 need_clear = TRUE;
9615 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009616
9617 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 if (need_clear || clear_cmdline)
9619 msg_clr_eos();
9620 msg_didout = FALSE; /* overwrite this message */
9621 length = msg_col;
9622 msg_col = 0;
9623 need_wait_return = nwr_save; /* never ask for hit-return for this */
9624 }
9625 else if (clear_cmdline && msg_silent == 0)
9626 /* Clear the whole command line. Will reset "clear_cmdline". */
9627 msg_clr_cmdline();
9628
9629#ifdef FEAT_CMDL_INFO
9630# ifdef FEAT_VISUAL
9631 /* In Visual mode the size of the selected area must be redrawn. */
9632 if (VIsual_active)
9633 clear_showcmd();
9634# endif
9635
9636 /* If the last window has no status line, the ruler is after the mode
9637 * message and must be redrawn */
9638 if (redrawing()
9639# ifdef FEAT_WINDOWS
9640 && lastwin->w_status_height == 0
9641# endif
9642 )
9643 win_redr_ruler(lastwin, TRUE);
9644#endif
9645 redraw_cmdline = FALSE;
9646 clear_cmdline = FALSE;
9647
9648 return length;
9649}
9650
9651/*
9652 * Position for a mode message.
9653 */
9654 static void
9655msg_pos_mode()
9656{
9657 msg_col = 0;
9658 msg_row = Rows - 1;
9659}
9660
9661/*
9662 * Delete mode message. Used when ESC is typed which is expected to end
9663 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009664 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 */
9666 void
9667unshowmode(force)
9668 int force;
9669{
9670 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009671 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 */
9673 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9674 redraw_cmdline = TRUE; /* delete mode later */
9675 else
9676 {
9677 msg_pos_mode();
9678 if (Recording)
9679 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9680 msg_clr_eos();
9681 }
9682}
9683
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009684#if defined(FEAT_WINDOWS)
9685/*
9686 * Draw the tab pages line at the top of the Vim window.
9687 */
9688 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009689draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009690{
9691 int tabcount = 0;
9692 tabpage_T *tp;
9693 int tabwidth;
9694 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009695 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009696 int attr;
9697 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009698 win_T *cwp;
9699 int wincount;
9700 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009701 int c;
9702 int len;
9703 int attr_sel = hl_attr(HLF_TPS);
9704 int attr_nosel = hl_attr(HLF_TP);
9705 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009706 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009707 int room;
9708 int use_sep_chars = (t_colors < 8
9709#ifdef FEAT_GUI
9710 && !gui.in_use
9711#endif
9712 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009713
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009714 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009715
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009716#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009717 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009718 if (gui_use_tabline())
9719 {
9720 gui_update_tabline();
9721 return;
9722 }
9723#endif
9724
9725 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009726 return;
9727
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009728#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009729
9730 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9731 for (scol = 0; scol < Columns; ++scol)
9732 TabPageIdxs[scol] = 0;
9733
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009734 /* Use the 'tabline' option if it's set. */
9735 if (*p_tal != NUL)
9736 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009737 int save_called_emsg = called_emsg;
9738
9739 /* Check for an error. If there is one we would loop in redrawing the
9740 * screen. Avoid that by making 'tabline' empty. */
9741 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009742 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009743 if (called_emsg)
9744 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009745 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009746 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009747 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009748 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009749#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009750 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009751 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9752 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009753
Bram Moolenaar238a5642006-02-21 22:12:05 +00009754 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9755 if (tabwidth < 6)
9756 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009757
Bram Moolenaar238a5642006-02-21 22:12:05 +00009758 attr = attr_nosel;
9759 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009760 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009761 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9762 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009763 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009764 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009765
Bram Moolenaar238a5642006-02-21 22:12:05 +00009766 if (tp->tp_topframe == topframe)
9767 attr = attr_sel;
9768 if (use_sep_chars && col > 0)
9769 screen_putchar('|', 0, col++, attr);
9770
9771 if (tp->tp_topframe != topframe)
9772 attr = attr_nosel;
9773
9774 screen_putchar(' ', 0, col++, attr);
9775
9776 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009777 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009778 cwp = curwin;
9779 wp = firstwin;
9780 }
9781 else
9782 {
9783 cwp = tp->tp_curwin;
9784 wp = tp->tp_firstwin;
9785 }
9786
9787 modified = FALSE;
9788 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9789 if (bufIsChanged(wp->w_buffer))
9790 modified = TRUE;
9791 if (modified || wincount > 1)
9792 {
9793 if (wincount > 1)
9794 {
9795 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009796 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009797 if (col + len >= Columns - 3)
9798 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009799 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009800#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009801 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009802#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009803 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009804#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009805 );
9806 col += len;
9807 }
9808 if (modified)
9809 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9810 screen_putchar(' ', 0, col++, attr);
9811 }
9812
9813 room = scol - col + tabwidth - 1;
9814 if (room > 0)
9815 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009816 /* Get buffer name in NameBuff[] */
9817 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009818 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009819 len = vim_strsize(NameBuff);
9820 p = NameBuff;
9821#ifdef FEAT_MBYTE
9822 if (has_mbyte)
9823 while (len > room)
9824 {
9825 len -= ptr2cells(p);
9826 mb_ptr_adv(p);
9827 }
9828 else
9829#endif
9830 if (len > room)
9831 {
9832 p += len - room;
9833 len = room;
9834 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009835 if (len > Columns - col - 1)
9836 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009837
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009838 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009839 col += len;
9840 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009841 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009842
9843 /* Store the tab page number in TabPageIdxs[], so that
9844 * jump_to_mouse() knows where each one is. */
9845 ++tabcount;
9846 while (scol < col)
9847 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009848 }
9849
Bram Moolenaar238a5642006-02-21 22:12:05 +00009850 if (use_sep_chars)
9851 c = '_';
9852 else
9853 c = ' ';
9854 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009855
9856 /* Put an "X" for closing the current tab if there are several. */
9857 if (first_tabpage->tp_next != NULL)
9858 {
9859 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9860 TabPageIdxs[Columns - 1] = -999;
9861 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009862 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009863
9864 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9865 * set. */
9866 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009867}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009868
9869/*
9870 * Get buffer name for "buf" into NameBuff[].
9871 * Takes care of special buffer names and translates special characters.
9872 */
9873 void
9874get_trans_bufname(buf)
9875 buf_T *buf;
9876{
9877 if (buf_spname(buf) != NULL)
9878 STRCPY(NameBuff, buf_spname(buf));
9879 else
9880 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9881 trans_characters(NameBuff, MAXPATHL);
9882}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009883#endif
9884
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9886/*
9887 * Get the character to use in a status line. Get its attributes in "*attr".
9888 */
9889 static int
9890fillchar_status(attr, is_curwin)
9891 int *attr;
9892 int is_curwin;
9893{
9894 int fill;
9895 if (is_curwin)
9896 {
9897 *attr = hl_attr(HLF_S);
9898 fill = fill_stl;
9899 }
9900 else
9901 {
9902 *attr = hl_attr(HLF_SNC);
9903 fill = fill_stlnc;
9904 }
9905 /* Use fill when there is highlighting, and highlighting of current
9906 * window differs, or the fillchars differ, or this is not the
9907 * current window */
9908 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9909 || !is_curwin || firstwin == lastwin)
9910 || (fill_stl != fill_stlnc)))
9911 return fill;
9912 if (is_curwin)
9913 return '^';
9914 return '=';
9915}
9916#endif
9917
9918#ifdef FEAT_VERTSPLIT
9919/*
9920 * Get the character to use in a separator between vertically split windows.
9921 * Get its attributes in "*attr".
9922 */
9923 static int
9924fillchar_vsep(attr)
9925 int *attr;
9926{
9927 *attr = hl_attr(HLF_C);
9928 if (*attr == 0 && fill_vert == ' ')
9929 return '|';
9930 else
9931 return fill_vert;
9932}
9933#endif
9934
9935/*
9936 * Return TRUE if redrawing should currently be done.
9937 */
9938 int
9939redrawing()
9940{
9941 return (!RedrawingDisabled
9942 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9943}
9944
9945/*
9946 * Return TRUE if printing messages should currently be done.
9947 */
9948 int
9949messaging()
9950{
9951 return (!(p_lz && char_avail() && !KeyTyped));
9952}
9953
9954/*
9955 * Show current status info in ruler and various other places
9956 * If always is FALSE, only show ruler if position has changed.
9957 */
9958 void
9959showruler(always)
9960 int always;
9961{
9962 if (!always && !redrawing())
9963 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009964#ifdef FEAT_INS_EXPAND
9965 if (pum_visible())
9966 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009967# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009968 /* Don't redraw right now, do it later. */
9969 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009970# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009971 return;
9972 }
9973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009975 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009976 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009977 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 else
9980#endif
9981#ifdef FEAT_CMDL_INFO
9982 win_redr_ruler(curwin, always);
9983#endif
9984
9985#ifdef FEAT_TITLE
9986 if (need_maketitle
9987# ifdef FEAT_STL_OPT
9988 || (p_icon && (stl_syntax & STL_IN_ICON))
9989 || (p_title && (stl_syntax & STL_IN_TITLE))
9990# endif
9991 )
9992 maketitle();
9993#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009994#ifdef FEAT_WINDOWS
9995 /* Redraw the tab pages line if needed. */
9996 if (redraw_tabline)
9997 draw_tabline();
9998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
10001#ifdef FEAT_CMDL_INFO
10002 static void
10003win_redr_ruler(wp, always)
10004 win_T *wp;
10005 int always;
10006{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010007#define RULER_BUF_LEN 70
10008 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 int row;
10010 int fillchar;
10011 int attr;
10012 int empty_line = FALSE;
10013 colnr_T virtcol;
10014 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010015 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 int o;
10017#ifdef FEAT_VERTSPLIT
10018 int this_ru_col;
10019 int off = 0;
10020 int width = Columns;
10021# define WITH_OFF(x) x
10022# define WITH_WIDTH(x) x
10023#else
10024# define WITH_OFF(x) 0
10025# define WITH_WIDTH(x) Columns
10026# define this_ru_col ru_col
10027#endif
10028
10029 /* If 'ruler' off or redrawing disabled, don't do anything */
10030 if (!p_ru)
10031 return;
10032
10033 /*
10034 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10035 * after deleting lines, before cursor.lnum is corrected.
10036 */
10037 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10038 return;
10039
10040#ifdef FEAT_INS_EXPAND
10041 /* Don't draw the ruler while doing insert-completion, it might overwrite
10042 * the (long) mode message. */
10043# ifdef FEAT_WINDOWS
10044 if (wp == lastwin && lastwin->w_status_height == 0)
10045# endif
10046 if (edit_submode != NULL)
10047 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010048 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10049 if (pum_visible())
10050 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051#endif
10052
10053#ifdef FEAT_STL_OPT
10054 if (*p_ruf)
10055 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010056 int save_called_emsg = called_emsg;
10057
10058 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010060 if (called_emsg)
10061 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010062 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010063 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 return;
10065 }
10066#endif
10067
10068 /*
10069 * Check if not in Insert mode and the line is empty (will show "0-1").
10070 */
10071 if (!(State & INSERT)
10072 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10073 empty_line = TRUE;
10074
10075 /*
10076 * Only draw the ruler when something changed.
10077 */
10078 validate_virtcol_win(wp);
10079 if ( redraw_cmdline
10080 || always
10081 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10082 || wp->w_cursor.col != wp->w_ru_cursor.col
10083 || wp->w_virtcol != wp->w_ru_virtcol
10084#ifdef FEAT_VIRTUALEDIT
10085 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10086#endif
10087 || wp->w_topline != wp->w_ru_topline
10088 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10089#ifdef FEAT_DIFF
10090 || wp->w_topfill != wp->w_ru_topfill
10091#endif
10092 || empty_line != wp->w_ru_empty)
10093 {
10094 cursor_off();
10095#ifdef FEAT_WINDOWS
10096 if (wp->w_status_height)
10097 {
10098 row = W_WINROW(wp) + wp->w_height;
10099 fillchar = fillchar_status(&attr, wp == curwin);
10100# ifdef FEAT_VERTSPLIT
10101 off = W_WINCOL(wp);
10102 width = W_WIDTH(wp);
10103# endif
10104 }
10105 else
10106#endif
10107 {
10108 row = Rows - 1;
10109 fillchar = ' ';
10110 attr = 0;
10111#ifdef FEAT_VERTSPLIT
10112 width = Columns;
10113 off = 0;
10114#endif
10115 }
10116
10117 /* In list mode virtcol needs to be recomputed */
10118 virtcol = wp->w_virtcol;
10119 if (wp->w_p_list && lcs_tab1 == NUL)
10120 {
10121 wp->w_p_list = FALSE;
10122 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10123 wp->w_p_list = TRUE;
10124 }
10125
10126 /*
10127 * Some sprintfs return the length, some return a pointer.
10128 * To avoid portability problems we use strlen() here.
10129 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010130 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10132 ? 0L
10133 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010134 len = STRLEN(buffer);
10135 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10137 (int)virtcol + 1);
10138
10139 /*
10140 * Add a "50%" if there is room for it.
10141 * On the last line, don't print in the last column (scrolls the
10142 * screen up on some terminals).
10143 */
10144 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010145 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 o = i + vim_strsize(buffer + i + 1);
10147#ifdef FEAT_WINDOWS
10148 if (wp->w_status_height == 0) /* can't use last char of screen */
10149#endif
10150 ++o;
10151#ifdef FEAT_VERTSPLIT
10152 this_ru_col = ru_col - (Columns - width);
10153 if (this_ru_col < 0)
10154 this_ru_col = 0;
10155#endif
10156 /* Never use more than half the window/screen width, leave the other
10157 * half for the filename. */
10158 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10159 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10160 if (this_ru_col + o < WITH_WIDTH(width))
10161 {
10162 while (this_ru_col + o < WITH_WIDTH(width))
10163 {
10164#ifdef FEAT_MBYTE
10165 if (has_mbyte)
10166 i += (*mb_char2bytes)(fillchar, buffer + i);
10167 else
10168#endif
10169 buffer[i++] = fillchar;
10170 ++o;
10171 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010172 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 }
10174 /* Truncate at window boundary. */
10175#ifdef FEAT_MBYTE
10176 if (has_mbyte)
10177 {
10178 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010179 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 {
10181 o += (*mb_ptr2cells)(buffer + i);
10182 if (this_ru_col + o > WITH_WIDTH(width))
10183 {
10184 buffer[i] = NUL;
10185 break;
10186 }
10187 }
10188 }
10189 else
10190#endif
10191 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10192 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10193
10194 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10195 i = redraw_cmdline;
10196 screen_fill(row, row + 1,
10197 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10198 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10199 fillchar, fillchar, attr);
10200 /* don't redraw the cmdline because of showing the ruler */
10201 redraw_cmdline = i;
10202 wp->w_ru_cursor = wp->w_cursor;
10203 wp->w_ru_virtcol = wp->w_virtcol;
10204 wp->w_ru_empty = empty_line;
10205 wp->w_ru_topline = wp->w_topline;
10206 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10207#ifdef FEAT_DIFF
10208 wp->w_ru_topfill = wp->w_topfill;
10209#endif
10210 }
10211}
10212#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010213
10214#if defined(FEAT_LINEBREAK) || defined(PROTO)
10215/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010216 * Return the width of the 'number' and 'relativenumber' column.
10217 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010218 * Otherwise it depends on 'numberwidth' and the line count.
10219 */
10220 int
10221number_width(wp)
10222 win_T *wp;
10223{
10224 int n;
10225 linenr_T lnum;
10226
Bram Moolenaar64486672010-05-16 15:46:46 +020010227 if (wp->w_p_nu)
10228 /* 'number' */
10229 lnum = wp->w_buffer->b_ml.ml_line_count;
10230 else
10231 /* 'relativenumber' */
10232 lnum = wp->w_height;
10233
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010234 if (lnum == wp->w_nrwidth_line_count)
10235 return wp->w_nrwidth_width;
10236 wp->w_nrwidth_line_count = lnum;
10237
10238 n = 0;
10239 do
10240 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010241 lnum /= 10;
10242 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010243 } while (lnum > 0);
10244
10245 /* 'numberwidth' gives the minimal width plus one */
10246 if (n < wp->w_p_nuw - 1)
10247 n = wp->w_p_nuw - 1;
10248
10249 wp->w_nrwidth_width = n;
10250 return n;
10251}
10252#endif