blob: 97d636d700dc02c435306182ced3aacee290762e [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 Moolenaar860cae12010-06-05 23:22:07 +02003231 word_end = (int)(spell_to_word_end(ptr, wp)
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003232 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003233 }
3234 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003235 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003236 /* bad word found, use attributes until end of word */
3237 word_end = wp->w_cursor.col + len + 1;
3238
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003239 /* Turn index into actual attributes. */
3240 if (spell_hlf != HLF_COUNT)
3241 spell_attr = highlight_attr[spell_hlf];
3242 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003243 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003244
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003245# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003246 /* Need to restart syntax highlighting for this line. */
3247 if (has_syntax)
3248 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003249# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003250 }
3251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
3253
3254 /*
3255 * Correct highlighting for cursor that can't be disabled.
3256 * Avoids having to check this for each character.
3257 */
3258 if (fromcol >= 0)
3259 {
3260 if (noinvcur)
3261 {
3262 if ((colnr_T)fromcol == wp->w_virtcol)
3263 {
3264 /* highlighting starts at cursor, let it start just after the
3265 * cursor */
3266 fromcol_prev = fromcol;
3267 fromcol = -1;
3268 }
3269 else if ((colnr_T)fromcol < wp->w_virtcol)
3270 /* restart highlighting after the cursor */
3271 fromcol_prev = wp->w_virtcol;
3272 }
3273 if (fromcol >= tocol)
3274 fromcol = -1;
3275 }
3276
3277#ifdef FEAT_SEARCH_EXTRA
3278 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003279 * Handle highlighting the last used search pattern and matches.
3280 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003282 cur = wp->w_match_head;
3283 shl_flag = FALSE;
3284 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003286 if (shl_flag == FALSE)
3287 {
3288 shl = &search_hl;
3289 shl_flag = TRUE;
3290 }
3291 else
3292 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003293 shl->startcol = MAXCOL;
3294 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 shl->attr_cur = 0;
3296 if (shl->rm.regprog != NULL)
3297 {
3298 v = (long)(ptr - line);
3299 next_search_hl(wp, shl, lnum, (colnr_T)v);
3300
3301 /* Need to get the line again, a multi-line regexp may have made it
3302 * invalid. */
3303 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3304 ptr = line + v;
3305
3306 if (shl->lnum != 0 && shl->lnum <= lnum)
3307 {
3308 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003309 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003311 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3313 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003314 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003316 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003318 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
3320#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003321 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003322 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 else
3324#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003325 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003327 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
3329 shl->attr_cur = shl->attr;
3330 search_attr = shl->attr;
3331 }
3332 area_highlighting = TRUE;
3333 }
3334 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003335 if (shl != &search_hl && cur != NULL)
3336 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338#endif
3339
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003340#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003341 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3342 * active, because it's not clear what is selected then. */
3343 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003344 {
3345 line_attr = hl_attr(HLF_CUL);
3346 area_highlighting = TRUE;
3347 }
3348#endif
3349
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003350 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 col = 0;
3352#ifdef FEAT_RIGHTLEFT
3353 if (wp->w_p_rl)
3354 {
3355 /* Rightleft window: process the text in the normal direction, but put
3356 * it in current_ScreenLine[] from right to left. Start at the
3357 * rightmost column of the window. */
3358 col = W_WIDTH(wp) - 1;
3359 off += col;
3360 }
3361#endif
3362
3363 /*
3364 * Repeat for the whole displayed line.
3365 */
3366 for (;;)
3367 {
3368 /* Skip this quickly when working on the text. */
3369 if (draw_state != WL_LINE)
3370 {
3371#ifdef FEAT_CMDWIN
3372 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3373 {
3374 draw_state = WL_CMDLINE;
3375 if (cmdwin_type != 0 && wp == curwin)
3376 {
3377 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003379 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 char_attr = hl_attr(HLF_AT);
3381 }
3382 }
3383#endif
3384
3385#ifdef FEAT_FOLDING
3386 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3387 {
3388 draw_state = WL_FOLD;
3389 if (wp->w_p_fdc > 0)
3390 {
3391 /* Draw the 'foldcolumn'. */
3392 fill_foldcolumn(extra, wp, FALSE, lnum);
3393 n_extra = wp->w_p_fdc;
3394 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003395 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 c_extra = NUL;
3397 char_attr = hl_attr(HLF_FC);
3398 }
3399 }
3400#endif
3401
3402#ifdef FEAT_SIGNS
3403 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3404 {
3405 draw_state = WL_SIGN;
3406 /* Show the sign column when there are any signs in this
3407 * buffer or when using Netbeans. */
3408 if (draw_signcolumn(wp)
3409# ifdef FEAT_DIFF
3410 && filler_todo <= 0
3411# endif
3412 )
3413 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003414 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003416 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417# endif
3418
3419 /* Draw two cells with the sign value or blank. */
3420 c_extra = ' ';
3421 char_attr = hl_attr(HLF_SC);
3422 n_extra = 2;
3423
3424 if (row == startrow)
3425 {
3426 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3427 SIGN_TEXT);
3428# ifdef FEAT_SIGN_ICONS
3429 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3430 SIGN_ICON);
3431 if (gui.in_use && icon_sign != 0)
3432 {
3433 /* Use the image in this position. */
3434 c_extra = SIGN_BYTE;
3435# ifdef FEAT_NETBEANS_INTG
3436 if (buf_signcount(wp->w_buffer, lnum) > 1)
3437 c_extra = MULTISIGN_BYTE;
3438# endif
3439 char_attr = icon_sign;
3440 }
3441 else
3442# endif
3443 if (text_sign != 0)
3444 {
3445 p_extra = sign_get_text(text_sign);
3446 if (p_extra != NULL)
3447 {
3448 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003449 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
3451 char_attr = sign_get_attr(text_sign, FALSE);
3452 }
3453 }
3454 }
3455 }
3456#endif
3457
3458 if (draw_state == WL_NR - 1 && n_extra == 0)
3459 {
3460 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003461 /* Display the absolute or relative line number. After the
3462 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3463 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 && (row == startrow
3465#ifdef FEAT_DIFF
3466 + filler_lines
3467#endif
3468 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3469 {
3470 /* Draw the line number (empty space after wrapping). */
3471 if (row == startrow
3472#ifdef FEAT_DIFF
3473 + filler_lines
3474#endif
3475 )
3476 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003477 long num;
3478
3479 if (wp->w_p_nu)
3480 /* 'number' */
3481 num = (long)lnum;
3482 else
3483 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003484 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02003485
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003486 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003487 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (wp->w_skipcol > 0)
3489 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3490 *p_extra = '-';
3491#ifdef FEAT_RIGHTLEFT
3492 if (wp->w_p_rl) /* reverse line numbers */
3493 rl_mirror(extra);
3494#endif
3495 p_extra = extra;
3496 c_extra = NUL;
3497 }
3498 else
3499 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003500 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003502#ifdef FEAT_SYN_HL
3503 /* When 'cursorline' is set highlight the line number of
3504 * the current line differently. */
3505 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3506 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509 }
3510
3511#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3512 if (draw_state == WL_SBR - 1 && n_extra == 0)
3513 {
3514 draw_state = WL_SBR;
3515# ifdef FEAT_DIFF
3516 if (filler_todo > 0)
3517 {
3518 /* Draw "deleted" diff line(s). */
3519 if (char2cells(fill_diff) > 1)
3520 c_extra = '-';
3521 else
3522 c_extra = fill_diff;
3523# ifdef FEAT_RIGHTLEFT
3524 if (wp->w_p_rl)
3525 n_extra = col + 1;
3526 else
3527# endif
3528 n_extra = W_WIDTH(wp) - col;
3529 char_attr = hl_attr(HLF_DED);
3530 }
3531# endif
3532# ifdef FEAT_LINEBREAK
3533 if (*p_sbr != NUL && need_showbreak)
3534 {
3535 /* Draw 'showbreak' at the start of each broken line. */
3536 p_extra = p_sbr;
3537 c_extra = NUL;
3538 n_extra = (int)STRLEN(p_sbr);
3539 char_attr = hl_attr(HLF_AT);
3540 need_showbreak = FALSE;
3541 /* Correct end of highlighted area for 'showbreak',
3542 * required when 'linebreak' is also set. */
3543 if (tocol == vcol)
3544 tocol += n_extra;
3545 }
3546# endif
3547 }
3548#endif
3549
3550 if (draw_state == WL_LINE - 1 && n_extra == 0)
3551 {
3552 draw_state = WL_LINE;
3553 if (saved_n_extra)
3554 {
3555 /* Continue item from end of wrapped line. */
3556 n_extra = saved_n_extra;
3557 c_extra = saved_c_extra;
3558 p_extra = saved_p_extra;
3559 char_attr = saved_char_attr;
3560 }
3561 else
3562 char_attr = 0;
3563 }
3564 }
3565
3566 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003567 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003568 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569#ifdef FEAT_DIFF
3570 && filler_todo <= 0
3571#endif
3572 )
3573 {
3574 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3575 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003576 /* Pretend we have finished updating the window. Except when
3577 * 'cursorcolumn' is set. */
3578#ifdef FEAT_SYN_HL
3579 if (wp->w_p_cuc)
3580 row = wp->w_cline_row + wp->w_cline_height;
3581 else
3582#endif
3583 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 break;
3585 }
3586
3587 if (draw_state == WL_LINE && area_highlighting)
3588 {
3589 /* handle Visual or match highlighting in this line */
3590 if (vcol == fromcol
3591#ifdef FEAT_MBYTE
3592 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3593 && (*mb_ptr2cells)(ptr) > 1)
3594#endif
3595 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003596 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 && vcol < tocol))
3598 area_attr = attr; /* start highlighting */
3599 else if (area_attr != 0
3600 && (vcol == tocol
3601 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
3604#ifdef FEAT_SEARCH_EXTRA
3605 if (!n_extra)
3606 {
3607 /*
3608 * Check for start/end of search pattern match.
3609 * After end, check for start/end of next match.
3610 * When another match, have to check for start again.
3611 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003612 * Do this for 'search_hl' and the match list (ordered by
3613 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003615 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003616 cur = wp->w_match_head;
3617 shl_flag = FALSE;
3618 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003620 if (shl_flag == FALSE
3621 && ((cur != NULL
3622 && cur->priority > SEARCH_HL_PRIORITY)
3623 || cur == NULL))
3624 {
3625 shl = &search_hl;
3626 shl_flag = TRUE;
3627 }
3628 else
3629 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 while (shl->rm.regprog != NULL)
3631 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003632 if (shl->startcol != MAXCOL
3633 && v >= (long)shl->startcol
3634 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
3636 shl->attr_cur = shl->attr;
3637 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003638 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
3640 shl->attr_cur = 0;
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 next_search_hl(wp, shl, lnum, (colnr_T)v);
3643
3644 /* Need to get the line again, a multi-line regexp
3645 * may have made it invalid. */
3646 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3647 ptr = line + v;
3648
3649 if (shl->lnum == lnum)
3650 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003651 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003653 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003655 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003657 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 /* highlight empty match, try again after
3660 * it */
3661#ifdef FEAT_MBYTE
3662 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003663 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003664 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 else
3666#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003667 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669
3670 /* Loop to check if the match starts at the
3671 * current position */
3672 continue;
3673 }
3674 }
3675 break;
3676 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003677 if (shl != &search_hl && cur != NULL)
3678 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003680
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003681 /* Use attributes from match with highest priority among
3682 * 'search_hl' and the match list. */
3683 search_attr = search_hl.attr_cur;
3684 cur = wp->w_match_head;
3685 shl_flag = FALSE;
3686 while (cur != NULL || shl_flag == FALSE)
3687 {
3688 if (shl_flag == FALSE
3689 && ((cur != NULL
3690 && cur->priority > SEARCH_HL_PRIORITY)
3691 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003692 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003693 shl = &search_hl;
3694 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003695 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003696 else
3697 shl = &cur->hl;
3698 if (shl->attr_cur != 0)
3699 search_attr = shl->attr_cur;
3700 if (shl != &search_hl && cur != NULL)
3701 cur = cur->next;
3702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704#endif
3705
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003707 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003709 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3710 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003712 if (diff_hlf == HLF_TXD && ptr - line > change_end
3713 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003715 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003718
3719 /* Decide which of the highlight attributes to use. */
3720 attr_pri = TRUE;
3721 if (area_attr != 0)
3722 char_attr = area_attr;
3723 else if (search_attr != 0)
3724 char_attr = search_attr;
3725#ifdef LINE_ATTR
3726 /* Use line_attr when not in the Visual or 'incsearch' area
3727 * (area_attr may be 0 when "noinvcur" is set). */
3728 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003729 || vcol < fromcol || vcol_prev < fromcol_prev
3730 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003731 char_attr = line_attr;
3732#endif
3733 else
3734 {
3735 attr_pri = FALSE;
3736#ifdef FEAT_SYN_HL
3737 if (has_syntax)
3738 char_attr = syntax_attr;
3739 else
3740#endif
3741 char_attr = 0;
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744
3745 /*
3746 * Get the next character to put on the screen.
3747 */
3748 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003749 * The "p_extra" points to the extra stuff that is inserted to
3750 * represent special characters (non-printable stuff) and other
3751 * things. When all characters are the same, c_extra is used.
3752 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3753 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3755 */
3756 if (n_extra > 0)
3757 {
3758 if (c_extra != NUL)
3759 {
3760 c = c_extra;
3761#ifdef FEAT_MBYTE
3762 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3763 if (enc_utf8 && (*mb_char2len)(c) > 1)
3764 {
3765 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003766 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003767 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 else
3770 mb_utf8 = FALSE;
3771#endif
3772 }
3773 else
3774 {
3775 c = *p_extra;
3776#ifdef FEAT_MBYTE
3777 if (has_mbyte)
3778 {
3779 mb_c = c;
3780 if (enc_utf8)
3781 {
3782 /* If the UTF-8 character is more than one byte:
3783 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003784 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 mb_utf8 = FALSE;
3786 if (mb_l > n_extra)
3787 mb_l = 1;
3788 else if (mb_l > 1)
3789 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003790 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003792 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 }
3795 else
3796 {
3797 /* if this is a DBCS character, put it in "mb_c" */
3798 mb_l = MB_BYTE2LEN(c);
3799 if (mb_l >= n_extra)
3800 mb_l = 1;
3801 else if (mb_l > 1)
3802 mb_c = (c << 8) + p_extra[1];
3803 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003804 if (mb_l == 0) /* at the NUL at end-of-line */
3805 mb_l = 1;
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 /* If a double-width char doesn't fit display a '>' in the
3808 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003809 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810# ifdef FEAT_RIGHTLEFT
3811 wp->w_p_rl ? (col <= 0) :
3812# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003813 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 && (*mb_char2cells)(mb_c) == 2)
3815 {
3816 c = '>';
3817 mb_c = c;
3818 mb_l = 1;
3819 mb_utf8 = FALSE;
3820 multi_attr = hl_attr(HLF_AT);
3821 /* put the pointer back to output the double-width
3822 * character at the start of the next line. */
3823 ++n_extra;
3824 --p_extra;
3825 }
3826 else
3827 {
3828 n_extra -= mb_l - 1;
3829 p_extra += mb_l - 1;
3830 }
3831 }
3832#endif
3833 ++p_extra;
3834 }
3835 --n_extra;
3836 }
3837 else
3838 {
3839 /*
3840 * Get a character from the line itself.
3841 */
3842 c = *ptr;
3843#ifdef FEAT_MBYTE
3844 if (has_mbyte)
3845 {
3846 mb_c = c;
3847 if (enc_utf8)
3848 {
3849 /* If the UTF-8 character is more than one byte: Decode it
3850 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003851 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 mb_utf8 = FALSE;
3853 if (mb_l > 1)
3854 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003855 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 /* Overlong encoded ASCII or ASCII with composing char
3857 * is displayed normally, except a NUL. */
3858 if (mb_c < 0x80)
3859 c = mb_c;
3860 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003861
3862 /* At start of the line we can have a composing char.
3863 * Draw it as a space with a composing char. */
3864 if (utf_iscomposing(mb_c))
3865 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003866 int i;
3867
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003868 for (i = Screen_mco - 1; i > 0; --i)
3869 u8cc[i] = u8cc[i - 1];
3870 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003871 mb_c = ' ';
3872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874
3875 if ((mb_l == 1 && c >= 0x80)
3876 || (mb_l >= 1 && mb_c == 0)
3877 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003878# ifdef UNICODE16
3879 || mb_c >= 0x10000
3880# endif
3881 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 /*
3884 * Illegal UTF-8 byte: display as <xx>.
3885 * Non-BMP character : display as ? or fullwidth ?.
3886 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003887# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
3891 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003892# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (wp->w_p_rl) /* reverse */
3894 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003897# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else if (utf_char2cells(mb_c) != 2)
3899 STRCPY(extra, "?");
3900 else
3901 /* 0xff1f in UTF-8: full-width '?' */
3902 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905 p_extra = extra;
3906 c = *p_extra;
3907 mb_c = mb_ptr2char_adv(&p_extra);
3908 mb_utf8 = (c >= 0x80);
3909 n_extra = (int)STRLEN(p_extra);
3910 c_extra = NUL;
3911 if (area_attr == 0 && search_attr == 0)
3912 {
3913 n_attr = n_extra + 1;
3914 extra_attr = hl_attr(HLF_8);
3915 saved_attr2 = char_attr; /* save current attr */
3916 }
3917 }
3918 else if (mb_l == 0) /* at the NUL at end-of-line */
3919 mb_l = 1;
3920#ifdef FEAT_ARABIC
3921 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3922 {
3923 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003924 int pc, pc1, nc;
3925 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 /* The idea of what is the previous and next
3928 * character depends on 'rightleft'. */
3929 if (wp->w_p_rl)
3930 {
3931 pc = prev_c;
3932 pc1 = prev_c1;
3933 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003934 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936 else
3937 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003938 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003940 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942 prev_c = mb_c;
3943
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003944 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 else
3947 prev_c = mb_c;
3948#endif
3949 }
3950 else /* enc_dbcs */
3951 {
3952 mb_l = MB_BYTE2LEN(c);
3953 if (mb_l == 0) /* at the NUL at end-of-line */
3954 mb_l = 1;
3955 else if (mb_l > 1)
3956 {
3957 /* We assume a second byte below 32 is illegal.
3958 * Hopefully this is OK for all double-byte encodings!
3959 */
3960 if (ptr[1] >= 32)
3961 mb_c = (c << 8) + ptr[1];
3962 else
3963 {
3964 if (ptr[1] == NUL)
3965 {
3966 /* head byte at end of line */
3967 mb_l = 1;
3968 transchar_nonprint(extra, c);
3969 }
3970 else
3971 {
3972 /* illegal tail byte */
3973 mb_l = 2;
3974 STRCPY(extra, "XX");
3975 }
3976 p_extra = extra;
3977 n_extra = (int)STRLEN(extra) - 1;
3978 c_extra = NUL;
3979 c = *p_extra++;
3980 if (area_attr == 0 && search_attr == 0)
3981 {
3982 n_attr = n_extra + 1;
3983 extra_attr = hl_attr(HLF_8);
3984 saved_attr2 = char_attr; /* save current attr */
3985 }
3986 mb_c = c;
3987 }
3988 }
3989 }
3990 /* If a double-width char doesn't fit display a '>' in the
3991 * last column; the character is displayed at the start of the
3992 * next line. */
3993 if ((
3994# ifdef FEAT_RIGHTLEFT
3995 wp->w_p_rl ? (col <= 0) :
3996# endif
3997 (col >= W_WIDTH(wp) - 1))
3998 && (*mb_char2cells)(mb_c) == 2)
3999 {
4000 c = '>';
4001 mb_c = c;
4002 mb_utf8 = FALSE;
4003 mb_l = 1;
4004 multi_attr = hl_attr(HLF_AT);
4005 /* Put pointer back so that the character will be
4006 * displayed at the start of the next line. */
4007 --ptr;
4008 }
4009 else if (*ptr != NUL)
4010 ptr += mb_l - 1;
4011
4012 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004013 * a '<' in the first column. Don't do this for unprintable
4014 * charactes. */
4015 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004018 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 c = ' ';
4020 if (area_attr == 0 && search_attr == 0)
4021 {
4022 n_attr = n_extra + 1;
4023 extra_attr = hl_attr(HLF_AT);
4024 saved_attr2 = char_attr; /* save current attr */
4025 }
4026 mb_c = c;
4027 mb_utf8 = FALSE;
4028 mb_l = 1;
4029 }
4030
4031 }
4032#endif
4033 ++ptr;
4034
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004035 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004036 if (wp->w_p_list && (c == 160
4037#ifdef FEAT_MBYTE
4038 || (mb_utf8 && mb_c == 160)
4039#endif
4040 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004041 {
4042 c = lcs_nbsp;
4043 if (area_attr == 0 && search_attr == 0)
4044 {
4045 n_attr = 1;
4046 extra_attr = hl_attr(HLF_8);
4047 saved_attr2 = char_attr; /* save current attr */
4048 }
4049#ifdef FEAT_MBYTE
4050 mb_c = c;
4051 if (enc_utf8 && (*mb_char2len)(c) > 1)
4052 {
4053 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004054 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004055 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004056 }
4057 else
4058 mb_utf8 = FALSE;
4059#endif
4060 }
4061
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (extra_check)
4063 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004064#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004065 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004066#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004067
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004068#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 /* Get syntax attribute, unless still at the start of the line
4070 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004071 v = (long)(ptr - line);
4072 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
4074 /* Get the syntax attribute for the character. If there
4075 * is an error, disable syntax highlighting. */
4076 save_did_emsg = did_emsg;
4077 did_emsg = FALSE;
4078
Bram Moolenaar217ad922005-03-20 22:37:15 +00004079 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004080# ifdef FEAT_SPELL
4081 has_spell ? &can_spell :
4082# endif
4083 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004086 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004087 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004088 has_syntax = FALSE;
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 else
4091 did_emsg = save_did_emsg;
4092
4093 /* Need to get the line again, a multi-line regexp may
4094 * have made it invalid. */
4095 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4096 ptr = line + v;
4097
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004098 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004100 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004101 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004102# ifdef FEAT_CONCEAL
4103 /* no concealing past the end of the line, it interferes
4104 * with line highlighting */
4105 if (c == NUL)
4106 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004107 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004108 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004111#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004112
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004113#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004114 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004115 * Only do this when there is no syntax highlighting, the
4116 * @Spell cluster is not used or the current syntax item
4117 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004118 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004119 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004120 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004121# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004122 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004123 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004124# endif
4125 if (c != 0 && (
4126# ifdef FEAT_SYN_HL
4127 !has_syntax ||
4128# endif
4129 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004130 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004131 char_u *prev_ptr, *p;
4132 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004133 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004134# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004135 if (has_mbyte)
4136 {
4137 prev_ptr = ptr - mb_l;
4138 v -= mb_l - 1;
4139 }
4140 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004141# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004142 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004143
4144 /* Use nextline[] if possible, it has the start of the
4145 * next line concatenated. */
4146 if ((prev_ptr - line) - nextlinecol >= 0)
4147 p = nextline + (prev_ptr - line) - nextlinecol;
4148 else
4149 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004150 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004151 len = spell_check(wp, p, &spell_hlf, &cap_col,
4152 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004153 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004154
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004155 /* In Insert mode only highlight a word that
4156 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004157 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004158 && (State & INSERT) != 0
4159 && wp->w_cursor.lnum == lnum
4160 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004161 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004162 && wp->w_cursor.col < (colnr_T)word_end)
4163 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004164 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004165 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004166 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004167
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004168 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004169 && (p - nextline) + len > nextline_idx)
4170 {
4171 /* Remember that the good word continues at the
4172 * start of the next line. */
4173 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004174 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004175 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004176
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004177 /* Turn index into actual attributes. */
4178 if (spell_hlf != HLF_COUNT)
4179 spell_attr = highlight_attr[spell_hlf];
4180
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004181 if (cap_col > 0)
4182 {
4183 if (p != prev_ptr
4184 && (p - nextline) + cap_col >= nextline_idx)
4185 {
4186 /* Remember that the word in the next line
4187 * must start with a capital. */
4188 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004189 cap_col = (int)((p - nextline) + cap_col
4190 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004191 }
4192 else
4193 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004194 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004195 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004196 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004197 }
4198 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004199 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004200 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004201 char_attr = hl_combine_attr(char_attr, spell_attr);
4202 else
4203 char_attr = hl_combine_attr(spell_attr, char_attr);
4204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205#endif
4206#ifdef FEAT_LINEBREAK
4207 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004208 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 */
4210 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004211 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
4213 n_extra = win_lbr_chartabsize(wp, ptr - (
4214# ifdef FEAT_MBYTE
4215 has_mbyte ? mb_l :
4216# endif
4217 1), (colnr_T)vcol, NULL) - 1;
4218 c_extra = ' ';
4219 if (vim_iswhite(c))
4220 c = ' ';
4221 }
4222#endif
4223
4224 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4225 {
4226 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004227 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 n_attr = 1;
4230 extra_attr = hl_attr(HLF_8);
4231 saved_attr2 = char_attr; /* save current attr */
4232 }
4233#ifdef FEAT_MBYTE
4234 mb_c = c;
4235 if (enc_utf8 && (*mb_char2len)(c) > 1)
4236 {
4237 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004238 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004239 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 else
4242 mb_utf8 = FALSE;
4243#endif
4244 }
4245 }
4246
4247 /*
4248 * Handling of non-printable characters.
4249 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004250 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 /*
4253 * when getting a character from the file, we may have to
4254 * turn it into something else on the way to putting it
4255 * into "ScreenLines".
4256 */
4257 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4258 {
4259 /* tab amount depends on current column */
4260 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar8a20b0f2011-08-10 14:32:39 +02004261 - VCOL_HLC % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#ifdef FEAT_MBYTE
4263 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4264#endif
4265 if (wp->w_p_list)
4266 {
4267 c = lcs_tab1;
4268 c_extra = lcs_tab2;
4269 n_attr = n_extra + 1;
4270 extra_attr = hl_attr(HLF_8);
4271 saved_attr2 = char_attr; /* save current attr */
4272#ifdef FEAT_MBYTE
4273 mb_c = c;
4274 if (enc_utf8 && (*mb_char2len)(c) > 1)
4275 {
4276 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004277 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004278 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280#endif
4281 }
4282 else
4283 {
4284 c_extra = ' ';
4285 c = ' ';
4286 }
4287 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004288 else if (c == NUL
4289 && ((wp->w_p_list && lcs_eol > 0)
4290 || ((fromcol >= 0 || fromcol_prev >= 0)
4291 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004292#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004293 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004294#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004295 && (
4296# ifdef FEAT_RIGHTLEFT
4297 wp->w_p_rl ? (col >= 0) :
4298# endif
4299 (col < W_WIDTH(wp)))
4300 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004301 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004302 && (colnr_T)vcol == wp->w_virtcol)))
4303 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004305 /* Display a '$' after the line or highlight an extra
4306 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4308 /* For a diff line the highlighting continues after the
4309 * "$". */
4310 if (
4311# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004312 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313# ifdef LINE_ATTR
4314 &&
4315# endif
4316# endif
4317# ifdef LINE_ATTR
4318 line_attr == 0
4319# endif
4320 )
4321#endif
4322 {
4323#ifdef FEAT_VIRTUALEDIT
4324 /* In virtualedit, visual selections may extend
4325 * beyond end of line. */
4326 if (area_highlighting && virtual_active()
4327 && tocol != MAXCOL && vcol < tocol)
4328 n_extra = 0;
4329 else
4330#endif
4331 {
4332 p_extra = at_end_str;
4333 n_extra = 1;
4334 c_extra = NUL;
4335 }
4336 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004337 if (wp->w_p_list)
4338 c = lcs_eol;
4339 else
4340 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 lcs_eol_one = -1;
4342 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004343 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
4345 extra_attr = hl_attr(HLF_AT);
4346 n_attr = 1;
4347 }
4348#ifdef FEAT_MBYTE
4349 mb_c = c;
4350 if (enc_utf8 && (*mb_char2len)(c) > 1)
4351 {
4352 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004353 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004354 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 else
4357 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4358#endif
4359 }
4360 else if (c != NUL)
4361 {
4362 p_extra = transchar(c);
4363#ifdef FEAT_RIGHTLEFT
4364 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4365 rl_mirror(p_extra); /* reverse "<12>" */
4366#endif
4367 n_extra = byte2cells(c) - 1;
4368 c_extra = NUL;
4369 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004370 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
4372 n_attr = n_extra + 1;
4373 extra_attr = hl_attr(HLF_8);
4374 saved_attr2 = char_attr; /* save current attr */
4375 }
4376#ifdef FEAT_MBYTE
4377 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4378#endif
4379 }
4380#ifdef FEAT_VIRTUALEDIT
4381 else if (VIsual_active
4382 && (VIsual_mode == Ctrl_V
4383 || VIsual_mode == 'v')
4384 && virtual_active()
4385 && tocol != MAXCOL
4386 && vcol < tocol
4387 && (
4388# ifdef FEAT_RIGHTLEFT
4389 wp->w_p_rl ? (col >= 0) :
4390# endif
4391 (col < W_WIDTH(wp))))
4392 {
4393 c = ' ';
4394 --ptr; /* put it back at the NUL */
4395 }
4396#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004397#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else if ((
4399# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004400 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 ) && (
4404# ifdef FEAT_RIGHTLEFT
4405 wp->w_p_rl ? (col >= 0) :
4406# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004407 (col
4408# ifdef FEAT_CONCEAL
4409 - boguscols
4410# endif
4411 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
4413 /* Highlight until the right side of the window */
4414 c = ' ';
4415 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004416
4417 /* Remember we do the char for line highlighting. */
4418 ++did_line_attr;
4419
4420 /* don't do search HL for the rest of the line */
4421 if (line_attr != 0 && char_attr == search_attr && col > 0)
4422 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423# ifdef FEAT_DIFF
4424 if (diff_hlf == HLF_TXD)
4425 {
4426 diff_hlf = HLF_CHD;
4427 if (attr == 0 || char_attr != attr)
4428 char_attr = hl_attr(diff_hlf);
4429 }
4430# endif
4431 }
4432#endif
4433 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004434
4435#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004436 if ( wp->w_p_cole > 0
4437 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4438 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004439 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004440 && !(lnum_in_visual_area
4441 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004442 {
4443 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004444 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004445 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4446 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004447 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004448 /* First time at this concealed item: display one
4449 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004450 if (syn_get_sub_char() != NUL)
4451 c = syn_get_sub_char();
4452 else if (lcs_conceal != NUL)
4453 c = lcs_conceal;
4454 else
4455 c = ' ';
4456
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004457 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004458
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004459 if (n_extra > 0)
4460 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004461 vcol += n_extra;
4462 if (wp->w_p_wrap && n_extra > 0)
4463 {
4464# ifdef FEAT_RIGHTLEFT
4465 if (wp->w_p_rl)
4466 {
4467 col -= n_extra;
4468 boguscols -= n_extra;
4469 }
4470 else
4471# endif
4472 {
4473 boguscols += n_extra;
4474 col += n_extra;
4475 }
4476 }
4477 n_extra = 0;
4478 n_attr = 0;
4479 }
4480 else if (n_skip == 0)
4481 {
4482 is_concealing = TRUE;
4483 n_skip = 1;
4484 }
4485# ifdef FEAT_MBYTE
4486 mb_c = c;
4487 if (enc_utf8 && (*mb_char2len)(c) > 1)
4488 {
4489 mb_utf8 = TRUE;
4490 u8cc[0] = 0;
4491 c = 0xc0;
4492 }
4493 else
4494 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4495# endif
4496 }
4497 else
4498 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004499 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004500 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004501 }
4502#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004505#ifdef FEAT_CONCEAL
4506 /* In the cursor line and we may be concealing characters: correct
4507 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004508 if (!did_wcol && draw_state == WL_LINE
4509 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004510 && conceal_cursor_line(wp)
4511 && (int)wp->w_virtcol <= vcol + n_skip)
4512 {
4513 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004514 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004515 did_wcol = TRUE;
4516 }
4517#endif
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /* Don't override visual selection highlighting. */
4520 if (n_attr > 0
4521 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004522 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 char_attr = extra_attr;
4524
Bram Moolenaar81695252004-12-29 20:58:21 +00004525#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 /* XIM don't send preedit_start and preedit_end, but they send
4527 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4528 * im_is_preediting() here. */
4529 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004530 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 && (State & INSERT)
4532 && !p_imdisable
4533 && im_is_preediting()
4534 && draw_state == WL_LINE)
4535 {
4536 colnr_T tcol;
4537
4538 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004539 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 else
4541 tcol = preedit_end_col;
4542 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4543 {
4544 if (feedback_old_attr < 0)
4545 {
4546 feedback_col = 0;
4547 feedback_old_attr = char_attr;
4548 }
4549 char_attr = im_get_feedback_attr(feedback_col);
4550 if (char_attr < 0)
4551 char_attr = feedback_old_attr;
4552 feedback_col++;
4553 }
4554 else if (feedback_old_attr >= 0)
4555 {
4556 char_attr = feedback_old_attr;
4557 feedback_old_attr = -1;
4558 feedback_col = 0;
4559 }
4560 }
4561#endif
4562 /*
4563 * Handle the case where we are in column 0 but not on the first
4564 * character of the line and the user wants us to show us a
4565 * special character (via 'listchars' option "precedes:<char>".
4566 */
4567 if (lcs_prec_todo != NUL
4568 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4569#ifdef FEAT_DIFF
4570 && filler_todo <= 0
4571#endif
4572 && draw_state > WL_NR
4573 && c != NUL)
4574 {
4575 c = lcs_prec;
4576 lcs_prec_todo = NUL;
4577#ifdef FEAT_MBYTE
4578 mb_c = c;
4579 if (enc_utf8 && (*mb_char2len)(c) > 1)
4580 {
4581 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004582 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004583 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 else
4586 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4587#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004588 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {
4590 saved_attr3 = char_attr; /* save current attr */
4591 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4592 n_attr3 = 1;
4593 }
4594 }
4595
4596 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004597 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004599 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004600#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004601 || did_line_attr == 1
4602#endif
4603 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004605#ifdef FEAT_SEARCH_EXTRA
4606 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004607
4608 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004609 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004610 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004611#endif
4612
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004613 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 * highlight match at end of line. If it's beyond the last
4615 * char on the screen, just overwrite that one (tricky!) Not
4616 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004617#ifdef FEAT_SEARCH_EXTRA
4618 prevcol_hl_flag = FALSE;
4619 if (prevcol == (long)search_hl.startcol)
4620 prevcol_hl_flag = TRUE;
4621 else
4622 {
4623 cur = wp->w_match_head;
4624 while (cur != NULL)
4625 {
4626 if (prevcol == (long)cur->hl.startcol)
4627 {
4628 prevcol_hl_flag = TRUE;
4629 break;
4630 }
4631 cur = cur->next;
4632 }
4633 }
4634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004636 && ((area_attr != 0 && vcol == fromcol
4637#ifdef FEAT_VISUAL
4638 && (VIsual_mode != Ctrl_V
4639 || lnum == VIsual.lnum
4640 || lnum == curwin->w_cursor.lnum)
4641#endif
4642 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#ifdef FEAT_SEARCH_EXTRA
4644 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004645 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004646# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004647 && did_line_attr <= 1
4648# endif
4649 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650#endif
4651 ))
4652 {
4653 int n = 0;
4654
4655#ifdef FEAT_RIGHTLEFT
4656 if (wp->w_p_rl)
4657 {
4658 if (col < 0)
4659 n = 1;
4660 }
4661 else
4662#endif
4663 {
4664 if (col >= W_WIDTH(wp))
4665 n = -1;
4666 }
4667 if (n != 0)
4668 {
4669 /* At the window boundary, highlight the last character
4670 * instead (better than nothing). */
4671 off += n;
4672 col += n;
4673 }
4674 else
4675 {
4676 /* Add a blank character to highlight. */
4677 ScreenLines[off] = ' ';
4678#ifdef FEAT_MBYTE
4679 if (enc_utf8)
4680 ScreenLinesUC[off] = 0;
4681#endif
4682 }
4683#ifdef FEAT_SEARCH_EXTRA
4684 if (area_attr == 0)
4685 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004686 /* Use attributes from match with highest priority among
4687 * 'search_hl' and the match list. */
4688 char_attr = search_hl.attr;
4689 cur = wp->w_match_head;
4690 shl_flag = FALSE;
4691 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004692 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004693 if (shl_flag == FALSE
4694 && ((cur != NULL
4695 && cur->priority > SEARCH_HL_PRIORITY)
4696 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004697 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004698 shl = &search_hl;
4699 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004700 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004701 else
4702 shl = &cur->hl;
4703 if ((ptr - line) - 1 == (long)shl->startcol)
4704 char_attr = shl->attr;
4705 if (shl != &search_hl && cur != NULL)
4706 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709#endif
4710 ScreenAttrs[off] = char_attr;
4711#ifdef FEAT_RIGHTLEFT
4712 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004715 --off;
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 else
4718#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004721 ++off;
4722 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004723 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004724#ifdef FEAT_SYN_HL
4725 eol_hl_off = 1;
4726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
Bram Moolenaar91170f82006-05-05 21:15:17 +00004730 /*
4731 * At end of the text line.
4732 */
4733 if (c == NUL)
4734 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004735#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004736 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4737 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004738 {
4739 /* highlight last char after line */
4740 --col;
4741 --off;
4742 --vcol;
4743 }
4744
Bram Moolenaar1a384422010-07-14 19:53:30 +02004745 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004746 if (wp->w_p_wrap)
4747 v = wp->w_skipcol;
4748 else
4749 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004750
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004751 /* check if line ends before left margin */
4752 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004753 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004754#ifdef FEAT_CONCEAL
4755 /* Get rid of the boguscols now, we want to draw until the right
4756 * edge for 'cursorcolumn'. */
4757 col -= boguscols;
4758 boguscols = 0;
4759#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004760
4761 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004762 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004763
4764 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004765 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4766 && (int)wp->w_virtcol <
4767 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004768 && lnum != wp->w_cursor.lnum)
4769 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004770# ifdef FEAT_RIGHTLEFT
4771 && !wp->w_p_rl
4772# endif
4773 )
4774 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004775 int rightmost_vcol = 0;
4776 int i;
4777
4778 if (wp->w_p_cuc)
4779 rightmost_vcol = wp->w_virtcol;
4780 if (draw_color_col)
4781 /* determine rightmost colorcolumn to possibly draw */
4782 for (i = 0; color_cols[i] >= 0; ++i)
4783 if (rightmost_vcol < color_cols[i])
4784 rightmost_vcol = color_cols[i];
4785
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004786 while (col < W_WIDTH(wp))
4787 {
4788 ScreenLines[off] = ' ';
4789#ifdef FEAT_MBYTE
4790 if (enc_utf8)
4791 ScreenLinesUC[off] = 0;
4792#endif
4793 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004794 if (draw_color_col)
4795 draw_color_col = advance_color_col(VCOL_HLC,
4796 &color_cols);
4797
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004798 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004799 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004800 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004801 ScreenAttrs[off++] = hl_attr(HLF_MC);
4802 else
4803 ScreenAttrs[off++] = 0;
4804
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004805 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004806 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004807
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004808 ++vcol;
4809 }
4810 }
4811#endif
4812
Bram Moolenaar860cae12010-06-05 23:22:07 +02004813 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4814 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 row++;
4816
4817 /*
4818 * Update w_cline_height and w_cline_folded if the cursor line was
4819 * updated (saves a call to plines() later).
4820 */
4821 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4822 {
4823 curwin->w_cline_row = startrow;
4824 curwin->w_cline_height = row - startrow;
4825#ifdef FEAT_FOLDING
4826 curwin->w_cline_folded = FALSE;
4827#endif
4828 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4829 }
4830
4831 break;
4832 }
4833
4834 /* line continues beyond line end */
4835 if (lcs_ext
4836 && !wp->w_p_wrap
4837#ifdef FEAT_DIFF
4838 && filler_todo <= 0
4839#endif
4840 && (
4841#ifdef FEAT_RIGHTLEFT
4842 wp->w_p_rl ? col == 0 :
4843#endif
4844 col == W_WIDTH(wp) - 1)
4845 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004846 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4848 {
4849 c = lcs_ext;
4850 char_attr = hl_attr(HLF_AT);
4851#ifdef FEAT_MBYTE
4852 mb_c = c;
4853 if (enc_utf8 && (*mb_char2len)(c) > 1)
4854 {
4855 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004856 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004857 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
4859 else
4860 mb_utf8 = FALSE;
4861#endif
4862 }
4863
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004864#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004865 /* advance to the next 'colorcolumn' */
4866 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004867 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004868
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004869 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004870 * highlight the cursor position itself.
4871 * Also highlight the 'colorcolumn' if it is different than
4872 * 'cursorcolumn' */
4873 vcol_save_attr = -1;
4874 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004875 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004876 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004877 && lnum != wp->w_cursor.lnum)
4878 {
4879 vcol_save_attr = char_attr;
4880 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4881 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004882 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004883 {
4884 vcol_save_attr = char_attr;
4885 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4886 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004887 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004888#endif
4889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 /*
4891 * Store character to be displayed.
4892 * Skip characters that are left of the screen for 'nowrap'.
4893 */
4894 vcol_prev = vcol;
4895 if (draw_state < WL_LINE || n_skip <= 0)
4896 {
4897 /*
4898 * Store the character.
4899 */
4900#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4901 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4902 {
4903 /* A double-wide character is: put first halve in left cell. */
4904 --off;
4905 --col;
4906 }
4907#endif
4908 ScreenLines[off] = c;
4909#ifdef FEAT_MBYTE
4910 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004911 {
4912 if ((mb_c & 0xff00) == 0x8e00)
4913 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else if (enc_utf8)
4917 {
4918 if (mb_utf8)
4919 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004920 int i;
4921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004923 if ((c & 0xff) == 0)
4924 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004925 for (i = 0; i < Screen_mco; ++i)
4926 {
4927 ScreenLinesC[i][off] = u8cc[i];
4928 if (u8cc[i] == 0)
4929 break;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 else
4933 ScreenLinesUC[off] = 0;
4934 }
4935 if (multi_attr)
4936 {
4937 ScreenAttrs[off] = multi_attr;
4938 multi_attr = 0;
4939 }
4940 else
4941#endif
4942 ScreenAttrs[off] = char_attr;
4943
4944#ifdef FEAT_MBYTE
4945 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4946 {
4947 /* Need to fill two screen columns. */
4948 ++off;
4949 ++col;
4950 if (enc_utf8)
4951 /* UTF-8: Put a 0 in the second screen char. */
4952 ScreenLines[off] = 0;
4953 else
4954 /* DBCS: Put second byte in the second screen char. */
4955 ScreenLines[off] = mb_c & 0xff;
4956 ++vcol;
4957 /* When "tocol" is halfway a character, set it to the end of
4958 * the character, otherwise highlighting won't stop. */
4959 if (tocol == vcol)
4960 ++tocol;
4961#ifdef FEAT_RIGHTLEFT
4962 if (wp->w_p_rl)
4963 {
4964 /* now it's time to backup one cell */
4965 --off;
4966 --col;
4967 }
4968#endif
4969 }
4970#endif
4971#ifdef FEAT_RIGHTLEFT
4972 if (wp->w_p_rl)
4973 {
4974 --off;
4975 --col;
4976 }
4977 else
4978#endif
4979 {
4980 ++off;
4981 ++col;
4982 }
4983 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004984#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004985 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004986 {
4987 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004988 ++vcol_off;
4989 if (n_extra > 0)
4990 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004991 if (wp->w_p_wrap)
4992 {
4993 /*
4994 * Special voodoo required if 'wrap' is on.
4995 *
4996 * Advance the column indicator to force the line
4997 * drawing to wrap early. This will make the line
4998 * take up the same screen space when parts are concealed,
4999 * so that cursor line computations aren't messed up.
5000 *
5001 * To avoid the fictitious advance of 'col' causing
5002 * trailing junk to be written out of the screen line
5003 * we are building, 'boguscols' keeps track of the number
5004 * of bad columns we have advanced.
5005 */
5006 if (n_extra > 0)
5007 {
5008 vcol += n_extra;
5009# ifdef FEAT_RIGHTLEFT
5010 if (wp->w_p_rl)
5011 {
5012 col -= n_extra;
5013 boguscols -= n_extra;
5014 }
5015 else
5016# endif
5017 {
5018 col += n_extra;
5019 boguscols += n_extra;
5020 }
5021 n_extra = 0;
5022 n_attr = 0;
5023 }
5024
5025
5026# ifdef FEAT_MBYTE
5027 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5028 {
5029 /* Need to fill two screen columns. */
5030# ifdef FEAT_RIGHTLEFT
5031 if (wp->w_p_rl)
5032 {
5033 --boguscols;
5034 --col;
5035 }
5036 else
5037# endif
5038 {
5039 ++boguscols;
5040 ++col;
5041 }
5042 }
5043# endif
5044
5045# ifdef FEAT_RIGHTLEFT
5046 if (wp->w_p_rl)
5047 {
5048 --boguscols;
5049 --col;
5050 }
5051 else
5052# endif
5053 {
5054 ++boguscols;
5055 ++col;
5056 }
5057 }
5058 else
5059 {
5060 if (n_extra > 0)
5061 {
5062 vcol += n_extra;
5063 n_extra = 0;
5064 n_attr = 0;
5065 }
5066 }
5067
5068 }
5069#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 else
5071 --n_skip;
5072
Bram Moolenaar64486672010-05-16 15:46:46 +02005073 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5074 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005075 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076#ifdef FEAT_DIFF
5077 && filler_todo <= 0
5078#endif
5079 )
5080 ++vcol;
5081
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005082#ifdef FEAT_SYN_HL
5083 if (vcol_save_attr >= 0)
5084 char_attr = vcol_save_attr;
5085#endif
5086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 /* restore attributes after "predeces" in 'listchars' */
5088 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5089 char_attr = saved_attr3;
5090
5091 /* restore attributes after last 'listchars' or 'number' char */
5092 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5093 char_attr = saved_attr2;
5094
5095 /*
5096 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005097 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 */
5099 if ((
5100#ifdef FEAT_RIGHTLEFT
5101 wp->w_p_rl ? (col < 0) :
5102#endif
5103 (col >= W_WIDTH(wp)))
5104 && (*ptr != NUL
5105#ifdef FEAT_DIFF
5106 || filler_todo > 0
5107#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005108 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5110 )
5111 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005112#ifdef FEAT_CONCEAL
5113 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5114 (int)W_WIDTH(wp), wp->w_p_rl);
5115 boguscols = 0;
5116#else
5117 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5118 (int)W_WIDTH(wp), wp->w_p_rl);
5119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 ++row;
5121 ++screen_row;
5122
5123 /* When not wrapping and finished diff lines, or when displayed
5124 * '$' and highlighting until last column, break here. */
5125 if ((!wp->w_p_wrap
5126#ifdef FEAT_DIFF
5127 && filler_todo <= 0
5128#endif
5129 ) || lcs_eol_one == -1)
5130 break;
5131
5132 /* When the window is too narrow draw all "@" lines. */
5133 if (draw_state != WL_LINE
5134#ifdef FEAT_DIFF
5135 && filler_todo <= 0
5136#endif
5137 )
5138 {
5139 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5140#ifdef FEAT_VERTSPLIT
5141 draw_vsep_win(wp, row);
5142#endif
5143 row = endrow;
5144 }
5145
5146 /* When line got too long for screen break here. */
5147 if (row == endrow)
5148 {
5149 ++row;
5150 break;
5151 }
5152
5153 if (screen_cur_row == screen_row - 1
5154#ifdef FEAT_DIFF
5155 && filler_todo <= 0
5156#endif
5157 && W_WIDTH(wp) == Columns)
5158 {
5159 /* Remember that the line wraps, used for modeless copy. */
5160 LineWraps[screen_row - 1] = TRUE;
5161
5162 /*
5163 * Special trick to make copy/paste of wrapped lines work with
5164 * xterm/screen: write an extra character beyond the end of
5165 * the line. This will work with all terminal types
5166 * (regardless of the xn,am settings).
5167 * Only do this on a fast tty.
5168 * Only do this if the cursor is on the current line
5169 * (something has been written in it).
5170 * Don't do this for the GUI.
5171 * Don't do this for double-width characters.
5172 * Don't do this for a window not at the right screen border.
5173 */
5174 if (p_tf
5175#ifdef FEAT_GUI
5176 && !gui.in_use
5177#endif
5178#ifdef FEAT_MBYTE
5179 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005180 && ((*mb_off2cells)(LineOffset[screen_row],
5181 LineOffset[screen_row] + screen_Columns)
5182 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005184 + (int)Columns - 2,
5185 LineOffset[screen_row] + screen_Columns)
5186 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187#endif
5188 )
5189 {
5190 /* First make sure we are at the end of the screen line,
5191 * then output the same character again to let the
5192 * terminal know about the wrap. If the terminal doesn't
5193 * auto-wrap, we overwrite the character. */
5194 if (screen_cur_col != W_WIDTH(wp))
5195 screen_char(LineOffset[screen_row - 1]
5196 + (unsigned)Columns - 1,
5197 screen_row - 1, (int)(Columns - 1));
5198
5199#ifdef FEAT_MBYTE
5200 /* When there is a multi-byte character, just output a
5201 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005202 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5203 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 out_char(' ');
5205 else
5206#endif
5207 out_char(ScreenLines[LineOffset[screen_row - 1]
5208 + (Columns - 1)]);
5209 /* force a redraw of the first char on the next line */
5210 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5211 screen_start(); /* don't know where cursor is now */
5212 }
5213 }
5214
5215 col = 0;
5216 off = (unsigned)(current_ScreenLine - ScreenLines);
5217#ifdef FEAT_RIGHTLEFT
5218 if (wp->w_p_rl)
5219 {
5220 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5221 off += col;
5222 }
5223#endif
5224
5225 /* reset the drawing state for the start of a wrapped line */
5226 draw_state = WL_START;
5227 saved_n_extra = n_extra;
5228 saved_p_extra = p_extra;
5229 saved_c_extra = c_extra;
5230 saved_char_attr = char_attr;
5231 n_extra = 0;
5232 lcs_prec_todo = lcs_prec;
5233#ifdef FEAT_LINEBREAK
5234# ifdef FEAT_DIFF
5235 if (filler_todo <= 0)
5236# endif
5237 need_showbreak = TRUE;
5238#endif
5239#ifdef FEAT_DIFF
5240 --filler_todo;
5241 /* When the filler lines are actually below the last line of the
5242 * file, don't draw the line itself, break here. */
5243 if (filler_todo == 0 && wp->w_botfill)
5244 break;
5245#endif
5246 }
5247
5248 } /* for every character in the line */
5249
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005250#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005251 /* After an empty line check first word for capital. */
5252 if (*skipwhite(line) == NUL)
5253 {
5254 capcol_lnum = lnum + 1;
5255 cap_col = 0;
5256 }
5257#endif
5258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return row;
5260}
5261
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005262#ifdef FEAT_MBYTE
5263static int comp_char_differs __ARGS((int, int));
5264
5265/*
5266 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005267 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005268 */
5269 static int
5270comp_char_differs(off_from, off_to)
5271 int off_from;
5272 int off_to;
5273{
5274 int i;
5275
5276 for (i = 0; i < Screen_mco; ++i)
5277 {
5278 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5279 return TRUE;
5280 if (ScreenLinesC[i][off_from] == 0)
5281 break;
5282 }
5283 return FALSE;
5284}
5285#endif
5286
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287/*
5288 * Check whether the given character needs redrawing:
5289 * - the (first byte of the) character is different
5290 * - the attributes are different
5291 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005292 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 */
5294 static int
5295char_needs_redraw(off_from, off_to, cols)
5296 int off_from;
5297 int off_to;
5298 int cols;
5299{
5300 if (cols > 0
5301 && ((ScreenLines[off_from] != ScreenLines[off_to]
5302 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5303
5304#ifdef FEAT_MBYTE
5305 || (enc_dbcs != 0
5306 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5307 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5308 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5309 : (cols > 1 && ScreenLines[off_from + 1]
5310 != ScreenLines[off_to + 1])))
5311 || (enc_utf8
5312 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5313 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005314 && comp_char_differs(off_from, off_to))
5315 || (cols > 1 && ScreenLines[off_from + 1]
5316 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#endif
5318 ))
5319 return TRUE;
5320 return FALSE;
5321}
5322
5323/*
5324 * Move one "cooked" screen line to the screen, but only the characters that
5325 * have actually changed. Handle insert/delete character.
5326 * "coloff" gives the first column on the screen for this line.
5327 * "endcol" gives the columns where valid characters are.
5328 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5329 * needs to be cleared, negative otherwise.
5330 * "rlflag" is TRUE in a rightleft window:
5331 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5332 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5333 */
5334 static void
5335screen_line(row, coloff, endcol, clear_width
5336#ifdef FEAT_RIGHTLEFT
5337 , rlflag
5338#endif
5339 )
5340 int row;
5341 int coloff;
5342 int endcol;
5343 int clear_width;
5344#ifdef FEAT_RIGHTLEFT
5345 int rlflag;
5346#endif
5347{
5348 unsigned off_from;
5349 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005350#ifdef FEAT_MBYTE
5351 unsigned max_off_from;
5352 unsigned max_off_to;
5353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 int col = 0;
5355#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5356 int hl;
5357#endif
5358 int force = FALSE; /* force update rest of the line */
5359 int redraw_this /* bool: does character need redraw? */
5360#ifdef FEAT_GUI
5361 = TRUE /* For GUI when while-loop empty */
5362#endif
5363 ;
5364 int redraw_next; /* redraw_this for next character */
5365#ifdef FEAT_MBYTE
5366 int clear_next = FALSE;
5367 int char_cells; /* 1: normal char */
5368 /* 2: occupies two display cells */
5369# define CHAR_CELLS char_cells
5370#else
5371# define CHAR_CELLS 1
5372#endif
5373
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005374 /* Check for illegal row and col, just in case. */
5375 if (row >= Rows)
5376 row = Rows - 1;
5377 if (endcol > Columns)
5378 endcol = Columns;
5379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380# ifdef FEAT_CLIPBOARD
5381 clip_may_clear_selection(row, row);
5382# endif
5383
5384 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5385 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005386#ifdef FEAT_MBYTE
5387 max_off_from = off_from + screen_Columns;
5388 max_off_to = LineOffset[row] + screen_Columns;
5389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391#ifdef FEAT_RIGHTLEFT
5392 if (rlflag)
5393 {
5394 /* Clear rest first, because it's left of the text. */
5395 if (clear_width > 0)
5396 {
5397 while (col <= endcol && ScreenLines[off_to] == ' '
5398 && ScreenAttrs[off_to] == 0
5399# ifdef FEAT_MBYTE
5400 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5401# endif
5402 )
5403 {
5404 ++off_to;
5405 ++col;
5406 }
5407 if (col <= endcol)
5408 screen_fill(row, row + 1, col + coloff,
5409 endcol + coloff + 1, ' ', ' ', 0);
5410 }
5411 col = endcol + 1;
5412 off_to = LineOffset[row] + col + coloff;
5413 off_from += col;
5414 endcol = (clear_width > 0 ? clear_width : -clear_width);
5415 }
5416#endif /* FEAT_RIGHTLEFT */
5417
5418 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5419
5420 while (col < endcol)
5421 {
5422#ifdef FEAT_MBYTE
5423 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005424 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 else
5426 char_cells = 1;
5427#endif
5428
5429 redraw_this = redraw_next;
5430 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5431 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5432
5433#ifdef FEAT_GUI
5434 /* If the next character was bold, then redraw the current character to
5435 * remove any pixels that might have spilt over into us. This only
5436 * happens in the GUI.
5437 */
5438 if (redraw_next && gui.in_use)
5439 {
5440 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005441 if (hl > HL_ALL)
5442 hl = syn_attr2attr(hl);
5443 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 redraw_this = TRUE;
5445 }
5446#endif
5447
5448 if (redraw_this)
5449 {
5450 /*
5451 * Special handling when 'xs' termcap flag set (hpterm):
5452 * Attributes for characters are stored at the position where the
5453 * cursor is when writing the highlighting code. The
5454 * start-highlighting code must be written with the cursor on the
5455 * first highlighted character. The stop-highlighting code must
5456 * be written with the cursor just after the last highlighted
5457 * character.
5458 * Overwriting a character doesn't remove it's highlighting. Need
5459 * to clear the rest of the line, and force redrawing it
5460 * completely.
5461 */
5462 if ( p_wiv
5463 && !force
5464#ifdef FEAT_GUI
5465 && !gui.in_use
5466#endif
5467 && ScreenAttrs[off_to] != 0
5468 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5469 {
5470 /*
5471 * Need to remove highlighting attributes here.
5472 */
5473 windgoto(row, col + coloff);
5474 out_str(T_CE); /* clear rest of this screen line */
5475 screen_start(); /* don't know where cursor is now */
5476 force = TRUE; /* force redraw of rest of the line */
5477 redraw_next = TRUE; /* or else next char would miss out */
5478
5479 /*
5480 * If the previous character was highlighted, need to stop
5481 * highlighting at this character.
5482 */
5483 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5484 {
5485 screen_attr = ScreenAttrs[off_to - 1];
5486 term_windgoto(row, col + coloff);
5487 screen_stop_highlight();
5488 }
5489 else
5490 screen_attr = 0; /* highlighting has stopped */
5491 }
5492#ifdef FEAT_MBYTE
5493 if (enc_dbcs != 0)
5494 {
5495 /* Check if overwriting a double-byte with a single-byte or
5496 * the other way around requires another character to be
5497 * redrawn. For UTF-8 this isn't needed, because comparing
5498 * ScreenLinesUC[] is sufficient. */
5499 if (char_cells == 1
5500 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005501 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 /* Writing a single-cell character over a double-cell
5504 * character: need to redraw the next cell. */
5505 ScreenLines[off_to + 1] = 0;
5506 redraw_next = TRUE;
5507 }
5508 else if (char_cells == 2
5509 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005510 && (*mb_off2cells)(off_to, max_off_to) == 1
5511 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
5513 /* Writing the second half of a double-cell character over
5514 * a double-cell character: need to redraw the second
5515 * cell. */
5516 ScreenLines[off_to + 2] = 0;
5517 redraw_next = TRUE;
5518 }
5519
5520 if (enc_dbcs == DBCS_JPNU)
5521 ScreenLines2[off_to] = ScreenLines2[off_from];
5522 }
5523 /* When writing a single-width character over a double-width
5524 * character and at the end of the redrawn text, need to clear out
5525 * the right halve of the old character.
5526 * Also required when writing the right halve of a double-width
5527 * char over the left halve of an existing one. */
5528 if (has_mbyte && col + char_cells == endcol
5529 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005530 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005532 && (*mb_off2cells)(off_to, max_off_to) == 1
5533 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 clear_next = TRUE;
5535#endif
5536
5537 ScreenLines[off_to] = ScreenLines[off_from];
5538#ifdef FEAT_MBYTE
5539 if (enc_utf8)
5540 {
5541 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5542 if (ScreenLinesUC[off_from] != 0)
5543 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005544 int i;
5545
5546 for (i = 0; i < Screen_mco; ++i)
5547 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549 }
5550 if (char_cells == 2)
5551 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5552#endif
5553
5554#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005555 /* The bold trick makes a single column of pixels appear in the
5556 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 * character should be redrawn too. This happens for our own GUI
5558 * and for some xterms. */
5559 if (
5560# ifdef FEAT_GUI
5561 gui.in_use
5562# endif
5563# if defined(FEAT_GUI) && defined(UNIX)
5564 ||
5565# endif
5566# ifdef UNIX
5567 term_is_xterm
5568# endif
5569 )
5570 {
5571 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005572 if (hl > HL_ALL)
5573 hl = syn_attr2attr(hl);
5574 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 redraw_next = TRUE;
5576 }
5577#endif
5578 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5579#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005580 /* For simplicity set the attributes of second half of a
5581 * double-wide character equal to the first half. */
5582 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005584
5585 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 else
5588#endif
5589 screen_char(off_to, row, col + coloff);
5590 }
5591 else if ( p_wiv
5592#ifdef FEAT_GUI
5593 && !gui.in_use
5594#endif
5595 && col + coloff > 0)
5596 {
5597 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5598 {
5599 /*
5600 * Don't output stop-highlight when moving the cursor, it will
5601 * stop the highlighting when it should continue.
5602 */
5603 screen_attr = 0;
5604 }
5605 else if (screen_attr != 0)
5606 screen_stop_highlight();
5607 }
5608
5609 off_to += CHAR_CELLS;
5610 off_from += CHAR_CELLS;
5611 col += CHAR_CELLS;
5612 }
5613
5614#ifdef FEAT_MBYTE
5615 if (clear_next)
5616 {
5617 /* Clear the second half of a double-wide character of which the left
5618 * half was overwritten with a single-wide character. */
5619 ScreenLines[off_to] = ' ';
5620 if (enc_utf8)
5621 ScreenLinesUC[off_to] = 0;
5622 screen_char(off_to, row, col + coloff);
5623 }
5624#endif
5625
5626 if (clear_width > 0
5627#ifdef FEAT_RIGHTLEFT
5628 && !rlflag
5629#endif
5630 )
5631 {
5632#ifdef FEAT_GUI
5633 int startCol = col;
5634#endif
5635
5636 /* blank out the rest of the line */
5637 while (col < clear_width && ScreenLines[off_to] == ' '
5638 && ScreenAttrs[off_to] == 0
5639#ifdef FEAT_MBYTE
5640 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5641#endif
5642 )
5643 {
5644 ++off_to;
5645 ++col;
5646 }
5647 if (col < clear_width)
5648 {
5649#ifdef FEAT_GUI
5650 /*
5651 * In the GUI, clearing the rest of the line may leave pixels
5652 * behind if the first character cleared was bold. Some bold
5653 * fonts spill over the left. In this case we redraw the previous
5654 * character too. If we didn't skip any blanks above, then we
5655 * only redraw if the character wasn't already redrawn anyway.
5656 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005657 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
5659 hl = ScreenAttrs[off_to];
5660 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005661 {
5662 int prev_cells = 1;
5663# ifdef FEAT_MBYTE
5664 if (enc_utf8)
5665 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5666 * that its width is 2. */
5667 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5668 else if (enc_dbcs != 0)
5669 {
5670 /* find previous character by counting from first
5671 * column and get its width. */
5672 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005673 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005674
5675 while (off < off_to)
5676 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005677 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005678 off += prev_cells;
5679 }
5680 }
5681
5682 if (enc_dbcs != 0 && prev_cells > 1)
5683 screen_char_2(off_to - prev_cells, row,
5684 col + coloff - prev_cells);
5685 else
5686# endif
5687 screen_char(off_to - prev_cells, row,
5688 col + coloff - prev_cells);
5689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691#endif
5692 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5693 ' ', ' ', 0);
5694#ifdef FEAT_VERTSPLIT
5695 off_to += clear_width - col;
5696 col = clear_width;
5697#endif
5698 }
5699 }
5700
5701 if (clear_width > 0)
5702 {
5703#ifdef FEAT_VERTSPLIT
5704 /* For a window that's left of another, draw the separator char. */
5705 if (col + coloff < Columns)
5706 {
5707 int c;
5708
5709 c = fillchar_vsep(&hl);
5710 if (ScreenLines[off_to] != c
5711# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005712 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5713 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714# endif
5715 || ScreenAttrs[off_to] != hl)
5716 {
5717 ScreenLines[off_to] = c;
5718 ScreenAttrs[off_to] = hl;
5719# ifdef FEAT_MBYTE
5720 if (enc_utf8)
5721 {
5722 if (c >= 0x80)
5723 {
5724 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005725 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727 else
5728 ScreenLinesUC[off_to] = 0;
5729 }
5730# endif
5731 screen_char(off_to, row, col + coloff);
5732 }
5733 }
5734 else
5735#endif
5736 LineWraps[row] = FALSE;
5737 }
5738}
5739
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005740#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005742 * Mirror text "str" for right-left displaying.
5743 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005745 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746rl_mirror(str)
5747 char_u *str;
5748{
5749 char_u *p1, *p2;
5750 int t;
5751
5752 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5753 {
5754 t = *p1;
5755 *p1 = *p2;
5756 *p2 = t;
5757 }
5758}
5759#endif
5760
5761#if defined(FEAT_WINDOWS) || defined(PROTO)
5762/*
5763 * mark all status lines for redraw; used after first :cd
5764 */
5765 void
5766status_redraw_all()
5767{
5768 win_T *wp;
5769
5770 for (wp = firstwin; wp; wp = wp->w_next)
5771 if (wp->w_status_height)
5772 {
5773 wp->w_redr_status = TRUE;
5774 redraw_later(VALID);
5775 }
5776}
5777
5778/*
5779 * mark all status lines of the current buffer for redraw
5780 */
5781 void
5782status_redraw_curbuf()
5783{
5784 win_T *wp;
5785
5786 for (wp = firstwin; wp; wp = wp->w_next)
5787 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5788 {
5789 wp->w_redr_status = TRUE;
5790 redraw_later(VALID);
5791 }
5792}
5793
5794/*
5795 * Redraw all status lines that need to be redrawn.
5796 */
5797 void
5798redraw_statuslines()
5799{
5800 win_T *wp;
5801
5802 for (wp = firstwin; wp; wp = wp->w_next)
5803 if (wp->w_redr_status)
5804 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005805 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005806 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807}
5808#endif
5809
5810#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5811/*
5812 * Redraw all status lines at the bottom of frame "frp".
5813 */
5814 void
5815win_redraw_last_status(frp)
5816 frame_T *frp;
5817{
5818 if (frp->fr_layout == FR_LEAF)
5819 frp->fr_win->w_redr_status = TRUE;
5820 else if (frp->fr_layout == FR_ROW)
5821 {
5822 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5823 win_redraw_last_status(frp);
5824 }
5825 else /* frp->fr_layout == FR_COL */
5826 {
5827 frp = frp->fr_child;
5828 while (frp->fr_next != NULL)
5829 frp = frp->fr_next;
5830 win_redraw_last_status(frp);
5831 }
5832}
5833#endif
5834
5835#ifdef FEAT_VERTSPLIT
5836/*
5837 * Draw the verticap separator right of window "wp" starting with line "row".
5838 */
5839 static void
5840draw_vsep_win(wp, row)
5841 win_T *wp;
5842 int row;
5843{
5844 int hl;
5845 int c;
5846
5847 if (wp->w_vsep_width)
5848 {
5849 /* draw the vertical separator right of this window */
5850 c = fillchar_vsep(&hl);
5851 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5852 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5853 c, ' ', hl);
5854 }
5855}
5856#endif
5857
5858#ifdef FEAT_WILDMENU
5859static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005860static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
5862/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005863 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 */
5865 static int
5866status_match_len(xp, s)
5867 expand_T *xp;
5868 char_u *s;
5869{
5870 int len = 0;
5871
5872#ifdef FEAT_MENU
5873 int emenu = (xp->xp_context == EXPAND_MENUS
5874 || xp->xp_context == EXPAND_MENUNAMES);
5875
5876 /* Check for menu separators - replace with '|'. */
5877 if (emenu && menu_is_separator(s))
5878 return 1;
5879#endif
5880
5881 while (*s != NUL)
5882 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005883 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005884 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005885 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
5887
5888 return len;
5889}
5890
5891/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005892 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005893 * These are backslashes used for escaping. Do show backslashes in help tags.
5894 */
5895 static int
5896skip_status_match_char(xp, s)
5897 expand_T *xp;
5898 char_u *s;
5899{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005900 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005901#ifdef FEAT_MENU
5902 || ((xp->xp_context == EXPAND_MENUS
5903 || xp->xp_context == EXPAND_MENUNAMES)
5904 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5905#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005906 )
5907 {
5908#ifndef BACKSLASH_IN_FILENAME
5909 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5910 return 2;
5911#endif
5912 return 1;
5913 }
5914 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005915}
5916
5917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 * Show wildchar matches in the status line.
5919 * Show at least the "match" item.
5920 * We start at item 'first_match' in the list and show all matches that fit.
5921 *
5922 * If inversion is possible we use it. Else '=' characters are used.
5923 */
5924 void
5925win_redr_status_matches(xp, num_matches, matches, match, showtail)
5926 expand_T *xp;
5927 int num_matches;
5928 char_u **matches; /* list of matches */
5929 int match;
5930 int showtail;
5931{
5932#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5933 int row;
5934 char_u *buf;
5935 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005936 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 int fillchar;
5938 int attr;
5939 int i;
5940 int highlight = TRUE;
5941 char_u *selstart = NULL;
5942 int selstart_col = 0;
5943 char_u *selend = NULL;
5944 static int first_match = 0;
5945 int add_left = FALSE;
5946 char_u *s;
5947#ifdef FEAT_MENU
5948 int emenu;
5949#endif
5950#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5951 int l;
5952#endif
5953
5954 if (matches == NULL) /* interrupted completion? */
5955 return;
5956
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005957#ifdef FEAT_MBYTE
5958 if (has_mbyte)
5959 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5960 else
5961#endif
5962 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 if (buf == NULL)
5964 return;
5965
5966 if (match == -1) /* don't show match but original text */
5967 {
5968 match = 0;
5969 highlight = FALSE;
5970 }
5971 /* count 1 for the ending ">" */
5972 clen = status_match_len(xp, L_MATCH(match)) + 3;
5973 if (match == 0)
5974 first_match = 0;
5975 else if (match < first_match)
5976 {
5977 /* jumping left, as far as we can go */
5978 first_match = match;
5979 add_left = TRUE;
5980 }
5981 else
5982 {
5983 /* check if match fits on the screen */
5984 for (i = first_match; i < match; ++i)
5985 clen += status_match_len(xp, L_MATCH(i)) + 2;
5986 if (first_match > 0)
5987 clen += 2;
5988 /* jumping right, put match at the left */
5989 if ((long)clen > Columns)
5990 {
5991 first_match = match;
5992 /* if showing the last match, we can add some on the left */
5993 clen = 2;
5994 for (i = match; i < num_matches; ++i)
5995 {
5996 clen += status_match_len(xp, L_MATCH(i)) + 2;
5997 if ((long)clen >= Columns)
5998 break;
5999 }
6000 if (i == num_matches)
6001 add_left = TRUE;
6002 }
6003 }
6004 if (add_left)
6005 while (first_match > 0)
6006 {
6007 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6008 if ((long)clen >= Columns)
6009 break;
6010 --first_match;
6011 }
6012
6013 fillchar = fillchar_status(&attr, TRUE);
6014
6015 if (first_match == 0)
6016 {
6017 *buf = NUL;
6018 len = 0;
6019 }
6020 else
6021 {
6022 STRCPY(buf, "< ");
6023 len = 2;
6024 }
6025 clen = len;
6026
6027 i = first_match;
6028 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6029 {
6030 if (i == match)
6031 {
6032 selstart = buf + len;
6033 selstart_col = clen;
6034 }
6035
6036 s = L_MATCH(i);
6037 /* Check for menu separators - replace with '|' */
6038#ifdef FEAT_MENU
6039 emenu = (xp->xp_context == EXPAND_MENUS
6040 || xp->xp_context == EXPAND_MENUNAMES);
6041 if (emenu && menu_is_separator(s))
6042 {
6043 STRCPY(buf + len, transchar('|'));
6044 l = (int)STRLEN(buf + len);
6045 len += l;
6046 clen += l;
6047 }
6048 else
6049#endif
6050 for ( ; *s != NUL; ++s)
6051 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006052 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 clen += ptr2cells(s);
6054#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006055 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 {
6057 STRNCPY(buf + len, s, l);
6058 s += l - 1;
6059 len += l;
6060 }
6061 else
6062#endif
6063 {
6064 STRCPY(buf + len, transchar_byte(*s));
6065 len += (int)STRLEN(buf + len);
6066 }
6067 }
6068 if (i == match)
6069 selend = buf + len;
6070
6071 *(buf + len++) = ' ';
6072 *(buf + len++) = ' ';
6073 clen += 2;
6074 if (++i == num_matches)
6075 break;
6076 }
6077
6078 if (i != num_matches)
6079 {
6080 *(buf + len++) = '>';
6081 ++clen;
6082 }
6083
6084 buf[len] = NUL;
6085
6086 row = cmdline_row - 1;
6087 if (row >= 0)
6088 {
6089 if (wild_menu_showing == 0)
6090 {
6091 if (msg_scrolled > 0)
6092 {
6093 /* Put the wildmenu just above the command line. If there is
6094 * no room, scroll the screen one line up. */
6095 if (cmdline_row == Rows - 1)
6096 {
6097 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6098 ++msg_scrolled;
6099 }
6100 else
6101 {
6102 ++cmdline_row;
6103 ++row;
6104 }
6105 wild_menu_showing = WM_SCROLLED;
6106 }
6107 else
6108 {
6109 /* Create status line if needed by setting 'laststatus' to 2.
6110 * Set 'winminheight' to zero to avoid that the window is
6111 * resized. */
6112 if (lastwin->w_status_height == 0)
6113 {
6114 save_p_ls = p_ls;
6115 save_p_wmh = p_wmh;
6116 p_ls = 2;
6117 p_wmh = 0;
6118 last_status(FALSE);
6119 }
6120 wild_menu_showing = WM_SHOWN;
6121 }
6122 }
6123
6124 screen_puts(buf, row, 0, attr);
6125 if (selstart != NULL && highlight)
6126 {
6127 *selend = NUL;
6128 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6129 }
6130
6131 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6132 }
6133
6134#ifdef FEAT_VERTSPLIT
6135 win_redraw_last_status(topframe);
6136#else
6137 lastwin->w_redr_status = TRUE;
6138#endif
6139 vim_free(buf);
6140}
6141#endif
6142
6143#if defined(FEAT_WINDOWS) || defined(PROTO)
6144/*
6145 * Redraw the status line of window wp.
6146 *
6147 * If inversion is possible we use it. Else '=' characters are used.
6148 */
6149 void
6150win_redr_status(wp)
6151 win_T *wp;
6152{
6153 int row;
6154 char_u *p;
6155 int len;
6156 int fillchar;
6157 int attr;
6158 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006159 static int busy = FALSE;
6160
6161 /* It's possible to get here recursively when 'statusline' (indirectly)
6162 * invokes ":redrawstatus". Simply ignore the call then. */
6163 if (busy)
6164 return;
6165 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166
6167 wp->w_redr_status = FALSE;
6168 if (wp->w_status_height == 0)
6169 {
6170 /* no status line, can only be last window */
6171 redraw_cmdline = TRUE;
6172 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006173 else if (!redrawing()
6174#ifdef FEAT_INS_EXPAND
6175 /* don't update status line when popup menu is visible and may be
6176 * drawn over it */
6177 || pum_visible()
6178#endif
6179 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {
6181 /* Don't redraw right now, do it later. */
6182 wp->w_redr_status = TRUE;
6183 }
6184#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006185 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
6187 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006188 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
6190#endif
6191 else
6192 {
6193 fillchar = fillchar_status(&attr, wp == curwin);
6194
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006195 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 p = NameBuff;
6197 len = (int)STRLEN(p);
6198
6199 if (wp->w_buffer->b_help
6200#ifdef FEAT_QUICKFIX
6201 || wp->w_p_pvw
6202#endif
6203 || bufIsChanged(wp->w_buffer)
6204 || wp->w_buffer->b_p_ro)
6205 *(p + len++) = ' ';
6206 if (wp->w_buffer->b_help)
6207 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006208 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 len += (int)STRLEN(p + len);
6210 }
6211#ifdef FEAT_QUICKFIX
6212 if (wp->w_p_pvw)
6213 {
6214 STRCPY(p + len, _("[Preview]"));
6215 len += (int)STRLEN(p + len);
6216 }
6217#endif
6218 if (bufIsChanged(wp->w_buffer))
6219 {
6220 STRCPY(p + len, "[+]");
6221 len += 3;
6222 }
6223 if (wp->w_buffer->b_p_ro)
6224 {
6225 STRCPY(p + len, "[RO]");
6226 len += 4;
6227 }
6228
6229#ifndef FEAT_VERTSPLIT
6230 this_ru_col = ru_col;
6231 if (this_ru_col < (Columns + 1) / 2)
6232 this_ru_col = (Columns + 1) / 2;
6233#else
6234 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6235 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6236 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6237 if (this_ru_col <= 1)
6238 {
6239 p = (char_u *)"<"; /* No room for file name! */
6240 len = 1;
6241 }
6242 else
6243#endif
6244#ifdef FEAT_MBYTE
6245 if (has_mbyte)
6246 {
6247 int clen = 0, i;
6248
6249 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006250 clen = mb_string2cells(p, -1);
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 /* Find first character that will fit.
6253 * Going from start to end is much faster for DBCS. */
6254 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006255 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 clen -= (*mb_ptr2cells)(p + i);
6257 len = clen;
6258 if (i > 0)
6259 {
6260 p = p + i - 1;
6261 *p = '<';
6262 ++len;
6263 }
6264
6265 }
6266 else
6267#endif
6268 if (len > this_ru_col - 1)
6269 {
6270 p += len - (this_ru_col - 1);
6271 *p = '<';
6272 len = this_ru_col - 1;
6273 }
6274
6275 row = W_WINROW(wp) + wp->w_height;
6276 screen_puts(p, row, W_WINCOL(wp), attr);
6277 screen_fill(row, row + 1, len + W_WINCOL(wp),
6278 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6279
6280 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6281 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6282 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6283 - 1 + W_WINCOL(wp)), attr);
6284
6285#ifdef FEAT_CMDL_INFO
6286 win_redr_ruler(wp, TRUE);
6287#endif
6288 }
6289
6290#ifdef FEAT_VERTSPLIT
6291 /*
6292 * May need to draw the character below the vertical separator.
6293 */
6294 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6295 {
6296 if (stl_connected(wp))
6297 fillchar = fillchar_status(&attr, wp == curwin);
6298 else
6299 fillchar = fillchar_vsep(&attr);
6300 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6301 attr);
6302 }
6303#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006304 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305}
6306
Bram Moolenaar238a5642006-02-21 22:12:05 +00006307#ifdef FEAT_STL_OPT
6308/*
6309 * Redraw the status line according to 'statusline' and take care of any
6310 * errors encountered.
6311 */
6312 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006313redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006314 win_T *wp;
6315{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006316 static int entered = FALSE;
6317 int save_called_emsg = called_emsg;
6318
6319 /* When called recursively return. This can happen when the statusline
6320 * contains an expression that triggers a redraw. */
6321 if (entered)
6322 return;
6323 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006324
6325 called_emsg = FALSE;
6326 win_redr_custom(wp, FALSE);
6327 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006328 {
6329 /* When there is an error disable the statusline, otherwise the
6330 * display is messed up with errors and a redraw triggers the problem
6331 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006332 set_string_option_direct((char_u *)"statusline", -1,
6333 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006334 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006335 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006336 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006337 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006338}
6339#endif
6340
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341# ifdef FEAT_VERTSPLIT
6342/*
6343 * Return TRUE if the status line of window "wp" is connected to the status
6344 * line of the window right of it. If not, then it's a vertical separator.
6345 * Only call if (wp->w_vsep_width != 0).
6346 */
6347 int
6348stl_connected(wp)
6349 win_T *wp;
6350{
6351 frame_T *fr;
6352
6353 fr = wp->w_frame;
6354 while (fr->fr_parent != NULL)
6355 {
6356 if (fr->fr_parent->fr_layout == FR_COL)
6357 {
6358 if (fr->fr_next != NULL)
6359 break;
6360 }
6361 else
6362 {
6363 if (fr->fr_next != NULL)
6364 return TRUE;
6365 }
6366 fr = fr->fr_parent;
6367 }
6368 return FALSE;
6369}
6370# endif
6371
6372#endif /* FEAT_WINDOWS */
6373
6374#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6375/*
6376 * Get the value to show for the language mappings, active 'keymap'.
6377 */
6378 int
6379get_keymap_str(wp, buf, len)
6380 win_T *wp;
6381 char_u *buf; /* buffer for the result */
6382 int len; /* length of buffer */
6383{
6384 char_u *p;
6385
6386 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6387 return FALSE;
6388
6389 {
6390#ifdef FEAT_EVAL
6391 buf_T *old_curbuf = curbuf;
6392 win_T *old_curwin = curwin;
6393 char_u *s;
6394
6395 curbuf = wp->w_buffer;
6396 curwin = wp;
6397 STRCPY(buf, "b:keymap_name"); /* must be writable */
6398 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006399 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 --emsg_skip;
6401 curbuf = old_curbuf;
6402 curwin = old_curwin;
6403 if (p == NULL || *p == NUL)
6404#endif
6405 {
6406#ifdef FEAT_KEYMAP
6407 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6408 p = wp->w_buffer->b_p_keymap;
6409 else
6410#endif
6411 p = (char_u *)"lang";
6412 }
6413 if ((int)(STRLEN(p) + 3) < len)
6414 sprintf((char *)buf, "<%s>", p);
6415 else
6416 buf[0] = NUL;
6417#ifdef FEAT_EVAL
6418 vim_free(s);
6419#endif
6420 }
6421 return buf[0] != NUL;
6422}
6423#endif
6424
6425#if defined(FEAT_STL_OPT) || defined(PROTO)
6426/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006427 * Redraw the status line or ruler of window "wp".
6428 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 */
6430 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006431win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006433 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
6435 int attr;
6436 int curattr;
6437 int row;
6438 int col = 0;
6439 int maxwidth;
6440 int width;
6441 int n;
6442 int len;
6443 int fillchar;
6444 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006445 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006447 struct stl_hlrec hltab[STL_MAX_ITEM];
6448 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006449 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006450 win_T *ewp;
6451 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006454 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006456 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006457 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006458 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006459 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006460 attr = hl_attr(HLF_TPF);
6461 maxwidth = Columns;
6462# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006463 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006466 else
6467 {
6468 row = W_WINROW(wp) + wp->w_height;
6469 fillchar = fillchar_status(&attr, wp == curwin);
6470 maxwidth = W_WIDTH(wp);
6471
6472 if (draw_ruler)
6473 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006474 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006475 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006476 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006477 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006478 if (*++stl == '-')
6479 stl++;
6480 if (atoi((char *)stl))
6481 while (VIM_ISDIGIT(*stl))
6482 stl++;
6483 if (*stl++ != '(')
6484 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006485 }
6486#ifdef FEAT_VERTSPLIT
6487 col = ru_col - (Columns - W_WIDTH(wp));
6488 if (col < (W_WIDTH(wp) + 1) / 2)
6489 col = (W_WIDTH(wp) + 1) / 2;
6490#else
6491 col = ru_col;
6492 if (col > (Columns + 1) / 2)
6493 col = (Columns + 1) / 2;
6494#endif
6495 maxwidth = W_WIDTH(wp) - col;
6496#ifdef FEAT_WINDOWS
6497 if (!wp->w_status_height)
6498#endif
6499 {
6500 row = Rows - 1;
6501 --maxwidth; /* writing in last column may cause scrolling */
6502 fillchar = ' ';
6503 attr = 0;
6504 }
6505
6506# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006507 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006508# endif
6509 }
6510 else
6511 {
6512 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006513 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006514 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006515 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006516# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006517 use_sandbox = was_set_insecurely((char_u *)"statusline",
6518 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006519# endif
6520 }
6521
6522#ifdef FEAT_VERTSPLIT
6523 col += W_WINCOL(wp);
6524#endif
6525 }
6526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (maxwidth <= 0)
6528 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
Bram Moolenaar61452852011-02-01 18:01:11 +01006530 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6531 * the cursor away and back. */
6532 ewp = wp == NULL ? curwin : wp;
6533 p_crb_save = ewp->w_p_crb;
6534 ewp->w_p_crb = FALSE;
6535
Bram Moolenaar362f3562009-11-03 16:20:34 +00006536 /* Make a copy, because the statusline may include a function call that
6537 * might change the option value and free the memory. */
6538 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006539 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006540 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006541 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006542 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006543 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006545 /* Make all characters printable. */
6546 p = transstr(buf);
6547 if (p != NULL)
6548 {
6549 vim_strncpy(buf, p, sizeof(buf) - 1);
6550 vim_free(p);
6551 }
6552
6553 /* fill up with "fillchar" */
6554 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006555 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 {
6557#ifdef FEAT_MBYTE
6558 len += (*mb_char2bytes)(fillchar, buf + len);
6559#else
6560 buf[len++] = fillchar;
6561#endif
6562 ++width;
6563 }
6564 buf[len] = NUL;
6565
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006566 /*
6567 * Draw each snippet with the specified highlighting.
6568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 curattr = attr;
6570 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006571 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006573 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 screen_puts_len(p, len, row, col, curattr);
6575 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006576 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006578 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006580 else if (hltab[n].userhl < 0)
6581 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006583 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006584 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585#endif
6586 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006587 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 }
6589 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006590
6591 if (wp == NULL)
6592 {
6593 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6594 col = 0;
6595 len = 0;
6596 p = buf;
6597 fillchar = 0;
6598 for (n = 0; tabtab[n].start != NULL; n++)
6599 {
6600 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6601 while (col < len)
6602 TabPageIdxs[col++] = fillchar;
6603 p = tabtab[n].start;
6604 fillchar = tabtab[n].userhl;
6605 }
6606 while (col < Columns)
6607 TabPageIdxs[col++] = fillchar;
6608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609}
6610
6611#endif /* FEAT_STL_OPT */
6612
6613/*
6614 * Output a single character directly to the screen and update ScreenLines.
6615 */
6616 void
6617screen_putchar(c, row, col, attr)
6618 int c;
6619 int row, col;
6620 int attr;
6621{
6622#ifdef FEAT_MBYTE
6623 char_u buf[MB_MAXBYTES + 1];
6624
6625 buf[(*mb_char2bytes)(c, buf)] = NUL;
6626#else
6627 char_u buf[2];
6628
6629 buf[0] = c;
6630 buf[1] = NUL;
6631#endif
6632 screen_puts(buf, row, col, attr);
6633}
6634
6635/*
6636 * Get a single character directly from ScreenLines into "bytes[]".
6637 * Also return its attribute in *attrp;
6638 */
6639 void
6640screen_getbytes(row, col, bytes, attrp)
6641 int row, col;
6642 char_u *bytes;
6643 int *attrp;
6644{
6645 unsigned off;
6646
6647 /* safety check */
6648 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6649 {
6650 off = LineOffset[row] + col;
6651 *attrp = ScreenAttrs[off];
6652 bytes[0] = ScreenLines[off];
6653 bytes[1] = NUL;
6654
6655#ifdef FEAT_MBYTE
6656 if (enc_utf8 && ScreenLinesUC[off] != 0)
6657 bytes[utfc_char2bytes(off, bytes)] = NUL;
6658 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6659 {
6660 bytes[0] = ScreenLines[off];
6661 bytes[1] = ScreenLines2[off];
6662 bytes[2] = NUL;
6663 }
6664 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6665 {
6666 bytes[1] = ScreenLines[off + 1];
6667 bytes[2] = NUL;
6668 }
6669#endif
6670 }
6671}
6672
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006673#ifdef FEAT_MBYTE
6674static int screen_comp_differs __ARGS((int, int*));
6675
6676/*
6677 * Return TRUE if composing characters for screen posn "off" differs from
6678 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006679 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006680 */
6681 static int
6682screen_comp_differs(off, u8cc)
6683 int off;
6684 int *u8cc;
6685{
6686 int i;
6687
6688 for (i = 0; i < Screen_mco; ++i)
6689 {
6690 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6691 return TRUE;
6692 if (u8cc[i] == 0)
6693 break;
6694 }
6695 return FALSE;
6696}
6697#endif
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699/*
6700 * Put string '*text' on the screen at position 'row' and 'col', with
6701 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6702 * Note: only outputs within one row, message is truncated at screen boundary!
6703 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6704 */
6705 void
6706screen_puts(text, row, col, attr)
6707 char_u *text;
6708 int row;
6709 int col;
6710 int attr;
6711{
6712 screen_puts_len(text, -1, row, col, attr);
6713}
6714
6715/*
6716 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6717 * a NUL.
6718 */
6719 void
6720screen_puts_len(text, len, row, col, attr)
6721 char_u *text;
6722 int len;
6723 int row;
6724 int col;
6725 int attr;
6726{
6727 unsigned off;
6728 char_u *ptr = text;
6729 int c;
6730#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006731 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 int mbyte_blen = 1;
6733 int mbyte_cells = 1;
6734 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006735 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 int clear_next_cell = FALSE;
6737# ifdef FEAT_ARABIC
6738 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006739 int pc, nc, nc1;
6740 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741# endif
6742#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006743#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6744 int force_redraw_this;
6745 int force_redraw_next = FALSE;
6746#endif
6747 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6750 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006751 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
Bram Moolenaarc236c162008-07-13 17:41:49 +00006753#ifdef FEAT_MBYTE
6754 /* When drawing over the right halve of a double-wide char clear out the
6755 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006756 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006757# ifdef FEAT_GUI
6758 && !gui.in_use
6759# endif
6760 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006761 {
6762 ScreenLines[off - 1] = ' ';
6763 ScreenAttrs[off - 1] = 0;
6764 if (enc_utf8)
6765 {
6766 ScreenLinesUC[off - 1] = 0;
6767 ScreenLinesC[0][off - 1] = 0;
6768 }
6769 /* redraw the previous cell, make it empty */
6770 screen_char(off - 1, row, col - 1);
6771 /* force the cell at "col" to be redrawn */
6772 force_redraw_next = TRUE;
6773 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006774#endif
6775
Bram Moolenaar367329b2007-08-30 11:53:22 +00006776#ifdef FEAT_MBYTE
6777 max_off = LineOffset[row] + screen_Columns;
6778#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006779 while (col < screen_Columns
6780 && (len < 0 || (int)(ptr - text) < len)
6781 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
6783 c = *ptr;
6784#ifdef FEAT_MBYTE
6785 /* check if this is the first byte of a multibyte */
6786 if (has_mbyte)
6787 {
6788 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006789 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006791 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6793 mbyte_cells = 1;
6794 else if (enc_dbcs != 0)
6795 mbyte_cells = mbyte_blen;
6796 else /* enc_utf8 */
6797 {
6798 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006799 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 (int)((text + len) - ptr));
6801 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006802 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006804# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 /* Non-BMP character: display as ? or fullwidth ?. */
6806 if (u8c >= 0x10000)
6807 {
6808 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6809 if (attr == 0)
6810 attr = hl_attr(HLF_8);
6811 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813# ifdef FEAT_ARABIC
6814 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6815 {
6816 /* Do Arabic shaping. */
6817 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6818 {
6819 /* Past end of string to be displayed. */
6820 nc = NUL;
6821 nc1 = NUL;
6822 }
6823 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006824 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006825 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6826 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006827 nc1 = pcc[0];
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 pc = prev_c;
6830 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006831 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 }
6833 else
6834 prev_c = u8c;
6835# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006836 if (col + mbyte_cells > screen_Columns)
6837 {
6838 /* Only 1 cell left, but character requires 2 cells:
6839 * display a '>' in the last column to avoid wrapping. */
6840 c = '>';
6841 mbyte_cells = 1;
6842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 }
6844 }
6845#endif
6846
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006847#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6848 force_redraw_this = force_redraw_next;
6849 force_redraw_next = FALSE;
6850#endif
6851
6852 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853#ifdef FEAT_MBYTE
6854 || (mbyte_cells == 2
6855 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6856 || (enc_dbcs == DBCS_JPNU
6857 && c == 0x8e
6858 && ScreenLines2[off] != ptr[1])
6859 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006860 && (ScreenLinesUC[off] !=
6861 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6862 || (ScreenLinesUC[off] != 0
6863 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864#endif
6865 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006866 || exmode_active;
6867
6868 if (need_redraw
6869#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6870 || force_redraw_this
6871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 )
6873 {
6874#if defined(FEAT_GUI) || defined(UNIX)
6875 /* The bold trick makes a single row of pixels appear in the next
6876 * character. When a bold character is removed, the next
6877 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006878 * and for some xterms. */
6879 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880# ifdef FEAT_GUI
6881 gui.in_use
6882# endif
6883# if defined(FEAT_GUI) && defined(UNIX)
6884 ||
6885# endif
6886# ifdef UNIX
6887 term_is_xterm
6888# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006889 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006891 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006893 if (n > HL_ALL)
6894 n = syn_attr2attr(n);
6895 if (n & HL_BOLD)
6896 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 }
6898#endif
6899#ifdef FEAT_MBYTE
6900 /* When at the end of the text and overwriting a two-cell
6901 * character with a one-cell character, need to clear the next
6902 * cell. Also when overwriting the left halve of a two-cell char
6903 * with the right halve of a two-cell char. Do this only once
6904 * (mb_off2cells() may return 2 on the right halve). */
6905 if (clear_next_cell)
6906 clear_next_cell = FALSE;
6907 else if (has_mbyte
6908 && (len < 0 ? ptr[mbyte_blen] == NUL
6909 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006910 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006912 && (*mb_off2cells)(off, max_off) == 1
6913 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 clear_next_cell = TRUE;
6915
6916 /* Make sure we never leave a second byte of a double-byte behind,
6917 * it confuses mb_off2cells(). */
6918 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006919 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006921 && (*mb_off2cells)(off, max_off) == 1
6922 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 ScreenLines[off + mbyte_blen] = 0;
6924#endif
6925 ScreenLines[off] = c;
6926 ScreenAttrs[off] = attr;
6927#ifdef FEAT_MBYTE
6928 if (enc_utf8)
6929 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006930 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 ScreenLinesUC[off] = 0;
6932 else
6933 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006934 int i;
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006937 for (i = 0; i < Screen_mco; ++i)
6938 {
6939 ScreenLinesC[i][off] = u8cc[i];
6940 if (u8cc[i] == 0)
6941 break;
6942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944 if (mbyte_cells == 2)
6945 {
6946 ScreenLines[off + 1] = 0;
6947 ScreenAttrs[off + 1] = attr;
6948 }
6949 screen_char(off, row, col);
6950 }
6951 else if (mbyte_cells == 2)
6952 {
6953 ScreenLines[off + 1] = ptr[1];
6954 ScreenAttrs[off + 1] = attr;
6955 screen_char_2(off, row, col);
6956 }
6957 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6958 {
6959 ScreenLines2[off] = ptr[1];
6960 screen_char(off, row, col);
6961 }
6962 else
6963#endif
6964 screen_char(off, row, col);
6965 }
6966#ifdef FEAT_MBYTE
6967 if (has_mbyte)
6968 {
6969 off += mbyte_cells;
6970 col += mbyte_cells;
6971 ptr += mbyte_blen;
6972 if (clear_next_cell)
6973 ptr = (char_u *)" ";
6974 }
6975 else
6976#endif
6977 {
6978 ++off;
6979 ++col;
6980 ++ptr;
6981 }
6982 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006983
6984#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6985 /* If we detected the next character needs to be redrawn, but the text
6986 * doesn't extend up to there, update the character here. */
6987 if (force_redraw_next && col < screen_Columns)
6988 {
6989# ifdef FEAT_MBYTE
6990 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6991 screen_char_2(off, row, col);
6992 else
6993# endif
6994 screen_char(off, row, col);
6995 }
6996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997}
6998
6999#ifdef FEAT_SEARCH_EXTRA
7000/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007001 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 */
7003 static void
7004start_search_hl()
7005{
7006 if (p_hls && !no_hlsearch)
7007 {
7008 last_pat_prog(&search_hl.rm);
7009 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007010# ifdef FEAT_RELTIME
7011 /* Set the time limit to 'redrawtime'. */
7012 profile_setlimit(p_rdt, &search_hl.tm);
7013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015}
7016
7017/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007018 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 */
7020 static void
7021end_search_hl()
7022{
7023 if (search_hl.rm.regprog != NULL)
7024 {
7025 vim_free(search_hl.rm.regprog);
7026 search_hl.rm.regprog = NULL;
7027 }
7028}
7029
7030/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007031 * Init for calling prepare_search_hl().
7032 */
7033 static void
7034init_search_hl(wp)
7035 win_T *wp;
7036{
7037 matchitem_T *cur;
7038
7039 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7040 * match */
7041 cur = wp->w_match_head;
7042 while (cur != NULL)
7043 {
7044 cur->hl.rm = cur->match;
7045 if (cur->hlg_id == 0)
7046 cur->hl.attr = 0;
7047 else
7048 cur->hl.attr = syn_id2attr(cur->hlg_id);
7049 cur->hl.buf = wp->w_buffer;
7050 cur->hl.lnum = 0;
7051 cur->hl.first_lnum = 0;
7052# ifdef FEAT_RELTIME
7053 /* Set the time limit to 'redrawtime'. */
7054 profile_setlimit(p_rdt, &(cur->hl.tm));
7055# endif
7056 cur = cur->next;
7057 }
7058 search_hl.buf = wp->w_buffer;
7059 search_hl.lnum = 0;
7060 search_hl.first_lnum = 0;
7061 /* time limit is set at the toplevel, for all windows */
7062}
7063
7064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 * Advance to the match in window "wp" line "lnum" or past it.
7066 */
7067 static void
7068prepare_search_hl(wp, lnum)
7069 win_T *wp;
7070 linenr_T lnum;
7071{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007072 matchitem_T *cur; /* points to the match list */
7073 match_T *shl; /* points to search_hl or a match */
7074 int shl_flag; /* flag to indicate whether search_hl
7075 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 int n;
7077
7078 /*
7079 * When using a multi-line pattern, start searching at the top
7080 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007081 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007083 cur = wp->w_match_head;
7084 shl_flag = FALSE;
7085 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007087 if (shl_flag == FALSE)
7088 {
7089 shl = &search_hl;
7090 shl_flag = TRUE;
7091 }
7092 else
7093 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 if (shl->rm.regprog != NULL
7095 && shl->lnum == 0
7096 && re_multiline(shl->rm.regprog))
7097 {
7098 if (shl->first_lnum == 0)
7099 {
7100# ifdef FEAT_FOLDING
7101 for (shl->first_lnum = lnum;
7102 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7103 if (hasFoldingWin(wp, shl->first_lnum - 1,
7104 NULL, NULL, TRUE, NULL))
7105 break;
7106# else
7107 shl->first_lnum = wp->w_topline;
7108# endif
7109 }
7110 n = 0;
7111 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7112 {
7113 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7114 if (shl->lnum != 0)
7115 {
7116 shl->first_lnum = shl->lnum
7117 + shl->rm.endpos[0].lnum
7118 - shl->rm.startpos[0].lnum;
7119 n = shl->rm.endpos[0].col;
7120 }
7121 else
7122 {
7123 ++shl->first_lnum;
7124 n = 0;
7125 }
7126 }
7127 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007128 if (shl != &search_hl && cur != NULL)
7129 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
7131}
7132
7133/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007134 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 * Uses shl->buf.
7136 * Sets shl->lnum and shl->rm contents.
7137 * Note: Assumes a previous match is always before "lnum", unless
7138 * shl->lnum is zero.
7139 * Careful: Any pointers for buffer lines will become invalid.
7140 */
7141 static void
7142next_search_hl(win, shl, lnum, mincol)
7143 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007144 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 linenr_T lnum;
7146 colnr_T mincol; /* minimal column for a match */
7147{
7148 linenr_T l;
7149 colnr_T matchcol;
7150 long nmatched;
7151
7152 if (shl->lnum != 0)
7153 {
7154 /* Check for three situations:
7155 * 1. If the "lnum" is below a previous match, start a new search.
7156 * 2. If the previous match includes "mincol", use it.
7157 * 3. Continue after the previous match.
7158 */
7159 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7160 if (lnum > l)
7161 shl->lnum = 0;
7162 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7163 return;
7164 }
7165
7166 /*
7167 * Repeat searching for a match until one is found that includes "mincol"
7168 * or none is found in this line.
7169 */
7170 called_emsg = FALSE;
7171 for (;;)
7172 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007173#ifdef FEAT_RELTIME
7174 /* Stop searching after passing the time limit. */
7175 if (profile_passed_limit(&(shl->tm)))
7176 {
7177 shl->lnum = 0; /* no match found in time */
7178 break;
7179 }
7180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 /* Three situations:
7182 * 1. No useful previous match: search from start of line.
7183 * 2. Not Vi compatible or empty match: continue at next character.
7184 * Break the loop if this is beyond the end of the line.
7185 * 3. Vi compatible searching: continue at end of previous match.
7186 */
7187 if (shl->lnum == 0)
7188 matchcol = 0;
7189 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7190 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007191 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007193 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007194
7195 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007196 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007197 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007199 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 shl->lnum = 0;
7201 break;
7202 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007203#ifdef FEAT_MBYTE
7204 if (has_mbyte)
7205 matchcol += mb_ptr2len(ml);
7206 else
7207#endif
7208 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 }
7210 else
7211 matchcol = shl->rm.endpos[0].col;
7212
7213 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007214 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7215#ifdef FEAT_RELTIME
7216 &(shl->tm)
7217#else
7218 NULL
7219#endif
7220 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007221 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {
7223 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007224 if (shl == &search_hl)
7225 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007226 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007227 vim_free(shl->rm.regprog);
7228 no_hlsearch = TRUE;
7229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007231 shl->lnum = 0;
7232 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 break;
7234 }
7235 if (nmatched == 0)
7236 {
7237 shl->lnum = 0; /* no match found */
7238 break;
7239 }
7240 if (shl->rm.startpos[0].lnum > 0
7241 || shl->rm.startpos[0].col >= mincol
7242 || nmatched > 1
7243 || shl->rm.endpos[0].col > mincol)
7244 {
7245 shl->lnum += shl->rm.startpos[0].lnum;
7246 break; /* useful match found */
7247 }
7248 }
7249}
7250#endif
7251
7252 static void
7253screen_start_highlight(attr)
7254 int attr;
7255{
7256 attrentry_T *aep = NULL;
7257
7258 screen_attr = attr;
7259 if (full_screen
7260#ifdef WIN3264
7261 && termcap_active
7262#endif
7263 )
7264 {
7265#ifdef FEAT_GUI
7266 if (gui.in_use)
7267 {
7268 char buf[20];
7269
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007270 /* The GUI handles this internally. */
7271 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 OUT_STR(buf);
7273 }
7274 else
7275#endif
7276 {
7277 if (attr > HL_ALL) /* special HL attr. */
7278 {
7279 if (t_colors > 1)
7280 aep = syn_cterm_attr2entry(attr);
7281 else
7282 aep = syn_term_attr2entry(attr);
7283 if (aep == NULL) /* did ":syntax clear" */
7284 attr = 0;
7285 else
7286 attr = aep->ae_attr;
7287 }
7288 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7289 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007290 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7291 && cterm_normal_fg_bold)
7292 /* If the Normal FG color has BOLD attribute and the new HL
7293 * has a FG color defined, clear BOLD. */
7294 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7296 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007297 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7298 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 out_str(T_US);
7300 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7301 out_str(T_CZH);
7302 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7303 out_str(T_MR);
7304
7305 /*
7306 * Output the color or start string after bold etc., in case the
7307 * bold etc. override the color setting.
7308 */
7309 if (aep != NULL)
7310 {
7311 if (t_colors > 1)
7312 {
7313 if (aep->ae_u.cterm.fg_color)
7314 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7315 if (aep->ae_u.cterm.bg_color)
7316 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7317 }
7318 else
7319 {
7320 if (aep->ae_u.term.start != NULL)
7321 out_str(aep->ae_u.term.start);
7322 }
7323 }
7324 }
7325 }
7326}
7327
7328 void
7329screen_stop_highlight()
7330{
7331 int do_ME = FALSE; /* output T_ME code */
7332
7333 if (screen_attr != 0
7334#ifdef WIN3264
7335 && termcap_active
7336#endif
7337 )
7338 {
7339#ifdef FEAT_GUI
7340 if (gui.in_use)
7341 {
7342 char buf[20];
7343
7344 /* use internal GUI code */
7345 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7346 OUT_STR(buf);
7347 }
7348 else
7349#endif
7350 {
7351 if (screen_attr > HL_ALL) /* special HL attr. */
7352 {
7353 attrentry_T *aep;
7354
7355 if (t_colors > 1)
7356 {
7357 /*
7358 * Assume that t_me restores the original colors!
7359 */
7360 aep = syn_cterm_attr2entry(screen_attr);
7361 if (aep != NULL && (aep->ae_u.cterm.fg_color
7362 || aep->ae_u.cterm.bg_color))
7363 do_ME = TRUE;
7364 }
7365 else
7366 {
7367 aep = syn_term_attr2entry(screen_attr);
7368 if (aep != NULL && aep->ae_u.term.stop != NULL)
7369 {
7370 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7371 do_ME = TRUE;
7372 else
7373 out_str(aep->ae_u.term.stop);
7374 }
7375 }
7376 if (aep == NULL) /* did ":syntax clear" */
7377 screen_attr = 0;
7378 else
7379 screen_attr = aep->ae_attr;
7380 }
7381
7382 /*
7383 * Often all ending-codes are equal to T_ME. Avoid outputting the
7384 * same sequence several times.
7385 */
7386 if (screen_attr & HL_STANDOUT)
7387 {
7388 if (STRCMP(T_SE, T_ME) == 0)
7389 do_ME = TRUE;
7390 else
7391 out_str(T_SE);
7392 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007393 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
7395 if (STRCMP(T_UE, T_ME) == 0)
7396 do_ME = TRUE;
7397 else
7398 out_str(T_UE);
7399 }
7400 if (screen_attr & HL_ITALIC)
7401 {
7402 if (STRCMP(T_CZR, T_ME) == 0)
7403 do_ME = TRUE;
7404 else
7405 out_str(T_CZR);
7406 }
7407 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7408 out_str(T_ME);
7409
7410 if (t_colors > 1)
7411 {
7412 /* set Normal cterm colors */
7413 if (cterm_normal_fg_color != 0)
7414 term_fg_color(cterm_normal_fg_color - 1);
7415 if (cterm_normal_bg_color != 0)
7416 term_bg_color(cterm_normal_bg_color - 1);
7417 if (cterm_normal_fg_bold)
7418 out_str(T_MD);
7419 }
7420 }
7421 }
7422 screen_attr = 0;
7423}
7424
7425/*
7426 * Reset the colors for a cterm. Used when leaving Vim.
7427 * The machine specific code may override this again.
7428 */
7429 void
7430reset_cterm_colors()
7431{
7432 if (t_colors > 1)
7433 {
7434 /* set Normal cterm colors */
7435 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7436 {
7437 out_str(T_OP);
7438 screen_attr = -1;
7439 }
7440 if (cterm_normal_fg_bold)
7441 {
7442 out_str(T_ME);
7443 screen_attr = -1;
7444 }
7445 }
7446}
7447
7448/*
7449 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7450 * using the attributes from ScreenAttrs["off"].
7451 */
7452 static void
7453screen_char(off, row, col)
7454 unsigned off;
7455 int row;
7456 int col;
7457{
7458 int attr;
7459
7460 /* Check for illegal values, just in case (could happen just after
7461 * resizing). */
7462 if (row >= screen_Rows || col >= screen_Columns)
7463 return;
7464
7465 /* Outputting the last character on the screen may scrollup the screen.
7466 * Don't to it! Mark the character invalid (update it when scrolled up) */
7467 if (row == screen_Rows - 1 && col == screen_Columns - 1
7468#ifdef FEAT_RIGHTLEFT
7469 /* account for first command-line character in rightleft mode */
7470 && !cmdmsg_rl
7471#endif
7472 )
7473 {
7474 ScreenAttrs[off] = (sattr_T)-1;
7475 return;
7476 }
7477
7478 /*
7479 * Stop highlighting first, so it's easier to move the cursor.
7480 */
7481#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7482 if (screen_char_attr != 0)
7483 attr = screen_char_attr;
7484 else
7485#endif
7486 attr = ScreenAttrs[off];
7487 if (screen_attr != attr)
7488 screen_stop_highlight();
7489
7490 windgoto(row, col);
7491
7492 if (screen_attr != attr)
7493 screen_start_highlight(attr);
7494
7495#ifdef FEAT_MBYTE
7496 if (enc_utf8 && ScreenLinesUC[off] != 0)
7497 {
7498 char_u buf[MB_MAXBYTES + 1];
7499
7500 /* Convert UTF-8 character to bytes and write it. */
7501
7502 buf[utfc_char2bytes(off, buf)] = NUL;
7503
7504 out_str(buf);
7505 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7506 ++screen_cur_col;
7507 }
7508 else
7509#endif
7510 {
7511#ifdef FEAT_MBYTE
7512 out_flush_check();
7513#endif
7514 out_char(ScreenLines[off]);
7515#ifdef FEAT_MBYTE
7516 /* double-byte character in single-width cell */
7517 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7518 out_char(ScreenLines2[off]);
7519#endif
7520 }
7521
7522 screen_cur_col++;
7523}
7524
7525#ifdef FEAT_MBYTE
7526
7527/*
7528 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7529 * on the screen at position 'row' and 'col'.
7530 * The attributes of the first byte is used for all. This is required to
7531 * output the two bytes of a double-byte character with nothing in between.
7532 */
7533 static void
7534screen_char_2(off, row, col)
7535 unsigned off;
7536 int row;
7537 int col;
7538{
7539 /* Check for illegal values (could be wrong when screen was resized). */
7540 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7541 return;
7542
7543 /* Outputting the last character on the screen may scrollup the screen.
7544 * Don't to it! Mark the character invalid (update it when scrolled up) */
7545 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7546 {
7547 ScreenAttrs[off] = (sattr_T)-1;
7548 return;
7549 }
7550
7551 /* Output the first byte normally (positions the cursor), then write the
7552 * second byte directly. */
7553 screen_char(off, row, col);
7554 out_char(ScreenLines[off + 1]);
7555 ++screen_cur_col;
7556}
7557#endif
7558
7559#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7560/*
7561 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7562 * This uses the contents of ScreenLines[] and doesn't change it.
7563 */
7564 void
7565screen_draw_rectangle(row, col, height, width, invert)
7566 int row;
7567 int col;
7568 int height;
7569 int width;
7570 int invert;
7571{
7572 int r, c;
7573 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007574#ifdef FEAT_MBYTE
7575 int max_off;
7576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007578 /* Can't use ScreenLines unless initialized */
7579 if (ScreenLines == NULL)
7580 return;
7581
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 if (invert)
7583 screen_char_attr = HL_INVERSE;
7584 for (r = row; r < row + height; ++r)
7585 {
7586 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007587#ifdef FEAT_MBYTE
7588 max_off = off + screen_Columns;
7589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 for (c = col; c < col + width; ++c)
7591 {
7592#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007593 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 {
7595 screen_char_2(off + c, r, c);
7596 ++c;
7597 }
7598 else
7599#endif
7600 {
7601 screen_char(off + c, r, c);
7602#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007603 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 ++c;
7605#endif
7606 }
7607 }
7608 }
7609 screen_char_attr = 0;
7610}
7611#endif
7612
7613#ifdef FEAT_VERTSPLIT
7614/*
7615 * Redraw the characters for a vertically split window.
7616 */
7617 static void
7618redraw_block(row, end, wp)
7619 int row;
7620 int end;
7621 win_T *wp;
7622{
7623 int col;
7624 int width;
7625
7626# ifdef FEAT_CLIPBOARD
7627 clip_may_clear_selection(row, end - 1);
7628# endif
7629
7630 if (wp == NULL)
7631 {
7632 col = 0;
7633 width = Columns;
7634 }
7635 else
7636 {
7637 col = wp->w_wincol;
7638 width = wp->w_width;
7639 }
7640 screen_draw_rectangle(row, col, end - row, width, FALSE);
7641}
7642#endif
7643
7644/*
7645 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7646 * with character 'c1' in first column followed by 'c2' in the other columns.
7647 * Use attributes 'attr'.
7648 */
7649 void
7650screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7651 int start_row, end_row;
7652 int start_col, end_col;
7653 int c1, c2;
7654 int attr;
7655{
7656 int row;
7657 int col;
7658 int off;
7659 int end_off;
7660 int did_delete;
7661 int c;
7662 int norm_term;
7663#if defined(FEAT_GUI) || defined(UNIX)
7664 int force_next = FALSE;
7665#endif
7666
7667 if (end_row > screen_Rows) /* safety check */
7668 end_row = screen_Rows;
7669 if (end_col > screen_Columns) /* safety check */
7670 end_col = screen_Columns;
7671 if (ScreenLines == NULL
7672 || start_row >= end_row
7673 || start_col >= end_col) /* nothing to do */
7674 return;
7675
7676 /* it's a "normal" terminal when not in a GUI or cterm */
7677 norm_term = (
7678#ifdef FEAT_GUI
7679 !gui.in_use &&
7680#endif
7681 t_colors <= 1);
7682 for (row = start_row; row < end_row; ++row)
7683 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007684#ifdef FEAT_MBYTE
7685 if (has_mbyte
7686# ifdef FEAT_GUI
7687 && !gui.in_use
7688# endif
7689 )
7690 {
7691 /* When drawing over the right halve of a double-wide char clear
7692 * out the left halve. When drawing over the left halve of a
7693 * double wide-char clear out the right halve. Only needed in a
7694 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007695 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007696 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007697 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007698 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007699 }
7700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 /*
7702 * Try to use delete-line termcap code, when no attributes or in a
7703 * "normal" terminal, where a bold/italic space is just a
7704 * space.
7705 */
7706 did_delete = FALSE;
7707 if (c2 == ' '
7708 && end_col == Columns
7709 && can_clear(T_CE)
7710 && (attr == 0
7711 || (norm_term
7712 && attr <= HL_ALL
7713 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7714 {
7715 /*
7716 * check if we really need to clear something
7717 */
7718 col = start_col;
7719 if (c1 != ' ') /* don't clear first char */
7720 ++col;
7721
7722 off = LineOffset[row] + col;
7723 end_off = LineOffset[row] + end_col;
7724
7725 /* skip blanks (used often, keep it fast!) */
7726#ifdef FEAT_MBYTE
7727 if (enc_utf8)
7728 while (off < end_off && ScreenLines[off] == ' '
7729 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7730 ++off;
7731 else
7732#endif
7733 while (off < end_off && ScreenLines[off] == ' '
7734 && ScreenAttrs[off] == 0)
7735 ++off;
7736 if (off < end_off) /* something to be cleared */
7737 {
7738 col = off - LineOffset[row];
7739 screen_stop_highlight();
7740 term_windgoto(row, col);/* clear rest of this screen line */
7741 out_str(T_CE);
7742 screen_start(); /* don't know where cursor is now */
7743 col = end_col - col;
7744 while (col--) /* clear chars in ScreenLines */
7745 {
7746 ScreenLines[off] = ' ';
7747#ifdef FEAT_MBYTE
7748 if (enc_utf8)
7749 ScreenLinesUC[off] = 0;
7750#endif
7751 ScreenAttrs[off] = 0;
7752 ++off;
7753 }
7754 }
7755 did_delete = TRUE; /* the chars are cleared now */
7756 }
7757
7758 off = LineOffset[row] + start_col;
7759 c = c1;
7760 for (col = start_col; col < end_col; ++col)
7761 {
7762 if (ScreenLines[off] != c
7763#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007764 || (enc_utf8 && (int)ScreenLinesUC[off]
7765 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766#endif
7767 || ScreenAttrs[off] != attr
7768#if defined(FEAT_GUI) || defined(UNIX)
7769 || force_next
7770#endif
7771 )
7772 {
7773#if defined(FEAT_GUI) || defined(UNIX)
7774 /* The bold trick may make a single row of pixels appear in
7775 * the next character. When a bold character is removed, the
7776 * next character should be redrawn too. This happens for our
7777 * own GUI and for some xterms. */
7778 if (
7779# ifdef FEAT_GUI
7780 gui.in_use
7781# endif
7782# if defined(FEAT_GUI) && defined(UNIX)
7783 ||
7784# endif
7785# ifdef UNIX
7786 term_is_xterm
7787# endif
7788 )
7789 {
7790 if (ScreenLines[off] != ' '
7791 && (ScreenAttrs[off] > HL_ALL
7792 || ScreenAttrs[off] & HL_BOLD))
7793 force_next = TRUE;
7794 else
7795 force_next = FALSE;
7796 }
7797#endif
7798 ScreenLines[off] = c;
7799#ifdef FEAT_MBYTE
7800 if (enc_utf8)
7801 {
7802 if (c >= 0x80)
7803 {
7804 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007805 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807 else
7808 ScreenLinesUC[off] = 0;
7809 }
7810#endif
7811 ScreenAttrs[off] = attr;
7812 if (!did_delete || c != ' ')
7813 screen_char(off, row, col);
7814 }
7815 ++off;
7816 if (col == start_col)
7817 {
7818 if (did_delete)
7819 break;
7820 c = c2;
7821 }
7822 }
7823 if (end_col == Columns)
7824 LineWraps[row] = FALSE;
7825 if (row == Rows - 1) /* overwritten the command line */
7826 {
7827 redraw_cmdline = TRUE;
7828 if (c1 == ' ' && c2 == ' ')
7829 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007830 if (start_col == 0)
7831 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 }
7833 }
7834}
7835
7836/*
7837 * Check if there should be a delay. Used before clearing or redrawing the
7838 * screen or the command line.
7839 */
7840 void
7841check_for_delay(check_msg_scroll)
7842 int check_msg_scroll;
7843{
7844 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7845 && !did_wait_return
7846 && emsg_silent == 0)
7847 {
7848 out_flush();
7849 ui_delay(1000L, TRUE);
7850 emsg_on_display = FALSE;
7851 if (check_msg_scroll)
7852 msg_scroll = FALSE;
7853 }
7854}
7855
7856/*
7857 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007858 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 * Returns TRUE if there is a valid screen to write to.
7860 * Returns FALSE when starting up and screen not initialized yet.
7861 */
7862 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007863screen_valid(doclear)
7864 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007866 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 return (ScreenLines != NULL);
7868}
7869
7870/*
7871 * Resize the shell to Rows and Columns.
7872 * Allocate ScreenLines[] and associated items.
7873 *
7874 * There may be some time between setting Rows and Columns and (re)allocating
7875 * ScreenLines[]. This happens when starting up and when (manually) changing
7876 * the shell size. Always use screen_Rows and screen_Columns to access items
7877 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7878 * final size of the shell is needed.
7879 */
7880 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007881screenalloc(doclear)
7882 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
7884 int new_row, old_row;
7885#ifdef FEAT_GUI
7886 int old_Rows;
7887#endif
7888 win_T *wp;
7889 int outofmem = FALSE;
7890 int len;
7891 schar_T *new_ScreenLines;
7892#ifdef FEAT_MBYTE
7893 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007894 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007896 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897#endif
7898 sattr_T *new_ScreenAttrs;
7899 unsigned *new_LineOffset;
7900 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007901#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007902 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007903 tabpage_T *tp;
7904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007906 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007907#ifdef FEAT_AUTOCMD
7908 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007910retry:
7911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 /*
7913 * Allocation of the screen buffers is done only when the size changes and
7914 * when Rows and Columns have been set and we have started doing full
7915 * screen stuff.
7916 */
7917 if ((ScreenLines != NULL
7918 && Rows == screen_Rows
7919 && Columns == screen_Columns
7920#ifdef FEAT_MBYTE
7921 && enc_utf8 == (ScreenLinesUC != NULL)
7922 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007923 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924#endif
7925 )
7926 || Rows == 0
7927 || Columns == 0
7928 || (!full_screen && ScreenLines == NULL))
7929 return;
7930
7931 /*
7932 * It's possible that we produce an out-of-memory message below, which
7933 * will cause this function to be called again. To break the loop, just
7934 * return here.
7935 */
7936 if (entered)
7937 return;
7938 entered = TRUE;
7939
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007940 /*
7941 * Note that the window sizes are updated before reallocating the arrays,
7942 * thus we must not redraw here!
7943 */
7944 ++RedrawingDisabled;
7945
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 win_new_shellsize(); /* fit the windows in the new sized shell */
7947
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 comp_col(); /* recompute columns for shown command and ruler */
7949
7950 /*
7951 * We're changing the size of the screen.
7952 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7953 * - Move lines from the old arrays into the new arrays, clear extra
7954 * lines (unless the screen is going to be cleared).
7955 * - Free the old arrays.
7956 *
7957 * If anything fails, make ScreenLines NULL, so we don't do anything!
7958 * Continuing with the old ScreenLines may result in a crash, because the
7959 * size is wrong.
7960 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007961 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007963#ifdef FEAT_AUTOCMD
7964 if (aucmd_win != NULL)
7965 win_free_lsize(aucmd_win);
7966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967
7968 new_ScreenLines = (schar_T *)lalloc((long_u)(
7969 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7970#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007971 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 if (enc_utf8)
7973 {
7974 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7975 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007976 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007977 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7979 }
7980 if (enc_dbcs == DBCS_JPNU)
7981 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7982 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7983#endif
7984 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7985 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7986 new_LineOffset = (unsigned *)lalloc((long_u)(
7987 Rows * sizeof(unsigned)), FALSE);
7988 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007989#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007990 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007993 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {
7995 if (win_alloc_lines(wp) == FAIL)
7996 {
7997 outofmem = TRUE;
7998#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007999 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000#endif
8001 }
8002 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008003#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008004 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8005 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008006 outofmem = TRUE;
8007#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008008#ifdef FEAT_WINDOWS
8009give_up:
8010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008012#ifdef FEAT_MBYTE
8013 for (i = 0; i < p_mco; ++i)
8014 if (new_ScreenLinesC[i] == NULL)
8015 break;
8016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (new_ScreenLines == NULL
8018#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008019 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8021#endif
8022 || new_ScreenAttrs == NULL
8023 || new_LineOffset == NULL
8024 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008025#ifdef FEAT_WINDOWS
8026 || new_TabPageIdxs == NULL
8027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 || outofmem)
8029 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008030 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008031 {
8032 /* guess the size */
8033 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8034
8035 /* Remember we did this to avoid getting outofmem messages over
8036 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008037 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 vim_free(new_ScreenLines);
8040 new_ScreenLines = NULL;
8041#ifdef FEAT_MBYTE
8042 vim_free(new_ScreenLinesUC);
8043 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008044 for (i = 0; i < p_mco; ++i)
8045 {
8046 vim_free(new_ScreenLinesC[i]);
8047 new_ScreenLinesC[i] = NULL;
8048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 vim_free(new_ScreenLines2);
8050 new_ScreenLines2 = NULL;
8051#endif
8052 vim_free(new_ScreenAttrs);
8053 new_ScreenAttrs = NULL;
8054 vim_free(new_LineOffset);
8055 new_LineOffset = NULL;
8056 vim_free(new_LineWraps);
8057 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008058#ifdef FEAT_WINDOWS
8059 vim_free(new_TabPageIdxs);
8060 new_TabPageIdxs = NULL;
8061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 }
8063 else
8064 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008065 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 for (new_row = 0; new_row < Rows; ++new_row)
8068 {
8069 new_LineOffset[new_row] = new_row * Columns;
8070 new_LineWraps[new_row] = FALSE;
8071
8072 /*
8073 * If the screen is not going to be cleared, copy as much as
8074 * possible from the old screen to the new one and clear the rest
8075 * (used when resizing the window at the "--more--" prompt or when
8076 * executing an external command, for the GUI).
8077 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008078 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
8080 (void)vim_memset(new_ScreenLines + new_row * Columns,
8081 ' ', (size_t)Columns * sizeof(schar_T));
8082#ifdef FEAT_MBYTE
8083 if (enc_utf8)
8084 {
8085 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8086 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008087 for (i = 0; i < p_mco; ++i)
8088 (void)vim_memset(new_ScreenLinesC[i]
8089 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 0, (size_t)Columns * sizeof(u8char_T));
8091 }
8092 if (enc_dbcs == DBCS_JPNU)
8093 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8094 0, (size_t)Columns * sizeof(schar_T));
8095#endif
8096 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8097 0, (size_t)Columns * sizeof(sattr_T));
8098 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008099 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {
8101 if (screen_Columns < Columns)
8102 len = screen_Columns;
8103 else
8104 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008105#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008106 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008107 * may be invalid now. Also when p_mco changes. */
8108 if (!(enc_utf8 && ScreenLinesUC == NULL)
8109 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008110#endif
8111 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8112 ScreenLines + LineOffset[old_row],
8113 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008115 if (enc_utf8 && ScreenLinesUC != NULL
8116 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {
8118 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8119 ScreenLinesUC + LineOffset[old_row],
8120 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008121 for (i = 0; i < p_mco; ++i)
8122 mch_memmove(new_ScreenLinesC[i]
8123 + new_LineOffset[new_row],
8124 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 (size_t)len * sizeof(u8char_T));
8126 }
8127 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8128 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8129 ScreenLines2 + LineOffset[old_row],
8130 (size_t)len * sizeof(schar_T));
8131#endif
8132 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8133 ScreenAttrs + LineOffset[old_row],
8134 (size_t)len * sizeof(sattr_T));
8135 }
8136 }
8137 }
8138 /* Use the last line of the screen for the current line. */
8139 current_ScreenLine = new_ScreenLines + Rows * Columns;
8140 }
8141
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008142 free_screenlines();
8143
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 ScreenLines = new_ScreenLines;
8145#ifdef FEAT_MBYTE
8146 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008147 for (i = 0; i < p_mco; ++i)
8148 ScreenLinesC[i] = new_ScreenLinesC[i];
8149 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 ScreenLines2 = new_ScreenLines2;
8151#endif
8152 ScreenAttrs = new_ScreenAttrs;
8153 LineOffset = new_LineOffset;
8154 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008155#ifdef FEAT_WINDOWS
8156 TabPageIdxs = new_TabPageIdxs;
8157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
8159 /* It's important that screen_Rows and screen_Columns reflect the actual
8160 * size of ScreenLines[]. Set them before calling anything. */
8161#ifdef FEAT_GUI
8162 old_Rows = screen_Rows;
8163#endif
8164 screen_Rows = Rows;
8165 screen_Columns = Columns;
8166
8167 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008168 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 screenclear2();
8170
8171#ifdef FEAT_GUI
8172 else if (gui.in_use
8173 && !gui.starting
8174 && ScreenLines != NULL
8175 && old_Rows != Rows)
8176 {
8177 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8178 /*
8179 * Adjust the position of the cursor, for when executing an external
8180 * command.
8181 */
8182 if (msg_row >= Rows) /* Rows got smaller */
8183 msg_row = Rows - 1; /* put cursor at last row */
8184 else if (Rows > old_Rows) /* Rows got bigger */
8185 msg_row += Rows - old_Rows; /* put cursor in same place */
8186 if (msg_col >= Columns) /* Columns got smaller */
8187 msg_col = Columns - 1; /* put cursor at last column */
8188 }
8189#endif
8190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008192 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008193
8194#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008195 /*
8196 * Do not apply autocommands more than 3 times to avoid an endless loop
8197 * in case applying autocommands always changes Rows or Columns.
8198 */
8199 if (starting == 0 && ++retry_count <= 3)
8200 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008201 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008202 /* In rare cases, autocommands may have altered Rows or Columns,
8203 * jump back to check if we need to allocate the screen again. */
8204 goto retry;
8205 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207}
8208
8209 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008210free_screenlines()
8211{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008212#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008213 int i;
8214
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008215 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008216 for (i = 0; i < Screen_mco; ++i)
8217 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008218 vim_free(ScreenLines2);
8219#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008220 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008221 vim_free(ScreenAttrs);
8222 vim_free(LineOffset);
8223 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008224#ifdef FEAT_WINDOWS
8225 vim_free(TabPageIdxs);
8226#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008227}
8228
8229 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230screenclear()
8231{
8232 check_for_delay(FALSE);
8233 screenalloc(FALSE); /* allocate screen buffers if size changed */
8234 screenclear2(); /* clear the screen */
8235}
8236
8237 static void
8238screenclear2()
8239{
8240 int i;
8241
8242 if (starting == NO_SCREEN || ScreenLines == NULL
8243#ifdef FEAT_GUI
8244 || (gui.in_use && gui.starting)
8245#endif
8246 )
8247 return;
8248
8249#ifdef FEAT_GUI
8250 if (!gui.in_use)
8251#endif
8252 screen_attr = -1; /* force setting the Normal colors */
8253 screen_stop_highlight(); /* don't want highlighting here */
8254
8255#ifdef FEAT_CLIPBOARD
8256 /* disable selection without redrawing it */
8257 clip_scroll_selection(9999);
8258#endif
8259
8260 /* blank out ScreenLines */
8261 for (i = 0; i < Rows; ++i)
8262 {
8263 lineclear(LineOffset[i], (int)Columns);
8264 LineWraps[i] = FALSE;
8265 }
8266
8267 if (can_clear(T_CL))
8268 {
8269 out_str(T_CL); /* clear the display */
8270 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008271 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 }
8273 else
8274 {
8275 /* can't clear the screen, mark all chars with invalid attributes */
8276 for (i = 0; i < Rows; ++i)
8277 lineinvalid(LineOffset[i], (int)Columns);
8278 clear_cmdline = TRUE;
8279 }
8280
8281 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8282
8283 win_rest_invalid(firstwin);
8284 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008285#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008286 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (must_redraw == CLEAR) /* no need to clear again */
8289 must_redraw = NOT_VALID;
8290 compute_cmdrow();
8291 msg_row = cmdline_row; /* put cursor on last line for messages */
8292 msg_col = 0;
8293 screen_start(); /* don't know where cursor is now */
8294 msg_scrolled = 0; /* can't scroll back */
8295 msg_didany = FALSE;
8296 msg_didout = FALSE;
8297}
8298
8299/*
8300 * Clear one line in ScreenLines.
8301 */
8302 static void
8303lineclear(off, width)
8304 unsigned off;
8305 int width;
8306{
8307 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8308#ifdef FEAT_MBYTE
8309 if (enc_utf8)
8310 (void)vim_memset(ScreenLinesUC + off, 0,
8311 (size_t)width * sizeof(u8char_T));
8312#endif
8313 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8314}
8315
8316/*
8317 * Mark one line in ScreenLines invalid by setting the attributes to an
8318 * invalid value.
8319 */
8320 static void
8321lineinvalid(off, width)
8322 unsigned off;
8323 int width;
8324{
8325 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8326}
8327
8328#ifdef FEAT_VERTSPLIT
8329/*
8330 * Copy part of a Screenline for vertically split window "wp".
8331 */
8332 static void
8333linecopy(to, from, wp)
8334 int to;
8335 int from;
8336 win_T *wp;
8337{
8338 unsigned off_to = LineOffset[to] + wp->w_wincol;
8339 unsigned off_from = LineOffset[from] + wp->w_wincol;
8340
8341 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8342 wp->w_width * sizeof(schar_T));
8343# ifdef FEAT_MBYTE
8344 if (enc_utf8)
8345 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008346 int i;
8347
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8349 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008350 for (i = 0; i < p_mco; ++i)
8351 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8352 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 }
8354 if (enc_dbcs == DBCS_JPNU)
8355 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8356 wp->w_width * sizeof(schar_T));
8357# endif
8358 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8359 wp->w_width * sizeof(sattr_T));
8360}
8361#endif
8362
8363/*
8364 * Return TRUE if clearing with term string "p" would work.
8365 * It can't work when the string is empty or it won't set the right background.
8366 */
8367 int
8368can_clear(p)
8369 char_u *p;
8370{
8371 return (*p != NUL && (t_colors <= 1
8372#ifdef FEAT_GUI
8373 || gui.in_use
8374#endif
8375 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8376}
8377
8378/*
8379 * Reset cursor position. Use whenever cursor was moved because of outputting
8380 * something directly to the screen (shell commands) or a terminal control
8381 * code.
8382 */
8383 void
8384screen_start()
8385{
8386 screen_cur_row = screen_cur_col = 9999;
8387}
8388
8389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 * Move the cursor to position "row","col" in the screen.
8391 * This tries to find the most efficient way to move, minimizing the number of
8392 * characters sent to the terminal.
8393 */
8394 void
8395windgoto(row, col)
8396 int row;
8397 int col;
8398{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008399 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 int i;
8401 int plan;
8402 int cost;
8403 int wouldbe_col;
8404 int noinvcurs;
8405 char_u *bs;
8406 int goto_cost;
8407 int attr;
8408
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008409#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8411
8412#define PLAN_LE 1
8413#define PLAN_CR 2
8414#define PLAN_NL 3
8415#define PLAN_WRITE 4
8416 /* Can't use ScreenLines unless initialized */
8417 if (ScreenLines == NULL)
8418 return;
8419
8420 if (col != screen_cur_col || row != screen_cur_row)
8421 {
8422 /* Check for valid position. */
8423 if (row < 0) /* window without text lines? */
8424 row = 0;
8425 if (row >= screen_Rows)
8426 row = screen_Rows - 1;
8427 if (col >= screen_Columns)
8428 col = screen_Columns - 1;
8429
8430 /* check if no cursor movement is allowed in highlight mode */
8431 if (screen_attr && *T_MS == NUL)
8432 noinvcurs = HIGHL_COST;
8433 else
8434 noinvcurs = 0;
8435 goto_cost = GOTO_COST + noinvcurs;
8436
8437 /*
8438 * Plan how to do the positioning:
8439 * 1. Use CR to move it to column 0, same row.
8440 * 2. Use T_LE to move it a few columns to the left.
8441 * 3. Use NL to move a few lines down, column 0.
8442 * 4. Move a few columns to the right with T_ND or by writing chars.
8443 *
8444 * Don't do this if the cursor went beyond the last column, the cursor
8445 * position is unknown then (some terminals wrap, some don't )
8446 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008447 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 * characters to move the cursor to the right.
8449 */
8450 if (row >= screen_cur_row && screen_cur_col < Columns)
8451 {
8452 /*
8453 * If the cursor is in the same row, bigger col, we can use CR
8454 * or T_LE.
8455 */
8456 bs = NULL; /* init for GCC */
8457 attr = screen_attr;
8458 if (row == screen_cur_row && col < screen_cur_col)
8459 {
8460 /* "le" is preferred over "bc", because "bc" is obsolete */
8461 if (*T_LE)
8462 bs = T_LE; /* "cursor left" */
8463 else
8464 bs = T_BC; /* "backspace character (old) */
8465 if (*bs)
8466 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8467 else
8468 cost = 999;
8469 if (col + 1 < cost) /* using CR is less characters */
8470 {
8471 plan = PLAN_CR;
8472 wouldbe_col = 0;
8473 cost = 1; /* CR is just one character */
8474 }
8475 else
8476 {
8477 plan = PLAN_LE;
8478 wouldbe_col = col;
8479 }
8480 if (noinvcurs) /* will stop highlighting */
8481 {
8482 cost += noinvcurs;
8483 attr = 0;
8484 }
8485 }
8486
8487 /*
8488 * If the cursor is above where we want to be, we can use CR LF.
8489 */
8490 else if (row > screen_cur_row)
8491 {
8492 plan = PLAN_NL;
8493 wouldbe_col = 0;
8494 cost = (row - screen_cur_row) * 2; /* CR LF */
8495 if (noinvcurs) /* will stop highlighting */
8496 {
8497 cost += noinvcurs;
8498 attr = 0;
8499 }
8500 }
8501
8502 /*
8503 * If the cursor is in the same row, smaller col, just use write.
8504 */
8505 else
8506 {
8507 plan = PLAN_WRITE;
8508 wouldbe_col = screen_cur_col;
8509 cost = 0;
8510 }
8511
8512 /*
8513 * Check if any characters that need to be written have the
8514 * correct attributes. Also avoid UTF-8 characters.
8515 */
8516 i = col - wouldbe_col;
8517 if (i > 0)
8518 cost += i;
8519 if (cost < goto_cost && i > 0)
8520 {
8521 /*
8522 * Check if the attributes are correct without additionally
8523 * stopping highlighting.
8524 */
8525 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8526 while (i && *p++ == attr)
8527 --i;
8528 if (i != 0)
8529 {
8530 /*
8531 * Try if it works when highlighting is stopped here.
8532 */
8533 if (*--p == 0)
8534 {
8535 cost += noinvcurs;
8536 while (i && *p++ == 0)
8537 --i;
8538 }
8539 if (i != 0)
8540 cost = 999; /* different attributes, don't do it */
8541 }
8542#ifdef FEAT_MBYTE
8543 if (enc_utf8)
8544 {
8545 /* Don't use an UTF-8 char for positioning, it's slow. */
8546 for (i = wouldbe_col; i < col; ++i)
8547 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8548 {
8549 cost = 999;
8550 break;
8551 }
8552 }
8553#endif
8554 }
8555
8556 /*
8557 * We can do it without term_windgoto()!
8558 */
8559 if (cost < goto_cost)
8560 {
8561 if (plan == PLAN_LE)
8562 {
8563 if (noinvcurs)
8564 screen_stop_highlight();
8565 while (screen_cur_col > col)
8566 {
8567 out_str(bs);
8568 --screen_cur_col;
8569 }
8570 }
8571 else if (plan == PLAN_CR)
8572 {
8573 if (noinvcurs)
8574 screen_stop_highlight();
8575 out_char('\r');
8576 screen_cur_col = 0;
8577 }
8578 else if (plan == PLAN_NL)
8579 {
8580 if (noinvcurs)
8581 screen_stop_highlight();
8582 while (screen_cur_row < row)
8583 {
8584 out_char('\n');
8585 ++screen_cur_row;
8586 }
8587 screen_cur_col = 0;
8588 }
8589
8590 i = col - screen_cur_col;
8591 if (i > 0)
8592 {
8593 /*
8594 * Use cursor-right if it's one character only. Avoids
8595 * removing a line of pixels from the last bold char, when
8596 * using the bold trick in the GUI.
8597 */
8598 if (T_ND[0] != NUL && T_ND[1] == NUL)
8599 {
8600 while (i-- > 0)
8601 out_char(*T_ND);
8602 }
8603 else
8604 {
8605 int off;
8606
8607 off = LineOffset[row] + screen_cur_col;
8608 while (i-- > 0)
8609 {
8610 if (ScreenAttrs[off] != screen_attr)
8611 screen_stop_highlight();
8612#ifdef FEAT_MBYTE
8613 out_flush_check();
8614#endif
8615 out_char(ScreenLines[off]);
8616#ifdef FEAT_MBYTE
8617 if (enc_dbcs == DBCS_JPNU
8618 && ScreenLines[off] == 0x8e)
8619 out_char(ScreenLines2[off]);
8620#endif
8621 ++off;
8622 }
8623 }
8624 }
8625 }
8626 }
8627 else
8628 cost = 999;
8629
8630 if (cost >= goto_cost)
8631 {
8632 if (noinvcurs)
8633 screen_stop_highlight();
8634 if (row == screen_cur_row && (col > screen_cur_col) &&
8635 *T_CRI != NUL)
8636 term_cursor_right(col - screen_cur_col);
8637 else
8638 term_windgoto(row, col);
8639 }
8640 screen_cur_row = row;
8641 screen_cur_col = col;
8642 }
8643}
8644
8645/*
8646 * Set cursor to its position in the current window.
8647 */
8648 void
8649setcursor()
8650{
8651 if (redrawing())
8652 {
8653 validate_cursor();
8654 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8655 W_WINCOL(curwin) + (
8656#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008657 /* With 'rightleft' set and the cursor on a double-wide
8658 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8660# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008661 (has_mbyte
8662 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8663 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664# endif
8665 1)) :
8666#endif
8667 curwin->w_wcol));
8668 }
8669}
8670
8671
8672/*
8673 * insert 'line_count' lines at 'row' in window 'wp'
8674 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8675 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8676 * scrolling.
8677 * Returns FAIL if the lines are not inserted, OK for success.
8678 */
8679 int
8680win_ins_lines(wp, row, line_count, invalid, mayclear)
8681 win_T *wp;
8682 int row;
8683 int line_count;
8684 int invalid;
8685 int mayclear;
8686{
8687 int did_delete;
8688 int nextrow;
8689 int lastrow;
8690 int retval;
8691
8692 if (invalid)
8693 wp->w_lines_valid = 0;
8694
8695 if (wp->w_height < 5)
8696 return FAIL;
8697
8698 if (line_count > wp->w_height - row)
8699 line_count = wp->w_height - row;
8700
8701 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8702 if (retval != MAYBE)
8703 return retval;
8704
8705 /*
8706 * If there is a next window or a status line, we first try to delete the
8707 * lines at the bottom to avoid messing what is after the window.
8708 * If this fails and there are following windows, don't do anything to avoid
8709 * messing up those windows, better just redraw.
8710 */
8711 did_delete = FALSE;
8712#ifdef FEAT_WINDOWS
8713 if (wp->w_next != NULL || wp->w_status_height)
8714 {
8715 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8716 line_count, (int)Rows, FALSE, NULL) == OK)
8717 did_delete = TRUE;
8718 else if (wp->w_next)
8719 return FAIL;
8720 }
8721#endif
8722 /*
8723 * if no lines deleted, blank the lines that will end up below the window
8724 */
8725 if (!did_delete)
8726 {
8727#ifdef FEAT_WINDOWS
8728 wp->w_redr_status = TRUE;
8729#endif
8730 redraw_cmdline = TRUE;
8731 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8732 lastrow = nextrow + line_count;
8733 if (lastrow > Rows)
8734 lastrow = Rows;
8735 screen_fill(nextrow - line_count, lastrow - line_count,
8736 W_WINCOL(wp), (int)W_ENDCOL(wp),
8737 ' ', ' ', 0);
8738 }
8739
8740 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8741 == FAIL)
8742 {
8743 /* deletion will have messed up other windows */
8744 if (did_delete)
8745 {
8746#ifdef FEAT_WINDOWS
8747 wp->w_redr_status = TRUE;
8748#endif
8749 win_rest_invalid(W_NEXT(wp));
8750 }
8751 return FAIL;
8752 }
8753
8754 return OK;
8755}
8756
8757/*
8758 * delete "line_count" window lines at "row" in window "wp"
8759 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8760 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8761 * scrolling
8762 * Return OK for success, FAIL if the lines are not deleted.
8763 */
8764 int
8765win_del_lines(wp, row, line_count, invalid, mayclear)
8766 win_T *wp;
8767 int row;
8768 int line_count;
8769 int invalid;
8770 int mayclear;
8771{
8772 int retval;
8773
8774 if (invalid)
8775 wp->w_lines_valid = 0;
8776
8777 if (line_count > wp->w_height - row)
8778 line_count = wp->w_height - row;
8779
8780 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8781 if (retval != MAYBE)
8782 return retval;
8783
8784 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8785 (int)Rows, FALSE, NULL) == FAIL)
8786 return FAIL;
8787
8788#ifdef FEAT_WINDOWS
8789 /*
8790 * If there are windows or status lines below, try to put them at the
8791 * correct place. If we can't do that, they have to be redrawn.
8792 */
8793 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8794 {
8795 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8796 line_count, (int)Rows, NULL) == FAIL)
8797 {
8798 wp->w_redr_status = TRUE;
8799 win_rest_invalid(wp->w_next);
8800 }
8801 }
8802 /*
8803 * If this is the last window and there is no status line, redraw the
8804 * command line later.
8805 */
8806 else
8807#endif
8808 redraw_cmdline = TRUE;
8809 return OK;
8810}
8811
8812/*
8813 * Common code for win_ins_lines() and win_del_lines().
8814 * Returns OK or FAIL when the work has been done.
8815 * Returns MAYBE when not finished yet.
8816 */
8817 static int
8818win_do_lines(wp, row, line_count, mayclear, del)
8819 win_T *wp;
8820 int row;
8821 int line_count;
8822 int mayclear;
8823 int del;
8824{
8825 int retval;
8826
8827 if (!redrawing() || line_count <= 0)
8828 return FAIL;
8829
8830 /* only a few lines left: redraw is faster */
8831 if (mayclear && Rows - line_count < 5
8832#ifdef FEAT_VERTSPLIT
8833 && wp->w_width == Columns
8834#endif
8835 )
8836 {
8837 screenclear(); /* will set wp->w_lines_valid to 0 */
8838 return FAIL;
8839 }
8840
8841 /*
8842 * Delete all remaining lines
8843 */
8844 if (row + line_count >= wp->w_height)
8845 {
8846 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8847 W_WINCOL(wp), (int)W_ENDCOL(wp),
8848 ' ', ' ', 0);
8849 return OK;
8850 }
8851
8852 /*
8853 * when scrolling, the message on the command line should be cleared,
8854 * otherwise it will stay there forever.
8855 */
8856 clear_cmdline = TRUE;
8857
8858 /*
8859 * If the terminal can set a scroll region, use that.
8860 * Always do this in a vertically split window. This will redraw from
8861 * ScreenLines[] when t_CV isn't defined. That's faster than using
8862 * win_line().
8863 * Don't use a scroll region when we are going to redraw the text, writing
8864 * a character in the lower right corner of the scroll region causes a
8865 * scroll-up in the DJGPP version.
8866 */
8867 if (scroll_region
8868#ifdef FEAT_VERTSPLIT
8869 || W_WIDTH(wp) != Columns
8870#endif
8871 )
8872 {
8873#ifdef FEAT_VERTSPLIT
8874 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8875#endif
8876 scroll_region_set(wp, row);
8877 if (del)
8878 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8879 wp->w_height - row, FALSE, wp);
8880 else
8881 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8882 wp->w_height - row, wp);
8883#ifdef FEAT_VERTSPLIT
8884 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8885#endif
8886 scroll_region_reset();
8887 return retval;
8888 }
8889
8890#ifdef FEAT_WINDOWS
8891 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8892 return FAIL;
8893#endif
8894
8895 return MAYBE;
8896}
8897
8898/*
8899 * window 'wp' and everything after it is messed up, mark it for redraw
8900 */
8901 static void
8902win_rest_invalid(wp)
8903 win_T *wp;
8904{
8905#ifdef FEAT_WINDOWS
8906 while (wp != NULL)
8907#else
8908 if (wp != NULL)
8909#endif
8910 {
8911 redraw_win_later(wp, NOT_VALID);
8912#ifdef FEAT_WINDOWS
8913 wp->w_redr_status = TRUE;
8914 wp = wp->w_next;
8915#endif
8916 }
8917 redraw_cmdline = TRUE;
8918}
8919
8920/*
8921 * The rest of the routines in this file perform screen manipulations. The
8922 * given operation is performed physically on the screen. The corresponding
8923 * change is also made to the internal screen image. In this way, the editor
8924 * anticipates the effect of editing changes on the appearance of the screen.
8925 * That way, when we call screenupdate a complete redraw isn't usually
8926 * necessary. Another advantage is that we can keep adding code to anticipate
8927 * screen changes, and in the meantime, everything still works.
8928 */
8929
8930/*
8931 * types for inserting or deleting lines
8932 */
8933#define USE_T_CAL 1
8934#define USE_T_CDL 2
8935#define USE_T_AL 3
8936#define USE_T_CE 4
8937#define USE_T_DL 5
8938#define USE_T_SR 6
8939#define USE_NL 7
8940#define USE_T_CD 8
8941#define USE_REDRAW 9
8942
8943/*
8944 * insert lines on the screen and update ScreenLines[]
8945 * 'end' is the line after the scrolled part. Normally it is Rows.
8946 * When scrolling region used 'off' is the offset from the top for the region.
8947 * 'row' and 'end' are relative to the start of the region.
8948 *
8949 * return FAIL for failure, OK for success.
8950 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008951 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952screen_ins_lines(off, row, line_count, end, wp)
8953 int off;
8954 int row;
8955 int line_count;
8956 int end;
8957 win_T *wp; /* NULL or window to use width from */
8958{
8959 int i;
8960 int j;
8961 unsigned temp;
8962 int cursor_row;
8963 int type;
8964 int result_empty;
8965 int can_ce = can_clear(T_CE);
8966
8967 /*
8968 * FAIL if
8969 * - there is no valid screen
8970 * - the screen has to be redrawn completely
8971 * - the line count is less than one
8972 * - the line count is more than 'ttyscroll'
8973 */
8974 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8975 return FAIL;
8976
8977 /*
8978 * There are seven ways to insert lines:
8979 * 0. When in a vertically split window and t_CV isn't set, redraw the
8980 * characters from ScreenLines[].
8981 * 1. Use T_CD (clear to end of display) if it exists and the result of
8982 * the insert is just empty lines
8983 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8984 * present or line_count > 1. It looks better if we do all the inserts
8985 * at once.
8986 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8987 * insert is just empty lines and T_CE is not present or line_count >
8988 * 1.
8989 * 4. Use T_AL (insert line) if it exists.
8990 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8991 * just empty lines.
8992 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8993 * just empty lines.
8994 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8995 * the 'da' flag is not set or we have clear line capability.
8996 * 8. redraw the characters from ScreenLines[].
8997 *
8998 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8999 * the scrollbar for the window. It does have insert line, use that if it
9000 * exists.
9001 */
9002 result_empty = (row + line_count >= end);
9003#ifdef FEAT_VERTSPLIT
9004 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9005 type = USE_REDRAW;
9006 else
9007#endif
9008 if (can_clear(T_CD) && result_empty)
9009 type = USE_T_CD;
9010 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9011 type = USE_T_CAL;
9012 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9013 type = USE_T_CDL;
9014 else if (*T_AL != NUL)
9015 type = USE_T_AL;
9016 else if (can_ce && result_empty)
9017 type = USE_T_CE;
9018 else if (*T_DL != NUL && result_empty)
9019 type = USE_T_DL;
9020 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9021 type = USE_T_SR;
9022 else
9023 return FAIL;
9024
9025 /*
9026 * For clearing the lines screen_del_lines() is used. This will also take
9027 * care of t_db if necessary.
9028 */
9029 if (type == USE_T_CD || type == USE_T_CDL ||
9030 type == USE_T_CE || type == USE_T_DL)
9031 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9032
9033 /*
9034 * If text is retained below the screen, first clear or delete as many
9035 * lines at the bottom of the window as are about to be inserted so that
9036 * the deleted lines won't later surface during a screen_del_lines.
9037 */
9038 if (*T_DB)
9039 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9040
9041#ifdef FEAT_CLIPBOARD
9042 /* Remove a modeless selection when inserting lines halfway the screen
9043 * or not the full width of the screen. */
9044 if (off + row > 0
9045# ifdef FEAT_VERTSPLIT
9046 || (wp != NULL && wp->w_width != Columns)
9047# endif
9048 )
9049 clip_clear_selection();
9050 else
9051 clip_scroll_selection(-line_count);
9052#endif
9053
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054#ifdef FEAT_GUI
9055 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9056 * scrolling is actually carried out. */
9057 gui_dont_update_cursor();
9058#endif
9059
9060 if (*T_CCS != NUL) /* cursor relative to region */
9061 cursor_row = row;
9062 else
9063 cursor_row = row + off;
9064
9065 /*
9066 * Shift LineOffset[] line_count down to reflect the inserted lines.
9067 * Clear the inserted lines in ScreenLines[].
9068 */
9069 row += off;
9070 end += off;
9071 for (i = 0; i < line_count; ++i)
9072 {
9073#ifdef FEAT_VERTSPLIT
9074 if (wp != NULL && wp->w_width != Columns)
9075 {
9076 /* need to copy part of a line */
9077 j = end - 1 - i;
9078 while ((j -= line_count) >= row)
9079 linecopy(j + line_count, j, wp);
9080 j += line_count;
9081 if (can_clear((char_u *)" "))
9082 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9083 else
9084 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9085 LineWraps[j] = FALSE;
9086 }
9087 else
9088#endif
9089 {
9090 j = end - 1 - i;
9091 temp = LineOffset[j];
9092 while ((j -= line_count) >= row)
9093 {
9094 LineOffset[j + line_count] = LineOffset[j];
9095 LineWraps[j + line_count] = LineWraps[j];
9096 }
9097 LineOffset[j + line_count] = temp;
9098 LineWraps[j + line_count] = FALSE;
9099 if (can_clear((char_u *)" "))
9100 lineclear(temp, (int)Columns);
9101 else
9102 lineinvalid(temp, (int)Columns);
9103 }
9104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105
9106 screen_stop_highlight();
9107 windgoto(cursor_row, 0);
9108
9109#ifdef FEAT_VERTSPLIT
9110 /* redraw the characters */
9111 if (type == USE_REDRAW)
9112 redraw_block(row, end, wp);
9113 else
9114#endif
9115 if (type == USE_T_CAL)
9116 {
9117 term_append_lines(line_count);
9118 screen_start(); /* don't know where cursor is now */
9119 }
9120 else
9121 {
9122 for (i = 0; i < line_count; i++)
9123 {
9124 if (type == USE_T_AL)
9125 {
9126 if (i && cursor_row != 0)
9127 windgoto(cursor_row, 0);
9128 out_str(T_AL);
9129 }
9130 else /* type == USE_T_SR */
9131 out_str(T_SR);
9132 screen_start(); /* don't know where cursor is now */
9133 }
9134 }
9135
9136 /*
9137 * With scroll-reverse and 'da' flag set we need to clear the lines that
9138 * have been scrolled down into the region.
9139 */
9140 if (type == USE_T_SR && *T_DA)
9141 {
9142 for (i = 0; i < line_count; ++i)
9143 {
9144 windgoto(off + i, 0);
9145 out_str(T_CE);
9146 screen_start(); /* don't know where cursor is now */
9147 }
9148 }
9149
9150#ifdef FEAT_GUI
9151 gui_can_update_cursor();
9152 if (gui.in_use)
9153 out_flush(); /* always flush after a scroll */
9154#endif
9155 return OK;
9156}
9157
9158/*
9159 * delete lines on the screen and update ScreenLines[]
9160 * 'end' is the line after the scrolled part. Normally it is Rows.
9161 * When scrolling region used 'off' is the offset from the top for the region.
9162 * 'row' and 'end' are relative to the start of the region.
9163 *
9164 * Return OK for success, FAIL if the lines are not deleted.
9165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 int
9167screen_del_lines(off, row, line_count, end, force, wp)
9168 int off;
9169 int row;
9170 int line_count;
9171 int end;
9172 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009173 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
9175 int j;
9176 int i;
9177 unsigned temp;
9178 int cursor_row;
9179 int cursor_end;
9180 int result_empty; /* result is empty until end of region */
9181 int can_delete; /* deleting line codes can be used */
9182 int type;
9183
9184 /*
9185 * FAIL if
9186 * - there is no valid screen
9187 * - the screen has to be redrawn completely
9188 * - the line count is less than one
9189 * - the line count is more than 'ttyscroll'
9190 */
9191 if (!screen_valid(TRUE) || line_count <= 0 ||
9192 (!force && line_count > p_ttyscroll))
9193 return FAIL;
9194
9195 /*
9196 * Check if the rest of the current region will become empty.
9197 */
9198 result_empty = row + line_count >= end;
9199
9200 /*
9201 * We can delete lines only when 'db' flag not set or when 'ce' option
9202 * available.
9203 */
9204 can_delete = (*T_DB == NUL || can_clear(T_CE));
9205
9206 /*
9207 * There are six ways to delete lines:
9208 * 0. When in a vertically split window and t_CV isn't set, redraw the
9209 * characters from ScreenLines[].
9210 * 1. Use T_CD if it exists and the result is empty.
9211 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9212 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9213 * none of the other ways work.
9214 * 4. Use T_CE (erase line) if the result is empty.
9215 * 5. Use T_DL (delete line) if it exists.
9216 * 6. redraw the characters from ScreenLines[].
9217 */
9218#ifdef FEAT_VERTSPLIT
9219 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9220 type = USE_REDRAW;
9221 else
9222#endif
9223 if (can_clear(T_CD) && result_empty)
9224 type = USE_T_CD;
9225#if defined(__BEOS__) && defined(BEOS_DR8)
9226 /*
9227 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9228 * its internal termcap... this works okay for tests which test *T_DB !=
9229 * NUL. It has the disadvantage that the user cannot use any :set t_*
9230 * command to get T_DB (back) to empty_option, only :set term=... will do
9231 * the trick...
9232 * Anyway, this hack will hopefully go away with the next OS release.
9233 * (Olaf Seibert)
9234 */
9235 else if (row == 0 && T_DB == empty_option
9236 && (line_count == 1 || *T_CDL == NUL))
9237#else
9238 else if (row == 0 && (
9239#ifndef AMIGA
9240 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9241 * up, so use delete-line command */
9242 line_count == 1 ||
9243#endif
9244 *T_CDL == NUL))
9245#endif
9246 type = USE_NL;
9247 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9248 type = USE_T_CDL;
9249 else if (can_clear(T_CE) && result_empty
9250#ifdef FEAT_VERTSPLIT
9251 && (wp == NULL || wp->w_width == Columns)
9252#endif
9253 )
9254 type = USE_T_CE;
9255 else if (*T_DL != NUL && can_delete)
9256 type = USE_T_DL;
9257 else if (*T_CDL != NUL && can_delete)
9258 type = USE_T_CDL;
9259 else
9260 return FAIL;
9261
9262#ifdef FEAT_CLIPBOARD
9263 /* Remove a modeless selection when deleting lines halfway the screen or
9264 * not the full width of the screen. */
9265 if (off + row > 0
9266# ifdef FEAT_VERTSPLIT
9267 || (wp != NULL && wp->w_width != Columns)
9268# endif
9269 )
9270 clip_clear_selection();
9271 else
9272 clip_scroll_selection(line_count);
9273#endif
9274
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275#ifdef FEAT_GUI
9276 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9277 * scrolling is actually carried out. */
9278 gui_dont_update_cursor();
9279#endif
9280
9281 if (*T_CCS != NUL) /* cursor relative to region */
9282 {
9283 cursor_row = row;
9284 cursor_end = end;
9285 }
9286 else
9287 {
9288 cursor_row = row + off;
9289 cursor_end = end + off;
9290 }
9291
9292 /*
9293 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9294 * Clear the inserted lines in ScreenLines[].
9295 */
9296 row += off;
9297 end += off;
9298 for (i = 0; i < line_count; ++i)
9299 {
9300#ifdef FEAT_VERTSPLIT
9301 if (wp != NULL && wp->w_width != Columns)
9302 {
9303 /* need to copy part of a line */
9304 j = row + i;
9305 while ((j += line_count) <= end - 1)
9306 linecopy(j - line_count, j, wp);
9307 j -= line_count;
9308 if (can_clear((char_u *)" "))
9309 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9310 else
9311 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9312 LineWraps[j] = FALSE;
9313 }
9314 else
9315#endif
9316 {
9317 /* whole width, moving the line pointers is faster */
9318 j = row + i;
9319 temp = LineOffset[j];
9320 while ((j += line_count) <= end - 1)
9321 {
9322 LineOffset[j - line_count] = LineOffset[j];
9323 LineWraps[j - line_count] = LineWraps[j];
9324 }
9325 LineOffset[j - line_count] = temp;
9326 LineWraps[j - line_count] = FALSE;
9327 if (can_clear((char_u *)" "))
9328 lineclear(temp, (int)Columns);
9329 else
9330 lineinvalid(temp, (int)Columns);
9331 }
9332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
9334 screen_stop_highlight();
9335
9336#ifdef FEAT_VERTSPLIT
9337 /* redraw the characters */
9338 if (type == USE_REDRAW)
9339 redraw_block(row, end, wp);
9340 else
9341#endif
9342 if (type == USE_T_CD) /* delete the lines */
9343 {
9344 windgoto(cursor_row, 0);
9345 out_str(T_CD);
9346 screen_start(); /* don't know where cursor is now */
9347 }
9348 else if (type == USE_T_CDL)
9349 {
9350 windgoto(cursor_row, 0);
9351 term_delete_lines(line_count);
9352 screen_start(); /* don't know where cursor is now */
9353 }
9354 /*
9355 * Deleting lines at top of the screen or scroll region: Just scroll
9356 * the whole screen (scroll region) up by outputting newlines on the
9357 * last line.
9358 */
9359 else if (type == USE_NL)
9360 {
9361 windgoto(cursor_end - 1, 0);
9362 for (i = line_count; --i >= 0; )
9363 out_char('\n'); /* cursor will remain on same line */
9364 }
9365 else
9366 {
9367 for (i = line_count; --i >= 0; )
9368 {
9369 if (type == USE_T_DL)
9370 {
9371 windgoto(cursor_row, 0);
9372 out_str(T_DL); /* delete a line */
9373 }
9374 else /* type == USE_T_CE */
9375 {
9376 windgoto(cursor_row + i, 0);
9377 out_str(T_CE); /* erase a line */
9378 }
9379 screen_start(); /* don't know where cursor is now */
9380 }
9381 }
9382
9383 /*
9384 * If the 'db' flag is set, we need to clear the lines that have been
9385 * scrolled up at the bottom of the region.
9386 */
9387 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9388 {
9389 for (i = line_count; i > 0; --i)
9390 {
9391 windgoto(cursor_end - i, 0);
9392 out_str(T_CE); /* erase a line */
9393 screen_start(); /* don't know where cursor is now */
9394 }
9395 }
9396
9397#ifdef FEAT_GUI
9398 gui_can_update_cursor();
9399 if (gui.in_use)
9400 out_flush(); /* always flush after a scroll */
9401#endif
9402
9403 return OK;
9404}
9405
9406/*
9407 * show the current mode and ruler
9408 *
9409 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9410 * If clear_cmdline is FALSE there may be a message there that needs to be
9411 * cleared only if a mode is shown.
9412 * Return the length of the message (0 if no message).
9413 */
9414 int
9415showmode()
9416{
9417 int need_clear;
9418 int length = 0;
9419 int do_mode;
9420 int attr;
9421 int nwr_save;
9422#ifdef FEAT_INS_EXPAND
9423 int sub_attr;
9424#endif
9425
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009426 do_mode = ((p_smd && msg_silent == 0)
9427 && ((State & INSERT)
9428 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429#ifdef FEAT_VISUAL
9430 || VIsual_active
9431#endif
9432 ));
9433 if (do_mode || Recording)
9434 {
9435 /*
9436 * Don't show mode right now, when not redrawing or inside a mapping.
9437 * Call char_avail() only when we are going to show something, because
9438 * it takes a bit of time.
9439 */
9440 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9441 {
9442 redraw_cmdline = TRUE; /* show mode later */
9443 return 0;
9444 }
9445
9446 nwr_save = need_wait_return;
9447
9448 /* wait a bit before overwriting an important message */
9449 check_for_delay(FALSE);
9450
9451 /* if the cmdline is more than one line high, erase top lines */
9452 need_clear = clear_cmdline;
9453 if (clear_cmdline && cmdline_row < Rows - 1)
9454 msg_clr_cmdline(); /* will reset clear_cmdline */
9455
9456 /* Position on the last line in the window, column 0 */
9457 msg_pos_mode();
9458 cursor_off();
9459 attr = hl_attr(HLF_CM); /* Highlight mode */
9460 if (do_mode)
9461 {
9462 MSG_PUTS_ATTR("--", attr);
9463#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009464 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009465# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009466 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009467# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009468 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009469# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009470 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009471# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 MSG_PUTS_ATTR(" IM", attr);
9473# else
9474 MSG_PUTS_ATTR(" XIM", attr);
9475# endif
9476#endif
9477#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9478 if (gui.in_use)
9479 {
9480 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009481 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 }
9483#endif
9484#ifdef FEAT_INS_EXPAND
9485 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9486 {
9487 /* These messages can get long, avoid a wrap in a narrow
9488 * window. Prefer showing edit_submode_extra. */
9489 length = (Rows - msg_row) * Columns - 3;
9490 if (edit_submode_extra != NULL)
9491 length -= vim_strsize(edit_submode_extra);
9492 if (length > 0)
9493 {
9494 if (edit_submode_pre != NULL)
9495 length -= vim_strsize(edit_submode_pre);
9496 if (length - vim_strsize(edit_submode) > 0)
9497 {
9498 if (edit_submode_pre != NULL)
9499 msg_puts_attr(edit_submode_pre, attr);
9500 msg_puts_attr(edit_submode, attr);
9501 }
9502 if (edit_submode_extra != NULL)
9503 {
9504 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9505 if ((int)edit_submode_highl < (int)HLF_COUNT)
9506 sub_attr = hl_attr(edit_submode_highl);
9507 else
9508 sub_attr = attr;
9509 msg_puts_attr(edit_submode_extra, sub_attr);
9510 }
9511 }
9512 length = 0;
9513 }
9514 else
9515#endif
9516 {
9517#ifdef FEAT_VREPLACE
9518 if (State & VREPLACE_FLAG)
9519 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9520 else
9521#endif
9522 if (State & REPLACE_FLAG)
9523 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9524 else if (State & INSERT)
9525 {
9526#ifdef FEAT_RIGHTLEFT
9527 if (p_ri)
9528 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9529#endif
9530 MSG_PUTS_ATTR(_(" INSERT"), attr);
9531 }
9532 else if (restart_edit == 'I')
9533 MSG_PUTS_ATTR(_(" (insert)"), attr);
9534 else if (restart_edit == 'R')
9535 MSG_PUTS_ATTR(_(" (replace)"), attr);
9536 else if (restart_edit == 'V')
9537 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9538#ifdef FEAT_RIGHTLEFT
9539 if (p_hkmap)
9540 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9541# ifdef FEAT_FKMAP
9542 if (p_fkmap)
9543 MSG_PUTS_ATTR(farsi_text_5, attr);
9544# endif
9545#endif
9546#ifdef FEAT_KEYMAP
9547 if (State & LANGMAP)
9548 {
9549# ifdef FEAT_ARABIC
9550 if (curwin->w_p_arab)
9551 MSG_PUTS_ATTR(_(" Arabic"), attr);
9552 else
9553# endif
9554 MSG_PUTS_ATTR(_(" (lang)"), attr);
9555 }
9556#endif
9557 if ((State & INSERT) && p_paste)
9558 MSG_PUTS_ATTR(_(" (paste)"), attr);
9559
9560#ifdef FEAT_VISUAL
9561 if (VIsual_active)
9562 {
9563 char *p;
9564
9565 /* Don't concatenate separate words to avoid translation
9566 * problems. */
9567 switch ((VIsual_select ? 4 : 0)
9568 + (VIsual_mode == Ctrl_V) * 2
9569 + (VIsual_mode == 'V'))
9570 {
9571 case 0: p = N_(" VISUAL"); break;
9572 case 1: p = N_(" VISUAL LINE"); break;
9573 case 2: p = N_(" VISUAL BLOCK"); break;
9574 case 4: p = N_(" SELECT"); break;
9575 case 5: p = N_(" SELECT LINE"); break;
9576 default: p = N_(" SELECT BLOCK"); break;
9577 }
9578 MSG_PUTS_ATTR(_(p), attr);
9579 }
9580#endif
9581 MSG_PUTS_ATTR(" --", attr);
9582 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009583
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 need_clear = TRUE;
9585 }
9586 if (Recording
9587#ifdef FEAT_INS_EXPAND
9588 && edit_submode == NULL /* otherwise it gets too long */
9589#endif
9590 )
9591 {
9592 MSG_PUTS_ATTR(_("recording"), attr);
9593 need_clear = TRUE;
9594 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009595
9596 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 if (need_clear || clear_cmdline)
9598 msg_clr_eos();
9599 msg_didout = FALSE; /* overwrite this message */
9600 length = msg_col;
9601 msg_col = 0;
9602 need_wait_return = nwr_save; /* never ask for hit-return for this */
9603 }
9604 else if (clear_cmdline && msg_silent == 0)
9605 /* Clear the whole command line. Will reset "clear_cmdline". */
9606 msg_clr_cmdline();
9607
9608#ifdef FEAT_CMDL_INFO
9609# ifdef FEAT_VISUAL
9610 /* In Visual mode the size of the selected area must be redrawn. */
9611 if (VIsual_active)
9612 clear_showcmd();
9613# endif
9614
9615 /* If the last window has no status line, the ruler is after the mode
9616 * message and must be redrawn */
9617 if (redrawing()
9618# ifdef FEAT_WINDOWS
9619 && lastwin->w_status_height == 0
9620# endif
9621 )
9622 win_redr_ruler(lastwin, TRUE);
9623#endif
9624 redraw_cmdline = FALSE;
9625 clear_cmdline = FALSE;
9626
9627 return length;
9628}
9629
9630/*
9631 * Position for a mode message.
9632 */
9633 static void
9634msg_pos_mode()
9635{
9636 msg_col = 0;
9637 msg_row = Rows - 1;
9638}
9639
9640/*
9641 * Delete mode message. Used when ESC is typed which is expected to end
9642 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009643 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 */
9645 void
9646unshowmode(force)
9647 int force;
9648{
9649 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009650 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 */
9652 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9653 redraw_cmdline = TRUE; /* delete mode later */
9654 else
9655 {
9656 msg_pos_mode();
9657 if (Recording)
9658 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9659 msg_clr_eos();
9660 }
9661}
9662
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009663#if defined(FEAT_WINDOWS)
9664/*
9665 * Draw the tab pages line at the top of the Vim window.
9666 */
9667 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009668draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009669{
9670 int tabcount = 0;
9671 tabpage_T *tp;
9672 int tabwidth;
9673 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009674 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009675 int attr;
9676 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009677 win_T *cwp;
9678 int wincount;
9679 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009680 int c;
9681 int len;
9682 int attr_sel = hl_attr(HLF_TPS);
9683 int attr_nosel = hl_attr(HLF_TP);
9684 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009685 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009686 int room;
9687 int use_sep_chars = (t_colors < 8
9688#ifdef FEAT_GUI
9689 && !gui.in_use
9690#endif
9691 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009692
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009693 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009694
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009695#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009696 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009697 if (gui_use_tabline())
9698 {
9699 gui_update_tabline();
9700 return;
9701 }
9702#endif
9703
9704 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009705 return;
9706
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009707#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009708
9709 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9710 for (scol = 0; scol < Columns; ++scol)
9711 TabPageIdxs[scol] = 0;
9712
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009713 /* Use the 'tabline' option if it's set. */
9714 if (*p_tal != NUL)
9715 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009716 int save_called_emsg = called_emsg;
9717
9718 /* Check for an error. If there is one we would loop in redrawing the
9719 * screen. Avoid that by making 'tabline' empty. */
9720 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009721 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009722 if (called_emsg)
9723 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009724 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009725 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009726 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009727 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009728#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009729 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009730 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9731 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009732
Bram Moolenaar238a5642006-02-21 22:12:05 +00009733 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9734 if (tabwidth < 6)
9735 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009736
Bram Moolenaar238a5642006-02-21 22:12:05 +00009737 attr = attr_nosel;
9738 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009739 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009740 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9741 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009742 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009743 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009744
Bram Moolenaar238a5642006-02-21 22:12:05 +00009745 if (tp->tp_topframe == topframe)
9746 attr = attr_sel;
9747 if (use_sep_chars && col > 0)
9748 screen_putchar('|', 0, col++, attr);
9749
9750 if (tp->tp_topframe != topframe)
9751 attr = attr_nosel;
9752
9753 screen_putchar(' ', 0, col++, attr);
9754
9755 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009756 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009757 cwp = curwin;
9758 wp = firstwin;
9759 }
9760 else
9761 {
9762 cwp = tp->tp_curwin;
9763 wp = tp->tp_firstwin;
9764 }
9765
9766 modified = FALSE;
9767 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9768 if (bufIsChanged(wp->w_buffer))
9769 modified = TRUE;
9770 if (modified || wincount > 1)
9771 {
9772 if (wincount > 1)
9773 {
9774 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009775 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009776 if (col + len >= Columns - 3)
9777 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009778 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009779#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009780 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009781#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009782 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009783#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009784 );
9785 col += len;
9786 }
9787 if (modified)
9788 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9789 screen_putchar(' ', 0, col++, attr);
9790 }
9791
9792 room = scol - col + tabwidth - 1;
9793 if (room > 0)
9794 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009795 /* Get buffer name in NameBuff[] */
9796 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009797 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009798 len = vim_strsize(NameBuff);
9799 p = NameBuff;
9800#ifdef FEAT_MBYTE
9801 if (has_mbyte)
9802 while (len > room)
9803 {
9804 len -= ptr2cells(p);
9805 mb_ptr_adv(p);
9806 }
9807 else
9808#endif
9809 if (len > room)
9810 {
9811 p += len - room;
9812 len = room;
9813 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009814 if (len > Columns - col - 1)
9815 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009816
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009817 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009818 col += len;
9819 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009820 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009821
9822 /* Store the tab page number in TabPageIdxs[], so that
9823 * jump_to_mouse() knows where each one is. */
9824 ++tabcount;
9825 while (scol < col)
9826 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009827 }
9828
Bram Moolenaar238a5642006-02-21 22:12:05 +00009829 if (use_sep_chars)
9830 c = '_';
9831 else
9832 c = ' ';
9833 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009834
9835 /* Put an "X" for closing the current tab if there are several. */
9836 if (first_tabpage->tp_next != NULL)
9837 {
9838 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9839 TabPageIdxs[Columns - 1] = -999;
9840 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009841 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009842
9843 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9844 * set. */
9845 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009846}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009847
9848/*
9849 * Get buffer name for "buf" into NameBuff[].
9850 * Takes care of special buffer names and translates special characters.
9851 */
9852 void
9853get_trans_bufname(buf)
9854 buf_T *buf;
9855{
9856 if (buf_spname(buf) != NULL)
9857 STRCPY(NameBuff, buf_spname(buf));
9858 else
9859 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9860 trans_characters(NameBuff, MAXPATHL);
9861}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009862#endif
9863
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9865/*
9866 * Get the character to use in a status line. Get its attributes in "*attr".
9867 */
9868 static int
9869fillchar_status(attr, is_curwin)
9870 int *attr;
9871 int is_curwin;
9872{
9873 int fill;
9874 if (is_curwin)
9875 {
9876 *attr = hl_attr(HLF_S);
9877 fill = fill_stl;
9878 }
9879 else
9880 {
9881 *attr = hl_attr(HLF_SNC);
9882 fill = fill_stlnc;
9883 }
9884 /* Use fill when there is highlighting, and highlighting of current
9885 * window differs, or the fillchars differ, or this is not the
9886 * current window */
9887 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9888 || !is_curwin || firstwin == lastwin)
9889 || (fill_stl != fill_stlnc)))
9890 return fill;
9891 if (is_curwin)
9892 return '^';
9893 return '=';
9894}
9895#endif
9896
9897#ifdef FEAT_VERTSPLIT
9898/*
9899 * Get the character to use in a separator between vertically split windows.
9900 * Get its attributes in "*attr".
9901 */
9902 static int
9903fillchar_vsep(attr)
9904 int *attr;
9905{
9906 *attr = hl_attr(HLF_C);
9907 if (*attr == 0 && fill_vert == ' ')
9908 return '|';
9909 else
9910 return fill_vert;
9911}
9912#endif
9913
9914/*
9915 * Return TRUE if redrawing should currently be done.
9916 */
9917 int
9918redrawing()
9919{
9920 return (!RedrawingDisabled
9921 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9922}
9923
9924/*
9925 * Return TRUE if printing messages should currently be done.
9926 */
9927 int
9928messaging()
9929{
9930 return (!(p_lz && char_avail() && !KeyTyped));
9931}
9932
9933/*
9934 * Show current status info in ruler and various other places
9935 * If always is FALSE, only show ruler if position has changed.
9936 */
9937 void
9938showruler(always)
9939 int always;
9940{
9941 if (!always && !redrawing())
9942 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009943#ifdef FEAT_INS_EXPAND
9944 if (pum_visible())
9945 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009946# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009947 /* Don't redraw right now, do it later. */
9948 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009949# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009950 return;
9951 }
9952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009954 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009955 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009956 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 else
9959#endif
9960#ifdef FEAT_CMDL_INFO
9961 win_redr_ruler(curwin, always);
9962#endif
9963
9964#ifdef FEAT_TITLE
9965 if (need_maketitle
9966# ifdef FEAT_STL_OPT
9967 || (p_icon && (stl_syntax & STL_IN_ICON))
9968 || (p_title && (stl_syntax & STL_IN_TITLE))
9969# endif
9970 )
9971 maketitle();
9972#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009973#ifdef FEAT_WINDOWS
9974 /* Redraw the tab pages line if needed. */
9975 if (redraw_tabline)
9976 draw_tabline();
9977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978}
9979
9980#ifdef FEAT_CMDL_INFO
9981 static void
9982win_redr_ruler(wp, always)
9983 win_T *wp;
9984 int always;
9985{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009986#define RULER_BUF_LEN 70
9987 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 int row;
9989 int fillchar;
9990 int attr;
9991 int empty_line = FALSE;
9992 colnr_T virtcol;
9993 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009994 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 int o;
9996#ifdef FEAT_VERTSPLIT
9997 int this_ru_col;
9998 int off = 0;
9999 int width = Columns;
10000# define WITH_OFF(x) x
10001# define WITH_WIDTH(x) x
10002#else
10003# define WITH_OFF(x) 0
10004# define WITH_WIDTH(x) Columns
10005# define this_ru_col ru_col
10006#endif
10007
10008 /* If 'ruler' off or redrawing disabled, don't do anything */
10009 if (!p_ru)
10010 return;
10011
10012 /*
10013 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10014 * after deleting lines, before cursor.lnum is corrected.
10015 */
10016 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10017 return;
10018
10019#ifdef FEAT_INS_EXPAND
10020 /* Don't draw the ruler while doing insert-completion, it might overwrite
10021 * the (long) mode message. */
10022# ifdef FEAT_WINDOWS
10023 if (wp == lastwin && lastwin->w_status_height == 0)
10024# endif
10025 if (edit_submode != NULL)
10026 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010027 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10028 if (pum_visible())
10029 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030#endif
10031
10032#ifdef FEAT_STL_OPT
10033 if (*p_ruf)
10034 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010035 int save_called_emsg = called_emsg;
10036
10037 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010039 if (called_emsg)
10040 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010041 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010042 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 return;
10044 }
10045#endif
10046
10047 /*
10048 * Check if not in Insert mode and the line is empty (will show "0-1").
10049 */
10050 if (!(State & INSERT)
10051 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10052 empty_line = TRUE;
10053
10054 /*
10055 * Only draw the ruler when something changed.
10056 */
10057 validate_virtcol_win(wp);
10058 if ( redraw_cmdline
10059 || always
10060 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10061 || wp->w_cursor.col != wp->w_ru_cursor.col
10062 || wp->w_virtcol != wp->w_ru_virtcol
10063#ifdef FEAT_VIRTUALEDIT
10064 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10065#endif
10066 || wp->w_topline != wp->w_ru_topline
10067 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10068#ifdef FEAT_DIFF
10069 || wp->w_topfill != wp->w_ru_topfill
10070#endif
10071 || empty_line != wp->w_ru_empty)
10072 {
10073 cursor_off();
10074#ifdef FEAT_WINDOWS
10075 if (wp->w_status_height)
10076 {
10077 row = W_WINROW(wp) + wp->w_height;
10078 fillchar = fillchar_status(&attr, wp == curwin);
10079# ifdef FEAT_VERTSPLIT
10080 off = W_WINCOL(wp);
10081 width = W_WIDTH(wp);
10082# endif
10083 }
10084 else
10085#endif
10086 {
10087 row = Rows - 1;
10088 fillchar = ' ';
10089 attr = 0;
10090#ifdef FEAT_VERTSPLIT
10091 width = Columns;
10092 off = 0;
10093#endif
10094 }
10095
10096 /* In list mode virtcol needs to be recomputed */
10097 virtcol = wp->w_virtcol;
10098 if (wp->w_p_list && lcs_tab1 == NUL)
10099 {
10100 wp->w_p_list = FALSE;
10101 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10102 wp->w_p_list = TRUE;
10103 }
10104
10105 /*
10106 * Some sprintfs return the length, some return a pointer.
10107 * To avoid portability problems we use strlen() here.
10108 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010109 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10111 ? 0L
10112 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010113 len = STRLEN(buffer);
10114 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10116 (int)virtcol + 1);
10117
10118 /*
10119 * Add a "50%" if there is room for it.
10120 * On the last line, don't print in the last column (scrolls the
10121 * screen up on some terminals).
10122 */
10123 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010124 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 o = i + vim_strsize(buffer + i + 1);
10126#ifdef FEAT_WINDOWS
10127 if (wp->w_status_height == 0) /* can't use last char of screen */
10128#endif
10129 ++o;
10130#ifdef FEAT_VERTSPLIT
10131 this_ru_col = ru_col - (Columns - width);
10132 if (this_ru_col < 0)
10133 this_ru_col = 0;
10134#endif
10135 /* Never use more than half the window/screen width, leave the other
10136 * half for the filename. */
10137 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10138 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10139 if (this_ru_col + o < WITH_WIDTH(width))
10140 {
10141 while (this_ru_col + o < WITH_WIDTH(width))
10142 {
10143#ifdef FEAT_MBYTE
10144 if (has_mbyte)
10145 i += (*mb_char2bytes)(fillchar, buffer + i);
10146 else
10147#endif
10148 buffer[i++] = fillchar;
10149 ++o;
10150 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010151 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 }
10153 /* Truncate at window boundary. */
10154#ifdef FEAT_MBYTE
10155 if (has_mbyte)
10156 {
10157 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010158 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 {
10160 o += (*mb_ptr2cells)(buffer + i);
10161 if (this_ru_col + o > WITH_WIDTH(width))
10162 {
10163 buffer[i] = NUL;
10164 break;
10165 }
10166 }
10167 }
10168 else
10169#endif
10170 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10171 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10172
10173 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10174 i = redraw_cmdline;
10175 screen_fill(row, row + 1,
10176 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10177 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10178 fillchar, fillchar, attr);
10179 /* don't redraw the cmdline because of showing the ruler */
10180 redraw_cmdline = i;
10181 wp->w_ru_cursor = wp->w_cursor;
10182 wp->w_ru_virtcol = wp->w_virtcol;
10183 wp->w_ru_empty = empty_line;
10184 wp->w_ru_topline = wp->w_topline;
10185 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10186#ifdef FEAT_DIFF
10187 wp->w_ru_topfill = wp->w_topfill;
10188#endif
10189 }
10190}
10191#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010192
10193#if defined(FEAT_LINEBREAK) || defined(PROTO)
10194/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010195 * Return the width of the 'number' and 'relativenumber' column.
10196 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010197 * Otherwise it depends on 'numberwidth' and the line count.
10198 */
10199 int
10200number_width(wp)
10201 win_T *wp;
10202{
10203 int n;
10204 linenr_T lnum;
10205
Bram Moolenaar64486672010-05-16 15:46:46 +020010206 if (wp->w_p_nu)
10207 /* 'number' */
10208 lnum = wp->w_buffer->b_ml.ml_line_count;
10209 else
10210 /* 'relativenumber' */
10211 lnum = wp->w_height;
10212
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010213 if (lnum == wp->w_nrwidth_line_count)
10214 return wp->w_nrwidth_width;
10215 wp->w_nrwidth_line_count = lnum;
10216
10217 n = 0;
10218 do
10219 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010220 lnum /= 10;
10221 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010222 } while (lnum > 0);
10223
10224 /* 'numberwidth' gives the minimal width plus one */
10225 if (n < wp->w_p_nuw - 1)
10226 n = wp->w_p_nuw - 1;
10227
10228 wp->w_nrwidth_width = n;
10229 return n;
10230}
10231#endif