blob: 64d06ebe08fb63777e8869ac21fec938037ea8e9 [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. */
522 if (clip_star.available && clip_isautosel())
523 clip_update_selection();
524# endif
525#ifdef FEAT_GUI
526 /* Remove the cursor before starting to do anything, because
527 * scrolling may make it difficult to redraw the text under
528 * it. */
529 if (gui.in_use)
530 gui_undraw_cursor();
531#endif
532 }
533#endif
534 win_update(wp);
535 }
536
537#ifdef FEAT_WINDOWS
538 /* redraw status line after the window to minimize cursor movement */
539 if (wp->w_redr_status)
540 {
541 cursor_off();
542 win_redr_status(wp);
543 }
544#endif
545 }
546#if defined(FEAT_SEARCH_EXTRA)
547 end_search_hl();
548#endif
549
550#ifdef FEAT_WINDOWS
551 /* Reset b_mod_set flags. Going through all windows is probably faster
552 * than going through all buffers (there could be many buffers). */
553 for (wp = firstwin; wp != NULL; wp = wp->w_next)
554 wp->w_buffer->b_mod_set = FALSE;
555#else
556 curbuf->b_mod_set = FALSE;
557#endif
558
559 updating_screen = FALSE;
560#ifdef FEAT_GUI
561 gui_may_resize_shell();
562#endif
563
564 /* Clear or redraw the command line. Done last, because scrolling may
565 * mess up the command line. */
566 if (clear_cmdline || redraw_cmdline)
567 showmode();
568
569 /* May put up an introductory message when not editing a file */
570 if (!did_intro && bufempty()
571 && curbuf->b_fname == NULL
572#ifdef FEAT_WINDOWS
573 && firstwin->w_next == NULL
574#endif
575 && vim_strchr(p_shm, SHM_INTRO) == NULL)
576 intro_message(FALSE);
577 did_intro = TRUE;
578
579#ifdef FEAT_GUI
580 /* Redraw the cursor and update the scrollbars when all screen updating is
581 * done. */
582 if (gui.in_use)
583 {
584 out_flush(); /* required before updating the cursor */
585 if (did_one)
586 gui_update_cursor(FALSE, FALSE);
587 gui_update_scrollbars(FALSE);
588 }
589#endif
590}
591
Bram Moolenaar860cae12010-06-05 23:22:07 +0200592#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200593/*
594 * Return TRUE if the cursor line in window "wp" may be concealed, according
595 * to the 'concealcursor' option.
596 */
597 int
598conceal_cursor_line(wp)
599 win_T *wp;
600{
601 int c;
602
603 if (*wp->w_p_cocu == NUL)
604 return FALSE;
605 if (get_real_state() & VISUAL)
606 c = 'v';
607 else if (State & INSERT)
608 c = 'i';
609 else if (State & NORMAL)
610 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200611 else if (State & CMDLINE)
612 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200613 else
614 return FALSE;
615 return vim_strchr(wp->w_p_cocu, c) != NULL;
616}
617
618/*
619 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
620 */
621 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200622conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200623{
624 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
625 {
626 need_cursor_line_redraw = TRUE;
627 /* Need to recompute cursor column, e.g., when starting Visual mode
628 * without concealing. */
629 curs_columns(TRUE);
630 }
631}
632
Bram Moolenaar860cae12010-06-05 23:22:07 +0200633 void
634update_single_line(wp, lnum)
635 win_T *wp;
636 linenr_T lnum;
637{
638 int row;
639 int j;
640
641 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200642 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200643 {
644# ifdef FEAT_GUI
645 /* Remove the cursor before starting to do anything, because scrolling
646 * may make it difficult to redraw the text under it. */
647 if (gui.in_use)
648 gui_undraw_cursor();
649# endif
650 row = 0;
651 for (j = 0; j < wp->w_lines_valid; ++j)
652 {
653 if (lnum == wp->w_lines[j].wl_lnum)
654 {
655 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200656# ifdef FEAT_SEARCH_EXTRA
657 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200658 start_search_hl();
659 prepare_search_hl(wp, lnum);
660# endif
661 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
662# if defined(FEAT_SEARCH_EXTRA)
663 end_search_hl();
664# endif
665 break;
666 }
667 row += wp->w_lines[j].wl_size;
668 }
669# ifdef FEAT_GUI
670 /* Redraw the cursor */
671 if (gui.in_use)
672 {
673 out_flush(); /* required before updating the cursor */
674 gui_update_cursor(FALSE, FALSE);
675 }
676# endif
677 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200678 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200679}
680#endif
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
683static void update_prepare __ARGS((void));
684static void update_finish __ARGS((void));
685
686/*
687 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000688 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 */
690 static void
691update_prepare()
692{
693 cursor_off();
694 updating_screen = TRUE;
695#ifdef FEAT_GUI
696 /* Remove the cursor before starting to do anything, because scrolling may
697 * make it difficult to redraw the text under it. */
698 if (gui.in_use)
699 gui_undraw_cursor();
700#endif
701#ifdef FEAT_SEARCH_EXTRA
702 start_search_hl();
703#endif
704}
705
706/*
707 * Finish updating one or more windows.
708 */
709 static void
710update_finish()
711{
712 if (redraw_cmdline)
713 showmode();
714
715# ifdef FEAT_SEARCH_EXTRA
716 end_search_hl();
717# endif
718
719 updating_screen = FALSE;
720
721# ifdef FEAT_GUI
722 gui_may_resize_shell();
723
724 /* Redraw the cursor and update the scrollbars when all screen updating is
725 * done. */
726 if (gui.in_use)
727 {
728 out_flush(); /* required before updating the cursor */
729 gui_update_cursor(FALSE, FALSE);
730 gui_update_scrollbars(FALSE);
731 }
732# endif
733}
734#endif
735
736#if defined(FEAT_SIGNS) || defined(PROTO)
737 void
738update_debug_sign(buf, lnum)
739 buf_T *buf;
740 linenr_T lnum;
741{
742 win_T *wp;
743 int doit = FALSE;
744
745# ifdef FEAT_FOLDING
746 win_foldinfo.fi_level = 0;
747# endif
748
749 /* update/delete a specific mark */
750 FOR_ALL_WINDOWS(wp)
751 {
752 if (buf != NULL && lnum > 0)
753 {
754 if (wp->w_buffer == buf && lnum >= wp->w_topline
755 && lnum < wp->w_botline)
756 {
757 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
758 wp->w_redraw_top = lnum;
759 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
760 wp->w_redraw_bot = lnum;
761 redraw_win_later(wp, VALID);
762 }
763 }
764 else
765 redraw_win_later(wp, VALID);
766 if (wp->w_redr_type != 0)
767 doit = TRUE;
768 }
769
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100770 /* Return when there is nothing to do, screen updating is already
771 * happening (recursive call) or still starting up. */
772 if (!doit || updating_screen
773#ifdef FEAT_GUI
774 || gui.starting
775#endif
776 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 return;
778
779 /* update all windows that need updating */
780 update_prepare();
781
782# ifdef FEAT_WINDOWS
783 for (wp = firstwin; wp; wp = wp->w_next)
784 {
785 if (wp->w_redr_type != 0)
786 win_update(wp);
787 if (wp->w_redr_status)
788 win_redr_status(wp);
789 }
790# else
791 if (curwin->w_redr_type != 0)
792 win_update(curwin);
793# endif
794
795 update_finish();
796}
797#endif
798
799
800#if defined(FEAT_GUI) || defined(PROTO)
801/*
802 * Update a single window, its status line and maybe the command line msg.
803 * Used for the GUI scrollbar.
804 */
805 void
806updateWindow(wp)
807 win_T *wp;
808{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000809 /* return if already busy updating */
810 if (updating_screen)
811 return;
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 update_prepare();
814
815#ifdef FEAT_CLIPBOARD
816 /* When Visual area changed, may have to update selection. */
817 if (clip_star.available && clip_isautosel())
818 clip_update_selection();
819#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000822
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000824 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000825 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000826 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 if (wp->w_redr_status
829# ifdef FEAT_CMDL_INFO
830 || p_ru
831# endif
832# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000833 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834# endif
835 )
836 win_redr_status(wp);
837#endif
838
839 update_finish();
840}
841#endif
842
843/*
844 * Update a single window.
845 *
846 * This may cause the windows below it also to be redrawn (when clearing the
847 * screen or scrolling lines).
848 *
849 * How the window is redrawn depends on wp->w_redr_type. Each type also
850 * implies the one below it.
851 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000852 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
854 * INVERTED redraw the changed part of the Visual area
855 * INVERTED_ALL redraw the whole Visual area
856 * VALID 1. scroll up/down to adjust for a changed w_topline
857 * 2. update lines at the top when scrolled down
858 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000859 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 * b_mod_top and b_mod_bot.
861 * - if wp->w_redraw_top non-zero, redraw lines between
862 * wp->w_redraw_top and wp->w_redr_bot.
863 * - continue redrawing when syntax status is invalid.
864 * 4. if scrolled up, update lines at the bottom.
865 * This results in three areas that may need updating:
866 * top: from first row to top_end (when scrolled down)
867 * mid: from mid_start to mid_end (update inversion or changed text)
868 * bot: from bot_start to last row (when scrolled up)
869 */
870 static void
871win_update(wp)
872 win_T *wp;
873{
874 buf_T *buf = wp->w_buffer;
875 int type;
876 int top_end = 0; /* Below last row of the top area that needs
877 updating. 0 when no top area updating. */
878 int mid_start = 999;/* first row of the mid area that needs
879 updating. 999 when no mid area updating. */
880 int mid_end = 0; /* Below last row of the mid area that needs
881 updating. 0 when no mid area updating. */
882 int bot_start = 999;/* first row of the bot area that needs
883 updating. 999 when no bot area updating */
884#ifdef FEAT_VISUAL
885 int scrolled_down = FALSE; /* TRUE when scrolled down when
886 w_topline got smaller a bit */
887#endif
888#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000889 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 int top_to_mod = FALSE; /* redraw above mod_top */
891#endif
892
893 int row; /* current window row to display */
894 linenr_T lnum; /* current buffer lnum to display */
895 int idx; /* current index in w_lines[] */
896 int srow; /* starting row of the current line */
897
898 int eof = FALSE; /* if TRUE, we hit the end of the file */
899 int didline = FALSE; /* if TRUE, we finished the last line */
900 int i;
901 long j;
902 static int recursive = FALSE; /* being called recursively */
903 int old_botline = wp->w_botline;
904#ifdef FEAT_FOLDING
905 long fold_count;
906#endif
907#ifdef FEAT_SYN_HL
908 /* remember what happened to the previous line, to know if
909 * check_visual_highlight() can be used */
910#define DID_NONE 1 /* didn't update a line */
911#define DID_LINE 2 /* updated a normal line */
912#define DID_FOLD 3 /* updated a folded line */
913 int did_update = DID_NONE;
914 linenr_T syntax_last_parsed = 0; /* last parsed text line */
915#endif
916 linenr_T mod_top = 0;
917 linenr_T mod_bot = 0;
918#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
919 int save_got_int;
920#endif
921
922 type = wp->w_redr_type;
923
924 if (type == NOT_VALID)
925 {
926#ifdef FEAT_WINDOWS
927 wp->w_redr_status = TRUE;
928#endif
929 wp->w_lines_valid = 0;
930 }
931
932 /* Window is zero-height: nothing to draw. */
933 if (wp->w_height == 0)
934 {
935 wp->w_redr_type = 0;
936 return;
937 }
938
939#ifdef FEAT_VERTSPLIT
940 /* Window is zero-width: Only need to draw the separator. */
941 if (wp->w_width == 0)
942 {
943 /* draw the vertical separator right of this window */
944 draw_vsep_win(wp, 0);
945 wp->w_redr_type = 0;
946 return;
947 }
948#endif
949
950#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200951 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952#endif
953
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000954#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200955 /* Force redraw when width of 'number' or 'relativenumber' column
956 * changes. */
957 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000958 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000959 {
960 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000961 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000962 }
963 else
964#endif
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
967 {
968 /*
969 * When there are both inserted/deleted lines and specific lines to be
970 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
971 * everything (only happens when redrawing is off for while).
972 */
973 type = NOT_VALID;
974 }
975 else
976 {
977 /*
978 * Set mod_top to the first line that needs displaying because of
979 * changes. Set mod_bot to the first line after the changes.
980 */
981 mod_top = wp->w_redraw_top;
982 if (wp->w_redraw_bot != 0)
983 mod_bot = wp->w_redraw_bot + 1;
984 else
985 mod_bot = 0;
986 wp->w_redraw_top = 0; /* reset for next time */
987 wp->w_redraw_bot = 0;
988 if (buf->b_mod_set)
989 {
990 if (mod_top == 0 || mod_top > buf->b_mod_top)
991 {
992 mod_top = buf->b_mod_top;
993#ifdef FEAT_SYN_HL
994 /* Need to redraw lines above the change that may be included
995 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200996 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200998 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (mod_top < 1)
1000 mod_top = 1;
1001 }
1002#endif
1003 }
1004 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1005 mod_bot = buf->b_mod_bot;
1006
1007#ifdef FEAT_SEARCH_EXTRA
1008 /* When 'hlsearch' is on and using a multi-line search pattern, a
1009 * change in one line may make the Search highlighting in a
1010 * previous line invalid. Simple solution: redraw all visible
1011 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001012 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001014 if (search_hl.rm.regprog != NULL
1015 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001017 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001018 {
1019 cur = wp->w_match_head;
1020 while (cur != NULL)
1021 {
1022 if (cur->match.regprog != NULL
1023 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001024 {
1025 top_to_mod = TRUE;
1026 break;
1027 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001028 cur = cur->next;
1029 }
1030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#endif
1032 }
1033#ifdef FEAT_FOLDING
1034 if (mod_top != 0 && hasAnyFolding(wp))
1035 {
1036 linenr_T lnumt, lnumb;
1037
1038 /*
1039 * A change in a line can cause lines above it to become folded or
1040 * unfolded. Find the top most buffer line that may be affected.
1041 * If the line was previously folded and displayed, get the first
1042 * line of that fold. If the line is folded now, get the first
1043 * folded line. Use the minimum of these two.
1044 */
1045
1046 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1047 * the line below it. If there is no valid entry, use w_topline.
1048 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1049 * to this line. If there is no valid entry, use MAXLNUM. */
1050 lnumt = wp->w_topline;
1051 lnumb = MAXLNUM;
1052 for (i = 0; i < wp->w_lines_valid; ++i)
1053 if (wp->w_lines[i].wl_valid)
1054 {
1055 if (wp->w_lines[i].wl_lastlnum < mod_top)
1056 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1057 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1058 {
1059 lnumb = wp->w_lines[i].wl_lnum;
1060 /* When there is a fold column it might need updating
1061 * in the next line ("J" just above an open fold). */
1062 if (wp->w_p_fdc > 0)
1063 ++lnumb;
1064 }
1065 }
1066
1067 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1068 if (mod_top > lnumt)
1069 mod_top = lnumt;
1070
1071 /* Now do the same for the bottom line (one above mod_bot). */
1072 --mod_bot;
1073 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1074 ++mod_bot;
1075 if (mod_bot < lnumb)
1076 mod_bot = lnumb;
1077 }
1078#endif
1079
1080 /* When a change starts above w_topline and the end is below
1081 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001082 * If the end of the change is above w_topline: do like no change was
1083 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (mod_top != 0 && mod_top < wp->w_topline)
1085 {
1086 if (mod_bot > wp->w_topline)
1087 mod_top = wp->w_topline;
1088#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001089 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 top_end = 1;
1091#endif
1092 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001093
1094 /* When line numbers are displayed need to redraw all lines below
1095 * inserted/deleted lines. */
1096 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1097 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099
1100 /*
1101 * When only displaying the lines at the top, set top_end. Used when
1102 * window has scrolled down for msg_scrolled.
1103 */
1104 if (type == REDRAW_TOP)
1105 {
1106 j = 0;
1107 for (i = 0; i < wp->w_lines_valid; ++i)
1108 {
1109 j += wp->w_lines[i].wl_size;
1110 if (j >= wp->w_upd_rows)
1111 {
1112 top_end = j;
1113 break;
1114 }
1115 }
1116 if (top_end == 0)
1117 /* not found (cannot happen?): redraw everything */
1118 type = NOT_VALID;
1119 else
1120 /* top area defined, the rest is VALID */
1121 type = VALID;
1122 }
1123
Bram Moolenaar367329b2007-08-30 11:53:22 +00001124 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001125 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1126 * non-zero and thus not FALSE) will indicate that screenclear() was not
1127 * called. */
1128 if (screen_cleared)
1129 screen_cleared = MAYBE;
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 /*
1132 * If there are no changes on the screen that require a complete redraw,
1133 * handle three cases:
1134 * 1: we are off the top of the screen by a few lines: scroll down
1135 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1136 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1137 * w_lines[] that needs updating.
1138 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001139 if ((type == VALID || type == SOME_VALID
1140 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#ifdef FEAT_DIFF
1142 && !wp->w_botfill && !wp->w_old_botfill
1143#endif
1144 )
1145 {
1146 if (mod_top != 0 && wp->w_topline == mod_top)
1147 {
1148 /*
1149 * w_topline is the first changed line, the scrolling will be done
1150 * further down.
1151 */
1152 }
1153 else if (wp->w_lines[0].wl_valid
1154 && (wp->w_topline < wp->w_lines[0].wl_lnum
1155#ifdef FEAT_DIFF
1156 || (wp->w_topline == wp->w_lines[0].wl_lnum
1157 && wp->w_topfill > wp->w_old_topfill)
1158#endif
1159 ))
1160 {
1161 /*
1162 * New topline is above old topline: May scroll down.
1163 */
1164#ifdef FEAT_FOLDING
1165 if (hasAnyFolding(wp))
1166 {
1167 linenr_T ln;
1168
1169 /* count the number of lines we are off, counting a sequence
1170 * of folded lines as one */
1171 j = 0;
1172 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1173 {
1174 ++j;
1175 if (j >= wp->w_height - 2)
1176 break;
1177 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1178 }
1179 }
1180 else
1181#endif
1182 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1183 if (j < wp->w_height - 2) /* not too far off */
1184 {
1185 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1186#ifdef FEAT_DIFF
1187 /* insert extra lines for previously invisible filler lines */
1188 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1189 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1190 - wp->w_old_topfill;
1191#endif
1192 if (i < wp->w_height - 2) /* less than a screen off */
1193 {
1194 /*
1195 * Try to insert the correct number of lines.
1196 * If not the last window, delete the lines at the bottom.
1197 * win_ins_lines may fail when the terminal can't do it.
1198 */
1199 if (i > 0)
1200 check_for_delay(FALSE);
1201 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1202 {
1203 if (wp->w_lines_valid != 0)
1204 {
1205 /* Need to update rows that are new, stop at the
1206 * first one that scrolled down. */
1207 top_end = i;
1208#ifdef FEAT_VISUAL
1209 scrolled_down = TRUE;
1210#endif
1211
1212 /* Move the entries that were scrolled, disable
1213 * the entries for the lines to be redrawn. */
1214 if ((wp->w_lines_valid += j) > wp->w_height)
1215 wp->w_lines_valid = wp->w_height;
1216 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1217 wp->w_lines[idx] = wp->w_lines[idx - j];
1218 while (idx >= 0)
1219 wp->w_lines[idx--].wl_valid = FALSE;
1220 }
1221 }
1222 else
1223 mid_start = 0; /* redraw all lines */
1224 }
1225 else
1226 mid_start = 0; /* redraw all lines */
1227 }
1228 else
1229 mid_start = 0; /* redraw all lines */
1230 }
1231 else
1232 {
1233 /*
1234 * New topline is at or below old topline: May scroll up.
1235 * When topline didn't change, find first entry in w_lines[] that
1236 * needs updating.
1237 */
1238
1239 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1240 j = -1;
1241 row = 0;
1242 for (i = 0; i < wp->w_lines_valid; i++)
1243 {
1244 if (wp->w_lines[i].wl_valid
1245 && wp->w_lines[i].wl_lnum == wp->w_topline)
1246 {
1247 j = i;
1248 break;
1249 }
1250 row += wp->w_lines[i].wl_size;
1251 }
1252 if (j == -1)
1253 {
1254 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1255 * lines */
1256 mid_start = 0;
1257 }
1258 else
1259 {
1260 /*
1261 * Try to delete the correct number of lines.
1262 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1263 */
1264#ifdef FEAT_DIFF
1265 /* If the topline didn't change, delete old filler lines,
1266 * otherwise delete filler lines of the new topline... */
1267 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1268 row += wp->w_old_topfill;
1269 else
1270 row += diff_check_fill(wp, wp->w_topline);
1271 /* ... but don't delete new filler lines. */
1272 row -= wp->w_topfill;
1273#endif
1274 if (row > 0)
1275 {
1276 check_for_delay(FALSE);
1277 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1278 bot_start = wp->w_height - row;
1279 else
1280 mid_start = 0; /* redraw all lines */
1281 }
1282 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1283 {
1284 /*
1285 * Skip the lines (below the deleted lines) that are still
1286 * valid and don't need redrawing. Copy their info
1287 * upwards, to compensate for the deleted lines. Set
1288 * bot_start to the first row that needs redrawing.
1289 */
1290 bot_start = 0;
1291 idx = 0;
1292 for (;;)
1293 {
1294 wp->w_lines[idx] = wp->w_lines[j];
1295 /* stop at line that didn't fit, unless it is still
1296 * valid (no lines deleted) */
1297 if (row > 0 && bot_start + row
1298 + (int)wp->w_lines[j].wl_size > wp->w_height)
1299 {
1300 wp->w_lines_valid = idx + 1;
1301 break;
1302 }
1303 bot_start += wp->w_lines[idx++].wl_size;
1304
1305 /* stop at the last valid entry in w_lines[].wl_size */
1306 if (++j >= wp->w_lines_valid)
1307 {
1308 wp->w_lines_valid = idx;
1309 break;
1310 }
1311 }
1312#ifdef FEAT_DIFF
1313 /* Correct the first entry for filler lines at the top
1314 * when it won't get updated below. */
1315 if (wp->w_p_diff && bot_start > 0)
1316 wp->w_lines[0].wl_size =
1317 plines_win_nofill(wp, wp->w_topline, TRUE)
1318 + wp->w_topfill;
1319#endif
1320 }
1321 }
1322 }
1323
1324 /* When starting redraw in the first line, redraw all lines. When
1325 * there is only one window it's probably faster to clear the screen
1326 * first. */
1327 if (mid_start == 0)
1328 {
1329 mid_end = wp->w_height;
1330 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001331 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001332 /* Clear the screen when it was not done by win_del_lines() or
1333 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1334 * then. */
1335 if (screen_cleared != TRUE)
1336 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001337#ifdef FEAT_WINDOWS
1338 /* The screen was cleared, redraw the tab pages line. */
1339 if (redraw_tabline)
1340 draw_tabline();
1341#endif
1342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001344
1345 /* When win_del_lines() or win_ins_lines() caused the screen to be
1346 * cleared (only happens for the first window) or when screenclear()
1347 * was called directly above, "must_redraw" will have been set to
1348 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1349 if (screen_cleared == TRUE)
1350 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352 else
1353 {
1354 /* Not VALID or INVERTED: redraw all lines. */
1355 mid_start = 0;
1356 mid_end = wp->w_height;
1357 }
1358
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001359 if (type == SOME_VALID)
1360 {
1361 /* SOME_VALID: redraw all lines. */
1362 mid_start = 0;
1363 mid_end = wp->w_height;
1364 type = NOT_VALID;
1365 }
1366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#ifdef FEAT_VISUAL
1368 /* check if we are updating or removing the inverted part */
1369 if ((VIsual_active && buf == curwin->w_buffer)
1370 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1371 {
1372 linenr_T from, to;
1373
1374 if (VIsual_active)
1375 {
1376 if (VIsual_active
1377 && (VIsual_mode != wp->w_old_visual_mode
1378 || type == INVERTED_ALL))
1379 {
1380 /*
1381 * If the type of Visual selection changed, redraw the whole
1382 * selection. Also when the ownership of the X selection is
1383 * gained or lost.
1384 */
1385 if (curwin->w_cursor.lnum < VIsual.lnum)
1386 {
1387 from = curwin->w_cursor.lnum;
1388 to = VIsual.lnum;
1389 }
1390 else
1391 {
1392 from = VIsual.lnum;
1393 to = curwin->w_cursor.lnum;
1394 }
1395 /* redraw more when the cursor moved as well */
1396 if (wp->w_old_cursor_lnum < from)
1397 from = wp->w_old_cursor_lnum;
1398 if (wp->w_old_cursor_lnum > to)
1399 to = wp->w_old_cursor_lnum;
1400 if (wp->w_old_visual_lnum < from)
1401 from = wp->w_old_visual_lnum;
1402 if (wp->w_old_visual_lnum > to)
1403 to = wp->w_old_visual_lnum;
1404 }
1405 else
1406 {
1407 /*
1408 * Find the line numbers that need to be updated: The lines
1409 * between the old cursor position and the current cursor
1410 * position. Also check if the Visual position changed.
1411 */
1412 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1413 {
1414 from = curwin->w_cursor.lnum;
1415 to = wp->w_old_cursor_lnum;
1416 }
1417 else
1418 {
1419 from = wp->w_old_cursor_lnum;
1420 to = curwin->w_cursor.lnum;
1421 if (from == 0) /* Visual mode just started */
1422 from = to;
1423 }
1424
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001425 if (VIsual.lnum != wp->w_old_visual_lnum
1426 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 {
1428 if (wp->w_old_visual_lnum < from
1429 && wp->w_old_visual_lnum != 0)
1430 from = wp->w_old_visual_lnum;
1431 if (wp->w_old_visual_lnum > to)
1432 to = wp->w_old_visual_lnum;
1433 if (VIsual.lnum < from)
1434 from = VIsual.lnum;
1435 if (VIsual.lnum > to)
1436 to = VIsual.lnum;
1437 }
1438 }
1439
1440 /*
1441 * If in block mode and changed column or curwin->w_curswant:
1442 * update all lines.
1443 * First compute the actual start and end column.
1444 */
1445 if (VIsual_mode == Ctrl_V)
1446 {
1447 colnr_T fromc, toc;
1448
1449 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1450 ++toc;
1451 if (curwin->w_curswant == MAXCOL)
1452 toc = MAXCOL;
1453
1454 if (fromc != wp->w_old_cursor_fcol
1455 || toc != wp->w_old_cursor_lcol)
1456 {
1457 if (from > VIsual.lnum)
1458 from = VIsual.lnum;
1459 if (to < VIsual.lnum)
1460 to = VIsual.lnum;
1461 }
1462 wp->w_old_cursor_fcol = fromc;
1463 wp->w_old_cursor_lcol = toc;
1464 }
1465 }
1466 else
1467 {
1468 /* Use the line numbers of the old Visual area. */
1469 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1470 {
1471 from = wp->w_old_cursor_lnum;
1472 to = wp->w_old_visual_lnum;
1473 }
1474 else
1475 {
1476 from = wp->w_old_visual_lnum;
1477 to = wp->w_old_cursor_lnum;
1478 }
1479 }
1480
1481 /*
1482 * There is no need to update lines above the top of the window.
1483 */
1484 if (from < wp->w_topline)
1485 from = wp->w_topline;
1486
1487 /*
1488 * If we know the value of w_botline, use it to restrict the update to
1489 * the lines that are visible in the window.
1490 */
1491 if (wp->w_valid & VALID_BOTLINE)
1492 {
1493 if (from >= wp->w_botline)
1494 from = wp->w_botline - 1;
1495 if (to >= wp->w_botline)
1496 to = wp->w_botline - 1;
1497 }
1498
1499 /*
1500 * Find the minimal part to be updated.
1501 * Watch out for scrolling that made entries in w_lines[] invalid.
1502 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1503 * top_end; need to redraw from top_end to the "to" line.
1504 * A middle mouse click with a Visual selection may change the text
1505 * above the Visual area and reset wl_valid, do count these for
1506 * mid_end (in srow).
1507 */
1508 if (mid_start > 0)
1509 {
1510 lnum = wp->w_topline;
1511 idx = 0;
1512 srow = 0;
1513 if (scrolled_down)
1514 mid_start = top_end;
1515 else
1516 mid_start = 0;
1517 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1518 {
1519 if (wp->w_lines[idx].wl_valid)
1520 mid_start += wp->w_lines[idx].wl_size;
1521 else if (!scrolled_down)
1522 srow += wp->w_lines[idx].wl_size;
1523 ++idx;
1524# ifdef FEAT_FOLDING
1525 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1526 lnum = wp->w_lines[idx].wl_lnum;
1527 else
1528# endif
1529 ++lnum;
1530 }
1531 srow += mid_start;
1532 mid_end = wp->w_height;
1533 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1534 {
1535 if (wp->w_lines[idx].wl_valid
1536 && wp->w_lines[idx].wl_lnum >= to + 1)
1537 {
1538 /* Only update until first row of this line */
1539 mid_end = srow;
1540 break;
1541 }
1542 srow += wp->w_lines[idx].wl_size;
1543 }
1544 }
1545 }
1546
1547 if (VIsual_active && buf == curwin->w_buffer)
1548 {
1549 wp->w_old_visual_mode = VIsual_mode;
1550 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1551 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001552 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 wp->w_old_curswant = curwin->w_curswant;
1554 }
1555 else
1556 {
1557 wp->w_old_visual_mode = 0;
1558 wp->w_old_cursor_lnum = 0;
1559 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001560 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
1562#endif /* FEAT_VISUAL */
1563
1564#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1565 /* reset got_int, otherwise regexp won't work */
1566 save_got_int = got_int;
1567 got_int = 0;
1568#endif
1569#ifdef FEAT_FOLDING
1570 win_foldinfo.fi_level = 0;
1571#endif
1572
1573 /*
1574 * Update all the window rows.
1575 */
1576 idx = 0; /* first entry in w_lines[].wl_size */
1577 row = 0;
1578 srow = 0;
1579 lnum = wp->w_topline; /* first line shown in window */
1580 for (;;)
1581 {
1582 /* stop updating when reached the end of the window (check for _past_
1583 * the end of the window is at the end of the loop) */
1584 if (row == wp->w_height)
1585 {
1586 didline = TRUE;
1587 break;
1588 }
1589
1590 /* stop updating when hit the end of the file */
1591 if (lnum > buf->b_ml.ml_line_count)
1592 {
1593 eof = TRUE;
1594 break;
1595 }
1596
1597 /* Remember the starting row of the line that is going to be dealt
1598 * with. It is used further down when the line doesn't fit. */
1599 srow = row;
1600
1601 /*
1602 * Update a line when it is in an area that needs updating, when it
1603 * has changes or w_lines[idx] is invalid.
1604 * bot_start may be halfway a wrapped line after using
1605 * win_del_lines(), check if the current line includes it.
1606 * When syntax folding is being used, the saved syntax states will
1607 * already have been updated, we can't see where the syntax state is
1608 * the same again, just update until the end of the window.
1609 */
1610 if (row < top_end
1611 || (row >= mid_start && row < mid_end)
1612#ifdef FEAT_SEARCH_EXTRA
1613 || top_to_mod
1614#endif
1615 || idx >= wp->w_lines_valid
1616 || (row + wp->w_lines[idx].wl_size > bot_start)
1617 || (mod_top != 0
1618 && (lnum == mod_top
1619 || (lnum >= mod_top
1620 && (lnum < mod_bot
1621#ifdef FEAT_SYN_HL
1622 || did_update == DID_FOLD
1623 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001624 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 && (
1626# ifdef FEAT_FOLDING
1627 (foldmethodIsSyntax(wp)
1628 && hasAnyFolding(wp)) ||
1629# endif
1630 syntax_check_changed(lnum)))
1631#endif
1632 )))))
1633 {
1634#ifdef FEAT_SEARCH_EXTRA
1635 if (lnum == mod_top)
1636 top_to_mod = FALSE;
1637#endif
1638
1639 /*
1640 * When at start of changed lines: May scroll following lines
1641 * up or down to minimize redrawing.
1642 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001643 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 */
1645 if (lnum == mod_top
1646 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001647 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 {
1649 int old_rows = 0;
1650 int new_rows = 0;
1651 int xtra_rows;
1652 linenr_T l;
1653
1654 /* Count the old number of window rows, using w_lines[], which
1655 * should still contain the sizes for the lines as they are
1656 * currently displayed. */
1657 for (i = idx; i < wp->w_lines_valid; ++i)
1658 {
1659 /* Only valid lines have a meaningful wl_lnum. Invalid
1660 * lines are part of the changed area. */
1661 if (wp->w_lines[i].wl_valid
1662 && wp->w_lines[i].wl_lnum == mod_bot)
1663 break;
1664 old_rows += wp->w_lines[i].wl_size;
1665#ifdef FEAT_FOLDING
1666 if (wp->w_lines[i].wl_valid
1667 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1668 {
1669 /* Must have found the last valid entry above mod_bot.
1670 * Add following invalid entries. */
1671 ++i;
1672 while (i < wp->w_lines_valid
1673 && !wp->w_lines[i].wl_valid)
1674 old_rows += wp->w_lines[i++].wl_size;
1675 break;
1676 }
1677#endif
1678 }
1679
1680 if (i >= wp->w_lines_valid)
1681 {
1682 /* We can't find a valid line below the changed lines,
1683 * need to redraw until the end of the window.
1684 * Inserting/deleting lines has no use. */
1685 bot_start = 0;
1686 }
1687 else
1688 {
1689 /* Able to count old number of rows: Count new window
1690 * rows, and may insert/delete lines */
1691 j = idx;
1692 for (l = lnum; l < mod_bot; ++l)
1693 {
1694#ifdef FEAT_FOLDING
1695 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1696 ++new_rows;
1697 else
1698#endif
1699#ifdef FEAT_DIFF
1700 if (l == wp->w_topline)
1701 new_rows += plines_win_nofill(wp, l, TRUE)
1702 + wp->w_topfill;
1703 else
1704#endif
1705 new_rows += plines_win(wp, l, TRUE);
1706 ++j;
1707 if (new_rows > wp->w_height - row - 2)
1708 {
1709 /* it's getting too much, must redraw the rest */
1710 new_rows = 9999;
1711 break;
1712 }
1713 }
1714 xtra_rows = new_rows - old_rows;
1715 if (xtra_rows < 0)
1716 {
1717 /* May scroll text up. If there is not enough
1718 * remaining text or scrolling fails, must redraw the
1719 * rest. If scrolling works, must redraw the text
1720 * below the scrolled text. */
1721 if (row - xtra_rows >= wp->w_height - 2)
1722 mod_bot = MAXLNUM;
1723 else
1724 {
1725 check_for_delay(FALSE);
1726 if (win_del_lines(wp, row,
1727 -xtra_rows, FALSE, FALSE) == FAIL)
1728 mod_bot = MAXLNUM;
1729 else
1730 bot_start = wp->w_height + xtra_rows;
1731 }
1732 }
1733 else if (xtra_rows > 0)
1734 {
1735 /* May scroll text down. If there is not enough
1736 * remaining text of scrolling fails, must redraw the
1737 * rest. */
1738 if (row + xtra_rows >= wp->w_height - 2)
1739 mod_bot = MAXLNUM;
1740 else
1741 {
1742 check_for_delay(FALSE);
1743 if (win_ins_lines(wp, row + old_rows,
1744 xtra_rows, FALSE, FALSE) == FAIL)
1745 mod_bot = MAXLNUM;
1746 else if (top_end > row + old_rows)
1747 /* Scrolled the part at the top that requires
1748 * updating down. */
1749 top_end += xtra_rows;
1750 }
1751 }
1752
1753 /* When not updating the rest, may need to move w_lines[]
1754 * entries. */
1755 if (mod_bot != MAXLNUM && i != j)
1756 {
1757 if (j < i)
1758 {
1759 int x = row + new_rows;
1760
1761 /* move entries in w_lines[] upwards */
1762 for (;;)
1763 {
1764 /* stop at last valid entry in w_lines[] */
1765 if (i >= wp->w_lines_valid)
1766 {
1767 wp->w_lines_valid = j;
1768 break;
1769 }
1770 wp->w_lines[j] = wp->w_lines[i];
1771 /* stop at a line that won't fit */
1772 if (x + (int)wp->w_lines[j].wl_size
1773 > wp->w_height)
1774 {
1775 wp->w_lines_valid = j + 1;
1776 break;
1777 }
1778 x += wp->w_lines[j++].wl_size;
1779 ++i;
1780 }
1781 if (bot_start > x)
1782 bot_start = x;
1783 }
1784 else /* j > i */
1785 {
1786 /* move entries in w_lines[] downwards */
1787 j -= i;
1788 wp->w_lines_valid += j;
1789 if (wp->w_lines_valid > wp->w_height)
1790 wp->w_lines_valid = wp->w_height;
1791 for (i = wp->w_lines_valid; i - j >= idx; --i)
1792 wp->w_lines[i] = wp->w_lines[i - j];
1793
1794 /* The w_lines[] entries for inserted lines are
1795 * now invalid, but wl_size may be used above.
1796 * Reset to zero. */
1797 while (i >= idx)
1798 {
1799 wp->w_lines[i].wl_size = 0;
1800 wp->w_lines[i--].wl_valid = FALSE;
1801 }
1802 }
1803 }
1804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806
1807#ifdef FEAT_FOLDING
1808 /*
1809 * When lines are folded, display one line for all of them.
1810 * Otherwise, display normally (can be several display lines when
1811 * 'wrap' is on).
1812 */
1813 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1814 if (fold_count != 0)
1815 {
1816 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1817 ++row;
1818 --fold_count;
1819 wp->w_lines[idx].wl_folded = TRUE;
1820 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1821# ifdef FEAT_SYN_HL
1822 did_update = DID_FOLD;
1823# endif
1824 }
1825 else
1826#endif
1827 if (idx < wp->w_lines_valid
1828 && wp->w_lines[idx].wl_valid
1829 && wp->w_lines[idx].wl_lnum == lnum
1830 && lnum > wp->w_topline
1831 && !(dy_flags & DY_LASTLINE)
1832 && srow + wp->w_lines[idx].wl_size > wp->w_height
1833#ifdef FEAT_DIFF
1834 && diff_check_fill(wp, lnum) == 0
1835#endif
1836 )
1837 {
1838 /* This line is not going to fit. Don't draw anything here,
1839 * will draw "@ " lines below. */
1840 row = wp->w_height + 1;
1841 }
1842 else
1843 {
1844#ifdef FEAT_SEARCH_EXTRA
1845 prepare_search_hl(wp, lnum);
1846#endif
1847#ifdef FEAT_SYN_HL
1848 /* Let the syntax stuff know we skipped a few lines. */
1849 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001850 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 syntax_end_parsing(syntax_last_parsed + 1);
1852#endif
1853
1854 /*
1855 * Display one line.
1856 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001857 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
1859#ifdef FEAT_FOLDING
1860 wp->w_lines[idx].wl_folded = FALSE;
1861 wp->w_lines[idx].wl_lastlnum = lnum;
1862#endif
1863#ifdef FEAT_SYN_HL
1864 did_update = DID_LINE;
1865 syntax_last_parsed = lnum;
1866#endif
1867 }
1868
1869 wp->w_lines[idx].wl_lnum = lnum;
1870 wp->w_lines[idx].wl_valid = TRUE;
1871 if (row > wp->w_height) /* past end of screen */
1872 {
1873 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001874 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1876 ++idx;
1877 break;
1878 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001879 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 wp->w_lines[idx].wl_size = row - srow;
1881 ++idx;
1882#ifdef FEAT_FOLDING
1883 lnum += fold_count + 1;
1884#else
1885 ++lnum;
1886#endif
1887 }
1888 else
1889 {
1890 /* This line does not need updating, advance to the next one */
1891 row += wp->w_lines[idx++].wl_size;
1892 if (row > wp->w_height) /* past end of screen */
1893 break;
1894#ifdef FEAT_FOLDING
1895 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1896#else
1897 ++lnum;
1898#endif
1899#ifdef FEAT_SYN_HL
1900 did_update = DID_NONE;
1901#endif
1902 }
1903
1904 if (lnum > buf->b_ml.ml_line_count)
1905 {
1906 eof = TRUE;
1907 break;
1908 }
1909 }
1910 /*
1911 * End of loop over all window lines.
1912 */
1913
1914
1915 if (idx > wp->w_lines_valid)
1916 wp->w_lines_valid = idx;
1917
1918#ifdef FEAT_SYN_HL
1919 /*
1920 * Let the syntax stuff know we stop parsing here.
1921 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001922 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 syntax_end_parsing(syntax_last_parsed + 1);
1924#endif
1925
1926 /*
1927 * If we didn't hit the end of the file, and we didn't finish the last
1928 * line we were working on, then the line didn't fit.
1929 */
1930 wp->w_empty_rows = 0;
1931#ifdef FEAT_DIFF
1932 wp->w_filler_rows = 0;
1933#endif
1934 if (!eof && !didline)
1935 {
1936 if (lnum == wp->w_topline)
1937 {
1938 /*
1939 * Single line that does not fit!
1940 * Don't overwrite it, it can be edited.
1941 */
1942 wp->w_botline = lnum + 1;
1943 }
1944#ifdef FEAT_DIFF
1945 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1946 {
1947 /* Window ends in filler lines. */
1948 wp->w_botline = lnum;
1949 wp->w_filler_rows = wp->w_height - srow;
1950 }
1951#endif
1952 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1953 {
1954 /*
1955 * Last line isn't finished: Display "@@@" at the end.
1956 */
1957 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1958 W_WINROW(wp) + wp->w_height,
1959 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1960 '@', '@', hl_attr(HLF_AT));
1961 set_empty_rows(wp, srow);
1962 wp->w_botline = lnum;
1963 }
1964 else
1965 {
1966 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1967 wp->w_botline = lnum;
1968 }
1969 }
1970 else
1971 {
1972#ifdef FEAT_VERTSPLIT
1973 draw_vsep_win(wp, row);
1974#endif
1975 if (eof) /* we hit the end of the file */
1976 {
1977 wp->w_botline = buf->b_ml.ml_line_count + 1;
1978#ifdef FEAT_DIFF
1979 j = diff_check_fill(wp, wp->w_botline);
1980 if (j > 0 && !wp->w_botfill)
1981 {
1982 /*
1983 * Display filler lines at the end of the file
1984 */
1985 if (char2cells(fill_diff) > 1)
1986 i = '-';
1987 else
1988 i = fill_diff;
1989 if (row + j > wp->w_height)
1990 j = wp->w_height - row;
1991 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1992 row += j;
1993 }
1994#endif
1995 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001996 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 wp->w_botline = lnum;
1998
1999 /* make sure the rest of the screen is blank */
2000 /* put '~'s on rows that aren't part of the file. */
2001 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2002 }
2003
2004 /* Reset the type of redrawing required, the window has been updated. */
2005 wp->w_redr_type = 0;
2006#ifdef FEAT_DIFF
2007 wp->w_old_topfill = wp->w_topfill;
2008 wp->w_old_botfill = wp->w_botfill;
2009#endif
2010
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002011 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
2013 /*
2014 * There is a trick with w_botline. If we invalidate it on each
2015 * change that might modify it, this will cause a lot of expensive
2016 * calls to plines() in update_topline() each time. Therefore the
2017 * value of w_botline is often approximated, and this value is used to
2018 * compute the value of w_topline. If the value of w_botline was
2019 * wrong, check that the value of w_topline is correct (cursor is on
2020 * the visible part of the text). If it's not, we need to redraw
2021 * again. Mostly this just means scrolling up a few lines, so it
2022 * doesn't look too bad. Only do this for the current window (where
2023 * changes are relevant).
2024 */
2025 wp->w_valid |= VALID_BOTLINE;
2026 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2027 {
2028 recursive = TRUE;
2029 curwin->w_valid &= ~VALID_TOPLINE;
2030 update_topline(); /* may invalidate w_botline again */
2031 if (must_redraw != 0)
2032 {
2033 /* Don't update for changes in buffer again. */
2034 i = curbuf->b_mod_set;
2035 curbuf->b_mod_set = FALSE;
2036 win_update(curwin);
2037 must_redraw = 0;
2038 curbuf->b_mod_set = i;
2039 }
2040 recursive = FALSE;
2041 }
2042 }
2043
2044#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2045 /* restore got_int, unless CTRL-C was hit while redrawing */
2046 if (!got_int)
2047 got_int = save_got_int;
2048#endif
2049}
2050
2051#ifdef FEAT_SIGNS
2052static int draw_signcolumn __ARGS((win_T *wp));
2053
2054/*
2055 * Return TRUE when window "wp" has a column to draw signs in.
2056 */
2057 static int
2058draw_signcolumn(wp)
2059 win_T *wp;
2060{
2061 return (wp->w_buffer->b_signlist != NULL
2062# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002063 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064# endif
2065 );
2066}
2067#endif
2068
2069/*
2070 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2071 * as the filler character.
2072 */
2073 static void
2074win_draw_end(wp, c1, c2, row, endrow, hl)
2075 win_T *wp;
2076 int c1;
2077 int c2;
2078 int row;
2079 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002080 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081{
2082#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2083 int n = 0;
2084# define FDC_OFF n
2085#else
2086# define FDC_OFF 0
2087#endif
2088
2089#ifdef FEAT_RIGHTLEFT
2090 if (wp->w_p_rl)
2091 {
2092 /* No check for cmdline window: should never be right-left. */
2093# ifdef FEAT_FOLDING
2094 n = wp->w_p_fdc;
2095
2096 if (n > 0)
2097 {
2098 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002099 if (n > W_WIDTH(wp))
2100 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2102 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2103 ' ', ' ', hl_attr(HLF_FC));
2104 }
2105# endif
2106# ifdef FEAT_SIGNS
2107 if (draw_signcolumn(wp))
2108 {
2109 int nn = n + 2;
2110
2111 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002112 if (nn > W_WIDTH(wp))
2113 nn = 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) - nn, (int)W_ENDCOL(wp) - n,
2116 ' ', ' ', hl_attr(HLF_SC));
2117 n = nn;
2118 }
2119# endif
2120 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2121 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2122 c2, c2, hl_attr(hl));
2123 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2124 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2125 c1, c2, hl_attr(hl));
2126 }
2127 else
2128#endif
2129 {
2130#ifdef FEAT_CMDWIN
2131 if (cmdwin_type != 0 && wp == curwin)
2132 {
2133 /* draw the cmdline character in the leftmost column */
2134 n = 1;
2135 if (n > wp->w_width)
2136 n = wp->w_width;
2137 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2138 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2139 cmdwin_type, ' ', hl_attr(HLF_AT));
2140 }
2141#endif
2142#ifdef FEAT_FOLDING
2143 if (wp->w_p_fdc > 0)
2144 {
2145 int nn = n + wp->w_p_fdc;
2146
2147 /* draw the fold column at the left */
2148 if (nn > W_WIDTH(wp))
2149 nn = W_WIDTH(wp);
2150 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2151 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2152 ' ', ' ', hl_attr(HLF_FC));
2153 n = nn;
2154 }
2155#endif
2156#ifdef FEAT_SIGNS
2157 if (draw_signcolumn(wp))
2158 {
2159 int nn = n + 2;
2160
2161 /* draw the sign column after the fold column */
2162 if (nn > W_WIDTH(wp))
2163 nn = W_WIDTH(wp);
2164 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2165 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2166 ' ', ' ', hl_attr(HLF_SC));
2167 n = nn;
2168 }
2169#endif
2170 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2171 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2172 c1, c2, hl_attr(hl));
2173 }
2174 set_empty_rows(wp, row);
2175}
2176
Bram Moolenaar1a384422010-07-14 19:53:30 +02002177#ifdef FEAT_SYN_HL
2178static int advance_color_col __ARGS((int vcol, int **color_cols));
2179
2180/*
2181 * Advance **color_cols and return TRUE when there are columns to draw.
2182 */
2183 static int
2184advance_color_col(vcol, color_cols)
2185 int vcol;
2186 int **color_cols;
2187{
2188 while (**color_cols >= 0 && vcol > **color_cols)
2189 ++*color_cols;
2190 return (**color_cols >= 0);
2191}
2192#endif
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194#ifdef FEAT_FOLDING
2195/*
2196 * Display one folded line.
2197 */
2198 static void
2199fold_line(wp, fold_count, foldinfo, lnum, row)
2200 win_T *wp;
2201 long fold_count;
2202 foldinfo_T *foldinfo;
2203 linenr_T lnum;
2204 int row;
2205{
2206 char_u buf[51];
2207 pos_T *top, *bot;
2208 linenr_T lnume = lnum + fold_count - 1;
2209 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002210 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 int col;
2213 int txtcol;
2214 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002215 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217 /* Build the fold line:
2218 * 1. Add the cmdwin_type for the command-line window
2219 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002220 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * 4. Compose the text
2222 * 5. Add the text
2223 * 6. set highlighting for the Visual area an other text
2224 */
2225 col = 0;
2226
2227 /*
2228 * 1. Add the cmdwin_type for the command-line window
2229 * Ignores 'rightleft', this window is never right-left.
2230 */
2231#ifdef FEAT_CMDWIN
2232 if (cmdwin_type != 0 && wp == curwin)
2233 {
2234 ScreenLines[off] = cmdwin_type;
2235 ScreenAttrs[off] = hl_attr(HLF_AT);
2236#ifdef FEAT_MBYTE
2237 if (enc_utf8)
2238 ScreenLinesUC[off] = 0;
2239#endif
2240 ++col;
2241 }
2242#endif
2243
2244 /*
2245 * 2. Add the 'foldcolumn'
2246 */
2247 fdc = wp->w_p_fdc;
2248 if (fdc > W_WIDTH(wp) - col)
2249 fdc = W_WIDTH(wp) - col;
2250 if (fdc > 0)
2251 {
2252 fill_foldcolumn(buf, wp, TRUE, lnum);
2253#ifdef FEAT_RIGHTLEFT
2254 if (wp->w_p_rl)
2255 {
2256 int i;
2257
2258 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2259 hl_attr(HLF_FC));
2260 /* reverse the fold column */
2261 for (i = 0; i < fdc; ++i)
2262 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2263 }
2264 else
2265#endif
2266 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2267 col += fdc;
2268 }
2269
2270#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002271# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2272 for (ri = 0; ri < l; ++ri) \
2273 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2274 else \
2275 for (ri = 0; ri < l; ++ri) \
2276 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002278# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2279 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280#endif
2281
Bram Moolenaar64486672010-05-16 15:46:46 +02002282 /* Set all attributes of the 'number' or 'relativenumber' column and the
2283 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002284 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286#ifdef FEAT_SIGNS
2287 /* If signs are being displayed, add two spaces. */
2288 if (draw_signcolumn(wp))
2289 {
2290 len = W_WIDTH(wp) - col;
2291 if (len > 0)
2292 {
2293 if (len > 2)
2294 len = 2;
2295# ifdef FEAT_RIGHTLEFT
2296 if (wp->w_p_rl)
2297 /* the line number isn't reversed */
2298 copy_text_attr(off + W_WIDTH(wp) - len - col,
2299 (char_u *)" ", len, hl_attr(HLF_FL));
2300 else
2301# endif
2302 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2303 col += len;
2304 }
2305 }
2306#endif
2307
2308 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002309 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002311 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
2313 len = W_WIDTH(wp) - col;
2314 if (len > 0)
2315 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002316 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002317 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002318
2319 if (len > w + 1)
2320 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002321
2322 if (wp->w_p_nu)
2323 /* 'number' */
2324 num = (long)lnum;
2325 else
2326 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002327 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02002328
2329 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#ifdef FEAT_RIGHTLEFT
2331 if (wp->w_p_rl)
2332 /* the line number isn't reversed */
2333 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2334 hl_attr(HLF_FL));
2335 else
2336#endif
2337 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2338 col += len;
2339 }
2340 }
2341
2342 /*
2343 * 4. Compose the folded-line string with 'foldtext', if set.
2344 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002345 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347 txtcol = col; /* remember where text starts */
2348
2349 /*
2350 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2351 * Right-left text is put in columns 0 - number-col, normal text is put
2352 * in columns number-col - window-width.
2353 */
2354#ifdef FEAT_MBYTE
2355 if (has_mbyte)
2356 {
2357 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002358 int u8c, u8cc[MAX_MCO];
2359 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 int idx;
2361 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002362 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363# ifdef FEAT_ARABIC
2364 int prev_c = 0; /* previous Arabic character */
2365 int prev_c1 = 0; /* first composing char for prev_c */
2366# endif
2367
2368# ifdef FEAT_RIGHTLEFT
2369 if (wp->w_p_rl)
2370 idx = off;
2371 else
2372# endif
2373 idx = off + col;
2374
2375 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2376 for (p = text; *p != NUL; )
2377 {
2378 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002379 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (col + cells > W_WIDTH(wp)
2381# ifdef FEAT_RIGHTLEFT
2382 - (wp->w_p_rl ? col : 0)
2383# endif
2384 )
2385 break;
2386 ScreenLines[idx] = *p;
2387 if (enc_utf8)
2388 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002389 u8c = utfc_ptr2char(p, u8cc);
2390 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
2392 ScreenLinesUC[idx] = 0;
2393#ifdef FEAT_ARABIC
2394 prev_c = u8c;
2395#endif
2396 }
2397 else
2398 {
2399#ifdef FEAT_ARABIC
2400 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2401 {
2402 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002403 int pc, pc1, nc;
2404 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 int firstbyte = *p;
2406
2407 /* The idea of what is the previous and next
2408 * character depends on 'rightleft'. */
2409 if (wp->w_p_rl)
2410 {
2411 pc = prev_c;
2412 pc1 = prev_c1;
2413 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002414 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 else
2417 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002418 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002420 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422 prev_c = u8c;
2423
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002424 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 pc, pc1, nc);
2426 ScreenLines[idx] = firstbyte;
2427 }
2428 else
2429 prev_c = u8c;
2430#endif
2431 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002432#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (u8c >= 0x10000)
2434 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2435 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002438 for (i = 0; i < Screen_mco; ++i)
2439 {
2440 ScreenLinesC[i][idx] = u8cc[i];
2441 if (u8cc[i] == 0)
2442 break;
2443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
2445 if (cells > 1)
2446 ScreenLines[idx + 1] = 0;
2447 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002448 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2449 /* double-byte single width character */
2450 ScreenLines2[idx] = p[1];
2451 else if (cells > 1)
2452 /* double-width character */
2453 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 col += cells;
2455 idx += cells;
2456 p += c_len;
2457 }
2458 }
2459 else
2460#endif
2461 {
2462 len = (int)STRLEN(text);
2463 if (len > W_WIDTH(wp) - col)
2464 len = W_WIDTH(wp) - col;
2465 if (len > 0)
2466 {
2467#ifdef FEAT_RIGHTLEFT
2468 if (wp->w_p_rl)
2469 STRNCPY(current_ScreenLine, text, len);
2470 else
2471#endif
2472 STRNCPY(current_ScreenLine + col, text, len);
2473 col += len;
2474 }
2475 }
2476
2477 /* Fill the rest of the line with the fold filler */
2478#ifdef FEAT_RIGHTLEFT
2479 if (wp->w_p_rl)
2480 col -= txtcol;
2481#endif
2482 while (col < W_WIDTH(wp)
2483#ifdef FEAT_RIGHTLEFT
2484 - (wp->w_p_rl ? txtcol : 0)
2485#endif
2486 )
2487 {
2488#ifdef FEAT_MBYTE
2489 if (enc_utf8)
2490 {
2491 if (fill_fold >= 0x80)
2492 {
2493 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002494 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
2496 else
2497 ScreenLinesUC[off + col] = 0;
2498 }
2499#endif
2500 ScreenLines[off + col++] = fill_fold;
2501 }
2502
2503 if (text != buf)
2504 vim_free(text);
2505
2506 /*
2507 * 6. set highlighting for the Visual area an other text.
2508 * If all folded lines are in the Visual area, highlight the line.
2509 */
2510#ifdef FEAT_VISUAL
2511 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2512 {
2513 if (ltoreq(curwin->w_cursor, VIsual))
2514 {
2515 /* Visual is after curwin->w_cursor */
2516 top = &curwin->w_cursor;
2517 bot = &VIsual;
2518 }
2519 else
2520 {
2521 /* Visual is before curwin->w_cursor */
2522 top = &VIsual;
2523 bot = &curwin->w_cursor;
2524 }
2525 if (lnum >= top->lnum
2526 && lnume <= bot->lnum
2527 && (VIsual_mode != 'v'
2528 || ((lnum > top->lnum
2529 || (lnum == top->lnum
2530 && top->col == 0))
2531 && (lnume < bot->lnum
2532 || (lnume == bot->lnum
2533 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002534 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 if (VIsual_mode == Ctrl_V)
2537 {
2538 /* Visual block mode: highlight the chars part of the block */
2539 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2540 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002541 if (wp->w_old_cursor_lcol != MAXCOL
2542 && wp->w_old_cursor_lcol + txtcol
2543 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 len = wp->w_old_cursor_lcol;
2545 else
2546 len = W_WIDTH(wp) - txtcol;
2547 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002548 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 }
2550 }
2551 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002554 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 }
2557 }
2558#endif
2559
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002560#ifdef FEAT_SYN_HL
2561 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002562 if (wp->w_p_cuc)
2563 {
2564 txtcol += wp->w_virtcol;
2565 if (wp->w_p_wrap)
2566 txtcol -= wp->w_skipcol;
2567 else
2568 txtcol -= wp->w_leftcol;
2569 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2570 ScreenAttrs[off + txtcol] = hl_combine_attr(
2571 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2572 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
2575 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2576 (int)W_WIDTH(wp), FALSE);
2577
2578 /*
2579 * Update w_cline_height and w_cline_folded if the cursor line was
2580 * updated (saves a call to plines() later).
2581 */
2582 if (wp == curwin
2583 && lnum <= curwin->w_cursor.lnum
2584 && lnume >= curwin->w_cursor.lnum)
2585 {
2586 curwin->w_cline_row = row;
2587 curwin->w_cline_height = 1;
2588 curwin->w_cline_folded = TRUE;
2589 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2590 }
2591}
2592
2593/*
2594 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2595 */
2596 static void
2597copy_text_attr(off, buf, len, attr)
2598 int off;
2599 char_u *buf;
2600 int len;
2601 int attr;
2602{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002603 int i;
2604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 mch_memmove(ScreenLines + off, buf, (size_t)len);
2606# ifdef FEAT_MBYTE
2607 if (enc_utf8)
2608 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2609# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002610 for (i = 0; i < len; ++i)
2611 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612}
2613
2614/*
2615 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002616 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 */
2618 static void
2619fill_foldcolumn(p, wp, closed, lnum)
2620 char_u *p;
2621 win_T *wp;
2622 int closed; /* TRUE of FALSE */
2623 linenr_T lnum; /* current line number */
2624{
2625 int i = 0;
2626 int level;
2627 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002628 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
2630 /* Init to all spaces. */
2631 copy_spaces(p, (size_t)wp->w_p_fdc);
2632
2633 level = win_foldinfo.fi_level;
2634 if (level > 0)
2635 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002636 /* If there is only one column put more info in it. */
2637 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 /* If the column is too narrow, we start at the lowest level that
2640 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002641 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if (first_level < 1)
2643 first_level = 1;
2644
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002645 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
2647 if (win_foldinfo.fi_lnum == lnum
2648 && first_level + i >= win_foldinfo.fi_low_level)
2649 p[i] = '-';
2650 else if (first_level == 1)
2651 p[i] = '|';
2652 else if (first_level + i <= 9)
2653 p[i] = '0' + first_level + i;
2654 else
2655 p[i] = '>';
2656 if (first_level + i == level)
2657 break;
2658 }
2659 }
2660 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002661 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662}
2663#endif /* FEAT_FOLDING */
2664
2665/*
2666 * Display line "lnum" of window 'wp' on the screen.
2667 * Start at row "startrow", stop when "endrow" is reached.
2668 * wp->w_virtcol needs to be valid.
2669 *
2670 * Return the number of last row the line occupies.
2671 */
2672 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002673win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 win_T *wp;
2675 linenr_T lnum;
2676 int startrow;
2677 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002678 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680 int col; /* visual column on screen */
2681 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2682 int c = 0; /* init for GCC */
2683 long vcol = 0; /* virtual column (for tabs) */
2684 long vcol_prev = -1; /* "vcol" of previous character */
2685 char_u *line; /* current line */
2686 char_u *ptr; /* current position in "line" */
2687 int row; /* row in the window, excl w_winrow */
2688 int screen_row; /* row on the screen, incl w_winrow */
2689
2690 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2691 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002692 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 int c_extra = NUL; /* extra chars, all the same */
2694 int extra_attr = 0; /* attributes when n_extra != 0 */
2695 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2696 displaying lcs_eol at end-of-line */
2697 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2698 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2699
2700 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2701 int saved_n_extra = 0;
2702 char_u *saved_p_extra = NULL;
2703 int saved_c_extra = 0;
2704 int saved_char_attr = 0;
2705
2706 int n_attr = 0; /* chars with special attr */
2707 int saved_attr2 = 0; /* char_attr saved for n_attr */
2708 int n_attr3 = 0; /* chars with overruling special attr */
2709 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2710
2711 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2712
2713 int fromcol, tocol; /* start/end of inverting */
2714 int fromcol_prev = -2; /* start of inverting after cursor */
2715 int noinvcur = FALSE; /* don't invert the cursor */
2716#ifdef FEAT_VISUAL
2717 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002718 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719#endif
2720 pos_T pos;
2721 long v;
2722
2723 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002724 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2726 in this line */
2727 int attr = 0; /* attributes for area highlighting */
2728 int area_attr = 0; /* attributes desired by highlighting */
2729 int search_attr = 0; /* attributes desired by 'hlsearch' */
2730#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002731 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 int syntax_attr = 0; /* attributes desired by syntax */
2733 int has_syntax = FALSE; /* this buffer has syntax highl. */
2734 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002735 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002736 int draw_color_col = FALSE; /* highlight colorcolumn */
2737 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002738#endif
2739#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002740 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002741# define SPWORDLEN 150
2742 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002743 int nextlinecol = 0; /* column where nextline[] starts */
2744 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002745 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002746 int spell_attr = 0; /* attributes desired by spelling */
2747 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002748 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2749 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002750 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002751 static int cap_col = -1; /* column to check for Cap word */
2752 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002753 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754#endif
2755 int extra_check; /* has syntax or linebreak */
2756#ifdef FEAT_MBYTE
2757 int multi_attr = 0; /* attributes desired by multibyte */
2758 int mb_l = 1; /* multi-byte byte length */
2759 int mb_c = 0; /* decoded multi-byte character */
2760 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002761 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762#endif
2763#ifdef FEAT_DIFF
2764 int filler_lines; /* nr of filler lines to be drawn */
2765 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002766 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 int change_start = MAXCOL; /* first col of changed area */
2768 int change_end = -1; /* last col of changed area */
2769#endif
2770 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2771#ifdef FEAT_LINEBREAK
2772 int need_showbreak = FALSE;
2773#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002774#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2775 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002777 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778#endif
2779#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002780 matchitem_T *cur; /* points to the match list */
2781 match_T *shl; /* points to search_hl or a match */
2782 int shl_flag; /* flag to indicate whether search_hl
2783 has been processed or not */
2784 int prevcol_hl_flag; /* flag to indicate whether prevcol
2785 equals startcol of search_hl or one
2786 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787#endif
2788#ifdef FEAT_ARABIC
2789 int prev_c = 0; /* previous Arabic character */
2790 int prev_c1 = 0; /* first composing char for prev_c */
2791#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002792#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002793 int did_line_attr = 0;
2794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
2796 /* draw_state: items that are drawn in sequence: */
2797#define WL_START 0 /* nothing done yet */
2798#ifdef FEAT_CMDWIN
2799# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2800#else
2801# define WL_CMDLINE WL_START
2802#endif
2803#ifdef FEAT_FOLDING
2804# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2805#else
2806# define WL_FOLD WL_CMDLINE
2807#endif
2808#ifdef FEAT_SIGNS
2809# define WL_SIGN WL_FOLD + 1 /* column for signs */
2810#else
2811# define WL_SIGN WL_FOLD /* column for signs */
2812#endif
2813#define WL_NR WL_SIGN + 1 /* line number */
2814#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2815# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2816#else
2817# define WL_SBR WL_NR
2818#endif
2819#define WL_LINE WL_SBR + 1 /* text in the line */
2820 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002821#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 int feedback_col = 0;
2823 int feedback_old_attr = -1;
2824#endif
2825
Bram Moolenaar860cae12010-06-05 23:22:07 +02002826#ifdef FEAT_CONCEAL
2827 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002828 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002829 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002830 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002831 int is_concealing = FALSE;
2832 int boguscols = 0; /* nonexistent columns added to force
2833 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002834 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002835 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002836# define VCOL_HLC (vcol - vcol_off)
2837#else
2838# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
2841 if (startrow > endrow) /* past the end already! */
2842 return startrow;
2843
2844 row = startrow;
2845 screen_row = row + W_WINROW(wp);
2846
2847 /*
2848 * To speed up the loop below, set extra_check when there is linebreak,
2849 * trailing white space and/or syntax processing to be done.
2850 */
2851#ifdef FEAT_LINEBREAK
2852 extra_check = wp->w_p_lbr;
2853#else
2854 extra_check = 0;
2855#endif
2856#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002857 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
2859 /* Prepare for syntax highlighting in this line. When there is an
2860 * error, stop syntax highlighting. */
2861 save_did_emsg = did_emsg;
2862 did_emsg = FALSE;
2863 syntax_start(wp, lnum);
2864 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002865 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 else
2867 {
2868 did_emsg = save_did_emsg;
2869 has_syntax = TRUE;
2870 extra_check = TRUE;
2871 }
2872 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002873
2874 /* Check for columns to display for 'colorcolumn'. */
2875 color_cols = wp->w_p_cc_cols;
2876 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002877 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002878#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002879
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002880#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002881 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002882 && *wp->w_s->b_p_spl != NUL
2883 && wp->w_s->b_langp.ga_len > 0
2884 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002885 {
2886 /* Prepare for spell checking. */
2887 has_spell = TRUE;
2888 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002889
2890 /* Get the start of the next line, so that words that wrap to the next
2891 * line are found too: "et<line-break>al.".
2892 * Trick: skip a few chars for C/shell/Vim comments */
2893 nextline[SPWORDLEN] = NUL;
2894 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2895 {
2896 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2897 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2898 }
2899
2900 /* When a word wrapped from the previous line the start of the current
2901 * line is valid. */
2902 if (lnum == checked_lnum)
2903 cur_checked_col = checked_col;
2904 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002905
2906 /* When there was a sentence end in the previous line may require a
2907 * word starting with capital in this line. In line 1 always check
2908 * the first word. */
2909 if (lnum != capcol_lnum)
2910 cap_col = -1;
2911 if (lnum == 1)
2912 cap_col = 0;
2913 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915#endif
2916
2917 /*
2918 * handle visual active in this window
2919 */
2920 fromcol = -10;
2921 tocol = MAXCOL;
2922#ifdef FEAT_VISUAL
2923 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2924 {
2925 /* Visual is after curwin->w_cursor */
2926 if (ltoreq(curwin->w_cursor, VIsual))
2927 {
2928 top = &curwin->w_cursor;
2929 bot = &VIsual;
2930 }
2931 else /* Visual is before curwin->w_cursor */
2932 {
2933 top = &VIsual;
2934 bot = &curwin->w_cursor;
2935 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002936 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 if (VIsual_mode == Ctrl_V) /* block mode */
2938 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002939 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
2941 fromcol = wp->w_old_cursor_fcol;
2942 tocol = wp->w_old_cursor_lcol;
2943 }
2944 }
2945 else /* non-block mode */
2946 {
2947 if (lnum > top->lnum && lnum <= bot->lnum)
2948 fromcol = 0;
2949 else if (lnum == top->lnum)
2950 {
2951 if (VIsual_mode == 'V') /* linewise */
2952 fromcol = 0;
2953 else
2954 {
2955 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2956 if (gchar_pos(top) == NUL)
2957 tocol = fromcol + 1;
2958 }
2959 }
2960 if (VIsual_mode != 'V' && lnum == bot->lnum)
2961 {
2962 if (*p_sel == 'e' && bot->col == 0
2963#ifdef FEAT_VIRTUALEDIT
2964 && bot->coladd == 0
2965#endif
2966 )
2967 {
2968 fromcol = -10;
2969 tocol = MAXCOL;
2970 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002971 else if (bot->col == MAXCOL)
2972 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 else
2974 {
2975 pos = *bot;
2976 if (*p_sel == 'e')
2977 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2978 else
2979 {
2980 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2981 ++tocol;
2982 }
2983 }
2984 }
2985 }
2986
2987#ifndef MSDOS
2988 /* Check if the character under the cursor should not be inverted */
2989 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2990# ifdef FEAT_GUI
2991 && !gui.in_use
2992# endif
2993 )
2994 noinvcur = TRUE;
2995#endif
2996
2997 /* if inverting in this line set area_highlighting */
2998 if (fromcol >= 0)
2999 {
3000 area_highlighting = TRUE;
3001 attr = hl_attr(HLF_V);
3002#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3003 if (clip_star.available && !clip_star.owned && clip_isautosel())
3004 attr = hl_attr(HLF_VNC);
3005#endif
3006 }
3007 }
3008
3009 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003010 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 */
3012 else
3013#endif /* FEAT_VISUAL */
3014 if (highlight_match
3015 && wp == curwin
3016 && lnum >= curwin->w_cursor.lnum
3017 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3018 {
3019 if (lnum == curwin->w_cursor.lnum)
3020 getvcol(curwin, &(curwin->w_cursor),
3021 (colnr_T *)&fromcol, NULL, NULL);
3022 else
3023 fromcol = 0;
3024 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3025 {
3026 pos.lnum = lnum;
3027 pos.col = search_match_endcol;
3028 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3029 }
3030 else
3031 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003032 /* do at least one character; happens when past end of line */
3033 if (fromcol == tocol)
3034 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 area_highlighting = TRUE;
3036 attr = hl_attr(HLF_I);
3037 }
3038
3039#ifdef FEAT_DIFF
3040 filler_lines = diff_check(wp, lnum);
3041 if (filler_lines < 0)
3042 {
3043 if (filler_lines == -1)
3044 {
3045 if (diff_find_change(wp, lnum, &change_start, &change_end))
3046 diff_hlf = HLF_ADD; /* added line */
3047 else if (change_start == 0)
3048 diff_hlf = HLF_TXD; /* changed text */
3049 else
3050 diff_hlf = HLF_CHD; /* changed line */
3051 }
3052 else
3053 diff_hlf = HLF_ADD; /* added line */
3054 filler_lines = 0;
3055 area_highlighting = TRUE;
3056 }
3057 if (lnum == wp->w_topline)
3058 filler_lines = wp->w_topfill;
3059 filler_todo = filler_lines;
3060#endif
3061
3062#ifdef LINE_ATTR
3063# ifdef FEAT_SIGNS
3064 /* If this line has a sign with line highlighting set line_attr. */
3065 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3066 if (v != 0)
3067 line_attr = sign_get_attr((int)v, TRUE);
3068# endif
3069# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3070 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003071 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 line_attr = hl_attr(HLF_L);
3073# endif
3074 if (line_attr != 0)
3075 area_highlighting = TRUE;
3076#endif
3077
3078 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3079 ptr = line;
3080
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003081#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003082 if (has_spell)
3083 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003084 /* For checking first word with a capital skip white space. */
3085 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003086 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003087
Bram Moolenaar30abd282005-06-22 22:35:10 +00003088 /* To be able to spell-check over line boundaries copy the end of the
3089 * current line into nextline[]. Above the start of the next line was
3090 * copied to nextline[SPWORDLEN]. */
3091 if (nextline[SPWORDLEN] == NUL)
3092 {
3093 /* No next line or it is empty. */
3094 nextlinecol = MAXCOL;
3095 nextline_idx = 0;
3096 }
3097 else
3098 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003099 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003100 if (v < SPWORDLEN)
3101 {
3102 /* Short line, use it completely and append the start of the
3103 * next line. */
3104 nextlinecol = 0;
3105 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003106 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003107 nextline_idx = v + 1;
3108 }
3109 else
3110 {
3111 /* Long line, use only the last SPWORDLEN bytes. */
3112 nextlinecol = v - SPWORDLEN;
3113 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3114 nextline_idx = SPWORDLEN + 1;
3115 }
3116 }
3117 }
3118#endif
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 /* find start of trailing whitespace */
3121 if (wp->w_p_list && lcs_trail)
3122 {
3123 trailcol = (colnr_T)STRLEN(ptr);
3124 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3125 --trailcol;
3126 trailcol += (colnr_T) (ptr - line);
3127 extra_check = TRUE;
3128 }
3129
3130 /*
3131 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3132 * first character to be displayed.
3133 */
3134 if (wp->w_p_wrap)
3135 v = wp->w_skipcol;
3136 else
3137 v = wp->w_leftcol;
3138 if (v > 0)
3139 {
3140#ifdef FEAT_MBYTE
3141 char_u *prev_ptr = ptr;
3142#endif
3143 while (vcol < v && *ptr != NUL)
3144 {
3145 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3146 vcol += c;
3147#ifdef FEAT_MBYTE
3148 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003150 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 }
3152
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003153#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3154 /* When:
3155 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003156 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003157 * - 'virtualedit' is set, or
3158 * - the visual mode is active,
3159 * the end of the line may be before the start of the displayed part.
3160 */
3161 if (vcol < v && (
3162# ifdef FEAT_SYN_HL
3163 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003164 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003165# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3166 ||
3167# endif
3168# endif
3169# ifdef FEAT_VIRTUALEDIT
3170 virtual_active()
3171# ifdef FEAT_VISUAL
3172 ||
3173# endif
3174# endif
3175# ifdef FEAT_VISUAL
3176 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3177# endif
3178 ))
3179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#endif
3183
3184 /* Handle a character that's not completely on the screen: Put ptr at
3185 * that character but skip the first few screen characters. */
3186 if (vcol > v)
3187 {
3188 vcol -= c;
3189#ifdef FEAT_MBYTE
3190 ptr = prev_ptr;
3191#else
3192 --ptr;
3193#endif
3194 n_skip = v - vcol;
3195 }
3196
3197 /*
3198 * Adjust for when the inverted text is before the screen,
3199 * and when the start of the inverted text is before the screen.
3200 */
3201 if (tocol <= vcol)
3202 fromcol = 0;
3203 else if (fromcol >= 0 && fromcol < vcol)
3204 fromcol = vcol;
3205
3206#ifdef FEAT_LINEBREAK
3207 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3208 if (wp->w_p_wrap)
3209 need_showbreak = TRUE;
3210#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003211#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003212 /* When spell checking a word we need to figure out the start of the
3213 * word and if it's badly spelled or not. */
3214 if (has_spell)
3215 {
3216 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003217 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003218 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003219
3220 pos = wp->w_cursor;
3221 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003222 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003223 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003224
3225 /* spell_move_to() may call ml_get() and make "line" invalid */
3226 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3227 ptr = line + linecol;
3228
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003229 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003230 {
3231 /* no bad word found at line start, don't check until end of a
3232 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003233 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003234 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003235 }
3236 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003237 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003238 /* bad word found, use attributes until end of word */
3239 word_end = wp->w_cursor.col + len + 1;
3240
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003241 /* Turn index into actual attributes. */
3242 if (spell_hlf != HLF_COUNT)
3243 spell_attr = highlight_attr[spell_hlf];
3244 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003245 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003246
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003247# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003248 /* Need to restart syntax highlighting for this line. */
3249 if (has_syntax)
3250 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003251# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003252 }
3253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 }
3255
3256 /*
3257 * Correct highlighting for cursor that can't be disabled.
3258 * Avoids having to check this for each character.
3259 */
3260 if (fromcol >= 0)
3261 {
3262 if (noinvcur)
3263 {
3264 if ((colnr_T)fromcol == wp->w_virtcol)
3265 {
3266 /* highlighting starts at cursor, let it start just after the
3267 * cursor */
3268 fromcol_prev = fromcol;
3269 fromcol = -1;
3270 }
3271 else if ((colnr_T)fromcol < wp->w_virtcol)
3272 /* restart highlighting after the cursor */
3273 fromcol_prev = wp->w_virtcol;
3274 }
3275 if (fromcol >= tocol)
3276 fromcol = -1;
3277 }
3278
3279#ifdef FEAT_SEARCH_EXTRA
3280 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003281 * Handle highlighting the last used search pattern and matches.
3282 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003284 cur = wp->w_match_head;
3285 shl_flag = FALSE;
3286 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003288 if (shl_flag == FALSE)
3289 {
3290 shl = &search_hl;
3291 shl_flag = TRUE;
3292 }
3293 else
3294 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003295 shl->startcol = MAXCOL;
3296 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 shl->attr_cur = 0;
3298 if (shl->rm.regprog != NULL)
3299 {
3300 v = (long)(ptr - line);
3301 next_search_hl(wp, shl, lnum, (colnr_T)v);
3302
3303 /* Need to get the line again, a multi-line regexp may have made it
3304 * invalid. */
3305 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3306 ptr = line + v;
3307
3308 if (shl->lnum != 0 && shl->lnum <= lnum)
3309 {
3310 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003311 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003313 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3315 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003316 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003318 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003320 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
3322#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003323 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003324 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 else
3326#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003327 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003329 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
3331 shl->attr_cur = shl->attr;
3332 search_attr = shl->attr;
3333 }
3334 area_highlighting = TRUE;
3335 }
3336 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003337 if (shl != &search_hl && cur != NULL)
3338 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 }
3340#endif
3341
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003342#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003343 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3344 * active, because it's not clear what is selected then. */
3345 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003346 {
3347 line_attr = hl_attr(HLF_CUL);
3348 area_highlighting = TRUE;
3349 }
3350#endif
3351
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003352 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 col = 0;
3354#ifdef FEAT_RIGHTLEFT
3355 if (wp->w_p_rl)
3356 {
3357 /* Rightleft window: process the text in the normal direction, but put
3358 * it in current_ScreenLine[] from right to left. Start at the
3359 * rightmost column of the window. */
3360 col = W_WIDTH(wp) - 1;
3361 off += col;
3362 }
3363#endif
3364
3365 /*
3366 * Repeat for the whole displayed line.
3367 */
3368 for (;;)
3369 {
3370 /* Skip this quickly when working on the text. */
3371 if (draw_state != WL_LINE)
3372 {
3373#ifdef FEAT_CMDWIN
3374 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3375 {
3376 draw_state = WL_CMDLINE;
3377 if (cmdwin_type != 0 && wp == curwin)
3378 {
3379 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003381 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 char_attr = hl_attr(HLF_AT);
3383 }
3384 }
3385#endif
3386
3387#ifdef FEAT_FOLDING
3388 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3389 {
3390 draw_state = WL_FOLD;
3391 if (wp->w_p_fdc > 0)
3392 {
3393 /* Draw the 'foldcolumn'. */
3394 fill_foldcolumn(extra, wp, FALSE, lnum);
3395 n_extra = wp->w_p_fdc;
3396 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003397 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 c_extra = NUL;
3399 char_attr = hl_attr(HLF_FC);
3400 }
3401 }
3402#endif
3403
3404#ifdef FEAT_SIGNS
3405 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3406 {
3407 draw_state = WL_SIGN;
3408 /* Show the sign column when there are any signs in this
3409 * buffer or when using Netbeans. */
3410 if (draw_signcolumn(wp)
3411# ifdef FEAT_DIFF
3412 && filler_todo <= 0
3413# endif
3414 )
3415 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003416 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003418 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419# endif
3420
3421 /* Draw two cells with the sign value or blank. */
3422 c_extra = ' ';
3423 char_attr = hl_attr(HLF_SC);
3424 n_extra = 2;
3425
3426 if (row == startrow)
3427 {
3428 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3429 SIGN_TEXT);
3430# ifdef FEAT_SIGN_ICONS
3431 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3432 SIGN_ICON);
3433 if (gui.in_use && icon_sign != 0)
3434 {
3435 /* Use the image in this position. */
3436 c_extra = SIGN_BYTE;
3437# ifdef FEAT_NETBEANS_INTG
3438 if (buf_signcount(wp->w_buffer, lnum) > 1)
3439 c_extra = MULTISIGN_BYTE;
3440# endif
3441 char_attr = icon_sign;
3442 }
3443 else
3444# endif
3445 if (text_sign != 0)
3446 {
3447 p_extra = sign_get_text(text_sign);
3448 if (p_extra != NULL)
3449 {
3450 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003451 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453 char_attr = sign_get_attr(text_sign, FALSE);
3454 }
3455 }
3456 }
3457 }
3458#endif
3459
3460 if (draw_state == WL_NR - 1 && n_extra == 0)
3461 {
3462 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003463 /* Display the absolute or relative line number. After the
3464 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3465 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 && (row == startrow
3467#ifdef FEAT_DIFF
3468 + filler_lines
3469#endif
3470 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3471 {
3472 /* Draw the line number (empty space after wrapping). */
3473 if (row == startrow
3474#ifdef FEAT_DIFF
3475 + filler_lines
3476#endif
3477 )
3478 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003479 long num;
3480
3481 if (wp->w_p_nu)
3482 /* 'number' */
3483 num = (long)lnum;
3484 else
3485 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003486 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02003487
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003488 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003489 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (wp->w_skipcol > 0)
3491 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3492 *p_extra = '-';
3493#ifdef FEAT_RIGHTLEFT
3494 if (wp->w_p_rl) /* reverse line numbers */
3495 rl_mirror(extra);
3496#endif
3497 p_extra = extra;
3498 c_extra = NUL;
3499 }
3500 else
3501 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003502 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003504#ifdef FEAT_SYN_HL
3505 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003506 * the current line differently.
3507 * TODO: Can we use CursorLine instead of CursorLineNr
3508 * when CursorLineNr isn't set? */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003509 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003510 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513 }
3514
3515#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3516 if (draw_state == WL_SBR - 1 && n_extra == 0)
3517 {
3518 draw_state = WL_SBR;
3519# ifdef FEAT_DIFF
3520 if (filler_todo > 0)
3521 {
3522 /* Draw "deleted" diff line(s). */
3523 if (char2cells(fill_diff) > 1)
3524 c_extra = '-';
3525 else
3526 c_extra = fill_diff;
3527# ifdef FEAT_RIGHTLEFT
3528 if (wp->w_p_rl)
3529 n_extra = col + 1;
3530 else
3531# endif
3532 n_extra = W_WIDTH(wp) - col;
3533 char_attr = hl_attr(HLF_DED);
3534 }
3535# endif
3536# ifdef FEAT_LINEBREAK
3537 if (*p_sbr != NUL && need_showbreak)
3538 {
3539 /* Draw 'showbreak' at the start of each broken line. */
3540 p_extra = p_sbr;
3541 c_extra = NUL;
3542 n_extra = (int)STRLEN(p_sbr);
3543 char_attr = hl_attr(HLF_AT);
3544 need_showbreak = FALSE;
3545 /* Correct end of highlighted area for 'showbreak',
3546 * required when 'linebreak' is also set. */
3547 if (tocol == vcol)
3548 tocol += n_extra;
3549 }
3550# endif
3551 }
3552#endif
3553
3554 if (draw_state == WL_LINE - 1 && n_extra == 0)
3555 {
3556 draw_state = WL_LINE;
3557 if (saved_n_extra)
3558 {
3559 /* Continue item from end of wrapped line. */
3560 n_extra = saved_n_extra;
3561 c_extra = saved_c_extra;
3562 p_extra = saved_p_extra;
3563 char_attr = saved_char_attr;
3564 }
3565 else
3566 char_attr = 0;
3567 }
3568 }
3569
3570 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003571 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003572 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573#ifdef FEAT_DIFF
3574 && filler_todo <= 0
3575#endif
3576 )
3577 {
3578 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3579 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003580 /* Pretend we have finished updating the window. Except when
3581 * 'cursorcolumn' is set. */
3582#ifdef FEAT_SYN_HL
3583 if (wp->w_p_cuc)
3584 row = wp->w_cline_row + wp->w_cline_height;
3585 else
3586#endif
3587 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 break;
3589 }
3590
3591 if (draw_state == WL_LINE && area_highlighting)
3592 {
3593 /* handle Visual or match highlighting in this line */
3594 if (vcol == fromcol
3595#ifdef FEAT_MBYTE
3596 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3597 && (*mb_ptr2cells)(ptr) > 1)
3598#endif
3599 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003600 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 && vcol < tocol))
3602 area_attr = attr; /* start highlighting */
3603 else if (area_attr != 0
3604 && (vcol == tocol
3605 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
3608#ifdef FEAT_SEARCH_EXTRA
3609 if (!n_extra)
3610 {
3611 /*
3612 * Check for start/end of search pattern match.
3613 * After end, check for start/end of next match.
3614 * When another match, have to check for start again.
3615 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003616 * Do this for 'search_hl' and the match list (ordered by
3617 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003619 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003620 cur = wp->w_match_head;
3621 shl_flag = FALSE;
3622 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003624 if (shl_flag == FALSE
3625 && ((cur != NULL
3626 && cur->priority > SEARCH_HL_PRIORITY)
3627 || cur == NULL))
3628 {
3629 shl = &search_hl;
3630 shl_flag = TRUE;
3631 }
3632 else
3633 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 while (shl->rm.regprog != NULL)
3635 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003636 if (shl->startcol != MAXCOL
3637 && v >= (long)shl->startcol
3638 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
3640 shl->attr_cur = shl->attr;
3641 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003642 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 shl->attr_cur = 0;
3645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 next_search_hl(wp, shl, lnum, (colnr_T)v);
3647
3648 /* Need to get the line again, a multi-line regexp
3649 * may have made it invalid. */
3650 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3651 ptr = line + v;
3652
3653 if (shl->lnum == lnum)
3654 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003655 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003657 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003659 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003661 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 /* highlight empty match, try again after
3664 * it */
3665#ifdef FEAT_MBYTE
3666 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003667 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003668 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 else
3670#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003671 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673
3674 /* Loop to check if the match starts at the
3675 * current position */
3676 continue;
3677 }
3678 }
3679 break;
3680 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003681 if (shl != &search_hl && cur != NULL)
3682 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003684
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003685 /* Use attributes from match with highest priority among
3686 * 'search_hl' and the match list. */
3687 search_attr = search_hl.attr_cur;
3688 cur = wp->w_match_head;
3689 shl_flag = FALSE;
3690 while (cur != NULL || shl_flag == FALSE)
3691 {
3692 if (shl_flag == FALSE
3693 && ((cur != NULL
3694 && cur->priority > SEARCH_HL_PRIORITY)
3695 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003696 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003697 shl = &search_hl;
3698 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003699 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003700 else
3701 shl = &cur->hl;
3702 if (shl->attr_cur != 0)
3703 search_attr = shl->attr_cur;
3704 if (shl != &search_hl && cur != NULL)
3705 cur = cur->next;
3706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708#endif
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003711 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003713 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3714 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003716 if (diff_hlf == HLF_TXD && ptr - line > change_end
3717 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003719 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003722
3723 /* Decide which of the highlight attributes to use. */
3724 attr_pri = TRUE;
3725 if (area_attr != 0)
3726 char_attr = area_attr;
3727 else if (search_attr != 0)
3728 char_attr = search_attr;
3729#ifdef LINE_ATTR
3730 /* Use line_attr when not in the Visual or 'incsearch' area
3731 * (area_attr may be 0 when "noinvcur" is set). */
3732 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003733 || vcol < fromcol || vcol_prev < fromcol_prev
3734 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003735 char_attr = line_attr;
3736#endif
3737 else
3738 {
3739 attr_pri = FALSE;
3740#ifdef FEAT_SYN_HL
3741 if (has_syntax)
3742 char_attr = syntax_attr;
3743 else
3744#endif
3745 char_attr = 0;
3746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748
3749 /*
3750 * Get the next character to put on the screen.
3751 */
3752 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003753 * The "p_extra" points to the extra stuff that is inserted to
3754 * represent special characters (non-printable stuff) and other
3755 * things. When all characters are the same, c_extra is used.
3756 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3757 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3759 */
3760 if (n_extra > 0)
3761 {
3762 if (c_extra != NUL)
3763 {
3764 c = c_extra;
3765#ifdef FEAT_MBYTE
3766 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3767 if (enc_utf8 && (*mb_char2len)(c) > 1)
3768 {
3769 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003770 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003771 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 else
3774 mb_utf8 = FALSE;
3775#endif
3776 }
3777 else
3778 {
3779 c = *p_extra;
3780#ifdef FEAT_MBYTE
3781 if (has_mbyte)
3782 {
3783 mb_c = c;
3784 if (enc_utf8)
3785 {
3786 /* If the UTF-8 character is more than one byte:
3787 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003788 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 mb_utf8 = FALSE;
3790 if (mb_l > n_extra)
3791 mb_l = 1;
3792 else if (mb_l > 1)
3793 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003794 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003796 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
3798 }
3799 else
3800 {
3801 /* if this is a DBCS character, put it in "mb_c" */
3802 mb_l = MB_BYTE2LEN(c);
3803 if (mb_l >= n_extra)
3804 mb_l = 1;
3805 else if (mb_l > 1)
3806 mb_c = (c << 8) + p_extra[1];
3807 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003808 if (mb_l == 0) /* at the NUL at end-of-line */
3809 mb_l = 1;
3810
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 /* If a double-width char doesn't fit display a '>' in the
3812 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003813 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814# ifdef FEAT_RIGHTLEFT
3815 wp->w_p_rl ? (col <= 0) :
3816# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003817 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 && (*mb_char2cells)(mb_c) == 2)
3819 {
3820 c = '>';
3821 mb_c = c;
3822 mb_l = 1;
3823 mb_utf8 = FALSE;
3824 multi_attr = hl_attr(HLF_AT);
3825 /* put the pointer back to output the double-width
3826 * character at the start of the next line. */
3827 ++n_extra;
3828 --p_extra;
3829 }
3830 else
3831 {
3832 n_extra -= mb_l - 1;
3833 p_extra += mb_l - 1;
3834 }
3835 }
3836#endif
3837 ++p_extra;
3838 }
3839 --n_extra;
3840 }
3841 else
3842 {
3843 /*
3844 * Get a character from the line itself.
3845 */
3846 c = *ptr;
3847#ifdef FEAT_MBYTE
3848 if (has_mbyte)
3849 {
3850 mb_c = c;
3851 if (enc_utf8)
3852 {
3853 /* If the UTF-8 character is more than one byte: Decode it
3854 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003855 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 mb_utf8 = FALSE;
3857 if (mb_l > 1)
3858 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003859 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 /* Overlong encoded ASCII or ASCII with composing char
3861 * is displayed normally, except a NUL. */
3862 if (mb_c < 0x80)
3863 c = mb_c;
3864 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003865
3866 /* At start of the line we can have a composing char.
3867 * Draw it as a space with a composing char. */
3868 if (utf_iscomposing(mb_c))
3869 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003870 int i;
3871
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003872 for (i = Screen_mco - 1; i > 0; --i)
3873 u8cc[i] = u8cc[i - 1];
3874 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003875 mb_c = ' ';
3876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 }
3878
3879 if ((mb_l == 1 && c >= 0x80)
3880 || (mb_l >= 1 && mb_c == 0)
3881 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003882# ifdef UNICODE16
3883 || mb_c >= 0x10000
3884# endif
3885 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
3887 /*
3888 * Illegal UTF-8 byte: display as <xx>.
3889 * Non-BMP character : display as ? or fullwidth ?.
3890 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003891# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
3895 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003896# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if (wp->w_p_rl) /* reverse */
3898 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003901# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 else if (utf_char2cells(mb_c) != 2)
3903 STRCPY(extra, "?");
3904 else
3905 /* 0xff1f in UTF-8: full-width '?' */
3906 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
3909 p_extra = extra;
3910 c = *p_extra;
3911 mb_c = mb_ptr2char_adv(&p_extra);
3912 mb_utf8 = (c >= 0x80);
3913 n_extra = (int)STRLEN(p_extra);
3914 c_extra = NUL;
3915 if (area_attr == 0 && search_attr == 0)
3916 {
3917 n_attr = n_extra + 1;
3918 extra_attr = hl_attr(HLF_8);
3919 saved_attr2 = char_attr; /* save current attr */
3920 }
3921 }
3922 else if (mb_l == 0) /* at the NUL at end-of-line */
3923 mb_l = 1;
3924#ifdef FEAT_ARABIC
3925 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3926 {
3927 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003928 int pc, pc1, nc;
3929 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931 /* The idea of what is the previous and next
3932 * character depends on 'rightleft'. */
3933 if (wp->w_p_rl)
3934 {
3935 pc = prev_c;
3936 pc1 = prev_c1;
3937 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003938 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 }
3940 else
3941 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003942 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003944 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 prev_c = mb_c;
3947
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003948 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 else
3951 prev_c = mb_c;
3952#endif
3953 }
3954 else /* enc_dbcs */
3955 {
3956 mb_l = MB_BYTE2LEN(c);
3957 if (mb_l == 0) /* at the NUL at end-of-line */
3958 mb_l = 1;
3959 else if (mb_l > 1)
3960 {
3961 /* We assume a second byte below 32 is illegal.
3962 * Hopefully this is OK for all double-byte encodings!
3963 */
3964 if (ptr[1] >= 32)
3965 mb_c = (c << 8) + ptr[1];
3966 else
3967 {
3968 if (ptr[1] == NUL)
3969 {
3970 /* head byte at end of line */
3971 mb_l = 1;
3972 transchar_nonprint(extra, c);
3973 }
3974 else
3975 {
3976 /* illegal tail byte */
3977 mb_l = 2;
3978 STRCPY(extra, "XX");
3979 }
3980 p_extra = extra;
3981 n_extra = (int)STRLEN(extra) - 1;
3982 c_extra = NUL;
3983 c = *p_extra++;
3984 if (area_attr == 0 && search_attr == 0)
3985 {
3986 n_attr = n_extra + 1;
3987 extra_attr = hl_attr(HLF_8);
3988 saved_attr2 = char_attr; /* save current attr */
3989 }
3990 mb_c = c;
3991 }
3992 }
3993 }
3994 /* If a double-width char doesn't fit display a '>' in the
3995 * last column; the character is displayed at the start of the
3996 * next line. */
3997 if ((
3998# ifdef FEAT_RIGHTLEFT
3999 wp->w_p_rl ? (col <= 0) :
4000# endif
4001 (col >= W_WIDTH(wp) - 1))
4002 && (*mb_char2cells)(mb_c) == 2)
4003 {
4004 c = '>';
4005 mb_c = c;
4006 mb_utf8 = FALSE;
4007 mb_l = 1;
4008 multi_attr = hl_attr(HLF_AT);
4009 /* Put pointer back so that the character will be
4010 * displayed at the start of the next line. */
4011 --ptr;
4012 }
4013 else if (*ptr != NUL)
4014 ptr += mb_l - 1;
4015
4016 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004017 * a '<' in the first column. Don't do this for unprintable
4018 * charactes. */
4019 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004022 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 c = ' ';
4024 if (area_attr == 0 && search_attr == 0)
4025 {
4026 n_attr = n_extra + 1;
4027 extra_attr = hl_attr(HLF_AT);
4028 saved_attr2 = char_attr; /* save current attr */
4029 }
4030 mb_c = c;
4031 mb_utf8 = FALSE;
4032 mb_l = 1;
4033 }
4034
4035 }
4036#endif
4037 ++ptr;
4038
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004039 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004040 if (wp->w_p_list && (c == 160
4041#ifdef FEAT_MBYTE
4042 || (mb_utf8 && mb_c == 160)
4043#endif
4044 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004045 {
4046 c = lcs_nbsp;
4047 if (area_attr == 0 && search_attr == 0)
4048 {
4049 n_attr = 1;
4050 extra_attr = hl_attr(HLF_8);
4051 saved_attr2 = char_attr; /* save current attr */
4052 }
4053#ifdef FEAT_MBYTE
4054 mb_c = c;
4055 if (enc_utf8 && (*mb_char2len)(c) > 1)
4056 {
4057 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004058 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004060 }
4061 else
4062 mb_utf8 = FALSE;
4063#endif
4064 }
4065
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (extra_check)
4067 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004068#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004069 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004070#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004071
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004072#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 /* Get syntax attribute, unless still at the start of the line
4074 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004075 v = (long)(ptr - line);
4076 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 /* Get the syntax attribute for the character. If there
4079 * is an error, disable syntax highlighting. */
4080 save_did_emsg = did_emsg;
4081 did_emsg = FALSE;
4082
Bram Moolenaar217ad922005-03-20 22:37:15 +00004083 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004084# ifdef FEAT_SPELL
4085 has_spell ? &can_spell :
4086# endif
4087 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004090 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004091 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004092 has_syntax = FALSE;
4093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 else
4095 did_emsg = save_did_emsg;
4096
4097 /* Need to get the line again, a multi-line regexp may
4098 * have made it invalid. */
4099 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4100 ptr = line + v;
4101
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004102 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004104 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004105 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004106# ifdef FEAT_CONCEAL
4107 /* no concealing past the end of the line, it interferes
4108 * with line highlighting */
4109 if (c == NUL)
4110 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004111 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004112 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004115#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004116
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004117#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004118 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004119 * Only do this when there is no syntax highlighting, the
4120 * @Spell cluster is not used or the current syntax item
4121 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004122 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004123 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004124 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004125# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004126 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004127 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004128# endif
4129 if (c != 0 && (
4130# ifdef FEAT_SYN_HL
4131 !has_syntax ||
4132# endif
4133 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004134 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004135 char_u *prev_ptr, *p;
4136 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004137 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004138# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004139 if (has_mbyte)
4140 {
4141 prev_ptr = ptr - mb_l;
4142 v -= mb_l - 1;
4143 }
4144 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004145# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004146 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004147
4148 /* Use nextline[] if possible, it has the start of the
4149 * next line concatenated. */
4150 if ((prev_ptr - line) - nextlinecol >= 0)
4151 p = nextline + (prev_ptr - line) - nextlinecol;
4152 else
4153 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004154 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004155 len = spell_check(wp, p, &spell_hlf, &cap_col,
4156 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004157 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004158
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004159 /* In Insert mode only highlight a word that
4160 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004161 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004162 && (State & INSERT) != 0
4163 && wp->w_cursor.lnum == lnum
4164 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004165 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004166 && wp->w_cursor.col < (colnr_T)word_end)
4167 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004168 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004169 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004170 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004171
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004172 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004173 && (p - nextline) + len > nextline_idx)
4174 {
4175 /* Remember that the good word continues at the
4176 * start of the next line. */
4177 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004178 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004179 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004180
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004181 /* Turn index into actual attributes. */
4182 if (spell_hlf != HLF_COUNT)
4183 spell_attr = highlight_attr[spell_hlf];
4184
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004185 if (cap_col > 0)
4186 {
4187 if (p != prev_ptr
4188 && (p - nextline) + cap_col >= nextline_idx)
4189 {
4190 /* Remember that the word in the next line
4191 * must start with a capital. */
4192 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004193 cap_col = (int)((p - nextline) + cap_col
4194 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004195 }
4196 else
4197 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004198 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004199 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004200 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004201 }
4202 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004203 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004204 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004205 char_attr = hl_combine_attr(char_attr, spell_attr);
4206 else
4207 char_attr = hl_combine_attr(spell_attr, char_attr);
4208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209#endif
4210#ifdef FEAT_LINEBREAK
4211 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004212 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
4214 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004215 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
4217 n_extra = win_lbr_chartabsize(wp, ptr - (
4218# ifdef FEAT_MBYTE
4219 has_mbyte ? mb_l :
4220# endif
4221 1), (colnr_T)vcol, NULL) - 1;
4222 c_extra = ' ';
4223 if (vim_iswhite(c))
4224 c = ' ';
4225 }
4226#endif
4227
4228 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4229 {
4230 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004231 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
4233 n_attr = 1;
4234 extra_attr = hl_attr(HLF_8);
4235 saved_attr2 = char_attr; /* save current attr */
4236 }
4237#ifdef FEAT_MBYTE
4238 mb_c = c;
4239 if (enc_utf8 && (*mb_char2len)(c) > 1)
4240 {
4241 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004242 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004243 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245 else
4246 mb_utf8 = FALSE;
4247#endif
4248 }
4249 }
4250
4251 /*
4252 * Handling of non-printable characters.
4253 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004254 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
4256 /*
4257 * when getting a character from the file, we may have to
4258 * turn it into something else on the way to putting it
4259 * into "ScreenLines".
4260 */
4261 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4262 {
4263 /* tab amount depends on current column */
4264 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar8a20b0f2011-08-10 14:32:39 +02004265 - VCOL_HLC % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266#ifdef FEAT_MBYTE
4267 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4268#endif
4269 if (wp->w_p_list)
4270 {
4271 c = lcs_tab1;
4272 c_extra = lcs_tab2;
4273 n_attr = n_extra + 1;
4274 extra_attr = hl_attr(HLF_8);
4275 saved_attr2 = char_attr; /* save current attr */
4276#ifdef FEAT_MBYTE
4277 mb_c = c;
4278 if (enc_utf8 && (*mb_char2len)(c) > 1)
4279 {
4280 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004281 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004282 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284#endif
4285 }
4286 else
4287 {
4288 c_extra = ' ';
4289 c = ' ';
4290 }
4291 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004292 else if (c == NUL
4293 && ((wp->w_p_list && lcs_eol > 0)
4294 || ((fromcol >= 0 || fromcol_prev >= 0)
4295 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004296#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004297 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004298#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004299 && (
4300# ifdef FEAT_RIGHTLEFT
4301 wp->w_p_rl ? (col >= 0) :
4302# endif
4303 (col < W_WIDTH(wp)))
4304 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004305 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004306 && (colnr_T)vcol == wp->w_virtcol)))
4307 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004309 /* Display a '$' after the line or highlight an extra
4310 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4312 /* For a diff line the highlighting continues after the
4313 * "$". */
4314 if (
4315# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004316 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317# ifdef LINE_ATTR
4318 &&
4319# endif
4320# endif
4321# ifdef LINE_ATTR
4322 line_attr == 0
4323# endif
4324 )
4325#endif
4326 {
4327#ifdef FEAT_VIRTUALEDIT
4328 /* In virtualedit, visual selections may extend
4329 * beyond end of line. */
4330 if (area_highlighting && virtual_active()
4331 && tocol != MAXCOL && vcol < tocol)
4332 n_extra = 0;
4333 else
4334#endif
4335 {
4336 p_extra = at_end_str;
4337 n_extra = 1;
4338 c_extra = NUL;
4339 }
4340 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004341 if (wp->w_p_list)
4342 c = lcs_eol;
4343 else
4344 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 lcs_eol_one = -1;
4346 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004347 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 extra_attr = hl_attr(HLF_AT);
4350 n_attr = 1;
4351 }
4352#ifdef FEAT_MBYTE
4353 mb_c = c;
4354 if (enc_utf8 && (*mb_char2len)(c) > 1)
4355 {
4356 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004357 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004358 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 else
4361 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4362#endif
4363 }
4364 else if (c != NUL)
4365 {
4366 p_extra = transchar(c);
4367#ifdef FEAT_RIGHTLEFT
4368 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4369 rl_mirror(p_extra); /* reverse "<12>" */
4370#endif
4371 n_extra = byte2cells(c) - 1;
4372 c_extra = NUL;
4373 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004374 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
4376 n_attr = n_extra + 1;
4377 extra_attr = hl_attr(HLF_8);
4378 saved_attr2 = char_attr; /* save current attr */
4379 }
4380#ifdef FEAT_MBYTE
4381 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4382#endif
4383 }
4384#ifdef FEAT_VIRTUALEDIT
4385 else if (VIsual_active
4386 && (VIsual_mode == Ctrl_V
4387 || VIsual_mode == 'v')
4388 && virtual_active()
4389 && tocol != MAXCOL
4390 && vcol < tocol
4391 && (
4392# ifdef FEAT_RIGHTLEFT
4393 wp->w_p_rl ? (col >= 0) :
4394# endif
4395 (col < W_WIDTH(wp))))
4396 {
4397 c = ' ';
4398 --ptr; /* put it back at the NUL */
4399 }
4400#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004401#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 else if ((
4403# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004404 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 ) && (
4408# ifdef FEAT_RIGHTLEFT
4409 wp->w_p_rl ? (col >= 0) :
4410# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004411 (col
4412# ifdef FEAT_CONCEAL
4413 - boguscols
4414# endif
4415 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
4417 /* Highlight until the right side of the window */
4418 c = ' ';
4419 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004420
4421 /* Remember we do the char for line highlighting. */
4422 ++did_line_attr;
4423
4424 /* don't do search HL for the rest of the line */
4425 if (line_attr != 0 && char_attr == search_attr && col > 0)
4426 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427# ifdef FEAT_DIFF
4428 if (diff_hlf == HLF_TXD)
4429 {
4430 diff_hlf = HLF_CHD;
4431 if (attr == 0 || char_attr != attr)
4432 char_attr = hl_attr(diff_hlf);
4433 }
4434# endif
4435 }
4436#endif
4437 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004438
4439#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004440 if ( wp->w_p_cole > 0
4441 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4442 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004443 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004444 && !(lnum_in_visual_area
4445 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004446 {
4447 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004448 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004449 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4450 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004451 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004452 /* First time at this concealed item: display one
4453 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004454 if (syn_get_sub_char() != NUL)
4455 c = syn_get_sub_char();
4456 else if (lcs_conceal != NUL)
4457 c = lcs_conceal;
4458 else
4459 c = ' ';
4460
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004461 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004462
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004463 if (n_extra > 0)
4464 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004465 vcol += n_extra;
4466 if (wp->w_p_wrap && n_extra > 0)
4467 {
4468# ifdef FEAT_RIGHTLEFT
4469 if (wp->w_p_rl)
4470 {
4471 col -= n_extra;
4472 boguscols -= n_extra;
4473 }
4474 else
4475# endif
4476 {
4477 boguscols += n_extra;
4478 col += n_extra;
4479 }
4480 }
4481 n_extra = 0;
4482 n_attr = 0;
4483 }
4484 else if (n_skip == 0)
4485 {
4486 is_concealing = TRUE;
4487 n_skip = 1;
4488 }
4489# ifdef FEAT_MBYTE
4490 mb_c = c;
4491 if (enc_utf8 && (*mb_char2len)(c) > 1)
4492 {
4493 mb_utf8 = TRUE;
4494 u8cc[0] = 0;
4495 c = 0xc0;
4496 }
4497 else
4498 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4499# endif
4500 }
4501 else
4502 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004503 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004504 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004505 }
4506#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004509#ifdef FEAT_CONCEAL
4510 /* In the cursor line and we may be concealing characters: correct
4511 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004512 if (!did_wcol && draw_state == WL_LINE
4513 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004514 && conceal_cursor_line(wp)
4515 && (int)wp->w_virtcol <= vcol + n_skip)
4516 {
4517 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004518 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004519 did_wcol = TRUE;
4520 }
4521#endif
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 /* Don't override visual selection highlighting. */
4524 if (n_attr > 0
4525 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004526 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 char_attr = extra_attr;
4528
Bram Moolenaar81695252004-12-29 20:58:21 +00004529#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /* XIM don't send preedit_start and preedit_end, but they send
4531 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4532 * im_is_preediting() here. */
4533 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004534 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 && (State & INSERT)
4536 && !p_imdisable
4537 && im_is_preediting()
4538 && draw_state == WL_LINE)
4539 {
4540 colnr_T tcol;
4541
4542 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004543 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 else
4545 tcol = preedit_end_col;
4546 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4547 {
4548 if (feedback_old_attr < 0)
4549 {
4550 feedback_col = 0;
4551 feedback_old_attr = char_attr;
4552 }
4553 char_attr = im_get_feedback_attr(feedback_col);
4554 if (char_attr < 0)
4555 char_attr = feedback_old_attr;
4556 feedback_col++;
4557 }
4558 else if (feedback_old_attr >= 0)
4559 {
4560 char_attr = feedback_old_attr;
4561 feedback_old_attr = -1;
4562 feedback_col = 0;
4563 }
4564 }
4565#endif
4566 /*
4567 * Handle the case where we are in column 0 but not on the first
4568 * character of the line and the user wants us to show us a
4569 * special character (via 'listchars' option "precedes:<char>".
4570 */
4571 if (lcs_prec_todo != NUL
4572 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4573#ifdef FEAT_DIFF
4574 && filler_todo <= 0
4575#endif
4576 && draw_state > WL_NR
4577 && c != NUL)
4578 {
4579 c = lcs_prec;
4580 lcs_prec_todo = NUL;
4581#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004582 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4583 {
4584 /* Double-width character being overwritten by the "precedes"
4585 * character, need to fill up half the character. */
4586 c_extra = MB_FILLER_CHAR;
4587 n_extra = 1;
4588 n_attr = 2;
4589 extra_attr = hl_attr(HLF_AT);
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 mb_c = c;
4592 if (enc_utf8 && (*mb_char2len)(c) > 1)
4593 {
4594 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004595 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004596 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 else
4599 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4600#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004601 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
4603 saved_attr3 = char_attr; /* save current attr */
4604 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4605 n_attr3 = 1;
4606 }
4607 }
4608
4609 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004610 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004612 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004613#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004614 || did_line_attr == 1
4615#endif
4616 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004618#ifdef FEAT_SEARCH_EXTRA
4619 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004620
4621 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004622 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004623 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004624#endif
4625
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004626 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 * highlight match at end of line. If it's beyond the last
4628 * char on the screen, just overwrite that one (tricky!) Not
4629 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004630#ifdef FEAT_SEARCH_EXTRA
4631 prevcol_hl_flag = FALSE;
4632 if (prevcol == (long)search_hl.startcol)
4633 prevcol_hl_flag = TRUE;
4634 else
4635 {
4636 cur = wp->w_match_head;
4637 while (cur != NULL)
4638 {
4639 if (prevcol == (long)cur->hl.startcol)
4640 {
4641 prevcol_hl_flag = TRUE;
4642 break;
4643 }
4644 cur = cur->next;
4645 }
4646 }
4647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004649 && ((area_attr != 0 && vcol == fromcol
4650#ifdef FEAT_VISUAL
4651 && (VIsual_mode != Ctrl_V
4652 || lnum == VIsual.lnum
4653 || lnum == curwin->w_cursor.lnum)
4654#endif
4655 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#ifdef FEAT_SEARCH_EXTRA
4657 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004658 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004659# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004660 && did_line_attr <= 1
4661# endif
4662 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#endif
4664 ))
4665 {
4666 int n = 0;
4667
4668#ifdef FEAT_RIGHTLEFT
4669 if (wp->w_p_rl)
4670 {
4671 if (col < 0)
4672 n = 1;
4673 }
4674 else
4675#endif
4676 {
4677 if (col >= W_WIDTH(wp))
4678 n = -1;
4679 }
4680 if (n != 0)
4681 {
4682 /* At the window boundary, highlight the last character
4683 * instead (better than nothing). */
4684 off += n;
4685 col += n;
4686 }
4687 else
4688 {
4689 /* Add a blank character to highlight. */
4690 ScreenLines[off] = ' ';
4691#ifdef FEAT_MBYTE
4692 if (enc_utf8)
4693 ScreenLinesUC[off] = 0;
4694#endif
4695 }
4696#ifdef FEAT_SEARCH_EXTRA
4697 if (area_attr == 0)
4698 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004699 /* Use attributes from match with highest priority among
4700 * 'search_hl' and the match list. */
4701 char_attr = search_hl.attr;
4702 cur = wp->w_match_head;
4703 shl_flag = FALSE;
4704 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004705 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004706 if (shl_flag == FALSE
4707 && ((cur != NULL
4708 && cur->priority > SEARCH_HL_PRIORITY)
4709 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004710 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004711 shl = &search_hl;
4712 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004713 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004714 else
4715 shl = &cur->hl;
4716 if ((ptr - line) - 1 == (long)shl->startcol)
4717 char_attr = shl->attr;
4718 if (shl != &search_hl && cur != NULL)
4719 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722#endif
4723 ScreenAttrs[off] = char_attr;
4724#ifdef FEAT_RIGHTLEFT
4725 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004728 --off;
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 else
4731#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004732 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004734 ++off;
4735 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004736 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004737#ifdef FEAT_SYN_HL
4738 eol_hl_off = 1;
4739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaar91170f82006-05-05 21:15:17 +00004743 /*
4744 * At end of the text line.
4745 */
4746 if (c == NUL)
4747 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004748#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004749 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4750 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004751 {
4752 /* highlight last char after line */
4753 --col;
4754 --off;
4755 --vcol;
4756 }
4757
Bram Moolenaar1a384422010-07-14 19:53:30 +02004758 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004759 if (wp->w_p_wrap)
4760 v = wp->w_skipcol;
4761 else
4762 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004763
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004764 /* check if line ends before left margin */
4765 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004766 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004767#ifdef FEAT_CONCEAL
4768 /* Get rid of the boguscols now, we want to draw until the right
4769 * edge for 'cursorcolumn'. */
4770 col -= boguscols;
4771 boguscols = 0;
4772#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004773
4774 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004775 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004776
4777 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004778 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4779 && (int)wp->w_virtcol <
4780 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004781 && lnum != wp->w_cursor.lnum)
4782 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004783# ifdef FEAT_RIGHTLEFT
4784 && !wp->w_p_rl
4785# endif
4786 )
4787 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004788 int rightmost_vcol = 0;
4789 int i;
4790
4791 if (wp->w_p_cuc)
4792 rightmost_vcol = wp->w_virtcol;
4793 if (draw_color_col)
4794 /* determine rightmost colorcolumn to possibly draw */
4795 for (i = 0; color_cols[i] >= 0; ++i)
4796 if (rightmost_vcol < color_cols[i])
4797 rightmost_vcol = color_cols[i];
4798
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004799 while (col < W_WIDTH(wp))
4800 {
4801 ScreenLines[off] = ' ';
4802#ifdef FEAT_MBYTE
4803 if (enc_utf8)
4804 ScreenLinesUC[off] = 0;
4805#endif
4806 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004807 if (draw_color_col)
4808 draw_color_col = advance_color_col(VCOL_HLC,
4809 &color_cols);
4810
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004811 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004812 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004813 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004814 ScreenAttrs[off++] = hl_attr(HLF_MC);
4815 else
4816 ScreenAttrs[off++] = 0;
4817
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004818 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004819 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004820
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004821 ++vcol;
4822 }
4823 }
4824#endif
4825
Bram Moolenaar860cae12010-06-05 23:22:07 +02004826 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4827 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 row++;
4829
4830 /*
4831 * Update w_cline_height and w_cline_folded if the cursor line was
4832 * updated (saves a call to plines() later).
4833 */
4834 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4835 {
4836 curwin->w_cline_row = startrow;
4837 curwin->w_cline_height = row - startrow;
4838#ifdef FEAT_FOLDING
4839 curwin->w_cline_folded = FALSE;
4840#endif
4841 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4842 }
4843
4844 break;
4845 }
4846
4847 /* line continues beyond line end */
4848 if (lcs_ext
4849 && !wp->w_p_wrap
4850#ifdef FEAT_DIFF
4851 && filler_todo <= 0
4852#endif
4853 && (
4854#ifdef FEAT_RIGHTLEFT
4855 wp->w_p_rl ? col == 0 :
4856#endif
4857 col == W_WIDTH(wp) - 1)
4858 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004859 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4861 {
4862 c = lcs_ext;
4863 char_attr = hl_attr(HLF_AT);
4864#ifdef FEAT_MBYTE
4865 mb_c = c;
4866 if (enc_utf8 && (*mb_char2len)(c) > 1)
4867 {
4868 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004869 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004870 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 else
4873 mb_utf8 = FALSE;
4874#endif
4875 }
4876
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004877#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004878 /* advance to the next 'colorcolumn' */
4879 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004880 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004881
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004882 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004883 * highlight the cursor position itself.
4884 * Also highlight the 'colorcolumn' if it is different than
4885 * 'cursorcolumn' */
4886 vcol_save_attr = -1;
4887 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004888 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004889 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004890 && lnum != wp->w_cursor.lnum)
4891 {
4892 vcol_save_attr = char_attr;
4893 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4894 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004895 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004896 {
4897 vcol_save_attr = char_attr;
4898 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4899 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004900 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004901#endif
4902
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 /*
4904 * Store character to be displayed.
4905 * Skip characters that are left of the screen for 'nowrap'.
4906 */
4907 vcol_prev = vcol;
4908 if (draw_state < WL_LINE || n_skip <= 0)
4909 {
4910 /*
4911 * Store the character.
4912 */
4913#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4914 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4915 {
4916 /* A double-wide character is: put first halve in left cell. */
4917 --off;
4918 --col;
4919 }
4920#endif
4921 ScreenLines[off] = c;
4922#ifdef FEAT_MBYTE
4923 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004924 {
4925 if ((mb_c & 0xff00) == 0x8e00)
4926 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 else if (enc_utf8)
4930 {
4931 if (mb_utf8)
4932 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004933 int i;
4934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004936 if ((c & 0xff) == 0)
4937 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004938 for (i = 0; i < Screen_mco; ++i)
4939 {
4940 ScreenLinesC[i][off] = u8cc[i];
4941 if (u8cc[i] == 0)
4942 break;
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945 else
4946 ScreenLinesUC[off] = 0;
4947 }
4948 if (multi_attr)
4949 {
4950 ScreenAttrs[off] = multi_attr;
4951 multi_attr = 0;
4952 }
4953 else
4954#endif
4955 ScreenAttrs[off] = char_attr;
4956
4957#ifdef FEAT_MBYTE
4958 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4959 {
4960 /* Need to fill two screen columns. */
4961 ++off;
4962 ++col;
4963 if (enc_utf8)
4964 /* UTF-8: Put a 0 in the second screen char. */
4965 ScreenLines[off] = 0;
4966 else
4967 /* DBCS: Put second byte in the second screen char. */
4968 ScreenLines[off] = mb_c & 0xff;
4969 ++vcol;
4970 /* When "tocol" is halfway a character, set it to the end of
4971 * the character, otherwise highlighting won't stop. */
4972 if (tocol == vcol)
4973 ++tocol;
4974#ifdef FEAT_RIGHTLEFT
4975 if (wp->w_p_rl)
4976 {
4977 /* now it's time to backup one cell */
4978 --off;
4979 --col;
4980 }
4981#endif
4982 }
4983#endif
4984#ifdef FEAT_RIGHTLEFT
4985 if (wp->w_p_rl)
4986 {
4987 --off;
4988 --col;
4989 }
4990 else
4991#endif
4992 {
4993 ++off;
4994 ++col;
4995 }
4996 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004997#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004998 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004999 {
5000 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005001 ++vcol_off;
5002 if (n_extra > 0)
5003 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005004 if (wp->w_p_wrap)
5005 {
5006 /*
5007 * Special voodoo required if 'wrap' is on.
5008 *
5009 * Advance the column indicator to force the line
5010 * drawing to wrap early. This will make the line
5011 * take up the same screen space when parts are concealed,
5012 * so that cursor line computations aren't messed up.
5013 *
5014 * To avoid the fictitious advance of 'col' causing
5015 * trailing junk to be written out of the screen line
5016 * we are building, 'boguscols' keeps track of the number
5017 * of bad columns we have advanced.
5018 */
5019 if (n_extra > 0)
5020 {
5021 vcol += n_extra;
5022# ifdef FEAT_RIGHTLEFT
5023 if (wp->w_p_rl)
5024 {
5025 col -= n_extra;
5026 boguscols -= n_extra;
5027 }
5028 else
5029# endif
5030 {
5031 col += n_extra;
5032 boguscols += n_extra;
5033 }
5034 n_extra = 0;
5035 n_attr = 0;
5036 }
5037
5038
5039# ifdef FEAT_MBYTE
5040 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5041 {
5042 /* Need to fill two screen columns. */
5043# ifdef FEAT_RIGHTLEFT
5044 if (wp->w_p_rl)
5045 {
5046 --boguscols;
5047 --col;
5048 }
5049 else
5050# endif
5051 {
5052 ++boguscols;
5053 ++col;
5054 }
5055 }
5056# endif
5057
5058# ifdef FEAT_RIGHTLEFT
5059 if (wp->w_p_rl)
5060 {
5061 --boguscols;
5062 --col;
5063 }
5064 else
5065# endif
5066 {
5067 ++boguscols;
5068 ++col;
5069 }
5070 }
5071 else
5072 {
5073 if (n_extra > 0)
5074 {
5075 vcol += n_extra;
5076 n_extra = 0;
5077 n_attr = 0;
5078 }
5079 }
5080
5081 }
5082#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else
5084 --n_skip;
5085
Bram Moolenaar64486672010-05-16 15:46:46 +02005086 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5087 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005088 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089#ifdef FEAT_DIFF
5090 && filler_todo <= 0
5091#endif
5092 )
5093 ++vcol;
5094
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005095#ifdef FEAT_SYN_HL
5096 if (vcol_save_attr >= 0)
5097 char_attr = vcol_save_attr;
5098#endif
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 /* restore attributes after "predeces" in 'listchars' */
5101 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5102 char_attr = saved_attr3;
5103
5104 /* restore attributes after last 'listchars' or 'number' char */
5105 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5106 char_attr = saved_attr2;
5107
5108 /*
5109 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005110 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 */
5112 if ((
5113#ifdef FEAT_RIGHTLEFT
5114 wp->w_p_rl ? (col < 0) :
5115#endif
5116 (col >= W_WIDTH(wp)))
5117 && (*ptr != NUL
5118#ifdef FEAT_DIFF
5119 || filler_todo > 0
5120#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005121 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5123 )
5124 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005125#ifdef FEAT_CONCEAL
5126 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5127 (int)W_WIDTH(wp), wp->w_p_rl);
5128 boguscols = 0;
5129#else
5130 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5131 (int)W_WIDTH(wp), wp->w_p_rl);
5132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 ++row;
5134 ++screen_row;
5135
5136 /* When not wrapping and finished diff lines, or when displayed
5137 * '$' and highlighting until last column, break here. */
5138 if ((!wp->w_p_wrap
5139#ifdef FEAT_DIFF
5140 && filler_todo <= 0
5141#endif
5142 ) || lcs_eol_one == -1)
5143 break;
5144
5145 /* When the window is too narrow draw all "@" lines. */
5146 if (draw_state != WL_LINE
5147#ifdef FEAT_DIFF
5148 && filler_todo <= 0
5149#endif
5150 )
5151 {
5152 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5153#ifdef FEAT_VERTSPLIT
5154 draw_vsep_win(wp, row);
5155#endif
5156 row = endrow;
5157 }
5158
5159 /* When line got too long for screen break here. */
5160 if (row == endrow)
5161 {
5162 ++row;
5163 break;
5164 }
5165
5166 if (screen_cur_row == screen_row - 1
5167#ifdef FEAT_DIFF
5168 && filler_todo <= 0
5169#endif
5170 && W_WIDTH(wp) == Columns)
5171 {
5172 /* Remember that the line wraps, used for modeless copy. */
5173 LineWraps[screen_row - 1] = TRUE;
5174
5175 /*
5176 * Special trick to make copy/paste of wrapped lines work with
5177 * xterm/screen: write an extra character beyond the end of
5178 * the line. This will work with all terminal types
5179 * (regardless of the xn,am settings).
5180 * Only do this on a fast tty.
5181 * Only do this if the cursor is on the current line
5182 * (something has been written in it).
5183 * Don't do this for the GUI.
5184 * Don't do this for double-width characters.
5185 * Don't do this for a window not at the right screen border.
5186 */
5187 if (p_tf
5188#ifdef FEAT_GUI
5189 && !gui.in_use
5190#endif
5191#ifdef FEAT_MBYTE
5192 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005193 && ((*mb_off2cells)(LineOffset[screen_row],
5194 LineOffset[screen_row] + screen_Columns)
5195 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005197 + (int)Columns - 2,
5198 LineOffset[screen_row] + screen_Columns)
5199 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200#endif
5201 )
5202 {
5203 /* First make sure we are at the end of the screen line,
5204 * then output the same character again to let the
5205 * terminal know about the wrap. If the terminal doesn't
5206 * auto-wrap, we overwrite the character. */
5207 if (screen_cur_col != W_WIDTH(wp))
5208 screen_char(LineOffset[screen_row - 1]
5209 + (unsigned)Columns - 1,
5210 screen_row - 1, (int)(Columns - 1));
5211
5212#ifdef FEAT_MBYTE
5213 /* When there is a multi-byte character, just output a
5214 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005215 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5216 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 out_char(' ');
5218 else
5219#endif
5220 out_char(ScreenLines[LineOffset[screen_row - 1]
5221 + (Columns - 1)]);
5222 /* force a redraw of the first char on the next line */
5223 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5224 screen_start(); /* don't know where cursor is now */
5225 }
5226 }
5227
5228 col = 0;
5229 off = (unsigned)(current_ScreenLine - ScreenLines);
5230#ifdef FEAT_RIGHTLEFT
5231 if (wp->w_p_rl)
5232 {
5233 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5234 off += col;
5235 }
5236#endif
5237
5238 /* reset the drawing state for the start of a wrapped line */
5239 draw_state = WL_START;
5240 saved_n_extra = n_extra;
5241 saved_p_extra = p_extra;
5242 saved_c_extra = c_extra;
5243 saved_char_attr = char_attr;
5244 n_extra = 0;
5245 lcs_prec_todo = lcs_prec;
5246#ifdef FEAT_LINEBREAK
5247# ifdef FEAT_DIFF
5248 if (filler_todo <= 0)
5249# endif
5250 need_showbreak = TRUE;
5251#endif
5252#ifdef FEAT_DIFF
5253 --filler_todo;
5254 /* When the filler lines are actually below the last line of the
5255 * file, don't draw the line itself, break here. */
5256 if (filler_todo == 0 && wp->w_botfill)
5257 break;
5258#endif
5259 }
5260
5261 } /* for every character in the line */
5262
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005263#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005264 /* After an empty line check first word for capital. */
5265 if (*skipwhite(line) == NUL)
5266 {
5267 capcol_lnum = lnum + 1;
5268 cap_col = 0;
5269 }
5270#endif
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 return row;
5273}
5274
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005275#ifdef FEAT_MBYTE
5276static int comp_char_differs __ARGS((int, int));
5277
5278/*
5279 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005280 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005281 */
5282 static int
5283comp_char_differs(off_from, off_to)
5284 int off_from;
5285 int off_to;
5286{
5287 int i;
5288
5289 for (i = 0; i < Screen_mco; ++i)
5290 {
5291 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5292 return TRUE;
5293 if (ScreenLinesC[i][off_from] == 0)
5294 break;
5295 }
5296 return FALSE;
5297}
5298#endif
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300/*
5301 * Check whether the given character needs redrawing:
5302 * - the (first byte of the) character is different
5303 * - the attributes are different
5304 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005305 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 */
5307 static int
5308char_needs_redraw(off_from, off_to, cols)
5309 int off_from;
5310 int off_to;
5311 int cols;
5312{
5313 if (cols > 0
5314 && ((ScreenLines[off_from] != ScreenLines[off_to]
5315 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5316
5317#ifdef FEAT_MBYTE
5318 || (enc_dbcs != 0
5319 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5320 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5321 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5322 : (cols > 1 && ScreenLines[off_from + 1]
5323 != ScreenLines[off_to + 1])))
5324 || (enc_utf8
5325 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5326 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005327 && comp_char_differs(off_from, off_to))
5328 || (cols > 1 && ScreenLines[off_from + 1]
5329 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330#endif
5331 ))
5332 return TRUE;
5333 return FALSE;
5334}
5335
5336/*
5337 * Move one "cooked" screen line to the screen, but only the characters that
5338 * have actually changed. Handle insert/delete character.
5339 * "coloff" gives the first column on the screen for this line.
5340 * "endcol" gives the columns where valid characters are.
5341 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5342 * needs to be cleared, negative otherwise.
5343 * "rlflag" is TRUE in a rightleft window:
5344 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5345 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5346 */
5347 static void
5348screen_line(row, coloff, endcol, clear_width
5349#ifdef FEAT_RIGHTLEFT
5350 , rlflag
5351#endif
5352 )
5353 int row;
5354 int coloff;
5355 int endcol;
5356 int clear_width;
5357#ifdef FEAT_RIGHTLEFT
5358 int rlflag;
5359#endif
5360{
5361 unsigned off_from;
5362 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005363#ifdef FEAT_MBYTE
5364 unsigned max_off_from;
5365 unsigned max_off_to;
5366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 int col = 0;
5368#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5369 int hl;
5370#endif
5371 int force = FALSE; /* force update rest of the line */
5372 int redraw_this /* bool: does character need redraw? */
5373#ifdef FEAT_GUI
5374 = TRUE /* For GUI when while-loop empty */
5375#endif
5376 ;
5377 int redraw_next; /* redraw_this for next character */
5378#ifdef FEAT_MBYTE
5379 int clear_next = FALSE;
5380 int char_cells; /* 1: normal char */
5381 /* 2: occupies two display cells */
5382# define CHAR_CELLS char_cells
5383#else
5384# define CHAR_CELLS 1
5385#endif
5386
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005387 /* Check for illegal row and col, just in case. */
5388 if (row >= Rows)
5389 row = Rows - 1;
5390 if (endcol > Columns)
5391 endcol = Columns;
5392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393# ifdef FEAT_CLIPBOARD
5394 clip_may_clear_selection(row, row);
5395# endif
5396
5397 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5398 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005399#ifdef FEAT_MBYTE
5400 max_off_from = off_from + screen_Columns;
5401 max_off_to = LineOffset[row] + screen_Columns;
5402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403
5404#ifdef FEAT_RIGHTLEFT
5405 if (rlflag)
5406 {
5407 /* Clear rest first, because it's left of the text. */
5408 if (clear_width > 0)
5409 {
5410 while (col <= endcol && ScreenLines[off_to] == ' '
5411 && ScreenAttrs[off_to] == 0
5412# ifdef FEAT_MBYTE
5413 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5414# endif
5415 )
5416 {
5417 ++off_to;
5418 ++col;
5419 }
5420 if (col <= endcol)
5421 screen_fill(row, row + 1, col + coloff,
5422 endcol + coloff + 1, ' ', ' ', 0);
5423 }
5424 col = endcol + 1;
5425 off_to = LineOffset[row] + col + coloff;
5426 off_from += col;
5427 endcol = (clear_width > 0 ? clear_width : -clear_width);
5428 }
5429#endif /* FEAT_RIGHTLEFT */
5430
5431 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5432
5433 while (col < endcol)
5434 {
5435#ifdef FEAT_MBYTE
5436 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005437 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 else
5439 char_cells = 1;
5440#endif
5441
5442 redraw_this = redraw_next;
5443 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5444 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5445
5446#ifdef FEAT_GUI
5447 /* If the next character was bold, then redraw the current character to
5448 * remove any pixels that might have spilt over into us. This only
5449 * happens in the GUI.
5450 */
5451 if (redraw_next && gui.in_use)
5452 {
5453 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005454 if (hl > HL_ALL)
5455 hl = syn_attr2attr(hl);
5456 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 redraw_this = TRUE;
5458 }
5459#endif
5460
5461 if (redraw_this)
5462 {
5463 /*
5464 * Special handling when 'xs' termcap flag set (hpterm):
5465 * Attributes for characters are stored at the position where the
5466 * cursor is when writing the highlighting code. The
5467 * start-highlighting code must be written with the cursor on the
5468 * first highlighted character. The stop-highlighting code must
5469 * be written with the cursor just after the last highlighted
5470 * character.
5471 * Overwriting a character doesn't remove it's highlighting. Need
5472 * to clear the rest of the line, and force redrawing it
5473 * completely.
5474 */
5475 if ( p_wiv
5476 && !force
5477#ifdef FEAT_GUI
5478 && !gui.in_use
5479#endif
5480 && ScreenAttrs[off_to] != 0
5481 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5482 {
5483 /*
5484 * Need to remove highlighting attributes here.
5485 */
5486 windgoto(row, col + coloff);
5487 out_str(T_CE); /* clear rest of this screen line */
5488 screen_start(); /* don't know where cursor is now */
5489 force = TRUE; /* force redraw of rest of the line */
5490 redraw_next = TRUE; /* or else next char would miss out */
5491
5492 /*
5493 * If the previous character was highlighted, need to stop
5494 * highlighting at this character.
5495 */
5496 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5497 {
5498 screen_attr = ScreenAttrs[off_to - 1];
5499 term_windgoto(row, col + coloff);
5500 screen_stop_highlight();
5501 }
5502 else
5503 screen_attr = 0; /* highlighting has stopped */
5504 }
5505#ifdef FEAT_MBYTE
5506 if (enc_dbcs != 0)
5507 {
5508 /* Check if overwriting a double-byte with a single-byte or
5509 * the other way around requires another character to be
5510 * redrawn. For UTF-8 this isn't needed, because comparing
5511 * ScreenLinesUC[] is sufficient. */
5512 if (char_cells == 1
5513 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005514 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
5516 /* Writing a single-cell character over a double-cell
5517 * character: need to redraw the next cell. */
5518 ScreenLines[off_to + 1] = 0;
5519 redraw_next = TRUE;
5520 }
5521 else if (char_cells == 2
5522 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005523 && (*mb_off2cells)(off_to, max_off_to) == 1
5524 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 {
5526 /* Writing the second half of a double-cell character over
5527 * a double-cell character: need to redraw the second
5528 * cell. */
5529 ScreenLines[off_to + 2] = 0;
5530 redraw_next = TRUE;
5531 }
5532
5533 if (enc_dbcs == DBCS_JPNU)
5534 ScreenLines2[off_to] = ScreenLines2[off_from];
5535 }
5536 /* When writing a single-width character over a double-width
5537 * character and at the end of the redrawn text, need to clear out
5538 * the right halve of the old character.
5539 * Also required when writing the right halve of a double-width
5540 * char over the left halve of an existing one. */
5541 if (has_mbyte && col + char_cells == endcol
5542 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005543 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005545 && (*mb_off2cells)(off_to, max_off_to) == 1
5546 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 clear_next = TRUE;
5548#endif
5549
5550 ScreenLines[off_to] = ScreenLines[off_from];
5551#ifdef FEAT_MBYTE
5552 if (enc_utf8)
5553 {
5554 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5555 if (ScreenLinesUC[off_from] != 0)
5556 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005557 int i;
5558
5559 for (i = 0; i < Screen_mco; ++i)
5560 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 }
5562 }
5563 if (char_cells == 2)
5564 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5565#endif
5566
5567#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005568 /* The bold trick makes a single column of pixels appear in the
5569 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 * character should be redrawn too. This happens for our own GUI
5571 * and for some xterms. */
5572 if (
5573# ifdef FEAT_GUI
5574 gui.in_use
5575# endif
5576# if defined(FEAT_GUI) && defined(UNIX)
5577 ||
5578# endif
5579# ifdef UNIX
5580 term_is_xterm
5581# endif
5582 )
5583 {
5584 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005585 if (hl > HL_ALL)
5586 hl = syn_attr2attr(hl);
5587 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 redraw_next = TRUE;
5589 }
5590#endif
5591 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5592#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005593 /* For simplicity set the attributes of second half of a
5594 * double-wide character equal to the first half. */
5595 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005597
5598 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 else
5601#endif
5602 screen_char(off_to, row, col + coloff);
5603 }
5604 else if ( p_wiv
5605#ifdef FEAT_GUI
5606 && !gui.in_use
5607#endif
5608 && col + coloff > 0)
5609 {
5610 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5611 {
5612 /*
5613 * Don't output stop-highlight when moving the cursor, it will
5614 * stop the highlighting when it should continue.
5615 */
5616 screen_attr = 0;
5617 }
5618 else if (screen_attr != 0)
5619 screen_stop_highlight();
5620 }
5621
5622 off_to += CHAR_CELLS;
5623 off_from += CHAR_CELLS;
5624 col += CHAR_CELLS;
5625 }
5626
5627#ifdef FEAT_MBYTE
5628 if (clear_next)
5629 {
5630 /* Clear the second half of a double-wide character of which the left
5631 * half was overwritten with a single-wide character. */
5632 ScreenLines[off_to] = ' ';
5633 if (enc_utf8)
5634 ScreenLinesUC[off_to] = 0;
5635 screen_char(off_to, row, col + coloff);
5636 }
5637#endif
5638
5639 if (clear_width > 0
5640#ifdef FEAT_RIGHTLEFT
5641 && !rlflag
5642#endif
5643 )
5644 {
5645#ifdef FEAT_GUI
5646 int startCol = col;
5647#endif
5648
5649 /* blank out the rest of the line */
5650 while (col < clear_width && ScreenLines[off_to] == ' '
5651 && ScreenAttrs[off_to] == 0
5652#ifdef FEAT_MBYTE
5653 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5654#endif
5655 )
5656 {
5657 ++off_to;
5658 ++col;
5659 }
5660 if (col < clear_width)
5661 {
5662#ifdef FEAT_GUI
5663 /*
5664 * In the GUI, clearing the rest of the line may leave pixels
5665 * behind if the first character cleared was bold. Some bold
5666 * fonts spill over the left. In this case we redraw the previous
5667 * character too. If we didn't skip any blanks above, then we
5668 * only redraw if the character wasn't already redrawn anyway.
5669 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005670 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
5672 hl = ScreenAttrs[off_to];
5673 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005674 {
5675 int prev_cells = 1;
5676# ifdef FEAT_MBYTE
5677 if (enc_utf8)
5678 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5679 * that its width is 2. */
5680 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5681 else if (enc_dbcs != 0)
5682 {
5683 /* find previous character by counting from first
5684 * column and get its width. */
5685 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005686 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005687
5688 while (off < off_to)
5689 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005690 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005691 off += prev_cells;
5692 }
5693 }
5694
5695 if (enc_dbcs != 0 && prev_cells > 1)
5696 screen_char_2(off_to - prev_cells, row,
5697 col + coloff - prev_cells);
5698 else
5699# endif
5700 screen_char(off_to - prev_cells, row,
5701 col + coloff - prev_cells);
5702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704#endif
5705 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5706 ' ', ' ', 0);
5707#ifdef FEAT_VERTSPLIT
5708 off_to += clear_width - col;
5709 col = clear_width;
5710#endif
5711 }
5712 }
5713
5714 if (clear_width > 0)
5715 {
5716#ifdef FEAT_VERTSPLIT
5717 /* For a window that's left of another, draw the separator char. */
5718 if (col + coloff < Columns)
5719 {
5720 int c;
5721
5722 c = fillchar_vsep(&hl);
5723 if (ScreenLines[off_to] != c
5724# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005725 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5726 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727# endif
5728 || ScreenAttrs[off_to] != hl)
5729 {
5730 ScreenLines[off_to] = c;
5731 ScreenAttrs[off_to] = hl;
5732# ifdef FEAT_MBYTE
5733 if (enc_utf8)
5734 {
5735 if (c >= 0x80)
5736 {
5737 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005738 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
5740 else
5741 ScreenLinesUC[off_to] = 0;
5742 }
5743# endif
5744 screen_char(off_to, row, col + coloff);
5745 }
5746 }
5747 else
5748#endif
5749 LineWraps[row] = FALSE;
5750 }
5751}
5752
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005753#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005755 * Mirror text "str" for right-left displaying.
5756 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005758 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759rl_mirror(str)
5760 char_u *str;
5761{
5762 char_u *p1, *p2;
5763 int t;
5764
5765 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5766 {
5767 t = *p1;
5768 *p1 = *p2;
5769 *p2 = t;
5770 }
5771}
5772#endif
5773
5774#if defined(FEAT_WINDOWS) || defined(PROTO)
5775/*
5776 * mark all status lines for redraw; used after first :cd
5777 */
5778 void
5779status_redraw_all()
5780{
5781 win_T *wp;
5782
5783 for (wp = firstwin; wp; wp = wp->w_next)
5784 if (wp->w_status_height)
5785 {
5786 wp->w_redr_status = TRUE;
5787 redraw_later(VALID);
5788 }
5789}
5790
5791/*
5792 * mark all status lines of the current buffer for redraw
5793 */
5794 void
5795status_redraw_curbuf()
5796{
5797 win_T *wp;
5798
5799 for (wp = firstwin; wp; wp = wp->w_next)
5800 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5801 {
5802 wp->w_redr_status = TRUE;
5803 redraw_later(VALID);
5804 }
5805}
5806
5807/*
5808 * Redraw all status lines that need to be redrawn.
5809 */
5810 void
5811redraw_statuslines()
5812{
5813 win_T *wp;
5814
5815 for (wp = firstwin; wp; wp = wp->w_next)
5816 if (wp->w_redr_status)
5817 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005818 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005819 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820}
5821#endif
5822
5823#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5824/*
5825 * Redraw all status lines at the bottom of frame "frp".
5826 */
5827 void
5828win_redraw_last_status(frp)
5829 frame_T *frp;
5830{
5831 if (frp->fr_layout == FR_LEAF)
5832 frp->fr_win->w_redr_status = TRUE;
5833 else if (frp->fr_layout == FR_ROW)
5834 {
5835 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5836 win_redraw_last_status(frp);
5837 }
5838 else /* frp->fr_layout == FR_COL */
5839 {
5840 frp = frp->fr_child;
5841 while (frp->fr_next != NULL)
5842 frp = frp->fr_next;
5843 win_redraw_last_status(frp);
5844 }
5845}
5846#endif
5847
5848#ifdef FEAT_VERTSPLIT
5849/*
5850 * Draw the verticap separator right of window "wp" starting with line "row".
5851 */
5852 static void
5853draw_vsep_win(wp, row)
5854 win_T *wp;
5855 int row;
5856{
5857 int hl;
5858 int c;
5859
5860 if (wp->w_vsep_width)
5861 {
5862 /* draw the vertical separator right of this window */
5863 c = fillchar_vsep(&hl);
5864 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5865 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5866 c, ' ', hl);
5867 }
5868}
5869#endif
5870
5871#ifdef FEAT_WILDMENU
5872static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005873static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
5875/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005876 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 */
5878 static int
5879status_match_len(xp, s)
5880 expand_T *xp;
5881 char_u *s;
5882{
5883 int len = 0;
5884
5885#ifdef FEAT_MENU
5886 int emenu = (xp->xp_context == EXPAND_MENUS
5887 || xp->xp_context == EXPAND_MENUNAMES);
5888
5889 /* Check for menu separators - replace with '|'. */
5890 if (emenu && menu_is_separator(s))
5891 return 1;
5892#endif
5893
5894 while (*s != NUL)
5895 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005896 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005897 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005898 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 }
5900
5901 return len;
5902}
5903
5904/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005905 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005906 * These are backslashes used for escaping. Do show backslashes in help tags.
5907 */
5908 static int
5909skip_status_match_char(xp, s)
5910 expand_T *xp;
5911 char_u *s;
5912{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005913 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005914#ifdef FEAT_MENU
5915 || ((xp->xp_context == EXPAND_MENUS
5916 || xp->xp_context == EXPAND_MENUNAMES)
5917 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5918#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005919 )
5920 {
5921#ifndef BACKSLASH_IN_FILENAME
5922 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5923 return 2;
5924#endif
5925 return 1;
5926 }
5927 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005928}
5929
5930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 * Show wildchar matches in the status line.
5932 * Show at least the "match" item.
5933 * We start at item 'first_match' in the list and show all matches that fit.
5934 *
5935 * If inversion is possible we use it. Else '=' characters are used.
5936 */
5937 void
5938win_redr_status_matches(xp, num_matches, matches, match, showtail)
5939 expand_T *xp;
5940 int num_matches;
5941 char_u **matches; /* list of matches */
5942 int match;
5943 int showtail;
5944{
5945#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5946 int row;
5947 char_u *buf;
5948 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005949 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 int fillchar;
5951 int attr;
5952 int i;
5953 int highlight = TRUE;
5954 char_u *selstart = NULL;
5955 int selstart_col = 0;
5956 char_u *selend = NULL;
5957 static int first_match = 0;
5958 int add_left = FALSE;
5959 char_u *s;
5960#ifdef FEAT_MENU
5961 int emenu;
5962#endif
5963#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5964 int l;
5965#endif
5966
5967 if (matches == NULL) /* interrupted completion? */
5968 return;
5969
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005970#ifdef FEAT_MBYTE
5971 if (has_mbyte)
5972 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5973 else
5974#endif
5975 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 if (buf == NULL)
5977 return;
5978
5979 if (match == -1) /* don't show match but original text */
5980 {
5981 match = 0;
5982 highlight = FALSE;
5983 }
5984 /* count 1 for the ending ">" */
5985 clen = status_match_len(xp, L_MATCH(match)) + 3;
5986 if (match == 0)
5987 first_match = 0;
5988 else if (match < first_match)
5989 {
5990 /* jumping left, as far as we can go */
5991 first_match = match;
5992 add_left = TRUE;
5993 }
5994 else
5995 {
5996 /* check if match fits on the screen */
5997 for (i = first_match; i < match; ++i)
5998 clen += status_match_len(xp, L_MATCH(i)) + 2;
5999 if (first_match > 0)
6000 clen += 2;
6001 /* jumping right, put match at the left */
6002 if ((long)clen > Columns)
6003 {
6004 first_match = match;
6005 /* if showing the last match, we can add some on the left */
6006 clen = 2;
6007 for (i = match; i < num_matches; ++i)
6008 {
6009 clen += status_match_len(xp, L_MATCH(i)) + 2;
6010 if ((long)clen >= Columns)
6011 break;
6012 }
6013 if (i == num_matches)
6014 add_left = TRUE;
6015 }
6016 }
6017 if (add_left)
6018 while (first_match > 0)
6019 {
6020 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6021 if ((long)clen >= Columns)
6022 break;
6023 --first_match;
6024 }
6025
6026 fillchar = fillchar_status(&attr, TRUE);
6027
6028 if (first_match == 0)
6029 {
6030 *buf = NUL;
6031 len = 0;
6032 }
6033 else
6034 {
6035 STRCPY(buf, "< ");
6036 len = 2;
6037 }
6038 clen = len;
6039
6040 i = first_match;
6041 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6042 {
6043 if (i == match)
6044 {
6045 selstart = buf + len;
6046 selstart_col = clen;
6047 }
6048
6049 s = L_MATCH(i);
6050 /* Check for menu separators - replace with '|' */
6051#ifdef FEAT_MENU
6052 emenu = (xp->xp_context == EXPAND_MENUS
6053 || xp->xp_context == EXPAND_MENUNAMES);
6054 if (emenu && menu_is_separator(s))
6055 {
6056 STRCPY(buf + len, transchar('|'));
6057 l = (int)STRLEN(buf + len);
6058 len += l;
6059 clen += l;
6060 }
6061 else
6062#endif
6063 for ( ; *s != NUL; ++s)
6064 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006065 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 clen += ptr2cells(s);
6067#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006068 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 {
6070 STRNCPY(buf + len, s, l);
6071 s += l - 1;
6072 len += l;
6073 }
6074 else
6075#endif
6076 {
6077 STRCPY(buf + len, transchar_byte(*s));
6078 len += (int)STRLEN(buf + len);
6079 }
6080 }
6081 if (i == match)
6082 selend = buf + len;
6083
6084 *(buf + len++) = ' ';
6085 *(buf + len++) = ' ';
6086 clen += 2;
6087 if (++i == num_matches)
6088 break;
6089 }
6090
6091 if (i != num_matches)
6092 {
6093 *(buf + len++) = '>';
6094 ++clen;
6095 }
6096
6097 buf[len] = NUL;
6098
6099 row = cmdline_row - 1;
6100 if (row >= 0)
6101 {
6102 if (wild_menu_showing == 0)
6103 {
6104 if (msg_scrolled > 0)
6105 {
6106 /* Put the wildmenu just above the command line. If there is
6107 * no room, scroll the screen one line up. */
6108 if (cmdline_row == Rows - 1)
6109 {
6110 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6111 ++msg_scrolled;
6112 }
6113 else
6114 {
6115 ++cmdline_row;
6116 ++row;
6117 }
6118 wild_menu_showing = WM_SCROLLED;
6119 }
6120 else
6121 {
6122 /* Create status line if needed by setting 'laststatus' to 2.
6123 * Set 'winminheight' to zero to avoid that the window is
6124 * resized. */
6125 if (lastwin->w_status_height == 0)
6126 {
6127 save_p_ls = p_ls;
6128 save_p_wmh = p_wmh;
6129 p_ls = 2;
6130 p_wmh = 0;
6131 last_status(FALSE);
6132 }
6133 wild_menu_showing = WM_SHOWN;
6134 }
6135 }
6136
6137 screen_puts(buf, row, 0, attr);
6138 if (selstart != NULL && highlight)
6139 {
6140 *selend = NUL;
6141 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6142 }
6143
6144 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6145 }
6146
6147#ifdef FEAT_VERTSPLIT
6148 win_redraw_last_status(topframe);
6149#else
6150 lastwin->w_redr_status = TRUE;
6151#endif
6152 vim_free(buf);
6153}
6154#endif
6155
6156#if defined(FEAT_WINDOWS) || defined(PROTO)
6157/*
6158 * Redraw the status line of window wp.
6159 *
6160 * If inversion is possible we use it. Else '=' characters are used.
6161 */
6162 void
6163win_redr_status(wp)
6164 win_T *wp;
6165{
6166 int row;
6167 char_u *p;
6168 int len;
6169 int fillchar;
6170 int attr;
6171 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006172 static int busy = FALSE;
6173
6174 /* It's possible to get here recursively when 'statusline' (indirectly)
6175 * invokes ":redrawstatus". Simply ignore the call then. */
6176 if (busy)
6177 return;
6178 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
6180 wp->w_redr_status = FALSE;
6181 if (wp->w_status_height == 0)
6182 {
6183 /* no status line, can only be last window */
6184 redraw_cmdline = TRUE;
6185 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006186 else if (!redrawing()
6187#ifdef FEAT_INS_EXPAND
6188 /* don't update status line when popup menu is visible and may be
6189 * drawn over it */
6190 || pum_visible()
6191#endif
6192 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
6194 /* Don't redraw right now, do it later. */
6195 wp->w_redr_status = TRUE;
6196 }
6197#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006198 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 {
6200 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006201 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203#endif
6204 else
6205 {
6206 fillchar = fillchar_status(&attr, wp == curwin);
6207
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006208 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 p = NameBuff;
6210 len = (int)STRLEN(p);
6211
6212 if (wp->w_buffer->b_help
6213#ifdef FEAT_QUICKFIX
6214 || wp->w_p_pvw
6215#endif
6216 || bufIsChanged(wp->w_buffer)
6217 || wp->w_buffer->b_p_ro)
6218 *(p + len++) = ' ';
6219 if (wp->w_buffer->b_help)
6220 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006221 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 len += (int)STRLEN(p + len);
6223 }
6224#ifdef FEAT_QUICKFIX
6225 if (wp->w_p_pvw)
6226 {
6227 STRCPY(p + len, _("[Preview]"));
6228 len += (int)STRLEN(p + len);
6229 }
6230#endif
6231 if (bufIsChanged(wp->w_buffer))
6232 {
6233 STRCPY(p + len, "[+]");
6234 len += 3;
6235 }
6236 if (wp->w_buffer->b_p_ro)
6237 {
6238 STRCPY(p + len, "[RO]");
6239 len += 4;
6240 }
6241
6242#ifndef FEAT_VERTSPLIT
6243 this_ru_col = ru_col;
6244 if (this_ru_col < (Columns + 1) / 2)
6245 this_ru_col = (Columns + 1) / 2;
6246#else
6247 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6248 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6249 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6250 if (this_ru_col <= 1)
6251 {
6252 p = (char_u *)"<"; /* No room for file name! */
6253 len = 1;
6254 }
6255 else
6256#endif
6257#ifdef FEAT_MBYTE
6258 if (has_mbyte)
6259 {
6260 int clen = 0, i;
6261
6262 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006263 clen = mb_string2cells(p, -1);
6264
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 /* Find first character that will fit.
6266 * Going from start to end is much faster for DBCS. */
6267 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006268 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 clen -= (*mb_ptr2cells)(p + i);
6270 len = clen;
6271 if (i > 0)
6272 {
6273 p = p + i - 1;
6274 *p = '<';
6275 ++len;
6276 }
6277
6278 }
6279 else
6280#endif
6281 if (len > this_ru_col - 1)
6282 {
6283 p += len - (this_ru_col - 1);
6284 *p = '<';
6285 len = this_ru_col - 1;
6286 }
6287
6288 row = W_WINROW(wp) + wp->w_height;
6289 screen_puts(p, row, W_WINCOL(wp), attr);
6290 screen_fill(row, row + 1, len + W_WINCOL(wp),
6291 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6292
6293 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6294 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6295 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6296 - 1 + W_WINCOL(wp)), attr);
6297
6298#ifdef FEAT_CMDL_INFO
6299 win_redr_ruler(wp, TRUE);
6300#endif
6301 }
6302
6303#ifdef FEAT_VERTSPLIT
6304 /*
6305 * May need to draw the character below the vertical separator.
6306 */
6307 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6308 {
6309 if (stl_connected(wp))
6310 fillchar = fillchar_status(&attr, wp == curwin);
6311 else
6312 fillchar = fillchar_vsep(&attr);
6313 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6314 attr);
6315 }
6316#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006317 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318}
6319
Bram Moolenaar238a5642006-02-21 22:12:05 +00006320#ifdef FEAT_STL_OPT
6321/*
6322 * Redraw the status line according to 'statusline' and take care of any
6323 * errors encountered.
6324 */
6325 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006326redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006327 win_T *wp;
6328{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006329 static int entered = FALSE;
6330 int save_called_emsg = called_emsg;
6331
6332 /* When called recursively return. This can happen when the statusline
6333 * contains an expression that triggers a redraw. */
6334 if (entered)
6335 return;
6336 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006337
6338 called_emsg = FALSE;
6339 win_redr_custom(wp, FALSE);
6340 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006341 {
6342 /* When there is an error disable the statusline, otherwise the
6343 * display is messed up with errors and a redraw triggers the problem
6344 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006345 set_string_option_direct((char_u *)"statusline", -1,
6346 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006347 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006348 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006349 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006350 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006351}
6352#endif
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354# ifdef FEAT_VERTSPLIT
6355/*
6356 * Return TRUE if the status line of window "wp" is connected to the status
6357 * line of the window right of it. If not, then it's a vertical separator.
6358 * Only call if (wp->w_vsep_width != 0).
6359 */
6360 int
6361stl_connected(wp)
6362 win_T *wp;
6363{
6364 frame_T *fr;
6365
6366 fr = wp->w_frame;
6367 while (fr->fr_parent != NULL)
6368 {
6369 if (fr->fr_parent->fr_layout == FR_COL)
6370 {
6371 if (fr->fr_next != NULL)
6372 break;
6373 }
6374 else
6375 {
6376 if (fr->fr_next != NULL)
6377 return TRUE;
6378 }
6379 fr = fr->fr_parent;
6380 }
6381 return FALSE;
6382}
6383# endif
6384
6385#endif /* FEAT_WINDOWS */
6386
6387#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6388/*
6389 * Get the value to show for the language mappings, active 'keymap'.
6390 */
6391 int
6392get_keymap_str(wp, buf, len)
6393 win_T *wp;
6394 char_u *buf; /* buffer for the result */
6395 int len; /* length of buffer */
6396{
6397 char_u *p;
6398
6399 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6400 return FALSE;
6401
6402 {
6403#ifdef FEAT_EVAL
6404 buf_T *old_curbuf = curbuf;
6405 win_T *old_curwin = curwin;
6406 char_u *s;
6407
6408 curbuf = wp->w_buffer;
6409 curwin = wp;
6410 STRCPY(buf, "b:keymap_name"); /* must be writable */
6411 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006412 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 --emsg_skip;
6414 curbuf = old_curbuf;
6415 curwin = old_curwin;
6416 if (p == NULL || *p == NUL)
6417#endif
6418 {
6419#ifdef FEAT_KEYMAP
6420 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6421 p = wp->w_buffer->b_p_keymap;
6422 else
6423#endif
6424 p = (char_u *)"lang";
6425 }
6426 if ((int)(STRLEN(p) + 3) < len)
6427 sprintf((char *)buf, "<%s>", p);
6428 else
6429 buf[0] = NUL;
6430#ifdef FEAT_EVAL
6431 vim_free(s);
6432#endif
6433 }
6434 return buf[0] != NUL;
6435}
6436#endif
6437
6438#if defined(FEAT_STL_OPT) || defined(PROTO)
6439/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006440 * Redraw the status line or ruler of window "wp".
6441 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 */
6443 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006444win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006446 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
6448 int attr;
6449 int curattr;
6450 int row;
6451 int col = 0;
6452 int maxwidth;
6453 int width;
6454 int n;
6455 int len;
6456 int fillchar;
6457 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006458 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006460 struct stl_hlrec hltab[STL_MAX_ITEM];
6461 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006462 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006463 win_T *ewp;
6464 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
6466 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006467 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006469 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006470 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006471 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006472 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006473 attr = hl_attr(HLF_TPF);
6474 maxwidth = Columns;
6475# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006476 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006479 else
6480 {
6481 row = W_WINROW(wp) + wp->w_height;
6482 fillchar = fillchar_status(&attr, wp == curwin);
6483 maxwidth = W_WIDTH(wp);
6484
6485 if (draw_ruler)
6486 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006487 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006488 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006489 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006490 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006491 if (*++stl == '-')
6492 stl++;
6493 if (atoi((char *)stl))
6494 while (VIM_ISDIGIT(*stl))
6495 stl++;
6496 if (*stl++ != '(')
6497 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006498 }
6499#ifdef FEAT_VERTSPLIT
6500 col = ru_col - (Columns - W_WIDTH(wp));
6501 if (col < (W_WIDTH(wp) + 1) / 2)
6502 col = (W_WIDTH(wp) + 1) / 2;
6503#else
6504 col = ru_col;
6505 if (col > (Columns + 1) / 2)
6506 col = (Columns + 1) / 2;
6507#endif
6508 maxwidth = W_WIDTH(wp) - col;
6509#ifdef FEAT_WINDOWS
6510 if (!wp->w_status_height)
6511#endif
6512 {
6513 row = Rows - 1;
6514 --maxwidth; /* writing in last column may cause scrolling */
6515 fillchar = ' ';
6516 attr = 0;
6517 }
6518
6519# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006520 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006521# endif
6522 }
6523 else
6524 {
6525 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006526 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006527 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006528 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006529# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006530 use_sandbox = was_set_insecurely((char_u *)"statusline",
6531 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006532# endif
6533 }
6534
6535#ifdef FEAT_VERTSPLIT
6536 col += W_WINCOL(wp);
6537#endif
6538 }
6539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (maxwidth <= 0)
6541 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542
Bram Moolenaar61452852011-02-01 18:01:11 +01006543 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6544 * the cursor away and back. */
6545 ewp = wp == NULL ? curwin : wp;
6546 p_crb_save = ewp->w_p_crb;
6547 ewp->w_p_crb = FALSE;
6548
Bram Moolenaar362f3562009-11-03 16:20:34 +00006549 /* Make a copy, because the statusline may include a function call that
6550 * might change the option value and free the memory. */
6551 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006552 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006553 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006554 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006555 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006556 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006558 /* Make all characters printable. */
6559 p = transstr(buf);
6560 if (p != NULL)
6561 {
6562 vim_strncpy(buf, p, sizeof(buf) - 1);
6563 vim_free(p);
6564 }
6565
6566 /* fill up with "fillchar" */
6567 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006568 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 {
6570#ifdef FEAT_MBYTE
6571 len += (*mb_char2bytes)(fillchar, buf + len);
6572#else
6573 buf[len++] = fillchar;
6574#endif
6575 ++width;
6576 }
6577 buf[len] = NUL;
6578
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006579 /*
6580 * Draw each snippet with the specified highlighting.
6581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 curattr = attr;
6583 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006584 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006586 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 screen_puts_len(p, len, row, col, curattr);
6588 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006589 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006591 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006593 else if (hltab[n].userhl < 0)
6594 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006596 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006597 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598#endif
6599 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006600 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 }
6602 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006603
6604 if (wp == NULL)
6605 {
6606 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6607 col = 0;
6608 len = 0;
6609 p = buf;
6610 fillchar = 0;
6611 for (n = 0; tabtab[n].start != NULL; n++)
6612 {
6613 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6614 while (col < len)
6615 TabPageIdxs[col++] = fillchar;
6616 p = tabtab[n].start;
6617 fillchar = tabtab[n].userhl;
6618 }
6619 while (col < Columns)
6620 TabPageIdxs[col++] = fillchar;
6621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622}
6623
6624#endif /* FEAT_STL_OPT */
6625
6626/*
6627 * Output a single character directly to the screen and update ScreenLines.
6628 */
6629 void
6630screen_putchar(c, row, col, attr)
6631 int c;
6632 int row, col;
6633 int attr;
6634{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 char_u buf[MB_MAXBYTES + 1];
6636
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006637#ifdef FEAT_MBYTE
6638 if (has_mbyte)
6639 buf[(*mb_char2bytes)(c, buf)] = NUL;
6640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006642 {
6643 buf[0] = c;
6644 buf[1] = NUL;
6645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 screen_puts(buf, row, col, attr);
6647}
6648
6649/*
6650 * Get a single character directly from ScreenLines into "bytes[]".
6651 * Also return its attribute in *attrp;
6652 */
6653 void
6654screen_getbytes(row, col, bytes, attrp)
6655 int row, col;
6656 char_u *bytes;
6657 int *attrp;
6658{
6659 unsigned off;
6660
6661 /* safety check */
6662 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6663 {
6664 off = LineOffset[row] + col;
6665 *attrp = ScreenAttrs[off];
6666 bytes[0] = ScreenLines[off];
6667 bytes[1] = NUL;
6668
6669#ifdef FEAT_MBYTE
6670 if (enc_utf8 && ScreenLinesUC[off] != 0)
6671 bytes[utfc_char2bytes(off, bytes)] = NUL;
6672 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6673 {
6674 bytes[0] = ScreenLines[off];
6675 bytes[1] = ScreenLines2[off];
6676 bytes[2] = NUL;
6677 }
6678 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6679 {
6680 bytes[1] = ScreenLines[off + 1];
6681 bytes[2] = NUL;
6682 }
6683#endif
6684 }
6685}
6686
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006687#ifdef FEAT_MBYTE
6688static int screen_comp_differs __ARGS((int, int*));
6689
6690/*
6691 * Return TRUE if composing characters for screen posn "off" differs from
6692 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006693 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006694 */
6695 static int
6696screen_comp_differs(off, u8cc)
6697 int off;
6698 int *u8cc;
6699{
6700 int i;
6701
6702 for (i = 0; i < Screen_mco; ++i)
6703 {
6704 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6705 return TRUE;
6706 if (u8cc[i] == 0)
6707 break;
6708 }
6709 return FALSE;
6710}
6711#endif
6712
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713/*
6714 * Put string '*text' on the screen at position 'row' and 'col', with
6715 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6716 * Note: only outputs within one row, message is truncated at screen boundary!
6717 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6718 */
6719 void
6720screen_puts(text, row, col, attr)
6721 char_u *text;
6722 int row;
6723 int col;
6724 int attr;
6725{
6726 screen_puts_len(text, -1, row, col, attr);
6727}
6728
6729/*
6730 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6731 * a NUL.
6732 */
6733 void
6734screen_puts_len(text, len, row, col, attr)
6735 char_u *text;
6736 int len;
6737 int row;
6738 int col;
6739 int attr;
6740{
6741 unsigned off;
6742 char_u *ptr = text;
6743 int c;
6744#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006745 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 int mbyte_blen = 1;
6747 int mbyte_cells = 1;
6748 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006749 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 int clear_next_cell = FALSE;
6751# ifdef FEAT_ARABIC
6752 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006753 int pc, nc, nc1;
6754 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755# endif
6756#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006757#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6758 int force_redraw_this;
6759 int force_redraw_next = FALSE;
6760#endif
6761 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762
6763 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6764 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006765 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766
Bram Moolenaarc236c162008-07-13 17:41:49 +00006767#ifdef FEAT_MBYTE
6768 /* When drawing over the right halve of a double-wide char clear out the
6769 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006770 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006771# ifdef FEAT_GUI
6772 && !gui.in_use
6773# endif
6774 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006775 {
6776 ScreenLines[off - 1] = ' ';
6777 ScreenAttrs[off - 1] = 0;
6778 if (enc_utf8)
6779 {
6780 ScreenLinesUC[off - 1] = 0;
6781 ScreenLinesC[0][off - 1] = 0;
6782 }
6783 /* redraw the previous cell, make it empty */
6784 screen_char(off - 1, row, col - 1);
6785 /* force the cell at "col" to be redrawn */
6786 force_redraw_next = TRUE;
6787 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006788#endif
6789
Bram Moolenaar367329b2007-08-30 11:53:22 +00006790#ifdef FEAT_MBYTE
6791 max_off = LineOffset[row] + screen_Columns;
6792#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006793 while (col < screen_Columns
6794 && (len < 0 || (int)(ptr - text) < len)
6795 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
6797 c = *ptr;
6798#ifdef FEAT_MBYTE
6799 /* check if this is the first byte of a multibyte */
6800 if (has_mbyte)
6801 {
6802 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006803 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006805 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6807 mbyte_cells = 1;
6808 else if (enc_dbcs != 0)
6809 mbyte_cells = mbyte_blen;
6810 else /* enc_utf8 */
6811 {
6812 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006813 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 (int)((text + len) - ptr));
6815 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006816 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006818# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 /* Non-BMP character: display as ? or fullwidth ?. */
6820 if (u8c >= 0x10000)
6821 {
6822 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6823 if (attr == 0)
6824 attr = hl_attr(HLF_8);
6825 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827# ifdef FEAT_ARABIC
6828 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6829 {
6830 /* Do Arabic shaping. */
6831 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6832 {
6833 /* Past end of string to be displayed. */
6834 nc = NUL;
6835 nc1 = NUL;
6836 }
6837 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006838 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006839 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6840 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006841 nc1 = pcc[0];
6842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 pc = prev_c;
6844 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006845 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 }
6847 else
6848 prev_c = u8c;
6849# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006850 if (col + mbyte_cells > screen_Columns)
6851 {
6852 /* Only 1 cell left, but character requires 2 cells:
6853 * display a '>' in the last column to avoid wrapping. */
6854 c = '>';
6855 mbyte_cells = 1;
6856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 }
6858 }
6859#endif
6860
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006861#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6862 force_redraw_this = force_redraw_next;
6863 force_redraw_next = FALSE;
6864#endif
6865
6866 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867#ifdef FEAT_MBYTE
6868 || (mbyte_cells == 2
6869 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6870 || (enc_dbcs == DBCS_JPNU
6871 && c == 0x8e
6872 && ScreenLines2[off] != ptr[1])
6873 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006874 && (ScreenLinesUC[off] !=
6875 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6876 || (ScreenLinesUC[off] != 0
6877 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878#endif
6879 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006880 || exmode_active;
6881
6882 if (need_redraw
6883#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6884 || force_redraw_this
6885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 )
6887 {
6888#if defined(FEAT_GUI) || defined(UNIX)
6889 /* The bold trick makes a single row of pixels appear in the next
6890 * character. When a bold character is removed, the next
6891 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006892 * and for some xterms. */
6893 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894# ifdef FEAT_GUI
6895 gui.in_use
6896# endif
6897# if defined(FEAT_GUI) && defined(UNIX)
6898 ||
6899# endif
6900# ifdef UNIX
6901 term_is_xterm
6902# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006903 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006905 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006907 if (n > HL_ALL)
6908 n = syn_attr2attr(n);
6909 if (n & HL_BOLD)
6910 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 }
6912#endif
6913#ifdef FEAT_MBYTE
6914 /* When at the end of the text and overwriting a two-cell
6915 * character with a one-cell character, need to clear the next
6916 * cell. Also when overwriting the left halve of a two-cell char
6917 * with the right halve of a two-cell char. Do this only once
6918 * (mb_off2cells() may return 2 on the right halve). */
6919 if (clear_next_cell)
6920 clear_next_cell = FALSE;
6921 else if (has_mbyte
6922 && (len < 0 ? ptr[mbyte_blen] == NUL
6923 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006924 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006926 && (*mb_off2cells)(off, max_off) == 1
6927 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 clear_next_cell = TRUE;
6929
6930 /* Make sure we never leave a second byte of a double-byte behind,
6931 * it confuses mb_off2cells(). */
6932 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006933 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006935 && (*mb_off2cells)(off, max_off) == 1
6936 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 ScreenLines[off + mbyte_blen] = 0;
6938#endif
6939 ScreenLines[off] = c;
6940 ScreenAttrs[off] = attr;
6941#ifdef FEAT_MBYTE
6942 if (enc_utf8)
6943 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006944 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 ScreenLinesUC[off] = 0;
6946 else
6947 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006948 int i;
6949
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006951 for (i = 0; i < Screen_mco; ++i)
6952 {
6953 ScreenLinesC[i][off] = u8cc[i];
6954 if (u8cc[i] == 0)
6955 break;
6956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958 if (mbyte_cells == 2)
6959 {
6960 ScreenLines[off + 1] = 0;
6961 ScreenAttrs[off + 1] = attr;
6962 }
6963 screen_char(off, row, col);
6964 }
6965 else if (mbyte_cells == 2)
6966 {
6967 ScreenLines[off + 1] = ptr[1];
6968 ScreenAttrs[off + 1] = attr;
6969 screen_char_2(off, row, col);
6970 }
6971 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6972 {
6973 ScreenLines2[off] = ptr[1];
6974 screen_char(off, row, col);
6975 }
6976 else
6977#endif
6978 screen_char(off, row, col);
6979 }
6980#ifdef FEAT_MBYTE
6981 if (has_mbyte)
6982 {
6983 off += mbyte_cells;
6984 col += mbyte_cells;
6985 ptr += mbyte_blen;
6986 if (clear_next_cell)
6987 ptr = (char_u *)" ";
6988 }
6989 else
6990#endif
6991 {
6992 ++off;
6993 ++col;
6994 ++ptr;
6995 }
6996 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006997
6998#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6999 /* If we detected the next character needs to be redrawn, but the text
7000 * doesn't extend up to there, update the character here. */
7001 if (force_redraw_next && col < screen_Columns)
7002 {
7003# ifdef FEAT_MBYTE
7004 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7005 screen_char_2(off, row, col);
7006 else
7007# endif
7008 screen_char(off, row, col);
7009 }
7010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011}
7012
7013#ifdef FEAT_SEARCH_EXTRA
7014/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007015 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 */
7017 static void
7018start_search_hl()
7019{
7020 if (p_hls && !no_hlsearch)
7021 {
7022 last_pat_prog(&search_hl.rm);
7023 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007024# ifdef FEAT_RELTIME
7025 /* Set the time limit to 'redrawtime'. */
7026 profile_setlimit(p_rdt, &search_hl.tm);
7027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 }
7029}
7030
7031/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007032 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 */
7034 static void
7035end_search_hl()
7036{
7037 if (search_hl.rm.regprog != NULL)
7038 {
7039 vim_free(search_hl.rm.regprog);
7040 search_hl.rm.regprog = NULL;
7041 }
7042}
7043
7044/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007045 * Init for calling prepare_search_hl().
7046 */
7047 static void
7048init_search_hl(wp)
7049 win_T *wp;
7050{
7051 matchitem_T *cur;
7052
7053 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7054 * match */
7055 cur = wp->w_match_head;
7056 while (cur != NULL)
7057 {
7058 cur->hl.rm = cur->match;
7059 if (cur->hlg_id == 0)
7060 cur->hl.attr = 0;
7061 else
7062 cur->hl.attr = syn_id2attr(cur->hlg_id);
7063 cur->hl.buf = wp->w_buffer;
7064 cur->hl.lnum = 0;
7065 cur->hl.first_lnum = 0;
7066# ifdef FEAT_RELTIME
7067 /* Set the time limit to 'redrawtime'. */
7068 profile_setlimit(p_rdt, &(cur->hl.tm));
7069# endif
7070 cur = cur->next;
7071 }
7072 search_hl.buf = wp->w_buffer;
7073 search_hl.lnum = 0;
7074 search_hl.first_lnum = 0;
7075 /* time limit is set at the toplevel, for all windows */
7076}
7077
7078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 * Advance to the match in window "wp" line "lnum" or past it.
7080 */
7081 static void
7082prepare_search_hl(wp, lnum)
7083 win_T *wp;
7084 linenr_T lnum;
7085{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007086 matchitem_T *cur; /* points to the match list */
7087 match_T *shl; /* points to search_hl or a match */
7088 int shl_flag; /* flag to indicate whether search_hl
7089 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 int n;
7091
7092 /*
7093 * When using a multi-line pattern, start searching at the top
7094 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007095 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007097 cur = wp->w_match_head;
7098 shl_flag = FALSE;
7099 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007101 if (shl_flag == FALSE)
7102 {
7103 shl = &search_hl;
7104 shl_flag = TRUE;
7105 }
7106 else
7107 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 if (shl->rm.regprog != NULL
7109 && shl->lnum == 0
7110 && re_multiline(shl->rm.regprog))
7111 {
7112 if (shl->first_lnum == 0)
7113 {
7114# ifdef FEAT_FOLDING
7115 for (shl->first_lnum = lnum;
7116 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7117 if (hasFoldingWin(wp, shl->first_lnum - 1,
7118 NULL, NULL, TRUE, NULL))
7119 break;
7120# else
7121 shl->first_lnum = wp->w_topline;
7122# endif
7123 }
7124 n = 0;
7125 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7126 {
7127 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7128 if (shl->lnum != 0)
7129 {
7130 shl->first_lnum = shl->lnum
7131 + shl->rm.endpos[0].lnum
7132 - shl->rm.startpos[0].lnum;
7133 n = shl->rm.endpos[0].col;
7134 }
7135 else
7136 {
7137 ++shl->first_lnum;
7138 n = 0;
7139 }
7140 }
7141 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007142 if (shl != &search_hl && cur != NULL)
7143 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 }
7145}
7146
7147/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007148 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 * Uses shl->buf.
7150 * Sets shl->lnum and shl->rm contents.
7151 * Note: Assumes a previous match is always before "lnum", unless
7152 * shl->lnum is zero.
7153 * Careful: Any pointers for buffer lines will become invalid.
7154 */
7155 static void
7156next_search_hl(win, shl, lnum, mincol)
7157 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007158 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 linenr_T lnum;
7160 colnr_T mincol; /* minimal column for a match */
7161{
7162 linenr_T l;
7163 colnr_T matchcol;
7164 long nmatched;
7165
7166 if (shl->lnum != 0)
7167 {
7168 /* Check for three situations:
7169 * 1. If the "lnum" is below a previous match, start a new search.
7170 * 2. If the previous match includes "mincol", use it.
7171 * 3. Continue after the previous match.
7172 */
7173 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7174 if (lnum > l)
7175 shl->lnum = 0;
7176 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7177 return;
7178 }
7179
7180 /*
7181 * Repeat searching for a match until one is found that includes "mincol"
7182 * or none is found in this line.
7183 */
7184 called_emsg = FALSE;
7185 for (;;)
7186 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007187#ifdef FEAT_RELTIME
7188 /* Stop searching after passing the time limit. */
7189 if (profile_passed_limit(&(shl->tm)))
7190 {
7191 shl->lnum = 0; /* no match found in time */
7192 break;
7193 }
7194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 /* Three situations:
7196 * 1. No useful previous match: search from start of line.
7197 * 2. Not Vi compatible or empty match: continue at next character.
7198 * Break the loop if this is beyond the end of the line.
7199 * 3. Vi compatible searching: continue at end of previous match.
7200 */
7201 if (shl->lnum == 0)
7202 matchcol = 0;
7203 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7204 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007205 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007207 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007208
7209 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007210 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007211 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007213 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 shl->lnum = 0;
7215 break;
7216 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007217#ifdef FEAT_MBYTE
7218 if (has_mbyte)
7219 matchcol += mb_ptr2len(ml);
7220 else
7221#endif
7222 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 }
7224 else
7225 matchcol = shl->rm.endpos[0].col;
7226
7227 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007228 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7229#ifdef FEAT_RELTIME
7230 &(shl->tm)
7231#else
7232 NULL
7233#endif
7234 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007235 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 {
7237 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007238 if (shl == &search_hl)
7239 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007240 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007241 vim_free(shl->rm.regprog);
7242 no_hlsearch = TRUE;
7243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007245 shl->lnum = 0;
7246 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 break;
7248 }
7249 if (nmatched == 0)
7250 {
7251 shl->lnum = 0; /* no match found */
7252 break;
7253 }
7254 if (shl->rm.startpos[0].lnum > 0
7255 || shl->rm.startpos[0].col >= mincol
7256 || nmatched > 1
7257 || shl->rm.endpos[0].col > mincol)
7258 {
7259 shl->lnum += shl->rm.startpos[0].lnum;
7260 break; /* useful match found */
7261 }
7262 }
7263}
7264#endif
7265
7266 static void
7267screen_start_highlight(attr)
7268 int attr;
7269{
7270 attrentry_T *aep = NULL;
7271
7272 screen_attr = attr;
7273 if (full_screen
7274#ifdef WIN3264
7275 && termcap_active
7276#endif
7277 )
7278 {
7279#ifdef FEAT_GUI
7280 if (gui.in_use)
7281 {
7282 char buf[20];
7283
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007284 /* The GUI handles this internally. */
7285 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 OUT_STR(buf);
7287 }
7288 else
7289#endif
7290 {
7291 if (attr > HL_ALL) /* special HL attr. */
7292 {
7293 if (t_colors > 1)
7294 aep = syn_cterm_attr2entry(attr);
7295 else
7296 aep = syn_term_attr2entry(attr);
7297 if (aep == NULL) /* did ":syntax clear" */
7298 attr = 0;
7299 else
7300 attr = aep->ae_attr;
7301 }
7302 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7303 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007304 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7305 && cterm_normal_fg_bold)
7306 /* If the Normal FG color has BOLD attribute and the new HL
7307 * has a FG color defined, clear BOLD. */
7308 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7310 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007311 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7312 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 out_str(T_US);
7314 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7315 out_str(T_CZH);
7316 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7317 out_str(T_MR);
7318
7319 /*
7320 * Output the color or start string after bold etc., in case the
7321 * bold etc. override the color setting.
7322 */
7323 if (aep != NULL)
7324 {
7325 if (t_colors > 1)
7326 {
7327 if (aep->ae_u.cterm.fg_color)
7328 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7329 if (aep->ae_u.cterm.bg_color)
7330 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7331 }
7332 else
7333 {
7334 if (aep->ae_u.term.start != NULL)
7335 out_str(aep->ae_u.term.start);
7336 }
7337 }
7338 }
7339 }
7340}
7341
7342 void
7343screen_stop_highlight()
7344{
7345 int do_ME = FALSE; /* output T_ME code */
7346
7347 if (screen_attr != 0
7348#ifdef WIN3264
7349 && termcap_active
7350#endif
7351 )
7352 {
7353#ifdef FEAT_GUI
7354 if (gui.in_use)
7355 {
7356 char buf[20];
7357
7358 /* use internal GUI code */
7359 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7360 OUT_STR(buf);
7361 }
7362 else
7363#endif
7364 {
7365 if (screen_attr > HL_ALL) /* special HL attr. */
7366 {
7367 attrentry_T *aep;
7368
7369 if (t_colors > 1)
7370 {
7371 /*
7372 * Assume that t_me restores the original colors!
7373 */
7374 aep = syn_cterm_attr2entry(screen_attr);
7375 if (aep != NULL && (aep->ae_u.cterm.fg_color
7376 || aep->ae_u.cterm.bg_color))
7377 do_ME = TRUE;
7378 }
7379 else
7380 {
7381 aep = syn_term_attr2entry(screen_attr);
7382 if (aep != NULL && aep->ae_u.term.stop != NULL)
7383 {
7384 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7385 do_ME = TRUE;
7386 else
7387 out_str(aep->ae_u.term.stop);
7388 }
7389 }
7390 if (aep == NULL) /* did ":syntax clear" */
7391 screen_attr = 0;
7392 else
7393 screen_attr = aep->ae_attr;
7394 }
7395
7396 /*
7397 * Often all ending-codes are equal to T_ME. Avoid outputting the
7398 * same sequence several times.
7399 */
7400 if (screen_attr & HL_STANDOUT)
7401 {
7402 if (STRCMP(T_SE, T_ME) == 0)
7403 do_ME = TRUE;
7404 else
7405 out_str(T_SE);
7406 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007407 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 {
7409 if (STRCMP(T_UE, T_ME) == 0)
7410 do_ME = TRUE;
7411 else
7412 out_str(T_UE);
7413 }
7414 if (screen_attr & HL_ITALIC)
7415 {
7416 if (STRCMP(T_CZR, T_ME) == 0)
7417 do_ME = TRUE;
7418 else
7419 out_str(T_CZR);
7420 }
7421 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7422 out_str(T_ME);
7423
7424 if (t_colors > 1)
7425 {
7426 /* set Normal cterm colors */
7427 if (cterm_normal_fg_color != 0)
7428 term_fg_color(cterm_normal_fg_color - 1);
7429 if (cterm_normal_bg_color != 0)
7430 term_bg_color(cterm_normal_bg_color - 1);
7431 if (cterm_normal_fg_bold)
7432 out_str(T_MD);
7433 }
7434 }
7435 }
7436 screen_attr = 0;
7437}
7438
7439/*
7440 * Reset the colors for a cterm. Used when leaving Vim.
7441 * The machine specific code may override this again.
7442 */
7443 void
7444reset_cterm_colors()
7445{
7446 if (t_colors > 1)
7447 {
7448 /* set Normal cterm colors */
7449 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7450 {
7451 out_str(T_OP);
7452 screen_attr = -1;
7453 }
7454 if (cterm_normal_fg_bold)
7455 {
7456 out_str(T_ME);
7457 screen_attr = -1;
7458 }
7459 }
7460}
7461
7462/*
7463 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7464 * using the attributes from ScreenAttrs["off"].
7465 */
7466 static void
7467screen_char(off, row, col)
7468 unsigned off;
7469 int row;
7470 int col;
7471{
7472 int attr;
7473
7474 /* Check for illegal values, just in case (could happen just after
7475 * resizing). */
7476 if (row >= screen_Rows || col >= screen_Columns)
7477 return;
7478
7479 /* Outputting the last character on the screen may scrollup the screen.
7480 * Don't to it! Mark the character invalid (update it when scrolled up) */
7481 if (row == screen_Rows - 1 && col == screen_Columns - 1
7482#ifdef FEAT_RIGHTLEFT
7483 /* account for first command-line character in rightleft mode */
7484 && !cmdmsg_rl
7485#endif
7486 )
7487 {
7488 ScreenAttrs[off] = (sattr_T)-1;
7489 return;
7490 }
7491
7492 /*
7493 * Stop highlighting first, so it's easier to move the cursor.
7494 */
7495#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7496 if (screen_char_attr != 0)
7497 attr = screen_char_attr;
7498 else
7499#endif
7500 attr = ScreenAttrs[off];
7501 if (screen_attr != attr)
7502 screen_stop_highlight();
7503
7504 windgoto(row, col);
7505
7506 if (screen_attr != attr)
7507 screen_start_highlight(attr);
7508
7509#ifdef FEAT_MBYTE
7510 if (enc_utf8 && ScreenLinesUC[off] != 0)
7511 {
7512 char_u buf[MB_MAXBYTES + 1];
7513
7514 /* Convert UTF-8 character to bytes and write it. */
7515
7516 buf[utfc_char2bytes(off, buf)] = NUL;
7517
7518 out_str(buf);
7519 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7520 ++screen_cur_col;
7521 }
7522 else
7523#endif
7524 {
7525#ifdef FEAT_MBYTE
7526 out_flush_check();
7527#endif
7528 out_char(ScreenLines[off]);
7529#ifdef FEAT_MBYTE
7530 /* double-byte character in single-width cell */
7531 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7532 out_char(ScreenLines2[off]);
7533#endif
7534 }
7535
7536 screen_cur_col++;
7537}
7538
7539#ifdef FEAT_MBYTE
7540
7541/*
7542 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7543 * on the screen at position 'row' and 'col'.
7544 * The attributes of the first byte is used for all. This is required to
7545 * output the two bytes of a double-byte character with nothing in between.
7546 */
7547 static void
7548screen_char_2(off, row, col)
7549 unsigned off;
7550 int row;
7551 int col;
7552{
7553 /* Check for illegal values (could be wrong when screen was resized). */
7554 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7555 return;
7556
7557 /* Outputting the last character on the screen may scrollup the screen.
7558 * Don't to it! Mark the character invalid (update it when scrolled up) */
7559 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7560 {
7561 ScreenAttrs[off] = (sattr_T)-1;
7562 return;
7563 }
7564
7565 /* Output the first byte normally (positions the cursor), then write the
7566 * second byte directly. */
7567 screen_char(off, row, col);
7568 out_char(ScreenLines[off + 1]);
7569 ++screen_cur_col;
7570}
7571#endif
7572
7573#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7574/*
7575 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7576 * This uses the contents of ScreenLines[] and doesn't change it.
7577 */
7578 void
7579screen_draw_rectangle(row, col, height, width, invert)
7580 int row;
7581 int col;
7582 int height;
7583 int width;
7584 int invert;
7585{
7586 int r, c;
7587 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007588#ifdef FEAT_MBYTE
7589 int max_off;
7590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007592 /* Can't use ScreenLines unless initialized */
7593 if (ScreenLines == NULL)
7594 return;
7595
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 if (invert)
7597 screen_char_attr = HL_INVERSE;
7598 for (r = row; r < row + height; ++r)
7599 {
7600 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007601#ifdef FEAT_MBYTE
7602 max_off = off + screen_Columns;
7603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 for (c = col; c < col + width; ++c)
7605 {
7606#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007607 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {
7609 screen_char_2(off + c, r, c);
7610 ++c;
7611 }
7612 else
7613#endif
7614 {
7615 screen_char(off + c, r, c);
7616#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007617 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 ++c;
7619#endif
7620 }
7621 }
7622 }
7623 screen_char_attr = 0;
7624}
7625#endif
7626
7627#ifdef FEAT_VERTSPLIT
7628/*
7629 * Redraw the characters for a vertically split window.
7630 */
7631 static void
7632redraw_block(row, end, wp)
7633 int row;
7634 int end;
7635 win_T *wp;
7636{
7637 int col;
7638 int width;
7639
7640# ifdef FEAT_CLIPBOARD
7641 clip_may_clear_selection(row, end - 1);
7642# endif
7643
7644 if (wp == NULL)
7645 {
7646 col = 0;
7647 width = Columns;
7648 }
7649 else
7650 {
7651 col = wp->w_wincol;
7652 width = wp->w_width;
7653 }
7654 screen_draw_rectangle(row, col, end - row, width, FALSE);
7655}
7656#endif
7657
7658/*
7659 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7660 * with character 'c1' in first column followed by 'c2' in the other columns.
7661 * Use attributes 'attr'.
7662 */
7663 void
7664screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7665 int start_row, end_row;
7666 int start_col, end_col;
7667 int c1, c2;
7668 int attr;
7669{
7670 int row;
7671 int col;
7672 int off;
7673 int end_off;
7674 int did_delete;
7675 int c;
7676 int norm_term;
7677#if defined(FEAT_GUI) || defined(UNIX)
7678 int force_next = FALSE;
7679#endif
7680
7681 if (end_row > screen_Rows) /* safety check */
7682 end_row = screen_Rows;
7683 if (end_col > screen_Columns) /* safety check */
7684 end_col = screen_Columns;
7685 if (ScreenLines == NULL
7686 || start_row >= end_row
7687 || start_col >= end_col) /* nothing to do */
7688 return;
7689
7690 /* it's a "normal" terminal when not in a GUI or cterm */
7691 norm_term = (
7692#ifdef FEAT_GUI
7693 !gui.in_use &&
7694#endif
7695 t_colors <= 1);
7696 for (row = start_row; row < end_row; ++row)
7697 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007698#ifdef FEAT_MBYTE
7699 if (has_mbyte
7700# ifdef FEAT_GUI
7701 && !gui.in_use
7702# endif
7703 )
7704 {
7705 /* When drawing over the right halve of a double-wide char clear
7706 * out the left halve. When drawing over the left halve of a
7707 * double wide-char clear out the right halve. Only needed in a
7708 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007709 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007710 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007711 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007712 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007713 }
7714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 /*
7716 * Try to use delete-line termcap code, when no attributes or in a
7717 * "normal" terminal, where a bold/italic space is just a
7718 * space.
7719 */
7720 did_delete = FALSE;
7721 if (c2 == ' '
7722 && end_col == Columns
7723 && can_clear(T_CE)
7724 && (attr == 0
7725 || (norm_term
7726 && attr <= HL_ALL
7727 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7728 {
7729 /*
7730 * check if we really need to clear something
7731 */
7732 col = start_col;
7733 if (c1 != ' ') /* don't clear first char */
7734 ++col;
7735
7736 off = LineOffset[row] + col;
7737 end_off = LineOffset[row] + end_col;
7738
7739 /* skip blanks (used often, keep it fast!) */
7740#ifdef FEAT_MBYTE
7741 if (enc_utf8)
7742 while (off < end_off && ScreenLines[off] == ' '
7743 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7744 ++off;
7745 else
7746#endif
7747 while (off < end_off && ScreenLines[off] == ' '
7748 && ScreenAttrs[off] == 0)
7749 ++off;
7750 if (off < end_off) /* something to be cleared */
7751 {
7752 col = off - LineOffset[row];
7753 screen_stop_highlight();
7754 term_windgoto(row, col);/* clear rest of this screen line */
7755 out_str(T_CE);
7756 screen_start(); /* don't know where cursor is now */
7757 col = end_col - col;
7758 while (col--) /* clear chars in ScreenLines */
7759 {
7760 ScreenLines[off] = ' ';
7761#ifdef FEAT_MBYTE
7762 if (enc_utf8)
7763 ScreenLinesUC[off] = 0;
7764#endif
7765 ScreenAttrs[off] = 0;
7766 ++off;
7767 }
7768 }
7769 did_delete = TRUE; /* the chars are cleared now */
7770 }
7771
7772 off = LineOffset[row] + start_col;
7773 c = c1;
7774 for (col = start_col; col < end_col; ++col)
7775 {
7776 if (ScreenLines[off] != c
7777#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007778 || (enc_utf8 && (int)ScreenLinesUC[off]
7779 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780#endif
7781 || ScreenAttrs[off] != attr
7782#if defined(FEAT_GUI) || defined(UNIX)
7783 || force_next
7784#endif
7785 )
7786 {
7787#if defined(FEAT_GUI) || defined(UNIX)
7788 /* The bold trick may make a single row of pixels appear in
7789 * the next character. When a bold character is removed, the
7790 * next character should be redrawn too. This happens for our
7791 * own GUI and for some xterms. */
7792 if (
7793# ifdef FEAT_GUI
7794 gui.in_use
7795# endif
7796# if defined(FEAT_GUI) && defined(UNIX)
7797 ||
7798# endif
7799# ifdef UNIX
7800 term_is_xterm
7801# endif
7802 )
7803 {
7804 if (ScreenLines[off] != ' '
7805 && (ScreenAttrs[off] > HL_ALL
7806 || ScreenAttrs[off] & HL_BOLD))
7807 force_next = TRUE;
7808 else
7809 force_next = FALSE;
7810 }
7811#endif
7812 ScreenLines[off] = c;
7813#ifdef FEAT_MBYTE
7814 if (enc_utf8)
7815 {
7816 if (c >= 0x80)
7817 {
7818 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007819 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 }
7821 else
7822 ScreenLinesUC[off] = 0;
7823 }
7824#endif
7825 ScreenAttrs[off] = attr;
7826 if (!did_delete || c != ' ')
7827 screen_char(off, row, col);
7828 }
7829 ++off;
7830 if (col == start_col)
7831 {
7832 if (did_delete)
7833 break;
7834 c = c2;
7835 }
7836 }
7837 if (end_col == Columns)
7838 LineWraps[row] = FALSE;
7839 if (row == Rows - 1) /* overwritten the command line */
7840 {
7841 redraw_cmdline = TRUE;
7842 if (c1 == ' ' && c2 == ' ')
7843 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007844 if (start_col == 0)
7845 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 }
7847 }
7848}
7849
7850/*
7851 * Check if there should be a delay. Used before clearing or redrawing the
7852 * screen or the command line.
7853 */
7854 void
7855check_for_delay(check_msg_scroll)
7856 int check_msg_scroll;
7857{
7858 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7859 && !did_wait_return
7860 && emsg_silent == 0)
7861 {
7862 out_flush();
7863 ui_delay(1000L, TRUE);
7864 emsg_on_display = FALSE;
7865 if (check_msg_scroll)
7866 msg_scroll = FALSE;
7867 }
7868}
7869
7870/*
7871 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007872 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 * Returns TRUE if there is a valid screen to write to.
7874 * Returns FALSE when starting up and screen not initialized yet.
7875 */
7876 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007877screen_valid(doclear)
7878 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007880 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 return (ScreenLines != NULL);
7882}
7883
7884/*
7885 * Resize the shell to Rows and Columns.
7886 * Allocate ScreenLines[] and associated items.
7887 *
7888 * There may be some time between setting Rows and Columns and (re)allocating
7889 * ScreenLines[]. This happens when starting up and when (manually) changing
7890 * the shell size. Always use screen_Rows and screen_Columns to access items
7891 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7892 * final size of the shell is needed.
7893 */
7894 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007895screenalloc(doclear)
7896 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 int new_row, old_row;
7899#ifdef FEAT_GUI
7900 int old_Rows;
7901#endif
7902 win_T *wp;
7903 int outofmem = FALSE;
7904 int len;
7905 schar_T *new_ScreenLines;
7906#ifdef FEAT_MBYTE
7907 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007908 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007910 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911#endif
7912 sattr_T *new_ScreenAttrs;
7913 unsigned *new_LineOffset;
7914 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007915#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007916 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007917 tabpage_T *tp;
7918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007920 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007921#ifdef FEAT_AUTOCMD
7922 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007924retry:
7925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 /*
7927 * Allocation of the screen buffers is done only when the size changes and
7928 * when Rows and Columns have been set and we have started doing full
7929 * screen stuff.
7930 */
7931 if ((ScreenLines != NULL
7932 && Rows == screen_Rows
7933 && Columns == screen_Columns
7934#ifdef FEAT_MBYTE
7935 && enc_utf8 == (ScreenLinesUC != NULL)
7936 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007937 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938#endif
7939 )
7940 || Rows == 0
7941 || Columns == 0
7942 || (!full_screen && ScreenLines == NULL))
7943 return;
7944
7945 /*
7946 * It's possible that we produce an out-of-memory message below, which
7947 * will cause this function to be called again. To break the loop, just
7948 * return here.
7949 */
7950 if (entered)
7951 return;
7952 entered = TRUE;
7953
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007954 /*
7955 * Note that the window sizes are updated before reallocating the arrays,
7956 * thus we must not redraw here!
7957 */
7958 ++RedrawingDisabled;
7959
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 win_new_shellsize(); /* fit the windows in the new sized shell */
7961
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 comp_col(); /* recompute columns for shown command and ruler */
7963
7964 /*
7965 * We're changing the size of the screen.
7966 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7967 * - Move lines from the old arrays into the new arrays, clear extra
7968 * lines (unless the screen is going to be cleared).
7969 * - Free the old arrays.
7970 *
7971 * If anything fails, make ScreenLines NULL, so we don't do anything!
7972 * Continuing with the old ScreenLines may result in a crash, because the
7973 * size is wrong.
7974 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007975 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007977#ifdef FEAT_AUTOCMD
7978 if (aucmd_win != NULL)
7979 win_free_lsize(aucmd_win);
7980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981
7982 new_ScreenLines = (schar_T *)lalloc((long_u)(
7983 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7984#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007985 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 if (enc_utf8)
7987 {
7988 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7989 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007990 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007991 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7993 }
7994 if (enc_dbcs == DBCS_JPNU)
7995 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7996 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7997#endif
7998 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7999 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8000 new_LineOffset = (unsigned *)lalloc((long_u)(
8001 Rows * sizeof(unsigned)), FALSE);
8002 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008003#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008004 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008007 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
8009 if (win_alloc_lines(wp) == FAIL)
8010 {
8011 outofmem = TRUE;
8012#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008013 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014#endif
8015 }
8016 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008017#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008018 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8019 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008020 outofmem = TRUE;
8021#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008022#ifdef FEAT_WINDOWS
8023give_up:
8024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008026#ifdef FEAT_MBYTE
8027 for (i = 0; i < p_mco; ++i)
8028 if (new_ScreenLinesC[i] == NULL)
8029 break;
8030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 if (new_ScreenLines == NULL
8032#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008033 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8035#endif
8036 || new_ScreenAttrs == NULL
8037 || new_LineOffset == NULL
8038 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008039#ifdef FEAT_WINDOWS
8040 || new_TabPageIdxs == NULL
8041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 || outofmem)
8043 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008044 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008045 {
8046 /* guess the size */
8047 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8048
8049 /* Remember we did this to avoid getting outofmem messages over
8050 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008051 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 vim_free(new_ScreenLines);
8054 new_ScreenLines = NULL;
8055#ifdef FEAT_MBYTE
8056 vim_free(new_ScreenLinesUC);
8057 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008058 for (i = 0; i < p_mco; ++i)
8059 {
8060 vim_free(new_ScreenLinesC[i]);
8061 new_ScreenLinesC[i] = NULL;
8062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 vim_free(new_ScreenLines2);
8064 new_ScreenLines2 = NULL;
8065#endif
8066 vim_free(new_ScreenAttrs);
8067 new_ScreenAttrs = NULL;
8068 vim_free(new_LineOffset);
8069 new_LineOffset = NULL;
8070 vim_free(new_LineWraps);
8071 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008072#ifdef FEAT_WINDOWS
8073 vim_free(new_TabPageIdxs);
8074 new_TabPageIdxs = NULL;
8075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 }
8077 else
8078 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008079 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008080
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 for (new_row = 0; new_row < Rows; ++new_row)
8082 {
8083 new_LineOffset[new_row] = new_row * Columns;
8084 new_LineWraps[new_row] = FALSE;
8085
8086 /*
8087 * If the screen is not going to be cleared, copy as much as
8088 * possible from the old screen to the new one and clear the rest
8089 * (used when resizing the window at the "--more--" prompt or when
8090 * executing an external command, for the GUI).
8091 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008092 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {
8094 (void)vim_memset(new_ScreenLines + new_row * Columns,
8095 ' ', (size_t)Columns * sizeof(schar_T));
8096#ifdef FEAT_MBYTE
8097 if (enc_utf8)
8098 {
8099 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8100 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008101 for (i = 0; i < p_mco; ++i)
8102 (void)vim_memset(new_ScreenLinesC[i]
8103 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 0, (size_t)Columns * sizeof(u8char_T));
8105 }
8106 if (enc_dbcs == DBCS_JPNU)
8107 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8108 0, (size_t)Columns * sizeof(schar_T));
8109#endif
8110 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8111 0, (size_t)Columns * sizeof(sattr_T));
8112 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008113 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
8115 if (screen_Columns < Columns)
8116 len = screen_Columns;
8117 else
8118 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008119#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008120 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008121 * may be invalid now. Also when p_mco changes. */
8122 if (!(enc_utf8 && ScreenLinesUC == NULL)
8123 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008124#endif
8125 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8126 ScreenLines + LineOffset[old_row],
8127 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008129 if (enc_utf8 && ScreenLinesUC != NULL
8130 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {
8132 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8133 ScreenLinesUC + LineOffset[old_row],
8134 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008135 for (i = 0; i < p_mco; ++i)
8136 mch_memmove(new_ScreenLinesC[i]
8137 + new_LineOffset[new_row],
8138 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 (size_t)len * sizeof(u8char_T));
8140 }
8141 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8142 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8143 ScreenLines2 + LineOffset[old_row],
8144 (size_t)len * sizeof(schar_T));
8145#endif
8146 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8147 ScreenAttrs + LineOffset[old_row],
8148 (size_t)len * sizeof(sattr_T));
8149 }
8150 }
8151 }
8152 /* Use the last line of the screen for the current line. */
8153 current_ScreenLine = new_ScreenLines + Rows * Columns;
8154 }
8155
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008156 free_screenlines();
8157
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 ScreenLines = new_ScreenLines;
8159#ifdef FEAT_MBYTE
8160 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008161 for (i = 0; i < p_mco; ++i)
8162 ScreenLinesC[i] = new_ScreenLinesC[i];
8163 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 ScreenLines2 = new_ScreenLines2;
8165#endif
8166 ScreenAttrs = new_ScreenAttrs;
8167 LineOffset = new_LineOffset;
8168 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008169#ifdef FEAT_WINDOWS
8170 TabPageIdxs = new_TabPageIdxs;
8171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172
8173 /* It's important that screen_Rows and screen_Columns reflect the actual
8174 * size of ScreenLines[]. Set them before calling anything. */
8175#ifdef FEAT_GUI
8176 old_Rows = screen_Rows;
8177#endif
8178 screen_Rows = Rows;
8179 screen_Columns = Columns;
8180
8181 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008182 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 screenclear2();
8184
8185#ifdef FEAT_GUI
8186 else if (gui.in_use
8187 && !gui.starting
8188 && ScreenLines != NULL
8189 && old_Rows != Rows)
8190 {
8191 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8192 /*
8193 * Adjust the position of the cursor, for when executing an external
8194 * command.
8195 */
8196 if (msg_row >= Rows) /* Rows got smaller */
8197 msg_row = Rows - 1; /* put cursor at last row */
8198 else if (Rows > old_Rows) /* Rows got bigger */
8199 msg_row += Rows - old_Rows; /* put cursor in same place */
8200 if (msg_col >= Columns) /* Columns got smaller */
8201 msg_col = Columns - 1; /* put cursor at last column */
8202 }
8203#endif
8204
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008206 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008207
8208#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008209 /*
8210 * Do not apply autocommands more than 3 times to avoid an endless loop
8211 * in case applying autocommands always changes Rows or Columns.
8212 */
8213 if (starting == 0 && ++retry_count <= 3)
8214 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008215 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008216 /* In rare cases, autocommands may have altered Rows or Columns,
8217 * jump back to check if we need to allocate the screen again. */
8218 goto retry;
8219 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221}
8222
8223 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008224free_screenlines()
8225{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008226#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008227 int i;
8228
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008229 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008230 for (i = 0; i < Screen_mco; ++i)
8231 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008232 vim_free(ScreenLines2);
8233#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008234 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008235 vim_free(ScreenAttrs);
8236 vim_free(LineOffset);
8237 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008238#ifdef FEAT_WINDOWS
8239 vim_free(TabPageIdxs);
8240#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008241}
8242
8243 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244screenclear()
8245{
8246 check_for_delay(FALSE);
8247 screenalloc(FALSE); /* allocate screen buffers if size changed */
8248 screenclear2(); /* clear the screen */
8249}
8250
8251 static void
8252screenclear2()
8253{
8254 int i;
8255
8256 if (starting == NO_SCREEN || ScreenLines == NULL
8257#ifdef FEAT_GUI
8258 || (gui.in_use && gui.starting)
8259#endif
8260 )
8261 return;
8262
8263#ifdef FEAT_GUI
8264 if (!gui.in_use)
8265#endif
8266 screen_attr = -1; /* force setting the Normal colors */
8267 screen_stop_highlight(); /* don't want highlighting here */
8268
8269#ifdef FEAT_CLIPBOARD
8270 /* disable selection without redrawing it */
8271 clip_scroll_selection(9999);
8272#endif
8273
8274 /* blank out ScreenLines */
8275 for (i = 0; i < Rows; ++i)
8276 {
8277 lineclear(LineOffset[i], (int)Columns);
8278 LineWraps[i] = FALSE;
8279 }
8280
8281 if (can_clear(T_CL))
8282 {
8283 out_str(T_CL); /* clear the display */
8284 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008285 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 }
8287 else
8288 {
8289 /* can't clear the screen, mark all chars with invalid attributes */
8290 for (i = 0; i < Rows; ++i)
8291 lineinvalid(LineOffset[i], (int)Columns);
8292 clear_cmdline = TRUE;
8293 }
8294
8295 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8296
8297 win_rest_invalid(firstwin);
8298 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008299#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008300 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (must_redraw == CLEAR) /* no need to clear again */
8303 must_redraw = NOT_VALID;
8304 compute_cmdrow();
8305 msg_row = cmdline_row; /* put cursor on last line for messages */
8306 msg_col = 0;
8307 screen_start(); /* don't know where cursor is now */
8308 msg_scrolled = 0; /* can't scroll back */
8309 msg_didany = FALSE;
8310 msg_didout = FALSE;
8311}
8312
8313/*
8314 * Clear one line in ScreenLines.
8315 */
8316 static void
8317lineclear(off, width)
8318 unsigned off;
8319 int width;
8320{
8321 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8322#ifdef FEAT_MBYTE
8323 if (enc_utf8)
8324 (void)vim_memset(ScreenLinesUC + off, 0,
8325 (size_t)width * sizeof(u8char_T));
8326#endif
8327 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8328}
8329
8330/*
8331 * Mark one line in ScreenLines invalid by setting the attributes to an
8332 * invalid value.
8333 */
8334 static void
8335lineinvalid(off, width)
8336 unsigned off;
8337 int width;
8338{
8339 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8340}
8341
8342#ifdef FEAT_VERTSPLIT
8343/*
8344 * Copy part of a Screenline for vertically split window "wp".
8345 */
8346 static void
8347linecopy(to, from, wp)
8348 int to;
8349 int from;
8350 win_T *wp;
8351{
8352 unsigned off_to = LineOffset[to] + wp->w_wincol;
8353 unsigned off_from = LineOffset[from] + wp->w_wincol;
8354
8355 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8356 wp->w_width * sizeof(schar_T));
8357# ifdef FEAT_MBYTE
8358 if (enc_utf8)
8359 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008360 int i;
8361
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8363 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008364 for (i = 0; i < p_mco; ++i)
8365 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8366 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 }
8368 if (enc_dbcs == DBCS_JPNU)
8369 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8370 wp->w_width * sizeof(schar_T));
8371# endif
8372 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8373 wp->w_width * sizeof(sattr_T));
8374}
8375#endif
8376
8377/*
8378 * Return TRUE if clearing with term string "p" would work.
8379 * It can't work when the string is empty or it won't set the right background.
8380 */
8381 int
8382can_clear(p)
8383 char_u *p;
8384{
8385 return (*p != NUL && (t_colors <= 1
8386#ifdef FEAT_GUI
8387 || gui.in_use
8388#endif
8389 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8390}
8391
8392/*
8393 * Reset cursor position. Use whenever cursor was moved because of outputting
8394 * something directly to the screen (shell commands) or a terminal control
8395 * code.
8396 */
8397 void
8398screen_start()
8399{
8400 screen_cur_row = screen_cur_col = 9999;
8401}
8402
8403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 * Move the cursor to position "row","col" in the screen.
8405 * This tries to find the most efficient way to move, minimizing the number of
8406 * characters sent to the terminal.
8407 */
8408 void
8409windgoto(row, col)
8410 int row;
8411 int col;
8412{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008413 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 int i;
8415 int plan;
8416 int cost;
8417 int wouldbe_col;
8418 int noinvcurs;
8419 char_u *bs;
8420 int goto_cost;
8421 int attr;
8422
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008423#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8425
8426#define PLAN_LE 1
8427#define PLAN_CR 2
8428#define PLAN_NL 3
8429#define PLAN_WRITE 4
8430 /* Can't use ScreenLines unless initialized */
8431 if (ScreenLines == NULL)
8432 return;
8433
8434 if (col != screen_cur_col || row != screen_cur_row)
8435 {
8436 /* Check for valid position. */
8437 if (row < 0) /* window without text lines? */
8438 row = 0;
8439 if (row >= screen_Rows)
8440 row = screen_Rows - 1;
8441 if (col >= screen_Columns)
8442 col = screen_Columns - 1;
8443
8444 /* check if no cursor movement is allowed in highlight mode */
8445 if (screen_attr && *T_MS == NUL)
8446 noinvcurs = HIGHL_COST;
8447 else
8448 noinvcurs = 0;
8449 goto_cost = GOTO_COST + noinvcurs;
8450
8451 /*
8452 * Plan how to do the positioning:
8453 * 1. Use CR to move it to column 0, same row.
8454 * 2. Use T_LE to move it a few columns to the left.
8455 * 3. Use NL to move a few lines down, column 0.
8456 * 4. Move a few columns to the right with T_ND or by writing chars.
8457 *
8458 * Don't do this if the cursor went beyond the last column, the cursor
8459 * position is unknown then (some terminals wrap, some don't )
8460 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008461 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 * characters to move the cursor to the right.
8463 */
8464 if (row >= screen_cur_row && screen_cur_col < Columns)
8465 {
8466 /*
8467 * If the cursor is in the same row, bigger col, we can use CR
8468 * or T_LE.
8469 */
8470 bs = NULL; /* init for GCC */
8471 attr = screen_attr;
8472 if (row == screen_cur_row && col < screen_cur_col)
8473 {
8474 /* "le" is preferred over "bc", because "bc" is obsolete */
8475 if (*T_LE)
8476 bs = T_LE; /* "cursor left" */
8477 else
8478 bs = T_BC; /* "backspace character (old) */
8479 if (*bs)
8480 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8481 else
8482 cost = 999;
8483 if (col + 1 < cost) /* using CR is less characters */
8484 {
8485 plan = PLAN_CR;
8486 wouldbe_col = 0;
8487 cost = 1; /* CR is just one character */
8488 }
8489 else
8490 {
8491 plan = PLAN_LE;
8492 wouldbe_col = col;
8493 }
8494 if (noinvcurs) /* will stop highlighting */
8495 {
8496 cost += noinvcurs;
8497 attr = 0;
8498 }
8499 }
8500
8501 /*
8502 * If the cursor is above where we want to be, we can use CR LF.
8503 */
8504 else if (row > screen_cur_row)
8505 {
8506 plan = PLAN_NL;
8507 wouldbe_col = 0;
8508 cost = (row - screen_cur_row) * 2; /* CR LF */
8509 if (noinvcurs) /* will stop highlighting */
8510 {
8511 cost += noinvcurs;
8512 attr = 0;
8513 }
8514 }
8515
8516 /*
8517 * If the cursor is in the same row, smaller col, just use write.
8518 */
8519 else
8520 {
8521 plan = PLAN_WRITE;
8522 wouldbe_col = screen_cur_col;
8523 cost = 0;
8524 }
8525
8526 /*
8527 * Check if any characters that need to be written have the
8528 * correct attributes. Also avoid UTF-8 characters.
8529 */
8530 i = col - wouldbe_col;
8531 if (i > 0)
8532 cost += i;
8533 if (cost < goto_cost && i > 0)
8534 {
8535 /*
8536 * Check if the attributes are correct without additionally
8537 * stopping highlighting.
8538 */
8539 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8540 while (i && *p++ == attr)
8541 --i;
8542 if (i != 0)
8543 {
8544 /*
8545 * Try if it works when highlighting is stopped here.
8546 */
8547 if (*--p == 0)
8548 {
8549 cost += noinvcurs;
8550 while (i && *p++ == 0)
8551 --i;
8552 }
8553 if (i != 0)
8554 cost = 999; /* different attributes, don't do it */
8555 }
8556#ifdef FEAT_MBYTE
8557 if (enc_utf8)
8558 {
8559 /* Don't use an UTF-8 char for positioning, it's slow. */
8560 for (i = wouldbe_col; i < col; ++i)
8561 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8562 {
8563 cost = 999;
8564 break;
8565 }
8566 }
8567#endif
8568 }
8569
8570 /*
8571 * We can do it without term_windgoto()!
8572 */
8573 if (cost < goto_cost)
8574 {
8575 if (plan == PLAN_LE)
8576 {
8577 if (noinvcurs)
8578 screen_stop_highlight();
8579 while (screen_cur_col > col)
8580 {
8581 out_str(bs);
8582 --screen_cur_col;
8583 }
8584 }
8585 else if (plan == PLAN_CR)
8586 {
8587 if (noinvcurs)
8588 screen_stop_highlight();
8589 out_char('\r');
8590 screen_cur_col = 0;
8591 }
8592 else if (plan == PLAN_NL)
8593 {
8594 if (noinvcurs)
8595 screen_stop_highlight();
8596 while (screen_cur_row < row)
8597 {
8598 out_char('\n');
8599 ++screen_cur_row;
8600 }
8601 screen_cur_col = 0;
8602 }
8603
8604 i = col - screen_cur_col;
8605 if (i > 0)
8606 {
8607 /*
8608 * Use cursor-right if it's one character only. Avoids
8609 * removing a line of pixels from the last bold char, when
8610 * using the bold trick in the GUI.
8611 */
8612 if (T_ND[0] != NUL && T_ND[1] == NUL)
8613 {
8614 while (i-- > 0)
8615 out_char(*T_ND);
8616 }
8617 else
8618 {
8619 int off;
8620
8621 off = LineOffset[row] + screen_cur_col;
8622 while (i-- > 0)
8623 {
8624 if (ScreenAttrs[off] != screen_attr)
8625 screen_stop_highlight();
8626#ifdef FEAT_MBYTE
8627 out_flush_check();
8628#endif
8629 out_char(ScreenLines[off]);
8630#ifdef FEAT_MBYTE
8631 if (enc_dbcs == DBCS_JPNU
8632 && ScreenLines[off] == 0x8e)
8633 out_char(ScreenLines2[off]);
8634#endif
8635 ++off;
8636 }
8637 }
8638 }
8639 }
8640 }
8641 else
8642 cost = 999;
8643
8644 if (cost >= goto_cost)
8645 {
8646 if (noinvcurs)
8647 screen_stop_highlight();
8648 if (row == screen_cur_row && (col > screen_cur_col) &&
8649 *T_CRI != NUL)
8650 term_cursor_right(col - screen_cur_col);
8651 else
8652 term_windgoto(row, col);
8653 }
8654 screen_cur_row = row;
8655 screen_cur_col = col;
8656 }
8657}
8658
8659/*
8660 * Set cursor to its position in the current window.
8661 */
8662 void
8663setcursor()
8664{
8665 if (redrawing())
8666 {
8667 validate_cursor();
8668 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8669 W_WINCOL(curwin) + (
8670#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008671 /* With 'rightleft' set and the cursor on a double-wide
8672 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8674# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008675 (has_mbyte
8676 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8677 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678# endif
8679 1)) :
8680#endif
8681 curwin->w_wcol));
8682 }
8683}
8684
8685
8686/*
8687 * insert 'line_count' lines at 'row' in window 'wp'
8688 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8689 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8690 * scrolling.
8691 * Returns FAIL if the lines are not inserted, OK for success.
8692 */
8693 int
8694win_ins_lines(wp, row, line_count, invalid, mayclear)
8695 win_T *wp;
8696 int row;
8697 int line_count;
8698 int invalid;
8699 int mayclear;
8700{
8701 int did_delete;
8702 int nextrow;
8703 int lastrow;
8704 int retval;
8705
8706 if (invalid)
8707 wp->w_lines_valid = 0;
8708
8709 if (wp->w_height < 5)
8710 return FAIL;
8711
8712 if (line_count > wp->w_height - row)
8713 line_count = wp->w_height - row;
8714
8715 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8716 if (retval != MAYBE)
8717 return retval;
8718
8719 /*
8720 * If there is a next window or a status line, we first try to delete the
8721 * lines at the bottom to avoid messing what is after the window.
8722 * If this fails and there are following windows, don't do anything to avoid
8723 * messing up those windows, better just redraw.
8724 */
8725 did_delete = FALSE;
8726#ifdef FEAT_WINDOWS
8727 if (wp->w_next != NULL || wp->w_status_height)
8728 {
8729 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8730 line_count, (int)Rows, FALSE, NULL) == OK)
8731 did_delete = TRUE;
8732 else if (wp->w_next)
8733 return FAIL;
8734 }
8735#endif
8736 /*
8737 * if no lines deleted, blank the lines that will end up below the window
8738 */
8739 if (!did_delete)
8740 {
8741#ifdef FEAT_WINDOWS
8742 wp->w_redr_status = TRUE;
8743#endif
8744 redraw_cmdline = TRUE;
8745 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8746 lastrow = nextrow + line_count;
8747 if (lastrow > Rows)
8748 lastrow = Rows;
8749 screen_fill(nextrow - line_count, lastrow - line_count,
8750 W_WINCOL(wp), (int)W_ENDCOL(wp),
8751 ' ', ' ', 0);
8752 }
8753
8754 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8755 == FAIL)
8756 {
8757 /* deletion will have messed up other windows */
8758 if (did_delete)
8759 {
8760#ifdef FEAT_WINDOWS
8761 wp->w_redr_status = TRUE;
8762#endif
8763 win_rest_invalid(W_NEXT(wp));
8764 }
8765 return FAIL;
8766 }
8767
8768 return OK;
8769}
8770
8771/*
8772 * delete "line_count" window lines at "row" in window "wp"
8773 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8774 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8775 * scrolling
8776 * Return OK for success, FAIL if the lines are not deleted.
8777 */
8778 int
8779win_del_lines(wp, row, line_count, invalid, mayclear)
8780 win_T *wp;
8781 int row;
8782 int line_count;
8783 int invalid;
8784 int mayclear;
8785{
8786 int retval;
8787
8788 if (invalid)
8789 wp->w_lines_valid = 0;
8790
8791 if (line_count > wp->w_height - row)
8792 line_count = wp->w_height - row;
8793
8794 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8795 if (retval != MAYBE)
8796 return retval;
8797
8798 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8799 (int)Rows, FALSE, NULL) == FAIL)
8800 return FAIL;
8801
8802#ifdef FEAT_WINDOWS
8803 /*
8804 * If there are windows or status lines below, try to put them at the
8805 * correct place. If we can't do that, they have to be redrawn.
8806 */
8807 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8808 {
8809 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8810 line_count, (int)Rows, NULL) == FAIL)
8811 {
8812 wp->w_redr_status = TRUE;
8813 win_rest_invalid(wp->w_next);
8814 }
8815 }
8816 /*
8817 * If this is the last window and there is no status line, redraw the
8818 * command line later.
8819 */
8820 else
8821#endif
8822 redraw_cmdline = TRUE;
8823 return OK;
8824}
8825
8826/*
8827 * Common code for win_ins_lines() and win_del_lines().
8828 * Returns OK or FAIL when the work has been done.
8829 * Returns MAYBE when not finished yet.
8830 */
8831 static int
8832win_do_lines(wp, row, line_count, mayclear, del)
8833 win_T *wp;
8834 int row;
8835 int line_count;
8836 int mayclear;
8837 int del;
8838{
8839 int retval;
8840
8841 if (!redrawing() || line_count <= 0)
8842 return FAIL;
8843
8844 /* only a few lines left: redraw is faster */
8845 if (mayclear && Rows - line_count < 5
8846#ifdef FEAT_VERTSPLIT
8847 && wp->w_width == Columns
8848#endif
8849 )
8850 {
8851 screenclear(); /* will set wp->w_lines_valid to 0 */
8852 return FAIL;
8853 }
8854
8855 /*
8856 * Delete all remaining lines
8857 */
8858 if (row + line_count >= wp->w_height)
8859 {
8860 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8861 W_WINCOL(wp), (int)W_ENDCOL(wp),
8862 ' ', ' ', 0);
8863 return OK;
8864 }
8865
8866 /*
8867 * when scrolling, the message on the command line should be cleared,
8868 * otherwise it will stay there forever.
8869 */
8870 clear_cmdline = TRUE;
8871
8872 /*
8873 * If the terminal can set a scroll region, use that.
8874 * Always do this in a vertically split window. This will redraw from
8875 * ScreenLines[] when t_CV isn't defined. That's faster than using
8876 * win_line().
8877 * Don't use a scroll region when we are going to redraw the text, writing
8878 * a character in the lower right corner of the scroll region causes a
8879 * scroll-up in the DJGPP version.
8880 */
8881 if (scroll_region
8882#ifdef FEAT_VERTSPLIT
8883 || W_WIDTH(wp) != Columns
8884#endif
8885 )
8886 {
8887#ifdef FEAT_VERTSPLIT
8888 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8889#endif
8890 scroll_region_set(wp, row);
8891 if (del)
8892 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8893 wp->w_height - row, FALSE, wp);
8894 else
8895 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8896 wp->w_height - row, wp);
8897#ifdef FEAT_VERTSPLIT
8898 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8899#endif
8900 scroll_region_reset();
8901 return retval;
8902 }
8903
8904#ifdef FEAT_WINDOWS
8905 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8906 return FAIL;
8907#endif
8908
8909 return MAYBE;
8910}
8911
8912/*
8913 * window 'wp' and everything after it is messed up, mark it for redraw
8914 */
8915 static void
8916win_rest_invalid(wp)
8917 win_T *wp;
8918{
8919#ifdef FEAT_WINDOWS
8920 while (wp != NULL)
8921#else
8922 if (wp != NULL)
8923#endif
8924 {
8925 redraw_win_later(wp, NOT_VALID);
8926#ifdef FEAT_WINDOWS
8927 wp->w_redr_status = TRUE;
8928 wp = wp->w_next;
8929#endif
8930 }
8931 redraw_cmdline = TRUE;
8932}
8933
8934/*
8935 * The rest of the routines in this file perform screen manipulations. The
8936 * given operation is performed physically on the screen. The corresponding
8937 * change is also made to the internal screen image. In this way, the editor
8938 * anticipates the effect of editing changes on the appearance of the screen.
8939 * That way, when we call screenupdate a complete redraw isn't usually
8940 * necessary. Another advantage is that we can keep adding code to anticipate
8941 * screen changes, and in the meantime, everything still works.
8942 */
8943
8944/*
8945 * types for inserting or deleting lines
8946 */
8947#define USE_T_CAL 1
8948#define USE_T_CDL 2
8949#define USE_T_AL 3
8950#define USE_T_CE 4
8951#define USE_T_DL 5
8952#define USE_T_SR 6
8953#define USE_NL 7
8954#define USE_T_CD 8
8955#define USE_REDRAW 9
8956
8957/*
8958 * insert lines on the screen and update ScreenLines[]
8959 * 'end' is the line after the scrolled part. Normally it is Rows.
8960 * When scrolling region used 'off' is the offset from the top for the region.
8961 * 'row' and 'end' are relative to the start of the region.
8962 *
8963 * return FAIL for failure, OK for success.
8964 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008965 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966screen_ins_lines(off, row, line_count, end, wp)
8967 int off;
8968 int row;
8969 int line_count;
8970 int end;
8971 win_T *wp; /* NULL or window to use width from */
8972{
8973 int i;
8974 int j;
8975 unsigned temp;
8976 int cursor_row;
8977 int type;
8978 int result_empty;
8979 int can_ce = can_clear(T_CE);
8980
8981 /*
8982 * FAIL if
8983 * - there is no valid screen
8984 * - the screen has to be redrawn completely
8985 * - the line count is less than one
8986 * - the line count is more than 'ttyscroll'
8987 */
8988 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8989 return FAIL;
8990
8991 /*
8992 * There are seven ways to insert lines:
8993 * 0. When in a vertically split window and t_CV isn't set, redraw the
8994 * characters from ScreenLines[].
8995 * 1. Use T_CD (clear to end of display) if it exists and the result of
8996 * the insert is just empty lines
8997 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8998 * present or line_count > 1. It looks better if we do all the inserts
8999 * at once.
9000 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9001 * insert is just empty lines and T_CE is not present or line_count >
9002 * 1.
9003 * 4. Use T_AL (insert line) if it exists.
9004 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9005 * just empty lines.
9006 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9007 * just empty lines.
9008 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9009 * the 'da' flag is not set or we have clear line capability.
9010 * 8. redraw the characters from ScreenLines[].
9011 *
9012 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9013 * the scrollbar for the window. It does have insert line, use that if it
9014 * exists.
9015 */
9016 result_empty = (row + line_count >= end);
9017#ifdef FEAT_VERTSPLIT
9018 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9019 type = USE_REDRAW;
9020 else
9021#endif
9022 if (can_clear(T_CD) && result_empty)
9023 type = USE_T_CD;
9024 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9025 type = USE_T_CAL;
9026 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9027 type = USE_T_CDL;
9028 else if (*T_AL != NUL)
9029 type = USE_T_AL;
9030 else if (can_ce && result_empty)
9031 type = USE_T_CE;
9032 else if (*T_DL != NUL && result_empty)
9033 type = USE_T_DL;
9034 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9035 type = USE_T_SR;
9036 else
9037 return FAIL;
9038
9039 /*
9040 * For clearing the lines screen_del_lines() is used. This will also take
9041 * care of t_db if necessary.
9042 */
9043 if (type == USE_T_CD || type == USE_T_CDL ||
9044 type == USE_T_CE || type == USE_T_DL)
9045 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9046
9047 /*
9048 * If text is retained below the screen, first clear or delete as many
9049 * lines at the bottom of the window as are about to be inserted so that
9050 * the deleted lines won't later surface during a screen_del_lines.
9051 */
9052 if (*T_DB)
9053 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9054
9055#ifdef FEAT_CLIPBOARD
9056 /* Remove a modeless selection when inserting lines halfway the screen
9057 * or not the full width of the screen. */
9058 if (off + row > 0
9059# ifdef FEAT_VERTSPLIT
9060 || (wp != NULL && wp->w_width != Columns)
9061# endif
9062 )
9063 clip_clear_selection();
9064 else
9065 clip_scroll_selection(-line_count);
9066#endif
9067
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068#ifdef FEAT_GUI
9069 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9070 * scrolling is actually carried out. */
9071 gui_dont_update_cursor();
9072#endif
9073
9074 if (*T_CCS != NUL) /* cursor relative to region */
9075 cursor_row = row;
9076 else
9077 cursor_row = row + off;
9078
9079 /*
9080 * Shift LineOffset[] line_count down to reflect the inserted lines.
9081 * Clear the inserted lines in ScreenLines[].
9082 */
9083 row += off;
9084 end += off;
9085 for (i = 0; i < line_count; ++i)
9086 {
9087#ifdef FEAT_VERTSPLIT
9088 if (wp != NULL && wp->w_width != Columns)
9089 {
9090 /* need to copy part of a line */
9091 j = end - 1 - i;
9092 while ((j -= line_count) >= row)
9093 linecopy(j + line_count, j, wp);
9094 j += line_count;
9095 if (can_clear((char_u *)" "))
9096 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9097 else
9098 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9099 LineWraps[j] = FALSE;
9100 }
9101 else
9102#endif
9103 {
9104 j = end - 1 - i;
9105 temp = LineOffset[j];
9106 while ((j -= line_count) >= row)
9107 {
9108 LineOffset[j + line_count] = LineOffset[j];
9109 LineWraps[j + line_count] = LineWraps[j];
9110 }
9111 LineOffset[j + line_count] = temp;
9112 LineWraps[j + line_count] = FALSE;
9113 if (can_clear((char_u *)" "))
9114 lineclear(temp, (int)Columns);
9115 else
9116 lineinvalid(temp, (int)Columns);
9117 }
9118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119
9120 screen_stop_highlight();
9121 windgoto(cursor_row, 0);
9122
9123#ifdef FEAT_VERTSPLIT
9124 /* redraw the characters */
9125 if (type == USE_REDRAW)
9126 redraw_block(row, end, wp);
9127 else
9128#endif
9129 if (type == USE_T_CAL)
9130 {
9131 term_append_lines(line_count);
9132 screen_start(); /* don't know where cursor is now */
9133 }
9134 else
9135 {
9136 for (i = 0; i < line_count; i++)
9137 {
9138 if (type == USE_T_AL)
9139 {
9140 if (i && cursor_row != 0)
9141 windgoto(cursor_row, 0);
9142 out_str(T_AL);
9143 }
9144 else /* type == USE_T_SR */
9145 out_str(T_SR);
9146 screen_start(); /* don't know where cursor is now */
9147 }
9148 }
9149
9150 /*
9151 * With scroll-reverse and 'da' flag set we need to clear the lines that
9152 * have been scrolled down into the region.
9153 */
9154 if (type == USE_T_SR && *T_DA)
9155 {
9156 for (i = 0; i < line_count; ++i)
9157 {
9158 windgoto(off + i, 0);
9159 out_str(T_CE);
9160 screen_start(); /* don't know where cursor is now */
9161 }
9162 }
9163
9164#ifdef FEAT_GUI
9165 gui_can_update_cursor();
9166 if (gui.in_use)
9167 out_flush(); /* always flush after a scroll */
9168#endif
9169 return OK;
9170}
9171
9172/*
9173 * delete lines on the screen and update ScreenLines[]
9174 * 'end' is the line after the scrolled part. Normally it is Rows.
9175 * When scrolling region used 'off' is the offset from the top for the region.
9176 * 'row' and 'end' are relative to the start of the region.
9177 *
9178 * Return OK for success, FAIL if the lines are not deleted.
9179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 int
9181screen_del_lines(off, row, line_count, end, force, wp)
9182 int off;
9183 int row;
9184 int line_count;
9185 int end;
9186 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009187 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
9189 int j;
9190 int i;
9191 unsigned temp;
9192 int cursor_row;
9193 int cursor_end;
9194 int result_empty; /* result is empty until end of region */
9195 int can_delete; /* deleting line codes can be used */
9196 int type;
9197
9198 /*
9199 * FAIL if
9200 * - there is no valid screen
9201 * - the screen has to be redrawn completely
9202 * - the line count is less than one
9203 * - the line count is more than 'ttyscroll'
9204 */
9205 if (!screen_valid(TRUE) || line_count <= 0 ||
9206 (!force && line_count > p_ttyscroll))
9207 return FAIL;
9208
9209 /*
9210 * Check if the rest of the current region will become empty.
9211 */
9212 result_empty = row + line_count >= end;
9213
9214 /*
9215 * We can delete lines only when 'db' flag not set or when 'ce' option
9216 * available.
9217 */
9218 can_delete = (*T_DB == NUL || can_clear(T_CE));
9219
9220 /*
9221 * There are six ways to delete lines:
9222 * 0. When in a vertically split window and t_CV isn't set, redraw the
9223 * characters from ScreenLines[].
9224 * 1. Use T_CD if it exists and the result is empty.
9225 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9226 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9227 * none of the other ways work.
9228 * 4. Use T_CE (erase line) if the result is empty.
9229 * 5. Use T_DL (delete line) if it exists.
9230 * 6. redraw the characters from ScreenLines[].
9231 */
9232#ifdef FEAT_VERTSPLIT
9233 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9234 type = USE_REDRAW;
9235 else
9236#endif
9237 if (can_clear(T_CD) && result_empty)
9238 type = USE_T_CD;
9239#if defined(__BEOS__) && defined(BEOS_DR8)
9240 /*
9241 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9242 * its internal termcap... this works okay for tests which test *T_DB !=
9243 * NUL. It has the disadvantage that the user cannot use any :set t_*
9244 * command to get T_DB (back) to empty_option, only :set term=... will do
9245 * the trick...
9246 * Anyway, this hack will hopefully go away with the next OS release.
9247 * (Olaf Seibert)
9248 */
9249 else if (row == 0 && T_DB == empty_option
9250 && (line_count == 1 || *T_CDL == NUL))
9251#else
9252 else if (row == 0 && (
9253#ifndef AMIGA
9254 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9255 * up, so use delete-line command */
9256 line_count == 1 ||
9257#endif
9258 *T_CDL == NUL))
9259#endif
9260 type = USE_NL;
9261 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9262 type = USE_T_CDL;
9263 else if (can_clear(T_CE) && result_empty
9264#ifdef FEAT_VERTSPLIT
9265 && (wp == NULL || wp->w_width == Columns)
9266#endif
9267 )
9268 type = USE_T_CE;
9269 else if (*T_DL != NUL && can_delete)
9270 type = USE_T_DL;
9271 else if (*T_CDL != NUL && can_delete)
9272 type = USE_T_CDL;
9273 else
9274 return FAIL;
9275
9276#ifdef FEAT_CLIPBOARD
9277 /* Remove a modeless selection when deleting lines halfway the screen or
9278 * not the full width of the screen. */
9279 if (off + row > 0
9280# ifdef FEAT_VERTSPLIT
9281 || (wp != NULL && wp->w_width != Columns)
9282# endif
9283 )
9284 clip_clear_selection();
9285 else
9286 clip_scroll_selection(line_count);
9287#endif
9288
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289#ifdef FEAT_GUI
9290 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9291 * scrolling is actually carried out. */
9292 gui_dont_update_cursor();
9293#endif
9294
9295 if (*T_CCS != NUL) /* cursor relative to region */
9296 {
9297 cursor_row = row;
9298 cursor_end = end;
9299 }
9300 else
9301 {
9302 cursor_row = row + off;
9303 cursor_end = end + off;
9304 }
9305
9306 /*
9307 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9308 * Clear the inserted lines in ScreenLines[].
9309 */
9310 row += off;
9311 end += off;
9312 for (i = 0; i < line_count; ++i)
9313 {
9314#ifdef FEAT_VERTSPLIT
9315 if (wp != NULL && wp->w_width != Columns)
9316 {
9317 /* need to copy part of a line */
9318 j = row + i;
9319 while ((j += line_count) <= end - 1)
9320 linecopy(j - line_count, j, wp);
9321 j -= line_count;
9322 if (can_clear((char_u *)" "))
9323 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9324 else
9325 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9326 LineWraps[j] = FALSE;
9327 }
9328 else
9329#endif
9330 {
9331 /* whole width, moving the line pointers is faster */
9332 j = row + i;
9333 temp = LineOffset[j];
9334 while ((j += line_count) <= end - 1)
9335 {
9336 LineOffset[j - line_count] = LineOffset[j];
9337 LineWraps[j - line_count] = LineWraps[j];
9338 }
9339 LineOffset[j - line_count] = temp;
9340 LineWraps[j - line_count] = FALSE;
9341 if (can_clear((char_u *)" "))
9342 lineclear(temp, (int)Columns);
9343 else
9344 lineinvalid(temp, (int)Columns);
9345 }
9346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348 screen_stop_highlight();
9349
9350#ifdef FEAT_VERTSPLIT
9351 /* redraw the characters */
9352 if (type == USE_REDRAW)
9353 redraw_block(row, end, wp);
9354 else
9355#endif
9356 if (type == USE_T_CD) /* delete the lines */
9357 {
9358 windgoto(cursor_row, 0);
9359 out_str(T_CD);
9360 screen_start(); /* don't know where cursor is now */
9361 }
9362 else if (type == USE_T_CDL)
9363 {
9364 windgoto(cursor_row, 0);
9365 term_delete_lines(line_count);
9366 screen_start(); /* don't know where cursor is now */
9367 }
9368 /*
9369 * Deleting lines at top of the screen or scroll region: Just scroll
9370 * the whole screen (scroll region) up by outputting newlines on the
9371 * last line.
9372 */
9373 else if (type == USE_NL)
9374 {
9375 windgoto(cursor_end - 1, 0);
9376 for (i = line_count; --i >= 0; )
9377 out_char('\n'); /* cursor will remain on same line */
9378 }
9379 else
9380 {
9381 for (i = line_count; --i >= 0; )
9382 {
9383 if (type == USE_T_DL)
9384 {
9385 windgoto(cursor_row, 0);
9386 out_str(T_DL); /* delete a line */
9387 }
9388 else /* type == USE_T_CE */
9389 {
9390 windgoto(cursor_row + i, 0);
9391 out_str(T_CE); /* erase a line */
9392 }
9393 screen_start(); /* don't know where cursor is now */
9394 }
9395 }
9396
9397 /*
9398 * If the 'db' flag is set, we need to clear the lines that have been
9399 * scrolled up at the bottom of the region.
9400 */
9401 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9402 {
9403 for (i = line_count; i > 0; --i)
9404 {
9405 windgoto(cursor_end - i, 0);
9406 out_str(T_CE); /* erase a line */
9407 screen_start(); /* don't know where cursor is now */
9408 }
9409 }
9410
9411#ifdef FEAT_GUI
9412 gui_can_update_cursor();
9413 if (gui.in_use)
9414 out_flush(); /* always flush after a scroll */
9415#endif
9416
9417 return OK;
9418}
9419
9420/*
9421 * show the current mode and ruler
9422 *
9423 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9424 * If clear_cmdline is FALSE there may be a message there that needs to be
9425 * cleared only if a mode is shown.
9426 * Return the length of the message (0 if no message).
9427 */
9428 int
9429showmode()
9430{
9431 int need_clear;
9432 int length = 0;
9433 int do_mode;
9434 int attr;
9435 int nwr_save;
9436#ifdef FEAT_INS_EXPAND
9437 int sub_attr;
9438#endif
9439
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009440 do_mode = ((p_smd && msg_silent == 0)
9441 && ((State & INSERT)
9442 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443#ifdef FEAT_VISUAL
9444 || VIsual_active
9445#endif
9446 ));
9447 if (do_mode || Recording)
9448 {
9449 /*
9450 * Don't show mode right now, when not redrawing or inside a mapping.
9451 * Call char_avail() only when we are going to show something, because
9452 * it takes a bit of time.
9453 */
9454 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9455 {
9456 redraw_cmdline = TRUE; /* show mode later */
9457 return 0;
9458 }
9459
9460 nwr_save = need_wait_return;
9461
9462 /* wait a bit before overwriting an important message */
9463 check_for_delay(FALSE);
9464
9465 /* if the cmdline is more than one line high, erase top lines */
9466 need_clear = clear_cmdline;
9467 if (clear_cmdline && cmdline_row < Rows - 1)
9468 msg_clr_cmdline(); /* will reset clear_cmdline */
9469
9470 /* Position on the last line in the window, column 0 */
9471 msg_pos_mode();
9472 cursor_off();
9473 attr = hl_attr(HLF_CM); /* Highlight mode */
9474 if (do_mode)
9475 {
9476 MSG_PUTS_ATTR("--", attr);
9477#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009478 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009479# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009480 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009481# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009482 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009483# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009484 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009485# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 MSG_PUTS_ATTR(" IM", attr);
9487# else
9488 MSG_PUTS_ATTR(" XIM", attr);
9489# endif
9490#endif
9491#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9492 if (gui.in_use)
9493 {
9494 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009495 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 }
9497#endif
9498#ifdef FEAT_INS_EXPAND
9499 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9500 {
9501 /* These messages can get long, avoid a wrap in a narrow
9502 * window. Prefer showing edit_submode_extra. */
9503 length = (Rows - msg_row) * Columns - 3;
9504 if (edit_submode_extra != NULL)
9505 length -= vim_strsize(edit_submode_extra);
9506 if (length > 0)
9507 {
9508 if (edit_submode_pre != NULL)
9509 length -= vim_strsize(edit_submode_pre);
9510 if (length - vim_strsize(edit_submode) > 0)
9511 {
9512 if (edit_submode_pre != NULL)
9513 msg_puts_attr(edit_submode_pre, attr);
9514 msg_puts_attr(edit_submode, attr);
9515 }
9516 if (edit_submode_extra != NULL)
9517 {
9518 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9519 if ((int)edit_submode_highl < (int)HLF_COUNT)
9520 sub_attr = hl_attr(edit_submode_highl);
9521 else
9522 sub_attr = attr;
9523 msg_puts_attr(edit_submode_extra, sub_attr);
9524 }
9525 }
9526 length = 0;
9527 }
9528 else
9529#endif
9530 {
9531#ifdef FEAT_VREPLACE
9532 if (State & VREPLACE_FLAG)
9533 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9534 else
9535#endif
9536 if (State & REPLACE_FLAG)
9537 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9538 else if (State & INSERT)
9539 {
9540#ifdef FEAT_RIGHTLEFT
9541 if (p_ri)
9542 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9543#endif
9544 MSG_PUTS_ATTR(_(" INSERT"), attr);
9545 }
9546 else if (restart_edit == 'I')
9547 MSG_PUTS_ATTR(_(" (insert)"), attr);
9548 else if (restart_edit == 'R')
9549 MSG_PUTS_ATTR(_(" (replace)"), attr);
9550 else if (restart_edit == 'V')
9551 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9552#ifdef FEAT_RIGHTLEFT
9553 if (p_hkmap)
9554 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9555# ifdef FEAT_FKMAP
9556 if (p_fkmap)
9557 MSG_PUTS_ATTR(farsi_text_5, attr);
9558# endif
9559#endif
9560#ifdef FEAT_KEYMAP
9561 if (State & LANGMAP)
9562 {
9563# ifdef FEAT_ARABIC
9564 if (curwin->w_p_arab)
9565 MSG_PUTS_ATTR(_(" Arabic"), attr);
9566 else
9567# endif
9568 MSG_PUTS_ATTR(_(" (lang)"), attr);
9569 }
9570#endif
9571 if ((State & INSERT) && p_paste)
9572 MSG_PUTS_ATTR(_(" (paste)"), attr);
9573
9574#ifdef FEAT_VISUAL
9575 if (VIsual_active)
9576 {
9577 char *p;
9578
9579 /* Don't concatenate separate words to avoid translation
9580 * problems. */
9581 switch ((VIsual_select ? 4 : 0)
9582 + (VIsual_mode == Ctrl_V) * 2
9583 + (VIsual_mode == 'V'))
9584 {
9585 case 0: p = N_(" VISUAL"); break;
9586 case 1: p = N_(" VISUAL LINE"); break;
9587 case 2: p = N_(" VISUAL BLOCK"); break;
9588 case 4: p = N_(" SELECT"); break;
9589 case 5: p = N_(" SELECT LINE"); break;
9590 default: p = N_(" SELECT BLOCK"); break;
9591 }
9592 MSG_PUTS_ATTR(_(p), attr);
9593 }
9594#endif
9595 MSG_PUTS_ATTR(" --", attr);
9596 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009597
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 need_clear = TRUE;
9599 }
9600 if (Recording
9601#ifdef FEAT_INS_EXPAND
9602 && edit_submode == NULL /* otherwise it gets too long */
9603#endif
9604 )
9605 {
9606 MSG_PUTS_ATTR(_("recording"), attr);
9607 need_clear = TRUE;
9608 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009609
9610 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 if (need_clear || clear_cmdline)
9612 msg_clr_eos();
9613 msg_didout = FALSE; /* overwrite this message */
9614 length = msg_col;
9615 msg_col = 0;
9616 need_wait_return = nwr_save; /* never ask for hit-return for this */
9617 }
9618 else if (clear_cmdline && msg_silent == 0)
9619 /* Clear the whole command line. Will reset "clear_cmdline". */
9620 msg_clr_cmdline();
9621
9622#ifdef FEAT_CMDL_INFO
9623# ifdef FEAT_VISUAL
9624 /* In Visual mode the size of the selected area must be redrawn. */
9625 if (VIsual_active)
9626 clear_showcmd();
9627# endif
9628
9629 /* If the last window has no status line, the ruler is after the mode
9630 * message and must be redrawn */
9631 if (redrawing()
9632# ifdef FEAT_WINDOWS
9633 && lastwin->w_status_height == 0
9634# endif
9635 )
9636 win_redr_ruler(lastwin, TRUE);
9637#endif
9638 redraw_cmdline = FALSE;
9639 clear_cmdline = FALSE;
9640
9641 return length;
9642}
9643
9644/*
9645 * Position for a mode message.
9646 */
9647 static void
9648msg_pos_mode()
9649{
9650 msg_col = 0;
9651 msg_row = Rows - 1;
9652}
9653
9654/*
9655 * Delete mode message. Used when ESC is typed which is expected to end
9656 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009657 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 */
9659 void
9660unshowmode(force)
9661 int force;
9662{
9663 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009664 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 */
9666 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9667 redraw_cmdline = TRUE; /* delete mode later */
9668 else
9669 {
9670 msg_pos_mode();
9671 if (Recording)
9672 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9673 msg_clr_eos();
9674 }
9675}
9676
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009677#if defined(FEAT_WINDOWS)
9678/*
9679 * Draw the tab pages line at the top of the Vim window.
9680 */
9681 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009682draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009683{
9684 int tabcount = 0;
9685 tabpage_T *tp;
9686 int tabwidth;
9687 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009688 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009689 int attr;
9690 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009691 win_T *cwp;
9692 int wincount;
9693 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009694 int c;
9695 int len;
9696 int attr_sel = hl_attr(HLF_TPS);
9697 int attr_nosel = hl_attr(HLF_TP);
9698 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009699 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009700 int room;
9701 int use_sep_chars = (t_colors < 8
9702#ifdef FEAT_GUI
9703 && !gui.in_use
9704#endif
9705 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009706
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009707 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009708
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009709#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009710 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009711 if (gui_use_tabline())
9712 {
9713 gui_update_tabline();
9714 return;
9715 }
9716#endif
9717
9718 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009719 return;
9720
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009721#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009722
9723 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9724 for (scol = 0; scol < Columns; ++scol)
9725 TabPageIdxs[scol] = 0;
9726
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009727 /* Use the 'tabline' option if it's set. */
9728 if (*p_tal != NUL)
9729 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009730 int save_called_emsg = called_emsg;
9731
9732 /* Check for an error. If there is one we would loop in redrawing the
9733 * screen. Avoid that by making 'tabline' empty. */
9734 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009735 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009736 if (called_emsg)
9737 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009738 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009739 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009740 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009741 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009742#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009743 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009744 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9745 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009746
Bram Moolenaar238a5642006-02-21 22:12:05 +00009747 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9748 if (tabwidth < 6)
9749 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009750
Bram Moolenaar238a5642006-02-21 22:12:05 +00009751 attr = attr_nosel;
9752 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009753 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009754 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9755 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009756 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009757 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009758
Bram Moolenaar238a5642006-02-21 22:12:05 +00009759 if (tp->tp_topframe == topframe)
9760 attr = attr_sel;
9761 if (use_sep_chars && col > 0)
9762 screen_putchar('|', 0, col++, attr);
9763
9764 if (tp->tp_topframe != topframe)
9765 attr = attr_nosel;
9766
9767 screen_putchar(' ', 0, col++, attr);
9768
9769 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009770 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009771 cwp = curwin;
9772 wp = firstwin;
9773 }
9774 else
9775 {
9776 cwp = tp->tp_curwin;
9777 wp = tp->tp_firstwin;
9778 }
9779
9780 modified = FALSE;
9781 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9782 if (bufIsChanged(wp->w_buffer))
9783 modified = TRUE;
9784 if (modified || wincount > 1)
9785 {
9786 if (wincount > 1)
9787 {
9788 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009789 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009790 if (col + len >= Columns - 3)
9791 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009792 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009793#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009794 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009795#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009796 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009797#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009798 );
9799 col += len;
9800 }
9801 if (modified)
9802 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9803 screen_putchar(' ', 0, col++, attr);
9804 }
9805
9806 room = scol - col + tabwidth - 1;
9807 if (room > 0)
9808 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009809 /* Get buffer name in NameBuff[] */
9810 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009811 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009812 len = vim_strsize(NameBuff);
9813 p = NameBuff;
9814#ifdef FEAT_MBYTE
9815 if (has_mbyte)
9816 while (len > room)
9817 {
9818 len -= ptr2cells(p);
9819 mb_ptr_adv(p);
9820 }
9821 else
9822#endif
9823 if (len > room)
9824 {
9825 p += len - room;
9826 len = room;
9827 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009828 if (len > Columns - col - 1)
9829 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009830
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009831 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009832 col += len;
9833 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009834 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009835
9836 /* Store the tab page number in TabPageIdxs[], so that
9837 * jump_to_mouse() knows where each one is. */
9838 ++tabcount;
9839 while (scol < col)
9840 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009841 }
9842
Bram Moolenaar238a5642006-02-21 22:12:05 +00009843 if (use_sep_chars)
9844 c = '_';
9845 else
9846 c = ' ';
9847 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009848
9849 /* Put an "X" for closing the current tab if there are several. */
9850 if (first_tabpage->tp_next != NULL)
9851 {
9852 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9853 TabPageIdxs[Columns - 1] = -999;
9854 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009855 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009856
9857 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9858 * set. */
9859 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009860}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009861
9862/*
9863 * Get buffer name for "buf" into NameBuff[].
9864 * Takes care of special buffer names and translates special characters.
9865 */
9866 void
9867get_trans_bufname(buf)
9868 buf_T *buf;
9869{
9870 if (buf_spname(buf) != NULL)
9871 STRCPY(NameBuff, buf_spname(buf));
9872 else
9873 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9874 trans_characters(NameBuff, MAXPATHL);
9875}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009876#endif
9877
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9879/*
9880 * Get the character to use in a status line. Get its attributes in "*attr".
9881 */
9882 static int
9883fillchar_status(attr, is_curwin)
9884 int *attr;
9885 int is_curwin;
9886{
9887 int fill;
9888 if (is_curwin)
9889 {
9890 *attr = hl_attr(HLF_S);
9891 fill = fill_stl;
9892 }
9893 else
9894 {
9895 *attr = hl_attr(HLF_SNC);
9896 fill = fill_stlnc;
9897 }
9898 /* Use fill when there is highlighting, and highlighting of current
9899 * window differs, or the fillchars differ, or this is not the
9900 * current window */
9901 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9902 || !is_curwin || firstwin == lastwin)
9903 || (fill_stl != fill_stlnc)))
9904 return fill;
9905 if (is_curwin)
9906 return '^';
9907 return '=';
9908}
9909#endif
9910
9911#ifdef FEAT_VERTSPLIT
9912/*
9913 * Get the character to use in a separator between vertically split windows.
9914 * Get its attributes in "*attr".
9915 */
9916 static int
9917fillchar_vsep(attr)
9918 int *attr;
9919{
9920 *attr = hl_attr(HLF_C);
9921 if (*attr == 0 && fill_vert == ' ')
9922 return '|';
9923 else
9924 return fill_vert;
9925}
9926#endif
9927
9928/*
9929 * Return TRUE if redrawing should currently be done.
9930 */
9931 int
9932redrawing()
9933{
9934 return (!RedrawingDisabled
9935 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9936}
9937
9938/*
9939 * Return TRUE if printing messages should currently be done.
9940 */
9941 int
9942messaging()
9943{
9944 return (!(p_lz && char_avail() && !KeyTyped));
9945}
9946
9947/*
9948 * Show current status info in ruler and various other places
9949 * If always is FALSE, only show ruler if position has changed.
9950 */
9951 void
9952showruler(always)
9953 int always;
9954{
9955 if (!always && !redrawing())
9956 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009957#ifdef FEAT_INS_EXPAND
9958 if (pum_visible())
9959 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009960# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009961 /* Don't redraw right now, do it later. */
9962 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009963# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009964 return;
9965 }
9966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009968 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009969 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009970 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 else
9973#endif
9974#ifdef FEAT_CMDL_INFO
9975 win_redr_ruler(curwin, always);
9976#endif
9977
9978#ifdef FEAT_TITLE
9979 if (need_maketitle
9980# ifdef FEAT_STL_OPT
9981 || (p_icon && (stl_syntax & STL_IN_ICON))
9982 || (p_title && (stl_syntax & STL_IN_TITLE))
9983# endif
9984 )
9985 maketitle();
9986#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009987#ifdef FEAT_WINDOWS
9988 /* Redraw the tab pages line if needed. */
9989 if (redraw_tabline)
9990 draw_tabline();
9991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992}
9993
9994#ifdef FEAT_CMDL_INFO
9995 static void
9996win_redr_ruler(wp, always)
9997 win_T *wp;
9998 int always;
9999{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010000#define RULER_BUF_LEN 70
10001 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 int row;
10003 int fillchar;
10004 int attr;
10005 int empty_line = FALSE;
10006 colnr_T virtcol;
10007 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010008 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 int o;
10010#ifdef FEAT_VERTSPLIT
10011 int this_ru_col;
10012 int off = 0;
10013 int width = Columns;
10014# define WITH_OFF(x) x
10015# define WITH_WIDTH(x) x
10016#else
10017# define WITH_OFF(x) 0
10018# define WITH_WIDTH(x) Columns
10019# define this_ru_col ru_col
10020#endif
10021
10022 /* If 'ruler' off or redrawing disabled, don't do anything */
10023 if (!p_ru)
10024 return;
10025
10026 /*
10027 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10028 * after deleting lines, before cursor.lnum is corrected.
10029 */
10030 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10031 return;
10032
10033#ifdef FEAT_INS_EXPAND
10034 /* Don't draw the ruler while doing insert-completion, it might overwrite
10035 * the (long) mode message. */
10036# ifdef FEAT_WINDOWS
10037 if (wp == lastwin && lastwin->w_status_height == 0)
10038# endif
10039 if (edit_submode != NULL)
10040 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010041 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10042 if (pum_visible())
10043 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044#endif
10045
10046#ifdef FEAT_STL_OPT
10047 if (*p_ruf)
10048 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010049 int save_called_emsg = called_emsg;
10050
10051 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010053 if (called_emsg)
10054 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010055 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010056 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 return;
10058 }
10059#endif
10060
10061 /*
10062 * Check if not in Insert mode and the line is empty (will show "0-1").
10063 */
10064 if (!(State & INSERT)
10065 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10066 empty_line = TRUE;
10067
10068 /*
10069 * Only draw the ruler when something changed.
10070 */
10071 validate_virtcol_win(wp);
10072 if ( redraw_cmdline
10073 || always
10074 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10075 || wp->w_cursor.col != wp->w_ru_cursor.col
10076 || wp->w_virtcol != wp->w_ru_virtcol
10077#ifdef FEAT_VIRTUALEDIT
10078 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10079#endif
10080 || wp->w_topline != wp->w_ru_topline
10081 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10082#ifdef FEAT_DIFF
10083 || wp->w_topfill != wp->w_ru_topfill
10084#endif
10085 || empty_line != wp->w_ru_empty)
10086 {
10087 cursor_off();
10088#ifdef FEAT_WINDOWS
10089 if (wp->w_status_height)
10090 {
10091 row = W_WINROW(wp) + wp->w_height;
10092 fillchar = fillchar_status(&attr, wp == curwin);
10093# ifdef FEAT_VERTSPLIT
10094 off = W_WINCOL(wp);
10095 width = W_WIDTH(wp);
10096# endif
10097 }
10098 else
10099#endif
10100 {
10101 row = Rows - 1;
10102 fillchar = ' ';
10103 attr = 0;
10104#ifdef FEAT_VERTSPLIT
10105 width = Columns;
10106 off = 0;
10107#endif
10108 }
10109
10110 /* In list mode virtcol needs to be recomputed */
10111 virtcol = wp->w_virtcol;
10112 if (wp->w_p_list && lcs_tab1 == NUL)
10113 {
10114 wp->w_p_list = FALSE;
10115 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10116 wp->w_p_list = TRUE;
10117 }
10118
10119 /*
10120 * Some sprintfs return the length, some return a pointer.
10121 * To avoid portability problems we use strlen() here.
10122 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010123 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10125 ? 0L
10126 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010127 len = STRLEN(buffer);
10128 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10130 (int)virtcol + 1);
10131
10132 /*
10133 * Add a "50%" if there is room for it.
10134 * On the last line, don't print in the last column (scrolls the
10135 * screen up on some terminals).
10136 */
10137 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010138 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 o = i + vim_strsize(buffer + i + 1);
10140#ifdef FEAT_WINDOWS
10141 if (wp->w_status_height == 0) /* can't use last char of screen */
10142#endif
10143 ++o;
10144#ifdef FEAT_VERTSPLIT
10145 this_ru_col = ru_col - (Columns - width);
10146 if (this_ru_col < 0)
10147 this_ru_col = 0;
10148#endif
10149 /* Never use more than half the window/screen width, leave the other
10150 * half for the filename. */
10151 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10152 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10153 if (this_ru_col + o < WITH_WIDTH(width))
10154 {
10155 while (this_ru_col + o < WITH_WIDTH(width))
10156 {
10157#ifdef FEAT_MBYTE
10158 if (has_mbyte)
10159 i += (*mb_char2bytes)(fillchar, buffer + i);
10160 else
10161#endif
10162 buffer[i++] = fillchar;
10163 ++o;
10164 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010165 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 }
10167 /* Truncate at window boundary. */
10168#ifdef FEAT_MBYTE
10169 if (has_mbyte)
10170 {
10171 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010172 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 {
10174 o += (*mb_ptr2cells)(buffer + i);
10175 if (this_ru_col + o > WITH_WIDTH(width))
10176 {
10177 buffer[i] = NUL;
10178 break;
10179 }
10180 }
10181 }
10182 else
10183#endif
10184 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10185 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10186
10187 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10188 i = redraw_cmdline;
10189 screen_fill(row, row + 1,
10190 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10191 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10192 fillchar, fillchar, attr);
10193 /* don't redraw the cmdline because of showing the ruler */
10194 redraw_cmdline = i;
10195 wp->w_ru_cursor = wp->w_cursor;
10196 wp->w_ru_virtcol = wp->w_virtcol;
10197 wp->w_ru_empty = empty_line;
10198 wp->w_ru_topline = wp->w_topline;
10199 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10200#ifdef FEAT_DIFF
10201 wp->w_ru_topfill = wp->w_topfill;
10202#endif
10203 }
10204}
10205#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010206
10207#if defined(FEAT_LINEBREAK) || defined(PROTO)
10208/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010209 * Return the width of the 'number' and 'relativenumber' column.
10210 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010211 * Otherwise it depends on 'numberwidth' and the line count.
10212 */
10213 int
10214number_width(wp)
10215 win_T *wp;
10216{
10217 int n;
10218 linenr_T lnum;
10219
Bram Moolenaar64486672010-05-16 15:46:46 +020010220 if (wp->w_p_nu)
10221 /* 'number' */
10222 lnum = wp->w_buffer->b_ml.ml_line_count;
10223 else
10224 /* 'relativenumber' */
10225 lnum = wp->w_height;
10226
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010227 if (lnum == wp->w_nrwidth_line_count)
10228 return wp->w_nrwidth_width;
10229 wp->w_nrwidth_line_count = lnum;
10230
10231 n = 0;
10232 do
10233 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010234 lnum /= 10;
10235 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010236 } while (lnum > 0);
10237
10238 /* 'numberwidth' gives the minimal width plus one */
10239 if (n < wp->w_p_nuw - 1)
10240 n = wp->w_p_nuw - 1;
10241
10242 wp->w_nrwidth_width = n;
10243 return n;
10244}
10245#endif