blob: 661da039cd81fd79930516dee5d51b20f4acdf55 [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
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001641#ifdef FEAT_SEARCH_EXTRA
1642 /* match in fixed position might need redraw */
1643 || wp->w_match_head != NULL
1644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 )))))
1646 {
1647#ifdef FEAT_SEARCH_EXTRA
1648 if (lnum == mod_top)
1649 top_to_mod = FALSE;
1650#endif
1651
1652 /*
1653 * When at start of changed lines: May scroll following lines
1654 * up or down to minimize redrawing.
1655 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001656 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
1658 if (lnum == mod_top
1659 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001660 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
1662 int old_rows = 0;
1663 int new_rows = 0;
1664 int xtra_rows;
1665 linenr_T l;
1666
1667 /* Count the old number of window rows, using w_lines[], which
1668 * should still contain the sizes for the lines as they are
1669 * currently displayed. */
1670 for (i = idx; i < wp->w_lines_valid; ++i)
1671 {
1672 /* Only valid lines have a meaningful wl_lnum. Invalid
1673 * lines are part of the changed area. */
1674 if (wp->w_lines[i].wl_valid
1675 && wp->w_lines[i].wl_lnum == mod_bot)
1676 break;
1677 old_rows += wp->w_lines[i].wl_size;
1678#ifdef FEAT_FOLDING
1679 if (wp->w_lines[i].wl_valid
1680 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1681 {
1682 /* Must have found the last valid entry above mod_bot.
1683 * Add following invalid entries. */
1684 ++i;
1685 while (i < wp->w_lines_valid
1686 && !wp->w_lines[i].wl_valid)
1687 old_rows += wp->w_lines[i++].wl_size;
1688 break;
1689 }
1690#endif
1691 }
1692
1693 if (i >= wp->w_lines_valid)
1694 {
1695 /* We can't find a valid line below the changed lines,
1696 * need to redraw until the end of the window.
1697 * Inserting/deleting lines has no use. */
1698 bot_start = 0;
1699 }
1700 else
1701 {
1702 /* Able to count old number of rows: Count new window
1703 * rows, and may insert/delete lines */
1704 j = idx;
1705 for (l = lnum; l < mod_bot; ++l)
1706 {
1707#ifdef FEAT_FOLDING
1708 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1709 ++new_rows;
1710 else
1711#endif
1712#ifdef FEAT_DIFF
1713 if (l == wp->w_topline)
1714 new_rows += plines_win_nofill(wp, l, TRUE)
1715 + wp->w_topfill;
1716 else
1717#endif
1718 new_rows += plines_win(wp, l, TRUE);
1719 ++j;
1720 if (new_rows > wp->w_height - row - 2)
1721 {
1722 /* it's getting too much, must redraw the rest */
1723 new_rows = 9999;
1724 break;
1725 }
1726 }
1727 xtra_rows = new_rows - old_rows;
1728 if (xtra_rows < 0)
1729 {
1730 /* May scroll text up. If there is not enough
1731 * remaining text or scrolling fails, must redraw the
1732 * rest. If scrolling works, must redraw the text
1733 * below the scrolled text. */
1734 if (row - xtra_rows >= wp->w_height - 2)
1735 mod_bot = MAXLNUM;
1736 else
1737 {
1738 check_for_delay(FALSE);
1739 if (win_del_lines(wp, row,
1740 -xtra_rows, FALSE, FALSE) == FAIL)
1741 mod_bot = MAXLNUM;
1742 else
1743 bot_start = wp->w_height + xtra_rows;
1744 }
1745 }
1746 else if (xtra_rows > 0)
1747 {
1748 /* May scroll text down. If there is not enough
1749 * remaining text of scrolling fails, must redraw the
1750 * rest. */
1751 if (row + xtra_rows >= wp->w_height - 2)
1752 mod_bot = MAXLNUM;
1753 else
1754 {
1755 check_for_delay(FALSE);
1756 if (win_ins_lines(wp, row + old_rows,
1757 xtra_rows, FALSE, FALSE) == FAIL)
1758 mod_bot = MAXLNUM;
1759 else if (top_end > row + old_rows)
1760 /* Scrolled the part at the top that requires
1761 * updating down. */
1762 top_end += xtra_rows;
1763 }
1764 }
1765
1766 /* When not updating the rest, may need to move w_lines[]
1767 * entries. */
1768 if (mod_bot != MAXLNUM && i != j)
1769 {
1770 if (j < i)
1771 {
1772 int x = row + new_rows;
1773
1774 /* move entries in w_lines[] upwards */
1775 for (;;)
1776 {
1777 /* stop at last valid entry in w_lines[] */
1778 if (i >= wp->w_lines_valid)
1779 {
1780 wp->w_lines_valid = j;
1781 break;
1782 }
1783 wp->w_lines[j] = wp->w_lines[i];
1784 /* stop at a line that won't fit */
1785 if (x + (int)wp->w_lines[j].wl_size
1786 > wp->w_height)
1787 {
1788 wp->w_lines_valid = j + 1;
1789 break;
1790 }
1791 x += wp->w_lines[j++].wl_size;
1792 ++i;
1793 }
1794 if (bot_start > x)
1795 bot_start = x;
1796 }
1797 else /* j > i */
1798 {
1799 /* move entries in w_lines[] downwards */
1800 j -= i;
1801 wp->w_lines_valid += j;
1802 if (wp->w_lines_valid > wp->w_height)
1803 wp->w_lines_valid = wp->w_height;
1804 for (i = wp->w_lines_valid; i - j >= idx; --i)
1805 wp->w_lines[i] = wp->w_lines[i - j];
1806
1807 /* The w_lines[] entries for inserted lines are
1808 * now invalid, but wl_size may be used above.
1809 * Reset to zero. */
1810 while (i >= idx)
1811 {
1812 wp->w_lines[i].wl_size = 0;
1813 wp->w_lines[i--].wl_valid = FALSE;
1814 }
1815 }
1816 }
1817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 }
1819
1820#ifdef FEAT_FOLDING
1821 /*
1822 * When lines are folded, display one line for all of them.
1823 * Otherwise, display normally (can be several display lines when
1824 * 'wrap' is on).
1825 */
1826 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1827 if (fold_count != 0)
1828 {
1829 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1830 ++row;
1831 --fold_count;
1832 wp->w_lines[idx].wl_folded = TRUE;
1833 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1834# ifdef FEAT_SYN_HL
1835 did_update = DID_FOLD;
1836# endif
1837 }
1838 else
1839#endif
1840 if (idx < wp->w_lines_valid
1841 && wp->w_lines[idx].wl_valid
1842 && wp->w_lines[idx].wl_lnum == lnum
1843 && lnum > wp->w_topline
1844 && !(dy_flags & DY_LASTLINE)
1845 && srow + wp->w_lines[idx].wl_size > wp->w_height
1846#ifdef FEAT_DIFF
1847 && diff_check_fill(wp, lnum) == 0
1848#endif
1849 )
1850 {
1851 /* This line is not going to fit. Don't draw anything here,
1852 * will draw "@ " lines below. */
1853 row = wp->w_height + 1;
1854 }
1855 else
1856 {
1857#ifdef FEAT_SEARCH_EXTRA
1858 prepare_search_hl(wp, lnum);
1859#endif
1860#ifdef FEAT_SYN_HL
1861 /* Let the syntax stuff know we skipped a few lines. */
1862 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001863 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 syntax_end_parsing(syntax_last_parsed + 1);
1865#endif
1866
1867 /*
1868 * Display one line.
1869 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001870 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
1872#ifdef FEAT_FOLDING
1873 wp->w_lines[idx].wl_folded = FALSE;
1874 wp->w_lines[idx].wl_lastlnum = lnum;
1875#endif
1876#ifdef FEAT_SYN_HL
1877 did_update = DID_LINE;
1878 syntax_last_parsed = lnum;
1879#endif
1880 }
1881
1882 wp->w_lines[idx].wl_lnum = lnum;
1883 wp->w_lines[idx].wl_valid = TRUE;
1884 if (row > wp->w_height) /* past end of screen */
1885 {
1886 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001887 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1889 ++idx;
1890 break;
1891 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001892 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 wp->w_lines[idx].wl_size = row - srow;
1894 ++idx;
1895#ifdef FEAT_FOLDING
1896 lnum += fold_count + 1;
1897#else
1898 ++lnum;
1899#endif
1900 }
1901 else
1902 {
1903 /* This line does not need updating, advance to the next one */
1904 row += wp->w_lines[idx++].wl_size;
1905 if (row > wp->w_height) /* past end of screen */
1906 break;
1907#ifdef FEAT_FOLDING
1908 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1909#else
1910 ++lnum;
1911#endif
1912#ifdef FEAT_SYN_HL
1913 did_update = DID_NONE;
1914#endif
1915 }
1916
1917 if (lnum > buf->b_ml.ml_line_count)
1918 {
1919 eof = TRUE;
1920 break;
1921 }
1922 }
1923 /*
1924 * End of loop over all window lines.
1925 */
1926
1927
1928 if (idx > wp->w_lines_valid)
1929 wp->w_lines_valid = idx;
1930
1931#ifdef FEAT_SYN_HL
1932 /*
1933 * Let the syntax stuff know we stop parsing here.
1934 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001935 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 syntax_end_parsing(syntax_last_parsed + 1);
1937#endif
1938
1939 /*
1940 * If we didn't hit the end of the file, and we didn't finish the last
1941 * line we were working on, then the line didn't fit.
1942 */
1943 wp->w_empty_rows = 0;
1944#ifdef FEAT_DIFF
1945 wp->w_filler_rows = 0;
1946#endif
1947 if (!eof && !didline)
1948 {
1949 if (lnum == wp->w_topline)
1950 {
1951 /*
1952 * Single line that does not fit!
1953 * Don't overwrite it, it can be edited.
1954 */
1955 wp->w_botline = lnum + 1;
1956 }
1957#ifdef FEAT_DIFF
1958 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1959 {
1960 /* Window ends in filler lines. */
1961 wp->w_botline = lnum;
1962 wp->w_filler_rows = wp->w_height - srow;
1963 }
1964#endif
1965 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1966 {
1967 /*
1968 * Last line isn't finished: Display "@@@" at the end.
1969 */
1970 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1971 W_WINROW(wp) + wp->w_height,
1972 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1973 '@', '@', hl_attr(HLF_AT));
1974 set_empty_rows(wp, srow);
1975 wp->w_botline = lnum;
1976 }
1977 else
1978 {
1979 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1980 wp->w_botline = lnum;
1981 }
1982 }
1983 else
1984 {
1985#ifdef FEAT_VERTSPLIT
1986 draw_vsep_win(wp, row);
1987#endif
1988 if (eof) /* we hit the end of the file */
1989 {
1990 wp->w_botline = buf->b_ml.ml_line_count + 1;
1991#ifdef FEAT_DIFF
1992 j = diff_check_fill(wp, wp->w_botline);
1993 if (j > 0 && !wp->w_botfill)
1994 {
1995 /*
1996 * Display filler lines at the end of the file
1997 */
1998 if (char2cells(fill_diff) > 1)
1999 i = '-';
2000 else
2001 i = fill_diff;
2002 if (row + j > wp->w_height)
2003 j = wp->w_height - row;
2004 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2005 row += j;
2006 }
2007#endif
2008 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002009 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 wp->w_botline = lnum;
2011
2012 /* make sure the rest of the screen is blank */
2013 /* put '~'s on rows that aren't part of the file. */
2014 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2015 }
2016
2017 /* Reset the type of redrawing required, the window has been updated. */
2018 wp->w_redr_type = 0;
2019#ifdef FEAT_DIFF
2020 wp->w_old_topfill = wp->w_topfill;
2021 wp->w_old_botfill = wp->w_botfill;
2022#endif
2023
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002024 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026 /*
2027 * There is a trick with w_botline. If we invalidate it on each
2028 * change that might modify it, this will cause a lot of expensive
2029 * calls to plines() in update_topline() each time. Therefore the
2030 * value of w_botline is often approximated, and this value is used to
2031 * compute the value of w_topline. If the value of w_botline was
2032 * wrong, check that the value of w_topline is correct (cursor is on
2033 * the visible part of the text). If it's not, we need to redraw
2034 * again. Mostly this just means scrolling up a few lines, so it
2035 * doesn't look too bad. Only do this for the current window (where
2036 * changes are relevant).
2037 */
2038 wp->w_valid |= VALID_BOTLINE;
2039 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2040 {
2041 recursive = TRUE;
2042 curwin->w_valid &= ~VALID_TOPLINE;
2043 update_topline(); /* may invalidate w_botline again */
2044 if (must_redraw != 0)
2045 {
2046 /* Don't update for changes in buffer again. */
2047 i = curbuf->b_mod_set;
2048 curbuf->b_mod_set = FALSE;
2049 win_update(curwin);
2050 must_redraw = 0;
2051 curbuf->b_mod_set = i;
2052 }
2053 recursive = FALSE;
2054 }
2055 }
2056
2057#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2058 /* restore got_int, unless CTRL-C was hit while redrawing */
2059 if (!got_int)
2060 got_int = save_got_int;
2061#endif
2062}
2063
2064#ifdef FEAT_SIGNS
2065static int draw_signcolumn __ARGS((win_T *wp));
2066
2067/*
2068 * Return TRUE when window "wp" has a column to draw signs in.
2069 */
2070 static int
2071draw_signcolumn(wp)
2072 win_T *wp;
2073{
2074 return (wp->w_buffer->b_signlist != NULL
2075# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002076 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077# endif
2078 );
2079}
2080#endif
2081
2082/*
2083 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2084 * as the filler character.
2085 */
2086 static void
2087win_draw_end(wp, c1, c2, row, endrow, hl)
2088 win_T *wp;
2089 int c1;
2090 int c2;
2091 int row;
2092 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002093 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094{
2095#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2096 int n = 0;
2097# define FDC_OFF n
2098#else
2099# define FDC_OFF 0
2100#endif
2101
2102#ifdef FEAT_RIGHTLEFT
2103 if (wp->w_p_rl)
2104 {
2105 /* No check for cmdline window: should never be right-left. */
2106# ifdef FEAT_FOLDING
2107 n = wp->w_p_fdc;
2108
2109 if (n > 0)
2110 {
2111 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002112 if (n > W_WIDTH(wp))
2113 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2115 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2116 ' ', ' ', hl_attr(HLF_FC));
2117 }
2118# endif
2119# ifdef FEAT_SIGNS
2120 if (draw_signcolumn(wp))
2121 {
2122 int nn = n + 2;
2123
2124 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002125 if (nn > W_WIDTH(wp))
2126 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2128 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2129 ' ', ' ', hl_attr(HLF_SC));
2130 n = nn;
2131 }
2132# endif
2133 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2134 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2135 c2, c2, hl_attr(hl));
2136 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2137 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2138 c1, c2, hl_attr(hl));
2139 }
2140 else
2141#endif
2142 {
2143#ifdef FEAT_CMDWIN
2144 if (cmdwin_type != 0 && wp == curwin)
2145 {
2146 /* draw the cmdline character in the leftmost column */
2147 n = 1;
2148 if (n > wp->w_width)
2149 n = wp->w_width;
2150 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2151 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2152 cmdwin_type, ' ', hl_attr(HLF_AT));
2153 }
2154#endif
2155#ifdef FEAT_FOLDING
2156 if (wp->w_p_fdc > 0)
2157 {
2158 int nn = n + wp->w_p_fdc;
2159
2160 /* draw the fold column at the left */
2161 if (nn > W_WIDTH(wp))
2162 nn = W_WIDTH(wp);
2163 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2164 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2165 ' ', ' ', hl_attr(HLF_FC));
2166 n = nn;
2167 }
2168#endif
2169#ifdef FEAT_SIGNS
2170 if (draw_signcolumn(wp))
2171 {
2172 int nn = n + 2;
2173
2174 /* draw the sign column after the fold column */
2175 if (nn > W_WIDTH(wp))
2176 nn = W_WIDTH(wp);
2177 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2178 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2179 ' ', ' ', hl_attr(HLF_SC));
2180 n = nn;
2181 }
2182#endif
2183 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2184 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2185 c1, c2, hl_attr(hl));
2186 }
2187 set_empty_rows(wp, row);
2188}
2189
Bram Moolenaar1a384422010-07-14 19:53:30 +02002190#ifdef FEAT_SYN_HL
2191static int advance_color_col __ARGS((int vcol, int **color_cols));
2192
2193/*
2194 * Advance **color_cols and return TRUE when there are columns to draw.
2195 */
2196 static int
2197advance_color_col(vcol, color_cols)
2198 int vcol;
2199 int **color_cols;
2200{
2201 while (**color_cols >= 0 && vcol > **color_cols)
2202 ++*color_cols;
2203 return (**color_cols >= 0);
2204}
2205#endif
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207#ifdef FEAT_FOLDING
2208/*
2209 * Display one folded line.
2210 */
2211 static void
2212fold_line(wp, fold_count, foldinfo, lnum, row)
2213 win_T *wp;
2214 long fold_count;
2215 foldinfo_T *foldinfo;
2216 linenr_T lnum;
2217 int row;
2218{
2219 char_u buf[51];
2220 pos_T *top, *bot;
2221 linenr_T lnume = lnum + fold_count - 1;
2222 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002223 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 int col;
2226 int txtcol;
2227 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002228 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 /* Build the fold line:
2231 * 1. Add the cmdwin_type for the command-line window
2232 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002233 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 * 4. Compose the text
2235 * 5. Add the text
2236 * 6. set highlighting for the Visual area an other text
2237 */
2238 col = 0;
2239
2240 /*
2241 * 1. Add the cmdwin_type for the command-line window
2242 * Ignores 'rightleft', this window is never right-left.
2243 */
2244#ifdef FEAT_CMDWIN
2245 if (cmdwin_type != 0 && wp == curwin)
2246 {
2247 ScreenLines[off] = cmdwin_type;
2248 ScreenAttrs[off] = hl_attr(HLF_AT);
2249#ifdef FEAT_MBYTE
2250 if (enc_utf8)
2251 ScreenLinesUC[off] = 0;
2252#endif
2253 ++col;
2254 }
2255#endif
2256
2257 /*
2258 * 2. Add the 'foldcolumn'
2259 */
2260 fdc = wp->w_p_fdc;
2261 if (fdc > W_WIDTH(wp) - col)
2262 fdc = W_WIDTH(wp) - col;
2263 if (fdc > 0)
2264 {
2265 fill_foldcolumn(buf, wp, TRUE, lnum);
2266#ifdef FEAT_RIGHTLEFT
2267 if (wp->w_p_rl)
2268 {
2269 int i;
2270
2271 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2272 hl_attr(HLF_FC));
2273 /* reverse the fold column */
2274 for (i = 0; i < fdc; ++i)
2275 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2276 }
2277 else
2278#endif
2279 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2280 col += fdc;
2281 }
2282
2283#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002284# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2285 for (ri = 0; ri < l; ++ri) \
2286 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2287 else \
2288 for (ri = 0; ri < l; ++ri) \
2289 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002291# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2292 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293#endif
2294
Bram Moolenaar64486672010-05-16 15:46:46 +02002295 /* Set all attributes of the 'number' or 'relativenumber' column and the
2296 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002297 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299#ifdef FEAT_SIGNS
2300 /* If signs are being displayed, add two spaces. */
2301 if (draw_signcolumn(wp))
2302 {
2303 len = W_WIDTH(wp) - col;
2304 if (len > 0)
2305 {
2306 if (len > 2)
2307 len = 2;
2308# ifdef FEAT_RIGHTLEFT
2309 if (wp->w_p_rl)
2310 /* the line number isn't reversed */
2311 copy_text_attr(off + W_WIDTH(wp) - len - col,
2312 (char_u *)" ", len, hl_attr(HLF_FL));
2313 else
2314# endif
2315 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2316 col += len;
2317 }
2318 }
2319#endif
2320
2321 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002322 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002324 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
2326 len = W_WIDTH(wp) - col;
2327 if (len > 0)
2328 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002329 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002330 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01002331 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002332
2333 if (len > w + 1)
2334 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002335
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002336 if (wp->w_p_nu && !wp->w_p_rnu)
2337 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002338 num = (long)lnum;
2339 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002340 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002341 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002342 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002343 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002344 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002345 /* 'number' + 'relativenumber': cursor line shows absolute
2346 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002347 num = lnum;
2348 fmt = "%-*ld ";
2349 }
2350 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002351
Bram Moolenaar700e7342013-01-30 12:31:36 +01002352 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#ifdef FEAT_RIGHTLEFT
2354 if (wp->w_p_rl)
2355 /* the line number isn't reversed */
2356 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2357 hl_attr(HLF_FL));
2358 else
2359#endif
2360 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2361 col += len;
2362 }
2363 }
2364
2365 /*
2366 * 4. Compose the folded-line string with 'foldtext', if set.
2367 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002368 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 txtcol = col; /* remember where text starts */
2371
2372 /*
2373 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2374 * Right-left text is put in columns 0 - number-col, normal text is put
2375 * in columns number-col - window-width.
2376 */
2377#ifdef FEAT_MBYTE
2378 if (has_mbyte)
2379 {
2380 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002381 int u8c, u8cc[MAX_MCO];
2382 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 int idx;
2384 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002385 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386# ifdef FEAT_ARABIC
2387 int prev_c = 0; /* previous Arabic character */
2388 int prev_c1 = 0; /* first composing char for prev_c */
2389# endif
2390
2391# ifdef FEAT_RIGHTLEFT
2392 if (wp->w_p_rl)
2393 idx = off;
2394 else
2395# endif
2396 idx = off + col;
2397
2398 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2399 for (p = text; *p != NUL; )
2400 {
2401 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002402 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (col + cells > W_WIDTH(wp)
2404# ifdef FEAT_RIGHTLEFT
2405 - (wp->w_p_rl ? col : 0)
2406# endif
2407 )
2408 break;
2409 ScreenLines[idx] = *p;
2410 if (enc_utf8)
2411 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002412 u8c = utfc_ptr2char(p, u8cc);
2413 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 ScreenLinesUC[idx] = 0;
2416#ifdef FEAT_ARABIC
2417 prev_c = u8c;
2418#endif
2419 }
2420 else
2421 {
2422#ifdef FEAT_ARABIC
2423 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2424 {
2425 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002426 int pc, pc1, nc;
2427 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 int firstbyte = *p;
2429
2430 /* The idea of what is the previous and next
2431 * character depends on 'rightleft'. */
2432 if (wp->w_p_rl)
2433 {
2434 pc = prev_c;
2435 pc1 = prev_c1;
2436 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002437 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 else
2440 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002441 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002443 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
2445 prev_c = u8c;
2446
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002447 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 pc, pc1, nc);
2449 ScreenLines[idx] = firstbyte;
2450 }
2451 else
2452 prev_c = u8c;
2453#endif
2454 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002455#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (u8c >= 0x10000)
2457 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2458 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002461 for (i = 0; i < Screen_mco; ++i)
2462 {
2463 ScreenLinesC[i][idx] = u8cc[i];
2464 if (u8cc[i] == 0)
2465 break;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
2468 if (cells > 1)
2469 ScreenLines[idx + 1] = 0;
2470 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002471 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2472 /* double-byte single width character */
2473 ScreenLines2[idx] = p[1];
2474 else if (cells > 1)
2475 /* double-width character */
2476 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 col += cells;
2478 idx += cells;
2479 p += c_len;
2480 }
2481 }
2482 else
2483#endif
2484 {
2485 len = (int)STRLEN(text);
2486 if (len > W_WIDTH(wp) - col)
2487 len = W_WIDTH(wp) - col;
2488 if (len > 0)
2489 {
2490#ifdef FEAT_RIGHTLEFT
2491 if (wp->w_p_rl)
2492 STRNCPY(current_ScreenLine, text, len);
2493 else
2494#endif
2495 STRNCPY(current_ScreenLine + col, text, len);
2496 col += len;
2497 }
2498 }
2499
2500 /* Fill the rest of the line with the fold filler */
2501#ifdef FEAT_RIGHTLEFT
2502 if (wp->w_p_rl)
2503 col -= txtcol;
2504#endif
2505 while (col < W_WIDTH(wp)
2506#ifdef FEAT_RIGHTLEFT
2507 - (wp->w_p_rl ? txtcol : 0)
2508#endif
2509 )
2510 {
2511#ifdef FEAT_MBYTE
2512 if (enc_utf8)
2513 {
2514 if (fill_fold >= 0x80)
2515 {
2516 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002517 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519 else
2520 ScreenLinesUC[off + col] = 0;
2521 }
2522#endif
2523 ScreenLines[off + col++] = fill_fold;
2524 }
2525
2526 if (text != buf)
2527 vim_free(text);
2528
2529 /*
2530 * 6. set highlighting for the Visual area an other text.
2531 * If all folded lines are in the Visual area, highlight the line.
2532 */
2533#ifdef FEAT_VISUAL
2534 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2535 {
2536 if (ltoreq(curwin->w_cursor, VIsual))
2537 {
2538 /* Visual is after curwin->w_cursor */
2539 top = &curwin->w_cursor;
2540 bot = &VIsual;
2541 }
2542 else
2543 {
2544 /* Visual is before curwin->w_cursor */
2545 top = &VIsual;
2546 bot = &curwin->w_cursor;
2547 }
2548 if (lnum >= top->lnum
2549 && lnume <= bot->lnum
2550 && (VIsual_mode != 'v'
2551 || ((lnum > top->lnum
2552 || (lnum == top->lnum
2553 && top->col == 0))
2554 && (lnume < bot->lnum
2555 || (lnume == bot->lnum
2556 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002557 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 if (VIsual_mode == Ctrl_V)
2560 {
2561 /* Visual block mode: highlight the chars part of the block */
2562 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2563 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002564 if (wp->w_old_cursor_lcol != MAXCOL
2565 && wp->w_old_cursor_lcol + txtcol
2566 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 len = wp->w_old_cursor_lcol;
2568 else
2569 len = W_WIDTH(wp) - txtcol;
2570 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002571 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573 }
2574 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002577 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580 }
2581#endif
2582
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002583#ifdef FEAT_SYN_HL
2584 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002585 if (wp->w_p_cuc)
2586 {
2587 txtcol += wp->w_virtcol;
2588 if (wp->w_p_wrap)
2589 txtcol -= wp->w_skipcol;
2590 else
2591 txtcol -= wp->w_leftcol;
2592 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2593 ScreenAttrs[off + txtcol] = hl_combine_attr(
2594 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2595 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
2598 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2599 (int)W_WIDTH(wp), FALSE);
2600
2601 /*
2602 * Update w_cline_height and w_cline_folded if the cursor line was
2603 * updated (saves a call to plines() later).
2604 */
2605 if (wp == curwin
2606 && lnum <= curwin->w_cursor.lnum
2607 && lnume >= curwin->w_cursor.lnum)
2608 {
2609 curwin->w_cline_row = row;
2610 curwin->w_cline_height = 1;
2611 curwin->w_cline_folded = TRUE;
2612 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2613 }
2614}
2615
2616/*
2617 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2618 */
2619 static void
2620copy_text_attr(off, buf, len, attr)
2621 int off;
2622 char_u *buf;
2623 int len;
2624 int attr;
2625{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002626 int i;
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 mch_memmove(ScreenLines + off, buf, (size_t)len);
2629# ifdef FEAT_MBYTE
2630 if (enc_utf8)
2631 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2632# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002633 for (i = 0; i < len; ++i)
2634 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635}
2636
2637/*
2638 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002639 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 */
2641 static void
2642fill_foldcolumn(p, wp, closed, lnum)
2643 char_u *p;
2644 win_T *wp;
2645 int closed; /* TRUE of FALSE */
2646 linenr_T lnum; /* current line number */
2647{
2648 int i = 0;
2649 int level;
2650 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002651 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 /* Init to all spaces. */
2654 copy_spaces(p, (size_t)wp->w_p_fdc);
2655
2656 level = win_foldinfo.fi_level;
2657 if (level > 0)
2658 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002659 /* If there is only one column put more info in it. */
2660 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2661
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 /* If the column is too narrow, we start at the lowest level that
2663 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002664 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (first_level < 1)
2666 first_level = 1;
2667
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002668 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 if (win_foldinfo.fi_lnum == lnum
2671 && first_level + i >= win_foldinfo.fi_low_level)
2672 p[i] = '-';
2673 else if (first_level == 1)
2674 p[i] = '|';
2675 else if (first_level + i <= 9)
2676 p[i] = '0' + first_level + i;
2677 else
2678 p[i] = '>';
2679 if (first_level + i == level)
2680 break;
2681 }
2682 }
2683 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002684 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685}
2686#endif /* FEAT_FOLDING */
2687
2688/*
2689 * Display line "lnum" of window 'wp' on the screen.
2690 * Start at row "startrow", stop when "endrow" is reached.
2691 * wp->w_virtcol needs to be valid.
2692 *
2693 * Return the number of last row the line occupies.
2694 */
2695 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002696win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 win_T *wp;
2698 linenr_T lnum;
2699 int startrow;
2700 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002701 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 int col; /* visual column on screen */
2704 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2705 int c = 0; /* init for GCC */
2706 long vcol = 0; /* virtual column (for tabs) */
2707 long vcol_prev = -1; /* "vcol" of previous character */
2708 char_u *line; /* current line */
2709 char_u *ptr; /* current position in "line" */
2710 int row; /* row in the window, excl w_winrow */
2711 int screen_row; /* row on the screen, incl w_winrow */
2712
2713 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2714 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002715 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 int c_extra = NUL; /* extra chars, all the same */
2717 int extra_attr = 0; /* attributes when n_extra != 0 */
2718 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2719 displaying lcs_eol at end-of-line */
2720 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2721 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2722
2723 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2724 int saved_n_extra = 0;
2725 char_u *saved_p_extra = NULL;
2726 int saved_c_extra = 0;
2727 int saved_char_attr = 0;
2728
2729 int n_attr = 0; /* chars with special attr */
2730 int saved_attr2 = 0; /* char_attr saved for n_attr */
2731 int n_attr3 = 0; /* chars with overruling special attr */
2732 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2733
2734 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2735
2736 int fromcol, tocol; /* start/end of inverting */
2737 int fromcol_prev = -2; /* start of inverting after cursor */
2738 int noinvcur = FALSE; /* don't invert the cursor */
2739#ifdef FEAT_VISUAL
2740 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002741 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742#endif
2743 pos_T pos;
2744 long v;
2745
2746 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002747 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2749 in this line */
2750 int attr = 0; /* attributes for area highlighting */
2751 int area_attr = 0; /* attributes desired by highlighting */
2752 int search_attr = 0; /* attributes desired by 'hlsearch' */
2753#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002754 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 int syntax_attr = 0; /* attributes desired by syntax */
2756 int has_syntax = FALSE; /* this buffer has syntax highl. */
2757 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002758 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002759 int draw_color_col = FALSE; /* highlight colorcolumn */
2760 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002761#endif
2762#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002763 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002764# define SPWORDLEN 150
2765 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002766 int nextlinecol = 0; /* column where nextline[] starts */
2767 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002768 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002769 int spell_attr = 0; /* attributes desired by spelling */
2770 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002771 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2772 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002773 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002774 static int cap_col = -1; /* column to check for Cap word */
2775 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002776 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#endif
2778 int extra_check; /* has syntax or linebreak */
2779#ifdef FEAT_MBYTE
2780 int multi_attr = 0; /* attributes desired by multibyte */
2781 int mb_l = 1; /* multi-byte byte length */
2782 int mb_c = 0; /* decoded multi-byte character */
2783 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002784 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785#endif
2786#ifdef FEAT_DIFF
2787 int filler_lines; /* nr of filler lines to be drawn */
2788 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002789 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 int change_start = MAXCOL; /* first col of changed area */
2791 int change_end = -1; /* last col of changed area */
2792#endif
2793 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2794#ifdef FEAT_LINEBREAK
2795 int need_showbreak = FALSE;
2796#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002797#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2798 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002800 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801#endif
2802#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002803 matchitem_T *cur; /* points to the match list */
2804 match_T *shl; /* points to search_hl or a match */
2805 int shl_flag; /* flag to indicate whether search_hl
2806 has been processed or not */
2807 int prevcol_hl_flag; /* flag to indicate whether prevcol
2808 equals startcol of search_hl or one
2809 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810#endif
2811#ifdef FEAT_ARABIC
2812 int prev_c = 0; /* previous Arabic character */
2813 int prev_c1 = 0; /* first composing char for prev_c */
2814#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002815#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002816 int did_line_attr = 0;
2817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
2819 /* draw_state: items that are drawn in sequence: */
2820#define WL_START 0 /* nothing done yet */
2821#ifdef FEAT_CMDWIN
2822# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2823#else
2824# define WL_CMDLINE WL_START
2825#endif
2826#ifdef FEAT_FOLDING
2827# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2828#else
2829# define WL_FOLD WL_CMDLINE
2830#endif
2831#ifdef FEAT_SIGNS
2832# define WL_SIGN WL_FOLD + 1 /* column for signs */
2833#else
2834# define WL_SIGN WL_FOLD /* column for signs */
2835#endif
2836#define WL_NR WL_SIGN + 1 /* line number */
2837#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2838# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2839#else
2840# define WL_SBR WL_NR
2841#endif
2842#define WL_LINE WL_SBR + 1 /* text in the line */
2843 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002844#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 int feedback_col = 0;
2846 int feedback_old_attr = -1;
2847#endif
2848
Bram Moolenaar860cae12010-06-05 23:22:07 +02002849#ifdef FEAT_CONCEAL
2850 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002851 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002852 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002853 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002854 int is_concealing = FALSE;
2855 int boguscols = 0; /* nonexistent columns added to force
2856 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002857 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002858 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002859# define VCOL_HLC (vcol - vcol_off)
2860#else
2861# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
2864 if (startrow > endrow) /* past the end already! */
2865 return startrow;
2866
2867 row = startrow;
2868 screen_row = row + W_WINROW(wp);
2869
2870 /*
2871 * To speed up the loop below, set extra_check when there is linebreak,
2872 * trailing white space and/or syntax processing to be done.
2873 */
2874#ifdef FEAT_LINEBREAK
2875 extra_check = wp->w_p_lbr;
2876#else
2877 extra_check = 0;
2878#endif
2879#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002880 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
2882 /* Prepare for syntax highlighting in this line. When there is an
2883 * error, stop syntax highlighting. */
2884 save_did_emsg = did_emsg;
2885 did_emsg = FALSE;
2886 syntax_start(wp, lnum);
2887 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002888 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 else
2890 {
2891 did_emsg = save_did_emsg;
2892 has_syntax = TRUE;
2893 extra_check = TRUE;
2894 }
2895 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002896
2897 /* Check for columns to display for 'colorcolumn'. */
2898 color_cols = wp->w_p_cc_cols;
2899 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002900 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002901#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002902
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002903#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002904 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002905 && *wp->w_s->b_p_spl != NUL
2906 && wp->w_s->b_langp.ga_len > 0
2907 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002908 {
2909 /* Prepare for spell checking. */
2910 has_spell = TRUE;
2911 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002912
2913 /* Get the start of the next line, so that words that wrap to the next
2914 * line are found too: "et<line-break>al.".
2915 * Trick: skip a few chars for C/shell/Vim comments */
2916 nextline[SPWORDLEN] = NUL;
2917 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2918 {
2919 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2920 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2921 }
2922
2923 /* When a word wrapped from the previous line the start of the current
2924 * line is valid. */
2925 if (lnum == checked_lnum)
2926 cur_checked_col = checked_col;
2927 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002928
2929 /* When there was a sentence end in the previous line may require a
2930 * word starting with capital in this line. In line 1 always check
2931 * the first word. */
2932 if (lnum != capcol_lnum)
2933 cap_col = -1;
2934 if (lnum == 1)
2935 cap_col = 0;
2936 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938#endif
2939
2940 /*
2941 * handle visual active in this window
2942 */
2943 fromcol = -10;
2944 tocol = MAXCOL;
2945#ifdef FEAT_VISUAL
2946 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2947 {
2948 /* Visual is after curwin->w_cursor */
2949 if (ltoreq(curwin->w_cursor, VIsual))
2950 {
2951 top = &curwin->w_cursor;
2952 bot = &VIsual;
2953 }
2954 else /* Visual is before curwin->w_cursor */
2955 {
2956 top = &VIsual;
2957 bot = &curwin->w_cursor;
2958 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002959 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 if (VIsual_mode == Ctrl_V) /* block mode */
2961 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002962 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 {
2964 fromcol = wp->w_old_cursor_fcol;
2965 tocol = wp->w_old_cursor_lcol;
2966 }
2967 }
2968 else /* non-block mode */
2969 {
2970 if (lnum > top->lnum && lnum <= bot->lnum)
2971 fromcol = 0;
2972 else if (lnum == top->lnum)
2973 {
2974 if (VIsual_mode == 'V') /* linewise */
2975 fromcol = 0;
2976 else
2977 {
2978 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2979 if (gchar_pos(top) == NUL)
2980 tocol = fromcol + 1;
2981 }
2982 }
2983 if (VIsual_mode != 'V' && lnum == bot->lnum)
2984 {
2985 if (*p_sel == 'e' && bot->col == 0
2986#ifdef FEAT_VIRTUALEDIT
2987 && bot->coladd == 0
2988#endif
2989 )
2990 {
2991 fromcol = -10;
2992 tocol = MAXCOL;
2993 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002994 else if (bot->col == MAXCOL)
2995 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 else
2997 {
2998 pos = *bot;
2999 if (*p_sel == 'e')
3000 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3001 else
3002 {
3003 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3004 ++tocol;
3005 }
3006 }
3007 }
3008 }
3009
3010#ifndef MSDOS
3011 /* Check if the character under the cursor should not be inverted */
3012 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3013# ifdef FEAT_GUI
3014 && !gui.in_use
3015# endif
3016 )
3017 noinvcur = TRUE;
3018#endif
3019
3020 /* if inverting in this line set area_highlighting */
3021 if (fromcol >= 0)
3022 {
3023 area_highlighting = TRUE;
3024 attr = hl_attr(HLF_V);
3025#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003026 if ((clip_star.available && !clip_star.owned
3027 && clip_isautosel_star())
3028 || (clip_plus.available && !clip_plus.owned
3029 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 attr = hl_attr(HLF_VNC);
3031#endif
3032 }
3033 }
3034
3035 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003036 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 */
3038 else
3039#endif /* FEAT_VISUAL */
3040 if (highlight_match
3041 && wp == curwin
3042 && lnum >= curwin->w_cursor.lnum
3043 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3044 {
3045 if (lnum == curwin->w_cursor.lnum)
3046 getvcol(curwin, &(curwin->w_cursor),
3047 (colnr_T *)&fromcol, NULL, NULL);
3048 else
3049 fromcol = 0;
3050 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3051 {
3052 pos.lnum = lnum;
3053 pos.col = search_match_endcol;
3054 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3055 }
3056 else
3057 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003058 /* do at least one character; happens when past end of line */
3059 if (fromcol == tocol)
3060 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 area_highlighting = TRUE;
3062 attr = hl_attr(HLF_I);
3063 }
3064
3065#ifdef FEAT_DIFF
3066 filler_lines = diff_check(wp, lnum);
3067 if (filler_lines < 0)
3068 {
3069 if (filler_lines == -1)
3070 {
3071 if (diff_find_change(wp, lnum, &change_start, &change_end))
3072 diff_hlf = HLF_ADD; /* added line */
3073 else if (change_start == 0)
3074 diff_hlf = HLF_TXD; /* changed text */
3075 else
3076 diff_hlf = HLF_CHD; /* changed line */
3077 }
3078 else
3079 diff_hlf = HLF_ADD; /* added line */
3080 filler_lines = 0;
3081 area_highlighting = TRUE;
3082 }
3083 if (lnum == wp->w_topline)
3084 filler_lines = wp->w_topfill;
3085 filler_todo = filler_lines;
3086#endif
3087
3088#ifdef LINE_ATTR
3089# ifdef FEAT_SIGNS
3090 /* If this line has a sign with line highlighting set line_attr. */
3091 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3092 if (v != 0)
3093 line_attr = sign_get_attr((int)v, TRUE);
3094# endif
3095# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3096 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003097 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 line_attr = hl_attr(HLF_L);
3099# endif
3100 if (line_attr != 0)
3101 area_highlighting = TRUE;
3102#endif
3103
3104 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3105 ptr = line;
3106
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003107#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003108 if (has_spell)
3109 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003110 /* For checking first word with a capital skip white space. */
3111 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003112 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003113
Bram Moolenaar30abd282005-06-22 22:35:10 +00003114 /* To be able to spell-check over line boundaries copy the end of the
3115 * current line into nextline[]. Above the start of the next line was
3116 * copied to nextline[SPWORDLEN]. */
3117 if (nextline[SPWORDLEN] == NUL)
3118 {
3119 /* No next line or it is empty. */
3120 nextlinecol = MAXCOL;
3121 nextline_idx = 0;
3122 }
3123 else
3124 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003125 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003126 if (v < SPWORDLEN)
3127 {
3128 /* Short line, use it completely and append the start of the
3129 * next line. */
3130 nextlinecol = 0;
3131 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003132 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003133 nextline_idx = v + 1;
3134 }
3135 else
3136 {
3137 /* Long line, use only the last SPWORDLEN bytes. */
3138 nextlinecol = v - SPWORDLEN;
3139 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3140 nextline_idx = SPWORDLEN + 1;
3141 }
3142 }
3143 }
3144#endif
3145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 /* find start of trailing whitespace */
3147 if (wp->w_p_list && lcs_trail)
3148 {
3149 trailcol = (colnr_T)STRLEN(ptr);
3150 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3151 --trailcol;
3152 trailcol += (colnr_T) (ptr - line);
3153 extra_check = TRUE;
3154 }
3155
3156 /*
3157 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3158 * first character to be displayed.
3159 */
3160 if (wp->w_p_wrap)
3161 v = wp->w_skipcol;
3162 else
3163 v = wp->w_leftcol;
3164 if (v > 0)
3165 {
3166#ifdef FEAT_MBYTE
3167 char_u *prev_ptr = ptr;
3168#endif
3169 while (vcol < v && *ptr != NUL)
3170 {
3171 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3172 vcol += c;
3173#ifdef FEAT_MBYTE
3174 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003176 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003179#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3180 /* When:
3181 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003182 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003183 * - 'virtualedit' is set, or
3184 * - the visual mode is active,
3185 * the end of the line may be before the start of the displayed part.
3186 */
3187 if (vcol < v && (
3188# ifdef FEAT_SYN_HL
3189 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003190 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003191# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3192 ||
3193# endif
3194# endif
3195# ifdef FEAT_VIRTUALEDIT
3196 virtual_active()
3197# ifdef FEAT_VISUAL
3198 ||
3199# endif
3200# endif
3201# ifdef FEAT_VISUAL
3202 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3203# endif
3204 ))
3205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#endif
3209
3210 /* Handle a character that's not completely on the screen: Put ptr at
3211 * that character but skip the first few screen characters. */
3212 if (vcol > v)
3213 {
3214 vcol -= c;
3215#ifdef FEAT_MBYTE
3216 ptr = prev_ptr;
3217#else
3218 --ptr;
3219#endif
3220 n_skip = v - vcol;
3221 }
3222
3223 /*
3224 * Adjust for when the inverted text is before the screen,
3225 * and when the start of the inverted text is before the screen.
3226 */
3227 if (tocol <= vcol)
3228 fromcol = 0;
3229 else if (fromcol >= 0 && fromcol < vcol)
3230 fromcol = vcol;
3231
3232#ifdef FEAT_LINEBREAK
3233 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3234 if (wp->w_p_wrap)
3235 need_showbreak = TRUE;
3236#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003237#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003238 /* When spell checking a word we need to figure out the start of the
3239 * word and if it's badly spelled or not. */
3240 if (has_spell)
3241 {
3242 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003243 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003244 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003245
3246 pos = wp->w_cursor;
3247 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003248 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003249 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003250
3251 /* spell_move_to() may call ml_get() and make "line" invalid */
3252 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3253 ptr = line + linecol;
3254
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003255 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003256 {
3257 /* no bad word found at line start, don't check until end of a
3258 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003259 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003260 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003261 }
3262 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003263 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003264 /* bad word found, use attributes until end of word */
3265 word_end = wp->w_cursor.col + len + 1;
3266
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003267 /* Turn index into actual attributes. */
3268 if (spell_hlf != HLF_COUNT)
3269 spell_attr = highlight_attr[spell_hlf];
3270 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003271 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003272
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003273# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003274 /* Need to restart syntax highlighting for this line. */
3275 if (has_syntax)
3276 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003277# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003278 }
3279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281
3282 /*
3283 * Correct highlighting for cursor that can't be disabled.
3284 * Avoids having to check this for each character.
3285 */
3286 if (fromcol >= 0)
3287 {
3288 if (noinvcur)
3289 {
3290 if ((colnr_T)fromcol == wp->w_virtcol)
3291 {
3292 /* highlighting starts at cursor, let it start just after the
3293 * cursor */
3294 fromcol_prev = fromcol;
3295 fromcol = -1;
3296 }
3297 else if ((colnr_T)fromcol < wp->w_virtcol)
3298 /* restart highlighting after the cursor */
3299 fromcol_prev = wp->w_virtcol;
3300 }
3301 if (fromcol >= tocol)
3302 fromcol = -1;
3303 }
3304
3305#ifdef FEAT_SEARCH_EXTRA
3306 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003307 * Handle highlighting the last used search pattern and matches.
3308 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003310 cur = wp->w_match_head;
3311 shl_flag = FALSE;
3312 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003314 if (shl_flag == FALSE)
3315 {
3316 shl = &search_hl;
3317 shl_flag = TRUE;
3318 }
3319 else
3320 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003321 shl->startcol = MAXCOL;
3322 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 shl->attr_cur = 0;
3324 if (shl->rm.regprog != NULL)
3325 {
3326 v = (long)(ptr - line);
3327 next_search_hl(wp, shl, lnum, (colnr_T)v);
3328
3329 /* Need to get the line again, a multi-line regexp may have made it
3330 * invalid. */
3331 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3332 ptr = line + v;
3333
3334 if (shl->lnum != 0 && shl->lnum <= lnum)
3335 {
3336 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003337 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003339 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3341 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003342 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003344 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003346 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003349 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003350 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 else
3352#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003353 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003355 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
3357 shl->attr_cur = shl->attr;
3358 search_attr = shl->attr;
3359 }
3360 area_highlighting = TRUE;
3361 }
3362 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003363 if (shl != &search_hl && cur != NULL)
3364 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366#endif
3367
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003368#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003369 /* Cursor line highlighting for 'cursorline' in the current window. Not
3370 * when Visual mode is active, because it's not clear what is selected
3371 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003372 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
3373 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003374 {
3375 line_attr = hl_attr(HLF_CUL);
3376 area_highlighting = TRUE;
3377 }
3378#endif
3379
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003380 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 col = 0;
3382#ifdef FEAT_RIGHTLEFT
3383 if (wp->w_p_rl)
3384 {
3385 /* Rightleft window: process the text in the normal direction, but put
3386 * it in current_ScreenLine[] from right to left. Start at the
3387 * rightmost column of the window. */
3388 col = W_WIDTH(wp) - 1;
3389 off += col;
3390 }
3391#endif
3392
3393 /*
3394 * Repeat for the whole displayed line.
3395 */
3396 for (;;)
3397 {
3398 /* Skip this quickly when working on the text. */
3399 if (draw_state != WL_LINE)
3400 {
3401#ifdef FEAT_CMDWIN
3402 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3403 {
3404 draw_state = WL_CMDLINE;
3405 if (cmdwin_type != 0 && wp == curwin)
3406 {
3407 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003409 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 char_attr = hl_attr(HLF_AT);
3411 }
3412 }
3413#endif
3414
3415#ifdef FEAT_FOLDING
3416 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3417 {
3418 draw_state = WL_FOLD;
3419 if (wp->w_p_fdc > 0)
3420 {
3421 /* Draw the 'foldcolumn'. */
3422 fill_foldcolumn(extra, wp, FALSE, lnum);
3423 n_extra = wp->w_p_fdc;
3424 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003425 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 c_extra = NUL;
3427 char_attr = hl_attr(HLF_FC);
3428 }
3429 }
3430#endif
3431
3432#ifdef FEAT_SIGNS
3433 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3434 {
3435 draw_state = WL_SIGN;
3436 /* Show the sign column when there are any signs in this
3437 * buffer or when using Netbeans. */
3438 if (draw_signcolumn(wp)
3439# ifdef FEAT_DIFF
3440 && filler_todo <= 0
3441# endif
3442 )
3443 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003444 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003446 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447# endif
3448
3449 /* Draw two cells with the sign value or blank. */
3450 c_extra = ' ';
3451 char_attr = hl_attr(HLF_SC);
3452 n_extra = 2;
3453
3454 if (row == startrow)
3455 {
3456 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3457 SIGN_TEXT);
3458# ifdef FEAT_SIGN_ICONS
3459 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3460 SIGN_ICON);
3461 if (gui.in_use && icon_sign != 0)
3462 {
3463 /* Use the image in this position. */
3464 c_extra = SIGN_BYTE;
3465# ifdef FEAT_NETBEANS_INTG
3466 if (buf_signcount(wp->w_buffer, lnum) > 1)
3467 c_extra = MULTISIGN_BYTE;
3468# endif
3469 char_attr = icon_sign;
3470 }
3471 else
3472# endif
3473 if (text_sign != 0)
3474 {
3475 p_extra = sign_get_text(text_sign);
3476 if (p_extra != NULL)
3477 {
3478 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003479 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 char_attr = sign_get_attr(text_sign, FALSE);
3482 }
3483 }
3484 }
3485 }
3486#endif
3487
3488 if (draw_state == WL_NR - 1 && n_extra == 0)
3489 {
3490 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003491 /* Display the absolute or relative line number. After the
3492 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3493 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 && (row == startrow
3495#ifdef FEAT_DIFF
3496 + filler_lines
3497#endif
3498 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3499 {
3500 /* Draw the line number (empty space after wrapping). */
3501 if (row == startrow
3502#ifdef FEAT_DIFF
3503 + filler_lines
3504#endif
3505 )
3506 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003507 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003508 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003509
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003510 if (wp->w_p_nu && !wp->w_p_rnu)
3511 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003512 num = (long)lnum;
3513 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003514 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003515 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003516 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003517 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003518 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003519 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003520 num = lnum;
3521 fmt = "%-*ld ";
3522 }
3523 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003524
Bram Moolenaar700e7342013-01-30 12:31:36 +01003525 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003526 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 if (wp->w_skipcol > 0)
3528 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3529 *p_extra = '-';
3530#ifdef FEAT_RIGHTLEFT
3531 if (wp->w_p_rl) /* reverse line numbers */
3532 rl_mirror(extra);
3533#endif
3534 p_extra = extra;
3535 c_extra = NUL;
3536 }
3537 else
3538 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003539 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003541#ifdef FEAT_SYN_HL
3542 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003543 * the current line differently.
3544 * TODO: Can we use CursorLine instead of CursorLineNr
3545 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003546 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003547 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003548 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551 }
3552
3553#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3554 if (draw_state == WL_SBR - 1 && n_extra == 0)
3555 {
3556 draw_state = WL_SBR;
3557# ifdef FEAT_DIFF
3558 if (filler_todo > 0)
3559 {
3560 /* Draw "deleted" diff line(s). */
3561 if (char2cells(fill_diff) > 1)
3562 c_extra = '-';
3563 else
3564 c_extra = fill_diff;
3565# ifdef FEAT_RIGHTLEFT
3566 if (wp->w_p_rl)
3567 n_extra = col + 1;
3568 else
3569# endif
3570 n_extra = W_WIDTH(wp) - col;
3571 char_attr = hl_attr(HLF_DED);
3572 }
3573# endif
3574# ifdef FEAT_LINEBREAK
3575 if (*p_sbr != NUL && need_showbreak)
3576 {
3577 /* Draw 'showbreak' at the start of each broken line. */
3578 p_extra = p_sbr;
3579 c_extra = NUL;
3580 n_extra = (int)STRLEN(p_sbr);
3581 char_attr = hl_attr(HLF_AT);
3582 need_showbreak = FALSE;
3583 /* Correct end of highlighted area for 'showbreak',
3584 * required when 'linebreak' is also set. */
3585 if (tocol == vcol)
3586 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003587#ifdef FEAT_SYN_HL
3588 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003589 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003590 char_attr = hl_combine_attr(char_attr, HLF_CLN);
3591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
3593# endif
3594 }
3595#endif
3596
3597 if (draw_state == WL_LINE - 1 && n_extra == 0)
3598 {
3599 draw_state = WL_LINE;
3600 if (saved_n_extra)
3601 {
3602 /* Continue item from end of wrapped line. */
3603 n_extra = saved_n_extra;
3604 c_extra = saved_c_extra;
3605 p_extra = saved_p_extra;
3606 char_attr = saved_char_attr;
3607 }
3608 else
3609 char_attr = 0;
3610 }
3611 }
3612
3613 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003614 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003615 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616#ifdef FEAT_DIFF
3617 && filler_todo <= 0
3618#endif
3619 )
3620 {
3621 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3622 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003623 /* Pretend we have finished updating the window. Except when
3624 * 'cursorcolumn' is set. */
3625#ifdef FEAT_SYN_HL
3626 if (wp->w_p_cuc)
3627 row = wp->w_cline_row + wp->w_cline_height;
3628 else
3629#endif
3630 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 break;
3632 }
3633
3634 if (draw_state == WL_LINE && area_highlighting)
3635 {
3636 /* handle Visual or match highlighting in this line */
3637 if (vcol == fromcol
3638#ifdef FEAT_MBYTE
3639 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3640 && (*mb_ptr2cells)(ptr) > 1)
3641#endif
3642 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003643 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 && vcol < tocol))
3645 area_attr = attr; /* start highlighting */
3646 else if (area_attr != 0
3647 && (vcol == tocol
3648 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651#ifdef FEAT_SEARCH_EXTRA
3652 if (!n_extra)
3653 {
3654 /*
3655 * Check for start/end of search pattern match.
3656 * After end, check for start/end of next match.
3657 * When another match, have to check for start again.
3658 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003659 * Do this for 'search_hl' and the match list (ordered by
3660 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003662 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003663 cur = wp->w_match_head;
3664 shl_flag = FALSE;
3665 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003667 if (shl_flag == FALSE
3668 && ((cur != NULL
3669 && cur->priority > SEARCH_HL_PRIORITY)
3670 || cur == NULL))
3671 {
3672 shl = &search_hl;
3673 shl_flag = TRUE;
3674 }
3675 else
3676 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 while (shl->rm.regprog != NULL)
3678 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003679 if (shl->startcol != MAXCOL
3680 && v >= (long)shl->startcol
3681 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
3683 shl->attr_cur = shl->attr;
3684 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003685 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
3687 shl->attr_cur = 0;
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 next_search_hl(wp, shl, lnum, (colnr_T)v);
3690
3691 /* Need to get the line again, a multi-line regexp
3692 * may have made it invalid. */
3693 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3694 ptr = line + v;
3695
3696 if (shl->lnum == lnum)
3697 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003698 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003700 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003702 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003704 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
3706 /* highlight empty match, try again after
3707 * it */
3708#ifdef FEAT_MBYTE
3709 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003710 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003711 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 else
3713#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003714 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716
3717 /* Loop to check if the match starts at the
3718 * current position */
3719 continue;
3720 }
3721 }
3722 break;
3723 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003724 if (shl != &search_hl && cur != NULL)
3725 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003727
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003728 /* Use attributes from match with highest priority among
3729 * 'search_hl' and the match list. */
3730 search_attr = search_hl.attr_cur;
3731 cur = wp->w_match_head;
3732 shl_flag = FALSE;
3733 while (cur != NULL || shl_flag == FALSE)
3734 {
3735 if (shl_flag == FALSE
3736 && ((cur != NULL
3737 && cur->priority > SEARCH_HL_PRIORITY)
3738 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003739 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003740 shl = &search_hl;
3741 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003742 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003743 else
3744 shl = &cur->hl;
3745 if (shl->attr_cur != 0)
3746 search_attr = shl->attr_cur;
3747 if (shl != &search_hl && cur != NULL)
3748 cur = cur->next;
3749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751#endif
3752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003754 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003756 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3757 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003759 if (diff_hlf == HLF_TXD && ptr - line > change_end
3760 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003762 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
3764#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003765
3766 /* Decide which of the highlight attributes to use. */
3767 attr_pri = TRUE;
3768 if (area_attr != 0)
3769 char_attr = area_attr;
3770 else if (search_attr != 0)
3771 char_attr = search_attr;
3772#ifdef LINE_ATTR
3773 /* Use line_attr when not in the Visual or 'incsearch' area
3774 * (area_attr may be 0 when "noinvcur" is set). */
3775 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003776 || vcol < fromcol || vcol_prev < fromcol_prev
3777 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003778 char_attr = line_attr;
3779#endif
3780 else
3781 {
3782 attr_pri = FALSE;
3783#ifdef FEAT_SYN_HL
3784 if (has_syntax)
3785 char_attr = syntax_attr;
3786 else
3787#endif
3788 char_attr = 0;
3789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791
3792 /*
3793 * Get the next character to put on the screen.
3794 */
3795 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003796 * The "p_extra" points to the extra stuff that is inserted to
3797 * represent special characters (non-printable stuff) and other
3798 * things. When all characters are the same, c_extra is used.
3799 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3800 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3802 */
3803 if (n_extra > 0)
3804 {
3805 if (c_extra != NUL)
3806 {
3807 c = c_extra;
3808#ifdef FEAT_MBYTE
3809 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3810 if (enc_utf8 && (*mb_char2len)(c) > 1)
3811 {
3812 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003813 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003814 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 else
3817 mb_utf8 = FALSE;
3818#endif
3819 }
3820 else
3821 {
3822 c = *p_extra;
3823#ifdef FEAT_MBYTE
3824 if (has_mbyte)
3825 {
3826 mb_c = c;
3827 if (enc_utf8)
3828 {
3829 /* If the UTF-8 character is more than one byte:
3830 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003831 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 mb_utf8 = FALSE;
3833 if (mb_l > n_extra)
3834 mb_l = 1;
3835 else if (mb_l > 1)
3836 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003837 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003839 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841 }
3842 else
3843 {
3844 /* if this is a DBCS character, put it in "mb_c" */
3845 mb_l = MB_BYTE2LEN(c);
3846 if (mb_l >= n_extra)
3847 mb_l = 1;
3848 else if (mb_l > 1)
3849 mb_c = (c << 8) + p_extra[1];
3850 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003851 if (mb_l == 0) /* at the NUL at end-of-line */
3852 mb_l = 1;
3853
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 /* If a double-width char doesn't fit display a '>' in the
3855 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003856 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857# ifdef FEAT_RIGHTLEFT
3858 wp->w_p_rl ? (col <= 0) :
3859# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003860 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 && (*mb_char2cells)(mb_c) == 2)
3862 {
3863 c = '>';
3864 mb_c = c;
3865 mb_l = 1;
3866 mb_utf8 = FALSE;
3867 multi_attr = hl_attr(HLF_AT);
3868 /* put the pointer back to output the double-width
3869 * character at the start of the next line. */
3870 ++n_extra;
3871 --p_extra;
3872 }
3873 else
3874 {
3875 n_extra -= mb_l - 1;
3876 p_extra += mb_l - 1;
3877 }
3878 }
3879#endif
3880 ++p_extra;
3881 }
3882 --n_extra;
3883 }
3884 else
3885 {
3886 /*
3887 * Get a character from the line itself.
3888 */
3889 c = *ptr;
3890#ifdef FEAT_MBYTE
3891 if (has_mbyte)
3892 {
3893 mb_c = c;
3894 if (enc_utf8)
3895 {
3896 /* If the UTF-8 character is more than one byte: Decode it
3897 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003898 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 mb_utf8 = FALSE;
3900 if (mb_l > 1)
3901 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003902 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 /* Overlong encoded ASCII or ASCII with composing char
3904 * is displayed normally, except a NUL. */
3905 if (mb_c < 0x80)
3906 c = mb_c;
3907 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003908
3909 /* At start of the line we can have a composing char.
3910 * Draw it as a space with a composing char. */
3911 if (utf_iscomposing(mb_c))
3912 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003913 int i;
3914
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003915 for (i = Screen_mco - 1; i > 0; --i)
3916 u8cc[i] = u8cc[i - 1];
3917 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003918 mb_c = ' ';
3919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921
3922 if ((mb_l == 1 && c >= 0x80)
3923 || (mb_l >= 1 && mb_c == 0)
3924 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003925# ifdef UNICODE16
3926 || mb_c >= 0x10000
3927# endif
3928 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
3930 /*
3931 * Illegal UTF-8 byte: display as <xx>.
3932 * Non-BMP character : display as ? or fullwidth ?.
3933 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003934# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
3938 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003939# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 if (wp->w_p_rl) /* reverse */
3941 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003944# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 else if (utf_char2cells(mb_c) != 2)
3946 STRCPY(extra, "?");
3947 else
3948 /* 0xff1f in UTF-8: full-width '?' */
3949 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
3952 p_extra = extra;
3953 c = *p_extra;
3954 mb_c = mb_ptr2char_adv(&p_extra);
3955 mb_utf8 = (c >= 0x80);
3956 n_extra = (int)STRLEN(p_extra);
3957 c_extra = NUL;
3958 if (area_attr == 0 && search_attr == 0)
3959 {
3960 n_attr = n_extra + 1;
3961 extra_attr = hl_attr(HLF_8);
3962 saved_attr2 = char_attr; /* save current attr */
3963 }
3964 }
3965 else if (mb_l == 0) /* at the NUL at end-of-line */
3966 mb_l = 1;
3967#ifdef FEAT_ARABIC
3968 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3969 {
3970 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003971 int pc, pc1, nc;
3972 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974 /* The idea of what is the previous and next
3975 * character depends on 'rightleft'. */
3976 if (wp->w_p_rl)
3977 {
3978 pc = prev_c;
3979 pc1 = prev_c1;
3980 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003981 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 }
3983 else
3984 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003985 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003987 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 prev_c = mb_c;
3990
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003991 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 else
3994 prev_c = mb_c;
3995#endif
3996 }
3997 else /* enc_dbcs */
3998 {
3999 mb_l = MB_BYTE2LEN(c);
4000 if (mb_l == 0) /* at the NUL at end-of-line */
4001 mb_l = 1;
4002 else if (mb_l > 1)
4003 {
4004 /* We assume a second byte below 32 is illegal.
4005 * Hopefully this is OK for all double-byte encodings!
4006 */
4007 if (ptr[1] >= 32)
4008 mb_c = (c << 8) + ptr[1];
4009 else
4010 {
4011 if (ptr[1] == NUL)
4012 {
4013 /* head byte at end of line */
4014 mb_l = 1;
4015 transchar_nonprint(extra, c);
4016 }
4017 else
4018 {
4019 /* illegal tail byte */
4020 mb_l = 2;
4021 STRCPY(extra, "XX");
4022 }
4023 p_extra = extra;
4024 n_extra = (int)STRLEN(extra) - 1;
4025 c_extra = NUL;
4026 c = *p_extra++;
4027 if (area_attr == 0 && search_attr == 0)
4028 {
4029 n_attr = n_extra + 1;
4030 extra_attr = hl_attr(HLF_8);
4031 saved_attr2 = char_attr; /* save current attr */
4032 }
4033 mb_c = c;
4034 }
4035 }
4036 }
4037 /* If a double-width char doesn't fit display a '>' in the
4038 * last column; the character is displayed at the start of the
4039 * next line. */
4040 if ((
4041# ifdef FEAT_RIGHTLEFT
4042 wp->w_p_rl ? (col <= 0) :
4043# endif
4044 (col >= W_WIDTH(wp) - 1))
4045 && (*mb_char2cells)(mb_c) == 2)
4046 {
4047 c = '>';
4048 mb_c = c;
4049 mb_utf8 = FALSE;
4050 mb_l = 1;
4051 multi_attr = hl_attr(HLF_AT);
4052 /* Put pointer back so that the character will be
4053 * displayed at the start of the next line. */
4054 --ptr;
4055 }
4056 else if (*ptr != NUL)
4057 ptr += mb_l - 1;
4058
4059 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004060 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004061 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004062 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004065 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 c = ' ';
4067 if (area_attr == 0 && search_attr == 0)
4068 {
4069 n_attr = n_extra + 1;
4070 extra_attr = hl_attr(HLF_AT);
4071 saved_attr2 = char_attr; /* save current attr */
4072 }
4073 mb_c = c;
4074 mb_utf8 = FALSE;
4075 mb_l = 1;
4076 }
4077
4078 }
4079#endif
4080 ++ptr;
4081
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004082 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004083 if (wp->w_p_list && (c == 160
4084#ifdef FEAT_MBYTE
4085 || (mb_utf8 && mb_c == 160)
4086#endif
4087 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004088 {
4089 c = lcs_nbsp;
4090 if (area_attr == 0 && search_attr == 0)
4091 {
4092 n_attr = 1;
4093 extra_attr = hl_attr(HLF_8);
4094 saved_attr2 = char_attr; /* save current attr */
4095 }
4096#ifdef FEAT_MBYTE
4097 mb_c = c;
4098 if (enc_utf8 && (*mb_char2len)(c) > 1)
4099 {
4100 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004101 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004102 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004103 }
4104 else
4105 mb_utf8 = FALSE;
4106#endif
4107 }
4108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (extra_check)
4110 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004111#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004112 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004113#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004114
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004115#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 /* Get syntax attribute, unless still at the start of the line
4117 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004118 v = (long)(ptr - line);
4119 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
4121 /* Get the syntax attribute for the character. If there
4122 * is an error, disable syntax highlighting. */
4123 save_did_emsg = did_emsg;
4124 did_emsg = FALSE;
4125
Bram Moolenaar217ad922005-03-20 22:37:15 +00004126 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004127# ifdef FEAT_SPELL
4128 has_spell ? &can_spell :
4129# endif
4130 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004133 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004134 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004135 has_syntax = FALSE;
4136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else
4138 did_emsg = save_did_emsg;
4139
4140 /* Need to get the line again, a multi-line regexp may
4141 * have made it invalid. */
4142 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4143 ptr = line + v;
4144
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004145 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004147 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004148 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004149# ifdef FEAT_CONCEAL
4150 /* no concealing past the end of the line, it interferes
4151 * with line highlighting */
4152 if (c == NUL)
4153 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004154 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004155 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004158#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004159
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004160#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004161 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004162 * Only do this when there is no syntax highlighting, the
4163 * @Spell cluster is not used or the current syntax item
4164 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004165 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004166 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004167 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004168# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004169 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004170 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004171# endif
4172 if (c != 0 && (
4173# ifdef FEAT_SYN_HL
4174 !has_syntax ||
4175# endif
4176 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004177 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004178 char_u *prev_ptr, *p;
4179 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004180 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004181# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004182 if (has_mbyte)
4183 {
4184 prev_ptr = ptr - mb_l;
4185 v -= mb_l - 1;
4186 }
4187 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004188# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004189 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004190
4191 /* Use nextline[] if possible, it has the start of the
4192 * next line concatenated. */
4193 if ((prev_ptr - line) - nextlinecol >= 0)
4194 p = nextline + (prev_ptr - line) - nextlinecol;
4195 else
4196 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004197 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004198 len = spell_check(wp, p, &spell_hlf, &cap_col,
4199 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004200 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004201
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004202 /* In Insert mode only highlight a word that
4203 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004204 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004205 && (State & INSERT) != 0
4206 && wp->w_cursor.lnum == lnum
4207 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004208 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004209 && wp->w_cursor.col < (colnr_T)word_end)
4210 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004211 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004212 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004213 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004214
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004215 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004216 && (p - nextline) + len > nextline_idx)
4217 {
4218 /* Remember that the good word continues at the
4219 * start of the next line. */
4220 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004221 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004222 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004223
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004224 /* Turn index into actual attributes. */
4225 if (spell_hlf != HLF_COUNT)
4226 spell_attr = highlight_attr[spell_hlf];
4227
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004228 if (cap_col > 0)
4229 {
4230 if (p != prev_ptr
4231 && (p - nextline) + cap_col >= nextline_idx)
4232 {
4233 /* Remember that the word in the next line
4234 * must start with a capital. */
4235 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004236 cap_col = (int)((p - nextline) + cap_col
4237 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004238 }
4239 else
4240 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004241 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004242 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004243 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004244 }
4245 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004246 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004247 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004248 char_attr = hl_combine_attr(char_attr, spell_attr);
4249 else
4250 char_attr = hl_combine_attr(spell_attr, char_attr);
4251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252#endif
4253#ifdef FEAT_LINEBREAK
4254 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004255 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 */
4257 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004258 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
4260 n_extra = win_lbr_chartabsize(wp, ptr - (
4261# ifdef FEAT_MBYTE
4262 has_mbyte ? mb_l :
4263# endif
4264 1), (colnr_T)vcol, NULL) - 1;
4265 c_extra = ' ';
4266 if (vim_iswhite(c))
4267 c = ' ';
4268 }
4269#endif
4270
4271 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4272 {
4273 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004274 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
4276 n_attr = 1;
4277 extra_attr = hl_attr(HLF_8);
4278 saved_attr2 = char_attr; /* save current attr */
4279 }
4280#ifdef FEAT_MBYTE
4281 mb_c = c;
4282 if (enc_utf8 && (*mb_char2len)(c) > 1)
4283 {
4284 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004285 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004286 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 else
4289 mb_utf8 = FALSE;
4290#endif
4291 }
4292 }
4293
4294 /*
4295 * Handling of non-printable characters.
4296 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004297 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
4299 /*
4300 * when getting a character from the file, we may have to
4301 * turn it into something else on the way to putting it
4302 * into "ScreenLines".
4303 */
4304 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4305 {
4306 /* tab amount depends on current column */
4307 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004308 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4309#ifdef FEAT_CONCEAL
4310 /* Tab alignment should be identical regardless of
4311 * 'conceallevel' value. So tab compensates of all
4312 * previous concealed characters, and thus resets vcol_off
4313 * and boguscols accumulated so far in the line. Note that
4314 * the tab can be longer than 'tabstop' when there
4315 * are concealed characters. */
4316 n_extra += vcol_off;
4317 vcol -= vcol_off;
4318 vcol_off = 0;
4319 col -= boguscols;
4320 boguscols = 0;
4321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322#ifdef FEAT_MBYTE
4323 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4324#endif
4325 if (wp->w_p_list)
4326 {
4327 c = lcs_tab1;
4328 c_extra = lcs_tab2;
4329 n_attr = n_extra + 1;
4330 extra_attr = hl_attr(HLF_8);
4331 saved_attr2 = char_attr; /* save current attr */
4332#ifdef FEAT_MBYTE
4333 mb_c = c;
4334 if (enc_utf8 && (*mb_char2len)(c) > 1)
4335 {
4336 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004337 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004338 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340#endif
4341 }
4342 else
4343 {
4344 c_extra = ' ';
4345 c = ' ';
4346 }
4347 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004348 else if (c == NUL
4349 && ((wp->w_p_list && lcs_eol > 0)
4350 || ((fromcol >= 0 || fromcol_prev >= 0)
4351 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004352#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004353 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004354#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004355 && (
4356# ifdef FEAT_RIGHTLEFT
4357 wp->w_p_rl ? (col >= 0) :
4358# endif
4359 (col < W_WIDTH(wp)))
4360 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004361 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004362 && (colnr_T)vcol == wp->w_virtcol)))
4363 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004365 /* Display a '$' after the line or highlight an extra
4366 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4368 /* For a diff line the highlighting continues after the
4369 * "$". */
4370 if (
4371# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004372 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373# ifdef LINE_ATTR
4374 &&
4375# endif
4376# endif
4377# ifdef LINE_ATTR
4378 line_attr == 0
4379# endif
4380 )
4381#endif
4382 {
4383#ifdef FEAT_VIRTUALEDIT
4384 /* In virtualedit, visual selections may extend
4385 * beyond end of line. */
4386 if (area_highlighting && virtual_active()
4387 && tocol != MAXCOL && vcol < tocol)
4388 n_extra = 0;
4389 else
4390#endif
4391 {
4392 p_extra = at_end_str;
4393 n_extra = 1;
4394 c_extra = NUL;
4395 }
4396 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004397 if (wp->w_p_list)
4398 c = lcs_eol;
4399 else
4400 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 lcs_eol_one = -1;
4402 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004403 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
4405 extra_attr = hl_attr(HLF_AT);
4406 n_attr = 1;
4407 }
4408#ifdef FEAT_MBYTE
4409 mb_c = c;
4410 if (enc_utf8 && (*mb_char2len)(c) > 1)
4411 {
4412 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004413 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004414 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 else
4417 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4418#endif
4419 }
4420 else if (c != NUL)
4421 {
4422 p_extra = transchar(c);
4423#ifdef FEAT_RIGHTLEFT
4424 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4425 rl_mirror(p_extra); /* reverse "<12>" */
4426#endif
4427 n_extra = byte2cells(c) - 1;
4428 c_extra = NUL;
4429 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004430 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
4432 n_attr = n_extra + 1;
4433 extra_attr = hl_attr(HLF_8);
4434 saved_attr2 = char_attr; /* save current attr */
4435 }
4436#ifdef FEAT_MBYTE
4437 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4438#endif
4439 }
4440#ifdef FEAT_VIRTUALEDIT
4441 else if (VIsual_active
4442 && (VIsual_mode == Ctrl_V
4443 || VIsual_mode == 'v')
4444 && virtual_active()
4445 && tocol != MAXCOL
4446 && vcol < tocol
4447 && (
4448# ifdef FEAT_RIGHTLEFT
4449 wp->w_p_rl ? (col >= 0) :
4450# endif
4451 (col < W_WIDTH(wp))))
4452 {
4453 c = ' ';
4454 --ptr; /* put it back at the NUL */
4455 }
4456#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004457#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 else if ((
4459# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004460 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 ) && (
4464# ifdef FEAT_RIGHTLEFT
4465 wp->w_p_rl ? (col >= 0) :
4466# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004467 (col
4468# ifdef FEAT_CONCEAL
4469 - boguscols
4470# endif
4471 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
4473 /* Highlight until the right side of the window */
4474 c = ' ';
4475 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004476
4477 /* Remember we do the char for line highlighting. */
4478 ++did_line_attr;
4479
4480 /* don't do search HL for the rest of the line */
4481 if (line_attr != 0 && char_attr == search_attr && col > 0)
4482 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483# ifdef FEAT_DIFF
4484 if (diff_hlf == HLF_TXD)
4485 {
4486 diff_hlf = HLF_CHD;
4487 if (attr == 0 || char_attr != attr)
4488 char_attr = hl_attr(diff_hlf);
4489 }
4490# endif
4491 }
4492#endif
4493 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004494
4495#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004496 if ( wp->w_p_cole > 0
4497 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4498 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004499 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004500 && !(lnum_in_visual_area
4501 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004502 {
4503 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004504 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004505 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4506 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004507 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004508 /* First time at this concealed item: display one
4509 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004510 if (syn_get_sub_char() != NUL)
4511 c = syn_get_sub_char();
4512 else if (lcs_conceal != NUL)
4513 c = lcs_conceal;
4514 else
4515 c = ' ';
4516
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004517 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004518
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004519 if (n_extra > 0)
4520 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004521 vcol += n_extra;
4522 if (wp->w_p_wrap && n_extra > 0)
4523 {
4524# ifdef FEAT_RIGHTLEFT
4525 if (wp->w_p_rl)
4526 {
4527 col -= n_extra;
4528 boguscols -= n_extra;
4529 }
4530 else
4531# endif
4532 {
4533 boguscols += n_extra;
4534 col += n_extra;
4535 }
4536 }
4537 n_extra = 0;
4538 n_attr = 0;
4539 }
4540 else if (n_skip == 0)
4541 {
4542 is_concealing = TRUE;
4543 n_skip = 1;
4544 }
4545# ifdef FEAT_MBYTE
4546 mb_c = c;
4547 if (enc_utf8 && (*mb_char2len)(c) > 1)
4548 {
4549 mb_utf8 = TRUE;
4550 u8cc[0] = 0;
4551 c = 0xc0;
4552 }
4553 else
4554 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4555# endif
4556 }
4557 else
4558 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004559 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004560 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004561 }
4562#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
4564
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004565#ifdef FEAT_CONCEAL
4566 /* In the cursor line and we may be concealing characters: correct
4567 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004568 if (!did_wcol && draw_state == WL_LINE
4569 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004570 && conceal_cursor_line(wp)
4571 && (int)wp->w_virtcol <= vcol + n_skip)
4572 {
4573 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004574 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004575 did_wcol = TRUE;
4576 }
4577#endif
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 /* Don't override visual selection highlighting. */
4580 if (n_attr > 0
4581 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004582 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 char_attr = extra_attr;
4584
Bram Moolenaar81695252004-12-29 20:58:21 +00004585#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 /* XIM don't send preedit_start and preedit_end, but they send
4587 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4588 * im_is_preediting() here. */
4589 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004590 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 && (State & INSERT)
4592 && !p_imdisable
4593 && im_is_preediting()
4594 && draw_state == WL_LINE)
4595 {
4596 colnr_T tcol;
4597
4598 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004599 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 else
4601 tcol = preedit_end_col;
4602 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4603 {
4604 if (feedback_old_attr < 0)
4605 {
4606 feedback_col = 0;
4607 feedback_old_attr = char_attr;
4608 }
4609 char_attr = im_get_feedback_attr(feedback_col);
4610 if (char_attr < 0)
4611 char_attr = feedback_old_attr;
4612 feedback_col++;
4613 }
4614 else if (feedback_old_attr >= 0)
4615 {
4616 char_attr = feedback_old_attr;
4617 feedback_old_attr = -1;
4618 feedback_col = 0;
4619 }
4620 }
4621#endif
4622 /*
4623 * Handle the case where we are in column 0 but not on the first
4624 * character of the line and the user wants us to show us a
4625 * special character (via 'listchars' option "precedes:<char>".
4626 */
4627 if (lcs_prec_todo != NUL
4628 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4629#ifdef FEAT_DIFF
4630 && filler_todo <= 0
4631#endif
4632 && draw_state > WL_NR
4633 && c != NUL)
4634 {
4635 c = lcs_prec;
4636 lcs_prec_todo = NUL;
4637#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004638 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4639 {
4640 /* Double-width character being overwritten by the "precedes"
4641 * character, need to fill up half the character. */
4642 c_extra = MB_FILLER_CHAR;
4643 n_extra = 1;
4644 n_attr = 2;
4645 extra_attr = hl_attr(HLF_AT);
4646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 mb_c = c;
4648 if (enc_utf8 && (*mb_char2len)(c) > 1)
4649 {
4650 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004651 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004652 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 else
4655 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4656#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004657 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
4659 saved_attr3 = char_attr; /* save current attr */
4660 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4661 n_attr3 = 1;
4662 }
4663 }
4664
4665 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004666 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004668 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004669#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004670 || did_line_attr == 1
4671#endif
4672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004674#ifdef FEAT_SEARCH_EXTRA
4675 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004676
4677 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004678 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004679 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004680#endif
4681
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004682 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 * highlight match at end of line. If it's beyond the last
4684 * char on the screen, just overwrite that one (tricky!) Not
4685 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004686#ifdef FEAT_SEARCH_EXTRA
4687 prevcol_hl_flag = FALSE;
4688 if (prevcol == (long)search_hl.startcol)
4689 prevcol_hl_flag = TRUE;
4690 else
4691 {
4692 cur = wp->w_match_head;
4693 while (cur != NULL)
4694 {
4695 if (prevcol == (long)cur->hl.startcol)
4696 {
4697 prevcol_hl_flag = TRUE;
4698 break;
4699 }
4700 cur = cur->next;
4701 }
4702 }
4703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004705 && ((area_attr != 0 && vcol == fromcol
4706#ifdef FEAT_VISUAL
4707 && (VIsual_mode != Ctrl_V
4708 || lnum == VIsual.lnum
4709 || lnum == curwin->w_cursor.lnum)
4710#endif
4711 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712#ifdef FEAT_SEARCH_EXTRA
4713 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004714 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004715# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004716 && did_line_attr <= 1
4717# endif
4718 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719#endif
4720 ))
4721 {
4722 int n = 0;
4723
4724#ifdef FEAT_RIGHTLEFT
4725 if (wp->w_p_rl)
4726 {
4727 if (col < 0)
4728 n = 1;
4729 }
4730 else
4731#endif
4732 {
4733 if (col >= W_WIDTH(wp))
4734 n = -1;
4735 }
4736 if (n != 0)
4737 {
4738 /* At the window boundary, highlight the last character
4739 * instead (better than nothing). */
4740 off += n;
4741 col += n;
4742 }
4743 else
4744 {
4745 /* Add a blank character to highlight. */
4746 ScreenLines[off] = ' ';
4747#ifdef FEAT_MBYTE
4748 if (enc_utf8)
4749 ScreenLinesUC[off] = 0;
4750#endif
4751 }
4752#ifdef FEAT_SEARCH_EXTRA
4753 if (area_attr == 0)
4754 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004755 /* Use attributes from match with highest priority among
4756 * 'search_hl' and the match list. */
4757 char_attr = search_hl.attr;
4758 cur = wp->w_match_head;
4759 shl_flag = FALSE;
4760 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004761 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004762 if (shl_flag == FALSE
4763 && ((cur != NULL
4764 && cur->priority > SEARCH_HL_PRIORITY)
4765 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004766 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004767 shl = &search_hl;
4768 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004769 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004770 else
4771 shl = &cur->hl;
4772 if ((ptr - line) - 1 == (long)shl->startcol)
4773 char_attr = shl->attr;
4774 if (shl != &search_hl && cur != NULL)
4775 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778#endif
4779 ScreenAttrs[off] = char_attr;
4780#ifdef FEAT_RIGHTLEFT
4781 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004784 --off;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 else
4787#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004790 ++off;
4791 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004792 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004793#ifdef FEAT_SYN_HL
4794 eol_hl_off = 1;
4795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
Bram Moolenaar91170f82006-05-05 21:15:17 +00004799 /*
4800 * At end of the text line.
4801 */
4802 if (c == NUL)
4803 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004804#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004805 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4806 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004807 {
4808 /* highlight last char after line */
4809 --col;
4810 --off;
4811 --vcol;
4812 }
4813
Bram Moolenaar1a384422010-07-14 19:53:30 +02004814 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004815 if (wp->w_p_wrap)
4816 v = wp->w_skipcol;
4817 else
4818 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004819
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004820 /* check if line ends before left margin */
4821 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004822 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004823#ifdef FEAT_CONCEAL
4824 /* Get rid of the boguscols now, we want to draw until the right
4825 * edge for 'cursorcolumn'. */
4826 col -= boguscols;
4827 boguscols = 0;
4828#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004829
4830 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004831 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004832
4833 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004834 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4835 && (int)wp->w_virtcol <
4836 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004837 && lnum != wp->w_cursor.lnum)
4838 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004839# ifdef FEAT_RIGHTLEFT
4840 && !wp->w_p_rl
4841# endif
4842 )
4843 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004844 int rightmost_vcol = 0;
4845 int i;
4846
4847 if (wp->w_p_cuc)
4848 rightmost_vcol = wp->w_virtcol;
4849 if (draw_color_col)
4850 /* determine rightmost colorcolumn to possibly draw */
4851 for (i = 0; color_cols[i] >= 0; ++i)
4852 if (rightmost_vcol < color_cols[i])
4853 rightmost_vcol = color_cols[i];
4854
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004855 while (col < W_WIDTH(wp))
4856 {
4857 ScreenLines[off] = ' ';
4858#ifdef FEAT_MBYTE
4859 if (enc_utf8)
4860 ScreenLinesUC[off] = 0;
4861#endif
4862 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004863 if (draw_color_col)
4864 draw_color_col = advance_color_col(VCOL_HLC,
4865 &color_cols);
4866
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004867 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004868 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004869 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004870 ScreenAttrs[off++] = hl_attr(HLF_MC);
4871 else
4872 ScreenAttrs[off++] = 0;
4873
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004874 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004875 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004876
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004877 ++vcol;
4878 }
4879 }
4880#endif
4881
Bram Moolenaar860cae12010-06-05 23:22:07 +02004882 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4883 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 row++;
4885
4886 /*
4887 * Update w_cline_height and w_cline_folded if the cursor line was
4888 * updated (saves a call to plines() later).
4889 */
4890 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4891 {
4892 curwin->w_cline_row = startrow;
4893 curwin->w_cline_height = row - startrow;
4894#ifdef FEAT_FOLDING
4895 curwin->w_cline_folded = FALSE;
4896#endif
4897 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4898 }
4899
4900 break;
4901 }
4902
4903 /* line continues beyond line end */
4904 if (lcs_ext
4905 && !wp->w_p_wrap
4906#ifdef FEAT_DIFF
4907 && filler_todo <= 0
4908#endif
4909 && (
4910#ifdef FEAT_RIGHTLEFT
4911 wp->w_p_rl ? col == 0 :
4912#endif
4913 col == W_WIDTH(wp) - 1)
4914 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004915 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4917 {
4918 c = lcs_ext;
4919 char_attr = hl_attr(HLF_AT);
4920#ifdef FEAT_MBYTE
4921 mb_c = c;
4922 if (enc_utf8 && (*mb_char2len)(c) > 1)
4923 {
4924 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004925 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004926 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
4929 mb_utf8 = FALSE;
4930#endif
4931 }
4932
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004933#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004934 /* advance to the next 'colorcolumn' */
4935 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004936 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004937
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004938 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004939 * highlight the cursor position itself.
4940 * Also highlight the 'colorcolumn' if it is different than
4941 * 'cursorcolumn' */
4942 vcol_save_attr = -1;
4943 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004944 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004945 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004946 && lnum != wp->w_cursor.lnum)
4947 {
4948 vcol_save_attr = char_attr;
4949 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4950 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004951 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004952 {
4953 vcol_save_attr = char_attr;
4954 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4955 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004956 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004957#endif
4958
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 /*
4960 * Store character to be displayed.
4961 * Skip characters that are left of the screen for 'nowrap'.
4962 */
4963 vcol_prev = vcol;
4964 if (draw_state < WL_LINE || n_skip <= 0)
4965 {
4966 /*
4967 * Store the character.
4968 */
4969#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4970 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4971 {
4972 /* A double-wide character is: put first halve in left cell. */
4973 --off;
4974 --col;
4975 }
4976#endif
4977 ScreenLines[off] = c;
4978#ifdef FEAT_MBYTE
4979 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004980 {
4981 if ((mb_c & 0xff00) == 0x8e00)
4982 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 else if (enc_utf8)
4986 {
4987 if (mb_utf8)
4988 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004989 int i;
4990
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004992 if ((c & 0xff) == 0)
4993 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004994 for (i = 0; i < Screen_mco; ++i)
4995 {
4996 ScreenLinesC[i][off] = u8cc[i];
4997 if (u8cc[i] == 0)
4998 break;
4999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
5001 else
5002 ScreenLinesUC[off] = 0;
5003 }
5004 if (multi_attr)
5005 {
5006 ScreenAttrs[off] = multi_attr;
5007 multi_attr = 0;
5008 }
5009 else
5010#endif
5011 ScreenAttrs[off] = char_attr;
5012
5013#ifdef FEAT_MBYTE
5014 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5015 {
5016 /* Need to fill two screen columns. */
5017 ++off;
5018 ++col;
5019 if (enc_utf8)
5020 /* UTF-8: Put a 0 in the second screen char. */
5021 ScreenLines[off] = 0;
5022 else
5023 /* DBCS: Put second byte in the second screen char. */
5024 ScreenLines[off] = mb_c & 0xff;
5025 ++vcol;
5026 /* When "tocol" is halfway a character, set it to the end of
5027 * the character, otherwise highlighting won't stop. */
5028 if (tocol == vcol)
5029 ++tocol;
5030#ifdef FEAT_RIGHTLEFT
5031 if (wp->w_p_rl)
5032 {
5033 /* now it's time to backup one cell */
5034 --off;
5035 --col;
5036 }
5037#endif
5038 }
5039#endif
5040#ifdef FEAT_RIGHTLEFT
5041 if (wp->w_p_rl)
5042 {
5043 --off;
5044 --col;
5045 }
5046 else
5047#endif
5048 {
5049 ++off;
5050 ++col;
5051 }
5052 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005053#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005054 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005055 {
5056 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005057 ++vcol_off;
5058 if (n_extra > 0)
5059 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005060 if (wp->w_p_wrap)
5061 {
5062 /*
5063 * Special voodoo required if 'wrap' is on.
5064 *
5065 * Advance the column indicator to force the line
5066 * drawing to wrap early. This will make the line
5067 * take up the same screen space when parts are concealed,
5068 * so that cursor line computations aren't messed up.
5069 *
5070 * To avoid the fictitious advance of 'col' causing
5071 * trailing junk to be written out of the screen line
5072 * we are building, 'boguscols' keeps track of the number
5073 * of bad columns we have advanced.
5074 */
5075 if (n_extra > 0)
5076 {
5077 vcol += n_extra;
5078# ifdef FEAT_RIGHTLEFT
5079 if (wp->w_p_rl)
5080 {
5081 col -= n_extra;
5082 boguscols -= n_extra;
5083 }
5084 else
5085# endif
5086 {
5087 col += n_extra;
5088 boguscols += n_extra;
5089 }
5090 n_extra = 0;
5091 n_attr = 0;
5092 }
5093
5094
5095# ifdef FEAT_MBYTE
5096 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5097 {
5098 /* Need to fill two screen columns. */
5099# ifdef FEAT_RIGHTLEFT
5100 if (wp->w_p_rl)
5101 {
5102 --boguscols;
5103 --col;
5104 }
5105 else
5106# endif
5107 {
5108 ++boguscols;
5109 ++col;
5110 }
5111 }
5112# endif
5113
5114# ifdef FEAT_RIGHTLEFT
5115 if (wp->w_p_rl)
5116 {
5117 --boguscols;
5118 --col;
5119 }
5120 else
5121# endif
5122 {
5123 ++boguscols;
5124 ++col;
5125 }
5126 }
5127 else
5128 {
5129 if (n_extra > 0)
5130 {
5131 vcol += n_extra;
5132 n_extra = 0;
5133 n_attr = 0;
5134 }
5135 }
5136
5137 }
5138#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 else
5140 --n_skip;
5141
Bram Moolenaar64486672010-05-16 15:46:46 +02005142 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5143 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005144 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145#ifdef FEAT_DIFF
5146 && filler_todo <= 0
5147#endif
5148 )
5149 ++vcol;
5150
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005151#ifdef FEAT_SYN_HL
5152 if (vcol_save_attr >= 0)
5153 char_attr = vcol_save_attr;
5154#endif
5155
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 /* restore attributes after "predeces" in 'listchars' */
5157 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5158 char_attr = saved_attr3;
5159
5160 /* restore attributes after last 'listchars' or 'number' char */
5161 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5162 char_attr = saved_attr2;
5163
5164 /*
5165 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005166 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 */
5168 if ((
5169#ifdef FEAT_RIGHTLEFT
5170 wp->w_p_rl ? (col < 0) :
5171#endif
5172 (col >= W_WIDTH(wp)))
5173 && (*ptr != NUL
5174#ifdef FEAT_DIFF
5175 || filler_todo > 0
5176#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005177 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5179 )
5180 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005181#ifdef FEAT_CONCEAL
5182 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5183 (int)W_WIDTH(wp), wp->w_p_rl);
5184 boguscols = 0;
5185#else
5186 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5187 (int)W_WIDTH(wp), wp->w_p_rl);
5188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 ++row;
5190 ++screen_row;
5191
5192 /* When not wrapping and finished diff lines, or when displayed
5193 * '$' and highlighting until last column, break here. */
5194 if ((!wp->w_p_wrap
5195#ifdef FEAT_DIFF
5196 && filler_todo <= 0
5197#endif
5198 ) || lcs_eol_one == -1)
5199 break;
5200
5201 /* When the window is too narrow draw all "@" lines. */
5202 if (draw_state != WL_LINE
5203#ifdef FEAT_DIFF
5204 && filler_todo <= 0
5205#endif
5206 )
5207 {
5208 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5209#ifdef FEAT_VERTSPLIT
5210 draw_vsep_win(wp, row);
5211#endif
5212 row = endrow;
5213 }
5214
5215 /* When line got too long for screen break here. */
5216 if (row == endrow)
5217 {
5218 ++row;
5219 break;
5220 }
5221
5222 if (screen_cur_row == screen_row - 1
5223#ifdef FEAT_DIFF
5224 && filler_todo <= 0
5225#endif
5226 && W_WIDTH(wp) == Columns)
5227 {
5228 /* Remember that the line wraps, used for modeless copy. */
5229 LineWraps[screen_row - 1] = TRUE;
5230
5231 /*
5232 * Special trick to make copy/paste of wrapped lines work with
5233 * xterm/screen: write an extra character beyond the end of
5234 * the line. This will work with all terminal types
5235 * (regardless of the xn,am settings).
5236 * Only do this on a fast tty.
5237 * Only do this if the cursor is on the current line
5238 * (something has been written in it).
5239 * Don't do this for the GUI.
5240 * Don't do this for double-width characters.
5241 * Don't do this for a window not at the right screen border.
5242 */
5243 if (p_tf
5244#ifdef FEAT_GUI
5245 && !gui.in_use
5246#endif
5247#ifdef FEAT_MBYTE
5248 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005249 && ((*mb_off2cells)(LineOffset[screen_row],
5250 LineOffset[screen_row] + screen_Columns)
5251 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005253 + (int)Columns - 2,
5254 LineOffset[screen_row] + screen_Columns)
5255 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#endif
5257 )
5258 {
5259 /* First make sure we are at the end of the screen line,
5260 * then output the same character again to let the
5261 * terminal know about the wrap. If the terminal doesn't
5262 * auto-wrap, we overwrite the character. */
5263 if (screen_cur_col != W_WIDTH(wp))
5264 screen_char(LineOffset[screen_row - 1]
5265 + (unsigned)Columns - 1,
5266 screen_row - 1, (int)(Columns - 1));
5267
5268#ifdef FEAT_MBYTE
5269 /* When there is a multi-byte character, just output a
5270 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005271 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5272 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 out_char(' ');
5274 else
5275#endif
5276 out_char(ScreenLines[LineOffset[screen_row - 1]
5277 + (Columns - 1)]);
5278 /* force a redraw of the first char on the next line */
5279 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5280 screen_start(); /* don't know where cursor is now */
5281 }
5282 }
5283
5284 col = 0;
5285 off = (unsigned)(current_ScreenLine - ScreenLines);
5286#ifdef FEAT_RIGHTLEFT
5287 if (wp->w_p_rl)
5288 {
5289 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5290 off += col;
5291 }
5292#endif
5293
5294 /* reset the drawing state for the start of a wrapped line */
5295 draw_state = WL_START;
5296 saved_n_extra = n_extra;
5297 saved_p_extra = p_extra;
5298 saved_c_extra = c_extra;
5299 saved_char_attr = char_attr;
5300 n_extra = 0;
5301 lcs_prec_todo = lcs_prec;
5302#ifdef FEAT_LINEBREAK
5303# ifdef FEAT_DIFF
5304 if (filler_todo <= 0)
5305# endif
5306 need_showbreak = TRUE;
5307#endif
5308#ifdef FEAT_DIFF
5309 --filler_todo;
5310 /* When the filler lines are actually below the last line of the
5311 * file, don't draw the line itself, break here. */
5312 if (filler_todo == 0 && wp->w_botfill)
5313 break;
5314#endif
5315 }
5316
5317 } /* for every character in the line */
5318
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005319#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005320 /* After an empty line check first word for capital. */
5321 if (*skipwhite(line) == NUL)
5322 {
5323 capcol_lnum = lnum + 1;
5324 cap_col = 0;
5325 }
5326#endif
5327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 return row;
5329}
5330
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005331#ifdef FEAT_MBYTE
5332static int comp_char_differs __ARGS((int, int));
5333
5334/*
5335 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005336 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005337 */
5338 static int
5339comp_char_differs(off_from, off_to)
5340 int off_from;
5341 int off_to;
5342{
5343 int i;
5344
5345 for (i = 0; i < Screen_mco; ++i)
5346 {
5347 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5348 return TRUE;
5349 if (ScreenLinesC[i][off_from] == 0)
5350 break;
5351 }
5352 return FALSE;
5353}
5354#endif
5355
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356/*
5357 * Check whether the given character needs redrawing:
5358 * - the (first byte of the) character is different
5359 * - the attributes are different
5360 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005361 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 */
5363 static int
5364char_needs_redraw(off_from, off_to, cols)
5365 int off_from;
5366 int off_to;
5367 int cols;
5368{
5369 if (cols > 0
5370 && ((ScreenLines[off_from] != ScreenLines[off_to]
5371 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5372
5373#ifdef FEAT_MBYTE
5374 || (enc_dbcs != 0
5375 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5376 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5377 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5378 : (cols > 1 && ScreenLines[off_from + 1]
5379 != ScreenLines[off_to + 1])))
5380 || (enc_utf8
5381 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5382 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005383 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005384 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5385 && ScreenLines[off_from + 1]
5386 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387#endif
5388 ))
5389 return TRUE;
5390 return FALSE;
5391}
5392
5393/*
5394 * Move one "cooked" screen line to the screen, but only the characters that
5395 * have actually changed. Handle insert/delete character.
5396 * "coloff" gives the first column on the screen for this line.
5397 * "endcol" gives the columns where valid characters are.
5398 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5399 * needs to be cleared, negative otherwise.
5400 * "rlflag" is TRUE in a rightleft window:
5401 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5402 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5403 */
5404 static void
5405screen_line(row, coloff, endcol, clear_width
5406#ifdef FEAT_RIGHTLEFT
5407 , rlflag
5408#endif
5409 )
5410 int row;
5411 int coloff;
5412 int endcol;
5413 int clear_width;
5414#ifdef FEAT_RIGHTLEFT
5415 int rlflag;
5416#endif
5417{
5418 unsigned off_from;
5419 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005420#ifdef FEAT_MBYTE
5421 unsigned max_off_from;
5422 unsigned max_off_to;
5423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 int col = 0;
5425#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5426 int hl;
5427#endif
5428 int force = FALSE; /* force update rest of the line */
5429 int redraw_this /* bool: does character need redraw? */
5430#ifdef FEAT_GUI
5431 = TRUE /* For GUI when while-loop empty */
5432#endif
5433 ;
5434 int redraw_next; /* redraw_this for next character */
5435#ifdef FEAT_MBYTE
5436 int clear_next = FALSE;
5437 int char_cells; /* 1: normal char */
5438 /* 2: occupies two display cells */
5439# define CHAR_CELLS char_cells
5440#else
5441# define CHAR_CELLS 1
5442#endif
5443
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005444 /* Check for illegal row and col, just in case. */
5445 if (row >= Rows)
5446 row = Rows - 1;
5447 if (endcol > Columns)
5448 endcol = Columns;
5449
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450# ifdef FEAT_CLIPBOARD
5451 clip_may_clear_selection(row, row);
5452# endif
5453
5454 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5455 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005456#ifdef FEAT_MBYTE
5457 max_off_from = off_from + screen_Columns;
5458 max_off_to = LineOffset[row] + screen_Columns;
5459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
5461#ifdef FEAT_RIGHTLEFT
5462 if (rlflag)
5463 {
5464 /* Clear rest first, because it's left of the text. */
5465 if (clear_width > 0)
5466 {
5467 while (col <= endcol && ScreenLines[off_to] == ' '
5468 && ScreenAttrs[off_to] == 0
5469# ifdef FEAT_MBYTE
5470 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5471# endif
5472 )
5473 {
5474 ++off_to;
5475 ++col;
5476 }
5477 if (col <= endcol)
5478 screen_fill(row, row + 1, col + coloff,
5479 endcol + coloff + 1, ' ', ' ', 0);
5480 }
5481 col = endcol + 1;
5482 off_to = LineOffset[row] + col + coloff;
5483 off_from += col;
5484 endcol = (clear_width > 0 ? clear_width : -clear_width);
5485 }
5486#endif /* FEAT_RIGHTLEFT */
5487
5488 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5489
5490 while (col < endcol)
5491 {
5492#ifdef FEAT_MBYTE
5493 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005494 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 else
5496 char_cells = 1;
5497#endif
5498
5499 redraw_this = redraw_next;
5500 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5501 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5502
5503#ifdef FEAT_GUI
5504 /* If the next character was bold, then redraw the current character to
5505 * remove any pixels that might have spilt over into us. This only
5506 * happens in the GUI.
5507 */
5508 if (redraw_next && gui.in_use)
5509 {
5510 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005511 if (hl > HL_ALL)
5512 hl = syn_attr2attr(hl);
5513 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 redraw_this = TRUE;
5515 }
5516#endif
5517
5518 if (redraw_this)
5519 {
5520 /*
5521 * Special handling when 'xs' termcap flag set (hpterm):
5522 * Attributes for characters are stored at the position where the
5523 * cursor is when writing the highlighting code. The
5524 * start-highlighting code must be written with the cursor on the
5525 * first highlighted character. The stop-highlighting code must
5526 * be written with the cursor just after the last highlighted
5527 * character.
5528 * Overwriting a character doesn't remove it's highlighting. Need
5529 * to clear the rest of the line, and force redrawing it
5530 * completely.
5531 */
5532 if ( p_wiv
5533 && !force
5534#ifdef FEAT_GUI
5535 && !gui.in_use
5536#endif
5537 && ScreenAttrs[off_to] != 0
5538 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5539 {
5540 /*
5541 * Need to remove highlighting attributes here.
5542 */
5543 windgoto(row, col + coloff);
5544 out_str(T_CE); /* clear rest of this screen line */
5545 screen_start(); /* don't know where cursor is now */
5546 force = TRUE; /* force redraw of rest of the line */
5547 redraw_next = TRUE; /* or else next char would miss out */
5548
5549 /*
5550 * If the previous character was highlighted, need to stop
5551 * highlighting at this character.
5552 */
5553 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5554 {
5555 screen_attr = ScreenAttrs[off_to - 1];
5556 term_windgoto(row, col + coloff);
5557 screen_stop_highlight();
5558 }
5559 else
5560 screen_attr = 0; /* highlighting has stopped */
5561 }
5562#ifdef FEAT_MBYTE
5563 if (enc_dbcs != 0)
5564 {
5565 /* Check if overwriting a double-byte with a single-byte or
5566 * the other way around requires another character to be
5567 * redrawn. For UTF-8 this isn't needed, because comparing
5568 * ScreenLinesUC[] is sufficient. */
5569 if (char_cells == 1
5570 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005571 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
5573 /* Writing a single-cell character over a double-cell
5574 * character: need to redraw the next cell. */
5575 ScreenLines[off_to + 1] = 0;
5576 redraw_next = TRUE;
5577 }
5578 else if (char_cells == 2
5579 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005580 && (*mb_off2cells)(off_to, max_off_to) == 1
5581 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
5583 /* Writing the second half of a double-cell character over
5584 * a double-cell character: need to redraw the second
5585 * cell. */
5586 ScreenLines[off_to + 2] = 0;
5587 redraw_next = TRUE;
5588 }
5589
5590 if (enc_dbcs == DBCS_JPNU)
5591 ScreenLines2[off_to] = ScreenLines2[off_from];
5592 }
5593 /* When writing a single-width character over a double-width
5594 * character and at the end of the redrawn text, need to clear out
5595 * the right halve of the old character.
5596 * Also required when writing the right halve of a double-width
5597 * char over the left halve of an existing one. */
5598 if (has_mbyte && col + char_cells == endcol
5599 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005600 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005602 && (*mb_off2cells)(off_to, max_off_to) == 1
5603 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 clear_next = TRUE;
5605#endif
5606
5607 ScreenLines[off_to] = ScreenLines[off_from];
5608#ifdef FEAT_MBYTE
5609 if (enc_utf8)
5610 {
5611 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5612 if (ScreenLinesUC[off_from] != 0)
5613 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005614 int i;
5615
5616 for (i = 0; i < Screen_mco; ++i)
5617 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619 }
5620 if (char_cells == 2)
5621 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5622#endif
5623
5624#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005625 /* The bold trick makes a single column of pixels appear in the
5626 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 * character should be redrawn too. This happens for our own GUI
5628 * and for some xterms. */
5629 if (
5630# ifdef FEAT_GUI
5631 gui.in_use
5632# endif
5633# if defined(FEAT_GUI) && defined(UNIX)
5634 ||
5635# endif
5636# ifdef UNIX
5637 term_is_xterm
5638# endif
5639 )
5640 {
5641 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005642 if (hl > HL_ALL)
5643 hl = syn_attr2attr(hl);
5644 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 redraw_next = TRUE;
5646 }
5647#endif
5648 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5649#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005650 /* For simplicity set the attributes of second half of a
5651 * double-wide character equal to the first half. */
5652 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005654
5655 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 else
5658#endif
5659 screen_char(off_to, row, col + coloff);
5660 }
5661 else if ( p_wiv
5662#ifdef FEAT_GUI
5663 && !gui.in_use
5664#endif
5665 && col + coloff > 0)
5666 {
5667 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5668 {
5669 /*
5670 * Don't output stop-highlight when moving the cursor, it will
5671 * stop the highlighting when it should continue.
5672 */
5673 screen_attr = 0;
5674 }
5675 else if (screen_attr != 0)
5676 screen_stop_highlight();
5677 }
5678
5679 off_to += CHAR_CELLS;
5680 off_from += CHAR_CELLS;
5681 col += CHAR_CELLS;
5682 }
5683
5684#ifdef FEAT_MBYTE
5685 if (clear_next)
5686 {
5687 /* Clear the second half of a double-wide character of which the left
5688 * half was overwritten with a single-wide character. */
5689 ScreenLines[off_to] = ' ';
5690 if (enc_utf8)
5691 ScreenLinesUC[off_to] = 0;
5692 screen_char(off_to, row, col + coloff);
5693 }
5694#endif
5695
5696 if (clear_width > 0
5697#ifdef FEAT_RIGHTLEFT
5698 && !rlflag
5699#endif
5700 )
5701 {
5702#ifdef FEAT_GUI
5703 int startCol = col;
5704#endif
5705
5706 /* blank out the rest of the line */
5707 while (col < clear_width && ScreenLines[off_to] == ' '
5708 && ScreenAttrs[off_to] == 0
5709#ifdef FEAT_MBYTE
5710 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5711#endif
5712 )
5713 {
5714 ++off_to;
5715 ++col;
5716 }
5717 if (col < clear_width)
5718 {
5719#ifdef FEAT_GUI
5720 /*
5721 * In the GUI, clearing the rest of the line may leave pixels
5722 * behind if the first character cleared was bold. Some bold
5723 * fonts spill over the left. In this case we redraw the previous
5724 * character too. If we didn't skip any blanks above, then we
5725 * only redraw if the character wasn't already redrawn anyway.
5726 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005727 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 hl = ScreenAttrs[off_to];
5730 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005731 {
5732 int prev_cells = 1;
5733# ifdef FEAT_MBYTE
5734 if (enc_utf8)
5735 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5736 * that its width is 2. */
5737 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5738 else if (enc_dbcs != 0)
5739 {
5740 /* find previous character by counting from first
5741 * column and get its width. */
5742 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005743 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005744
5745 while (off < off_to)
5746 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005747 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005748 off += prev_cells;
5749 }
5750 }
5751
5752 if (enc_dbcs != 0 && prev_cells > 1)
5753 screen_char_2(off_to - prev_cells, row,
5754 col + coloff - prev_cells);
5755 else
5756# endif
5757 screen_char(off_to - prev_cells, row,
5758 col + coloff - prev_cells);
5759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761#endif
5762 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5763 ' ', ' ', 0);
5764#ifdef FEAT_VERTSPLIT
5765 off_to += clear_width - col;
5766 col = clear_width;
5767#endif
5768 }
5769 }
5770
5771 if (clear_width > 0)
5772 {
5773#ifdef FEAT_VERTSPLIT
5774 /* For a window that's left of another, draw the separator char. */
5775 if (col + coloff < Columns)
5776 {
5777 int c;
5778
5779 c = fillchar_vsep(&hl);
5780 if (ScreenLines[off_to] != c
5781# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005782 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5783 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784# endif
5785 || ScreenAttrs[off_to] != hl)
5786 {
5787 ScreenLines[off_to] = c;
5788 ScreenAttrs[off_to] = hl;
5789# ifdef FEAT_MBYTE
5790 if (enc_utf8)
5791 {
5792 if (c >= 0x80)
5793 {
5794 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005795 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
5797 else
5798 ScreenLinesUC[off_to] = 0;
5799 }
5800# endif
5801 screen_char(off_to, row, col + coloff);
5802 }
5803 }
5804 else
5805#endif
5806 LineWraps[row] = FALSE;
5807 }
5808}
5809
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005810#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005812 * Mirror text "str" for right-left displaying.
5813 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005815 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816rl_mirror(str)
5817 char_u *str;
5818{
5819 char_u *p1, *p2;
5820 int t;
5821
5822 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5823 {
5824 t = *p1;
5825 *p1 = *p2;
5826 *p2 = t;
5827 }
5828}
5829#endif
5830
5831#if defined(FEAT_WINDOWS) || defined(PROTO)
5832/*
5833 * mark all status lines for redraw; used after first :cd
5834 */
5835 void
5836status_redraw_all()
5837{
5838 win_T *wp;
5839
5840 for (wp = firstwin; wp; wp = wp->w_next)
5841 if (wp->w_status_height)
5842 {
5843 wp->w_redr_status = TRUE;
5844 redraw_later(VALID);
5845 }
5846}
5847
5848/*
5849 * mark all status lines of the current buffer for redraw
5850 */
5851 void
5852status_redraw_curbuf()
5853{
5854 win_T *wp;
5855
5856 for (wp = firstwin; wp; wp = wp->w_next)
5857 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5858 {
5859 wp->w_redr_status = TRUE;
5860 redraw_later(VALID);
5861 }
5862}
5863
5864/*
5865 * Redraw all status lines that need to be redrawn.
5866 */
5867 void
5868redraw_statuslines()
5869{
5870 win_T *wp;
5871
5872 for (wp = firstwin; wp; wp = wp->w_next)
5873 if (wp->w_redr_status)
5874 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005875 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005876 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877}
5878#endif
5879
5880#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5881/*
5882 * Redraw all status lines at the bottom of frame "frp".
5883 */
5884 void
5885win_redraw_last_status(frp)
5886 frame_T *frp;
5887{
5888 if (frp->fr_layout == FR_LEAF)
5889 frp->fr_win->w_redr_status = TRUE;
5890 else if (frp->fr_layout == FR_ROW)
5891 {
5892 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5893 win_redraw_last_status(frp);
5894 }
5895 else /* frp->fr_layout == FR_COL */
5896 {
5897 frp = frp->fr_child;
5898 while (frp->fr_next != NULL)
5899 frp = frp->fr_next;
5900 win_redraw_last_status(frp);
5901 }
5902}
5903#endif
5904
5905#ifdef FEAT_VERTSPLIT
5906/*
5907 * Draw the verticap separator right of window "wp" starting with line "row".
5908 */
5909 static void
5910draw_vsep_win(wp, row)
5911 win_T *wp;
5912 int row;
5913{
5914 int hl;
5915 int c;
5916
5917 if (wp->w_vsep_width)
5918 {
5919 /* draw the vertical separator right of this window */
5920 c = fillchar_vsep(&hl);
5921 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5922 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5923 c, ' ', hl);
5924 }
5925}
5926#endif
5927
5928#ifdef FEAT_WILDMENU
5929static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005930static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
5932/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005933 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 */
5935 static int
5936status_match_len(xp, s)
5937 expand_T *xp;
5938 char_u *s;
5939{
5940 int len = 0;
5941
5942#ifdef FEAT_MENU
5943 int emenu = (xp->xp_context == EXPAND_MENUS
5944 || xp->xp_context == EXPAND_MENUNAMES);
5945
5946 /* Check for menu separators - replace with '|'. */
5947 if (emenu && menu_is_separator(s))
5948 return 1;
5949#endif
5950
5951 while (*s != NUL)
5952 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005953 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005954 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005955 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 }
5957
5958 return len;
5959}
5960
5961/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005962 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005963 * These are backslashes used for escaping. Do show backslashes in help tags.
5964 */
5965 static int
5966skip_status_match_char(xp, s)
5967 expand_T *xp;
5968 char_u *s;
5969{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005970 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005971#ifdef FEAT_MENU
5972 || ((xp->xp_context == EXPAND_MENUS
5973 || xp->xp_context == EXPAND_MENUNAMES)
5974 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5975#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005976 )
5977 {
5978#ifndef BACKSLASH_IN_FILENAME
5979 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5980 return 2;
5981#endif
5982 return 1;
5983 }
5984 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005985}
5986
5987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 * Show wildchar matches in the status line.
5989 * Show at least the "match" item.
5990 * We start at item 'first_match' in the list and show all matches that fit.
5991 *
5992 * If inversion is possible we use it. Else '=' characters are used.
5993 */
5994 void
5995win_redr_status_matches(xp, num_matches, matches, match, showtail)
5996 expand_T *xp;
5997 int num_matches;
5998 char_u **matches; /* list of matches */
5999 int match;
6000 int showtail;
6001{
6002#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6003 int row;
6004 char_u *buf;
6005 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006006 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 int fillchar;
6008 int attr;
6009 int i;
6010 int highlight = TRUE;
6011 char_u *selstart = NULL;
6012 int selstart_col = 0;
6013 char_u *selend = NULL;
6014 static int first_match = 0;
6015 int add_left = FALSE;
6016 char_u *s;
6017#ifdef FEAT_MENU
6018 int emenu;
6019#endif
6020#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6021 int l;
6022#endif
6023
6024 if (matches == NULL) /* interrupted completion? */
6025 return;
6026
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006027#ifdef FEAT_MBYTE
6028 if (has_mbyte)
6029 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6030 else
6031#endif
6032 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 if (buf == NULL)
6034 return;
6035
6036 if (match == -1) /* don't show match but original text */
6037 {
6038 match = 0;
6039 highlight = FALSE;
6040 }
6041 /* count 1 for the ending ">" */
6042 clen = status_match_len(xp, L_MATCH(match)) + 3;
6043 if (match == 0)
6044 first_match = 0;
6045 else if (match < first_match)
6046 {
6047 /* jumping left, as far as we can go */
6048 first_match = match;
6049 add_left = TRUE;
6050 }
6051 else
6052 {
6053 /* check if match fits on the screen */
6054 for (i = first_match; i < match; ++i)
6055 clen += status_match_len(xp, L_MATCH(i)) + 2;
6056 if (first_match > 0)
6057 clen += 2;
6058 /* jumping right, put match at the left */
6059 if ((long)clen > Columns)
6060 {
6061 first_match = match;
6062 /* if showing the last match, we can add some on the left */
6063 clen = 2;
6064 for (i = match; i < num_matches; ++i)
6065 {
6066 clen += status_match_len(xp, L_MATCH(i)) + 2;
6067 if ((long)clen >= Columns)
6068 break;
6069 }
6070 if (i == num_matches)
6071 add_left = TRUE;
6072 }
6073 }
6074 if (add_left)
6075 while (first_match > 0)
6076 {
6077 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6078 if ((long)clen >= Columns)
6079 break;
6080 --first_match;
6081 }
6082
6083 fillchar = fillchar_status(&attr, TRUE);
6084
6085 if (first_match == 0)
6086 {
6087 *buf = NUL;
6088 len = 0;
6089 }
6090 else
6091 {
6092 STRCPY(buf, "< ");
6093 len = 2;
6094 }
6095 clen = len;
6096
6097 i = first_match;
6098 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6099 {
6100 if (i == match)
6101 {
6102 selstart = buf + len;
6103 selstart_col = clen;
6104 }
6105
6106 s = L_MATCH(i);
6107 /* Check for menu separators - replace with '|' */
6108#ifdef FEAT_MENU
6109 emenu = (xp->xp_context == EXPAND_MENUS
6110 || xp->xp_context == EXPAND_MENUNAMES);
6111 if (emenu && menu_is_separator(s))
6112 {
6113 STRCPY(buf + len, transchar('|'));
6114 l = (int)STRLEN(buf + len);
6115 len += l;
6116 clen += l;
6117 }
6118 else
6119#endif
6120 for ( ; *s != NUL; ++s)
6121 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006122 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 clen += ptr2cells(s);
6124#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006125 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
6127 STRNCPY(buf + len, s, l);
6128 s += l - 1;
6129 len += l;
6130 }
6131 else
6132#endif
6133 {
6134 STRCPY(buf + len, transchar_byte(*s));
6135 len += (int)STRLEN(buf + len);
6136 }
6137 }
6138 if (i == match)
6139 selend = buf + len;
6140
6141 *(buf + len++) = ' ';
6142 *(buf + len++) = ' ';
6143 clen += 2;
6144 if (++i == num_matches)
6145 break;
6146 }
6147
6148 if (i != num_matches)
6149 {
6150 *(buf + len++) = '>';
6151 ++clen;
6152 }
6153
6154 buf[len] = NUL;
6155
6156 row = cmdline_row - 1;
6157 if (row >= 0)
6158 {
6159 if (wild_menu_showing == 0)
6160 {
6161 if (msg_scrolled > 0)
6162 {
6163 /* Put the wildmenu just above the command line. If there is
6164 * no room, scroll the screen one line up. */
6165 if (cmdline_row == Rows - 1)
6166 {
6167 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6168 ++msg_scrolled;
6169 }
6170 else
6171 {
6172 ++cmdline_row;
6173 ++row;
6174 }
6175 wild_menu_showing = WM_SCROLLED;
6176 }
6177 else
6178 {
6179 /* Create status line if needed by setting 'laststatus' to 2.
6180 * Set 'winminheight' to zero to avoid that the window is
6181 * resized. */
6182 if (lastwin->w_status_height == 0)
6183 {
6184 save_p_ls = p_ls;
6185 save_p_wmh = p_wmh;
6186 p_ls = 2;
6187 p_wmh = 0;
6188 last_status(FALSE);
6189 }
6190 wild_menu_showing = WM_SHOWN;
6191 }
6192 }
6193
6194 screen_puts(buf, row, 0, attr);
6195 if (selstart != NULL && highlight)
6196 {
6197 *selend = NUL;
6198 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6199 }
6200
6201 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6202 }
6203
6204#ifdef FEAT_VERTSPLIT
6205 win_redraw_last_status(topframe);
6206#else
6207 lastwin->w_redr_status = TRUE;
6208#endif
6209 vim_free(buf);
6210}
6211#endif
6212
6213#if defined(FEAT_WINDOWS) || defined(PROTO)
6214/*
6215 * Redraw the status line of window wp.
6216 *
6217 * If inversion is possible we use it. Else '=' characters are used.
6218 */
6219 void
6220win_redr_status(wp)
6221 win_T *wp;
6222{
6223 int row;
6224 char_u *p;
6225 int len;
6226 int fillchar;
6227 int attr;
6228 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006229 static int busy = FALSE;
6230
6231 /* It's possible to get here recursively when 'statusline' (indirectly)
6232 * invokes ":redrawstatus". Simply ignore the call then. */
6233 if (busy)
6234 return;
6235 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
6237 wp->w_redr_status = FALSE;
6238 if (wp->w_status_height == 0)
6239 {
6240 /* no status line, can only be last window */
6241 redraw_cmdline = TRUE;
6242 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006243 else if (!redrawing()
6244#ifdef FEAT_INS_EXPAND
6245 /* don't update status line when popup menu is visible and may be
6246 * drawn over it */
6247 || pum_visible()
6248#endif
6249 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251 /* Don't redraw right now, do it later. */
6252 wp->w_redr_status = TRUE;
6253 }
6254#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006255 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
6257 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006258 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
6260#endif
6261 else
6262 {
6263 fillchar = fillchar_status(&attr, wp == curwin);
6264
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006265 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 p = NameBuff;
6267 len = (int)STRLEN(p);
6268
6269 if (wp->w_buffer->b_help
6270#ifdef FEAT_QUICKFIX
6271 || wp->w_p_pvw
6272#endif
6273 || bufIsChanged(wp->w_buffer)
6274 || wp->w_buffer->b_p_ro)
6275 *(p + len++) = ' ';
6276 if (wp->w_buffer->b_help)
6277 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006278 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 len += (int)STRLEN(p + len);
6280 }
6281#ifdef FEAT_QUICKFIX
6282 if (wp->w_p_pvw)
6283 {
6284 STRCPY(p + len, _("[Preview]"));
6285 len += (int)STRLEN(p + len);
6286 }
6287#endif
6288 if (bufIsChanged(wp->w_buffer))
6289 {
6290 STRCPY(p + len, "[+]");
6291 len += 3;
6292 }
6293 if (wp->w_buffer->b_p_ro)
6294 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006295 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 len += 4;
6297 }
6298
6299#ifndef FEAT_VERTSPLIT
6300 this_ru_col = ru_col;
6301 if (this_ru_col < (Columns + 1) / 2)
6302 this_ru_col = (Columns + 1) / 2;
6303#else
6304 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6305 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6306 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6307 if (this_ru_col <= 1)
6308 {
6309 p = (char_u *)"<"; /* No room for file name! */
6310 len = 1;
6311 }
6312 else
6313#endif
6314#ifdef FEAT_MBYTE
6315 if (has_mbyte)
6316 {
6317 int clen = 0, i;
6318
6319 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006320 clen = mb_string2cells(p, -1);
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 /* Find first character that will fit.
6323 * Going from start to end is much faster for DBCS. */
6324 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006325 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 clen -= (*mb_ptr2cells)(p + i);
6327 len = clen;
6328 if (i > 0)
6329 {
6330 p = p + i - 1;
6331 *p = '<';
6332 ++len;
6333 }
6334
6335 }
6336 else
6337#endif
6338 if (len > this_ru_col - 1)
6339 {
6340 p += len - (this_ru_col - 1);
6341 *p = '<';
6342 len = this_ru_col - 1;
6343 }
6344
6345 row = W_WINROW(wp) + wp->w_height;
6346 screen_puts(p, row, W_WINCOL(wp), attr);
6347 screen_fill(row, row + 1, len + W_WINCOL(wp),
6348 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6349
6350 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6351 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6352 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6353 - 1 + W_WINCOL(wp)), attr);
6354
6355#ifdef FEAT_CMDL_INFO
6356 win_redr_ruler(wp, TRUE);
6357#endif
6358 }
6359
6360#ifdef FEAT_VERTSPLIT
6361 /*
6362 * May need to draw the character below the vertical separator.
6363 */
6364 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6365 {
6366 if (stl_connected(wp))
6367 fillchar = fillchar_status(&attr, wp == curwin);
6368 else
6369 fillchar = fillchar_vsep(&attr);
6370 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6371 attr);
6372 }
6373#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006374 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375}
6376
Bram Moolenaar238a5642006-02-21 22:12:05 +00006377#ifdef FEAT_STL_OPT
6378/*
6379 * Redraw the status line according to 'statusline' and take care of any
6380 * errors encountered.
6381 */
6382 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006383redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006384 win_T *wp;
6385{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006386 static int entered = FALSE;
6387 int save_called_emsg = called_emsg;
6388
6389 /* When called recursively return. This can happen when the statusline
6390 * contains an expression that triggers a redraw. */
6391 if (entered)
6392 return;
6393 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006394
6395 called_emsg = FALSE;
6396 win_redr_custom(wp, FALSE);
6397 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006398 {
6399 /* When there is an error disable the statusline, otherwise the
6400 * display is messed up with errors and a redraw triggers the problem
6401 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006402 set_string_option_direct((char_u *)"statusline", -1,
6403 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006404 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006405 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006406 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006407 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006408}
6409#endif
6410
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411# ifdef FEAT_VERTSPLIT
6412/*
6413 * Return TRUE if the status line of window "wp" is connected to the status
6414 * line of the window right of it. If not, then it's a vertical separator.
6415 * Only call if (wp->w_vsep_width != 0).
6416 */
6417 int
6418stl_connected(wp)
6419 win_T *wp;
6420{
6421 frame_T *fr;
6422
6423 fr = wp->w_frame;
6424 while (fr->fr_parent != NULL)
6425 {
6426 if (fr->fr_parent->fr_layout == FR_COL)
6427 {
6428 if (fr->fr_next != NULL)
6429 break;
6430 }
6431 else
6432 {
6433 if (fr->fr_next != NULL)
6434 return TRUE;
6435 }
6436 fr = fr->fr_parent;
6437 }
6438 return FALSE;
6439}
6440# endif
6441
6442#endif /* FEAT_WINDOWS */
6443
6444#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6445/*
6446 * Get the value to show for the language mappings, active 'keymap'.
6447 */
6448 int
6449get_keymap_str(wp, buf, len)
6450 win_T *wp;
6451 char_u *buf; /* buffer for the result */
6452 int len; /* length of buffer */
6453{
6454 char_u *p;
6455
6456 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6457 return FALSE;
6458
6459 {
6460#ifdef FEAT_EVAL
6461 buf_T *old_curbuf = curbuf;
6462 win_T *old_curwin = curwin;
6463 char_u *s;
6464
6465 curbuf = wp->w_buffer;
6466 curwin = wp;
6467 STRCPY(buf, "b:keymap_name"); /* must be writable */
6468 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006469 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 --emsg_skip;
6471 curbuf = old_curbuf;
6472 curwin = old_curwin;
6473 if (p == NULL || *p == NUL)
6474#endif
6475 {
6476#ifdef FEAT_KEYMAP
6477 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6478 p = wp->w_buffer->b_p_keymap;
6479 else
6480#endif
6481 p = (char_u *)"lang";
6482 }
6483 if ((int)(STRLEN(p) + 3) < len)
6484 sprintf((char *)buf, "<%s>", p);
6485 else
6486 buf[0] = NUL;
6487#ifdef FEAT_EVAL
6488 vim_free(s);
6489#endif
6490 }
6491 return buf[0] != NUL;
6492}
6493#endif
6494
6495#if defined(FEAT_STL_OPT) || defined(PROTO)
6496/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006497 * Redraw the status line or ruler of window "wp".
6498 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 */
6500 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006501win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006503 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 int attr;
6506 int curattr;
6507 int row;
6508 int col = 0;
6509 int maxwidth;
6510 int width;
6511 int n;
6512 int len;
6513 int fillchar;
6514 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006515 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006517 struct stl_hlrec hltab[STL_MAX_ITEM];
6518 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006519 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006520 win_T *ewp;
6521 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522
6523 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006524 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006526 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006527 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006528 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006529 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006530 attr = hl_attr(HLF_TPF);
6531 maxwidth = Columns;
6532# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006533 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006536 else
6537 {
6538 row = W_WINROW(wp) + wp->w_height;
6539 fillchar = fillchar_status(&attr, wp == curwin);
6540 maxwidth = W_WIDTH(wp);
6541
6542 if (draw_ruler)
6543 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006544 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006545 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006546 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006547 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006548 if (*++stl == '-')
6549 stl++;
6550 if (atoi((char *)stl))
6551 while (VIM_ISDIGIT(*stl))
6552 stl++;
6553 if (*stl++ != '(')
6554 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006555 }
6556#ifdef FEAT_VERTSPLIT
6557 col = ru_col - (Columns - W_WIDTH(wp));
6558 if (col < (W_WIDTH(wp) + 1) / 2)
6559 col = (W_WIDTH(wp) + 1) / 2;
6560#else
6561 col = ru_col;
6562 if (col > (Columns + 1) / 2)
6563 col = (Columns + 1) / 2;
6564#endif
6565 maxwidth = W_WIDTH(wp) - col;
6566#ifdef FEAT_WINDOWS
6567 if (!wp->w_status_height)
6568#endif
6569 {
6570 row = Rows - 1;
6571 --maxwidth; /* writing in last column may cause scrolling */
6572 fillchar = ' ';
6573 attr = 0;
6574 }
6575
6576# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006577 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006578# endif
6579 }
6580 else
6581 {
6582 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006583 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006584 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006585 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006586# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006587 use_sandbox = was_set_insecurely((char_u *)"statusline",
6588 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006589# endif
6590 }
6591
6592#ifdef FEAT_VERTSPLIT
6593 col += W_WINCOL(wp);
6594#endif
6595 }
6596
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 if (maxwidth <= 0)
6598 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
Bram Moolenaar61452852011-02-01 18:01:11 +01006600 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6601 * the cursor away and back. */
6602 ewp = wp == NULL ? curwin : wp;
6603 p_crb_save = ewp->w_p_crb;
6604 ewp->w_p_crb = FALSE;
6605
Bram Moolenaar362f3562009-11-03 16:20:34 +00006606 /* Make a copy, because the statusline may include a function call that
6607 * might change the option value and free the memory. */
6608 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006609 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006610 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006611 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006612 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006613 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006615 /* Make all characters printable. */
6616 p = transstr(buf);
6617 if (p != NULL)
6618 {
6619 vim_strncpy(buf, p, sizeof(buf) - 1);
6620 vim_free(p);
6621 }
6622
6623 /* fill up with "fillchar" */
6624 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006625 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {
6627#ifdef FEAT_MBYTE
6628 len += (*mb_char2bytes)(fillchar, buf + len);
6629#else
6630 buf[len++] = fillchar;
6631#endif
6632 ++width;
6633 }
6634 buf[len] = NUL;
6635
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006636 /*
6637 * Draw each snippet with the specified highlighting.
6638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 curattr = attr;
6640 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006641 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006643 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 screen_puts_len(p, len, row, col, curattr);
6645 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006646 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006648 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006650 else if (hltab[n].userhl < 0)
6651 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006653 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006654 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655#endif
6656 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006657 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006660
6661 if (wp == NULL)
6662 {
6663 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6664 col = 0;
6665 len = 0;
6666 p = buf;
6667 fillchar = 0;
6668 for (n = 0; tabtab[n].start != NULL; n++)
6669 {
6670 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6671 while (col < len)
6672 TabPageIdxs[col++] = fillchar;
6673 p = tabtab[n].start;
6674 fillchar = tabtab[n].userhl;
6675 }
6676 while (col < Columns)
6677 TabPageIdxs[col++] = fillchar;
6678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
6680
6681#endif /* FEAT_STL_OPT */
6682
6683/*
6684 * Output a single character directly to the screen and update ScreenLines.
6685 */
6686 void
6687screen_putchar(c, row, col, attr)
6688 int c;
6689 int row, col;
6690 int attr;
6691{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 char_u buf[MB_MAXBYTES + 1];
6693
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006694#ifdef FEAT_MBYTE
6695 if (has_mbyte)
6696 buf[(*mb_char2bytes)(c, buf)] = NUL;
6697 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006699 {
6700 buf[0] = c;
6701 buf[1] = NUL;
6702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 screen_puts(buf, row, col, attr);
6704}
6705
6706/*
6707 * Get a single character directly from ScreenLines into "bytes[]".
6708 * Also return its attribute in *attrp;
6709 */
6710 void
6711screen_getbytes(row, col, bytes, attrp)
6712 int row, col;
6713 char_u *bytes;
6714 int *attrp;
6715{
6716 unsigned off;
6717
6718 /* safety check */
6719 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6720 {
6721 off = LineOffset[row] + col;
6722 *attrp = ScreenAttrs[off];
6723 bytes[0] = ScreenLines[off];
6724 bytes[1] = NUL;
6725
6726#ifdef FEAT_MBYTE
6727 if (enc_utf8 && ScreenLinesUC[off] != 0)
6728 bytes[utfc_char2bytes(off, bytes)] = NUL;
6729 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6730 {
6731 bytes[0] = ScreenLines[off];
6732 bytes[1] = ScreenLines2[off];
6733 bytes[2] = NUL;
6734 }
6735 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6736 {
6737 bytes[1] = ScreenLines[off + 1];
6738 bytes[2] = NUL;
6739 }
6740#endif
6741 }
6742}
6743
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006744#ifdef FEAT_MBYTE
6745static int screen_comp_differs __ARGS((int, int*));
6746
6747/*
6748 * Return TRUE if composing characters for screen posn "off" differs from
6749 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006750 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006751 */
6752 static int
6753screen_comp_differs(off, u8cc)
6754 int off;
6755 int *u8cc;
6756{
6757 int i;
6758
6759 for (i = 0; i < Screen_mco; ++i)
6760 {
6761 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6762 return TRUE;
6763 if (u8cc[i] == 0)
6764 break;
6765 }
6766 return FALSE;
6767}
6768#endif
6769
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770/*
6771 * Put string '*text' on the screen at position 'row' and 'col', with
6772 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6773 * Note: only outputs within one row, message is truncated at screen boundary!
6774 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6775 */
6776 void
6777screen_puts(text, row, col, attr)
6778 char_u *text;
6779 int row;
6780 int col;
6781 int attr;
6782{
6783 screen_puts_len(text, -1, row, col, attr);
6784}
6785
6786/*
6787 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6788 * a NUL.
6789 */
6790 void
6791screen_puts_len(text, len, row, col, attr)
6792 char_u *text;
6793 int len;
6794 int row;
6795 int col;
6796 int attr;
6797{
6798 unsigned off;
6799 char_u *ptr = text;
6800 int c;
6801#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006802 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 int mbyte_blen = 1;
6804 int mbyte_cells = 1;
6805 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006806 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 int clear_next_cell = FALSE;
6808# ifdef FEAT_ARABIC
6809 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006810 int pc, nc, nc1;
6811 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812# endif
6813#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006814#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6815 int force_redraw_this;
6816 int force_redraw_next = FALSE;
6817#endif
6818 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
6820 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6821 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006822 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
Bram Moolenaarc236c162008-07-13 17:41:49 +00006824#ifdef FEAT_MBYTE
6825 /* When drawing over the right halve of a double-wide char clear out the
6826 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006827 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006828# ifdef FEAT_GUI
6829 && !gui.in_use
6830# endif
6831 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006832 {
6833 ScreenLines[off - 1] = ' ';
6834 ScreenAttrs[off - 1] = 0;
6835 if (enc_utf8)
6836 {
6837 ScreenLinesUC[off - 1] = 0;
6838 ScreenLinesC[0][off - 1] = 0;
6839 }
6840 /* redraw the previous cell, make it empty */
6841 screen_char(off - 1, row, col - 1);
6842 /* force the cell at "col" to be redrawn */
6843 force_redraw_next = TRUE;
6844 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006845#endif
6846
Bram Moolenaar367329b2007-08-30 11:53:22 +00006847#ifdef FEAT_MBYTE
6848 max_off = LineOffset[row] + screen_Columns;
6849#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006850 while (col < screen_Columns
6851 && (len < 0 || (int)(ptr - text) < len)
6852 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {
6854 c = *ptr;
6855#ifdef FEAT_MBYTE
6856 /* check if this is the first byte of a multibyte */
6857 if (has_mbyte)
6858 {
6859 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006860 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006862 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6864 mbyte_cells = 1;
6865 else if (enc_dbcs != 0)
6866 mbyte_cells = mbyte_blen;
6867 else /* enc_utf8 */
6868 {
6869 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006870 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 (int)((text + len) - ptr));
6872 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006873 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006875# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 /* Non-BMP character: display as ? or fullwidth ?. */
6877 if (u8c >= 0x10000)
6878 {
6879 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6880 if (attr == 0)
6881 attr = hl_attr(HLF_8);
6882 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884# ifdef FEAT_ARABIC
6885 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6886 {
6887 /* Do Arabic shaping. */
6888 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6889 {
6890 /* Past end of string to be displayed. */
6891 nc = NUL;
6892 nc1 = NUL;
6893 }
6894 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006895 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006896 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6897 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006898 nc1 = pcc[0];
6899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 pc = prev_c;
6901 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006902 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 }
6904 else
6905 prev_c = u8c;
6906# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006907 if (col + mbyte_cells > screen_Columns)
6908 {
6909 /* Only 1 cell left, but character requires 2 cells:
6910 * display a '>' in the last column to avoid wrapping. */
6911 c = '>';
6912 mbyte_cells = 1;
6913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 }
6915 }
6916#endif
6917
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006918#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6919 force_redraw_this = force_redraw_next;
6920 force_redraw_next = FALSE;
6921#endif
6922
6923 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924#ifdef FEAT_MBYTE
6925 || (mbyte_cells == 2
6926 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6927 || (enc_dbcs == DBCS_JPNU
6928 && c == 0x8e
6929 && ScreenLines2[off] != ptr[1])
6930 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006931 && (ScreenLinesUC[off] !=
6932 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6933 || (ScreenLinesUC[off] != 0
6934 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935#endif
6936 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006937 || exmode_active;
6938
6939 if (need_redraw
6940#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6941 || force_redraw_this
6942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 )
6944 {
6945#if defined(FEAT_GUI) || defined(UNIX)
6946 /* The bold trick makes a single row of pixels appear in the next
6947 * character. When a bold character is removed, the next
6948 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006949 * and for some xterms. */
6950 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951# ifdef FEAT_GUI
6952 gui.in_use
6953# endif
6954# if defined(FEAT_GUI) && defined(UNIX)
6955 ||
6956# endif
6957# ifdef UNIX
6958 term_is_xterm
6959# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006960 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006962 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006964 if (n > HL_ALL)
6965 n = syn_attr2attr(n);
6966 if (n & HL_BOLD)
6967 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 }
6969#endif
6970#ifdef FEAT_MBYTE
6971 /* When at the end of the text and overwriting a two-cell
6972 * character with a one-cell character, need to clear the next
6973 * cell. Also when overwriting the left halve of a two-cell char
6974 * with the right halve of a two-cell char. Do this only once
6975 * (mb_off2cells() may return 2 on the right halve). */
6976 if (clear_next_cell)
6977 clear_next_cell = FALSE;
6978 else if (has_mbyte
6979 && (len < 0 ? ptr[mbyte_blen] == NUL
6980 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006981 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006983 && (*mb_off2cells)(off, max_off) == 1
6984 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 clear_next_cell = TRUE;
6986
6987 /* Make sure we never leave a second byte of a double-byte behind,
6988 * it confuses mb_off2cells(). */
6989 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006990 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006992 && (*mb_off2cells)(off, max_off) == 1
6993 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 ScreenLines[off + mbyte_blen] = 0;
6995#endif
6996 ScreenLines[off] = c;
6997 ScreenAttrs[off] = attr;
6998#ifdef FEAT_MBYTE
6999 if (enc_utf8)
7000 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007001 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 ScreenLinesUC[off] = 0;
7003 else
7004 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007005 int i;
7006
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007008 for (i = 0; i < Screen_mco; ++i)
7009 {
7010 ScreenLinesC[i][off] = u8cc[i];
7011 if (u8cc[i] == 0)
7012 break;
7013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015 if (mbyte_cells == 2)
7016 {
7017 ScreenLines[off + 1] = 0;
7018 ScreenAttrs[off + 1] = attr;
7019 }
7020 screen_char(off, row, col);
7021 }
7022 else if (mbyte_cells == 2)
7023 {
7024 ScreenLines[off + 1] = ptr[1];
7025 ScreenAttrs[off + 1] = attr;
7026 screen_char_2(off, row, col);
7027 }
7028 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7029 {
7030 ScreenLines2[off] = ptr[1];
7031 screen_char(off, row, col);
7032 }
7033 else
7034#endif
7035 screen_char(off, row, col);
7036 }
7037#ifdef FEAT_MBYTE
7038 if (has_mbyte)
7039 {
7040 off += mbyte_cells;
7041 col += mbyte_cells;
7042 ptr += mbyte_blen;
7043 if (clear_next_cell)
7044 ptr = (char_u *)" ";
7045 }
7046 else
7047#endif
7048 {
7049 ++off;
7050 ++col;
7051 ++ptr;
7052 }
7053 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007054
7055#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7056 /* If we detected the next character needs to be redrawn, but the text
7057 * doesn't extend up to there, update the character here. */
7058 if (force_redraw_next && col < screen_Columns)
7059 {
7060# ifdef FEAT_MBYTE
7061 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7062 screen_char_2(off, row, col);
7063 else
7064# endif
7065 screen_char(off, row, col);
7066 }
7067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068}
7069
7070#ifdef FEAT_SEARCH_EXTRA
7071/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007072 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 */
7074 static void
7075start_search_hl()
7076{
7077 if (p_hls && !no_hlsearch)
7078 {
7079 last_pat_prog(&search_hl.rm);
7080 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007081# ifdef FEAT_RELTIME
7082 /* Set the time limit to 'redrawtime'. */
7083 profile_setlimit(p_rdt, &search_hl.tm);
7084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
7086}
7087
7088/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007089 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 */
7091 static void
7092end_search_hl()
7093{
7094 if (search_hl.rm.regprog != NULL)
7095 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007096 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 search_hl.rm.regprog = NULL;
7098 }
7099}
7100
7101/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007102 * Init for calling prepare_search_hl().
7103 */
7104 static void
7105init_search_hl(wp)
7106 win_T *wp;
7107{
7108 matchitem_T *cur;
7109
7110 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7111 * match */
7112 cur = wp->w_match_head;
7113 while (cur != NULL)
7114 {
7115 cur->hl.rm = cur->match;
7116 if (cur->hlg_id == 0)
7117 cur->hl.attr = 0;
7118 else
7119 cur->hl.attr = syn_id2attr(cur->hlg_id);
7120 cur->hl.buf = wp->w_buffer;
7121 cur->hl.lnum = 0;
7122 cur->hl.first_lnum = 0;
7123# ifdef FEAT_RELTIME
7124 /* Set the time limit to 'redrawtime'. */
7125 profile_setlimit(p_rdt, &(cur->hl.tm));
7126# endif
7127 cur = cur->next;
7128 }
7129 search_hl.buf = wp->w_buffer;
7130 search_hl.lnum = 0;
7131 search_hl.first_lnum = 0;
7132 /* time limit is set at the toplevel, for all windows */
7133}
7134
7135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 * Advance to the match in window "wp" line "lnum" or past it.
7137 */
7138 static void
7139prepare_search_hl(wp, lnum)
7140 win_T *wp;
7141 linenr_T lnum;
7142{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007143 matchitem_T *cur; /* points to the match list */
7144 match_T *shl; /* points to search_hl or a match */
7145 int shl_flag; /* flag to indicate whether search_hl
7146 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 int n;
7148
7149 /*
7150 * When using a multi-line pattern, start searching at the top
7151 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007152 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007154 cur = wp->w_match_head;
7155 shl_flag = FALSE;
7156 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007158 if (shl_flag == FALSE)
7159 {
7160 shl = &search_hl;
7161 shl_flag = TRUE;
7162 }
7163 else
7164 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 if (shl->rm.regprog != NULL
7166 && shl->lnum == 0
7167 && re_multiline(shl->rm.regprog))
7168 {
7169 if (shl->first_lnum == 0)
7170 {
7171# ifdef FEAT_FOLDING
7172 for (shl->first_lnum = lnum;
7173 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7174 if (hasFoldingWin(wp, shl->first_lnum - 1,
7175 NULL, NULL, TRUE, NULL))
7176 break;
7177# else
7178 shl->first_lnum = wp->w_topline;
7179# endif
7180 }
7181 n = 0;
7182 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7183 {
7184 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7185 if (shl->lnum != 0)
7186 {
7187 shl->first_lnum = shl->lnum
7188 + shl->rm.endpos[0].lnum
7189 - shl->rm.startpos[0].lnum;
7190 n = shl->rm.endpos[0].col;
7191 }
7192 else
7193 {
7194 ++shl->first_lnum;
7195 n = 0;
7196 }
7197 }
7198 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007199 if (shl != &search_hl && cur != NULL)
7200 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 }
7202}
7203
7204/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007205 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 * Uses shl->buf.
7207 * Sets shl->lnum and shl->rm contents.
7208 * Note: Assumes a previous match is always before "lnum", unless
7209 * shl->lnum is zero.
7210 * Careful: Any pointers for buffer lines will become invalid.
7211 */
7212 static void
7213next_search_hl(win, shl, lnum, mincol)
7214 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007215 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 linenr_T lnum;
7217 colnr_T mincol; /* minimal column for a match */
7218{
7219 linenr_T l;
7220 colnr_T matchcol;
7221 long nmatched;
7222
7223 if (shl->lnum != 0)
7224 {
7225 /* Check for three situations:
7226 * 1. If the "lnum" is below a previous match, start a new search.
7227 * 2. If the previous match includes "mincol", use it.
7228 * 3. Continue after the previous match.
7229 */
7230 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7231 if (lnum > l)
7232 shl->lnum = 0;
7233 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7234 return;
7235 }
7236
7237 /*
7238 * Repeat searching for a match until one is found that includes "mincol"
7239 * or none is found in this line.
7240 */
7241 called_emsg = FALSE;
7242 for (;;)
7243 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007244#ifdef FEAT_RELTIME
7245 /* Stop searching after passing the time limit. */
7246 if (profile_passed_limit(&(shl->tm)))
7247 {
7248 shl->lnum = 0; /* no match found in time */
7249 break;
7250 }
7251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 /* Three situations:
7253 * 1. No useful previous match: search from start of line.
7254 * 2. Not Vi compatible or empty match: continue at next character.
7255 * Break the loop if this is beyond the end of the line.
7256 * 3. Vi compatible searching: continue at end of previous match.
7257 */
7258 if (shl->lnum == 0)
7259 matchcol = 0;
7260 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7261 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007262 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007264 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007265
7266 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007267 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007268 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007270 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 shl->lnum = 0;
7272 break;
7273 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007274#ifdef FEAT_MBYTE
7275 if (has_mbyte)
7276 matchcol += mb_ptr2len(ml);
7277 else
7278#endif
7279 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281 else
7282 matchcol = shl->rm.endpos[0].col;
7283
7284 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007285 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7286#ifdef FEAT_RELTIME
7287 &(shl->tm)
7288#else
7289 NULL
7290#endif
7291 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007292 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
7294 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007295 if (shl == &search_hl)
7296 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007297 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar473de612013-06-08 18:19:48 +02007298 vim_regfree(shl->rm.regprog);
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007299 no_hlsearch = TRUE;
7300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007302 shl->lnum = 0;
7303 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 break;
7305 }
7306 if (nmatched == 0)
7307 {
7308 shl->lnum = 0; /* no match found */
7309 break;
7310 }
7311 if (shl->rm.startpos[0].lnum > 0
7312 || shl->rm.startpos[0].col >= mincol
7313 || nmatched > 1
7314 || shl->rm.endpos[0].col > mincol)
7315 {
7316 shl->lnum += shl->rm.startpos[0].lnum;
7317 break; /* useful match found */
7318 }
7319 }
7320}
7321#endif
7322
7323 static void
7324screen_start_highlight(attr)
7325 int attr;
7326{
7327 attrentry_T *aep = NULL;
7328
7329 screen_attr = attr;
7330 if (full_screen
7331#ifdef WIN3264
7332 && termcap_active
7333#endif
7334 )
7335 {
7336#ifdef FEAT_GUI
7337 if (gui.in_use)
7338 {
7339 char buf[20];
7340
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007341 /* The GUI handles this internally. */
7342 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 OUT_STR(buf);
7344 }
7345 else
7346#endif
7347 {
7348 if (attr > HL_ALL) /* special HL attr. */
7349 {
7350 if (t_colors > 1)
7351 aep = syn_cterm_attr2entry(attr);
7352 else
7353 aep = syn_term_attr2entry(attr);
7354 if (aep == NULL) /* did ":syntax clear" */
7355 attr = 0;
7356 else
7357 attr = aep->ae_attr;
7358 }
7359 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7360 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007361 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7362 && cterm_normal_fg_bold)
7363 /* If the Normal FG color has BOLD attribute and the new HL
7364 * has a FG color defined, clear BOLD. */
7365 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7367 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007368 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7369 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 out_str(T_US);
7371 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7372 out_str(T_CZH);
7373 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7374 out_str(T_MR);
7375
7376 /*
7377 * Output the color or start string after bold etc., in case the
7378 * bold etc. override the color setting.
7379 */
7380 if (aep != NULL)
7381 {
7382 if (t_colors > 1)
7383 {
7384 if (aep->ae_u.cterm.fg_color)
7385 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7386 if (aep->ae_u.cterm.bg_color)
7387 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7388 }
7389 else
7390 {
7391 if (aep->ae_u.term.start != NULL)
7392 out_str(aep->ae_u.term.start);
7393 }
7394 }
7395 }
7396 }
7397}
7398
7399 void
7400screen_stop_highlight()
7401{
7402 int do_ME = FALSE; /* output T_ME code */
7403
7404 if (screen_attr != 0
7405#ifdef WIN3264
7406 && termcap_active
7407#endif
7408 )
7409 {
7410#ifdef FEAT_GUI
7411 if (gui.in_use)
7412 {
7413 char buf[20];
7414
7415 /* use internal GUI code */
7416 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7417 OUT_STR(buf);
7418 }
7419 else
7420#endif
7421 {
7422 if (screen_attr > HL_ALL) /* special HL attr. */
7423 {
7424 attrentry_T *aep;
7425
7426 if (t_colors > 1)
7427 {
7428 /*
7429 * Assume that t_me restores the original colors!
7430 */
7431 aep = syn_cterm_attr2entry(screen_attr);
7432 if (aep != NULL && (aep->ae_u.cterm.fg_color
7433 || aep->ae_u.cterm.bg_color))
7434 do_ME = TRUE;
7435 }
7436 else
7437 {
7438 aep = syn_term_attr2entry(screen_attr);
7439 if (aep != NULL && aep->ae_u.term.stop != NULL)
7440 {
7441 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7442 do_ME = TRUE;
7443 else
7444 out_str(aep->ae_u.term.stop);
7445 }
7446 }
7447 if (aep == NULL) /* did ":syntax clear" */
7448 screen_attr = 0;
7449 else
7450 screen_attr = aep->ae_attr;
7451 }
7452
7453 /*
7454 * Often all ending-codes are equal to T_ME. Avoid outputting the
7455 * same sequence several times.
7456 */
7457 if (screen_attr & HL_STANDOUT)
7458 {
7459 if (STRCMP(T_SE, T_ME) == 0)
7460 do_ME = TRUE;
7461 else
7462 out_str(T_SE);
7463 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007464 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 {
7466 if (STRCMP(T_UE, T_ME) == 0)
7467 do_ME = TRUE;
7468 else
7469 out_str(T_UE);
7470 }
7471 if (screen_attr & HL_ITALIC)
7472 {
7473 if (STRCMP(T_CZR, T_ME) == 0)
7474 do_ME = TRUE;
7475 else
7476 out_str(T_CZR);
7477 }
7478 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7479 out_str(T_ME);
7480
7481 if (t_colors > 1)
7482 {
7483 /* set Normal cterm colors */
7484 if (cterm_normal_fg_color != 0)
7485 term_fg_color(cterm_normal_fg_color - 1);
7486 if (cterm_normal_bg_color != 0)
7487 term_bg_color(cterm_normal_bg_color - 1);
7488 if (cterm_normal_fg_bold)
7489 out_str(T_MD);
7490 }
7491 }
7492 }
7493 screen_attr = 0;
7494}
7495
7496/*
7497 * Reset the colors for a cterm. Used when leaving Vim.
7498 * The machine specific code may override this again.
7499 */
7500 void
7501reset_cterm_colors()
7502{
7503 if (t_colors > 1)
7504 {
7505 /* set Normal cterm colors */
7506 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7507 {
7508 out_str(T_OP);
7509 screen_attr = -1;
7510 }
7511 if (cterm_normal_fg_bold)
7512 {
7513 out_str(T_ME);
7514 screen_attr = -1;
7515 }
7516 }
7517}
7518
7519/*
7520 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7521 * using the attributes from ScreenAttrs["off"].
7522 */
7523 static void
7524screen_char(off, row, col)
7525 unsigned off;
7526 int row;
7527 int col;
7528{
7529 int attr;
7530
7531 /* Check for illegal values, just in case (could happen just after
7532 * resizing). */
7533 if (row >= screen_Rows || col >= screen_Columns)
7534 return;
7535
7536 /* Outputting the last character on the screen may scrollup the screen.
7537 * Don't to it! Mark the character invalid (update it when scrolled up) */
7538 if (row == screen_Rows - 1 && col == screen_Columns - 1
7539#ifdef FEAT_RIGHTLEFT
7540 /* account for first command-line character in rightleft mode */
7541 && !cmdmsg_rl
7542#endif
7543 )
7544 {
7545 ScreenAttrs[off] = (sattr_T)-1;
7546 return;
7547 }
7548
7549 /*
7550 * Stop highlighting first, so it's easier to move the cursor.
7551 */
7552#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7553 if (screen_char_attr != 0)
7554 attr = screen_char_attr;
7555 else
7556#endif
7557 attr = ScreenAttrs[off];
7558 if (screen_attr != attr)
7559 screen_stop_highlight();
7560
7561 windgoto(row, col);
7562
7563 if (screen_attr != attr)
7564 screen_start_highlight(attr);
7565
7566#ifdef FEAT_MBYTE
7567 if (enc_utf8 && ScreenLinesUC[off] != 0)
7568 {
7569 char_u buf[MB_MAXBYTES + 1];
7570
7571 /* Convert UTF-8 character to bytes and write it. */
7572
7573 buf[utfc_char2bytes(off, buf)] = NUL;
7574
7575 out_str(buf);
7576 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7577 ++screen_cur_col;
7578 }
7579 else
7580#endif
7581 {
7582#ifdef FEAT_MBYTE
7583 out_flush_check();
7584#endif
7585 out_char(ScreenLines[off]);
7586#ifdef FEAT_MBYTE
7587 /* double-byte character in single-width cell */
7588 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7589 out_char(ScreenLines2[off]);
7590#endif
7591 }
7592
7593 screen_cur_col++;
7594}
7595
7596#ifdef FEAT_MBYTE
7597
7598/*
7599 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7600 * on the screen at position 'row' and 'col'.
7601 * The attributes of the first byte is used for all. This is required to
7602 * output the two bytes of a double-byte character with nothing in between.
7603 */
7604 static void
7605screen_char_2(off, row, col)
7606 unsigned off;
7607 int row;
7608 int col;
7609{
7610 /* Check for illegal values (could be wrong when screen was resized). */
7611 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7612 return;
7613
7614 /* Outputting the last character on the screen may scrollup the screen.
7615 * Don't to it! Mark the character invalid (update it when scrolled up) */
7616 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7617 {
7618 ScreenAttrs[off] = (sattr_T)-1;
7619 return;
7620 }
7621
7622 /* Output the first byte normally (positions the cursor), then write the
7623 * second byte directly. */
7624 screen_char(off, row, col);
7625 out_char(ScreenLines[off + 1]);
7626 ++screen_cur_col;
7627}
7628#endif
7629
7630#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7631/*
7632 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7633 * This uses the contents of ScreenLines[] and doesn't change it.
7634 */
7635 void
7636screen_draw_rectangle(row, col, height, width, invert)
7637 int row;
7638 int col;
7639 int height;
7640 int width;
7641 int invert;
7642{
7643 int r, c;
7644 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007645#ifdef FEAT_MBYTE
7646 int max_off;
7647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007649 /* Can't use ScreenLines unless initialized */
7650 if (ScreenLines == NULL)
7651 return;
7652
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (invert)
7654 screen_char_attr = HL_INVERSE;
7655 for (r = row; r < row + height; ++r)
7656 {
7657 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007658#ifdef FEAT_MBYTE
7659 max_off = off + screen_Columns;
7660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 for (c = col; c < col + width; ++c)
7662 {
7663#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007664 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 {
7666 screen_char_2(off + c, r, c);
7667 ++c;
7668 }
7669 else
7670#endif
7671 {
7672 screen_char(off + c, r, c);
7673#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007674 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 ++c;
7676#endif
7677 }
7678 }
7679 }
7680 screen_char_attr = 0;
7681}
7682#endif
7683
7684#ifdef FEAT_VERTSPLIT
7685/*
7686 * Redraw the characters for a vertically split window.
7687 */
7688 static void
7689redraw_block(row, end, wp)
7690 int row;
7691 int end;
7692 win_T *wp;
7693{
7694 int col;
7695 int width;
7696
7697# ifdef FEAT_CLIPBOARD
7698 clip_may_clear_selection(row, end - 1);
7699# endif
7700
7701 if (wp == NULL)
7702 {
7703 col = 0;
7704 width = Columns;
7705 }
7706 else
7707 {
7708 col = wp->w_wincol;
7709 width = wp->w_width;
7710 }
7711 screen_draw_rectangle(row, col, end - row, width, FALSE);
7712}
7713#endif
7714
7715/*
7716 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7717 * with character 'c1' in first column followed by 'c2' in the other columns.
7718 * Use attributes 'attr'.
7719 */
7720 void
7721screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7722 int start_row, end_row;
7723 int start_col, end_col;
7724 int c1, c2;
7725 int attr;
7726{
7727 int row;
7728 int col;
7729 int off;
7730 int end_off;
7731 int did_delete;
7732 int c;
7733 int norm_term;
7734#if defined(FEAT_GUI) || defined(UNIX)
7735 int force_next = FALSE;
7736#endif
7737
7738 if (end_row > screen_Rows) /* safety check */
7739 end_row = screen_Rows;
7740 if (end_col > screen_Columns) /* safety check */
7741 end_col = screen_Columns;
7742 if (ScreenLines == NULL
7743 || start_row >= end_row
7744 || start_col >= end_col) /* nothing to do */
7745 return;
7746
7747 /* it's a "normal" terminal when not in a GUI or cterm */
7748 norm_term = (
7749#ifdef FEAT_GUI
7750 !gui.in_use &&
7751#endif
7752 t_colors <= 1);
7753 for (row = start_row; row < end_row; ++row)
7754 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007755#ifdef FEAT_MBYTE
7756 if (has_mbyte
7757# ifdef FEAT_GUI
7758 && !gui.in_use
7759# endif
7760 )
7761 {
7762 /* When drawing over the right halve of a double-wide char clear
7763 * out the left halve. When drawing over the left halve of a
7764 * double wide-char clear out the right halve. Only needed in a
7765 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007766 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007767 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007768 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007769 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007770 }
7771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 /*
7773 * Try to use delete-line termcap code, when no attributes or in a
7774 * "normal" terminal, where a bold/italic space is just a
7775 * space.
7776 */
7777 did_delete = FALSE;
7778 if (c2 == ' '
7779 && end_col == Columns
7780 && can_clear(T_CE)
7781 && (attr == 0
7782 || (norm_term
7783 && attr <= HL_ALL
7784 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7785 {
7786 /*
7787 * check if we really need to clear something
7788 */
7789 col = start_col;
7790 if (c1 != ' ') /* don't clear first char */
7791 ++col;
7792
7793 off = LineOffset[row] + col;
7794 end_off = LineOffset[row] + end_col;
7795
7796 /* skip blanks (used often, keep it fast!) */
7797#ifdef FEAT_MBYTE
7798 if (enc_utf8)
7799 while (off < end_off && ScreenLines[off] == ' '
7800 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7801 ++off;
7802 else
7803#endif
7804 while (off < end_off && ScreenLines[off] == ' '
7805 && ScreenAttrs[off] == 0)
7806 ++off;
7807 if (off < end_off) /* something to be cleared */
7808 {
7809 col = off - LineOffset[row];
7810 screen_stop_highlight();
7811 term_windgoto(row, col);/* clear rest of this screen line */
7812 out_str(T_CE);
7813 screen_start(); /* don't know where cursor is now */
7814 col = end_col - col;
7815 while (col--) /* clear chars in ScreenLines */
7816 {
7817 ScreenLines[off] = ' ';
7818#ifdef FEAT_MBYTE
7819 if (enc_utf8)
7820 ScreenLinesUC[off] = 0;
7821#endif
7822 ScreenAttrs[off] = 0;
7823 ++off;
7824 }
7825 }
7826 did_delete = TRUE; /* the chars are cleared now */
7827 }
7828
7829 off = LineOffset[row] + start_col;
7830 c = c1;
7831 for (col = start_col; col < end_col; ++col)
7832 {
7833 if (ScreenLines[off] != c
7834#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007835 || (enc_utf8 && (int)ScreenLinesUC[off]
7836 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837#endif
7838 || ScreenAttrs[off] != attr
7839#if defined(FEAT_GUI) || defined(UNIX)
7840 || force_next
7841#endif
7842 )
7843 {
7844#if defined(FEAT_GUI) || defined(UNIX)
7845 /* The bold trick may make a single row of pixels appear in
7846 * the next character. When a bold character is removed, the
7847 * next character should be redrawn too. This happens for our
7848 * own GUI and for some xterms. */
7849 if (
7850# ifdef FEAT_GUI
7851 gui.in_use
7852# endif
7853# if defined(FEAT_GUI) && defined(UNIX)
7854 ||
7855# endif
7856# ifdef UNIX
7857 term_is_xterm
7858# endif
7859 )
7860 {
7861 if (ScreenLines[off] != ' '
7862 && (ScreenAttrs[off] > HL_ALL
7863 || ScreenAttrs[off] & HL_BOLD))
7864 force_next = TRUE;
7865 else
7866 force_next = FALSE;
7867 }
7868#endif
7869 ScreenLines[off] = c;
7870#ifdef FEAT_MBYTE
7871 if (enc_utf8)
7872 {
7873 if (c >= 0x80)
7874 {
7875 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007876 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 }
7878 else
7879 ScreenLinesUC[off] = 0;
7880 }
7881#endif
7882 ScreenAttrs[off] = attr;
7883 if (!did_delete || c != ' ')
7884 screen_char(off, row, col);
7885 }
7886 ++off;
7887 if (col == start_col)
7888 {
7889 if (did_delete)
7890 break;
7891 c = c2;
7892 }
7893 }
7894 if (end_col == Columns)
7895 LineWraps[row] = FALSE;
7896 if (row == Rows - 1) /* overwritten the command line */
7897 {
7898 redraw_cmdline = TRUE;
7899 if (c1 == ' ' && c2 == ' ')
7900 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007901 if (start_col == 0)
7902 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904 }
7905}
7906
7907/*
7908 * Check if there should be a delay. Used before clearing or redrawing the
7909 * screen or the command line.
7910 */
7911 void
7912check_for_delay(check_msg_scroll)
7913 int check_msg_scroll;
7914{
7915 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7916 && !did_wait_return
7917 && emsg_silent == 0)
7918 {
7919 out_flush();
7920 ui_delay(1000L, TRUE);
7921 emsg_on_display = FALSE;
7922 if (check_msg_scroll)
7923 msg_scroll = FALSE;
7924 }
7925}
7926
7927/*
7928 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007929 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 * Returns TRUE if there is a valid screen to write to.
7931 * Returns FALSE when starting up and screen not initialized yet.
7932 */
7933 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007934screen_valid(doclear)
7935 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007937 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 return (ScreenLines != NULL);
7939}
7940
7941/*
7942 * Resize the shell to Rows and Columns.
7943 * Allocate ScreenLines[] and associated items.
7944 *
7945 * There may be some time between setting Rows and Columns and (re)allocating
7946 * ScreenLines[]. This happens when starting up and when (manually) changing
7947 * the shell size. Always use screen_Rows and screen_Columns to access items
7948 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7949 * final size of the shell is needed.
7950 */
7951 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007952screenalloc(doclear)
7953 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 int new_row, old_row;
7956#ifdef FEAT_GUI
7957 int old_Rows;
7958#endif
7959 win_T *wp;
7960 int outofmem = FALSE;
7961 int len;
7962 schar_T *new_ScreenLines;
7963#ifdef FEAT_MBYTE
7964 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007965 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007967 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968#endif
7969 sattr_T *new_ScreenAttrs;
7970 unsigned *new_LineOffset;
7971 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007972#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007973 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007974 tabpage_T *tp;
7975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007977 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007978#ifdef FEAT_AUTOCMD
7979 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007981retry:
7982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 /*
7984 * Allocation of the screen buffers is done only when the size changes and
7985 * when Rows and Columns have been set and we have started doing full
7986 * screen stuff.
7987 */
7988 if ((ScreenLines != NULL
7989 && Rows == screen_Rows
7990 && Columns == screen_Columns
7991#ifdef FEAT_MBYTE
7992 && enc_utf8 == (ScreenLinesUC != NULL)
7993 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007994 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995#endif
7996 )
7997 || Rows == 0
7998 || Columns == 0
7999 || (!full_screen && ScreenLines == NULL))
8000 return;
8001
8002 /*
8003 * It's possible that we produce an out-of-memory message below, which
8004 * will cause this function to be called again. To break the loop, just
8005 * return here.
8006 */
8007 if (entered)
8008 return;
8009 entered = TRUE;
8010
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008011 /*
8012 * Note that the window sizes are updated before reallocating the arrays,
8013 * thus we must not redraw here!
8014 */
8015 ++RedrawingDisabled;
8016
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 win_new_shellsize(); /* fit the windows in the new sized shell */
8018
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 comp_col(); /* recompute columns for shown command and ruler */
8020
8021 /*
8022 * We're changing the size of the screen.
8023 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8024 * - Move lines from the old arrays into the new arrays, clear extra
8025 * lines (unless the screen is going to be cleared).
8026 * - Free the old arrays.
8027 *
8028 * If anything fails, make ScreenLines NULL, so we don't do anything!
8029 * Continuing with the old ScreenLines may result in a crash, because the
8030 * size is wrong.
8031 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008032 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008034#ifdef FEAT_AUTOCMD
8035 if (aucmd_win != NULL)
8036 win_free_lsize(aucmd_win);
8037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038
8039 new_ScreenLines = (schar_T *)lalloc((long_u)(
8040 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8041#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008042 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (enc_utf8)
8044 {
8045 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8046 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008047 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008048 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8050 }
8051 if (enc_dbcs == DBCS_JPNU)
8052 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8053 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8054#endif
8055 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8056 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8057 new_LineOffset = (unsigned *)lalloc((long_u)(
8058 Rows * sizeof(unsigned)), FALSE);
8059 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008060#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008061 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008064 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {
8066 if (win_alloc_lines(wp) == FAIL)
8067 {
8068 outofmem = TRUE;
8069#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008070 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071#endif
8072 }
8073 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008074#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008075 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8076 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008077 outofmem = TRUE;
8078#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008079#ifdef FEAT_WINDOWS
8080give_up:
8081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008083#ifdef FEAT_MBYTE
8084 for (i = 0; i < p_mco; ++i)
8085 if (new_ScreenLinesC[i] == NULL)
8086 break;
8087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 if (new_ScreenLines == NULL
8089#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008090 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8092#endif
8093 || new_ScreenAttrs == NULL
8094 || new_LineOffset == NULL
8095 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008096#ifdef FEAT_WINDOWS
8097 || new_TabPageIdxs == NULL
8098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 || outofmem)
8100 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008101 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008102 {
8103 /* guess the size */
8104 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8105
8106 /* Remember we did this to avoid getting outofmem messages over
8107 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008108 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 vim_free(new_ScreenLines);
8111 new_ScreenLines = NULL;
8112#ifdef FEAT_MBYTE
8113 vim_free(new_ScreenLinesUC);
8114 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008115 for (i = 0; i < p_mco; ++i)
8116 {
8117 vim_free(new_ScreenLinesC[i]);
8118 new_ScreenLinesC[i] = NULL;
8119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 vim_free(new_ScreenLines2);
8121 new_ScreenLines2 = NULL;
8122#endif
8123 vim_free(new_ScreenAttrs);
8124 new_ScreenAttrs = NULL;
8125 vim_free(new_LineOffset);
8126 new_LineOffset = NULL;
8127 vim_free(new_LineWraps);
8128 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008129#ifdef FEAT_WINDOWS
8130 vim_free(new_TabPageIdxs);
8131 new_TabPageIdxs = NULL;
8132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 }
8134 else
8135 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008136 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008137
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 for (new_row = 0; new_row < Rows; ++new_row)
8139 {
8140 new_LineOffset[new_row] = new_row * Columns;
8141 new_LineWraps[new_row] = FALSE;
8142
8143 /*
8144 * If the screen is not going to be cleared, copy as much as
8145 * possible from the old screen to the new one and clear the rest
8146 * (used when resizing the window at the "--more--" prompt or when
8147 * executing an external command, for the GUI).
8148 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008149 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
8151 (void)vim_memset(new_ScreenLines + new_row * Columns,
8152 ' ', (size_t)Columns * sizeof(schar_T));
8153#ifdef FEAT_MBYTE
8154 if (enc_utf8)
8155 {
8156 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8157 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008158 for (i = 0; i < p_mco; ++i)
8159 (void)vim_memset(new_ScreenLinesC[i]
8160 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 0, (size_t)Columns * sizeof(u8char_T));
8162 }
8163 if (enc_dbcs == DBCS_JPNU)
8164 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8165 0, (size_t)Columns * sizeof(schar_T));
8166#endif
8167 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8168 0, (size_t)Columns * sizeof(sattr_T));
8169 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008170 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
8172 if (screen_Columns < Columns)
8173 len = screen_Columns;
8174 else
8175 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008176#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008177 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008178 * may be invalid now. Also when p_mco changes. */
8179 if (!(enc_utf8 && ScreenLinesUC == NULL)
8180 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008181#endif
8182 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8183 ScreenLines + LineOffset[old_row],
8184 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008186 if (enc_utf8 && ScreenLinesUC != NULL
8187 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {
8189 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8190 ScreenLinesUC + LineOffset[old_row],
8191 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008192 for (i = 0; i < p_mco; ++i)
8193 mch_memmove(new_ScreenLinesC[i]
8194 + new_LineOffset[new_row],
8195 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 (size_t)len * sizeof(u8char_T));
8197 }
8198 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8199 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8200 ScreenLines2 + LineOffset[old_row],
8201 (size_t)len * sizeof(schar_T));
8202#endif
8203 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8204 ScreenAttrs + LineOffset[old_row],
8205 (size_t)len * sizeof(sattr_T));
8206 }
8207 }
8208 }
8209 /* Use the last line of the screen for the current line. */
8210 current_ScreenLine = new_ScreenLines + Rows * Columns;
8211 }
8212
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008213 free_screenlines();
8214
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 ScreenLines = new_ScreenLines;
8216#ifdef FEAT_MBYTE
8217 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008218 for (i = 0; i < p_mco; ++i)
8219 ScreenLinesC[i] = new_ScreenLinesC[i];
8220 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 ScreenLines2 = new_ScreenLines2;
8222#endif
8223 ScreenAttrs = new_ScreenAttrs;
8224 LineOffset = new_LineOffset;
8225 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008226#ifdef FEAT_WINDOWS
8227 TabPageIdxs = new_TabPageIdxs;
8228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
8230 /* It's important that screen_Rows and screen_Columns reflect the actual
8231 * size of ScreenLines[]. Set them before calling anything. */
8232#ifdef FEAT_GUI
8233 old_Rows = screen_Rows;
8234#endif
8235 screen_Rows = Rows;
8236 screen_Columns = Columns;
8237
8238 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008239 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 screenclear2();
8241
8242#ifdef FEAT_GUI
8243 else if (gui.in_use
8244 && !gui.starting
8245 && ScreenLines != NULL
8246 && old_Rows != Rows)
8247 {
8248 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8249 /*
8250 * Adjust the position of the cursor, for when executing an external
8251 * command.
8252 */
8253 if (msg_row >= Rows) /* Rows got smaller */
8254 msg_row = Rows - 1; /* put cursor at last row */
8255 else if (Rows > old_Rows) /* Rows got bigger */
8256 msg_row += Rows - old_Rows; /* put cursor in same place */
8257 if (msg_col >= Columns) /* Columns got smaller */
8258 msg_col = Columns - 1; /* put cursor at last column */
8259 }
8260#endif
8261
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008263 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008264
8265#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008266 /*
8267 * Do not apply autocommands more than 3 times to avoid an endless loop
8268 * in case applying autocommands always changes Rows or Columns.
8269 */
8270 if (starting == 0 && ++retry_count <= 3)
8271 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008272 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008273 /* In rare cases, autocommands may have altered Rows or Columns,
8274 * jump back to check if we need to allocate the screen again. */
8275 goto retry;
8276 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278}
8279
8280 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008281free_screenlines()
8282{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008283#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008284 int i;
8285
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008286 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008287 for (i = 0; i < Screen_mco; ++i)
8288 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008289 vim_free(ScreenLines2);
8290#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008291 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008292 vim_free(ScreenAttrs);
8293 vim_free(LineOffset);
8294 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008295#ifdef FEAT_WINDOWS
8296 vim_free(TabPageIdxs);
8297#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008298}
8299
8300 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301screenclear()
8302{
8303 check_for_delay(FALSE);
8304 screenalloc(FALSE); /* allocate screen buffers if size changed */
8305 screenclear2(); /* clear the screen */
8306}
8307
8308 static void
8309screenclear2()
8310{
8311 int i;
8312
8313 if (starting == NO_SCREEN || ScreenLines == NULL
8314#ifdef FEAT_GUI
8315 || (gui.in_use && gui.starting)
8316#endif
8317 )
8318 return;
8319
8320#ifdef FEAT_GUI
8321 if (!gui.in_use)
8322#endif
8323 screen_attr = -1; /* force setting the Normal colors */
8324 screen_stop_highlight(); /* don't want highlighting here */
8325
8326#ifdef FEAT_CLIPBOARD
8327 /* disable selection without redrawing it */
8328 clip_scroll_selection(9999);
8329#endif
8330
8331 /* blank out ScreenLines */
8332 for (i = 0; i < Rows; ++i)
8333 {
8334 lineclear(LineOffset[i], (int)Columns);
8335 LineWraps[i] = FALSE;
8336 }
8337
8338 if (can_clear(T_CL))
8339 {
8340 out_str(T_CL); /* clear the display */
8341 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008342 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 }
8344 else
8345 {
8346 /* can't clear the screen, mark all chars with invalid attributes */
8347 for (i = 0; i < Rows; ++i)
8348 lineinvalid(LineOffset[i], (int)Columns);
8349 clear_cmdline = TRUE;
8350 }
8351
8352 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8353
8354 win_rest_invalid(firstwin);
8355 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008356#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008357 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 if (must_redraw == CLEAR) /* no need to clear again */
8360 must_redraw = NOT_VALID;
8361 compute_cmdrow();
8362 msg_row = cmdline_row; /* put cursor on last line for messages */
8363 msg_col = 0;
8364 screen_start(); /* don't know where cursor is now */
8365 msg_scrolled = 0; /* can't scroll back */
8366 msg_didany = FALSE;
8367 msg_didout = FALSE;
8368}
8369
8370/*
8371 * Clear one line in ScreenLines.
8372 */
8373 static void
8374lineclear(off, width)
8375 unsigned off;
8376 int width;
8377{
8378 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8379#ifdef FEAT_MBYTE
8380 if (enc_utf8)
8381 (void)vim_memset(ScreenLinesUC + off, 0,
8382 (size_t)width * sizeof(u8char_T));
8383#endif
8384 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8385}
8386
8387/*
8388 * Mark one line in ScreenLines invalid by setting the attributes to an
8389 * invalid value.
8390 */
8391 static void
8392lineinvalid(off, width)
8393 unsigned off;
8394 int width;
8395{
8396 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8397}
8398
8399#ifdef FEAT_VERTSPLIT
8400/*
8401 * Copy part of a Screenline for vertically split window "wp".
8402 */
8403 static void
8404linecopy(to, from, wp)
8405 int to;
8406 int from;
8407 win_T *wp;
8408{
8409 unsigned off_to = LineOffset[to] + wp->w_wincol;
8410 unsigned off_from = LineOffset[from] + wp->w_wincol;
8411
8412 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8413 wp->w_width * sizeof(schar_T));
8414# ifdef FEAT_MBYTE
8415 if (enc_utf8)
8416 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008417 int i;
8418
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8420 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008421 for (i = 0; i < p_mco; ++i)
8422 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8423 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 }
8425 if (enc_dbcs == DBCS_JPNU)
8426 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8427 wp->w_width * sizeof(schar_T));
8428# endif
8429 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8430 wp->w_width * sizeof(sattr_T));
8431}
8432#endif
8433
8434/*
8435 * Return TRUE if clearing with term string "p" would work.
8436 * It can't work when the string is empty or it won't set the right background.
8437 */
8438 int
8439can_clear(p)
8440 char_u *p;
8441{
8442 return (*p != NUL && (t_colors <= 1
8443#ifdef FEAT_GUI
8444 || gui.in_use
8445#endif
8446 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8447}
8448
8449/*
8450 * Reset cursor position. Use whenever cursor was moved because of outputting
8451 * something directly to the screen (shell commands) or a terminal control
8452 * code.
8453 */
8454 void
8455screen_start()
8456{
8457 screen_cur_row = screen_cur_col = 9999;
8458}
8459
8460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 * Move the cursor to position "row","col" in the screen.
8462 * This tries to find the most efficient way to move, minimizing the number of
8463 * characters sent to the terminal.
8464 */
8465 void
8466windgoto(row, col)
8467 int row;
8468 int col;
8469{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008470 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 int i;
8472 int plan;
8473 int cost;
8474 int wouldbe_col;
8475 int noinvcurs;
8476 char_u *bs;
8477 int goto_cost;
8478 int attr;
8479
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008480#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8482
8483#define PLAN_LE 1
8484#define PLAN_CR 2
8485#define PLAN_NL 3
8486#define PLAN_WRITE 4
8487 /* Can't use ScreenLines unless initialized */
8488 if (ScreenLines == NULL)
8489 return;
8490
8491 if (col != screen_cur_col || row != screen_cur_row)
8492 {
8493 /* Check for valid position. */
8494 if (row < 0) /* window without text lines? */
8495 row = 0;
8496 if (row >= screen_Rows)
8497 row = screen_Rows - 1;
8498 if (col >= screen_Columns)
8499 col = screen_Columns - 1;
8500
8501 /* check if no cursor movement is allowed in highlight mode */
8502 if (screen_attr && *T_MS == NUL)
8503 noinvcurs = HIGHL_COST;
8504 else
8505 noinvcurs = 0;
8506 goto_cost = GOTO_COST + noinvcurs;
8507
8508 /*
8509 * Plan how to do the positioning:
8510 * 1. Use CR to move it to column 0, same row.
8511 * 2. Use T_LE to move it a few columns to the left.
8512 * 3. Use NL to move a few lines down, column 0.
8513 * 4. Move a few columns to the right with T_ND or by writing chars.
8514 *
8515 * Don't do this if the cursor went beyond the last column, the cursor
8516 * position is unknown then (some terminals wrap, some don't )
8517 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008518 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 * characters to move the cursor to the right.
8520 */
8521 if (row >= screen_cur_row && screen_cur_col < Columns)
8522 {
8523 /*
8524 * If the cursor is in the same row, bigger col, we can use CR
8525 * or T_LE.
8526 */
8527 bs = NULL; /* init for GCC */
8528 attr = screen_attr;
8529 if (row == screen_cur_row && col < screen_cur_col)
8530 {
8531 /* "le" is preferred over "bc", because "bc" is obsolete */
8532 if (*T_LE)
8533 bs = T_LE; /* "cursor left" */
8534 else
8535 bs = T_BC; /* "backspace character (old) */
8536 if (*bs)
8537 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8538 else
8539 cost = 999;
8540 if (col + 1 < cost) /* using CR is less characters */
8541 {
8542 plan = PLAN_CR;
8543 wouldbe_col = 0;
8544 cost = 1; /* CR is just one character */
8545 }
8546 else
8547 {
8548 plan = PLAN_LE;
8549 wouldbe_col = col;
8550 }
8551 if (noinvcurs) /* will stop highlighting */
8552 {
8553 cost += noinvcurs;
8554 attr = 0;
8555 }
8556 }
8557
8558 /*
8559 * If the cursor is above where we want to be, we can use CR LF.
8560 */
8561 else if (row > screen_cur_row)
8562 {
8563 plan = PLAN_NL;
8564 wouldbe_col = 0;
8565 cost = (row - screen_cur_row) * 2; /* CR LF */
8566 if (noinvcurs) /* will stop highlighting */
8567 {
8568 cost += noinvcurs;
8569 attr = 0;
8570 }
8571 }
8572
8573 /*
8574 * If the cursor is in the same row, smaller col, just use write.
8575 */
8576 else
8577 {
8578 plan = PLAN_WRITE;
8579 wouldbe_col = screen_cur_col;
8580 cost = 0;
8581 }
8582
8583 /*
8584 * Check if any characters that need to be written have the
8585 * correct attributes. Also avoid UTF-8 characters.
8586 */
8587 i = col - wouldbe_col;
8588 if (i > 0)
8589 cost += i;
8590 if (cost < goto_cost && i > 0)
8591 {
8592 /*
8593 * Check if the attributes are correct without additionally
8594 * stopping highlighting.
8595 */
8596 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8597 while (i && *p++ == attr)
8598 --i;
8599 if (i != 0)
8600 {
8601 /*
8602 * Try if it works when highlighting is stopped here.
8603 */
8604 if (*--p == 0)
8605 {
8606 cost += noinvcurs;
8607 while (i && *p++ == 0)
8608 --i;
8609 }
8610 if (i != 0)
8611 cost = 999; /* different attributes, don't do it */
8612 }
8613#ifdef FEAT_MBYTE
8614 if (enc_utf8)
8615 {
8616 /* Don't use an UTF-8 char for positioning, it's slow. */
8617 for (i = wouldbe_col; i < col; ++i)
8618 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8619 {
8620 cost = 999;
8621 break;
8622 }
8623 }
8624#endif
8625 }
8626
8627 /*
8628 * We can do it without term_windgoto()!
8629 */
8630 if (cost < goto_cost)
8631 {
8632 if (plan == PLAN_LE)
8633 {
8634 if (noinvcurs)
8635 screen_stop_highlight();
8636 while (screen_cur_col > col)
8637 {
8638 out_str(bs);
8639 --screen_cur_col;
8640 }
8641 }
8642 else if (plan == PLAN_CR)
8643 {
8644 if (noinvcurs)
8645 screen_stop_highlight();
8646 out_char('\r');
8647 screen_cur_col = 0;
8648 }
8649 else if (plan == PLAN_NL)
8650 {
8651 if (noinvcurs)
8652 screen_stop_highlight();
8653 while (screen_cur_row < row)
8654 {
8655 out_char('\n');
8656 ++screen_cur_row;
8657 }
8658 screen_cur_col = 0;
8659 }
8660
8661 i = col - screen_cur_col;
8662 if (i > 0)
8663 {
8664 /*
8665 * Use cursor-right if it's one character only. Avoids
8666 * removing a line of pixels from the last bold char, when
8667 * using the bold trick in the GUI.
8668 */
8669 if (T_ND[0] != NUL && T_ND[1] == NUL)
8670 {
8671 while (i-- > 0)
8672 out_char(*T_ND);
8673 }
8674 else
8675 {
8676 int off;
8677
8678 off = LineOffset[row] + screen_cur_col;
8679 while (i-- > 0)
8680 {
8681 if (ScreenAttrs[off] != screen_attr)
8682 screen_stop_highlight();
8683#ifdef FEAT_MBYTE
8684 out_flush_check();
8685#endif
8686 out_char(ScreenLines[off]);
8687#ifdef FEAT_MBYTE
8688 if (enc_dbcs == DBCS_JPNU
8689 && ScreenLines[off] == 0x8e)
8690 out_char(ScreenLines2[off]);
8691#endif
8692 ++off;
8693 }
8694 }
8695 }
8696 }
8697 }
8698 else
8699 cost = 999;
8700
8701 if (cost >= goto_cost)
8702 {
8703 if (noinvcurs)
8704 screen_stop_highlight();
8705 if (row == screen_cur_row && (col > screen_cur_col) &&
8706 *T_CRI != NUL)
8707 term_cursor_right(col - screen_cur_col);
8708 else
8709 term_windgoto(row, col);
8710 }
8711 screen_cur_row = row;
8712 screen_cur_col = col;
8713 }
8714}
8715
8716/*
8717 * Set cursor to its position in the current window.
8718 */
8719 void
8720setcursor()
8721{
8722 if (redrawing())
8723 {
8724 validate_cursor();
8725 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8726 W_WINCOL(curwin) + (
8727#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008728 /* With 'rightleft' set and the cursor on a double-wide
8729 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8731# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008732 (has_mbyte
8733 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8734 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735# endif
8736 1)) :
8737#endif
8738 curwin->w_wcol));
8739 }
8740}
8741
8742
8743/*
8744 * insert 'line_count' lines at 'row' in window 'wp'
8745 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8746 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8747 * scrolling.
8748 * Returns FAIL if the lines are not inserted, OK for success.
8749 */
8750 int
8751win_ins_lines(wp, row, line_count, invalid, mayclear)
8752 win_T *wp;
8753 int row;
8754 int line_count;
8755 int invalid;
8756 int mayclear;
8757{
8758 int did_delete;
8759 int nextrow;
8760 int lastrow;
8761 int retval;
8762
8763 if (invalid)
8764 wp->w_lines_valid = 0;
8765
8766 if (wp->w_height < 5)
8767 return FAIL;
8768
8769 if (line_count > wp->w_height - row)
8770 line_count = wp->w_height - row;
8771
8772 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8773 if (retval != MAYBE)
8774 return retval;
8775
8776 /*
8777 * If there is a next window or a status line, we first try to delete the
8778 * lines at the bottom to avoid messing what is after the window.
8779 * If this fails and there are following windows, don't do anything to avoid
8780 * messing up those windows, better just redraw.
8781 */
8782 did_delete = FALSE;
8783#ifdef FEAT_WINDOWS
8784 if (wp->w_next != NULL || wp->w_status_height)
8785 {
8786 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8787 line_count, (int)Rows, FALSE, NULL) == OK)
8788 did_delete = TRUE;
8789 else if (wp->w_next)
8790 return FAIL;
8791 }
8792#endif
8793 /*
8794 * if no lines deleted, blank the lines that will end up below the window
8795 */
8796 if (!did_delete)
8797 {
8798#ifdef FEAT_WINDOWS
8799 wp->w_redr_status = TRUE;
8800#endif
8801 redraw_cmdline = TRUE;
8802 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8803 lastrow = nextrow + line_count;
8804 if (lastrow > Rows)
8805 lastrow = Rows;
8806 screen_fill(nextrow - line_count, lastrow - line_count,
8807 W_WINCOL(wp), (int)W_ENDCOL(wp),
8808 ' ', ' ', 0);
8809 }
8810
8811 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8812 == FAIL)
8813 {
8814 /* deletion will have messed up other windows */
8815 if (did_delete)
8816 {
8817#ifdef FEAT_WINDOWS
8818 wp->w_redr_status = TRUE;
8819#endif
8820 win_rest_invalid(W_NEXT(wp));
8821 }
8822 return FAIL;
8823 }
8824
8825 return OK;
8826}
8827
8828/*
8829 * delete "line_count" window lines at "row" in window "wp"
8830 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8831 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8832 * scrolling
8833 * Return OK for success, FAIL if the lines are not deleted.
8834 */
8835 int
8836win_del_lines(wp, row, line_count, invalid, mayclear)
8837 win_T *wp;
8838 int row;
8839 int line_count;
8840 int invalid;
8841 int mayclear;
8842{
8843 int retval;
8844
8845 if (invalid)
8846 wp->w_lines_valid = 0;
8847
8848 if (line_count > wp->w_height - row)
8849 line_count = wp->w_height - row;
8850
8851 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8852 if (retval != MAYBE)
8853 return retval;
8854
8855 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8856 (int)Rows, FALSE, NULL) == FAIL)
8857 return FAIL;
8858
8859#ifdef FEAT_WINDOWS
8860 /*
8861 * If there are windows or status lines below, try to put them at the
8862 * correct place. If we can't do that, they have to be redrawn.
8863 */
8864 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8865 {
8866 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8867 line_count, (int)Rows, NULL) == FAIL)
8868 {
8869 wp->w_redr_status = TRUE;
8870 win_rest_invalid(wp->w_next);
8871 }
8872 }
8873 /*
8874 * If this is the last window and there is no status line, redraw the
8875 * command line later.
8876 */
8877 else
8878#endif
8879 redraw_cmdline = TRUE;
8880 return OK;
8881}
8882
8883/*
8884 * Common code for win_ins_lines() and win_del_lines().
8885 * Returns OK or FAIL when the work has been done.
8886 * Returns MAYBE when not finished yet.
8887 */
8888 static int
8889win_do_lines(wp, row, line_count, mayclear, del)
8890 win_T *wp;
8891 int row;
8892 int line_count;
8893 int mayclear;
8894 int del;
8895{
8896 int retval;
8897
8898 if (!redrawing() || line_count <= 0)
8899 return FAIL;
8900
8901 /* only a few lines left: redraw is faster */
8902 if (mayclear && Rows - line_count < 5
8903#ifdef FEAT_VERTSPLIT
8904 && wp->w_width == Columns
8905#endif
8906 )
8907 {
8908 screenclear(); /* will set wp->w_lines_valid to 0 */
8909 return FAIL;
8910 }
8911
8912 /*
8913 * Delete all remaining lines
8914 */
8915 if (row + line_count >= wp->w_height)
8916 {
8917 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8918 W_WINCOL(wp), (int)W_ENDCOL(wp),
8919 ' ', ' ', 0);
8920 return OK;
8921 }
8922
8923 /*
8924 * when scrolling, the message on the command line should be cleared,
8925 * otherwise it will stay there forever.
8926 */
8927 clear_cmdline = TRUE;
8928
8929 /*
8930 * If the terminal can set a scroll region, use that.
8931 * Always do this in a vertically split window. This will redraw from
8932 * ScreenLines[] when t_CV isn't defined. That's faster than using
8933 * win_line().
8934 * Don't use a scroll region when we are going to redraw the text, writing
8935 * a character in the lower right corner of the scroll region causes a
8936 * scroll-up in the DJGPP version.
8937 */
8938 if (scroll_region
8939#ifdef FEAT_VERTSPLIT
8940 || W_WIDTH(wp) != Columns
8941#endif
8942 )
8943 {
8944#ifdef FEAT_VERTSPLIT
8945 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8946#endif
8947 scroll_region_set(wp, row);
8948 if (del)
8949 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8950 wp->w_height - row, FALSE, wp);
8951 else
8952 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8953 wp->w_height - row, wp);
8954#ifdef FEAT_VERTSPLIT
8955 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8956#endif
8957 scroll_region_reset();
8958 return retval;
8959 }
8960
8961#ifdef FEAT_WINDOWS
8962 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8963 return FAIL;
8964#endif
8965
8966 return MAYBE;
8967}
8968
8969/*
8970 * window 'wp' and everything after it is messed up, mark it for redraw
8971 */
8972 static void
8973win_rest_invalid(wp)
8974 win_T *wp;
8975{
8976#ifdef FEAT_WINDOWS
8977 while (wp != NULL)
8978#else
8979 if (wp != NULL)
8980#endif
8981 {
8982 redraw_win_later(wp, NOT_VALID);
8983#ifdef FEAT_WINDOWS
8984 wp->w_redr_status = TRUE;
8985 wp = wp->w_next;
8986#endif
8987 }
8988 redraw_cmdline = TRUE;
8989}
8990
8991/*
8992 * The rest of the routines in this file perform screen manipulations. The
8993 * given operation is performed physically on the screen. The corresponding
8994 * change is also made to the internal screen image. In this way, the editor
8995 * anticipates the effect of editing changes on the appearance of the screen.
8996 * That way, when we call screenupdate a complete redraw isn't usually
8997 * necessary. Another advantage is that we can keep adding code to anticipate
8998 * screen changes, and in the meantime, everything still works.
8999 */
9000
9001/*
9002 * types for inserting or deleting lines
9003 */
9004#define USE_T_CAL 1
9005#define USE_T_CDL 2
9006#define USE_T_AL 3
9007#define USE_T_CE 4
9008#define USE_T_DL 5
9009#define USE_T_SR 6
9010#define USE_NL 7
9011#define USE_T_CD 8
9012#define USE_REDRAW 9
9013
9014/*
9015 * insert lines on the screen and update ScreenLines[]
9016 * 'end' is the line after the scrolled part. Normally it is Rows.
9017 * When scrolling region used 'off' is the offset from the top for the region.
9018 * 'row' and 'end' are relative to the start of the region.
9019 *
9020 * return FAIL for failure, OK for success.
9021 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009022 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023screen_ins_lines(off, row, line_count, end, wp)
9024 int off;
9025 int row;
9026 int line_count;
9027 int end;
9028 win_T *wp; /* NULL or window to use width from */
9029{
9030 int i;
9031 int j;
9032 unsigned temp;
9033 int cursor_row;
9034 int type;
9035 int result_empty;
9036 int can_ce = can_clear(T_CE);
9037
9038 /*
9039 * FAIL if
9040 * - there is no valid screen
9041 * - the screen has to be redrawn completely
9042 * - the line count is less than one
9043 * - the line count is more than 'ttyscroll'
9044 */
9045 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9046 return FAIL;
9047
9048 /*
9049 * There are seven ways to insert lines:
9050 * 0. When in a vertically split window and t_CV isn't set, redraw the
9051 * characters from ScreenLines[].
9052 * 1. Use T_CD (clear to end of display) if it exists and the result of
9053 * the insert is just empty lines
9054 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9055 * present or line_count > 1. It looks better if we do all the inserts
9056 * at once.
9057 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9058 * insert is just empty lines and T_CE is not present or line_count >
9059 * 1.
9060 * 4. Use T_AL (insert line) if it exists.
9061 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9062 * just empty lines.
9063 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9064 * just empty lines.
9065 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9066 * the 'da' flag is not set or we have clear line capability.
9067 * 8. redraw the characters from ScreenLines[].
9068 *
9069 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9070 * the scrollbar for the window. It does have insert line, use that if it
9071 * exists.
9072 */
9073 result_empty = (row + line_count >= end);
9074#ifdef FEAT_VERTSPLIT
9075 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9076 type = USE_REDRAW;
9077 else
9078#endif
9079 if (can_clear(T_CD) && result_empty)
9080 type = USE_T_CD;
9081 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9082 type = USE_T_CAL;
9083 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9084 type = USE_T_CDL;
9085 else if (*T_AL != NUL)
9086 type = USE_T_AL;
9087 else if (can_ce && result_empty)
9088 type = USE_T_CE;
9089 else if (*T_DL != NUL && result_empty)
9090 type = USE_T_DL;
9091 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9092 type = USE_T_SR;
9093 else
9094 return FAIL;
9095
9096 /*
9097 * For clearing the lines screen_del_lines() is used. This will also take
9098 * care of t_db if necessary.
9099 */
9100 if (type == USE_T_CD || type == USE_T_CDL ||
9101 type == USE_T_CE || type == USE_T_DL)
9102 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9103
9104 /*
9105 * If text is retained below the screen, first clear or delete as many
9106 * lines at the bottom of the window as are about to be inserted so that
9107 * the deleted lines won't later surface during a screen_del_lines.
9108 */
9109 if (*T_DB)
9110 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9111
9112#ifdef FEAT_CLIPBOARD
9113 /* Remove a modeless selection when inserting lines halfway the screen
9114 * or not the full width of the screen. */
9115 if (off + row > 0
9116# ifdef FEAT_VERTSPLIT
9117 || (wp != NULL && wp->w_width != Columns)
9118# endif
9119 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009120 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 else
9122 clip_scroll_selection(-line_count);
9123#endif
9124
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125#ifdef FEAT_GUI
9126 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9127 * scrolling is actually carried out. */
9128 gui_dont_update_cursor();
9129#endif
9130
9131 if (*T_CCS != NUL) /* cursor relative to region */
9132 cursor_row = row;
9133 else
9134 cursor_row = row + off;
9135
9136 /*
9137 * Shift LineOffset[] line_count down to reflect the inserted lines.
9138 * Clear the inserted lines in ScreenLines[].
9139 */
9140 row += off;
9141 end += off;
9142 for (i = 0; i < line_count; ++i)
9143 {
9144#ifdef FEAT_VERTSPLIT
9145 if (wp != NULL && wp->w_width != Columns)
9146 {
9147 /* need to copy part of a line */
9148 j = end - 1 - i;
9149 while ((j -= line_count) >= row)
9150 linecopy(j + line_count, j, wp);
9151 j += line_count;
9152 if (can_clear((char_u *)" "))
9153 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9154 else
9155 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9156 LineWraps[j] = FALSE;
9157 }
9158 else
9159#endif
9160 {
9161 j = end - 1 - i;
9162 temp = LineOffset[j];
9163 while ((j -= line_count) >= row)
9164 {
9165 LineOffset[j + line_count] = LineOffset[j];
9166 LineWraps[j + line_count] = LineWraps[j];
9167 }
9168 LineOffset[j + line_count] = temp;
9169 LineWraps[j + line_count] = FALSE;
9170 if (can_clear((char_u *)" "))
9171 lineclear(temp, (int)Columns);
9172 else
9173 lineinvalid(temp, (int)Columns);
9174 }
9175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176
9177 screen_stop_highlight();
9178 windgoto(cursor_row, 0);
9179
9180#ifdef FEAT_VERTSPLIT
9181 /* redraw the characters */
9182 if (type == USE_REDRAW)
9183 redraw_block(row, end, wp);
9184 else
9185#endif
9186 if (type == USE_T_CAL)
9187 {
9188 term_append_lines(line_count);
9189 screen_start(); /* don't know where cursor is now */
9190 }
9191 else
9192 {
9193 for (i = 0; i < line_count; i++)
9194 {
9195 if (type == USE_T_AL)
9196 {
9197 if (i && cursor_row != 0)
9198 windgoto(cursor_row, 0);
9199 out_str(T_AL);
9200 }
9201 else /* type == USE_T_SR */
9202 out_str(T_SR);
9203 screen_start(); /* don't know where cursor is now */
9204 }
9205 }
9206
9207 /*
9208 * With scroll-reverse and 'da' flag set we need to clear the lines that
9209 * have been scrolled down into the region.
9210 */
9211 if (type == USE_T_SR && *T_DA)
9212 {
9213 for (i = 0; i < line_count; ++i)
9214 {
9215 windgoto(off + i, 0);
9216 out_str(T_CE);
9217 screen_start(); /* don't know where cursor is now */
9218 }
9219 }
9220
9221#ifdef FEAT_GUI
9222 gui_can_update_cursor();
9223 if (gui.in_use)
9224 out_flush(); /* always flush after a scroll */
9225#endif
9226 return OK;
9227}
9228
9229/*
9230 * delete lines on the screen and update ScreenLines[]
9231 * 'end' is the line after the scrolled part. Normally it is Rows.
9232 * When scrolling region used 'off' is the offset from the top for the region.
9233 * 'row' and 'end' are relative to the start of the region.
9234 *
9235 * Return OK for success, FAIL if the lines are not deleted.
9236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 int
9238screen_del_lines(off, row, line_count, end, force, wp)
9239 int off;
9240 int row;
9241 int line_count;
9242 int end;
9243 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009244 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245{
9246 int j;
9247 int i;
9248 unsigned temp;
9249 int cursor_row;
9250 int cursor_end;
9251 int result_empty; /* result is empty until end of region */
9252 int can_delete; /* deleting line codes can be used */
9253 int type;
9254
9255 /*
9256 * FAIL if
9257 * - there is no valid screen
9258 * - the screen has to be redrawn completely
9259 * - the line count is less than one
9260 * - the line count is more than 'ttyscroll'
9261 */
9262 if (!screen_valid(TRUE) || line_count <= 0 ||
9263 (!force && line_count > p_ttyscroll))
9264 return FAIL;
9265
9266 /*
9267 * Check if the rest of the current region will become empty.
9268 */
9269 result_empty = row + line_count >= end;
9270
9271 /*
9272 * We can delete lines only when 'db' flag not set or when 'ce' option
9273 * available.
9274 */
9275 can_delete = (*T_DB == NUL || can_clear(T_CE));
9276
9277 /*
9278 * There are six ways to delete lines:
9279 * 0. When in a vertically split window and t_CV isn't set, redraw the
9280 * characters from ScreenLines[].
9281 * 1. Use T_CD if it exists and the result is empty.
9282 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9283 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9284 * none of the other ways work.
9285 * 4. Use T_CE (erase line) if the result is empty.
9286 * 5. Use T_DL (delete line) if it exists.
9287 * 6. redraw the characters from ScreenLines[].
9288 */
9289#ifdef FEAT_VERTSPLIT
9290 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9291 type = USE_REDRAW;
9292 else
9293#endif
9294 if (can_clear(T_CD) && result_empty)
9295 type = USE_T_CD;
9296#if defined(__BEOS__) && defined(BEOS_DR8)
9297 /*
9298 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9299 * its internal termcap... this works okay for tests which test *T_DB !=
9300 * NUL. It has the disadvantage that the user cannot use any :set t_*
9301 * command to get T_DB (back) to empty_option, only :set term=... will do
9302 * the trick...
9303 * Anyway, this hack will hopefully go away with the next OS release.
9304 * (Olaf Seibert)
9305 */
9306 else if (row == 0 && T_DB == empty_option
9307 && (line_count == 1 || *T_CDL == NUL))
9308#else
9309 else if (row == 0 && (
9310#ifndef AMIGA
9311 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9312 * up, so use delete-line command */
9313 line_count == 1 ||
9314#endif
9315 *T_CDL == NUL))
9316#endif
9317 type = USE_NL;
9318 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9319 type = USE_T_CDL;
9320 else if (can_clear(T_CE) && result_empty
9321#ifdef FEAT_VERTSPLIT
9322 && (wp == NULL || wp->w_width == Columns)
9323#endif
9324 )
9325 type = USE_T_CE;
9326 else if (*T_DL != NUL && can_delete)
9327 type = USE_T_DL;
9328 else if (*T_CDL != NUL && can_delete)
9329 type = USE_T_CDL;
9330 else
9331 return FAIL;
9332
9333#ifdef FEAT_CLIPBOARD
9334 /* Remove a modeless selection when deleting lines halfway the screen or
9335 * not the full width of the screen. */
9336 if (off + row > 0
9337# ifdef FEAT_VERTSPLIT
9338 || (wp != NULL && wp->w_width != Columns)
9339# endif
9340 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009341 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 else
9343 clip_scroll_selection(line_count);
9344#endif
9345
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346#ifdef FEAT_GUI
9347 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9348 * scrolling is actually carried out. */
9349 gui_dont_update_cursor();
9350#endif
9351
9352 if (*T_CCS != NUL) /* cursor relative to region */
9353 {
9354 cursor_row = row;
9355 cursor_end = end;
9356 }
9357 else
9358 {
9359 cursor_row = row + off;
9360 cursor_end = end + off;
9361 }
9362
9363 /*
9364 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9365 * Clear the inserted lines in ScreenLines[].
9366 */
9367 row += off;
9368 end += off;
9369 for (i = 0; i < line_count; ++i)
9370 {
9371#ifdef FEAT_VERTSPLIT
9372 if (wp != NULL && wp->w_width != Columns)
9373 {
9374 /* need to copy part of a line */
9375 j = row + i;
9376 while ((j += line_count) <= end - 1)
9377 linecopy(j - line_count, j, wp);
9378 j -= line_count;
9379 if (can_clear((char_u *)" "))
9380 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9381 else
9382 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9383 LineWraps[j] = FALSE;
9384 }
9385 else
9386#endif
9387 {
9388 /* whole width, moving the line pointers is faster */
9389 j = row + i;
9390 temp = LineOffset[j];
9391 while ((j += line_count) <= end - 1)
9392 {
9393 LineOffset[j - line_count] = LineOffset[j];
9394 LineWraps[j - line_count] = LineWraps[j];
9395 }
9396 LineOffset[j - line_count] = temp;
9397 LineWraps[j - line_count] = FALSE;
9398 if (can_clear((char_u *)" "))
9399 lineclear(temp, (int)Columns);
9400 else
9401 lineinvalid(temp, (int)Columns);
9402 }
9403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404
9405 screen_stop_highlight();
9406
9407#ifdef FEAT_VERTSPLIT
9408 /* redraw the characters */
9409 if (type == USE_REDRAW)
9410 redraw_block(row, end, wp);
9411 else
9412#endif
9413 if (type == USE_T_CD) /* delete the lines */
9414 {
9415 windgoto(cursor_row, 0);
9416 out_str(T_CD);
9417 screen_start(); /* don't know where cursor is now */
9418 }
9419 else if (type == USE_T_CDL)
9420 {
9421 windgoto(cursor_row, 0);
9422 term_delete_lines(line_count);
9423 screen_start(); /* don't know where cursor is now */
9424 }
9425 /*
9426 * Deleting lines at top of the screen or scroll region: Just scroll
9427 * the whole screen (scroll region) up by outputting newlines on the
9428 * last line.
9429 */
9430 else if (type == USE_NL)
9431 {
9432 windgoto(cursor_end - 1, 0);
9433 for (i = line_count; --i >= 0; )
9434 out_char('\n'); /* cursor will remain on same line */
9435 }
9436 else
9437 {
9438 for (i = line_count; --i >= 0; )
9439 {
9440 if (type == USE_T_DL)
9441 {
9442 windgoto(cursor_row, 0);
9443 out_str(T_DL); /* delete a line */
9444 }
9445 else /* type == USE_T_CE */
9446 {
9447 windgoto(cursor_row + i, 0);
9448 out_str(T_CE); /* erase a line */
9449 }
9450 screen_start(); /* don't know where cursor is now */
9451 }
9452 }
9453
9454 /*
9455 * If the 'db' flag is set, we need to clear the lines that have been
9456 * scrolled up at the bottom of the region.
9457 */
9458 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9459 {
9460 for (i = line_count; i > 0; --i)
9461 {
9462 windgoto(cursor_end - i, 0);
9463 out_str(T_CE); /* erase a line */
9464 screen_start(); /* don't know where cursor is now */
9465 }
9466 }
9467
9468#ifdef FEAT_GUI
9469 gui_can_update_cursor();
9470 if (gui.in_use)
9471 out_flush(); /* always flush after a scroll */
9472#endif
9473
9474 return OK;
9475}
9476
9477/*
9478 * show the current mode and ruler
9479 *
9480 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9481 * If clear_cmdline is FALSE there may be a message there that needs to be
9482 * cleared only if a mode is shown.
9483 * Return the length of the message (0 if no message).
9484 */
9485 int
9486showmode()
9487{
9488 int need_clear;
9489 int length = 0;
9490 int do_mode;
9491 int attr;
9492 int nwr_save;
9493#ifdef FEAT_INS_EXPAND
9494 int sub_attr;
9495#endif
9496
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009497 do_mode = ((p_smd && msg_silent == 0)
9498 && ((State & INSERT)
9499 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500#ifdef FEAT_VISUAL
9501 || VIsual_active
9502#endif
9503 ));
9504 if (do_mode || Recording)
9505 {
9506 /*
9507 * Don't show mode right now, when not redrawing or inside a mapping.
9508 * Call char_avail() only when we are going to show something, because
9509 * it takes a bit of time.
9510 */
9511 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9512 {
9513 redraw_cmdline = TRUE; /* show mode later */
9514 return 0;
9515 }
9516
9517 nwr_save = need_wait_return;
9518
9519 /* wait a bit before overwriting an important message */
9520 check_for_delay(FALSE);
9521
9522 /* if the cmdline is more than one line high, erase top lines */
9523 need_clear = clear_cmdline;
9524 if (clear_cmdline && cmdline_row < Rows - 1)
9525 msg_clr_cmdline(); /* will reset clear_cmdline */
9526
9527 /* Position on the last line in the window, column 0 */
9528 msg_pos_mode();
9529 cursor_off();
9530 attr = hl_attr(HLF_CM); /* Highlight mode */
9531 if (do_mode)
9532 {
9533 MSG_PUTS_ATTR("--", attr);
9534#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009535 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009536# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009537 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009538# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009539 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009540# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009541 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009542# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 MSG_PUTS_ATTR(" IM", attr);
9544# else
9545 MSG_PUTS_ATTR(" XIM", attr);
9546# endif
9547#endif
9548#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9549 if (gui.in_use)
9550 {
9551 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009552 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 }
9554#endif
9555#ifdef FEAT_INS_EXPAND
9556 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9557 {
9558 /* These messages can get long, avoid a wrap in a narrow
9559 * window. Prefer showing edit_submode_extra. */
9560 length = (Rows - msg_row) * Columns - 3;
9561 if (edit_submode_extra != NULL)
9562 length -= vim_strsize(edit_submode_extra);
9563 if (length > 0)
9564 {
9565 if (edit_submode_pre != NULL)
9566 length -= vim_strsize(edit_submode_pre);
9567 if (length - vim_strsize(edit_submode) > 0)
9568 {
9569 if (edit_submode_pre != NULL)
9570 msg_puts_attr(edit_submode_pre, attr);
9571 msg_puts_attr(edit_submode, attr);
9572 }
9573 if (edit_submode_extra != NULL)
9574 {
9575 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9576 if ((int)edit_submode_highl < (int)HLF_COUNT)
9577 sub_attr = hl_attr(edit_submode_highl);
9578 else
9579 sub_attr = attr;
9580 msg_puts_attr(edit_submode_extra, sub_attr);
9581 }
9582 }
9583 length = 0;
9584 }
9585 else
9586#endif
9587 {
9588#ifdef FEAT_VREPLACE
9589 if (State & VREPLACE_FLAG)
9590 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9591 else
9592#endif
9593 if (State & REPLACE_FLAG)
9594 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9595 else if (State & INSERT)
9596 {
9597#ifdef FEAT_RIGHTLEFT
9598 if (p_ri)
9599 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9600#endif
9601 MSG_PUTS_ATTR(_(" INSERT"), attr);
9602 }
9603 else if (restart_edit == 'I')
9604 MSG_PUTS_ATTR(_(" (insert)"), attr);
9605 else if (restart_edit == 'R')
9606 MSG_PUTS_ATTR(_(" (replace)"), attr);
9607 else if (restart_edit == 'V')
9608 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9609#ifdef FEAT_RIGHTLEFT
9610 if (p_hkmap)
9611 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9612# ifdef FEAT_FKMAP
9613 if (p_fkmap)
9614 MSG_PUTS_ATTR(farsi_text_5, attr);
9615# endif
9616#endif
9617#ifdef FEAT_KEYMAP
9618 if (State & LANGMAP)
9619 {
9620# ifdef FEAT_ARABIC
9621 if (curwin->w_p_arab)
9622 MSG_PUTS_ATTR(_(" Arabic"), attr);
9623 else
9624# endif
9625 MSG_PUTS_ATTR(_(" (lang)"), attr);
9626 }
9627#endif
9628 if ((State & INSERT) && p_paste)
9629 MSG_PUTS_ATTR(_(" (paste)"), attr);
9630
9631#ifdef FEAT_VISUAL
9632 if (VIsual_active)
9633 {
9634 char *p;
9635
9636 /* Don't concatenate separate words to avoid translation
9637 * problems. */
9638 switch ((VIsual_select ? 4 : 0)
9639 + (VIsual_mode == Ctrl_V) * 2
9640 + (VIsual_mode == 'V'))
9641 {
9642 case 0: p = N_(" VISUAL"); break;
9643 case 1: p = N_(" VISUAL LINE"); break;
9644 case 2: p = N_(" VISUAL BLOCK"); break;
9645 case 4: p = N_(" SELECT"); break;
9646 case 5: p = N_(" SELECT LINE"); break;
9647 default: p = N_(" SELECT BLOCK"); break;
9648 }
9649 MSG_PUTS_ATTR(_(p), attr);
9650 }
9651#endif
9652 MSG_PUTS_ATTR(" --", attr);
9653 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009654
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 need_clear = TRUE;
9656 }
9657 if (Recording
9658#ifdef FEAT_INS_EXPAND
9659 && edit_submode == NULL /* otherwise it gets too long */
9660#endif
9661 )
9662 {
9663 MSG_PUTS_ATTR(_("recording"), attr);
9664 need_clear = TRUE;
9665 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009666
9667 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 if (need_clear || clear_cmdline)
9669 msg_clr_eos();
9670 msg_didout = FALSE; /* overwrite this message */
9671 length = msg_col;
9672 msg_col = 0;
9673 need_wait_return = nwr_save; /* never ask for hit-return for this */
9674 }
9675 else if (clear_cmdline && msg_silent == 0)
9676 /* Clear the whole command line. Will reset "clear_cmdline". */
9677 msg_clr_cmdline();
9678
9679#ifdef FEAT_CMDL_INFO
9680# ifdef FEAT_VISUAL
9681 /* In Visual mode the size of the selected area must be redrawn. */
9682 if (VIsual_active)
9683 clear_showcmd();
9684# endif
9685
9686 /* If the last window has no status line, the ruler is after the mode
9687 * message and must be redrawn */
9688 if (redrawing()
9689# ifdef FEAT_WINDOWS
9690 && lastwin->w_status_height == 0
9691# endif
9692 )
9693 win_redr_ruler(lastwin, TRUE);
9694#endif
9695 redraw_cmdline = FALSE;
9696 clear_cmdline = FALSE;
9697
9698 return length;
9699}
9700
9701/*
9702 * Position for a mode message.
9703 */
9704 static void
9705msg_pos_mode()
9706{
9707 msg_col = 0;
9708 msg_row = Rows - 1;
9709}
9710
9711/*
9712 * Delete mode message. Used when ESC is typed which is expected to end
9713 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009714 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 */
9716 void
9717unshowmode(force)
9718 int force;
9719{
9720 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009721 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 */
9723 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9724 redraw_cmdline = TRUE; /* delete mode later */
9725 else
9726 {
9727 msg_pos_mode();
9728 if (Recording)
9729 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9730 msg_clr_eos();
9731 }
9732}
9733
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009734#if defined(FEAT_WINDOWS)
9735/*
9736 * Draw the tab pages line at the top of the Vim window.
9737 */
9738 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009739draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009740{
9741 int tabcount = 0;
9742 tabpage_T *tp;
9743 int tabwidth;
9744 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009745 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009746 int attr;
9747 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009748 win_T *cwp;
9749 int wincount;
9750 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009751 int c;
9752 int len;
9753 int attr_sel = hl_attr(HLF_TPS);
9754 int attr_nosel = hl_attr(HLF_TP);
9755 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009756 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009757 int room;
9758 int use_sep_chars = (t_colors < 8
9759#ifdef FEAT_GUI
9760 && !gui.in_use
9761#endif
9762 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009763
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009764 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009765
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009766#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009767 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009768 if (gui_use_tabline())
9769 {
9770 gui_update_tabline();
9771 return;
9772 }
9773#endif
9774
9775 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009776 return;
9777
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009778#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009779
9780 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9781 for (scol = 0; scol < Columns; ++scol)
9782 TabPageIdxs[scol] = 0;
9783
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009784 /* Use the 'tabline' option if it's set. */
9785 if (*p_tal != NUL)
9786 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009787 int save_called_emsg = called_emsg;
9788
9789 /* Check for an error. If there is one we would loop in redrawing the
9790 * screen. Avoid that by making 'tabline' empty. */
9791 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009792 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009793 if (called_emsg)
9794 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009795 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009796 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009797 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009798 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009799#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009800 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009801 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9802 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009803
Bram Moolenaar238a5642006-02-21 22:12:05 +00009804 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9805 if (tabwidth < 6)
9806 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009807
Bram Moolenaar238a5642006-02-21 22:12:05 +00009808 attr = attr_nosel;
9809 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009810 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009811 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9812 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009813 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009814 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009815
Bram Moolenaar238a5642006-02-21 22:12:05 +00009816 if (tp->tp_topframe == topframe)
9817 attr = attr_sel;
9818 if (use_sep_chars && col > 0)
9819 screen_putchar('|', 0, col++, attr);
9820
9821 if (tp->tp_topframe != topframe)
9822 attr = attr_nosel;
9823
9824 screen_putchar(' ', 0, col++, attr);
9825
9826 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009827 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009828 cwp = curwin;
9829 wp = firstwin;
9830 }
9831 else
9832 {
9833 cwp = tp->tp_curwin;
9834 wp = tp->tp_firstwin;
9835 }
9836
9837 modified = FALSE;
9838 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9839 if (bufIsChanged(wp->w_buffer))
9840 modified = TRUE;
9841 if (modified || wincount > 1)
9842 {
9843 if (wincount > 1)
9844 {
9845 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009846 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009847 if (col + len >= Columns - 3)
9848 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009849 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009850#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009851 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009852#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009853 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009854#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009855 );
9856 col += len;
9857 }
9858 if (modified)
9859 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9860 screen_putchar(' ', 0, col++, attr);
9861 }
9862
9863 room = scol - col + tabwidth - 1;
9864 if (room > 0)
9865 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009866 /* Get buffer name in NameBuff[] */
9867 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009868 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009869 len = vim_strsize(NameBuff);
9870 p = NameBuff;
9871#ifdef FEAT_MBYTE
9872 if (has_mbyte)
9873 while (len > room)
9874 {
9875 len -= ptr2cells(p);
9876 mb_ptr_adv(p);
9877 }
9878 else
9879#endif
9880 if (len > room)
9881 {
9882 p += len - room;
9883 len = room;
9884 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009885 if (len > Columns - col - 1)
9886 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009887
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009888 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009889 col += len;
9890 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009891 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009892
9893 /* Store the tab page number in TabPageIdxs[], so that
9894 * jump_to_mouse() knows where each one is. */
9895 ++tabcount;
9896 while (scol < col)
9897 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009898 }
9899
Bram Moolenaar238a5642006-02-21 22:12:05 +00009900 if (use_sep_chars)
9901 c = '_';
9902 else
9903 c = ' ';
9904 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009905
9906 /* Put an "X" for closing the current tab if there are several. */
9907 if (first_tabpage->tp_next != NULL)
9908 {
9909 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9910 TabPageIdxs[Columns - 1] = -999;
9911 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009912 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009913
9914 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9915 * set. */
9916 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009917}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009918
9919/*
9920 * Get buffer name for "buf" into NameBuff[].
9921 * Takes care of special buffer names and translates special characters.
9922 */
9923 void
9924get_trans_bufname(buf)
9925 buf_T *buf;
9926{
9927 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02009928 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009929 else
9930 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9931 trans_characters(NameBuff, MAXPATHL);
9932}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009933#endif
9934
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9936/*
9937 * Get the character to use in a status line. Get its attributes in "*attr".
9938 */
9939 static int
9940fillchar_status(attr, is_curwin)
9941 int *attr;
9942 int is_curwin;
9943{
9944 int fill;
9945 if (is_curwin)
9946 {
9947 *attr = hl_attr(HLF_S);
9948 fill = fill_stl;
9949 }
9950 else
9951 {
9952 *attr = hl_attr(HLF_SNC);
9953 fill = fill_stlnc;
9954 }
9955 /* Use fill when there is highlighting, and highlighting of current
9956 * window differs, or the fillchars differ, or this is not the
9957 * current window */
9958 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9959 || !is_curwin || firstwin == lastwin)
9960 || (fill_stl != fill_stlnc)))
9961 return fill;
9962 if (is_curwin)
9963 return '^';
9964 return '=';
9965}
9966#endif
9967
9968#ifdef FEAT_VERTSPLIT
9969/*
9970 * Get the character to use in a separator between vertically split windows.
9971 * Get its attributes in "*attr".
9972 */
9973 static int
9974fillchar_vsep(attr)
9975 int *attr;
9976{
9977 *attr = hl_attr(HLF_C);
9978 if (*attr == 0 && fill_vert == ' ')
9979 return '|';
9980 else
9981 return fill_vert;
9982}
9983#endif
9984
9985/*
9986 * Return TRUE if redrawing should currently be done.
9987 */
9988 int
9989redrawing()
9990{
9991 return (!RedrawingDisabled
9992 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9993}
9994
9995/*
9996 * Return TRUE if printing messages should currently be done.
9997 */
9998 int
9999messaging()
10000{
10001 return (!(p_lz && char_avail() && !KeyTyped));
10002}
10003
10004/*
10005 * Show current status info in ruler and various other places
10006 * If always is FALSE, only show ruler if position has changed.
10007 */
10008 void
10009showruler(always)
10010 int always;
10011{
10012 if (!always && !redrawing())
10013 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010014#ifdef FEAT_INS_EXPAND
10015 if (pum_visible())
10016 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010017# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010018 /* Don't redraw right now, do it later. */
10019 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010020# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010021 return;
10022 }
10023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010025 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010026 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010027 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 else
10030#endif
10031#ifdef FEAT_CMDL_INFO
10032 win_redr_ruler(curwin, always);
10033#endif
10034
10035#ifdef FEAT_TITLE
10036 if (need_maketitle
10037# ifdef FEAT_STL_OPT
10038 || (p_icon && (stl_syntax & STL_IN_ICON))
10039 || (p_title && (stl_syntax & STL_IN_TITLE))
10040# endif
10041 )
10042 maketitle();
10043#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010044#ifdef FEAT_WINDOWS
10045 /* Redraw the tab pages line if needed. */
10046 if (redraw_tabline)
10047 draw_tabline();
10048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049}
10050
10051#ifdef FEAT_CMDL_INFO
10052 static void
10053win_redr_ruler(wp, always)
10054 win_T *wp;
10055 int always;
10056{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010057#define RULER_BUF_LEN 70
10058 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 int row;
10060 int fillchar;
10061 int attr;
10062 int empty_line = FALSE;
10063 colnr_T virtcol;
10064 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010065 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 int o;
10067#ifdef FEAT_VERTSPLIT
10068 int this_ru_col;
10069 int off = 0;
10070 int width = Columns;
10071# define WITH_OFF(x) x
10072# define WITH_WIDTH(x) x
10073#else
10074# define WITH_OFF(x) 0
10075# define WITH_WIDTH(x) Columns
10076# define this_ru_col ru_col
10077#endif
10078
10079 /* If 'ruler' off or redrawing disabled, don't do anything */
10080 if (!p_ru)
10081 return;
10082
10083 /*
10084 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10085 * after deleting lines, before cursor.lnum is corrected.
10086 */
10087 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10088 return;
10089
10090#ifdef FEAT_INS_EXPAND
10091 /* Don't draw the ruler while doing insert-completion, it might overwrite
10092 * the (long) mode message. */
10093# ifdef FEAT_WINDOWS
10094 if (wp == lastwin && lastwin->w_status_height == 0)
10095# endif
10096 if (edit_submode != NULL)
10097 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010098 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10099 if (pum_visible())
10100 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101#endif
10102
10103#ifdef FEAT_STL_OPT
10104 if (*p_ruf)
10105 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010106 int save_called_emsg = called_emsg;
10107
10108 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010110 if (called_emsg)
10111 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010112 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010113 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 return;
10115 }
10116#endif
10117
10118 /*
10119 * Check if not in Insert mode and the line is empty (will show "0-1").
10120 */
10121 if (!(State & INSERT)
10122 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10123 empty_line = TRUE;
10124
10125 /*
10126 * Only draw the ruler when something changed.
10127 */
10128 validate_virtcol_win(wp);
10129 if ( redraw_cmdline
10130 || always
10131 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10132 || wp->w_cursor.col != wp->w_ru_cursor.col
10133 || wp->w_virtcol != wp->w_ru_virtcol
10134#ifdef FEAT_VIRTUALEDIT
10135 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10136#endif
10137 || wp->w_topline != wp->w_ru_topline
10138 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10139#ifdef FEAT_DIFF
10140 || wp->w_topfill != wp->w_ru_topfill
10141#endif
10142 || empty_line != wp->w_ru_empty)
10143 {
10144 cursor_off();
10145#ifdef FEAT_WINDOWS
10146 if (wp->w_status_height)
10147 {
10148 row = W_WINROW(wp) + wp->w_height;
10149 fillchar = fillchar_status(&attr, wp == curwin);
10150# ifdef FEAT_VERTSPLIT
10151 off = W_WINCOL(wp);
10152 width = W_WIDTH(wp);
10153# endif
10154 }
10155 else
10156#endif
10157 {
10158 row = Rows - 1;
10159 fillchar = ' ';
10160 attr = 0;
10161#ifdef FEAT_VERTSPLIT
10162 width = Columns;
10163 off = 0;
10164#endif
10165 }
10166
10167 /* In list mode virtcol needs to be recomputed */
10168 virtcol = wp->w_virtcol;
10169 if (wp->w_p_list && lcs_tab1 == NUL)
10170 {
10171 wp->w_p_list = FALSE;
10172 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10173 wp->w_p_list = TRUE;
10174 }
10175
10176 /*
10177 * Some sprintfs return the length, some return a pointer.
10178 * To avoid portability problems we use strlen() here.
10179 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010180 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10182 ? 0L
10183 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010184 len = STRLEN(buffer);
10185 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10187 (int)virtcol + 1);
10188
10189 /*
10190 * Add a "50%" if there is room for it.
10191 * On the last line, don't print in the last column (scrolls the
10192 * screen up on some terminals).
10193 */
10194 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010195 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 o = i + vim_strsize(buffer + i + 1);
10197#ifdef FEAT_WINDOWS
10198 if (wp->w_status_height == 0) /* can't use last char of screen */
10199#endif
10200 ++o;
10201#ifdef FEAT_VERTSPLIT
10202 this_ru_col = ru_col - (Columns - width);
10203 if (this_ru_col < 0)
10204 this_ru_col = 0;
10205#endif
10206 /* Never use more than half the window/screen width, leave the other
10207 * half for the filename. */
10208 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10209 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10210 if (this_ru_col + o < WITH_WIDTH(width))
10211 {
10212 while (this_ru_col + o < WITH_WIDTH(width))
10213 {
10214#ifdef FEAT_MBYTE
10215 if (has_mbyte)
10216 i += (*mb_char2bytes)(fillchar, buffer + i);
10217 else
10218#endif
10219 buffer[i++] = fillchar;
10220 ++o;
10221 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010222 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 }
10224 /* Truncate at window boundary. */
10225#ifdef FEAT_MBYTE
10226 if (has_mbyte)
10227 {
10228 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010229 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 {
10231 o += (*mb_ptr2cells)(buffer + i);
10232 if (this_ru_col + o > WITH_WIDTH(width))
10233 {
10234 buffer[i] = NUL;
10235 break;
10236 }
10237 }
10238 }
10239 else
10240#endif
10241 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10242 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10243
10244 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10245 i = redraw_cmdline;
10246 screen_fill(row, row + 1,
10247 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10248 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10249 fillchar, fillchar, attr);
10250 /* don't redraw the cmdline because of showing the ruler */
10251 redraw_cmdline = i;
10252 wp->w_ru_cursor = wp->w_cursor;
10253 wp->w_ru_virtcol = wp->w_virtcol;
10254 wp->w_ru_empty = empty_line;
10255 wp->w_ru_topline = wp->w_topline;
10256 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10257#ifdef FEAT_DIFF
10258 wp->w_ru_topfill = wp->w_topfill;
10259#endif
10260 }
10261}
10262#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010263
10264#if defined(FEAT_LINEBREAK) || defined(PROTO)
10265/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010266 * Return the width of the 'number' and 'relativenumber' column.
10267 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010268 * Otherwise it depends on 'numberwidth' and the line count.
10269 */
10270 int
10271number_width(wp)
10272 win_T *wp;
10273{
10274 int n;
10275 linenr_T lnum;
10276
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010277 if (wp->w_p_rnu && !wp->w_p_nu)
10278 /* cursor line shows "0" */
10279 lnum = wp->w_height;
10280 else
10281 /* cursor line shows absolute line number */
10282 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010283
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010284 if (lnum == wp->w_nrwidth_line_count)
10285 return wp->w_nrwidth_width;
10286 wp->w_nrwidth_line_count = lnum;
10287
10288 n = 0;
10289 do
10290 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010291 lnum /= 10;
10292 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010293 } while (lnum > 0);
10294
10295 /* 'numberwidth' gives the minimal width plus one */
10296 if (n < wp->w_p_nuw - 1)
10297 n = wp->w_p_nuw - 1;
10298
10299 wp->w_nrwidth_width = n;
10300 return n;
10301}
10302#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010303
10304/*
10305 * Return the current cursor column. This is the actual position on the
10306 * screen. First column is 0.
10307 */
10308 int
10309screen_screencol()
10310{
10311 return screen_cur_col;
10312}
10313
10314/*
10315 * Return the current cursor row. This is the actual position on the screen.
10316 * First row is 0.
10317 */
10318 int
10319screen_screenrow()
10320{
10321 return screen_cur_row;
10322}