blob: 72a93da64b820820cef9d7c90942e9a6acdc6612 [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));
142static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
143static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
144#endif
145static void screen_start_highlight __ARGS((int attr));
146static void screen_char __ARGS((unsigned off, int row, int col));
147#ifdef FEAT_MBYTE
148static void screen_char_2 __ARGS((unsigned off, int row, int col));
149#endif
150static void screenclear2 __ARGS((void));
151static void lineclear __ARGS((unsigned off, int width));
152static void lineinvalid __ARGS((unsigned off, int width));
153#ifdef FEAT_VERTSPLIT
154static void linecopy __ARGS((int to, int from, win_T *wp));
155static void redraw_block __ARGS((int row, int end, win_T *wp));
156#endif
157static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
158static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000160#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000161static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
164static int fillchar_status __ARGS((int *attr, int is_curwin));
165#endif
166#ifdef FEAT_VERTSPLIT
167static int fillchar_vsep __ARGS((int *attr));
168#endif
169#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000170static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#ifdef FEAT_CMDL_INFO
173static void win_redr_ruler __ARGS((win_T *wp, int always));
174#endif
175
176#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
177/* Ugly global: overrule attribute used by screen_char() */
178static int screen_char_attr = 0;
179#endif
180
181/*
182 * Redraw the current window later, with update_screen(type).
183 * Set must_redraw only if not already set to a higher value.
184 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
185 */
186 void
187redraw_later(type)
188 int type;
189{
190 redraw_win_later(curwin, type);
191}
192
193 void
194redraw_win_later(wp, type)
195 win_T *wp;
196 int type;
197{
198 if (wp->w_redr_type < type)
199 {
200 wp->w_redr_type = type;
201 if (type >= NOT_VALID)
202 wp->w_lines_valid = 0;
203 if (must_redraw < type) /* must_redraw is the maximum of all windows */
204 must_redraw = type;
205 }
206}
207
208/*
209 * Force a complete redraw later. Also resets the highlighting. To be used
210 * after executing a shell command that messes up the screen.
211 */
212 void
213redraw_later_clear()
214{
215 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000216#ifdef FEAT_GUI
217 if (gui.in_use)
218 /* Use a code that will reset gui.highlight_mask in
219 * gui_stop_highlight(). */
220 screen_attr = HL_ALL + 1;
221 else
222#endif
223 /* Use attributes that is very unlikely to appear in text. */
224 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225}
226
227/*
228 * Mark all windows to be redrawn later.
229 */
230 void
231redraw_all_later(type)
232 int type;
233{
234 win_T *wp;
235
236 FOR_ALL_WINDOWS(wp)
237 {
238 redraw_win_later(wp, type);
239 }
240}
241
242/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000243 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 */
245 void
246redraw_curbuf_later(type)
247 int type;
248{
249 redraw_buf_later(curbuf, type);
250}
251
252 void
253redraw_buf_later(buf, type)
254 buf_T *buf;
255 int type;
256{
257 win_T *wp;
258
259 FOR_ALL_WINDOWS(wp)
260 {
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
263 }
264}
265
266/*
267 * Changed something in the current window, at buffer line "lnum", that
268 * requires that line and possibly other lines to be redrawn.
269 * Used when entering/leaving Insert mode with the cursor on a folded line.
270 * Used to remove the "$" from a change command.
271 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
272 * may become invalid and the whole window will have to be redrawn.
273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 void
275redrawWinline(lnum, invalid)
276 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000277 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278{
279#ifdef FEAT_FOLDING
280 int i;
281#endif
282
283 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
284 curwin->w_redraw_top = lnum;
285 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
286 curwin->w_redraw_bot = lnum;
287 redraw_later(VALID);
288
289#ifdef FEAT_FOLDING
290 if (invalid)
291 {
292 /* A w_lines[] entry for this lnum has become invalid. */
293 i = find_wl_entry(curwin, lnum);
294 if (i >= 0)
295 curwin->w_lines[i].wl_valid = FALSE;
296 }
297#endif
298}
299
300/*
301 * update all windows that are editing the current buffer
302 */
303 void
304update_curbuf(type)
305 int type;
306{
307 redraw_curbuf_later(type);
308 update_screen(type);
309}
310
311/*
312 * update_screen()
313 *
314 * Based on the current value of curwin->w_topline, transfer a screenfull
315 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
316 */
317 void
318update_screen(type)
319 int type;
320{
321 win_T *wp;
322 static int did_intro = FALSE;
323#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
324 int did_one;
325#endif
326
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000327 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 if (!screen_valid(TRUE))
329 return;
330
331 if (must_redraw)
332 {
333 if (type < must_redraw) /* use maximal type */
334 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000335
336 /* must_redraw is reset here, so that when we run into some weird
337 * reason to redraw while busy redrawing (e.g., asynchronous
338 * scrolling), or update_topline() in win_update() will cause a
339 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 must_redraw = 0;
341 }
342
343 /* Need to update w_lines[]. */
344 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
345 type = NOT_VALID;
346
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000347 /* Postpone the redrawing when it's not needed and when being called
348 * recursively. */
349 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 {
351 redraw_later(type); /* remember type for next time */
352 must_redraw = type;
353 if (type > INVERTED_ALL)
354 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
355 return;
356 }
357
358 updating_screen = TRUE;
359#ifdef FEAT_SYN_HL
360 ++display_tick; /* let syntax code know we're in a next round of
361 * display updating */
362#endif
363
364 /*
365 * if the screen was scrolled up when displaying a message, scroll it down
366 */
367 if (msg_scrolled)
368 {
369 clear_cmdline = TRUE;
370 if (msg_scrolled > Rows - 5) /* clearing is faster */
371 type = CLEAR;
372 else if (type != CLEAR)
373 {
374 check_for_delay(FALSE);
375 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
376 type = CLEAR;
377 FOR_ALL_WINDOWS(wp)
378 {
379 if (W_WINROW(wp) < msg_scrolled)
380 {
381 if (W_WINROW(wp) + wp->w_height > msg_scrolled
382 && wp->w_redr_type < REDRAW_TOP
383 && wp->w_lines_valid > 0
384 && wp->w_topline == wp->w_lines[0].wl_lnum)
385 {
386 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
387 wp->w_redr_type = REDRAW_TOP;
388 }
389 else
390 {
391 wp->w_redr_type = NOT_VALID;
392#ifdef FEAT_WINDOWS
393 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
394 <= msg_scrolled)
395 wp->w_redr_status = TRUE;
396#endif
397 }
398 }
399 }
400 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000401#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000402 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 }
405 msg_scrolled = 0;
406 need_wait_return = FALSE;
407 }
408
409 /* reset cmdline_row now (may have been changed temporarily) */
410 compute_cmdrow();
411
412 /* Check for changed highlighting */
413 if (need_highlight_changed)
414 highlight_changed();
415
416 if (type == CLEAR) /* first clear screen */
417 {
418 screenclear(); /* will reset clear_cmdline */
419 type = NOT_VALID;
420 }
421
422 if (clear_cmdline) /* going to clear cmdline (done below) */
423 check_for_delay(FALSE);
424
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000425#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200426 /* Force redraw when width of 'number' or 'relativenumber' column
427 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000428 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200429 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
430 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000431 curwin->w_redr_type = NOT_VALID;
432#endif
433
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 /*
435 * Only start redrawing if there is really something to do.
436 */
437 if (type == INVERTED)
438 update_curswant();
439 if (curwin->w_redr_type < type
440 && !((type == VALID
441 && curwin->w_lines[0].wl_valid
442#ifdef FEAT_DIFF
443 && curwin->w_topfill == curwin->w_old_topfill
444 && curwin->w_botfill == curwin->w_old_botfill
445#endif
446 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
447#ifdef FEAT_VISUAL
448 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000449 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
451 && curwin->w_old_visual_mode == VIsual_mode
452 && (curwin->w_valid & VALID_VIRTCOL)
453 && curwin->w_old_curswant == curwin->w_curswant)
454#endif
455 ))
456 curwin->w_redr_type = type;
457
Bram Moolenaar5a305422006-04-28 22:38:25 +0000458#ifdef FEAT_WINDOWS
459 /* Redraw the tab pages line if needed. */
460 if (redraw_tabline || type >= NOT_VALID)
461 draw_tabline();
462#endif
463
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#ifdef FEAT_SYN_HL
465 /*
466 * Correct stored syntax highlighting info for changes in each displayed
467 * buffer. Each buffer must only be done once.
468 */
469 FOR_ALL_WINDOWS(wp)
470 {
471 if (wp->w_buffer->b_mod_set)
472 {
473# ifdef FEAT_WINDOWS
474 win_T *wwp;
475
476 /* Check if we already did this buffer. */
477 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
478 if (wwp->w_buffer == wp->w_buffer)
479 break;
480# endif
481 if (
482# ifdef FEAT_WINDOWS
483 wwp == wp &&
484# endif
485 syntax_present(wp->w_buffer))
486 syn_stack_apply_changes(wp->w_buffer);
487 }
488 }
489#endif
490
491 /*
492 * Go from top to bottom through the windows, redrawing the ones that need
493 * it.
494 */
495#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
496 did_one = FALSE;
497#endif
498#ifdef FEAT_SEARCH_EXTRA
499 search_hl.rm.regprog = NULL;
500#endif
501 FOR_ALL_WINDOWS(wp)
502 {
503 if (wp->w_redr_type != 0)
504 {
505 cursor_off();
506#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
507 if (!did_one)
508 {
509 did_one = TRUE;
510# ifdef FEAT_SEARCH_EXTRA
511 start_search_hl();
512# endif
513# ifdef FEAT_CLIPBOARD
514 /* When Visual area changed, may have to update selection. */
515 if (clip_star.available && clip_isautosel())
516 clip_update_selection();
517# endif
518#ifdef FEAT_GUI
519 /* Remove the cursor before starting to do anything, because
520 * scrolling may make it difficult to redraw the text under
521 * it. */
522 if (gui.in_use)
523 gui_undraw_cursor();
524#endif
525 }
526#endif
527 win_update(wp);
528 }
529
530#ifdef FEAT_WINDOWS
531 /* redraw status line after the window to minimize cursor movement */
532 if (wp->w_redr_status)
533 {
534 cursor_off();
535 win_redr_status(wp);
536 }
537#endif
538 }
539#if defined(FEAT_SEARCH_EXTRA)
540 end_search_hl();
541#endif
542
543#ifdef FEAT_WINDOWS
544 /* Reset b_mod_set flags. Going through all windows is probably faster
545 * than going through all buffers (there could be many buffers). */
546 for (wp = firstwin; wp != NULL; wp = wp->w_next)
547 wp->w_buffer->b_mod_set = FALSE;
548#else
549 curbuf->b_mod_set = FALSE;
550#endif
551
552 updating_screen = FALSE;
553#ifdef FEAT_GUI
554 gui_may_resize_shell();
555#endif
556
557 /* Clear or redraw the command line. Done last, because scrolling may
558 * mess up the command line. */
559 if (clear_cmdline || redraw_cmdline)
560 showmode();
561
562 /* May put up an introductory message when not editing a file */
563 if (!did_intro && bufempty()
564 && curbuf->b_fname == NULL
565#ifdef FEAT_WINDOWS
566 && firstwin->w_next == NULL
567#endif
568 && vim_strchr(p_shm, SHM_INTRO) == NULL)
569 intro_message(FALSE);
570 did_intro = TRUE;
571
572#ifdef FEAT_GUI
573 /* Redraw the cursor and update the scrollbars when all screen updating is
574 * done. */
575 if (gui.in_use)
576 {
577 out_flush(); /* required before updating the cursor */
578 if (did_one)
579 gui_update_cursor(FALSE, FALSE);
580 gui_update_scrollbars(FALSE);
581 }
582#endif
583}
584
585#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
586static void update_prepare __ARGS((void));
587static void update_finish __ARGS((void));
588
589/*
590 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000591 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 */
593 static void
594update_prepare()
595{
596 cursor_off();
597 updating_screen = TRUE;
598#ifdef FEAT_GUI
599 /* Remove the cursor before starting to do anything, because scrolling may
600 * make it difficult to redraw the text under it. */
601 if (gui.in_use)
602 gui_undraw_cursor();
603#endif
604#ifdef FEAT_SEARCH_EXTRA
605 start_search_hl();
606#endif
607}
608
609/*
610 * Finish updating one or more windows.
611 */
612 static void
613update_finish()
614{
615 if (redraw_cmdline)
616 showmode();
617
618# ifdef FEAT_SEARCH_EXTRA
619 end_search_hl();
620# endif
621
622 updating_screen = FALSE;
623
624# ifdef FEAT_GUI
625 gui_may_resize_shell();
626
627 /* Redraw the cursor and update the scrollbars when all screen updating is
628 * done. */
629 if (gui.in_use)
630 {
631 out_flush(); /* required before updating the cursor */
632 gui_update_cursor(FALSE, FALSE);
633 gui_update_scrollbars(FALSE);
634 }
635# endif
636}
637#endif
638
639#if defined(FEAT_SIGNS) || defined(PROTO)
640 void
641update_debug_sign(buf, lnum)
642 buf_T *buf;
643 linenr_T lnum;
644{
645 win_T *wp;
646 int doit = FALSE;
647
648# ifdef FEAT_FOLDING
649 win_foldinfo.fi_level = 0;
650# endif
651
652 /* update/delete a specific mark */
653 FOR_ALL_WINDOWS(wp)
654 {
655 if (buf != NULL && lnum > 0)
656 {
657 if (wp->w_buffer == buf && lnum >= wp->w_topline
658 && lnum < wp->w_botline)
659 {
660 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
661 wp->w_redraw_top = lnum;
662 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
663 wp->w_redraw_bot = lnum;
664 redraw_win_later(wp, VALID);
665 }
666 }
667 else
668 redraw_win_later(wp, VALID);
669 if (wp->w_redr_type != 0)
670 doit = TRUE;
671 }
672
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000673 /* Return when there is nothing to do or screen updating already
674 * happening. */
675 if (!doit || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 return;
677
678 /* update all windows that need updating */
679 update_prepare();
680
681# ifdef FEAT_WINDOWS
682 for (wp = firstwin; wp; wp = wp->w_next)
683 {
684 if (wp->w_redr_type != 0)
685 win_update(wp);
686 if (wp->w_redr_status)
687 win_redr_status(wp);
688 }
689# else
690 if (curwin->w_redr_type != 0)
691 win_update(curwin);
692# endif
693
694 update_finish();
695}
696#endif
697
698
699#if defined(FEAT_GUI) || defined(PROTO)
700/*
701 * Update a single window, its status line and maybe the command line msg.
702 * Used for the GUI scrollbar.
703 */
704 void
705updateWindow(wp)
706 win_T *wp;
707{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000708 /* return if already busy updating */
709 if (updating_screen)
710 return;
711
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 update_prepare();
713
714#ifdef FEAT_CLIPBOARD
715 /* When Visual area changed, may have to update selection. */
716 if (clip_star.available && clip_isautosel())
717 clip_update_selection();
718#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000723 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000724 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000726
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 if (wp->w_redr_status
728# ifdef FEAT_CMDL_INFO
729 || p_ru
730# endif
731# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000732 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733# endif
734 )
735 win_redr_status(wp);
736#endif
737
738 update_finish();
739}
740#endif
741
742/*
743 * Update a single window.
744 *
745 * This may cause the windows below it also to be redrawn (when clearing the
746 * screen or scrolling lines).
747 *
748 * How the window is redrawn depends on wp->w_redr_type. Each type also
749 * implies the one below it.
750 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000751 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
753 * INVERTED redraw the changed part of the Visual area
754 * INVERTED_ALL redraw the whole Visual area
755 * VALID 1. scroll up/down to adjust for a changed w_topline
756 * 2. update lines at the top when scrolled down
757 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000758 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * b_mod_top and b_mod_bot.
760 * - if wp->w_redraw_top non-zero, redraw lines between
761 * wp->w_redraw_top and wp->w_redr_bot.
762 * - continue redrawing when syntax status is invalid.
763 * 4. if scrolled up, update lines at the bottom.
764 * This results in three areas that may need updating:
765 * top: from first row to top_end (when scrolled down)
766 * mid: from mid_start to mid_end (update inversion or changed text)
767 * bot: from bot_start to last row (when scrolled up)
768 */
769 static void
770win_update(wp)
771 win_T *wp;
772{
773 buf_T *buf = wp->w_buffer;
774 int type;
775 int top_end = 0; /* Below last row of the top area that needs
776 updating. 0 when no top area updating. */
777 int mid_start = 999;/* first row of the mid area that needs
778 updating. 999 when no mid area updating. */
779 int mid_end = 0; /* Below last row of the mid area that needs
780 updating. 0 when no mid area updating. */
781 int bot_start = 999;/* first row of the bot area that needs
782 updating. 999 when no bot area updating */
783#ifdef FEAT_VISUAL
784 int scrolled_down = FALSE; /* TRUE when scrolled down when
785 w_topline got smaller a bit */
786#endif
787#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000788 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 int top_to_mod = FALSE; /* redraw above mod_top */
790#endif
791
792 int row; /* current window row to display */
793 linenr_T lnum; /* current buffer lnum to display */
794 int idx; /* current index in w_lines[] */
795 int srow; /* starting row of the current line */
796
797 int eof = FALSE; /* if TRUE, we hit the end of the file */
798 int didline = FALSE; /* if TRUE, we finished the last line */
799 int i;
800 long j;
801 static int recursive = FALSE; /* being called recursively */
802 int old_botline = wp->w_botline;
803#ifdef FEAT_FOLDING
804 long fold_count;
805#endif
806#ifdef FEAT_SYN_HL
807 /* remember what happened to the previous line, to know if
808 * check_visual_highlight() can be used */
809#define DID_NONE 1 /* didn't update a line */
810#define DID_LINE 2 /* updated a normal line */
811#define DID_FOLD 3 /* updated a folded line */
812 int did_update = DID_NONE;
813 linenr_T syntax_last_parsed = 0; /* last parsed text line */
814#endif
815 linenr_T mod_top = 0;
816 linenr_T mod_bot = 0;
817#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
818 int save_got_int;
819#endif
820
821 type = wp->w_redr_type;
822
823 if (type == NOT_VALID)
824 {
825#ifdef FEAT_WINDOWS
826 wp->w_redr_status = TRUE;
827#endif
828 wp->w_lines_valid = 0;
829 }
830
831 /* Window is zero-height: nothing to draw. */
832 if (wp->w_height == 0)
833 {
834 wp->w_redr_type = 0;
835 return;
836 }
837
838#ifdef FEAT_VERTSPLIT
839 /* Window is zero-width: Only need to draw the separator. */
840 if (wp->w_width == 0)
841 {
842 /* draw the vertical separator right of this window */
843 draw_vsep_win(wp, 0);
844 wp->w_redr_type = 0;
845 return;
846 }
847#endif
848
849#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000850 /* Setup for match and 'hlsearch' highlighting. Disable any previous
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000851 * match */
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000852 cur = wp->w_match_head;
853 while (cur != NULL)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000854 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000855 cur->hl.rm = cur->match;
856 if (cur->hlg_id == 0)
857 cur->hl.attr = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000858 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000859 cur->hl.attr = syn_id2attr(cur->hlg_id);
860 cur->hl.buf = buf;
861 cur->hl.lnum = 0;
862 cur->hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000863# ifdef FEAT_RELTIME
864 /* Set the time limit to 'redrawtime'. */
865 profile_setlimit(p_rdt, &(cur->hl.tm));
866# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000867 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 search_hl.buf = buf;
870 search_hl.lnum = 0;
871 search_hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000872 /* time limit is set at the toplevel, for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif
874
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000875#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200876 /* Force redraw when width of 'number' or 'relativenumber' column
877 * changes. */
878 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000879 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000880 {
881 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000882 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000883 }
884 else
885#endif
886
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
888 {
889 /*
890 * When there are both inserted/deleted lines and specific lines to be
891 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
892 * everything (only happens when redrawing is off for while).
893 */
894 type = NOT_VALID;
895 }
896 else
897 {
898 /*
899 * Set mod_top to the first line that needs displaying because of
900 * changes. Set mod_bot to the first line after the changes.
901 */
902 mod_top = wp->w_redraw_top;
903 if (wp->w_redraw_bot != 0)
904 mod_bot = wp->w_redraw_bot + 1;
905 else
906 mod_bot = 0;
907 wp->w_redraw_top = 0; /* reset for next time */
908 wp->w_redraw_bot = 0;
909 if (buf->b_mod_set)
910 {
911 if (mod_top == 0 || mod_top > buf->b_mod_top)
912 {
913 mod_top = buf->b_mod_top;
914#ifdef FEAT_SYN_HL
915 /* Need to redraw lines above the change that may be included
916 * in a pattern match. */
917 if (syntax_present(buf))
918 {
919 mod_top -= buf->b_syn_sync_linebreaks;
920 if (mod_top < 1)
921 mod_top = 1;
922 }
923#endif
924 }
925 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
926 mod_bot = buf->b_mod_bot;
927
928#ifdef FEAT_SEARCH_EXTRA
929 /* When 'hlsearch' is on and using a multi-line search pattern, a
930 * change in one line may make the Search highlighting in a
931 * previous line invalid. Simple solution: redraw all visible
932 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000933 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000935 if (search_hl.rm.regprog != NULL
936 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000938 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000939 {
940 cur = wp->w_match_head;
941 while (cur != NULL)
942 {
943 if (cur->match.regprog != NULL
944 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000945 {
946 top_to_mod = TRUE;
947 break;
948 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000949 cur = cur->next;
950 }
951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952#endif
953 }
954#ifdef FEAT_FOLDING
955 if (mod_top != 0 && hasAnyFolding(wp))
956 {
957 linenr_T lnumt, lnumb;
958
959 /*
960 * A change in a line can cause lines above it to become folded or
961 * unfolded. Find the top most buffer line that may be affected.
962 * If the line was previously folded and displayed, get the first
963 * line of that fold. If the line is folded now, get the first
964 * folded line. Use the minimum of these two.
965 */
966
967 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
968 * the line below it. If there is no valid entry, use w_topline.
969 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
970 * to this line. If there is no valid entry, use MAXLNUM. */
971 lnumt = wp->w_topline;
972 lnumb = MAXLNUM;
973 for (i = 0; i < wp->w_lines_valid; ++i)
974 if (wp->w_lines[i].wl_valid)
975 {
976 if (wp->w_lines[i].wl_lastlnum < mod_top)
977 lnumt = wp->w_lines[i].wl_lastlnum + 1;
978 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
979 {
980 lnumb = wp->w_lines[i].wl_lnum;
981 /* When there is a fold column it might need updating
982 * in the next line ("J" just above an open fold). */
983 if (wp->w_p_fdc > 0)
984 ++lnumb;
985 }
986 }
987
988 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
989 if (mod_top > lnumt)
990 mod_top = lnumt;
991
992 /* Now do the same for the bottom line (one above mod_bot). */
993 --mod_bot;
994 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
995 ++mod_bot;
996 if (mod_bot < lnumb)
997 mod_bot = lnumb;
998 }
999#endif
1000
1001 /* When a change starts above w_topline and the end is below
1002 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001003 * If the end of the change is above w_topline: do like no change was
1004 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (mod_top != 0 && mod_top < wp->w_topline)
1006 {
1007 if (mod_bot > wp->w_topline)
1008 mod_top = wp->w_topline;
1009#ifdef FEAT_SYN_HL
1010 else if (syntax_present(buf))
1011 top_end = 1;
1012#endif
1013 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001014
1015 /* When line numbers are displayed need to redraw all lines below
1016 * inserted/deleted lines. */
1017 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1018 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020
1021 /*
1022 * When only displaying the lines at the top, set top_end. Used when
1023 * window has scrolled down for msg_scrolled.
1024 */
1025 if (type == REDRAW_TOP)
1026 {
1027 j = 0;
1028 for (i = 0; i < wp->w_lines_valid; ++i)
1029 {
1030 j += wp->w_lines[i].wl_size;
1031 if (j >= wp->w_upd_rows)
1032 {
1033 top_end = j;
1034 break;
1035 }
1036 }
1037 if (top_end == 0)
1038 /* not found (cannot happen?): redraw everything */
1039 type = NOT_VALID;
1040 else
1041 /* top area defined, the rest is VALID */
1042 type = VALID;
1043 }
1044
Bram Moolenaar367329b2007-08-30 11:53:22 +00001045 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001046 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1047 * non-zero and thus not FALSE) will indicate that screenclear() was not
1048 * called. */
1049 if (screen_cleared)
1050 screen_cleared = MAYBE;
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 /*
1053 * If there are no changes on the screen that require a complete redraw,
1054 * handle three cases:
1055 * 1: we are off the top of the screen by a few lines: scroll down
1056 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1057 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1058 * w_lines[] that needs updating.
1059 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001060 if ((type == VALID || type == SOME_VALID
1061 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#ifdef FEAT_DIFF
1063 && !wp->w_botfill && !wp->w_old_botfill
1064#endif
1065 )
1066 {
1067 if (mod_top != 0 && wp->w_topline == mod_top)
1068 {
1069 /*
1070 * w_topline is the first changed line, the scrolling will be done
1071 * further down.
1072 */
1073 }
1074 else if (wp->w_lines[0].wl_valid
1075 && (wp->w_topline < wp->w_lines[0].wl_lnum
1076#ifdef FEAT_DIFF
1077 || (wp->w_topline == wp->w_lines[0].wl_lnum
1078 && wp->w_topfill > wp->w_old_topfill)
1079#endif
1080 ))
1081 {
1082 /*
1083 * New topline is above old topline: May scroll down.
1084 */
1085#ifdef FEAT_FOLDING
1086 if (hasAnyFolding(wp))
1087 {
1088 linenr_T ln;
1089
1090 /* count the number of lines we are off, counting a sequence
1091 * of folded lines as one */
1092 j = 0;
1093 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1094 {
1095 ++j;
1096 if (j >= wp->w_height - 2)
1097 break;
1098 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1099 }
1100 }
1101 else
1102#endif
1103 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1104 if (j < wp->w_height - 2) /* not too far off */
1105 {
1106 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1107#ifdef FEAT_DIFF
1108 /* insert extra lines for previously invisible filler lines */
1109 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1110 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1111 - wp->w_old_topfill;
1112#endif
1113 if (i < wp->w_height - 2) /* less than a screen off */
1114 {
1115 /*
1116 * Try to insert the correct number of lines.
1117 * If not the last window, delete the lines at the bottom.
1118 * win_ins_lines may fail when the terminal can't do it.
1119 */
1120 if (i > 0)
1121 check_for_delay(FALSE);
1122 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1123 {
1124 if (wp->w_lines_valid != 0)
1125 {
1126 /* Need to update rows that are new, stop at the
1127 * first one that scrolled down. */
1128 top_end = i;
1129#ifdef FEAT_VISUAL
1130 scrolled_down = TRUE;
1131#endif
1132
1133 /* Move the entries that were scrolled, disable
1134 * the entries for the lines to be redrawn. */
1135 if ((wp->w_lines_valid += j) > wp->w_height)
1136 wp->w_lines_valid = wp->w_height;
1137 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1138 wp->w_lines[idx] = wp->w_lines[idx - j];
1139 while (idx >= 0)
1140 wp->w_lines[idx--].wl_valid = FALSE;
1141 }
1142 }
1143 else
1144 mid_start = 0; /* redraw all lines */
1145 }
1146 else
1147 mid_start = 0; /* redraw all lines */
1148 }
1149 else
1150 mid_start = 0; /* redraw all lines */
1151 }
1152 else
1153 {
1154 /*
1155 * New topline is at or below old topline: May scroll up.
1156 * When topline didn't change, find first entry in w_lines[] that
1157 * needs updating.
1158 */
1159
1160 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1161 j = -1;
1162 row = 0;
1163 for (i = 0; i < wp->w_lines_valid; i++)
1164 {
1165 if (wp->w_lines[i].wl_valid
1166 && wp->w_lines[i].wl_lnum == wp->w_topline)
1167 {
1168 j = i;
1169 break;
1170 }
1171 row += wp->w_lines[i].wl_size;
1172 }
1173 if (j == -1)
1174 {
1175 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1176 * lines */
1177 mid_start = 0;
1178 }
1179 else
1180 {
1181 /*
1182 * Try to delete the correct number of lines.
1183 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1184 */
1185#ifdef FEAT_DIFF
1186 /* If the topline didn't change, delete old filler lines,
1187 * otherwise delete filler lines of the new topline... */
1188 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1189 row += wp->w_old_topfill;
1190 else
1191 row += diff_check_fill(wp, wp->w_topline);
1192 /* ... but don't delete new filler lines. */
1193 row -= wp->w_topfill;
1194#endif
1195 if (row > 0)
1196 {
1197 check_for_delay(FALSE);
1198 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1199 bot_start = wp->w_height - row;
1200 else
1201 mid_start = 0; /* redraw all lines */
1202 }
1203 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1204 {
1205 /*
1206 * Skip the lines (below the deleted lines) that are still
1207 * valid and don't need redrawing. Copy their info
1208 * upwards, to compensate for the deleted lines. Set
1209 * bot_start to the first row that needs redrawing.
1210 */
1211 bot_start = 0;
1212 idx = 0;
1213 for (;;)
1214 {
1215 wp->w_lines[idx] = wp->w_lines[j];
1216 /* stop at line that didn't fit, unless it is still
1217 * valid (no lines deleted) */
1218 if (row > 0 && bot_start + row
1219 + (int)wp->w_lines[j].wl_size > wp->w_height)
1220 {
1221 wp->w_lines_valid = idx + 1;
1222 break;
1223 }
1224 bot_start += wp->w_lines[idx++].wl_size;
1225
1226 /* stop at the last valid entry in w_lines[].wl_size */
1227 if (++j >= wp->w_lines_valid)
1228 {
1229 wp->w_lines_valid = idx;
1230 break;
1231 }
1232 }
1233#ifdef FEAT_DIFF
1234 /* Correct the first entry for filler lines at the top
1235 * when it won't get updated below. */
1236 if (wp->w_p_diff && bot_start > 0)
1237 wp->w_lines[0].wl_size =
1238 plines_win_nofill(wp, wp->w_topline, TRUE)
1239 + wp->w_topfill;
1240#endif
1241 }
1242 }
1243 }
1244
1245 /* When starting redraw in the first line, redraw all lines. When
1246 * there is only one window it's probably faster to clear the screen
1247 * first. */
1248 if (mid_start == 0)
1249 {
1250 mid_end = wp->w_height;
1251 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001252 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001253 /* Clear the screen when it was not done by win_del_lines() or
1254 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1255 * then. */
1256 if (screen_cleared != TRUE)
1257 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001258#ifdef FEAT_WINDOWS
1259 /* The screen was cleared, redraw the tab pages line. */
1260 if (redraw_tabline)
1261 draw_tabline();
1262#endif
1263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001265
1266 /* When win_del_lines() or win_ins_lines() caused the screen to be
1267 * cleared (only happens for the first window) or when screenclear()
1268 * was called directly above, "must_redraw" will have been set to
1269 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1270 if (screen_cleared == TRUE)
1271 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273 else
1274 {
1275 /* Not VALID or INVERTED: redraw all lines. */
1276 mid_start = 0;
1277 mid_end = wp->w_height;
1278 }
1279
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001280 if (type == SOME_VALID)
1281 {
1282 /* SOME_VALID: redraw all lines. */
1283 mid_start = 0;
1284 mid_end = wp->w_height;
1285 type = NOT_VALID;
1286 }
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#ifdef FEAT_VISUAL
1289 /* check if we are updating or removing the inverted part */
1290 if ((VIsual_active && buf == curwin->w_buffer)
1291 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1292 {
1293 linenr_T from, to;
1294
1295 if (VIsual_active)
1296 {
1297 if (VIsual_active
1298 && (VIsual_mode != wp->w_old_visual_mode
1299 || type == INVERTED_ALL))
1300 {
1301 /*
1302 * If the type of Visual selection changed, redraw the whole
1303 * selection. Also when the ownership of the X selection is
1304 * gained or lost.
1305 */
1306 if (curwin->w_cursor.lnum < VIsual.lnum)
1307 {
1308 from = curwin->w_cursor.lnum;
1309 to = VIsual.lnum;
1310 }
1311 else
1312 {
1313 from = VIsual.lnum;
1314 to = curwin->w_cursor.lnum;
1315 }
1316 /* redraw more when the cursor moved as well */
1317 if (wp->w_old_cursor_lnum < from)
1318 from = wp->w_old_cursor_lnum;
1319 if (wp->w_old_cursor_lnum > to)
1320 to = wp->w_old_cursor_lnum;
1321 if (wp->w_old_visual_lnum < from)
1322 from = wp->w_old_visual_lnum;
1323 if (wp->w_old_visual_lnum > to)
1324 to = wp->w_old_visual_lnum;
1325 }
1326 else
1327 {
1328 /*
1329 * Find the line numbers that need to be updated: The lines
1330 * between the old cursor position and the current cursor
1331 * position. Also check if the Visual position changed.
1332 */
1333 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1334 {
1335 from = curwin->w_cursor.lnum;
1336 to = wp->w_old_cursor_lnum;
1337 }
1338 else
1339 {
1340 from = wp->w_old_cursor_lnum;
1341 to = curwin->w_cursor.lnum;
1342 if (from == 0) /* Visual mode just started */
1343 from = to;
1344 }
1345
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001346 if (VIsual.lnum != wp->w_old_visual_lnum
1347 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
1349 if (wp->w_old_visual_lnum < from
1350 && wp->w_old_visual_lnum != 0)
1351 from = wp->w_old_visual_lnum;
1352 if (wp->w_old_visual_lnum > to)
1353 to = wp->w_old_visual_lnum;
1354 if (VIsual.lnum < from)
1355 from = VIsual.lnum;
1356 if (VIsual.lnum > to)
1357 to = VIsual.lnum;
1358 }
1359 }
1360
1361 /*
1362 * If in block mode and changed column or curwin->w_curswant:
1363 * update all lines.
1364 * First compute the actual start and end column.
1365 */
1366 if (VIsual_mode == Ctrl_V)
1367 {
1368 colnr_T fromc, toc;
1369
1370 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1371 ++toc;
1372 if (curwin->w_curswant == MAXCOL)
1373 toc = MAXCOL;
1374
1375 if (fromc != wp->w_old_cursor_fcol
1376 || toc != wp->w_old_cursor_lcol)
1377 {
1378 if (from > VIsual.lnum)
1379 from = VIsual.lnum;
1380 if (to < VIsual.lnum)
1381 to = VIsual.lnum;
1382 }
1383 wp->w_old_cursor_fcol = fromc;
1384 wp->w_old_cursor_lcol = toc;
1385 }
1386 }
1387 else
1388 {
1389 /* Use the line numbers of the old Visual area. */
1390 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1391 {
1392 from = wp->w_old_cursor_lnum;
1393 to = wp->w_old_visual_lnum;
1394 }
1395 else
1396 {
1397 from = wp->w_old_visual_lnum;
1398 to = wp->w_old_cursor_lnum;
1399 }
1400 }
1401
1402 /*
1403 * There is no need to update lines above the top of the window.
1404 */
1405 if (from < wp->w_topline)
1406 from = wp->w_topline;
1407
1408 /*
1409 * If we know the value of w_botline, use it to restrict the update to
1410 * the lines that are visible in the window.
1411 */
1412 if (wp->w_valid & VALID_BOTLINE)
1413 {
1414 if (from >= wp->w_botline)
1415 from = wp->w_botline - 1;
1416 if (to >= wp->w_botline)
1417 to = wp->w_botline - 1;
1418 }
1419
1420 /*
1421 * Find the minimal part to be updated.
1422 * Watch out for scrolling that made entries in w_lines[] invalid.
1423 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1424 * top_end; need to redraw from top_end to the "to" line.
1425 * A middle mouse click with a Visual selection may change the text
1426 * above the Visual area and reset wl_valid, do count these for
1427 * mid_end (in srow).
1428 */
1429 if (mid_start > 0)
1430 {
1431 lnum = wp->w_topline;
1432 idx = 0;
1433 srow = 0;
1434 if (scrolled_down)
1435 mid_start = top_end;
1436 else
1437 mid_start = 0;
1438 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1439 {
1440 if (wp->w_lines[idx].wl_valid)
1441 mid_start += wp->w_lines[idx].wl_size;
1442 else if (!scrolled_down)
1443 srow += wp->w_lines[idx].wl_size;
1444 ++idx;
1445# ifdef FEAT_FOLDING
1446 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1447 lnum = wp->w_lines[idx].wl_lnum;
1448 else
1449# endif
1450 ++lnum;
1451 }
1452 srow += mid_start;
1453 mid_end = wp->w_height;
1454 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1455 {
1456 if (wp->w_lines[idx].wl_valid
1457 && wp->w_lines[idx].wl_lnum >= to + 1)
1458 {
1459 /* Only update until first row of this line */
1460 mid_end = srow;
1461 break;
1462 }
1463 srow += wp->w_lines[idx].wl_size;
1464 }
1465 }
1466 }
1467
1468 if (VIsual_active && buf == curwin->w_buffer)
1469 {
1470 wp->w_old_visual_mode = VIsual_mode;
1471 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1472 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001473 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 wp->w_old_curswant = curwin->w_curswant;
1475 }
1476 else
1477 {
1478 wp->w_old_visual_mode = 0;
1479 wp->w_old_cursor_lnum = 0;
1480 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001481 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483#endif /* FEAT_VISUAL */
1484
1485#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1486 /* reset got_int, otherwise regexp won't work */
1487 save_got_int = got_int;
1488 got_int = 0;
1489#endif
1490#ifdef FEAT_FOLDING
1491 win_foldinfo.fi_level = 0;
1492#endif
1493
1494 /*
1495 * Update all the window rows.
1496 */
1497 idx = 0; /* first entry in w_lines[].wl_size */
1498 row = 0;
1499 srow = 0;
1500 lnum = wp->w_topline; /* first line shown in window */
1501 for (;;)
1502 {
1503 /* stop updating when reached the end of the window (check for _past_
1504 * the end of the window is at the end of the loop) */
1505 if (row == wp->w_height)
1506 {
1507 didline = TRUE;
1508 break;
1509 }
1510
1511 /* stop updating when hit the end of the file */
1512 if (lnum > buf->b_ml.ml_line_count)
1513 {
1514 eof = TRUE;
1515 break;
1516 }
1517
1518 /* Remember the starting row of the line that is going to be dealt
1519 * with. It is used further down when the line doesn't fit. */
1520 srow = row;
1521
1522 /*
1523 * Update a line when it is in an area that needs updating, when it
1524 * has changes or w_lines[idx] is invalid.
1525 * bot_start may be halfway a wrapped line after using
1526 * win_del_lines(), check if the current line includes it.
1527 * When syntax folding is being used, the saved syntax states will
1528 * already have been updated, we can't see where the syntax state is
1529 * the same again, just update until the end of the window.
1530 */
1531 if (row < top_end
1532 || (row >= mid_start && row < mid_end)
1533#ifdef FEAT_SEARCH_EXTRA
1534 || top_to_mod
1535#endif
1536 || idx >= wp->w_lines_valid
1537 || (row + wp->w_lines[idx].wl_size > bot_start)
1538 || (mod_top != 0
1539 && (lnum == mod_top
1540 || (lnum >= mod_top
1541 && (lnum < mod_bot
1542#ifdef FEAT_SYN_HL
1543 || did_update == DID_FOLD
1544 || (did_update == DID_LINE
1545 && syntax_present(buf)
1546 && (
1547# ifdef FEAT_FOLDING
1548 (foldmethodIsSyntax(wp)
1549 && hasAnyFolding(wp)) ||
1550# endif
1551 syntax_check_changed(lnum)))
1552#endif
1553 )))))
1554 {
1555#ifdef FEAT_SEARCH_EXTRA
1556 if (lnum == mod_top)
1557 top_to_mod = FALSE;
1558#endif
1559
1560 /*
1561 * When at start of changed lines: May scroll following lines
1562 * up or down to minimize redrawing.
1563 * Don't do this when the change continues until the end.
1564 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1565 */
1566 if (lnum == mod_top
1567 && mod_bot != MAXLNUM
1568 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1569 {
1570 int old_rows = 0;
1571 int new_rows = 0;
1572 int xtra_rows;
1573 linenr_T l;
1574
1575 /* Count the old number of window rows, using w_lines[], which
1576 * should still contain the sizes for the lines as they are
1577 * currently displayed. */
1578 for (i = idx; i < wp->w_lines_valid; ++i)
1579 {
1580 /* Only valid lines have a meaningful wl_lnum. Invalid
1581 * lines are part of the changed area. */
1582 if (wp->w_lines[i].wl_valid
1583 && wp->w_lines[i].wl_lnum == mod_bot)
1584 break;
1585 old_rows += wp->w_lines[i].wl_size;
1586#ifdef FEAT_FOLDING
1587 if (wp->w_lines[i].wl_valid
1588 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1589 {
1590 /* Must have found the last valid entry above mod_bot.
1591 * Add following invalid entries. */
1592 ++i;
1593 while (i < wp->w_lines_valid
1594 && !wp->w_lines[i].wl_valid)
1595 old_rows += wp->w_lines[i++].wl_size;
1596 break;
1597 }
1598#endif
1599 }
1600
1601 if (i >= wp->w_lines_valid)
1602 {
1603 /* We can't find a valid line below the changed lines,
1604 * need to redraw until the end of the window.
1605 * Inserting/deleting lines has no use. */
1606 bot_start = 0;
1607 }
1608 else
1609 {
1610 /* Able to count old number of rows: Count new window
1611 * rows, and may insert/delete lines */
1612 j = idx;
1613 for (l = lnum; l < mod_bot; ++l)
1614 {
1615#ifdef FEAT_FOLDING
1616 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1617 ++new_rows;
1618 else
1619#endif
1620#ifdef FEAT_DIFF
1621 if (l == wp->w_topline)
1622 new_rows += plines_win_nofill(wp, l, TRUE)
1623 + wp->w_topfill;
1624 else
1625#endif
1626 new_rows += plines_win(wp, l, TRUE);
1627 ++j;
1628 if (new_rows > wp->w_height - row - 2)
1629 {
1630 /* it's getting too much, must redraw the rest */
1631 new_rows = 9999;
1632 break;
1633 }
1634 }
1635 xtra_rows = new_rows - old_rows;
1636 if (xtra_rows < 0)
1637 {
1638 /* May scroll text up. If there is not enough
1639 * remaining text or scrolling fails, must redraw the
1640 * rest. If scrolling works, must redraw the text
1641 * below the scrolled text. */
1642 if (row - xtra_rows >= wp->w_height - 2)
1643 mod_bot = MAXLNUM;
1644 else
1645 {
1646 check_for_delay(FALSE);
1647 if (win_del_lines(wp, row,
1648 -xtra_rows, FALSE, FALSE) == FAIL)
1649 mod_bot = MAXLNUM;
1650 else
1651 bot_start = wp->w_height + xtra_rows;
1652 }
1653 }
1654 else if (xtra_rows > 0)
1655 {
1656 /* May scroll text down. If there is not enough
1657 * remaining text of scrolling fails, must redraw the
1658 * rest. */
1659 if (row + xtra_rows >= wp->w_height - 2)
1660 mod_bot = MAXLNUM;
1661 else
1662 {
1663 check_for_delay(FALSE);
1664 if (win_ins_lines(wp, row + old_rows,
1665 xtra_rows, FALSE, FALSE) == FAIL)
1666 mod_bot = MAXLNUM;
1667 else if (top_end > row + old_rows)
1668 /* Scrolled the part at the top that requires
1669 * updating down. */
1670 top_end += xtra_rows;
1671 }
1672 }
1673
1674 /* When not updating the rest, may need to move w_lines[]
1675 * entries. */
1676 if (mod_bot != MAXLNUM && i != j)
1677 {
1678 if (j < i)
1679 {
1680 int x = row + new_rows;
1681
1682 /* move entries in w_lines[] upwards */
1683 for (;;)
1684 {
1685 /* stop at last valid entry in w_lines[] */
1686 if (i >= wp->w_lines_valid)
1687 {
1688 wp->w_lines_valid = j;
1689 break;
1690 }
1691 wp->w_lines[j] = wp->w_lines[i];
1692 /* stop at a line that won't fit */
1693 if (x + (int)wp->w_lines[j].wl_size
1694 > wp->w_height)
1695 {
1696 wp->w_lines_valid = j + 1;
1697 break;
1698 }
1699 x += wp->w_lines[j++].wl_size;
1700 ++i;
1701 }
1702 if (bot_start > x)
1703 bot_start = x;
1704 }
1705 else /* j > i */
1706 {
1707 /* move entries in w_lines[] downwards */
1708 j -= i;
1709 wp->w_lines_valid += j;
1710 if (wp->w_lines_valid > wp->w_height)
1711 wp->w_lines_valid = wp->w_height;
1712 for (i = wp->w_lines_valid; i - j >= idx; --i)
1713 wp->w_lines[i] = wp->w_lines[i - j];
1714
1715 /* The w_lines[] entries for inserted lines are
1716 * now invalid, but wl_size may be used above.
1717 * Reset to zero. */
1718 while (i >= idx)
1719 {
1720 wp->w_lines[i].wl_size = 0;
1721 wp->w_lines[i--].wl_valid = FALSE;
1722 }
1723 }
1724 }
1725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
1727
1728#ifdef FEAT_FOLDING
1729 /*
1730 * When lines are folded, display one line for all of them.
1731 * Otherwise, display normally (can be several display lines when
1732 * 'wrap' is on).
1733 */
1734 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1735 if (fold_count != 0)
1736 {
1737 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1738 ++row;
1739 --fold_count;
1740 wp->w_lines[idx].wl_folded = TRUE;
1741 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1742# ifdef FEAT_SYN_HL
1743 did_update = DID_FOLD;
1744# endif
1745 }
1746 else
1747#endif
1748 if (idx < wp->w_lines_valid
1749 && wp->w_lines[idx].wl_valid
1750 && wp->w_lines[idx].wl_lnum == lnum
1751 && lnum > wp->w_topline
1752 && !(dy_flags & DY_LASTLINE)
1753 && srow + wp->w_lines[idx].wl_size > wp->w_height
1754#ifdef FEAT_DIFF
1755 && diff_check_fill(wp, lnum) == 0
1756#endif
1757 )
1758 {
1759 /* This line is not going to fit. Don't draw anything here,
1760 * will draw "@ " lines below. */
1761 row = wp->w_height + 1;
1762 }
1763 else
1764 {
1765#ifdef FEAT_SEARCH_EXTRA
1766 prepare_search_hl(wp, lnum);
1767#endif
1768#ifdef FEAT_SYN_HL
1769 /* Let the syntax stuff know we skipped a few lines. */
1770 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1771 && syntax_present(buf))
1772 syntax_end_parsing(syntax_last_parsed + 1);
1773#endif
1774
1775 /*
1776 * Display one line.
1777 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001778 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780#ifdef FEAT_FOLDING
1781 wp->w_lines[idx].wl_folded = FALSE;
1782 wp->w_lines[idx].wl_lastlnum = lnum;
1783#endif
1784#ifdef FEAT_SYN_HL
1785 did_update = DID_LINE;
1786 syntax_last_parsed = lnum;
1787#endif
1788 }
1789
1790 wp->w_lines[idx].wl_lnum = lnum;
1791 wp->w_lines[idx].wl_valid = TRUE;
1792 if (row > wp->w_height) /* past end of screen */
1793 {
1794 /* we may need the size of that too long line later on */
1795 if (dollar_vcol == 0)
1796 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1797 ++idx;
1798 break;
1799 }
1800 if (dollar_vcol == 0)
1801 wp->w_lines[idx].wl_size = row - srow;
1802 ++idx;
1803#ifdef FEAT_FOLDING
1804 lnum += fold_count + 1;
1805#else
1806 ++lnum;
1807#endif
1808 }
1809 else
1810 {
1811 /* This line does not need updating, advance to the next one */
1812 row += wp->w_lines[idx++].wl_size;
1813 if (row > wp->w_height) /* past end of screen */
1814 break;
1815#ifdef FEAT_FOLDING
1816 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1817#else
1818 ++lnum;
1819#endif
1820#ifdef FEAT_SYN_HL
1821 did_update = DID_NONE;
1822#endif
1823 }
1824
1825 if (lnum > buf->b_ml.ml_line_count)
1826 {
1827 eof = TRUE;
1828 break;
1829 }
1830 }
1831 /*
1832 * End of loop over all window lines.
1833 */
1834
1835
1836 if (idx > wp->w_lines_valid)
1837 wp->w_lines_valid = idx;
1838
1839#ifdef FEAT_SYN_HL
1840 /*
1841 * Let the syntax stuff know we stop parsing here.
1842 */
1843 if (syntax_last_parsed != 0 && syntax_present(buf))
1844 syntax_end_parsing(syntax_last_parsed + 1);
1845#endif
1846
1847 /*
1848 * If we didn't hit the end of the file, and we didn't finish the last
1849 * line we were working on, then the line didn't fit.
1850 */
1851 wp->w_empty_rows = 0;
1852#ifdef FEAT_DIFF
1853 wp->w_filler_rows = 0;
1854#endif
1855 if (!eof && !didline)
1856 {
1857 if (lnum == wp->w_topline)
1858 {
1859 /*
1860 * Single line that does not fit!
1861 * Don't overwrite it, it can be edited.
1862 */
1863 wp->w_botline = lnum + 1;
1864 }
1865#ifdef FEAT_DIFF
1866 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1867 {
1868 /* Window ends in filler lines. */
1869 wp->w_botline = lnum;
1870 wp->w_filler_rows = wp->w_height - srow;
1871 }
1872#endif
1873 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1874 {
1875 /*
1876 * Last line isn't finished: Display "@@@" at the end.
1877 */
1878 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1879 W_WINROW(wp) + wp->w_height,
1880 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1881 '@', '@', hl_attr(HLF_AT));
1882 set_empty_rows(wp, srow);
1883 wp->w_botline = lnum;
1884 }
1885 else
1886 {
1887 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1888 wp->w_botline = lnum;
1889 }
1890 }
1891 else
1892 {
1893#ifdef FEAT_VERTSPLIT
1894 draw_vsep_win(wp, row);
1895#endif
1896 if (eof) /* we hit the end of the file */
1897 {
1898 wp->w_botline = buf->b_ml.ml_line_count + 1;
1899#ifdef FEAT_DIFF
1900 j = diff_check_fill(wp, wp->w_botline);
1901 if (j > 0 && !wp->w_botfill)
1902 {
1903 /*
1904 * Display filler lines at the end of the file
1905 */
1906 if (char2cells(fill_diff) > 1)
1907 i = '-';
1908 else
1909 i = fill_diff;
1910 if (row + j > wp->w_height)
1911 j = wp->w_height - row;
1912 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1913 row += j;
1914 }
1915#endif
1916 }
1917 else if (dollar_vcol == 0)
1918 wp->w_botline = lnum;
1919
1920 /* make sure the rest of the screen is blank */
1921 /* put '~'s on rows that aren't part of the file. */
1922 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1923 }
1924
1925 /* Reset the type of redrawing required, the window has been updated. */
1926 wp->w_redr_type = 0;
1927#ifdef FEAT_DIFF
1928 wp->w_old_topfill = wp->w_topfill;
1929 wp->w_old_botfill = wp->w_botfill;
1930#endif
1931
1932 if (dollar_vcol == 0)
1933 {
1934 /*
1935 * There is a trick with w_botline. If we invalidate it on each
1936 * change that might modify it, this will cause a lot of expensive
1937 * calls to plines() in update_topline() each time. Therefore the
1938 * value of w_botline is often approximated, and this value is used to
1939 * compute the value of w_topline. If the value of w_botline was
1940 * wrong, check that the value of w_topline is correct (cursor is on
1941 * the visible part of the text). If it's not, we need to redraw
1942 * again. Mostly this just means scrolling up a few lines, so it
1943 * doesn't look too bad. Only do this for the current window (where
1944 * changes are relevant).
1945 */
1946 wp->w_valid |= VALID_BOTLINE;
1947 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1948 {
1949 recursive = TRUE;
1950 curwin->w_valid &= ~VALID_TOPLINE;
1951 update_topline(); /* may invalidate w_botline again */
1952 if (must_redraw != 0)
1953 {
1954 /* Don't update for changes in buffer again. */
1955 i = curbuf->b_mod_set;
1956 curbuf->b_mod_set = FALSE;
1957 win_update(curwin);
1958 must_redraw = 0;
1959 curbuf->b_mod_set = i;
1960 }
1961 recursive = FALSE;
1962 }
1963 }
1964
1965#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1966 /* restore got_int, unless CTRL-C was hit while redrawing */
1967 if (!got_int)
1968 got_int = save_got_int;
1969#endif
1970}
1971
1972#ifdef FEAT_SIGNS
1973static int draw_signcolumn __ARGS((win_T *wp));
1974
1975/*
1976 * Return TRUE when window "wp" has a column to draw signs in.
1977 */
1978 static int
1979draw_signcolumn(wp)
1980 win_T *wp;
1981{
1982 return (wp->w_buffer->b_signlist != NULL
1983# ifdef FEAT_NETBEANS_INTG
1984 || usingNetbeans
1985# endif
1986 );
1987}
1988#endif
1989
1990/*
1991 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1992 * as the filler character.
1993 */
1994 static void
1995win_draw_end(wp, c1, c2, row, endrow, hl)
1996 win_T *wp;
1997 int c1;
1998 int c2;
1999 int row;
2000 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002001 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
2003#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2004 int n = 0;
2005# define FDC_OFF n
2006#else
2007# define FDC_OFF 0
2008#endif
2009
2010#ifdef FEAT_RIGHTLEFT
2011 if (wp->w_p_rl)
2012 {
2013 /* No check for cmdline window: should never be right-left. */
2014# ifdef FEAT_FOLDING
2015 n = wp->w_p_fdc;
2016
2017 if (n > 0)
2018 {
2019 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002020 if (n > W_WIDTH(wp))
2021 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2023 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2024 ' ', ' ', hl_attr(HLF_FC));
2025 }
2026# endif
2027# ifdef FEAT_SIGNS
2028 if (draw_signcolumn(wp))
2029 {
2030 int nn = n + 2;
2031
2032 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002033 if (nn > W_WIDTH(wp))
2034 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2036 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2037 ' ', ' ', hl_attr(HLF_SC));
2038 n = nn;
2039 }
2040# endif
2041 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2042 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2043 c2, c2, hl_attr(hl));
2044 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2045 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2046 c1, c2, hl_attr(hl));
2047 }
2048 else
2049#endif
2050 {
2051#ifdef FEAT_CMDWIN
2052 if (cmdwin_type != 0 && wp == curwin)
2053 {
2054 /* draw the cmdline character in the leftmost column */
2055 n = 1;
2056 if (n > wp->w_width)
2057 n = wp->w_width;
2058 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2059 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2060 cmdwin_type, ' ', hl_attr(HLF_AT));
2061 }
2062#endif
2063#ifdef FEAT_FOLDING
2064 if (wp->w_p_fdc > 0)
2065 {
2066 int nn = n + wp->w_p_fdc;
2067
2068 /* draw the fold column at the left */
2069 if (nn > W_WIDTH(wp))
2070 nn = W_WIDTH(wp);
2071 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2072 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2073 ' ', ' ', hl_attr(HLF_FC));
2074 n = nn;
2075 }
2076#endif
2077#ifdef FEAT_SIGNS
2078 if (draw_signcolumn(wp))
2079 {
2080 int nn = n + 2;
2081
2082 /* draw the sign column after the fold column */
2083 if (nn > W_WIDTH(wp))
2084 nn = W_WIDTH(wp);
2085 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2086 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2087 ' ', ' ', hl_attr(HLF_SC));
2088 n = nn;
2089 }
2090#endif
2091 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2092 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2093 c1, c2, hl_attr(hl));
2094 }
2095 set_empty_rows(wp, row);
2096}
2097
2098#ifdef FEAT_FOLDING
2099/*
2100 * Display one folded line.
2101 */
2102 static void
2103fold_line(wp, fold_count, foldinfo, lnum, row)
2104 win_T *wp;
2105 long fold_count;
2106 foldinfo_T *foldinfo;
2107 linenr_T lnum;
2108 int row;
2109{
2110 char_u buf[51];
2111 pos_T *top, *bot;
2112 linenr_T lnume = lnum + fold_count - 1;
2113 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002114 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 int col;
2117 int txtcol;
2118 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002119 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120
2121 /* Build the fold line:
2122 * 1. Add the cmdwin_type for the command-line window
2123 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002124 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 * 4. Compose the text
2126 * 5. Add the text
2127 * 6. set highlighting for the Visual area an other text
2128 */
2129 col = 0;
2130
2131 /*
2132 * 1. Add the cmdwin_type for the command-line window
2133 * Ignores 'rightleft', this window is never right-left.
2134 */
2135#ifdef FEAT_CMDWIN
2136 if (cmdwin_type != 0 && wp == curwin)
2137 {
2138 ScreenLines[off] = cmdwin_type;
2139 ScreenAttrs[off] = hl_attr(HLF_AT);
2140#ifdef FEAT_MBYTE
2141 if (enc_utf8)
2142 ScreenLinesUC[off] = 0;
2143#endif
2144 ++col;
2145 }
2146#endif
2147
2148 /*
2149 * 2. Add the 'foldcolumn'
2150 */
2151 fdc = wp->w_p_fdc;
2152 if (fdc > W_WIDTH(wp) - col)
2153 fdc = W_WIDTH(wp) - col;
2154 if (fdc > 0)
2155 {
2156 fill_foldcolumn(buf, wp, TRUE, lnum);
2157#ifdef FEAT_RIGHTLEFT
2158 if (wp->w_p_rl)
2159 {
2160 int i;
2161
2162 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2163 hl_attr(HLF_FC));
2164 /* reverse the fold column */
2165 for (i = 0; i < fdc; ++i)
2166 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2167 }
2168 else
2169#endif
2170 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2171 col += fdc;
2172 }
2173
2174#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002175# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2176 for (ri = 0; ri < l; ++ri) \
2177 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2178 else \
2179 for (ri = 0; ri < l; ++ri) \
2180 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002182# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2183 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184#endif
2185
Bram Moolenaar64486672010-05-16 15:46:46 +02002186 /* Set all attributes of the 'number' or 'relativenumber' column and the
2187 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002188 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
2190#ifdef FEAT_SIGNS
2191 /* If signs are being displayed, add two spaces. */
2192 if (draw_signcolumn(wp))
2193 {
2194 len = W_WIDTH(wp) - col;
2195 if (len > 0)
2196 {
2197 if (len > 2)
2198 len = 2;
2199# ifdef FEAT_RIGHTLEFT
2200 if (wp->w_p_rl)
2201 /* the line number isn't reversed */
2202 copy_text_attr(off + W_WIDTH(wp) - len - col,
2203 (char_u *)" ", len, hl_attr(HLF_FL));
2204 else
2205# endif
2206 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2207 col += len;
2208 }
2209 }
2210#endif
2211
2212 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002213 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002215 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
2217 len = W_WIDTH(wp) - col;
2218 if (len > 0)
2219 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002220 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002221 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002222
2223 if (len > w + 1)
2224 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002225
2226 if (wp->w_p_nu)
2227 /* 'number' */
2228 num = (long)lnum;
2229 else
2230 /* 'relativenumber', don't use negative numbers */
2231 num = (long)abs((int)get_cursor_rel_lnum(wp, lnum));
2232
2233 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#ifdef FEAT_RIGHTLEFT
2235 if (wp->w_p_rl)
2236 /* the line number isn't reversed */
2237 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2238 hl_attr(HLF_FL));
2239 else
2240#endif
2241 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2242 col += len;
2243 }
2244 }
2245
2246 /*
2247 * 4. Compose the folded-line string with 'foldtext', if set.
2248 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002249 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 txtcol = col; /* remember where text starts */
2252
2253 /*
2254 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2255 * Right-left text is put in columns 0 - number-col, normal text is put
2256 * in columns number-col - window-width.
2257 */
2258#ifdef FEAT_MBYTE
2259 if (has_mbyte)
2260 {
2261 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002262 int u8c, u8cc[MAX_MCO];
2263 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 int idx;
2265 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002266 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267# ifdef FEAT_ARABIC
2268 int prev_c = 0; /* previous Arabic character */
2269 int prev_c1 = 0; /* first composing char for prev_c */
2270# endif
2271
2272# ifdef FEAT_RIGHTLEFT
2273 if (wp->w_p_rl)
2274 idx = off;
2275 else
2276# endif
2277 idx = off + col;
2278
2279 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2280 for (p = text; *p != NUL; )
2281 {
2282 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002283 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (col + cells > W_WIDTH(wp)
2285# ifdef FEAT_RIGHTLEFT
2286 - (wp->w_p_rl ? col : 0)
2287# endif
2288 )
2289 break;
2290 ScreenLines[idx] = *p;
2291 if (enc_utf8)
2292 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002293 u8c = utfc_ptr2char(p, u8cc);
2294 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 ScreenLinesUC[idx] = 0;
2297#ifdef FEAT_ARABIC
2298 prev_c = u8c;
2299#endif
2300 }
2301 else
2302 {
2303#ifdef FEAT_ARABIC
2304 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2305 {
2306 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002307 int pc, pc1, nc;
2308 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 int firstbyte = *p;
2310
2311 /* The idea of what is the previous and next
2312 * character depends on 'rightleft'. */
2313 if (wp->w_p_rl)
2314 {
2315 pc = prev_c;
2316 pc1 = prev_c1;
2317 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002318 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 }
2320 else
2321 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002322 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002324 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326 prev_c = u8c;
2327
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002328 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 pc, pc1, nc);
2330 ScreenLines[idx] = firstbyte;
2331 }
2332 else
2333 prev_c = u8c;
2334#endif
2335 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002336#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if (u8c >= 0x10000)
2338 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2339 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002342 for (i = 0; i < Screen_mco; ++i)
2343 {
2344 ScreenLinesC[i][idx] = u8cc[i];
2345 if (u8cc[i] == 0)
2346 break;
2347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349 if (cells > 1)
2350 ScreenLines[idx + 1] = 0;
2351 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002352 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2353 /* double-byte single width character */
2354 ScreenLines2[idx] = p[1];
2355 else if (cells > 1)
2356 /* double-width character */
2357 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 col += cells;
2359 idx += cells;
2360 p += c_len;
2361 }
2362 }
2363 else
2364#endif
2365 {
2366 len = (int)STRLEN(text);
2367 if (len > W_WIDTH(wp) - col)
2368 len = W_WIDTH(wp) - col;
2369 if (len > 0)
2370 {
2371#ifdef FEAT_RIGHTLEFT
2372 if (wp->w_p_rl)
2373 STRNCPY(current_ScreenLine, text, len);
2374 else
2375#endif
2376 STRNCPY(current_ScreenLine + col, text, len);
2377 col += len;
2378 }
2379 }
2380
2381 /* Fill the rest of the line with the fold filler */
2382#ifdef FEAT_RIGHTLEFT
2383 if (wp->w_p_rl)
2384 col -= txtcol;
2385#endif
2386 while (col < W_WIDTH(wp)
2387#ifdef FEAT_RIGHTLEFT
2388 - (wp->w_p_rl ? txtcol : 0)
2389#endif
2390 )
2391 {
2392#ifdef FEAT_MBYTE
2393 if (enc_utf8)
2394 {
2395 if (fill_fold >= 0x80)
2396 {
2397 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002398 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 else
2401 ScreenLinesUC[off + col] = 0;
2402 }
2403#endif
2404 ScreenLines[off + col++] = fill_fold;
2405 }
2406
2407 if (text != buf)
2408 vim_free(text);
2409
2410 /*
2411 * 6. set highlighting for the Visual area an other text.
2412 * If all folded lines are in the Visual area, highlight the line.
2413 */
2414#ifdef FEAT_VISUAL
2415 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2416 {
2417 if (ltoreq(curwin->w_cursor, VIsual))
2418 {
2419 /* Visual is after curwin->w_cursor */
2420 top = &curwin->w_cursor;
2421 bot = &VIsual;
2422 }
2423 else
2424 {
2425 /* Visual is before curwin->w_cursor */
2426 top = &VIsual;
2427 bot = &curwin->w_cursor;
2428 }
2429 if (lnum >= top->lnum
2430 && lnume <= bot->lnum
2431 && (VIsual_mode != 'v'
2432 || ((lnum > top->lnum
2433 || (lnum == top->lnum
2434 && top->col == 0))
2435 && (lnume < bot->lnum
2436 || (lnume == bot->lnum
2437 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002438 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
2440 if (VIsual_mode == Ctrl_V)
2441 {
2442 /* Visual block mode: highlight the chars part of the block */
2443 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2444 {
2445 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2446 len = wp->w_old_cursor_lcol;
2447 else
2448 len = W_WIDTH(wp) - txtcol;
2449 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002450 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
2452 }
2453 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002456 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459 }
2460#endif
2461
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002462#ifdef FEAT_SYN_HL
2463 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002464 if (wp->w_p_cuc)
2465 {
2466 txtcol += wp->w_virtcol;
2467 if (wp->w_p_wrap)
2468 txtcol -= wp->w_skipcol;
2469 else
2470 txtcol -= wp->w_leftcol;
2471 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2472 ScreenAttrs[off + txtcol] = hl_combine_attr(
2473 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2474 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2478 (int)W_WIDTH(wp), FALSE);
2479
2480 /*
2481 * Update w_cline_height and w_cline_folded if the cursor line was
2482 * updated (saves a call to plines() later).
2483 */
2484 if (wp == curwin
2485 && lnum <= curwin->w_cursor.lnum
2486 && lnume >= curwin->w_cursor.lnum)
2487 {
2488 curwin->w_cline_row = row;
2489 curwin->w_cline_height = 1;
2490 curwin->w_cline_folded = TRUE;
2491 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2492 }
2493}
2494
2495/*
2496 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2497 */
2498 static void
2499copy_text_attr(off, buf, len, attr)
2500 int off;
2501 char_u *buf;
2502 int len;
2503 int attr;
2504{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002505 int i;
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 mch_memmove(ScreenLines + off, buf, (size_t)len);
2508# ifdef FEAT_MBYTE
2509 if (enc_utf8)
2510 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2511# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002512 for (i = 0; i < len; ++i)
2513 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514}
2515
2516/*
2517 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002518 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 */
2520 static void
2521fill_foldcolumn(p, wp, closed, lnum)
2522 char_u *p;
2523 win_T *wp;
2524 int closed; /* TRUE of FALSE */
2525 linenr_T lnum; /* current line number */
2526{
2527 int i = 0;
2528 int level;
2529 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002530 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532 /* Init to all spaces. */
2533 copy_spaces(p, (size_t)wp->w_p_fdc);
2534
2535 level = win_foldinfo.fi_level;
2536 if (level > 0)
2537 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002538 /* If there is only one column put more info in it. */
2539 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 /* If the column is too narrow, we start at the lowest level that
2542 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002543 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (first_level < 1)
2545 first_level = 1;
2546
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002547 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
2549 if (win_foldinfo.fi_lnum == lnum
2550 && first_level + i >= win_foldinfo.fi_low_level)
2551 p[i] = '-';
2552 else if (first_level == 1)
2553 p[i] = '|';
2554 else if (first_level + i <= 9)
2555 p[i] = '0' + first_level + i;
2556 else
2557 p[i] = '>';
2558 if (first_level + i == level)
2559 break;
2560 }
2561 }
2562 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002563 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564}
2565#endif /* FEAT_FOLDING */
2566
2567/*
2568 * Display line "lnum" of window 'wp' on the screen.
2569 * Start at row "startrow", stop when "endrow" is reached.
2570 * wp->w_virtcol needs to be valid.
2571 *
2572 * Return the number of last row the line occupies.
2573 */
2574 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002575win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 win_T *wp;
2577 linenr_T lnum;
2578 int startrow;
2579 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002580 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 int col; /* visual column on screen */
2583 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2584 int c = 0; /* init for GCC */
2585 long vcol = 0; /* virtual column (for tabs) */
2586 long vcol_prev = -1; /* "vcol" of previous character */
2587 char_u *line; /* current line */
2588 char_u *ptr; /* current position in "line" */
2589 int row; /* row in the window, excl w_winrow */
2590 int screen_row; /* row on the screen, incl w_winrow */
2591
2592 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2593 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002594 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 int c_extra = NUL; /* extra chars, all the same */
2596 int extra_attr = 0; /* attributes when n_extra != 0 */
2597 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2598 displaying lcs_eol at end-of-line */
2599 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2600 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2601
2602 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2603 int saved_n_extra = 0;
2604 char_u *saved_p_extra = NULL;
2605 int saved_c_extra = 0;
2606 int saved_char_attr = 0;
2607
2608 int n_attr = 0; /* chars with special attr */
2609 int saved_attr2 = 0; /* char_attr saved for n_attr */
2610 int n_attr3 = 0; /* chars with overruling special attr */
2611 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2612
2613 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2614
2615 int fromcol, tocol; /* start/end of inverting */
2616 int fromcol_prev = -2; /* start of inverting after cursor */
2617 int noinvcur = FALSE; /* don't invert the cursor */
2618#ifdef FEAT_VISUAL
2619 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002620 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621#endif
2622 pos_T pos;
2623 long v;
2624
2625 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002626 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2628 in this line */
2629 int attr = 0; /* attributes for area highlighting */
2630 int area_attr = 0; /* attributes desired by highlighting */
2631 int search_attr = 0; /* attributes desired by 'hlsearch' */
2632#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002633 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 int syntax_attr = 0; /* attributes desired by syntax */
2635 int has_syntax = FALSE; /* this buffer has syntax highl. */
2636 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002637 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002638#endif
2639#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002640 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002641# define SPWORDLEN 150
2642 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002643 int nextlinecol = 0; /* column where nextline[] starts */
2644 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002645 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002646 int spell_attr = 0; /* attributes desired by spelling */
2647 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002648 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2649 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002650 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002651 static int cap_col = -1; /* column to check for Cap word */
2652 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002653 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654#endif
2655 int extra_check; /* has syntax or linebreak */
2656#ifdef FEAT_MBYTE
2657 int multi_attr = 0; /* attributes desired by multibyte */
2658 int mb_l = 1; /* multi-byte byte length */
2659 int mb_c = 0; /* decoded multi-byte character */
2660 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002661 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#endif
2663#ifdef FEAT_DIFF
2664 int filler_lines; /* nr of filler lines to be drawn */
2665 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002666 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 int change_start = MAXCOL; /* first col of changed area */
2668 int change_end = -1; /* last col of changed area */
2669#endif
2670 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2671#ifdef FEAT_LINEBREAK
2672 int need_showbreak = FALSE;
2673#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002674#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2675 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002677 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678#endif
2679#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002680 matchitem_T *cur; /* points to the match list */
2681 match_T *shl; /* points to search_hl or a match */
2682 int shl_flag; /* flag to indicate whether search_hl
2683 has been processed or not */
2684 int prevcol_hl_flag; /* flag to indicate whether prevcol
2685 equals startcol of search_hl or one
2686 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687#endif
2688#ifdef FEAT_ARABIC
2689 int prev_c = 0; /* previous Arabic character */
2690 int prev_c1 = 0; /* first composing char for prev_c */
2691#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002692#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002693 int did_line_attr = 0;
2694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 /* draw_state: items that are drawn in sequence: */
2697#define WL_START 0 /* nothing done yet */
2698#ifdef FEAT_CMDWIN
2699# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2700#else
2701# define WL_CMDLINE WL_START
2702#endif
2703#ifdef FEAT_FOLDING
2704# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2705#else
2706# define WL_FOLD WL_CMDLINE
2707#endif
2708#ifdef FEAT_SIGNS
2709# define WL_SIGN WL_FOLD + 1 /* column for signs */
2710#else
2711# define WL_SIGN WL_FOLD /* column for signs */
2712#endif
2713#define WL_NR WL_SIGN + 1 /* line number */
2714#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2715# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2716#else
2717# define WL_SBR WL_NR
2718#endif
2719#define WL_LINE WL_SBR + 1 /* text in the line */
2720 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002721#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 int feedback_col = 0;
2723 int feedback_old_attr = -1;
2724#endif
2725
2726
2727 if (startrow > endrow) /* past the end already! */
2728 return startrow;
2729
2730 row = startrow;
2731 screen_row = row + W_WINROW(wp);
2732
2733 /*
2734 * To speed up the loop below, set extra_check when there is linebreak,
2735 * trailing white space and/or syntax processing to be done.
2736 */
2737#ifdef FEAT_LINEBREAK
2738 extra_check = wp->w_p_lbr;
2739#else
2740 extra_check = 0;
2741#endif
2742#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002743 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
2745 /* Prepare for syntax highlighting in this line. When there is an
2746 * error, stop syntax highlighting. */
2747 save_did_emsg = did_emsg;
2748 did_emsg = FALSE;
2749 syntax_start(wp, lnum);
2750 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002751 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else
2753 {
2754 did_emsg = save_did_emsg;
2755 has_syntax = TRUE;
2756 extra_check = TRUE;
2757 }
2758 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002759#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002760
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002761#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002762 if (wp->w_p_spell
2763 && *wp->w_buffer->b_p_spl != NUL
2764 && wp->w_buffer->b_langp.ga_len > 0
2765 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002766 {
2767 /* Prepare for spell checking. */
2768 has_spell = TRUE;
2769 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002770
2771 /* Get the start of the next line, so that words that wrap to the next
2772 * line are found too: "et<line-break>al.".
2773 * Trick: skip a few chars for C/shell/Vim comments */
2774 nextline[SPWORDLEN] = NUL;
2775 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2776 {
2777 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2778 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2779 }
2780
2781 /* When a word wrapped from the previous line the start of the current
2782 * line is valid. */
2783 if (lnum == checked_lnum)
2784 cur_checked_col = checked_col;
2785 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002786
2787 /* When there was a sentence end in the previous line may require a
2788 * word starting with capital in this line. In line 1 always check
2789 * the first word. */
2790 if (lnum != capcol_lnum)
2791 cap_col = -1;
2792 if (lnum == 1)
2793 cap_col = 0;
2794 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796#endif
2797
2798 /*
2799 * handle visual active in this window
2800 */
2801 fromcol = -10;
2802 tocol = MAXCOL;
2803#ifdef FEAT_VISUAL
2804 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2805 {
2806 /* Visual is after curwin->w_cursor */
2807 if (ltoreq(curwin->w_cursor, VIsual))
2808 {
2809 top = &curwin->w_cursor;
2810 bot = &VIsual;
2811 }
2812 else /* Visual is before curwin->w_cursor */
2813 {
2814 top = &VIsual;
2815 bot = &curwin->w_cursor;
2816 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002817 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (VIsual_mode == Ctrl_V) /* block mode */
2819 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002820 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
2822 fromcol = wp->w_old_cursor_fcol;
2823 tocol = wp->w_old_cursor_lcol;
2824 }
2825 }
2826 else /* non-block mode */
2827 {
2828 if (lnum > top->lnum && lnum <= bot->lnum)
2829 fromcol = 0;
2830 else if (lnum == top->lnum)
2831 {
2832 if (VIsual_mode == 'V') /* linewise */
2833 fromcol = 0;
2834 else
2835 {
2836 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2837 if (gchar_pos(top) == NUL)
2838 tocol = fromcol + 1;
2839 }
2840 }
2841 if (VIsual_mode != 'V' && lnum == bot->lnum)
2842 {
2843 if (*p_sel == 'e' && bot->col == 0
2844#ifdef FEAT_VIRTUALEDIT
2845 && bot->coladd == 0
2846#endif
2847 )
2848 {
2849 fromcol = -10;
2850 tocol = MAXCOL;
2851 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002852 else if (bot->col == MAXCOL)
2853 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 else
2855 {
2856 pos = *bot;
2857 if (*p_sel == 'e')
2858 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2859 else
2860 {
2861 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2862 ++tocol;
2863 }
2864 }
2865 }
2866 }
2867
2868#ifndef MSDOS
2869 /* Check if the character under the cursor should not be inverted */
2870 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2871# ifdef FEAT_GUI
2872 && !gui.in_use
2873# endif
2874 )
2875 noinvcur = TRUE;
2876#endif
2877
2878 /* if inverting in this line set area_highlighting */
2879 if (fromcol >= 0)
2880 {
2881 area_highlighting = TRUE;
2882 attr = hl_attr(HLF_V);
2883#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2884 if (clip_star.available && !clip_star.owned && clip_isautosel())
2885 attr = hl_attr(HLF_VNC);
2886#endif
2887 }
2888 }
2889
2890 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002891 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 */
2893 else
2894#endif /* FEAT_VISUAL */
2895 if (highlight_match
2896 && wp == curwin
2897 && lnum >= curwin->w_cursor.lnum
2898 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2899 {
2900 if (lnum == curwin->w_cursor.lnum)
2901 getvcol(curwin, &(curwin->w_cursor),
2902 (colnr_T *)&fromcol, NULL, NULL);
2903 else
2904 fromcol = 0;
2905 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2906 {
2907 pos.lnum = lnum;
2908 pos.col = search_match_endcol;
2909 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2910 }
2911 else
2912 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00002913 /* do at least one character; happens when past end of line */
2914 if (fromcol == tocol)
2915 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 area_highlighting = TRUE;
2917 attr = hl_attr(HLF_I);
2918 }
2919
2920#ifdef FEAT_DIFF
2921 filler_lines = diff_check(wp, lnum);
2922 if (filler_lines < 0)
2923 {
2924 if (filler_lines == -1)
2925 {
2926 if (diff_find_change(wp, lnum, &change_start, &change_end))
2927 diff_hlf = HLF_ADD; /* added line */
2928 else if (change_start == 0)
2929 diff_hlf = HLF_TXD; /* changed text */
2930 else
2931 diff_hlf = HLF_CHD; /* changed line */
2932 }
2933 else
2934 diff_hlf = HLF_ADD; /* added line */
2935 filler_lines = 0;
2936 area_highlighting = TRUE;
2937 }
2938 if (lnum == wp->w_topline)
2939 filler_lines = wp->w_topfill;
2940 filler_todo = filler_lines;
2941#endif
2942
2943#ifdef LINE_ATTR
2944# ifdef FEAT_SIGNS
2945 /* If this line has a sign with line highlighting set line_attr. */
2946 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2947 if (v != 0)
2948 line_attr = sign_get_attr((int)v, TRUE);
2949# endif
2950# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2951 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002952 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 line_attr = hl_attr(HLF_L);
2954# endif
2955 if (line_attr != 0)
2956 area_highlighting = TRUE;
2957#endif
2958
2959 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2960 ptr = line;
2961
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002962#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002963 if (has_spell)
2964 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002965 /* For checking first word with a capital skip white space. */
2966 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002967 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002968
Bram Moolenaar30abd282005-06-22 22:35:10 +00002969 /* To be able to spell-check over line boundaries copy the end of the
2970 * current line into nextline[]. Above the start of the next line was
2971 * copied to nextline[SPWORDLEN]. */
2972 if (nextline[SPWORDLEN] == NUL)
2973 {
2974 /* No next line or it is empty. */
2975 nextlinecol = MAXCOL;
2976 nextline_idx = 0;
2977 }
2978 else
2979 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002980 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002981 if (v < SPWORDLEN)
2982 {
2983 /* Short line, use it completely and append the start of the
2984 * next line. */
2985 nextlinecol = 0;
2986 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00002987 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002988 nextline_idx = v + 1;
2989 }
2990 else
2991 {
2992 /* Long line, use only the last SPWORDLEN bytes. */
2993 nextlinecol = v - SPWORDLEN;
2994 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2995 nextline_idx = SPWORDLEN + 1;
2996 }
2997 }
2998 }
2999#endif
3000
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 /* find start of trailing whitespace */
3002 if (wp->w_p_list && lcs_trail)
3003 {
3004 trailcol = (colnr_T)STRLEN(ptr);
3005 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3006 --trailcol;
3007 trailcol += (colnr_T) (ptr - line);
3008 extra_check = TRUE;
3009 }
3010
3011 /*
3012 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3013 * first character to be displayed.
3014 */
3015 if (wp->w_p_wrap)
3016 v = wp->w_skipcol;
3017 else
3018 v = wp->w_leftcol;
3019 if (v > 0)
3020 {
3021#ifdef FEAT_MBYTE
3022 char_u *prev_ptr = ptr;
3023#endif
3024 while (vcol < v && *ptr != NUL)
3025 {
3026 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3027 vcol += c;
3028#ifdef FEAT_MBYTE
3029 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003031 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003034#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3035 /* When:
3036 * - 'cuc' is set, or
3037 * - 'virtualedit' is set, or
3038 * - the visual mode is active,
3039 * the end of the line may be before the start of the displayed part.
3040 */
3041 if (vcol < v && (
3042# ifdef FEAT_SYN_HL
3043 wp->w_p_cuc
3044# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3045 ||
3046# endif
3047# endif
3048# ifdef FEAT_VIRTUALEDIT
3049 virtual_active()
3050# ifdef FEAT_VISUAL
3051 ||
3052# endif
3053# endif
3054# ifdef FEAT_VISUAL
3055 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3056# endif
3057 ))
3058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061#endif
3062
3063 /* Handle a character that's not completely on the screen: Put ptr at
3064 * that character but skip the first few screen characters. */
3065 if (vcol > v)
3066 {
3067 vcol -= c;
3068#ifdef FEAT_MBYTE
3069 ptr = prev_ptr;
3070#else
3071 --ptr;
3072#endif
3073 n_skip = v - vcol;
3074 }
3075
3076 /*
3077 * Adjust for when the inverted text is before the screen,
3078 * and when the start of the inverted text is before the screen.
3079 */
3080 if (tocol <= vcol)
3081 fromcol = 0;
3082 else if (fromcol >= 0 && fromcol < vcol)
3083 fromcol = vcol;
3084
3085#ifdef FEAT_LINEBREAK
3086 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3087 if (wp->w_p_wrap)
3088 need_showbreak = TRUE;
3089#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003090#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003091 /* When spell checking a word we need to figure out the start of the
3092 * word and if it's badly spelled or not. */
3093 if (has_spell)
3094 {
3095 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003096 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003097 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003098
3099 pos = wp->w_cursor;
3100 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003101 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003102 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003103
3104 /* spell_move_to() may call ml_get() and make "line" invalid */
3105 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3106 ptr = line + linecol;
3107
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003108 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003109 {
3110 /* no bad word found at line start, don't check until end of a
3111 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003112 spell_hlf = HLF_COUNT;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003113 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3114 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003115 }
3116 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003117 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003118 /* bad word found, use attributes until end of word */
3119 word_end = wp->w_cursor.col + len + 1;
3120
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003121 /* Turn index into actual attributes. */
3122 if (spell_hlf != HLF_COUNT)
3123 spell_attr = highlight_attr[spell_hlf];
3124 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003125 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003126
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003127# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003128 /* Need to restart syntax highlighting for this line. */
3129 if (has_syntax)
3130 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003131# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003132 }
3133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 }
3135
3136 /*
3137 * Correct highlighting for cursor that can't be disabled.
3138 * Avoids having to check this for each character.
3139 */
3140 if (fromcol >= 0)
3141 {
3142 if (noinvcur)
3143 {
3144 if ((colnr_T)fromcol == wp->w_virtcol)
3145 {
3146 /* highlighting starts at cursor, let it start just after the
3147 * cursor */
3148 fromcol_prev = fromcol;
3149 fromcol = -1;
3150 }
3151 else if ((colnr_T)fromcol < wp->w_virtcol)
3152 /* restart highlighting after the cursor */
3153 fromcol_prev = wp->w_virtcol;
3154 }
3155 if (fromcol >= tocol)
3156 fromcol = -1;
3157 }
3158
3159#ifdef FEAT_SEARCH_EXTRA
3160 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003161 * Handle highlighting the last used search pattern and matches.
3162 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003164 cur = wp->w_match_head;
3165 shl_flag = FALSE;
3166 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003168 if (shl_flag == FALSE)
3169 {
3170 shl = &search_hl;
3171 shl_flag = TRUE;
3172 }
3173 else
3174 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003175 shl->startcol = MAXCOL;
3176 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 shl->attr_cur = 0;
3178 if (shl->rm.regprog != NULL)
3179 {
3180 v = (long)(ptr - line);
3181 next_search_hl(wp, shl, lnum, (colnr_T)v);
3182
3183 /* Need to get the line again, a multi-line regexp may have made it
3184 * invalid. */
3185 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3186 ptr = line + v;
3187
3188 if (shl->lnum != 0 && shl->lnum <= lnum)
3189 {
3190 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003191 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003193 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3195 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003196 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003198 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003200 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
3202#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003203 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003204 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 else
3206#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003207 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003209 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 {
3211 shl->attr_cur = shl->attr;
3212 search_attr = shl->attr;
3213 }
3214 area_highlighting = TRUE;
3215 }
3216 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003217 if (shl != &search_hl && cur != NULL)
3218 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
3220#endif
3221
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003222#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003223 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3224 * active, because it's not clear what is selected then. */
3225 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003226 {
3227 line_attr = hl_attr(HLF_CUL);
3228 area_highlighting = TRUE;
3229 }
3230#endif
3231
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003232 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 col = 0;
3234#ifdef FEAT_RIGHTLEFT
3235 if (wp->w_p_rl)
3236 {
3237 /* Rightleft window: process the text in the normal direction, but put
3238 * it in current_ScreenLine[] from right to left. Start at the
3239 * rightmost column of the window. */
3240 col = W_WIDTH(wp) - 1;
3241 off += col;
3242 }
3243#endif
3244
3245 /*
3246 * Repeat for the whole displayed line.
3247 */
3248 for (;;)
3249 {
3250 /* Skip this quickly when working on the text. */
3251 if (draw_state != WL_LINE)
3252 {
3253#ifdef FEAT_CMDWIN
3254 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3255 {
3256 draw_state = WL_CMDLINE;
3257 if (cmdwin_type != 0 && wp == curwin)
3258 {
3259 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003261 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 char_attr = hl_attr(HLF_AT);
3263 }
3264 }
3265#endif
3266
3267#ifdef FEAT_FOLDING
3268 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3269 {
3270 draw_state = WL_FOLD;
3271 if (wp->w_p_fdc > 0)
3272 {
3273 /* Draw the 'foldcolumn'. */
3274 fill_foldcolumn(extra, wp, FALSE, lnum);
3275 n_extra = wp->w_p_fdc;
3276 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003277 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 c_extra = NUL;
3279 char_attr = hl_attr(HLF_FC);
3280 }
3281 }
3282#endif
3283
3284#ifdef FEAT_SIGNS
3285 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3286 {
3287 draw_state = WL_SIGN;
3288 /* Show the sign column when there are any signs in this
3289 * buffer or when using Netbeans. */
3290 if (draw_signcolumn(wp)
3291# ifdef FEAT_DIFF
3292 && filler_todo <= 0
3293# endif
3294 )
3295 {
3296 int_u text_sign;
3297# ifdef FEAT_SIGN_ICONS
3298 int_u icon_sign;
3299# endif
3300
3301 /* Draw two cells with the sign value or blank. */
3302 c_extra = ' ';
3303 char_attr = hl_attr(HLF_SC);
3304 n_extra = 2;
3305
3306 if (row == startrow)
3307 {
3308 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3309 SIGN_TEXT);
3310# ifdef FEAT_SIGN_ICONS
3311 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3312 SIGN_ICON);
3313 if (gui.in_use && icon_sign != 0)
3314 {
3315 /* Use the image in this position. */
3316 c_extra = SIGN_BYTE;
3317# ifdef FEAT_NETBEANS_INTG
3318 if (buf_signcount(wp->w_buffer, lnum) > 1)
3319 c_extra = MULTISIGN_BYTE;
3320# endif
3321 char_attr = icon_sign;
3322 }
3323 else
3324# endif
3325 if (text_sign != 0)
3326 {
3327 p_extra = sign_get_text(text_sign);
3328 if (p_extra != NULL)
3329 {
3330 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003331 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 }
3333 char_attr = sign_get_attr(text_sign, FALSE);
3334 }
3335 }
3336 }
3337 }
3338#endif
3339
3340 if (draw_state == WL_NR - 1 && n_extra == 0)
3341 {
3342 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003343 /* Display the absolute or relative line number. After the
3344 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3345 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 && (row == startrow
3347#ifdef FEAT_DIFF
3348 + filler_lines
3349#endif
3350 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3351 {
3352 /* Draw the line number (empty space after wrapping). */
3353 if (row == startrow
3354#ifdef FEAT_DIFF
3355 + filler_lines
3356#endif
3357 )
3358 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003359 long num;
3360
3361 if (wp->w_p_nu)
3362 /* 'number' */
3363 num = (long)lnum;
3364 else
3365 /* 'relativenumber', don't use negative numbers */
3366 num = (long)abs((int)get_cursor_rel_lnum(wp,
3367 lnum));
3368
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003369 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003370 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (wp->w_skipcol > 0)
3372 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3373 *p_extra = '-';
3374#ifdef FEAT_RIGHTLEFT
3375 if (wp->w_p_rl) /* reverse line numbers */
3376 rl_mirror(extra);
3377#endif
3378 p_extra = extra;
3379 c_extra = NUL;
3380 }
3381 else
3382 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003383 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003385#ifdef FEAT_SYN_HL
3386 /* When 'cursorline' is set highlight the line number of
3387 * the current line differently. */
3388 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3389 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 }
3393
3394#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3395 if (draw_state == WL_SBR - 1 && n_extra == 0)
3396 {
3397 draw_state = WL_SBR;
3398# ifdef FEAT_DIFF
3399 if (filler_todo > 0)
3400 {
3401 /* Draw "deleted" diff line(s). */
3402 if (char2cells(fill_diff) > 1)
3403 c_extra = '-';
3404 else
3405 c_extra = fill_diff;
3406# ifdef FEAT_RIGHTLEFT
3407 if (wp->w_p_rl)
3408 n_extra = col + 1;
3409 else
3410# endif
3411 n_extra = W_WIDTH(wp) - col;
3412 char_attr = hl_attr(HLF_DED);
3413 }
3414# endif
3415# ifdef FEAT_LINEBREAK
3416 if (*p_sbr != NUL && need_showbreak)
3417 {
3418 /* Draw 'showbreak' at the start of each broken line. */
3419 p_extra = p_sbr;
3420 c_extra = NUL;
3421 n_extra = (int)STRLEN(p_sbr);
3422 char_attr = hl_attr(HLF_AT);
3423 need_showbreak = FALSE;
3424 /* Correct end of highlighted area for 'showbreak',
3425 * required when 'linebreak' is also set. */
3426 if (tocol == vcol)
3427 tocol += n_extra;
3428 }
3429# endif
3430 }
3431#endif
3432
3433 if (draw_state == WL_LINE - 1 && n_extra == 0)
3434 {
3435 draw_state = WL_LINE;
3436 if (saved_n_extra)
3437 {
3438 /* Continue item from end of wrapped line. */
3439 n_extra = saved_n_extra;
3440 c_extra = saved_c_extra;
3441 p_extra = saved_p_extra;
3442 char_attr = saved_char_attr;
3443 }
3444 else
3445 char_attr = 0;
3446 }
3447 }
3448
3449 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003450 if (dollar_vcol != 0 && wp == curwin
3451 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452#ifdef FEAT_DIFF
3453 && filler_todo <= 0
3454#endif
3455 )
3456 {
3457 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3458 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003459 /* Pretend we have finished updating the window. Except when
3460 * 'cursorcolumn' is set. */
3461#ifdef FEAT_SYN_HL
3462 if (wp->w_p_cuc)
3463 row = wp->w_cline_row + wp->w_cline_height;
3464 else
3465#endif
3466 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 break;
3468 }
3469
3470 if (draw_state == WL_LINE && area_highlighting)
3471 {
3472 /* handle Visual or match highlighting in this line */
3473 if (vcol == fromcol
3474#ifdef FEAT_MBYTE
3475 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3476 && (*mb_ptr2cells)(ptr) > 1)
3477#endif
3478 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003479 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 && vcol < tocol))
3481 area_attr = attr; /* start highlighting */
3482 else if (area_attr != 0
3483 && (vcol == tocol
3484 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
3487#ifdef FEAT_SEARCH_EXTRA
3488 if (!n_extra)
3489 {
3490 /*
3491 * Check for start/end of search pattern match.
3492 * After end, check for start/end of next match.
3493 * When another match, have to check for start again.
3494 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003495 * Do this for 'search_hl' and the match list (ordered by
3496 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003498 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003499 cur = wp->w_match_head;
3500 shl_flag = FALSE;
3501 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003503 if (shl_flag == FALSE
3504 && ((cur != NULL
3505 && cur->priority > SEARCH_HL_PRIORITY)
3506 || cur == NULL))
3507 {
3508 shl = &search_hl;
3509 shl_flag = TRUE;
3510 }
3511 else
3512 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 while (shl->rm.regprog != NULL)
3514 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003515 if (shl->startcol != MAXCOL
3516 && v >= (long)shl->startcol
3517 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
3519 shl->attr_cur = shl->attr;
3520 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003521 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
3523 shl->attr_cur = 0;
3524
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 next_search_hl(wp, shl, lnum, (colnr_T)v);
3526
3527 /* Need to get the line again, a multi-line regexp
3528 * may have made it invalid. */
3529 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3530 ptr = line + v;
3531
3532 if (shl->lnum == lnum)
3533 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003534 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003536 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003538 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003540 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 /* highlight empty match, try again after
3543 * it */
3544#ifdef FEAT_MBYTE
3545 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003546 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003547 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 else
3549#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003550 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552
3553 /* Loop to check if the match starts at the
3554 * current position */
3555 continue;
3556 }
3557 }
3558 break;
3559 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003560 if (shl != &search_hl && cur != NULL)
3561 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003563
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003564 /* Use attributes from match with highest priority among
3565 * 'search_hl' and the match list. */
3566 search_attr = search_hl.attr_cur;
3567 cur = wp->w_match_head;
3568 shl_flag = FALSE;
3569 while (cur != NULL || shl_flag == FALSE)
3570 {
3571 if (shl_flag == FALSE
3572 && ((cur != NULL
3573 && cur->priority > SEARCH_HL_PRIORITY)
3574 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003575 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003576 shl = &search_hl;
3577 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003578 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003579 else
3580 shl = &cur->hl;
3581 if (shl->attr_cur != 0)
3582 search_attr = shl->attr_cur;
3583 if (shl != &search_hl && cur != NULL)
3584 cur = cur->next;
3585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587#endif
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003590 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003592 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3593 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003595 if (diff_hlf == HLF_TXD && ptr - line > change_end
3596 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003598 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
3600#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003601
3602 /* Decide which of the highlight attributes to use. */
3603 attr_pri = TRUE;
3604 if (area_attr != 0)
3605 char_attr = area_attr;
3606 else if (search_attr != 0)
3607 char_attr = search_attr;
3608#ifdef LINE_ATTR
3609 /* Use line_attr when not in the Visual or 'incsearch' area
3610 * (area_attr may be 0 when "noinvcur" is set). */
3611 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003612 || vcol < fromcol || vcol_prev < fromcol_prev
3613 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003614 char_attr = line_attr;
3615#endif
3616 else
3617 {
3618 attr_pri = FALSE;
3619#ifdef FEAT_SYN_HL
3620 if (has_syntax)
3621 char_attr = syntax_attr;
3622 else
3623#endif
3624 char_attr = 0;
3625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627
3628 /*
3629 * Get the next character to put on the screen.
3630 */
3631 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003632 * The "p_extra" points to the extra stuff that is inserted to
3633 * represent special characters (non-printable stuff) and other
3634 * things. When all characters are the same, c_extra is used.
3635 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3636 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3638 */
3639 if (n_extra > 0)
3640 {
3641 if (c_extra != NUL)
3642 {
3643 c = c_extra;
3644#ifdef FEAT_MBYTE
3645 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3646 if (enc_utf8 && (*mb_char2len)(c) > 1)
3647 {
3648 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003649 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003650 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652 else
3653 mb_utf8 = FALSE;
3654#endif
3655 }
3656 else
3657 {
3658 c = *p_extra;
3659#ifdef FEAT_MBYTE
3660 if (has_mbyte)
3661 {
3662 mb_c = c;
3663 if (enc_utf8)
3664 {
3665 /* If the UTF-8 character is more than one byte:
3666 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003667 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 mb_utf8 = FALSE;
3669 if (mb_l > n_extra)
3670 mb_l = 1;
3671 else if (mb_l > 1)
3672 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003673 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003675 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677 }
3678 else
3679 {
3680 /* if this is a DBCS character, put it in "mb_c" */
3681 mb_l = MB_BYTE2LEN(c);
3682 if (mb_l >= n_extra)
3683 mb_l = 1;
3684 else if (mb_l > 1)
3685 mb_c = (c << 8) + p_extra[1];
3686 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003687 if (mb_l == 0) /* at the NUL at end-of-line */
3688 mb_l = 1;
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 /* If a double-width char doesn't fit display a '>' in the
3691 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003692 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693# ifdef FEAT_RIGHTLEFT
3694 wp->w_p_rl ? (col <= 0) :
3695# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003696 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 && (*mb_char2cells)(mb_c) == 2)
3698 {
3699 c = '>';
3700 mb_c = c;
3701 mb_l = 1;
3702 mb_utf8 = FALSE;
3703 multi_attr = hl_attr(HLF_AT);
3704 /* put the pointer back to output the double-width
3705 * character at the start of the next line. */
3706 ++n_extra;
3707 --p_extra;
3708 }
3709 else
3710 {
3711 n_extra -= mb_l - 1;
3712 p_extra += mb_l - 1;
3713 }
3714 }
3715#endif
3716 ++p_extra;
3717 }
3718 --n_extra;
3719 }
3720 else
3721 {
3722 /*
3723 * Get a character from the line itself.
3724 */
3725 c = *ptr;
3726#ifdef FEAT_MBYTE
3727 if (has_mbyte)
3728 {
3729 mb_c = c;
3730 if (enc_utf8)
3731 {
3732 /* If the UTF-8 character is more than one byte: Decode it
3733 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003734 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 mb_utf8 = FALSE;
3736 if (mb_l > 1)
3737 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003738 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 /* Overlong encoded ASCII or ASCII with composing char
3740 * is displayed normally, except a NUL. */
3741 if (mb_c < 0x80)
3742 c = mb_c;
3743 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003744
3745 /* At start of the line we can have a composing char.
3746 * Draw it as a space with a composing char. */
3747 if (utf_iscomposing(mb_c))
3748 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003749 int i;
3750
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003751 for (i = Screen_mco - 1; i > 0; --i)
3752 u8cc[i] = u8cc[i - 1];
3753 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003754 mb_c = ' ';
3755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757
3758 if ((mb_l == 1 && c >= 0x80)
3759 || (mb_l >= 1 && mb_c == 0)
3760 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003761# ifdef UNICODE16
3762 || mb_c >= 0x10000
3763# endif
3764 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
3766 /*
3767 * Illegal UTF-8 byte: display as <xx>.
3768 * Non-BMP character : display as ? or fullwidth ?.
3769 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003770# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
3774 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003775# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (wp->w_p_rl) /* reverse */
3777 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003780# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 else if (utf_char2cells(mb_c) != 2)
3782 STRCPY(extra, "?");
3783 else
3784 /* 0xff1f in UTF-8: full-width '?' */
3785 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
3788 p_extra = extra;
3789 c = *p_extra;
3790 mb_c = mb_ptr2char_adv(&p_extra);
3791 mb_utf8 = (c >= 0x80);
3792 n_extra = (int)STRLEN(p_extra);
3793 c_extra = NUL;
3794 if (area_attr == 0 && search_attr == 0)
3795 {
3796 n_attr = n_extra + 1;
3797 extra_attr = hl_attr(HLF_8);
3798 saved_attr2 = char_attr; /* save current attr */
3799 }
3800 }
3801 else if (mb_l == 0) /* at the NUL at end-of-line */
3802 mb_l = 1;
3803#ifdef FEAT_ARABIC
3804 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3805 {
3806 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003807 int pc, pc1, nc;
3808 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 /* The idea of what is the previous and next
3811 * character depends on 'rightleft'. */
3812 if (wp->w_p_rl)
3813 {
3814 pc = prev_c;
3815 pc1 = prev_c1;
3816 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003817 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 else
3820 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003821 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003823 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825 prev_c = mb_c;
3826
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003827 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829 else
3830 prev_c = mb_c;
3831#endif
3832 }
3833 else /* enc_dbcs */
3834 {
3835 mb_l = MB_BYTE2LEN(c);
3836 if (mb_l == 0) /* at the NUL at end-of-line */
3837 mb_l = 1;
3838 else if (mb_l > 1)
3839 {
3840 /* We assume a second byte below 32 is illegal.
3841 * Hopefully this is OK for all double-byte encodings!
3842 */
3843 if (ptr[1] >= 32)
3844 mb_c = (c << 8) + ptr[1];
3845 else
3846 {
3847 if (ptr[1] == NUL)
3848 {
3849 /* head byte at end of line */
3850 mb_l = 1;
3851 transchar_nonprint(extra, c);
3852 }
3853 else
3854 {
3855 /* illegal tail byte */
3856 mb_l = 2;
3857 STRCPY(extra, "XX");
3858 }
3859 p_extra = extra;
3860 n_extra = (int)STRLEN(extra) - 1;
3861 c_extra = NUL;
3862 c = *p_extra++;
3863 if (area_attr == 0 && search_attr == 0)
3864 {
3865 n_attr = n_extra + 1;
3866 extra_attr = hl_attr(HLF_8);
3867 saved_attr2 = char_attr; /* save current attr */
3868 }
3869 mb_c = c;
3870 }
3871 }
3872 }
3873 /* If a double-width char doesn't fit display a '>' in the
3874 * last column; the character is displayed at the start of the
3875 * next line. */
3876 if ((
3877# ifdef FEAT_RIGHTLEFT
3878 wp->w_p_rl ? (col <= 0) :
3879# endif
3880 (col >= W_WIDTH(wp) - 1))
3881 && (*mb_char2cells)(mb_c) == 2)
3882 {
3883 c = '>';
3884 mb_c = c;
3885 mb_utf8 = FALSE;
3886 mb_l = 1;
3887 multi_attr = hl_attr(HLF_AT);
3888 /* Put pointer back so that the character will be
3889 * displayed at the start of the next line. */
3890 --ptr;
3891 }
3892 else if (*ptr != NUL)
3893 ptr += mb_l - 1;
3894
3895 /* If a double-width char doesn't fit at the left side display
3896 * a '<' in the first column. */
3897 if (n_skip > 0 && mb_l > 1)
3898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003900 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 c = ' ';
3902 if (area_attr == 0 && search_attr == 0)
3903 {
3904 n_attr = n_extra + 1;
3905 extra_attr = hl_attr(HLF_AT);
3906 saved_attr2 = char_attr; /* save current attr */
3907 }
3908 mb_c = c;
3909 mb_utf8 = FALSE;
3910 mb_l = 1;
3911 }
3912
3913 }
3914#endif
3915 ++ptr;
3916
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003917 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003918 if (wp->w_p_list && (c == 160
3919#ifdef FEAT_MBYTE
3920 || (mb_utf8 && mb_c == 160)
3921#endif
3922 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003923 {
3924 c = lcs_nbsp;
3925 if (area_attr == 0 && search_attr == 0)
3926 {
3927 n_attr = 1;
3928 extra_attr = hl_attr(HLF_8);
3929 saved_attr2 = char_attr; /* save current attr */
3930 }
3931#ifdef FEAT_MBYTE
3932 mb_c = c;
3933 if (enc_utf8 && (*mb_char2len)(c) > 1)
3934 {
3935 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003936 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003937 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003938 }
3939 else
3940 mb_utf8 = FALSE;
3941#endif
3942 }
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (extra_check)
3945 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003946#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003947 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003948#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003949
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003950#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 /* Get syntax attribute, unless still at the start of the line
3952 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003953 v = (long)(ptr - line);
3954 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
3956 /* Get the syntax attribute for the character. If there
3957 * is an error, disable syntax highlighting. */
3958 save_did_emsg = did_emsg;
3959 did_emsg = FALSE;
3960
Bram Moolenaar217ad922005-03-20 22:37:15 +00003961 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003962# ifdef FEAT_SPELL
3963 has_spell ? &can_spell :
3964# endif
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00003965 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
3967 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003968 {
3969 wp->w_buffer->b_syn_error = TRUE;
3970 has_syntax = FALSE;
3971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 else
3973 did_emsg = save_did_emsg;
3974
3975 /* Need to get the line again, a multi-line regexp may
3976 * have made it invalid. */
3977 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3978 ptr = line + v;
3979
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003980 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003982 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003983 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003985#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003986
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003987#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003988 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003989 * Only do this when there is no syntax highlighting, the
3990 * @Spell cluster is not used or the current syntax item
3991 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003992 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003993 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003994 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003995# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003996 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003997 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003998# endif
3999 if (c != 0 && (
4000# ifdef FEAT_SYN_HL
4001 !has_syntax ||
4002# endif
4003 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004004 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004005 char_u *prev_ptr, *p;
4006 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004007 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004008# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004009 if (has_mbyte)
4010 {
4011 prev_ptr = ptr - mb_l;
4012 v -= mb_l - 1;
4013 }
4014 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004015# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004016 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004017
4018 /* Use nextline[] if possible, it has the start of the
4019 * next line concatenated. */
4020 if ((prev_ptr - line) - nextlinecol >= 0)
4021 p = nextline + (prev_ptr - line) - nextlinecol;
4022 else
4023 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004024 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004025 len = spell_check(wp, p, &spell_hlf, &cap_col,
4026 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004027 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004028
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004029 /* In Insert mode only highlight a word that
4030 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004031 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004032 && (State & INSERT) != 0
4033 && wp->w_cursor.lnum == lnum
4034 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004035 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004036 && wp->w_cursor.col < (colnr_T)word_end)
4037 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004038 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004039 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004040 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004041
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004042 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004043 && (p - nextline) + len > nextline_idx)
4044 {
4045 /* Remember that the good word continues at the
4046 * start of the next line. */
4047 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004048 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004049 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004050
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004051 /* Turn index into actual attributes. */
4052 if (spell_hlf != HLF_COUNT)
4053 spell_attr = highlight_attr[spell_hlf];
4054
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004055 if (cap_col > 0)
4056 {
4057 if (p != prev_ptr
4058 && (p - nextline) + cap_col >= nextline_idx)
4059 {
4060 /* Remember that the word in the next line
4061 * must start with a capital. */
4062 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004063 cap_col = (int)((p - nextline) + cap_col
4064 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004065 }
4066 else
4067 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004068 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004069 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004070 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004071 }
4072 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004073 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004074 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004075 char_attr = hl_combine_attr(char_attr, spell_attr);
4076 else
4077 char_attr = hl_combine_attr(spell_attr, char_attr);
4078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079#endif
4080#ifdef FEAT_LINEBREAK
4081 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004082 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 */
4084 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004085 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
4087 n_extra = win_lbr_chartabsize(wp, ptr - (
4088# ifdef FEAT_MBYTE
4089 has_mbyte ? mb_l :
4090# endif
4091 1), (colnr_T)vcol, NULL) - 1;
4092 c_extra = ' ';
4093 if (vim_iswhite(c))
4094 c = ' ';
4095 }
4096#endif
4097
4098 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4099 {
4100 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004101 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
4103 n_attr = 1;
4104 extra_attr = hl_attr(HLF_8);
4105 saved_attr2 = char_attr; /* save current attr */
4106 }
4107#ifdef FEAT_MBYTE
4108 mb_c = c;
4109 if (enc_utf8 && (*mb_char2len)(c) > 1)
4110 {
4111 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004112 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004113 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 else
4116 mb_utf8 = FALSE;
4117#endif
4118 }
4119 }
4120
4121 /*
4122 * Handling of non-printable characters.
4123 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004124 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
4126 /*
4127 * when getting a character from the file, we may have to
4128 * turn it into something else on the way to putting it
4129 * into "ScreenLines".
4130 */
4131 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4132 {
4133 /* tab amount depends on current column */
4134 n_extra = (int)wp->w_buffer->b_p_ts
4135 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4136#ifdef FEAT_MBYTE
4137 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4138#endif
4139 if (wp->w_p_list)
4140 {
4141 c = lcs_tab1;
4142 c_extra = lcs_tab2;
4143 n_attr = n_extra + 1;
4144 extra_attr = hl_attr(HLF_8);
4145 saved_attr2 = char_attr; /* save current attr */
4146#ifdef FEAT_MBYTE
4147 mb_c = c;
4148 if (enc_utf8 && (*mb_char2len)(c) > 1)
4149 {
4150 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004151 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004152 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154#endif
4155 }
4156 else
4157 {
4158 c_extra = ' ';
4159 c = ' ';
4160 }
4161 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004162 else if (c == NUL
4163 && ((wp->w_p_list && lcs_eol > 0)
4164 || ((fromcol >= 0 || fromcol_prev >= 0)
4165 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004166#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004167 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004168#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004169 && (
4170# ifdef FEAT_RIGHTLEFT
4171 wp->w_p_rl ? (col >= 0) :
4172# endif
4173 (col < W_WIDTH(wp)))
4174 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004175 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004176 && (colnr_T)vcol == wp->w_virtcol)))
4177 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004179 /* Display a '$' after the line or highlight an extra
4180 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4182 /* For a diff line the highlighting continues after the
4183 * "$". */
4184 if (
4185# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004186 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187# ifdef LINE_ATTR
4188 &&
4189# endif
4190# endif
4191# ifdef LINE_ATTR
4192 line_attr == 0
4193# endif
4194 )
4195#endif
4196 {
4197#ifdef FEAT_VIRTUALEDIT
4198 /* In virtualedit, visual selections may extend
4199 * beyond end of line. */
4200 if (area_highlighting && virtual_active()
4201 && tocol != MAXCOL && vcol < tocol)
4202 n_extra = 0;
4203 else
4204#endif
4205 {
4206 p_extra = at_end_str;
4207 n_extra = 1;
4208 c_extra = NUL;
4209 }
4210 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004211 if (wp->w_p_list)
4212 c = lcs_eol;
4213 else
4214 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 lcs_eol_one = -1;
4216 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004217 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
4219 extra_attr = hl_attr(HLF_AT);
4220 n_attr = 1;
4221 }
4222#ifdef FEAT_MBYTE
4223 mb_c = c;
4224 if (enc_utf8 && (*mb_char2len)(c) > 1)
4225 {
4226 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004227 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004228 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230 else
4231 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4232#endif
4233 }
4234 else if (c != NUL)
4235 {
4236 p_extra = transchar(c);
4237#ifdef FEAT_RIGHTLEFT
4238 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4239 rl_mirror(p_extra); /* reverse "<12>" */
4240#endif
4241 n_extra = byte2cells(c) - 1;
4242 c_extra = NUL;
4243 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004244 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 n_attr = n_extra + 1;
4247 extra_attr = hl_attr(HLF_8);
4248 saved_attr2 = char_attr; /* save current attr */
4249 }
4250#ifdef FEAT_MBYTE
4251 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4252#endif
4253 }
4254#ifdef FEAT_VIRTUALEDIT
4255 else if (VIsual_active
4256 && (VIsual_mode == Ctrl_V
4257 || VIsual_mode == 'v')
4258 && virtual_active()
4259 && tocol != MAXCOL
4260 && vcol < tocol
4261 && (
4262# ifdef FEAT_RIGHTLEFT
4263 wp->w_p_rl ? (col >= 0) :
4264# endif
4265 (col < W_WIDTH(wp))))
4266 {
4267 c = ' ';
4268 --ptr; /* put it back at the NUL */
4269 }
4270#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004271#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 else if ((
4273# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004274 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 ) && (
4278# ifdef FEAT_RIGHTLEFT
4279 wp->w_p_rl ? (col >= 0) :
4280# endif
4281 (col < W_WIDTH(wp))))
4282 {
4283 /* Highlight until the right side of the window */
4284 c = ' ';
4285 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004286
4287 /* Remember we do the char for line highlighting. */
4288 ++did_line_attr;
4289
4290 /* don't do search HL for the rest of the line */
4291 if (line_attr != 0 && char_attr == search_attr && col > 0)
4292 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293# ifdef FEAT_DIFF
4294 if (diff_hlf == HLF_TXD)
4295 {
4296 diff_hlf = HLF_CHD;
4297 if (attr == 0 || char_attr != attr)
4298 char_attr = hl_attr(diff_hlf);
4299 }
4300# endif
4301 }
4302#endif
4303 }
4304 }
4305
4306 /* Don't override visual selection highlighting. */
4307 if (n_attr > 0
4308 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004309 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 char_attr = extra_attr;
4311
Bram Moolenaar81695252004-12-29 20:58:21 +00004312#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 /* XIM don't send preedit_start and preedit_end, but they send
4314 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4315 * im_is_preediting() here. */
4316 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004317 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 && (State & INSERT)
4319 && !p_imdisable
4320 && im_is_preediting()
4321 && draw_state == WL_LINE)
4322 {
4323 colnr_T tcol;
4324
4325 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004326 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 else
4328 tcol = preedit_end_col;
4329 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4330 {
4331 if (feedback_old_attr < 0)
4332 {
4333 feedback_col = 0;
4334 feedback_old_attr = char_attr;
4335 }
4336 char_attr = im_get_feedback_attr(feedback_col);
4337 if (char_attr < 0)
4338 char_attr = feedback_old_attr;
4339 feedback_col++;
4340 }
4341 else if (feedback_old_attr >= 0)
4342 {
4343 char_attr = feedback_old_attr;
4344 feedback_old_attr = -1;
4345 feedback_col = 0;
4346 }
4347 }
4348#endif
4349 /*
4350 * Handle the case where we are in column 0 but not on the first
4351 * character of the line and the user wants us to show us a
4352 * special character (via 'listchars' option "precedes:<char>".
4353 */
4354 if (lcs_prec_todo != NUL
4355 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4356#ifdef FEAT_DIFF
4357 && filler_todo <= 0
4358#endif
4359 && draw_state > WL_NR
4360 && c != NUL)
4361 {
4362 c = lcs_prec;
4363 lcs_prec_todo = NUL;
4364#ifdef FEAT_MBYTE
4365 mb_c = c;
4366 if (enc_utf8 && (*mb_char2len)(c) > 1)
4367 {
4368 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004369 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004370 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
4372 else
4373 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4374#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004375 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
4377 saved_attr3 = char_attr; /* save current attr */
4378 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4379 n_attr3 = 1;
4380 }
4381 }
4382
4383 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004384 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004386 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004387#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004388 || did_line_attr == 1
4389#endif
4390 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004392#ifdef FEAT_SEARCH_EXTRA
4393 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004394
4395 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004396 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004397 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004398#endif
4399
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 /* invert at least one char, used for Visual and empty line or
4401 * highlight match at end of line. If it's beyond the last
4402 * char on the screen, just overwrite that one (tricky!) Not
4403 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004404#ifdef FEAT_SEARCH_EXTRA
4405 prevcol_hl_flag = FALSE;
4406 if (prevcol == (long)search_hl.startcol)
4407 prevcol_hl_flag = TRUE;
4408 else
4409 {
4410 cur = wp->w_match_head;
4411 while (cur != NULL)
4412 {
4413 if (prevcol == (long)cur->hl.startcol)
4414 {
4415 prevcol_hl_flag = TRUE;
4416 break;
4417 }
4418 cur = cur->next;
4419 }
4420 }
4421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004423 && ((area_attr != 0 && vcol == fromcol
4424#ifdef FEAT_VISUAL
4425 && (VIsual_mode != Ctrl_V
4426 || lnum == VIsual.lnum
4427 || lnum == curwin->w_cursor.lnum)
4428#endif
4429 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430#ifdef FEAT_SEARCH_EXTRA
4431 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004432 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004433# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004434 && did_line_attr <= 1
4435# endif
4436 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437#endif
4438 ))
4439 {
4440 int n = 0;
4441
4442#ifdef FEAT_RIGHTLEFT
4443 if (wp->w_p_rl)
4444 {
4445 if (col < 0)
4446 n = 1;
4447 }
4448 else
4449#endif
4450 {
4451 if (col >= W_WIDTH(wp))
4452 n = -1;
4453 }
4454 if (n != 0)
4455 {
4456 /* At the window boundary, highlight the last character
4457 * instead (better than nothing). */
4458 off += n;
4459 col += n;
4460 }
4461 else
4462 {
4463 /* Add a blank character to highlight. */
4464 ScreenLines[off] = ' ';
4465#ifdef FEAT_MBYTE
4466 if (enc_utf8)
4467 ScreenLinesUC[off] = 0;
4468#endif
4469 }
4470#ifdef FEAT_SEARCH_EXTRA
4471 if (area_attr == 0)
4472 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004473 /* Use attributes from match with highest priority among
4474 * 'search_hl' and the match list. */
4475 char_attr = search_hl.attr;
4476 cur = wp->w_match_head;
4477 shl_flag = FALSE;
4478 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004479 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004480 if (shl_flag == FALSE
4481 && ((cur != NULL
4482 && cur->priority > SEARCH_HL_PRIORITY)
4483 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004484 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004485 shl = &search_hl;
4486 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004487 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004488 else
4489 shl = &cur->hl;
4490 if ((ptr - line) - 1 == (long)shl->startcol)
4491 char_attr = shl->attr;
4492 if (shl != &search_hl && cur != NULL)
4493 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496#endif
4497 ScreenAttrs[off] = char_attr;
4498#ifdef FEAT_RIGHTLEFT
4499 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004502 --off;
4503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 else
4505#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004508 ++off;
4509 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004510 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004511#ifdef FEAT_SYN_HL
4512 eol_hl_off = 1;
4513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
Bram Moolenaar91170f82006-05-05 21:15:17 +00004517 /*
4518 * At end of the text line.
4519 */
4520 if (c == NUL)
4521 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004522#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004523 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4524 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004525 {
4526 /* highlight last char after line */
4527 --col;
4528 --off;
4529 --vcol;
4530 }
4531
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004532 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004533 if (wp->w_p_wrap)
4534 v = wp->w_skipcol;
4535 else
4536 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004537 /* check if line ends before left margin */
4538 if (vcol < v + col - win_col_off(wp))
4539
4540 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004541 if (wp->w_p_cuc
Bram Moolenaara443af82007-11-08 13:51:42 +00004542 && (int)wp->w_virtcol >= vcol - eol_hl_off
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004543 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4544 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004545 && lnum != wp->w_cursor.lnum
4546# ifdef FEAT_RIGHTLEFT
4547 && !wp->w_p_rl
4548# endif
4549 )
4550 {
4551 while (col < W_WIDTH(wp))
4552 {
4553 ScreenLines[off] = ' ';
4554#ifdef FEAT_MBYTE
4555 if (enc_utf8)
4556 ScreenLinesUC[off] = 0;
4557#endif
4558 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004559 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004560 {
4561 ScreenAttrs[off] = hl_attr(HLF_CUC);
4562 break;
4563 }
4564 ScreenAttrs[off++] = 0;
4565 ++vcol;
4566 }
4567 }
4568#endif
4569
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4571 wp->w_p_rl);
4572 row++;
4573
4574 /*
4575 * Update w_cline_height and w_cline_folded if the cursor line was
4576 * updated (saves a call to plines() later).
4577 */
4578 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4579 {
4580 curwin->w_cline_row = startrow;
4581 curwin->w_cline_height = row - startrow;
4582#ifdef FEAT_FOLDING
4583 curwin->w_cline_folded = FALSE;
4584#endif
4585 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4586 }
4587
4588 break;
4589 }
4590
4591 /* line continues beyond line end */
4592 if (lcs_ext
4593 && !wp->w_p_wrap
4594#ifdef FEAT_DIFF
4595 && filler_todo <= 0
4596#endif
4597 && (
4598#ifdef FEAT_RIGHTLEFT
4599 wp->w_p_rl ? col == 0 :
4600#endif
4601 col == W_WIDTH(wp) - 1)
4602 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004603 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4605 {
4606 c = lcs_ext;
4607 char_attr = hl_attr(HLF_AT);
4608#ifdef FEAT_MBYTE
4609 mb_c = c;
4610 if (enc_utf8 && (*mb_char2len)(c) > 1)
4611 {
4612 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004613 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004614 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
4616 else
4617 mb_utf8 = FALSE;
4618#endif
4619 }
4620
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004621#ifdef FEAT_SYN_HL
4622 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4623 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004624 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004625 && lnum != wp->w_cursor.lnum
Bram Moolenaar54ef7112009-02-21 20:11:41 +00004626 && draw_state == WL_LINE
4627 && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004628 {
4629 vcol_save_attr = char_attr;
4630 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4631 }
4632 else
4633 vcol_save_attr = -1;
4634#endif
4635
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 /*
4637 * Store character to be displayed.
4638 * Skip characters that are left of the screen for 'nowrap'.
4639 */
4640 vcol_prev = vcol;
4641 if (draw_state < WL_LINE || n_skip <= 0)
4642 {
4643 /*
4644 * Store the character.
4645 */
4646#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4647 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4648 {
4649 /* A double-wide character is: put first halve in left cell. */
4650 --off;
4651 --col;
4652 }
4653#endif
4654 ScreenLines[off] = c;
4655#ifdef FEAT_MBYTE
4656 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004657 {
4658 if ((mb_c & 0xff00) == 0x8e00)
4659 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 else if (enc_utf8)
4663 {
4664 if (mb_utf8)
4665 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004666 int i;
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004669 if ((c & 0xff) == 0)
4670 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004671 for (i = 0; i < Screen_mco; ++i)
4672 {
4673 ScreenLinesC[i][off] = u8cc[i];
4674 if (u8cc[i] == 0)
4675 break;
4676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
4678 else
4679 ScreenLinesUC[off] = 0;
4680 }
4681 if (multi_attr)
4682 {
4683 ScreenAttrs[off] = multi_attr;
4684 multi_attr = 0;
4685 }
4686 else
4687#endif
4688 ScreenAttrs[off] = char_attr;
4689
4690#ifdef FEAT_MBYTE
4691 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4692 {
4693 /* Need to fill two screen columns. */
4694 ++off;
4695 ++col;
4696 if (enc_utf8)
4697 /* UTF-8: Put a 0 in the second screen char. */
4698 ScreenLines[off] = 0;
4699 else
4700 /* DBCS: Put second byte in the second screen char. */
4701 ScreenLines[off] = mb_c & 0xff;
4702 ++vcol;
4703 /* When "tocol" is halfway a character, set it to the end of
4704 * the character, otherwise highlighting won't stop. */
4705 if (tocol == vcol)
4706 ++tocol;
4707#ifdef FEAT_RIGHTLEFT
4708 if (wp->w_p_rl)
4709 {
4710 /* now it's time to backup one cell */
4711 --off;
4712 --col;
4713 }
4714#endif
4715 }
4716#endif
4717#ifdef FEAT_RIGHTLEFT
4718 if (wp->w_p_rl)
4719 {
4720 --off;
4721 --col;
4722 }
4723 else
4724#endif
4725 {
4726 ++off;
4727 ++col;
4728 }
4729 }
4730 else
4731 --n_skip;
4732
Bram Moolenaar64486672010-05-16 15:46:46 +02004733 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
4734 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00004735 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736#ifdef FEAT_DIFF
4737 && filler_todo <= 0
4738#endif
4739 )
4740 ++vcol;
4741
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004742#ifdef FEAT_SYN_HL
4743 if (vcol_save_attr >= 0)
4744 char_attr = vcol_save_attr;
4745#endif
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 /* restore attributes after "predeces" in 'listchars' */
4748 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4749 char_attr = saved_attr3;
4750
4751 /* restore attributes after last 'listchars' or 'number' char */
4752 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4753 char_attr = saved_attr2;
4754
4755 /*
4756 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004757 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 */
4759 if ((
4760#ifdef FEAT_RIGHTLEFT
4761 wp->w_p_rl ? (col < 0) :
4762#endif
4763 (col >= W_WIDTH(wp)))
4764 && (*ptr != NUL
4765#ifdef FEAT_DIFF
4766 || filler_todo > 0
4767#endif
4768 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4769 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4770 )
4771 {
4772 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4773 wp->w_p_rl);
4774 ++row;
4775 ++screen_row;
4776
4777 /* When not wrapping and finished diff lines, or when displayed
4778 * '$' and highlighting until last column, break here. */
4779 if ((!wp->w_p_wrap
4780#ifdef FEAT_DIFF
4781 && filler_todo <= 0
4782#endif
4783 ) || lcs_eol_one == -1)
4784 break;
4785
4786 /* When the window is too narrow draw all "@" lines. */
4787 if (draw_state != WL_LINE
4788#ifdef FEAT_DIFF
4789 && filler_todo <= 0
4790#endif
4791 )
4792 {
4793 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4794#ifdef FEAT_VERTSPLIT
4795 draw_vsep_win(wp, row);
4796#endif
4797 row = endrow;
4798 }
4799
4800 /* When line got too long for screen break here. */
4801 if (row == endrow)
4802 {
4803 ++row;
4804 break;
4805 }
4806
4807 if (screen_cur_row == screen_row - 1
4808#ifdef FEAT_DIFF
4809 && filler_todo <= 0
4810#endif
4811 && W_WIDTH(wp) == Columns)
4812 {
4813 /* Remember that the line wraps, used for modeless copy. */
4814 LineWraps[screen_row - 1] = TRUE;
4815
4816 /*
4817 * Special trick to make copy/paste of wrapped lines work with
4818 * xterm/screen: write an extra character beyond the end of
4819 * the line. This will work with all terminal types
4820 * (regardless of the xn,am settings).
4821 * Only do this on a fast tty.
4822 * Only do this if the cursor is on the current line
4823 * (something has been written in it).
4824 * Don't do this for the GUI.
4825 * Don't do this for double-width characters.
4826 * Don't do this for a window not at the right screen border.
4827 */
4828 if (p_tf
4829#ifdef FEAT_GUI
4830 && !gui.in_use
4831#endif
4832#ifdef FEAT_MBYTE
4833 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004834 && ((*mb_off2cells)(LineOffset[screen_row],
4835 LineOffset[screen_row] + screen_Columns)
4836 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004838 + (int)Columns - 2,
4839 LineOffset[screen_row] + screen_Columns)
4840 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#endif
4842 )
4843 {
4844 /* First make sure we are at the end of the screen line,
4845 * then output the same character again to let the
4846 * terminal know about the wrap. If the terminal doesn't
4847 * auto-wrap, we overwrite the character. */
4848 if (screen_cur_col != W_WIDTH(wp))
4849 screen_char(LineOffset[screen_row - 1]
4850 + (unsigned)Columns - 1,
4851 screen_row - 1, (int)(Columns - 1));
4852
4853#ifdef FEAT_MBYTE
4854 /* When there is a multi-byte character, just output a
4855 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004856 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4857 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 out_char(' ');
4859 else
4860#endif
4861 out_char(ScreenLines[LineOffset[screen_row - 1]
4862 + (Columns - 1)]);
4863 /* force a redraw of the first char on the next line */
4864 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4865 screen_start(); /* don't know where cursor is now */
4866 }
4867 }
4868
4869 col = 0;
4870 off = (unsigned)(current_ScreenLine - ScreenLines);
4871#ifdef FEAT_RIGHTLEFT
4872 if (wp->w_p_rl)
4873 {
4874 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4875 off += col;
4876 }
4877#endif
4878
4879 /* reset the drawing state for the start of a wrapped line */
4880 draw_state = WL_START;
4881 saved_n_extra = n_extra;
4882 saved_p_extra = p_extra;
4883 saved_c_extra = c_extra;
4884 saved_char_attr = char_attr;
4885 n_extra = 0;
4886 lcs_prec_todo = lcs_prec;
4887#ifdef FEAT_LINEBREAK
4888# ifdef FEAT_DIFF
4889 if (filler_todo <= 0)
4890# endif
4891 need_showbreak = TRUE;
4892#endif
4893#ifdef FEAT_DIFF
4894 --filler_todo;
4895 /* When the filler lines are actually below the last line of the
4896 * file, don't draw the line itself, break here. */
4897 if (filler_todo == 0 && wp->w_botfill)
4898 break;
4899#endif
4900 }
4901
4902 } /* for every character in the line */
4903
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004904#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004905 /* After an empty line check first word for capital. */
4906 if (*skipwhite(line) == NUL)
4907 {
4908 capcol_lnum = lnum + 1;
4909 cap_col = 0;
4910 }
4911#endif
4912
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 return row;
4914}
4915
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004916#ifdef FEAT_MBYTE
4917static int comp_char_differs __ARGS((int, int));
4918
4919/*
4920 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01004921 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004922 */
4923 static int
4924comp_char_differs(off_from, off_to)
4925 int off_from;
4926 int off_to;
4927{
4928 int i;
4929
4930 for (i = 0; i < Screen_mco; ++i)
4931 {
4932 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4933 return TRUE;
4934 if (ScreenLinesC[i][off_from] == 0)
4935 break;
4936 }
4937 return FALSE;
4938}
4939#endif
4940
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941/*
4942 * Check whether the given character needs redrawing:
4943 * - the (first byte of the) character is different
4944 * - the attributes are different
4945 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004946 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 */
4948 static int
4949char_needs_redraw(off_from, off_to, cols)
4950 int off_from;
4951 int off_to;
4952 int cols;
4953{
4954 if (cols > 0
4955 && ((ScreenLines[off_from] != ScreenLines[off_to]
4956 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4957
4958#ifdef FEAT_MBYTE
4959 || (enc_dbcs != 0
4960 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4961 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4962 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4963 : (cols > 1 && ScreenLines[off_from + 1]
4964 != ScreenLines[off_to + 1])))
4965 || (enc_utf8
4966 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4967 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004968 && comp_char_differs(off_from, off_to))
4969 || (cols > 1 && ScreenLines[off_from + 1]
4970 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971#endif
4972 ))
4973 return TRUE;
4974 return FALSE;
4975}
4976
4977/*
4978 * Move one "cooked" screen line to the screen, but only the characters that
4979 * have actually changed. Handle insert/delete character.
4980 * "coloff" gives the first column on the screen for this line.
4981 * "endcol" gives the columns where valid characters are.
4982 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4983 * needs to be cleared, negative otherwise.
4984 * "rlflag" is TRUE in a rightleft window:
4985 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4986 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4987 */
4988 static void
4989screen_line(row, coloff, endcol, clear_width
4990#ifdef FEAT_RIGHTLEFT
4991 , rlflag
4992#endif
4993 )
4994 int row;
4995 int coloff;
4996 int endcol;
4997 int clear_width;
4998#ifdef FEAT_RIGHTLEFT
4999 int rlflag;
5000#endif
5001{
5002 unsigned off_from;
5003 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005004#ifdef FEAT_MBYTE
5005 unsigned max_off_from;
5006 unsigned max_off_to;
5007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 int col = 0;
5009#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5010 int hl;
5011#endif
5012 int force = FALSE; /* force update rest of the line */
5013 int redraw_this /* bool: does character need redraw? */
5014#ifdef FEAT_GUI
5015 = TRUE /* For GUI when while-loop empty */
5016#endif
5017 ;
5018 int redraw_next; /* redraw_this for next character */
5019#ifdef FEAT_MBYTE
5020 int clear_next = FALSE;
5021 int char_cells; /* 1: normal char */
5022 /* 2: occupies two display cells */
5023# define CHAR_CELLS char_cells
5024#else
5025# define CHAR_CELLS 1
5026#endif
5027
5028# ifdef FEAT_CLIPBOARD
5029 clip_may_clear_selection(row, row);
5030# endif
5031
5032 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5033 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005034#ifdef FEAT_MBYTE
5035 max_off_from = off_from + screen_Columns;
5036 max_off_to = LineOffset[row] + screen_Columns;
5037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
5039#ifdef FEAT_RIGHTLEFT
5040 if (rlflag)
5041 {
5042 /* Clear rest first, because it's left of the text. */
5043 if (clear_width > 0)
5044 {
5045 while (col <= endcol && ScreenLines[off_to] == ' '
5046 && ScreenAttrs[off_to] == 0
5047# ifdef FEAT_MBYTE
5048 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5049# endif
5050 )
5051 {
5052 ++off_to;
5053 ++col;
5054 }
5055 if (col <= endcol)
5056 screen_fill(row, row + 1, col + coloff,
5057 endcol + coloff + 1, ' ', ' ', 0);
5058 }
5059 col = endcol + 1;
5060 off_to = LineOffset[row] + col + coloff;
5061 off_from += col;
5062 endcol = (clear_width > 0 ? clear_width : -clear_width);
5063 }
5064#endif /* FEAT_RIGHTLEFT */
5065
5066 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5067
5068 while (col < endcol)
5069 {
5070#ifdef FEAT_MBYTE
5071 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005072 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 else
5074 char_cells = 1;
5075#endif
5076
5077 redraw_this = redraw_next;
5078 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5079 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5080
5081#ifdef FEAT_GUI
5082 /* If the next character was bold, then redraw the current character to
5083 * remove any pixels that might have spilt over into us. This only
5084 * happens in the GUI.
5085 */
5086 if (redraw_next && gui.in_use)
5087 {
5088 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005089 if (hl > HL_ALL)
5090 hl = syn_attr2attr(hl);
5091 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 redraw_this = TRUE;
5093 }
5094#endif
5095
5096 if (redraw_this)
5097 {
5098 /*
5099 * Special handling when 'xs' termcap flag set (hpterm):
5100 * Attributes for characters are stored at the position where the
5101 * cursor is when writing the highlighting code. The
5102 * start-highlighting code must be written with the cursor on the
5103 * first highlighted character. The stop-highlighting code must
5104 * be written with the cursor just after the last highlighted
5105 * character.
5106 * Overwriting a character doesn't remove it's highlighting. Need
5107 * to clear the rest of the line, and force redrawing it
5108 * completely.
5109 */
5110 if ( p_wiv
5111 && !force
5112#ifdef FEAT_GUI
5113 && !gui.in_use
5114#endif
5115 && ScreenAttrs[off_to] != 0
5116 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5117 {
5118 /*
5119 * Need to remove highlighting attributes here.
5120 */
5121 windgoto(row, col + coloff);
5122 out_str(T_CE); /* clear rest of this screen line */
5123 screen_start(); /* don't know where cursor is now */
5124 force = TRUE; /* force redraw of rest of the line */
5125 redraw_next = TRUE; /* or else next char would miss out */
5126
5127 /*
5128 * If the previous character was highlighted, need to stop
5129 * highlighting at this character.
5130 */
5131 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5132 {
5133 screen_attr = ScreenAttrs[off_to - 1];
5134 term_windgoto(row, col + coloff);
5135 screen_stop_highlight();
5136 }
5137 else
5138 screen_attr = 0; /* highlighting has stopped */
5139 }
5140#ifdef FEAT_MBYTE
5141 if (enc_dbcs != 0)
5142 {
5143 /* Check if overwriting a double-byte with a single-byte or
5144 * the other way around requires another character to be
5145 * redrawn. For UTF-8 this isn't needed, because comparing
5146 * ScreenLinesUC[] is sufficient. */
5147 if (char_cells == 1
5148 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005149 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 /* Writing a single-cell character over a double-cell
5152 * character: need to redraw the next cell. */
5153 ScreenLines[off_to + 1] = 0;
5154 redraw_next = TRUE;
5155 }
5156 else if (char_cells == 2
5157 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005158 && (*mb_off2cells)(off_to, max_off_to) == 1
5159 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
5161 /* Writing the second half of a double-cell character over
5162 * a double-cell character: need to redraw the second
5163 * cell. */
5164 ScreenLines[off_to + 2] = 0;
5165 redraw_next = TRUE;
5166 }
5167
5168 if (enc_dbcs == DBCS_JPNU)
5169 ScreenLines2[off_to] = ScreenLines2[off_from];
5170 }
5171 /* When writing a single-width character over a double-width
5172 * character and at the end of the redrawn text, need to clear out
5173 * the right halve of the old character.
5174 * Also required when writing the right halve of a double-width
5175 * char over the left halve of an existing one. */
5176 if (has_mbyte && col + char_cells == endcol
5177 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005178 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005180 && (*mb_off2cells)(off_to, max_off_to) == 1
5181 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 clear_next = TRUE;
5183#endif
5184
5185 ScreenLines[off_to] = ScreenLines[off_from];
5186#ifdef FEAT_MBYTE
5187 if (enc_utf8)
5188 {
5189 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5190 if (ScreenLinesUC[off_from] != 0)
5191 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005192 int i;
5193
5194 for (i = 0; i < Screen_mco; ++i)
5195 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
5197 }
5198 if (char_cells == 2)
5199 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5200#endif
5201
5202#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005203 /* The bold trick makes a single column of pixels appear in the
5204 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 * character should be redrawn too. This happens for our own GUI
5206 * and for some xterms. */
5207 if (
5208# ifdef FEAT_GUI
5209 gui.in_use
5210# endif
5211# if defined(FEAT_GUI) && defined(UNIX)
5212 ||
5213# endif
5214# ifdef UNIX
5215 term_is_xterm
5216# endif
5217 )
5218 {
5219 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005220 if (hl > HL_ALL)
5221 hl = syn_attr2attr(hl);
5222 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 redraw_next = TRUE;
5224 }
5225#endif
5226 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5227#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005228 /* For simplicity set the attributes of second half of a
5229 * double-wide character equal to the first half. */
5230 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005232
5233 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 else
5236#endif
5237 screen_char(off_to, row, col + coloff);
5238 }
5239 else if ( p_wiv
5240#ifdef FEAT_GUI
5241 && !gui.in_use
5242#endif
5243 && col + coloff > 0)
5244 {
5245 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5246 {
5247 /*
5248 * Don't output stop-highlight when moving the cursor, it will
5249 * stop the highlighting when it should continue.
5250 */
5251 screen_attr = 0;
5252 }
5253 else if (screen_attr != 0)
5254 screen_stop_highlight();
5255 }
5256
5257 off_to += CHAR_CELLS;
5258 off_from += CHAR_CELLS;
5259 col += CHAR_CELLS;
5260 }
5261
5262#ifdef FEAT_MBYTE
5263 if (clear_next)
5264 {
5265 /* Clear the second half of a double-wide character of which the left
5266 * half was overwritten with a single-wide character. */
5267 ScreenLines[off_to] = ' ';
5268 if (enc_utf8)
5269 ScreenLinesUC[off_to] = 0;
5270 screen_char(off_to, row, col + coloff);
5271 }
5272#endif
5273
5274 if (clear_width > 0
5275#ifdef FEAT_RIGHTLEFT
5276 && !rlflag
5277#endif
5278 )
5279 {
5280#ifdef FEAT_GUI
5281 int startCol = col;
5282#endif
5283
5284 /* blank out the rest of the line */
5285 while (col < clear_width && ScreenLines[off_to] == ' '
5286 && ScreenAttrs[off_to] == 0
5287#ifdef FEAT_MBYTE
5288 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5289#endif
5290 )
5291 {
5292 ++off_to;
5293 ++col;
5294 }
5295 if (col < clear_width)
5296 {
5297#ifdef FEAT_GUI
5298 /*
5299 * In the GUI, clearing the rest of the line may leave pixels
5300 * behind if the first character cleared was bold. Some bold
5301 * fonts spill over the left. In this case we redraw the previous
5302 * character too. If we didn't skip any blanks above, then we
5303 * only redraw if the character wasn't already redrawn anyway.
5304 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005305 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
5307 hl = ScreenAttrs[off_to];
5308 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005309 {
5310 int prev_cells = 1;
5311# ifdef FEAT_MBYTE
5312 if (enc_utf8)
5313 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5314 * that its width is 2. */
5315 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5316 else if (enc_dbcs != 0)
5317 {
5318 /* find previous character by counting from first
5319 * column and get its width. */
5320 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005321 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005322
5323 while (off < off_to)
5324 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005325 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005326 off += prev_cells;
5327 }
5328 }
5329
5330 if (enc_dbcs != 0 && prev_cells > 1)
5331 screen_char_2(off_to - prev_cells, row,
5332 col + coloff - prev_cells);
5333 else
5334# endif
5335 screen_char(off_to - prev_cells, row,
5336 col + coloff - prev_cells);
5337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
5339#endif
5340 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5341 ' ', ' ', 0);
5342#ifdef FEAT_VERTSPLIT
5343 off_to += clear_width - col;
5344 col = clear_width;
5345#endif
5346 }
5347 }
5348
5349 if (clear_width > 0)
5350 {
5351#ifdef FEAT_VERTSPLIT
5352 /* For a window that's left of another, draw the separator char. */
5353 if (col + coloff < Columns)
5354 {
5355 int c;
5356
5357 c = fillchar_vsep(&hl);
5358 if (ScreenLines[off_to] != c
5359# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005360 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5361 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362# endif
5363 || ScreenAttrs[off_to] != hl)
5364 {
5365 ScreenLines[off_to] = c;
5366 ScreenAttrs[off_to] = hl;
5367# ifdef FEAT_MBYTE
5368 if (enc_utf8)
5369 {
5370 if (c >= 0x80)
5371 {
5372 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005373 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 else
5376 ScreenLinesUC[off_to] = 0;
5377 }
5378# endif
5379 screen_char(off_to, row, col + coloff);
5380 }
5381 }
5382 else
5383#endif
5384 LineWraps[row] = FALSE;
5385 }
5386}
5387
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005388#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005390 * Mirror text "str" for right-left displaying.
5391 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005393 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394rl_mirror(str)
5395 char_u *str;
5396{
5397 char_u *p1, *p2;
5398 int t;
5399
5400 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5401 {
5402 t = *p1;
5403 *p1 = *p2;
5404 *p2 = t;
5405 }
5406}
5407#endif
5408
5409#if defined(FEAT_WINDOWS) || defined(PROTO)
5410/*
5411 * mark all status lines for redraw; used after first :cd
5412 */
5413 void
5414status_redraw_all()
5415{
5416 win_T *wp;
5417
5418 for (wp = firstwin; wp; wp = wp->w_next)
5419 if (wp->w_status_height)
5420 {
5421 wp->w_redr_status = TRUE;
5422 redraw_later(VALID);
5423 }
5424}
5425
5426/*
5427 * mark all status lines of the current buffer for redraw
5428 */
5429 void
5430status_redraw_curbuf()
5431{
5432 win_T *wp;
5433
5434 for (wp = firstwin; wp; wp = wp->w_next)
5435 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5436 {
5437 wp->w_redr_status = TRUE;
5438 redraw_later(VALID);
5439 }
5440}
5441
5442/*
5443 * Redraw all status lines that need to be redrawn.
5444 */
5445 void
5446redraw_statuslines()
5447{
5448 win_T *wp;
5449
5450 for (wp = firstwin; wp; wp = wp->w_next)
5451 if (wp->w_redr_status)
5452 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005453 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005454 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455}
5456#endif
5457
5458#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5459/*
5460 * Redraw all status lines at the bottom of frame "frp".
5461 */
5462 void
5463win_redraw_last_status(frp)
5464 frame_T *frp;
5465{
5466 if (frp->fr_layout == FR_LEAF)
5467 frp->fr_win->w_redr_status = TRUE;
5468 else if (frp->fr_layout == FR_ROW)
5469 {
5470 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5471 win_redraw_last_status(frp);
5472 }
5473 else /* frp->fr_layout == FR_COL */
5474 {
5475 frp = frp->fr_child;
5476 while (frp->fr_next != NULL)
5477 frp = frp->fr_next;
5478 win_redraw_last_status(frp);
5479 }
5480}
5481#endif
5482
5483#ifdef FEAT_VERTSPLIT
5484/*
5485 * Draw the verticap separator right of window "wp" starting with line "row".
5486 */
5487 static void
5488draw_vsep_win(wp, row)
5489 win_T *wp;
5490 int row;
5491{
5492 int hl;
5493 int c;
5494
5495 if (wp->w_vsep_width)
5496 {
5497 /* draw the vertical separator right of this window */
5498 c = fillchar_vsep(&hl);
5499 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5500 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5501 c, ' ', hl);
5502 }
5503}
5504#endif
5505
5506#ifdef FEAT_WILDMENU
5507static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005508static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
5510/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005511 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 */
5513 static int
5514status_match_len(xp, s)
5515 expand_T *xp;
5516 char_u *s;
5517{
5518 int len = 0;
5519
5520#ifdef FEAT_MENU
5521 int emenu = (xp->xp_context == EXPAND_MENUS
5522 || xp->xp_context == EXPAND_MENUNAMES);
5523
5524 /* Check for menu separators - replace with '|'. */
5525 if (emenu && menu_is_separator(s))
5526 return 1;
5527#endif
5528
5529 while (*s != NUL)
5530 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005531 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005532 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005533 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535
5536 return len;
5537}
5538
5539/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005540 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005541 * These are backslashes used for escaping. Do show backslashes in help tags.
5542 */
5543 static int
5544skip_status_match_char(xp, s)
5545 expand_T *xp;
5546 char_u *s;
5547{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005548 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005549#ifdef FEAT_MENU
5550 || ((xp->xp_context == EXPAND_MENUS
5551 || xp->xp_context == EXPAND_MENUNAMES)
5552 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5553#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005554 )
5555 {
5556#ifndef BACKSLASH_IN_FILENAME
5557 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5558 return 2;
5559#endif
5560 return 1;
5561 }
5562 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005563}
5564
5565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 * Show wildchar matches in the status line.
5567 * Show at least the "match" item.
5568 * We start at item 'first_match' in the list and show all matches that fit.
5569 *
5570 * If inversion is possible we use it. Else '=' characters are used.
5571 */
5572 void
5573win_redr_status_matches(xp, num_matches, matches, match, showtail)
5574 expand_T *xp;
5575 int num_matches;
5576 char_u **matches; /* list of matches */
5577 int match;
5578 int showtail;
5579{
5580#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5581 int row;
5582 char_u *buf;
5583 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005584 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 int fillchar;
5586 int attr;
5587 int i;
5588 int highlight = TRUE;
5589 char_u *selstart = NULL;
5590 int selstart_col = 0;
5591 char_u *selend = NULL;
5592 static int first_match = 0;
5593 int add_left = FALSE;
5594 char_u *s;
5595#ifdef FEAT_MENU
5596 int emenu;
5597#endif
5598#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5599 int l;
5600#endif
5601
5602 if (matches == NULL) /* interrupted completion? */
5603 return;
5604
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005605#ifdef FEAT_MBYTE
5606 if (has_mbyte)
5607 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5608 else
5609#endif
5610 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if (buf == NULL)
5612 return;
5613
5614 if (match == -1) /* don't show match but original text */
5615 {
5616 match = 0;
5617 highlight = FALSE;
5618 }
5619 /* count 1 for the ending ">" */
5620 clen = status_match_len(xp, L_MATCH(match)) + 3;
5621 if (match == 0)
5622 first_match = 0;
5623 else if (match < first_match)
5624 {
5625 /* jumping left, as far as we can go */
5626 first_match = match;
5627 add_left = TRUE;
5628 }
5629 else
5630 {
5631 /* check if match fits on the screen */
5632 for (i = first_match; i < match; ++i)
5633 clen += status_match_len(xp, L_MATCH(i)) + 2;
5634 if (first_match > 0)
5635 clen += 2;
5636 /* jumping right, put match at the left */
5637 if ((long)clen > Columns)
5638 {
5639 first_match = match;
5640 /* if showing the last match, we can add some on the left */
5641 clen = 2;
5642 for (i = match; i < num_matches; ++i)
5643 {
5644 clen += status_match_len(xp, L_MATCH(i)) + 2;
5645 if ((long)clen >= Columns)
5646 break;
5647 }
5648 if (i == num_matches)
5649 add_left = TRUE;
5650 }
5651 }
5652 if (add_left)
5653 while (first_match > 0)
5654 {
5655 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5656 if ((long)clen >= Columns)
5657 break;
5658 --first_match;
5659 }
5660
5661 fillchar = fillchar_status(&attr, TRUE);
5662
5663 if (first_match == 0)
5664 {
5665 *buf = NUL;
5666 len = 0;
5667 }
5668 else
5669 {
5670 STRCPY(buf, "< ");
5671 len = 2;
5672 }
5673 clen = len;
5674
5675 i = first_match;
5676 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5677 {
5678 if (i == match)
5679 {
5680 selstart = buf + len;
5681 selstart_col = clen;
5682 }
5683
5684 s = L_MATCH(i);
5685 /* Check for menu separators - replace with '|' */
5686#ifdef FEAT_MENU
5687 emenu = (xp->xp_context == EXPAND_MENUS
5688 || xp->xp_context == EXPAND_MENUNAMES);
5689 if (emenu && menu_is_separator(s))
5690 {
5691 STRCPY(buf + len, transchar('|'));
5692 l = (int)STRLEN(buf + len);
5693 len += l;
5694 clen += l;
5695 }
5696 else
5697#endif
5698 for ( ; *s != NUL; ++s)
5699 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005700 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 clen += ptr2cells(s);
5702#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005703 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 STRNCPY(buf + len, s, l);
5706 s += l - 1;
5707 len += l;
5708 }
5709 else
5710#endif
5711 {
5712 STRCPY(buf + len, transchar_byte(*s));
5713 len += (int)STRLEN(buf + len);
5714 }
5715 }
5716 if (i == match)
5717 selend = buf + len;
5718
5719 *(buf + len++) = ' ';
5720 *(buf + len++) = ' ';
5721 clen += 2;
5722 if (++i == num_matches)
5723 break;
5724 }
5725
5726 if (i != num_matches)
5727 {
5728 *(buf + len++) = '>';
5729 ++clen;
5730 }
5731
5732 buf[len] = NUL;
5733
5734 row = cmdline_row - 1;
5735 if (row >= 0)
5736 {
5737 if (wild_menu_showing == 0)
5738 {
5739 if (msg_scrolled > 0)
5740 {
5741 /* Put the wildmenu just above the command line. If there is
5742 * no room, scroll the screen one line up. */
5743 if (cmdline_row == Rows - 1)
5744 {
5745 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5746 ++msg_scrolled;
5747 }
5748 else
5749 {
5750 ++cmdline_row;
5751 ++row;
5752 }
5753 wild_menu_showing = WM_SCROLLED;
5754 }
5755 else
5756 {
5757 /* Create status line if needed by setting 'laststatus' to 2.
5758 * Set 'winminheight' to zero to avoid that the window is
5759 * resized. */
5760 if (lastwin->w_status_height == 0)
5761 {
5762 save_p_ls = p_ls;
5763 save_p_wmh = p_wmh;
5764 p_ls = 2;
5765 p_wmh = 0;
5766 last_status(FALSE);
5767 }
5768 wild_menu_showing = WM_SHOWN;
5769 }
5770 }
5771
5772 screen_puts(buf, row, 0, attr);
5773 if (selstart != NULL && highlight)
5774 {
5775 *selend = NUL;
5776 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5777 }
5778
5779 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5780 }
5781
5782#ifdef FEAT_VERTSPLIT
5783 win_redraw_last_status(topframe);
5784#else
5785 lastwin->w_redr_status = TRUE;
5786#endif
5787 vim_free(buf);
5788}
5789#endif
5790
5791#if defined(FEAT_WINDOWS) || defined(PROTO)
5792/*
5793 * Redraw the status line of window wp.
5794 *
5795 * If inversion is possible we use it. Else '=' characters are used.
5796 */
5797 void
5798win_redr_status(wp)
5799 win_T *wp;
5800{
5801 int row;
5802 char_u *p;
5803 int len;
5804 int fillchar;
5805 int attr;
5806 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00005807 static int busy = FALSE;
5808
5809 /* It's possible to get here recursively when 'statusline' (indirectly)
5810 * invokes ":redrawstatus". Simply ignore the call then. */
5811 if (busy)
5812 return;
5813 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
5815 wp->w_redr_status = FALSE;
5816 if (wp->w_status_height == 0)
5817 {
5818 /* no status line, can only be last window */
5819 redraw_cmdline = TRUE;
5820 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005821 else if (!redrawing()
5822#ifdef FEAT_INS_EXPAND
5823 /* don't update status line when popup menu is visible and may be
5824 * drawn over it */
5825 || pum_visible()
5826#endif
5827 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 {
5829 /* Don't redraw right now, do it later. */
5830 wp->w_redr_status = TRUE;
5831 }
5832#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005833 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 {
5835 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00005836 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 }
5838#endif
5839 else
5840 {
5841 fillchar = fillchar_status(&attr, wp == curwin);
5842
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005843 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 p = NameBuff;
5845 len = (int)STRLEN(p);
5846
5847 if (wp->w_buffer->b_help
5848#ifdef FEAT_QUICKFIX
5849 || wp->w_p_pvw
5850#endif
5851 || bufIsChanged(wp->w_buffer)
5852 || wp->w_buffer->b_p_ro)
5853 *(p + len++) = ' ';
5854 if (wp->w_buffer->b_help)
5855 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005856 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 len += (int)STRLEN(p + len);
5858 }
5859#ifdef FEAT_QUICKFIX
5860 if (wp->w_p_pvw)
5861 {
5862 STRCPY(p + len, _("[Preview]"));
5863 len += (int)STRLEN(p + len);
5864 }
5865#endif
5866 if (bufIsChanged(wp->w_buffer))
5867 {
5868 STRCPY(p + len, "[+]");
5869 len += 3;
5870 }
5871 if (wp->w_buffer->b_p_ro)
5872 {
5873 STRCPY(p + len, "[RO]");
5874 len += 4;
5875 }
5876
5877#ifndef FEAT_VERTSPLIT
5878 this_ru_col = ru_col;
5879 if (this_ru_col < (Columns + 1) / 2)
5880 this_ru_col = (Columns + 1) / 2;
5881#else
5882 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5883 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5884 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5885 if (this_ru_col <= 1)
5886 {
5887 p = (char_u *)"<"; /* No room for file name! */
5888 len = 1;
5889 }
5890 else
5891#endif
5892#ifdef FEAT_MBYTE
5893 if (has_mbyte)
5894 {
5895 int clen = 0, i;
5896
5897 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005898 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 clen += (*mb_ptr2cells)(p + i);
5900 /* Find first character that will fit.
5901 * Going from start to end is much faster for DBCS. */
5902 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005903 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 clen -= (*mb_ptr2cells)(p + i);
5905 len = clen;
5906 if (i > 0)
5907 {
5908 p = p + i - 1;
5909 *p = '<';
5910 ++len;
5911 }
5912
5913 }
5914 else
5915#endif
5916 if (len > this_ru_col - 1)
5917 {
5918 p += len - (this_ru_col - 1);
5919 *p = '<';
5920 len = this_ru_col - 1;
5921 }
5922
5923 row = W_WINROW(wp) + wp->w_height;
5924 screen_puts(p, row, W_WINCOL(wp), attr);
5925 screen_fill(row, row + 1, len + W_WINCOL(wp),
5926 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5927
5928 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5929 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5930 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5931 - 1 + W_WINCOL(wp)), attr);
5932
5933#ifdef FEAT_CMDL_INFO
5934 win_redr_ruler(wp, TRUE);
5935#endif
5936 }
5937
5938#ifdef FEAT_VERTSPLIT
5939 /*
5940 * May need to draw the character below the vertical separator.
5941 */
5942 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5943 {
5944 if (stl_connected(wp))
5945 fillchar = fillchar_status(&attr, wp == curwin);
5946 else
5947 fillchar = fillchar_vsep(&attr);
5948 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5949 attr);
5950 }
5951#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00005952 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953}
5954
Bram Moolenaar238a5642006-02-21 22:12:05 +00005955#ifdef FEAT_STL_OPT
5956/*
5957 * Redraw the status line according to 'statusline' and take care of any
5958 * errors encountered.
5959 */
5960 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00005961redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00005962 win_T *wp;
5963{
Bram Moolenaar362f3562009-11-03 16:20:34 +00005964 static int entered = FALSE;
5965 int save_called_emsg = called_emsg;
5966
5967 /* When called recursively return. This can happen when the statusline
5968 * contains an expression that triggers a redraw. */
5969 if (entered)
5970 return;
5971 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00005972
5973 called_emsg = FALSE;
5974 win_redr_custom(wp, FALSE);
5975 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00005976 {
5977 /* When there is an error disable the statusline, otherwise the
5978 * display is messed up with errors and a redraw triggers the problem
5979 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005980 set_string_option_direct((char_u *)"statusline", -1,
5981 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005982 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00005983 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00005984 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00005985 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00005986}
5987#endif
5988
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989# ifdef FEAT_VERTSPLIT
5990/*
5991 * Return TRUE if the status line of window "wp" is connected to the status
5992 * line of the window right of it. If not, then it's a vertical separator.
5993 * Only call if (wp->w_vsep_width != 0).
5994 */
5995 int
5996stl_connected(wp)
5997 win_T *wp;
5998{
5999 frame_T *fr;
6000
6001 fr = wp->w_frame;
6002 while (fr->fr_parent != NULL)
6003 {
6004 if (fr->fr_parent->fr_layout == FR_COL)
6005 {
6006 if (fr->fr_next != NULL)
6007 break;
6008 }
6009 else
6010 {
6011 if (fr->fr_next != NULL)
6012 return TRUE;
6013 }
6014 fr = fr->fr_parent;
6015 }
6016 return FALSE;
6017}
6018# endif
6019
6020#endif /* FEAT_WINDOWS */
6021
6022#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6023/*
6024 * Get the value to show for the language mappings, active 'keymap'.
6025 */
6026 int
6027get_keymap_str(wp, buf, len)
6028 win_T *wp;
6029 char_u *buf; /* buffer for the result */
6030 int len; /* length of buffer */
6031{
6032 char_u *p;
6033
6034 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6035 return FALSE;
6036
6037 {
6038#ifdef FEAT_EVAL
6039 buf_T *old_curbuf = curbuf;
6040 win_T *old_curwin = curwin;
6041 char_u *s;
6042
6043 curbuf = wp->w_buffer;
6044 curwin = wp;
6045 STRCPY(buf, "b:keymap_name"); /* must be writable */
6046 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006047 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 --emsg_skip;
6049 curbuf = old_curbuf;
6050 curwin = old_curwin;
6051 if (p == NULL || *p == NUL)
6052#endif
6053 {
6054#ifdef FEAT_KEYMAP
6055 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6056 p = wp->w_buffer->b_p_keymap;
6057 else
6058#endif
6059 p = (char_u *)"lang";
6060 }
6061 if ((int)(STRLEN(p) + 3) < len)
6062 sprintf((char *)buf, "<%s>", p);
6063 else
6064 buf[0] = NUL;
6065#ifdef FEAT_EVAL
6066 vim_free(s);
6067#endif
6068 }
6069 return buf[0] != NUL;
6070}
6071#endif
6072
6073#if defined(FEAT_STL_OPT) || defined(PROTO)
6074/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006075 * Redraw the status line or ruler of window "wp".
6076 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 */
6078 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006079win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006081 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082{
6083 int attr;
6084 int curattr;
6085 int row;
6086 int col = 0;
6087 int maxwidth;
6088 int width;
6089 int n;
6090 int len;
6091 int fillchar;
6092 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006093 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006095 struct stl_hlrec hltab[STL_MAX_ITEM];
6096 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006097 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
6099 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006100 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006102 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006103 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006104 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006105 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006106 attr = hl_attr(HLF_TPF);
6107 maxwidth = Columns;
6108# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006109 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006112 else
6113 {
6114 row = W_WINROW(wp) + wp->w_height;
6115 fillchar = fillchar_status(&attr, wp == curwin);
6116 maxwidth = W_WIDTH(wp);
6117
6118 if (draw_ruler)
6119 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006120 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006121 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006122 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006123 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006124 if (*++stl == '-')
6125 stl++;
6126 if (atoi((char *)stl))
6127 while (VIM_ISDIGIT(*stl))
6128 stl++;
6129 if (*stl++ != '(')
6130 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006131 }
6132#ifdef FEAT_VERTSPLIT
6133 col = ru_col - (Columns - W_WIDTH(wp));
6134 if (col < (W_WIDTH(wp) + 1) / 2)
6135 col = (W_WIDTH(wp) + 1) / 2;
6136#else
6137 col = ru_col;
6138 if (col > (Columns + 1) / 2)
6139 col = (Columns + 1) / 2;
6140#endif
6141 maxwidth = W_WIDTH(wp) - col;
6142#ifdef FEAT_WINDOWS
6143 if (!wp->w_status_height)
6144#endif
6145 {
6146 row = Rows - 1;
6147 --maxwidth; /* writing in last column may cause scrolling */
6148 fillchar = ' ';
6149 attr = 0;
6150 }
6151
6152# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006153 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006154# endif
6155 }
6156 else
6157 {
6158 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006159 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006160 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006161 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006162# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006163 use_sandbox = was_set_insecurely((char_u *)"statusline",
6164 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006165# endif
6166 }
6167
6168#ifdef FEAT_VERTSPLIT
6169 col += W_WINCOL(wp);
6170#endif
6171 }
6172
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 if (maxwidth <= 0)
6174 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
Bram Moolenaar362f3562009-11-03 16:20:34 +00006176 /* Make a copy, because the statusline may include a function call that
6177 * might change the option value and free the memory. */
6178 stl = vim_strsave(stl);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006179 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6180 buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006181 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006182 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006183 vim_free(stl);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006184 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006186 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 {
6188#ifdef FEAT_MBYTE
6189 len += (*mb_char2bytes)(fillchar, buf + len);
6190#else
6191 buf[len++] = fillchar;
6192#endif
6193 ++width;
6194 }
6195 buf[len] = NUL;
6196
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006197 /*
6198 * Draw each snippet with the specified highlighting.
6199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 curattr = attr;
6201 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006202 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006204 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 screen_puts_len(p, len, row, col, curattr);
6206 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006207 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006209 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006211 else if (hltab[n].userhl < 0)
6212 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006214 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006215 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216#endif
6217 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006218 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
6220 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006221
6222 if (wp == NULL)
6223 {
6224 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6225 col = 0;
6226 len = 0;
6227 p = buf;
6228 fillchar = 0;
6229 for (n = 0; tabtab[n].start != NULL; n++)
6230 {
6231 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6232 while (col < len)
6233 TabPageIdxs[col++] = fillchar;
6234 p = tabtab[n].start;
6235 fillchar = tabtab[n].userhl;
6236 }
6237 while (col < Columns)
6238 TabPageIdxs[col++] = fillchar;
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240}
6241
6242#endif /* FEAT_STL_OPT */
6243
6244/*
6245 * Output a single character directly to the screen and update ScreenLines.
6246 */
6247 void
6248screen_putchar(c, row, col, attr)
6249 int c;
6250 int row, col;
6251 int attr;
6252{
6253#ifdef FEAT_MBYTE
6254 char_u buf[MB_MAXBYTES + 1];
6255
6256 buf[(*mb_char2bytes)(c, buf)] = NUL;
6257#else
6258 char_u buf[2];
6259
6260 buf[0] = c;
6261 buf[1] = NUL;
6262#endif
6263 screen_puts(buf, row, col, attr);
6264}
6265
6266/*
6267 * Get a single character directly from ScreenLines into "bytes[]".
6268 * Also return its attribute in *attrp;
6269 */
6270 void
6271screen_getbytes(row, col, bytes, attrp)
6272 int row, col;
6273 char_u *bytes;
6274 int *attrp;
6275{
6276 unsigned off;
6277
6278 /* safety check */
6279 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6280 {
6281 off = LineOffset[row] + col;
6282 *attrp = ScreenAttrs[off];
6283 bytes[0] = ScreenLines[off];
6284 bytes[1] = NUL;
6285
6286#ifdef FEAT_MBYTE
6287 if (enc_utf8 && ScreenLinesUC[off] != 0)
6288 bytes[utfc_char2bytes(off, bytes)] = NUL;
6289 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6290 {
6291 bytes[0] = ScreenLines[off];
6292 bytes[1] = ScreenLines2[off];
6293 bytes[2] = NUL;
6294 }
6295 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6296 {
6297 bytes[1] = ScreenLines[off + 1];
6298 bytes[2] = NUL;
6299 }
6300#endif
6301 }
6302}
6303
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006304#ifdef FEAT_MBYTE
6305static int screen_comp_differs __ARGS((int, int*));
6306
6307/*
6308 * Return TRUE if composing characters for screen posn "off" differs from
6309 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006310 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006311 */
6312 static int
6313screen_comp_differs(off, u8cc)
6314 int off;
6315 int *u8cc;
6316{
6317 int i;
6318
6319 for (i = 0; i < Screen_mco; ++i)
6320 {
6321 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6322 return TRUE;
6323 if (u8cc[i] == 0)
6324 break;
6325 }
6326 return FALSE;
6327}
6328#endif
6329
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330/*
6331 * Put string '*text' on the screen at position 'row' and 'col', with
6332 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6333 * Note: only outputs within one row, message is truncated at screen boundary!
6334 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6335 */
6336 void
6337screen_puts(text, row, col, attr)
6338 char_u *text;
6339 int row;
6340 int col;
6341 int attr;
6342{
6343 screen_puts_len(text, -1, row, col, attr);
6344}
6345
6346/*
6347 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6348 * a NUL.
6349 */
6350 void
6351screen_puts_len(text, len, row, col, attr)
6352 char_u *text;
6353 int len;
6354 int row;
6355 int col;
6356 int attr;
6357{
6358 unsigned off;
6359 char_u *ptr = text;
6360 int c;
6361#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006362 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 int mbyte_blen = 1;
6364 int mbyte_cells = 1;
6365 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006366 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 int clear_next_cell = FALSE;
6368# ifdef FEAT_ARABIC
6369 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006370 int pc, nc, nc1;
6371 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372# endif
6373#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006374#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6375 int force_redraw_this;
6376 int force_redraw_next = FALSE;
6377#endif
6378 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
6380 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6381 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006382 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
Bram Moolenaarc236c162008-07-13 17:41:49 +00006384#ifdef FEAT_MBYTE
6385 /* When drawing over the right halve of a double-wide char clear out the
6386 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006387 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006388# ifdef FEAT_GUI
6389 && !gui.in_use
6390# endif
6391 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006392 {
6393 ScreenLines[off - 1] = ' ';
6394 ScreenAttrs[off - 1] = 0;
6395 if (enc_utf8)
6396 {
6397 ScreenLinesUC[off - 1] = 0;
6398 ScreenLinesC[0][off - 1] = 0;
6399 }
6400 /* redraw the previous cell, make it empty */
6401 screen_char(off - 1, row, col - 1);
6402 /* force the cell at "col" to be redrawn */
6403 force_redraw_next = TRUE;
6404 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006405#endif
6406
Bram Moolenaar367329b2007-08-30 11:53:22 +00006407#ifdef FEAT_MBYTE
6408 max_off = LineOffset[row] + screen_Columns;
6409#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006410 while (col < screen_Columns
6411 && (len < 0 || (int)(ptr - text) < len)
6412 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 {
6414 c = *ptr;
6415#ifdef FEAT_MBYTE
6416 /* check if this is the first byte of a multibyte */
6417 if (has_mbyte)
6418 {
6419 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006420 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006422 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6424 mbyte_cells = 1;
6425 else if (enc_dbcs != 0)
6426 mbyte_cells = mbyte_blen;
6427 else /* enc_utf8 */
6428 {
6429 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006430 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 (int)((text + len) - ptr));
6432 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006433 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006435# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 /* Non-BMP character: display as ? or fullwidth ?. */
6437 if (u8c >= 0x10000)
6438 {
6439 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6440 if (attr == 0)
6441 attr = hl_attr(HLF_8);
6442 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444# ifdef FEAT_ARABIC
6445 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6446 {
6447 /* Do Arabic shaping. */
6448 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6449 {
6450 /* Past end of string to be displayed. */
6451 nc = NUL;
6452 nc1 = NUL;
6453 }
6454 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006455 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006456 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6457 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006458 nc1 = pcc[0];
6459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 pc = prev_c;
6461 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006462 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 }
6464 else
6465 prev_c = u8c;
6466# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006467 if (col + mbyte_cells > screen_Columns)
6468 {
6469 /* Only 1 cell left, but character requires 2 cells:
6470 * display a '>' in the last column to avoid wrapping. */
6471 c = '>';
6472 mbyte_cells = 1;
6473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 }
6475 }
6476#endif
6477
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006478#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6479 force_redraw_this = force_redraw_next;
6480 force_redraw_next = FALSE;
6481#endif
6482
6483 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484#ifdef FEAT_MBYTE
6485 || (mbyte_cells == 2
6486 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6487 || (enc_dbcs == DBCS_JPNU
6488 && c == 0x8e
6489 && ScreenLines2[off] != ptr[1])
6490 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006491 && (ScreenLinesUC[off] !=
6492 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6493 || (ScreenLinesUC[off] != 0
6494 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495#endif
6496 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006497 || exmode_active;
6498
6499 if (need_redraw
6500#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6501 || force_redraw_this
6502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 )
6504 {
6505#if defined(FEAT_GUI) || defined(UNIX)
6506 /* The bold trick makes a single row of pixels appear in the next
6507 * character. When a bold character is removed, the next
6508 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006509 * and for some xterms. */
6510 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511# ifdef FEAT_GUI
6512 gui.in_use
6513# endif
6514# if defined(FEAT_GUI) && defined(UNIX)
6515 ||
6516# endif
6517# ifdef UNIX
6518 term_is_xterm
6519# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006520 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006522 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006524 if (n > HL_ALL)
6525 n = syn_attr2attr(n);
6526 if (n & HL_BOLD)
6527 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 }
6529#endif
6530#ifdef FEAT_MBYTE
6531 /* When at the end of the text and overwriting a two-cell
6532 * character with a one-cell character, need to clear the next
6533 * cell. Also when overwriting the left halve of a two-cell char
6534 * with the right halve of a two-cell char. Do this only once
6535 * (mb_off2cells() may return 2 on the right halve). */
6536 if (clear_next_cell)
6537 clear_next_cell = FALSE;
6538 else if (has_mbyte
6539 && (len < 0 ? ptr[mbyte_blen] == NUL
6540 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006541 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006543 && (*mb_off2cells)(off, max_off) == 1
6544 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 clear_next_cell = TRUE;
6546
6547 /* Make sure we never leave a second byte of a double-byte behind,
6548 * it confuses mb_off2cells(). */
6549 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006550 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006552 && (*mb_off2cells)(off, max_off) == 1
6553 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 ScreenLines[off + mbyte_blen] = 0;
6555#endif
6556 ScreenLines[off] = c;
6557 ScreenAttrs[off] = attr;
6558#ifdef FEAT_MBYTE
6559 if (enc_utf8)
6560 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006561 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 ScreenLinesUC[off] = 0;
6563 else
6564 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006565 int i;
6566
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006568 for (i = 0; i < Screen_mco; ++i)
6569 {
6570 ScreenLinesC[i][off] = u8cc[i];
6571 if (u8cc[i] == 0)
6572 break;
6573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
6575 if (mbyte_cells == 2)
6576 {
6577 ScreenLines[off + 1] = 0;
6578 ScreenAttrs[off + 1] = attr;
6579 }
6580 screen_char(off, row, col);
6581 }
6582 else if (mbyte_cells == 2)
6583 {
6584 ScreenLines[off + 1] = ptr[1];
6585 ScreenAttrs[off + 1] = attr;
6586 screen_char_2(off, row, col);
6587 }
6588 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6589 {
6590 ScreenLines2[off] = ptr[1];
6591 screen_char(off, row, col);
6592 }
6593 else
6594#endif
6595 screen_char(off, row, col);
6596 }
6597#ifdef FEAT_MBYTE
6598 if (has_mbyte)
6599 {
6600 off += mbyte_cells;
6601 col += mbyte_cells;
6602 ptr += mbyte_blen;
6603 if (clear_next_cell)
6604 ptr = (char_u *)" ";
6605 }
6606 else
6607#endif
6608 {
6609 ++off;
6610 ++col;
6611 ++ptr;
6612 }
6613 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006614
6615#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6616 /* If we detected the next character needs to be redrawn, but the text
6617 * doesn't extend up to there, update the character here. */
6618 if (force_redraw_next && col < screen_Columns)
6619 {
6620# ifdef FEAT_MBYTE
6621 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6622 screen_char_2(off, row, col);
6623 else
6624# endif
6625 screen_char(off, row, col);
6626 }
6627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628}
6629
6630#ifdef FEAT_SEARCH_EXTRA
6631/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006632 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 */
6634 static void
6635start_search_hl()
6636{
6637 if (p_hls && !no_hlsearch)
6638 {
6639 last_pat_prog(&search_hl.rm);
6640 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006641# ifdef FEAT_RELTIME
6642 /* Set the time limit to 'redrawtime'. */
6643 profile_setlimit(p_rdt, &search_hl.tm);
6644# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
6646}
6647
6648/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006649 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 */
6651 static void
6652end_search_hl()
6653{
6654 if (search_hl.rm.regprog != NULL)
6655 {
6656 vim_free(search_hl.rm.regprog);
6657 search_hl.rm.regprog = NULL;
6658 }
6659}
6660
6661/*
6662 * Advance to the match in window "wp" line "lnum" or past it.
6663 */
6664 static void
6665prepare_search_hl(wp, lnum)
6666 win_T *wp;
6667 linenr_T lnum;
6668{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006669 matchitem_T *cur; /* points to the match list */
6670 match_T *shl; /* points to search_hl or a match */
6671 int shl_flag; /* flag to indicate whether search_hl
6672 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 int n;
6674
6675 /*
6676 * When using a multi-line pattern, start searching at the top
6677 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006678 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006680 cur = wp->w_match_head;
6681 shl_flag = FALSE;
6682 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006684 if (shl_flag == FALSE)
6685 {
6686 shl = &search_hl;
6687 shl_flag = TRUE;
6688 }
6689 else
6690 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (shl->rm.regprog != NULL
6692 && shl->lnum == 0
6693 && re_multiline(shl->rm.regprog))
6694 {
6695 if (shl->first_lnum == 0)
6696 {
6697# ifdef FEAT_FOLDING
6698 for (shl->first_lnum = lnum;
6699 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6700 if (hasFoldingWin(wp, shl->first_lnum - 1,
6701 NULL, NULL, TRUE, NULL))
6702 break;
6703# else
6704 shl->first_lnum = wp->w_topline;
6705# endif
6706 }
6707 n = 0;
6708 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6709 {
6710 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6711 if (shl->lnum != 0)
6712 {
6713 shl->first_lnum = shl->lnum
6714 + shl->rm.endpos[0].lnum
6715 - shl->rm.startpos[0].lnum;
6716 n = shl->rm.endpos[0].col;
6717 }
6718 else
6719 {
6720 ++shl->first_lnum;
6721 n = 0;
6722 }
6723 }
6724 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006725 if (shl != &search_hl && cur != NULL)
6726 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 }
6728}
6729
6730/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006731 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 * Uses shl->buf.
6733 * Sets shl->lnum and shl->rm contents.
6734 * Note: Assumes a previous match is always before "lnum", unless
6735 * shl->lnum is zero.
6736 * Careful: Any pointers for buffer lines will become invalid.
6737 */
6738 static void
6739next_search_hl(win, shl, lnum, mincol)
6740 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006741 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 linenr_T lnum;
6743 colnr_T mincol; /* minimal column for a match */
6744{
6745 linenr_T l;
6746 colnr_T matchcol;
6747 long nmatched;
6748
6749 if (shl->lnum != 0)
6750 {
6751 /* Check for three situations:
6752 * 1. If the "lnum" is below a previous match, start a new search.
6753 * 2. If the previous match includes "mincol", use it.
6754 * 3. Continue after the previous match.
6755 */
6756 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6757 if (lnum > l)
6758 shl->lnum = 0;
6759 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6760 return;
6761 }
6762
6763 /*
6764 * Repeat searching for a match until one is found that includes "mincol"
6765 * or none is found in this line.
6766 */
6767 called_emsg = FALSE;
6768 for (;;)
6769 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006770#ifdef FEAT_RELTIME
6771 /* Stop searching after passing the time limit. */
6772 if (profile_passed_limit(&(shl->tm)))
6773 {
6774 shl->lnum = 0; /* no match found in time */
6775 break;
6776 }
6777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 /* Three situations:
6779 * 1. No useful previous match: search from start of line.
6780 * 2. Not Vi compatible or empty match: continue at next character.
6781 * Break the loop if this is beyond the end of the line.
6782 * 3. Vi compatible searching: continue at end of previous match.
6783 */
6784 if (shl->lnum == 0)
6785 matchcol = 0;
6786 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6787 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006788 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006790 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006791
6792 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006793 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006794 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006796 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 shl->lnum = 0;
6798 break;
6799 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006800#ifdef FEAT_MBYTE
6801 if (has_mbyte)
6802 matchcol += mb_ptr2len(ml);
6803 else
6804#endif
6805 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 }
6807 else
6808 matchcol = shl->rm.endpos[0].col;
6809
6810 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006811 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6812#ifdef FEAT_RELTIME
6813 &(shl->tm)
6814#else
6815 NULL
6816#endif
6817 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (called_emsg)
6819 {
6820 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006821 if (shl == &search_hl)
6822 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006823 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006824 vim_free(shl->rm.regprog);
6825 no_hlsearch = TRUE;
6826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006828 shl->lnum = 0;
6829 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 break;
6831 }
6832 if (nmatched == 0)
6833 {
6834 shl->lnum = 0; /* no match found */
6835 break;
6836 }
6837 if (shl->rm.startpos[0].lnum > 0
6838 || shl->rm.startpos[0].col >= mincol
6839 || nmatched > 1
6840 || shl->rm.endpos[0].col > mincol)
6841 {
6842 shl->lnum += shl->rm.startpos[0].lnum;
6843 break; /* useful match found */
6844 }
6845 }
6846}
6847#endif
6848
6849 static void
6850screen_start_highlight(attr)
6851 int attr;
6852{
6853 attrentry_T *aep = NULL;
6854
6855 screen_attr = attr;
6856 if (full_screen
6857#ifdef WIN3264
6858 && termcap_active
6859#endif
6860 )
6861 {
6862#ifdef FEAT_GUI
6863 if (gui.in_use)
6864 {
6865 char buf[20];
6866
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006867 /* The GUI handles this internally. */
6868 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 OUT_STR(buf);
6870 }
6871 else
6872#endif
6873 {
6874 if (attr > HL_ALL) /* special HL attr. */
6875 {
6876 if (t_colors > 1)
6877 aep = syn_cterm_attr2entry(attr);
6878 else
6879 aep = syn_term_attr2entry(attr);
6880 if (aep == NULL) /* did ":syntax clear" */
6881 attr = 0;
6882 else
6883 attr = aep->ae_attr;
6884 }
6885 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6886 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006887 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6888 && cterm_normal_fg_bold)
6889 /* If the Normal FG color has BOLD attribute and the new HL
6890 * has a FG color defined, clear BOLD. */
6891 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6893 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006894 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6895 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 out_str(T_US);
6897 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6898 out_str(T_CZH);
6899 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6900 out_str(T_MR);
6901
6902 /*
6903 * Output the color or start string after bold etc., in case the
6904 * bold etc. override the color setting.
6905 */
6906 if (aep != NULL)
6907 {
6908 if (t_colors > 1)
6909 {
6910 if (aep->ae_u.cterm.fg_color)
6911 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6912 if (aep->ae_u.cterm.bg_color)
6913 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6914 }
6915 else
6916 {
6917 if (aep->ae_u.term.start != NULL)
6918 out_str(aep->ae_u.term.start);
6919 }
6920 }
6921 }
6922 }
6923}
6924
6925 void
6926screen_stop_highlight()
6927{
6928 int do_ME = FALSE; /* output T_ME code */
6929
6930 if (screen_attr != 0
6931#ifdef WIN3264
6932 && termcap_active
6933#endif
6934 )
6935 {
6936#ifdef FEAT_GUI
6937 if (gui.in_use)
6938 {
6939 char buf[20];
6940
6941 /* use internal GUI code */
6942 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6943 OUT_STR(buf);
6944 }
6945 else
6946#endif
6947 {
6948 if (screen_attr > HL_ALL) /* special HL attr. */
6949 {
6950 attrentry_T *aep;
6951
6952 if (t_colors > 1)
6953 {
6954 /*
6955 * Assume that t_me restores the original colors!
6956 */
6957 aep = syn_cterm_attr2entry(screen_attr);
6958 if (aep != NULL && (aep->ae_u.cterm.fg_color
6959 || aep->ae_u.cterm.bg_color))
6960 do_ME = TRUE;
6961 }
6962 else
6963 {
6964 aep = syn_term_attr2entry(screen_attr);
6965 if (aep != NULL && aep->ae_u.term.stop != NULL)
6966 {
6967 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6968 do_ME = TRUE;
6969 else
6970 out_str(aep->ae_u.term.stop);
6971 }
6972 }
6973 if (aep == NULL) /* did ":syntax clear" */
6974 screen_attr = 0;
6975 else
6976 screen_attr = aep->ae_attr;
6977 }
6978
6979 /*
6980 * Often all ending-codes are equal to T_ME. Avoid outputting the
6981 * same sequence several times.
6982 */
6983 if (screen_attr & HL_STANDOUT)
6984 {
6985 if (STRCMP(T_SE, T_ME) == 0)
6986 do_ME = TRUE;
6987 else
6988 out_str(T_SE);
6989 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006990 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
6992 if (STRCMP(T_UE, T_ME) == 0)
6993 do_ME = TRUE;
6994 else
6995 out_str(T_UE);
6996 }
6997 if (screen_attr & HL_ITALIC)
6998 {
6999 if (STRCMP(T_CZR, T_ME) == 0)
7000 do_ME = TRUE;
7001 else
7002 out_str(T_CZR);
7003 }
7004 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7005 out_str(T_ME);
7006
7007 if (t_colors > 1)
7008 {
7009 /* set Normal cterm colors */
7010 if (cterm_normal_fg_color != 0)
7011 term_fg_color(cterm_normal_fg_color - 1);
7012 if (cterm_normal_bg_color != 0)
7013 term_bg_color(cterm_normal_bg_color - 1);
7014 if (cterm_normal_fg_bold)
7015 out_str(T_MD);
7016 }
7017 }
7018 }
7019 screen_attr = 0;
7020}
7021
7022/*
7023 * Reset the colors for a cterm. Used when leaving Vim.
7024 * The machine specific code may override this again.
7025 */
7026 void
7027reset_cterm_colors()
7028{
7029 if (t_colors > 1)
7030 {
7031 /* set Normal cterm colors */
7032 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7033 {
7034 out_str(T_OP);
7035 screen_attr = -1;
7036 }
7037 if (cterm_normal_fg_bold)
7038 {
7039 out_str(T_ME);
7040 screen_attr = -1;
7041 }
7042 }
7043}
7044
7045/*
7046 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7047 * using the attributes from ScreenAttrs["off"].
7048 */
7049 static void
7050screen_char(off, row, col)
7051 unsigned off;
7052 int row;
7053 int col;
7054{
7055 int attr;
7056
7057 /* Check for illegal values, just in case (could happen just after
7058 * resizing). */
7059 if (row >= screen_Rows || col >= screen_Columns)
7060 return;
7061
7062 /* Outputting the last character on the screen may scrollup the screen.
7063 * Don't to it! Mark the character invalid (update it when scrolled up) */
7064 if (row == screen_Rows - 1 && col == screen_Columns - 1
7065#ifdef FEAT_RIGHTLEFT
7066 /* account for first command-line character in rightleft mode */
7067 && !cmdmsg_rl
7068#endif
7069 )
7070 {
7071 ScreenAttrs[off] = (sattr_T)-1;
7072 return;
7073 }
7074
7075 /*
7076 * Stop highlighting first, so it's easier to move the cursor.
7077 */
7078#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7079 if (screen_char_attr != 0)
7080 attr = screen_char_attr;
7081 else
7082#endif
7083 attr = ScreenAttrs[off];
7084 if (screen_attr != attr)
7085 screen_stop_highlight();
7086
7087 windgoto(row, col);
7088
7089 if (screen_attr != attr)
7090 screen_start_highlight(attr);
7091
7092#ifdef FEAT_MBYTE
7093 if (enc_utf8 && ScreenLinesUC[off] != 0)
7094 {
7095 char_u buf[MB_MAXBYTES + 1];
7096
7097 /* Convert UTF-8 character to bytes and write it. */
7098
7099 buf[utfc_char2bytes(off, buf)] = NUL;
7100
7101 out_str(buf);
7102 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7103 ++screen_cur_col;
7104 }
7105 else
7106#endif
7107 {
7108#ifdef FEAT_MBYTE
7109 out_flush_check();
7110#endif
7111 out_char(ScreenLines[off]);
7112#ifdef FEAT_MBYTE
7113 /* double-byte character in single-width cell */
7114 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7115 out_char(ScreenLines2[off]);
7116#endif
7117 }
7118
7119 screen_cur_col++;
7120}
7121
7122#ifdef FEAT_MBYTE
7123
7124/*
7125 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7126 * on the screen at position 'row' and 'col'.
7127 * The attributes of the first byte is used for all. This is required to
7128 * output the two bytes of a double-byte character with nothing in between.
7129 */
7130 static void
7131screen_char_2(off, row, col)
7132 unsigned off;
7133 int row;
7134 int col;
7135{
7136 /* Check for illegal values (could be wrong when screen was resized). */
7137 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7138 return;
7139
7140 /* Outputting the last character on the screen may scrollup the screen.
7141 * Don't to it! Mark the character invalid (update it when scrolled up) */
7142 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7143 {
7144 ScreenAttrs[off] = (sattr_T)-1;
7145 return;
7146 }
7147
7148 /* Output the first byte normally (positions the cursor), then write the
7149 * second byte directly. */
7150 screen_char(off, row, col);
7151 out_char(ScreenLines[off + 1]);
7152 ++screen_cur_col;
7153}
7154#endif
7155
7156#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7157/*
7158 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7159 * This uses the contents of ScreenLines[] and doesn't change it.
7160 */
7161 void
7162screen_draw_rectangle(row, col, height, width, invert)
7163 int row;
7164 int col;
7165 int height;
7166 int width;
7167 int invert;
7168{
7169 int r, c;
7170 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007171#ifdef FEAT_MBYTE
7172 int max_off;
7173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007175 /* Can't use ScreenLines unless initialized */
7176 if (ScreenLines == NULL)
7177 return;
7178
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 if (invert)
7180 screen_char_attr = HL_INVERSE;
7181 for (r = row; r < row + height; ++r)
7182 {
7183 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007184#ifdef FEAT_MBYTE
7185 max_off = off + screen_Columns;
7186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 for (c = col; c < col + width; ++c)
7188 {
7189#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007190 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
7192 screen_char_2(off + c, r, c);
7193 ++c;
7194 }
7195 else
7196#endif
7197 {
7198 screen_char(off + c, r, c);
7199#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007200 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 ++c;
7202#endif
7203 }
7204 }
7205 }
7206 screen_char_attr = 0;
7207}
7208#endif
7209
7210#ifdef FEAT_VERTSPLIT
7211/*
7212 * Redraw the characters for a vertically split window.
7213 */
7214 static void
7215redraw_block(row, end, wp)
7216 int row;
7217 int end;
7218 win_T *wp;
7219{
7220 int col;
7221 int width;
7222
7223# ifdef FEAT_CLIPBOARD
7224 clip_may_clear_selection(row, end - 1);
7225# endif
7226
7227 if (wp == NULL)
7228 {
7229 col = 0;
7230 width = Columns;
7231 }
7232 else
7233 {
7234 col = wp->w_wincol;
7235 width = wp->w_width;
7236 }
7237 screen_draw_rectangle(row, col, end - row, width, FALSE);
7238}
7239#endif
7240
7241/*
7242 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7243 * with character 'c1' in first column followed by 'c2' in the other columns.
7244 * Use attributes 'attr'.
7245 */
7246 void
7247screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7248 int start_row, end_row;
7249 int start_col, end_col;
7250 int c1, c2;
7251 int attr;
7252{
7253 int row;
7254 int col;
7255 int off;
7256 int end_off;
7257 int did_delete;
7258 int c;
7259 int norm_term;
7260#if defined(FEAT_GUI) || defined(UNIX)
7261 int force_next = FALSE;
7262#endif
7263
7264 if (end_row > screen_Rows) /* safety check */
7265 end_row = screen_Rows;
7266 if (end_col > screen_Columns) /* safety check */
7267 end_col = screen_Columns;
7268 if (ScreenLines == NULL
7269 || start_row >= end_row
7270 || start_col >= end_col) /* nothing to do */
7271 return;
7272
7273 /* it's a "normal" terminal when not in a GUI or cterm */
7274 norm_term = (
7275#ifdef FEAT_GUI
7276 !gui.in_use &&
7277#endif
7278 t_colors <= 1);
7279 for (row = start_row; row < end_row; ++row)
7280 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007281#ifdef FEAT_MBYTE
7282 if (has_mbyte
7283# ifdef FEAT_GUI
7284 && !gui.in_use
7285# endif
7286 )
7287 {
7288 /* When drawing over the right halve of a double-wide char clear
7289 * out the left halve. When drawing over the left halve of a
7290 * double wide-char clear out the right halve. Only needed in a
7291 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007292 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007293 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007294 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007295 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007296 }
7297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 /*
7299 * Try to use delete-line termcap code, when no attributes or in a
7300 * "normal" terminal, where a bold/italic space is just a
7301 * space.
7302 */
7303 did_delete = FALSE;
7304 if (c2 == ' '
7305 && end_col == Columns
7306 && can_clear(T_CE)
7307 && (attr == 0
7308 || (norm_term
7309 && attr <= HL_ALL
7310 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7311 {
7312 /*
7313 * check if we really need to clear something
7314 */
7315 col = start_col;
7316 if (c1 != ' ') /* don't clear first char */
7317 ++col;
7318
7319 off = LineOffset[row] + col;
7320 end_off = LineOffset[row] + end_col;
7321
7322 /* skip blanks (used often, keep it fast!) */
7323#ifdef FEAT_MBYTE
7324 if (enc_utf8)
7325 while (off < end_off && ScreenLines[off] == ' '
7326 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7327 ++off;
7328 else
7329#endif
7330 while (off < end_off && ScreenLines[off] == ' '
7331 && ScreenAttrs[off] == 0)
7332 ++off;
7333 if (off < end_off) /* something to be cleared */
7334 {
7335 col = off - LineOffset[row];
7336 screen_stop_highlight();
7337 term_windgoto(row, col);/* clear rest of this screen line */
7338 out_str(T_CE);
7339 screen_start(); /* don't know where cursor is now */
7340 col = end_col - col;
7341 while (col--) /* clear chars in ScreenLines */
7342 {
7343 ScreenLines[off] = ' ';
7344#ifdef FEAT_MBYTE
7345 if (enc_utf8)
7346 ScreenLinesUC[off] = 0;
7347#endif
7348 ScreenAttrs[off] = 0;
7349 ++off;
7350 }
7351 }
7352 did_delete = TRUE; /* the chars are cleared now */
7353 }
7354
7355 off = LineOffset[row] + start_col;
7356 c = c1;
7357 for (col = start_col; col < end_col; ++col)
7358 {
7359 if (ScreenLines[off] != c
7360#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007361 || (enc_utf8 && (int)ScreenLinesUC[off]
7362 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363#endif
7364 || ScreenAttrs[off] != attr
7365#if defined(FEAT_GUI) || defined(UNIX)
7366 || force_next
7367#endif
7368 )
7369 {
7370#if defined(FEAT_GUI) || defined(UNIX)
7371 /* The bold trick may make a single row of pixels appear in
7372 * the next character. When a bold character is removed, the
7373 * next character should be redrawn too. This happens for our
7374 * own GUI and for some xterms. */
7375 if (
7376# ifdef FEAT_GUI
7377 gui.in_use
7378# endif
7379# if defined(FEAT_GUI) && defined(UNIX)
7380 ||
7381# endif
7382# ifdef UNIX
7383 term_is_xterm
7384# endif
7385 )
7386 {
7387 if (ScreenLines[off] != ' '
7388 && (ScreenAttrs[off] > HL_ALL
7389 || ScreenAttrs[off] & HL_BOLD))
7390 force_next = TRUE;
7391 else
7392 force_next = FALSE;
7393 }
7394#endif
7395 ScreenLines[off] = c;
7396#ifdef FEAT_MBYTE
7397 if (enc_utf8)
7398 {
7399 if (c >= 0x80)
7400 {
7401 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007402 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 }
7404 else
7405 ScreenLinesUC[off] = 0;
7406 }
7407#endif
7408 ScreenAttrs[off] = attr;
7409 if (!did_delete || c != ' ')
7410 screen_char(off, row, col);
7411 }
7412 ++off;
7413 if (col == start_col)
7414 {
7415 if (did_delete)
7416 break;
7417 c = c2;
7418 }
7419 }
7420 if (end_col == Columns)
7421 LineWraps[row] = FALSE;
7422 if (row == Rows - 1) /* overwritten the command line */
7423 {
7424 redraw_cmdline = TRUE;
7425 if (c1 == ' ' && c2 == ' ')
7426 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007427 if (start_col == 0)
7428 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 }
7431}
7432
7433/*
7434 * Check if there should be a delay. Used before clearing or redrawing the
7435 * screen or the command line.
7436 */
7437 void
7438check_for_delay(check_msg_scroll)
7439 int check_msg_scroll;
7440{
7441 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7442 && !did_wait_return
7443 && emsg_silent == 0)
7444 {
7445 out_flush();
7446 ui_delay(1000L, TRUE);
7447 emsg_on_display = FALSE;
7448 if (check_msg_scroll)
7449 msg_scroll = FALSE;
7450 }
7451}
7452
7453/*
7454 * screen_valid - allocate screen buffers if size changed
7455 * If "clear" is TRUE: clear screen if it has been resized.
7456 * Returns TRUE if there is a valid screen to write to.
7457 * Returns FALSE when starting up and screen not initialized yet.
7458 */
7459 int
7460screen_valid(clear)
7461 int clear;
7462{
7463 screenalloc(clear); /* allocate screen buffers if size changed */
7464 return (ScreenLines != NULL);
7465}
7466
7467/*
7468 * Resize the shell to Rows and Columns.
7469 * Allocate ScreenLines[] and associated items.
7470 *
7471 * There may be some time between setting Rows and Columns and (re)allocating
7472 * ScreenLines[]. This happens when starting up and when (manually) changing
7473 * the shell size. Always use screen_Rows and screen_Columns to access items
7474 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7475 * final size of the shell is needed.
7476 */
7477 void
7478screenalloc(clear)
7479 int clear;
7480{
7481 int new_row, old_row;
7482#ifdef FEAT_GUI
7483 int old_Rows;
7484#endif
7485 win_T *wp;
7486 int outofmem = FALSE;
7487 int len;
7488 schar_T *new_ScreenLines;
7489#ifdef FEAT_MBYTE
7490 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007491 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007493 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494#endif
7495 sattr_T *new_ScreenAttrs;
7496 unsigned *new_LineOffset;
7497 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007498#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007499 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007500 tabpage_T *tp;
7501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007503 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007504#ifdef FEAT_AUTOCMD
7505 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007507retry:
7508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 /*
7510 * Allocation of the screen buffers is done only when the size changes and
7511 * when Rows and Columns have been set and we have started doing full
7512 * screen stuff.
7513 */
7514 if ((ScreenLines != NULL
7515 && Rows == screen_Rows
7516 && Columns == screen_Columns
7517#ifdef FEAT_MBYTE
7518 && enc_utf8 == (ScreenLinesUC != NULL)
7519 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007520 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521#endif
7522 )
7523 || Rows == 0
7524 || Columns == 0
7525 || (!full_screen && ScreenLines == NULL))
7526 return;
7527
7528 /*
7529 * It's possible that we produce an out-of-memory message below, which
7530 * will cause this function to be called again. To break the loop, just
7531 * return here.
7532 */
7533 if (entered)
7534 return;
7535 entered = TRUE;
7536
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007537 /*
7538 * Note that the window sizes are updated before reallocating the arrays,
7539 * thus we must not redraw here!
7540 */
7541 ++RedrawingDisabled;
7542
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 win_new_shellsize(); /* fit the windows in the new sized shell */
7544
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 comp_col(); /* recompute columns for shown command and ruler */
7546
7547 /*
7548 * We're changing the size of the screen.
7549 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7550 * - Move lines from the old arrays into the new arrays, clear extra
7551 * lines (unless the screen is going to be cleared).
7552 * - Free the old arrays.
7553 *
7554 * If anything fails, make ScreenLines NULL, so we don't do anything!
7555 * Continuing with the old ScreenLines may result in a crash, because the
7556 * size is wrong.
7557 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007558 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007560#ifdef FEAT_AUTOCMD
7561 if (aucmd_win != NULL)
7562 win_free_lsize(aucmd_win);
7563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564
7565 new_ScreenLines = (schar_T *)lalloc((long_u)(
7566 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7567#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007568 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 if (enc_utf8)
7570 {
7571 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7572 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007573 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007574 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7576 }
7577 if (enc_dbcs == DBCS_JPNU)
7578 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7579 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7580#endif
7581 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7582 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7583 new_LineOffset = (unsigned *)lalloc((long_u)(
7584 Rows * sizeof(unsigned)), FALSE);
7585 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007586#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007587 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007590 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {
7592 if (win_alloc_lines(wp) == FAIL)
7593 {
7594 outofmem = TRUE;
7595#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007596 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597#endif
7598 }
7599 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007600#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007601 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7602 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007603 outofmem = TRUE;
7604#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007605#ifdef FEAT_WINDOWS
7606give_up:
7607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007609#ifdef FEAT_MBYTE
7610 for (i = 0; i < p_mco; ++i)
7611 if (new_ScreenLinesC[i] == NULL)
7612 break;
7613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 if (new_ScreenLines == NULL
7615#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007616 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7618#endif
7619 || new_ScreenAttrs == NULL
7620 || new_LineOffset == NULL
7621 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007622#ifdef FEAT_WINDOWS
7623 || new_TabPageIdxs == NULL
7624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 || outofmem)
7626 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007627 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007628 {
7629 /* guess the size */
7630 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7631
7632 /* Remember we did this to avoid getting outofmem messages over
7633 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007634 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 vim_free(new_ScreenLines);
7637 new_ScreenLines = NULL;
7638#ifdef FEAT_MBYTE
7639 vim_free(new_ScreenLinesUC);
7640 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007641 for (i = 0; i < p_mco; ++i)
7642 {
7643 vim_free(new_ScreenLinesC[i]);
7644 new_ScreenLinesC[i] = NULL;
7645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 vim_free(new_ScreenLines2);
7647 new_ScreenLines2 = NULL;
7648#endif
7649 vim_free(new_ScreenAttrs);
7650 new_ScreenAttrs = NULL;
7651 vim_free(new_LineOffset);
7652 new_LineOffset = NULL;
7653 vim_free(new_LineWraps);
7654 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007655#ifdef FEAT_WINDOWS
7656 vim_free(new_TabPageIdxs);
7657 new_TabPageIdxs = NULL;
7658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 }
7660 else
7661 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007662 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007663
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 for (new_row = 0; new_row < Rows; ++new_row)
7665 {
7666 new_LineOffset[new_row] = new_row * Columns;
7667 new_LineWraps[new_row] = FALSE;
7668
7669 /*
7670 * If the screen is not going to be cleared, copy as much as
7671 * possible from the old screen to the new one and clear the rest
7672 * (used when resizing the window at the "--more--" prompt or when
7673 * executing an external command, for the GUI).
7674 */
7675 if (!clear)
7676 {
7677 (void)vim_memset(new_ScreenLines + new_row * Columns,
7678 ' ', (size_t)Columns * sizeof(schar_T));
7679#ifdef FEAT_MBYTE
7680 if (enc_utf8)
7681 {
7682 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7683 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007684 for (i = 0; i < p_mco; ++i)
7685 (void)vim_memset(new_ScreenLinesC[i]
7686 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 0, (size_t)Columns * sizeof(u8char_T));
7688 }
7689 if (enc_dbcs == DBCS_JPNU)
7690 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7691 0, (size_t)Columns * sizeof(schar_T));
7692#endif
7693 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7694 0, (size_t)Columns * sizeof(sattr_T));
7695 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007696 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {
7698 if (screen_Columns < Columns)
7699 len = screen_Columns;
7700 else
7701 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007702#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007703 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007704 * may be invalid now. Also when p_mco changes. */
7705 if (!(enc_utf8 && ScreenLinesUC == NULL)
7706 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007707#endif
7708 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7709 ScreenLines + LineOffset[old_row],
7710 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007712 if (enc_utf8 && ScreenLinesUC != NULL
7713 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
7715 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7716 ScreenLinesUC + LineOffset[old_row],
7717 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007718 for (i = 0; i < p_mco; ++i)
7719 mch_memmove(new_ScreenLinesC[i]
7720 + new_LineOffset[new_row],
7721 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 (size_t)len * sizeof(u8char_T));
7723 }
7724 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7725 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7726 ScreenLines2 + LineOffset[old_row],
7727 (size_t)len * sizeof(schar_T));
7728#endif
7729 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7730 ScreenAttrs + LineOffset[old_row],
7731 (size_t)len * sizeof(sattr_T));
7732 }
7733 }
7734 }
7735 /* Use the last line of the screen for the current line. */
7736 current_ScreenLine = new_ScreenLines + Rows * Columns;
7737 }
7738
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007739 free_screenlines();
7740
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 ScreenLines = new_ScreenLines;
7742#ifdef FEAT_MBYTE
7743 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007744 for (i = 0; i < p_mco; ++i)
7745 ScreenLinesC[i] = new_ScreenLinesC[i];
7746 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 ScreenLines2 = new_ScreenLines2;
7748#endif
7749 ScreenAttrs = new_ScreenAttrs;
7750 LineOffset = new_LineOffset;
7751 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007752#ifdef FEAT_WINDOWS
7753 TabPageIdxs = new_TabPageIdxs;
7754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
7756 /* It's important that screen_Rows and screen_Columns reflect the actual
7757 * size of ScreenLines[]. Set them before calling anything. */
7758#ifdef FEAT_GUI
7759 old_Rows = screen_Rows;
7760#endif
7761 screen_Rows = Rows;
7762 screen_Columns = Columns;
7763
7764 must_redraw = CLEAR; /* need to clear the screen later */
7765 if (clear)
7766 screenclear2();
7767
7768#ifdef FEAT_GUI
7769 else if (gui.in_use
7770 && !gui.starting
7771 && ScreenLines != NULL
7772 && old_Rows != Rows)
7773 {
7774 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7775 /*
7776 * Adjust the position of the cursor, for when executing an external
7777 * command.
7778 */
7779 if (msg_row >= Rows) /* Rows got smaller */
7780 msg_row = Rows - 1; /* put cursor at last row */
7781 else if (Rows > old_Rows) /* Rows got bigger */
7782 msg_row += Rows - old_Rows; /* put cursor in same place */
7783 if (msg_col >= Columns) /* Columns got smaller */
7784 msg_col = Columns - 1; /* put cursor at last column */
7785 }
7786#endif
7787
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007789 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007790
7791#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007792 /*
7793 * Do not apply autocommands more than 3 times to avoid an endless loop
7794 * in case applying autocommands always changes Rows or Columns.
7795 */
7796 if (starting == 0 && ++retry_count <= 3)
7797 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007798 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007799 /* In rare cases, autocommands may have altered Rows or Columns,
7800 * jump back to check if we need to allocate the screen again. */
7801 goto retry;
7802 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804}
7805
7806 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007807free_screenlines()
7808{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007809#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007810 int i;
7811
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007812 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007813 for (i = 0; i < Screen_mco; ++i)
7814 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007815 vim_free(ScreenLines2);
7816#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007817 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007818 vim_free(ScreenAttrs);
7819 vim_free(LineOffset);
7820 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007821#ifdef FEAT_WINDOWS
7822 vim_free(TabPageIdxs);
7823#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007824}
7825
7826 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827screenclear()
7828{
7829 check_for_delay(FALSE);
7830 screenalloc(FALSE); /* allocate screen buffers if size changed */
7831 screenclear2(); /* clear the screen */
7832}
7833
7834 static void
7835screenclear2()
7836{
7837 int i;
7838
7839 if (starting == NO_SCREEN || ScreenLines == NULL
7840#ifdef FEAT_GUI
7841 || (gui.in_use && gui.starting)
7842#endif
7843 )
7844 return;
7845
7846#ifdef FEAT_GUI
7847 if (!gui.in_use)
7848#endif
7849 screen_attr = -1; /* force setting the Normal colors */
7850 screen_stop_highlight(); /* don't want highlighting here */
7851
7852#ifdef FEAT_CLIPBOARD
7853 /* disable selection without redrawing it */
7854 clip_scroll_selection(9999);
7855#endif
7856
7857 /* blank out ScreenLines */
7858 for (i = 0; i < Rows; ++i)
7859 {
7860 lineclear(LineOffset[i], (int)Columns);
7861 LineWraps[i] = FALSE;
7862 }
7863
7864 if (can_clear(T_CL))
7865 {
7866 out_str(T_CL); /* clear the display */
7867 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007868 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870 else
7871 {
7872 /* can't clear the screen, mark all chars with invalid attributes */
7873 for (i = 0; i < Rows; ++i)
7874 lineinvalid(LineOffset[i], (int)Columns);
7875 clear_cmdline = TRUE;
7876 }
7877
7878 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7879
7880 win_rest_invalid(firstwin);
7881 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007882#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007883 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (must_redraw == CLEAR) /* no need to clear again */
7886 must_redraw = NOT_VALID;
7887 compute_cmdrow();
7888 msg_row = cmdline_row; /* put cursor on last line for messages */
7889 msg_col = 0;
7890 screen_start(); /* don't know where cursor is now */
7891 msg_scrolled = 0; /* can't scroll back */
7892 msg_didany = FALSE;
7893 msg_didout = FALSE;
7894}
7895
7896/*
7897 * Clear one line in ScreenLines.
7898 */
7899 static void
7900lineclear(off, width)
7901 unsigned off;
7902 int width;
7903{
7904 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7905#ifdef FEAT_MBYTE
7906 if (enc_utf8)
7907 (void)vim_memset(ScreenLinesUC + off, 0,
7908 (size_t)width * sizeof(u8char_T));
7909#endif
7910 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7911}
7912
7913/*
7914 * Mark one line in ScreenLines invalid by setting the attributes to an
7915 * invalid value.
7916 */
7917 static void
7918lineinvalid(off, width)
7919 unsigned off;
7920 int width;
7921{
7922 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7923}
7924
7925#ifdef FEAT_VERTSPLIT
7926/*
7927 * Copy part of a Screenline for vertically split window "wp".
7928 */
7929 static void
7930linecopy(to, from, wp)
7931 int to;
7932 int from;
7933 win_T *wp;
7934{
7935 unsigned off_to = LineOffset[to] + wp->w_wincol;
7936 unsigned off_from = LineOffset[from] + wp->w_wincol;
7937
7938 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7939 wp->w_width * sizeof(schar_T));
7940# ifdef FEAT_MBYTE
7941 if (enc_utf8)
7942 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007943 int i;
7944
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7946 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007947 for (i = 0; i < p_mco; ++i)
7948 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7949 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 }
7951 if (enc_dbcs == DBCS_JPNU)
7952 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7953 wp->w_width * sizeof(schar_T));
7954# endif
7955 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7956 wp->w_width * sizeof(sattr_T));
7957}
7958#endif
7959
7960/*
7961 * Return TRUE if clearing with term string "p" would work.
7962 * It can't work when the string is empty or it won't set the right background.
7963 */
7964 int
7965can_clear(p)
7966 char_u *p;
7967{
7968 return (*p != NUL && (t_colors <= 1
7969#ifdef FEAT_GUI
7970 || gui.in_use
7971#endif
7972 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7973}
7974
7975/*
7976 * Reset cursor position. Use whenever cursor was moved because of outputting
7977 * something directly to the screen (shell commands) or a terminal control
7978 * code.
7979 */
7980 void
7981screen_start()
7982{
7983 screen_cur_row = screen_cur_col = 9999;
7984}
7985
7986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 * Move the cursor to position "row","col" in the screen.
7988 * This tries to find the most efficient way to move, minimizing the number of
7989 * characters sent to the terminal.
7990 */
7991 void
7992windgoto(row, col)
7993 int row;
7994 int col;
7995{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007996 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 int i;
7998 int plan;
7999 int cost;
8000 int wouldbe_col;
8001 int noinvcurs;
8002 char_u *bs;
8003 int goto_cost;
8004 int attr;
8005
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008006#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8008
8009#define PLAN_LE 1
8010#define PLAN_CR 2
8011#define PLAN_NL 3
8012#define PLAN_WRITE 4
8013 /* Can't use ScreenLines unless initialized */
8014 if (ScreenLines == NULL)
8015 return;
8016
8017 if (col != screen_cur_col || row != screen_cur_row)
8018 {
8019 /* Check for valid position. */
8020 if (row < 0) /* window without text lines? */
8021 row = 0;
8022 if (row >= screen_Rows)
8023 row = screen_Rows - 1;
8024 if (col >= screen_Columns)
8025 col = screen_Columns - 1;
8026
8027 /* check if no cursor movement is allowed in highlight mode */
8028 if (screen_attr && *T_MS == NUL)
8029 noinvcurs = HIGHL_COST;
8030 else
8031 noinvcurs = 0;
8032 goto_cost = GOTO_COST + noinvcurs;
8033
8034 /*
8035 * Plan how to do the positioning:
8036 * 1. Use CR to move it to column 0, same row.
8037 * 2. Use T_LE to move it a few columns to the left.
8038 * 3. Use NL to move a few lines down, column 0.
8039 * 4. Move a few columns to the right with T_ND or by writing chars.
8040 *
8041 * Don't do this if the cursor went beyond the last column, the cursor
8042 * position is unknown then (some terminals wrap, some don't )
8043 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008044 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 * characters to move the cursor to the right.
8046 */
8047 if (row >= screen_cur_row && screen_cur_col < Columns)
8048 {
8049 /*
8050 * If the cursor is in the same row, bigger col, we can use CR
8051 * or T_LE.
8052 */
8053 bs = NULL; /* init for GCC */
8054 attr = screen_attr;
8055 if (row == screen_cur_row && col < screen_cur_col)
8056 {
8057 /* "le" is preferred over "bc", because "bc" is obsolete */
8058 if (*T_LE)
8059 bs = T_LE; /* "cursor left" */
8060 else
8061 bs = T_BC; /* "backspace character (old) */
8062 if (*bs)
8063 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8064 else
8065 cost = 999;
8066 if (col + 1 < cost) /* using CR is less characters */
8067 {
8068 plan = PLAN_CR;
8069 wouldbe_col = 0;
8070 cost = 1; /* CR is just one character */
8071 }
8072 else
8073 {
8074 plan = PLAN_LE;
8075 wouldbe_col = col;
8076 }
8077 if (noinvcurs) /* will stop highlighting */
8078 {
8079 cost += noinvcurs;
8080 attr = 0;
8081 }
8082 }
8083
8084 /*
8085 * If the cursor is above where we want to be, we can use CR LF.
8086 */
8087 else if (row > screen_cur_row)
8088 {
8089 plan = PLAN_NL;
8090 wouldbe_col = 0;
8091 cost = (row - screen_cur_row) * 2; /* CR LF */
8092 if (noinvcurs) /* will stop highlighting */
8093 {
8094 cost += noinvcurs;
8095 attr = 0;
8096 }
8097 }
8098
8099 /*
8100 * If the cursor is in the same row, smaller col, just use write.
8101 */
8102 else
8103 {
8104 plan = PLAN_WRITE;
8105 wouldbe_col = screen_cur_col;
8106 cost = 0;
8107 }
8108
8109 /*
8110 * Check if any characters that need to be written have the
8111 * correct attributes. Also avoid UTF-8 characters.
8112 */
8113 i = col - wouldbe_col;
8114 if (i > 0)
8115 cost += i;
8116 if (cost < goto_cost && i > 0)
8117 {
8118 /*
8119 * Check if the attributes are correct without additionally
8120 * stopping highlighting.
8121 */
8122 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8123 while (i && *p++ == attr)
8124 --i;
8125 if (i != 0)
8126 {
8127 /*
8128 * Try if it works when highlighting is stopped here.
8129 */
8130 if (*--p == 0)
8131 {
8132 cost += noinvcurs;
8133 while (i && *p++ == 0)
8134 --i;
8135 }
8136 if (i != 0)
8137 cost = 999; /* different attributes, don't do it */
8138 }
8139#ifdef FEAT_MBYTE
8140 if (enc_utf8)
8141 {
8142 /* Don't use an UTF-8 char for positioning, it's slow. */
8143 for (i = wouldbe_col; i < col; ++i)
8144 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8145 {
8146 cost = 999;
8147 break;
8148 }
8149 }
8150#endif
8151 }
8152
8153 /*
8154 * We can do it without term_windgoto()!
8155 */
8156 if (cost < goto_cost)
8157 {
8158 if (plan == PLAN_LE)
8159 {
8160 if (noinvcurs)
8161 screen_stop_highlight();
8162 while (screen_cur_col > col)
8163 {
8164 out_str(bs);
8165 --screen_cur_col;
8166 }
8167 }
8168 else if (plan == PLAN_CR)
8169 {
8170 if (noinvcurs)
8171 screen_stop_highlight();
8172 out_char('\r');
8173 screen_cur_col = 0;
8174 }
8175 else if (plan == PLAN_NL)
8176 {
8177 if (noinvcurs)
8178 screen_stop_highlight();
8179 while (screen_cur_row < row)
8180 {
8181 out_char('\n');
8182 ++screen_cur_row;
8183 }
8184 screen_cur_col = 0;
8185 }
8186
8187 i = col - screen_cur_col;
8188 if (i > 0)
8189 {
8190 /*
8191 * Use cursor-right if it's one character only. Avoids
8192 * removing a line of pixels from the last bold char, when
8193 * using the bold trick in the GUI.
8194 */
8195 if (T_ND[0] != NUL && T_ND[1] == NUL)
8196 {
8197 while (i-- > 0)
8198 out_char(*T_ND);
8199 }
8200 else
8201 {
8202 int off;
8203
8204 off = LineOffset[row] + screen_cur_col;
8205 while (i-- > 0)
8206 {
8207 if (ScreenAttrs[off] != screen_attr)
8208 screen_stop_highlight();
8209#ifdef FEAT_MBYTE
8210 out_flush_check();
8211#endif
8212 out_char(ScreenLines[off]);
8213#ifdef FEAT_MBYTE
8214 if (enc_dbcs == DBCS_JPNU
8215 && ScreenLines[off] == 0x8e)
8216 out_char(ScreenLines2[off]);
8217#endif
8218 ++off;
8219 }
8220 }
8221 }
8222 }
8223 }
8224 else
8225 cost = 999;
8226
8227 if (cost >= goto_cost)
8228 {
8229 if (noinvcurs)
8230 screen_stop_highlight();
8231 if (row == screen_cur_row && (col > screen_cur_col) &&
8232 *T_CRI != NUL)
8233 term_cursor_right(col - screen_cur_col);
8234 else
8235 term_windgoto(row, col);
8236 }
8237 screen_cur_row = row;
8238 screen_cur_col = col;
8239 }
8240}
8241
8242/*
8243 * Set cursor to its position in the current window.
8244 */
8245 void
8246setcursor()
8247{
8248 if (redrawing())
8249 {
8250 validate_cursor();
8251 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8252 W_WINCOL(curwin) + (
8253#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008254 /* With 'rightleft' set and the cursor on a double-wide
8255 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8257# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008258 (has_mbyte
8259 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8260 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261# endif
8262 1)) :
8263#endif
8264 curwin->w_wcol));
8265 }
8266}
8267
8268
8269/*
8270 * insert 'line_count' lines at 'row' in window 'wp'
8271 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8272 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8273 * scrolling.
8274 * Returns FAIL if the lines are not inserted, OK for success.
8275 */
8276 int
8277win_ins_lines(wp, row, line_count, invalid, mayclear)
8278 win_T *wp;
8279 int row;
8280 int line_count;
8281 int invalid;
8282 int mayclear;
8283{
8284 int did_delete;
8285 int nextrow;
8286 int lastrow;
8287 int retval;
8288
8289 if (invalid)
8290 wp->w_lines_valid = 0;
8291
8292 if (wp->w_height < 5)
8293 return FAIL;
8294
8295 if (line_count > wp->w_height - row)
8296 line_count = wp->w_height - row;
8297
8298 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8299 if (retval != MAYBE)
8300 return retval;
8301
8302 /*
8303 * If there is a next window or a status line, we first try to delete the
8304 * lines at the bottom to avoid messing what is after the window.
8305 * If this fails and there are following windows, don't do anything to avoid
8306 * messing up those windows, better just redraw.
8307 */
8308 did_delete = FALSE;
8309#ifdef FEAT_WINDOWS
8310 if (wp->w_next != NULL || wp->w_status_height)
8311 {
8312 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8313 line_count, (int)Rows, FALSE, NULL) == OK)
8314 did_delete = TRUE;
8315 else if (wp->w_next)
8316 return FAIL;
8317 }
8318#endif
8319 /*
8320 * if no lines deleted, blank the lines that will end up below the window
8321 */
8322 if (!did_delete)
8323 {
8324#ifdef FEAT_WINDOWS
8325 wp->w_redr_status = TRUE;
8326#endif
8327 redraw_cmdline = TRUE;
8328 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8329 lastrow = nextrow + line_count;
8330 if (lastrow > Rows)
8331 lastrow = Rows;
8332 screen_fill(nextrow - line_count, lastrow - line_count,
8333 W_WINCOL(wp), (int)W_ENDCOL(wp),
8334 ' ', ' ', 0);
8335 }
8336
8337 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8338 == FAIL)
8339 {
8340 /* deletion will have messed up other windows */
8341 if (did_delete)
8342 {
8343#ifdef FEAT_WINDOWS
8344 wp->w_redr_status = TRUE;
8345#endif
8346 win_rest_invalid(W_NEXT(wp));
8347 }
8348 return FAIL;
8349 }
8350
8351 return OK;
8352}
8353
8354/*
8355 * delete "line_count" window lines at "row" in window "wp"
8356 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8357 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8358 * scrolling
8359 * Return OK for success, FAIL if the lines are not deleted.
8360 */
8361 int
8362win_del_lines(wp, row, line_count, invalid, mayclear)
8363 win_T *wp;
8364 int row;
8365 int line_count;
8366 int invalid;
8367 int mayclear;
8368{
8369 int retval;
8370
8371 if (invalid)
8372 wp->w_lines_valid = 0;
8373
8374 if (line_count > wp->w_height - row)
8375 line_count = wp->w_height - row;
8376
8377 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8378 if (retval != MAYBE)
8379 return retval;
8380
8381 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8382 (int)Rows, FALSE, NULL) == FAIL)
8383 return FAIL;
8384
8385#ifdef FEAT_WINDOWS
8386 /*
8387 * If there are windows or status lines below, try to put them at the
8388 * correct place. If we can't do that, they have to be redrawn.
8389 */
8390 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8391 {
8392 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8393 line_count, (int)Rows, NULL) == FAIL)
8394 {
8395 wp->w_redr_status = TRUE;
8396 win_rest_invalid(wp->w_next);
8397 }
8398 }
8399 /*
8400 * If this is the last window and there is no status line, redraw the
8401 * command line later.
8402 */
8403 else
8404#endif
8405 redraw_cmdline = TRUE;
8406 return OK;
8407}
8408
8409/*
8410 * Common code for win_ins_lines() and win_del_lines().
8411 * Returns OK or FAIL when the work has been done.
8412 * Returns MAYBE when not finished yet.
8413 */
8414 static int
8415win_do_lines(wp, row, line_count, mayclear, del)
8416 win_T *wp;
8417 int row;
8418 int line_count;
8419 int mayclear;
8420 int del;
8421{
8422 int retval;
8423
8424 if (!redrawing() || line_count <= 0)
8425 return FAIL;
8426
8427 /* only a few lines left: redraw is faster */
8428 if (mayclear && Rows - line_count < 5
8429#ifdef FEAT_VERTSPLIT
8430 && wp->w_width == Columns
8431#endif
8432 )
8433 {
8434 screenclear(); /* will set wp->w_lines_valid to 0 */
8435 return FAIL;
8436 }
8437
8438 /*
8439 * Delete all remaining lines
8440 */
8441 if (row + line_count >= wp->w_height)
8442 {
8443 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8444 W_WINCOL(wp), (int)W_ENDCOL(wp),
8445 ' ', ' ', 0);
8446 return OK;
8447 }
8448
8449 /*
8450 * when scrolling, the message on the command line should be cleared,
8451 * otherwise it will stay there forever.
8452 */
8453 clear_cmdline = TRUE;
8454
8455 /*
8456 * If the terminal can set a scroll region, use that.
8457 * Always do this in a vertically split window. This will redraw from
8458 * ScreenLines[] when t_CV isn't defined. That's faster than using
8459 * win_line().
8460 * Don't use a scroll region when we are going to redraw the text, writing
8461 * a character in the lower right corner of the scroll region causes a
8462 * scroll-up in the DJGPP version.
8463 */
8464 if (scroll_region
8465#ifdef FEAT_VERTSPLIT
8466 || W_WIDTH(wp) != Columns
8467#endif
8468 )
8469 {
8470#ifdef FEAT_VERTSPLIT
8471 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8472#endif
8473 scroll_region_set(wp, row);
8474 if (del)
8475 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8476 wp->w_height - row, FALSE, wp);
8477 else
8478 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8479 wp->w_height - row, wp);
8480#ifdef FEAT_VERTSPLIT
8481 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8482#endif
8483 scroll_region_reset();
8484 return retval;
8485 }
8486
8487#ifdef FEAT_WINDOWS
8488 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8489 return FAIL;
8490#endif
8491
8492 return MAYBE;
8493}
8494
8495/*
8496 * window 'wp' and everything after it is messed up, mark it for redraw
8497 */
8498 static void
8499win_rest_invalid(wp)
8500 win_T *wp;
8501{
8502#ifdef FEAT_WINDOWS
8503 while (wp != NULL)
8504#else
8505 if (wp != NULL)
8506#endif
8507 {
8508 redraw_win_later(wp, NOT_VALID);
8509#ifdef FEAT_WINDOWS
8510 wp->w_redr_status = TRUE;
8511 wp = wp->w_next;
8512#endif
8513 }
8514 redraw_cmdline = TRUE;
8515}
8516
8517/*
8518 * The rest of the routines in this file perform screen manipulations. The
8519 * given operation is performed physically on the screen. The corresponding
8520 * change is also made to the internal screen image. In this way, the editor
8521 * anticipates the effect of editing changes on the appearance of the screen.
8522 * That way, when we call screenupdate a complete redraw isn't usually
8523 * necessary. Another advantage is that we can keep adding code to anticipate
8524 * screen changes, and in the meantime, everything still works.
8525 */
8526
8527/*
8528 * types for inserting or deleting lines
8529 */
8530#define USE_T_CAL 1
8531#define USE_T_CDL 2
8532#define USE_T_AL 3
8533#define USE_T_CE 4
8534#define USE_T_DL 5
8535#define USE_T_SR 6
8536#define USE_NL 7
8537#define USE_T_CD 8
8538#define USE_REDRAW 9
8539
8540/*
8541 * insert lines on the screen and update ScreenLines[]
8542 * 'end' is the line after the scrolled part. Normally it is Rows.
8543 * When scrolling region used 'off' is the offset from the top for the region.
8544 * 'row' and 'end' are relative to the start of the region.
8545 *
8546 * return FAIL for failure, OK for success.
8547 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008548 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549screen_ins_lines(off, row, line_count, end, wp)
8550 int off;
8551 int row;
8552 int line_count;
8553 int end;
8554 win_T *wp; /* NULL or window to use width from */
8555{
8556 int i;
8557 int j;
8558 unsigned temp;
8559 int cursor_row;
8560 int type;
8561 int result_empty;
8562 int can_ce = can_clear(T_CE);
8563
8564 /*
8565 * FAIL if
8566 * - there is no valid screen
8567 * - the screen has to be redrawn completely
8568 * - the line count is less than one
8569 * - the line count is more than 'ttyscroll'
8570 */
8571 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8572 return FAIL;
8573
8574 /*
8575 * There are seven ways to insert lines:
8576 * 0. When in a vertically split window and t_CV isn't set, redraw the
8577 * characters from ScreenLines[].
8578 * 1. Use T_CD (clear to end of display) if it exists and the result of
8579 * the insert is just empty lines
8580 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8581 * present or line_count > 1. It looks better if we do all the inserts
8582 * at once.
8583 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8584 * insert is just empty lines and T_CE is not present or line_count >
8585 * 1.
8586 * 4. Use T_AL (insert line) if it exists.
8587 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8588 * just empty lines.
8589 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8590 * just empty lines.
8591 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8592 * the 'da' flag is not set or we have clear line capability.
8593 * 8. redraw the characters from ScreenLines[].
8594 *
8595 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8596 * the scrollbar for the window. It does have insert line, use that if it
8597 * exists.
8598 */
8599 result_empty = (row + line_count >= end);
8600#ifdef FEAT_VERTSPLIT
8601 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8602 type = USE_REDRAW;
8603 else
8604#endif
8605 if (can_clear(T_CD) && result_empty)
8606 type = USE_T_CD;
8607 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8608 type = USE_T_CAL;
8609 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8610 type = USE_T_CDL;
8611 else if (*T_AL != NUL)
8612 type = USE_T_AL;
8613 else if (can_ce && result_empty)
8614 type = USE_T_CE;
8615 else if (*T_DL != NUL && result_empty)
8616 type = USE_T_DL;
8617 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8618 type = USE_T_SR;
8619 else
8620 return FAIL;
8621
8622 /*
8623 * For clearing the lines screen_del_lines() is used. This will also take
8624 * care of t_db if necessary.
8625 */
8626 if (type == USE_T_CD || type == USE_T_CDL ||
8627 type == USE_T_CE || type == USE_T_DL)
8628 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8629
8630 /*
8631 * If text is retained below the screen, first clear or delete as many
8632 * lines at the bottom of the window as are about to be inserted so that
8633 * the deleted lines won't later surface during a screen_del_lines.
8634 */
8635 if (*T_DB)
8636 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8637
8638#ifdef FEAT_CLIPBOARD
8639 /* Remove a modeless selection when inserting lines halfway the screen
8640 * or not the full width of the screen. */
8641 if (off + row > 0
8642# ifdef FEAT_VERTSPLIT
8643 || (wp != NULL && wp->w_width != Columns)
8644# endif
8645 )
8646 clip_clear_selection();
8647 else
8648 clip_scroll_selection(-line_count);
8649#endif
8650
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651#ifdef FEAT_GUI
8652 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8653 * scrolling is actually carried out. */
8654 gui_dont_update_cursor();
8655#endif
8656
8657 if (*T_CCS != NUL) /* cursor relative to region */
8658 cursor_row = row;
8659 else
8660 cursor_row = row + off;
8661
8662 /*
8663 * Shift LineOffset[] line_count down to reflect the inserted lines.
8664 * Clear the inserted lines in ScreenLines[].
8665 */
8666 row += off;
8667 end += off;
8668 for (i = 0; i < line_count; ++i)
8669 {
8670#ifdef FEAT_VERTSPLIT
8671 if (wp != NULL && wp->w_width != Columns)
8672 {
8673 /* need to copy part of a line */
8674 j = end - 1 - i;
8675 while ((j -= line_count) >= row)
8676 linecopy(j + line_count, j, wp);
8677 j += line_count;
8678 if (can_clear((char_u *)" "))
8679 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8680 else
8681 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8682 LineWraps[j] = FALSE;
8683 }
8684 else
8685#endif
8686 {
8687 j = end - 1 - i;
8688 temp = LineOffset[j];
8689 while ((j -= line_count) >= row)
8690 {
8691 LineOffset[j + line_count] = LineOffset[j];
8692 LineWraps[j + line_count] = LineWraps[j];
8693 }
8694 LineOffset[j + line_count] = temp;
8695 LineWraps[j + line_count] = FALSE;
8696 if (can_clear((char_u *)" "))
8697 lineclear(temp, (int)Columns);
8698 else
8699 lineinvalid(temp, (int)Columns);
8700 }
8701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702
8703 screen_stop_highlight();
8704 windgoto(cursor_row, 0);
8705
8706#ifdef FEAT_VERTSPLIT
8707 /* redraw the characters */
8708 if (type == USE_REDRAW)
8709 redraw_block(row, end, wp);
8710 else
8711#endif
8712 if (type == USE_T_CAL)
8713 {
8714 term_append_lines(line_count);
8715 screen_start(); /* don't know where cursor is now */
8716 }
8717 else
8718 {
8719 for (i = 0; i < line_count; i++)
8720 {
8721 if (type == USE_T_AL)
8722 {
8723 if (i && cursor_row != 0)
8724 windgoto(cursor_row, 0);
8725 out_str(T_AL);
8726 }
8727 else /* type == USE_T_SR */
8728 out_str(T_SR);
8729 screen_start(); /* don't know where cursor is now */
8730 }
8731 }
8732
8733 /*
8734 * With scroll-reverse and 'da' flag set we need to clear the lines that
8735 * have been scrolled down into the region.
8736 */
8737 if (type == USE_T_SR && *T_DA)
8738 {
8739 for (i = 0; i < line_count; ++i)
8740 {
8741 windgoto(off + i, 0);
8742 out_str(T_CE);
8743 screen_start(); /* don't know where cursor is now */
8744 }
8745 }
8746
8747#ifdef FEAT_GUI
8748 gui_can_update_cursor();
8749 if (gui.in_use)
8750 out_flush(); /* always flush after a scroll */
8751#endif
8752 return OK;
8753}
8754
8755/*
8756 * delete lines on the screen and update ScreenLines[]
8757 * 'end' is the line after the scrolled part. Normally it is Rows.
8758 * When scrolling region used 'off' is the offset from the top for the region.
8759 * 'row' and 'end' are relative to the start of the region.
8760 *
8761 * Return OK for success, FAIL if the lines are not deleted.
8762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 int
8764screen_del_lines(off, row, line_count, end, force, wp)
8765 int off;
8766 int row;
8767 int line_count;
8768 int end;
8769 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00008770 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771{
8772 int j;
8773 int i;
8774 unsigned temp;
8775 int cursor_row;
8776 int cursor_end;
8777 int result_empty; /* result is empty until end of region */
8778 int can_delete; /* deleting line codes can be used */
8779 int type;
8780
8781 /*
8782 * FAIL if
8783 * - there is no valid screen
8784 * - the screen has to be redrawn completely
8785 * - the line count is less than one
8786 * - the line count is more than 'ttyscroll'
8787 */
8788 if (!screen_valid(TRUE) || line_count <= 0 ||
8789 (!force && line_count > p_ttyscroll))
8790 return FAIL;
8791
8792 /*
8793 * Check if the rest of the current region will become empty.
8794 */
8795 result_empty = row + line_count >= end;
8796
8797 /*
8798 * We can delete lines only when 'db' flag not set or when 'ce' option
8799 * available.
8800 */
8801 can_delete = (*T_DB == NUL || can_clear(T_CE));
8802
8803 /*
8804 * There are six ways to delete lines:
8805 * 0. When in a vertically split window and t_CV isn't set, redraw the
8806 * characters from ScreenLines[].
8807 * 1. Use T_CD if it exists and the result is empty.
8808 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8809 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8810 * none of the other ways work.
8811 * 4. Use T_CE (erase line) if the result is empty.
8812 * 5. Use T_DL (delete line) if it exists.
8813 * 6. redraw the characters from ScreenLines[].
8814 */
8815#ifdef FEAT_VERTSPLIT
8816 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8817 type = USE_REDRAW;
8818 else
8819#endif
8820 if (can_clear(T_CD) && result_empty)
8821 type = USE_T_CD;
8822#if defined(__BEOS__) && defined(BEOS_DR8)
8823 /*
8824 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8825 * its internal termcap... this works okay for tests which test *T_DB !=
8826 * NUL. It has the disadvantage that the user cannot use any :set t_*
8827 * command to get T_DB (back) to empty_option, only :set term=... will do
8828 * the trick...
8829 * Anyway, this hack will hopefully go away with the next OS release.
8830 * (Olaf Seibert)
8831 */
8832 else if (row == 0 && T_DB == empty_option
8833 && (line_count == 1 || *T_CDL == NUL))
8834#else
8835 else if (row == 0 && (
8836#ifndef AMIGA
8837 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8838 * up, so use delete-line command */
8839 line_count == 1 ||
8840#endif
8841 *T_CDL == NUL))
8842#endif
8843 type = USE_NL;
8844 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8845 type = USE_T_CDL;
8846 else if (can_clear(T_CE) && result_empty
8847#ifdef FEAT_VERTSPLIT
8848 && (wp == NULL || wp->w_width == Columns)
8849#endif
8850 )
8851 type = USE_T_CE;
8852 else if (*T_DL != NUL && can_delete)
8853 type = USE_T_DL;
8854 else if (*T_CDL != NUL && can_delete)
8855 type = USE_T_CDL;
8856 else
8857 return FAIL;
8858
8859#ifdef FEAT_CLIPBOARD
8860 /* Remove a modeless selection when deleting lines halfway the screen or
8861 * not the full width of the screen. */
8862 if (off + row > 0
8863# ifdef FEAT_VERTSPLIT
8864 || (wp != NULL && wp->w_width != Columns)
8865# endif
8866 )
8867 clip_clear_selection();
8868 else
8869 clip_scroll_selection(line_count);
8870#endif
8871
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872#ifdef FEAT_GUI
8873 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8874 * scrolling is actually carried out. */
8875 gui_dont_update_cursor();
8876#endif
8877
8878 if (*T_CCS != NUL) /* cursor relative to region */
8879 {
8880 cursor_row = row;
8881 cursor_end = end;
8882 }
8883 else
8884 {
8885 cursor_row = row + off;
8886 cursor_end = end + off;
8887 }
8888
8889 /*
8890 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8891 * Clear the inserted lines in ScreenLines[].
8892 */
8893 row += off;
8894 end += off;
8895 for (i = 0; i < line_count; ++i)
8896 {
8897#ifdef FEAT_VERTSPLIT
8898 if (wp != NULL && wp->w_width != Columns)
8899 {
8900 /* need to copy part of a line */
8901 j = row + i;
8902 while ((j += line_count) <= end - 1)
8903 linecopy(j - line_count, j, wp);
8904 j -= line_count;
8905 if (can_clear((char_u *)" "))
8906 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8907 else
8908 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8909 LineWraps[j] = FALSE;
8910 }
8911 else
8912#endif
8913 {
8914 /* whole width, moving the line pointers is faster */
8915 j = row + i;
8916 temp = LineOffset[j];
8917 while ((j += line_count) <= end - 1)
8918 {
8919 LineOffset[j - line_count] = LineOffset[j];
8920 LineWraps[j - line_count] = LineWraps[j];
8921 }
8922 LineOffset[j - line_count] = temp;
8923 LineWraps[j - line_count] = FALSE;
8924 if (can_clear((char_u *)" "))
8925 lineclear(temp, (int)Columns);
8926 else
8927 lineinvalid(temp, (int)Columns);
8928 }
8929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930
8931 screen_stop_highlight();
8932
8933#ifdef FEAT_VERTSPLIT
8934 /* redraw the characters */
8935 if (type == USE_REDRAW)
8936 redraw_block(row, end, wp);
8937 else
8938#endif
8939 if (type == USE_T_CD) /* delete the lines */
8940 {
8941 windgoto(cursor_row, 0);
8942 out_str(T_CD);
8943 screen_start(); /* don't know where cursor is now */
8944 }
8945 else if (type == USE_T_CDL)
8946 {
8947 windgoto(cursor_row, 0);
8948 term_delete_lines(line_count);
8949 screen_start(); /* don't know where cursor is now */
8950 }
8951 /*
8952 * Deleting lines at top of the screen or scroll region: Just scroll
8953 * the whole screen (scroll region) up by outputting newlines on the
8954 * last line.
8955 */
8956 else if (type == USE_NL)
8957 {
8958 windgoto(cursor_end - 1, 0);
8959 for (i = line_count; --i >= 0; )
8960 out_char('\n'); /* cursor will remain on same line */
8961 }
8962 else
8963 {
8964 for (i = line_count; --i >= 0; )
8965 {
8966 if (type == USE_T_DL)
8967 {
8968 windgoto(cursor_row, 0);
8969 out_str(T_DL); /* delete a line */
8970 }
8971 else /* type == USE_T_CE */
8972 {
8973 windgoto(cursor_row + i, 0);
8974 out_str(T_CE); /* erase a line */
8975 }
8976 screen_start(); /* don't know where cursor is now */
8977 }
8978 }
8979
8980 /*
8981 * If the 'db' flag is set, we need to clear the lines that have been
8982 * scrolled up at the bottom of the region.
8983 */
8984 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8985 {
8986 for (i = line_count; i > 0; --i)
8987 {
8988 windgoto(cursor_end - i, 0);
8989 out_str(T_CE); /* erase a line */
8990 screen_start(); /* don't know where cursor is now */
8991 }
8992 }
8993
8994#ifdef FEAT_GUI
8995 gui_can_update_cursor();
8996 if (gui.in_use)
8997 out_flush(); /* always flush after a scroll */
8998#endif
8999
9000 return OK;
9001}
9002
9003/*
9004 * show the current mode and ruler
9005 *
9006 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9007 * If clear_cmdline is FALSE there may be a message there that needs to be
9008 * cleared only if a mode is shown.
9009 * Return the length of the message (0 if no message).
9010 */
9011 int
9012showmode()
9013{
9014 int need_clear;
9015 int length = 0;
9016 int do_mode;
9017 int attr;
9018 int nwr_save;
9019#ifdef FEAT_INS_EXPAND
9020 int sub_attr;
9021#endif
9022
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009023 do_mode = ((p_smd && msg_silent == 0)
9024 && ((State & INSERT)
9025 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026#ifdef FEAT_VISUAL
9027 || VIsual_active
9028#endif
9029 ));
9030 if (do_mode || Recording)
9031 {
9032 /*
9033 * Don't show mode right now, when not redrawing or inside a mapping.
9034 * Call char_avail() only when we are going to show something, because
9035 * it takes a bit of time.
9036 */
9037 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9038 {
9039 redraw_cmdline = TRUE; /* show mode later */
9040 return 0;
9041 }
9042
9043 nwr_save = need_wait_return;
9044
9045 /* wait a bit before overwriting an important message */
9046 check_for_delay(FALSE);
9047
9048 /* if the cmdline is more than one line high, erase top lines */
9049 need_clear = clear_cmdline;
9050 if (clear_cmdline && cmdline_row < Rows - 1)
9051 msg_clr_cmdline(); /* will reset clear_cmdline */
9052
9053 /* Position on the last line in the window, column 0 */
9054 msg_pos_mode();
9055 cursor_off();
9056 attr = hl_attr(HLF_CM); /* Highlight mode */
9057 if (do_mode)
9058 {
9059 MSG_PUTS_ATTR("--", attr);
9060#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009061# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 if (xic != NULL && im_get_status() && !p_imdisable
9063 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009064# else
9065 if (
9066# ifdef HAVE_GTK2
9067 preedit_get_status()
9068# else
9069 im_get_status()
9070# endif
9071 )
9072# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
9074 MSG_PUTS_ATTR(" IM", attr);
9075# else
9076 MSG_PUTS_ATTR(" XIM", attr);
9077# endif
9078#endif
9079#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9080 if (gui.in_use)
9081 {
9082 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009083 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 }
9085#endif
9086#ifdef FEAT_INS_EXPAND
9087 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9088 {
9089 /* These messages can get long, avoid a wrap in a narrow
9090 * window. Prefer showing edit_submode_extra. */
9091 length = (Rows - msg_row) * Columns - 3;
9092 if (edit_submode_extra != NULL)
9093 length -= vim_strsize(edit_submode_extra);
9094 if (length > 0)
9095 {
9096 if (edit_submode_pre != NULL)
9097 length -= vim_strsize(edit_submode_pre);
9098 if (length - vim_strsize(edit_submode) > 0)
9099 {
9100 if (edit_submode_pre != NULL)
9101 msg_puts_attr(edit_submode_pre, attr);
9102 msg_puts_attr(edit_submode, attr);
9103 }
9104 if (edit_submode_extra != NULL)
9105 {
9106 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9107 if ((int)edit_submode_highl < (int)HLF_COUNT)
9108 sub_attr = hl_attr(edit_submode_highl);
9109 else
9110 sub_attr = attr;
9111 msg_puts_attr(edit_submode_extra, sub_attr);
9112 }
9113 }
9114 length = 0;
9115 }
9116 else
9117#endif
9118 {
9119#ifdef FEAT_VREPLACE
9120 if (State & VREPLACE_FLAG)
9121 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9122 else
9123#endif
9124 if (State & REPLACE_FLAG)
9125 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9126 else if (State & INSERT)
9127 {
9128#ifdef FEAT_RIGHTLEFT
9129 if (p_ri)
9130 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9131#endif
9132 MSG_PUTS_ATTR(_(" INSERT"), attr);
9133 }
9134 else if (restart_edit == 'I')
9135 MSG_PUTS_ATTR(_(" (insert)"), attr);
9136 else if (restart_edit == 'R')
9137 MSG_PUTS_ATTR(_(" (replace)"), attr);
9138 else if (restart_edit == 'V')
9139 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9140#ifdef FEAT_RIGHTLEFT
9141 if (p_hkmap)
9142 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9143# ifdef FEAT_FKMAP
9144 if (p_fkmap)
9145 MSG_PUTS_ATTR(farsi_text_5, attr);
9146# endif
9147#endif
9148#ifdef FEAT_KEYMAP
9149 if (State & LANGMAP)
9150 {
9151# ifdef FEAT_ARABIC
9152 if (curwin->w_p_arab)
9153 MSG_PUTS_ATTR(_(" Arabic"), attr);
9154 else
9155# endif
9156 MSG_PUTS_ATTR(_(" (lang)"), attr);
9157 }
9158#endif
9159 if ((State & INSERT) && p_paste)
9160 MSG_PUTS_ATTR(_(" (paste)"), attr);
9161
9162#ifdef FEAT_VISUAL
9163 if (VIsual_active)
9164 {
9165 char *p;
9166
9167 /* Don't concatenate separate words to avoid translation
9168 * problems. */
9169 switch ((VIsual_select ? 4 : 0)
9170 + (VIsual_mode == Ctrl_V) * 2
9171 + (VIsual_mode == 'V'))
9172 {
9173 case 0: p = N_(" VISUAL"); break;
9174 case 1: p = N_(" VISUAL LINE"); break;
9175 case 2: p = N_(" VISUAL BLOCK"); break;
9176 case 4: p = N_(" SELECT"); break;
9177 case 5: p = N_(" SELECT LINE"); break;
9178 default: p = N_(" SELECT BLOCK"); break;
9179 }
9180 MSG_PUTS_ATTR(_(p), attr);
9181 }
9182#endif
9183 MSG_PUTS_ATTR(" --", attr);
9184 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009185
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 need_clear = TRUE;
9187 }
9188 if (Recording
9189#ifdef FEAT_INS_EXPAND
9190 && edit_submode == NULL /* otherwise it gets too long */
9191#endif
9192 )
9193 {
9194 MSG_PUTS_ATTR(_("recording"), attr);
9195 need_clear = TRUE;
9196 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009197
9198 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 if (need_clear || clear_cmdline)
9200 msg_clr_eos();
9201 msg_didout = FALSE; /* overwrite this message */
9202 length = msg_col;
9203 msg_col = 0;
9204 need_wait_return = nwr_save; /* never ask for hit-return for this */
9205 }
9206 else if (clear_cmdline && msg_silent == 0)
9207 /* Clear the whole command line. Will reset "clear_cmdline". */
9208 msg_clr_cmdline();
9209
9210#ifdef FEAT_CMDL_INFO
9211# ifdef FEAT_VISUAL
9212 /* In Visual mode the size of the selected area must be redrawn. */
9213 if (VIsual_active)
9214 clear_showcmd();
9215# endif
9216
9217 /* If the last window has no status line, the ruler is after the mode
9218 * message and must be redrawn */
9219 if (redrawing()
9220# ifdef FEAT_WINDOWS
9221 && lastwin->w_status_height == 0
9222# endif
9223 )
9224 win_redr_ruler(lastwin, TRUE);
9225#endif
9226 redraw_cmdline = FALSE;
9227 clear_cmdline = FALSE;
9228
9229 return length;
9230}
9231
9232/*
9233 * Position for a mode message.
9234 */
9235 static void
9236msg_pos_mode()
9237{
9238 msg_col = 0;
9239 msg_row = Rows - 1;
9240}
9241
9242/*
9243 * Delete mode message. Used when ESC is typed which is expected to end
9244 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009245 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 */
9247 void
9248unshowmode(force)
9249 int force;
9250{
9251 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009252 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 */
9254 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9255 redraw_cmdline = TRUE; /* delete mode later */
9256 else
9257 {
9258 msg_pos_mode();
9259 if (Recording)
9260 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9261 msg_clr_eos();
9262 }
9263}
9264
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009265#if defined(FEAT_WINDOWS)
9266/*
9267 * Draw the tab pages line at the top of the Vim window.
9268 */
9269 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009270draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009271{
9272 int tabcount = 0;
9273 tabpage_T *tp;
9274 int tabwidth;
9275 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009276 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009277 int attr;
9278 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009279 win_T *cwp;
9280 int wincount;
9281 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009282 int c;
9283 int len;
9284 int attr_sel = hl_attr(HLF_TPS);
9285 int attr_nosel = hl_attr(HLF_TP);
9286 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009287 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009288 int room;
9289 int use_sep_chars = (t_colors < 8
9290#ifdef FEAT_GUI
9291 && !gui.in_use
9292#endif
9293 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009294
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009295 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009296
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009297#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009298 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009299 if (gui_use_tabline())
9300 {
9301 gui_update_tabline();
9302 return;
9303 }
9304#endif
9305
9306 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009307 return;
9308
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009309#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009310
9311 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9312 for (scol = 0; scol < Columns; ++scol)
9313 TabPageIdxs[scol] = 0;
9314
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009315 /* Use the 'tabline' option if it's set. */
9316 if (*p_tal != NUL)
9317 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009318 int save_called_emsg = called_emsg;
9319
9320 /* Check for an error. If there is one we would loop in redrawing the
9321 * screen. Avoid that by making 'tabline' empty. */
9322 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009323 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009324 if (called_emsg)
9325 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009326 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009327 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009328 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009329 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009330#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009331 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009332 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9333 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009334
Bram Moolenaar238a5642006-02-21 22:12:05 +00009335 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9336 if (tabwidth < 6)
9337 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009338
Bram Moolenaar238a5642006-02-21 22:12:05 +00009339 attr = attr_nosel;
9340 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009341 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009342 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9343 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009344 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009345 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009346
Bram Moolenaar238a5642006-02-21 22:12:05 +00009347 if (tp->tp_topframe == topframe)
9348 attr = attr_sel;
9349 if (use_sep_chars && col > 0)
9350 screen_putchar('|', 0, col++, attr);
9351
9352 if (tp->tp_topframe != topframe)
9353 attr = attr_nosel;
9354
9355 screen_putchar(' ', 0, col++, attr);
9356
9357 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009358 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009359 cwp = curwin;
9360 wp = firstwin;
9361 }
9362 else
9363 {
9364 cwp = tp->tp_curwin;
9365 wp = tp->tp_firstwin;
9366 }
9367
9368 modified = FALSE;
9369 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9370 if (bufIsChanged(wp->w_buffer))
9371 modified = TRUE;
9372 if (modified || wincount > 1)
9373 {
9374 if (wincount > 1)
9375 {
9376 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009377 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009378 if (col + len >= Columns - 3)
9379 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009380 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009381#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009382 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009383#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009384 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009385#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009386 );
9387 col += len;
9388 }
9389 if (modified)
9390 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9391 screen_putchar(' ', 0, col++, attr);
9392 }
9393
9394 room = scol - col + tabwidth - 1;
9395 if (room > 0)
9396 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009397 /* Get buffer name in NameBuff[] */
9398 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009399 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009400 len = vim_strsize(NameBuff);
9401 p = NameBuff;
9402#ifdef FEAT_MBYTE
9403 if (has_mbyte)
9404 while (len > room)
9405 {
9406 len -= ptr2cells(p);
9407 mb_ptr_adv(p);
9408 }
9409 else
9410#endif
9411 if (len > room)
9412 {
9413 p += len - room;
9414 len = room;
9415 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009416 if (len > Columns - col - 1)
9417 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009418
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009419 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009420 col += len;
9421 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009422 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009423
9424 /* Store the tab page number in TabPageIdxs[], so that
9425 * jump_to_mouse() knows where each one is. */
9426 ++tabcount;
9427 while (scol < col)
9428 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009429 }
9430
Bram Moolenaar238a5642006-02-21 22:12:05 +00009431 if (use_sep_chars)
9432 c = '_';
9433 else
9434 c = ' ';
9435 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009436
9437 /* Put an "X" for closing the current tab if there are several. */
9438 if (first_tabpage->tp_next != NULL)
9439 {
9440 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9441 TabPageIdxs[Columns - 1] = -999;
9442 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009443 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009444
9445 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9446 * set. */
9447 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009448}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009449
9450/*
9451 * Get buffer name for "buf" into NameBuff[].
9452 * Takes care of special buffer names and translates special characters.
9453 */
9454 void
9455get_trans_bufname(buf)
9456 buf_T *buf;
9457{
9458 if (buf_spname(buf) != NULL)
9459 STRCPY(NameBuff, buf_spname(buf));
9460 else
9461 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9462 trans_characters(NameBuff, MAXPATHL);
9463}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009464#endif
9465
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9467/*
9468 * Get the character to use in a status line. Get its attributes in "*attr".
9469 */
9470 static int
9471fillchar_status(attr, is_curwin)
9472 int *attr;
9473 int is_curwin;
9474{
9475 int fill;
9476 if (is_curwin)
9477 {
9478 *attr = hl_attr(HLF_S);
9479 fill = fill_stl;
9480 }
9481 else
9482 {
9483 *attr = hl_attr(HLF_SNC);
9484 fill = fill_stlnc;
9485 }
9486 /* Use fill when there is highlighting, and highlighting of current
9487 * window differs, or the fillchars differ, or this is not the
9488 * current window */
9489 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9490 || !is_curwin || firstwin == lastwin)
9491 || (fill_stl != fill_stlnc)))
9492 return fill;
9493 if (is_curwin)
9494 return '^';
9495 return '=';
9496}
9497#endif
9498
9499#ifdef FEAT_VERTSPLIT
9500/*
9501 * Get the character to use in a separator between vertically split windows.
9502 * Get its attributes in "*attr".
9503 */
9504 static int
9505fillchar_vsep(attr)
9506 int *attr;
9507{
9508 *attr = hl_attr(HLF_C);
9509 if (*attr == 0 && fill_vert == ' ')
9510 return '|';
9511 else
9512 return fill_vert;
9513}
9514#endif
9515
9516/*
9517 * Return TRUE if redrawing should currently be done.
9518 */
9519 int
9520redrawing()
9521{
9522 return (!RedrawingDisabled
9523 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9524}
9525
9526/*
9527 * Return TRUE if printing messages should currently be done.
9528 */
9529 int
9530messaging()
9531{
9532 return (!(p_lz && char_avail() && !KeyTyped));
9533}
9534
9535/*
9536 * Show current status info in ruler and various other places
9537 * If always is FALSE, only show ruler if position has changed.
9538 */
9539 void
9540showruler(always)
9541 int always;
9542{
9543 if (!always && !redrawing())
9544 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009545#ifdef FEAT_INS_EXPAND
9546 if (pum_visible())
9547 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009548# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009549 /* Don't redraw right now, do it later. */
9550 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009551# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009552 return;
9553 }
9554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009556 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009557 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009558 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 else
9561#endif
9562#ifdef FEAT_CMDL_INFO
9563 win_redr_ruler(curwin, always);
9564#endif
9565
9566#ifdef FEAT_TITLE
9567 if (need_maketitle
9568# ifdef FEAT_STL_OPT
9569 || (p_icon && (stl_syntax & STL_IN_ICON))
9570 || (p_title && (stl_syntax & STL_IN_TITLE))
9571# endif
9572 )
9573 maketitle();
9574#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009575#ifdef FEAT_WINDOWS
9576 /* Redraw the tab pages line if needed. */
9577 if (redraw_tabline)
9578 draw_tabline();
9579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580}
9581
9582#ifdef FEAT_CMDL_INFO
9583 static void
9584win_redr_ruler(wp, always)
9585 win_T *wp;
9586 int always;
9587{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009588#define RULER_BUF_LEN 70
9589 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 int row;
9591 int fillchar;
9592 int attr;
9593 int empty_line = FALSE;
9594 colnr_T virtcol;
9595 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009596 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 int o;
9598#ifdef FEAT_VERTSPLIT
9599 int this_ru_col;
9600 int off = 0;
9601 int width = Columns;
9602# define WITH_OFF(x) x
9603# define WITH_WIDTH(x) x
9604#else
9605# define WITH_OFF(x) 0
9606# define WITH_WIDTH(x) Columns
9607# define this_ru_col ru_col
9608#endif
9609
9610 /* If 'ruler' off or redrawing disabled, don't do anything */
9611 if (!p_ru)
9612 return;
9613
9614 /*
9615 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9616 * after deleting lines, before cursor.lnum is corrected.
9617 */
9618 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9619 return;
9620
9621#ifdef FEAT_INS_EXPAND
9622 /* Don't draw the ruler while doing insert-completion, it might overwrite
9623 * the (long) mode message. */
9624# ifdef FEAT_WINDOWS
9625 if (wp == lastwin && lastwin->w_status_height == 0)
9626# endif
9627 if (edit_submode != NULL)
9628 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009629 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9630 if (pum_visible())
9631 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632#endif
9633
9634#ifdef FEAT_STL_OPT
9635 if (*p_ruf)
9636 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009637 int save_called_emsg = called_emsg;
9638
9639 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009641 if (called_emsg)
9642 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009643 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009644 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 return;
9646 }
9647#endif
9648
9649 /*
9650 * Check if not in Insert mode and the line is empty (will show "0-1").
9651 */
9652 if (!(State & INSERT)
9653 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9654 empty_line = TRUE;
9655
9656 /*
9657 * Only draw the ruler when something changed.
9658 */
9659 validate_virtcol_win(wp);
9660 if ( redraw_cmdline
9661 || always
9662 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9663 || wp->w_cursor.col != wp->w_ru_cursor.col
9664 || wp->w_virtcol != wp->w_ru_virtcol
9665#ifdef FEAT_VIRTUALEDIT
9666 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9667#endif
9668 || wp->w_topline != wp->w_ru_topline
9669 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9670#ifdef FEAT_DIFF
9671 || wp->w_topfill != wp->w_ru_topfill
9672#endif
9673 || empty_line != wp->w_ru_empty)
9674 {
9675 cursor_off();
9676#ifdef FEAT_WINDOWS
9677 if (wp->w_status_height)
9678 {
9679 row = W_WINROW(wp) + wp->w_height;
9680 fillchar = fillchar_status(&attr, wp == curwin);
9681# ifdef FEAT_VERTSPLIT
9682 off = W_WINCOL(wp);
9683 width = W_WIDTH(wp);
9684# endif
9685 }
9686 else
9687#endif
9688 {
9689 row = Rows - 1;
9690 fillchar = ' ';
9691 attr = 0;
9692#ifdef FEAT_VERTSPLIT
9693 width = Columns;
9694 off = 0;
9695#endif
9696 }
9697
9698 /* In list mode virtcol needs to be recomputed */
9699 virtcol = wp->w_virtcol;
9700 if (wp->w_p_list && lcs_tab1 == NUL)
9701 {
9702 wp->w_p_list = FALSE;
9703 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9704 wp->w_p_list = TRUE;
9705 }
9706
9707 /*
9708 * Some sprintfs return the length, some return a pointer.
9709 * To avoid portability problems we use strlen() here.
9710 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009711 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9713 ? 0L
9714 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009715 len = STRLEN(buffer);
9716 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9718 (int)virtcol + 1);
9719
9720 /*
9721 * Add a "50%" if there is room for it.
9722 * On the last line, don't print in the last column (scrolls the
9723 * screen up on some terminals).
9724 */
9725 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009726 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 o = i + vim_strsize(buffer + i + 1);
9728#ifdef FEAT_WINDOWS
9729 if (wp->w_status_height == 0) /* can't use last char of screen */
9730#endif
9731 ++o;
9732#ifdef FEAT_VERTSPLIT
9733 this_ru_col = ru_col - (Columns - width);
9734 if (this_ru_col < 0)
9735 this_ru_col = 0;
9736#endif
9737 /* Never use more than half the window/screen width, leave the other
9738 * half for the filename. */
9739 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9740 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9741 if (this_ru_col + o < WITH_WIDTH(width))
9742 {
9743 while (this_ru_col + o < WITH_WIDTH(width))
9744 {
9745#ifdef FEAT_MBYTE
9746 if (has_mbyte)
9747 i += (*mb_char2bytes)(fillchar, buffer + i);
9748 else
9749#endif
9750 buffer[i++] = fillchar;
9751 ++o;
9752 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009753 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 }
9755 /* Truncate at window boundary. */
9756#ifdef FEAT_MBYTE
9757 if (has_mbyte)
9758 {
9759 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009760 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 {
9762 o += (*mb_ptr2cells)(buffer + i);
9763 if (this_ru_col + o > WITH_WIDTH(width))
9764 {
9765 buffer[i] = NUL;
9766 break;
9767 }
9768 }
9769 }
9770 else
9771#endif
9772 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9773 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9774
9775 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9776 i = redraw_cmdline;
9777 screen_fill(row, row + 1,
9778 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9779 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9780 fillchar, fillchar, attr);
9781 /* don't redraw the cmdline because of showing the ruler */
9782 redraw_cmdline = i;
9783 wp->w_ru_cursor = wp->w_cursor;
9784 wp->w_ru_virtcol = wp->w_virtcol;
9785 wp->w_ru_empty = empty_line;
9786 wp->w_ru_topline = wp->w_topline;
9787 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9788#ifdef FEAT_DIFF
9789 wp->w_ru_topfill = wp->w_topfill;
9790#endif
9791 }
9792}
9793#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009794
9795#if defined(FEAT_LINEBREAK) || defined(PROTO)
9796/*
Bram Moolenaar64486672010-05-16 15:46:46 +02009797 * Return the width of the 'number' and 'relativenumber' column.
9798 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009799 * Otherwise it depends on 'numberwidth' and the line count.
9800 */
9801 int
9802number_width(wp)
9803 win_T *wp;
9804{
9805 int n;
9806 linenr_T lnum;
9807
Bram Moolenaar64486672010-05-16 15:46:46 +02009808 if (wp->w_p_nu)
9809 /* 'number' */
9810 lnum = wp->w_buffer->b_ml.ml_line_count;
9811 else
9812 /* 'relativenumber' */
9813 lnum = wp->w_height;
9814
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009815 if (lnum == wp->w_nrwidth_line_count)
9816 return wp->w_nrwidth_width;
9817 wp->w_nrwidth_line_count = lnum;
9818
9819 n = 0;
9820 do
9821 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009822 lnum /= 10;
9823 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009824 } while (lnum > 0);
9825
9826 /* 'numberwidth' gives the minimal width plus one */
9827 if (n < wp->w_p_nuw - 1)
9828 n = wp->w_p_nuw - 1;
9829
9830 wp->w_nrwidth_width = n;
9831 return n;
9832}
9833#endif