blob: cc8afa6d215ad201eb018ae8ae08743df6471dc4 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
112#endif
113
114/*
115 * Buffer for one screen line (characters and attributes).
116 */
117static schar_T *current_ScreenLine;
118
119static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000120static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_FOLDING
122static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
123static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
124static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
125#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000126static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
128#ifdef FEAT_RIGHTLEFT
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
131#else
132static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
133# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifdef FEAT_VERTSPLIT
136static void draw_vsep_win __ARGS((win_T *wp, int row));
137#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000139static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000142#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void start_search_hl __ARGS((void));
144static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200145static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
147static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
148#endif
149static void screen_start_highlight __ARGS((int attr));
150static void screen_char __ARGS((unsigned off, int row, int col));
151#ifdef FEAT_MBYTE
152static void screen_char_2 __ARGS((unsigned off, int row, int col));
153#endif
154static void screenclear2 __ARGS((void));
155static void lineclear __ARGS((unsigned off, int width));
156static void lineinvalid __ARGS((unsigned off, int width));
157#ifdef FEAT_VERTSPLIT
158static void linecopy __ARGS((int to, int from, win_T *wp));
159static void redraw_block __ARGS((int row, int end, win_T *wp));
160#endif
161static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
162static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000164#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000165static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
168static int fillchar_status __ARGS((int *attr, int is_curwin));
169#endif
170#ifdef FEAT_VERTSPLIT
171static int fillchar_vsep __ARGS((int *attr));
172#endif
173#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000174static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_CMDL_INFO
177static void win_redr_ruler __ARGS((win_T *wp, int always));
178#endif
179
180#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
181/* Ugly global: overrule attribute used by screen_char() */
182static int screen_char_attr = 0;
183#endif
184
185/*
186 * Redraw the current window later, with update_screen(type).
187 * Set must_redraw only if not already set to a higher value.
188 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
189 */
190 void
191redraw_later(type)
192 int type;
193{
194 redraw_win_later(curwin, type);
195}
196
197 void
198redraw_win_later(wp, type)
199 win_T *wp;
200 int type;
201{
202 if (wp->w_redr_type < type)
203 {
204 wp->w_redr_type = type;
205 if (type >= NOT_VALID)
206 wp->w_lines_valid = 0;
207 if (must_redraw < type) /* must_redraw is the maximum of all windows */
208 must_redraw = type;
209 }
210}
211
212/*
213 * Force a complete redraw later. Also resets the highlighting. To be used
214 * after executing a shell command that messes up the screen.
215 */
216 void
217redraw_later_clear()
218{
219 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000220#ifdef FEAT_GUI
221 if (gui.in_use)
222 /* Use a code that will reset gui.highlight_mask in
223 * gui_stop_highlight(). */
224 screen_attr = HL_ALL + 1;
225 else
226#endif
227 /* Use attributes that is very unlikely to appear in text. */
228 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229}
230
231/*
232 * Mark all windows to be redrawn later.
233 */
234 void
235redraw_all_later(type)
236 int type;
237{
238 win_T *wp;
239
240 FOR_ALL_WINDOWS(wp)
241 {
242 redraw_win_later(wp, type);
243 }
244}
245
246/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000247 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 */
249 void
250redraw_curbuf_later(type)
251 int type;
252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
257redraw_buf_later(buf, type)
258 buf_T *buf;
259 int type;
260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
270/*
271 * Changed something in the current window, at buffer line "lnum", that
272 * requires that line and possibly other lines to be redrawn.
273 * Used when entering/leaving Insert mode with the cursor on a folded line.
274 * Used to remove the "$" from a change command.
275 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
276 * may become invalid and the whole window will have to be redrawn.
277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 void
279redrawWinline(lnum, invalid)
280 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000281 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282{
283#ifdef FEAT_FOLDING
284 int i;
285#endif
286
287 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
288 curwin->w_redraw_top = lnum;
289 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
290 curwin->w_redraw_bot = lnum;
291 redraw_later(VALID);
292
293#ifdef FEAT_FOLDING
294 if (invalid)
295 {
296 /* A w_lines[] entry for this lnum has become invalid. */
297 i = find_wl_entry(curwin, lnum);
298 if (i >= 0)
299 curwin->w_lines[i].wl_valid = FALSE;
300 }
301#endif
302}
303
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200304#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200305 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
307 * update all windows that are editing the current buffer
308 */
309 void
310update_curbuf(type)
311 int type;
312{
313 redraw_curbuf_later(type);
314 update_screen(type);
315}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
318/*
319 * update_screen()
320 *
321 * Based on the current value of curwin->w_topline, transfer a screenfull
322 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
323 */
324 void
325update_screen(type)
326 int type;
327{
328 win_T *wp;
329 static int did_intro = FALSE;
330#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
331 int did_one;
332#endif
333
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000334 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 if (!screen_valid(TRUE))
336 return;
337
338 if (must_redraw)
339 {
340 if (type < must_redraw) /* use maximal type */
341 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000342
343 /* must_redraw is reset here, so that when we run into some weird
344 * reason to redraw while busy redrawing (e.g., asynchronous
345 * scrolling), or update_topline() in win_update() will cause a
346 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 must_redraw = 0;
348 }
349
350 /* Need to update w_lines[]. */
351 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
352 type = NOT_VALID;
353
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000354 /* Postpone the redrawing when it's not needed and when being called
355 * recursively. */
356 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
358 redraw_later(type); /* remember type for next time */
359 must_redraw = type;
360 if (type > INVERTED_ALL)
361 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
362 return;
363 }
364
365 updating_screen = TRUE;
366#ifdef FEAT_SYN_HL
367 ++display_tick; /* let syntax code know we're in a next round of
368 * display updating */
369#endif
370
371 /*
372 * if the screen was scrolled up when displaying a message, scroll it down
373 */
374 if (msg_scrolled)
375 {
376 clear_cmdline = TRUE;
377 if (msg_scrolled > Rows - 5) /* clearing is faster */
378 type = CLEAR;
379 else if (type != CLEAR)
380 {
381 check_for_delay(FALSE);
382 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
383 type = CLEAR;
384 FOR_ALL_WINDOWS(wp)
385 {
386 if (W_WINROW(wp) < msg_scrolled)
387 {
388 if (W_WINROW(wp) + wp->w_height > msg_scrolled
389 && wp->w_redr_type < REDRAW_TOP
390 && wp->w_lines_valid > 0
391 && wp->w_topline == wp->w_lines[0].wl_lnum)
392 {
393 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
394 wp->w_redr_type = REDRAW_TOP;
395 }
396 else
397 {
398 wp->w_redr_type = NOT_VALID;
399#ifdef FEAT_WINDOWS
400 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
401 <= msg_scrolled)
402 wp->w_redr_status = TRUE;
403#endif
404 }
405 }
406 }
407 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000408#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000409 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 }
412 msg_scrolled = 0;
413 need_wait_return = FALSE;
414 }
415
416 /* reset cmdline_row now (may have been changed temporarily) */
417 compute_cmdrow();
418
419 /* Check for changed highlighting */
420 if (need_highlight_changed)
421 highlight_changed();
422
423 if (type == CLEAR) /* first clear screen */
424 {
425 screenclear(); /* will reset clear_cmdline */
426 type = NOT_VALID;
427 }
428
429 if (clear_cmdline) /* going to clear cmdline (done below) */
430 check_for_delay(FALSE);
431
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 /* Force redraw when width of 'number' or 'relativenumber' column
434 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200436 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
437 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000438 curwin->w_redr_type = NOT_VALID;
439#endif
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 /*
442 * Only start redrawing if there is really something to do.
443 */
444 if (type == INVERTED)
445 update_curswant();
446 if (curwin->w_redr_type < type
447 && !((type == VALID
448 && curwin->w_lines[0].wl_valid
449#ifdef FEAT_DIFF
450 && curwin->w_topfill == curwin->w_old_topfill
451 && curwin->w_botfill == curwin->w_old_botfill
452#endif
453 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
454#ifdef FEAT_VISUAL
455 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000456 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
458 && curwin->w_old_visual_mode == VIsual_mode
459 && (curwin->w_valid & VALID_VIRTCOL)
460 && curwin->w_old_curswant == curwin->w_curswant)
461#endif
462 ))
463 curwin->w_redr_type = type;
464
Bram Moolenaar5a305422006-04-28 22:38:25 +0000465#ifdef FEAT_WINDOWS
466 /* Redraw the tab pages line if needed. */
467 if (redraw_tabline || type >= NOT_VALID)
468 draw_tabline();
469#endif
470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471#ifdef FEAT_SYN_HL
472 /*
473 * Correct stored syntax highlighting info for changes in each displayed
474 * buffer. Each buffer must only be done once.
475 */
476 FOR_ALL_WINDOWS(wp)
477 {
478 if (wp->w_buffer->b_mod_set)
479 {
480# ifdef FEAT_WINDOWS
481 win_T *wwp;
482
483 /* Check if we already did this buffer. */
484 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
485 if (wwp->w_buffer == wp->w_buffer)
486 break;
487# endif
488 if (
489# ifdef FEAT_WINDOWS
490 wwp == wp &&
491# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200492 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 syn_stack_apply_changes(wp->w_buffer);
494 }
495 }
496#endif
497
498 /*
499 * Go from top to bottom through the windows, redrawing the ones that need
500 * it.
501 */
502#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
503 did_one = FALSE;
504#endif
505#ifdef FEAT_SEARCH_EXTRA
506 search_hl.rm.regprog = NULL;
507#endif
508 FOR_ALL_WINDOWS(wp)
509 {
510 if (wp->w_redr_type != 0)
511 {
512 cursor_off();
513#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
514 if (!did_one)
515 {
516 did_one = TRUE;
517# ifdef FEAT_SEARCH_EXTRA
518 start_search_hl();
519# endif
520# ifdef FEAT_CLIPBOARD
521 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200522 if (clip_star.available && clip_isautosel_star())
523 clip_update_selection(&clip_star);
524 if (clip_plus.available && clip_isautosel_plus())
525 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# endif
527#ifdef FEAT_GUI
528 /* Remove the cursor before starting to do anything, because
529 * scrolling may make it difficult to redraw the text under
530 * it. */
531 if (gui.in_use)
532 gui_undraw_cursor();
533#endif
534 }
535#endif
536 win_update(wp);
537 }
538
539#ifdef FEAT_WINDOWS
540 /* redraw status line after the window to minimize cursor movement */
541 if (wp->w_redr_status)
542 {
543 cursor_off();
544 win_redr_status(wp);
545 }
546#endif
547 }
548#if defined(FEAT_SEARCH_EXTRA)
549 end_search_hl();
550#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100551#ifdef FEAT_INS_EXPAND
552 /* May need to redraw the popup menu. */
553 if (pum_visible())
554 pum_redraw();
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557#ifdef FEAT_WINDOWS
558 /* Reset b_mod_set flags. Going through all windows is probably faster
559 * than going through all buffers (there could be many buffers). */
560 for (wp = firstwin; wp != NULL; wp = wp->w_next)
561 wp->w_buffer->b_mod_set = FALSE;
562#else
563 curbuf->b_mod_set = FALSE;
564#endif
565
566 updating_screen = FALSE;
567#ifdef FEAT_GUI
568 gui_may_resize_shell();
569#endif
570
571 /* Clear or redraw the command line. Done last, because scrolling may
572 * mess up the command line. */
573 if (clear_cmdline || redraw_cmdline)
574 showmode();
575
576 /* May put up an introductory message when not editing a file */
577 if (!did_intro && bufempty()
578 && curbuf->b_fname == NULL
579#ifdef FEAT_WINDOWS
580 && firstwin->w_next == NULL
581#endif
582 && vim_strchr(p_shm, SHM_INTRO) == NULL)
583 intro_message(FALSE);
584 did_intro = TRUE;
585
586#ifdef FEAT_GUI
587 /* Redraw the cursor and update the scrollbars when all screen updating is
588 * done. */
589 if (gui.in_use)
590 {
591 out_flush(); /* required before updating the cursor */
592 if (did_one)
593 gui_update_cursor(FALSE, FALSE);
594 gui_update_scrollbars(FALSE);
595 }
596#endif
597}
598
Bram Moolenaar860cae12010-06-05 23:22:07 +0200599#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200600/*
601 * Return TRUE if the cursor line in window "wp" may be concealed, according
602 * to the 'concealcursor' option.
603 */
604 int
605conceal_cursor_line(wp)
606 win_T *wp;
607{
608 int c;
609
610 if (*wp->w_p_cocu == NUL)
611 return FALSE;
612 if (get_real_state() & VISUAL)
613 c = 'v';
614 else if (State & INSERT)
615 c = 'i';
616 else if (State & NORMAL)
617 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200618 else if (State & CMDLINE)
619 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200620 else
621 return FALSE;
622 return vim_strchr(wp->w_p_cocu, c) != NULL;
623}
624
625/*
626 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
627 */
628 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200629conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200630{
631 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
632 {
633 need_cursor_line_redraw = TRUE;
634 /* Need to recompute cursor column, e.g., when starting Visual mode
635 * without concealing. */
636 curs_columns(TRUE);
637 }
638}
639
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 void
641update_single_line(wp, lnum)
642 win_T *wp;
643 linenr_T lnum;
644{
645 int row;
646 int j;
647
648 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200649 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200650 {
651# ifdef FEAT_GUI
652 /* Remove the cursor before starting to do anything, because scrolling
653 * may make it difficult to redraw the text under it. */
654 if (gui.in_use)
655 gui_undraw_cursor();
656# endif
657 row = 0;
658 for (j = 0; j < wp->w_lines_valid; ++j)
659 {
660 if (lnum == wp->w_lines[j].wl_lnum)
661 {
662 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200663# ifdef FEAT_SEARCH_EXTRA
664 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200665 start_search_hl();
666 prepare_search_hl(wp, lnum);
667# endif
668 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
669# if defined(FEAT_SEARCH_EXTRA)
670 end_search_hl();
671# endif
672 break;
673 }
674 row += wp->w_lines[j].wl_size;
675 }
676# ifdef FEAT_GUI
677 /* Redraw the cursor */
678 if (gui.in_use)
679 {
680 out_flush(); /* required before updating the cursor */
681 gui_update_cursor(FALSE, FALSE);
682 }
683# endif
684 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200685 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200686}
687#endif
688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
690static void update_prepare __ARGS((void));
691static void update_finish __ARGS((void));
692
693/*
694 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000695 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 */
697 static void
698update_prepare()
699{
700 cursor_off();
701 updating_screen = TRUE;
702#ifdef FEAT_GUI
703 /* Remove the cursor before starting to do anything, because scrolling may
704 * make it difficult to redraw the text under it. */
705 if (gui.in_use)
706 gui_undraw_cursor();
707#endif
708#ifdef FEAT_SEARCH_EXTRA
709 start_search_hl();
710#endif
711}
712
713/*
714 * Finish updating one or more windows.
715 */
716 static void
717update_finish()
718{
719 if (redraw_cmdline)
720 showmode();
721
722# ifdef FEAT_SEARCH_EXTRA
723 end_search_hl();
724# endif
725
726 updating_screen = FALSE;
727
728# ifdef FEAT_GUI
729 gui_may_resize_shell();
730
731 /* Redraw the cursor and update the scrollbars when all screen updating is
732 * done. */
733 if (gui.in_use)
734 {
735 out_flush(); /* required before updating the cursor */
736 gui_update_cursor(FALSE, FALSE);
737 gui_update_scrollbars(FALSE);
738 }
739# endif
740}
741#endif
742
743#if defined(FEAT_SIGNS) || defined(PROTO)
744 void
745update_debug_sign(buf, lnum)
746 buf_T *buf;
747 linenr_T lnum;
748{
749 win_T *wp;
750 int doit = FALSE;
751
752# ifdef FEAT_FOLDING
753 win_foldinfo.fi_level = 0;
754# endif
755
756 /* update/delete a specific mark */
757 FOR_ALL_WINDOWS(wp)
758 {
759 if (buf != NULL && lnum > 0)
760 {
761 if (wp->w_buffer == buf && lnum >= wp->w_topline
762 && lnum < wp->w_botline)
763 {
764 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
765 wp->w_redraw_top = lnum;
766 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
767 wp->w_redraw_bot = lnum;
768 redraw_win_later(wp, VALID);
769 }
770 }
771 else
772 redraw_win_later(wp, VALID);
773 if (wp->w_redr_type != 0)
774 doit = TRUE;
775 }
776
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100777 /* Return when there is nothing to do, screen updating is already
778 * happening (recursive call) or still starting up. */
779 if (!doit || updating_screen
780#ifdef FEAT_GUI
781 || gui.starting
782#endif
783 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 return;
785
786 /* update all windows that need updating */
787 update_prepare();
788
789# ifdef FEAT_WINDOWS
790 for (wp = firstwin; wp; wp = wp->w_next)
791 {
792 if (wp->w_redr_type != 0)
793 win_update(wp);
794 if (wp->w_redr_status)
795 win_redr_status(wp);
796 }
797# else
798 if (curwin->w_redr_type != 0)
799 win_update(curwin);
800# endif
801
802 update_finish();
803}
804#endif
805
806
807#if defined(FEAT_GUI) || defined(PROTO)
808/*
809 * Update a single window, its status line and maybe the command line msg.
810 * Used for the GUI scrollbar.
811 */
812 void
813updateWindow(wp)
814 win_T *wp;
815{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000816 /* return if already busy updating */
817 if (updating_screen)
818 return;
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 update_prepare();
821
822#ifdef FEAT_CLIPBOARD
823 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200824 if (clip_star.available && clip_isautosel_star())
825 clip_update_selection(&clip_star);
826 if (clip_plus.available && clip_isautosel_plus())
827 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000833 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000834 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000835 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (wp->w_redr_status
838# ifdef FEAT_CMDL_INFO
839 || p_ru
840# endif
841# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000842 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843# endif
844 )
845 win_redr_status(wp);
846#endif
847
848 update_finish();
849}
850#endif
851
852/*
853 * Update a single window.
854 *
855 * This may cause the windows below it also to be redrawn (when clearing the
856 * screen or scrolling lines).
857 *
858 * How the window is redrawn depends on wp->w_redr_type. Each type also
859 * implies the one below it.
860 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000861 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
863 * INVERTED redraw the changed part of the Visual area
864 * INVERTED_ALL redraw the whole Visual area
865 * VALID 1. scroll up/down to adjust for a changed w_topline
866 * 2. update lines at the top when scrolled down
867 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000868 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 * b_mod_top and b_mod_bot.
870 * - if wp->w_redraw_top non-zero, redraw lines between
871 * wp->w_redraw_top and wp->w_redr_bot.
872 * - continue redrawing when syntax status is invalid.
873 * 4. if scrolled up, update lines at the bottom.
874 * This results in three areas that may need updating:
875 * top: from first row to top_end (when scrolled down)
876 * mid: from mid_start to mid_end (update inversion or changed text)
877 * bot: from bot_start to last row (when scrolled up)
878 */
879 static void
880win_update(wp)
881 win_T *wp;
882{
883 buf_T *buf = wp->w_buffer;
884 int type;
885 int top_end = 0; /* Below last row of the top area that needs
886 updating. 0 when no top area updating. */
887 int mid_start = 999;/* first row of the mid area that needs
888 updating. 999 when no mid area updating. */
889 int mid_end = 0; /* Below last row of the mid area that needs
890 updating. 0 when no mid area updating. */
891 int bot_start = 999;/* first row of the bot area that needs
892 updating. 999 when no bot area updating */
893#ifdef FEAT_VISUAL
894 int scrolled_down = FALSE; /* TRUE when scrolled down when
895 w_topline got smaller a bit */
896#endif
897#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000898 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 int top_to_mod = FALSE; /* redraw above mod_top */
900#endif
901
902 int row; /* current window row to display */
903 linenr_T lnum; /* current buffer lnum to display */
904 int idx; /* current index in w_lines[] */
905 int srow; /* starting row of the current line */
906
907 int eof = FALSE; /* if TRUE, we hit the end of the file */
908 int didline = FALSE; /* if TRUE, we finished the last line */
909 int i;
910 long j;
911 static int recursive = FALSE; /* being called recursively */
912 int old_botline = wp->w_botline;
913#ifdef FEAT_FOLDING
914 long fold_count;
915#endif
916#ifdef FEAT_SYN_HL
917 /* remember what happened to the previous line, to know if
918 * check_visual_highlight() can be used */
919#define DID_NONE 1 /* didn't update a line */
920#define DID_LINE 2 /* updated a normal line */
921#define DID_FOLD 3 /* updated a folded line */
922 int did_update = DID_NONE;
923 linenr_T syntax_last_parsed = 0; /* last parsed text line */
924#endif
925 linenr_T mod_top = 0;
926 linenr_T mod_bot = 0;
927#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
928 int save_got_int;
929#endif
930
931 type = wp->w_redr_type;
932
933 if (type == NOT_VALID)
934 {
935#ifdef FEAT_WINDOWS
936 wp->w_redr_status = TRUE;
937#endif
938 wp->w_lines_valid = 0;
939 }
940
941 /* Window is zero-height: nothing to draw. */
942 if (wp->w_height == 0)
943 {
944 wp->w_redr_type = 0;
945 return;
946 }
947
948#ifdef FEAT_VERTSPLIT
949 /* Window is zero-width: Only need to draw the separator. */
950 if (wp->w_width == 0)
951 {
952 /* draw the vertical separator right of this window */
953 draw_vsep_win(wp, 0);
954 wp->w_redr_type = 0;
955 return;
956 }
957#endif
958
959#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200960 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#endif
962
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000963#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200964 /* Force redraw when width of 'number' or 'relativenumber' column
965 * changes. */
966 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000967 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000968 {
969 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000970 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000971 }
972 else
973#endif
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
976 {
977 /*
978 * When there are both inserted/deleted lines and specific lines to be
979 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
980 * everything (only happens when redrawing is off for while).
981 */
982 type = NOT_VALID;
983 }
984 else
985 {
986 /*
987 * Set mod_top to the first line that needs displaying because of
988 * changes. Set mod_bot to the first line after the changes.
989 */
990 mod_top = wp->w_redraw_top;
991 if (wp->w_redraw_bot != 0)
992 mod_bot = wp->w_redraw_bot + 1;
993 else
994 mod_bot = 0;
995 wp->w_redraw_top = 0; /* reset for next time */
996 wp->w_redraw_bot = 0;
997 if (buf->b_mod_set)
998 {
999 if (mod_top == 0 || mod_top > buf->b_mod_top)
1000 {
1001 mod_top = buf->b_mod_top;
1002#ifdef FEAT_SYN_HL
1003 /* Need to redraw lines above the change that may be included
1004 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001005 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001007 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (mod_top < 1)
1009 mod_top = 1;
1010 }
1011#endif
1012 }
1013 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1014 mod_bot = buf->b_mod_bot;
1015
1016#ifdef FEAT_SEARCH_EXTRA
1017 /* When 'hlsearch' is on and using a multi-line search pattern, a
1018 * change in one line may make the Search highlighting in a
1019 * previous line invalid. Simple solution: redraw all visible
1020 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001021 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001023 if (search_hl.rm.regprog != NULL
1024 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001026 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001027 {
1028 cur = wp->w_match_head;
1029 while (cur != NULL)
1030 {
1031 if (cur->match.regprog != NULL
1032 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001033 {
1034 top_to_mod = TRUE;
1035 break;
1036 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001037 cur = cur->next;
1038 }
1039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#endif
1041 }
1042#ifdef FEAT_FOLDING
1043 if (mod_top != 0 && hasAnyFolding(wp))
1044 {
1045 linenr_T lnumt, lnumb;
1046
1047 /*
1048 * A change in a line can cause lines above it to become folded or
1049 * unfolded. Find the top most buffer line that may be affected.
1050 * If the line was previously folded and displayed, get the first
1051 * line of that fold. If the line is folded now, get the first
1052 * folded line. Use the minimum of these two.
1053 */
1054
1055 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1056 * the line below it. If there is no valid entry, use w_topline.
1057 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1058 * to this line. If there is no valid entry, use MAXLNUM. */
1059 lnumt = wp->w_topline;
1060 lnumb = MAXLNUM;
1061 for (i = 0; i < wp->w_lines_valid; ++i)
1062 if (wp->w_lines[i].wl_valid)
1063 {
1064 if (wp->w_lines[i].wl_lastlnum < mod_top)
1065 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1066 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1067 {
1068 lnumb = wp->w_lines[i].wl_lnum;
1069 /* When there is a fold column it might need updating
1070 * in the next line ("J" just above an open fold). */
1071 if (wp->w_p_fdc > 0)
1072 ++lnumb;
1073 }
1074 }
1075
1076 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1077 if (mod_top > lnumt)
1078 mod_top = lnumt;
1079
1080 /* Now do the same for the bottom line (one above mod_bot). */
1081 --mod_bot;
1082 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1083 ++mod_bot;
1084 if (mod_bot < lnumb)
1085 mod_bot = lnumb;
1086 }
1087#endif
1088
1089 /* When a change starts above w_topline and the end is below
1090 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001091 * If the end of the change is above w_topline: do like no change was
1092 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (mod_top != 0 && mod_top < wp->w_topline)
1094 {
1095 if (mod_bot > wp->w_topline)
1096 mod_top = wp->w_topline;
1097#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001098 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 top_end = 1;
1100#endif
1101 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001102
1103 /* When line numbers are displayed need to redraw all lines below
1104 * inserted/deleted lines. */
1105 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1106 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 }
1108
1109 /*
1110 * When only displaying the lines at the top, set top_end. Used when
1111 * window has scrolled down for msg_scrolled.
1112 */
1113 if (type == REDRAW_TOP)
1114 {
1115 j = 0;
1116 for (i = 0; i < wp->w_lines_valid; ++i)
1117 {
1118 j += wp->w_lines[i].wl_size;
1119 if (j >= wp->w_upd_rows)
1120 {
1121 top_end = j;
1122 break;
1123 }
1124 }
1125 if (top_end == 0)
1126 /* not found (cannot happen?): redraw everything */
1127 type = NOT_VALID;
1128 else
1129 /* top area defined, the rest is VALID */
1130 type = VALID;
1131 }
1132
Bram Moolenaar367329b2007-08-30 11:53:22 +00001133 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001134 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1135 * non-zero and thus not FALSE) will indicate that screenclear() was not
1136 * called. */
1137 if (screen_cleared)
1138 screen_cleared = MAYBE;
1139
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 /*
1141 * If there are no changes on the screen that require a complete redraw,
1142 * handle three cases:
1143 * 1: we are off the top of the screen by a few lines: scroll down
1144 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1145 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1146 * w_lines[] that needs updating.
1147 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001148 if ((type == VALID || type == SOME_VALID
1149 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150#ifdef FEAT_DIFF
1151 && !wp->w_botfill && !wp->w_old_botfill
1152#endif
1153 )
1154 {
1155 if (mod_top != 0 && wp->w_topline == mod_top)
1156 {
1157 /*
1158 * w_topline is the first changed line, the scrolling will be done
1159 * further down.
1160 */
1161 }
1162 else if (wp->w_lines[0].wl_valid
1163 && (wp->w_topline < wp->w_lines[0].wl_lnum
1164#ifdef FEAT_DIFF
1165 || (wp->w_topline == wp->w_lines[0].wl_lnum
1166 && wp->w_topfill > wp->w_old_topfill)
1167#endif
1168 ))
1169 {
1170 /*
1171 * New topline is above old topline: May scroll down.
1172 */
1173#ifdef FEAT_FOLDING
1174 if (hasAnyFolding(wp))
1175 {
1176 linenr_T ln;
1177
1178 /* count the number of lines we are off, counting a sequence
1179 * of folded lines as one */
1180 j = 0;
1181 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1182 {
1183 ++j;
1184 if (j >= wp->w_height - 2)
1185 break;
1186 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1187 }
1188 }
1189 else
1190#endif
1191 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1192 if (j < wp->w_height - 2) /* not too far off */
1193 {
1194 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1195#ifdef FEAT_DIFF
1196 /* insert extra lines for previously invisible filler lines */
1197 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1198 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1199 - wp->w_old_topfill;
1200#endif
1201 if (i < wp->w_height - 2) /* less than a screen off */
1202 {
1203 /*
1204 * Try to insert the correct number of lines.
1205 * If not the last window, delete the lines at the bottom.
1206 * win_ins_lines may fail when the terminal can't do it.
1207 */
1208 if (i > 0)
1209 check_for_delay(FALSE);
1210 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1211 {
1212 if (wp->w_lines_valid != 0)
1213 {
1214 /* Need to update rows that are new, stop at the
1215 * first one that scrolled down. */
1216 top_end = i;
1217#ifdef FEAT_VISUAL
1218 scrolled_down = TRUE;
1219#endif
1220
1221 /* Move the entries that were scrolled, disable
1222 * the entries for the lines to be redrawn. */
1223 if ((wp->w_lines_valid += j) > wp->w_height)
1224 wp->w_lines_valid = wp->w_height;
1225 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1226 wp->w_lines[idx] = wp->w_lines[idx - j];
1227 while (idx >= 0)
1228 wp->w_lines[idx--].wl_valid = FALSE;
1229 }
1230 }
1231 else
1232 mid_start = 0; /* redraw all lines */
1233 }
1234 else
1235 mid_start = 0; /* redraw all lines */
1236 }
1237 else
1238 mid_start = 0; /* redraw all lines */
1239 }
1240 else
1241 {
1242 /*
1243 * New topline is at or below old topline: May scroll up.
1244 * When topline didn't change, find first entry in w_lines[] that
1245 * needs updating.
1246 */
1247
1248 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1249 j = -1;
1250 row = 0;
1251 for (i = 0; i < wp->w_lines_valid; i++)
1252 {
1253 if (wp->w_lines[i].wl_valid
1254 && wp->w_lines[i].wl_lnum == wp->w_topline)
1255 {
1256 j = i;
1257 break;
1258 }
1259 row += wp->w_lines[i].wl_size;
1260 }
1261 if (j == -1)
1262 {
1263 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1264 * lines */
1265 mid_start = 0;
1266 }
1267 else
1268 {
1269 /*
1270 * Try to delete the correct number of lines.
1271 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1272 */
1273#ifdef FEAT_DIFF
1274 /* If the topline didn't change, delete old filler lines,
1275 * otherwise delete filler lines of the new topline... */
1276 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1277 row += wp->w_old_topfill;
1278 else
1279 row += diff_check_fill(wp, wp->w_topline);
1280 /* ... but don't delete new filler lines. */
1281 row -= wp->w_topfill;
1282#endif
1283 if (row > 0)
1284 {
1285 check_for_delay(FALSE);
1286 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1287 bot_start = wp->w_height - row;
1288 else
1289 mid_start = 0; /* redraw all lines */
1290 }
1291 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1292 {
1293 /*
1294 * Skip the lines (below the deleted lines) that are still
1295 * valid and don't need redrawing. Copy their info
1296 * upwards, to compensate for the deleted lines. Set
1297 * bot_start to the first row that needs redrawing.
1298 */
1299 bot_start = 0;
1300 idx = 0;
1301 for (;;)
1302 {
1303 wp->w_lines[idx] = wp->w_lines[j];
1304 /* stop at line that didn't fit, unless it is still
1305 * valid (no lines deleted) */
1306 if (row > 0 && bot_start + row
1307 + (int)wp->w_lines[j].wl_size > wp->w_height)
1308 {
1309 wp->w_lines_valid = idx + 1;
1310 break;
1311 }
1312 bot_start += wp->w_lines[idx++].wl_size;
1313
1314 /* stop at the last valid entry in w_lines[].wl_size */
1315 if (++j >= wp->w_lines_valid)
1316 {
1317 wp->w_lines_valid = idx;
1318 break;
1319 }
1320 }
1321#ifdef FEAT_DIFF
1322 /* Correct the first entry for filler lines at the top
1323 * when it won't get updated below. */
1324 if (wp->w_p_diff && bot_start > 0)
1325 wp->w_lines[0].wl_size =
1326 plines_win_nofill(wp, wp->w_topline, TRUE)
1327 + wp->w_topfill;
1328#endif
1329 }
1330 }
1331 }
1332
1333 /* When starting redraw in the first line, redraw all lines. When
1334 * there is only one window it's probably faster to clear the screen
1335 * first. */
1336 if (mid_start == 0)
1337 {
1338 mid_end = wp->w_height;
1339 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001340 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001341 /* Clear the screen when it was not done by win_del_lines() or
1342 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1343 * then. */
1344 if (screen_cleared != TRUE)
1345 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001346#ifdef FEAT_WINDOWS
1347 /* The screen was cleared, redraw the tab pages line. */
1348 if (redraw_tabline)
1349 draw_tabline();
1350#endif
1351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001353
1354 /* When win_del_lines() or win_ins_lines() caused the screen to be
1355 * cleared (only happens for the first window) or when screenclear()
1356 * was called directly above, "must_redraw" will have been set to
1357 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1358 if (screen_cleared == TRUE)
1359 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 else
1362 {
1363 /* Not VALID or INVERTED: redraw all lines. */
1364 mid_start = 0;
1365 mid_end = wp->w_height;
1366 }
1367
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001368 if (type == SOME_VALID)
1369 {
1370 /* SOME_VALID: redraw all lines. */
1371 mid_start = 0;
1372 mid_end = wp->w_height;
1373 type = NOT_VALID;
1374 }
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#ifdef FEAT_VISUAL
1377 /* check if we are updating or removing the inverted part */
1378 if ((VIsual_active && buf == curwin->w_buffer)
1379 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1380 {
1381 linenr_T from, to;
1382
1383 if (VIsual_active)
1384 {
1385 if (VIsual_active
1386 && (VIsual_mode != wp->w_old_visual_mode
1387 || type == INVERTED_ALL))
1388 {
1389 /*
1390 * If the type of Visual selection changed, redraw the whole
1391 * selection. Also when the ownership of the X selection is
1392 * gained or lost.
1393 */
1394 if (curwin->w_cursor.lnum < VIsual.lnum)
1395 {
1396 from = curwin->w_cursor.lnum;
1397 to = VIsual.lnum;
1398 }
1399 else
1400 {
1401 from = VIsual.lnum;
1402 to = curwin->w_cursor.lnum;
1403 }
1404 /* redraw more when the cursor moved as well */
1405 if (wp->w_old_cursor_lnum < from)
1406 from = wp->w_old_cursor_lnum;
1407 if (wp->w_old_cursor_lnum > to)
1408 to = wp->w_old_cursor_lnum;
1409 if (wp->w_old_visual_lnum < from)
1410 from = wp->w_old_visual_lnum;
1411 if (wp->w_old_visual_lnum > to)
1412 to = wp->w_old_visual_lnum;
1413 }
1414 else
1415 {
1416 /*
1417 * Find the line numbers that need to be updated: The lines
1418 * between the old cursor position and the current cursor
1419 * position. Also check if the Visual position changed.
1420 */
1421 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1422 {
1423 from = curwin->w_cursor.lnum;
1424 to = wp->w_old_cursor_lnum;
1425 }
1426 else
1427 {
1428 from = wp->w_old_cursor_lnum;
1429 to = curwin->w_cursor.lnum;
1430 if (from == 0) /* Visual mode just started */
1431 from = to;
1432 }
1433
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001434 if (VIsual.lnum != wp->w_old_visual_lnum
1435 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 {
1437 if (wp->w_old_visual_lnum < from
1438 && wp->w_old_visual_lnum != 0)
1439 from = wp->w_old_visual_lnum;
1440 if (wp->w_old_visual_lnum > to)
1441 to = wp->w_old_visual_lnum;
1442 if (VIsual.lnum < from)
1443 from = VIsual.lnum;
1444 if (VIsual.lnum > to)
1445 to = VIsual.lnum;
1446 }
1447 }
1448
1449 /*
1450 * If in block mode and changed column or curwin->w_curswant:
1451 * update all lines.
1452 * First compute the actual start and end column.
1453 */
1454 if (VIsual_mode == Ctrl_V)
1455 {
1456 colnr_T fromc, toc;
1457
1458 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1459 ++toc;
1460 if (curwin->w_curswant == MAXCOL)
1461 toc = MAXCOL;
1462
1463 if (fromc != wp->w_old_cursor_fcol
1464 || toc != wp->w_old_cursor_lcol)
1465 {
1466 if (from > VIsual.lnum)
1467 from = VIsual.lnum;
1468 if (to < VIsual.lnum)
1469 to = VIsual.lnum;
1470 }
1471 wp->w_old_cursor_fcol = fromc;
1472 wp->w_old_cursor_lcol = toc;
1473 }
1474 }
1475 else
1476 {
1477 /* Use the line numbers of the old Visual area. */
1478 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1479 {
1480 from = wp->w_old_cursor_lnum;
1481 to = wp->w_old_visual_lnum;
1482 }
1483 else
1484 {
1485 from = wp->w_old_visual_lnum;
1486 to = wp->w_old_cursor_lnum;
1487 }
1488 }
1489
1490 /*
1491 * There is no need to update lines above the top of the window.
1492 */
1493 if (from < wp->w_topline)
1494 from = wp->w_topline;
1495
1496 /*
1497 * If we know the value of w_botline, use it to restrict the update to
1498 * the lines that are visible in the window.
1499 */
1500 if (wp->w_valid & VALID_BOTLINE)
1501 {
1502 if (from >= wp->w_botline)
1503 from = wp->w_botline - 1;
1504 if (to >= wp->w_botline)
1505 to = wp->w_botline - 1;
1506 }
1507
1508 /*
1509 * Find the minimal part to be updated.
1510 * Watch out for scrolling that made entries in w_lines[] invalid.
1511 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1512 * top_end; need to redraw from top_end to the "to" line.
1513 * A middle mouse click with a Visual selection may change the text
1514 * above the Visual area and reset wl_valid, do count these for
1515 * mid_end (in srow).
1516 */
1517 if (mid_start > 0)
1518 {
1519 lnum = wp->w_topline;
1520 idx = 0;
1521 srow = 0;
1522 if (scrolled_down)
1523 mid_start = top_end;
1524 else
1525 mid_start = 0;
1526 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1527 {
1528 if (wp->w_lines[idx].wl_valid)
1529 mid_start += wp->w_lines[idx].wl_size;
1530 else if (!scrolled_down)
1531 srow += wp->w_lines[idx].wl_size;
1532 ++idx;
1533# ifdef FEAT_FOLDING
1534 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1535 lnum = wp->w_lines[idx].wl_lnum;
1536 else
1537# endif
1538 ++lnum;
1539 }
1540 srow += mid_start;
1541 mid_end = wp->w_height;
1542 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1543 {
1544 if (wp->w_lines[idx].wl_valid
1545 && wp->w_lines[idx].wl_lnum >= to + 1)
1546 {
1547 /* Only update until first row of this line */
1548 mid_end = srow;
1549 break;
1550 }
1551 srow += wp->w_lines[idx].wl_size;
1552 }
1553 }
1554 }
1555
1556 if (VIsual_active && buf == curwin->w_buffer)
1557 {
1558 wp->w_old_visual_mode = VIsual_mode;
1559 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1560 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001561 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 wp->w_old_curswant = curwin->w_curswant;
1563 }
1564 else
1565 {
1566 wp->w_old_visual_mode = 0;
1567 wp->w_old_cursor_lnum = 0;
1568 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001569 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
1571#endif /* FEAT_VISUAL */
1572
1573#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1574 /* reset got_int, otherwise regexp won't work */
1575 save_got_int = got_int;
1576 got_int = 0;
1577#endif
1578#ifdef FEAT_FOLDING
1579 win_foldinfo.fi_level = 0;
1580#endif
1581
1582 /*
1583 * Update all the window rows.
1584 */
1585 idx = 0; /* first entry in w_lines[].wl_size */
1586 row = 0;
1587 srow = 0;
1588 lnum = wp->w_topline; /* first line shown in window */
1589 for (;;)
1590 {
1591 /* stop updating when reached the end of the window (check for _past_
1592 * the end of the window is at the end of the loop) */
1593 if (row == wp->w_height)
1594 {
1595 didline = TRUE;
1596 break;
1597 }
1598
1599 /* stop updating when hit the end of the file */
1600 if (lnum > buf->b_ml.ml_line_count)
1601 {
1602 eof = TRUE;
1603 break;
1604 }
1605
1606 /* Remember the starting row of the line that is going to be dealt
1607 * with. It is used further down when the line doesn't fit. */
1608 srow = row;
1609
1610 /*
1611 * Update a line when it is in an area that needs updating, when it
1612 * has changes or w_lines[idx] is invalid.
1613 * bot_start may be halfway a wrapped line after using
1614 * win_del_lines(), check if the current line includes it.
1615 * When syntax folding is being used, the saved syntax states will
1616 * already have been updated, we can't see where the syntax state is
1617 * the same again, just update until the end of the window.
1618 */
1619 if (row < top_end
1620 || (row >= mid_start && row < mid_end)
1621#ifdef FEAT_SEARCH_EXTRA
1622 || top_to_mod
1623#endif
1624 || idx >= wp->w_lines_valid
1625 || (row + wp->w_lines[idx].wl_size > bot_start)
1626 || (mod_top != 0
1627 && (lnum == mod_top
1628 || (lnum >= mod_top
1629 && (lnum < mod_bot
1630#ifdef FEAT_SYN_HL
1631 || did_update == DID_FOLD
1632 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001633 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 && (
1635# ifdef FEAT_FOLDING
1636 (foldmethodIsSyntax(wp)
1637 && hasAnyFolding(wp)) ||
1638# endif
1639 syntax_check_changed(lnum)))
1640#endif
1641 )))))
1642 {
1643#ifdef FEAT_SEARCH_EXTRA
1644 if (lnum == mod_top)
1645 top_to_mod = FALSE;
1646#endif
1647
1648 /*
1649 * When at start of changed lines: May scroll following lines
1650 * up or down to minimize redrawing.
1651 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001652 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 */
1654 if (lnum == mod_top
1655 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001656 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658 int old_rows = 0;
1659 int new_rows = 0;
1660 int xtra_rows;
1661 linenr_T l;
1662
1663 /* Count the old number of window rows, using w_lines[], which
1664 * should still contain the sizes for the lines as they are
1665 * currently displayed. */
1666 for (i = idx; i < wp->w_lines_valid; ++i)
1667 {
1668 /* Only valid lines have a meaningful wl_lnum. Invalid
1669 * lines are part of the changed area. */
1670 if (wp->w_lines[i].wl_valid
1671 && wp->w_lines[i].wl_lnum == mod_bot)
1672 break;
1673 old_rows += wp->w_lines[i].wl_size;
1674#ifdef FEAT_FOLDING
1675 if (wp->w_lines[i].wl_valid
1676 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1677 {
1678 /* Must have found the last valid entry above mod_bot.
1679 * Add following invalid entries. */
1680 ++i;
1681 while (i < wp->w_lines_valid
1682 && !wp->w_lines[i].wl_valid)
1683 old_rows += wp->w_lines[i++].wl_size;
1684 break;
1685 }
1686#endif
1687 }
1688
1689 if (i >= wp->w_lines_valid)
1690 {
1691 /* We can't find a valid line below the changed lines,
1692 * need to redraw until the end of the window.
1693 * Inserting/deleting lines has no use. */
1694 bot_start = 0;
1695 }
1696 else
1697 {
1698 /* Able to count old number of rows: Count new window
1699 * rows, and may insert/delete lines */
1700 j = idx;
1701 for (l = lnum; l < mod_bot; ++l)
1702 {
1703#ifdef FEAT_FOLDING
1704 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1705 ++new_rows;
1706 else
1707#endif
1708#ifdef FEAT_DIFF
1709 if (l == wp->w_topline)
1710 new_rows += plines_win_nofill(wp, l, TRUE)
1711 + wp->w_topfill;
1712 else
1713#endif
1714 new_rows += plines_win(wp, l, TRUE);
1715 ++j;
1716 if (new_rows > wp->w_height - row - 2)
1717 {
1718 /* it's getting too much, must redraw the rest */
1719 new_rows = 9999;
1720 break;
1721 }
1722 }
1723 xtra_rows = new_rows - old_rows;
1724 if (xtra_rows < 0)
1725 {
1726 /* May scroll text up. If there is not enough
1727 * remaining text or scrolling fails, must redraw the
1728 * rest. If scrolling works, must redraw the text
1729 * below the scrolled text. */
1730 if (row - xtra_rows >= wp->w_height - 2)
1731 mod_bot = MAXLNUM;
1732 else
1733 {
1734 check_for_delay(FALSE);
1735 if (win_del_lines(wp, row,
1736 -xtra_rows, FALSE, FALSE) == FAIL)
1737 mod_bot = MAXLNUM;
1738 else
1739 bot_start = wp->w_height + xtra_rows;
1740 }
1741 }
1742 else if (xtra_rows > 0)
1743 {
1744 /* May scroll text down. If there is not enough
1745 * remaining text of scrolling fails, must redraw the
1746 * rest. */
1747 if (row + xtra_rows >= wp->w_height - 2)
1748 mod_bot = MAXLNUM;
1749 else
1750 {
1751 check_for_delay(FALSE);
1752 if (win_ins_lines(wp, row + old_rows,
1753 xtra_rows, FALSE, FALSE) == FAIL)
1754 mod_bot = MAXLNUM;
1755 else if (top_end > row + old_rows)
1756 /* Scrolled the part at the top that requires
1757 * updating down. */
1758 top_end += xtra_rows;
1759 }
1760 }
1761
1762 /* When not updating the rest, may need to move w_lines[]
1763 * entries. */
1764 if (mod_bot != MAXLNUM && i != j)
1765 {
1766 if (j < i)
1767 {
1768 int x = row + new_rows;
1769
1770 /* move entries in w_lines[] upwards */
1771 for (;;)
1772 {
1773 /* stop at last valid entry in w_lines[] */
1774 if (i >= wp->w_lines_valid)
1775 {
1776 wp->w_lines_valid = j;
1777 break;
1778 }
1779 wp->w_lines[j] = wp->w_lines[i];
1780 /* stop at a line that won't fit */
1781 if (x + (int)wp->w_lines[j].wl_size
1782 > wp->w_height)
1783 {
1784 wp->w_lines_valid = j + 1;
1785 break;
1786 }
1787 x += wp->w_lines[j++].wl_size;
1788 ++i;
1789 }
1790 if (bot_start > x)
1791 bot_start = x;
1792 }
1793 else /* j > i */
1794 {
1795 /* move entries in w_lines[] downwards */
1796 j -= i;
1797 wp->w_lines_valid += j;
1798 if (wp->w_lines_valid > wp->w_height)
1799 wp->w_lines_valid = wp->w_height;
1800 for (i = wp->w_lines_valid; i - j >= idx; --i)
1801 wp->w_lines[i] = wp->w_lines[i - j];
1802
1803 /* The w_lines[] entries for inserted lines are
1804 * now invalid, but wl_size may be used above.
1805 * Reset to zero. */
1806 while (i >= idx)
1807 {
1808 wp->w_lines[i].wl_size = 0;
1809 wp->w_lines[i--].wl_valid = FALSE;
1810 }
1811 }
1812 }
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815
1816#ifdef FEAT_FOLDING
1817 /*
1818 * When lines are folded, display one line for all of them.
1819 * Otherwise, display normally (can be several display lines when
1820 * 'wrap' is on).
1821 */
1822 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1823 if (fold_count != 0)
1824 {
1825 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1826 ++row;
1827 --fold_count;
1828 wp->w_lines[idx].wl_folded = TRUE;
1829 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1830# ifdef FEAT_SYN_HL
1831 did_update = DID_FOLD;
1832# endif
1833 }
1834 else
1835#endif
1836 if (idx < wp->w_lines_valid
1837 && wp->w_lines[idx].wl_valid
1838 && wp->w_lines[idx].wl_lnum == lnum
1839 && lnum > wp->w_topline
1840 && !(dy_flags & DY_LASTLINE)
1841 && srow + wp->w_lines[idx].wl_size > wp->w_height
1842#ifdef FEAT_DIFF
1843 && diff_check_fill(wp, lnum) == 0
1844#endif
1845 )
1846 {
1847 /* This line is not going to fit. Don't draw anything here,
1848 * will draw "@ " lines below. */
1849 row = wp->w_height + 1;
1850 }
1851 else
1852 {
1853#ifdef FEAT_SEARCH_EXTRA
1854 prepare_search_hl(wp, lnum);
1855#endif
1856#ifdef FEAT_SYN_HL
1857 /* Let the syntax stuff know we skipped a few lines. */
1858 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001859 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 syntax_end_parsing(syntax_last_parsed + 1);
1861#endif
1862
1863 /*
1864 * Display one line.
1865 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001866 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868#ifdef FEAT_FOLDING
1869 wp->w_lines[idx].wl_folded = FALSE;
1870 wp->w_lines[idx].wl_lastlnum = lnum;
1871#endif
1872#ifdef FEAT_SYN_HL
1873 did_update = DID_LINE;
1874 syntax_last_parsed = lnum;
1875#endif
1876 }
1877
1878 wp->w_lines[idx].wl_lnum = lnum;
1879 wp->w_lines[idx].wl_valid = TRUE;
1880 if (row > wp->w_height) /* past end of screen */
1881 {
1882 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001883 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1885 ++idx;
1886 break;
1887 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001888 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 wp->w_lines[idx].wl_size = row - srow;
1890 ++idx;
1891#ifdef FEAT_FOLDING
1892 lnum += fold_count + 1;
1893#else
1894 ++lnum;
1895#endif
1896 }
1897 else
1898 {
1899 /* This line does not need updating, advance to the next one */
1900 row += wp->w_lines[idx++].wl_size;
1901 if (row > wp->w_height) /* past end of screen */
1902 break;
1903#ifdef FEAT_FOLDING
1904 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1905#else
1906 ++lnum;
1907#endif
1908#ifdef FEAT_SYN_HL
1909 did_update = DID_NONE;
1910#endif
1911 }
1912
1913 if (lnum > buf->b_ml.ml_line_count)
1914 {
1915 eof = TRUE;
1916 break;
1917 }
1918 }
1919 /*
1920 * End of loop over all window lines.
1921 */
1922
1923
1924 if (idx > wp->w_lines_valid)
1925 wp->w_lines_valid = idx;
1926
1927#ifdef FEAT_SYN_HL
1928 /*
1929 * Let the syntax stuff know we stop parsing here.
1930 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001931 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 syntax_end_parsing(syntax_last_parsed + 1);
1933#endif
1934
1935 /*
1936 * If we didn't hit the end of the file, and we didn't finish the last
1937 * line we were working on, then the line didn't fit.
1938 */
1939 wp->w_empty_rows = 0;
1940#ifdef FEAT_DIFF
1941 wp->w_filler_rows = 0;
1942#endif
1943 if (!eof && !didline)
1944 {
1945 if (lnum == wp->w_topline)
1946 {
1947 /*
1948 * Single line that does not fit!
1949 * Don't overwrite it, it can be edited.
1950 */
1951 wp->w_botline = lnum + 1;
1952 }
1953#ifdef FEAT_DIFF
1954 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1955 {
1956 /* Window ends in filler lines. */
1957 wp->w_botline = lnum;
1958 wp->w_filler_rows = wp->w_height - srow;
1959 }
1960#endif
1961 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1962 {
1963 /*
1964 * Last line isn't finished: Display "@@@" at the end.
1965 */
1966 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1967 W_WINROW(wp) + wp->w_height,
1968 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1969 '@', '@', hl_attr(HLF_AT));
1970 set_empty_rows(wp, srow);
1971 wp->w_botline = lnum;
1972 }
1973 else
1974 {
1975 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1976 wp->w_botline = lnum;
1977 }
1978 }
1979 else
1980 {
1981#ifdef FEAT_VERTSPLIT
1982 draw_vsep_win(wp, row);
1983#endif
1984 if (eof) /* we hit the end of the file */
1985 {
1986 wp->w_botline = buf->b_ml.ml_line_count + 1;
1987#ifdef FEAT_DIFF
1988 j = diff_check_fill(wp, wp->w_botline);
1989 if (j > 0 && !wp->w_botfill)
1990 {
1991 /*
1992 * Display filler lines at the end of the file
1993 */
1994 if (char2cells(fill_diff) > 1)
1995 i = '-';
1996 else
1997 i = fill_diff;
1998 if (row + j > wp->w_height)
1999 j = wp->w_height - row;
2000 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2001 row += j;
2002 }
2003#endif
2004 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002005 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 wp->w_botline = lnum;
2007
2008 /* make sure the rest of the screen is blank */
2009 /* put '~'s on rows that aren't part of the file. */
2010 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2011 }
2012
2013 /* Reset the type of redrawing required, the window has been updated. */
2014 wp->w_redr_type = 0;
2015#ifdef FEAT_DIFF
2016 wp->w_old_topfill = wp->w_topfill;
2017 wp->w_old_botfill = wp->w_botfill;
2018#endif
2019
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002020 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 /*
2023 * There is a trick with w_botline. If we invalidate it on each
2024 * change that might modify it, this will cause a lot of expensive
2025 * calls to plines() in update_topline() each time. Therefore the
2026 * value of w_botline is often approximated, and this value is used to
2027 * compute the value of w_topline. If the value of w_botline was
2028 * wrong, check that the value of w_topline is correct (cursor is on
2029 * the visible part of the text). If it's not, we need to redraw
2030 * again. Mostly this just means scrolling up a few lines, so it
2031 * doesn't look too bad. Only do this for the current window (where
2032 * changes are relevant).
2033 */
2034 wp->w_valid |= VALID_BOTLINE;
2035 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2036 {
2037 recursive = TRUE;
2038 curwin->w_valid &= ~VALID_TOPLINE;
2039 update_topline(); /* may invalidate w_botline again */
2040 if (must_redraw != 0)
2041 {
2042 /* Don't update for changes in buffer again. */
2043 i = curbuf->b_mod_set;
2044 curbuf->b_mod_set = FALSE;
2045 win_update(curwin);
2046 must_redraw = 0;
2047 curbuf->b_mod_set = i;
2048 }
2049 recursive = FALSE;
2050 }
2051 }
2052
2053#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2054 /* restore got_int, unless CTRL-C was hit while redrawing */
2055 if (!got_int)
2056 got_int = save_got_int;
2057#endif
2058}
2059
2060#ifdef FEAT_SIGNS
2061static int draw_signcolumn __ARGS((win_T *wp));
2062
2063/*
2064 * Return TRUE when window "wp" has a column to draw signs in.
2065 */
2066 static int
2067draw_signcolumn(wp)
2068 win_T *wp;
2069{
2070 return (wp->w_buffer->b_signlist != NULL
2071# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002072 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073# endif
2074 );
2075}
2076#endif
2077
2078/*
2079 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2080 * as the filler character.
2081 */
2082 static void
2083win_draw_end(wp, c1, c2, row, endrow, hl)
2084 win_T *wp;
2085 int c1;
2086 int c2;
2087 int row;
2088 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002089 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
2091#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2092 int n = 0;
2093# define FDC_OFF n
2094#else
2095# define FDC_OFF 0
2096#endif
2097
2098#ifdef FEAT_RIGHTLEFT
2099 if (wp->w_p_rl)
2100 {
2101 /* No check for cmdline window: should never be right-left. */
2102# ifdef FEAT_FOLDING
2103 n = wp->w_p_fdc;
2104
2105 if (n > 0)
2106 {
2107 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002108 if (n > W_WIDTH(wp))
2109 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2111 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2112 ' ', ' ', hl_attr(HLF_FC));
2113 }
2114# endif
2115# ifdef FEAT_SIGNS
2116 if (draw_signcolumn(wp))
2117 {
2118 int nn = n + 2;
2119
2120 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002121 if (nn > W_WIDTH(wp))
2122 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2124 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2125 ' ', ' ', hl_attr(HLF_SC));
2126 n = nn;
2127 }
2128# endif
2129 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2130 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2131 c2, c2, hl_attr(hl));
2132 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2133 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2134 c1, c2, hl_attr(hl));
2135 }
2136 else
2137#endif
2138 {
2139#ifdef FEAT_CMDWIN
2140 if (cmdwin_type != 0 && wp == curwin)
2141 {
2142 /* draw the cmdline character in the leftmost column */
2143 n = 1;
2144 if (n > wp->w_width)
2145 n = wp->w_width;
2146 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2147 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2148 cmdwin_type, ' ', hl_attr(HLF_AT));
2149 }
2150#endif
2151#ifdef FEAT_FOLDING
2152 if (wp->w_p_fdc > 0)
2153 {
2154 int nn = n + wp->w_p_fdc;
2155
2156 /* draw the fold column at the left */
2157 if (nn > W_WIDTH(wp))
2158 nn = W_WIDTH(wp);
2159 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2160 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2161 ' ', ' ', hl_attr(HLF_FC));
2162 n = nn;
2163 }
2164#endif
2165#ifdef FEAT_SIGNS
2166 if (draw_signcolumn(wp))
2167 {
2168 int nn = n + 2;
2169
2170 /* draw the sign column after the fold column */
2171 if (nn > W_WIDTH(wp))
2172 nn = W_WIDTH(wp);
2173 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2174 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2175 ' ', ' ', hl_attr(HLF_SC));
2176 n = nn;
2177 }
2178#endif
2179 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2180 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2181 c1, c2, hl_attr(hl));
2182 }
2183 set_empty_rows(wp, row);
2184}
2185
Bram Moolenaar1a384422010-07-14 19:53:30 +02002186#ifdef FEAT_SYN_HL
2187static int advance_color_col __ARGS((int vcol, int **color_cols));
2188
2189/*
2190 * Advance **color_cols and return TRUE when there are columns to draw.
2191 */
2192 static int
2193advance_color_col(vcol, color_cols)
2194 int vcol;
2195 int **color_cols;
2196{
2197 while (**color_cols >= 0 && vcol > **color_cols)
2198 ++*color_cols;
2199 return (**color_cols >= 0);
2200}
2201#endif
2202
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203#ifdef FEAT_FOLDING
2204/*
2205 * Display one folded line.
2206 */
2207 static void
2208fold_line(wp, fold_count, foldinfo, lnum, row)
2209 win_T *wp;
2210 long fold_count;
2211 foldinfo_T *foldinfo;
2212 linenr_T lnum;
2213 int row;
2214{
2215 char_u buf[51];
2216 pos_T *top, *bot;
2217 linenr_T lnume = lnum + fold_count - 1;
2218 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002219 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 int col;
2222 int txtcol;
2223 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002224 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 /* Build the fold line:
2227 * 1. Add the cmdwin_type for the command-line window
2228 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002229 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 * 4. Compose the text
2231 * 5. Add the text
2232 * 6. set highlighting for the Visual area an other text
2233 */
2234 col = 0;
2235
2236 /*
2237 * 1. Add the cmdwin_type for the command-line window
2238 * Ignores 'rightleft', this window is never right-left.
2239 */
2240#ifdef FEAT_CMDWIN
2241 if (cmdwin_type != 0 && wp == curwin)
2242 {
2243 ScreenLines[off] = cmdwin_type;
2244 ScreenAttrs[off] = hl_attr(HLF_AT);
2245#ifdef FEAT_MBYTE
2246 if (enc_utf8)
2247 ScreenLinesUC[off] = 0;
2248#endif
2249 ++col;
2250 }
2251#endif
2252
2253 /*
2254 * 2. Add the 'foldcolumn'
2255 */
2256 fdc = wp->w_p_fdc;
2257 if (fdc > W_WIDTH(wp) - col)
2258 fdc = W_WIDTH(wp) - col;
2259 if (fdc > 0)
2260 {
2261 fill_foldcolumn(buf, wp, TRUE, lnum);
2262#ifdef FEAT_RIGHTLEFT
2263 if (wp->w_p_rl)
2264 {
2265 int i;
2266
2267 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2268 hl_attr(HLF_FC));
2269 /* reverse the fold column */
2270 for (i = 0; i < fdc; ++i)
2271 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2272 }
2273 else
2274#endif
2275 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2276 col += fdc;
2277 }
2278
2279#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002280# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2281 for (ri = 0; ri < l; ++ri) \
2282 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2283 else \
2284 for (ri = 0; ri < l; ++ri) \
2285 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002287# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2288 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#endif
2290
Bram Moolenaar64486672010-05-16 15:46:46 +02002291 /* Set all attributes of the 'number' or 'relativenumber' column and the
2292 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002293 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
2295#ifdef FEAT_SIGNS
2296 /* If signs are being displayed, add two spaces. */
2297 if (draw_signcolumn(wp))
2298 {
2299 len = W_WIDTH(wp) - col;
2300 if (len > 0)
2301 {
2302 if (len > 2)
2303 len = 2;
2304# ifdef FEAT_RIGHTLEFT
2305 if (wp->w_p_rl)
2306 /* the line number isn't reversed */
2307 copy_text_attr(off + W_WIDTH(wp) - len - col,
2308 (char_u *)" ", len, hl_attr(HLF_FL));
2309 else
2310# endif
2311 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2312 col += len;
2313 }
2314 }
2315#endif
2316
2317 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002318 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002320 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 len = W_WIDTH(wp) - col;
2323 if (len > 0)
2324 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002325 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002326 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01002327 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002328
2329 if (len > w + 1)
2330 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002331
2332 if (wp->w_p_nu)
2333 /* 'number' */
2334 num = (long)lnum;
2335 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002336 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002337 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002338 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar700e7342013-01-30 12:31:36 +01002339 if (num == 0)
2340 {
2341 num = lnum;
2342 fmt = "%-*ld ";
2343 }
2344 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002345
Bram Moolenaar700e7342013-01-30 12:31:36 +01002346 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#ifdef FEAT_RIGHTLEFT
2348 if (wp->w_p_rl)
2349 /* the line number isn't reversed */
2350 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2351 hl_attr(HLF_FL));
2352 else
2353#endif
2354 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2355 col += len;
2356 }
2357 }
2358
2359 /*
2360 * 4. Compose the folded-line string with 'foldtext', if set.
2361 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002362 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364 txtcol = col; /* remember where text starts */
2365
2366 /*
2367 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2368 * Right-left text is put in columns 0 - number-col, normal text is put
2369 * in columns number-col - window-width.
2370 */
2371#ifdef FEAT_MBYTE
2372 if (has_mbyte)
2373 {
2374 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002375 int u8c, u8cc[MAX_MCO];
2376 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 int idx;
2378 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002379 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380# ifdef FEAT_ARABIC
2381 int prev_c = 0; /* previous Arabic character */
2382 int prev_c1 = 0; /* first composing char for prev_c */
2383# endif
2384
2385# ifdef FEAT_RIGHTLEFT
2386 if (wp->w_p_rl)
2387 idx = off;
2388 else
2389# endif
2390 idx = off + col;
2391
2392 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2393 for (p = text; *p != NUL; )
2394 {
2395 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002396 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (col + cells > W_WIDTH(wp)
2398# ifdef FEAT_RIGHTLEFT
2399 - (wp->w_p_rl ? col : 0)
2400# endif
2401 )
2402 break;
2403 ScreenLines[idx] = *p;
2404 if (enc_utf8)
2405 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002406 u8c = utfc_ptr2char(p, u8cc);
2407 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
2409 ScreenLinesUC[idx] = 0;
2410#ifdef FEAT_ARABIC
2411 prev_c = u8c;
2412#endif
2413 }
2414 else
2415 {
2416#ifdef FEAT_ARABIC
2417 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2418 {
2419 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002420 int pc, pc1, nc;
2421 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 int firstbyte = *p;
2423
2424 /* The idea of what is the previous and next
2425 * character depends on 'rightleft'. */
2426 if (wp->w_p_rl)
2427 {
2428 pc = prev_c;
2429 pc1 = prev_c1;
2430 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002431 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433 else
2434 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002435 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002437 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 prev_c = u8c;
2440
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002441 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 pc, pc1, nc);
2443 ScreenLines[idx] = firstbyte;
2444 }
2445 else
2446 prev_c = u8c;
2447#endif
2448 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002449#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 if (u8c >= 0x10000)
2451 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2452 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002455 for (i = 0; i < Screen_mco; ++i)
2456 {
2457 ScreenLinesC[i][idx] = u8cc[i];
2458 if (u8cc[i] == 0)
2459 break;
2460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462 if (cells > 1)
2463 ScreenLines[idx + 1] = 0;
2464 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002465 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2466 /* double-byte single width character */
2467 ScreenLines2[idx] = p[1];
2468 else if (cells > 1)
2469 /* double-width character */
2470 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 col += cells;
2472 idx += cells;
2473 p += c_len;
2474 }
2475 }
2476 else
2477#endif
2478 {
2479 len = (int)STRLEN(text);
2480 if (len > W_WIDTH(wp) - col)
2481 len = W_WIDTH(wp) - col;
2482 if (len > 0)
2483 {
2484#ifdef FEAT_RIGHTLEFT
2485 if (wp->w_p_rl)
2486 STRNCPY(current_ScreenLine, text, len);
2487 else
2488#endif
2489 STRNCPY(current_ScreenLine + col, text, len);
2490 col += len;
2491 }
2492 }
2493
2494 /* Fill the rest of the line with the fold filler */
2495#ifdef FEAT_RIGHTLEFT
2496 if (wp->w_p_rl)
2497 col -= txtcol;
2498#endif
2499 while (col < W_WIDTH(wp)
2500#ifdef FEAT_RIGHTLEFT
2501 - (wp->w_p_rl ? txtcol : 0)
2502#endif
2503 )
2504 {
2505#ifdef FEAT_MBYTE
2506 if (enc_utf8)
2507 {
2508 if (fill_fold >= 0x80)
2509 {
2510 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002511 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
2513 else
2514 ScreenLinesUC[off + col] = 0;
2515 }
2516#endif
2517 ScreenLines[off + col++] = fill_fold;
2518 }
2519
2520 if (text != buf)
2521 vim_free(text);
2522
2523 /*
2524 * 6. set highlighting for the Visual area an other text.
2525 * If all folded lines are in the Visual area, highlight the line.
2526 */
2527#ifdef FEAT_VISUAL
2528 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2529 {
2530 if (ltoreq(curwin->w_cursor, VIsual))
2531 {
2532 /* Visual is after curwin->w_cursor */
2533 top = &curwin->w_cursor;
2534 bot = &VIsual;
2535 }
2536 else
2537 {
2538 /* Visual is before curwin->w_cursor */
2539 top = &VIsual;
2540 bot = &curwin->w_cursor;
2541 }
2542 if (lnum >= top->lnum
2543 && lnume <= bot->lnum
2544 && (VIsual_mode != 'v'
2545 || ((lnum > top->lnum
2546 || (lnum == top->lnum
2547 && top->col == 0))
2548 && (lnume < bot->lnum
2549 || (lnume == bot->lnum
2550 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002551 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
2553 if (VIsual_mode == Ctrl_V)
2554 {
2555 /* Visual block mode: highlight the chars part of the block */
2556 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2557 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002558 if (wp->w_old_cursor_lcol != MAXCOL
2559 && wp->w_old_cursor_lcol + txtcol
2560 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 len = wp->w_old_cursor_lcol;
2562 else
2563 len = W_WIDTH(wp) - txtcol;
2564 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002565 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567 }
2568 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002571 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574 }
2575#endif
2576
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002577#ifdef FEAT_SYN_HL
2578 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002579 if (wp->w_p_cuc)
2580 {
2581 txtcol += wp->w_virtcol;
2582 if (wp->w_p_wrap)
2583 txtcol -= wp->w_skipcol;
2584 else
2585 txtcol -= wp->w_leftcol;
2586 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2587 ScreenAttrs[off + txtcol] = hl_combine_attr(
2588 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2589 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2593 (int)W_WIDTH(wp), FALSE);
2594
2595 /*
2596 * Update w_cline_height and w_cline_folded if the cursor line was
2597 * updated (saves a call to plines() later).
2598 */
2599 if (wp == curwin
2600 && lnum <= curwin->w_cursor.lnum
2601 && lnume >= curwin->w_cursor.lnum)
2602 {
2603 curwin->w_cline_row = row;
2604 curwin->w_cline_height = 1;
2605 curwin->w_cline_folded = TRUE;
2606 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2607 }
2608}
2609
2610/*
2611 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2612 */
2613 static void
2614copy_text_attr(off, buf, len, attr)
2615 int off;
2616 char_u *buf;
2617 int len;
2618 int attr;
2619{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002620 int i;
2621
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 mch_memmove(ScreenLines + off, buf, (size_t)len);
2623# ifdef FEAT_MBYTE
2624 if (enc_utf8)
2625 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2626# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002627 for (i = 0; i < len; ++i)
2628 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629}
2630
2631/*
2632 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002633 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 */
2635 static void
2636fill_foldcolumn(p, wp, closed, lnum)
2637 char_u *p;
2638 win_T *wp;
2639 int closed; /* TRUE of FALSE */
2640 linenr_T lnum; /* current line number */
2641{
2642 int i = 0;
2643 int level;
2644 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002645 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
2647 /* Init to all spaces. */
2648 copy_spaces(p, (size_t)wp->w_p_fdc);
2649
2650 level = win_foldinfo.fi_level;
2651 if (level > 0)
2652 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002653 /* If there is only one column put more info in it. */
2654 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 /* If the column is too narrow, we start at the lowest level that
2657 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002658 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (first_level < 1)
2660 first_level = 1;
2661
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002662 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
2664 if (win_foldinfo.fi_lnum == lnum
2665 && first_level + i >= win_foldinfo.fi_low_level)
2666 p[i] = '-';
2667 else if (first_level == 1)
2668 p[i] = '|';
2669 else if (first_level + i <= 9)
2670 p[i] = '0' + first_level + i;
2671 else
2672 p[i] = '>';
2673 if (first_level + i == level)
2674 break;
2675 }
2676 }
2677 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002678 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679}
2680#endif /* FEAT_FOLDING */
2681
2682/*
2683 * Display line "lnum" of window 'wp' on the screen.
2684 * Start at row "startrow", stop when "endrow" is reached.
2685 * wp->w_virtcol needs to be valid.
2686 *
2687 * Return the number of last row the line occupies.
2688 */
2689 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002690win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 win_T *wp;
2692 linenr_T lnum;
2693 int startrow;
2694 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002695 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
2697 int col; /* visual column on screen */
2698 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2699 int c = 0; /* init for GCC */
2700 long vcol = 0; /* virtual column (for tabs) */
2701 long vcol_prev = -1; /* "vcol" of previous character */
2702 char_u *line; /* current line */
2703 char_u *ptr; /* current position in "line" */
2704 int row; /* row in the window, excl w_winrow */
2705 int screen_row; /* row on the screen, incl w_winrow */
2706
2707 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2708 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002709 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 int c_extra = NUL; /* extra chars, all the same */
2711 int extra_attr = 0; /* attributes when n_extra != 0 */
2712 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2713 displaying lcs_eol at end-of-line */
2714 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2715 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2716
2717 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2718 int saved_n_extra = 0;
2719 char_u *saved_p_extra = NULL;
2720 int saved_c_extra = 0;
2721 int saved_char_attr = 0;
2722
2723 int n_attr = 0; /* chars with special attr */
2724 int saved_attr2 = 0; /* char_attr saved for n_attr */
2725 int n_attr3 = 0; /* chars with overruling special attr */
2726 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2727
2728 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2729
2730 int fromcol, tocol; /* start/end of inverting */
2731 int fromcol_prev = -2; /* start of inverting after cursor */
2732 int noinvcur = FALSE; /* don't invert the cursor */
2733#ifdef FEAT_VISUAL
2734 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002735 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736#endif
2737 pos_T pos;
2738 long v;
2739
2740 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002741 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2743 in this line */
2744 int attr = 0; /* attributes for area highlighting */
2745 int area_attr = 0; /* attributes desired by highlighting */
2746 int search_attr = 0; /* attributes desired by 'hlsearch' */
2747#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002748 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 int syntax_attr = 0; /* attributes desired by syntax */
2750 int has_syntax = FALSE; /* this buffer has syntax highl. */
2751 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002752 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002753 int draw_color_col = FALSE; /* highlight colorcolumn */
2754 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002755#endif
2756#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002757 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002758# define SPWORDLEN 150
2759 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002760 int nextlinecol = 0; /* column where nextline[] starts */
2761 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002762 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002763 int spell_attr = 0; /* attributes desired by spelling */
2764 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002765 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2766 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002767 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002768 static int cap_col = -1; /* column to check for Cap word */
2769 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002770 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771#endif
2772 int extra_check; /* has syntax or linebreak */
2773#ifdef FEAT_MBYTE
2774 int multi_attr = 0; /* attributes desired by multibyte */
2775 int mb_l = 1; /* multi-byte byte length */
2776 int mb_c = 0; /* decoded multi-byte character */
2777 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002778 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779#endif
2780#ifdef FEAT_DIFF
2781 int filler_lines; /* nr of filler lines to be drawn */
2782 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002783 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 int change_start = MAXCOL; /* first col of changed area */
2785 int change_end = -1; /* last col of changed area */
2786#endif
2787 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2788#ifdef FEAT_LINEBREAK
2789 int need_showbreak = FALSE;
2790#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002791#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2792 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002794 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795#endif
2796#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002797 matchitem_T *cur; /* points to the match list */
2798 match_T *shl; /* points to search_hl or a match */
2799 int shl_flag; /* flag to indicate whether search_hl
2800 has been processed or not */
2801 int prevcol_hl_flag; /* flag to indicate whether prevcol
2802 equals startcol of search_hl or one
2803 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804#endif
2805#ifdef FEAT_ARABIC
2806 int prev_c = 0; /* previous Arabic character */
2807 int prev_c1 = 0; /* first composing char for prev_c */
2808#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002809#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002810 int did_line_attr = 0;
2811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 /* draw_state: items that are drawn in sequence: */
2814#define WL_START 0 /* nothing done yet */
2815#ifdef FEAT_CMDWIN
2816# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2817#else
2818# define WL_CMDLINE WL_START
2819#endif
2820#ifdef FEAT_FOLDING
2821# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2822#else
2823# define WL_FOLD WL_CMDLINE
2824#endif
2825#ifdef FEAT_SIGNS
2826# define WL_SIGN WL_FOLD + 1 /* column for signs */
2827#else
2828# define WL_SIGN WL_FOLD /* column for signs */
2829#endif
2830#define WL_NR WL_SIGN + 1 /* line number */
2831#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2832# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2833#else
2834# define WL_SBR WL_NR
2835#endif
2836#define WL_LINE WL_SBR + 1 /* text in the line */
2837 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002838#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 int feedback_col = 0;
2840 int feedback_old_attr = -1;
2841#endif
2842
Bram Moolenaar860cae12010-06-05 23:22:07 +02002843#ifdef FEAT_CONCEAL
2844 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002845 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002846 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002847 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002848 int is_concealing = FALSE;
2849 int boguscols = 0; /* nonexistent columns added to force
2850 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002851 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002852 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002853# define VCOL_HLC (vcol - vcol_off)
2854#else
2855# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
2858 if (startrow > endrow) /* past the end already! */
2859 return startrow;
2860
2861 row = startrow;
2862 screen_row = row + W_WINROW(wp);
2863
2864 /*
2865 * To speed up the loop below, set extra_check when there is linebreak,
2866 * trailing white space and/or syntax processing to be done.
2867 */
2868#ifdef FEAT_LINEBREAK
2869 extra_check = wp->w_p_lbr;
2870#else
2871 extra_check = 0;
2872#endif
2873#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002874 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
2876 /* Prepare for syntax highlighting in this line. When there is an
2877 * error, stop syntax highlighting. */
2878 save_did_emsg = did_emsg;
2879 did_emsg = FALSE;
2880 syntax_start(wp, lnum);
2881 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002882 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 else
2884 {
2885 did_emsg = save_did_emsg;
2886 has_syntax = TRUE;
2887 extra_check = TRUE;
2888 }
2889 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002890
2891 /* Check for columns to display for 'colorcolumn'. */
2892 color_cols = wp->w_p_cc_cols;
2893 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002894 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002895#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002896
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002897#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002898 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002899 && *wp->w_s->b_p_spl != NUL
2900 && wp->w_s->b_langp.ga_len > 0
2901 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002902 {
2903 /* Prepare for spell checking. */
2904 has_spell = TRUE;
2905 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002906
2907 /* Get the start of the next line, so that words that wrap to the next
2908 * line are found too: "et<line-break>al.".
2909 * Trick: skip a few chars for C/shell/Vim comments */
2910 nextline[SPWORDLEN] = NUL;
2911 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2912 {
2913 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2914 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2915 }
2916
2917 /* When a word wrapped from the previous line the start of the current
2918 * line is valid. */
2919 if (lnum == checked_lnum)
2920 cur_checked_col = checked_col;
2921 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002922
2923 /* When there was a sentence end in the previous line may require a
2924 * word starting with capital in this line. In line 1 always check
2925 * the first word. */
2926 if (lnum != capcol_lnum)
2927 cap_col = -1;
2928 if (lnum == 1)
2929 cap_col = 0;
2930 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932#endif
2933
2934 /*
2935 * handle visual active in this window
2936 */
2937 fromcol = -10;
2938 tocol = MAXCOL;
2939#ifdef FEAT_VISUAL
2940 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2941 {
2942 /* Visual is after curwin->w_cursor */
2943 if (ltoreq(curwin->w_cursor, VIsual))
2944 {
2945 top = &curwin->w_cursor;
2946 bot = &VIsual;
2947 }
2948 else /* Visual is before curwin->w_cursor */
2949 {
2950 top = &VIsual;
2951 bot = &curwin->w_cursor;
2952 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002953 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 if (VIsual_mode == Ctrl_V) /* block mode */
2955 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002956 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
2958 fromcol = wp->w_old_cursor_fcol;
2959 tocol = wp->w_old_cursor_lcol;
2960 }
2961 }
2962 else /* non-block mode */
2963 {
2964 if (lnum > top->lnum && lnum <= bot->lnum)
2965 fromcol = 0;
2966 else if (lnum == top->lnum)
2967 {
2968 if (VIsual_mode == 'V') /* linewise */
2969 fromcol = 0;
2970 else
2971 {
2972 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2973 if (gchar_pos(top) == NUL)
2974 tocol = fromcol + 1;
2975 }
2976 }
2977 if (VIsual_mode != 'V' && lnum == bot->lnum)
2978 {
2979 if (*p_sel == 'e' && bot->col == 0
2980#ifdef FEAT_VIRTUALEDIT
2981 && bot->coladd == 0
2982#endif
2983 )
2984 {
2985 fromcol = -10;
2986 tocol = MAXCOL;
2987 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002988 else if (bot->col == MAXCOL)
2989 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 else
2991 {
2992 pos = *bot;
2993 if (*p_sel == 'e')
2994 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2995 else
2996 {
2997 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2998 ++tocol;
2999 }
3000 }
3001 }
3002 }
3003
3004#ifndef MSDOS
3005 /* Check if the character under the cursor should not be inverted */
3006 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3007# ifdef FEAT_GUI
3008 && !gui.in_use
3009# endif
3010 )
3011 noinvcur = TRUE;
3012#endif
3013
3014 /* if inverting in this line set area_highlighting */
3015 if (fromcol >= 0)
3016 {
3017 area_highlighting = TRUE;
3018 attr = hl_attr(HLF_V);
3019#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003020 if ((clip_star.available && !clip_star.owned
3021 && clip_isautosel_star())
3022 || (clip_plus.available && !clip_plus.owned
3023 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 attr = hl_attr(HLF_VNC);
3025#endif
3026 }
3027 }
3028
3029 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003030 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 */
3032 else
3033#endif /* FEAT_VISUAL */
3034 if (highlight_match
3035 && wp == curwin
3036 && lnum >= curwin->w_cursor.lnum
3037 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3038 {
3039 if (lnum == curwin->w_cursor.lnum)
3040 getvcol(curwin, &(curwin->w_cursor),
3041 (colnr_T *)&fromcol, NULL, NULL);
3042 else
3043 fromcol = 0;
3044 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3045 {
3046 pos.lnum = lnum;
3047 pos.col = search_match_endcol;
3048 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3049 }
3050 else
3051 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003052 /* do at least one character; happens when past end of line */
3053 if (fromcol == tocol)
3054 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 area_highlighting = TRUE;
3056 attr = hl_attr(HLF_I);
3057 }
3058
3059#ifdef FEAT_DIFF
3060 filler_lines = diff_check(wp, lnum);
3061 if (filler_lines < 0)
3062 {
3063 if (filler_lines == -1)
3064 {
3065 if (diff_find_change(wp, lnum, &change_start, &change_end))
3066 diff_hlf = HLF_ADD; /* added line */
3067 else if (change_start == 0)
3068 diff_hlf = HLF_TXD; /* changed text */
3069 else
3070 diff_hlf = HLF_CHD; /* changed line */
3071 }
3072 else
3073 diff_hlf = HLF_ADD; /* added line */
3074 filler_lines = 0;
3075 area_highlighting = TRUE;
3076 }
3077 if (lnum == wp->w_topline)
3078 filler_lines = wp->w_topfill;
3079 filler_todo = filler_lines;
3080#endif
3081
3082#ifdef LINE_ATTR
3083# ifdef FEAT_SIGNS
3084 /* If this line has a sign with line highlighting set line_attr. */
3085 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3086 if (v != 0)
3087 line_attr = sign_get_attr((int)v, TRUE);
3088# endif
3089# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3090 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003091 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 line_attr = hl_attr(HLF_L);
3093# endif
3094 if (line_attr != 0)
3095 area_highlighting = TRUE;
3096#endif
3097
3098 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3099 ptr = line;
3100
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003101#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003102 if (has_spell)
3103 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003104 /* For checking first word with a capital skip white space. */
3105 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003106 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003107
Bram Moolenaar30abd282005-06-22 22:35:10 +00003108 /* To be able to spell-check over line boundaries copy the end of the
3109 * current line into nextline[]. Above the start of the next line was
3110 * copied to nextline[SPWORDLEN]. */
3111 if (nextline[SPWORDLEN] == NUL)
3112 {
3113 /* No next line or it is empty. */
3114 nextlinecol = MAXCOL;
3115 nextline_idx = 0;
3116 }
3117 else
3118 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003119 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003120 if (v < SPWORDLEN)
3121 {
3122 /* Short line, use it completely and append the start of the
3123 * next line. */
3124 nextlinecol = 0;
3125 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003126 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003127 nextline_idx = v + 1;
3128 }
3129 else
3130 {
3131 /* Long line, use only the last SPWORDLEN bytes. */
3132 nextlinecol = v - SPWORDLEN;
3133 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3134 nextline_idx = SPWORDLEN + 1;
3135 }
3136 }
3137 }
3138#endif
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 /* find start of trailing whitespace */
3141 if (wp->w_p_list && lcs_trail)
3142 {
3143 trailcol = (colnr_T)STRLEN(ptr);
3144 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3145 --trailcol;
3146 trailcol += (colnr_T) (ptr - line);
3147 extra_check = TRUE;
3148 }
3149
3150 /*
3151 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3152 * first character to be displayed.
3153 */
3154 if (wp->w_p_wrap)
3155 v = wp->w_skipcol;
3156 else
3157 v = wp->w_leftcol;
3158 if (v > 0)
3159 {
3160#ifdef FEAT_MBYTE
3161 char_u *prev_ptr = ptr;
3162#endif
3163 while (vcol < v && *ptr != NUL)
3164 {
3165 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3166 vcol += c;
3167#ifdef FEAT_MBYTE
3168 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003170 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003173#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3174 /* When:
3175 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003176 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003177 * - 'virtualedit' is set, or
3178 * - the visual mode is active,
3179 * the end of the line may be before the start of the displayed part.
3180 */
3181 if (vcol < v && (
3182# ifdef FEAT_SYN_HL
3183 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003184 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003185# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3186 ||
3187# endif
3188# endif
3189# ifdef FEAT_VIRTUALEDIT
3190 virtual_active()
3191# ifdef FEAT_VISUAL
3192 ||
3193# endif
3194# endif
3195# ifdef FEAT_VISUAL
3196 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3197# endif
3198 ))
3199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#endif
3203
3204 /* Handle a character that's not completely on the screen: Put ptr at
3205 * that character but skip the first few screen characters. */
3206 if (vcol > v)
3207 {
3208 vcol -= c;
3209#ifdef FEAT_MBYTE
3210 ptr = prev_ptr;
3211#else
3212 --ptr;
3213#endif
3214 n_skip = v - vcol;
3215 }
3216
3217 /*
3218 * Adjust for when the inverted text is before the screen,
3219 * and when the start of the inverted text is before the screen.
3220 */
3221 if (tocol <= vcol)
3222 fromcol = 0;
3223 else if (fromcol >= 0 && fromcol < vcol)
3224 fromcol = vcol;
3225
3226#ifdef FEAT_LINEBREAK
3227 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3228 if (wp->w_p_wrap)
3229 need_showbreak = TRUE;
3230#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003231#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003232 /* When spell checking a word we need to figure out the start of the
3233 * word and if it's badly spelled or not. */
3234 if (has_spell)
3235 {
3236 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003237 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003238 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003239
3240 pos = wp->w_cursor;
3241 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003242 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003243 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003244
3245 /* spell_move_to() may call ml_get() and make "line" invalid */
3246 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3247 ptr = line + linecol;
3248
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003249 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003250 {
3251 /* no bad word found at line start, don't check until end of a
3252 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003253 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003254 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003255 }
3256 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003257 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003258 /* bad word found, use attributes until end of word */
3259 word_end = wp->w_cursor.col + len + 1;
3260
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003261 /* Turn index into actual attributes. */
3262 if (spell_hlf != HLF_COUNT)
3263 spell_attr = highlight_attr[spell_hlf];
3264 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003265 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003266
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003267# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003268 /* Need to restart syntax highlighting for this line. */
3269 if (has_syntax)
3270 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003271# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003272 }
3273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275
3276 /*
3277 * Correct highlighting for cursor that can't be disabled.
3278 * Avoids having to check this for each character.
3279 */
3280 if (fromcol >= 0)
3281 {
3282 if (noinvcur)
3283 {
3284 if ((colnr_T)fromcol == wp->w_virtcol)
3285 {
3286 /* highlighting starts at cursor, let it start just after the
3287 * cursor */
3288 fromcol_prev = fromcol;
3289 fromcol = -1;
3290 }
3291 else if ((colnr_T)fromcol < wp->w_virtcol)
3292 /* restart highlighting after the cursor */
3293 fromcol_prev = wp->w_virtcol;
3294 }
3295 if (fromcol >= tocol)
3296 fromcol = -1;
3297 }
3298
3299#ifdef FEAT_SEARCH_EXTRA
3300 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003301 * Handle highlighting the last used search pattern and matches.
3302 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003304 cur = wp->w_match_head;
3305 shl_flag = FALSE;
3306 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003308 if (shl_flag == FALSE)
3309 {
3310 shl = &search_hl;
3311 shl_flag = TRUE;
3312 }
3313 else
3314 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003315 shl->startcol = MAXCOL;
3316 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 shl->attr_cur = 0;
3318 if (shl->rm.regprog != NULL)
3319 {
3320 v = (long)(ptr - line);
3321 next_search_hl(wp, shl, lnum, (colnr_T)v);
3322
3323 /* Need to get the line again, a multi-line regexp may have made it
3324 * invalid. */
3325 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3326 ptr = line + v;
3327
3328 if (shl->lnum != 0 && shl->lnum <= lnum)
3329 {
3330 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003331 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003333 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3335 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003336 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003338 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003340 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
3342#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003343 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003344 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 else
3346#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003347 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003349 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
3351 shl->attr_cur = shl->attr;
3352 search_attr = shl->attr;
3353 }
3354 area_highlighting = TRUE;
3355 }
3356 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003357 if (shl != &search_hl && cur != NULL)
3358 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360#endif
3361
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003362#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003363 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3364 * active, because it's not clear what is selected then. */
3365 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003366 {
3367 line_attr = hl_attr(HLF_CUL);
3368 area_highlighting = TRUE;
3369 }
3370#endif
3371
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003372 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 col = 0;
3374#ifdef FEAT_RIGHTLEFT
3375 if (wp->w_p_rl)
3376 {
3377 /* Rightleft window: process the text in the normal direction, but put
3378 * it in current_ScreenLine[] from right to left. Start at the
3379 * rightmost column of the window. */
3380 col = W_WIDTH(wp) - 1;
3381 off += col;
3382 }
3383#endif
3384
3385 /*
3386 * Repeat for the whole displayed line.
3387 */
3388 for (;;)
3389 {
3390 /* Skip this quickly when working on the text. */
3391 if (draw_state != WL_LINE)
3392 {
3393#ifdef FEAT_CMDWIN
3394 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3395 {
3396 draw_state = WL_CMDLINE;
3397 if (cmdwin_type != 0 && wp == curwin)
3398 {
3399 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003401 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 char_attr = hl_attr(HLF_AT);
3403 }
3404 }
3405#endif
3406
3407#ifdef FEAT_FOLDING
3408 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3409 {
3410 draw_state = WL_FOLD;
3411 if (wp->w_p_fdc > 0)
3412 {
3413 /* Draw the 'foldcolumn'. */
3414 fill_foldcolumn(extra, wp, FALSE, lnum);
3415 n_extra = wp->w_p_fdc;
3416 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003417 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 c_extra = NUL;
3419 char_attr = hl_attr(HLF_FC);
3420 }
3421 }
3422#endif
3423
3424#ifdef FEAT_SIGNS
3425 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3426 {
3427 draw_state = WL_SIGN;
3428 /* Show the sign column when there are any signs in this
3429 * buffer or when using Netbeans. */
3430 if (draw_signcolumn(wp)
3431# ifdef FEAT_DIFF
3432 && filler_todo <= 0
3433# endif
3434 )
3435 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003436 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003438 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439# endif
3440
3441 /* Draw two cells with the sign value or blank. */
3442 c_extra = ' ';
3443 char_attr = hl_attr(HLF_SC);
3444 n_extra = 2;
3445
3446 if (row == startrow)
3447 {
3448 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3449 SIGN_TEXT);
3450# ifdef FEAT_SIGN_ICONS
3451 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3452 SIGN_ICON);
3453 if (gui.in_use && icon_sign != 0)
3454 {
3455 /* Use the image in this position. */
3456 c_extra = SIGN_BYTE;
3457# ifdef FEAT_NETBEANS_INTG
3458 if (buf_signcount(wp->w_buffer, lnum) > 1)
3459 c_extra = MULTISIGN_BYTE;
3460# endif
3461 char_attr = icon_sign;
3462 }
3463 else
3464# endif
3465 if (text_sign != 0)
3466 {
3467 p_extra = sign_get_text(text_sign);
3468 if (p_extra != NULL)
3469 {
3470 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473 char_attr = sign_get_attr(text_sign, FALSE);
3474 }
3475 }
3476 }
3477 }
3478#endif
3479
3480 if (draw_state == WL_NR - 1 && n_extra == 0)
3481 {
3482 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003483 /* Display the absolute or relative line number. After the
3484 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3485 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 && (row == startrow
3487#ifdef FEAT_DIFF
3488 + filler_lines
3489#endif
3490 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3491 {
3492 /* Draw the line number (empty space after wrapping). */
3493 if (row == startrow
3494#ifdef FEAT_DIFF
3495 + filler_lines
3496#endif
3497 )
3498 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003499 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003500 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003501
3502 if (wp->w_p_nu)
3503 /* 'number' */
3504 num = (long)lnum;
3505 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003506 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003507 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003508 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar700e7342013-01-30 12:31:36 +01003509 if (num == 0)
3510 {
3511 num = lnum;
3512 fmt = "%-*ld ";
3513 }
3514 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003515
Bram Moolenaar700e7342013-01-30 12:31:36 +01003516 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003517 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (wp->w_skipcol > 0)
3519 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3520 *p_extra = '-';
3521#ifdef FEAT_RIGHTLEFT
3522 if (wp->w_p_rl) /* reverse line numbers */
3523 rl_mirror(extra);
3524#endif
3525 p_extra = extra;
3526 c_extra = NUL;
3527 }
3528 else
3529 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003530 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003532#ifdef FEAT_SYN_HL
3533 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003534 * the current line differently.
3535 * TODO: Can we use CursorLine instead of CursorLineNr
3536 * when CursorLineNr isn't set? */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003537 if ((wp->w_p_cul || wp->w_p_rnu)
3538 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003539 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
3542 }
3543
3544#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3545 if (draw_state == WL_SBR - 1 && n_extra == 0)
3546 {
3547 draw_state = WL_SBR;
3548# ifdef FEAT_DIFF
3549 if (filler_todo > 0)
3550 {
3551 /* Draw "deleted" diff line(s). */
3552 if (char2cells(fill_diff) > 1)
3553 c_extra = '-';
3554 else
3555 c_extra = fill_diff;
3556# ifdef FEAT_RIGHTLEFT
3557 if (wp->w_p_rl)
3558 n_extra = col + 1;
3559 else
3560# endif
3561 n_extra = W_WIDTH(wp) - col;
3562 char_attr = hl_attr(HLF_DED);
3563 }
3564# endif
3565# ifdef FEAT_LINEBREAK
3566 if (*p_sbr != NUL && need_showbreak)
3567 {
3568 /* Draw 'showbreak' at the start of each broken line. */
3569 p_extra = p_sbr;
3570 c_extra = NUL;
3571 n_extra = (int)STRLEN(p_sbr);
3572 char_attr = hl_attr(HLF_AT);
3573 need_showbreak = FALSE;
3574 /* Correct end of highlighted area for 'showbreak',
3575 * required when 'linebreak' is also set. */
3576 if (tocol == vcol)
3577 tocol += n_extra;
3578 }
3579# endif
3580 }
3581#endif
3582
3583 if (draw_state == WL_LINE - 1 && n_extra == 0)
3584 {
3585 draw_state = WL_LINE;
3586 if (saved_n_extra)
3587 {
3588 /* Continue item from end of wrapped line. */
3589 n_extra = saved_n_extra;
3590 c_extra = saved_c_extra;
3591 p_extra = saved_p_extra;
3592 char_attr = saved_char_attr;
3593 }
3594 else
3595 char_attr = 0;
3596 }
3597 }
3598
3599 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003600 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003601 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#ifdef FEAT_DIFF
3603 && filler_todo <= 0
3604#endif
3605 )
3606 {
3607 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3608 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003609 /* Pretend we have finished updating the window. Except when
3610 * 'cursorcolumn' is set. */
3611#ifdef FEAT_SYN_HL
3612 if (wp->w_p_cuc)
3613 row = wp->w_cline_row + wp->w_cline_height;
3614 else
3615#endif
3616 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 break;
3618 }
3619
3620 if (draw_state == WL_LINE && area_highlighting)
3621 {
3622 /* handle Visual or match highlighting in this line */
3623 if (vcol == fromcol
3624#ifdef FEAT_MBYTE
3625 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3626 && (*mb_ptr2cells)(ptr) > 1)
3627#endif
3628 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003629 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 && vcol < tocol))
3631 area_attr = attr; /* start highlighting */
3632 else if (area_attr != 0
3633 && (vcol == tocol
3634 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637#ifdef FEAT_SEARCH_EXTRA
3638 if (!n_extra)
3639 {
3640 /*
3641 * Check for start/end of search pattern match.
3642 * After end, check for start/end of next match.
3643 * When another match, have to check for start again.
3644 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003645 * Do this for 'search_hl' and the match list (ordered by
3646 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003648 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003649 cur = wp->w_match_head;
3650 shl_flag = FALSE;
3651 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003653 if (shl_flag == FALSE
3654 && ((cur != NULL
3655 && cur->priority > SEARCH_HL_PRIORITY)
3656 || cur == NULL))
3657 {
3658 shl = &search_hl;
3659 shl_flag = TRUE;
3660 }
3661 else
3662 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 while (shl->rm.regprog != NULL)
3664 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003665 if (shl->startcol != MAXCOL
3666 && v >= (long)shl->startcol
3667 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 shl->attr_cur = shl->attr;
3670 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003671 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
3673 shl->attr_cur = 0;
3674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 next_search_hl(wp, shl, lnum, (colnr_T)v);
3676
3677 /* Need to get the line again, a multi-line regexp
3678 * may have made it invalid. */
3679 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3680 ptr = line + v;
3681
3682 if (shl->lnum == lnum)
3683 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003684 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003686 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003688 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003690 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 {
3692 /* highlight empty match, try again after
3693 * it */
3694#ifdef FEAT_MBYTE
3695 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003696 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003697 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 else
3699#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003700 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702
3703 /* Loop to check if the match starts at the
3704 * current position */
3705 continue;
3706 }
3707 }
3708 break;
3709 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003710 if (shl != &search_hl && cur != NULL)
3711 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003713
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003714 /* Use attributes from match with highest priority among
3715 * 'search_hl' and the match list. */
3716 search_attr = search_hl.attr_cur;
3717 cur = wp->w_match_head;
3718 shl_flag = FALSE;
3719 while (cur != NULL || shl_flag == FALSE)
3720 {
3721 if (shl_flag == FALSE
3722 && ((cur != NULL
3723 && cur->priority > SEARCH_HL_PRIORITY)
3724 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003725 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003726 shl = &search_hl;
3727 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003728 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003729 else
3730 shl = &cur->hl;
3731 if (shl->attr_cur != 0)
3732 search_attr = shl->attr_cur;
3733 if (shl != &search_hl && cur != NULL)
3734 cur = cur->next;
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737#endif
3738
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003740 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003742 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3743 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003745 if (diff_hlf == HLF_TXD && ptr - line > change_end
3746 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003748 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
3750#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003751
3752 /* Decide which of the highlight attributes to use. */
3753 attr_pri = TRUE;
3754 if (area_attr != 0)
3755 char_attr = area_attr;
3756 else if (search_attr != 0)
3757 char_attr = search_attr;
3758#ifdef LINE_ATTR
3759 /* Use line_attr when not in the Visual or 'incsearch' area
3760 * (area_attr may be 0 when "noinvcur" is set). */
3761 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003762 || vcol < fromcol || vcol_prev < fromcol_prev
3763 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003764 char_attr = line_attr;
3765#endif
3766 else
3767 {
3768 attr_pri = FALSE;
3769#ifdef FEAT_SYN_HL
3770 if (has_syntax)
3771 char_attr = syntax_attr;
3772 else
3773#endif
3774 char_attr = 0;
3775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
3777
3778 /*
3779 * Get the next character to put on the screen.
3780 */
3781 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003782 * The "p_extra" points to the extra stuff that is inserted to
3783 * represent special characters (non-printable stuff) and other
3784 * things. When all characters are the same, c_extra is used.
3785 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3786 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3788 */
3789 if (n_extra > 0)
3790 {
3791 if (c_extra != NUL)
3792 {
3793 c = c_extra;
3794#ifdef FEAT_MBYTE
3795 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3796 if (enc_utf8 && (*mb_char2len)(c) > 1)
3797 {
3798 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003799 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003800 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 else
3803 mb_utf8 = FALSE;
3804#endif
3805 }
3806 else
3807 {
3808 c = *p_extra;
3809#ifdef FEAT_MBYTE
3810 if (has_mbyte)
3811 {
3812 mb_c = c;
3813 if (enc_utf8)
3814 {
3815 /* If the UTF-8 character is more than one byte:
3816 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003817 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 mb_utf8 = FALSE;
3819 if (mb_l > n_extra)
3820 mb_l = 1;
3821 else if (mb_l > 1)
3822 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003823 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003825 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827 }
3828 else
3829 {
3830 /* if this is a DBCS character, put it in "mb_c" */
3831 mb_l = MB_BYTE2LEN(c);
3832 if (mb_l >= n_extra)
3833 mb_l = 1;
3834 else if (mb_l > 1)
3835 mb_c = (c << 8) + p_extra[1];
3836 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003837 if (mb_l == 0) /* at the NUL at end-of-line */
3838 mb_l = 1;
3839
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 /* If a double-width char doesn't fit display a '>' in the
3841 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003842 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843# ifdef FEAT_RIGHTLEFT
3844 wp->w_p_rl ? (col <= 0) :
3845# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003846 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 && (*mb_char2cells)(mb_c) == 2)
3848 {
3849 c = '>';
3850 mb_c = c;
3851 mb_l = 1;
3852 mb_utf8 = FALSE;
3853 multi_attr = hl_attr(HLF_AT);
3854 /* put the pointer back to output the double-width
3855 * character at the start of the next line. */
3856 ++n_extra;
3857 --p_extra;
3858 }
3859 else
3860 {
3861 n_extra -= mb_l - 1;
3862 p_extra += mb_l - 1;
3863 }
3864 }
3865#endif
3866 ++p_extra;
3867 }
3868 --n_extra;
3869 }
3870 else
3871 {
3872 /*
3873 * Get a character from the line itself.
3874 */
3875 c = *ptr;
3876#ifdef FEAT_MBYTE
3877 if (has_mbyte)
3878 {
3879 mb_c = c;
3880 if (enc_utf8)
3881 {
3882 /* If the UTF-8 character is more than one byte: Decode it
3883 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003884 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 mb_utf8 = FALSE;
3886 if (mb_l > 1)
3887 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003888 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 /* Overlong encoded ASCII or ASCII with composing char
3890 * is displayed normally, except a NUL. */
3891 if (mb_c < 0x80)
3892 c = mb_c;
3893 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003894
3895 /* At start of the line we can have a composing char.
3896 * Draw it as a space with a composing char. */
3897 if (utf_iscomposing(mb_c))
3898 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003899 int i;
3900
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003901 for (i = Screen_mco - 1; i > 0; --i)
3902 u8cc[i] = u8cc[i - 1];
3903 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003904 mb_c = ' ';
3905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907
3908 if ((mb_l == 1 && c >= 0x80)
3909 || (mb_l >= 1 && mb_c == 0)
3910 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003911# ifdef UNICODE16
3912 || mb_c >= 0x10000
3913# endif
3914 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
3916 /*
3917 * Illegal UTF-8 byte: display as <xx>.
3918 * Non-BMP character : display as ? or fullwidth ?.
3919 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003920# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
3924 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003925# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (wp->w_p_rl) /* reverse */
3927 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003930# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 else if (utf_char2cells(mb_c) != 2)
3932 STRCPY(extra, "?");
3933 else
3934 /* 0xff1f in UTF-8: full-width '?' */
3935 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 p_extra = extra;
3939 c = *p_extra;
3940 mb_c = mb_ptr2char_adv(&p_extra);
3941 mb_utf8 = (c >= 0x80);
3942 n_extra = (int)STRLEN(p_extra);
3943 c_extra = NUL;
3944 if (area_attr == 0 && search_attr == 0)
3945 {
3946 n_attr = n_extra + 1;
3947 extra_attr = hl_attr(HLF_8);
3948 saved_attr2 = char_attr; /* save current attr */
3949 }
3950 }
3951 else if (mb_l == 0) /* at the NUL at end-of-line */
3952 mb_l = 1;
3953#ifdef FEAT_ARABIC
3954 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3955 {
3956 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003957 int pc, pc1, nc;
3958 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
3960 /* The idea of what is the previous and next
3961 * character depends on 'rightleft'. */
3962 if (wp->w_p_rl)
3963 {
3964 pc = prev_c;
3965 pc1 = prev_c1;
3966 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003967 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 else
3970 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003971 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003973 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 prev_c = mb_c;
3976
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003977 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
3979 else
3980 prev_c = mb_c;
3981#endif
3982 }
3983 else /* enc_dbcs */
3984 {
3985 mb_l = MB_BYTE2LEN(c);
3986 if (mb_l == 0) /* at the NUL at end-of-line */
3987 mb_l = 1;
3988 else if (mb_l > 1)
3989 {
3990 /* We assume a second byte below 32 is illegal.
3991 * Hopefully this is OK for all double-byte encodings!
3992 */
3993 if (ptr[1] >= 32)
3994 mb_c = (c << 8) + ptr[1];
3995 else
3996 {
3997 if (ptr[1] == NUL)
3998 {
3999 /* head byte at end of line */
4000 mb_l = 1;
4001 transchar_nonprint(extra, c);
4002 }
4003 else
4004 {
4005 /* illegal tail byte */
4006 mb_l = 2;
4007 STRCPY(extra, "XX");
4008 }
4009 p_extra = extra;
4010 n_extra = (int)STRLEN(extra) - 1;
4011 c_extra = NUL;
4012 c = *p_extra++;
4013 if (area_attr == 0 && search_attr == 0)
4014 {
4015 n_attr = n_extra + 1;
4016 extra_attr = hl_attr(HLF_8);
4017 saved_attr2 = char_attr; /* save current attr */
4018 }
4019 mb_c = c;
4020 }
4021 }
4022 }
4023 /* If a double-width char doesn't fit display a '>' in the
4024 * last column; the character is displayed at the start of the
4025 * next line. */
4026 if ((
4027# ifdef FEAT_RIGHTLEFT
4028 wp->w_p_rl ? (col <= 0) :
4029# endif
4030 (col >= W_WIDTH(wp) - 1))
4031 && (*mb_char2cells)(mb_c) == 2)
4032 {
4033 c = '>';
4034 mb_c = c;
4035 mb_utf8 = FALSE;
4036 mb_l = 1;
4037 multi_attr = hl_attr(HLF_AT);
4038 /* Put pointer back so that the character will be
4039 * displayed at the start of the next line. */
4040 --ptr;
4041 }
4042 else if (*ptr != NUL)
4043 ptr += mb_l - 1;
4044
4045 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004046 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004047 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004048 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004051 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 c = ' ';
4053 if (area_attr == 0 && search_attr == 0)
4054 {
4055 n_attr = n_extra + 1;
4056 extra_attr = hl_attr(HLF_AT);
4057 saved_attr2 = char_attr; /* save current attr */
4058 }
4059 mb_c = c;
4060 mb_utf8 = FALSE;
4061 mb_l = 1;
4062 }
4063
4064 }
4065#endif
4066 ++ptr;
4067
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004068 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 if (wp->w_p_list && (c == 160
4070#ifdef FEAT_MBYTE
4071 || (mb_utf8 && mb_c == 160)
4072#endif
4073 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004074 {
4075 c = lcs_nbsp;
4076 if (area_attr == 0 && search_attr == 0)
4077 {
4078 n_attr = 1;
4079 extra_attr = hl_attr(HLF_8);
4080 saved_attr2 = char_attr; /* save current attr */
4081 }
4082#ifdef FEAT_MBYTE
4083 mb_c = c;
4084 if (enc_utf8 && (*mb_char2len)(c) > 1)
4085 {
4086 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004087 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004088 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004089 }
4090 else
4091 mb_utf8 = FALSE;
4092#endif
4093 }
4094
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (extra_check)
4096 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004097#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004098 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004099#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004100
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004101#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 /* Get syntax attribute, unless still at the start of the line
4103 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004104 v = (long)(ptr - line);
4105 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
4107 /* Get the syntax attribute for the character. If there
4108 * is an error, disable syntax highlighting. */
4109 save_did_emsg = did_emsg;
4110 did_emsg = FALSE;
4111
Bram Moolenaar217ad922005-03-20 22:37:15 +00004112 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004113# ifdef FEAT_SPELL
4114 has_spell ? &can_spell :
4115# endif
4116 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
4118 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004119 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004120 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004121 has_syntax = FALSE;
4122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 else
4124 did_emsg = save_did_emsg;
4125
4126 /* Need to get the line again, a multi-line regexp may
4127 * have made it invalid. */
4128 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4129 ptr = line + v;
4130
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004131 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004133 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004134 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004135# ifdef FEAT_CONCEAL
4136 /* no concealing past the end of the line, it interferes
4137 * with line highlighting */
4138 if (c == NUL)
4139 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004140 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004141 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004144#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004145
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004146#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004147 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004148 * Only do this when there is no syntax highlighting, the
4149 * @Spell cluster is not used or the current syntax item
4150 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004151 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004152 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004153 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004154# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004155 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004156 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004157# endif
4158 if (c != 0 && (
4159# ifdef FEAT_SYN_HL
4160 !has_syntax ||
4161# endif
4162 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004163 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004164 char_u *prev_ptr, *p;
4165 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004166 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004167# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004168 if (has_mbyte)
4169 {
4170 prev_ptr = ptr - mb_l;
4171 v -= mb_l - 1;
4172 }
4173 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004174# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004175 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004176
4177 /* Use nextline[] if possible, it has the start of the
4178 * next line concatenated. */
4179 if ((prev_ptr - line) - nextlinecol >= 0)
4180 p = nextline + (prev_ptr - line) - nextlinecol;
4181 else
4182 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004183 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004184 len = spell_check(wp, p, &spell_hlf, &cap_col,
4185 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004186 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004187
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004188 /* In Insert mode only highlight a word that
4189 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004190 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004191 && (State & INSERT) != 0
4192 && wp->w_cursor.lnum == lnum
4193 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004194 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004195 && wp->w_cursor.col < (colnr_T)word_end)
4196 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004197 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004198 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004199 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004200
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004201 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004202 && (p - nextline) + len > nextline_idx)
4203 {
4204 /* Remember that the good word continues at the
4205 * start of the next line. */
4206 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004207 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004208 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004209
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004210 /* Turn index into actual attributes. */
4211 if (spell_hlf != HLF_COUNT)
4212 spell_attr = highlight_attr[spell_hlf];
4213
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004214 if (cap_col > 0)
4215 {
4216 if (p != prev_ptr
4217 && (p - nextline) + cap_col >= nextline_idx)
4218 {
4219 /* Remember that the word in the next line
4220 * must start with a capital. */
4221 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004222 cap_col = (int)((p - nextline) + cap_col
4223 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004224 }
4225 else
4226 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004227 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004228 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004229 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004230 }
4231 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004232 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004233 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004234 char_attr = hl_combine_attr(char_attr, spell_attr);
4235 else
4236 char_attr = hl_combine_attr(spell_attr, char_attr);
4237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238#endif
4239#ifdef FEAT_LINEBREAK
4240 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004241 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 */
4243 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004244 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 n_extra = win_lbr_chartabsize(wp, ptr - (
4247# ifdef FEAT_MBYTE
4248 has_mbyte ? mb_l :
4249# endif
4250 1), (colnr_T)vcol, NULL) - 1;
4251 c_extra = ' ';
4252 if (vim_iswhite(c))
4253 c = ' ';
4254 }
4255#endif
4256
4257 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4258 {
4259 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004260 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
4262 n_attr = 1;
4263 extra_attr = hl_attr(HLF_8);
4264 saved_attr2 = char_attr; /* save current attr */
4265 }
4266#ifdef FEAT_MBYTE
4267 mb_c = c;
4268 if (enc_utf8 && (*mb_char2len)(c) > 1)
4269 {
4270 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004271 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004272 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 else
4275 mb_utf8 = FALSE;
4276#endif
4277 }
4278 }
4279
4280 /*
4281 * Handling of non-printable characters.
4282 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004283 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
4285 /*
4286 * when getting a character from the file, we may have to
4287 * turn it into something else on the way to putting it
4288 * into "ScreenLines".
4289 */
4290 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4291 {
4292 /* tab amount depends on current column */
4293 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004294 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4295#ifdef FEAT_CONCEAL
4296 /* Tab alignment should be identical regardless of
4297 * 'conceallevel' value. So tab compensates of all
4298 * previous concealed characters, and thus resets vcol_off
4299 * and boguscols accumulated so far in the line. Note that
4300 * the tab can be longer than 'tabstop' when there
4301 * are concealed characters. */
4302 n_extra += vcol_off;
4303 vcol -= vcol_off;
4304 vcol_off = 0;
4305 col -= boguscols;
4306 boguscols = 0;
4307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308#ifdef FEAT_MBYTE
4309 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4310#endif
4311 if (wp->w_p_list)
4312 {
4313 c = lcs_tab1;
4314 c_extra = lcs_tab2;
4315 n_attr = n_extra + 1;
4316 extra_attr = hl_attr(HLF_8);
4317 saved_attr2 = char_attr; /* save current attr */
4318#ifdef FEAT_MBYTE
4319 mb_c = c;
4320 if (enc_utf8 && (*mb_char2len)(c) > 1)
4321 {
4322 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004323 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004324 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326#endif
4327 }
4328 else
4329 {
4330 c_extra = ' ';
4331 c = ' ';
4332 }
4333 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004334 else if (c == NUL
4335 && ((wp->w_p_list && lcs_eol > 0)
4336 || ((fromcol >= 0 || fromcol_prev >= 0)
4337 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004338#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004339 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004340#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004341 && (
4342# ifdef FEAT_RIGHTLEFT
4343 wp->w_p_rl ? (col >= 0) :
4344# endif
4345 (col < W_WIDTH(wp)))
4346 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004347 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004348 && (colnr_T)vcol == wp->w_virtcol)))
4349 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004351 /* Display a '$' after the line or highlight an extra
4352 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4354 /* For a diff line the highlighting continues after the
4355 * "$". */
4356 if (
4357# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004358 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359# ifdef LINE_ATTR
4360 &&
4361# endif
4362# endif
4363# ifdef LINE_ATTR
4364 line_attr == 0
4365# endif
4366 )
4367#endif
4368 {
4369#ifdef FEAT_VIRTUALEDIT
4370 /* In virtualedit, visual selections may extend
4371 * beyond end of line. */
4372 if (area_highlighting && virtual_active()
4373 && tocol != MAXCOL && vcol < tocol)
4374 n_extra = 0;
4375 else
4376#endif
4377 {
4378 p_extra = at_end_str;
4379 n_extra = 1;
4380 c_extra = NUL;
4381 }
4382 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004383 if (wp->w_p_list)
4384 c = lcs_eol;
4385 else
4386 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 lcs_eol_one = -1;
4388 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004389 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
4391 extra_attr = hl_attr(HLF_AT);
4392 n_attr = 1;
4393 }
4394#ifdef FEAT_MBYTE
4395 mb_c = c;
4396 if (enc_utf8 && (*mb_char2len)(c) > 1)
4397 {
4398 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004399 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004400 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 else
4403 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4404#endif
4405 }
4406 else if (c != NUL)
4407 {
4408 p_extra = transchar(c);
4409#ifdef FEAT_RIGHTLEFT
4410 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4411 rl_mirror(p_extra); /* reverse "<12>" */
4412#endif
4413 n_extra = byte2cells(c) - 1;
4414 c_extra = NUL;
4415 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004416 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
4418 n_attr = n_extra + 1;
4419 extra_attr = hl_attr(HLF_8);
4420 saved_attr2 = char_attr; /* save current attr */
4421 }
4422#ifdef FEAT_MBYTE
4423 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4424#endif
4425 }
4426#ifdef FEAT_VIRTUALEDIT
4427 else if (VIsual_active
4428 && (VIsual_mode == Ctrl_V
4429 || VIsual_mode == 'v')
4430 && virtual_active()
4431 && tocol != MAXCOL
4432 && vcol < tocol
4433 && (
4434# ifdef FEAT_RIGHTLEFT
4435 wp->w_p_rl ? (col >= 0) :
4436# endif
4437 (col < W_WIDTH(wp))))
4438 {
4439 c = ' ';
4440 --ptr; /* put it back at the NUL */
4441 }
4442#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004443#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 else if ((
4445# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004446 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 ) && (
4450# ifdef FEAT_RIGHTLEFT
4451 wp->w_p_rl ? (col >= 0) :
4452# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004453 (col
4454# ifdef FEAT_CONCEAL
4455 - boguscols
4456# endif
4457 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
4459 /* Highlight until the right side of the window */
4460 c = ' ';
4461 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004462
4463 /* Remember we do the char for line highlighting. */
4464 ++did_line_attr;
4465
4466 /* don't do search HL for the rest of the line */
4467 if (line_attr != 0 && char_attr == search_attr && col > 0)
4468 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469# ifdef FEAT_DIFF
4470 if (diff_hlf == HLF_TXD)
4471 {
4472 diff_hlf = HLF_CHD;
4473 if (attr == 0 || char_attr != attr)
4474 char_attr = hl_attr(diff_hlf);
4475 }
4476# endif
4477 }
4478#endif
4479 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004480
4481#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004482 if ( wp->w_p_cole > 0
4483 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4484 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004485 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004486 && !(lnum_in_visual_area
4487 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004488 {
4489 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004490 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004491 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4492 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004493 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004494 /* First time at this concealed item: display one
4495 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004496 if (syn_get_sub_char() != NUL)
4497 c = syn_get_sub_char();
4498 else if (lcs_conceal != NUL)
4499 c = lcs_conceal;
4500 else
4501 c = ' ';
4502
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004503 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004504
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004505 if (n_extra > 0)
4506 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004507 vcol += n_extra;
4508 if (wp->w_p_wrap && n_extra > 0)
4509 {
4510# ifdef FEAT_RIGHTLEFT
4511 if (wp->w_p_rl)
4512 {
4513 col -= n_extra;
4514 boguscols -= n_extra;
4515 }
4516 else
4517# endif
4518 {
4519 boguscols += n_extra;
4520 col += n_extra;
4521 }
4522 }
4523 n_extra = 0;
4524 n_attr = 0;
4525 }
4526 else if (n_skip == 0)
4527 {
4528 is_concealing = TRUE;
4529 n_skip = 1;
4530 }
4531# ifdef FEAT_MBYTE
4532 mb_c = c;
4533 if (enc_utf8 && (*mb_char2len)(c) > 1)
4534 {
4535 mb_utf8 = TRUE;
4536 u8cc[0] = 0;
4537 c = 0xc0;
4538 }
4539 else
4540 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4541# endif
4542 }
4543 else
4544 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004545 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004546 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004547 }
4548#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
4550
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004551#ifdef FEAT_CONCEAL
4552 /* In the cursor line and we may be concealing characters: correct
4553 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004554 if (!did_wcol && draw_state == WL_LINE
4555 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004556 && conceal_cursor_line(wp)
4557 && (int)wp->w_virtcol <= vcol + n_skip)
4558 {
4559 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004560 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004561 did_wcol = TRUE;
4562 }
4563#endif
4564
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 /* Don't override visual selection highlighting. */
4566 if (n_attr > 0
4567 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004568 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 char_attr = extra_attr;
4570
Bram Moolenaar81695252004-12-29 20:58:21 +00004571#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 /* XIM don't send preedit_start and preedit_end, but they send
4573 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4574 * im_is_preediting() here. */
4575 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004576 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 && (State & INSERT)
4578 && !p_imdisable
4579 && im_is_preediting()
4580 && draw_state == WL_LINE)
4581 {
4582 colnr_T tcol;
4583
4584 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004585 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 else
4587 tcol = preedit_end_col;
4588 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4589 {
4590 if (feedback_old_attr < 0)
4591 {
4592 feedback_col = 0;
4593 feedback_old_attr = char_attr;
4594 }
4595 char_attr = im_get_feedback_attr(feedback_col);
4596 if (char_attr < 0)
4597 char_attr = feedback_old_attr;
4598 feedback_col++;
4599 }
4600 else if (feedback_old_attr >= 0)
4601 {
4602 char_attr = feedback_old_attr;
4603 feedback_old_attr = -1;
4604 feedback_col = 0;
4605 }
4606 }
4607#endif
4608 /*
4609 * Handle the case where we are in column 0 but not on the first
4610 * character of the line and the user wants us to show us a
4611 * special character (via 'listchars' option "precedes:<char>".
4612 */
4613 if (lcs_prec_todo != NUL
4614 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4615#ifdef FEAT_DIFF
4616 && filler_todo <= 0
4617#endif
4618 && draw_state > WL_NR
4619 && c != NUL)
4620 {
4621 c = lcs_prec;
4622 lcs_prec_todo = NUL;
4623#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004624 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4625 {
4626 /* Double-width character being overwritten by the "precedes"
4627 * character, need to fill up half the character. */
4628 c_extra = MB_FILLER_CHAR;
4629 n_extra = 1;
4630 n_attr = 2;
4631 extra_attr = hl_attr(HLF_AT);
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 mb_c = c;
4634 if (enc_utf8 && (*mb_char2len)(c) > 1)
4635 {
4636 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004637 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004638 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 else
4641 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4642#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004643 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
4645 saved_attr3 = char_attr; /* save current attr */
4646 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4647 n_attr3 = 1;
4648 }
4649 }
4650
4651 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004652 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004654 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004655#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004656 || did_line_attr == 1
4657#endif
4658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004660#ifdef FEAT_SEARCH_EXTRA
4661 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004662
4663 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004664 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004665 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004666#endif
4667
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004668 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 * highlight match at end of line. If it's beyond the last
4670 * char on the screen, just overwrite that one (tricky!) Not
4671 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004672#ifdef FEAT_SEARCH_EXTRA
4673 prevcol_hl_flag = FALSE;
4674 if (prevcol == (long)search_hl.startcol)
4675 prevcol_hl_flag = TRUE;
4676 else
4677 {
4678 cur = wp->w_match_head;
4679 while (cur != NULL)
4680 {
4681 if (prevcol == (long)cur->hl.startcol)
4682 {
4683 prevcol_hl_flag = TRUE;
4684 break;
4685 }
4686 cur = cur->next;
4687 }
4688 }
4689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004691 && ((area_attr != 0 && vcol == fromcol
4692#ifdef FEAT_VISUAL
4693 && (VIsual_mode != Ctrl_V
4694 || lnum == VIsual.lnum
4695 || lnum == curwin->w_cursor.lnum)
4696#endif
4697 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698#ifdef FEAT_SEARCH_EXTRA
4699 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004700 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004701# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004702 && did_line_attr <= 1
4703# endif
4704 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#endif
4706 ))
4707 {
4708 int n = 0;
4709
4710#ifdef FEAT_RIGHTLEFT
4711 if (wp->w_p_rl)
4712 {
4713 if (col < 0)
4714 n = 1;
4715 }
4716 else
4717#endif
4718 {
4719 if (col >= W_WIDTH(wp))
4720 n = -1;
4721 }
4722 if (n != 0)
4723 {
4724 /* At the window boundary, highlight the last character
4725 * instead (better than nothing). */
4726 off += n;
4727 col += n;
4728 }
4729 else
4730 {
4731 /* Add a blank character to highlight. */
4732 ScreenLines[off] = ' ';
4733#ifdef FEAT_MBYTE
4734 if (enc_utf8)
4735 ScreenLinesUC[off] = 0;
4736#endif
4737 }
4738#ifdef FEAT_SEARCH_EXTRA
4739 if (area_attr == 0)
4740 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004741 /* Use attributes from match with highest priority among
4742 * 'search_hl' and the match list. */
4743 char_attr = search_hl.attr;
4744 cur = wp->w_match_head;
4745 shl_flag = FALSE;
4746 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004747 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004748 if (shl_flag == FALSE
4749 && ((cur != NULL
4750 && cur->priority > SEARCH_HL_PRIORITY)
4751 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004752 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004753 shl = &search_hl;
4754 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004755 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004756 else
4757 shl = &cur->hl;
4758 if ((ptr - line) - 1 == (long)shl->startcol)
4759 char_attr = shl->attr;
4760 if (shl != &search_hl && cur != NULL)
4761 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
4764#endif
4765 ScreenAttrs[off] = char_attr;
4766#ifdef FEAT_RIGHTLEFT
4767 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004770 --off;
4771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 else
4773#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004774 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004776 ++off;
4777 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004778 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004779#ifdef FEAT_SYN_HL
4780 eol_hl_off = 1;
4781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaar91170f82006-05-05 21:15:17 +00004785 /*
4786 * At end of the text line.
4787 */
4788 if (c == NUL)
4789 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004790#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004791 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4792 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004793 {
4794 /* highlight last char after line */
4795 --col;
4796 --off;
4797 --vcol;
4798 }
4799
Bram Moolenaar1a384422010-07-14 19:53:30 +02004800 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004801 if (wp->w_p_wrap)
4802 v = wp->w_skipcol;
4803 else
4804 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004805
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004806 /* check if line ends before left margin */
4807 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004808 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004809#ifdef FEAT_CONCEAL
4810 /* Get rid of the boguscols now, we want to draw until the right
4811 * edge for 'cursorcolumn'. */
4812 col -= boguscols;
4813 boguscols = 0;
4814#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004815
4816 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004817 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004818
4819 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004820 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4821 && (int)wp->w_virtcol <
4822 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004823 && lnum != wp->w_cursor.lnum)
4824 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004825# ifdef FEAT_RIGHTLEFT
4826 && !wp->w_p_rl
4827# endif
4828 )
4829 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004830 int rightmost_vcol = 0;
4831 int i;
4832
4833 if (wp->w_p_cuc)
4834 rightmost_vcol = wp->w_virtcol;
4835 if (draw_color_col)
4836 /* determine rightmost colorcolumn to possibly draw */
4837 for (i = 0; color_cols[i] >= 0; ++i)
4838 if (rightmost_vcol < color_cols[i])
4839 rightmost_vcol = color_cols[i];
4840
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004841 while (col < W_WIDTH(wp))
4842 {
4843 ScreenLines[off] = ' ';
4844#ifdef FEAT_MBYTE
4845 if (enc_utf8)
4846 ScreenLinesUC[off] = 0;
4847#endif
4848 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004849 if (draw_color_col)
4850 draw_color_col = advance_color_col(VCOL_HLC,
4851 &color_cols);
4852
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004853 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004854 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004855 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004856 ScreenAttrs[off++] = hl_attr(HLF_MC);
4857 else
4858 ScreenAttrs[off++] = 0;
4859
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004860 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004861 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004862
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004863 ++vcol;
4864 }
4865 }
4866#endif
4867
Bram Moolenaar860cae12010-06-05 23:22:07 +02004868 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4869 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 row++;
4871
4872 /*
4873 * Update w_cline_height and w_cline_folded if the cursor line was
4874 * updated (saves a call to plines() later).
4875 */
4876 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4877 {
4878 curwin->w_cline_row = startrow;
4879 curwin->w_cline_height = row - startrow;
4880#ifdef FEAT_FOLDING
4881 curwin->w_cline_folded = FALSE;
4882#endif
4883 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4884 }
4885
4886 break;
4887 }
4888
4889 /* line continues beyond line end */
4890 if (lcs_ext
4891 && !wp->w_p_wrap
4892#ifdef FEAT_DIFF
4893 && filler_todo <= 0
4894#endif
4895 && (
4896#ifdef FEAT_RIGHTLEFT
4897 wp->w_p_rl ? col == 0 :
4898#endif
4899 col == W_WIDTH(wp) - 1)
4900 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004901 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4903 {
4904 c = lcs_ext;
4905 char_attr = hl_attr(HLF_AT);
4906#ifdef FEAT_MBYTE
4907 mb_c = c;
4908 if (enc_utf8 && (*mb_char2len)(c) > 1)
4909 {
4910 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004911 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004912 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914 else
4915 mb_utf8 = FALSE;
4916#endif
4917 }
4918
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004919#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004920 /* advance to the next 'colorcolumn' */
4921 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004922 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004923
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004924 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004925 * highlight the cursor position itself.
4926 * Also highlight the 'colorcolumn' if it is different than
4927 * 'cursorcolumn' */
4928 vcol_save_attr = -1;
4929 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004930 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004931 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004932 && lnum != wp->w_cursor.lnum)
4933 {
4934 vcol_save_attr = char_attr;
4935 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4936 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004937 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004938 {
4939 vcol_save_attr = char_attr;
4940 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4941 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004942 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004943#endif
4944
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 /*
4946 * Store character to be displayed.
4947 * Skip characters that are left of the screen for 'nowrap'.
4948 */
4949 vcol_prev = vcol;
4950 if (draw_state < WL_LINE || n_skip <= 0)
4951 {
4952 /*
4953 * Store the character.
4954 */
4955#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4956 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4957 {
4958 /* A double-wide character is: put first halve in left cell. */
4959 --off;
4960 --col;
4961 }
4962#endif
4963 ScreenLines[off] = c;
4964#ifdef FEAT_MBYTE
4965 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004966 {
4967 if ((mb_c & 0xff00) == 0x8e00)
4968 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 else if (enc_utf8)
4972 {
4973 if (mb_utf8)
4974 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004975 int i;
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004978 if ((c & 0xff) == 0)
4979 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004980 for (i = 0; i < Screen_mco; ++i)
4981 {
4982 ScreenLinesC[i][off] = u8cc[i];
4983 if (u8cc[i] == 0)
4984 break;
4985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 else
4988 ScreenLinesUC[off] = 0;
4989 }
4990 if (multi_attr)
4991 {
4992 ScreenAttrs[off] = multi_attr;
4993 multi_attr = 0;
4994 }
4995 else
4996#endif
4997 ScreenAttrs[off] = char_attr;
4998
4999#ifdef FEAT_MBYTE
5000 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5001 {
5002 /* Need to fill two screen columns. */
5003 ++off;
5004 ++col;
5005 if (enc_utf8)
5006 /* UTF-8: Put a 0 in the second screen char. */
5007 ScreenLines[off] = 0;
5008 else
5009 /* DBCS: Put second byte in the second screen char. */
5010 ScreenLines[off] = mb_c & 0xff;
5011 ++vcol;
5012 /* When "tocol" is halfway a character, set it to the end of
5013 * the character, otherwise highlighting won't stop. */
5014 if (tocol == vcol)
5015 ++tocol;
5016#ifdef FEAT_RIGHTLEFT
5017 if (wp->w_p_rl)
5018 {
5019 /* now it's time to backup one cell */
5020 --off;
5021 --col;
5022 }
5023#endif
5024 }
5025#endif
5026#ifdef FEAT_RIGHTLEFT
5027 if (wp->w_p_rl)
5028 {
5029 --off;
5030 --col;
5031 }
5032 else
5033#endif
5034 {
5035 ++off;
5036 ++col;
5037 }
5038 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005039#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005040 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005041 {
5042 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005043 ++vcol_off;
5044 if (n_extra > 0)
5045 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005046 if (wp->w_p_wrap)
5047 {
5048 /*
5049 * Special voodoo required if 'wrap' is on.
5050 *
5051 * Advance the column indicator to force the line
5052 * drawing to wrap early. This will make the line
5053 * take up the same screen space when parts are concealed,
5054 * so that cursor line computations aren't messed up.
5055 *
5056 * To avoid the fictitious advance of 'col' causing
5057 * trailing junk to be written out of the screen line
5058 * we are building, 'boguscols' keeps track of the number
5059 * of bad columns we have advanced.
5060 */
5061 if (n_extra > 0)
5062 {
5063 vcol += n_extra;
5064# ifdef FEAT_RIGHTLEFT
5065 if (wp->w_p_rl)
5066 {
5067 col -= n_extra;
5068 boguscols -= n_extra;
5069 }
5070 else
5071# endif
5072 {
5073 col += n_extra;
5074 boguscols += n_extra;
5075 }
5076 n_extra = 0;
5077 n_attr = 0;
5078 }
5079
5080
5081# ifdef FEAT_MBYTE
5082 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5083 {
5084 /* Need to fill two screen columns. */
5085# ifdef FEAT_RIGHTLEFT
5086 if (wp->w_p_rl)
5087 {
5088 --boguscols;
5089 --col;
5090 }
5091 else
5092# endif
5093 {
5094 ++boguscols;
5095 ++col;
5096 }
5097 }
5098# endif
5099
5100# ifdef FEAT_RIGHTLEFT
5101 if (wp->w_p_rl)
5102 {
5103 --boguscols;
5104 --col;
5105 }
5106 else
5107# endif
5108 {
5109 ++boguscols;
5110 ++col;
5111 }
5112 }
5113 else
5114 {
5115 if (n_extra > 0)
5116 {
5117 vcol += n_extra;
5118 n_extra = 0;
5119 n_attr = 0;
5120 }
5121 }
5122
5123 }
5124#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 else
5126 --n_skip;
5127
Bram Moolenaar64486672010-05-16 15:46:46 +02005128 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5129 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005130 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131#ifdef FEAT_DIFF
5132 && filler_todo <= 0
5133#endif
5134 )
5135 ++vcol;
5136
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005137#ifdef FEAT_SYN_HL
5138 if (vcol_save_attr >= 0)
5139 char_attr = vcol_save_attr;
5140#endif
5141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 /* restore attributes after "predeces" in 'listchars' */
5143 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5144 char_attr = saved_attr3;
5145
5146 /* restore attributes after last 'listchars' or 'number' char */
5147 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5148 char_attr = saved_attr2;
5149
5150 /*
5151 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005152 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
5154 if ((
5155#ifdef FEAT_RIGHTLEFT
5156 wp->w_p_rl ? (col < 0) :
5157#endif
5158 (col >= W_WIDTH(wp)))
5159 && (*ptr != NUL
5160#ifdef FEAT_DIFF
5161 || filler_todo > 0
5162#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005163 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5165 )
5166 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005167#ifdef FEAT_CONCEAL
5168 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5169 (int)W_WIDTH(wp), wp->w_p_rl);
5170 boguscols = 0;
5171#else
5172 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5173 (int)W_WIDTH(wp), wp->w_p_rl);
5174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 ++row;
5176 ++screen_row;
5177
5178 /* When not wrapping and finished diff lines, or when displayed
5179 * '$' and highlighting until last column, break here. */
5180 if ((!wp->w_p_wrap
5181#ifdef FEAT_DIFF
5182 && filler_todo <= 0
5183#endif
5184 ) || lcs_eol_one == -1)
5185 break;
5186
5187 /* When the window is too narrow draw all "@" lines. */
5188 if (draw_state != WL_LINE
5189#ifdef FEAT_DIFF
5190 && filler_todo <= 0
5191#endif
5192 )
5193 {
5194 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5195#ifdef FEAT_VERTSPLIT
5196 draw_vsep_win(wp, row);
5197#endif
5198 row = endrow;
5199 }
5200
5201 /* When line got too long for screen break here. */
5202 if (row == endrow)
5203 {
5204 ++row;
5205 break;
5206 }
5207
5208 if (screen_cur_row == screen_row - 1
5209#ifdef FEAT_DIFF
5210 && filler_todo <= 0
5211#endif
5212 && W_WIDTH(wp) == Columns)
5213 {
5214 /* Remember that the line wraps, used for modeless copy. */
5215 LineWraps[screen_row - 1] = TRUE;
5216
5217 /*
5218 * Special trick to make copy/paste of wrapped lines work with
5219 * xterm/screen: write an extra character beyond the end of
5220 * the line. This will work with all terminal types
5221 * (regardless of the xn,am settings).
5222 * Only do this on a fast tty.
5223 * Only do this if the cursor is on the current line
5224 * (something has been written in it).
5225 * Don't do this for the GUI.
5226 * Don't do this for double-width characters.
5227 * Don't do this for a window not at the right screen border.
5228 */
5229 if (p_tf
5230#ifdef FEAT_GUI
5231 && !gui.in_use
5232#endif
5233#ifdef FEAT_MBYTE
5234 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005235 && ((*mb_off2cells)(LineOffset[screen_row],
5236 LineOffset[screen_row] + screen_Columns)
5237 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005239 + (int)Columns - 2,
5240 LineOffset[screen_row] + screen_Columns)
5241 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#endif
5243 )
5244 {
5245 /* First make sure we are at the end of the screen line,
5246 * then output the same character again to let the
5247 * terminal know about the wrap. If the terminal doesn't
5248 * auto-wrap, we overwrite the character. */
5249 if (screen_cur_col != W_WIDTH(wp))
5250 screen_char(LineOffset[screen_row - 1]
5251 + (unsigned)Columns - 1,
5252 screen_row - 1, (int)(Columns - 1));
5253
5254#ifdef FEAT_MBYTE
5255 /* When there is a multi-byte character, just output a
5256 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005257 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5258 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 out_char(' ');
5260 else
5261#endif
5262 out_char(ScreenLines[LineOffset[screen_row - 1]
5263 + (Columns - 1)]);
5264 /* force a redraw of the first char on the next line */
5265 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5266 screen_start(); /* don't know where cursor is now */
5267 }
5268 }
5269
5270 col = 0;
5271 off = (unsigned)(current_ScreenLine - ScreenLines);
5272#ifdef FEAT_RIGHTLEFT
5273 if (wp->w_p_rl)
5274 {
5275 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5276 off += col;
5277 }
5278#endif
5279
5280 /* reset the drawing state for the start of a wrapped line */
5281 draw_state = WL_START;
5282 saved_n_extra = n_extra;
5283 saved_p_extra = p_extra;
5284 saved_c_extra = c_extra;
5285 saved_char_attr = char_attr;
5286 n_extra = 0;
5287 lcs_prec_todo = lcs_prec;
5288#ifdef FEAT_LINEBREAK
5289# ifdef FEAT_DIFF
5290 if (filler_todo <= 0)
5291# endif
5292 need_showbreak = TRUE;
5293#endif
5294#ifdef FEAT_DIFF
5295 --filler_todo;
5296 /* When the filler lines are actually below the last line of the
5297 * file, don't draw the line itself, break here. */
5298 if (filler_todo == 0 && wp->w_botfill)
5299 break;
5300#endif
5301 }
5302
5303 } /* for every character in the line */
5304
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005305#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005306 /* After an empty line check first word for capital. */
5307 if (*skipwhite(line) == NUL)
5308 {
5309 capcol_lnum = lnum + 1;
5310 cap_col = 0;
5311 }
5312#endif
5313
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 return row;
5315}
5316
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005317#ifdef FEAT_MBYTE
5318static int comp_char_differs __ARGS((int, int));
5319
5320/*
5321 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005322 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005323 */
5324 static int
5325comp_char_differs(off_from, off_to)
5326 int off_from;
5327 int off_to;
5328{
5329 int i;
5330
5331 for (i = 0; i < Screen_mco; ++i)
5332 {
5333 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5334 return TRUE;
5335 if (ScreenLinesC[i][off_from] == 0)
5336 break;
5337 }
5338 return FALSE;
5339}
5340#endif
5341
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342/*
5343 * Check whether the given character needs redrawing:
5344 * - the (first byte of the) character is different
5345 * - the attributes are different
5346 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005347 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 */
5349 static int
5350char_needs_redraw(off_from, off_to, cols)
5351 int off_from;
5352 int off_to;
5353 int cols;
5354{
5355 if (cols > 0
5356 && ((ScreenLines[off_from] != ScreenLines[off_to]
5357 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5358
5359#ifdef FEAT_MBYTE
5360 || (enc_dbcs != 0
5361 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5362 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5363 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5364 : (cols > 1 && ScreenLines[off_from + 1]
5365 != ScreenLines[off_to + 1])))
5366 || (enc_utf8
5367 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5368 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005369 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005370 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5371 && ScreenLines[off_from + 1]
5372 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#endif
5374 ))
5375 return TRUE;
5376 return FALSE;
5377}
5378
5379/*
5380 * Move one "cooked" screen line to the screen, but only the characters that
5381 * have actually changed. Handle insert/delete character.
5382 * "coloff" gives the first column on the screen for this line.
5383 * "endcol" gives the columns where valid characters are.
5384 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5385 * needs to be cleared, negative otherwise.
5386 * "rlflag" is TRUE in a rightleft window:
5387 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5388 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5389 */
5390 static void
5391screen_line(row, coloff, endcol, clear_width
5392#ifdef FEAT_RIGHTLEFT
5393 , rlflag
5394#endif
5395 )
5396 int row;
5397 int coloff;
5398 int endcol;
5399 int clear_width;
5400#ifdef FEAT_RIGHTLEFT
5401 int rlflag;
5402#endif
5403{
5404 unsigned off_from;
5405 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005406#ifdef FEAT_MBYTE
5407 unsigned max_off_from;
5408 unsigned max_off_to;
5409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 int col = 0;
5411#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5412 int hl;
5413#endif
5414 int force = FALSE; /* force update rest of the line */
5415 int redraw_this /* bool: does character need redraw? */
5416#ifdef FEAT_GUI
5417 = TRUE /* For GUI when while-loop empty */
5418#endif
5419 ;
5420 int redraw_next; /* redraw_this for next character */
5421#ifdef FEAT_MBYTE
5422 int clear_next = FALSE;
5423 int char_cells; /* 1: normal char */
5424 /* 2: occupies two display cells */
5425# define CHAR_CELLS char_cells
5426#else
5427# define CHAR_CELLS 1
5428#endif
5429
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005430 /* Check for illegal row and col, just in case. */
5431 if (row >= Rows)
5432 row = Rows - 1;
5433 if (endcol > Columns)
5434 endcol = Columns;
5435
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436# ifdef FEAT_CLIPBOARD
5437 clip_may_clear_selection(row, row);
5438# endif
5439
5440 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5441 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005442#ifdef FEAT_MBYTE
5443 max_off_from = off_from + screen_Columns;
5444 max_off_to = LineOffset[row] + screen_Columns;
5445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
5447#ifdef FEAT_RIGHTLEFT
5448 if (rlflag)
5449 {
5450 /* Clear rest first, because it's left of the text. */
5451 if (clear_width > 0)
5452 {
5453 while (col <= endcol && ScreenLines[off_to] == ' '
5454 && ScreenAttrs[off_to] == 0
5455# ifdef FEAT_MBYTE
5456 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5457# endif
5458 )
5459 {
5460 ++off_to;
5461 ++col;
5462 }
5463 if (col <= endcol)
5464 screen_fill(row, row + 1, col + coloff,
5465 endcol + coloff + 1, ' ', ' ', 0);
5466 }
5467 col = endcol + 1;
5468 off_to = LineOffset[row] + col + coloff;
5469 off_from += col;
5470 endcol = (clear_width > 0 ? clear_width : -clear_width);
5471 }
5472#endif /* FEAT_RIGHTLEFT */
5473
5474 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5475
5476 while (col < endcol)
5477 {
5478#ifdef FEAT_MBYTE
5479 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005480 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 else
5482 char_cells = 1;
5483#endif
5484
5485 redraw_this = redraw_next;
5486 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5487 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5488
5489#ifdef FEAT_GUI
5490 /* If the next character was bold, then redraw the current character to
5491 * remove any pixels that might have spilt over into us. This only
5492 * happens in the GUI.
5493 */
5494 if (redraw_next && gui.in_use)
5495 {
5496 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005497 if (hl > HL_ALL)
5498 hl = syn_attr2attr(hl);
5499 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 redraw_this = TRUE;
5501 }
5502#endif
5503
5504 if (redraw_this)
5505 {
5506 /*
5507 * Special handling when 'xs' termcap flag set (hpterm):
5508 * Attributes for characters are stored at the position where the
5509 * cursor is when writing the highlighting code. The
5510 * start-highlighting code must be written with the cursor on the
5511 * first highlighted character. The stop-highlighting code must
5512 * be written with the cursor just after the last highlighted
5513 * character.
5514 * Overwriting a character doesn't remove it's highlighting. Need
5515 * to clear the rest of the line, and force redrawing it
5516 * completely.
5517 */
5518 if ( p_wiv
5519 && !force
5520#ifdef FEAT_GUI
5521 && !gui.in_use
5522#endif
5523 && ScreenAttrs[off_to] != 0
5524 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5525 {
5526 /*
5527 * Need to remove highlighting attributes here.
5528 */
5529 windgoto(row, col + coloff);
5530 out_str(T_CE); /* clear rest of this screen line */
5531 screen_start(); /* don't know where cursor is now */
5532 force = TRUE; /* force redraw of rest of the line */
5533 redraw_next = TRUE; /* or else next char would miss out */
5534
5535 /*
5536 * If the previous character was highlighted, need to stop
5537 * highlighting at this character.
5538 */
5539 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5540 {
5541 screen_attr = ScreenAttrs[off_to - 1];
5542 term_windgoto(row, col + coloff);
5543 screen_stop_highlight();
5544 }
5545 else
5546 screen_attr = 0; /* highlighting has stopped */
5547 }
5548#ifdef FEAT_MBYTE
5549 if (enc_dbcs != 0)
5550 {
5551 /* Check if overwriting a double-byte with a single-byte or
5552 * the other way around requires another character to be
5553 * redrawn. For UTF-8 this isn't needed, because comparing
5554 * ScreenLinesUC[] is sufficient. */
5555 if (char_cells == 1
5556 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005557 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
5559 /* Writing a single-cell character over a double-cell
5560 * character: need to redraw the next cell. */
5561 ScreenLines[off_to + 1] = 0;
5562 redraw_next = TRUE;
5563 }
5564 else if (char_cells == 2
5565 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005566 && (*mb_off2cells)(off_to, max_off_to) == 1
5567 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
5569 /* Writing the second half of a double-cell character over
5570 * a double-cell character: need to redraw the second
5571 * cell. */
5572 ScreenLines[off_to + 2] = 0;
5573 redraw_next = TRUE;
5574 }
5575
5576 if (enc_dbcs == DBCS_JPNU)
5577 ScreenLines2[off_to] = ScreenLines2[off_from];
5578 }
5579 /* When writing a single-width character over a double-width
5580 * character and at the end of the redrawn text, need to clear out
5581 * the right halve of the old character.
5582 * Also required when writing the right halve of a double-width
5583 * char over the left halve of an existing one. */
5584 if (has_mbyte && col + char_cells == endcol
5585 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005586 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005588 && (*mb_off2cells)(off_to, max_off_to) == 1
5589 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 clear_next = TRUE;
5591#endif
5592
5593 ScreenLines[off_to] = ScreenLines[off_from];
5594#ifdef FEAT_MBYTE
5595 if (enc_utf8)
5596 {
5597 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5598 if (ScreenLinesUC[off_from] != 0)
5599 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005600 int i;
5601
5602 for (i = 0; i < Screen_mco; ++i)
5603 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
5605 }
5606 if (char_cells == 2)
5607 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5608#endif
5609
5610#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005611 /* The bold trick makes a single column of pixels appear in the
5612 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 * character should be redrawn too. This happens for our own GUI
5614 * and for some xterms. */
5615 if (
5616# ifdef FEAT_GUI
5617 gui.in_use
5618# endif
5619# if defined(FEAT_GUI) && defined(UNIX)
5620 ||
5621# endif
5622# ifdef UNIX
5623 term_is_xterm
5624# endif
5625 )
5626 {
5627 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005628 if (hl > HL_ALL)
5629 hl = syn_attr2attr(hl);
5630 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 redraw_next = TRUE;
5632 }
5633#endif
5634 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5635#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005636 /* For simplicity set the attributes of second half of a
5637 * double-wide character equal to the first half. */
5638 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005640
5641 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 else
5644#endif
5645 screen_char(off_to, row, col + coloff);
5646 }
5647 else if ( p_wiv
5648#ifdef FEAT_GUI
5649 && !gui.in_use
5650#endif
5651 && col + coloff > 0)
5652 {
5653 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5654 {
5655 /*
5656 * Don't output stop-highlight when moving the cursor, it will
5657 * stop the highlighting when it should continue.
5658 */
5659 screen_attr = 0;
5660 }
5661 else if (screen_attr != 0)
5662 screen_stop_highlight();
5663 }
5664
5665 off_to += CHAR_CELLS;
5666 off_from += CHAR_CELLS;
5667 col += CHAR_CELLS;
5668 }
5669
5670#ifdef FEAT_MBYTE
5671 if (clear_next)
5672 {
5673 /* Clear the second half of a double-wide character of which the left
5674 * half was overwritten with a single-wide character. */
5675 ScreenLines[off_to] = ' ';
5676 if (enc_utf8)
5677 ScreenLinesUC[off_to] = 0;
5678 screen_char(off_to, row, col + coloff);
5679 }
5680#endif
5681
5682 if (clear_width > 0
5683#ifdef FEAT_RIGHTLEFT
5684 && !rlflag
5685#endif
5686 )
5687 {
5688#ifdef FEAT_GUI
5689 int startCol = col;
5690#endif
5691
5692 /* blank out the rest of the line */
5693 while (col < clear_width && ScreenLines[off_to] == ' '
5694 && ScreenAttrs[off_to] == 0
5695#ifdef FEAT_MBYTE
5696 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5697#endif
5698 )
5699 {
5700 ++off_to;
5701 ++col;
5702 }
5703 if (col < clear_width)
5704 {
5705#ifdef FEAT_GUI
5706 /*
5707 * In the GUI, clearing the rest of the line may leave pixels
5708 * behind if the first character cleared was bold. Some bold
5709 * fonts spill over the left. In this case we redraw the previous
5710 * character too. If we didn't skip any blanks above, then we
5711 * only redraw if the character wasn't already redrawn anyway.
5712 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005713 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
5715 hl = ScreenAttrs[off_to];
5716 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005717 {
5718 int prev_cells = 1;
5719# ifdef FEAT_MBYTE
5720 if (enc_utf8)
5721 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5722 * that its width is 2. */
5723 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5724 else if (enc_dbcs != 0)
5725 {
5726 /* find previous character by counting from first
5727 * column and get its width. */
5728 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005729 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005730
5731 while (off < off_to)
5732 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005733 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005734 off += prev_cells;
5735 }
5736 }
5737
5738 if (enc_dbcs != 0 && prev_cells > 1)
5739 screen_char_2(off_to - prev_cells, row,
5740 col + coloff - prev_cells);
5741 else
5742# endif
5743 screen_char(off_to - prev_cells, row,
5744 col + coloff - prev_cells);
5745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
5747#endif
5748 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5749 ' ', ' ', 0);
5750#ifdef FEAT_VERTSPLIT
5751 off_to += clear_width - col;
5752 col = clear_width;
5753#endif
5754 }
5755 }
5756
5757 if (clear_width > 0)
5758 {
5759#ifdef FEAT_VERTSPLIT
5760 /* For a window that's left of another, draw the separator char. */
5761 if (col + coloff < Columns)
5762 {
5763 int c;
5764
5765 c = fillchar_vsep(&hl);
5766 if (ScreenLines[off_to] != c
5767# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005768 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5769 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770# endif
5771 || ScreenAttrs[off_to] != hl)
5772 {
5773 ScreenLines[off_to] = c;
5774 ScreenAttrs[off_to] = hl;
5775# ifdef FEAT_MBYTE
5776 if (enc_utf8)
5777 {
5778 if (c >= 0x80)
5779 {
5780 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005781 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783 else
5784 ScreenLinesUC[off_to] = 0;
5785 }
5786# endif
5787 screen_char(off_to, row, col + coloff);
5788 }
5789 }
5790 else
5791#endif
5792 LineWraps[row] = FALSE;
5793 }
5794}
5795
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005796#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005798 * Mirror text "str" for right-left displaying.
5799 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005801 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802rl_mirror(str)
5803 char_u *str;
5804{
5805 char_u *p1, *p2;
5806 int t;
5807
5808 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5809 {
5810 t = *p1;
5811 *p1 = *p2;
5812 *p2 = t;
5813 }
5814}
5815#endif
5816
5817#if defined(FEAT_WINDOWS) || defined(PROTO)
5818/*
5819 * mark all status lines for redraw; used after first :cd
5820 */
5821 void
5822status_redraw_all()
5823{
5824 win_T *wp;
5825
5826 for (wp = firstwin; wp; wp = wp->w_next)
5827 if (wp->w_status_height)
5828 {
5829 wp->w_redr_status = TRUE;
5830 redraw_later(VALID);
5831 }
5832}
5833
5834/*
5835 * mark all status lines of the current buffer for redraw
5836 */
5837 void
5838status_redraw_curbuf()
5839{
5840 win_T *wp;
5841
5842 for (wp = firstwin; wp; wp = wp->w_next)
5843 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5844 {
5845 wp->w_redr_status = TRUE;
5846 redraw_later(VALID);
5847 }
5848}
5849
5850/*
5851 * Redraw all status lines that need to be redrawn.
5852 */
5853 void
5854redraw_statuslines()
5855{
5856 win_T *wp;
5857
5858 for (wp = firstwin; wp; wp = wp->w_next)
5859 if (wp->w_redr_status)
5860 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005861 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005862 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863}
5864#endif
5865
5866#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5867/*
5868 * Redraw all status lines at the bottom of frame "frp".
5869 */
5870 void
5871win_redraw_last_status(frp)
5872 frame_T *frp;
5873{
5874 if (frp->fr_layout == FR_LEAF)
5875 frp->fr_win->w_redr_status = TRUE;
5876 else if (frp->fr_layout == FR_ROW)
5877 {
5878 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5879 win_redraw_last_status(frp);
5880 }
5881 else /* frp->fr_layout == FR_COL */
5882 {
5883 frp = frp->fr_child;
5884 while (frp->fr_next != NULL)
5885 frp = frp->fr_next;
5886 win_redraw_last_status(frp);
5887 }
5888}
5889#endif
5890
5891#ifdef FEAT_VERTSPLIT
5892/*
5893 * Draw the verticap separator right of window "wp" starting with line "row".
5894 */
5895 static void
5896draw_vsep_win(wp, row)
5897 win_T *wp;
5898 int row;
5899{
5900 int hl;
5901 int c;
5902
5903 if (wp->w_vsep_width)
5904 {
5905 /* draw the vertical separator right of this window */
5906 c = fillchar_vsep(&hl);
5907 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5908 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5909 c, ' ', hl);
5910 }
5911}
5912#endif
5913
5914#ifdef FEAT_WILDMENU
5915static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005916static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005919 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 */
5921 static int
5922status_match_len(xp, s)
5923 expand_T *xp;
5924 char_u *s;
5925{
5926 int len = 0;
5927
5928#ifdef FEAT_MENU
5929 int emenu = (xp->xp_context == EXPAND_MENUS
5930 || xp->xp_context == EXPAND_MENUNAMES);
5931
5932 /* Check for menu separators - replace with '|'. */
5933 if (emenu && menu_is_separator(s))
5934 return 1;
5935#endif
5936
5937 while (*s != NUL)
5938 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005939 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005940 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005941 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 }
5943
5944 return len;
5945}
5946
5947/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005948 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005949 * These are backslashes used for escaping. Do show backslashes in help tags.
5950 */
5951 static int
5952skip_status_match_char(xp, s)
5953 expand_T *xp;
5954 char_u *s;
5955{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005956 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005957#ifdef FEAT_MENU
5958 || ((xp->xp_context == EXPAND_MENUS
5959 || xp->xp_context == EXPAND_MENUNAMES)
5960 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5961#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005962 )
5963 {
5964#ifndef BACKSLASH_IN_FILENAME
5965 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5966 return 2;
5967#endif
5968 return 1;
5969 }
5970 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005971}
5972
5973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 * Show wildchar matches in the status line.
5975 * Show at least the "match" item.
5976 * We start at item 'first_match' in the list and show all matches that fit.
5977 *
5978 * If inversion is possible we use it. Else '=' characters are used.
5979 */
5980 void
5981win_redr_status_matches(xp, num_matches, matches, match, showtail)
5982 expand_T *xp;
5983 int num_matches;
5984 char_u **matches; /* list of matches */
5985 int match;
5986 int showtail;
5987{
5988#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5989 int row;
5990 char_u *buf;
5991 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005992 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 int fillchar;
5994 int attr;
5995 int i;
5996 int highlight = TRUE;
5997 char_u *selstart = NULL;
5998 int selstart_col = 0;
5999 char_u *selend = NULL;
6000 static int first_match = 0;
6001 int add_left = FALSE;
6002 char_u *s;
6003#ifdef FEAT_MENU
6004 int emenu;
6005#endif
6006#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6007 int l;
6008#endif
6009
6010 if (matches == NULL) /* interrupted completion? */
6011 return;
6012
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006013#ifdef FEAT_MBYTE
6014 if (has_mbyte)
6015 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6016 else
6017#endif
6018 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (buf == NULL)
6020 return;
6021
6022 if (match == -1) /* don't show match but original text */
6023 {
6024 match = 0;
6025 highlight = FALSE;
6026 }
6027 /* count 1 for the ending ">" */
6028 clen = status_match_len(xp, L_MATCH(match)) + 3;
6029 if (match == 0)
6030 first_match = 0;
6031 else if (match < first_match)
6032 {
6033 /* jumping left, as far as we can go */
6034 first_match = match;
6035 add_left = TRUE;
6036 }
6037 else
6038 {
6039 /* check if match fits on the screen */
6040 for (i = first_match; i < match; ++i)
6041 clen += status_match_len(xp, L_MATCH(i)) + 2;
6042 if (first_match > 0)
6043 clen += 2;
6044 /* jumping right, put match at the left */
6045 if ((long)clen > Columns)
6046 {
6047 first_match = match;
6048 /* if showing the last match, we can add some on the left */
6049 clen = 2;
6050 for (i = match; i < num_matches; ++i)
6051 {
6052 clen += status_match_len(xp, L_MATCH(i)) + 2;
6053 if ((long)clen >= Columns)
6054 break;
6055 }
6056 if (i == num_matches)
6057 add_left = TRUE;
6058 }
6059 }
6060 if (add_left)
6061 while (first_match > 0)
6062 {
6063 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6064 if ((long)clen >= Columns)
6065 break;
6066 --first_match;
6067 }
6068
6069 fillchar = fillchar_status(&attr, TRUE);
6070
6071 if (first_match == 0)
6072 {
6073 *buf = NUL;
6074 len = 0;
6075 }
6076 else
6077 {
6078 STRCPY(buf, "< ");
6079 len = 2;
6080 }
6081 clen = len;
6082
6083 i = first_match;
6084 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6085 {
6086 if (i == match)
6087 {
6088 selstart = buf + len;
6089 selstart_col = clen;
6090 }
6091
6092 s = L_MATCH(i);
6093 /* Check for menu separators - replace with '|' */
6094#ifdef FEAT_MENU
6095 emenu = (xp->xp_context == EXPAND_MENUS
6096 || xp->xp_context == EXPAND_MENUNAMES);
6097 if (emenu && menu_is_separator(s))
6098 {
6099 STRCPY(buf + len, transchar('|'));
6100 l = (int)STRLEN(buf + len);
6101 len += l;
6102 clen += l;
6103 }
6104 else
6105#endif
6106 for ( ; *s != NUL; ++s)
6107 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006108 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 clen += ptr2cells(s);
6110#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006111 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
6113 STRNCPY(buf + len, s, l);
6114 s += l - 1;
6115 len += l;
6116 }
6117 else
6118#endif
6119 {
6120 STRCPY(buf + len, transchar_byte(*s));
6121 len += (int)STRLEN(buf + len);
6122 }
6123 }
6124 if (i == match)
6125 selend = buf + len;
6126
6127 *(buf + len++) = ' ';
6128 *(buf + len++) = ' ';
6129 clen += 2;
6130 if (++i == num_matches)
6131 break;
6132 }
6133
6134 if (i != num_matches)
6135 {
6136 *(buf + len++) = '>';
6137 ++clen;
6138 }
6139
6140 buf[len] = NUL;
6141
6142 row = cmdline_row - 1;
6143 if (row >= 0)
6144 {
6145 if (wild_menu_showing == 0)
6146 {
6147 if (msg_scrolled > 0)
6148 {
6149 /* Put the wildmenu just above the command line. If there is
6150 * no room, scroll the screen one line up. */
6151 if (cmdline_row == Rows - 1)
6152 {
6153 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6154 ++msg_scrolled;
6155 }
6156 else
6157 {
6158 ++cmdline_row;
6159 ++row;
6160 }
6161 wild_menu_showing = WM_SCROLLED;
6162 }
6163 else
6164 {
6165 /* Create status line if needed by setting 'laststatus' to 2.
6166 * Set 'winminheight' to zero to avoid that the window is
6167 * resized. */
6168 if (lastwin->w_status_height == 0)
6169 {
6170 save_p_ls = p_ls;
6171 save_p_wmh = p_wmh;
6172 p_ls = 2;
6173 p_wmh = 0;
6174 last_status(FALSE);
6175 }
6176 wild_menu_showing = WM_SHOWN;
6177 }
6178 }
6179
6180 screen_puts(buf, row, 0, attr);
6181 if (selstart != NULL && highlight)
6182 {
6183 *selend = NUL;
6184 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6185 }
6186
6187 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6188 }
6189
6190#ifdef FEAT_VERTSPLIT
6191 win_redraw_last_status(topframe);
6192#else
6193 lastwin->w_redr_status = TRUE;
6194#endif
6195 vim_free(buf);
6196}
6197#endif
6198
6199#if defined(FEAT_WINDOWS) || defined(PROTO)
6200/*
6201 * Redraw the status line of window wp.
6202 *
6203 * If inversion is possible we use it. Else '=' characters are used.
6204 */
6205 void
6206win_redr_status(wp)
6207 win_T *wp;
6208{
6209 int row;
6210 char_u *p;
6211 int len;
6212 int fillchar;
6213 int attr;
6214 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006215 static int busy = FALSE;
6216
6217 /* It's possible to get here recursively when 'statusline' (indirectly)
6218 * invokes ":redrawstatus". Simply ignore the call then. */
6219 if (busy)
6220 return;
6221 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
6223 wp->w_redr_status = FALSE;
6224 if (wp->w_status_height == 0)
6225 {
6226 /* no status line, can only be last window */
6227 redraw_cmdline = TRUE;
6228 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006229 else if (!redrawing()
6230#ifdef FEAT_INS_EXPAND
6231 /* don't update status line when popup menu is visible and may be
6232 * drawn over it */
6233 || pum_visible()
6234#endif
6235 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
6237 /* Don't redraw right now, do it later. */
6238 wp->w_redr_status = TRUE;
6239 }
6240#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006241 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 {
6243 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006244 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 }
6246#endif
6247 else
6248 {
6249 fillchar = fillchar_status(&attr, wp == curwin);
6250
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006251 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 p = NameBuff;
6253 len = (int)STRLEN(p);
6254
6255 if (wp->w_buffer->b_help
6256#ifdef FEAT_QUICKFIX
6257 || wp->w_p_pvw
6258#endif
6259 || bufIsChanged(wp->w_buffer)
6260 || wp->w_buffer->b_p_ro)
6261 *(p + len++) = ' ';
6262 if (wp->w_buffer->b_help)
6263 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006264 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 len += (int)STRLEN(p + len);
6266 }
6267#ifdef FEAT_QUICKFIX
6268 if (wp->w_p_pvw)
6269 {
6270 STRCPY(p + len, _("[Preview]"));
6271 len += (int)STRLEN(p + len);
6272 }
6273#endif
6274 if (bufIsChanged(wp->w_buffer))
6275 {
6276 STRCPY(p + len, "[+]");
6277 len += 3;
6278 }
6279 if (wp->w_buffer->b_p_ro)
6280 {
6281 STRCPY(p + len, "[RO]");
6282 len += 4;
6283 }
6284
6285#ifndef FEAT_VERTSPLIT
6286 this_ru_col = ru_col;
6287 if (this_ru_col < (Columns + 1) / 2)
6288 this_ru_col = (Columns + 1) / 2;
6289#else
6290 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6291 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6292 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6293 if (this_ru_col <= 1)
6294 {
6295 p = (char_u *)"<"; /* No room for file name! */
6296 len = 1;
6297 }
6298 else
6299#endif
6300#ifdef FEAT_MBYTE
6301 if (has_mbyte)
6302 {
6303 int clen = 0, i;
6304
6305 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006306 clen = mb_string2cells(p, -1);
6307
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 /* Find first character that will fit.
6309 * Going from start to end is much faster for DBCS. */
6310 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006311 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 clen -= (*mb_ptr2cells)(p + i);
6313 len = clen;
6314 if (i > 0)
6315 {
6316 p = p + i - 1;
6317 *p = '<';
6318 ++len;
6319 }
6320
6321 }
6322 else
6323#endif
6324 if (len > this_ru_col - 1)
6325 {
6326 p += len - (this_ru_col - 1);
6327 *p = '<';
6328 len = this_ru_col - 1;
6329 }
6330
6331 row = W_WINROW(wp) + wp->w_height;
6332 screen_puts(p, row, W_WINCOL(wp), attr);
6333 screen_fill(row, row + 1, len + W_WINCOL(wp),
6334 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6335
6336 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6337 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6338 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6339 - 1 + W_WINCOL(wp)), attr);
6340
6341#ifdef FEAT_CMDL_INFO
6342 win_redr_ruler(wp, TRUE);
6343#endif
6344 }
6345
6346#ifdef FEAT_VERTSPLIT
6347 /*
6348 * May need to draw the character below the vertical separator.
6349 */
6350 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6351 {
6352 if (stl_connected(wp))
6353 fillchar = fillchar_status(&attr, wp == curwin);
6354 else
6355 fillchar = fillchar_vsep(&attr);
6356 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6357 attr);
6358 }
6359#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006360 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361}
6362
Bram Moolenaar238a5642006-02-21 22:12:05 +00006363#ifdef FEAT_STL_OPT
6364/*
6365 * Redraw the status line according to 'statusline' and take care of any
6366 * errors encountered.
6367 */
6368 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006369redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006370 win_T *wp;
6371{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006372 static int entered = FALSE;
6373 int save_called_emsg = called_emsg;
6374
6375 /* When called recursively return. This can happen when the statusline
6376 * contains an expression that triggers a redraw. */
6377 if (entered)
6378 return;
6379 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006380
6381 called_emsg = FALSE;
6382 win_redr_custom(wp, FALSE);
6383 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006384 {
6385 /* When there is an error disable the statusline, otherwise the
6386 * display is messed up with errors and a redraw triggers the problem
6387 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006388 set_string_option_direct((char_u *)"statusline", -1,
6389 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006390 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006391 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006392 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006393 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006394}
6395#endif
6396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397# ifdef FEAT_VERTSPLIT
6398/*
6399 * Return TRUE if the status line of window "wp" is connected to the status
6400 * line of the window right of it. If not, then it's a vertical separator.
6401 * Only call if (wp->w_vsep_width != 0).
6402 */
6403 int
6404stl_connected(wp)
6405 win_T *wp;
6406{
6407 frame_T *fr;
6408
6409 fr = wp->w_frame;
6410 while (fr->fr_parent != NULL)
6411 {
6412 if (fr->fr_parent->fr_layout == FR_COL)
6413 {
6414 if (fr->fr_next != NULL)
6415 break;
6416 }
6417 else
6418 {
6419 if (fr->fr_next != NULL)
6420 return TRUE;
6421 }
6422 fr = fr->fr_parent;
6423 }
6424 return FALSE;
6425}
6426# endif
6427
6428#endif /* FEAT_WINDOWS */
6429
6430#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6431/*
6432 * Get the value to show for the language mappings, active 'keymap'.
6433 */
6434 int
6435get_keymap_str(wp, buf, len)
6436 win_T *wp;
6437 char_u *buf; /* buffer for the result */
6438 int len; /* length of buffer */
6439{
6440 char_u *p;
6441
6442 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6443 return FALSE;
6444
6445 {
6446#ifdef FEAT_EVAL
6447 buf_T *old_curbuf = curbuf;
6448 win_T *old_curwin = curwin;
6449 char_u *s;
6450
6451 curbuf = wp->w_buffer;
6452 curwin = wp;
6453 STRCPY(buf, "b:keymap_name"); /* must be writable */
6454 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006455 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 --emsg_skip;
6457 curbuf = old_curbuf;
6458 curwin = old_curwin;
6459 if (p == NULL || *p == NUL)
6460#endif
6461 {
6462#ifdef FEAT_KEYMAP
6463 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6464 p = wp->w_buffer->b_p_keymap;
6465 else
6466#endif
6467 p = (char_u *)"lang";
6468 }
6469 if ((int)(STRLEN(p) + 3) < len)
6470 sprintf((char *)buf, "<%s>", p);
6471 else
6472 buf[0] = NUL;
6473#ifdef FEAT_EVAL
6474 vim_free(s);
6475#endif
6476 }
6477 return buf[0] != NUL;
6478}
6479#endif
6480
6481#if defined(FEAT_STL_OPT) || defined(PROTO)
6482/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006483 * Redraw the status line or ruler of window "wp".
6484 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 */
6486 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006487win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006489 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491 int attr;
6492 int curattr;
6493 int row;
6494 int col = 0;
6495 int maxwidth;
6496 int width;
6497 int n;
6498 int len;
6499 int fillchar;
6500 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006501 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006503 struct stl_hlrec hltab[STL_MAX_ITEM];
6504 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006505 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006506 win_T *ewp;
6507 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
6509 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006510 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006512 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006513 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006514 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006515 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006516 attr = hl_attr(HLF_TPF);
6517 maxwidth = Columns;
6518# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006519 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006522 else
6523 {
6524 row = W_WINROW(wp) + wp->w_height;
6525 fillchar = fillchar_status(&attr, wp == curwin);
6526 maxwidth = W_WIDTH(wp);
6527
6528 if (draw_ruler)
6529 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006530 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006531 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006532 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006533 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006534 if (*++stl == '-')
6535 stl++;
6536 if (atoi((char *)stl))
6537 while (VIM_ISDIGIT(*stl))
6538 stl++;
6539 if (*stl++ != '(')
6540 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006541 }
6542#ifdef FEAT_VERTSPLIT
6543 col = ru_col - (Columns - W_WIDTH(wp));
6544 if (col < (W_WIDTH(wp) + 1) / 2)
6545 col = (W_WIDTH(wp) + 1) / 2;
6546#else
6547 col = ru_col;
6548 if (col > (Columns + 1) / 2)
6549 col = (Columns + 1) / 2;
6550#endif
6551 maxwidth = W_WIDTH(wp) - col;
6552#ifdef FEAT_WINDOWS
6553 if (!wp->w_status_height)
6554#endif
6555 {
6556 row = Rows - 1;
6557 --maxwidth; /* writing in last column may cause scrolling */
6558 fillchar = ' ';
6559 attr = 0;
6560 }
6561
6562# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006563 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006564# endif
6565 }
6566 else
6567 {
6568 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006569 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006570 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006571 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006572# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006573 use_sandbox = was_set_insecurely((char_u *)"statusline",
6574 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006575# endif
6576 }
6577
6578#ifdef FEAT_VERTSPLIT
6579 col += W_WINCOL(wp);
6580#endif
6581 }
6582
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 if (maxwidth <= 0)
6584 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
Bram Moolenaar61452852011-02-01 18:01:11 +01006586 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6587 * the cursor away and back. */
6588 ewp = wp == NULL ? curwin : wp;
6589 p_crb_save = ewp->w_p_crb;
6590 ewp->w_p_crb = FALSE;
6591
Bram Moolenaar362f3562009-11-03 16:20:34 +00006592 /* Make a copy, because the statusline may include a function call that
6593 * might change the option value and free the memory. */
6594 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006595 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006596 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006597 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006598 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006599 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006601 /* Make all characters printable. */
6602 p = transstr(buf);
6603 if (p != NULL)
6604 {
6605 vim_strncpy(buf, p, sizeof(buf) - 1);
6606 vim_free(p);
6607 }
6608
6609 /* fill up with "fillchar" */
6610 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006611 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 {
6613#ifdef FEAT_MBYTE
6614 len += (*mb_char2bytes)(fillchar, buf + len);
6615#else
6616 buf[len++] = fillchar;
6617#endif
6618 ++width;
6619 }
6620 buf[len] = NUL;
6621
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006622 /*
6623 * Draw each snippet with the specified highlighting.
6624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 curattr = attr;
6626 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006627 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006629 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 screen_puts_len(p, len, row, col, curattr);
6631 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006632 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006634 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006636 else if (hltab[n].userhl < 0)
6637 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006639 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006640 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641#endif
6642 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006643 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006646
6647 if (wp == NULL)
6648 {
6649 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6650 col = 0;
6651 len = 0;
6652 p = buf;
6653 fillchar = 0;
6654 for (n = 0; tabtab[n].start != NULL; n++)
6655 {
6656 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6657 while (col < len)
6658 TabPageIdxs[col++] = fillchar;
6659 p = tabtab[n].start;
6660 fillchar = tabtab[n].userhl;
6661 }
6662 while (col < Columns)
6663 TabPageIdxs[col++] = fillchar;
6664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665}
6666
6667#endif /* FEAT_STL_OPT */
6668
6669/*
6670 * Output a single character directly to the screen and update ScreenLines.
6671 */
6672 void
6673screen_putchar(c, row, col, attr)
6674 int c;
6675 int row, col;
6676 int attr;
6677{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 char_u buf[MB_MAXBYTES + 1];
6679
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006680#ifdef FEAT_MBYTE
6681 if (has_mbyte)
6682 buf[(*mb_char2bytes)(c, buf)] = NUL;
6683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006685 {
6686 buf[0] = c;
6687 buf[1] = NUL;
6688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 screen_puts(buf, row, col, attr);
6690}
6691
6692/*
6693 * Get a single character directly from ScreenLines into "bytes[]".
6694 * Also return its attribute in *attrp;
6695 */
6696 void
6697screen_getbytes(row, col, bytes, attrp)
6698 int row, col;
6699 char_u *bytes;
6700 int *attrp;
6701{
6702 unsigned off;
6703
6704 /* safety check */
6705 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6706 {
6707 off = LineOffset[row] + col;
6708 *attrp = ScreenAttrs[off];
6709 bytes[0] = ScreenLines[off];
6710 bytes[1] = NUL;
6711
6712#ifdef FEAT_MBYTE
6713 if (enc_utf8 && ScreenLinesUC[off] != 0)
6714 bytes[utfc_char2bytes(off, bytes)] = NUL;
6715 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6716 {
6717 bytes[0] = ScreenLines[off];
6718 bytes[1] = ScreenLines2[off];
6719 bytes[2] = NUL;
6720 }
6721 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6722 {
6723 bytes[1] = ScreenLines[off + 1];
6724 bytes[2] = NUL;
6725 }
6726#endif
6727 }
6728}
6729
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006730#ifdef FEAT_MBYTE
6731static int screen_comp_differs __ARGS((int, int*));
6732
6733/*
6734 * Return TRUE if composing characters for screen posn "off" differs from
6735 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006736 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006737 */
6738 static int
6739screen_comp_differs(off, u8cc)
6740 int off;
6741 int *u8cc;
6742{
6743 int i;
6744
6745 for (i = 0; i < Screen_mco; ++i)
6746 {
6747 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6748 return TRUE;
6749 if (u8cc[i] == 0)
6750 break;
6751 }
6752 return FALSE;
6753}
6754#endif
6755
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756/*
6757 * Put string '*text' on the screen at position 'row' and 'col', with
6758 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6759 * Note: only outputs within one row, message is truncated at screen boundary!
6760 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6761 */
6762 void
6763screen_puts(text, row, col, attr)
6764 char_u *text;
6765 int row;
6766 int col;
6767 int attr;
6768{
6769 screen_puts_len(text, -1, row, col, attr);
6770}
6771
6772/*
6773 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6774 * a NUL.
6775 */
6776 void
6777screen_puts_len(text, len, row, col, attr)
6778 char_u *text;
6779 int len;
6780 int row;
6781 int col;
6782 int attr;
6783{
6784 unsigned off;
6785 char_u *ptr = text;
6786 int c;
6787#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006788 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 int mbyte_blen = 1;
6790 int mbyte_cells = 1;
6791 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006792 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 int clear_next_cell = FALSE;
6794# ifdef FEAT_ARABIC
6795 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006796 int pc, nc, nc1;
6797 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798# endif
6799#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006800#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6801 int force_redraw_this;
6802 int force_redraw_next = FALSE;
6803#endif
6804 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6807 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006808 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
Bram Moolenaarc236c162008-07-13 17:41:49 +00006810#ifdef FEAT_MBYTE
6811 /* When drawing over the right halve of a double-wide char clear out the
6812 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006813 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006814# ifdef FEAT_GUI
6815 && !gui.in_use
6816# endif
6817 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006818 {
6819 ScreenLines[off - 1] = ' ';
6820 ScreenAttrs[off - 1] = 0;
6821 if (enc_utf8)
6822 {
6823 ScreenLinesUC[off - 1] = 0;
6824 ScreenLinesC[0][off - 1] = 0;
6825 }
6826 /* redraw the previous cell, make it empty */
6827 screen_char(off - 1, row, col - 1);
6828 /* force the cell at "col" to be redrawn */
6829 force_redraw_next = TRUE;
6830 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006831#endif
6832
Bram Moolenaar367329b2007-08-30 11:53:22 +00006833#ifdef FEAT_MBYTE
6834 max_off = LineOffset[row] + screen_Columns;
6835#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006836 while (col < screen_Columns
6837 && (len < 0 || (int)(ptr - text) < len)
6838 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {
6840 c = *ptr;
6841#ifdef FEAT_MBYTE
6842 /* check if this is the first byte of a multibyte */
6843 if (has_mbyte)
6844 {
6845 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006846 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006848 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6850 mbyte_cells = 1;
6851 else if (enc_dbcs != 0)
6852 mbyte_cells = mbyte_blen;
6853 else /* enc_utf8 */
6854 {
6855 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006856 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 (int)((text + len) - ptr));
6858 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006859 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006861# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 /* Non-BMP character: display as ? or fullwidth ?. */
6863 if (u8c >= 0x10000)
6864 {
6865 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6866 if (attr == 0)
6867 attr = hl_attr(HLF_8);
6868 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870# ifdef FEAT_ARABIC
6871 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6872 {
6873 /* Do Arabic shaping. */
6874 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6875 {
6876 /* Past end of string to be displayed. */
6877 nc = NUL;
6878 nc1 = NUL;
6879 }
6880 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006881 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006882 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6883 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006884 nc1 = pcc[0];
6885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 pc = prev_c;
6887 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006888 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890 else
6891 prev_c = u8c;
6892# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006893 if (col + mbyte_cells > screen_Columns)
6894 {
6895 /* Only 1 cell left, but character requires 2 cells:
6896 * display a '>' in the last column to avoid wrapping. */
6897 c = '>';
6898 mbyte_cells = 1;
6899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 }
6901 }
6902#endif
6903
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006904#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6905 force_redraw_this = force_redraw_next;
6906 force_redraw_next = FALSE;
6907#endif
6908
6909 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910#ifdef FEAT_MBYTE
6911 || (mbyte_cells == 2
6912 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6913 || (enc_dbcs == DBCS_JPNU
6914 && c == 0x8e
6915 && ScreenLines2[off] != ptr[1])
6916 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006917 && (ScreenLinesUC[off] !=
6918 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6919 || (ScreenLinesUC[off] != 0
6920 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921#endif
6922 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006923 || exmode_active;
6924
6925 if (need_redraw
6926#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6927 || force_redraw_this
6928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 )
6930 {
6931#if defined(FEAT_GUI) || defined(UNIX)
6932 /* The bold trick makes a single row of pixels appear in the next
6933 * character. When a bold character is removed, the next
6934 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006935 * and for some xterms. */
6936 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937# ifdef FEAT_GUI
6938 gui.in_use
6939# endif
6940# if defined(FEAT_GUI) && defined(UNIX)
6941 ||
6942# endif
6943# ifdef UNIX
6944 term_is_xterm
6945# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006946 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006948 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006950 if (n > HL_ALL)
6951 n = syn_attr2attr(n);
6952 if (n & HL_BOLD)
6953 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
6955#endif
6956#ifdef FEAT_MBYTE
6957 /* When at the end of the text and overwriting a two-cell
6958 * character with a one-cell character, need to clear the next
6959 * cell. Also when overwriting the left halve of a two-cell char
6960 * with the right halve of a two-cell char. Do this only once
6961 * (mb_off2cells() may return 2 on the right halve). */
6962 if (clear_next_cell)
6963 clear_next_cell = FALSE;
6964 else if (has_mbyte
6965 && (len < 0 ? ptr[mbyte_blen] == NUL
6966 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006967 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006969 && (*mb_off2cells)(off, max_off) == 1
6970 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 clear_next_cell = TRUE;
6972
6973 /* Make sure we never leave a second byte of a double-byte behind,
6974 * it confuses mb_off2cells(). */
6975 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006976 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006978 && (*mb_off2cells)(off, max_off) == 1
6979 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 ScreenLines[off + mbyte_blen] = 0;
6981#endif
6982 ScreenLines[off] = c;
6983 ScreenAttrs[off] = attr;
6984#ifdef FEAT_MBYTE
6985 if (enc_utf8)
6986 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006987 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 ScreenLinesUC[off] = 0;
6989 else
6990 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006991 int i;
6992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006994 for (i = 0; i < Screen_mco; ++i)
6995 {
6996 ScreenLinesC[i][off] = u8cc[i];
6997 if (u8cc[i] == 0)
6998 break;
6999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 }
7001 if (mbyte_cells == 2)
7002 {
7003 ScreenLines[off + 1] = 0;
7004 ScreenAttrs[off + 1] = attr;
7005 }
7006 screen_char(off, row, col);
7007 }
7008 else if (mbyte_cells == 2)
7009 {
7010 ScreenLines[off + 1] = ptr[1];
7011 ScreenAttrs[off + 1] = attr;
7012 screen_char_2(off, row, col);
7013 }
7014 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7015 {
7016 ScreenLines2[off] = ptr[1];
7017 screen_char(off, row, col);
7018 }
7019 else
7020#endif
7021 screen_char(off, row, col);
7022 }
7023#ifdef FEAT_MBYTE
7024 if (has_mbyte)
7025 {
7026 off += mbyte_cells;
7027 col += mbyte_cells;
7028 ptr += mbyte_blen;
7029 if (clear_next_cell)
7030 ptr = (char_u *)" ";
7031 }
7032 else
7033#endif
7034 {
7035 ++off;
7036 ++col;
7037 ++ptr;
7038 }
7039 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007040
7041#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7042 /* If we detected the next character needs to be redrawn, but the text
7043 * doesn't extend up to there, update the character here. */
7044 if (force_redraw_next && col < screen_Columns)
7045 {
7046# ifdef FEAT_MBYTE
7047 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7048 screen_char_2(off, row, col);
7049 else
7050# endif
7051 screen_char(off, row, col);
7052 }
7053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
7056#ifdef FEAT_SEARCH_EXTRA
7057/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007058 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 */
7060 static void
7061start_search_hl()
7062{
7063 if (p_hls && !no_hlsearch)
7064 {
7065 last_pat_prog(&search_hl.rm);
7066 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007067# ifdef FEAT_RELTIME
7068 /* Set the time limit to 'redrawtime'. */
7069 profile_setlimit(p_rdt, &search_hl.tm);
7070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 }
7072}
7073
7074/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007075 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 */
7077 static void
7078end_search_hl()
7079{
7080 if (search_hl.rm.regprog != NULL)
7081 {
7082 vim_free(search_hl.rm.regprog);
7083 search_hl.rm.regprog = NULL;
7084 }
7085}
7086
7087/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007088 * Init for calling prepare_search_hl().
7089 */
7090 static void
7091init_search_hl(wp)
7092 win_T *wp;
7093{
7094 matchitem_T *cur;
7095
7096 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7097 * match */
7098 cur = wp->w_match_head;
7099 while (cur != NULL)
7100 {
7101 cur->hl.rm = cur->match;
7102 if (cur->hlg_id == 0)
7103 cur->hl.attr = 0;
7104 else
7105 cur->hl.attr = syn_id2attr(cur->hlg_id);
7106 cur->hl.buf = wp->w_buffer;
7107 cur->hl.lnum = 0;
7108 cur->hl.first_lnum = 0;
7109# ifdef FEAT_RELTIME
7110 /* Set the time limit to 'redrawtime'. */
7111 profile_setlimit(p_rdt, &(cur->hl.tm));
7112# endif
7113 cur = cur->next;
7114 }
7115 search_hl.buf = wp->w_buffer;
7116 search_hl.lnum = 0;
7117 search_hl.first_lnum = 0;
7118 /* time limit is set at the toplevel, for all windows */
7119}
7120
7121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 * Advance to the match in window "wp" line "lnum" or past it.
7123 */
7124 static void
7125prepare_search_hl(wp, lnum)
7126 win_T *wp;
7127 linenr_T lnum;
7128{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007129 matchitem_T *cur; /* points to the match list */
7130 match_T *shl; /* points to search_hl or a match */
7131 int shl_flag; /* flag to indicate whether search_hl
7132 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 int n;
7134
7135 /*
7136 * When using a multi-line pattern, start searching at the top
7137 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007138 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007140 cur = wp->w_match_head;
7141 shl_flag = FALSE;
7142 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007144 if (shl_flag == FALSE)
7145 {
7146 shl = &search_hl;
7147 shl_flag = TRUE;
7148 }
7149 else
7150 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 if (shl->rm.regprog != NULL
7152 && shl->lnum == 0
7153 && re_multiline(shl->rm.regprog))
7154 {
7155 if (shl->first_lnum == 0)
7156 {
7157# ifdef FEAT_FOLDING
7158 for (shl->first_lnum = lnum;
7159 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7160 if (hasFoldingWin(wp, shl->first_lnum - 1,
7161 NULL, NULL, TRUE, NULL))
7162 break;
7163# else
7164 shl->first_lnum = wp->w_topline;
7165# endif
7166 }
7167 n = 0;
7168 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7169 {
7170 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7171 if (shl->lnum != 0)
7172 {
7173 shl->first_lnum = shl->lnum
7174 + shl->rm.endpos[0].lnum
7175 - shl->rm.startpos[0].lnum;
7176 n = shl->rm.endpos[0].col;
7177 }
7178 else
7179 {
7180 ++shl->first_lnum;
7181 n = 0;
7182 }
7183 }
7184 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007185 if (shl != &search_hl && cur != NULL)
7186 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
7188}
7189
7190/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007191 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 * Uses shl->buf.
7193 * Sets shl->lnum and shl->rm contents.
7194 * Note: Assumes a previous match is always before "lnum", unless
7195 * shl->lnum is zero.
7196 * Careful: Any pointers for buffer lines will become invalid.
7197 */
7198 static void
7199next_search_hl(win, shl, lnum, mincol)
7200 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007201 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 linenr_T lnum;
7203 colnr_T mincol; /* minimal column for a match */
7204{
7205 linenr_T l;
7206 colnr_T matchcol;
7207 long nmatched;
7208
7209 if (shl->lnum != 0)
7210 {
7211 /* Check for three situations:
7212 * 1. If the "lnum" is below a previous match, start a new search.
7213 * 2. If the previous match includes "mincol", use it.
7214 * 3. Continue after the previous match.
7215 */
7216 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7217 if (lnum > l)
7218 shl->lnum = 0;
7219 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7220 return;
7221 }
7222
7223 /*
7224 * Repeat searching for a match until one is found that includes "mincol"
7225 * or none is found in this line.
7226 */
7227 called_emsg = FALSE;
7228 for (;;)
7229 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007230#ifdef FEAT_RELTIME
7231 /* Stop searching after passing the time limit. */
7232 if (profile_passed_limit(&(shl->tm)))
7233 {
7234 shl->lnum = 0; /* no match found in time */
7235 break;
7236 }
7237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 /* Three situations:
7239 * 1. No useful previous match: search from start of line.
7240 * 2. Not Vi compatible or empty match: continue at next character.
7241 * Break the loop if this is beyond the end of the line.
7242 * 3. Vi compatible searching: continue at end of previous match.
7243 */
7244 if (shl->lnum == 0)
7245 matchcol = 0;
7246 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7247 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007248 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007250 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007251
7252 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007253 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007254 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007256 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 shl->lnum = 0;
7258 break;
7259 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007260#ifdef FEAT_MBYTE
7261 if (has_mbyte)
7262 matchcol += mb_ptr2len(ml);
7263 else
7264#endif
7265 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267 else
7268 matchcol = shl->rm.endpos[0].col;
7269
7270 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007271 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7272#ifdef FEAT_RELTIME
7273 &(shl->tm)
7274#else
7275 NULL
7276#endif
7277 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007278 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 {
7280 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007281 if (shl == &search_hl)
7282 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007283 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007284 vim_free(shl->rm.regprog);
7285 no_hlsearch = TRUE;
7286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007288 shl->lnum = 0;
7289 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 break;
7291 }
7292 if (nmatched == 0)
7293 {
7294 shl->lnum = 0; /* no match found */
7295 break;
7296 }
7297 if (shl->rm.startpos[0].lnum > 0
7298 || shl->rm.startpos[0].col >= mincol
7299 || nmatched > 1
7300 || shl->rm.endpos[0].col > mincol)
7301 {
7302 shl->lnum += shl->rm.startpos[0].lnum;
7303 break; /* useful match found */
7304 }
7305 }
7306}
7307#endif
7308
7309 static void
7310screen_start_highlight(attr)
7311 int attr;
7312{
7313 attrentry_T *aep = NULL;
7314
7315 screen_attr = attr;
7316 if (full_screen
7317#ifdef WIN3264
7318 && termcap_active
7319#endif
7320 )
7321 {
7322#ifdef FEAT_GUI
7323 if (gui.in_use)
7324 {
7325 char buf[20];
7326
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007327 /* The GUI handles this internally. */
7328 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 OUT_STR(buf);
7330 }
7331 else
7332#endif
7333 {
7334 if (attr > HL_ALL) /* special HL attr. */
7335 {
7336 if (t_colors > 1)
7337 aep = syn_cterm_attr2entry(attr);
7338 else
7339 aep = syn_term_attr2entry(attr);
7340 if (aep == NULL) /* did ":syntax clear" */
7341 attr = 0;
7342 else
7343 attr = aep->ae_attr;
7344 }
7345 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7346 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007347 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7348 && cterm_normal_fg_bold)
7349 /* If the Normal FG color has BOLD attribute and the new HL
7350 * has a FG color defined, clear BOLD. */
7351 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7353 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007354 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7355 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 out_str(T_US);
7357 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7358 out_str(T_CZH);
7359 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7360 out_str(T_MR);
7361
7362 /*
7363 * Output the color or start string after bold etc., in case the
7364 * bold etc. override the color setting.
7365 */
7366 if (aep != NULL)
7367 {
7368 if (t_colors > 1)
7369 {
7370 if (aep->ae_u.cterm.fg_color)
7371 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7372 if (aep->ae_u.cterm.bg_color)
7373 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7374 }
7375 else
7376 {
7377 if (aep->ae_u.term.start != NULL)
7378 out_str(aep->ae_u.term.start);
7379 }
7380 }
7381 }
7382 }
7383}
7384
7385 void
7386screen_stop_highlight()
7387{
7388 int do_ME = FALSE; /* output T_ME code */
7389
7390 if (screen_attr != 0
7391#ifdef WIN3264
7392 && termcap_active
7393#endif
7394 )
7395 {
7396#ifdef FEAT_GUI
7397 if (gui.in_use)
7398 {
7399 char buf[20];
7400
7401 /* use internal GUI code */
7402 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7403 OUT_STR(buf);
7404 }
7405 else
7406#endif
7407 {
7408 if (screen_attr > HL_ALL) /* special HL attr. */
7409 {
7410 attrentry_T *aep;
7411
7412 if (t_colors > 1)
7413 {
7414 /*
7415 * Assume that t_me restores the original colors!
7416 */
7417 aep = syn_cterm_attr2entry(screen_attr);
7418 if (aep != NULL && (aep->ae_u.cterm.fg_color
7419 || aep->ae_u.cterm.bg_color))
7420 do_ME = TRUE;
7421 }
7422 else
7423 {
7424 aep = syn_term_attr2entry(screen_attr);
7425 if (aep != NULL && aep->ae_u.term.stop != NULL)
7426 {
7427 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7428 do_ME = TRUE;
7429 else
7430 out_str(aep->ae_u.term.stop);
7431 }
7432 }
7433 if (aep == NULL) /* did ":syntax clear" */
7434 screen_attr = 0;
7435 else
7436 screen_attr = aep->ae_attr;
7437 }
7438
7439 /*
7440 * Often all ending-codes are equal to T_ME. Avoid outputting the
7441 * same sequence several times.
7442 */
7443 if (screen_attr & HL_STANDOUT)
7444 {
7445 if (STRCMP(T_SE, T_ME) == 0)
7446 do_ME = TRUE;
7447 else
7448 out_str(T_SE);
7449 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007450 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 {
7452 if (STRCMP(T_UE, T_ME) == 0)
7453 do_ME = TRUE;
7454 else
7455 out_str(T_UE);
7456 }
7457 if (screen_attr & HL_ITALIC)
7458 {
7459 if (STRCMP(T_CZR, T_ME) == 0)
7460 do_ME = TRUE;
7461 else
7462 out_str(T_CZR);
7463 }
7464 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7465 out_str(T_ME);
7466
7467 if (t_colors > 1)
7468 {
7469 /* set Normal cterm colors */
7470 if (cterm_normal_fg_color != 0)
7471 term_fg_color(cterm_normal_fg_color - 1);
7472 if (cterm_normal_bg_color != 0)
7473 term_bg_color(cterm_normal_bg_color - 1);
7474 if (cterm_normal_fg_bold)
7475 out_str(T_MD);
7476 }
7477 }
7478 }
7479 screen_attr = 0;
7480}
7481
7482/*
7483 * Reset the colors for a cterm. Used when leaving Vim.
7484 * The machine specific code may override this again.
7485 */
7486 void
7487reset_cterm_colors()
7488{
7489 if (t_colors > 1)
7490 {
7491 /* set Normal cterm colors */
7492 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7493 {
7494 out_str(T_OP);
7495 screen_attr = -1;
7496 }
7497 if (cterm_normal_fg_bold)
7498 {
7499 out_str(T_ME);
7500 screen_attr = -1;
7501 }
7502 }
7503}
7504
7505/*
7506 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7507 * using the attributes from ScreenAttrs["off"].
7508 */
7509 static void
7510screen_char(off, row, col)
7511 unsigned off;
7512 int row;
7513 int col;
7514{
7515 int attr;
7516
7517 /* Check for illegal values, just in case (could happen just after
7518 * resizing). */
7519 if (row >= screen_Rows || col >= screen_Columns)
7520 return;
7521
7522 /* Outputting the last character on the screen may scrollup the screen.
7523 * Don't to it! Mark the character invalid (update it when scrolled up) */
7524 if (row == screen_Rows - 1 && col == screen_Columns - 1
7525#ifdef FEAT_RIGHTLEFT
7526 /* account for first command-line character in rightleft mode */
7527 && !cmdmsg_rl
7528#endif
7529 )
7530 {
7531 ScreenAttrs[off] = (sattr_T)-1;
7532 return;
7533 }
7534
7535 /*
7536 * Stop highlighting first, so it's easier to move the cursor.
7537 */
7538#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7539 if (screen_char_attr != 0)
7540 attr = screen_char_attr;
7541 else
7542#endif
7543 attr = ScreenAttrs[off];
7544 if (screen_attr != attr)
7545 screen_stop_highlight();
7546
7547 windgoto(row, col);
7548
7549 if (screen_attr != attr)
7550 screen_start_highlight(attr);
7551
7552#ifdef FEAT_MBYTE
7553 if (enc_utf8 && ScreenLinesUC[off] != 0)
7554 {
7555 char_u buf[MB_MAXBYTES + 1];
7556
7557 /* Convert UTF-8 character to bytes and write it. */
7558
7559 buf[utfc_char2bytes(off, buf)] = NUL;
7560
7561 out_str(buf);
7562 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7563 ++screen_cur_col;
7564 }
7565 else
7566#endif
7567 {
7568#ifdef FEAT_MBYTE
7569 out_flush_check();
7570#endif
7571 out_char(ScreenLines[off]);
7572#ifdef FEAT_MBYTE
7573 /* double-byte character in single-width cell */
7574 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7575 out_char(ScreenLines2[off]);
7576#endif
7577 }
7578
7579 screen_cur_col++;
7580}
7581
7582#ifdef FEAT_MBYTE
7583
7584/*
7585 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7586 * on the screen at position 'row' and 'col'.
7587 * The attributes of the first byte is used for all. This is required to
7588 * output the two bytes of a double-byte character with nothing in between.
7589 */
7590 static void
7591screen_char_2(off, row, col)
7592 unsigned off;
7593 int row;
7594 int col;
7595{
7596 /* Check for illegal values (could be wrong when screen was resized). */
7597 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7598 return;
7599
7600 /* Outputting the last character on the screen may scrollup the screen.
7601 * Don't to it! Mark the character invalid (update it when scrolled up) */
7602 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7603 {
7604 ScreenAttrs[off] = (sattr_T)-1;
7605 return;
7606 }
7607
7608 /* Output the first byte normally (positions the cursor), then write the
7609 * second byte directly. */
7610 screen_char(off, row, col);
7611 out_char(ScreenLines[off + 1]);
7612 ++screen_cur_col;
7613}
7614#endif
7615
7616#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7617/*
7618 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7619 * This uses the contents of ScreenLines[] and doesn't change it.
7620 */
7621 void
7622screen_draw_rectangle(row, col, height, width, invert)
7623 int row;
7624 int col;
7625 int height;
7626 int width;
7627 int invert;
7628{
7629 int r, c;
7630 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007631#ifdef FEAT_MBYTE
7632 int max_off;
7633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007635 /* Can't use ScreenLines unless initialized */
7636 if (ScreenLines == NULL)
7637 return;
7638
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 if (invert)
7640 screen_char_attr = HL_INVERSE;
7641 for (r = row; r < row + height; ++r)
7642 {
7643 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007644#ifdef FEAT_MBYTE
7645 max_off = off + screen_Columns;
7646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 for (c = col; c < col + width; ++c)
7648 {
7649#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007650 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
7652 screen_char_2(off + c, r, c);
7653 ++c;
7654 }
7655 else
7656#endif
7657 {
7658 screen_char(off + c, r, c);
7659#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007660 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 ++c;
7662#endif
7663 }
7664 }
7665 }
7666 screen_char_attr = 0;
7667}
7668#endif
7669
7670#ifdef FEAT_VERTSPLIT
7671/*
7672 * Redraw the characters for a vertically split window.
7673 */
7674 static void
7675redraw_block(row, end, wp)
7676 int row;
7677 int end;
7678 win_T *wp;
7679{
7680 int col;
7681 int width;
7682
7683# ifdef FEAT_CLIPBOARD
7684 clip_may_clear_selection(row, end - 1);
7685# endif
7686
7687 if (wp == NULL)
7688 {
7689 col = 0;
7690 width = Columns;
7691 }
7692 else
7693 {
7694 col = wp->w_wincol;
7695 width = wp->w_width;
7696 }
7697 screen_draw_rectangle(row, col, end - row, width, FALSE);
7698}
7699#endif
7700
7701/*
7702 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7703 * with character 'c1' in first column followed by 'c2' in the other columns.
7704 * Use attributes 'attr'.
7705 */
7706 void
7707screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7708 int start_row, end_row;
7709 int start_col, end_col;
7710 int c1, c2;
7711 int attr;
7712{
7713 int row;
7714 int col;
7715 int off;
7716 int end_off;
7717 int did_delete;
7718 int c;
7719 int norm_term;
7720#if defined(FEAT_GUI) || defined(UNIX)
7721 int force_next = FALSE;
7722#endif
7723
7724 if (end_row > screen_Rows) /* safety check */
7725 end_row = screen_Rows;
7726 if (end_col > screen_Columns) /* safety check */
7727 end_col = screen_Columns;
7728 if (ScreenLines == NULL
7729 || start_row >= end_row
7730 || start_col >= end_col) /* nothing to do */
7731 return;
7732
7733 /* it's a "normal" terminal when not in a GUI or cterm */
7734 norm_term = (
7735#ifdef FEAT_GUI
7736 !gui.in_use &&
7737#endif
7738 t_colors <= 1);
7739 for (row = start_row; row < end_row; ++row)
7740 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007741#ifdef FEAT_MBYTE
7742 if (has_mbyte
7743# ifdef FEAT_GUI
7744 && !gui.in_use
7745# endif
7746 )
7747 {
7748 /* When drawing over the right halve of a double-wide char clear
7749 * out the left halve. When drawing over the left halve of a
7750 * double wide-char clear out the right halve. Only needed in a
7751 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007752 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007753 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007754 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007755 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007756 }
7757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 /*
7759 * Try to use delete-line termcap code, when no attributes or in a
7760 * "normal" terminal, where a bold/italic space is just a
7761 * space.
7762 */
7763 did_delete = FALSE;
7764 if (c2 == ' '
7765 && end_col == Columns
7766 && can_clear(T_CE)
7767 && (attr == 0
7768 || (norm_term
7769 && attr <= HL_ALL
7770 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7771 {
7772 /*
7773 * check if we really need to clear something
7774 */
7775 col = start_col;
7776 if (c1 != ' ') /* don't clear first char */
7777 ++col;
7778
7779 off = LineOffset[row] + col;
7780 end_off = LineOffset[row] + end_col;
7781
7782 /* skip blanks (used often, keep it fast!) */
7783#ifdef FEAT_MBYTE
7784 if (enc_utf8)
7785 while (off < end_off && ScreenLines[off] == ' '
7786 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7787 ++off;
7788 else
7789#endif
7790 while (off < end_off && ScreenLines[off] == ' '
7791 && ScreenAttrs[off] == 0)
7792 ++off;
7793 if (off < end_off) /* something to be cleared */
7794 {
7795 col = off - LineOffset[row];
7796 screen_stop_highlight();
7797 term_windgoto(row, col);/* clear rest of this screen line */
7798 out_str(T_CE);
7799 screen_start(); /* don't know where cursor is now */
7800 col = end_col - col;
7801 while (col--) /* clear chars in ScreenLines */
7802 {
7803 ScreenLines[off] = ' ';
7804#ifdef FEAT_MBYTE
7805 if (enc_utf8)
7806 ScreenLinesUC[off] = 0;
7807#endif
7808 ScreenAttrs[off] = 0;
7809 ++off;
7810 }
7811 }
7812 did_delete = TRUE; /* the chars are cleared now */
7813 }
7814
7815 off = LineOffset[row] + start_col;
7816 c = c1;
7817 for (col = start_col; col < end_col; ++col)
7818 {
7819 if (ScreenLines[off] != c
7820#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007821 || (enc_utf8 && (int)ScreenLinesUC[off]
7822 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823#endif
7824 || ScreenAttrs[off] != attr
7825#if defined(FEAT_GUI) || defined(UNIX)
7826 || force_next
7827#endif
7828 )
7829 {
7830#if defined(FEAT_GUI) || defined(UNIX)
7831 /* The bold trick may make a single row of pixels appear in
7832 * the next character. When a bold character is removed, the
7833 * next character should be redrawn too. This happens for our
7834 * own GUI and for some xterms. */
7835 if (
7836# ifdef FEAT_GUI
7837 gui.in_use
7838# endif
7839# if defined(FEAT_GUI) && defined(UNIX)
7840 ||
7841# endif
7842# ifdef UNIX
7843 term_is_xterm
7844# endif
7845 )
7846 {
7847 if (ScreenLines[off] != ' '
7848 && (ScreenAttrs[off] > HL_ALL
7849 || ScreenAttrs[off] & HL_BOLD))
7850 force_next = TRUE;
7851 else
7852 force_next = FALSE;
7853 }
7854#endif
7855 ScreenLines[off] = c;
7856#ifdef FEAT_MBYTE
7857 if (enc_utf8)
7858 {
7859 if (c >= 0x80)
7860 {
7861 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007862 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 }
7864 else
7865 ScreenLinesUC[off] = 0;
7866 }
7867#endif
7868 ScreenAttrs[off] = attr;
7869 if (!did_delete || c != ' ')
7870 screen_char(off, row, col);
7871 }
7872 ++off;
7873 if (col == start_col)
7874 {
7875 if (did_delete)
7876 break;
7877 c = c2;
7878 }
7879 }
7880 if (end_col == Columns)
7881 LineWraps[row] = FALSE;
7882 if (row == Rows - 1) /* overwritten the command line */
7883 {
7884 redraw_cmdline = TRUE;
7885 if (c1 == ' ' && c2 == ' ')
7886 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007887 if (start_col == 0)
7888 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 }
7891}
7892
7893/*
7894 * Check if there should be a delay. Used before clearing or redrawing the
7895 * screen or the command line.
7896 */
7897 void
7898check_for_delay(check_msg_scroll)
7899 int check_msg_scroll;
7900{
7901 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7902 && !did_wait_return
7903 && emsg_silent == 0)
7904 {
7905 out_flush();
7906 ui_delay(1000L, TRUE);
7907 emsg_on_display = FALSE;
7908 if (check_msg_scroll)
7909 msg_scroll = FALSE;
7910 }
7911}
7912
7913/*
7914 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007915 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 * Returns TRUE if there is a valid screen to write to.
7917 * Returns FALSE when starting up and screen not initialized yet.
7918 */
7919 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007920screen_valid(doclear)
7921 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007923 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 return (ScreenLines != NULL);
7925}
7926
7927/*
7928 * Resize the shell to Rows and Columns.
7929 * Allocate ScreenLines[] and associated items.
7930 *
7931 * There may be some time between setting Rows and Columns and (re)allocating
7932 * ScreenLines[]. This happens when starting up and when (manually) changing
7933 * the shell size. Always use screen_Rows and screen_Columns to access items
7934 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7935 * final size of the shell is needed.
7936 */
7937 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007938screenalloc(doclear)
7939 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
7941 int new_row, old_row;
7942#ifdef FEAT_GUI
7943 int old_Rows;
7944#endif
7945 win_T *wp;
7946 int outofmem = FALSE;
7947 int len;
7948 schar_T *new_ScreenLines;
7949#ifdef FEAT_MBYTE
7950 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007951 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007953 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954#endif
7955 sattr_T *new_ScreenAttrs;
7956 unsigned *new_LineOffset;
7957 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007958#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007959 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007960 tabpage_T *tp;
7961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007963 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007964#ifdef FEAT_AUTOCMD
7965 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007967retry:
7968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 /*
7970 * Allocation of the screen buffers is done only when the size changes and
7971 * when Rows and Columns have been set and we have started doing full
7972 * screen stuff.
7973 */
7974 if ((ScreenLines != NULL
7975 && Rows == screen_Rows
7976 && Columns == screen_Columns
7977#ifdef FEAT_MBYTE
7978 && enc_utf8 == (ScreenLinesUC != NULL)
7979 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007980 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981#endif
7982 )
7983 || Rows == 0
7984 || Columns == 0
7985 || (!full_screen && ScreenLines == NULL))
7986 return;
7987
7988 /*
7989 * It's possible that we produce an out-of-memory message below, which
7990 * will cause this function to be called again. To break the loop, just
7991 * return here.
7992 */
7993 if (entered)
7994 return;
7995 entered = TRUE;
7996
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007997 /*
7998 * Note that the window sizes are updated before reallocating the arrays,
7999 * thus we must not redraw here!
8000 */
8001 ++RedrawingDisabled;
8002
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 win_new_shellsize(); /* fit the windows in the new sized shell */
8004
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 comp_col(); /* recompute columns for shown command and ruler */
8006
8007 /*
8008 * We're changing the size of the screen.
8009 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8010 * - Move lines from the old arrays into the new arrays, clear extra
8011 * lines (unless the screen is going to be cleared).
8012 * - Free the old arrays.
8013 *
8014 * If anything fails, make ScreenLines NULL, so we don't do anything!
8015 * Continuing with the old ScreenLines may result in a crash, because the
8016 * size is wrong.
8017 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008018 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008020#ifdef FEAT_AUTOCMD
8021 if (aucmd_win != NULL)
8022 win_free_lsize(aucmd_win);
8023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024
8025 new_ScreenLines = (schar_T *)lalloc((long_u)(
8026 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8027#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008028 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 if (enc_utf8)
8030 {
8031 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8032 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008033 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008034 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8036 }
8037 if (enc_dbcs == DBCS_JPNU)
8038 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8039 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8040#endif
8041 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8042 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8043 new_LineOffset = (unsigned *)lalloc((long_u)(
8044 Rows * sizeof(unsigned)), FALSE);
8045 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008046#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008047 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008050 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {
8052 if (win_alloc_lines(wp) == FAIL)
8053 {
8054 outofmem = TRUE;
8055#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008056 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057#endif
8058 }
8059 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008060#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008061 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8062 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008063 outofmem = TRUE;
8064#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008065#ifdef FEAT_WINDOWS
8066give_up:
8067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008069#ifdef FEAT_MBYTE
8070 for (i = 0; i < p_mco; ++i)
8071 if (new_ScreenLinesC[i] == NULL)
8072 break;
8073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 if (new_ScreenLines == NULL
8075#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008076 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8078#endif
8079 || new_ScreenAttrs == NULL
8080 || new_LineOffset == NULL
8081 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008082#ifdef FEAT_WINDOWS
8083 || new_TabPageIdxs == NULL
8084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 || outofmem)
8086 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008087 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008088 {
8089 /* guess the size */
8090 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8091
8092 /* Remember we did this to avoid getting outofmem messages over
8093 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008094 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 vim_free(new_ScreenLines);
8097 new_ScreenLines = NULL;
8098#ifdef FEAT_MBYTE
8099 vim_free(new_ScreenLinesUC);
8100 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008101 for (i = 0; i < p_mco; ++i)
8102 {
8103 vim_free(new_ScreenLinesC[i]);
8104 new_ScreenLinesC[i] = NULL;
8105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 vim_free(new_ScreenLines2);
8107 new_ScreenLines2 = NULL;
8108#endif
8109 vim_free(new_ScreenAttrs);
8110 new_ScreenAttrs = NULL;
8111 vim_free(new_LineOffset);
8112 new_LineOffset = NULL;
8113 vim_free(new_LineWraps);
8114 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008115#ifdef FEAT_WINDOWS
8116 vim_free(new_TabPageIdxs);
8117 new_TabPageIdxs = NULL;
8118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 }
8120 else
8121 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008122 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008123
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 for (new_row = 0; new_row < Rows; ++new_row)
8125 {
8126 new_LineOffset[new_row] = new_row * Columns;
8127 new_LineWraps[new_row] = FALSE;
8128
8129 /*
8130 * If the screen is not going to be cleared, copy as much as
8131 * possible from the old screen to the new one and clear the rest
8132 * (used when resizing the window at the "--more--" prompt or when
8133 * executing an external command, for the GUI).
8134 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008135 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {
8137 (void)vim_memset(new_ScreenLines + new_row * Columns,
8138 ' ', (size_t)Columns * sizeof(schar_T));
8139#ifdef FEAT_MBYTE
8140 if (enc_utf8)
8141 {
8142 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8143 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008144 for (i = 0; i < p_mco; ++i)
8145 (void)vim_memset(new_ScreenLinesC[i]
8146 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 0, (size_t)Columns * sizeof(u8char_T));
8148 }
8149 if (enc_dbcs == DBCS_JPNU)
8150 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8151 0, (size_t)Columns * sizeof(schar_T));
8152#endif
8153 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8154 0, (size_t)Columns * sizeof(sattr_T));
8155 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008156 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {
8158 if (screen_Columns < Columns)
8159 len = screen_Columns;
8160 else
8161 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008162#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008163 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008164 * may be invalid now. Also when p_mco changes. */
8165 if (!(enc_utf8 && ScreenLinesUC == NULL)
8166 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008167#endif
8168 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8169 ScreenLines + LineOffset[old_row],
8170 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008172 if (enc_utf8 && ScreenLinesUC != NULL
8173 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
8175 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8176 ScreenLinesUC + LineOffset[old_row],
8177 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008178 for (i = 0; i < p_mco; ++i)
8179 mch_memmove(new_ScreenLinesC[i]
8180 + new_LineOffset[new_row],
8181 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 (size_t)len * sizeof(u8char_T));
8183 }
8184 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8185 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8186 ScreenLines2 + LineOffset[old_row],
8187 (size_t)len * sizeof(schar_T));
8188#endif
8189 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8190 ScreenAttrs + LineOffset[old_row],
8191 (size_t)len * sizeof(sattr_T));
8192 }
8193 }
8194 }
8195 /* Use the last line of the screen for the current line. */
8196 current_ScreenLine = new_ScreenLines + Rows * Columns;
8197 }
8198
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008199 free_screenlines();
8200
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 ScreenLines = new_ScreenLines;
8202#ifdef FEAT_MBYTE
8203 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008204 for (i = 0; i < p_mco; ++i)
8205 ScreenLinesC[i] = new_ScreenLinesC[i];
8206 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 ScreenLines2 = new_ScreenLines2;
8208#endif
8209 ScreenAttrs = new_ScreenAttrs;
8210 LineOffset = new_LineOffset;
8211 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008212#ifdef FEAT_WINDOWS
8213 TabPageIdxs = new_TabPageIdxs;
8214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215
8216 /* It's important that screen_Rows and screen_Columns reflect the actual
8217 * size of ScreenLines[]. Set them before calling anything. */
8218#ifdef FEAT_GUI
8219 old_Rows = screen_Rows;
8220#endif
8221 screen_Rows = Rows;
8222 screen_Columns = Columns;
8223
8224 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008225 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 screenclear2();
8227
8228#ifdef FEAT_GUI
8229 else if (gui.in_use
8230 && !gui.starting
8231 && ScreenLines != NULL
8232 && old_Rows != Rows)
8233 {
8234 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8235 /*
8236 * Adjust the position of the cursor, for when executing an external
8237 * command.
8238 */
8239 if (msg_row >= Rows) /* Rows got smaller */
8240 msg_row = Rows - 1; /* put cursor at last row */
8241 else if (Rows > old_Rows) /* Rows got bigger */
8242 msg_row += Rows - old_Rows; /* put cursor in same place */
8243 if (msg_col >= Columns) /* Columns got smaller */
8244 msg_col = Columns - 1; /* put cursor at last column */
8245 }
8246#endif
8247
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008249 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008250
8251#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008252 /*
8253 * Do not apply autocommands more than 3 times to avoid an endless loop
8254 * in case applying autocommands always changes Rows or Columns.
8255 */
8256 if (starting == 0 && ++retry_count <= 3)
8257 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008258 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008259 /* In rare cases, autocommands may have altered Rows or Columns,
8260 * jump back to check if we need to allocate the screen again. */
8261 goto retry;
8262 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264}
8265
8266 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008267free_screenlines()
8268{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008269#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008270 int i;
8271
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008272 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008273 for (i = 0; i < Screen_mco; ++i)
8274 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008275 vim_free(ScreenLines2);
8276#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008277 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008278 vim_free(ScreenAttrs);
8279 vim_free(LineOffset);
8280 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008281#ifdef FEAT_WINDOWS
8282 vim_free(TabPageIdxs);
8283#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008284}
8285
8286 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287screenclear()
8288{
8289 check_for_delay(FALSE);
8290 screenalloc(FALSE); /* allocate screen buffers if size changed */
8291 screenclear2(); /* clear the screen */
8292}
8293
8294 static void
8295screenclear2()
8296{
8297 int i;
8298
8299 if (starting == NO_SCREEN || ScreenLines == NULL
8300#ifdef FEAT_GUI
8301 || (gui.in_use && gui.starting)
8302#endif
8303 )
8304 return;
8305
8306#ifdef FEAT_GUI
8307 if (!gui.in_use)
8308#endif
8309 screen_attr = -1; /* force setting the Normal colors */
8310 screen_stop_highlight(); /* don't want highlighting here */
8311
8312#ifdef FEAT_CLIPBOARD
8313 /* disable selection without redrawing it */
8314 clip_scroll_selection(9999);
8315#endif
8316
8317 /* blank out ScreenLines */
8318 for (i = 0; i < Rows; ++i)
8319 {
8320 lineclear(LineOffset[i], (int)Columns);
8321 LineWraps[i] = FALSE;
8322 }
8323
8324 if (can_clear(T_CL))
8325 {
8326 out_str(T_CL); /* clear the display */
8327 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008328 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 }
8330 else
8331 {
8332 /* can't clear the screen, mark all chars with invalid attributes */
8333 for (i = 0; i < Rows; ++i)
8334 lineinvalid(LineOffset[i], (int)Columns);
8335 clear_cmdline = TRUE;
8336 }
8337
8338 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8339
8340 win_rest_invalid(firstwin);
8341 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008342#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008343 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 if (must_redraw == CLEAR) /* no need to clear again */
8346 must_redraw = NOT_VALID;
8347 compute_cmdrow();
8348 msg_row = cmdline_row; /* put cursor on last line for messages */
8349 msg_col = 0;
8350 screen_start(); /* don't know where cursor is now */
8351 msg_scrolled = 0; /* can't scroll back */
8352 msg_didany = FALSE;
8353 msg_didout = FALSE;
8354}
8355
8356/*
8357 * Clear one line in ScreenLines.
8358 */
8359 static void
8360lineclear(off, width)
8361 unsigned off;
8362 int width;
8363{
8364 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8365#ifdef FEAT_MBYTE
8366 if (enc_utf8)
8367 (void)vim_memset(ScreenLinesUC + off, 0,
8368 (size_t)width * sizeof(u8char_T));
8369#endif
8370 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8371}
8372
8373/*
8374 * Mark one line in ScreenLines invalid by setting the attributes to an
8375 * invalid value.
8376 */
8377 static void
8378lineinvalid(off, width)
8379 unsigned off;
8380 int width;
8381{
8382 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8383}
8384
8385#ifdef FEAT_VERTSPLIT
8386/*
8387 * Copy part of a Screenline for vertically split window "wp".
8388 */
8389 static void
8390linecopy(to, from, wp)
8391 int to;
8392 int from;
8393 win_T *wp;
8394{
8395 unsigned off_to = LineOffset[to] + wp->w_wincol;
8396 unsigned off_from = LineOffset[from] + wp->w_wincol;
8397
8398 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8399 wp->w_width * sizeof(schar_T));
8400# ifdef FEAT_MBYTE
8401 if (enc_utf8)
8402 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008403 int i;
8404
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8406 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008407 for (i = 0; i < p_mco; ++i)
8408 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8409 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 }
8411 if (enc_dbcs == DBCS_JPNU)
8412 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8413 wp->w_width * sizeof(schar_T));
8414# endif
8415 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8416 wp->w_width * sizeof(sattr_T));
8417}
8418#endif
8419
8420/*
8421 * Return TRUE if clearing with term string "p" would work.
8422 * It can't work when the string is empty or it won't set the right background.
8423 */
8424 int
8425can_clear(p)
8426 char_u *p;
8427{
8428 return (*p != NUL && (t_colors <= 1
8429#ifdef FEAT_GUI
8430 || gui.in_use
8431#endif
8432 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8433}
8434
8435/*
8436 * Reset cursor position. Use whenever cursor was moved because of outputting
8437 * something directly to the screen (shell commands) or a terminal control
8438 * code.
8439 */
8440 void
8441screen_start()
8442{
8443 screen_cur_row = screen_cur_col = 9999;
8444}
8445
8446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 * Move the cursor to position "row","col" in the screen.
8448 * This tries to find the most efficient way to move, minimizing the number of
8449 * characters sent to the terminal.
8450 */
8451 void
8452windgoto(row, col)
8453 int row;
8454 int col;
8455{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008456 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 int i;
8458 int plan;
8459 int cost;
8460 int wouldbe_col;
8461 int noinvcurs;
8462 char_u *bs;
8463 int goto_cost;
8464 int attr;
8465
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008466#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8468
8469#define PLAN_LE 1
8470#define PLAN_CR 2
8471#define PLAN_NL 3
8472#define PLAN_WRITE 4
8473 /* Can't use ScreenLines unless initialized */
8474 if (ScreenLines == NULL)
8475 return;
8476
8477 if (col != screen_cur_col || row != screen_cur_row)
8478 {
8479 /* Check for valid position. */
8480 if (row < 0) /* window without text lines? */
8481 row = 0;
8482 if (row >= screen_Rows)
8483 row = screen_Rows - 1;
8484 if (col >= screen_Columns)
8485 col = screen_Columns - 1;
8486
8487 /* check if no cursor movement is allowed in highlight mode */
8488 if (screen_attr && *T_MS == NUL)
8489 noinvcurs = HIGHL_COST;
8490 else
8491 noinvcurs = 0;
8492 goto_cost = GOTO_COST + noinvcurs;
8493
8494 /*
8495 * Plan how to do the positioning:
8496 * 1. Use CR to move it to column 0, same row.
8497 * 2. Use T_LE to move it a few columns to the left.
8498 * 3. Use NL to move a few lines down, column 0.
8499 * 4. Move a few columns to the right with T_ND or by writing chars.
8500 *
8501 * Don't do this if the cursor went beyond the last column, the cursor
8502 * position is unknown then (some terminals wrap, some don't )
8503 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008504 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 * characters to move the cursor to the right.
8506 */
8507 if (row >= screen_cur_row && screen_cur_col < Columns)
8508 {
8509 /*
8510 * If the cursor is in the same row, bigger col, we can use CR
8511 * or T_LE.
8512 */
8513 bs = NULL; /* init for GCC */
8514 attr = screen_attr;
8515 if (row == screen_cur_row && col < screen_cur_col)
8516 {
8517 /* "le" is preferred over "bc", because "bc" is obsolete */
8518 if (*T_LE)
8519 bs = T_LE; /* "cursor left" */
8520 else
8521 bs = T_BC; /* "backspace character (old) */
8522 if (*bs)
8523 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8524 else
8525 cost = 999;
8526 if (col + 1 < cost) /* using CR is less characters */
8527 {
8528 plan = PLAN_CR;
8529 wouldbe_col = 0;
8530 cost = 1; /* CR is just one character */
8531 }
8532 else
8533 {
8534 plan = PLAN_LE;
8535 wouldbe_col = col;
8536 }
8537 if (noinvcurs) /* will stop highlighting */
8538 {
8539 cost += noinvcurs;
8540 attr = 0;
8541 }
8542 }
8543
8544 /*
8545 * If the cursor is above where we want to be, we can use CR LF.
8546 */
8547 else if (row > screen_cur_row)
8548 {
8549 plan = PLAN_NL;
8550 wouldbe_col = 0;
8551 cost = (row - screen_cur_row) * 2; /* CR LF */
8552 if (noinvcurs) /* will stop highlighting */
8553 {
8554 cost += noinvcurs;
8555 attr = 0;
8556 }
8557 }
8558
8559 /*
8560 * If the cursor is in the same row, smaller col, just use write.
8561 */
8562 else
8563 {
8564 plan = PLAN_WRITE;
8565 wouldbe_col = screen_cur_col;
8566 cost = 0;
8567 }
8568
8569 /*
8570 * Check if any characters that need to be written have the
8571 * correct attributes. Also avoid UTF-8 characters.
8572 */
8573 i = col - wouldbe_col;
8574 if (i > 0)
8575 cost += i;
8576 if (cost < goto_cost && i > 0)
8577 {
8578 /*
8579 * Check if the attributes are correct without additionally
8580 * stopping highlighting.
8581 */
8582 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8583 while (i && *p++ == attr)
8584 --i;
8585 if (i != 0)
8586 {
8587 /*
8588 * Try if it works when highlighting is stopped here.
8589 */
8590 if (*--p == 0)
8591 {
8592 cost += noinvcurs;
8593 while (i && *p++ == 0)
8594 --i;
8595 }
8596 if (i != 0)
8597 cost = 999; /* different attributes, don't do it */
8598 }
8599#ifdef FEAT_MBYTE
8600 if (enc_utf8)
8601 {
8602 /* Don't use an UTF-8 char for positioning, it's slow. */
8603 for (i = wouldbe_col; i < col; ++i)
8604 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8605 {
8606 cost = 999;
8607 break;
8608 }
8609 }
8610#endif
8611 }
8612
8613 /*
8614 * We can do it without term_windgoto()!
8615 */
8616 if (cost < goto_cost)
8617 {
8618 if (plan == PLAN_LE)
8619 {
8620 if (noinvcurs)
8621 screen_stop_highlight();
8622 while (screen_cur_col > col)
8623 {
8624 out_str(bs);
8625 --screen_cur_col;
8626 }
8627 }
8628 else if (plan == PLAN_CR)
8629 {
8630 if (noinvcurs)
8631 screen_stop_highlight();
8632 out_char('\r');
8633 screen_cur_col = 0;
8634 }
8635 else if (plan == PLAN_NL)
8636 {
8637 if (noinvcurs)
8638 screen_stop_highlight();
8639 while (screen_cur_row < row)
8640 {
8641 out_char('\n');
8642 ++screen_cur_row;
8643 }
8644 screen_cur_col = 0;
8645 }
8646
8647 i = col - screen_cur_col;
8648 if (i > 0)
8649 {
8650 /*
8651 * Use cursor-right if it's one character only. Avoids
8652 * removing a line of pixels from the last bold char, when
8653 * using the bold trick in the GUI.
8654 */
8655 if (T_ND[0] != NUL && T_ND[1] == NUL)
8656 {
8657 while (i-- > 0)
8658 out_char(*T_ND);
8659 }
8660 else
8661 {
8662 int off;
8663
8664 off = LineOffset[row] + screen_cur_col;
8665 while (i-- > 0)
8666 {
8667 if (ScreenAttrs[off] != screen_attr)
8668 screen_stop_highlight();
8669#ifdef FEAT_MBYTE
8670 out_flush_check();
8671#endif
8672 out_char(ScreenLines[off]);
8673#ifdef FEAT_MBYTE
8674 if (enc_dbcs == DBCS_JPNU
8675 && ScreenLines[off] == 0x8e)
8676 out_char(ScreenLines2[off]);
8677#endif
8678 ++off;
8679 }
8680 }
8681 }
8682 }
8683 }
8684 else
8685 cost = 999;
8686
8687 if (cost >= goto_cost)
8688 {
8689 if (noinvcurs)
8690 screen_stop_highlight();
8691 if (row == screen_cur_row && (col > screen_cur_col) &&
8692 *T_CRI != NUL)
8693 term_cursor_right(col - screen_cur_col);
8694 else
8695 term_windgoto(row, col);
8696 }
8697 screen_cur_row = row;
8698 screen_cur_col = col;
8699 }
8700}
8701
8702/*
8703 * Set cursor to its position in the current window.
8704 */
8705 void
8706setcursor()
8707{
8708 if (redrawing())
8709 {
8710 validate_cursor();
8711 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8712 W_WINCOL(curwin) + (
8713#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008714 /* With 'rightleft' set and the cursor on a double-wide
8715 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8717# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008718 (has_mbyte
8719 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8720 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721# endif
8722 1)) :
8723#endif
8724 curwin->w_wcol));
8725 }
8726}
8727
8728
8729/*
8730 * insert 'line_count' lines at 'row' in window 'wp'
8731 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8732 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8733 * scrolling.
8734 * Returns FAIL if the lines are not inserted, OK for success.
8735 */
8736 int
8737win_ins_lines(wp, row, line_count, invalid, mayclear)
8738 win_T *wp;
8739 int row;
8740 int line_count;
8741 int invalid;
8742 int mayclear;
8743{
8744 int did_delete;
8745 int nextrow;
8746 int lastrow;
8747 int retval;
8748
8749 if (invalid)
8750 wp->w_lines_valid = 0;
8751
8752 if (wp->w_height < 5)
8753 return FAIL;
8754
8755 if (line_count > wp->w_height - row)
8756 line_count = wp->w_height - row;
8757
8758 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8759 if (retval != MAYBE)
8760 return retval;
8761
8762 /*
8763 * If there is a next window or a status line, we first try to delete the
8764 * lines at the bottom to avoid messing what is after the window.
8765 * If this fails and there are following windows, don't do anything to avoid
8766 * messing up those windows, better just redraw.
8767 */
8768 did_delete = FALSE;
8769#ifdef FEAT_WINDOWS
8770 if (wp->w_next != NULL || wp->w_status_height)
8771 {
8772 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8773 line_count, (int)Rows, FALSE, NULL) == OK)
8774 did_delete = TRUE;
8775 else if (wp->w_next)
8776 return FAIL;
8777 }
8778#endif
8779 /*
8780 * if no lines deleted, blank the lines that will end up below the window
8781 */
8782 if (!did_delete)
8783 {
8784#ifdef FEAT_WINDOWS
8785 wp->w_redr_status = TRUE;
8786#endif
8787 redraw_cmdline = TRUE;
8788 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8789 lastrow = nextrow + line_count;
8790 if (lastrow > Rows)
8791 lastrow = Rows;
8792 screen_fill(nextrow - line_count, lastrow - line_count,
8793 W_WINCOL(wp), (int)W_ENDCOL(wp),
8794 ' ', ' ', 0);
8795 }
8796
8797 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8798 == FAIL)
8799 {
8800 /* deletion will have messed up other windows */
8801 if (did_delete)
8802 {
8803#ifdef FEAT_WINDOWS
8804 wp->w_redr_status = TRUE;
8805#endif
8806 win_rest_invalid(W_NEXT(wp));
8807 }
8808 return FAIL;
8809 }
8810
8811 return OK;
8812}
8813
8814/*
8815 * delete "line_count" window lines at "row" in window "wp"
8816 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8817 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8818 * scrolling
8819 * Return OK for success, FAIL if the lines are not deleted.
8820 */
8821 int
8822win_del_lines(wp, row, line_count, invalid, mayclear)
8823 win_T *wp;
8824 int row;
8825 int line_count;
8826 int invalid;
8827 int mayclear;
8828{
8829 int retval;
8830
8831 if (invalid)
8832 wp->w_lines_valid = 0;
8833
8834 if (line_count > wp->w_height - row)
8835 line_count = wp->w_height - row;
8836
8837 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8838 if (retval != MAYBE)
8839 return retval;
8840
8841 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8842 (int)Rows, FALSE, NULL) == FAIL)
8843 return FAIL;
8844
8845#ifdef FEAT_WINDOWS
8846 /*
8847 * If there are windows or status lines below, try to put them at the
8848 * correct place. If we can't do that, they have to be redrawn.
8849 */
8850 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8851 {
8852 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8853 line_count, (int)Rows, NULL) == FAIL)
8854 {
8855 wp->w_redr_status = TRUE;
8856 win_rest_invalid(wp->w_next);
8857 }
8858 }
8859 /*
8860 * If this is the last window and there is no status line, redraw the
8861 * command line later.
8862 */
8863 else
8864#endif
8865 redraw_cmdline = TRUE;
8866 return OK;
8867}
8868
8869/*
8870 * Common code for win_ins_lines() and win_del_lines().
8871 * Returns OK or FAIL when the work has been done.
8872 * Returns MAYBE when not finished yet.
8873 */
8874 static int
8875win_do_lines(wp, row, line_count, mayclear, del)
8876 win_T *wp;
8877 int row;
8878 int line_count;
8879 int mayclear;
8880 int del;
8881{
8882 int retval;
8883
8884 if (!redrawing() || line_count <= 0)
8885 return FAIL;
8886
8887 /* only a few lines left: redraw is faster */
8888 if (mayclear && Rows - line_count < 5
8889#ifdef FEAT_VERTSPLIT
8890 && wp->w_width == Columns
8891#endif
8892 )
8893 {
8894 screenclear(); /* will set wp->w_lines_valid to 0 */
8895 return FAIL;
8896 }
8897
8898 /*
8899 * Delete all remaining lines
8900 */
8901 if (row + line_count >= wp->w_height)
8902 {
8903 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8904 W_WINCOL(wp), (int)W_ENDCOL(wp),
8905 ' ', ' ', 0);
8906 return OK;
8907 }
8908
8909 /*
8910 * when scrolling, the message on the command line should be cleared,
8911 * otherwise it will stay there forever.
8912 */
8913 clear_cmdline = TRUE;
8914
8915 /*
8916 * If the terminal can set a scroll region, use that.
8917 * Always do this in a vertically split window. This will redraw from
8918 * ScreenLines[] when t_CV isn't defined. That's faster than using
8919 * win_line().
8920 * Don't use a scroll region when we are going to redraw the text, writing
8921 * a character in the lower right corner of the scroll region causes a
8922 * scroll-up in the DJGPP version.
8923 */
8924 if (scroll_region
8925#ifdef FEAT_VERTSPLIT
8926 || W_WIDTH(wp) != Columns
8927#endif
8928 )
8929 {
8930#ifdef FEAT_VERTSPLIT
8931 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8932#endif
8933 scroll_region_set(wp, row);
8934 if (del)
8935 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8936 wp->w_height - row, FALSE, wp);
8937 else
8938 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8939 wp->w_height - row, wp);
8940#ifdef FEAT_VERTSPLIT
8941 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8942#endif
8943 scroll_region_reset();
8944 return retval;
8945 }
8946
8947#ifdef FEAT_WINDOWS
8948 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8949 return FAIL;
8950#endif
8951
8952 return MAYBE;
8953}
8954
8955/*
8956 * window 'wp' and everything after it is messed up, mark it for redraw
8957 */
8958 static void
8959win_rest_invalid(wp)
8960 win_T *wp;
8961{
8962#ifdef FEAT_WINDOWS
8963 while (wp != NULL)
8964#else
8965 if (wp != NULL)
8966#endif
8967 {
8968 redraw_win_later(wp, NOT_VALID);
8969#ifdef FEAT_WINDOWS
8970 wp->w_redr_status = TRUE;
8971 wp = wp->w_next;
8972#endif
8973 }
8974 redraw_cmdline = TRUE;
8975}
8976
8977/*
8978 * The rest of the routines in this file perform screen manipulations. The
8979 * given operation is performed physically on the screen. The corresponding
8980 * change is also made to the internal screen image. In this way, the editor
8981 * anticipates the effect of editing changes on the appearance of the screen.
8982 * That way, when we call screenupdate a complete redraw isn't usually
8983 * necessary. Another advantage is that we can keep adding code to anticipate
8984 * screen changes, and in the meantime, everything still works.
8985 */
8986
8987/*
8988 * types for inserting or deleting lines
8989 */
8990#define USE_T_CAL 1
8991#define USE_T_CDL 2
8992#define USE_T_AL 3
8993#define USE_T_CE 4
8994#define USE_T_DL 5
8995#define USE_T_SR 6
8996#define USE_NL 7
8997#define USE_T_CD 8
8998#define USE_REDRAW 9
8999
9000/*
9001 * insert lines on the screen and update ScreenLines[]
9002 * 'end' is the line after the scrolled part. Normally it is Rows.
9003 * When scrolling region used 'off' is the offset from the top for the region.
9004 * 'row' and 'end' are relative to the start of the region.
9005 *
9006 * return FAIL for failure, OK for success.
9007 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009008 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009screen_ins_lines(off, row, line_count, end, wp)
9010 int off;
9011 int row;
9012 int line_count;
9013 int end;
9014 win_T *wp; /* NULL or window to use width from */
9015{
9016 int i;
9017 int j;
9018 unsigned temp;
9019 int cursor_row;
9020 int type;
9021 int result_empty;
9022 int can_ce = can_clear(T_CE);
9023
9024 /*
9025 * FAIL if
9026 * - there is no valid screen
9027 * - the screen has to be redrawn completely
9028 * - the line count is less than one
9029 * - the line count is more than 'ttyscroll'
9030 */
9031 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9032 return FAIL;
9033
9034 /*
9035 * There are seven ways to insert lines:
9036 * 0. When in a vertically split window and t_CV isn't set, redraw the
9037 * characters from ScreenLines[].
9038 * 1. Use T_CD (clear to end of display) if it exists and the result of
9039 * the insert is just empty lines
9040 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9041 * present or line_count > 1. It looks better if we do all the inserts
9042 * at once.
9043 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9044 * insert is just empty lines and T_CE is not present or line_count >
9045 * 1.
9046 * 4. Use T_AL (insert line) if it exists.
9047 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9048 * just empty lines.
9049 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9050 * just empty lines.
9051 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9052 * the 'da' flag is not set or we have clear line capability.
9053 * 8. redraw the characters from ScreenLines[].
9054 *
9055 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9056 * the scrollbar for the window. It does have insert line, use that if it
9057 * exists.
9058 */
9059 result_empty = (row + line_count >= end);
9060#ifdef FEAT_VERTSPLIT
9061 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9062 type = USE_REDRAW;
9063 else
9064#endif
9065 if (can_clear(T_CD) && result_empty)
9066 type = USE_T_CD;
9067 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9068 type = USE_T_CAL;
9069 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9070 type = USE_T_CDL;
9071 else if (*T_AL != NUL)
9072 type = USE_T_AL;
9073 else if (can_ce && result_empty)
9074 type = USE_T_CE;
9075 else if (*T_DL != NUL && result_empty)
9076 type = USE_T_DL;
9077 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9078 type = USE_T_SR;
9079 else
9080 return FAIL;
9081
9082 /*
9083 * For clearing the lines screen_del_lines() is used. This will also take
9084 * care of t_db if necessary.
9085 */
9086 if (type == USE_T_CD || type == USE_T_CDL ||
9087 type == USE_T_CE || type == USE_T_DL)
9088 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9089
9090 /*
9091 * If text is retained below the screen, first clear or delete as many
9092 * lines at the bottom of the window as are about to be inserted so that
9093 * the deleted lines won't later surface during a screen_del_lines.
9094 */
9095 if (*T_DB)
9096 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9097
9098#ifdef FEAT_CLIPBOARD
9099 /* Remove a modeless selection when inserting lines halfway the screen
9100 * or not the full width of the screen. */
9101 if (off + row > 0
9102# ifdef FEAT_VERTSPLIT
9103 || (wp != NULL && wp->w_width != Columns)
9104# endif
9105 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009106 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 else
9108 clip_scroll_selection(-line_count);
9109#endif
9110
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111#ifdef FEAT_GUI
9112 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9113 * scrolling is actually carried out. */
9114 gui_dont_update_cursor();
9115#endif
9116
9117 if (*T_CCS != NUL) /* cursor relative to region */
9118 cursor_row = row;
9119 else
9120 cursor_row = row + off;
9121
9122 /*
9123 * Shift LineOffset[] line_count down to reflect the inserted lines.
9124 * Clear the inserted lines in ScreenLines[].
9125 */
9126 row += off;
9127 end += off;
9128 for (i = 0; i < line_count; ++i)
9129 {
9130#ifdef FEAT_VERTSPLIT
9131 if (wp != NULL && wp->w_width != Columns)
9132 {
9133 /* need to copy part of a line */
9134 j = end - 1 - i;
9135 while ((j -= line_count) >= row)
9136 linecopy(j + line_count, j, wp);
9137 j += line_count;
9138 if (can_clear((char_u *)" "))
9139 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9140 else
9141 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9142 LineWraps[j] = FALSE;
9143 }
9144 else
9145#endif
9146 {
9147 j = end - 1 - i;
9148 temp = LineOffset[j];
9149 while ((j -= line_count) >= row)
9150 {
9151 LineOffset[j + line_count] = LineOffset[j];
9152 LineWraps[j + line_count] = LineWraps[j];
9153 }
9154 LineOffset[j + line_count] = temp;
9155 LineWraps[j + line_count] = FALSE;
9156 if (can_clear((char_u *)" "))
9157 lineclear(temp, (int)Columns);
9158 else
9159 lineinvalid(temp, (int)Columns);
9160 }
9161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162
9163 screen_stop_highlight();
9164 windgoto(cursor_row, 0);
9165
9166#ifdef FEAT_VERTSPLIT
9167 /* redraw the characters */
9168 if (type == USE_REDRAW)
9169 redraw_block(row, end, wp);
9170 else
9171#endif
9172 if (type == USE_T_CAL)
9173 {
9174 term_append_lines(line_count);
9175 screen_start(); /* don't know where cursor is now */
9176 }
9177 else
9178 {
9179 for (i = 0; i < line_count; i++)
9180 {
9181 if (type == USE_T_AL)
9182 {
9183 if (i && cursor_row != 0)
9184 windgoto(cursor_row, 0);
9185 out_str(T_AL);
9186 }
9187 else /* type == USE_T_SR */
9188 out_str(T_SR);
9189 screen_start(); /* don't know where cursor is now */
9190 }
9191 }
9192
9193 /*
9194 * With scroll-reverse and 'da' flag set we need to clear the lines that
9195 * have been scrolled down into the region.
9196 */
9197 if (type == USE_T_SR && *T_DA)
9198 {
9199 for (i = 0; i < line_count; ++i)
9200 {
9201 windgoto(off + i, 0);
9202 out_str(T_CE);
9203 screen_start(); /* don't know where cursor is now */
9204 }
9205 }
9206
9207#ifdef FEAT_GUI
9208 gui_can_update_cursor();
9209 if (gui.in_use)
9210 out_flush(); /* always flush after a scroll */
9211#endif
9212 return OK;
9213}
9214
9215/*
9216 * delete lines on the screen and update ScreenLines[]
9217 * 'end' is the line after the scrolled part. Normally it is Rows.
9218 * When scrolling region used 'off' is the offset from the top for the region.
9219 * 'row' and 'end' are relative to the start of the region.
9220 *
9221 * Return OK for success, FAIL if the lines are not deleted.
9222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 int
9224screen_del_lines(off, row, line_count, end, force, wp)
9225 int off;
9226 int row;
9227 int line_count;
9228 int end;
9229 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009230 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
9232 int j;
9233 int i;
9234 unsigned temp;
9235 int cursor_row;
9236 int cursor_end;
9237 int result_empty; /* result is empty until end of region */
9238 int can_delete; /* deleting line codes can be used */
9239 int type;
9240
9241 /*
9242 * FAIL if
9243 * - there is no valid screen
9244 * - the screen has to be redrawn completely
9245 * - the line count is less than one
9246 * - the line count is more than 'ttyscroll'
9247 */
9248 if (!screen_valid(TRUE) || line_count <= 0 ||
9249 (!force && line_count > p_ttyscroll))
9250 return FAIL;
9251
9252 /*
9253 * Check if the rest of the current region will become empty.
9254 */
9255 result_empty = row + line_count >= end;
9256
9257 /*
9258 * We can delete lines only when 'db' flag not set or when 'ce' option
9259 * available.
9260 */
9261 can_delete = (*T_DB == NUL || can_clear(T_CE));
9262
9263 /*
9264 * There are six ways to delete lines:
9265 * 0. When in a vertically split window and t_CV isn't set, redraw the
9266 * characters from ScreenLines[].
9267 * 1. Use T_CD if it exists and the result is empty.
9268 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9269 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9270 * none of the other ways work.
9271 * 4. Use T_CE (erase line) if the result is empty.
9272 * 5. Use T_DL (delete line) if it exists.
9273 * 6. redraw the characters from ScreenLines[].
9274 */
9275#ifdef FEAT_VERTSPLIT
9276 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9277 type = USE_REDRAW;
9278 else
9279#endif
9280 if (can_clear(T_CD) && result_empty)
9281 type = USE_T_CD;
9282#if defined(__BEOS__) && defined(BEOS_DR8)
9283 /*
9284 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9285 * its internal termcap... this works okay for tests which test *T_DB !=
9286 * NUL. It has the disadvantage that the user cannot use any :set t_*
9287 * command to get T_DB (back) to empty_option, only :set term=... will do
9288 * the trick...
9289 * Anyway, this hack will hopefully go away with the next OS release.
9290 * (Olaf Seibert)
9291 */
9292 else if (row == 0 && T_DB == empty_option
9293 && (line_count == 1 || *T_CDL == NUL))
9294#else
9295 else if (row == 0 && (
9296#ifndef AMIGA
9297 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9298 * up, so use delete-line command */
9299 line_count == 1 ||
9300#endif
9301 *T_CDL == NUL))
9302#endif
9303 type = USE_NL;
9304 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9305 type = USE_T_CDL;
9306 else if (can_clear(T_CE) && result_empty
9307#ifdef FEAT_VERTSPLIT
9308 && (wp == NULL || wp->w_width == Columns)
9309#endif
9310 )
9311 type = USE_T_CE;
9312 else if (*T_DL != NUL && can_delete)
9313 type = USE_T_DL;
9314 else if (*T_CDL != NUL && can_delete)
9315 type = USE_T_CDL;
9316 else
9317 return FAIL;
9318
9319#ifdef FEAT_CLIPBOARD
9320 /* Remove a modeless selection when deleting lines halfway the screen or
9321 * not the full width of the screen. */
9322 if (off + row > 0
9323# ifdef FEAT_VERTSPLIT
9324 || (wp != NULL && wp->w_width != Columns)
9325# endif
9326 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009327 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 else
9329 clip_scroll_selection(line_count);
9330#endif
9331
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332#ifdef FEAT_GUI
9333 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9334 * scrolling is actually carried out. */
9335 gui_dont_update_cursor();
9336#endif
9337
9338 if (*T_CCS != NUL) /* cursor relative to region */
9339 {
9340 cursor_row = row;
9341 cursor_end = end;
9342 }
9343 else
9344 {
9345 cursor_row = row + off;
9346 cursor_end = end + off;
9347 }
9348
9349 /*
9350 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9351 * Clear the inserted lines in ScreenLines[].
9352 */
9353 row += off;
9354 end += off;
9355 for (i = 0; i < line_count; ++i)
9356 {
9357#ifdef FEAT_VERTSPLIT
9358 if (wp != NULL && wp->w_width != Columns)
9359 {
9360 /* need to copy part of a line */
9361 j = row + i;
9362 while ((j += line_count) <= end - 1)
9363 linecopy(j - line_count, j, wp);
9364 j -= line_count;
9365 if (can_clear((char_u *)" "))
9366 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9367 else
9368 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9369 LineWraps[j] = FALSE;
9370 }
9371 else
9372#endif
9373 {
9374 /* whole width, moving the line pointers is faster */
9375 j = row + i;
9376 temp = LineOffset[j];
9377 while ((j += line_count) <= end - 1)
9378 {
9379 LineOffset[j - line_count] = LineOffset[j];
9380 LineWraps[j - line_count] = LineWraps[j];
9381 }
9382 LineOffset[j - line_count] = temp;
9383 LineWraps[j - line_count] = FALSE;
9384 if (can_clear((char_u *)" "))
9385 lineclear(temp, (int)Columns);
9386 else
9387 lineinvalid(temp, (int)Columns);
9388 }
9389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390
9391 screen_stop_highlight();
9392
9393#ifdef FEAT_VERTSPLIT
9394 /* redraw the characters */
9395 if (type == USE_REDRAW)
9396 redraw_block(row, end, wp);
9397 else
9398#endif
9399 if (type == USE_T_CD) /* delete the lines */
9400 {
9401 windgoto(cursor_row, 0);
9402 out_str(T_CD);
9403 screen_start(); /* don't know where cursor is now */
9404 }
9405 else if (type == USE_T_CDL)
9406 {
9407 windgoto(cursor_row, 0);
9408 term_delete_lines(line_count);
9409 screen_start(); /* don't know where cursor is now */
9410 }
9411 /*
9412 * Deleting lines at top of the screen or scroll region: Just scroll
9413 * the whole screen (scroll region) up by outputting newlines on the
9414 * last line.
9415 */
9416 else if (type == USE_NL)
9417 {
9418 windgoto(cursor_end - 1, 0);
9419 for (i = line_count; --i >= 0; )
9420 out_char('\n'); /* cursor will remain on same line */
9421 }
9422 else
9423 {
9424 for (i = line_count; --i >= 0; )
9425 {
9426 if (type == USE_T_DL)
9427 {
9428 windgoto(cursor_row, 0);
9429 out_str(T_DL); /* delete a line */
9430 }
9431 else /* type == USE_T_CE */
9432 {
9433 windgoto(cursor_row + i, 0);
9434 out_str(T_CE); /* erase a line */
9435 }
9436 screen_start(); /* don't know where cursor is now */
9437 }
9438 }
9439
9440 /*
9441 * If the 'db' flag is set, we need to clear the lines that have been
9442 * scrolled up at the bottom of the region.
9443 */
9444 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9445 {
9446 for (i = line_count; i > 0; --i)
9447 {
9448 windgoto(cursor_end - i, 0);
9449 out_str(T_CE); /* erase a line */
9450 screen_start(); /* don't know where cursor is now */
9451 }
9452 }
9453
9454#ifdef FEAT_GUI
9455 gui_can_update_cursor();
9456 if (gui.in_use)
9457 out_flush(); /* always flush after a scroll */
9458#endif
9459
9460 return OK;
9461}
9462
9463/*
9464 * show the current mode and ruler
9465 *
9466 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9467 * If clear_cmdline is FALSE there may be a message there that needs to be
9468 * cleared only if a mode is shown.
9469 * Return the length of the message (0 if no message).
9470 */
9471 int
9472showmode()
9473{
9474 int need_clear;
9475 int length = 0;
9476 int do_mode;
9477 int attr;
9478 int nwr_save;
9479#ifdef FEAT_INS_EXPAND
9480 int sub_attr;
9481#endif
9482
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009483 do_mode = ((p_smd && msg_silent == 0)
9484 && ((State & INSERT)
9485 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486#ifdef FEAT_VISUAL
9487 || VIsual_active
9488#endif
9489 ));
9490 if (do_mode || Recording)
9491 {
9492 /*
9493 * Don't show mode right now, when not redrawing or inside a mapping.
9494 * Call char_avail() only when we are going to show something, because
9495 * it takes a bit of time.
9496 */
9497 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9498 {
9499 redraw_cmdline = TRUE; /* show mode later */
9500 return 0;
9501 }
9502
9503 nwr_save = need_wait_return;
9504
9505 /* wait a bit before overwriting an important message */
9506 check_for_delay(FALSE);
9507
9508 /* if the cmdline is more than one line high, erase top lines */
9509 need_clear = clear_cmdline;
9510 if (clear_cmdline && cmdline_row < Rows - 1)
9511 msg_clr_cmdline(); /* will reset clear_cmdline */
9512
9513 /* Position on the last line in the window, column 0 */
9514 msg_pos_mode();
9515 cursor_off();
9516 attr = hl_attr(HLF_CM); /* Highlight mode */
9517 if (do_mode)
9518 {
9519 MSG_PUTS_ATTR("--", attr);
9520#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009521 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009522# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009523 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009524# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009525 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009526# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009527 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009528# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 MSG_PUTS_ATTR(" IM", attr);
9530# else
9531 MSG_PUTS_ATTR(" XIM", attr);
9532# endif
9533#endif
9534#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9535 if (gui.in_use)
9536 {
9537 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009538 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 }
9540#endif
9541#ifdef FEAT_INS_EXPAND
9542 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9543 {
9544 /* These messages can get long, avoid a wrap in a narrow
9545 * window. Prefer showing edit_submode_extra. */
9546 length = (Rows - msg_row) * Columns - 3;
9547 if (edit_submode_extra != NULL)
9548 length -= vim_strsize(edit_submode_extra);
9549 if (length > 0)
9550 {
9551 if (edit_submode_pre != NULL)
9552 length -= vim_strsize(edit_submode_pre);
9553 if (length - vim_strsize(edit_submode) > 0)
9554 {
9555 if (edit_submode_pre != NULL)
9556 msg_puts_attr(edit_submode_pre, attr);
9557 msg_puts_attr(edit_submode, attr);
9558 }
9559 if (edit_submode_extra != NULL)
9560 {
9561 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9562 if ((int)edit_submode_highl < (int)HLF_COUNT)
9563 sub_attr = hl_attr(edit_submode_highl);
9564 else
9565 sub_attr = attr;
9566 msg_puts_attr(edit_submode_extra, sub_attr);
9567 }
9568 }
9569 length = 0;
9570 }
9571 else
9572#endif
9573 {
9574#ifdef FEAT_VREPLACE
9575 if (State & VREPLACE_FLAG)
9576 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9577 else
9578#endif
9579 if (State & REPLACE_FLAG)
9580 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9581 else if (State & INSERT)
9582 {
9583#ifdef FEAT_RIGHTLEFT
9584 if (p_ri)
9585 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9586#endif
9587 MSG_PUTS_ATTR(_(" INSERT"), attr);
9588 }
9589 else if (restart_edit == 'I')
9590 MSG_PUTS_ATTR(_(" (insert)"), attr);
9591 else if (restart_edit == 'R')
9592 MSG_PUTS_ATTR(_(" (replace)"), attr);
9593 else if (restart_edit == 'V')
9594 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9595#ifdef FEAT_RIGHTLEFT
9596 if (p_hkmap)
9597 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9598# ifdef FEAT_FKMAP
9599 if (p_fkmap)
9600 MSG_PUTS_ATTR(farsi_text_5, attr);
9601# endif
9602#endif
9603#ifdef FEAT_KEYMAP
9604 if (State & LANGMAP)
9605 {
9606# ifdef FEAT_ARABIC
9607 if (curwin->w_p_arab)
9608 MSG_PUTS_ATTR(_(" Arabic"), attr);
9609 else
9610# endif
9611 MSG_PUTS_ATTR(_(" (lang)"), attr);
9612 }
9613#endif
9614 if ((State & INSERT) && p_paste)
9615 MSG_PUTS_ATTR(_(" (paste)"), attr);
9616
9617#ifdef FEAT_VISUAL
9618 if (VIsual_active)
9619 {
9620 char *p;
9621
9622 /* Don't concatenate separate words to avoid translation
9623 * problems. */
9624 switch ((VIsual_select ? 4 : 0)
9625 + (VIsual_mode == Ctrl_V) * 2
9626 + (VIsual_mode == 'V'))
9627 {
9628 case 0: p = N_(" VISUAL"); break;
9629 case 1: p = N_(" VISUAL LINE"); break;
9630 case 2: p = N_(" VISUAL BLOCK"); break;
9631 case 4: p = N_(" SELECT"); break;
9632 case 5: p = N_(" SELECT LINE"); break;
9633 default: p = N_(" SELECT BLOCK"); break;
9634 }
9635 MSG_PUTS_ATTR(_(p), attr);
9636 }
9637#endif
9638 MSG_PUTS_ATTR(" --", attr);
9639 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009640
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 need_clear = TRUE;
9642 }
9643 if (Recording
9644#ifdef FEAT_INS_EXPAND
9645 && edit_submode == NULL /* otherwise it gets too long */
9646#endif
9647 )
9648 {
9649 MSG_PUTS_ATTR(_("recording"), attr);
9650 need_clear = TRUE;
9651 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009652
9653 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (need_clear || clear_cmdline)
9655 msg_clr_eos();
9656 msg_didout = FALSE; /* overwrite this message */
9657 length = msg_col;
9658 msg_col = 0;
9659 need_wait_return = nwr_save; /* never ask for hit-return for this */
9660 }
9661 else if (clear_cmdline && msg_silent == 0)
9662 /* Clear the whole command line. Will reset "clear_cmdline". */
9663 msg_clr_cmdline();
9664
9665#ifdef FEAT_CMDL_INFO
9666# ifdef FEAT_VISUAL
9667 /* In Visual mode the size of the selected area must be redrawn. */
9668 if (VIsual_active)
9669 clear_showcmd();
9670# endif
9671
9672 /* If the last window has no status line, the ruler is after the mode
9673 * message and must be redrawn */
9674 if (redrawing()
9675# ifdef FEAT_WINDOWS
9676 && lastwin->w_status_height == 0
9677# endif
9678 )
9679 win_redr_ruler(lastwin, TRUE);
9680#endif
9681 redraw_cmdline = FALSE;
9682 clear_cmdline = FALSE;
9683
9684 return length;
9685}
9686
9687/*
9688 * Position for a mode message.
9689 */
9690 static void
9691msg_pos_mode()
9692{
9693 msg_col = 0;
9694 msg_row = Rows - 1;
9695}
9696
9697/*
9698 * Delete mode message. Used when ESC is typed which is expected to end
9699 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009700 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 */
9702 void
9703unshowmode(force)
9704 int force;
9705{
9706 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009707 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 */
9709 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9710 redraw_cmdline = TRUE; /* delete mode later */
9711 else
9712 {
9713 msg_pos_mode();
9714 if (Recording)
9715 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9716 msg_clr_eos();
9717 }
9718}
9719
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009720#if defined(FEAT_WINDOWS)
9721/*
9722 * Draw the tab pages line at the top of the Vim window.
9723 */
9724 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009725draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009726{
9727 int tabcount = 0;
9728 tabpage_T *tp;
9729 int tabwidth;
9730 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009731 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009732 int attr;
9733 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009734 win_T *cwp;
9735 int wincount;
9736 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009737 int c;
9738 int len;
9739 int attr_sel = hl_attr(HLF_TPS);
9740 int attr_nosel = hl_attr(HLF_TP);
9741 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009742 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009743 int room;
9744 int use_sep_chars = (t_colors < 8
9745#ifdef FEAT_GUI
9746 && !gui.in_use
9747#endif
9748 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009749
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009750 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009751
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009752#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009753 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009754 if (gui_use_tabline())
9755 {
9756 gui_update_tabline();
9757 return;
9758 }
9759#endif
9760
9761 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009762 return;
9763
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009764#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009765
9766 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9767 for (scol = 0; scol < Columns; ++scol)
9768 TabPageIdxs[scol] = 0;
9769
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009770 /* Use the 'tabline' option if it's set. */
9771 if (*p_tal != NUL)
9772 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009773 int save_called_emsg = called_emsg;
9774
9775 /* Check for an error. If there is one we would loop in redrawing the
9776 * screen. Avoid that by making 'tabline' empty. */
9777 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009778 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009779 if (called_emsg)
9780 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009781 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009782 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009783 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009784 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009785#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009786 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009787 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9788 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009789
Bram Moolenaar238a5642006-02-21 22:12:05 +00009790 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9791 if (tabwidth < 6)
9792 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009793
Bram Moolenaar238a5642006-02-21 22:12:05 +00009794 attr = attr_nosel;
9795 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009796 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009797 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9798 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009799 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009800 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009801
Bram Moolenaar238a5642006-02-21 22:12:05 +00009802 if (tp->tp_topframe == topframe)
9803 attr = attr_sel;
9804 if (use_sep_chars && col > 0)
9805 screen_putchar('|', 0, col++, attr);
9806
9807 if (tp->tp_topframe != topframe)
9808 attr = attr_nosel;
9809
9810 screen_putchar(' ', 0, col++, attr);
9811
9812 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009813 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009814 cwp = curwin;
9815 wp = firstwin;
9816 }
9817 else
9818 {
9819 cwp = tp->tp_curwin;
9820 wp = tp->tp_firstwin;
9821 }
9822
9823 modified = FALSE;
9824 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9825 if (bufIsChanged(wp->w_buffer))
9826 modified = TRUE;
9827 if (modified || wincount > 1)
9828 {
9829 if (wincount > 1)
9830 {
9831 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009832 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009833 if (col + len >= Columns - 3)
9834 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009835 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009836#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009837 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009838#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009839 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009840#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009841 );
9842 col += len;
9843 }
9844 if (modified)
9845 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9846 screen_putchar(' ', 0, col++, attr);
9847 }
9848
9849 room = scol - col + tabwidth - 1;
9850 if (room > 0)
9851 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009852 /* Get buffer name in NameBuff[] */
9853 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009854 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009855 len = vim_strsize(NameBuff);
9856 p = NameBuff;
9857#ifdef FEAT_MBYTE
9858 if (has_mbyte)
9859 while (len > room)
9860 {
9861 len -= ptr2cells(p);
9862 mb_ptr_adv(p);
9863 }
9864 else
9865#endif
9866 if (len > room)
9867 {
9868 p += len - room;
9869 len = room;
9870 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009871 if (len > Columns - col - 1)
9872 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009873
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009874 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009875 col += len;
9876 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009877 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009878
9879 /* Store the tab page number in TabPageIdxs[], so that
9880 * jump_to_mouse() knows where each one is. */
9881 ++tabcount;
9882 while (scol < col)
9883 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009884 }
9885
Bram Moolenaar238a5642006-02-21 22:12:05 +00009886 if (use_sep_chars)
9887 c = '_';
9888 else
9889 c = ' ';
9890 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009891
9892 /* Put an "X" for closing the current tab if there are several. */
9893 if (first_tabpage->tp_next != NULL)
9894 {
9895 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9896 TabPageIdxs[Columns - 1] = -999;
9897 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009898 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009899
9900 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9901 * set. */
9902 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009903}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009904
9905/*
9906 * Get buffer name for "buf" into NameBuff[].
9907 * Takes care of special buffer names and translates special characters.
9908 */
9909 void
9910get_trans_bufname(buf)
9911 buf_T *buf;
9912{
9913 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02009914 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009915 else
9916 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9917 trans_characters(NameBuff, MAXPATHL);
9918}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009919#endif
9920
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9922/*
9923 * Get the character to use in a status line. Get its attributes in "*attr".
9924 */
9925 static int
9926fillchar_status(attr, is_curwin)
9927 int *attr;
9928 int is_curwin;
9929{
9930 int fill;
9931 if (is_curwin)
9932 {
9933 *attr = hl_attr(HLF_S);
9934 fill = fill_stl;
9935 }
9936 else
9937 {
9938 *attr = hl_attr(HLF_SNC);
9939 fill = fill_stlnc;
9940 }
9941 /* Use fill when there is highlighting, and highlighting of current
9942 * window differs, or the fillchars differ, or this is not the
9943 * current window */
9944 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9945 || !is_curwin || firstwin == lastwin)
9946 || (fill_stl != fill_stlnc)))
9947 return fill;
9948 if (is_curwin)
9949 return '^';
9950 return '=';
9951}
9952#endif
9953
9954#ifdef FEAT_VERTSPLIT
9955/*
9956 * Get the character to use in a separator between vertically split windows.
9957 * Get its attributes in "*attr".
9958 */
9959 static int
9960fillchar_vsep(attr)
9961 int *attr;
9962{
9963 *attr = hl_attr(HLF_C);
9964 if (*attr == 0 && fill_vert == ' ')
9965 return '|';
9966 else
9967 return fill_vert;
9968}
9969#endif
9970
9971/*
9972 * Return TRUE if redrawing should currently be done.
9973 */
9974 int
9975redrawing()
9976{
9977 return (!RedrawingDisabled
9978 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9979}
9980
9981/*
9982 * Return TRUE if printing messages should currently be done.
9983 */
9984 int
9985messaging()
9986{
9987 return (!(p_lz && char_avail() && !KeyTyped));
9988}
9989
9990/*
9991 * Show current status info in ruler and various other places
9992 * If always is FALSE, only show ruler if position has changed.
9993 */
9994 void
9995showruler(always)
9996 int always;
9997{
9998 if (!always && !redrawing())
9999 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010000#ifdef FEAT_INS_EXPAND
10001 if (pum_visible())
10002 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010003# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010004 /* Don't redraw right now, do it later. */
10005 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010006# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010007 return;
10008 }
10009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010011 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010012 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010013 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 else
10016#endif
10017#ifdef FEAT_CMDL_INFO
10018 win_redr_ruler(curwin, always);
10019#endif
10020
10021#ifdef FEAT_TITLE
10022 if (need_maketitle
10023# ifdef FEAT_STL_OPT
10024 || (p_icon && (stl_syntax & STL_IN_ICON))
10025 || (p_title && (stl_syntax & STL_IN_TITLE))
10026# endif
10027 )
10028 maketitle();
10029#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010030#ifdef FEAT_WINDOWS
10031 /* Redraw the tab pages line if needed. */
10032 if (redraw_tabline)
10033 draw_tabline();
10034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035}
10036
10037#ifdef FEAT_CMDL_INFO
10038 static void
10039win_redr_ruler(wp, always)
10040 win_T *wp;
10041 int always;
10042{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010043#define RULER_BUF_LEN 70
10044 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 int row;
10046 int fillchar;
10047 int attr;
10048 int empty_line = FALSE;
10049 colnr_T virtcol;
10050 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010051 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 int o;
10053#ifdef FEAT_VERTSPLIT
10054 int this_ru_col;
10055 int off = 0;
10056 int width = Columns;
10057# define WITH_OFF(x) x
10058# define WITH_WIDTH(x) x
10059#else
10060# define WITH_OFF(x) 0
10061# define WITH_WIDTH(x) Columns
10062# define this_ru_col ru_col
10063#endif
10064
10065 /* If 'ruler' off or redrawing disabled, don't do anything */
10066 if (!p_ru)
10067 return;
10068
10069 /*
10070 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10071 * after deleting lines, before cursor.lnum is corrected.
10072 */
10073 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10074 return;
10075
10076#ifdef FEAT_INS_EXPAND
10077 /* Don't draw the ruler while doing insert-completion, it might overwrite
10078 * the (long) mode message. */
10079# ifdef FEAT_WINDOWS
10080 if (wp == lastwin && lastwin->w_status_height == 0)
10081# endif
10082 if (edit_submode != NULL)
10083 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010084 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10085 if (pum_visible())
10086 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087#endif
10088
10089#ifdef FEAT_STL_OPT
10090 if (*p_ruf)
10091 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010092 int save_called_emsg = called_emsg;
10093
10094 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010096 if (called_emsg)
10097 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010098 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010099 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 return;
10101 }
10102#endif
10103
10104 /*
10105 * Check if not in Insert mode and the line is empty (will show "0-1").
10106 */
10107 if (!(State & INSERT)
10108 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10109 empty_line = TRUE;
10110
10111 /*
10112 * Only draw the ruler when something changed.
10113 */
10114 validate_virtcol_win(wp);
10115 if ( redraw_cmdline
10116 || always
10117 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10118 || wp->w_cursor.col != wp->w_ru_cursor.col
10119 || wp->w_virtcol != wp->w_ru_virtcol
10120#ifdef FEAT_VIRTUALEDIT
10121 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10122#endif
10123 || wp->w_topline != wp->w_ru_topline
10124 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10125#ifdef FEAT_DIFF
10126 || wp->w_topfill != wp->w_ru_topfill
10127#endif
10128 || empty_line != wp->w_ru_empty)
10129 {
10130 cursor_off();
10131#ifdef FEAT_WINDOWS
10132 if (wp->w_status_height)
10133 {
10134 row = W_WINROW(wp) + wp->w_height;
10135 fillchar = fillchar_status(&attr, wp == curwin);
10136# ifdef FEAT_VERTSPLIT
10137 off = W_WINCOL(wp);
10138 width = W_WIDTH(wp);
10139# endif
10140 }
10141 else
10142#endif
10143 {
10144 row = Rows - 1;
10145 fillchar = ' ';
10146 attr = 0;
10147#ifdef FEAT_VERTSPLIT
10148 width = Columns;
10149 off = 0;
10150#endif
10151 }
10152
10153 /* In list mode virtcol needs to be recomputed */
10154 virtcol = wp->w_virtcol;
10155 if (wp->w_p_list && lcs_tab1 == NUL)
10156 {
10157 wp->w_p_list = FALSE;
10158 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10159 wp->w_p_list = TRUE;
10160 }
10161
10162 /*
10163 * Some sprintfs return the length, some return a pointer.
10164 * To avoid portability problems we use strlen() here.
10165 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010166 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10168 ? 0L
10169 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010170 len = STRLEN(buffer);
10171 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10173 (int)virtcol + 1);
10174
10175 /*
10176 * Add a "50%" if there is room for it.
10177 * On the last line, don't print in the last column (scrolls the
10178 * screen up on some terminals).
10179 */
10180 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010181 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 o = i + vim_strsize(buffer + i + 1);
10183#ifdef FEAT_WINDOWS
10184 if (wp->w_status_height == 0) /* can't use last char of screen */
10185#endif
10186 ++o;
10187#ifdef FEAT_VERTSPLIT
10188 this_ru_col = ru_col - (Columns - width);
10189 if (this_ru_col < 0)
10190 this_ru_col = 0;
10191#endif
10192 /* Never use more than half the window/screen width, leave the other
10193 * half for the filename. */
10194 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10195 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10196 if (this_ru_col + o < WITH_WIDTH(width))
10197 {
10198 while (this_ru_col + o < WITH_WIDTH(width))
10199 {
10200#ifdef FEAT_MBYTE
10201 if (has_mbyte)
10202 i += (*mb_char2bytes)(fillchar, buffer + i);
10203 else
10204#endif
10205 buffer[i++] = fillchar;
10206 ++o;
10207 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010208 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 }
10210 /* Truncate at window boundary. */
10211#ifdef FEAT_MBYTE
10212 if (has_mbyte)
10213 {
10214 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010215 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 {
10217 o += (*mb_ptr2cells)(buffer + i);
10218 if (this_ru_col + o > WITH_WIDTH(width))
10219 {
10220 buffer[i] = NUL;
10221 break;
10222 }
10223 }
10224 }
10225 else
10226#endif
10227 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10228 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10229
10230 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10231 i = redraw_cmdline;
10232 screen_fill(row, row + 1,
10233 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10234 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10235 fillchar, fillchar, attr);
10236 /* don't redraw the cmdline because of showing the ruler */
10237 redraw_cmdline = i;
10238 wp->w_ru_cursor = wp->w_cursor;
10239 wp->w_ru_virtcol = wp->w_virtcol;
10240 wp->w_ru_empty = empty_line;
10241 wp->w_ru_topline = wp->w_topline;
10242 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10243#ifdef FEAT_DIFF
10244 wp->w_ru_topfill = wp->w_topfill;
10245#endif
10246 }
10247}
10248#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010249
10250#if defined(FEAT_LINEBREAK) || defined(PROTO)
10251/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010252 * Return the width of the 'number' and 'relativenumber' column.
10253 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010254 * Otherwise it depends on 'numberwidth' and the line count.
10255 */
10256 int
10257number_width(wp)
10258 win_T *wp;
10259{
10260 int n;
10261 linenr_T lnum;
10262
Bram Moolenaar700e7342013-01-30 12:31:36 +010010263 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010264
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010265 if (lnum == wp->w_nrwidth_line_count)
10266 return wp->w_nrwidth_width;
10267 wp->w_nrwidth_line_count = lnum;
10268
10269 n = 0;
10270 do
10271 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010272 lnum /= 10;
10273 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010274 } while (lnum > 0);
10275
10276 /* 'numberwidth' gives the minimal width plus one */
10277 if (n < wp->w_p_nuw - 1)
10278 n = wp->w_p_nuw - 1;
10279
10280 wp->w_nrwidth_width = n;
10281 return n;
10282}
10283#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010284
10285/*
10286 * Return the current cursor column. This is the actual position on the
10287 * screen. First column is 0.
10288 */
10289 int
10290screen_screencol()
10291{
10292 return screen_cur_col;
10293}
10294
10295/*
10296 * Return the current cursor row. This is the actual position on the screen.
10297 * First row is 0.
10298 */
10299 int
10300screen_screenrow()
10301{
10302 return screen_cur_row;
10303}