blob: 7bc77f296e25fe60f84e52af03199831b862550e [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
92/*
93 * The attributes that are actually active for writing to the screen.
94 */
95static int screen_attr = 0;
96
97/*
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
100 */
101static int screen_cur_row, screen_cur_col; /* last known cursor position */
102
103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106
107#ifdef FEAT_FOLDING
108static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109#endif
110
111/*
112 * Buffer for one screen line (characters and attributes).
113 */
114static schar_T *current_ScreenLine;
115
116static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000117static 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 +0000118#ifdef FEAT_FOLDING
119static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000123static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125#ifdef FEAT_RIGHTLEFT
126static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128#else
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_VERTSPLIT
133static void draw_vsep_win __ARGS((win_T *wp, int row));
134#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000135#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000136static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000139#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void start_search_hl __ARGS((void));
141static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200142static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
144static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
145#endif
146static void screen_start_highlight __ARGS((int attr));
147static void screen_char __ARGS((unsigned off, int row, int col));
148#ifdef FEAT_MBYTE
149static void screen_char_2 __ARGS((unsigned off, int row, int col));
150#endif
151static void screenclear2 __ARGS((void));
152static void lineclear __ARGS((unsigned off, int width));
153static void lineinvalid __ARGS((unsigned off, int width));
154#ifdef FEAT_VERTSPLIT
155static void linecopy __ARGS((int to, int from, win_T *wp));
156static void redraw_block __ARGS((int row, int end, win_T *wp));
157#endif
158static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
159static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000162static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
165static int fillchar_status __ARGS((int *attr, int is_curwin));
166#endif
167#ifdef FEAT_VERTSPLIT
168static int fillchar_vsep __ARGS((int *attr));
169#endif
170#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000171static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_CMDL_INFO
174static void win_redr_ruler __ARGS((win_T *wp, int always));
175#endif
176
177#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
178/* Ugly global: overrule attribute used by screen_char() */
179static int screen_char_attr = 0;
180#endif
181
182/*
183 * Redraw the current window later, with update_screen(type).
184 * Set must_redraw only if not already set to a higher value.
185 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
186 */
187 void
188redraw_later(type)
189 int type;
190{
191 redraw_win_later(curwin, type);
192}
193
194 void
195redraw_win_later(wp, type)
196 win_T *wp;
197 int type;
198{
199 if (wp->w_redr_type < type)
200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
214redraw_later_clear()
215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
232redraw_all_later(type)
233 int type;
234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
247redraw_curbuf_later(type)
248 int type;
249{
250 redraw_buf_later(curbuf, type);
251}
252
253 void
254redraw_buf_later(buf, type)
255 buf_T *buf;
256 int type;
257{
258 win_T *wp;
259
260 FOR_ALL_WINDOWS(wp)
261 {
262 if (wp->w_buffer == buf)
263 redraw_win_later(wp, type);
264 }
265}
266
267/*
268 * Changed something in the current window, at buffer line "lnum", that
269 * requires that line and possibly other lines to be redrawn.
270 * Used when entering/leaving Insert mode with the cursor on a folded line.
271 * Used to remove the "$" from a change command.
272 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
273 * may become invalid and the whole window will have to be redrawn.
274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 void
276redrawWinline(lnum, invalid)
277 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000278 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280#ifdef FEAT_FOLDING
281 int i;
282#endif
283
284 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
285 curwin->w_redraw_top = lnum;
286 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
287 curwin->w_redraw_bot = lnum;
288 redraw_later(VALID);
289
290#ifdef FEAT_FOLDING
291 if (invalid)
292 {
293 /* A w_lines[] entry for this lnum has become invalid. */
294 i = find_wl_entry(curwin, lnum);
295 if (i >= 0)
296 curwin->w_lines[i].wl_valid = FALSE;
297 }
298#endif
299}
300
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200301#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200302 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * update all windows that are editing the current buffer
305 */
306 void
307update_curbuf(type)
308 int type;
309{
310 redraw_curbuf_later(type);
311 update_screen(type);
312}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * update_screen()
317 *
318 * Based on the current value of curwin->w_topline, transfer a screenfull
319 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
320 */
321 void
322update_screen(type)
323 int type;
324{
325 win_T *wp;
326 static int did_intro = FALSE;
327#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
328 int did_one;
329#endif
330
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000331 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 if (!screen_valid(TRUE))
333 return;
334
335 if (must_redraw)
336 {
337 if (type < must_redraw) /* use maximal type */
338 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000339
340 /* must_redraw is reset here, so that when we run into some weird
341 * reason to redraw while busy redrawing (e.g., asynchronous
342 * scrolling), or update_topline() in win_update() will cause a
343 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 must_redraw = 0;
345 }
346
347 /* Need to update w_lines[]. */
348 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
349 type = NOT_VALID;
350
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000351 /* Postpone the redrawing when it's not needed and when being called
352 * recursively. */
353 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 redraw_later(type); /* remember type for next time */
356 must_redraw = type;
357 if (type > INVERTED_ALL)
358 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
359 return;
360 }
361
362 updating_screen = TRUE;
363#ifdef FEAT_SYN_HL
364 ++display_tick; /* let syntax code know we're in a next round of
365 * display updating */
366#endif
367
368 /*
369 * if the screen was scrolled up when displaying a message, scroll it down
370 */
371 if (msg_scrolled)
372 {
373 clear_cmdline = TRUE;
374 if (msg_scrolled > Rows - 5) /* clearing is faster */
375 type = CLEAR;
376 else if (type != CLEAR)
377 {
378 check_for_delay(FALSE);
379 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
380 type = CLEAR;
381 FOR_ALL_WINDOWS(wp)
382 {
383 if (W_WINROW(wp) < msg_scrolled)
384 {
385 if (W_WINROW(wp) + wp->w_height > msg_scrolled
386 && wp->w_redr_type < REDRAW_TOP
387 && wp->w_lines_valid > 0
388 && wp->w_topline == wp->w_lines[0].wl_lnum)
389 {
390 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
391 wp->w_redr_type = REDRAW_TOP;
392 }
393 else
394 {
395 wp->w_redr_type = NOT_VALID;
396#ifdef FEAT_WINDOWS
397 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
398 <= msg_scrolled)
399 wp->w_redr_status = TRUE;
400#endif
401 }
402 }
403 }
404 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000406 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409 msg_scrolled = 0;
410 need_wait_return = FALSE;
411 }
412
413 /* reset cmdline_row now (may have been changed temporarily) */
414 compute_cmdrow();
415
416 /* Check for changed highlighting */
417 if (need_highlight_changed)
418 highlight_changed();
419
420 if (type == CLEAR) /* first clear screen */
421 {
422 screenclear(); /* will reset clear_cmdline */
423 type = NOT_VALID;
424 }
425
426 if (clear_cmdline) /* going to clear cmdline (done below) */
427 check_for_delay(FALSE);
428
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000429#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200430 /* Force redraw when width of 'number' or 'relativenumber' column
431 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
434 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 curwin->w_redr_type = NOT_VALID;
436#endif
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 /*
439 * Only start redrawing if there is really something to do.
440 */
441 if (type == INVERTED)
442 update_curswant();
443 if (curwin->w_redr_type < type
444 && !((type == VALID
445 && curwin->w_lines[0].wl_valid
446#ifdef FEAT_DIFF
447 && curwin->w_topfill == curwin->w_old_topfill
448 && curwin->w_botfill == curwin->w_old_botfill
449#endif
450 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
451#ifdef FEAT_VISUAL
452 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000453 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
455 && curwin->w_old_visual_mode == VIsual_mode
456 && (curwin->w_valid & VALID_VIRTCOL)
457 && curwin->w_old_curswant == curwin->w_curswant)
458#endif
459 ))
460 curwin->w_redr_type = type;
461
Bram Moolenaar5a305422006-04-28 22:38:25 +0000462#ifdef FEAT_WINDOWS
463 /* Redraw the tab pages line if needed. */
464 if (redraw_tabline || type >= NOT_VALID)
465 draw_tabline();
466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#ifdef FEAT_SYN_HL
469 /*
470 * Correct stored syntax highlighting info for changes in each displayed
471 * buffer. Each buffer must only be done once.
472 */
473 FOR_ALL_WINDOWS(wp)
474 {
475 if (wp->w_buffer->b_mod_set)
476 {
477# ifdef FEAT_WINDOWS
478 win_T *wwp;
479
480 /* Check if we already did this buffer. */
481 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
482 if (wwp->w_buffer == wp->w_buffer)
483 break;
484# endif
485 if (
486# ifdef FEAT_WINDOWS
487 wwp == wp &&
488# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200489 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 syn_stack_apply_changes(wp->w_buffer);
491 }
492 }
493#endif
494
495 /*
496 * Go from top to bottom through the windows, redrawing the ones that need
497 * it.
498 */
499#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
500 did_one = FALSE;
501#endif
502#ifdef FEAT_SEARCH_EXTRA
503 search_hl.rm.regprog = NULL;
504#endif
505 FOR_ALL_WINDOWS(wp)
506 {
507 if (wp->w_redr_type != 0)
508 {
509 cursor_off();
510#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
511 if (!did_one)
512 {
513 did_one = TRUE;
514# ifdef FEAT_SEARCH_EXTRA
515 start_search_hl();
516# endif
517# ifdef FEAT_CLIPBOARD
518 /* When Visual area changed, may have to update selection. */
519 if (clip_star.available && clip_isautosel())
520 clip_update_selection();
521# endif
522#ifdef FEAT_GUI
523 /* Remove the cursor before starting to do anything, because
524 * scrolling may make it difficult to redraw the text under
525 * it. */
526 if (gui.in_use)
527 gui_undraw_cursor();
528#endif
529 }
530#endif
531 win_update(wp);
532 }
533
534#ifdef FEAT_WINDOWS
535 /* redraw status line after the window to minimize cursor movement */
536 if (wp->w_redr_status)
537 {
538 cursor_off();
539 win_redr_status(wp);
540 }
541#endif
542 }
543#if defined(FEAT_SEARCH_EXTRA)
544 end_search_hl();
545#endif
546
547#ifdef FEAT_WINDOWS
548 /* Reset b_mod_set flags. Going through all windows is probably faster
549 * than going through all buffers (there could be many buffers). */
550 for (wp = firstwin; wp != NULL; wp = wp->w_next)
551 wp->w_buffer->b_mod_set = FALSE;
552#else
553 curbuf->b_mod_set = FALSE;
554#endif
555
556 updating_screen = FALSE;
557#ifdef FEAT_GUI
558 gui_may_resize_shell();
559#endif
560
561 /* Clear or redraw the command line. Done last, because scrolling may
562 * mess up the command line. */
563 if (clear_cmdline || redraw_cmdline)
564 showmode();
565
566 /* May put up an introductory message when not editing a file */
567 if (!did_intro && bufempty()
568 && curbuf->b_fname == NULL
569#ifdef FEAT_WINDOWS
570 && firstwin->w_next == NULL
571#endif
572 && vim_strchr(p_shm, SHM_INTRO) == NULL)
573 intro_message(FALSE);
574 did_intro = TRUE;
575
576#ifdef FEAT_GUI
577 /* Redraw the cursor and update the scrollbars when all screen updating is
578 * done. */
579 if (gui.in_use)
580 {
581 out_flush(); /* required before updating the cursor */
582 if (did_one)
583 gui_update_cursor(FALSE, FALSE);
584 gui_update_scrollbars(FALSE);
585 }
586#endif
587}
588
Bram Moolenaar860cae12010-06-05 23:22:07 +0200589#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200590/*
591 * Return TRUE if the cursor line in window "wp" may be concealed, according
592 * to the 'concealcursor' option.
593 */
594 int
595conceal_cursor_line(wp)
596 win_T *wp;
597{
598 int c;
599
600 if (*wp->w_p_cocu == NUL)
601 return FALSE;
602 if (get_real_state() & VISUAL)
603 c = 'v';
604 else if (State & INSERT)
605 c = 'i';
606 else if (State & NORMAL)
607 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200608 else if (State & CMDLINE)
609 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200610 else
611 return FALSE;
612 return vim_strchr(wp->w_p_cocu, c) != NULL;
613}
614
615/*
616 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
617 */
618 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200619conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200620{
621 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
622 {
623 need_cursor_line_redraw = TRUE;
624 /* Need to recompute cursor column, e.g., when starting Visual mode
625 * without concealing. */
626 curs_columns(TRUE);
627 }
628}
629
Bram Moolenaar860cae12010-06-05 23:22:07 +0200630 void
631update_single_line(wp, lnum)
632 win_T *wp;
633 linenr_T lnum;
634{
635 int row;
636 int j;
637
638 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200639 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 {
641# ifdef FEAT_GUI
642 /* Remove the cursor before starting to do anything, because scrolling
643 * may make it difficult to redraw the text under it. */
644 if (gui.in_use)
645 gui_undraw_cursor();
646# endif
647 row = 0;
648 for (j = 0; j < wp->w_lines_valid; ++j)
649 {
650 if (lnum == wp->w_lines[j].wl_lnum)
651 {
652 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200653# ifdef FEAT_SEARCH_EXTRA
654 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200655 start_search_hl();
656 prepare_search_hl(wp, lnum);
657# endif
658 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
659# if defined(FEAT_SEARCH_EXTRA)
660 end_search_hl();
661# endif
662 break;
663 }
664 row += wp->w_lines[j].wl_size;
665 }
666# ifdef FEAT_GUI
667 /* Redraw the cursor */
668 if (gui.in_use)
669 {
670 out_flush(); /* required before updating the cursor */
671 gui_update_cursor(FALSE, FALSE);
672 }
673# endif
674 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200675 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200676}
677#endif
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
680static void update_prepare __ARGS((void));
681static void update_finish __ARGS((void));
682
683/*
684 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000685 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 */
687 static void
688update_prepare()
689{
690 cursor_off();
691 updating_screen = TRUE;
692#ifdef FEAT_GUI
693 /* Remove the cursor before starting to do anything, because scrolling may
694 * make it difficult to redraw the text under it. */
695 if (gui.in_use)
696 gui_undraw_cursor();
697#endif
698#ifdef FEAT_SEARCH_EXTRA
699 start_search_hl();
700#endif
701}
702
703/*
704 * Finish updating one or more windows.
705 */
706 static void
707update_finish()
708{
709 if (redraw_cmdline)
710 showmode();
711
712# ifdef FEAT_SEARCH_EXTRA
713 end_search_hl();
714# endif
715
716 updating_screen = FALSE;
717
718# ifdef FEAT_GUI
719 gui_may_resize_shell();
720
721 /* Redraw the cursor and update the scrollbars when all screen updating is
722 * done. */
723 if (gui.in_use)
724 {
725 out_flush(); /* required before updating the cursor */
726 gui_update_cursor(FALSE, FALSE);
727 gui_update_scrollbars(FALSE);
728 }
729# endif
730}
731#endif
732
733#if defined(FEAT_SIGNS) || defined(PROTO)
734 void
735update_debug_sign(buf, lnum)
736 buf_T *buf;
737 linenr_T lnum;
738{
739 win_T *wp;
740 int doit = FALSE;
741
742# ifdef FEAT_FOLDING
743 win_foldinfo.fi_level = 0;
744# endif
745
746 /* update/delete a specific mark */
747 FOR_ALL_WINDOWS(wp)
748 {
749 if (buf != NULL && lnum > 0)
750 {
751 if (wp->w_buffer == buf && lnum >= wp->w_topline
752 && lnum < wp->w_botline)
753 {
754 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
755 wp->w_redraw_top = lnum;
756 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
757 wp->w_redraw_bot = lnum;
758 redraw_win_later(wp, VALID);
759 }
760 }
761 else
762 redraw_win_later(wp, VALID);
763 if (wp->w_redr_type != 0)
764 doit = TRUE;
765 }
766
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100767 /* Return when there is nothing to do, screen updating is already
768 * happening (recursive call) or still starting up. */
769 if (!doit || updating_screen
770#ifdef FEAT_GUI
771 || gui.starting
772#endif
773 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 return;
775
776 /* update all windows that need updating */
777 update_prepare();
778
779# ifdef FEAT_WINDOWS
780 for (wp = firstwin; wp; wp = wp->w_next)
781 {
782 if (wp->w_redr_type != 0)
783 win_update(wp);
784 if (wp->w_redr_status)
785 win_redr_status(wp);
786 }
787# else
788 if (curwin->w_redr_type != 0)
789 win_update(curwin);
790# endif
791
792 update_finish();
793}
794#endif
795
796
797#if defined(FEAT_GUI) || defined(PROTO)
798/*
799 * Update a single window, its status line and maybe the command line msg.
800 * Used for the GUI scrollbar.
801 */
802 void
803updateWindow(wp)
804 win_T *wp;
805{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000806 /* return if already busy updating */
807 if (updating_screen)
808 return;
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 update_prepare();
811
812#ifdef FEAT_CLIPBOARD
813 /* When Visual area changed, may have to update selection. */
814 if (clip_star.available && clip_isautosel())
815 clip_update_selection();
816#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000821 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000822 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000823 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (wp->w_redr_status
826# ifdef FEAT_CMDL_INFO
827 || p_ru
828# endif
829# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000830 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831# endif
832 )
833 win_redr_status(wp);
834#endif
835
836 update_finish();
837}
838#endif
839
840/*
841 * Update a single window.
842 *
843 * This may cause the windows below it also to be redrawn (when clearing the
844 * screen or scrolling lines).
845 *
846 * How the window is redrawn depends on wp->w_redr_type. Each type also
847 * implies the one below it.
848 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000849 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
851 * INVERTED redraw the changed part of the Visual area
852 * INVERTED_ALL redraw the whole Visual area
853 * VALID 1. scroll up/down to adjust for a changed w_topline
854 * 2. update lines at the top when scrolled down
855 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000856 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 * b_mod_top and b_mod_bot.
858 * - if wp->w_redraw_top non-zero, redraw lines between
859 * wp->w_redraw_top and wp->w_redr_bot.
860 * - continue redrawing when syntax status is invalid.
861 * 4. if scrolled up, update lines at the bottom.
862 * This results in three areas that may need updating:
863 * top: from first row to top_end (when scrolled down)
864 * mid: from mid_start to mid_end (update inversion or changed text)
865 * bot: from bot_start to last row (when scrolled up)
866 */
867 static void
868win_update(wp)
869 win_T *wp;
870{
871 buf_T *buf = wp->w_buffer;
872 int type;
873 int top_end = 0; /* Below last row of the top area that needs
874 updating. 0 when no top area updating. */
875 int mid_start = 999;/* first row of the mid area that needs
876 updating. 999 when no mid area updating. */
877 int mid_end = 0; /* Below last row of the mid area that needs
878 updating. 0 when no mid area updating. */
879 int bot_start = 999;/* first row of the bot area that needs
880 updating. 999 when no bot area updating */
881#ifdef FEAT_VISUAL
882 int scrolled_down = FALSE; /* TRUE when scrolled down when
883 w_topline got smaller a bit */
884#endif
885#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000886 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int top_to_mod = FALSE; /* redraw above mod_top */
888#endif
889
890 int row; /* current window row to display */
891 linenr_T lnum; /* current buffer lnum to display */
892 int idx; /* current index in w_lines[] */
893 int srow; /* starting row of the current line */
894
895 int eof = FALSE; /* if TRUE, we hit the end of the file */
896 int didline = FALSE; /* if TRUE, we finished the last line */
897 int i;
898 long j;
899 static int recursive = FALSE; /* being called recursively */
900 int old_botline = wp->w_botline;
901#ifdef FEAT_FOLDING
902 long fold_count;
903#endif
904#ifdef FEAT_SYN_HL
905 /* remember what happened to the previous line, to know if
906 * check_visual_highlight() can be used */
907#define DID_NONE 1 /* didn't update a line */
908#define DID_LINE 2 /* updated a normal line */
909#define DID_FOLD 3 /* updated a folded line */
910 int did_update = DID_NONE;
911 linenr_T syntax_last_parsed = 0; /* last parsed text line */
912#endif
913 linenr_T mod_top = 0;
914 linenr_T mod_bot = 0;
915#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
916 int save_got_int;
917#endif
918
919 type = wp->w_redr_type;
920
921 if (type == NOT_VALID)
922 {
923#ifdef FEAT_WINDOWS
924 wp->w_redr_status = TRUE;
925#endif
926 wp->w_lines_valid = 0;
927 }
928
929 /* Window is zero-height: nothing to draw. */
930 if (wp->w_height == 0)
931 {
932 wp->w_redr_type = 0;
933 return;
934 }
935
936#ifdef FEAT_VERTSPLIT
937 /* Window is zero-width: Only need to draw the separator. */
938 if (wp->w_width == 0)
939 {
940 /* draw the vertical separator right of this window */
941 draw_vsep_win(wp, 0);
942 wp->w_redr_type = 0;
943 return;
944 }
945#endif
946
947#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200948 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#endif
950
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000951#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200952 /* Force redraw when width of 'number' or 'relativenumber' column
953 * changes. */
954 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000955 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000956 {
957 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000958 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000959 }
960 else
961#endif
962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
964 {
965 /*
966 * When there are both inserted/deleted lines and specific lines to be
967 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
968 * everything (only happens when redrawing is off for while).
969 */
970 type = NOT_VALID;
971 }
972 else
973 {
974 /*
975 * Set mod_top to the first line that needs displaying because of
976 * changes. Set mod_bot to the first line after the changes.
977 */
978 mod_top = wp->w_redraw_top;
979 if (wp->w_redraw_bot != 0)
980 mod_bot = wp->w_redraw_bot + 1;
981 else
982 mod_bot = 0;
983 wp->w_redraw_top = 0; /* reset for next time */
984 wp->w_redraw_bot = 0;
985 if (buf->b_mod_set)
986 {
987 if (mod_top == 0 || mod_top > buf->b_mod_top)
988 {
989 mod_top = buf->b_mod_top;
990#ifdef FEAT_SYN_HL
991 /* Need to redraw lines above the change that may be included
992 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200993 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200995 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (mod_top < 1)
997 mod_top = 1;
998 }
999#endif
1000 }
1001 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1002 mod_bot = buf->b_mod_bot;
1003
1004#ifdef FEAT_SEARCH_EXTRA
1005 /* When 'hlsearch' is on and using a multi-line search pattern, a
1006 * change in one line may make the Search highlighting in a
1007 * previous line invalid. Simple solution: redraw all visible
1008 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001009 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001011 if (search_hl.rm.regprog != NULL
1012 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001014 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001015 {
1016 cur = wp->w_match_head;
1017 while (cur != NULL)
1018 {
1019 if (cur->match.regprog != NULL
1020 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001021 {
1022 top_to_mod = TRUE;
1023 break;
1024 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001025 cur = cur->next;
1026 }
1027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#endif
1029 }
1030#ifdef FEAT_FOLDING
1031 if (mod_top != 0 && hasAnyFolding(wp))
1032 {
1033 linenr_T lnumt, lnumb;
1034
1035 /*
1036 * A change in a line can cause lines above it to become folded or
1037 * unfolded. Find the top most buffer line that may be affected.
1038 * If the line was previously folded and displayed, get the first
1039 * line of that fold. If the line is folded now, get the first
1040 * folded line. Use the minimum of these two.
1041 */
1042
1043 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1044 * the line below it. If there is no valid entry, use w_topline.
1045 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1046 * to this line. If there is no valid entry, use MAXLNUM. */
1047 lnumt = wp->w_topline;
1048 lnumb = MAXLNUM;
1049 for (i = 0; i < wp->w_lines_valid; ++i)
1050 if (wp->w_lines[i].wl_valid)
1051 {
1052 if (wp->w_lines[i].wl_lastlnum < mod_top)
1053 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1054 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1055 {
1056 lnumb = wp->w_lines[i].wl_lnum;
1057 /* When there is a fold column it might need updating
1058 * in the next line ("J" just above an open fold). */
1059 if (wp->w_p_fdc > 0)
1060 ++lnumb;
1061 }
1062 }
1063
1064 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1065 if (mod_top > lnumt)
1066 mod_top = lnumt;
1067
1068 /* Now do the same for the bottom line (one above mod_bot). */
1069 --mod_bot;
1070 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1071 ++mod_bot;
1072 if (mod_bot < lnumb)
1073 mod_bot = lnumb;
1074 }
1075#endif
1076
1077 /* When a change starts above w_topline and the end is below
1078 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001079 * If the end of the change is above w_topline: do like no change was
1080 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (mod_top != 0 && mod_top < wp->w_topline)
1082 {
1083 if (mod_bot > wp->w_topline)
1084 mod_top = wp->w_topline;
1085#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001086 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 top_end = 1;
1088#endif
1089 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001090
1091 /* When line numbers are displayed need to redraw all lines below
1092 * inserted/deleted lines. */
1093 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1094 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096
1097 /*
1098 * When only displaying the lines at the top, set top_end. Used when
1099 * window has scrolled down for msg_scrolled.
1100 */
1101 if (type == REDRAW_TOP)
1102 {
1103 j = 0;
1104 for (i = 0; i < wp->w_lines_valid; ++i)
1105 {
1106 j += wp->w_lines[i].wl_size;
1107 if (j >= wp->w_upd_rows)
1108 {
1109 top_end = j;
1110 break;
1111 }
1112 }
1113 if (top_end == 0)
1114 /* not found (cannot happen?): redraw everything */
1115 type = NOT_VALID;
1116 else
1117 /* top area defined, the rest is VALID */
1118 type = VALID;
1119 }
1120
Bram Moolenaar367329b2007-08-30 11:53:22 +00001121 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001122 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1123 * non-zero and thus not FALSE) will indicate that screenclear() was not
1124 * called. */
1125 if (screen_cleared)
1126 screen_cleared = MAYBE;
1127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 /*
1129 * If there are no changes on the screen that require a complete redraw,
1130 * handle three cases:
1131 * 1: we are off the top of the screen by a few lines: scroll down
1132 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1133 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1134 * w_lines[] that needs updating.
1135 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001136 if ((type == VALID || type == SOME_VALID
1137 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef FEAT_DIFF
1139 && !wp->w_botfill && !wp->w_old_botfill
1140#endif
1141 )
1142 {
1143 if (mod_top != 0 && wp->w_topline == mod_top)
1144 {
1145 /*
1146 * w_topline is the first changed line, the scrolling will be done
1147 * further down.
1148 */
1149 }
1150 else if (wp->w_lines[0].wl_valid
1151 && (wp->w_topline < wp->w_lines[0].wl_lnum
1152#ifdef FEAT_DIFF
1153 || (wp->w_topline == wp->w_lines[0].wl_lnum
1154 && wp->w_topfill > wp->w_old_topfill)
1155#endif
1156 ))
1157 {
1158 /*
1159 * New topline is above old topline: May scroll down.
1160 */
1161#ifdef FEAT_FOLDING
1162 if (hasAnyFolding(wp))
1163 {
1164 linenr_T ln;
1165
1166 /* count the number of lines we are off, counting a sequence
1167 * of folded lines as one */
1168 j = 0;
1169 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1170 {
1171 ++j;
1172 if (j >= wp->w_height - 2)
1173 break;
1174 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1175 }
1176 }
1177 else
1178#endif
1179 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1180 if (j < wp->w_height - 2) /* not too far off */
1181 {
1182 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1183#ifdef FEAT_DIFF
1184 /* insert extra lines for previously invisible filler lines */
1185 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1186 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1187 - wp->w_old_topfill;
1188#endif
1189 if (i < wp->w_height - 2) /* less than a screen off */
1190 {
1191 /*
1192 * Try to insert the correct number of lines.
1193 * If not the last window, delete the lines at the bottom.
1194 * win_ins_lines may fail when the terminal can't do it.
1195 */
1196 if (i > 0)
1197 check_for_delay(FALSE);
1198 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1199 {
1200 if (wp->w_lines_valid != 0)
1201 {
1202 /* Need to update rows that are new, stop at the
1203 * first one that scrolled down. */
1204 top_end = i;
1205#ifdef FEAT_VISUAL
1206 scrolled_down = TRUE;
1207#endif
1208
1209 /* Move the entries that were scrolled, disable
1210 * the entries for the lines to be redrawn. */
1211 if ((wp->w_lines_valid += j) > wp->w_height)
1212 wp->w_lines_valid = wp->w_height;
1213 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1214 wp->w_lines[idx] = wp->w_lines[idx - j];
1215 while (idx >= 0)
1216 wp->w_lines[idx--].wl_valid = FALSE;
1217 }
1218 }
1219 else
1220 mid_start = 0; /* redraw all lines */
1221 }
1222 else
1223 mid_start = 0; /* redraw all lines */
1224 }
1225 else
1226 mid_start = 0; /* redraw all lines */
1227 }
1228 else
1229 {
1230 /*
1231 * New topline is at or below old topline: May scroll up.
1232 * When topline didn't change, find first entry in w_lines[] that
1233 * needs updating.
1234 */
1235
1236 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1237 j = -1;
1238 row = 0;
1239 for (i = 0; i < wp->w_lines_valid; i++)
1240 {
1241 if (wp->w_lines[i].wl_valid
1242 && wp->w_lines[i].wl_lnum == wp->w_topline)
1243 {
1244 j = i;
1245 break;
1246 }
1247 row += wp->w_lines[i].wl_size;
1248 }
1249 if (j == -1)
1250 {
1251 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1252 * lines */
1253 mid_start = 0;
1254 }
1255 else
1256 {
1257 /*
1258 * Try to delete the correct number of lines.
1259 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1260 */
1261#ifdef FEAT_DIFF
1262 /* If the topline didn't change, delete old filler lines,
1263 * otherwise delete filler lines of the new topline... */
1264 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1265 row += wp->w_old_topfill;
1266 else
1267 row += diff_check_fill(wp, wp->w_topline);
1268 /* ... but don't delete new filler lines. */
1269 row -= wp->w_topfill;
1270#endif
1271 if (row > 0)
1272 {
1273 check_for_delay(FALSE);
1274 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1275 bot_start = wp->w_height - row;
1276 else
1277 mid_start = 0; /* redraw all lines */
1278 }
1279 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1280 {
1281 /*
1282 * Skip the lines (below the deleted lines) that are still
1283 * valid and don't need redrawing. Copy their info
1284 * upwards, to compensate for the deleted lines. Set
1285 * bot_start to the first row that needs redrawing.
1286 */
1287 bot_start = 0;
1288 idx = 0;
1289 for (;;)
1290 {
1291 wp->w_lines[idx] = wp->w_lines[j];
1292 /* stop at line that didn't fit, unless it is still
1293 * valid (no lines deleted) */
1294 if (row > 0 && bot_start + row
1295 + (int)wp->w_lines[j].wl_size > wp->w_height)
1296 {
1297 wp->w_lines_valid = idx + 1;
1298 break;
1299 }
1300 bot_start += wp->w_lines[idx++].wl_size;
1301
1302 /* stop at the last valid entry in w_lines[].wl_size */
1303 if (++j >= wp->w_lines_valid)
1304 {
1305 wp->w_lines_valid = idx;
1306 break;
1307 }
1308 }
1309#ifdef FEAT_DIFF
1310 /* Correct the first entry for filler lines at the top
1311 * when it won't get updated below. */
1312 if (wp->w_p_diff && bot_start > 0)
1313 wp->w_lines[0].wl_size =
1314 plines_win_nofill(wp, wp->w_topline, TRUE)
1315 + wp->w_topfill;
1316#endif
1317 }
1318 }
1319 }
1320
1321 /* When starting redraw in the first line, redraw all lines. When
1322 * there is only one window it's probably faster to clear the screen
1323 * first. */
1324 if (mid_start == 0)
1325 {
1326 mid_end = wp->w_height;
1327 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001328 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001329 /* Clear the screen when it was not done by win_del_lines() or
1330 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1331 * then. */
1332 if (screen_cleared != TRUE)
1333 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001334#ifdef FEAT_WINDOWS
1335 /* The screen was cleared, redraw the tab pages line. */
1336 if (redraw_tabline)
1337 draw_tabline();
1338#endif
1339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001341
1342 /* When win_del_lines() or win_ins_lines() caused the screen to be
1343 * cleared (only happens for the first window) or when screenclear()
1344 * was called directly above, "must_redraw" will have been set to
1345 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1346 if (screen_cleared == TRUE)
1347 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 else
1350 {
1351 /* Not VALID or INVERTED: redraw all lines. */
1352 mid_start = 0;
1353 mid_end = wp->w_height;
1354 }
1355
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001356 if (type == SOME_VALID)
1357 {
1358 /* SOME_VALID: redraw all lines. */
1359 mid_start = 0;
1360 mid_end = wp->w_height;
1361 type = NOT_VALID;
1362 }
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#ifdef FEAT_VISUAL
1365 /* check if we are updating or removing the inverted part */
1366 if ((VIsual_active && buf == curwin->w_buffer)
1367 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1368 {
1369 linenr_T from, to;
1370
1371 if (VIsual_active)
1372 {
1373 if (VIsual_active
1374 && (VIsual_mode != wp->w_old_visual_mode
1375 || type == INVERTED_ALL))
1376 {
1377 /*
1378 * If the type of Visual selection changed, redraw the whole
1379 * selection. Also when the ownership of the X selection is
1380 * gained or lost.
1381 */
1382 if (curwin->w_cursor.lnum < VIsual.lnum)
1383 {
1384 from = curwin->w_cursor.lnum;
1385 to = VIsual.lnum;
1386 }
1387 else
1388 {
1389 from = VIsual.lnum;
1390 to = curwin->w_cursor.lnum;
1391 }
1392 /* redraw more when the cursor moved as well */
1393 if (wp->w_old_cursor_lnum < from)
1394 from = wp->w_old_cursor_lnum;
1395 if (wp->w_old_cursor_lnum > to)
1396 to = wp->w_old_cursor_lnum;
1397 if (wp->w_old_visual_lnum < from)
1398 from = wp->w_old_visual_lnum;
1399 if (wp->w_old_visual_lnum > to)
1400 to = wp->w_old_visual_lnum;
1401 }
1402 else
1403 {
1404 /*
1405 * Find the line numbers that need to be updated: The lines
1406 * between the old cursor position and the current cursor
1407 * position. Also check if the Visual position changed.
1408 */
1409 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1410 {
1411 from = curwin->w_cursor.lnum;
1412 to = wp->w_old_cursor_lnum;
1413 }
1414 else
1415 {
1416 from = wp->w_old_cursor_lnum;
1417 to = curwin->w_cursor.lnum;
1418 if (from == 0) /* Visual mode just started */
1419 from = to;
1420 }
1421
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001422 if (VIsual.lnum != wp->w_old_visual_lnum
1423 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 {
1425 if (wp->w_old_visual_lnum < from
1426 && wp->w_old_visual_lnum != 0)
1427 from = wp->w_old_visual_lnum;
1428 if (wp->w_old_visual_lnum > to)
1429 to = wp->w_old_visual_lnum;
1430 if (VIsual.lnum < from)
1431 from = VIsual.lnum;
1432 if (VIsual.lnum > to)
1433 to = VIsual.lnum;
1434 }
1435 }
1436
1437 /*
1438 * If in block mode and changed column or curwin->w_curswant:
1439 * update all lines.
1440 * First compute the actual start and end column.
1441 */
1442 if (VIsual_mode == Ctrl_V)
1443 {
1444 colnr_T fromc, toc;
1445
1446 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1447 ++toc;
1448 if (curwin->w_curswant == MAXCOL)
1449 toc = MAXCOL;
1450
1451 if (fromc != wp->w_old_cursor_fcol
1452 || toc != wp->w_old_cursor_lcol)
1453 {
1454 if (from > VIsual.lnum)
1455 from = VIsual.lnum;
1456 if (to < VIsual.lnum)
1457 to = VIsual.lnum;
1458 }
1459 wp->w_old_cursor_fcol = fromc;
1460 wp->w_old_cursor_lcol = toc;
1461 }
1462 }
1463 else
1464 {
1465 /* Use the line numbers of the old Visual area. */
1466 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1467 {
1468 from = wp->w_old_cursor_lnum;
1469 to = wp->w_old_visual_lnum;
1470 }
1471 else
1472 {
1473 from = wp->w_old_visual_lnum;
1474 to = wp->w_old_cursor_lnum;
1475 }
1476 }
1477
1478 /*
1479 * There is no need to update lines above the top of the window.
1480 */
1481 if (from < wp->w_topline)
1482 from = wp->w_topline;
1483
1484 /*
1485 * If we know the value of w_botline, use it to restrict the update to
1486 * the lines that are visible in the window.
1487 */
1488 if (wp->w_valid & VALID_BOTLINE)
1489 {
1490 if (from >= wp->w_botline)
1491 from = wp->w_botline - 1;
1492 if (to >= wp->w_botline)
1493 to = wp->w_botline - 1;
1494 }
1495
1496 /*
1497 * Find the minimal part to be updated.
1498 * Watch out for scrolling that made entries in w_lines[] invalid.
1499 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1500 * top_end; need to redraw from top_end to the "to" line.
1501 * A middle mouse click with a Visual selection may change the text
1502 * above the Visual area and reset wl_valid, do count these for
1503 * mid_end (in srow).
1504 */
1505 if (mid_start > 0)
1506 {
1507 lnum = wp->w_topline;
1508 idx = 0;
1509 srow = 0;
1510 if (scrolled_down)
1511 mid_start = top_end;
1512 else
1513 mid_start = 0;
1514 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1515 {
1516 if (wp->w_lines[idx].wl_valid)
1517 mid_start += wp->w_lines[idx].wl_size;
1518 else if (!scrolled_down)
1519 srow += wp->w_lines[idx].wl_size;
1520 ++idx;
1521# ifdef FEAT_FOLDING
1522 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1523 lnum = wp->w_lines[idx].wl_lnum;
1524 else
1525# endif
1526 ++lnum;
1527 }
1528 srow += mid_start;
1529 mid_end = wp->w_height;
1530 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1531 {
1532 if (wp->w_lines[idx].wl_valid
1533 && wp->w_lines[idx].wl_lnum >= to + 1)
1534 {
1535 /* Only update until first row of this line */
1536 mid_end = srow;
1537 break;
1538 }
1539 srow += wp->w_lines[idx].wl_size;
1540 }
1541 }
1542 }
1543
1544 if (VIsual_active && buf == curwin->w_buffer)
1545 {
1546 wp->w_old_visual_mode = VIsual_mode;
1547 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1548 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001549 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 wp->w_old_curswant = curwin->w_curswant;
1551 }
1552 else
1553 {
1554 wp->w_old_visual_mode = 0;
1555 wp->w_old_cursor_lnum = 0;
1556 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001557 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559#endif /* FEAT_VISUAL */
1560
1561#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1562 /* reset got_int, otherwise regexp won't work */
1563 save_got_int = got_int;
1564 got_int = 0;
1565#endif
1566#ifdef FEAT_FOLDING
1567 win_foldinfo.fi_level = 0;
1568#endif
1569
1570 /*
1571 * Update all the window rows.
1572 */
1573 idx = 0; /* first entry in w_lines[].wl_size */
1574 row = 0;
1575 srow = 0;
1576 lnum = wp->w_topline; /* first line shown in window */
1577 for (;;)
1578 {
1579 /* stop updating when reached the end of the window (check for _past_
1580 * the end of the window is at the end of the loop) */
1581 if (row == wp->w_height)
1582 {
1583 didline = TRUE;
1584 break;
1585 }
1586
1587 /* stop updating when hit the end of the file */
1588 if (lnum > buf->b_ml.ml_line_count)
1589 {
1590 eof = TRUE;
1591 break;
1592 }
1593
1594 /* Remember the starting row of the line that is going to be dealt
1595 * with. It is used further down when the line doesn't fit. */
1596 srow = row;
1597
1598 /*
1599 * Update a line when it is in an area that needs updating, when it
1600 * has changes or w_lines[idx] is invalid.
1601 * bot_start may be halfway a wrapped line after using
1602 * win_del_lines(), check if the current line includes it.
1603 * When syntax folding is being used, the saved syntax states will
1604 * already have been updated, we can't see where the syntax state is
1605 * the same again, just update until the end of the window.
1606 */
1607 if (row < top_end
1608 || (row >= mid_start && row < mid_end)
1609#ifdef FEAT_SEARCH_EXTRA
1610 || top_to_mod
1611#endif
1612 || idx >= wp->w_lines_valid
1613 || (row + wp->w_lines[idx].wl_size > bot_start)
1614 || (mod_top != 0
1615 && (lnum == mod_top
1616 || (lnum >= mod_top
1617 && (lnum < mod_bot
1618#ifdef FEAT_SYN_HL
1619 || did_update == DID_FOLD
1620 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001621 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 && (
1623# ifdef FEAT_FOLDING
1624 (foldmethodIsSyntax(wp)
1625 && hasAnyFolding(wp)) ||
1626# endif
1627 syntax_check_changed(lnum)))
1628#endif
1629 )))))
1630 {
1631#ifdef FEAT_SEARCH_EXTRA
1632 if (lnum == mod_top)
1633 top_to_mod = FALSE;
1634#endif
1635
1636 /*
1637 * When at start of changed lines: May scroll following lines
1638 * up or down to minimize redrawing.
1639 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001640 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 */
1642 if (lnum == mod_top
1643 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001644 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 int old_rows = 0;
1647 int new_rows = 0;
1648 int xtra_rows;
1649 linenr_T l;
1650
1651 /* Count the old number of window rows, using w_lines[], which
1652 * should still contain the sizes for the lines as they are
1653 * currently displayed. */
1654 for (i = idx; i < wp->w_lines_valid; ++i)
1655 {
1656 /* Only valid lines have a meaningful wl_lnum. Invalid
1657 * lines are part of the changed area. */
1658 if (wp->w_lines[i].wl_valid
1659 && wp->w_lines[i].wl_lnum == mod_bot)
1660 break;
1661 old_rows += wp->w_lines[i].wl_size;
1662#ifdef FEAT_FOLDING
1663 if (wp->w_lines[i].wl_valid
1664 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1665 {
1666 /* Must have found the last valid entry above mod_bot.
1667 * Add following invalid entries. */
1668 ++i;
1669 while (i < wp->w_lines_valid
1670 && !wp->w_lines[i].wl_valid)
1671 old_rows += wp->w_lines[i++].wl_size;
1672 break;
1673 }
1674#endif
1675 }
1676
1677 if (i >= wp->w_lines_valid)
1678 {
1679 /* We can't find a valid line below the changed lines,
1680 * need to redraw until the end of the window.
1681 * Inserting/deleting lines has no use. */
1682 bot_start = 0;
1683 }
1684 else
1685 {
1686 /* Able to count old number of rows: Count new window
1687 * rows, and may insert/delete lines */
1688 j = idx;
1689 for (l = lnum; l < mod_bot; ++l)
1690 {
1691#ifdef FEAT_FOLDING
1692 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1693 ++new_rows;
1694 else
1695#endif
1696#ifdef FEAT_DIFF
1697 if (l == wp->w_topline)
1698 new_rows += plines_win_nofill(wp, l, TRUE)
1699 + wp->w_topfill;
1700 else
1701#endif
1702 new_rows += plines_win(wp, l, TRUE);
1703 ++j;
1704 if (new_rows > wp->w_height - row - 2)
1705 {
1706 /* it's getting too much, must redraw the rest */
1707 new_rows = 9999;
1708 break;
1709 }
1710 }
1711 xtra_rows = new_rows - old_rows;
1712 if (xtra_rows < 0)
1713 {
1714 /* May scroll text up. If there is not enough
1715 * remaining text or scrolling fails, must redraw the
1716 * rest. If scrolling works, must redraw the text
1717 * below the scrolled text. */
1718 if (row - xtra_rows >= wp->w_height - 2)
1719 mod_bot = MAXLNUM;
1720 else
1721 {
1722 check_for_delay(FALSE);
1723 if (win_del_lines(wp, row,
1724 -xtra_rows, FALSE, FALSE) == FAIL)
1725 mod_bot = MAXLNUM;
1726 else
1727 bot_start = wp->w_height + xtra_rows;
1728 }
1729 }
1730 else if (xtra_rows > 0)
1731 {
1732 /* May scroll text down. If there is not enough
1733 * remaining text of scrolling fails, must redraw the
1734 * rest. */
1735 if (row + xtra_rows >= wp->w_height - 2)
1736 mod_bot = MAXLNUM;
1737 else
1738 {
1739 check_for_delay(FALSE);
1740 if (win_ins_lines(wp, row + old_rows,
1741 xtra_rows, FALSE, FALSE) == FAIL)
1742 mod_bot = MAXLNUM;
1743 else if (top_end > row + old_rows)
1744 /* Scrolled the part at the top that requires
1745 * updating down. */
1746 top_end += xtra_rows;
1747 }
1748 }
1749
1750 /* When not updating the rest, may need to move w_lines[]
1751 * entries. */
1752 if (mod_bot != MAXLNUM && i != j)
1753 {
1754 if (j < i)
1755 {
1756 int x = row + new_rows;
1757
1758 /* move entries in w_lines[] upwards */
1759 for (;;)
1760 {
1761 /* stop at last valid entry in w_lines[] */
1762 if (i >= wp->w_lines_valid)
1763 {
1764 wp->w_lines_valid = j;
1765 break;
1766 }
1767 wp->w_lines[j] = wp->w_lines[i];
1768 /* stop at a line that won't fit */
1769 if (x + (int)wp->w_lines[j].wl_size
1770 > wp->w_height)
1771 {
1772 wp->w_lines_valid = j + 1;
1773 break;
1774 }
1775 x += wp->w_lines[j++].wl_size;
1776 ++i;
1777 }
1778 if (bot_start > x)
1779 bot_start = x;
1780 }
1781 else /* j > i */
1782 {
1783 /* move entries in w_lines[] downwards */
1784 j -= i;
1785 wp->w_lines_valid += j;
1786 if (wp->w_lines_valid > wp->w_height)
1787 wp->w_lines_valid = wp->w_height;
1788 for (i = wp->w_lines_valid; i - j >= idx; --i)
1789 wp->w_lines[i] = wp->w_lines[i - j];
1790
1791 /* The w_lines[] entries for inserted lines are
1792 * now invalid, but wl_size may be used above.
1793 * Reset to zero. */
1794 while (i >= idx)
1795 {
1796 wp->w_lines[i].wl_size = 0;
1797 wp->w_lines[i--].wl_valid = FALSE;
1798 }
1799 }
1800 }
1801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803
1804#ifdef FEAT_FOLDING
1805 /*
1806 * When lines are folded, display one line for all of them.
1807 * Otherwise, display normally (can be several display lines when
1808 * 'wrap' is on).
1809 */
1810 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1811 if (fold_count != 0)
1812 {
1813 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1814 ++row;
1815 --fold_count;
1816 wp->w_lines[idx].wl_folded = TRUE;
1817 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1818# ifdef FEAT_SYN_HL
1819 did_update = DID_FOLD;
1820# endif
1821 }
1822 else
1823#endif
1824 if (idx < wp->w_lines_valid
1825 && wp->w_lines[idx].wl_valid
1826 && wp->w_lines[idx].wl_lnum == lnum
1827 && lnum > wp->w_topline
1828 && !(dy_flags & DY_LASTLINE)
1829 && srow + wp->w_lines[idx].wl_size > wp->w_height
1830#ifdef FEAT_DIFF
1831 && diff_check_fill(wp, lnum) == 0
1832#endif
1833 )
1834 {
1835 /* This line is not going to fit. Don't draw anything here,
1836 * will draw "@ " lines below. */
1837 row = wp->w_height + 1;
1838 }
1839 else
1840 {
1841#ifdef FEAT_SEARCH_EXTRA
1842 prepare_search_hl(wp, lnum);
1843#endif
1844#ifdef FEAT_SYN_HL
1845 /* Let the syntax stuff know we skipped a few lines. */
1846 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001847 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 syntax_end_parsing(syntax_last_parsed + 1);
1849#endif
1850
1851 /*
1852 * Display one line.
1853 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001854 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856#ifdef FEAT_FOLDING
1857 wp->w_lines[idx].wl_folded = FALSE;
1858 wp->w_lines[idx].wl_lastlnum = lnum;
1859#endif
1860#ifdef FEAT_SYN_HL
1861 did_update = DID_LINE;
1862 syntax_last_parsed = lnum;
1863#endif
1864 }
1865
1866 wp->w_lines[idx].wl_lnum = lnum;
1867 wp->w_lines[idx].wl_valid = TRUE;
1868 if (row > wp->w_height) /* past end of screen */
1869 {
1870 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001871 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1873 ++idx;
1874 break;
1875 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001876 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 wp->w_lines[idx].wl_size = row - srow;
1878 ++idx;
1879#ifdef FEAT_FOLDING
1880 lnum += fold_count + 1;
1881#else
1882 ++lnum;
1883#endif
1884 }
1885 else
1886 {
1887 /* This line does not need updating, advance to the next one */
1888 row += wp->w_lines[idx++].wl_size;
1889 if (row > wp->w_height) /* past end of screen */
1890 break;
1891#ifdef FEAT_FOLDING
1892 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1893#else
1894 ++lnum;
1895#endif
1896#ifdef FEAT_SYN_HL
1897 did_update = DID_NONE;
1898#endif
1899 }
1900
1901 if (lnum > buf->b_ml.ml_line_count)
1902 {
1903 eof = TRUE;
1904 break;
1905 }
1906 }
1907 /*
1908 * End of loop over all window lines.
1909 */
1910
1911
1912 if (idx > wp->w_lines_valid)
1913 wp->w_lines_valid = idx;
1914
1915#ifdef FEAT_SYN_HL
1916 /*
1917 * Let the syntax stuff know we stop parsing here.
1918 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001919 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 syntax_end_parsing(syntax_last_parsed + 1);
1921#endif
1922
1923 /*
1924 * If we didn't hit the end of the file, and we didn't finish the last
1925 * line we were working on, then the line didn't fit.
1926 */
1927 wp->w_empty_rows = 0;
1928#ifdef FEAT_DIFF
1929 wp->w_filler_rows = 0;
1930#endif
1931 if (!eof && !didline)
1932 {
1933 if (lnum == wp->w_topline)
1934 {
1935 /*
1936 * Single line that does not fit!
1937 * Don't overwrite it, it can be edited.
1938 */
1939 wp->w_botline = lnum + 1;
1940 }
1941#ifdef FEAT_DIFF
1942 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1943 {
1944 /* Window ends in filler lines. */
1945 wp->w_botline = lnum;
1946 wp->w_filler_rows = wp->w_height - srow;
1947 }
1948#endif
1949 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1950 {
1951 /*
1952 * Last line isn't finished: Display "@@@" at the end.
1953 */
1954 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1955 W_WINROW(wp) + wp->w_height,
1956 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1957 '@', '@', hl_attr(HLF_AT));
1958 set_empty_rows(wp, srow);
1959 wp->w_botline = lnum;
1960 }
1961 else
1962 {
1963 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1964 wp->w_botline = lnum;
1965 }
1966 }
1967 else
1968 {
1969#ifdef FEAT_VERTSPLIT
1970 draw_vsep_win(wp, row);
1971#endif
1972 if (eof) /* we hit the end of the file */
1973 {
1974 wp->w_botline = buf->b_ml.ml_line_count + 1;
1975#ifdef FEAT_DIFF
1976 j = diff_check_fill(wp, wp->w_botline);
1977 if (j > 0 && !wp->w_botfill)
1978 {
1979 /*
1980 * Display filler lines at the end of the file
1981 */
1982 if (char2cells(fill_diff) > 1)
1983 i = '-';
1984 else
1985 i = fill_diff;
1986 if (row + j > wp->w_height)
1987 j = wp->w_height - row;
1988 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1989 row += j;
1990 }
1991#endif
1992 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001993 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 wp->w_botline = lnum;
1995
1996 /* make sure the rest of the screen is blank */
1997 /* put '~'s on rows that aren't part of the file. */
1998 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1999 }
2000
2001 /* Reset the type of redrawing required, the window has been updated. */
2002 wp->w_redr_type = 0;
2003#ifdef FEAT_DIFF
2004 wp->w_old_topfill = wp->w_topfill;
2005 wp->w_old_botfill = wp->w_botfill;
2006#endif
2007
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002008 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
2010 /*
2011 * There is a trick with w_botline. If we invalidate it on each
2012 * change that might modify it, this will cause a lot of expensive
2013 * calls to plines() in update_topline() each time. Therefore the
2014 * value of w_botline is often approximated, and this value is used to
2015 * compute the value of w_topline. If the value of w_botline was
2016 * wrong, check that the value of w_topline is correct (cursor is on
2017 * the visible part of the text). If it's not, we need to redraw
2018 * again. Mostly this just means scrolling up a few lines, so it
2019 * doesn't look too bad. Only do this for the current window (where
2020 * changes are relevant).
2021 */
2022 wp->w_valid |= VALID_BOTLINE;
2023 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2024 {
2025 recursive = TRUE;
2026 curwin->w_valid &= ~VALID_TOPLINE;
2027 update_topline(); /* may invalidate w_botline again */
2028 if (must_redraw != 0)
2029 {
2030 /* Don't update for changes in buffer again. */
2031 i = curbuf->b_mod_set;
2032 curbuf->b_mod_set = FALSE;
2033 win_update(curwin);
2034 must_redraw = 0;
2035 curbuf->b_mod_set = i;
2036 }
2037 recursive = FALSE;
2038 }
2039 }
2040
2041#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2042 /* restore got_int, unless CTRL-C was hit while redrawing */
2043 if (!got_int)
2044 got_int = save_got_int;
2045#endif
2046}
2047
2048#ifdef FEAT_SIGNS
2049static int draw_signcolumn __ARGS((win_T *wp));
2050
2051/*
2052 * Return TRUE when window "wp" has a column to draw signs in.
2053 */
2054 static int
2055draw_signcolumn(wp)
2056 win_T *wp;
2057{
2058 return (wp->w_buffer->b_signlist != NULL
2059# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002060 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061# endif
2062 );
2063}
2064#endif
2065
2066/*
2067 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2068 * as the filler character.
2069 */
2070 static void
2071win_draw_end(wp, c1, c2, row, endrow, hl)
2072 win_T *wp;
2073 int c1;
2074 int c2;
2075 int row;
2076 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002077 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078{
2079#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2080 int n = 0;
2081# define FDC_OFF n
2082#else
2083# define FDC_OFF 0
2084#endif
2085
2086#ifdef FEAT_RIGHTLEFT
2087 if (wp->w_p_rl)
2088 {
2089 /* No check for cmdline window: should never be right-left. */
2090# ifdef FEAT_FOLDING
2091 n = wp->w_p_fdc;
2092
2093 if (n > 0)
2094 {
2095 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002096 if (n > W_WIDTH(wp))
2097 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2099 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2100 ' ', ' ', hl_attr(HLF_FC));
2101 }
2102# endif
2103# ifdef FEAT_SIGNS
2104 if (draw_signcolumn(wp))
2105 {
2106 int nn = n + 2;
2107
2108 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002109 if (nn > W_WIDTH(wp))
2110 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2112 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2113 ' ', ' ', hl_attr(HLF_SC));
2114 n = nn;
2115 }
2116# endif
2117 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2118 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2119 c2, c2, hl_attr(hl));
2120 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2121 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2122 c1, c2, hl_attr(hl));
2123 }
2124 else
2125#endif
2126 {
2127#ifdef FEAT_CMDWIN
2128 if (cmdwin_type != 0 && wp == curwin)
2129 {
2130 /* draw the cmdline character in the leftmost column */
2131 n = 1;
2132 if (n > wp->w_width)
2133 n = wp->w_width;
2134 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2135 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2136 cmdwin_type, ' ', hl_attr(HLF_AT));
2137 }
2138#endif
2139#ifdef FEAT_FOLDING
2140 if (wp->w_p_fdc > 0)
2141 {
2142 int nn = n + wp->w_p_fdc;
2143
2144 /* draw the fold column at the left */
2145 if (nn > W_WIDTH(wp))
2146 nn = W_WIDTH(wp);
2147 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2148 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2149 ' ', ' ', hl_attr(HLF_FC));
2150 n = nn;
2151 }
2152#endif
2153#ifdef FEAT_SIGNS
2154 if (draw_signcolumn(wp))
2155 {
2156 int nn = n + 2;
2157
2158 /* draw the sign column after the fold column */
2159 if (nn > W_WIDTH(wp))
2160 nn = W_WIDTH(wp);
2161 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2162 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2163 ' ', ' ', hl_attr(HLF_SC));
2164 n = nn;
2165 }
2166#endif
2167 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2168 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2169 c1, c2, hl_attr(hl));
2170 }
2171 set_empty_rows(wp, row);
2172}
2173
Bram Moolenaar1a384422010-07-14 19:53:30 +02002174#ifdef FEAT_SYN_HL
2175static int advance_color_col __ARGS((int vcol, int **color_cols));
2176
2177/*
2178 * Advance **color_cols and return TRUE when there are columns to draw.
2179 */
2180 static int
2181advance_color_col(vcol, color_cols)
2182 int vcol;
2183 int **color_cols;
2184{
2185 while (**color_cols >= 0 && vcol > **color_cols)
2186 ++*color_cols;
2187 return (**color_cols >= 0);
2188}
2189#endif
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#ifdef FEAT_FOLDING
2192/*
2193 * Display one folded line.
2194 */
2195 static void
2196fold_line(wp, fold_count, foldinfo, lnum, row)
2197 win_T *wp;
2198 long fold_count;
2199 foldinfo_T *foldinfo;
2200 linenr_T lnum;
2201 int row;
2202{
2203 char_u buf[51];
2204 pos_T *top, *bot;
2205 linenr_T lnume = lnum + fold_count - 1;
2206 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002207 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 int col;
2210 int txtcol;
2211 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002212 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214 /* Build the fold line:
2215 * 1. Add the cmdwin_type for the command-line window
2216 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002217 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 * 4. Compose the text
2219 * 5. Add the text
2220 * 6. set highlighting for the Visual area an other text
2221 */
2222 col = 0;
2223
2224 /*
2225 * 1. Add the cmdwin_type for the command-line window
2226 * Ignores 'rightleft', this window is never right-left.
2227 */
2228#ifdef FEAT_CMDWIN
2229 if (cmdwin_type != 0 && wp == curwin)
2230 {
2231 ScreenLines[off] = cmdwin_type;
2232 ScreenAttrs[off] = hl_attr(HLF_AT);
2233#ifdef FEAT_MBYTE
2234 if (enc_utf8)
2235 ScreenLinesUC[off] = 0;
2236#endif
2237 ++col;
2238 }
2239#endif
2240
2241 /*
2242 * 2. Add the 'foldcolumn'
2243 */
2244 fdc = wp->w_p_fdc;
2245 if (fdc > W_WIDTH(wp) - col)
2246 fdc = W_WIDTH(wp) - col;
2247 if (fdc > 0)
2248 {
2249 fill_foldcolumn(buf, wp, TRUE, lnum);
2250#ifdef FEAT_RIGHTLEFT
2251 if (wp->w_p_rl)
2252 {
2253 int i;
2254
2255 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2256 hl_attr(HLF_FC));
2257 /* reverse the fold column */
2258 for (i = 0; i < fdc; ++i)
2259 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2260 }
2261 else
2262#endif
2263 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2264 col += fdc;
2265 }
2266
2267#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002268# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2269 for (ri = 0; ri < l; ++ri) \
2270 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2271 else \
2272 for (ri = 0; ri < l; ++ri) \
2273 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002275# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2276 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#endif
2278
Bram Moolenaar64486672010-05-16 15:46:46 +02002279 /* Set all attributes of the 'number' or 'relativenumber' column and the
2280 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002281 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283#ifdef FEAT_SIGNS
2284 /* If signs are being displayed, add two spaces. */
2285 if (draw_signcolumn(wp))
2286 {
2287 len = W_WIDTH(wp) - col;
2288 if (len > 0)
2289 {
2290 if (len > 2)
2291 len = 2;
2292# ifdef FEAT_RIGHTLEFT
2293 if (wp->w_p_rl)
2294 /* the line number isn't reversed */
2295 copy_text_attr(off + W_WIDTH(wp) - len - col,
2296 (char_u *)" ", len, hl_attr(HLF_FL));
2297 else
2298# endif
2299 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2300 col += len;
2301 }
2302 }
2303#endif
2304
2305 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002306 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002308 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 len = W_WIDTH(wp) - col;
2311 if (len > 0)
2312 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002313 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002314 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002315
2316 if (len > w + 1)
2317 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002318
2319 if (wp->w_p_nu)
2320 /* 'number' */
2321 num = (long)lnum;
2322 else
2323 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002324 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02002325
2326 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327#ifdef FEAT_RIGHTLEFT
2328 if (wp->w_p_rl)
2329 /* the line number isn't reversed */
2330 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2331 hl_attr(HLF_FL));
2332 else
2333#endif
2334 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2335 col += len;
2336 }
2337 }
2338
2339 /*
2340 * 4. Compose the folded-line string with 'foldtext', if set.
2341 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002342 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344 txtcol = col; /* remember where text starts */
2345
2346 /*
2347 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2348 * Right-left text is put in columns 0 - number-col, normal text is put
2349 * in columns number-col - window-width.
2350 */
2351#ifdef FEAT_MBYTE
2352 if (has_mbyte)
2353 {
2354 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002355 int u8c, u8cc[MAX_MCO];
2356 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int idx;
2358 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002359 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360# ifdef FEAT_ARABIC
2361 int prev_c = 0; /* previous Arabic character */
2362 int prev_c1 = 0; /* first composing char for prev_c */
2363# endif
2364
2365# ifdef FEAT_RIGHTLEFT
2366 if (wp->w_p_rl)
2367 idx = off;
2368 else
2369# endif
2370 idx = off + col;
2371
2372 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2373 for (p = text; *p != NUL; )
2374 {
2375 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002376 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (col + cells > W_WIDTH(wp)
2378# ifdef FEAT_RIGHTLEFT
2379 - (wp->w_p_rl ? col : 0)
2380# endif
2381 )
2382 break;
2383 ScreenLines[idx] = *p;
2384 if (enc_utf8)
2385 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002386 u8c = utfc_ptr2char(p, u8cc);
2387 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
2389 ScreenLinesUC[idx] = 0;
2390#ifdef FEAT_ARABIC
2391 prev_c = u8c;
2392#endif
2393 }
2394 else
2395 {
2396#ifdef FEAT_ARABIC
2397 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2398 {
2399 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002400 int pc, pc1, nc;
2401 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 int firstbyte = *p;
2403
2404 /* The idea of what is the previous and next
2405 * character depends on 'rightleft'. */
2406 if (wp->w_p_rl)
2407 {
2408 pc = prev_c;
2409 pc1 = prev_c1;
2410 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002411 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413 else
2414 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002415 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002417 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
2419 prev_c = u8c;
2420
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002421 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 pc, pc1, nc);
2423 ScreenLines[idx] = firstbyte;
2424 }
2425 else
2426 prev_c = u8c;
2427#endif
2428 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002429#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (u8c >= 0x10000)
2431 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2432 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002435 for (i = 0; i < Screen_mco; ++i)
2436 {
2437 ScreenLinesC[i][idx] = u8cc[i];
2438 if (u8cc[i] == 0)
2439 break;
2440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442 if (cells > 1)
2443 ScreenLines[idx + 1] = 0;
2444 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002445 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2446 /* double-byte single width character */
2447 ScreenLines2[idx] = p[1];
2448 else if (cells > 1)
2449 /* double-width character */
2450 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 col += cells;
2452 idx += cells;
2453 p += c_len;
2454 }
2455 }
2456 else
2457#endif
2458 {
2459 len = (int)STRLEN(text);
2460 if (len > W_WIDTH(wp) - col)
2461 len = W_WIDTH(wp) - col;
2462 if (len > 0)
2463 {
2464#ifdef FEAT_RIGHTLEFT
2465 if (wp->w_p_rl)
2466 STRNCPY(current_ScreenLine, text, len);
2467 else
2468#endif
2469 STRNCPY(current_ScreenLine + col, text, len);
2470 col += len;
2471 }
2472 }
2473
2474 /* Fill the rest of the line with the fold filler */
2475#ifdef FEAT_RIGHTLEFT
2476 if (wp->w_p_rl)
2477 col -= txtcol;
2478#endif
2479 while (col < W_WIDTH(wp)
2480#ifdef FEAT_RIGHTLEFT
2481 - (wp->w_p_rl ? txtcol : 0)
2482#endif
2483 )
2484 {
2485#ifdef FEAT_MBYTE
2486 if (enc_utf8)
2487 {
2488 if (fill_fold >= 0x80)
2489 {
2490 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002491 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493 else
2494 ScreenLinesUC[off + col] = 0;
2495 }
2496#endif
2497 ScreenLines[off + col++] = fill_fold;
2498 }
2499
2500 if (text != buf)
2501 vim_free(text);
2502
2503 /*
2504 * 6. set highlighting for the Visual area an other text.
2505 * If all folded lines are in the Visual area, highlight the line.
2506 */
2507#ifdef FEAT_VISUAL
2508 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2509 {
2510 if (ltoreq(curwin->w_cursor, VIsual))
2511 {
2512 /* Visual is after curwin->w_cursor */
2513 top = &curwin->w_cursor;
2514 bot = &VIsual;
2515 }
2516 else
2517 {
2518 /* Visual is before curwin->w_cursor */
2519 top = &VIsual;
2520 bot = &curwin->w_cursor;
2521 }
2522 if (lnum >= top->lnum
2523 && lnume <= bot->lnum
2524 && (VIsual_mode != 'v'
2525 || ((lnum > top->lnum
2526 || (lnum == top->lnum
2527 && top->col == 0))
2528 && (lnume < bot->lnum
2529 || (lnume == bot->lnum
2530 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002531 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 if (VIsual_mode == Ctrl_V)
2534 {
2535 /* Visual block mode: highlight the chars part of the block */
2536 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2537 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002538 if (wp->w_old_cursor_lcol != MAXCOL
2539 && wp->w_old_cursor_lcol + txtcol
2540 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 len = wp->w_old_cursor_lcol;
2542 else
2543 len = W_WIDTH(wp) - txtcol;
2544 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002545 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547 }
2548 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002551 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554 }
2555#endif
2556
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002557#ifdef FEAT_SYN_HL
2558 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002559 if (wp->w_p_cuc)
2560 {
2561 txtcol += wp->w_virtcol;
2562 if (wp->w_p_wrap)
2563 txtcol -= wp->w_skipcol;
2564 else
2565 txtcol -= wp->w_leftcol;
2566 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2567 ScreenAttrs[off + txtcol] = hl_combine_attr(
2568 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2569 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
2572 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2573 (int)W_WIDTH(wp), FALSE);
2574
2575 /*
2576 * Update w_cline_height and w_cline_folded if the cursor line was
2577 * updated (saves a call to plines() later).
2578 */
2579 if (wp == curwin
2580 && lnum <= curwin->w_cursor.lnum
2581 && lnume >= curwin->w_cursor.lnum)
2582 {
2583 curwin->w_cline_row = row;
2584 curwin->w_cline_height = 1;
2585 curwin->w_cline_folded = TRUE;
2586 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2587 }
2588}
2589
2590/*
2591 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2592 */
2593 static void
2594copy_text_attr(off, buf, len, attr)
2595 int off;
2596 char_u *buf;
2597 int len;
2598 int attr;
2599{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002600 int i;
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 mch_memmove(ScreenLines + off, buf, (size_t)len);
2603# ifdef FEAT_MBYTE
2604 if (enc_utf8)
2605 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2606# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002607 for (i = 0; i < len; ++i)
2608 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609}
2610
2611/*
2612 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002613 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 */
2615 static void
2616fill_foldcolumn(p, wp, closed, lnum)
2617 char_u *p;
2618 win_T *wp;
2619 int closed; /* TRUE of FALSE */
2620 linenr_T lnum; /* current line number */
2621{
2622 int i = 0;
2623 int level;
2624 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002625 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 /* Init to all spaces. */
2628 copy_spaces(p, (size_t)wp->w_p_fdc);
2629
2630 level = win_foldinfo.fi_level;
2631 if (level > 0)
2632 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002633 /* If there is only one column put more info in it. */
2634 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 /* If the column is too narrow, we start at the lowest level that
2637 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002638 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (first_level < 1)
2640 first_level = 1;
2641
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002642 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 {
2644 if (win_foldinfo.fi_lnum == lnum
2645 && first_level + i >= win_foldinfo.fi_low_level)
2646 p[i] = '-';
2647 else if (first_level == 1)
2648 p[i] = '|';
2649 else if (first_level + i <= 9)
2650 p[i] = '0' + first_level + i;
2651 else
2652 p[i] = '>';
2653 if (first_level + i == level)
2654 break;
2655 }
2656 }
2657 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002658 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659}
2660#endif /* FEAT_FOLDING */
2661
2662/*
2663 * Display line "lnum" of window 'wp' on the screen.
2664 * Start at row "startrow", stop when "endrow" is reached.
2665 * wp->w_virtcol needs to be valid.
2666 *
2667 * Return the number of last row the line occupies.
2668 */
2669 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002670win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 win_T *wp;
2672 linenr_T lnum;
2673 int startrow;
2674 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002675 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
2677 int col; /* visual column on screen */
2678 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2679 int c = 0; /* init for GCC */
2680 long vcol = 0; /* virtual column (for tabs) */
2681 long vcol_prev = -1; /* "vcol" of previous character */
2682 char_u *line; /* current line */
2683 char_u *ptr; /* current position in "line" */
2684 int row; /* row in the window, excl w_winrow */
2685 int screen_row; /* row on the screen, incl w_winrow */
2686
2687 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2688 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002689 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 int c_extra = NUL; /* extra chars, all the same */
2691 int extra_attr = 0; /* attributes when n_extra != 0 */
2692 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2693 displaying lcs_eol at end-of-line */
2694 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2695 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2696
2697 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2698 int saved_n_extra = 0;
2699 char_u *saved_p_extra = NULL;
2700 int saved_c_extra = 0;
2701 int saved_char_attr = 0;
2702
2703 int n_attr = 0; /* chars with special attr */
2704 int saved_attr2 = 0; /* char_attr saved for n_attr */
2705 int n_attr3 = 0; /* chars with overruling special attr */
2706 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2707
2708 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2709
2710 int fromcol, tocol; /* start/end of inverting */
2711 int fromcol_prev = -2; /* start of inverting after cursor */
2712 int noinvcur = FALSE; /* don't invert the cursor */
2713#ifdef FEAT_VISUAL
2714 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002715 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#endif
2717 pos_T pos;
2718 long v;
2719
2720 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002721 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2723 in this line */
2724 int attr = 0; /* attributes for area highlighting */
2725 int area_attr = 0; /* attributes desired by highlighting */
2726 int search_attr = 0; /* attributes desired by 'hlsearch' */
2727#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002728 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 int syntax_attr = 0; /* attributes desired by syntax */
2730 int has_syntax = FALSE; /* this buffer has syntax highl. */
2731 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002732 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002733 int draw_color_col = FALSE; /* highlight colorcolumn */
2734 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002735#endif
2736#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002737 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002738# define SPWORDLEN 150
2739 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002740 int nextlinecol = 0; /* column where nextline[] starts */
2741 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002742 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002743 int spell_attr = 0; /* attributes desired by spelling */
2744 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002745 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2746 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002747 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002748 static int cap_col = -1; /* column to check for Cap word */
2749 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002750 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#endif
2752 int extra_check; /* has syntax or linebreak */
2753#ifdef FEAT_MBYTE
2754 int multi_attr = 0; /* attributes desired by multibyte */
2755 int mb_l = 1; /* multi-byte byte length */
2756 int mb_c = 0; /* decoded multi-byte character */
2757 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002758 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759#endif
2760#ifdef FEAT_DIFF
2761 int filler_lines; /* nr of filler lines to be drawn */
2762 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002763 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 int change_start = MAXCOL; /* first col of changed area */
2765 int change_end = -1; /* last col of changed area */
2766#endif
2767 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2768#ifdef FEAT_LINEBREAK
2769 int need_showbreak = FALSE;
2770#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002771#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2772 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002774 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775#endif
2776#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002777 matchitem_T *cur; /* points to the match list */
2778 match_T *shl; /* points to search_hl or a match */
2779 int shl_flag; /* flag to indicate whether search_hl
2780 has been processed or not */
2781 int prevcol_hl_flag; /* flag to indicate whether prevcol
2782 equals startcol of search_hl or one
2783 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784#endif
2785#ifdef FEAT_ARABIC
2786 int prev_c = 0; /* previous Arabic character */
2787 int prev_c1 = 0; /* first composing char for prev_c */
2788#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002789#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002790 int did_line_attr = 0;
2791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
2793 /* draw_state: items that are drawn in sequence: */
2794#define WL_START 0 /* nothing done yet */
2795#ifdef FEAT_CMDWIN
2796# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2797#else
2798# define WL_CMDLINE WL_START
2799#endif
2800#ifdef FEAT_FOLDING
2801# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2802#else
2803# define WL_FOLD WL_CMDLINE
2804#endif
2805#ifdef FEAT_SIGNS
2806# define WL_SIGN WL_FOLD + 1 /* column for signs */
2807#else
2808# define WL_SIGN WL_FOLD /* column for signs */
2809#endif
2810#define WL_NR WL_SIGN + 1 /* line number */
2811#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2812# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2813#else
2814# define WL_SBR WL_NR
2815#endif
2816#define WL_LINE WL_SBR + 1 /* text in the line */
2817 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002818#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 int feedback_col = 0;
2820 int feedback_old_attr = -1;
2821#endif
2822
Bram Moolenaar860cae12010-06-05 23:22:07 +02002823#ifdef FEAT_CONCEAL
2824 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002825 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002826 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002827 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002828 int is_concealing = FALSE;
2829 int boguscols = 0; /* nonexistent columns added to force
2830 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002831 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002832 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002833# define VCOL_HLC (vcol - vcol_off)
2834#else
2835# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 if (startrow > endrow) /* past the end already! */
2839 return startrow;
2840
2841 row = startrow;
2842 screen_row = row + W_WINROW(wp);
2843
2844 /*
2845 * To speed up the loop below, set extra_check when there is linebreak,
2846 * trailing white space and/or syntax processing to be done.
2847 */
2848#ifdef FEAT_LINEBREAK
2849 extra_check = wp->w_p_lbr;
2850#else
2851 extra_check = 0;
2852#endif
2853#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002854 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
2856 /* Prepare for syntax highlighting in this line. When there is an
2857 * error, stop syntax highlighting. */
2858 save_did_emsg = did_emsg;
2859 did_emsg = FALSE;
2860 syntax_start(wp, lnum);
2861 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002862 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 else
2864 {
2865 did_emsg = save_did_emsg;
2866 has_syntax = TRUE;
2867 extra_check = TRUE;
2868 }
2869 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002870
2871 /* Check for columns to display for 'colorcolumn'. */
2872 color_cols = wp->w_p_cc_cols;
2873 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002874 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002875#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002876
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002877#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002878 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002879 && *wp->w_s->b_p_spl != NUL
2880 && wp->w_s->b_langp.ga_len > 0
2881 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002882 {
2883 /* Prepare for spell checking. */
2884 has_spell = TRUE;
2885 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002886
2887 /* Get the start of the next line, so that words that wrap to the next
2888 * line are found too: "et<line-break>al.".
2889 * Trick: skip a few chars for C/shell/Vim comments */
2890 nextline[SPWORDLEN] = NUL;
2891 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2892 {
2893 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2894 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2895 }
2896
2897 /* When a word wrapped from the previous line the start of the current
2898 * line is valid. */
2899 if (lnum == checked_lnum)
2900 cur_checked_col = checked_col;
2901 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002902
2903 /* When there was a sentence end in the previous line may require a
2904 * word starting with capital in this line. In line 1 always check
2905 * the first word. */
2906 if (lnum != capcol_lnum)
2907 cap_col = -1;
2908 if (lnum == 1)
2909 cap_col = 0;
2910 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#endif
2913
2914 /*
2915 * handle visual active in this window
2916 */
2917 fromcol = -10;
2918 tocol = MAXCOL;
2919#ifdef FEAT_VISUAL
2920 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2921 {
2922 /* Visual is after curwin->w_cursor */
2923 if (ltoreq(curwin->w_cursor, VIsual))
2924 {
2925 top = &curwin->w_cursor;
2926 bot = &VIsual;
2927 }
2928 else /* Visual is before curwin->w_cursor */
2929 {
2930 top = &VIsual;
2931 bot = &curwin->w_cursor;
2932 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002933 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 if (VIsual_mode == Ctrl_V) /* block mode */
2935 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002936 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 {
2938 fromcol = wp->w_old_cursor_fcol;
2939 tocol = wp->w_old_cursor_lcol;
2940 }
2941 }
2942 else /* non-block mode */
2943 {
2944 if (lnum > top->lnum && lnum <= bot->lnum)
2945 fromcol = 0;
2946 else if (lnum == top->lnum)
2947 {
2948 if (VIsual_mode == 'V') /* linewise */
2949 fromcol = 0;
2950 else
2951 {
2952 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2953 if (gchar_pos(top) == NUL)
2954 tocol = fromcol + 1;
2955 }
2956 }
2957 if (VIsual_mode != 'V' && lnum == bot->lnum)
2958 {
2959 if (*p_sel == 'e' && bot->col == 0
2960#ifdef FEAT_VIRTUALEDIT
2961 && bot->coladd == 0
2962#endif
2963 )
2964 {
2965 fromcol = -10;
2966 tocol = MAXCOL;
2967 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002968 else if (bot->col == MAXCOL)
2969 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 else
2971 {
2972 pos = *bot;
2973 if (*p_sel == 'e')
2974 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2975 else
2976 {
2977 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2978 ++tocol;
2979 }
2980 }
2981 }
2982 }
2983
2984#ifndef MSDOS
2985 /* Check if the character under the cursor should not be inverted */
2986 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2987# ifdef FEAT_GUI
2988 && !gui.in_use
2989# endif
2990 )
2991 noinvcur = TRUE;
2992#endif
2993
2994 /* if inverting in this line set area_highlighting */
2995 if (fromcol >= 0)
2996 {
2997 area_highlighting = TRUE;
2998 attr = hl_attr(HLF_V);
2999#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3000 if (clip_star.available && !clip_star.owned && clip_isautosel())
3001 attr = hl_attr(HLF_VNC);
3002#endif
3003 }
3004 }
3005
3006 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003007 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
3009 else
3010#endif /* FEAT_VISUAL */
3011 if (highlight_match
3012 && wp == curwin
3013 && lnum >= curwin->w_cursor.lnum
3014 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3015 {
3016 if (lnum == curwin->w_cursor.lnum)
3017 getvcol(curwin, &(curwin->w_cursor),
3018 (colnr_T *)&fromcol, NULL, NULL);
3019 else
3020 fromcol = 0;
3021 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3022 {
3023 pos.lnum = lnum;
3024 pos.col = search_match_endcol;
3025 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3026 }
3027 else
3028 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003029 /* do at least one character; happens when past end of line */
3030 if (fromcol == tocol)
3031 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 area_highlighting = TRUE;
3033 attr = hl_attr(HLF_I);
3034 }
3035
3036#ifdef FEAT_DIFF
3037 filler_lines = diff_check(wp, lnum);
3038 if (filler_lines < 0)
3039 {
3040 if (filler_lines == -1)
3041 {
3042 if (diff_find_change(wp, lnum, &change_start, &change_end))
3043 diff_hlf = HLF_ADD; /* added line */
3044 else if (change_start == 0)
3045 diff_hlf = HLF_TXD; /* changed text */
3046 else
3047 diff_hlf = HLF_CHD; /* changed line */
3048 }
3049 else
3050 diff_hlf = HLF_ADD; /* added line */
3051 filler_lines = 0;
3052 area_highlighting = TRUE;
3053 }
3054 if (lnum == wp->w_topline)
3055 filler_lines = wp->w_topfill;
3056 filler_todo = filler_lines;
3057#endif
3058
3059#ifdef LINE_ATTR
3060# ifdef FEAT_SIGNS
3061 /* If this line has a sign with line highlighting set line_attr. */
3062 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3063 if (v != 0)
3064 line_attr = sign_get_attr((int)v, TRUE);
3065# endif
3066# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3067 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003068 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 line_attr = hl_attr(HLF_L);
3070# endif
3071 if (line_attr != 0)
3072 area_highlighting = TRUE;
3073#endif
3074
3075 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3076 ptr = line;
3077
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003078#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003079 if (has_spell)
3080 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003081 /* For checking first word with a capital skip white space. */
3082 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003083 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003084
Bram Moolenaar30abd282005-06-22 22:35:10 +00003085 /* To be able to spell-check over line boundaries copy the end of the
3086 * current line into nextline[]. Above the start of the next line was
3087 * copied to nextline[SPWORDLEN]. */
3088 if (nextline[SPWORDLEN] == NUL)
3089 {
3090 /* No next line or it is empty. */
3091 nextlinecol = MAXCOL;
3092 nextline_idx = 0;
3093 }
3094 else
3095 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003096 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003097 if (v < SPWORDLEN)
3098 {
3099 /* Short line, use it completely and append the start of the
3100 * next line. */
3101 nextlinecol = 0;
3102 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003103 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003104 nextline_idx = v + 1;
3105 }
3106 else
3107 {
3108 /* Long line, use only the last SPWORDLEN bytes. */
3109 nextlinecol = v - SPWORDLEN;
3110 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3111 nextline_idx = SPWORDLEN + 1;
3112 }
3113 }
3114 }
3115#endif
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 /* find start of trailing whitespace */
3118 if (wp->w_p_list && lcs_trail)
3119 {
3120 trailcol = (colnr_T)STRLEN(ptr);
3121 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3122 --trailcol;
3123 trailcol += (colnr_T) (ptr - line);
3124 extra_check = TRUE;
3125 }
3126
3127 /*
3128 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3129 * first character to be displayed.
3130 */
3131 if (wp->w_p_wrap)
3132 v = wp->w_skipcol;
3133 else
3134 v = wp->w_leftcol;
3135 if (v > 0)
3136 {
3137#ifdef FEAT_MBYTE
3138 char_u *prev_ptr = ptr;
3139#endif
3140 while (vcol < v && *ptr != NUL)
3141 {
3142 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3143 vcol += c;
3144#ifdef FEAT_MBYTE
3145 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003147 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
3149
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003150#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3151 /* When:
3152 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003153 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003154 * - 'virtualedit' is set, or
3155 * - the visual mode is active,
3156 * the end of the line may be before the start of the displayed part.
3157 */
3158 if (vcol < v && (
3159# ifdef FEAT_SYN_HL
3160 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003161 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003162# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3163 ||
3164# endif
3165# endif
3166# ifdef FEAT_VIRTUALEDIT
3167 virtual_active()
3168# ifdef FEAT_VISUAL
3169 ||
3170# endif
3171# endif
3172# ifdef FEAT_VISUAL
3173 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3174# endif
3175 ))
3176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179#endif
3180
3181 /* Handle a character that's not completely on the screen: Put ptr at
3182 * that character but skip the first few screen characters. */
3183 if (vcol > v)
3184 {
3185 vcol -= c;
3186#ifdef FEAT_MBYTE
3187 ptr = prev_ptr;
3188#else
3189 --ptr;
3190#endif
3191 n_skip = v - vcol;
3192 }
3193
3194 /*
3195 * Adjust for when the inverted text is before the screen,
3196 * and when the start of the inverted text is before the screen.
3197 */
3198 if (tocol <= vcol)
3199 fromcol = 0;
3200 else if (fromcol >= 0 && fromcol < vcol)
3201 fromcol = vcol;
3202
3203#ifdef FEAT_LINEBREAK
3204 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3205 if (wp->w_p_wrap)
3206 need_showbreak = TRUE;
3207#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003208#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003209 /* When spell checking a word we need to figure out the start of the
3210 * word and if it's badly spelled or not. */
3211 if (has_spell)
3212 {
3213 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003214 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003215 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003216
3217 pos = wp->w_cursor;
3218 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003219 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003220 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003221
3222 /* spell_move_to() may call ml_get() and make "line" invalid */
3223 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3224 ptr = line + linecol;
3225
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003226 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003227 {
3228 /* no bad word found at line start, don't check until end of a
3229 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003230 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003231 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003232 }
3233 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003234 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003235 /* bad word found, use attributes until end of word */
3236 word_end = wp->w_cursor.col + len + 1;
3237
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003238 /* Turn index into actual attributes. */
3239 if (spell_hlf != HLF_COUNT)
3240 spell_attr = highlight_attr[spell_hlf];
3241 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003242 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003243
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003244# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003245 /* Need to restart syntax highlighting for this line. */
3246 if (has_syntax)
3247 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003248# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003249 }
3250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 }
3252
3253 /*
3254 * Correct highlighting for cursor that can't be disabled.
3255 * Avoids having to check this for each character.
3256 */
3257 if (fromcol >= 0)
3258 {
3259 if (noinvcur)
3260 {
3261 if ((colnr_T)fromcol == wp->w_virtcol)
3262 {
3263 /* highlighting starts at cursor, let it start just after the
3264 * cursor */
3265 fromcol_prev = fromcol;
3266 fromcol = -1;
3267 }
3268 else if ((colnr_T)fromcol < wp->w_virtcol)
3269 /* restart highlighting after the cursor */
3270 fromcol_prev = wp->w_virtcol;
3271 }
3272 if (fromcol >= tocol)
3273 fromcol = -1;
3274 }
3275
3276#ifdef FEAT_SEARCH_EXTRA
3277 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003278 * Handle highlighting the last used search pattern and matches.
3279 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003281 cur = wp->w_match_head;
3282 shl_flag = FALSE;
3283 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003285 if (shl_flag == FALSE)
3286 {
3287 shl = &search_hl;
3288 shl_flag = TRUE;
3289 }
3290 else
3291 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003292 shl->startcol = MAXCOL;
3293 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 shl->attr_cur = 0;
3295 if (shl->rm.regprog != NULL)
3296 {
3297 v = (long)(ptr - line);
3298 next_search_hl(wp, shl, lnum, (colnr_T)v);
3299
3300 /* Need to get the line again, a multi-line regexp may have made it
3301 * invalid. */
3302 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3303 ptr = line + v;
3304
3305 if (shl->lnum != 0 && shl->lnum <= lnum)
3306 {
3307 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003308 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003310 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3312 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003313 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003315 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003317 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
3319#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003320 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003321 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 else
3323#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003324 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003326 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 shl->attr_cur = shl->attr;
3329 search_attr = shl->attr;
3330 }
3331 area_highlighting = TRUE;
3332 }
3333 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003334 if (shl != &search_hl && cur != NULL)
3335 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337#endif
3338
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003339#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003340 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3341 * active, because it's not clear what is selected then. */
3342 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003343 {
3344 line_attr = hl_attr(HLF_CUL);
3345 area_highlighting = TRUE;
3346 }
3347#endif
3348
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003349 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 col = 0;
3351#ifdef FEAT_RIGHTLEFT
3352 if (wp->w_p_rl)
3353 {
3354 /* Rightleft window: process the text in the normal direction, but put
3355 * it in current_ScreenLine[] from right to left. Start at the
3356 * rightmost column of the window. */
3357 col = W_WIDTH(wp) - 1;
3358 off += col;
3359 }
3360#endif
3361
3362 /*
3363 * Repeat for the whole displayed line.
3364 */
3365 for (;;)
3366 {
3367 /* Skip this quickly when working on the text. */
3368 if (draw_state != WL_LINE)
3369 {
3370#ifdef FEAT_CMDWIN
3371 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3372 {
3373 draw_state = WL_CMDLINE;
3374 if (cmdwin_type != 0 && wp == curwin)
3375 {
3376 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003378 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 char_attr = hl_attr(HLF_AT);
3380 }
3381 }
3382#endif
3383
3384#ifdef FEAT_FOLDING
3385 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3386 {
3387 draw_state = WL_FOLD;
3388 if (wp->w_p_fdc > 0)
3389 {
3390 /* Draw the 'foldcolumn'. */
3391 fill_foldcolumn(extra, wp, FALSE, lnum);
3392 n_extra = wp->w_p_fdc;
3393 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003394 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 c_extra = NUL;
3396 char_attr = hl_attr(HLF_FC);
3397 }
3398 }
3399#endif
3400
3401#ifdef FEAT_SIGNS
3402 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3403 {
3404 draw_state = WL_SIGN;
3405 /* Show the sign column when there are any signs in this
3406 * buffer or when using Netbeans. */
3407 if (draw_signcolumn(wp)
3408# ifdef FEAT_DIFF
3409 && filler_todo <= 0
3410# endif
3411 )
3412 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003413 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003415 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416# endif
3417
3418 /* Draw two cells with the sign value or blank. */
3419 c_extra = ' ';
3420 char_attr = hl_attr(HLF_SC);
3421 n_extra = 2;
3422
3423 if (row == startrow)
3424 {
3425 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3426 SIGN_TEXT);
3427# ifdef FEAT_SIGN_ICONS
3428 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3429 SIGN_ICON);
3430 if (gui.in_use && icon_sign != 0)
3431 {
3432 /* Use the image in this position. */
3433 c_extra = SIGN_BYTE;
3434# ifdef FEAT_NETBEANS_INTG
3435 if (buf_signcount(wp->w_buffer, lnum) > 1)
3436 c_extra = MULTISIGN_BYTE;
3437# endif
3438 char_attr = icon_sign;
3439 }
3440 else
3441# endif
3442 if (text_sign != 0)
3443 {
3444 p_extra = sign_get_text(text_sign);
3445 if (p_extra != NULL)
3446 {
3447 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003448 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
3450 char_attr = sign_get_attr(text_sign, FALSE);
3451 }
3452 }
3453 }
3454 }
3455#endif
3456
3457 if (draw_state == WL_NR - 1 && n_extra == 0)
3458 {
3459 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003460 /* Display the absolute or relative line number. After the
3461 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3462 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 && (row == startrow
3464#ifdef FEAT_DIFF
3465 + filler_lines
3466#endif
3467 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3468 {
3469 /* Draw the line number (empty space after wrapping). */
3470 if (row == startrow
3471#ifdef FEAT_DIFF
3472 + filler_lines
3473#endif
3474 )
3475 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003476 long num;
3477
3478 if (wp->w_p_nu)
3479 /* 'number' */
3480 num = (long)lnum;
3481 else
3482 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003483 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02003484
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003485 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003486 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (wp->w_skipcol > 0)
3488 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3489 *p_extra = '-';
3490#ifdef FEAT_RIGHTLEFT
3491 if (wp->w_p_rl) /* reverse line numbers */
3492 rl_mirror(extra);
3493#endif
3494 p_extra = extra;
3495 c_extra = NUL;
3496 }
3497 else
3498 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003499 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003501#ifdef FEAT_SYN_HL
3502 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003503 * the current line differently.
3504 * TODO: Can we use CursorLine instead of CursorLineNr
3505 * when CursorLineNr isn't set? */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003506 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003507 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 }
3511
3512#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3513 if (draw_state == WL_SBR - 1 && n_extra == 0)
3514 {
3515 draw_state = WL_SBR;
3516# ifdef FEAT_DIFF
3517 if (filler_todo > 0)
3518 {
3519 /* Draw "deleted" diff line(s). */
3520 if (char2cells(fill_diff) > 1)
3521 c_extra = '-';
3522 else
3523 c_extra = fill_diff;
3524# ifdef FEAT_RIGHTLEFT
3525 if (wp->w_p_rl)
3526 n_extra = col + 1;
3527 else
3528# endif
3529 n_extra = W_WIDTH(wp) - col;
3530 char_attr = hl_attr(HLF_DED);
3531 }
3532# endif
3533# ifdef FEAT_LINEBREAK
3534 if (*p_sbr != NUL && need_showbreak)
3535 {
3536 /* Draw 'showbreak' at the start of each broken line. */
3537 p_extra = p_sbr;
3538 c_extra = NUL;
3539 n_extra = (int)STRLEN(p_sbr);
3540 char_attr = hl_attr(HLF_AT);
3541 need_showbreak = FALSE;
3542 /* Correct end of highlighted area for 'showbreak',
3543 * required when 'linebreak' is also set. */
3544 if (tocol == vcol)
3545 tocol += n_extra;
3546 }
3547# endif
3548 }
3549#endif
3550
3551 if (draw_state == WL_LINE - 1 && n_extra == 0)
3552 {
3553 draw_state = WL_LINE;
3554 if (saved_n_extra)
3555 {
3556 /* Continue item from end of wrapped line. */
3557 n_extra = saved_n_extra;
3558 c_extra = saved_c_extra;
3559 p_extra = saved_p_extra;
3560 char_attr = saved_char_attr;
3561 }
3562 else
3563 char_attr = 0;
3564 }
3565 }
3566
3567 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003568 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003569 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570#ifdef FEAT_DIFF
3571 && filler_todo <= 0
3572#endif
3573 )
3574 {
3575 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3576 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003577 /* Pretend we have finished updating the window. Except when
3578 * 'cursorcolumn' is set. */
3579#ifdef FEAT_SYN_HL
3580 if (wp->w_p_cuc)
3581 row = wp->w_cline_row + wp->w_cline_height;
3582 else
3583#endif
3584 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 break;
3586 }
3587
3588 if (draw_state == WL_LINE && area_highlighting)
3589 {
3590 /* handle Visual or match highlighting in this line */
3591 if (vcol == fromcol
3592#ifdef FEAT_MBYTE
3593 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3594 && (*mb_ptr2cells)(ptr) > 1)
3595#endif
3596 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003597 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 && vcol < tocol))
3599 area_attr = attr; /* start highlighting */
3600 else if (area_attr != 0
3601 && (vcol == tocol
3602 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605#ifdef FEAT_SEARCH_EXTRA
3606 if (!n_extra)
3607 {
3608 /*
3609 * Check for start/end of search pattern match.
3610 * After end, check for start/end of next match.
3611 * When another match, have to check for start again.
3612 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003613 * Do this for 'search_hl' and the match list (ordered by
3614 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003616 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003617 cur = wp->w_match_head;
3618 shl_flag = FALSE;
3619 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003621 if (shl_flag == FALSE
3622 && ((cur != NULL
3623 && cur->priority > SEARCH_HL_PRIORITY)
3624 || cur == NULL))
3625 {
3626 shl = &search_hl;
3627 shl_flag = TRUE;
3628 }
3629 else
3630 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 while (shl->rm.regprog != NULL)
3632 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003633 if (shl->startcol != MAXCOL
3634 && v >= (long)shl->startcol
3635 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
3637 shl->attr_cur = shl->attr;
3638 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003639 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641 shl->attr_cur = 0;
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 next_search_hl(wp, shl, lnum, (colnr_T)v);
3644
3645 /* Need to get the line again, a multi-line regexp
3646 * may have made it invalid. */
3647 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3648 ptr = line + v;
3649
3650 if (shl->lnum == lnum)
3651 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003652 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003654 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003656 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003658 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 /* highlight empty match, try again after
3661 * it */
3662#ifdef FEAT_MBYTE
3663 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003664 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003665 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 else
3667#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003668 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670
3671 /* Loop to check if the match starts at the
3672 * current position */
3673 continue;
3674 }
3675 }
3676 break;
3677 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003678 if (shl != &search_hl && cur != NULL)
3679 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003681
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003682 /* Use attributes from match with highest priority among
3683 * 'search_hl' and the match list. */
3684 search_attr = search_hl.attr_cur;
3685 cur = wp->w_match_head;
3686 shl_flag = FALSE;
3687 while (cur != NULL || shl_flag == FALSE)
3688 {
3689 if (shl_flag == FALSE
3690 && ((cur != NULL
3691 && cur->priority > SEARCH_HL_PRIORITY)
3692 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003693 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003694 shl = &search_hl;
3695 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003696 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003697 else
3698 shl = &cur->hl;
3699 if (shl->attr_cur != 0)
3700 search_attr = shl->attr_cur;
3701 if (shl != &search_hl && cur != NULL)
3702 cur = cur->next;
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705#endif
3706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003708 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003710 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3711 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003713 if (diff_hlf == HLF_TXD && ptr - line > change_end
3714 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003716 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003719
3720 /* Decide which of the highlight attributes to use. */
3721 attr_pri = TRUE;
3722 if (area_attr != 0)
3723 char_attr = area_attr;
3724 else if (search_attr != 0)
3725 char_attr = search_attr;
3726#ifdef LINE_ATTR
3727 /* Use line_attr when not in the Visual or 'incsearch' area
3728 * (area_attr may be 0 when "noinvcur" is set). */
3729 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003730 || vcol < fromcol || vcol_prev < fromcol_prev
3731 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003732 char_attr = line_attr;
3733#endif
3734 else
3735 {
3736 attr_pri = FALSE;
3737#ifdef FEAT_SYN_HL
3738 if (has_syntax)
3739 char_attr = syntax_attr;
3740 else
3741#endif
3742 char_attr = 0;
3743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745
3746 /*
3747 * Get the next character to put on the screen.
3748 */
3749 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003750 * The "p_extra" points to the extra stuff that is inserted to
3751 * represent special characters (non-printable stuff) and other
3752 * things. When all characters are the same, c_extra is used.
3753 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3754 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3756 */
3757 if (n_extra > 0)
3758 {
3759 if (c_extra != NUL)
3760 {
3761 c = c_extra;
3762#ifdef FEAT_MBYTE
3763 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3764 if (enc_utf8 && (*mb_char2len)(c) > 1)
3765 {
3766 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003767 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003768 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770 else
3771 mb_utf8 = FALSE;
3772#endif
3773 }
3774 else
3775 {
3776 c = *p_extra;
3777#ifdef FEAT_MBYTE
3778 if (has_mbyte)
3779 {
3780 mb_c = c;
3781 if (enc_utf8)
3782 {
3783 /* If the UTF-8 character is more than one byte:
3784 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003785 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 mb_utf8 = FALSE;
3787 if (mb_l > n_extra)
3788 mb_l = 1;
3789 else if (mb_l > 1)
3790 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003791 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003793 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 }
3796 else
3797 {
3798 /* if this is a DBCS character, put it in "mb_c" */
3799 mb_l = MB_BYTE2LEN(c);
3800 if (mb_l >= n_extra)
3801 mb_l = 1;
3802 else if (mb_l > 1)
3803 mb_c = (c << 8) + p_extra[1];
3804 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003805 if (mb_l == 0) /* at the NUL at end-of-line */
3806 mb_l = 1;
3807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 /* If a double-width char doesn't fit display a '>' in the
3809 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003810 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811# ifdef FEAT_RIGHTLEFT
3812 wp->w_p_rl ? (col <= 0) :
3813# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003814 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 && (*mb_char2cells)(mb_c) == 2)
3816 {
3817 c = '>';
3818 mb_c = c;
3819 mb_l = 1;
3820 mb_utf8 = FALSE;
3821 multi_attr = hl_attr(HLF_AT);
3822 /* put the pointer back to output the double-width
3823 * character at the start of the next line. */
3824 ++n_extra;
3825 --p_extra;
3826 }
3827 else
3828 {
3829 n_extra -= mb_l - 1;
3830 p_extra += mb_l - 1;
3831 }
3832 }
3833#endif
3834 ++p_extra;
3835 }
3836 --n_extra;
3837 }
3838 else
3839 {
3840 /*
3841 * Get a character from the line itself.
3842 */
3843 c = *ptr;
3844#ifdef FEAT_MBYTE
3845 if (has_mbyte)
3846 {
3847 mb_c = c;
3848 if (enc_utf8)
3849 {
3850 /* If the UTF-8 character is more than one byte: Decode it
3851 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003852 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 mb_utf8 = FALSE;
3854 if (mb_l > 1)
3855 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003856 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 /* Overlong encoded ASCII or ASCII with composing char
3858 * is displayed normally, except a NUL. */
3859 if (mb_c < 0x80)
3860 c = mb_c;
3861 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003862
3863 /* At start of the line we can have a composing char.
3864 * Draw it as a space with a composing char. */
3865 if (utf_iscomposing(mb_c))
3866 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003867 int i;
3868
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003869 for (i = Screen_mco - 1; i > 0; --i)
3870 u8cc[i] = u8cc[i - 1];
3871 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003872 mb_c = ' ';
3873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
3875
3876 if ((mb_l == 1 && c >= 0x80)
3877 || (mb_l >= 1 && mb_c == 0)
3878 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003879# ifdef UNICODE16
3880 || mb_c >= 0x10000
3881# endif
3882 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
3884 /*
3885 * Illegal UTF-8 byte: display as <xx>.
3886 * Non-BMP character : display as ? or fullwidth ?.
3887 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003888# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
3892 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003893# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (wp->w_p_rl) /* reverse */
3895 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003898# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 else if (utf_char2cells(mb_c) != 2)
3900 STRCPY(extra, "?");
3901 else
3902 /* 0xff1f in UTF-8: full-width '?' */
3903 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
3906 p_extra = extra;
3907 c = *p_extra;
3908 mb_c = mb_ptr2char_adv(&p_extra);
3909 mb_utf8 = (c >= 0x80);
3910 n_extra = (int)STRLEN(p_extra);
3911 c_extra = NUL;
3912 if (area_attr == 0 && search_attr == 0)
3913 {
3914 n_attr = n_extra + 1;
3915 extra_attr = hl_attr(HLF_8);
3916 saved_attr2 = char_attr; /* save current attr */
3917 }
3918 }
3919 else if (mb_l == 0) /* at the NUL at end-of-line */
3920 mb_l = 1;
3921#ifdef FEAT_ARABIC
3922 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3923 {
3924 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003925 int pc, pc1, nc;
3926 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 /* The idea of what is the previous and next
3929 * character depends on 'rightleft'. */
3930 if (wp->w_p_rl)
3931 {
3932 pc = prev_c;
3933 pc1 = prev_c1;
3934 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003935 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
3937 else
3938 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003939 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003941 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 prev_c = mb_c;
3944
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003945 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947 else
3948 prev_c = mb_c;
3949#endif
3950 }
3951 else /* enc_dbcs */
3952 {
3953 mb_l = MB_BYTE2LEN(c);
3954 if (mb_l == 0) /* at the NUL at end-of-line */
3955 mb_l = 1;
3956 else if (mb_l > 1)
3957 {
3958 /* We assume a second byte below 32 is illegal.
3959 * Hopefully this is OK for all double-byte encodings!
3960 */
3961 if (ptr[1] >= 32)
3962 mb_c = (c << 8) + ptr[1];
3963 else
3964 {
3965 if (ptr[1] == NUL)
3966 {
3967 /* head byte at end of line */
3968 mb_l = 1;
3969 transchar_nonprint(extra, c);
3970 }
3971 else
3972 {
3973 /* illegal tail byte */
3974 mb_l = 2;
3975 STRCPY(extra, "XX");
3976 }
3977 p_extra = extra;
3978 n_extra = (int)STRLEN(extra) - 1;
3979 c_extra = NUL;
3980 c = *p_extra++;
3981 if (area_attr == 0 && search_attr == 0)
3982 {
3983 n_attr = n_extra + 1;
3984 extra_attr = hl_attr(HLF_8);
3985 saved_attr2 = char_attr; /* save current attr */
3986 }
3987 mb_c = c;
3988 }
3989 }
3990 }
3991 /* If a double-width char doesn't fit display a '>' in the
3992 * last column; the character is displayed at the start of the
3993 * next line. */
3994 if ((
3995# ifdef FEAT_RIGHTLEFT
3996 wp->w_p_rl ? (col <= 0) :
3997# endif
3998 (col >= W_WIDTH(wp) - 1))
3999 && (*mb_char2cells)(mb_c) == 2)
4000 {
4001 c = '>';
4002 mb_c = c;
4003 mb_utf8 = FALSE;
4004 mb_l = 1;
4005 multi_attr = hl_attr(HLF_AT);
4006 /* Put pointer back so that the character will be
4007 * displayed at the start of the next line. */
4008 --ptr;
4009 }
4010 else if (*ptr != NUL)
4011 ptr += mb_l - 1;
4012
4013 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004014 * a '<' in the first column. Don't do this for unprintable
4015 * charactes. */
4016 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004019 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 c = ' ';
4021 if (area_attr == 0 && search_attr == 0)
4022 {
4023 n_attr = n_extra + 1;
4024 extra_attr = hl_attr(HLF_AT);
4025 saved_attr2 = char_attr; /* save current attr */
4026 }
4027 mb_c = c;
4028 mb_utf8 = FALSE;
4029 mb_l = 1;
4030 }
4031
4032 }
4033#endif
4034 ++ptr;
4035
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004036 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004037 if (wp->w_p_list && (c == 160
4038#ifdef FEAT_MBYTE
4039 || (mb_utf8 && mb_c == 160)
4040#endif
4041 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004042 {
4043 c = lcs_nbsp;
4044 if (area_attr == 0 && search_attr == 0)
4045 {
4046 n_attr = 1;
4047 extra_attr = hl_attr(HLF_8);
4048 saved_attr2 = char_attr; /* save current attr */
4049 }
4050#ifdef FEAT_MBYTE
4051 mb_c = c;
4052 if (enc_utf8 && (*mb_char2len)(c) > 1)
4053 {
4054 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004055 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004056 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004057 }
4058 else
4059 mb_utf8 = FALSE;
4060#endif
4061 }
4062
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 if (extra_check)
4064 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004065#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004066 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004067#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004068
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004069#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 /* Get syntax attribute, unless still at the start of the line
4071 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004072 v = (long)(ptr - line);
4073 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
4075 /* Get the syntax attribute for the character. If there
4076 * is an error, disable syntax highlighting. */
4077 save_did_emsg = did_emsg;
4078 did_emsg = FALSE;
4079
Bram Moolenaar217ad922005-03-20 22:37:15 +00004080 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004081# ifdef FEAT_SPELL
4082 has_spell ? &can_spell :
4083# endif
4084 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004087 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004088 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004089 has_syntax = FALSE;
4090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 else
4092 did_emsg = save_did_emsg;
4093
4094 /* Need to get the line again, a multi-line regexp may
4095 * have made it invalid. */
4096 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4097 ptr = line + v;
4098
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004099 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004101 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004102 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004103# ifdef FEAT_CONCEAL
4104 /* no concealing past the end of the line, it interferes
4105 * with line highlighting */
4106 if (c == NUL)
4107 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004108 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004109 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004112#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004113
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004114#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004115 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004116 * Only do this when there is no syntax highlighting, the
4117 * @Spell cluster is not used or the current syntax item
4118 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004119 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004120 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004121 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004122# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004123 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004124 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004125# endif
4126 if (c != 0 && (
4127# ifdef FEAT_SYN_HL
4128 !has_syntax ||
4129# endif
4130 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004131 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004132 char_u *prev_ptr, *p;
4133 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004134 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004135# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004136 if (has_mbyte)
4137 {
4138 prev_ptr = ptr - mb_l;
4139 v -= mb_l - 1;
4140 }
4141 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004142# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004143 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004144
4145 /* Use nextline[] if possible, it has the start of the
4146 * next line concatenated. */
4147 if ((prev_ptr - line) - nextlinecol >= 0)
4148 p = nextline + (prev_ptr - line) - nextlinecol;
4149 else
4150 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004151 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004152 len = spell_check(wp, p, &spell_hlf, &cap_col,
4153 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004154 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004155
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004156 /* In Insert mode only highlight a word that
4157 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004158 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004159 && (State & INSERT) != 0
4160 && wp->w_cursor.lnum == lnum
4161 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004162 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004163 && wp->w_cursor.col < (colnr_T)word_end)
4164 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004165 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004166 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004167 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004168
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004169 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004170 && (p - nextline) + len > nextline_idx)
4171 {
4172 /* Remember that the good word continues at the
4173 * start of the next line. */
4174 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004175 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004176 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004177
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004178 /* Turn index into actual attributes. */
4179 if (spell_hlf != HLF_COUNT)
4180 spell_attr = highlight_attr[spell_hlf];
4181
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004182 if (cap_col > 0)
4183 {
4184 if (p != prev_ptr
4185 && (p - nextline) + cap_col >= nextline_idx)
4186 {
4187 /* Remember that the word in the next line
4188 * must start with a capital. */
4189 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004190 cap_col = (int)((p - nextline) + cap_col
4191 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004192 }
4193 else
4194 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004195 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004196 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004197 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004198 }
4199 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004200 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004201 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004202 char_attr = hl_combine_attr(char_attr, spell_attr);
4203 else
4204 char_attr = hl_combine_attr(spell_attr, char_attr);
4205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#endif
4207#ifdef FEAT_LINEBREAK
4208 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004209 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 */
4211 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004212 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
4214 n_extra = win_lbr_chartabsize(wp, ptr - (
4215# ifdef FEAT_MBYTE
4216 has_mbyte ? mb_l :
4217# endif
4218 1), (colnr_T)vcol, NULL) - 1;
4219 c_extra = ' ';
4220 if (vim_iswhite(c))
4221 c = ' ';
4222 }
4223#endif
4224
4225 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4226 {
4227 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004228 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
4230 n_attr = 1;
4231 extra_attr = hl_attr(HLF_8);
4232 saved_attr2 = char_attr; /* save current attr */
4233 }
4234#ifdef FEAT_MBYTE
4235 mb_c = c;
4236 if (enc_utf8 && (*mb_char2len)(c) > 1)
4237 {
4238 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004239 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004240 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242 else
4243 mb_utf8 = FALSE;
4244#endif
4245 }
4246 }
4247
4248 /*
4249 * Handling of non-printable characters.
4250 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004251 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
4253 /*
4254 * when getting a character from the file, we may have to
4255 * turn it into something else on the way to putting it
4256 * into "ScreenLines".
4257 */
4258 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4259 {
4260 /* tab amount depends on current column */
4261 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar8a20b0f2011-08-10 14:32:39 +02004262 - VCOL_HLC % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263#ifdef FEAT_MBYTE
4264 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4265#endif
4266 if (wp->w_p_list)
4267 {
4268 c = lcs_tab1;
4269 c_extra = lcs_tab2;
4270 n_attr = n_extra + 1;
4271 extra_attr = hl_attr(HLF_8);
4272 saved_attr2 = char_attr; /* save current attr */
4273#ifdef FEAT_MBYTE
4274 mb_c = c;
4275 if (enc_utf8 && (*mb_char2len)(c) > 1)
4276 {
4277 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004278 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004279 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281#endif
4282 }
4283 else
4284 {
4285 c_extra = ' ';
4286 c = ' ';
4287 }
4288 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004289 else if (c == NUL
4290 && ((wp->w_p_list && lcs_eol > 0)
4291 || ((fromcol >= 0 || fromcol_prev >= 0)
4292 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004293#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004294 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004295#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004296 && (
4297# ifdef FEAT_RIGHTLEFT
4298 wp->w_p_rl ? (col >= 0) :
4299# endif
4300 (col < W_WIDTH(wp)))
4301 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004302 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004303 && (colnr_T)vcol == wp->w_virtcol)))
4304 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004306 /* Display a '$' after the line or highlight an extra
4307 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4309 /* For a diff line the highlighting continues after the
4310 * "$". */
4311 if (
4312# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004313 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314# ifdef LINE_ATTR
4315 &&
4316# endif
4317# endif
4318# ifdef LINE_ATTR
4319 line_attr == 0
4320# endif
4321 )
4322#endif
4323 {
4324#ifdef FEAT_VIRTUALEDIT
4325 /* In virtualedit, visual selections may extend
4326 * beyond end of line. */
4327 if (area_highlighting && virtual_active()
4328 && tocol != MAXCOL && vcol < tocol)
4329 n_extra = 0;
4330 else
4331#endif
4332 {
4333 p_extra = at_end_str;
4334 n_extra = 1;
4335 c_extra = NUL;
4336 }
4337 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004338 if (wp->w_p_list)
4339 c = lcs_eol;
4340 else
4341 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 lcs_eol_one = -1;
4343 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004344 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
4346 extra_attr = hl_attr(HLF_AT);
4347 n_attr = 1;
4348 }
4349#ifdef FEAT_MBYTE
4350 mb_c = c;
4351 if (enc_utf8 && (*mb_char2len)(c) > 1)
4352 {
4353 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004354 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004355 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 else
4358 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4359#endif
4360 }
4361 else if (c != NUL)
4362 {
4363 p_extra = transchar(c);
4364#ifdef FEAT_RIGHTLEFT
4365 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4366 rl_mirror(p_extra); /* reverse "<12>" */
4367#endif
4368 n_extra = byte2cells(c) - 1;
4369 c_extra = NUL;
4370 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004371 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
4373 n_attr = n_extra + 1;
4374 extra_attr = hl_attr(HLF_8);
4375 saved_attr2 = char_attr; /* save current attr */
4376 }
4377#ifdef FEAT_MBYTE
4378 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4379#endif
4380 }
4381#ifdef FEAT_VIRTUALEDIT
4382 else if (VIsual_active
4383 && (VIsual_mode == Ctrl_V
4384 || VIsual_mode == 'v')
4385 && virtual_active()
4386 && tocol != MAXCOL
4387 && vcol < tocol
4388 && (
4389# ifdef FEAT_RIGHTLEFT
4390 wp->w_p_rl ? (col >= 0) :
4391# endif
4392 (col < W_WIDTH(wp))))
4393 {
4394 c = ' ';
4395 --ptr; /* put it back at the NUL */
4396 }
4397#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004398#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 else if ((
4400# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004401 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 ) && (
4405# ifdef FEAT_RIGHTLEFT
4406 wp->w_p_rl ? (col >= 0) :
4407# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004408 (col
4409# ifdef FEAT_CONCEAL
4410 - boguscols
4411# endif
4412 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
4414 /* Highlight until the right side of the window */
4415 c = ' ';
4416 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004417
4418 /* Remember we do the char for line highlighting. */
4419 ++did_line_attr;
4420
4421 /* don't do search HL for the rest of the line */
4422 if (line_attr != 0 && char_attr == search_attr && col > 0)
4423 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424# ifdef FEAT_DIFF
4425 if (diff_hlf == HLF_TXD)
4426 {
4427 diff_hlf = HLF_CHD;
4428 if (attr == 0 || char_attr != attr)
4429 char_attr = hl_attr(diff_hlf);
4430 }
4431# endif
4432 }
4433#endif
4434 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004435
4436#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004437 if ( wp->w_p_cole > 0
4438 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4439 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004440 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004441 && !(lnum_in_visual_area
4442 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004443 {
4444 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004445 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004446 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4447 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004448 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004449 /* First time at this concealed item: display one
4450 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004451 if (syn_get_sub_char() != NUL)
4452 c = syn_get_sub_char();
4453 else if (lcs_conceal != NUL)
4454 c = lcs_conceal;
4455 else
4456 c = ' ';
4457
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004458 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004459
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004460 if (n_extra > 0)
4461 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004462 vcol += n_extra;
4463 if (wp->w_p_wrap && n_extra > 0)
4464 {
4465# ifdef FEAT_RIGHTLEFT
4466 if (wp->w_p_rl)
4467 {
4468 col -= n_extra;
4469 boguscols -= n_extra;
4470 }
4471 else
4472# endif
4473 {
4474 boguscols += n_extra;
4475 col += n_extra;
4476 }
4477 }
4478 n_extra = 0;
4479 n_attr = 0;
4480 }
4481 else if (n_skip == 0)
4482 {
4483 is_concealing = TRUE;
4484 n_skip = 1;
4485 }
4486# ifdef FEAT_MBYTE
4487 mb_c = c;
4488 if (enc_utf8 && (*mb_char2len)(c) > 1)
4489 {
4490 mb_utf8 = TRUE;
4491 u8cc[0] = 0;
4492 c = 0xc0;
4493 }
4494 else
4495 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4496# endif
4497 }
4498 else
4499 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004500 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004501 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004502 }
4503#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004506#ifdef FEAT_CONCEAL
4507 /* In the cursor line and we may be concealing characters: correct
4508 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004509 if (!did_wcol && draw_state == WL_LINE
4510 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004511 && conceal_cursor_line(wp)
4512 && (int)wp->w_virtcol <= vcol + n_skip)
4513 {
4514 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004515 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004516 did_wcol = TRUE;
4517 }
4518#endif
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 /* Don't override visual selection highlighting. */
4521 if (n_attr > 0
4522 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004523 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 char_attr = extra_attr;
4525
Bram Moolenaar81695252004-12-29 20:58:21 +00004526#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 /* XIM don't send preedit_start and preedit_end, but they send
4528 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4529 * im_is_preediting() here. */
4530 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004531 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 && (State & INSERT)
4533 && !p_imdisable
4534 && im_is_preediting()
4535 && draw_state == WL_LINE)
4536 {
4537 colnr_T tcol;
4538
4539 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004540 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 else
4542 tcol = preedit_end_col;
4543 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4544 {
4545 if (feedback_old_attr < 0)
4546 {
4547 feedback_col = 0;
4548 feedback_old_attr = char_attr;
4549 }
4550 char_attr = im_get_feedback_attr(feedback_col);
4551 if (char_attr < 0)
4552 char_attr = feedback_old_attr;
4553 feedback_col++;
4554 }
4555 else if (feedback_old_attr >= 0)
4556 {
4557 char_attr = feedback_old_attr;
4558 feedback_old_attr = -1;
4559 feedback_col = 0;
4560 }
4561 }
4562#endif
4563 /*
4564 * Handle the case where we are in column 0 but not on the first
4565 * character of the line and the user wants us to show us a
4566 * special character (via 'listchars' option "precedes:<char>".
4567 */
4568 if (lcs_prec_todo != NUL
4569 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4570#ifdef FEAT_DIFF
4571 && filler_todo <= 0
4572#endif
4573 && draw_state > WL_NR
4574 && c != NUL)
4575 {
4576 c = lcs_prec;
4577 lcs_prec_todo = NUL;
4578#ifdef FEAT_MBYTE
4579 mb_c = c;
4580 if (enc_utf8 && (*mb_char2len)(c) > 1)
4581 {
4582 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004583 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004584 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 else
4587 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4588#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004589 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
4591 saved_attr3 = char_attr; /* save current attr */
4592 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4593 n_attr3 = 1;
4594 }
4595 }
4596
4597 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004598 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004600 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004601#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004602 || did_line_attr == 1
4603#endif
4604 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004606#ifdef FEAT_SEARCH_EXTRA
4607 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004608
4609 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004610 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004611 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004612#endif
4613
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004614 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 * highlight match at end of line. If it's beyond the last
4616 * char on the screen, just overwrite that one (tricky!) Not
4617 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004618#ifdef FEAT_SEARCH_EXTRA
4619 prevcol_hl_flag = FALSE;
4620 if (prevcol == (long)search_hl.startcol)
4621 prevcol_hl_flag = TRUE;
4622 else
4623 {
4624 cur = wp->w_match_head;
4625 while (cur != NULL)
4626 {
4627 if (prevcol == (long)cur->hl.startcol)
4628 {
4629 prevcol_hl_flag = TRUE;
4630 break;
4631 }
4632 cur = cur->next;
4633 }
4634 }
4635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004637 && ((area_attr != 0 && vcol == fromcol
4638#ifdef FEAT_VISUAL
4639 && (VIsual_mode != Ctrl_V
4640 || lnum == VIsual.lnum
4641 || lnum == curwin->w_cursor.lnum)
4642#endif
4643 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644#ifdef FEAT_SEARCH_EXTRA
4645 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004646 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004647# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004648 && did_line_attr <= 1
4649# endif
4650 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651#endif
4652 ))
4653 {
4654 int n = 0;
4655
4656#ifdef FEAT_RIGHTLEFT
4657 if (wp->w_p_rl)
4658 {
4659 if (col < 0)
4660 n = 1;
4661 }
4662 else
4663#endif
4664 {
4665 if (col >= W_WIDTH(wp))
4666 n = -1;
4667 }
4668 if (n != 0)
4669 {
4670 /* At the window boundary, highlight the last character
4671 * instead (better than nothing). */
4672 off += n;
4673 col += n;
4674 }
4675 else
4676 {
4677 /* Add a blank character to highlight. */
4678 ScreenLines[off] = ' ';
4679#ifdef FEAT_MBYTE
4680 if (enc_utf8)
4681 ScreenLinesUC[off] = 0;
4682#endif
4683 }
4684#ifdef FEAT_SEARCH_EXTRA
4685 if (area_attr == 0)
4686 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004687 /* Use attributes from match with highest priority among
4688 * 'search_hl' and the match list. */
4689 char_attr = search_hl.attr;
4690 cur = wp->w_match_head;
4691 shl_flag = FALSE;
4692 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004693 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004694 if (shl_flag == FALSE
4695 && ((cur != NULL
4696 && cur->priority > SEARCH_HL_PRIORITY)
4697 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004698 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004699 shl = &search_hl;
4700 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004701 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004702 else
4703 shl = &cur->hl;
4704 if ((ptr - line) - 1 == (long)shl->startcol)
4705 char_attr = shl->attr;
4706 if (shl != &search_hl && cur != NULL)
4707 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710#endif
4711 ScreenAttrs[off] = char_attr;
4712#ifdef FEAT_RIGHTLEFT
4713 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004716 --off;
4717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 else
4719#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004722 ++off;
4723 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004724 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004725#ifdef FEAT_SYN_HL
4726 eol_hl_off = 1;
4727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
Bram Moolenaar91170f82006-05-05 21:15:17 +00004731 /*
4732 * At end of the text line.
4733 */
4734 if (c == NUL)
4735 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004736#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004737 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4738 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004739 {
4740 /* highlight last char after line */
4741 --col;
4742 --off;
4743 --vcol;
4744 }
4745
Bram Moolenaar1a384422010-07-14 19:53:30 +02004746 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004747 if (wp->w_p_wrap)
4748 v = wp->w_skipcol;
4749 else
4750 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004751
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004752 /* check if line ends before left margin */
4753 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004754 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004755#ifdef FEAT_CONCEAL
4756 /* Get rid of the boguscols now, we want to draw until the right
4757 * edge for 'cursorcolumn'. */
4758 col -= boguscols;
4759 boguscols = 0;
4760#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004761
4762 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004763 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004764
4765 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004766 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4767 && (int)wp->w_virtcol <
4768 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004769 && lnum != wp->w_cursor.lnum)
4770 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004771# ifdef FEAT_RIGHTLEFT
4772 && !wp->w_p_rl
4773# endif
4774 )
4775 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004776 int rightmost_vcol = 0;
4777 int i;
4778
4779 if (wp->w_p_cuc)
4780 rightmost_vcol = wp->w_virtcol;
4781 if (draw_color_col)
4782 /* determine rightmost colorcolumn to possibly draw */
4783 for (i = 0; color_cols[i] >= 0; ++i)
4784 if (rightmost_vcol < color_cols[i])
4785 rightmost_vcol = color_cols[i];
4786
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004787 while (col < W_WIDTH(wp))
4788 {
4789 ScreenLines[off] = ' ';
4790#ifdef FEAT_MBYTE
4791 if (enc_utf8)
4792 ScreenLinesUC[off] = 0;
4793#endif
4794 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004795 if (draw_color_col)
4796 draw_color_col = advance_color_col(VCOL_HLC,
4797 &color_cols);
4798
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004799 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004800 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004801 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004802 ScreenAttrs[off++] = hl_attr(HLF_MC);
4803 else
4804 ScreenAttrs[off++] = 0;
4805
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004806 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004807 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004808
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004809 ++vcol;
4810 }
4811 }
4812#endif
4813
Bram Moolenaar860cae12010-06-05 23:22:07 +02004814 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4815 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 row++;
4817
4818 /*
4819 * Update w_cline_height and w_cline_folded if the cursor line was
4820 * updated (saves a call to plines() later).
4821 */
4822 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4823 {
4824 curwin->w_cline_row = startrow;
4825 curwin->w_cline_height = row - startrow;
4826#ifdef FEAT_FOLDING
4827 curwin->w_cline_folded = FALSE;
4828#endif
4829 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4830 }
4831
4832 break;
4833 }
4834
4835 /* line continues beyond line end */
4836 if (lcs_ext
4837 && !wp->w_p_wrap
4838#ifdef FEAT_DIFF
4839 && filler_todo <= 0
4840#endif
4841 && (
4842#ifdef FEAT_RIGHTLEFT
4843 wp->w_p_rl ? col == 0 :
4844#endif
4845 col == W_WIDTH(wp) - 1)
4846 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004847 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4849 {
4850 c = lcs_ext;
4851 char_attr = hl_attr(HLF_AT);
4852#ifdef FEAT_MBYTE
4853 mb_c = c;
4854 if (enc_utf8 && (*mb_char2len)(c) > 1)
4855 {
4856 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004857 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004858 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860 else
4861 mb_utf8 = FALSE;
4862#endif
4863 }
4864
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004865#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004866 /* advance to the next 'colorcolumn' */
4867 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004868 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004869
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004870 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004871 * highlight the cursor position itself.
4872 * Also highlight the 'colorcolumn' if it is different than
4873 * 'cursorcolumn' */
4874 vcol_save_attr = -1;
4875 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004876 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004877 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004878 && lnum != wp->w_cursor.lnum)
4879 {
4880 vcol_save_attr = char_attr;
4881 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4882 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004883 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004884 {
4885 vcol_save_attr = char_attr;
4886 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4887 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004888 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004889#endif
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 /*
4892 * Store character to be displayed.
4893 * Skip characters that are left of the screen for 'nowrap'.
4894 */
4895 vcol_prev = vcol;
4896 if (draw_state < WL_LINE || n_skip <= 0)
4897 {
4898 /*
4899 * Store the character.
4900 */
4901#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4902 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4903 {
4904 /* A double-wide character is: put first halve in left cell. */
4905 --off;
4906 --col;
4907 }
4908#endif
4909 ScreenLines[off] = c;
4910#ifdef FEAT_MBYTE
4911 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004912 {
4913 if ((mb_c & 0xff00) == 0x8e00)
4914 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 else if (enc_utf8)
4918 {
4919 if (mb_utf8)
4920 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004921 int i;
4922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004924 if ((c & 0xff) == 0)
4925 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004926 for (i = 0; i < Screen_mco; ++i)
4927 {
4928 ScreenLinesC[i][off] = u8cc[i];
4929 if (u8cc[i] == 0)
4930 break;
4931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 else
4934 ScreenLinesUC[off] = 0;
4935 }
4936 if (multi_attr)
4937 {
4938 ScreenAttrs[off] = multi_attr;
4939 multi_attr = 0;
4940 }
4941 else
4942#endif
4943 ScreenAttrs[off] = char_attr;
4944
4945#ifdef FEAT_MBYTE
4946 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4947 {
4948 /* Need to fill two screen columns. */
4949 ++off;
4950 ++col;
4951 if (enc_utf8)
4952 /* UTF-8: Put a 0 in the second screen char. */
4953 ScreenLines[off] = 0;
4954 else
4955 /* DBCS: Put second byte in the second screen char. */
4956 ScreenLines[off] = mb_c & 0xff;
4957 ++vcol;
4958 /* When "tocol" is halfway a character, set it to the end of
4959 * the character, otherwise highlighting won't stop. */
4960 if (tocol == vcol)
4961 ++tocol;
4962#ifdef FEAT_RIGHTLEFT
4963 if (wp->w_p_rl)
4964 {
4965 /* now it's time to backup one cell */
4966 --off;
4967 --col;
4968 }
4969#endif
4970 }
4971#endif
4972#ifdef FEAT_RIGHTLEFT
4973 if (wp->w_p_rl)
4974 {
4975 --off;
4976 --col;
4977 }
4978 else
4979#endif
4980 {
4981 ++off;
4982 ++col;
4983 }
4984 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004985#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004986 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004987 {
4988 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004989 ++vcol_off;
4990 if (n_extra > 0)
4991 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004992 if (wp->w_p_wrap)
4993 {
4994 /*
4995 * Special voodoo required if 'wrap' is on.
4996 *
4997 * Advance the column indicator to force the line
4998 * drawing to wrap early. This will make the line
4999 * take up the same screen space when parts are concealed,
5000 * so that cursor line computations aren't messed up.
5001 *
5002 * To avoid the fictitious advance of 'col' causing
5003 * trailing junk to be written out of the screen line
5004 * we are building, 'boguscols' keeps track of the number
5005 * of bad columns we have advanced.
5006 */
5007 if (n_extra > 0)
5008 {
5009 vcol += n_extra;
5010# ifdef FEAT_RIGHTLEFT
5011 if (wp->w_p_rl)
5012 {
5013 col -= n_extra;
5014 boguscols -= n_extra;
5015 }
5016 else
5017# endif
5018 {
5019 col += n_extra;
5020 boguscols += n_extra;
5021 }
5022 n_extra = 0;
5023 n_attr = 0;
5024 }
5025
5026
5027# ifdef FEAT_MBYTE
5028 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5029 {
5030 /* Need to fill two screen columns. */
5031# ifdef FEAT_RIGHTLEFT
5032 if (wp->w_p_rl)
5033 {
5034 --boguscols;
5035 --col;
5036 }
5037 else
5038# endif
5039 {
5040 ++boguscols;
5041 ++col;
5042 }
5043 }
5044# endif
5045
5046# ifdef FEAT_RIGHTLEFT
5047 if (wp->w_p_rl)
5048 {
5049 --boguscols;
5050 --col;
5051 }
5052 else
5053# endif
5054 {
5055 ++boguscols;
5056 ++col;
5057 }
5058 }
5059 else
5060 {
5061 if (n_extra > 0)
5062 {
5063 vcol += n_extra;
5064 n_extra = 0;
5065 n_attr = 0;
5066 }
5067 }
5068
5069 }
5070#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 else
5072 --n_skip;
5073
Bram Moolenaar64486672010-05-16 15:46:46 +02005074 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5075 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005076 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077#ifdef FEAT_DIFF
5078 && filler_todo <= 0
5079#endif
5080 )
5081 ++vcol;
5082
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005083#ifdef FEAT_SYN_HL
5084 if (vcol_save_attr >= 0)
5085 char_attr = vcol_save_attr;
5086#endif
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 /* restore attributes after "predeces" in 'listchars' */
5089 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5090 char_attr = saved_attr3;
5091
5092 /* restore attributes after last 'listchars' or 'number' char */
5093 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5094 char_attr = saved_attr2;
5095
5096 /*
5097 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005098 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 */
5100 if ((
5101#ifdef FEAT_RIGHTLEFT
5102 wp->w_p_rl ? (col < 0) :
5103#endif
5104 (col >= W_WIDTH(wp)))
5105 && (*ptr != NUL
5106#ifdef FEAT_DIFF
5107 || filler_todo > 0
5108#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005109 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5111 )
5112 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005113#ifdef FEAT_CONCEAL
5114 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5115 (int)W_WIDTH(wp), wp->w_p_rl);
5116 boguscols = 0;
5117#else
5118 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5119 (int)W_WIDTH(wp), wp->w_p_rl);
5120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 ++row;
5122 ++screen_row;
5123
5124 /* When not wrapping and finished diff lines, or when displayed
5125 * '$' and highlighting until last column, break here. */
5126 if ((!wp->w_p_wrap
5127#ifdef FEAT_DIFF
5128 && filler_todo <= 0
5129#endif
5130 ) || lcs_eol_one == -1)
5131 break;
5132
5133 /* When the window is too narrow draw all "@" lines. */
5134 if (draw_state != WL_LINE
5135#ifdef FEAT_DIFF
5136 && filler_todo <= 0
5137#endif
5138 )
5139 {
5140 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5141#ifdef FEAT_VERTSPLIT
5142 draw_vsep_win(wp, row);
5143#endif
5144 row = endrow;
5145 }
5146
5147 /* When line got too long for screen break here. */
5148 if (row == endrow)
5149 {
5150 ++row;
5151 break;
5152 }
5153
5154 if (screen_cur_row == screen_row - 1
5155#ifdef FEAT_DIFF
5156 && filler_todo <= 0
5157#endif
5158 && W_WIDTH(wp) == Columns)
5159 {
5160 /* Remember that the line wraps, used for modeless copy. */
5161 LineWraps[screen_row - 1] = TRUE;
5162
5163 /*
5164 * Special trick to make copy/paste of wrapped lines work with
5165 * xterm/screen: write an extra character beyond the end of
5166 * the line. This will work with all terminal types
5167 * (regardless of the xn,am settings).
5168 * Only do this on a fast tty.
5169 * Only do this if the cursor is on the current line
5170 * (something has been written in it).
5171 * Don't do this for the GUI.
5172 * Don't do this for double-width characters.
5173 * Don't do this for a window not at the right screen border.
5174 */
5175 if (p_tf
5176#ifdef FEAT_GUI
5177 && !gui.in_use
5178#endif
5179#ifdef FEAT_MBYTE
5180 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005181 && ((*mb_off2cells)(LineOffset[screen_row],
5182 LineOffset[screen_row] + screen_Columns)
5183 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005185 + (int)Columns - 2,
5186 LineOffset[screen_row] + screen_Columns)
5187 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#endif
5189 )
5190 {
5191 /* First make sure we are at the end of the screen line,
5192 * then output the same character again to let the
5193 * terminal know about the wrap. If the terminal doesn't
5194 * auto-wrap, we overwrite the character. */
5195 if (screen_cur_col != W_WIDTH(wp))
5196 screen_char(LineOffset[screen_row - 1]
5197 + (unsigned)Columns - 1,
5198 screen_row - 1, (int)(Columns - 1));
5199
5200#ifdef FEAT_MBYTE
5201 /* When there is a multi-byte character, just output a
5202 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005203 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5204 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 out_char(' ');
5206 else
5207#endif
5208 out_char(ScreenLines[LineOffset[screen_row - 1]
5209 + (Columns - 1)]);
5210 /* force a redraw of the first char on the next line */
5211 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5212 screen_start(); /* don't know where cursor is now */
5213 }
5214 }
5215
5216 col = 0;
5217 off = (unsigned)(current_ScreenLine - ScreenLines);
5218#ifdef FEAT_RIGHTLEFT
5219 if (wp->w_p_rl)
5220 {
5221 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5222 off += col;
5223 }
5224#endif
5225
5226 /* reset the drawing state for the start of a wrapped line */
5227 draw_state = WL_START;
5228 saved_n_extra = n_extra;
5229 saved_p_extra = p_extra;
5230 saved_c_extra = c_extra;
5231 saved_char_attr = char_attr;
5232 n_extra = 0;
5233 lcs_prec_todo = lcs_prec;
5234#ifdef FEAT_LINEBREAK
5235# ifdef FEAT_DIFF
5236 if (filler_todo <= 0)
5237# endif
5238 need_showbreak = TRUE;
5239#endif
5240#ifdef FEAT_DIFF
5241 --filler_todo;
5242 /* When the filler lines are actually below the last line of the
5243 * file, don't draw the line itself, break here. */
5244 if (filler_todo == 0 && wp->w_botfill)
5245 break;
5246#endif
5247 }
5248
5249 } /* for every character in the line */
5250
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005251#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005252 /* After an empty line check first word for capital. */
5253 if (*skipwhite(line) == NUL)
5254 {
5255 capcol_lnum = lnum + 1;
5256 cap_col = 0;
5257 }
5258#endif
5259
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 return row;
5261}
5262
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005263#ifdef FEAT_MBYTE
5264static int comp_char_differs __ARGS((int, int));
5265
5266/*
5267 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005268 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005269 */
5270 static int
5271comp_char_differs(off_from, off_to)
5272 int off_from;
5273 int off_to;
5274{
5275 int i;
5276
5277 for (i = 0; i < Screen_mco; ++i)
5278 {
5279 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5280 return TRUE;
5281 if (ScreenLinesC[i][off_from] == 0)
5282 break;
5283 }
5284 return FALSE;
5285}
5286#endif
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288/*
5289 * Check whether the given character needs redrawing:
5290 * - the (first byte of the) character is different
5291 * - the attributes are different
5292 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005293 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 */
5295 static int
5296char_needs_redraw(off_from, off_to, cols)
5297 int off_from;
5298 int off_to;
5299 int cols;
5300{
5301 if (cols > 0
5302 && ((ScreenLines[off_from] != ScreenLines[off_to]
5303 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5304
5305#ifdef FEAT_MBYTE
5306 || (enc_dbcs != 0
5307 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5308 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5309 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5310 : (cols > 1 && ScreenLines[off_from + 1]
5311 != ScreenLines[off_to + 1])))
5312 || (enc_utf8
5313 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5314 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005315 && comp_char_differs(off_from, off_to))
5316 || (cols > 1 && ScreenLines[off_from + 1]
5317 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318#endif
5319 ))
5320 return TRUE;
5321 return FALSE;
5322}
5323
5324/*
5325 * Move one "cooked" screen line to the screen, but only the characters that
5326 * have actually changed. Handle insert/delete character.
5327 * "coloff" gives the first column on the screen for this line.
5328 * "endcol" gives the columns where valid characters are.
5329 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5330 * needs to be cleared, negative otherwise.
5331 * "rlflag" is TRUE in a rightleft window:
5332 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5333 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5334 */
5335 static void
5336screen_line(row, coloff, endcol, clear_width
5337#ifdef FEAT_RIGHTLEFT
5338 , rlflag
5339#endif
5340 )
5341 int row;
5342 int coloff;
5343 int endcol;
5344 int clear_width;
5345#ifdef FEAT_RIGHTLEFT
5346 int rlflag;
5347#endif
5348{
5349 unsigned off_from;
5350 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005351#ifdef FEAT_MBYTE
5352 unsigned max_off_from;
5353 unsigned max_off_to;
5354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 int col = 0;
5356#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5357 int hl;
5358#endif
5359 int force = FALSE; /* force update rest of the line */
5360 int redraw_this /* bool: does character need redraw? */
5361#ifdef FEAT_GUI
5362 = TRUE /* For GUI when while-loop empty */
5363#endif
5364 ;
5365 int redraw_next; /* redraw_this for next character */
5366#ifdef FEAT_MBYTE
5367 int clear_next = FALSE;
5368 int char_cells; /* 1: normal char */
5369 /* 2: occupies two display cells */
5370# define CHAR_CELLS char_cells
5371#else
5372# define CHAR_CELLS 1
5373#endif
5374
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005375 /* Check for illegal row and col, just in case. */
5376 if (row >= Rows)
5377 row = Rows - 1;
5378 if (endcol > Columns)
5379 endcol = Columns;
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381# ifdef FEAT_CLIPBOARD
5382 clip_may_clear_selection(row, row);
5383# endif
5384
5385 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5386 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005387#ifdef FEAT_MBYTE
5388 max_off_from = off_from + screen_Columns;
5389 max_off_to = LineOffset[row] + screen_Columns;
5390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
5392#ifdef FEAT_RIGHTLEFT
5393 if (rlflag)
5394 {
5395 /* Clear rest first, because it's left of the text. */
5396 if (clear_width > 0)
5397 {
5398 while (col <= endcol && ScreenLines[off_to] == ' '
5399 && ScreenAttrs[off_to] == 0
5400# ifdef FEAT_MBYTE
5401 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5402# endif
5403 )
5404 {
5405 ++off_to;
5406 ++col;
5407 }
5408 if (col <= endcol)
5409 screen_fill(row, row + 1, col + coloff,
5410 endcol + coloff + 1, ' ', ' ', 0);
5411 }
5412 col = endcol + 1;
5413 off_to = LineOffset[row] + col + coloff;
5414 off_from += col;
5415 endcol = (clear_width > 0 ? clear_width : -clear_width);
5416 }
5417#endif /* FEAT_RIGHTLEFT */
5418
5419 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5420
5421 while (col < endcol)
5422 {
5423#ifdef FEAT_MBYTE
5424 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005425 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 else
5427 char_cells = 1;
5428#endif
5429
5430 redraw_this = redraw_next;
5431 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5432 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5433
5434#ifdef FEAT_GUI
5435 /* If the next character was bold, then redraw the current character to
5436 * remove any pixels that might have spilt over into us. This only
5437 * happens in the GUI.
5438 */
5439 if (redraw_next && gui.in_use)
5440 {
5441 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005442 if (hl > HL_ALL)
5443 hl = syn_attr2attr(hl);
5444 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 redraw_this = TRUE;
5446 }
5447#endif
5448
5449 if (redraw_this)
5450 {
5451 /*
5452 * Special handling when 'xs' termcap flag set (hpterm):
5453 * Attributes for characters are stored at the position where the
5454 * cursor is when writing the highlighting code. The
5455 * start-highlighting code must be written with the cursor on the
5456 * first highlighted character. The stop-highlighting code must
5457 * be written with the cursor just after the last highlighted
5458 * character.
5459 * Overwriting a character doesn't remove it's highlighting. Need
5460 * to clear the rest of the line, and force redrawing it
5461 * completely.
5462 */
5463 if ( p_wiv
5464 && !force
5465#ifdef FEAT_GUI
5466 && !gui.in_use
5467#endif
5468 && ScreenAttrs[off_to] != 0
5469 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5470 {
5471 /*
5472 * Need to remove highlighting attributes here.
5473 */
5474 windgoto(row, col + coloff);
5475 out_str(T_CE); /* clear rest of this screen line */
5476 screen_start(); /* don't know where cursor is now */
5477 force = TRUE; /* force redraw of rest of the line */
5478 redraw_next = TRUE; /* or else next char would miss out */
5479
5480 /*
5481 * If the previous character was highlighted, need to stop
5482 * highlighting at this character.
5483 */
5484 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5485 {
5486 screen_attr = ScreenAttrs[off_to - 1];
5487 term_windgoto(row, col + coloff);
5488 screen_stop_highlight();
5489 }
5490 else
5491 screen_attr = 0; /* highlighting has stopped */
5492 }
5493#ifdef FEAT_MBYTE
5494 if (enc_dbcs != 0)
5495 {
5496 /* Check if overwriting a double-byte with a single-byte or
5497 * the other way around requires another character to be
5498 * redrawn. For UTF-8 this isn't needed, because comparing
5499 * ScreenLinesUC[] is sufficient. */
5500 if (char_cells == 1
5501 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005502 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 /* Writing a single-cell character over a double-cell
5505 * character: need to redraw the next cell. */
5506 ScreenLines[off_to + 1] = 0;
5507 redraw_next = TRUE;
5508 }
5509 else if (char_cells == 2
5510 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005511 && (*mb_off2cells)(off_to, max_off_to) == 1
5512 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
5514 /* Writing the second half of a double-cell character over
5515 * a double-cell character: need to redraw the second
5516 * cell. */
5517 ScreenLines[off_to + 2] = 0;
5518 redraw_next = TRUE;
5519 }
5520
5521 if (enc_dbcs == DBCS_JPNU)
5522 ScreenLines2[off_to] = ScreenLines2[off_from];
5523 }
5524 /* When writing a single-width character over a double-width
5525 * character and at the end of the redrawn text, need to clear out
5526 * the right halve of the old character.
5527 * Also required when writing the right halve of a double-width
5528 * char over the left halve of an existing one. */
5529 if (has_mbyte && col + char_cells == endcol
5530 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005531 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005533 && (*mb_off2cells)(off_to, max_off_to) == 1
5534 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 clear_next = TRUE;
5536#endif
5537
5538 ScreenLines[off_to] = ScreenLines[off_from];
5539#ifdef FEAT_MBYTE
5540 if (enc_utf8)
5541 {
5542 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5543 if (ScreenLinesUC[off_from] != 0)
5544 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005545 int i;
5546
5547 for (i = 0; i < Screen_mco; ++i)
5548 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550 }
5551 if (char_cells == 2)
5552 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5553#endif
5554
5555#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005556 /* The bold trick makes a single column of pixels appear in the
5557 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 * character should be redrawn too. This happens for our own GUI
5559 * and for some xterms. */
5560 if (
5561# ifdef FEAT_GUI
5562 gui.in_use
5563# endif
5564# if defined(FEAT_GUI) && defined(UNIX)
5565 ||
5566# endif
5567# ifdef UNIX
5568 term_is_xterm
5569# endif
5570 )
5571 {
5572 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005573 if (hl > HL_ALL)
5574 hl = syn_attr2attr(hl);
5575 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 redraw_next = TRUE;
5577 }
5578#endif
5579 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5580#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005581 /* For simplicity set the attributes of second half of a
5582 * double-wide character equal to the first half. */
5583 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005585
5586 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 else
5589#endif
5590 screen_char(off_to, row, col + coloff);
5591 }
5592 else if ( p_wiv
5593#ifdef FEAT_GUI
5594 && !gui.in_use
5595#endif
5596 && col + coloff > 0)
5597 {
5598 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5599 {
5600 /*
5601 * Don't output stop-highlight when moving the cursor, it will
5602 * stop the highlighting when it should continue.
5603 */
5604 screen_attr = 0;
5605 }
5606 else if (screen_attr != 0)
5607 screen_stop_highlight();
5608 }
5609
5610 off_to += CHAR_CELLS;
5611 off_from += CHAR_CELLS;
5612 col += CHAR_CELLS;
5613 }
5614
5615#ifdef FEAT_MBYTE
5616 if (clear_next)
5617 {
5618 /* Clear the second half of a double-wide character of which the left
5619 * half was overwritten with a single-wide character. */
5620 ScreenLines[off_to] = ' ';
5621 if (enc_utf8)
5622 ScreenLinesUC[off_to] = 0;
5623 screen_char(off_to, row, col + coloff);
5624 }
5625#endif
5626
5627 if (clear_width > 0
5628#ifdef FEAT_RIGHTLEFT
5629 && !rlflag
5630#endif
5631 )
5632 {
5633#ifdef FEAT_GUI
5634 int startCol = col;
5635#endif
5636
5637 /* blank out the rest of the line */
5638 while (col < clear_width && ScreenLines[off_to] == ' '
5639 && ScreenAttrs[off_to] == 0
5640#ifdef FEAT_MBYTE
5641 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5642#endif
5643 )
5644 {
5645 ++off_to;
5646 ++col;
5647 }
5648 if (col < clear_width)
5649 {
5650#ifdef FEAT_GUI
5651 /*
5652 * In the GUI, clearing the rest of the line may leave pixels
5653 * behind if the first character cleared was bold. Some bold
5654 * fonts spill over the left. In this case we redraw the previous
5655 * character too. If we didn't skip any blanks above, then we
5656 * only redraw if the character wasn't already redrawn anyway.
5657 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005658 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 {
5660 hl = ScreenAttrs[off_to];
5661 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005662 {
5663 int prev_cells = 1;
5664# ifdef FEAT_MBYTE
5665 if (enc_utf8)
5666 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5667 * that its width is 2. */
5668 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5669 else if (enc_dbcs != 0)
5670 {
5671 /* find previous character by counting from first
5672 * column and get its width. */
5673 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005674 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005675
5676 while (off < off_to)
5677 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005678 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005679 off += prev_cells;
5680 }
5681 }
5682
5683 if (enc_dbcs != 0 && prev_cells > 1)
5684 screen_char_2(off_to - prev_cells, row,
5685 col + coloff - prev_cells);
5686 else
5687# endif
5688 screen_char(off_to - prev_cells, row,
5689 col + coloff - prev_cells);
5690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692#endif
5693 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5694 ' ', ' ', 0);
5695#ifdef FEAT_VERTSPLIT
5696 off_to += clear_width - col;
5697 col = clear_width;
5698#endif
5699 }
5700 }
5701
5702 if (clear_width > 0)
5703 {
5704#ifdef FEAT_VERTSPLIT
5705 /* For a window that's left of another, draw the separator char. */
5706 if (col + coloff < Columns)
5707 {
5708 int c;
5709
5710 c = fillchar_vsep(&hl);
5711 if (ScreenLines[off_to] != c
5712# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005713 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5714 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715# endif
5716 || ScreenAttrs[off_to] != hl)
5717 {
5718 ScreenLines[off_to] = c;
5719 ScreenAttrs[off_to] = hl;
5720# ifdef FEAT_MBYTE
5721 if (enc_utf8)
5722 {
5723 if (c >= 0x80)
5724 {
5725 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005726 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
5728 else
5729 ScreenLinesUC[off_to] = 0;
5730 }
5731# endif
5732 screen_char(off_to, row, col + coloff);
5733 }
5734 }
5735 else
5736#endif
5737 LineWraps[row] = FALSE;
5738 }
5739}
5740
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005741#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005743 * Mirror text "str" for right-left displaying.
5744 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005746 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747rl_mirror(str)
5748 char_u *str;
5749{
5750 char_u *p1, *p2;
5751 int t;
5752
5753 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5754 {
5755 t = *p1;
5756 *p1 = *p2;
5757 *p2 = t;
5758 }
5759}
5760#endif
5761
5762#if defined(FEAT_WINDOWS) || defined(PROTO)
5763/*
5764 * mark all status lines for redraw; used after first :cd
5765 */
5766 void
5767status_redraw_all()
5768{
5769 win_T *wp;
5770
5771 for (wp = firstwin; wp; wp = wp->w_next)
5772 if (wp->w_status_height)
5773 {
5774 wp->w_redr_status = TRUE;
5775 redraw_later(VALID);
5776 }
5777}
5778
5779/*
5780 * mark all status lines of the current buffer for redraw
5781 */
5782 void
5783status_redraw_curbuf()
5784{
5785 win_T *wp;
5786
5787 for (wp = firstwin; wp; wp = wp->w_next)
5788 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5789 {
5790 wp->w_redr_status = TRUE;
5791 redraw_later(VALID);
5792 }
5793}
5794
5795/*
5796 * Redraw all status lines that need to be redrawn.
5797 */
5798 void
5799redraw_statuslines()
5800{
5801 win_T *wp;
5802
5803 for (wp = firstwin; wp; wp = wp->w_next)
5804 if (wp->w_redr_status)
5805 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005806 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005807 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808}
5809#endif
5810
5811#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5812/*
5813 * Redraw all status lines at the bottom of frame "frp".
5814 */
5815 void
5816win_redraw_last_status(frp)
5817 frame_T *frp;
5818{
5819 if (frp->fr_layout == FR_LEAF)
5820 frp->fr_win->w_redr_status = TRUE;
5821 else if (frp->fr_layout == FR_ROW)
5822 {
5823 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5824 win_redraw_last_status(frp);
5825 }
5826 else /* frp->fr_layout == FR_COL */
5827 {
5828 frp = frp->fr_child;
5829 while (frp->fr_next != NULL)
5830 frp = frp->fr_next;
5831 win_redraw_last_status(frp);
5832 }
5833}
5834#endif
5835
5836#ifdef FEAT_VERTSPLIT
5837/*
5838 * Draw the verticap separator right of window "wp" starting with line "row".
5839 */
5840 static void
5841draw_vsep_win(wp, row)
5842 win_T *wp;
5843 int row;
5844{
5845 int hl;
5846 int c;
5847
5848 if (wp->w_vsep_width)
5849 {
5850 /* draw the vertical separator right of this window */
5851 c = fillchar_vsep(&hl);
5852 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5853 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5854 c, ' ', hl);
5855 }
5856}
5857#endif
5858
5859#ifdef FEAT_WILDMENU
5860static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005861static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005864 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 */
5866 static int
5867status_match_len(xp, s)
5868 expand_T *xp;
5869 char_u *s;
5870{
5871 int len = 0;
5872
5873#ifdef FEAT_MENU
5874 int emenu = (xp->xp_context == EXPAND_MENUS
5875 || xp->xp_context == EXPAND_MENUNAMES);
5876
5877 /* Check for menu separators - replace with '|'. */
5878 if (emenu && menu_is_separator(s))
5879 return 1;
5880#endif
5881
5882 while (*s != NUL)
5883 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005884 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005885 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005886 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
5888
5889 return len;
5890}
5891
5892/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005893 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005894 * These are backslashes used for escaping. Do show backslashes in help tags.
5895 */
5896 static int
5897skip_status_match_char(xp, s)
5898 expand_T *xp;
5899 char_u *s;
5900{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005901 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005902#ifdef FEAT_MENU
5903 || ((xp->xp_context == EXPAND_MENUS
5904 || xp->xp_context == EXPAND_MENUNAMES)
5905 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5906#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005907 )
5908 {
5909#ifndef BACKSLASH_IN_FILENAME
5910 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5911 return 2;
5912#endif
5913 return 1;
5914 }
5915 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005916}
5917
5918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 * Show wildchar matches in the status line.
5920 * Show at least the "match" item.
5921 * We start at item 'first_match' in the list and show all matches that fit.
5922 *
5923 * If inversion is possible we use it. Else '=' characters are used.
5924 */
5925 void
5926win_redr_status_matches(xp, num_matches, matches, match, showtail)
5927 expand_T *xp;
5928 int num_matches;
5929 char_u **matches; /* list of matches */
5930 int match;
5931 int showtail;
5932{
5933#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5934 int row;
5935 char_u *buf;
5936 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005937 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 int fillchar;
5939 int attr;
5940 int i;
5941 int highlight = TRUE;
5942 char_u *selstart = NULL;
5943 int selstart_col = 0;
5944 char_u *selend = NULL;
5945 static int first_match = 0;
5946 int add_left = FALSE;
5947 char_u *s;
5948#ifdef FEAT_MENU
5949 int emenu;
5950#endif
5951#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5952 int l;
5953#endif
5954
5955 if (matches == NULL) /* interrupted completion? */
5956 return;
5957
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005958#ifdef FEAT_MBYTE
5959 if (has_mbyte)
5960 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5961 else
5962#endif
5963 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (buf == NULL)
5965 return;
5966
5967 if (match == -1) /* don't show match but original text */
5968 {
5969 match = 0;
5970 highlight = FALSE;
5971 }
5972 /* count 1 for the ending ">" */
5973 clen = status_match_len(xp, L_MATCH(match)) + 3;
5974 if (match == 0)
5975 first_match = 0;
5976 else if (match < first_match)
5977 {
5978 /* jumping left, as far as we can go */
5979 first_match = match;
5980 add_left = TRUE;
5981 }
5982 else
5983 {
5984 /* check if match fits on the screen */
5985 for (i = first_match; i < match; ++i)
5986 clen += status_match_len(xp, L_MATCH(i)) + 2;
5987 if (first_match > 0)
5988 clen += 2;
5989 /* jumping right, put match at the left */
5990 if ((long)clen > Columns)
5991 {
5992 first_match = match;
5993 /* if showing the last match, we can add some on the left */
5994 clen = 2;
5995 for (i = match; i < num_matches; ++i)
5996 {
5997 clen += status_match_len(xp, L_MATCH(i)) + 2;
5998 if ((long)clen >= Columns)
5999 break;
6000 }
6001 if (i == num_matches)
6002 add_left = TRUE;
6003 }
6004 }
6005 if (add_left)
6006 while (first_match > 0)
6007 {
6008 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6009 if ((long)clen >= Columns)
6010 break;
6011 --first_match;
6012 }
6013
6014 fillchar = fillchar_status(&attr, TRUE);
6015
6016 if (first_match == 0)
6017 {
6018 *buf = NUL;
6019 len = 0;
6020 }
6021 else
6022 {
6023 STRCPY(buf, "< ");
6024 len = 2;
6025 }
6026 clen = len;
6027
6028 i = first_match;
6029 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6030 {
6031 if (i == match)
6032 {
6033 selstart = buf + len;
6034 selstart_col = clen;
6035 }
6036
6037 s = L_MATCH(i);
6038 /* Check for menu separators - replace with '|' */
6039#ifdef FEAT_MENU
6040 emenu = (xp->xp_context == EXPAND_MENUS
6041 || xp->xp_context == EXPAND_MENUNAMES);
6042 if (emenu && menu_is_separator(s))
6043 {
6044 STRCPY(buf + len, transchar('|'));
6045 l = (int)STRLEN(buf + len);
6046 len += l;
6047 clen += l;
6048 }
6049 else
6050#endif
6051 for ( ; *s != NUL; ++s)
6052 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006053 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 clen += ptr2cells(s);
6055#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006056 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 {
6058 STRNCPY(buf + len, s, l);
6059 s += l - 1;
6060 len += l;
6061 }
6062 else
6063#endif
6064 {
6065 STRCPY(buf + len, transchar_byte(*s));
6066 len += (int)STRLEN(buf + len);
6067 }
6068 }
6069 if (i == match)
6070 selend = buf + len;
6071
6072 *(buf + len++) = ' ';
6073 *(buf + len++) = ' ';
6074 clen += 2;
6075 if (++i == num_matches)
6076 break;
6077 }
6078
6079 if (i != num_matches)
6080 {
6081 *(buf + len++) = '>';
6082 ++clen;
6083 }
6084
6085 buf[len] = NUL;
6086
6087 row = cmdline_row - 1;
6088 if (row >= 0)
6089 {
6090 if (wild_menu_showing == 0)
6091 {
6092 if (msg_scrolled > 0)
6093 {
6094 /* Put the wildmenu just above the command line. If there is
6095 * no room, scroll the screen one line up. */
6096 if (cmdline_row == Rows - 1)
6097 {
6098 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6099 ++msg_scrolled;
6100 }
6101 else
6102 {
6103 ++cmdline_row;
6104 ++row;
6105 }
6106 wild_menu_showing = WM_SCROLLED;
6107 }
6108 else
6109 {
6110 /* Create status line if needed by setting 'laststatus' to 2.
6111 * Set 'winminheight' to zero to avoid that the window is
6112 * resized. */
6113 if (lastwin->w_status_height == 0)
6114 {
6115 save_p_ls = p_ls;
6116 save_p_wmh = p_wmh;
6117 p_ls = 2;
6118 p_wmh = 0;
6119 last_status(FALSE);
6120 }
6121 wild_menu_showing = WM_SHOWN;
6122 }
6123 }
6124
6125 screen_puts(buf, row, 0, attr);
6126 if (selstart != NULL && highlight)
6127 {
6128 *selend = NUL;
6129 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6130 }
6131
6132 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6133 }
6134
6135#ifdef FEAT_VERTSPLIT
6136 win_redraw_last_status(topframe);
6137#else
6138 lastwin->w_redr_status = TRUE;
6139#endif
6140 vim_free(buf);
6141}
6142#endif
6143
6144#if defined(FEAT_WINDOWS) || defined(PROTO)
6145/*
6146 * Redraw the status line of window wp.
6147 *
6148 * If inversion is possible we use it. Else '=' characters are used.
6149 */
6150 void
6151win_redr_status(wp)
6152 win_T *wp;
6153{
6154 int row;
6155 char_u *p;
6156 int len;
6157 int fillchar;
6158 int attr;
6159 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006160 static int busy = FALSE;
6161
6162 /* It's possible to get here recursively when 'statusline' (indirectly)
6163 * invokes ":redrawstatus". Simply ignore the call then. */
6164 if (busy)
6165 return;
6166 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
6168 wp->w_redr_status = FALSE;
6169 if (wp->w_status_height == 0)
6170 {
6171 /* no status line, can only be last window */
6172 redraw_cmdline = TRUE;
6173 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006174 else if (!redrawing()
6175#ifdef FEAT_INS_EXPAND
6176 /* don't update status line when popup menu is visible and may be
6177 * drawn over it */
6178 || pum_visible()
6179#endif
6180 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 {
6182 /* Don't redraw right now, do it later. */
6183 wp->w_redr_status = TRUE;
6184 }
6185#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006186 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 {
6188 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006189 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 }
6191#endif
6192 else
6193 {
6194 fillchar = fillchar_status(&attr, wp == curwin);
6195
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006196 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 p = NameBuff;
6198 len = (int)STRLEN(p);
6199
6200 if (wp->w_buffer->b_help
6201#ifdef FEAT_QUICKFIX
6202 || wp->w_p_pvw
6203#endif
6204 || bufIsChanged(wp->w_buffer)
6205 || wp->w_buffer->b_p_ro)
6206 *(p + len++) = ' ';
6207 if (wp->w_buffer->b_help)
6208 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006209 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 len += (int)STRLEN(p + len);
6211 }
6212#ifdef FEAT_QUICKFIX
6213 if (wp->w_p_pvw)
6214 {
6215 STRCPY(p + len, _("[Preview]"));
6216 len += (int)STRLEN(p + len);
6217 }
6218#endif
6219 if (bufIsChanged(wp->w_buffer))
6220 {
6221 STRCPY(p + len, "[+]");
6222 len += 3;
6223 }
6224 if (wp->w_buffer->b_p_ro)
6225 {
6226 STRCPY(p + len, "[RO]");
6227 len += 4;
6228 }
6229
6230#ifndef FEAT_VERTSPLIT
6231 this_ru_col = ru_col;
6232 if (this_ru_col < (Columns + 1) / 2)
6233 this_ru_col = (Columns + 1) / 2;
6234#else
6235 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6236 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6237 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6238 if (this_ru_col <= 1)
6239 {
6240 p = (char_u *)"<"; /* No room for file name! */
6241 len = 1;
6242 }
6243 else
6244#endif
6245#ifdef FEAT_MBYTE
6246 if (has_mbyte)
6247 {
6248 int clen = 0, i;
6249
6250 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006251 clen = mb_string2cells(p, -1);
6252
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 /* Find first character that will fit.
6254 * Going from start to end is much faster for DBCS. */
6255 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006256 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 clen -= (*mb_ptr2cells)(p + i);
6258 len = clen;
6259 if (i > 0)
6260 {
6261 p = p + i - 1;
6262 *p = '<';
6263 ++len;
6264 }
6265
6266 }
6267 else
6268#endif
6269 if (len > this_ru_col - 1)
6270 {
6271 p += len - (this_ru_col - 1);
6272 *p = '<';
6273 len = this_ru_col - 1;
6274 }
6275
6276 row = W_WINROW(wp) + wp->w_height;
6277 screen_puts(p, row, W_WINCOL(wp), attr);
6278 screen_fill(row, row + 1, len + W_WINCOL(wp),
6279 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6280
6281 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6282 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6283 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6284 - 1 + W_WINCOL(wp)), attr);
6285
6286#ifdef FEAT_CMDL_INFO
6287 win_redr_ruler(wp, TRUE);
6288#endif
6289 }
6290
6291#ifdef FEAT_VERTSPLIT
6292 /*
6293 * May need to draw the character below the vertical separator.
6294 */
6295 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6296 {
6297 if (stl_connected(wp))
6298 fillchar = fillchar_status(&attr, wp == curwin);
6299 else
6300 fillchar = fillchar_vsep(&attr);
6301 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6302 attr);
6303 }
6304#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006305 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306}
6307
Bram Moolenaar238a5642006-02-21 22:12:05 +00006308#ifdef FEAT_STL_OPT
6309/*
6310 * Redraw the status line according to 'statusline' and take care of any
6311 * errors encountered.
6312 */
6313 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006314redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006315 win_T *wp;
6316{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006317 static int entered = FALSE;
6318 int save_called_emsg = called_emsg;
6319
6320 /* When called recursively return. This can happen when the statusline
6321 * contains an expression that triggers a redraw. */
6322 if (entered)
6323 return;
6324 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006325
6326 called_emsg = FALSE;
6327 win_redr_custom(wp, FALSE);
6328 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006329 {
6330 /* When there is an error disable the statusline, otherwise the
6331 * display is messed up with errors and a redraw triggers the problem
6332 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006333 set_string_option_direct((char_u *)"statusline", -1,
6334 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006335 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006336 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006337 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006338 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006339}
6340#endif
6341
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342# ifdef FEAT_VERTSPLIT
6343/*
6344 * Return TRUE if the status line of window "wp" is connected to the status
6345 * line of the window right of it. If not, then it's a vertical separator.
6346 * Only call if (wp->w_vsep_width != 0).
6347 */
6348 int
6349stl_connected(wp)
6350 win_T *wp;
6351{
6352 frame_T *fr;
6353
6354 fr = wp->w_frame;
6355 while (fr->fr_parent != NULL)
6356 {
6357 if (fr->fr_parent->fr_layout == FR_COL)
6358 {
6359 if (fr->fr_next != NULL)
6360 break;
6361 }
6362 else
6363 {
6364 if (fr->fr_next != NULL)
6365 return TRUE;
6366 }
6367 fr = fr->fr_parent;
6368 }
6369 return FALSE;
6370}
6371# endif
6372
6373#endif /* FEAT_WINDOWS */
6374
6375#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6376/*
6377 * Get the value to show for the language mappings, active 'keymap'.
6378 */
6379 int
6380get_keymap_str(wp, buf, len)
6381 win_T *wp;
6382 char_u *buf; /* buffer for the result */
6383 int len; /* length of buffer */
6384{
6385 char_u *p;
6386
6387 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6388 return FALSE;
6389
6390 {
6391#ifdef FEAT_EVAL
6392 buf_T *old_curbuf = curbuf;
6393 win_T *old_curwin = curwin;
6394 char_u *s;
6395
6396 curbuf = wp->w_buffer;
6397 curwin = wp;
6398 STRCPY(buf, "b:keymap_name"); /* must be writable */
6399 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006400 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 --emsg_skip;
6402 curbuf = old_curbuf;
6403 curwin = old_curwin;
6404 if (p == NULL || *p == NUL)
6405#endif
6406 {
6407#ifdef FEAT_KEYMAP
6408 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6409 p = wp->w_buffer->b_p_keymap;
6410 else
6411#endif
6412 p = (char_u *)"lang";
6413 }
6414 if ((int)(STRLEN(p) + 3) < len)
6415 sprintf((char *)buf, "<%s>", p);
6416 else
6417 buf[0] = NUL;
6418#ifdef FEAT_EVAL
6419 vim_free(s);
6420#endif
6421 }
6422 return buf[0] != NUL;
6423}
6424#endif
6425
6426#if defined(FEAT_STL_OPT) || defined(PROTO)
6427/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006428 * Redraw the status line or ruler of window "wp".
6429 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 */
6431 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006432win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006434 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
6436 int attr;
6437 int curattr;
6438 int row;
6439 int col = 0;
6440 int maxwidth;
6441 int width;
6442 int n;
6443 int len;
6444 int fillchar;
6445 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006446 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006448 struct stl_hlrec hltab[STL_MAX_ITEM];
6449 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006450 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006451 win_T *ewp;
6452 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
6454 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006455 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006457 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006458 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006459 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006460 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006461 attr = hl_attr(HLF_TPF);
6462 maxwidth = Columns;
6463# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006464 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006467 else
6468 {
6469 row = W_WINROW(wp) + wp->w_height;
6470 fillchar = fillchar_status(&attr, wp == curwin);
6471 maxwidth = W_WIDTH(wp);
6472
6473 if (draw_ruler)
6474 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006475 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006476 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006477 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006478 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006479 if (*++stl == '-')
6480 stl++;
6481 if (atoi((char *)stl))
6482 while (VIM_ISDIGIT(*stl))
6483 stl++;
6484 if (*stl++ != '(')
6485 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006486 }
6487#ifdef FEAT_VERTSPLIT
6488 col = ru_col - (Columns - W_WIDTH(wp));
6489 if (col < (W_WIDTH(wp) + 1) / 2)
6490 col = (W_WIDTH(wp) + 1) / 2;
6491#else
6492 col = ru_col;
6493 if (col > (Columns + 1) / 2)
6494 col = (Columns + 1) / 2;
6495#endif
6496 maxwidth = W_WIDTH(wp) - col;
6497#ifdef FEAT_WINDOWS
6498 if (!wp->w_status_height)
6499#endif
6500 {
6501 row = Rows - 1;
6502 --maxwidth; /* writing in last column may cause scrolling */
6503 fillchar = ' ';
6504 attr = 0;
6505 }
6506
6507# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006508 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006509# endif
6510 }
6511 else
6512 {
6513 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006514 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006515 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006516 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006517# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006518 use_sandbox = was_set_insecurely((char_u *)"statusline",
6519 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006520# endif
6521 }
6522
6523#ifdef FEAT_VERTSPLIT
6524 col += W_WINCOL(wp);
6525#endif
6526 }
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (maxwidth <= 0)
6529 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
Bram Moolenaar61452852011-02-01 18:01:11 +01006531 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6532 * the cursor away and back. */
6533 ewp = wp == NULL ? curwin : wp;
6534 p_crb_save = ewp->w_p_crb;
6535 ewp->w_p_crb = FALSE;
6536
Bram Moolenaar362f3562009-11-03 16:20:34 +00006537 /* Make a copy, because the statusline may include a function call that
6538 * might change the option value and free the memory. */
6539 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006540 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006541 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006542 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006543 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006544 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006546 /* Make all characters printable. */
6547 p = transstr(buf);
6548 if (p != NULL)
6549 {
6550 vim_strncpy(buf, p, sizeof(buf) - 1);
6551 vim_free(p);
6552 }
6553
6554 /* fill up with "fillchar" */
6555 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006556 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 {
6558#ifdef FEAT_MBYTE
6559 len += (*mb_char2bytes)(fillchar, buf + len);
6560#else
6561 buf[len++] = fillchar;
6562#endif
6563 ++width;
6564 }
6565 buf[len] = NUL;
6566
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006567 /*
6568 * Draw each snippet with the specified highlighting.
6569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 curattr = attr;
6571 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006572 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006574 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 screen_puts_len(p, len, row, col, curattr);
6576 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006577 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006579 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006581 else if (hltab[n].userhl < 0)
6582 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006584 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006585 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586#endif
6587 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006588 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
6590 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006591
6592 if (wp == NULL)
6593 {
6594 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6595 col = 0;
6596 len = 0;
6597 p = buf;
6598 fillchar = 0;
6599 for (n = 0; tabtab[n].start != NULL; n++)
6600 {
6601 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6602 while (col < len)
6603 TabPageIdxs[col++] = fillchar;
6604 p = tabtab[n].start;
6605 fillchar = tabtab[n].userhl;
6606 }
6607 while (col < Columns)
6608 TabPageIdxs[col++] = fillchar;
6609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610}
6611
6612#endif /* FEAT_STL_OPT */
6613
6614/*
6615 * Output a single character directly to the screen and update ScreenLines.
6616 */
6617 void
6618screen_putchar(c, row, col, attr)
6619 int c;
6620 int row, col;
6621 int attr;
6622{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 char_u buf[MB_MAXBYTES + 1];
6624
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006625#ifdef FEAT_MBYTE
6626 if (has_mbyte)
6627 buf[(*mb_char2bytes)(c, buf)] = NUL;
6628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006630 {
6631 buf[0] = c;
6632 buf[1] = NUL;
6633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 screen_puts(buf, row, col, attr);
6635}
6636
6637/*
6638 * Get a single character directly from ScreenLines into "bytes[]".
6639 * Also return its attribute in *attrp;
6640 */
6641 void
6642screen_getbytes(row, col, bytes, attrp)
6643 int row, col;
6644 char_u *bytes;
6645 int *attrp;
6646{
6647 unsigned off;
6648
6649 /* safety check */
6650 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6651 {
6652 off = LineOffset[row] + col;
6653 *attrp = ScreenAttrs[off];
6654 bytes[0] = ScreenLines[off];
6655 bytes[1] = NUL;
6656
6657#ifdef FEAT_MBYTE
6658 if (enc_utf8 && ScreenLinesUC[off] != 0)
6659 bytes[utfc_char2bytes(off, bytes)] = NUL;
6660 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6661 {
6662 bytes[0] = ScreenLines[off];
6663 bytes[1] = ScreenLines2[off];
6664 bytes[2] = NUL;
6665 }
6666 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6667 {
6668 bytes[1] = ScreenLines[off + 1];
6669 bytes[2] = NUL;
6670 }
6671#endif
6672 }
6673}
6674
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006675#ifdef FEAT_MBYTE
6676static int screen_comp_differs __ARGS((int, int*));
6677
6678/*
6679 * Return TRUE if composing characters for screen posn "off" differs from
6680 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006681 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006682 */
6683 static int
6684screen_comp_differs(off, u8cc)
6685 int off;
6686 int *u8cc;
6687{
6688 int i;
6689
6690 for (i = 0; i < Screen_mco; ++i)
6691 {
6692 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6693 return TRUE;
6694 if (u8cc[i] == 0)
6695 break;
6696 }
6697 return FALSE;
6698}
6699#endif
6700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701/*
6702 * Put string '*text' on the screen at position 'row' and 'col', with
6703 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6704 * Note: only outputs within one row, message is truncated at screen boundary!
6705 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6706 */
6707 void
6708screen_puts(text, row, col, attr)
6709 char_u *text;
6710 int row;
6711 int col;
6712 int attr;
6713{
6714 screen_puts_len(text, -1, row, col, attr);
6715}
6716
6717/*
6718 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6719 * a NUL.
6720 */
6721 void
6722screen_puts_len(text, len, row, col, attr)
6723 char_u *text;
6724 int len;
6725 int row;
6726 int col;
6727 int attr;
6728{
6729 unsigned off;
6730 char_u *ptr = text;
6731 int c;
6732#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006733 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 int mbyte_blen = 1;
6735 int mbyte_cells = 1;
6736 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006737 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 int clear_next_cell = FALSE;
6739# ifdef FEAT_ARABIC
6740 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006741 int pc, nc, nc1;
6742 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743# endif
6744#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006745#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6746 int force_redraw_this;
6747 int force_redraw_next = FALSE;
6748#endif
6749 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
6751 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6752 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006753 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
Bram Moolenaarc236c162008-07-13 17:41:49 +00006755#ifdef FEAT_MBYTE
6756 /* When drawing over the right halve of a double-wide char clear out the
6757 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006758 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006759# ifdef FEAT_GUI
6760 && !gui.in_use
6761# endif
6762 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006763 {
6764 ScreenLines[off - 1] = ' ';
6765 ScreenAttrs[off - 1] = 0;
6766 if (enc_utf8)
6767 {
6768 ScreenLinesUC[off - 1] = 0;
6769 ScreenLinesC[0][off - 1] = 0;
6770 }
6771 /* redraw the previous cell, make it empty */
6772 screen_char(off - 1, row, col - 1);
6773 /* force the cell at "col" to be redrawn */
6774 force_redraw_next = TRUE;
6775 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006776#endif
6777
Bram Moolenaar367329b2007-08-30 11:53:22 +00006778#ifdef FEAT_MBYTE
6779 max_off = LineOffset[row] + screen_Columns;
6780#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006781 while (col < screen_Columns
6782 && (len < 0 || (int)(ptr - text) < len)
6783 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {
6785 c = *ptr;
6786#ifdef FEAT_MBYTE
6787 /* check if this is the first byte of a multibyte */
6788 if (has_mbyte)
6789 {
6790 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006791 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006793 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6795 mbyte_cells = 1;
6796 else if (enc_dbcs != 0)
6797 mbyte_cells = mbyte_blen;
6798 else /* enc_utf8 */
6799 {
6800 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006801 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 (int)((text + len) - ptr));
6803 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006804 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006806# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 /* Non-BMP character: display as ? or fullwidth ?. */
6808 if (u8c >= 0x10000)
6809 {
6810 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6811 if (attr == 0)
6812 attr = hl_attr(HLF_8);
6813 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815# ifdef FEAT_ARABIC
6816 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6817 {
6818 /* Do Arabic shaping. */
6819 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6820 {
6821 /* Past end of string to be displayed. */
6822 nc = NUL;
6823 nc1 = NUL;
6824 }
6825 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006826 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006827 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6828 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006829 nc1 = pcc[0];
6830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 pc = prev_c;
6832 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006833 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 }
6835 else
6836 prev_c = u8c;
6837# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006838 if (col + mbyte_cells > screen_Columns)
6839 {
6840 /* Only 1 cell left, but character requires 2 cells:
6841 * display a '>' in the last column to avoid wrapping. */
6842 c = '>';
6843 mbyte_cells = 1;
6844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
6846 }
6847#endif
6848
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006849#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6850 force_redraw_this = force_redraw_next;
6851 force_redraw_next = FALSE;
6852#endif
6853
6854 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855#ifdef FEAT_MBYTE
6856 || (mbyte_cells == 2
6857 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6858 || (enc_dbcs == DBCS_JPNU
6859 && c == 0x8e
6860 && ScreenLines2[off] != ptr[1])
6861 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006862 && (ScreenLinesUC[off] !=
6863 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6864 || (ScreenLinesUC[off] != 0
6865 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866#endif
6867 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006868 || exmode_active;
6869
6870 if (need_redraw
6871#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6872 || force_redraw_this
6873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 )
6875 {
6876#if defined(FEAT_GUI) || defined(UNIX)
6877 /* The bold trick makes a single row of pixels appear in the next
6878 * character. When a bold character is removed, the next
6879 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006880 * and for some xterms. */
6881 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882# ifdef FEAT_GUI
6883 gui.in_use
6884# endif
6885# if defined(FEAT_GUI) && defined(UNIX)
6886 ||
6887# endif
6888# ifdef UNIX
6889 term_is_xterm
6890# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006891 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006893 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006895 if (n > HL_ALL)
6896 n = syn_attr2attr(n);
6897 if (n & HL_BOLD)
6898 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900#endif
6901#ifdef FEAT_MBYTE
6902 /* When at the end of the text and overwriting a two-cell
6903 * character with a one-cell character, need to clear the next
6904 * cell. Also when overwriting the left halve of a two-cell char
6905 * with the right halve of a two-cell char. Do this only once
6906 * (mb_off2cells() may return 2 on the right halve). */
6907 if (clear_next_cell)
6908 clear_next_cell = FALSE;
6909 else if (has_mbyte
6910 && (len < 0 ? ptr[mbyte_blen] == NUL
6911 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006912 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006914 && (*mb_off2cells)(off, max_off) == 1
6915 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 clear_next_cell = TRUE;
6917
6918 /* Make sure we never leave a second byte of a double-byte behind,
6919 * it confuses mb_off2cells(). */
6920 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006921 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006923 && (*mb_off2cells)(off, max_off) == 1
6924 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 ScreenLines[off + mbyte_blen] = 0;
6926#endif
6927 ScreenLines[off] = c;
6928 ScreenAttrs[off] = attr;
6929#ifdef FEAT_MBYTE
6930 if (enc_utf8)
6931 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006932 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 ScreenLinesUC[off] = 0;
6934 else
6935 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006936 int i;
6937
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006939 for (i = 0; i < Screen_mco; ++i)
6940 {
6941 ScreenLinesC[i][off] = u8cc[i];
6942 if (u8cc[i] == 0)
6943 break;
6944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 }
6946 if (mbyte_cells == 2)
6947 {
6948 ScreenLines[off + 1] = 0;
6949 ScreenAttrs[off + 1] = attr;
6950 }
6951 screen_char(off, row, col);
6952 }
6953 else if (mbyte_cells == 2)
6954 {
6955 ScreenLines[off + 1] = ptr[1];
6956 ScreenAttrs[off + 1] = attr;
6957 screen_char_2(off, row, col);
6958 }
6959 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6960 {
6961 ScreenLines2[off] = ptr[1];
6962 screen_char(off, row, col);
6963 }
6964 else
6965#endif
6966 screen_char(off, row, col);
6967 }
6968#ifdef FEAT_MBYTE
6969 if (has_mbyte)
6970 {
6971 off += mbyte_cells;
6972 col += mbyte_cells;
6973 ptr += mbyte_blen;
6974 if (clear_next_cell)
6975 ptr = (char_u *)" ";
6976 }
6977 else
6978#endif
6979 {
6980 ++off;
6981 ++col;
6982 ++ptr;
6983 }
6984 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006985
6986#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6987 /* If we detected the next character needs to be redrawn, but the text
6988 * doesn't extend up to there, update the character here. */
6989 if (force_redraw_next && col < screen_Columns)
6990 {
6991# ifdef FEAT_MBYTE
6992 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6993 screen_char_2(off, row, col);
6994 else
6995# endif
6996 screen_char(off, row, col);
6997 }
6998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999}
7000
7001#ifdef FEAT_SEARCH_EXTRA
7002/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007003 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 */
7005 static void
7006start_search_hl()
7007{
7008 if (p_hls && !no_hlsearch)
7009 {
7010 last_pat_prog(&search_hl.rm);
7011 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007012# ifdef FEAT_RELTIME
7013 /* Set the time limit to 'redrawtime'. */
7014 profile_setlimit(p_rdt, &search_hl.tm);
7015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017}
7018
7019/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007020 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 */
7022 static void
7023end_search_hl()
7024{
7025 if (search_hl.rm.regprog != NULL)
7026 {
7027 vim_free(search_hl.rm.regprog);
7028 search_hl.rm.regprog = NULL;
7029 }
7030}
7031
7032/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007033 * Init for calling prepare_search_hl().
7034 */
7035 static void
7036init_search_hl(wp)
7037 win_T *wp;
7038{
7039 matchitem_T *cur;
7040
7041 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7042 * match */
7043 cur = wp->w_match_head;
7044 while (cur != NULL)
7045 {
7046 cur->hl.rm = cur->match;
7047 if (cur->hlg_id == 0)
7048 cur->hl.attr = 0;
7049 else
7050 cur->hl.attr = syn_id2attr(cur->hlg_id);
7051 cur->hl.buf = wp->w_buffer;
7052 cur->hl.lnum = 0;
7053 cur->hl.first_lnum = 0;
7054# ifdef FEAT_RELTIME
7055 /* Set the time limit to 'redrawtime'. */
7056 profile_setlimit(p_rdt, &(cur->hl.tm));
7057# endif
7058 cur = cur->next;
7059 }
7060 search_hl.buf = wp->w_buffer;
7061 search_hl.lnum = 0;
7062 search_hl.first_lnum = 0;
7063 /* time limit is set at the toplevel, for all windows */
7064}
7065
7066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 * Advance to the match in window "wp" line "lnum" or past it.
7068 */
7069 static void
7070prepare_search_hl(wp, lnum)
7071 win_T *wp;
7072 linenr_T lnum;
7073{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007074 matchitem_T *cur; /* points to the match list */
7075 match_T *shl; /* points to search_hl or a match */
7076 int shl_flag; /* flag to indicate whether search_hl
7077 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 int n;
7079
7080 /*
7081 * When using a multi-line pattern, start searching at the top
7082 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007083 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007085 cur = wp->w_match_head;
7086 shl_flag = FALSE;
7087 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007089 if (shl_flag == FALSE)
7090 {
7091 shl = &search_hl;
7092 shl_flag = TRUE;
7093 }
7094 else
7095 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 if (shl->rm.regprog != NULL
7097 && shl->lnum == 0
7098 && re_multiline(shl->rm.regprog))
7099 {
7100 if (shl->first_lnum == 0)
7101 {
7102# ifdef FEAT_FOLDING
7103 for (shl->first_lnum = lnum;
7104 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7105 if (hasFoldingWin(wp, shl->first_lnum - 1,
7106 NULL, NULL, TRUE, NULL))
7107 break;
7108# else
7109 shl->first_lnum = wp->w_topline;
7110# endif
7111 }
7112 n = 0;
7113 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7114 {
7115 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7116 if (shl->lnum != 0)
7117 {
7118 shl->first_lnum = shl->lnum
7119 + shl->rm.endpos[0].lnum
7120 - shl->rm.startpos[0].lnum;
7121 n = shl->rm.endpos[0].col;
7122 }
7123 else
7124 {
7125 ++shl->first_lnum;
7126 n = 0;
7127 }
7128 }
7129 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007130 if (shl != &search_hl && cur != NULL)
7131 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 }
7133}
7134
7135/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007136 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 * Uses shl->buf.
7138 * Sets shl->lnum and shl->rm contents.
7139 * Note: Assumes a previous match is always before "lnum", unless
7140 * shl->lnum is zero.
7141 * Careful: Any pointers for buffer lines will become invalid.
7142 */
7143 static void
7144next_search_hl(win, shl, lnum, mincol)
7145 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007146 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 linenr_T lnum;
7148 colnr_T mincol; /* minimal column for a match */
7149{
7150 linenr_T l;
7151 colnr_T matchcol;
7152 long nmatched;
7153
7154 if (shl->lnum != 0)
7155 {
7156 /* Check for three situations:
7157 * 1. If the "lnum" is below a previous match, start a new search.
7158 * 2. If the previous match includes "mincol", use it.
7159 * 3. Continue after the previous match.
7160 */
7161 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7162 if (lnum > l)
7163 shl->lnum = 0;
7164 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7165 return;
7166 }
7167
7168 /*
7169 * Repeat searching for a match until one is found that includes "mincol"
7170 * or none is found in this line.
7171 */
7172 called_emsg = FALSE;
7173 for (;;)
7174 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007175#ifdef FEAT_RELTIME
7176 /* Stop searching after passing the time limit. */
7177 if (profile_passed_limit(&(shl->tm)))
7178 {
7179 shl->lnum = 0; /* no match found in time */
7180 break;
7181 }
7182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 /* Three situations:
7184 * 1. No useful previous match: search from start of line.
7185 * 2. Not Vi compatible or empty match: continue at next character.
7186 * Break the loop if this is beyond the end of the line.
7187 * 3. Vi compatible searching: continue at end of previous match.
7188 */
7189 if (shl->lnum == 0)
7190 matchcol = 0;
7191 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7192 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007193 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007195 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007196
7197 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007198 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007199 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007201 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 shl->lnum = 0;
7203 break;
7204 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007205#ifdef FEAT_MBYTE
7206 if (has_mbyte)
7207 matchcol += mb_ptr2len(ml);
7208 else
7209#endif
7210 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 }
7212 else
7213 matchcol = shl->rm.endpos[0].col;
7214
7215 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007216 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7217#ifdef FEAT_RELTIME
7218 &(shl->tm)
7219#else
7220 NULL
7221#endif
7222 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007223 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {
7225 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007226 if (shl == &search_hl)
7227 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007228 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007229 vim_free(shl->rm.regprog);
7230 no_hlsearch = TRUE;
7231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007233 shl->lnum = 0;
7234 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 break;
7236 }
7237 if (nmatched == 0)
7238 {
7239 shl->lnum = 0; /* no match found */
7240 break;
7241 }
7242 if (shl->rm.startpos[0].lnum > 0
7243 || shl->rm.startpos[0].col >= mincol
7244 || nmatched > 1
7245 || shl->rm.endpos[0].col > mincol)
7246 {
7247 shl->lnum += shl->rm.startpos[0].lnum;
7248 break; /* useful match found */
7249 }
7250 }
7251}
7252#endif
7253
7254 static void
7255screen_start_highlight(attr)
7256 int attr;
7257{
7258 attrentry_T *aep = NULL;
7259
7260 screen_attr = attr;
7261 if (full_screen
7262#ifdef WIN3264
7263 && termcap_active
7264#endif
7265 )
7266 {
7267#ifdef FEAT_GUI
7268 if (gui.in_use)
7269 {
7270 char buf[20];
7271
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007272 /* The GUI handles this internally. */
7273 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 OUT_STR(buf);
7275 }
7276 else
7277#endif
7278 {
7279 if (attr > HL_ALL) /* special HL attr. */
7280 {
7281 if (t_colors > 1)
7282 aep = syn_cterm_attr2entry(attr);
7283 else
7284 aep = syn_term_attr2entry(attr);
7285 if (aep == NULL) /* did ":syntax clear" */
7286 attr = 0;
7287 else
7288 attr = aep->ae_attr;
7289 }
7290 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7291 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007292 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7293 && cterm_normal_fg_bold)
7294 /* If the Normal FG color has BOLD attribute and the new HL
7295 * has a FG color defined, clear BOLD. */
7296 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7298 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007299 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7300 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 out_str(T_US);
7302 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7303 out_str(T_CZH);
7304 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7305 out_str(T_MR);
7306
7307 /*
7308 * Output the color or start string after bold etc., in case the
7309 * bold etc. override the color setting.
7310 */
7311 if (aep != NULL)
7312 {
7313 if (t_colors > 1)
7314 {
7315 if (aep->ae_u.cterm.fg_color)
7316 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7317 if (aep->ae_u.cterm.bg_color)
7318 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7319 }
7320 else
7321 {
7322 if (aep->ae_u.term.start != NULL)
7323 out_str(aep->ae_u.term.start);
7324 }
7325 }
7326 }
7327 }
7328}
7329
7330 void
7331screen_stop_highlight()
7332{
7333 int do_ME = FALSE; /* output T_ME code */
7334
7335 if (screen_attr != 0
7336#ifdef WIN3264
7337 && termcap_active
7338#endif
7339 )
7340 {
7341#ifdef FEAT_GUI
7342 if (gui.in_use)
7343 {
7344 char buf[20];
7345
7346 /* use internal GUI code */
7347 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7348 OUT_STR(buf);
7349 }
7350 else
7351#endif
7352 {
7353 if (screen_attr > HL_ALL) /* special HL attr. */
7354 {
7355 attrentry_T *aep;
7356
7357 if (t_colors > 1)
7358 {
7359 /*
7360 * Assume that t_me restores the original colors!
7361 */
7362 aep = syn_cterm_attr2entry(screen_attr);
7363 if (aep != NULL && (aep->ae_u.cterm.fg_color
7364 || aep->ae_u.cterm.bg_color))
7365 do_ME = TRUE;
7366 }
7367 else
7368 {
7369 aep = syn_term_attr2entry(screen_attr);
7370 if (aep != NULL && aep->ae_u.term.stop != NULL)
7371 {
7372 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7373 do_ME = TRUE;
7374 else
7375 out_str(aep->ae_u.term.stop);
7376 }
7377 }
7378 if (aep == NULL) /* did ":syntax clear" */
7379 screen_attr = 0;
7380 else
7381 screen_attr = aep->ae_attr;
7382 }
7383
7384 /*
7385 * Often all ending-codes are equal to T_ME. Avoid outputting the
7386 * same sequence several times.
7387 */
7388 if (screen_attr & HL_STANDOUT)
7389 {
7390 if (STRCMP(T_SE, T_ME) == 0)
7391 do_ME = TRUE;
7392 else
7393 out_str(T_SE);
7394 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007395 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 {
7397 if (STRCMP(T_UE, T_ME) == 0)
7398 do_ME = TRUE;
7399 else
7400 out_str(T_UE);
7401 }
7402 if (screen_attr & HL_ITALIC)
7403 {
7404 if (STRCMP(T_CZR, T_ME) == 0)
7405 do_ME = TRUE;
7406 else
7407 out_str(T_CZR);
7408 }
7409 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7410 out_str(T_ME);
7411
7412 if (t_colors > 1)
7413 {
7414 /* set Normal cterm colors */
7415 if (cterm_normal_fg_color != 0)
7416 term_fg_color(cterm_normal_fg_color - 1);
7417 if (cterm_normal_bg_color != 0)
7418 term_bg_color(cterm_normal_bg_color - 1);
7419 if (cterm_normal_fg_bold)
7420 out_str(T_MD);
7421 }
7422 }
7423 }
7424 screen_attr = 0;
7425}
7426
7427/*
7428 * Reset the colors for a cterm. Used when leaving Vim.
7429 * The machine specific code may override this again.
7430 */
7431 void
7432reset_cterm_colors()
7433{
7434 if (t_colors > 1)
7435 {
7436 /* set Normal cterm colors */
7437 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7438 {
7439 out_str(T_OP);
7440 screen_attr = -1;
7441 }
7442 if (cterm_normal_fg_bold)
7443 {
7444 out_str(T_ME);
7445 screen_attr = -1;
7446 }
7447 }
7448}
7449
7450/*
7451 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7452 * using the attributes from ScreenAttrs["off"].
7453 */
7454 static void
7455screen_char(off, row, col)
7456 unsigned off;
7457 int row;
7458 int col;
7459{
7460 int attr;
7461
7462 /* Check for illegal values, just in case (could happen just after
7463 * resizing). */
7464 if (row >= screen_Rows || col >= screen_Columns)
7465 return;
7466
7467 /* Outputting the last character on the screen may scrollup the screen.
7468 * Don't to it! Mark the character invalid (update it when scrolled up) */
7469 if (row == screen_Rows - 1 && col == screen_Columns - 1
7470#ifdef FEAT_RIGHTLEFT
7471 /* account for first command-line character in rightleft mode */
7472 && !cmdmsg_rl
7473#endif
7474 )
7475 {
7476 ScreenAttrs[off] = (sattr_T)-1;
7477 return;
7478 }
7479
7480 /*
7481 * Stop highlighting first, so it's easier to move the cursor.
7482 */
7483#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7484 if (screen_char_attr != 0)
7485 attr = screen_char_attr;
7486 else
7487#endif
7488 attr = ScreenAttrs[off];
7489 if (screen_attr != attr)
7490 screen_stop_highlight();
7491
7492 windgoto(row, col);
7493
7494 if (screen_attr != attr)
7495 screen_start_highlight(attr);
7496
7497#ifdef FEAT_MBYTE
7498 if (enc_utf8 && ScreenLinesUC[off] != 0)
7499 {
7500 char_u buf[MB_MAXBYTES + 1];
7501
7502 /* Convert UTF-8 character to bytes and write it. */
7503
7504 buf[utfc_char2bytes(off, buf)] = NUL;
7505
7506 out_str(buf);
7507 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7508 ++screen_cur_col;
7509 }
7510 else
7511#endif
7512 {
7513#ifdef FEAT_MBYTE
7514 out_flush_check();
7515#endif
7516 out_char(ScreenLines[off]);
7517#ifdef FEAT_MBYTE
7518 /* double-byte character in single-width cell */
7519 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7520 out_char(ScreenLines2[off]);
7521#endif
7522 }
7523
7524 screen_cur_col++;
7525}
7526
7527#ifdef FEAT_MBYTE
7528
7529/*
7530 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7531 * on the screen at position 'row' and 'col'.
7532 * The attributes of the first byte is used for all. This is required to
7533 * output the two bytes of a double-byte character with nothing in between.
7534 */
7535 static void
7536screen_char_2(off, row, col)
7537 unsigned off;
7538 int row;
7539 int col;
7540{
7541 /* Check for illegal values (could be wrong when screen was resized). */
7542 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7543 return;
7544
7545 /* Outputting the last character on the screen may scrollup the screen.
7546 * Don't to it! Mark the character invalid (update it when scrolled up) */
7547 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7548 {
7549 ScreenAttrs[off] = (sattr_T)-1;
7550 return;
7551 }
7552
7553 /* Output the first byte normally (positions the cursor), then write the
7554 * second byte directly. */
7555 screen_char(off, row, col);
7556 out_char(ScreenLines[off + 1]);
7557 ++screen_cur_col;
7558}
7559#endif
7560
7561#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7562/*
7563 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7564 * This uses the contents of ScreenLines[] and doesn't change it.
7565 */
7566 void
7567screen_draw_rectangle(row, col, height, width, invert)
7568 int row;
7569 int col;
7570 int height;
7571 int width;
7572 int invert;
7573{
7574 int r, c;
7575 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007576#ifdef FEAT_MBYTE
7577 int max_off;
7578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007580 /* Can't use ScreenLines unless initialized */
7581 if (ScreenLines == NULL)
7582 return;
7583
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 if (invert)
7585 screen_char_attr = HL_INVERSE;
7586 for (r = row; r < row + height; ++r)
7587 {
7588 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007589#ifdef FEAT_MBYTE
7590 max_off = off + screen_Columns;
7591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 for (c = col; c < col + width; ++c)
7593 {
7594#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007595 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {
7597 screen_char_2(off + c, r, c);
7598 ++c;
7599 }
7600 else
7601#endif
7602 {
7603 screen_char(off + c, r, c);
7604#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007605 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 ++c;
7607#endif
7608 }
7609 }
7610 }
7611 screen_char_attr = 0;
7612}
7613#endif
7614
7615#ifdef FEAT_VERTSPLIT
7616/*
7617 * Redraw the characters for a vertically split window.
7618 */
7619 static void
7620redraw_block(row, end, wp)
7621 int row;
7622 int end;
7623 win_T *wp;
7624{
7625 int col;
7626 int width;
7627
7628# ifdef FEAT_CLIPBOARD
7629 clip_may_clear_selection(row, end - 1);
7630# endif
7631
7632 if (wp == NULL)
7633 {
7634 col = 0;
7635 width = Columns;
7636 }
7637 else
7638 {
7639 col = wp->w_wincol;
7640 width = wp->w_width;
7641 }
7642 screen_draw_rectangle(row, col, end - row, width, FALSE);
7643}
7644#endif
7645
7646/*
7647 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7648 * with character 'c1' in first column followed by 'c2' in the other columns.
7649 * Use attributes 'attr'.
7650 */
7651 void
7652screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7653 int start_row, end_row;
7654 int start_col, end_col;
7655 int c1, c2;
7656 int attr;
7657{
7658 int row;
7659 int col;
7660 int off;
7661 int end_off;
7662 int did_delete;
7663 int c;
7664 int norm_term;
7665#if defined(FEAT_GUI) || defined(UNIX)
7666 int force_next = FALSE;
7667#endif
7668
7669 if (end_row > screen_Rows) /* safety check */
7670 end_row = screen_Rows;
7671 if (end_col > screen_Columns) /* safety check */
7672 end_col = screen_Columns;
7673 if (ScreenLines == NULL
7674 || start_row >= end_row
7675 || start_col >= end_col) /* nothing to do */
7676 return;
7677
7678 /* it's a "normal" terminal when not in a GUI or cterm */
7679 norm_term = (
7680#ifdef FEAT_GUI
7681 !gui.in_use &&
7682#endif
7683 t_colors <= 1);
7684 for (row = start_row; row < end_row; ++row)
7685 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007686#ifdef FEAT_MBYTE
7687 if (has_mbyte
7688# ifdef FEAT_GUI
7689 && !gui.in_use
7690# endif
7691 )
7692 {
7693 /* When drawing over the right halve of a double-wide char clear
7694 * out the left halve. When drawing over the left halve of a
7695 * double wide-char clear out the right halve. Only needed in a
7696 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007697 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007698 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007699 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007700 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007701 }
7702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 /*
7704 * Try to use delete-line termcap code, when no attributes or in a
7705 * "normal" terminal, where a bold/italic space is just a
7706 * space.
7707 */
7708 did_delete = FALSE;
7709 if (c2 == ' '
7710 && end_col == Columns
7711 && can_clear(T_CE)
7712 && (attr == 0
7713 || (norm_term
7714 && attr <= HL_ALL
7715 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7716 {
7717 /*
7718 * check if we really need to clear something
7719 */
7720 col = start_col;
7721 if (c1 != ' ') /* don't clear first char */
7722 ++col;
7723
7724 off = LineOffset[row] + col;
7725 end_off = LineOffset[row] + end_col;
7726
7727 /* skip blanks (used often, keep it fast!) */
7728#ifdef FEAT_MBYTE
7729 if (enc_utf8)
7730 while (off < end_off && ScreenLines[off] == ' '
7731 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7732 ++off;
7733 else
7734#endif
7735 while (off < end_off && ScreenLines[off] == ' '
7736 && ScreenAttrs[off] == 0)
7737 ++off;
7738 if (off < end_off) /* something to be cleared */
7739 {
7740 col = off - LineOffset[row];
7741 screen_stop_highlight();
7742 term_windgoto(row, col);/* clear rest of this screen line */
7743 out_str(T_CE);
7744 screen_start(); /* don't know where cursor is now */
7745 col = end_col - col;
7746 while (col--) /* clear chars in ScreenLines */
7747 {
7748 ScreenLines[off] = ' ';
7749#ifdef FEAT_MBYTE
7750 if (enc_utf8)
7751 ScreenLinesUC[off] = 0;
7752#endif
7753 ScreenAttrs[off] = 0;
7754 ++off;
7755 }
7756 }
7757 did_delete = TRUE; /* the chars are cleared now */
7758 }
7759
7760 off = LineOffset[row] + start_col;
7761 c = c1;
7762 for (col = start_col; col < end_col; ++col)
7763 {
7764 if (ScreenLines[off] != c
7765#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007766 || (enc_utf8 && (int)ScreenLinesUC[off]
7767 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768#endif
7769 || ScreenAttrs[off] != attr
7770#if defined(FEAT_GUI) || defined(UNIX)
7771 || force_next
7772#endif
7773 )
7774 {
7775#if defined(FEAT_GUI) || defined(UNIX)
7776 /* The bold trick may make a single row of pixels appear in
7777 * the next character. When a bold character is removed, the
7778 * next character should be redrawn too. This happens for our
7779 * own GUI and for some xterms. */
7780 if (
7781# ifdef FEAT_GUI
7782 gui.in_use
7783# endif
7784# if defined(FEAT_GUI) && defined(UNIX)
7785 ||
7786# endif
7787# ifdef UNIX
7788 term_is_xterm
7789# endif
7790 )
7791 {
7792 if (ScreenLines[off] != ' '
7793 && (ScreenAttrs[off] > HL_ALL
7794 || ScreenAttrs[off] & HL_BOLD))
7795 force_next = TRUE;
7796 else
7797 force_next = FALSE;
7798 }
7799#endif
7800 ScreenLines[off] = c;
7801#ifdef FEAT_MBYTE
7802 if (enc_utf8)
7803 {
7804 if (c >= 0x80)
7805 {
7806 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007807 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809 else
7810 ScreenLinesUC[off] = 0;
7811 }
7812#endif
7813 ScreenAttrs[off] = attr;
7814 if (!did_delete || c != ' ')
7815 screen_char(off, row, col);
7816 }
7817 ++off;
7818 if (col == start_col)
7819 {
7820 if (did_delete)
7821 break;
7822 c = c2;
7823 }
7824 }
7825 if (end_col == Columns)
7826 LineWraps[row] = FALSE;
7827 if (row == Rows - 1) /* overwritten the command line */
7828 {
7829 redraw_cmdline = TRUE;
7830 if (c1 == ' ' && c2 == ' ')
7831 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007832 if (start_col == 0)
7833 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 }
7835 }
7836}
7837
7838/*
7839 * Check if there should be a delay. Used before clearing or redrawing the
7840 * screen or the command line.
7841 */
7842 void
7843check_for_delay(check_msg_scroll)
7844 int check_msg_scroll;
7845{
7846 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7847 && !did_wait_return
7848 && emsg_silent == 0)
7849 {
7850 out_flush();
7851 ui_delay(1000L, TRUE);
7852 emsg_on_display = FALSE;
7853 if (check_msg_scroll)
7854 msg_scroll = FALSE;
7855 }
7856}
7857
7858/*
7859 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007860 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 * Returns TRUE if there is a valid screen to write to.
7862 * Returns FALSE when starting up and screen not initialized yet.
7863 */
7864 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007865screen_valid(doclear)
7866 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007868 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 return (ScreenLines != NULL);
7870}
7871
7872/*
7873 * Resize the shell to Rows and Columns.
7874 * Allocate ScreenLines[] and associated items.
7875 *
7876 * There may be some time between setting Rows and Columns and (re)allocating
7877 * ScreenLines[]. This happens when starting up and when (manually) changing
7878 * the shell size. Always use screen_Rows and screen_Columns to access items
7879 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7880 * final size of the shell is needed.
7881 */
7882 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007883screenalloc(doclear)
7884 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
7886 int new_row, old_row;
7887#ifdef FEAT_GUI
7888 int old_Rows;
7889#endif
7890 win_T *wp;
7891 int outofmem = FALSE;
7892 int len;
7893 schar_T *new_ScreenLines;
7894#ifdef FEAT_MBYTE
7895 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007896 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007898 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899#endif
7900 sattr_T *new_ScreenAttrs;
7901 unsigned *new_LineOffset;
7902 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007903#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007904 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007905 tabpage_T *tp;
7906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007908 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007909#ifdef FEAT_AUTOCMD
7910 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007912retry:
7913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 /*
7915 * Allocation of the screen buffers is done only when the size changes and
7916 * when Rows and Columns have been set and we have started doing full
7917 * screen stuff.
7918 */
7919 if ((ScreenLines != NULL
7920 && Rows == screen_Rows
7921 && Columns == screen_Columns
7922#ifdef FEAT_MBYTE
7923 && enc_utf8 == (ScreenLinesUC != NULL)
7924 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007925 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926#endif
7927 )
7928 || Rows == 0
7929 || Columns == 0
7930 || (!full_screen && ScreenLines == NULL))
7931 return;
7932
7933 /*
7934 * It's possible that we produce an out-of-memory message below, which
7935 * will cause this function to be called again. To break the loop, just
7936 * return here.
7937 */
7938 if (entered)
7939 return;
7940 entered = TRUE;
7941
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007942 /*
7943 * Note that the window sizes are updated before reallocating the arrays,
7944 * thus we must not redraw here!
7945 */
7946 ++RedrawingDisabled;
7947
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 win_new_shellsize(); /* fit the windows in the new sized shell */
7949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 comp_col(); /* recompute columns for shown command and ruler */
7951
7952 /*
7953 * We're changing the size of the screen.
7954 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7955 * - Move lines from the old arrays into the new arrays, clear extra
7956 * lines (unless the screen is going to be cleared).
7957 * - Free the old arrays.
7958 *
7959 * If anything fails, make ScreenLines NULL, so we don't do anything!
7960 * Continuing with the old ScreenLines may result in a crash, because the
7961 * size is wrong.
7962 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007963 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007965#ifdef FEAT_AUTOCMD
7966 if (aucmd_win != NULL)
7967 win_free_lsize(aucmd_win);
7968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969
7970 new_ScreenLines = (schar_T *)lalloc((long_u)(
7971 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7972#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007973 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (enc_utf8)
7975 {
7976 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7977 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007978 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007979 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7981 }
7982 if (enc_dbcs == DBCS_JPNU)
7983 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7984 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7985#endif
7986 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7987 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7988 new_LineOffset = (unsigned *)lalloc((long_u)(
7989 Rows * sizeof(unsigned)), FALSE);
7990 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007991#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007992 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007995 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 {
7997 if (win_alloc_lines(wp) == FAIL)
7998 {
7999 outofmem = TRUE;
8000#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008001 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002#endif
8003 }
8004 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008005#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008006 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8007 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008008 outofmem = TRUE;
8009#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008010#ifdef FEAT_WINDOWS
8011give_up:
8012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008014#ifdef FEAT_MBYTE
8015 for (i = 0; i < p_mco; ++i)
8016 if (new_ScreenLinesC[i] == NULL)
8017 break;
8018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 if (new_ScreenLines == NULL
8020#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008021 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8023#endif
8024 || new_ScreenAttrs == NULL
8025 || new_LineOffset == NULL
8026 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008027#ifdef FEAT_WINDOWS
8028 || new_TabPageIdxs == NULL
8029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 || outofmem)
8031 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008032 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008033 {
8034 /* guess the size */
8035 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8036
8037 /* Remember we did this to avoid getting outofmem messages over
8038 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008039 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 vim_free(new_ScreenLines);
8042 new_ScreenLines = NULL;
8043#ifdef FEAT_MBYTE
8044 vim_free(new_ScreenLinesUC);
8045 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008046 for (i = 0; i < p_mco; ++i)
8047 {
8048 vim_free(new_ScreenLinesC[i]);
8049 new_ScreenLinesC[i] = NULL;
8050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 vim_free(new_ScreenLines2);
8052 new_ScreenLines2 = NULL;
8053#endif
8054 vim_free(new_ScreenAttrs);
8055 new_ScreenAttrs = NULL;
8056 vim_free(new_LineOffset);
8057 new_LineOffset = NULL;
8058 vim_free(new_LineWraps);
8059 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008060#ifdef FEAT_WINDOWS
8061 vim_free(new_TabPageIdxs);
8062 new_TabPageIdxs = NULL;
8063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 }
8065 else
8066 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008067 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008068
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 for (new_row = 0; new_row < Rows; ++new_row)
8070 {
8071 new_LineOffset[new_row] = new_row * Columns;
8072 new_LineWraps[new_row] = FALSE;
8073
8074 /*
8075 * If the screen is not going to be cleared, copy as much as
8076 * possible from the old screen to the new one and clear the rest
8077 * (used when resizing the window at the "--more--" prompt or when
8078 * executing an external command, for the GUI).
8079 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008080 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
8082 (void)vim_memset(new_ScreenLines + new_row * Columns,
8083 ' ', (size_t)Columns * sizeof(schar_T));
8084#ifdef FEAT_MBYTE
8085 if (enc_utf8)
8086 {
8087 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8088 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008089 for (i = 0; i < p_mco; ++i)
8090 (void)vim_memset(new_ScreenLinesC[i]
8091 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 0, (size_t)Columns * sizeof(u8char_T));
8093 }
8094 if (enc_dbcs == DBCS_JPNU)
8095 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8096 0, (size_t)Columns * sizeof(schar_T));
8097#endif
8098 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8099 0, (size_t)Columns * sizeof(sattr_T));
8100 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008101 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {
8103 if (screen_Columns < Columns)
8104 len = screen_Columns;
8105 else
8106 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008107#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008108 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008109 * may be invalid now. Also when p_mco changes. */
8110 if (!(enc_utf8 && ScreenLinesUC == NULL)
8111 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008112#endif
8113 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8114 ScreenLines + LineOffset[old_row],
8115 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008117 if (enc_utf8 && ScreenLinesUC != NULL
8118 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {
8120 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8121 ScreenLinesUC + LineOffset[old_row],
8122 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008123 for (i = 0; i < p_mco; ++i)
8124 mch_memmove(new_ScreenLinesC[i]
8125 + new_LineOffset[new_row],
8126 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 (size_t)len * sizeof(u8char_T));
8128 }
8129 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8130 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8131 ScreenLines2 + LineOffset[old_row],
8132 (size_t)len * sizeof(schar_T));
8133#endif
8134 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8135 ScreenAttrs + LineOffset[old_row],
8136 (size_t)len * sizeof(sattr_T));
8137 }
8138 }
8139 }
8140 /* Use the last line of the screen for the current line. */
8141 current_ScreenLine = new_ScreenLines + Rows * Columns;
8142 }
8143
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008144 free_screenlines();
8145
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 ScreenLines = new_ScreenLines;
8147#ifdef FEAT_MBYTE
8148 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008149 for (i = 0; i < p_mco; ++i)
8150 ScreenLinesC[i] = new_ScreenLinesC[i];
8151 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 ScreenLines2 = new_ScreenLines2;
8153#endif
8154 ScreenAttrs = new_ScreenAttrs;
8155 LineOffset = new_LineOffset;
8156 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008157#ifdef FEAT_WINDOWS
8158 TabPageIdxs = new_TabPageIdxs;
8159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160
8161 /* It's important that screen_Rows and screen_Columns reflect the actual
8162 * size of ScreenLines[]. Set them before calling anything. */
8163#ifdef FEAT_GUI
8164 old_Rows = screen_Rows;
8165#endif
8166 screen_Rows = Rows;
8167 screen_Columns = Columns;
8168
8169 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008170 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 screenclear2();
8172
8173#ifdef FEAT_GUI
8174 else if (gui.in_use
8175 && !gui.starting
8176 && ScreenLines != NULL
8177 && old_Rows != Rows)
8178 {
8179 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8180 /*
8181 * Adjust the position of the cursor, for when executing an external
8182 * command.
8183 */
8184 if (msg_row >= Rows) /* Rows got smaller */
8185 msg_row = Rows - 1; /* put cursor at last row */
8186 else if (Rows > old_Rows) /* Rows got bigger */
8187 msg_row += Rows - old_Rows; /* put cursor in same place */
8188 if (msg_col >= Columns) /* Columns got smaller */
8189 msg_col = Columns - 1; /* put cursor at last column */
8190 }
8191#endif
8192
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008194 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008195
8196#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008197 /*
8198 * Do not apply autocommands more than 3 times to avoid an endless loop
8199 * in case applying autocommands always changes Rows or Columns.
8200 */
8201 if (starting == 0 && ++retry_count <= 3)
8202 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008203 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008204 /* In rare cases, autocommands may have altered Rows or Columns,
8205 * jump back to check if we need to allocate the screen again. */
8206 goto retry;
8207 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209}
8210
8211 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008212free_screenlines()
8213{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008214#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008215 int i;
8216
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008217 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008218 for (i = 0; i < Screen_mco; ++i)
8219 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008220 vim_free(ScreenLines2);
8221#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008222 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008223 vim_free(ScreenAttrs);
8224 vim_free(LineOffset);
8225 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008226#ifdef FEAT_WINDOWS
8227 vim_free(TabPageIdxs);
8228#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008229}
8230
8231 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232screenclear()
8233{
8234 check_for_delay(FALSE);
8235 screenalloc(FALSE); /* allocate screen buffers if size changed */
8236 screenclear2(); /* clear the screen */
8237}
8238
8239 static void
8240screenclear2()
8241{
8242 int i;
8243
8244 if (starting == NO_SCREEN || ScreenLines == NULL
8245#ifdef FEAT_GUI
8246 || (gui.in_use && gui.starting)
8247#endif
8248 )
8249 return;
8250
8251#ifdef FEAT_GUI
8252 if (!gui.in_use)
8253#endif
8254 screen_attr = -1; /* force setting the Normal colors */
8255 screen_stop_highlight(); /* don't want highlighting here */
8256
8257#ifdef FEAT_CLIPBOARD
8258 /* disable selection without redrawing it */
8259 clip_scroll_selection(9999);
8260#endif
8261
8262 /* blank out ScreenLines */
8263 for (i = 0; i < Rows; ++i)
8264 {
8265 lineclear(LineOffset[i], (int)Columns);
8266 LineWraps[i] = FALSE;
8267 }
8268
8269 if (can_clear(T_CL))
8270 {
8271 out_str(T_CL); /* clear the display */
8272 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008273 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 }
8275 else
8276 {
8277 /* can't clear the screen, mark all chars with invalid attributes */
8278 for (i = 0; i < Rows; ++i)
8279 lineinvalid(LineOffset[i], (int)Columns);
8280 clear_cmdline = TRUE;
8281 }
8282
8283 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8284
8285 win_rest_invalid(firstwin);
8286 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008287#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008288 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 if (must_redraw == CLEAR) /* no need to clear again */
8291 must_redraw = NOT_VALID;
8292 compute_cmdrow();
8293 msg_row = cmdline_row; /* put cursor on last line for messages */
8294 msg_col = 0;
8295 screen_start(); /* don't know where cursor is now */
8296 msg_scrolled = 0; /* can't scroll back */
8297 msg_didany = FALSE;
8298 msg_didout = FALSE;
8299}
8300
8301/*
8302 * Clear one line in ScreenLines.
8303 */
8304 static void
8305lineclear(off, width)
8306 unsigned off;
8307 int width;
8308{
8309 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8310#ifdef FEAT_MBYTE
8311 if (enc_utf8)
8312 (void)vim_memset(ScreenLinesUC + off, 0,
8313 (size_t)width * sizeof(u8char_T));
8314#endif
8315 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8316}
8317
8318/*
8319 * Mark one line in ScreenLines invalid by setting the attributes to an
8320 * invalid value.
8321 */
8322 static void
8323lineinvalid(off, width)
8324 unsigned off;
8325 int width;
8326{
8327 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8328}
8329
8330#ifdef FEAT_VERTSPLIT
8331/*
8332 * Copy part of a Screenline for vertically split window "wp".
8333 */
8334 static void
8335linecopy(to, from, wp)
8336 int to;
8337 int from;
8338 win_T *wp;
8339{
8340 unsigned off_to = LineOffset[to] + wp->w_wincol;
8341 unsigned off_from = LineOffset[from] + wp->w_wincol;
8342
8343 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8344 wp->w_width * sizeof(schar_T));
8345# ifdef FEAT_MBYTE
8346 if (enc_utf8)
8347 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008348 int i;
8349
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8351 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008352 for (i = 0; i < p_mco; ++i)
8353 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8354 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 }
8356 if (enc_dbcs == DBCS_JPNU)
8357 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8358 wp->w_width * sizeof(schar_T));
8359# endif
8360 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8361 wp->w_width * sizeof(sattr_T));
8362}
8363#endif
8364
8365/*
8366 * Return TRUE if clearing with term string "p" would work.
8367 * It can't work when the string is empty or it won't set the right background.
8368 */
8369 int
8370can_clear(p)
8371 char_u *p;
8372{
8373 return (*p != NUL && (t_colors <= 1
8374#ifdef FEAT_GUI
8375 || gui.in_use
8376#endif
8377 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8378}
8379
8380/*
8381 * Reset cursor position. Use whenever cursor was moved because of outputting
8382 * something directly to the screen (shell commands) or a terminal control
8383 * code.
8384 */
8385 void
8386screen_start()
8387{
8388 screen_cur_row = screen_cur_col = 9999;
8389}
8390
8391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 * Move the cursor to position "row","col" in the screen.
8393 * This tries to find the most efficient way to move, minimizing the number of
8394 * characters sent to the terminal.
8395 */
8396 void
8397windgoto(row, col)
8398 int row;
8399 int col;
8400{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008401 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 int i;
8403 int plan;
8404 int cost;
8405 int wouldbe_col;
8406 int noinvcurs;
8407 char_u *bs;
8408 int goto_cost;
8409 int attr;
8410
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008411#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8413
8414#define PLAN_LE 1
8415#define PLAN_CR 2
8416#define PLAN_NL 3
8417#define PLAN_WRITE 4
8418 /* Can't use ScreenLines unless initialized */
8419 if (ScreenLines == NULL)
8420 return;
8421
8422 if (col != screen_cur_col || row != screen_cur_row)
8423 {
8424 /* Check for valid position. */
8425 if (row < 0) /* window without text lines? */
8426 row = 0;
8427 if (row >= screen_Rows)
8428 row = screen_Rows - 1;
8429 if (col >= screen_Columns)
8430 col = screen_Columns - 1;
8431
8432 /* check if no cursor movement is allowed in highlight mode */
8433 if (screen_attr && *T_MS == NUL)
8434 noinvcurs = HIGHL_COST;
8435 else
8436 noinvcurs = 0;
8437 goto_cost = GOTO_COST + noinvcurs;
8438
8439 /*
8440 * Plan how to do the positioning:
8441 * 1. Use CR to move it to column 0, same row.
8442 * 2. Use T_LE to move it a few columns to the left.
8443 * 3. Use NL to move a few lines down, column 0.
8444 * 4. Move a few columns to the right with T_ND or by writing chars.
8445 *
8446 * Don't do this if the cursor went beyond the last column, the cursor
8447 * position is unknown then (some terminals wrap, some don't )
8448 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008449 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 * characters to move the cursor to the right.
8451 */
8452 if (row >= screen_cur_row && screen_cur_col < Columns)
8453 {
8454 /*
8455 * If the cursor is in the same row, bigger col, we can use CR
8456 * or T_LE.
8457 */
8458 bs = NULL; /* init for GCC */
8459 attr = screen_attr;
8460 if (row == screen_cur_row && col < screen_cur_col)
8461 {
8462 /* "le" is preferred over "bc", because "bc" is obsolete */
8463 if (*T_LE)
8464 bs = T_LE; /* "cursor left" */
8465 else
8466 bs = T_BC; /* "backspace character (old) */
8467 if (*bs)
8468 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8469 else
8470 cost = 999;
8471 if (col + 1 < cost) /* using CR is less characters */
8472 {
8473 plan = PLAN_CR;
8474 wouldbe_col = 0;
8475 cost = 1; /* CR is just one character */
8476 }
8477 else
8478 {
8479 plan = PLAN_LE;
8480 wouldbe_col = col;
8481 }
8482 if (noinvcurs) /* will stop highlighting */
8483 {
8484 cost += noinvcurs;
8485 attr = 0;
8486 }
8487 }
8488
8489 /*
8490 * If the cursor is above where we want to be, we can use CR LF.
8491 */
8492 else if (row > screen_cur_row)
8493 {
8494 plan = PLAN_NL;
8495 wouldbe_col = 0;
8496 cost = (row - screen_cur_row) * 2; /* CR LF */
8497 if (noinvcurs) /* will stop highlighting */
8498 {
8499 cost += noinvcurs;
8500 attr = 0;
8501 }
8502 }
8503
8504 /*
8505 * If the cursor is in the same row, smaller col, just use write.
8506 */
8507 else
8508 {
8509 plan = PLAN_WRITE;
8510 wouldbe_col = screen_cur_col;
8511 cost = 0;
8512 }
8513
8514 /*
8515 * Check if any characters that need to be written have the
8516 * correct attributes. Also avoid UTF-8 characters.
8517 */
8518 i = col - wouldbe_col;
8519 if (i > 0)
8520 cost += i;
8521 if (cost < goto_cost && i > 0)
8522 {
8523 /*
8524 * Check if the attributes are correct without additionally
8525 * stopping highlighting.
8526 */
8527 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8528 while (i && *p++ == attr)
8529 --i;
8530 if (i != 0)
8531 {
8532 /*
8533 * Try if it works when highlighting is stopped here.
8534 */
8535 if (*--p == 0)
8536 {
8537 cost += noinvcurs;
8538 while (i && *p++ == 0)
8539 --i;
8540 }
8541 if (i != 0)
8542 cost = 999; /* different attributes, don't do it */
8543 }
8544#ifdef FEAT_MBYTE
8545 if (enc_utf8)
8546 {
8547 /* Don't use an UTF-8 char for positioning, it's slow. */
8548 for (i = wouldbe_col; i < col; ++i)
8549 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8550 {
8551 cost = 999;
8552 break;
8553 }
8554 }
8555#endif
8556 }
8557
8558 /*
8559 * We can do it without term_windgoto()!
8560 */
8561 if (cost < goto_cost)
8562 {
8563 if (plan == PLAN_LE)
8564 {
8565 if (noinvcurs)
8566 screen_stop_highlight();
8567 while (screen_cur_col > col)
8568 {
8569 out_str(bs);
8570 --screen_cur_col;
8571 }
8572 }
8573 else if (plan == PLAN_CR)
8574 {
8575 if (noinvcurs)
8576 screen_stop_highlight();
8577 out_char('\r');
8578 screen_cur_col = 0;
8579 }
8580 else if (plan == PLAN_NL)
8581 {
8582 if (noinvcurs)
8583 screen_stop_highlight();
8584 while (screen_cur_row < row)
8585 {
8586 out_char('\n');
8587 ++screen_cur_row;
8588 }
8589 screen_cur_col = 0;
8590 }
8591
8592 i = col - screen_cur_col;
8593 if (i > 0)
8594 {
8595 /*
8596 * Use cursor-right if it's one character only. Avoids
8597 * removing a line of pixels from the last bold char, when
8598 * using the bold trick in the GUI.
8599 */
8600 if (T_ND[0] != NUL && T_ND[1] == NUL)
8601 {
8602 while (i-- > 0)
8603 out_char(*T_ND);
8604 }
8605 else
8606 {
8607 int off;
8608
8609 off = LineOffset[row] + screen_cur_col;
8610 while (i-- > 0)
8611 {
8612 if (ScreenAttrs[off] != screen_attr)
8613 screen_stop_highlight();
8614#ifdef FEAT_MBYTE
8615 out_flush_check();
8616#endif
8617 out_char(ScreenLines[off]);
8618#ifdef FEAT_MBYTE
8619 if (enc_dbcs == DBCS_JPNU
8620 && ScreenLines[off] == 0x8e)
8621 out_char(ScreenLines2[off]);
8622#endif
8623 ++off;
8624 }
8625 }
8626 }
8627 }
8628 }
8629 else
8630 cost = 999;
8631
8632 if (cost >= goto_cost)
8633 {
8634 if (noinvcurs)
8635 screen_stop_highlight();
8636 if (row == screen_cur_row && (col > screen_cur_col) &&
8637 *T_CRI != NUL)
8638 term_cursor_right(col - screen_cur_col);
8639 else
8640 term_windgoto(row, col);
8641 }
8642 screen_cur_row = row;
8643 screen_cur_col = col;
8644 }
8645}
8646
8647/*
8648 * Set cursor to its position in the current window.
8649 */
8650 void
8651setcursor()
8652{
8653 if (redrawing())
8654 {
8655 validate_cursor();
8656 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8657 W_WINCOL(curwin) + (
8658#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008659 /* With 'rightleft' set and the cursor on a double-wide
8660 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8662# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008663 (has_mbyte
8664 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8665 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666# endif
8667 1)) :
8668#endif
8669 curwin->w_wcol));
8670 }
8671}
8672
8673
8674/*
8675 * insert 'line_count' lines at 'row' in window 'wp'
8676 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8677 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8678 * scrolling.
8679 * Returns FAIL if the lines are not inserted, OK for success.
8680 */
8681 int
8682win_ins_lines(wp, row, line_count, invalid, mayclear)
8683 win_T *wp;
8684 int row;
8685 int line_count;
8686 int invalid;
8687 int mayclear;
8688{
8689 int did_delete;
8690 int nextrow;
8691 int lastrow;
8692 int retval;
8693
8694 if (invalid)
8695 wp->w_lines_valid = 0;
8696
8697 if (wp->w_height < 5)
8698 return FAIL;
8699
8700 if (line_count > wp->w_height - row)
8701 line_count = wp->w_height - row;
8702
8703 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8704 if (retval != MAYBE)
8705 return retval;
8706
8707 /*
8708 * If there is a next window or a status line, we first try to delete the
8709 * lines at the bottom to avoid messing what is after the window.
8710 * If this fails and there are following windows, don't do anything to avoid
8711 * messing up those windows, better just redraw.
8712 */
8713 did_delete = FALSE;
8714#ifdef FEAT_WINDOWS
8715 if (wp->w_next != NULL || wp->w_status_height)
8716 {
8717 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8718 line_count, (int)Rows, FALSE, NULL) == OK)
8719 did_delete = TRUE;
8720 else if (wp->w_next)
8721 return FAIL;
8722 }
8723#endif
8724 /*
8725 * if no lines deleted, blank the lines that will end up below the window
8726 */
8727 if (!did_delete)
8728 {
8729#ifdef FEAT_WINDOWS
8730 wp->w_redr_status = TRUE;
8731#endif
8732 redraw_cmdline = TRUE;
8733 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8734 lastrow = nextrow + line_count;
8735 if (lastrow > Rows)
8736 lastrow = Rows;
8737 screen_fill(nextrow - line_count, lastrow - line_count,
8738 W_WINCOL(wp), (int)W_ENDCOL(wp),
8739 ' ', ' ', 0);
8740 }
8741
8742 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8743 == FAIL)
8744 {
8745 /* deletion will have messed up other windows */
8746 if (did_delete)
8747 {
8748#ifdef FEAT_WINDOWS
8749 wp->w_redr_status = TRUE;
8750#endif
8751 win_rest_invalid(W_NEXT(wp));
8752 }
8753 return FAIL;
8754 }
8755
8756 return OK;
8757}
8758
8759/*
8760 * delete "line_count" window lines at "row" in window "wp"
8761 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8762 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8763 * scrolling
8764 * Return OK for success, FAIL if the lines are not deleted.
8765 */
8766 int
8767win_del_lines(wp, row, line_count, invalid, mayclear)
8768 win_T *wp;
8769 int row;
8770 int line_count;
8771 int invalid;
8772 int mayclear;
8773{
8774 int retval;
8775
8776 if (invalid)
8777 wp->w_lines_valid = 0;
8778
8779 if (line_count > wp->w_height - row)
8780 line_count = wp->w_height - row;
8781
8782 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8783 if (retval != MAYBE)
8784 return retval;
8785
8786 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8787 (int)Rows, FALSE, NULL) == FAIL)
8788 return FAIL;
8789
8790#ifdef FEAT_WINDOWS
8791 /*
8792 * If there are windows or status lines below, try to put them at the
8793 * correct place. If we can't do that, they have to be redrawn.
8794 */
8795 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8796 {
8797 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8798 line_count, (int)Rows, NULL) == FAIL)
8799 {
8800 wp->w_redr_status = TRUE;
8801 win_rest_invalid(wp->w_next);
8802 }
8803 }
8804 /*
8805 * If this is the last window and there is no status line, redraw the
8806 * command line later.
8807 */
8808 else
8809#endif
8810 redraw_cmdline = TRUE;
8811 return OK;
8812}
8813
8814/*
8815 * Common code for win_ins_lines() and win_del_lines().
8816 * Returns OK or FAIL when the work has been done.
8817 * Returns MAYBE when not finished yet.
8818 */
8819 static int
8820win_do_lines(wp, row, line_count, mayclear, del)
8821 win_T *wp;
8822 int row;
8823 int line_count;
8824 int mayclear;
8825 int del;
8826{
8827 int retval;
8828
8829 if (!redrawing() || line_count <= 0)
8830 return FAIL;
8831
8832 /* only a few lines left: redraw is faster */
8833 if (mayclear && Rows - line_count < 5
8834#ifdef FEAT_VERTSPLIT
8835 && wp->w_width == Columns
8836#endif
8837 )
8838 {
8839 screenclear(); /* will set wp->w_lines_valid to 0 */
8840 return FAIL;
8841 }
8842
8843 /*
8844 * Delete all remaining lines
8845 */
8846 if (row + line_count >= wp->w_height)
8847 {
8848 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8849 W_WINCOL(wp), (int)W_ENDCOL(wp),
8850 ' ', ' ', 0);
8851 return OK;
8852 }
8853
8854 /*
8855 * when scrolling, the message on the command line should be cleared,
8856 * otherwise it will stay there forever.
8857 */
8858 clear_cmdline = TRUE;
8859
8860 /*
8861 * If the terminal can set a scroll region, use that.
8862 * Always do this in a vertically split window. This will redraw from
8863 * ScreenLines[] when t_CV isn't defined. That's faster than using
8864 * win_line().
8865 * Don't use a scroll region when we are going to redraw the text, writing
8866 * a character in the lower right corner of the scroll region causes a
8867 * scroll-up in the DJGPP version.
8868 */
8869 if (scroll_region
8870#ifdef FEAT_VERTSPLIT
8871 || W_WIDTH(wp) != Columns
8872#endif
8873 )
8874 {
8875#ifdef FEAT_VERTSPLIT
8876 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8877#endif
8878 scroll_region_set(wp, row);
8879 if (del)
8880 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8881 wp->w_height - row, FALSE, wp);
8882 else
8883 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8884 wp->w_height - row, wp);
8885#ifdef FEAT_VERTSPLIT
8886 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8887#endif
8888 scroll_region_reset();
8889 return retval;
8890 }
8891
8892#ifdef FEAT_WINDOWS
8893 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8894 return FAIL;
8895#endif
8896
8897 return MAYBE;
8898}
8899
8900/*
8901 * window 'wp' and everything after it is messed up, mark it for redraw
8902 */
8903 static void
8904win_rest_invalid(wp)
8905 win_T *wp;
8906{
8907#ifdef FEAT_WINDOWS
8908 while (wp != NULL)
8909#else
8910 if (wp != NULL)
8911#endif
8912 {
8913 redraw_win_later(wp, NOT_VALID);
8914#ifdef FEAT_WINDOWS
8915 wp->w_redr_status = TRUE;
8916 wp = wp->w_next;
8917#endif
8918 }
8919 redraw_cmdline = TRUE;
8920}
8921
8922/*
8923 * The rest of the routines in this file perform screen manipulations. The
8924 * given operation is performed physically on the screen. The corresponding
8925 * change is also made to the internal screen image. In this way, the editor
8926 * anticipates the effect of editing changes on the appearance of the screen.
8927 * That way, when we call screenupdate a complete redraw isn't usually
8928 * necessary. Another advantage is that we can keep adding code to anticipate
8929 * screen changes, and in the meantime, everything still works.
8930 */
8931
8932/*
8933 * types for inserting or deleting lines
8934 */
8935#define USE_T_CAL 1
8936#define USE_T_CDL 2
8937#define USE_T_AL 3
8938#define USE_T_CE 4
8939#define USE_T_DL 5
8940#define USE_T_SR 6
8941#define USE_NL 7
8942#define USE_T_CD 8
8943#define USE_REDRAW 9
8944
8945/*
8946 * insert lines on the screen and update ScreenLines[]
8947 * 'end' is the line after the scrolled part. Normally it is Rows.
8948 * When scrolling region used 'off' is the offset from the top for the region.
8949 * 'row' and 'end' are relative to the start of the region.
8950 *
8951 * return FAIL for failure, OK for success.
8952 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008953 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954screen_ins_lines(off, row, line_count, end, wp)
8955 int off;
8956 int row;
8957 int line_count;
8958 int end;
8959 win_T *wp; /* NULL or window to use width from */
8960{
8961 int i;
8962 int j;
8963 unsigned temp;
8964 int cursor_row;
8965 int type;
8966 int result_empty;
8967 int can_ce = can_clear(T_CE);
8968
8969 /*
8970 * FAIL if
8971 * - there is no valid screen
8972 * - the screen has to be redrawn completely
8973 * - the line count is less than one
8974 * - the line count is more than 'ttyscroll'
8975 */
8976 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8977 return FAIL;
8978
8979 /*
8980 * There are seven ways to insert lines:
8981 * 0. When in a vertically split window and t_CV isn't set, redraw the
8982 * characters from ScreenLines[].
8983 * 1. Use T_CD (clear to end of display) if it exists and the result of
8984 * the insert is just empty lines
8985 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8986 * present or line_count > 1. It looks better if we do all the inserts
8987 * at once.
8988 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8989 * insert is just empty lines and T_CE is not present or line_count >
8990 * 1.
8991 * 4. Use T_AL (insert line) if it exists.
8992 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8993 * just empty lines.
8994 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8995 * just empty lines.
8996 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8997 * the 'da' flag is not set or we have clear line capability.
8998 * 8. redraw the characters from ScreenLines[].
8999 *
9000 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9001 * the scrollbar for the window. It does have insert line, use that if it
9002 * exists.
9003 */
9004 result_empty = (row + line_count >= end);
9005#ifdef FEAT_VERTSPLIT
9006 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9007 type = USE_REDRAW;
9008 else
9009#endif
9010 if (can_clear(T_CD) && result_empty)
9011 type = USE_T_CD;
9012 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9013 type = USE_T_CAL;
9014 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9015 type = USE_T_CDL;
9016 else if (*T_AL != NUL)
9017 type = USE_T_AL;
9018 else if (can_ce && result_empty)
9019 type = USE_T_CE;
9020 else if (*T_DL != NUL && result_empty)
9021 type = USE_T_DL;
9022 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9023 type = USE_T_SR;
9024 else
9025 return FAIL;
9026
9027 /*
9028 * For clearing the lines screen_del_lines() is used. This will also take
9029 * care of t_db if necessary.
9030 */
9031 if (type == USE_T_CD || type == USE_T_CDL ||
9032 type == USE_T_CE || type == USE_T_DL)
9033 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9034
9035 /*
9036 * If text is retained below the screen, first clear or delete as many
9037 * lines at the bottom of the window as are about to be inserted so that
9038 * the deleted lines won't later surface during a screen_del_lines.
9039 */
9040 if (*T_DB)
9041 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9042
9043#ifdef FEAT_CLIPBOARD
9044 /* Remove a modeless selection when inserting lines halfway the screen
9045 * or not the full width of the screen. */
9046 if (off + row > 0
9047# ifdef FEAT_VERTSPLIT
9048 || (wp != NULL && wp->w_width != Columns)
9049# endif
9050 )
9051 clip_clear_selection();
9052 else
9053 clip_scroll_selection(-line_count);
9054#endif
9055
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056#ifdef FEAT_GUI
9057 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9058 * scrolling is actually carried out. */
9059 gui_dont_update_cursor();
9060#endif
9061
9062 if (*T_CCS != NUL) /* cursor relative to region */
9063 cursor_row = row;
9064 else
9065 cursor_row = row + off;
9066
9067 /*
9068 * Shift LineOffset[] line_count down to reflect the inserted lines.
9069 * Clear the inserted lines in ScreenLines[].
9070 */
9071 row += off;
9072 end += off;
9073 for (i = 0; i < line_count; ++i)
9074 {
9075#ifdef FEAT_VERTSPLIT
9076 if (wp != NULL && wp->w_width != Columns)
9077 {
9078 /* need to copy part of a line */
9079 j = end - 1 - i;
9080 while ((j -= line_count) >= row)
9081 linecopy(j + line_count, j, wp);
9082 j += line_count;
9083 if (can_clear((char_u *)" "))
9084 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9085 else
9086 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9087 LineWraps[j] = FALSE;
9088 }
9089 else
9090#endif
9091 {
9092 j = end - 1 - i;
9093 temp = LineOffset[j];
9094 while ((j -= line_count) >= row)
9095 {
9096 LineOffset[j + line_count] = LineOffset[j];
9097 LineWraps[j + line_count] = LineWraps[j];
9098 }
9099 LineOffset[j + line_count] = temp;
9100 LineWraps[j + line_count] = FALSE;
9101 if (can_clear((char_u *)" "))
9102 lineclear(temp, (int)Columns);
9103 else
9104 lineinvalid(temp, (int)Columns);
9105 }
9106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107
9108 screen_stop_highlight();
9109 windgoto(cursor_row, 0);
9110
9111#ifdef FEAT_VERTSPLIT
9112 /* redraw the characters */
9113 if (type == USE_REDRAW)
9114 redraw_block(row, end, wp);
9115 else
9116#endif
9117 if (type == USE_T_CAL)
9118 {
9119 term_append_lines(line_count);
9120 screen_start(); /* don't know where cursor is now */
9121 }
9122 else
9123 {
9124 for (i = 0; i < line_count; i++)
9125 {
9126 if (type == USE_T_AL)
9127 {
9128 if (i && cursor_row != 0)
9129 windgoto(cursor_row, 0);
9130 out_str(T_AL);
9131 }
9132 else /* type == USE_T_SR */
9133 out_str(T_SR);
9134 screen_start(); /* don't know where cursor is now */
9135 }
9136 }
9137
9138 /*
9139 * With scroll-reverse and 'da' flag set we need to clear the lines that
9140 * have been scrolled down into the region.
9141 */
9142 if (type == USE_T_SR && *T_DA)
9143 {
9144 for (i = 0; i < line_count; ++i)
9145 {
9146 windgoto(off + i, 0);
9147 out_str(T_CE);
9148 screen_start(); /* don't know where cursor is now */
9149 }
9150 }
9151
9152#ifdef FEAT_GUI
9153 gui_can_update_cursor();
9154 if (gui.in_use)
9155 out_flush(); /* always flush after a scroll */
9156#endif
9157 return OK;
9158}
9159
9160/*
9161 * delete lines on the screen and update ScreenLines[]
9162 * 'end' is the line after the scrolled part. Normally it is Rows.
9163 * When scrolling region used 'off' is the offset from the top for the region.
9164 * 'row' and 'end' are relative to the start of the region.
9165 *
9166 * Return OK for success, FAIL if the lines are not deleted.
9167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 int
9169screen_del_lines(off, row, line_count, end, force, wp)
9170 int off;
9171 int row;
9172 int line_count;
9173 int end;
9174 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009175 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177 int j;
9178 int i;
9179 unsigned temp;
9180 int cursor_row;
9181 int cursor_end;
9182 int result_empty; /* result is empty until end of region */
9183 int can_delete; /* deleting line codes can be used */
9184 int type;
9185
9186 /*
9187 * FAIL if
9188 * - there is no valid screen
9189 * - the screen has to be redrawn completely
9190 * - the line count is less than one
9191 * - the line count is more than 'ttyscroll'
9192 */
9193 if (!screen_valid(TRUE) || line_count <= 0 ||
9194 (!force && line_count > p_ttyscroll))
9195 return FAIL;
9196
9197 /*
9198 * Check if the rest of the current region will become empty.
9199 */
9200 result_empty = row + line_count >= end;
9201
9202 /*
9203 * We can delete lines only when 'db' flag not set or when 'ce' option
9204 * available.
9205 */
9206 can_delete = (*T_DB == NUL || can_clear(T_CE));
9207
9208 /*
9209 * There are six ways to delete lines:
9210 * 0. When in a vertically split window and t_CV isn't set, redraw the
9211 * characters from ScreenLines[].
9212 * 1. Use T_CD if it exists and the result is empty.
9213 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9214 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9215 * none of the other ways work.
9216 * 4. Use T_CE (erase line) if the result is empty.
9217 * 5. Use T_DL (delete line) if it exists.
9218 * 6. redraw the characters from ScreenLines[].
9219 */
9220#ifdef FEAT_VERTSPLIT
9221 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9222 type = USE_REDRAW;
9223 else
9224#endif
9225 if (can_clear(T_CD) && result_empty)
9226 type = USE_T_CD;
9227#if defined(__BEOS__) && defined(BEOS_DR8)
9228 /*
9229 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9230 * its internal termcap... this works okay for tests which test *T_DB !=
9231 * NUL. It has the disadvantage that the user cannot use any :set t_*
9232 * command to get T_DB (back) to empty_option, only :set term=... will do
9233 * the trick...
9234 * Anyway, this hack will hopefully go away with the next OS release.
9235 * (Olaf Seibert)
9236 */
9237 else if (row == 0 && T_DB == empty_option
9238 && (line_count == 1 || *T_CDL == NUL))
9239#else
9240 else if (row == 0 && (
9241#ifndef AMIGA
9242 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9243 * up, so use delete-line command */
9244 line_count == 1 ||
9245#endif
9246 *T_CDL == NUL))
9247#endif
9248 type = USE_NL;
9249 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9250 type = USE_T_CDL;
9251 else if (can_clear(T_CE) && result_empty
9252#ifdef FEAT_VERTSPLIT
9253 && (wp == NULL || wp->w_width == Columns)
9254#endif
9255 )
9256 type = USE_T_CE;
9257 else if (*T_DL != NUL && can_delete)
9258 type = USE_T_DL;
9259 else if (*T_CDL != NUL && can_delete)
9260 type = USE_T_CDL;
9261 else
9262 return FAIL;
9263
9264#ifdef FEAT_CLIPBOARD
9265 /* Remove a modeless selection when deleting lines halfway the screen or
9266 * not the full width of the screen. */
9267 if (off + row > 0
9268# ifdef FEAT_VERTSPLIT
9269 || (wp != NULL && wp->w_width != Columns)
9270# endif
9271 )
9272 clip_clear_selection();
9273 else
9274 clip_scroll_selection(line_count);
9275#endif
9276
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277#ifdef FEAT_GUI
9278 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9279 * scrolling is actually carried out. */
9280 gui_dont_update_cursor();
9281#endif
9282
9283 if (*T_CCS != NUL) /* cursor relative to region */
9284 {
9285 cursor_row = row;
9286 cursor_end = end;
9287 }
9288 else
9289 {
9290 cursor_row = row + off;
9291 cursor_end = end + off;
9292 }
9293
9294 /*
9295 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9296 * Clear the inserted lines in ScreenLines[].
9297 */
9298 row += off;
9299 end += off;
9300 for (i = 0; i < line_count; ++i)
9301 {
9302#ifdef FEAT_VERTSPLIT
9303 if (wp != NULL && wp->w_width != Columns)
9304 {
9305 /* need to copy part of a line */
9306 j = row + i;
9307 while ((j += line_count) <= end - 1)
9308 linecopy(j - line_count, j, wp);
9309 j -= line_count;
9310 if (can_clear((char_u *)" "))
9311 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9312 else
9313 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9314 LineWraps[j] = FALSE;
9315 }
9316 else
9317#endif
9318 {
9319 /* whole width, moving the line pointers is faster */
9320 j = row + i;
9321 temp = LineOffset[j];
9322 while ((j += line_count) <= end - 1)
9323 {
9324 LineOffset[j - line_count] = LineOffset[j];
9325 LineWraps[j - line_count] = LineWraps[j];
9326 }
9327 LineOffset[j - line_count] = temp;
9328 LineWraps[j - line_count] = FALSE;
9329 if (can_clear((char_u *)" "))
9330 lineclear(temp, (int)Columns);
9331 else
9332 lineinvalid(temp, (int)Columns);
9333 }
9334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335
9336 screen_stop_highlight();
9337
9338#ifdef FEAT_VERTSPLIT
9339 /* redraw the characters */
9340 if (type == USE_REDRAW)
9341 redraw_block(row, end, wp);
9342 else
9343#endif
9344 if (type == USE_T_CD) /* delete the lines */
9345 {
9346 windgoto(cursor_row, 0);
9347 out_str(T_CD);
9348 screen_start(); /* don't know where cursor is now */
9349 }
9350 else if (type == USE_T_CDL)
9351 {
9352 windgoto(cursor_row, 0);
9353 term_delete_lines(line_count);
9354 screen_start(); /* don't know where cursor is now */
9355 }
9356 /*
9357 * Deleting lines at top of the screen or scroll region: Just scroll
9358 * the whole screen (scroll region) up by outputting newlines on the
9359 * last line.
9360 */
9361 else if (type == USE_NL)
9362 {
9363 windgoto(cursor_end - 1, 0);
9364 for (i = line_count; --i >= 0; )
9365 out_char('\n'); /* cursor will remain on same line */
9366 }
9367 else
9368 {
9369 for (i = line_count; --i >= 0; )
9370 {
9371 if (type == USE_T_DL)
9372 {
9373 windgoto(cursor_row, 0);
9374 out_str(T_DL); /* delete a line */
9375 }
9376 else /* type == USE_T_CE */
9377 {
9378 windgoto(cursor_row + i, 0);
9379 out_str(T_CE); /* erase a line */
9380 }
9381 screen_start(); /* don't know where cursor is now */
9382 }
9383 }
9384
9385 /*
9386 * If the 'db' flag is set, we need to clear the lines that have been
9387 * scrolled up at the bottom of the region.
9388 */
9389 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9390 {
9391 for (i = line_count; i > 0; --i)
9392 {
9393 windgoto(cursor_end - i, 0);
9394 out_str(T_CE); /* erase a line */
9395 screen_start(); /* don't know where cursor is now */
9396 }
9397 }
9398
9399#ifdef FEAT_GUI
9400 gui_can_update_cursor();
9401 if (gui.in_use)
9402 out_flush(); /* always flush after a scroll */
9403#endif
9404
9405 return OK;
9406}
9407
9408/*
9409 * show the current mode and ruler
9410 *
9411 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9412 * If clear_cmdline is FALSE there may be a message there that needs to be
9413 * cleared only if a mode is shown.
9414 * Return the length of the message (0 if no message).
9415 */
9416 int
9417showmode()
9418{
9419 int need_clear;
9420 int length = 0;
9421 int do_mode;
9422 int attr;
9423 int nwr_save;
9424#ifdef FEAT_INS_EXPAND
9425 int sub_attr;
9426#endif
9427
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009428 do_mode = ((p_smd && msg_silent == 0)
9429 && ((State & INSERT)
9430 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431#ifdef FEAT_VISUAL
9432 || VIsual_active
9433#endif
9434 ));
9435 if (do_mode || Recording)
9436 {
9437 /*
9438 * Don't show mode right now, when not redrawing or inside a mapping.
9439 * Call char_avail() only when we are going to show something, because
9440 * it takes a bit of time.
9441 */
9442 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9443 {
9444 redraw_cmdline = TRUE; /* show mode later */
9445 return 0;
9446 }
9447
9448 nwr_save = need_wait_return;
9449
9450 /* wait a bit before overwriting an important message */
9451 check_for_delay(FALSE);
9452
9453 /* if the cmdline is more than one line high, erase top lines */
9454 need_clear = clear_cmdline;
9455 if (clear_cmdline && cmdline_row < Rows - 1)
9456 msg_clr_cmdline(); /* will reset clear_cmdline */
9457
9458 /* Position on the last line in the window, column 0 */
9459 msg_pos_mode();
9460 cursor_off();
9461 attr = hl_attr(HLF_CM); /* Highlight mode */
9462 if (do_mode)
9463 {
9464 MSG_PUTS_ATTR("--", attr);
9465#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009466 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009467# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009468 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009469# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009470 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009471# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009472 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009473# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 MSG_PUTS_ATTR(" IM", attr);
9475# else
9476 MSG_PUTS_ATTR(" XIM", attr);
9477# endif
9478#endif
9479#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9480 if (gui.in_use)
9481 {
9482 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009483 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 }
9485#endif
9486#ifdef FEAT_INS_EXPAND
9487 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9488 {
9489 /* These messages can get long, avoid a wrap in a narrow
9490 * window. Prefer showing edit_submode_extra. */
9491 length = (Rows - msg_row) * Columns - 3;
9492 if (edit_submode_extra != NULL)
9493 length -= vim_strsize(edit_submode_extra);
9494 if (length > 0)
9495 {
9496 if (edit_submode_pre != NULL)
9497 length -= vim_strsize(edit_submode_pre);
9498 if (length - vim_strsize(edit_submode) > 0)
9499 {
9500 if (edit_submode_pre != NULL)
9501 msg_puts_attr(edit_submode_pre, attr);
9502 msg_puts_attr(edit_submode, attr);
9503 }
9504 if (edit_submode_extra != NULL)
9505 {
9506 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9507 if ((int)edit_submode_highl < (int)HLF_COUNT)
9508 sub_attr = hl_attr(edit_submode_highl);
9509 else
9510 sub_attr = attr;
9511 msg_puts_attr(edit_submode_extra, sub_attr);
9512 }
9513 }
9514 length = 0;
9515 }
9516 else
9517#endif
9518 {
9519#ifdef FEAT_VREPLACE
9520 if (State & VREPLACE_FLAG)
9521 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9522 else
9523#endif
9524 if (State & REPLACE_FLAG)
9525 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9526 else if (State & INSERT)
9527 {
9528#ifdef FEAT_RIGHTLEFT
9529 if (p_ri)
9530 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9531#endif
9532 MSG_PUTS_ATTR(_(" INSERT"), attr);
9533 }
9534 else if (restart_edit == 'I')
9535 MSG_PUTS_ATTR(_(" (insert)"), attr);
9536 else if (restart_edit == 'R')
9537 MSG_PUTS_ATTR(_(" (replace)"), attr);
9538 else if (restart_edit == 'V')
9539 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9540#ifdef FEAT_RIGHTLEFT
9541 if (p_hkmap)
9542 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9543# ifdef FEAT_FKMAP
9544 if (p_fkmap)
9545 MSG_PUTS_ATTR(farsi_text_5, attr);
9546# endif
9547#endif
9548#ifdef FEAT_KEYMAP
9549 if (State & LANGMAP)
9550 {
9551# ifdef FEAT_ARABIC
9552 if (curwin->w_p_arab)
9553 MSG_PUTS_ATTR(_(" Arabic"), attr);
9554 else
9555# endif
9556 MSG_PUTS_ATTR(_(" (lang)"), attr);
9557 }
9558#endif
9559 if ((State & INSERT) && p_paste)
9560 MSG_PUTS_ATTR(_(" (paste)"), attr);
9561
9562#ifdef FEAT_VISUAL
9563 if (VIsual_active)
9564 {
9565 char *p;
9566
9567 /* Don't concatenate separate words to avoid translation
9568 * problems. */
9569 switch ((VIsual_select ? 4 : 0)
9570 + (VIsual_mode == Ctrl_V) * 2
9571 + (VIsual_mode == 'V'))
9572 {
9573 case 0: p = N_(" VISUAL"); break;
9574 case 1: p = N_(" VISUAL LINE"); break;
9575 case 2: p = N_(" VISUAL BLOCK"); break;
9576 case 4: p = N_(" SELECT"); break;
9577 case 5: p = N_(" SELECT LINE"); break;
9578 default: p = N_(" SELECT BLOCK"); break;
9579 }
9580 MSG_PUTS_ATTR(_(p), attr);
9581 }
9582#endif
9583 MSG_PUTS_ATTR(" --", attr);
9584 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009585
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 need_clear = TRUE;
9587 }
9588 if (Recording
9589#ifdef FEAT_INS_EXPAND
9590 && edit_submode == NULL /* otherwise it gets too long */
9591#endif
9592 )
9593 {
9594 MSG_PUTS_ATTR(_("recording"), attr);
9595 need_clear = TRUE;
9596 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009597
9598 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 if (need_clear || clear_cmdline)
9600 msg_clr_eos();
9601 msg_didout = FALSE; /* overwrite this message */
9602 length = msg_col;
9603 msg_col = 0;
9604 need_wait_return = nwr_save; /* never ask for hit-return for this */
9605 }
9606 else if (clear_cmdline && msg_silent == 0)
9607 /* Clear the whole command line. Will reset "clear_cmdline". */
9608 msg_clr_cmdline();
9609
9610#ifdef FEAT_CMDL_INFO
9611# ifdef FEAT_VISUAL
9612 /* In Visual mode the size of the selected area must be redrawn. */
9613 if (VIsual_active)
9614 clear_showcmd();
9615# endif
9616
9617 /* If the last window has no status line, the ruler is after the mode
9618 * message and must be redrawn */
9619 if (redrawing()
9620# ifdef FEAT_WINDOWS
9621 && lastwin->w_status_height == 0
9622# endif
9623 )
9624 win_redr_ruler(lastwin, TRUE);
9625#endif
9626 redraw_cmdline = FALSE;
9627 clear_cmdline = FALSE;
9628
9629 return length;
9630}
9631
9632/*
9633 * Position for a mode message.
9634 */
9635 static void
9636msg_pos_mode()
9637{
9638 msg_col = 0;
9639 msg_row = Rows - 1;
9640}
9641
9642/*
9643 * Delete mode message. Used when ESC is typed which is expected to end
9644 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009645 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 */
9647 void
9648unshowmode(force)
9649 int force;
9650{
9651 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009652 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 */
9654 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9655 redraw_cmdline = TRUE; /* delete mode later */
9656 else
9657 {
9658 msg_pos_mode();
9659 if (Recording)
9660 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9661 msg_clr_eos();
9662 }
9663}
9664
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009665#if defined(FEAT_WINDOWS)
9666/*
9667 * Draw the tab pages line at the top of the Vim window.
9668 */
9669 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009670draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009671{
9672 int tabcount = 0;
9673 tabpage_T *tp;
9674 int tabwidth;
9675 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009676 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009677 int attr;
9678 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009679 win_T *cwp;
9680 int wincount;
9681 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009682 int c;
9683 int len;
9684 int attr_sel = hl_attr(HLF_TPS);
9685 int attr_nosel = hl_attr(HLF_TP);
9686 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009687 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009688 int room;
9689 int use_sep_chars = (t_colors < 8
9690#ifdef FEAT_GUI
9691 && !gui.in_use
9692#endif
9693 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009694
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009695 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009696
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009697#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009698 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009699 if (gui_use_tabline())
9700 {
9701 gui_update_tabline();
9702 return;
9703 }
9704#endif
9705
9706 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009707 return;
9708
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009709#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009710
9711 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9712 for (scol = 0; scol < Columns; ++scol)
9713 TabPageIdxs[scol] = 0;
9714
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009715 /* Use the 'tabline' option if it's set. */
9716 if (*p_tal != NUL)
9717 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009718 int save_called_emsg = called_emsg;
9719
9720 /* Check for an error. If there is one we would loop in redrawing the
9721 * screen. Avoid that by making 'tabline' empty. */
9722 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009723 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009724 if (called_emsg)
9725 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009726 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009727 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009728 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009729 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009730#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009731 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009732 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9733 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009734
Bram Moolenaar238a5642006-02-21 22:12:05 +00009735 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9736 if (tabwidth < 6)
9737 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009738
Bram Moolenaar238a5642006-02-21 22:12:05 +00009739 attr = attr_nosel;
9740 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009741 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009742 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9743 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009744 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009745 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009746
Bram Moolenaar238a5642006-02-21 22:12:05 +00009747 if (tp->tp_topframe == topframe)
9748 attr = attr_sel;
9749 if (use_sep_chars && col > 0)
9750 screen_putchar('|', 0, col++, attr);
9751
9752 if (tp->tp_topframe != topframe)
9753 attr = attr_nosel;
9754
9755 screen_putchar(' ', 0, col++, attr);
9756
9757 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009758 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009759 cwp = curwin;
9760 wp = firstwin;
9761 }
9762 else
9763 {
9764 cwp = tp->tp_curwin;
9765 wp = tp->tp_firstwin;
9766 }
9767
9768 modified = FALSE;
9769 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9770 if (bufIsChanged(wp->w_buffer))
9771 modified = TRUE;
9772 if (modified || wincount > 1)
9773 {
9774 if (wincount > 1)
9775 {
9776 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009777 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009778 if (col + len >= Columns - 3)
9779 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009780 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009781#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009782 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009783#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009784 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009785#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009786 );
9787 col += len;
9788 }
9789 if (modified)
9790 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9791 screen_putchar(' ', 0, col++, attr);
9792 }
9793
9794 room = scol - col + tabwidth - 1;
9795 if (room > 0)
9796 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009797 /* Get buffer name in NameBuff[] */
9798 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009799 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009800 len = vim_strsize(NameBuff);
9801 p = NameBuff;
9802#ifdef FEAT_MBYTE
9803 if (has_mbyte)
9804 while (len > room)
9805 {
9806 len -= ptr2cells(p);
9807 mb_ptr_adv(p);
9808 }
9809 else
9810#endif
9811 if (len > room)
9812 {
9813 p += len - room;
9814 len = room;
9815 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009816 if (len > Columns - col - 1)
9817 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009818
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009819 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009820 col += len;
9821 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009822 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009823
9824 /* Store the tab page number in TabPageIdxs[], so that
9825 * jump_to_mouse() knows where each one is. */
9826 ++tabcount;
9827 while (scol < col)
9828 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009829 }
9830
Bram Moolenaar238a5642006-02-21 22:12:05 +00009831 if (use_sep_chars)
9832 c = '_';
9833 else
9834 c = ' ';
9835 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009836
9837 /* Put an "X" for closing the current tab if there are several. */
9838 if (first_tabpage->tp_next != NULL)
9839 {
9840 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9841 TabPageIdxs[Columns - 1] = -999;
9842 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009843 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009844
9845 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9846 * set. */
9847 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009848}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009849
9850/*
9851 * Get buffer name for "buf" into NameBuff[].
9852 * Takes care of special buffer names and translates special characters.
9853 */
9854 void
9855get_trans_bufname(buf)
9856 buf_T *buf;
9857{
9858 if (buf_spname(buf) != NULL)
9859 STRCPY(NameBuff, buf_spname(buf));
9860 else
9861 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9862 trans_characters(NameBuff, MAXPATHL);
9863}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009864#endif
9865
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9867/*
9868 * Get the character to use in a status line. Get its attributes in "*attr".
9869 */
9870 static int
9871fillchar_status(attr, is_curwin)
9872 int *attr;
9873 int is_curwin;
9874{
9875 int fill;
9876 if (is_curwin)
9877 {
9878 *attr = hl_attr(HLF_S);
9879 fill = fill_stl;
9880 }
9881 else
9882 {
9883 *attr = hl_attr(HLF_SNC);
9884 fill = fill_stlnc;
9885 }
9886 /* Use fill when there is highlighting, and highlighting of current
9887 * window differs, or the fillchars differ, or this is not the
9888 * current window */
9889 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9890 || !is_curwin || firstwin == lastwin)
9891 || (fill_stl != fill_stlnc)))
9892 return fill;
9893 if (is_curwin)
9894 return '^';
9895 return '=';
9896}
9897#endif
9898
9899#ifdef FEAT_VERTSPLIT
9900/*
9901 * Get the character to use in a separator between vertically split windows.
9902 * Get its attributes in "*attr".
9903 */
9904 static int
9905fillchar_vsep(attr)
9906 int *attr;
9907{
9908 *attr = hl_attr(HLF_C);
9909 if (*attr == 0 && fill_vert == ' ')
9910 return '|';
9911 else
9912 return fill_vert;
9913}
9914#endif
9915
9916/*
9917 * Return TRUE if redrawing should currently be done.
9918 */
9919 int
9920redrawing()
9921{
9922 return (!RedrawingDisabled
9923 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9924}
9925
9926/*
9927 * Return TRUE if printing messages should currently be done.
9928 */
9929 int
9930messaging()
9931{
9932 return (!(p_lz && char_avail() && !KeyTyped));
9933}
9934
9935/*
9936 * Show current status info in ruler and various other places
9937 * If always is FALSE, only show ruler if position has changed.
9938 */
9939 void
9940showruler(always)
9941 int always;
9942{
9943 if (!always && !redrawing())
9944 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009945#ifdef FEAT_INS_EXPAND
9946 if (pum_visible())
9947 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009948# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009949 /* Don't redraw right now, do it later. */
9950 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009951# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009952 return;
9953 }
9954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009956 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009957 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009958 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 else
9961#endif
9962#ifdef FEAT_CMDL_INFO
9963 win_redr_ruler(curwin, always);
9964#endif
9965
9966#ifdef FEAT_TITLE
9967 if (need_maketitle
9968# ifdef FEAT_STL_OPT
9969 || (p_icon && (stl_syntax & STL_IN_ICON))
9970 || (p_title && (stl_syntax & STL_IN_TITLE))
9971# endif
9972 )
9973 maketitle();
9974#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009975#ifdef FEAT_WINDOWS
9976 /* Redraw the tab pages line if needed. */
9977 if (redraw_tabline)
9978 draw_tabline();
9979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980}
9981
9982#ifdef FEAT_CMDL_INFO
9983 static void
9984win_redr_ruler(wp, always)
9985 win_T *wp;
9986 int always;
9987{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009988#define RULER_BUF_LEN 70
9989 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 int row;
9991 int fillchar;
9992 int attr;
9993 int empty_line = FALSE;
9994 colnr_T virtcol;
9995 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009996 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 int o;
9998#ifdef FEAT_VERTSPLIT
9999 int this_ru_col;
10000 int off = 0;
10001 int width = Columns;
10002# define WITH_OFF(x) x
10003# define WITH_WIDTH(x) x
10004#else
10005# define WITH_OFF(x) 0
10006# define WITH_WIDTH(x) Columns
10007# define this_ru_col ru_col
10008#endif
10009
10010 /* If 'ruler' off or redrawing disabled, don't do anything */
10011 if (!p_ru)
10012 return;
10013
10014 /*
10015 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10016 * after deleting lines, before cursor.lnum is corrected.
10017 */
10018 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10019 return;
10020
10021#ifdef FEAT_INS_EXPAND
10022 /* Don't draw the ruler while doing insert-completion, it might overwrite
10023 * the (long) mode message. */
10024# ifdef FEAT_WINDOWS
10025 if (wp == lastwin && lastwin->w_status_height == 0)
10026# endif
10027 if (edit_submode != NULL)
10028 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010029 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10030 if (pum_visible())
10031 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032#endif
10033
10034#ifdef FEAT_STL_OPT
10035 if (*p_ruf)
10036 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010037 int save_called_emsg = called_emsg;
10038
10039 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010041 if (called_emsg)
10042 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010043 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010044 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 return;
10046 }
10047#endif
10048
10049 /*
10050 * Check if not in Insert mode and the line is empty (will show "0-1").
10051 */
10052 if (!(State & INSERT)
10053 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10054 empty_line = TRUE;
10055
10056 /*
10057 * Only draw the ruler when something changed.
10058 */
10059 validate_virtcol_win(wp);
10060 if ( redraw_cmdline
10061 || always
10062 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10063 || wp->w_cursor.col != wp->w_ru_cursor.col
10064 || wp->w_virtcol != wp->w_ru_virtcol
10065#ifdef FEAT_VIRTUALEDIT
10066 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10067#endif
10068 || wp->w_topline != wp->w_ru_topline
10069 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10070#ifdef FEAT_DIFF
10071 || wp->w_topfill != wp->w_ru_topfill
10072#endif
10073 || empty_line != wp->w_ru_empty)
10074 {
10075 cursor_off();
10076#ifdef FEAT_WINDOWS
10077 if (wp->w_status_height)
10078 {
10079 row = W_WINROW(wp) + wp->w_height;
10080 fillchar = fillchar_status(&attr, wp == curwin);
10081# ifdef FEAT_VERTSPLIT
10082 off = W_WINCOL(wp);
10083 width = W_WIDTH(wp);
10084# endif
10085 }
10086 else
10087#endif
10088 {
10089 row = Rows - 1;
10090 fillchar = ' ';
10091 attr = 0;
10092#ifdef FEAT_VERTSPLIT
10093 width = Columns;
10094 off = 0;
10095#endif
10096 }
10097
10098 /* In list mode virtcol needs to be recomputed */
10099 virtcol = wp->w_virtcol;
10100 if (wp->w_p_list && lcs_tab1 == NUL)
10101 {
10102 wp->w_p_list = FALSE;
10103 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10104 wp->w_p_list = TRUE;
10105 }
10106
10107 /*
10108 * Some sprintfs return the length, some return a pointer.
10109 * To avoid portability problems we use strlen() here.
10110 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010111 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10113 ? 0L
10114 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010115 len = STRLEN(buffer);
10116 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10118 (int)virtcol + 1);
10119
10120 /*
10121 * Add a "50%" if there is room for it.
10122 * On the last line, don't print in the last column (scrolls the
10123 * screen up on some terminals).
10124 */
10125 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010126 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 o = i + vim_strsize(buffer + i + 1);
10128#ifdef FEAT_WINDOWS
10129 if (wp->w_status_height == 0) /* can't use last char of screen */
10130#endif
10131 ++o;
10132#ifdef FEAT_VERTSPLIT
10133 this_ru_col = ru_col - (Columns - width);
10134 if (this_ru_col < 0)
10135 this_ru_col = 0;
10136#endif
10137 /* Never use more than half the window/screen width, leave the other
10138 * half for the filename. */
10139 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10140 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10141 if (this_ru_col + o < WITH_WIDTH(width))
10142 {
10143 while (this_ru_col + o < WITH_WIDTH(width))
10144 {
10145#ifdef FEAT_MBYTE
10146 if (has_mbyte)
10147 i += (*mb_char2bytes)(fillchar, buffer + i);
10148 else
10149#endif
10150 buffer[i++] = fillchar;
10151 ++o;
10152 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010153 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 }
10155 /* Truncate at window boundary. */
10156#ifdef FEAT_MBYTE
10157 if (has_mbyte)
10158 {
10159 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010160 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 {
10162 o += (*mb_ptr2cells)(buffer + i);
10163 if (this_ru_col + o > WITH_WIDTH(width))
10164 {
10165 buffer[i] = NUL;
10166 break;
10167 }
10168 }
10169 }
10170 else
10171#endif
10172 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10173 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10174
10175 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10176 i = redraw_cmdline;
10177 screen_fill(row, row + 1,
10178 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10179 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10180 fillchar, fillchar, attr);
10181 /* don't redraw the cmdline because of showing the ruler */
10182 redraw_cmdline = i;
10183 wp->w_ru_cursor = wp->w_cursor;
10184 wp->w_ru_virtcol = wp->w_virtcol;
10185 wp->w_ru_empty = empty_line;
10186 wp->w_ru_topline = wp->w_topline;
10187 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10188#ifdef FEAT_DIFF
10189 wp->w_ru_topfill = wp->w_topfill;
10190#endif
10191 }
10192}
10193#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010194
10195#if defined(FEAT_LINEBREAK) || defined(PROTO)
10196/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010197 * Return the width of the 'number' and 'relativenumber' column.
10198 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010199 * Otherwise it depends on 'numberwidth' and the line count.
10200 */
10201 int
10202number_width(wp)
10203 win_T *wp;
10204{
10205 int n;
10206 linenr_T lnum;
10207
Bram Moolenaar64486672010-05-16 15:46:46 +020010208 if (wp->w_p_nu)
10209 /* 'number' */
10210 lnum = wp->w_buffer->b_ml.ml_line_count;
10211 else
10212 /* 'relativenumber' */
10213 lnum = wp->w_height;
10214
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010215 if (lnum == wp->w_nrwidth_line_count)
10216 return wp->w_nrwidth_width;
10217 wp->w_nrwidth_line_count = lnum;
10218
10219 n = 0;
10220 do
10221 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010222 lnum /= 10;
10223 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010224 } while (lnum > 0);
10225
10226 /* 'numberwidth' gives the minimal width plus one */
10227 if (n < wp->w_p_nuw - 1)
10228 n = wp->w_p_nuw - 1;
10229
10230 wp->w_nrwidth_width = n;
10231 return n;
10232}
10233#endif