blob: 508e627058e435f05e99c6346387e8a3fb82bcb3 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
92/*
93 * The attributes that are actually active for writing to the screen.
94 */
95static int screen_attr = 0;
96
97/*
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
100 */
101static int screen_cur_row, screen_cur_col; /* last known cursor position */
102
103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106
107#ifdef FEAT_FOLDING
108static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109#endif
110
111/*
112 * Buffer for one screen line (characters and attributes).
113 */
114static schar_T *current_ScreenLine;
115
116static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000117static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_FOLDING
119static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000123static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125#ifdef FEAT_RIGHTLEFT
126static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128#else
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_VERTSPLIT
133static void draw_vsep_win __ARGS((win_T *wp, int row));
134#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000135#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000136static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000139#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void start_search_hl __ARGS((void));
141static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200142static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
144static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
145#endif
146static void screen_start_highlight __ARGS((int attr));
147static void screen_char __ARGS((unsigned off, int row, int col));
148#ifdef FEAT_MBYTE
149static void screen_char_2 __ARGS((unsigned off, int row, int col));
150#endif
151static void screenclear2 __ARGS((void));
152static void lineclear __ARGS((unsigned off, int width));
153static void lineinvalid __ARGS((unsigned off, int width));
154#ifdef FEAT_VERTSPLIT
155static void linecopy __ARGS((int to, int from, win_T *wp));
156static void redraw_block __ARGS((int row, int end, win_T *wp));
157#endif
158static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
159static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000162static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
165static int fillchar_status __ARGS((int *attr, int is_curwin));
166#endif
167#ifdef FEAT_VERTSPLIT
168static int fillchar_vsep __ARGS((int *attr));
169#endif
170#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000171static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_CMDL_INFO
174static void win_redr_ruler __ARGS((win_T *wp, int always));
175#endif
176
177#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
178/* Ugly global: overrule attribute used by screen_char() */
179static int screen_char_attr = 0;
180#endif
181
182/*
183 * Redraw the current window later, with update_screen(type).
184 * Set must_redraw only if not already set to a higher value.
185 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
186 */
187 void
188redraw_later(type)
189 int type;
190{
191 redraw_win_later(curwin, type);
192}
193
194 void
195redraw_win_later(wp, type)
196 win_T *wp;
197 int type;
198{
199 if (wp->w_redr_type < type)
200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
214redraw_later_clear()
215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
232redraw_all_later(type)
233 int type;
234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
247redraw_curbuf_later(type)
248 int type;
249{
250 redraw_buf_later(curbuf, type);
251}
252
253 void
254redraw_buf_later(buf, type)
255 buf_T *buf;
256 int type;
257{
258 win_T *wp;
259
260 FOR_ALL_WINDOWS(wp)
261 {
262 if (wp->w_buffer == buf)
263 redraw_win_later(wp, type);
264 }
265}
266
267/*
268 * Changed something in the current window, at buffer line "lnum", that
269 * requires that line and possibly other lines to be redrawn.
270 * Used when entering/leaving Insert mode with the cursor on a folded line.
271 * Used to remove the "$" from a change command.
272 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
273 * may become invalid and the whole window will have to be redrawn.
274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 void
276redrawWinline(lnum, invalid)
277 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000278 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280#ifdef FEAT_FOLDING
281 int i;
282#endif
283
284 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
285 curwin->w_redraw_top = lnum;
286 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
287 curwin->w_redraw_bot = lnum;
288 redraw_later(VALID);
289
290#ifdef FEAT_FOLDING
291 if (invalid)
292 {
293 /* A w_lines[] entry for this lnum has become invalid. */
294 i = find_wl_entry(curwin, lnum);
295 if (i >= 0)
296 curwin->w_lines[i].wl_valid = FALSE;
297 }
298#endif
299}
300
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200301#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200302 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * update all windows that are editing the current buffer
305 */
306 void
307update_curbuf(type)
308 int type;
309{
310 redraw_curbuf_later(type);
311 update_screen(type);
312}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * update_screen()
317 *
318 * Based on the current value of curwin->w_topline, transfer a screenfull
319 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
320 */
321 void
322update_screen(type)
323 int type;
324{
325 win_T *wp;
326 static int did_intro = FALSE;
327#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
328 int did_one;
329#endif
330
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000331 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 if (!screen_valid(TRUE))
333 return;
334
335 if (must_redraw)
336 {
337 if (type < must_redraw) /* use maximal type */
338 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000339
340 /* must_redraw is reset here, so that when we run into some weird
341 * reason to redraw while busy redrawing (e.g., asynchronous
342 * scrolling), or update_topline() in win_update() will cause a
343 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 must_redraw = 0;
345 }
346
347 /* Need to update w_lines[]. */
348 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
349 type = NOT_VALID;
350
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000351 /* Postpone the redrawing when it's not needed and when being called
352 * recursively. */
353 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 redraw_later(type); /* remember type for next time */
356 must_redraw = type;
357 if (type > INVERTED_ALL)
358 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
359 return;
360 }
361
362 updating_screen = TRUE;
363#ifdef FEAT_SYN_HL
364 ++display_tick; /* let syntax code know we're in a next round of
365 * display updating */
366#endif
367
368 /*
369 * if the screen was scrolled up when displaying a message, scroll it down
370 */
371 if (msg_scrolled)
372 {
373 clear_cmdline = TRUE;
374 if (msg_scrolled > Rows - 5) /* clearing is faster */
375 type = CLEAR;
376 else if (type != CLEAR)
377 {
378 check_for_delay(FALSE);
379 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
380 type = CLEAR;
381 FOR_ALL_WINDOWS(wp)
382 {
383 if (W_WINROW(wp) < msg_scrolled)
384 {
385 if (W_WINROW(wp) + wp->w_height > msg_scrolled
386 && wp->w_redr_type < REDRAW_TOP
387 && wp->w_lines_valid > 0
388 && wp->w_topline == wp->w_lines[0].wl_lnum)
389 {
390 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
391 wp->w_redr_type = REDRAW_TOP;
392 }
393 else
394 {
395 wp->w_redr_type = NOT_VALID;
396#ifdef FEAT_WINDOWS
397 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
398 <= msg_scrolled)
399 wp->w_redr_status = TRUE;
400#endif
401 }
402 }
403 }
404 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000406 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409 msg_scrolled = 0;
410 need_wait_return = FALSE;
411 }
412
413 /* reset cmdline_row now (may have been changed temporarily) */
414 compute_cmdrow();
415
416 /* Check for changed highlighting */
417 if (need_highlight_changed)
418 highlight_changed();
419
420 if (type == CLEAR) /* first clear screen */
421 {
422 screenclear(); /* will reset clear_cmdline */
423 type = NOT_VALID;
424 }
425
426 if (clear_cmdline) /* going to clear cmdline (done below) */
427 check_for_delay(FALSE);
428
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000429#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200430 /* Force redraw when width of 'number' or 'relativenumber' column
431 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
434 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 curwin->w_redr_type = NOT_VALID;
436#endif
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 /*
439 * Only start redrawing if there is really something to do.
440 */
441 if (type == INVERTED)
442 update_curswant();
443 if (curwin->w_redr_type < type
444 && !((type == VALID
445 && curwin->w_lines[0].wl_valid
446#ifdef FEAT_DIFF
447 && curwin->w_topfill == curwin->w_old_topfill
448 && curwin->w_botfill == curwin->w_old_botfill
449#endif
450 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
451#ifdef FEAT_VISUAL
452 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000453 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
455 && curwin->w_old_visual_mode == VIsual_mode
456 && (curwin->w_valid & VALID_VIRTCOL)
457 && curwin->w_old_curswant == curwin->w_curswant)
458#endif
459 ))
460 curwin->w_redr_type = type;
461
Bram Moolenaar5a305422006-04-28 22:38:25 +0000462#ifdef FEAT_WINDOWS
463 /* Redraw the tab pages line if needed. */
464 if (redraw_tabline || type >= NOT_VALID)
465 draw_tabline();
466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#ifdef FEAT_SYN_HL
469 /*
470 * Correct stored syntax highlighting info for changes in each displayed
471 * buffer. Each buffer must only be done once.
472 */
473 FOR_ALL_WINDOWS(wp)
474 {
475 if (wp->w_buffer->b_mod_set)
476 {
477# ifdef FEAT_WINDOWS
478 win_T *wwp;
479
480 /* Check if we already did this buffer. */
481 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
482 if (wwp->w_buffer == wp->w_buffer)
483 break;
484# endif
485 if (
486# ifdef FEAT_WINDOWS
487 wwp == wp &&
488# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200489 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 syn_stack_apply_changes(wp->w_buffer);
491 }
492 }
493#endif
494
495 /*
496 * Go from top to bottom through the windows, redrawing the ones that need
497 * it.
498 */
499#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
500 did_one = FALSE;
501#endif
502#ifdef FEAT_SEARCH_EXTRA
503 search_hl.rm.regprog = NULL;
504#endif
505 FOR_ALL_WINDOWS(wp)
506 {
507 if (wp->w_redr_type != 0)
508 {
509 cursor_off();
510#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
511 if (!did_one)
512 {
513 did_one = TRUE;
514# ifdef FEAT_SEARCH_EXTRA
515 start_search_hl();
516# endif
517# ifdef FEAT_CLIPBOARD
518 /* When Visual area changed, may have to update selection. */
519 if (clip_star.available && clip_isautosel())
520 clip_update_selection();
521# endif
522#ifdef FEAT_GUI
523 /* Remove the cursor before starting to do anything, because
524 * scrolling may make it difficult to redraw the text under
525 * it. */
526 if (gui.in_use)
527 gui_undraw_cursor();
528#endif
529 }
530#endif
531 win_update(wp);
532 }
533
534#ifdef FEAT_WINDOWS
535 /* redraw status line after the window to minimize cursor movement */
536 if (wp->w_redr_status)
537 {
538 cursor_off();
539 win_redr_status(wp);
540 }
541#endif
542 }
543#if defined(FEAT_SEARCH_EXTRA)
544 end_search_hl();
545#endif
546
547#ifdef FEAT_WINDOWS
548 /* Reset b_mod_set flags. Going through all windows is probably faster
549 * than going through all buffers (there could be many buffers). */
550 for (wp = firstwin; wp != NULL; wp = wp->w_next)
551 wp->w_buffer->b_mod_set = FALSE;
552#else
553 curbuf->b_mod_set = FALSE;
554#endif
555
556 updating_screen = FALSE;
557#ifdef FEAT_GUI
558 gui_may_resize_shell();
559#endif
560
561 /* Clear or redraw the command line. Done last, because scrolling may
562 * mess up the command line. */
563 if (clear_cmdline || redraw_cmdline)
564 showmode();
565
566 /* May put up an introductory message when not editing a file */
567 if (!did_intro && bufempty()
568 && curbuf->b_fname == NULL
569#ifdef FEAT_WINDOWS
570 && firstwin->w_next == NULL
571#endif
572 && vim_strchr(p_shm, SHM_INTRO) == NULL)
573 intro_message(FALSE);
574 did_intro = TRUE;
575
576#ifdef FEAT_GUI
577 /* Redraw the cursor and update the scrollbars when all screen updating is
578 * done. */
579 if (gui.in_use)
580 {
581 out_flush(); /* required before updating the cursor */
582 if (did_one)
583 gui_update_cursor(FALSE, FALSE);
584 gui_update_scrollbars(FALSE);
585 }
586#endif
587}
588
Bram Moolenaar860cae12010-06-05 23:22:07 +0200589#if defined(FEAT_CONCEAL) || defined(PROTO)
590 void
591update_single_line(wp, lnum)
592 win_T *wp;
593 linenr_T lnum;
594{
595 int row;
596 int j;
597
598 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200599 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200600 {
601# ifdef FEAT_GUI
602 /* Remove the cursor before starting to do anything, because scrolling
603 * may make it difficult to redraw the text under it. */
604 if (gui.in_use)
605 gui_undraw_cursor();
606# endif
607 row = 0;
608 for (j = 0; j < wp->w_lines_valid; ++j)
609 {
610 if (lnum == wp->w_lines[j].wl_lnum)
611 {
612 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200613# ifdef FEAT_SEARCH_EXTRA
614 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200615 start_search_hl();
616 prepare_search_hl(wp, lnum);
617# endif
618 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
619# if defined(FEAT_SEARCH_EXTRA)
620 end_search_hl();
621# endif
622 break;
623 }
624 row += wp->w_lines[j].wl_size;
625 }
626# ifdef FEAT_GUI
627 /* Redraw the cursor */
628 if (gui.in_use)
629 {
630 out_flush(); /* required before updating the cursor */
631 gui_update_cursor(FALSE, FALSE);
632 }
633# endif
634 }
635}
636#endif
637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
639static void update_prepare __ARGS((void));
640static void update_finish __ARGS((void));
641
642/*
643 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000644 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 */
646 static void
647update_prepare()
648{
649 cursor_off();
650 updating_screen = TRUE;
651#ifdef FEAT_GUI
652 /* Remove the cursor before starting to do anything, because scrolling may
653 * make it difficult to redraw the text under it. */
654 if (gui.in_use)
655 gui_undraw_cursor();
656#endif
657#ifdef FEAT_SEARCH_EXTRA
658 start_search_hl();
659#endif
660}
661
662/*
663 * Finish updating one or more windows.
664 */
665 static void
666update_finish()
667{
668 if (redraw_cmdline)
669 showmode();
670
671# ifdef FEAT_SEARCH_EXTRA
672 end_search_hl();
673# endif
674
675 updating_screen = FALSE;
676
677# ifdef FEAT_GUI
678 gui_may_resize_shell();
679
680 /* Redraw the cursor and update the scrollbars when all screen updating is
681 * done. */
682 if (gui.in_use)
683 {
684 out_flush(); /* required before updating the cursor */
685 gui_update_cursor(FALSE, FALSE);
686 gui_update_scrollbars(FALSE);
687 }
688# endif
689}
690#endif
691
692#if defined(FEAT_SIGNS) || defined(PROTO)
693 void
694update_debug_sign(buf, lnum)
695 buf_T *buf;
696 linenr_T lnum;
697{
698 win_T *wp;
699 int doit = FALSE;
700
701# ifdef FEAT_FOLDING
702 win_foldinfo.fi_level = 0;
703# endif
704
705 /* update/delete a specific mark */
706 FOR_ALL_WINDOWS(wp)
707 {
708 if (buf != NULL && lnum > 0)
709 {
710 if (wp->w_buffer == buf && lnum >= wp->w_topline
711 && lnum < wp->w_botline)
712 {
713 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
714 wp->w_redraw_top = lnum;
715 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
716 wp->w_redraw_bot = lnum;
717 redraw_win_later(wp, VALID);
718 }
719 }
720 else
721 redraw_win_later(wp, VALID);
722 if (wp->w_redr_type != 0)
723 doit = TRUE;
724 }
725
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000726 /* Return when there is nothing to do or screen updating already
727 * happening. */
728 if (!doit || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 return;
730
731 /* update all windows that need updating */
732 update_prepare();
733
734# ifdef FEAT_WINDOWS
735 for (wp = firstwin; wp; wp = wp->w_next)
736 {
737 if (wp->w_redr_type != 0)
738 win_update(wp);
739 if (wp->w_redr_status)
740 win_redr_status(wp);
741 }
742# else
743 if (curwin->w_redr_type != 0)
744 win_update(curwin);
745# endif
746
747 update_finish();
748}
749#endif
750
751
752#if defined(FEAT_GUI) || defined(PROTO)
753/*
754 * Update a single window, its status line and maybe the command line msg.
755 * Used for the GUI scrollbar.
756 */
757 void
758updateWindow(wp)
759 win_T *wp;
760{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000761 /* return if already busy updating */
762 if (updating_screen)
763 return;
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 update_prepare();
766
767#ifdef FEAT_CLIPBOARD
768 /* When Visual area changed, may have to update selection. */
769 if (clip_star.available && clip_isautosel())
770 clip_update_selection();
771#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000776 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000777 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000778 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (wp->w_redr_status
781# ifdef FEAT_CMDL_INFO
782 || p_ru
783# endif
784# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000785 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# endif
787 )
788 win_redr_status(wp);
789#endif
790
791 update_finish();
792}
793#endif
794
795/*
796 * Update a single window.
797 *
798 * This may cause the windows below it also to be redrawn (when clearing the
799 * screen or scrolling lines).
800 *
801 * How the window is redrawn depends on wp->w_redr_type. Each type also
802 * implies the one below it.
803 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000804 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
806 * INVERTED redraw the changed part of the Visual area
807 * INVERTED_ALL redraw the whole Visual area
808 * VALID 1. scroll up/down to adjust for a changed w_topline
809 * 2. update lines at the top when scrolled down
810 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000811 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 * b_mod_top and b_mod_bot.
813 * - if wp->w_redraw_top non-zero, redraw lines between
814 * wp->w_redraw_top and wp->w_redr_bot.
815 * - continue redrawing when syntax status is invalid.
816 * 4. if scrolled up, update lines at the bottom.
817 * This results in three areas that may need updating:
818 * top: from first row to top_end (when scrolled down)
819 * mid: from mid_start to mid_end (update inversion or changed text)
820 * bot: from bot_start to last row (when scrolled up)
821 */
822 static void
823win_update(wp)
824 win_T *wp;
825{
826 buf_T *buf = wp->w_buffer;
827 int type;
828 int top_end = 0; /* Below last row of the top area that needs
829 updating. 0 when no top area updating. */
830 int mid_start = 999;/* first row of the mid area that needs
831 updating. 999 when no mid area updating. */
832 int mid_end = 0; /* Below last row of the mid area that needs
833 updating. 0 when no mid area updating. */
834 int bot_start = 999;/* first row of the bot area that needs
835 updating. 999 when no bot area updating */
836#ifdef FEAT_VISUAL
837 int scrolled_down = FALSE; /* TRUE when scrolled down when
838 w_topline got smaller a bit */
839#endif
840#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000841 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 int top_to_mod = FALSE; /* redraw above mod_top */
843#endif
844
845 int row; /* current window row to display */
846 linenr_T lnum; /* current buffer lnum to display */
847 int idx; /* current index in w_lines[] */
848 int srow; /* starting row of the current line */
849
850 int eof = FALSE; /* if TRUE, we hit the end of the file */
851 int didline = FALSE; /* if TRUE, we finished the last line */
852 int i;
853 long j;
854 static int recursive = FALSE; /* being called recursively */
855 int old_botline = wp->w_botline;
856#ifdef FEAT_FOLDING
857 long fold_count;
858#endif
859#ifdef FEAT_SYN_HL
860 /* remember what happened to the previous line, to know if
861 * check_visual_highlight() can be used */
862#define DID_NONE 1 /* didn't update a line */
863#define DID_LINE 2 /* updated a normal line */
864#define DID_FOLD 3 /* updated a folded line */
865 int did_update = DID_NONE;
866 linenr_T syntax_last_parsed = 0; /* last parsed text line */
867#endif
868 linenr_T mod_top = 0;
869 linenr_T mod_bot = 0;
870#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
871 int save_got_int;
872#endif
873
874 type = wp->w_redr_type;
875
876 if (type == NOT_VALID)
877 {
878#ifdef FEAT_WINDOWS
879 wp->w_redr_status = TRUE;
880#endif
881 wp->w_lines_valid = 0;
882 }
883
884 /* Window is zero-height: nothing to draw. */
885 if (wp->w_height == 0)
886 {
887 wp->w_redr_type = 0;
888 return;
889 }
890
891#ifdef FEAT_VERTSPLIT
892 /* Window is zero-width: Only need to draw the separator. */
893 if (wp->w_width == 0)
894 {
895 /* draw the vertical separator right of this window */
896 draw_vsep_win(wp, 0);
897 wp->w_redr_type = 0;
898 return;
899 }
900#endif
901
902#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200903 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000906#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200907 /* Force redraw when width of 'number' or 'relativenumber' column
908 * changes. */
909 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000910 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000911 {
912 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000913 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000914 }
915 else
916#endif
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
919 {
920 /*
921 * When there are both inserted/deleted lines and specific lines to be
922 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
923 * everything (only happens when redrawing is off for while).
924 */
925 type = NOT_VALID;
926 }
927 else
928 {
929 /*
930 * Set mod_top to the first line that needs displaying because of
931 * changes. Set mod_bot to the first line after the changes.
932 */
933 mod_top = wp->w_redraw_top;
934 if (wp->w_redraw_bot != 0)
935 mod_bot = wp->w_redraw_bot + 1;
936 else
937 mod_bot = 0;
938 wp->w_redraw_top = 0; /* reset for next time */
939 wp->w_redraw_bot = 0;
940 if (buf->b_mod_set)
941 {
942 if (mod_top == 0 || mod_top > buf->b_mod_top)
943 {
944 mod_top = buf->b_mod_top;
945#ifdef FEAT_SYN_HL
946 /* Need to redraw lines above the change that may be included
947 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200950 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (mod_top < 1)
952 mod_top = 1;
953 }
954#endif
955 }
956 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
957 mod_bot = buf->b_mod_bot;
958
959#ifdef FEAT_SEARCH_EXTRA
960 /* When 'hlsearch' is on and using a multi-line search pattern, a
961 * change in one line may make the Search highlighting in a
962 * previous line invalid. Simple solution: redraw all visible
963 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000964 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000966 if (search_hl.rm.regprog != NULL
967 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000969 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000970 {
971 cur = wp->w_match_head;
972 while (cur != NULL)
973 {
974 if (cur->match.regprog != NULL
975 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000976 {
977 top_to_mod = TRUE;
978 break;
979 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000980 cur = cur->next;
981 }
982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#endif
984 }
985#ifdef FEAT_FOLDING
986 if (mod_top != 0 && hasAnyFolding(wp))
987 {
988 linenr_T lnumt, lnumb;
989
990 /*
991 * A change in a line can cause lines above it to become folded or
992 * unfolded. Find the top most buffer line that may be affected.
993 * If the line was previously folded and displayed, get the first
994 * line of that fold. If the line is folded now, get the first
995 * folded line. Use the minimum of these two.
996 */
997
998 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
999 * the line below it. If there is no valid entry, use w_topline.
1000 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1001 * to this line. If there is no valid entry, use MAXLNUM. */
1002 lnumt = wp->w_topline;
1003 lnumb = MAXLNUM;
1004 for (i = 0; i < wp->w_lines_valid; ++i)
1005 if (wp->w_lines[i].wl_valid)
1006 {
1007 if (wp->w_lines[i].wl_lastlnum < mod_top)
1008 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1009 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1010 {
1011 lnumb = wp->w_lines[i].wl_lnum;
1012 /* When there is a fold column it might need updating
1013 * in the next line ("J" just above an open fold). */
1014 if (wp->w_p_fdc > 0)
1015 ++lnumb;
1016 }
1017 }
1018
1019 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1020 if (mod_top > lnumt)
1021 mod_top = lnumt;
1022
1023 /* Now do the same for the bottom line (one above mod_bot). */
1024 --mod_bot;
1025 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1026 ++mod_bot;
1027 if (mod_bot < lnumb)
1028 mod_bot = lnumb;
1029 }
1030#endif
1031
1032 /* When a change starts above w_topline and the end is below
1033 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001034 * If the end of the change is above w_topline: do like no change was
1035 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (mod_top != 0 && mod_top < wp->w_topline)
1037 {
1038 if (mod_bot > wp->w_topline)
1039 mod_top = wp->w_topline;
1040#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001041 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 top_end = 1;
1043#endif
1044 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001045
1046 /* When line numbers are displayed need to redraw all lines below
1047 * inserted/deleted lines. */
1048 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1049 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 }
1051
1052 /*
1053 * When only displaying the lines at the top, set top_end. Used when
1054 * window has scrolled down for msg_scrolled.
1055 */
1056 if (type == REDRAW_TOP)
1057 {
1058 j = 0;
1059 for (i = 0; i < wp->w_lines_valid; ++i)
1060 {
1061 j += wp->w_lines[i].wl_size;
1062 if (j >= wp->w_upd_rows)
1063 {
1064 top_end = j;
1065 break;
1066 }
1067 }
1068 if (top_end == 0)
1069 /* not found (cannot happen?): redraw everything */
1070 type = NOT_VALID;
1071 else
1072 /* top area defined, the rest is VALID */
1073 type = VALID;
1074 }
1075
Bram Moolenaar367329b2007-08-30 11:53:22 +00001076 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001077 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1078 * non-zero and thus not FALSE) will indicate that screenclear() was not
1079 * called. */
1080 if (screen_cleared)
1081 screen_cleared = MAYBE;
1082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 /*
1084 * If there are no changes on the screen that require a complete redraw,
1085 * handle three cases:
1086 * 1: we are off the top of the screen by a few lines: scroll down
1087 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1088 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1089 * w_lines[] that needs updating.
1090 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001091 if ((type == VALID || type == SOME_VALID
1092 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#ifdef FEAT_DIFF
1094 && !wp->w_botfill && !wp->w_old_botfill
1095#endif
1096 )
1097 {
1098 if (mod_top != 0 && wp->w_topline == mod_top)
1099 {
1100 /*
1101 * w_topline is the first changed line, the scrolling will be done
1102 * further down.
1103 */
1104 }
1105 else if (wp->w_lines[0].wl_valid
1106 && (wp->w_topline < wp->w_lines[0].wl_lnum
1107#ifdef FEAT_DIFF
1108 || (wp->w_topline == wp->w_lines[0].wl_lnum
1109 && wp->w_topfill > wp->w_old_topfill)
1110#endif
1111 ))
1112 {
1113 /*
1114 * New topline is above old topline: May scroll down.
1115 */
1116#ifdef FEAT_FOLDING
1117 if (hasAnyFolding(wp))
1118 {
1119 linenr_T ln;
1120
1121 /* count the number of lines we are off, counting a sequence
1122 * of folded lines as one */
1123 j = 0;
1124 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1125 {
1126 ++j;
1127 if (j >= wp->w_height - 2)
1128 break;
1129 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1130 }
1131 }
1132 else
1133#endif
1134 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1135 if (j < wp->w_height - 2) /* not too far off */
1136 {
1137 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1138#ifdef FEAT_DIFF
1139 /* insert extra lines for previously invisible filler lines */
1140 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1141 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1142 - wp->w_old_topfill;
1143#endif
1144 if (i < wp->w_height - 2) /* less than a screen off */
1145 {
1146 /*
1147 * Try to insert the correct number of lines.
1148 * If not the last window, delete the lines at the bottom.
1149 * win_ins_lines may fail when the terminal can't do it.
1150 */
1151 if (i > 0)
1152 check_for_delay(FALSE);
1153 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1154 {
1155 if (wp->w_lines_valid != 0)
1156 {
1157 /* Need to update rows that are new, stop at the
1158 * first one that scrolled down. */
1159 top_end = i;
1160#ifdef FEAT_VISUAL
1161 scrolled_down = TRUE;
1162#endif
1163
1164 /* Move the entries that were scrolled, disable
1165 * the entries for the lines to be redrawn. */
1166 if ((wp->w_lines_valid += j) > wp->w_height)
1167 wp->w_lines_valid = wp->w_height;
1168 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1169 wp->w_lines[idx] = wp->w_lines[idx - j];
1170 while (idx >= 0)
1171 wp->w_lines[idx--].wl_valid = FALSE;
1172 }
1173 }
1174 else
1175 mid_start = 0; /* redraw all lines */
1176 }
1177 else
1178 mid_start = 0; /* redraw all lines */
1179 }
1180 else
1181 mid_start = 0; /* redraw all lines */
1182 }
1183 else
1184 {
1185 /*
1186 * New topline is at or below old topline: May scroll up.
1187 * When topline didn't change, find first entry in w_lines[] that
1188 * needs updating.
1189 */
1190
1191 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1192 j = -1;
1193 row = 0;
1194 for (i = 0; i < wp->w_lines_valid; i++)
1195 {
1196 if (wp->w_lines[i].wl_valid
1197 && wp->w_lines[i].wl_lnum == wp->w_topline)
1198 {
1199 j = i;
1200 break;
1201 }
1202 row += wp->w_lines[i].wl_size;
1203 }
1204 if (j == -1)
1205 {
1206 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1207 * lines */
1208 mid_start = 0;
1209 }
1210 else
1211 {
1212 /*
1213 * Try to delete the correct number of lines.
1214 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1215 */
1216#ifdef FEAT_DIFF
1217 /* If the topline didn't change, delete old filler lines,
1218 * otherwise delete filler lines of the new topline... */
1219 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1220 row += wp->w_old_topfill;
1221 else
1222 row += diff_check_fill(wp, wp->w_topline);
1223 /* ... but don't delete new filler lines. */
1224 row -= wp->w_topfill;
1225#endif
1226 if (row > 0)
1227 {
1228 check_for_delay(FALSE);
1229 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1230 bot_start = wp->w_height - row;
1231 else
1232 mid_start = 0; /* redraw all lines */
1233 }
1234 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1235 {
1236 /*
1237 * Skip the lines (below the deleted lines) that are still
1238 * valid and don't need redrawing. Copy their info
1239 * upwards, to compensate for the deleted lines. Set
1240 * bot_start to the first row that needs redrawing.
1241 */
1242 bot_start = 0;
1243 idx = 0;
1244 for (;;)
1245 {
1246 wp->w_lines[idx] = wp->w_lines[j];
1247 /* stop at line that didn't fit, unless it is still
1248 * valid (no lines deleted) */
1249 if (row > 0 && bot_start + row
1250 + (int)wp->w_lines[j].wl_size > wp->w_height)
1251 {
1252 wp->w_lines_valid = idx + 1;
1253 break;
1254 }
1255 bot_start += wp->w_lines[idx++].wl_size;
1256
1257 /* stop at the last valid entry in w_lines[].wl_size */
1258 if (++j >= wp->w_lines_valid)
1259 {
1260 wp->w_lines_valid = idx;
1261 break;
1262 }
1263 }
1264#ifdef FEAT_DIFF
1265 /* Correct the first entry for filler lines at the top
1266 * when it won't get updated below. */
1267 if (wp->w_p_diff && bot_start > 0)
1268 wp->w_lines[0].wl_size =
1269 plines_win_nofill(wp, wp->w_topline, TRUE)
1270 + wp->w_topfill;
1271#endif
1272 }
1273 }
1274 }
1275
1276 /* When starting redraw in the first line, redraw all lines. When
1277 * there is only one window it's probably faster to clear the screen
1278 * first. */
1279 if (mid_start == 0)
1280 {
1281 mid_end = wp->w_height;
1282 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001283 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001284 /* Clear the screen when it was not done by win_del_lines() or
1285 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1286 * then. */
1287 if (screen_cleared != TRUE)
1288 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001289#ifdef FEAT_WINDOWS
1290 /* The screen was cleared, redraw the tab pages line. */
1291 if (redraw_tabline)
1292 draw_tabline();
1293#endif
1294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001296
1297 /* When win_del_lines() or win_ins_lines() caused the screen to be
1298 * cleared (only happens for the first window) or when screenclear()
1299 * was called directly above, "must_redraw" will have been set to
1300 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1301 if (screen_cleared == TRUE)
1302 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 else
1305 {
1306 /* Not VALID or INVERTED: redraw all lines. */
1307 mid_start = 0;
1308 mid_end = wp->w_height;
1309 }
1310
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001311 if (type == SOME_VALID)
1312 {
1313 /* SOME_VALID: redraw all lines. */
1314 mid_start = 0;
1315 mid_end = wp->w_height;
1316 type = NOT_VALID;
1317 }
1318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319#ifdef FEAT_VISUAL
1320 /* check if we are updating or removing the inverted part */
1321 if ((VIsual_active && buf == curwin->w_buffer)
1322 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1323 {
1324 linenr_T from, to;
1325
1326 if (VIsual_active)
1327 {
1328 if (VIsual_active
1329 && (VIsual_mode != wp->w_old_visual_mode
1330 || type == INVERTED_ALL))
1331 {
1332 /*
1333 * If the type of Visual selection changed, redraw the whole
1334 * selection. Also when the ownership of the X selection is
1335 * gained or lost.
1336 */
1337 if (curwin->w_cursor.lnum < VIsual.lnum)
1338 {
1339 from = curwin->w_cursor.lnum;
1340 to = VIsual.lnum;
1341 }
1342 else
1343 {
1344 from = VIsual.lnum;
1345 to = curwin->w_cursor.lnum;
1346 }
1347 /* redraw more when the cursor moved as well */
1348 if (wp->w_old_cursor_lnum < from)
1349 from = wp->w_old_cursor_lnum;
1350 if (wp->w_old_cursor_lnum > to)
1351 to = wp->w_old_cursor_lnum;
1352 if (wp->w_old_visual_lnum < from)
1353 from = wp->w_old_visual_lnum;
1354 if (wp->w_old_visual_lnum > to)
1355 to = wp->w_old_visual_lnum;
1356 }
1357 else
1358 {
1359 /*
1360 * Find the line numbers that need to be updated: The lines
1361 * between the old cursor position and the current cursor
1362 * position. Also check if the Visual position changed.
1363 */
1364 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1365 {
1366 from = curwin->w_cursor.lnum;
1367 to = wp->w_old_cursor_lnum;
1368 }
1369 else
1370 {
1371 from = wp->w_old_cursor_lnum;
1372 to = curwin->w_cursor.lnum;
1373 if (from == 0) /* Visual mode just started */
1374 from = to;
1375 }
1376
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001377 if (VIsual.lnum != wp->w_old_visual_lnum
1378 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
1380 if (wp->w_old_visual_lnum < from
1381 && wp->w_old_visual_lnum != 0)
1382 from = wp->w_old_visual_lnum;
1383 if (wp->w_old_visual_lnum > to)
1384 to = wp->w_old_visual_lnum;
1385 if (VIsual.lnum < from)
1386 from = VIsual.lnum;
1387 if (VIsual.lnum > to)
1388 to = VIsual.lnum;
1389 }
1390 }
1391
1392 /*
1393 * If in block mode and changed column or curwin->w_curswant:
1394 * update all lines.
1395 * First compute the actual start and end column.
1396 */
1397 if (VIsual_mode == Ctrl_V)
1398 {
1399 colnr_T fromc, toc;
1400
1401 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1402 ++toc;
1403 if (curwin->w_curswant == MAXCOL)
1404 toc = MAXCOL;
1405
1406 if (fromc != wp->w_old_cursor_fcol
1407 || toc != wp->w_old_cursor_lcol)
1408 {
1409 if (from > VIsual.lnum)
1410 from = VIsual.lnum;
1411 if (to < VIsual.lnum)
1412 to = VIsual.lnum;
1413 }
1414 wp->w_old_cursor_fcol = fromc;
1415 wp->w_old_cursor_lcol = toc;
1416 }
1417 }
1418 else
1419 {
1420 /* Use the line numbers of the old Visual area. */
1421 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1422 {
1423 from = wp->w_old_cursor_lnum;
1424 to = wp->w_old_visual_lnum;
1425 }
1426 else
1427 {
1428 from = wp->w_old_visual_lnum;
1429 to = wp->w_old_cursor_lnum;
1430 }
1431 }
1432
1433 /*
1434 * There is no need to update lines above the top of the window.
1435 */
1436 if (from < wp->w_topline)
1437 from = wp->w_topline;
1438
1439 /*
1440 * If we know the value of w_botline, use it to restrict the update to
1441 * the lines that are visible in the window.
1442 */
1443 if (wp->w_valid & VALID_BOTLINE)
1444 {
1445 if (from >= wp->w_botline)
1446 from = wp->w_botline - 1;
1447 if (to >= wp->w_botline)
1448 to = wp->w_botline - 1;
1449 }
1450
1451 /*
1452 * Find the minimal part to be updated.
1453 * Watch out for scrolling that made entries in w_lines[] invalid.
1454 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1455 * top_end; need to redraw from top_end to the "to" line.
1456 * A middle mouse click with a Visual selection may change the text
1457 * above the Visual area and reset wl_valid, do count these for
1458 * mid_end (in srow).
1459 */
1460 if (mid_start > 0)
1461 {
1462 lnum = wp->w_topline;
1463 idx = 0;
1464 srow = 0;
1465 if (scrolled_down)
1466 mid_start = top_end;
1467 else
1468 mid_start = 0;
1469 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1470 {
1471 if (wp->w_lines[idx].wl_valid)
1472 mid_start += wp->w_lines[idx].wl_size;
1473 else if (!scrolled_down)
1474 srow += wp->w_lines[idx].wl_size;
1475 ++idx;
1476# ifdef FEAT_FOLDING
1477 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1478 lnum = wp->w_lines[idx].wl_lnum;
1479 else
1480# endif
1481 ++lnum;
1482 }
1483 srow += mid_start;
1484 mid_end = wp->w_height;
1485 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1486 {
1487 if (wp->w_lines[idx].wl_valid
1488 && wp->w_lines[idx].wl_lnum >= to + 1)
1489 {
1490 /* Only update until first row of this line */
1491 mid_end = srow;
1492 break;
1493 }
1494 srow += wp->w_lines[idx].wl_size;
1495 }
1496 }
1497 }
1498
1499 if (VIsual_active && buf == curwin->w_buffer)
1500 {
1501 wp->w_old_visual_mode = VIsual_mode;
1502 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1503 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001504 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 wp->w_old_curswant = curwin->w_curswant;
1506 }
1507 else
1508 {
1509 wp->w_old_visual_mode = 0;
1510 wp->w_old_cursor_lnum = 0;
1511 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001512 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514#endif /* FEAT_VISUAL */
1515
1516#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1517 /* reset got_int, otherwise regexp won't work */
1518 save_got_int = got_int;
1519 got_int = 0;
1520#endif
1521#ifdef FEAT_FOLDING
1522 win_foldinfo.fi_level = 0;
1523#endif
1524
1525 /*
1526 * Update all the window rows.
1527 */
1528 idx = 0; /* first entry in w_lines[].wl_size */
1529 row = 0;
1530 srow = 0;
1531 lnum = wp->w_topline; /* first line shown in window */
1532 for (;;)
1533 {
1534 /* stop updating when reached the end of the window (check for _past_
1535 * the end of the window is at the end of the loop) */
1536 if (row == wp->w_height)
1537 {
1538 didline = TRUE;
1539 break;
1540 }
1541
1542 /* stop updating when hit the end of the file */
1543 if (lnum > buf->b_ml.ml_line_count)
1544 {
1545 eof = TRUE;
1546 break;
1547 }
1548
1549 /* Remember the starting row of the line that is going to be dealt
1550 * with. It is used further down when the line doesn't fit. */
1551 srow = row;
1552
1553 /*
1554 * Update a line when it is in an area that needs updating, when it
1555 * has changes or w_lines[idx] is invalid.
1556 * bot_start may be halfway a wrapped line after using
1557 * win_del_lines(), check if the current line includes it.
1558 * When syntax folding is being used, the saved syntax states will
1559 * already have been updated, we can't see where the syntax state is
1560 * the same again, just update until the end of the window.
1561 */
1562 if (row < top_end
1563 || (row >= mid_start && row < mid_end)
1564#ifdef FEAT_SEARCH_EXTRA
1565 || top_to_mod
1566#endif
1567 || idx >= wp->w_lines_valid
1568 || (row + wp->w_lines[idx].wl_size > bot_start)
1569 || (mod_top != 0
1570 && (lnum == mod_top
1571 || (lnum >= mod_top
1572 && (lnum < mod_bot
1573#ifdef FEAT_SYN_HL
1574 || did_update == DID_FOLD
1575 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001576 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 && (
1578# ifdef FEAT_FOLDING
1579 (foldmethodIsSyntax(wp)
1580 && hasAnyFolding(wp)) ||
1581# endif
1582 syntax_check_changed(lnum)))
1583#endif
1584 )))))
1585 {
1586#ifdef FEAT_SEARCH_EXTRA
1587 if (lnum == mod_top)
1588 top_to_mod = FALSE;
1589#endif
1590
1591 /*
1592 * When at start of changed lines: May scroll following lines
1593 * up or down to minimize redrawing.
1594 * Don't do this when the change continues until the end.
1595 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1596 */
1597 if (lnum == mod_top
1598 && mod_bot != MAXLNUM
1599 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1600 {
1601 int old_rows = 0;
1602 int new_rows = 0;
1603 int xtra_rows;
1604 linenr_T l;
1605
1606 /* Count the old number of window rows, using w_lines[], which
1607 * should still contain the sizes for the lines as they are
1608 * currently displayed. */
1609 for (i = idx; i < wp->w_lines_valid; ++i)
1610 {
1611 /* Only valid lines have a meaningful wl_lnum. Invalid
1612 * lines are part of the changed area. */
1613 if (wp->w_lines[i].wl_valid
1614 && wp->w_lines[i].wl_lnum == mod_bot)
1615 break;
1616 old_rows += wp->w_lines[i].wl_size;
1617#ifdef FEAT_FOLDING
1618 if (wp->w_lines[i].wl_valid
1619 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1620 {
1621 /* Must have found the last valid entry above mod_bot.
1622 * Add following invalid entries. */
1623 ++i;
1624 while (i < wp->w_lines_valid
1625 && !wp->w_lines[i].wl_valid)
1626 old_rows += wp->w_lines[i++].wl_size;
1627 break;
1628 }
1629#endif
1630 }
1631
1632 if (i >= wp->w_lines_valid)
1633 {
1634 /* We can't find a valid line below the changed lines,
1635 * need to redraw until the end of the window.
1636 * Inserting/deleting lines has no use. */
1637 bot_start = 0;
1638 }
1639 else
1640 {
1641 /* Able to count old number of rows: Count new window
1642 * rows, and may insert/delete lines */
1643 j = idx;
1644 for (l = lnum; l < mod_bot; ++l)
1645 {
1646#ifdef FEAT_FOLDING
1647 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1648 ++new_rows;
1649 else
1650#endif
1651#ifdef FEAT_DIFF
1652 if (l == wp->w_topline)
1653 new_rows += plines_win_nofill(wp, l, TRUE)
1654 + wp->w_topfill;
1655 else
1656#endif
1657 new_rows += plines_win(wp, l, TRUE);
1658 ++j;
1659 if (new_rows > wp->w_height - row - 2)
1660 {
1661 /* it's getting too much, must redraw the rest */
1662 new_rows = 9999;
1663 break;
1664 }
1665 }
1666 xtra_rows = new_rows - old_rows;
1667 if (xtra_rows < 0)
1668 {
1669 /* May scroll text up. If there is not enough
1670 * remaining text or scrolling fails, must redraw the
1671 * rest. If scrolling works, must redraw the text
1672 * below the scrolled text. */
1673 if (row - xtra_rows >= wp->w_height - 2)
1674 mod_bot = MAXLNUM;
1675 else
1676 {
1677 check_for_delay(FALSE);
1678 if (win_del_lines(wp, row,
1679 -xtra_rows, FALSE, FALSE) == FAIL)
1680 mod_bot = MAXLNUM;
1681 else
1682 bot_start = wp->w_height + xtra_rows;
1683 }
1684 }
1685 else if (xtra_rows > 0)
1686 {
1687 /* May scroll text down. If there is not enough
1688 * remaining text of scrolling fails, must redraw the
1689 * rest. */
1690 if (row + xtra_rows >= wp->w_height - 2)
1691 mod_bot = MAXLNUM;
1692 else
1693 {
1694 check_for_delay(FALSE);
1695 if (win_ins_lines(wp, row + old_rows,
1696 xtra_rows, FALSE, FALSE) == FAIL)
1697 mod_bot = MAXLNUM;
1698 else if (top_end > row + old_rows)
1699 /* Scrolled the part at the top that requires
1700 * updating down. */
1701 top_end += xtra_rows;
1702 }
1703 }
1704
1705 /* When not updating the rest, may need to move w_lines[]
1706 * entries. */
1707 if (mod_bot != MAXLNUM && i != j)
1708 {
1709 if (j < i)
1710 {
1711 int x = row + new_rows;
1712
1713 /* move entries in w_lines[] upwards */
1714 for (;;)
1715 {
1716 /* stop at last valid entry in w_lines[] */
1717 if (i >= wp->w_lines_valid)
1718 {
1719 wp->w_lines_valid = j;
1720 break;
1721 }
1722 wp->w_lines[j] = wp->w_lines[i];
1723 /* stop at a line that won't fit */
1724 if (x + (int)wp->w_lines[j].wl_size
1725 > wp->w_height)
1726 {
1727 wp->w_lines_valid = j + 1;
1728 break;
1729 }
1730 x += wp->w_lines[j++].wl_size;
1731 ++i;
1732 }
1733 if (bot_start > x)
1734 bot_start = x;
1735 }
1736 else /* j > i */
1737 {
1738 /* move entries in w_lines[] downwards */
1739 j -= i;
1740 wp->w_lines_valid += j;
1741 if (wp->w_lines_valid > wp->w_height)
1742 wp->w_lines_valid = wp->w_height;
1743 for (i = wp->w_lines_valid; i - j >= idx; --i)
1744 wp->w_lines[i] = wp->w_lines[i - j];
1745
1746 /* The w_lines[] entries for inserted lines are
1747 * now invalid, but wl_size may be used above.
1748 * Reset to zero. */
1749 while (i >= idx)
1750 {
1751 wp->w_lines[i].wl_size = 0;
1752 wp->w_lines[i--].wl_valid = FALSE;
1753 }
1754 }
1755 }
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758
1759#ifdef FEAT_FOLDING
1760 /*
1761 * When lines are folded, display one line for all of them.
1762 * Otherwise, display normally (can be several display lines when
1763 * 'wrap' is on).
1764 */
1765 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1766 if (fold_count != 0)
1767 {
1768 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1769 ++row;
1770 --fold_count;
1771 wp->w_lines[idx].wl_folded = TRUE;
1772 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1773# ifdef FEAT_SYN_HL
1774 did_update = DID_FOLD;
1775# endif
1776 }
1777 else
1778#endif
1779 if (idx < wp->w_lines_valid
1780 && wp->w_lines[idx].wl_valid
1781 && wp->w_lines[idx].wl_lnum == lnum
1782 && lnum > wp->w_topline
1783 && !(dy_flags & DY_LASTLINE)
1784 && srow + wp->w_lines[idx].wl_size > wp->w_height
1785#ifdef FEAT_DIFF
1786 && diff_check_fill(wp, lnum) == 0
1787#endif
1788 )
1789 {
1790 /* This line is not going to fit. Don't draw anything here,
1791 * will draw "@ " lines below. */
1792 row = wp->w_height + 1;
1793 }
1794 else
1795 {
1796#ifdef FEAT_SEARCH_EXTRA
1797 prepare_search_hl(wp, lnum);
1798#endif
1799#ifdef FEAT_SYN_HL
1800 /* Let the syntax stuff know we skipped a few lines. */
1801 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001802 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 syntax_end_parsing(syntax_last_parsed + 1);
1804#endif
1805
1806 /*
1807 * Display one line.
1808 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001809 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811#ifdef FEAT_FOLDING
1812 wp->w_lines[idx].wl_folded = FALSE;
1813 wp->w_lines[idx].wl_lastlnum = lnum;
1814#endif
1815#ifdef FEAT_SYN_HL
1816 did_update = DID_LINE;
1817 syntax_last_parsed = lnum;
1818#endif
1819 }
1820
1821 wp->w_lines[idx].wl_lnum = lnum;
1822 wp->w_lines[idx].wl_valid = TRUE;
1823 if (row > wp->w_height) /* past end of screen */
1824 {
1825 /* we may need the size of that too long line later on */
1826 if (dollar_vcol == 0)
1827 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1828 ++idx;
1829 break;
1830 }
1831 if (dollar_vcol == 0)
1832 wp->w_lines[idx].wl_size = row - srow;
1833 ++idx;
1834#ifdef FEAT_FOLDING
1835 lnum += fold_count + 1;
1836#else
1837 ++lnum;
1838#endif
1839 }
1840 else
1841 {
1842 /* This line does not need updating, advance to the next one */
1843 row += wp->w_lines[idx++].wl_size;
1844 if (row > wp->w_height) /* past end of screen */
1845 break;
1846#ifdef FEAT_FOLDING
1847 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1848#else
1849 ++lnum;
1850#endif
1851#ifdef FEAT_SYN_HL
1852 did_update = DID_NONE;
1853#endif
1854 }
1855
1856 if (lnum > buf->b_ml.ml_line_count)
1857 {
1858 eof = TRUE;
1859 break;
1860 }
1861 }
1862 /*
1863 * End of loop over all window lines.
1864 */
1865
1866
1867 if (idx > wp->w_lines_valid)
1868 wp->w_lines_valid = idx;
1869
1870#ifdef FEAT_SYN_HL
1871 /*
1872 * Let the syntax stuff know we stop parsing here.
1873 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001874 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 syntax_end_parsing(syntax_last_parsed + 1);
1876#endif
1877
1878 /*
1879 * If we didn't hit the end of the file, and we didn't finish the last
1880 * line we were working on, then the line didn't fit.
1881 */
1882 wp->w_empty_rows = 0;
1883#ifdef FEAT_DIFF
1884 wp->w_filler_rows = 0;
1885#endif
1886 if (!eof && !didline)
1887 {
1888 if (lnum == wp->w_topline)
1889 {
1890 /*
1891 * Single line that does not fit!
1892 * Don't overwrite it, it can be edited.
1893 */
1894 wp->w_botline = lnum + 1;
1895 }
1896#ifdef FEAT_DIFF
1897 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1898 {
1899 /* Window ends in filler lines. */
1900 wp->w_botline = lnum;
1901 wp->w_filler_rows = wp->w_height - srow;
1902 }
1903#endif
1904 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1905 {
1906 /*
1907 * Last line isn't finished: Display "@@@" at the end.
1908 */
1909 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1910 W_WINROW(wp) + wp->w_height,
1911 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1912 '@', '@', hl_attr(HLF_AT));
1913 set_empty_rows(wp, srow);
1914 wp->w_botline = lnum;
1915 }
1916 else
1917 {
1918 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1919 wp->w_botline = lnum;
1920 }
1921 }
1922 else
1923 {
1924#ifdef FEAT_VERTSPLIT
1925 draw_vsep_win(wp, row);
1926#endif
1927 if (eof) /* we hit the end of the file */
1928 {
1929 wp->w_botline = buf->b_ml.ml_line_count + 1;
1930#ifdef FEAT_DIFF
1931 j = diff_check_fill(wp, wp->w_botline);
1932 if (j > 0 && !wp->w_botfill)
1933 {
1934 /*
1935 * Display filler lines at the end of the file
1936 */
1937 if (char2cells(fill_diff) > 1)
1938 i = '-';
1939 else
1940 i = fill_diff;
1941 if (row + j > wp->w_height)
1942 j = wp->w_height - row;
1943 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1944 row += j;
1945 }
1946#endif
1947 }
1948 else if (dollar_vcol == 0)
1949 wp->w_botline = lnum;
1950
1951 /* make sure the rest of the screen is blank */
1952 /* put '~'s on rows that aren't part of the file. */
1953 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1954 }
1955
1956 /* Reset the type of redrawing required, the window has been updated. */
1957 wp->w_redr_type = 0;
1958#ifdef FEAT_DIFF
1959 wp->w_old_topfill = wp->w_topfill;
1960 wp->w_old_botfill = wp->w_botfill;
1961#endif
1962
1963 if (dollar_vcol == 0)
1964 {
1965 /*
1966 * There is a trick with w_botline. If we invalidate it on each
1967 * change that might modify it, this will cause a lot of expensive
1968 * calls to plines() in update_topline() each time. Therefore the
1969 * value of w_botline is often approximated, and this value is used to
1970 * compute the value of w_topline. If the value of w_botline was
1971 * wrong, check that the value of w_topline is correct (cursor is on
1972 * the visible part of the text). If it's not, we need to redraw
1973 * again. Mostly this just means scrolling up a few lines, so it
1974 * doesn't look too bad. Only do this for the current window (where
1975 * changes are relevant).
1976 */
1977 wp->w_valid |= VALID_BOTLINE;
1978 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1979 {
1980 recursive = TRUE;
1981 curwin->w_valid &= ~VALID_TOPLINE;
1982 update_topline(); /* may invalidate w_botline again */
1983 if (must_redraw != 0)
1984 {
1985 /* Don't update for changes in buffer again. */
1986 i = curbuf->b_mod_set;
1987 curbuf->b_mod_set = FALSE;
1988 win_update(curwin);
1989 must_redraw = 0;
1990 curbuf->b_mod_set = i;
1991 }
1992 recursive = FALSE;
1993 }
1994 }
1995
1996#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1997 /* restore got_int, unless CTRL-C was hit while redrawing */
1998 if (!got_int)
1999 got_int = save_got_int;
2000#endif
2001}
2002
2003#ifdef FEAT_SIGNS
2004static int draw_signcolumn __ARGS((win_T *wp));
2005
2006/*
2007 * Return TRUE when window "wp" has a column to draw signs in.
2008 */
2009 static int
2010draw_signcolumn(wp)
2011 win_T *wp;
2012{
2013 return (wp->w_buffer->b_signlist != NULL
2014# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002015 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016# endif
2017 );
2018}
2019#endif
2020
2021/*
2022 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2023 * as the filler character.
2024 */
2025 static void
2026win_draw_end(wp, c1, c2, row, endrow, hl)
2027 win_T *wp;
2028 int c1;
2029 int c2;
2030 int row;
2031 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002032 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
2034#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2035 int n = 0;
2036# define FDC_OFF n
2037#else
2038# define FDC_OFF 0
2039#endif
2040
2041#ifdef FEAT_RIGHTLEFT
2042 if (wp->w_p_rl)
2043 {
2044 /* No check for cmdline window: should never be right-left. */
2045# ifdef FEAT_FOLDING
2046 n = wp->w_p_fdc;
2047
2048 if (n > 0)
2049 {
2050 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002051 if (n > W_WIDTH(wp))
2052 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2054 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2055 ' ', ' ', hl_attr(HLF_FC));
2056 }
2057# endif
2058# ifdef FEAT_SIGNS
2059 if (draw_signcolumn(wp))
2060 {
2061 int nn = n + 2;
2062
2063 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002064 if (nn > W_WIDTH(wp))
2065 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2067 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2068 ' ', ' ', hl_attr(HLF_SC));
2069 n = nn;
2070 }
2071# endif
2072 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2073 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2074 c2, c2, hl_attr(hl));
2075 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2076 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2077 c1, c2, hl_attr(hl));
2078 }
2079 else
2080#endif
2081 {
2082#ifdef FEAT_CMDWIN
2083 if (cmdwin_type != 0 && wp == curwin)
2084 {
2085 /* draw the cmdline character in the leftmost column */
2086 n = 1;
2087 if (n > wp->w_width)
2088 n = wp->w_width;
2089 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2090 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2091 cmdwin_type, ' ', hl_attr(HLF_AT));
2092 }
2093#endif
2094#ifdef FEAT_FOLDING
2095 if (wp->w_p_fdc > 0)
2096 {
2097 int nn = n + wp->w_p_fdc;
2098
2099 /* draw the fold column at the left */
2100 if (nn > W_WIDTH(wp))
2101 nn = W_WIDTH(wp);
2102 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2103 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2104 ' ', ' ', hl_attr(HLF_FC));
2105 n = nn;
2106 }
2107#endif
2108#ifdef FEAT_SIGNS
2109 if (draw_signcolumn(wp))
2110 {
2111 int nn = n + 2;
2112
2113 /* draw the sign column after the fold column */
2114 if (nn > W_WIDTH(wp))
2115 nn = W_WIDTH(wp);
2116 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2117 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2118 ' ', ' ', hl_attr(HLF_SC));
2119 n = nn;
2120 }
2121#endif
2122 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2123 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2124 c1, c2, hl_attr(hl));
2125 }
2126 set_empty_rows(wp, row);
2127}
2128
Bram Moolenaar1a384422010-07-14 19:53:30 +02002129#ifdef FEAT_SYN_HL
2130static int advance_color_col __ARGS((int vcol, int **color_cols));
2131
2132/*
2133 * Advance **color_cols and return TRUE when there are columns to draw.
2134 */
2135 static int
2136advance_color_col(vcol, color_cols)
2137 int vcol;
2138 int **color_cols;
2139{
2140 while (**color_cols >= 0 && vcol > **color_cols)
2141 ++*color_cols;
2142 return (**color_cols >= 0);
2143}
2144#endif
2145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146#ifdef FEAT_FOLDING
2147/*
2148 * Display one folded line.
2149 */
2150 static void
2151fold_line(wp, fold_count, foldinfo, lnum, row)
2152 win_T *wp;
2153 long fold_count;
2154 foldinfo_T *foldinfo;
2155 linenr_T lnum;
2156 int row;
2157{
2158 char_u buf[51];
2159 pos_T *top, *bot;
2160 linenr_T lnume = lnum + fold_count - 1;
2161 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002162 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 int col;
2165 int txtcol;
2166 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002167 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169 /* Build the fold line:
2170 * 1. Add the cmdwin_type for the command-line window
2171 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002172 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 * 4. Compose the text
2174 * 5. Add the text
2175 * 6. set highlighting for the Visual area an other text
2176 */
2177 col = 0;
2178
2179 /*
2180 * 1. Add the cmdwin_type for the command-line window
2181 * Ignores 'rightleft', this window is never right-left.
2182 */
2183#ifdef FEAT_CMDWIN
2184 if (cmdwin_type != 0 && wp == curwin)
2185 {
2186 ScreenLines[off] = cmdwin_type;
2187 ScreenAttrs[off] = hl_attr(HLF_AT);
2188#ifdef FEAT_MBYTE
2189 if (enc_utf8)
2190 ScreenLinesUC[off] = 0;
2191#endif
2192 ++col;
2193 }
2194#endif
2195
2196 /*
2197 * 2. Add the 'foldcolumn'
2198 */
2199 fdc = wp->w_p_fdc;
2200 if (fdc > W_WIDTH(wp) - col)
2201 fdc = W_WIDTH(wp) - col;
2202 if (fdc > 0)
2203 {
2204 fill_foldcolumn(buf, wp, TRUE, lnum);
2205#ifdef FEAT_RIGHTLEFT
2206 if (wp->w_p_rl)
2207 {
2208 int i;
2209
2210 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2211 hl_attr(HLF_FC));
2212 /* reverse the fold column */
2213 for (i = 0; i < fdc; ++i)
2214 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2215 }
2216 else
2217#endif
2218 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2219 col += fdc;
2220 }
2221
2222#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002223# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2224 for (ri = 0; ri < l; ++ri) \
2225 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2226 else \
2227 for (ri = 0; ri < l; ++ri) \
2228 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002230# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2231 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#endif
2233
Bram Moolenaar64486672010-05-16 15:46:46 +02002234 /* Set all attributes of the 'number' or 'relativenumber' column and the
2235 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002236 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238#ifdef FEAT_SIGNS
2239 /* If signs are being displayed, add two spaces. */
2240 if (draw_signcolumn(wp))
2241 {
2242 len = W_WIDTH(wp) - col;
2243 if (len > 0)
2244 {
2245 if (len > 2)
2246 len = 2;
2247# ifdef FEAT_RIGHTLEFT
2248 if (wp->w_p_rl)
2249 /* the line number isn't reversed */
2250 copy_text_attr(off + W_WIDTH(wp) - len - col,
2251 (char_u *)" ", len, hl_attr(HLF_FL));
2252 else
2253# endif
2254 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2255 col += len;
2256 }
2257 }
2258#endif
2259
2260 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002261 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002263 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 len = W_WIDTH(wp) - col;
2266 if (len > 0)
2267 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002268 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002269 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002270
2271 if (len > w + 1)
2272 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002273
2274 if (wp->w_p_nu)
2275 /* 'number' */
2276 num = (long)lnum;
2277 else
2278 /* 'relativenumber', don't use negative numbers */
2279 num = (long)abs((int)get_cursor_rel_lnum(wp, lnum));
2280
2281 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282#ifdef FEAT_RIGHTLEFT
2283 if (wp->w_p_rl)
2284 /* the line number isn't reversed */
2285 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2286 hl_attr(HLF_FL));
2287 else
2288#endif
2289 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2290 col += len;
2291 }
2292 }
2293
2294 /*
2295 * 4. Compose the folded-line string with 'foldtext', if set.
2296 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002297 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 txtcol = col; /* remember where text starts */
2300
2301 /*
2302 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2303 * Right-left text is put in columns 0 - number-col, normal text is put
2304 * in columns number-col - window-width.
2305 */
2306#ifdef FEAT_MBYTE
2307 if (has_mbyte)
2308 {
2309 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002310 int u8c, u8cc[MAX_MCO];
2311 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 int idx;
2313 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002314 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315# ifdef FEAT_ARABIC
2316 int prev_c = 0; /* previous Arabic character */
2317 int prev_c1 = 0; /* first composing char for prev_c */
2318# endif
2319
2320# ifdef FEAT_RIGHTLEFT
2321 if (wp->w_p_rl)
2322 idx = off;
2323 else
2324# endif
2325 idx = off + col;
2326
2327 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2328 for (p = text; *p != NUL; )
2329 {
2330 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002331 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (col + cells > W_WIDTH(wp)
2333# ifdef FEAT_RIGHTLEFT
2334 - (wp->w_p_rl ? col : 0)
2335# endif
2336 )
2337 break;
2338 ScreenLines[idx] = *p;
2339 if (enc_utf8)
2340 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002341 u8c = utfc_ptr2char(p, u8cc);
2342 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 ScreenLinesUC[idx] = 0;
2345#ifdef FEAT_ARABIC
2346 prev_c = u8c;
2347#endif
2348 }
2349 else
2350 {
2351#ifdef FEAT_ARABIC
2352 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2353 {
2354 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002355 int pc, pc1, nc;
2356 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int firstbyte = *p;
2358
2359 /* The idea of what is the previous and next
2360 * character depends on 'rightleft'. */
2361 if (wp->w_p_rl)
2362 {
2363 pc = prev_c;
2364 pc1 = prev_c1;
2365 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002366 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 else
2369 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002370 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002372 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374 prev_c = u8c;
2375
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002376 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 pc, pc1, nc);
2378 ScreenLines[idx] = firstbyte;
2379 }
2380 else
2381 prev_c = u8c;
2382#endif
2383 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002384#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (u8c >= 0x10000)
2386 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2387 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002390 for (i = 0; i < Screen_mco; ++i)
2391 {
2392 ScreenLinesC[i][idx] = u8cc[i];
2393 if (u8cc[i] == 0)
2394 break;
2395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 if (cells > 1)
2398 ScreenLines[idx + 1] = 0;
2399 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002400 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2401 /* double-byte single width character */
2402 ScreenLines2[idx] = p[1];
2403 else if (cells > 1)
2404 /* double-width character */
2405 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 col += cells;
2407 idx += cells;
2408 p += c_len;
2409 }
2410 }
2411 else
2412#endif
2413 {
2414 len = (int)STRLEN(text);
2415 if (len > W_WIDTH(wp) - col)
2416 len = W_WIDTH(wp) - col;
2417 if (len > 0)
2418 {
2419#ifdef FEAT_RIGHTLEFT
2420 if (wp->w_p_rl)
2421 STRNCPY(current_ScreenLine, text, len);
2422 else
2423#endif
2424 STRNCPY(current_ScreenLine + col, text, len);
2425 col += len;
2426 }
2427 }
2428
2429 /* Fill the rest of the line with the fold filler */
2430#ifdef FEAT_RIGHTLEFT
2431 if (wp->w_p_rl)
2432 col -= txtcol;
2433#endif
2434 while (col < W_WIDTH(wp)
2435#ifdef FEAT_RIGHTLEFT
2436 - (wp->w_p_rl ? txtcol : 0)
2437#endif
2438 )
2439 {
2440#ifdef FEAT_MBYTE
2441 if (enc_utf8)
2442 {
2443 if (fill_fold >= 0x80)
2444 {
2445 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002446 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448 else
2449 ScreenLinesUC[off + col] = 0;
2450 }
2451#endif
2452 ScreenLines[off + col++] = fill_fold;
2453 }
2454
2455 if (text != buf)
2456 vim_free(text);
2457
2458 /*
2459 * 6. set highlighting for the Visual area an other text.
2460 * If all folded lines are in the Visual area, highlight the line.
2461 */
2462#ifdef FEAT_VISUAL
2463 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2464 {
2465 if (ltoreq(curwin->w_cursor, VIsual))
2466 {
2467 /* Visual is after curwin->w_cursor */
2468 top = &curwin->w_cursor;
2469 bot = &VIsual;
2470 }
2471 else
2472 {
2473 /* Visual is before curwin->w_cursor */
2474 top = &VIsual;
2475 bot = &curwin->w_cursor;
2476 }
2477 if (lnum >= top->lnum
2478 && lnume <= bot->lnum
2479 && (VIsual_mode != 'v'
2480 || ((lnum > top->lnum
2481 || (lnum == top->lnum
2482 && top->col == 0))
2483 && (lnume < bot->lnum
2484 || (lnume == bot->lnum
2485 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002486 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 if (VIsual_mode == Ctrl_V)
2489 {
2490 /* Visual block mode: highlight the chars part of the block */
2491 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2492 {
2493 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2494 len = wp->w_old_cursor_lcol;
2495 else
2496 len = W_WIDTH(wp) - txtcol;
2497 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002498 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 }
2501 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002504 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
2507 }
2508#endif
2509
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002510#ifdef FEAT_SYN_HL
2511 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002512 if (wp->w_p_cuc)
2513 {
2514 txtcol += wp->w_virtcol;
2515 if (wp->w_p_wrap)
2516 txtcol -= wp->w_skipcol;
2517 else
2518 txtcol -= wp->w_leftcol;
2519 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2520 ScreenAttrs[off + txtcol] = hl_combine_attr(
2521 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2522 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
2525 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2526 (int)W_WIDTH(wp), FALSE);
2527
2528 /*
2529 * Update w_cline_height and w_cline_folded if the cursor line was
2530 * updated (saves a call to plines() later).
2531 */
2532 if (wp == curwin
2533 && lnum <= curwin->w_cursor.lnum
2534 && lnume >= curwin->w_cursor.lnum)
2535 {
2536 curwin->w_cline_row = row;
2537 curwin->w_cline_height = 1;
2538 curwin->w_cline_folded = TRUE;
2539 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2540 }
2541}
2542
2543/*
2544 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2545 */
2546 static void
2547copy_text_attr(off, buf, len, attr)
2548 int off;
2549 char_u *buf;
2550 int len;
2551 int attr;
2552{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002553 int i;
2554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 mch_memmove(ScreenLines + off, buf, (size_t)len);
2556# ifdef FEAT_MBYTE
2557 if (enc_utf8)
2558 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2559# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002560 for (i = 0; i < len; ++i)
2561 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562}
2563
2564/*
2565 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002566 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
2568 static void
2569fill_foldcolumn(p, wp, closed, lnum)
2570 char_u *p;
2571 win_T *wp;
2572 int closed; /* TRUE of FALSE */
2573 linenr_T lnum; /* current line number */
2574{
2575 int i = 0;
2576 int level;
2577 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002578 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
2580 /* Init to all spaces. */
2581 copy_spaces(p, (size_t)wp->w_p_fdc);
2582
2583 level = win_foldinfo.fi_level;
2584 if (level > 0)
2585 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002586 /* If there is only one column put more info in it. */
2587 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 /* If the column is too narrow, we start at the lowest level that
2590 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002591 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (first_level < 1)
2593 first_level = 1;
2594
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002595 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 if (win_foldinfo.fi_lnum == lnum
2598 && first_level + i >= win_foldinfo.fi_low_level)
2599 p[i] = '-';
2600 else if (first_level == 1)
2601 p[i] = '|';
2602 else if (first_level + i <= 9)
2603 p[i] = '0' + first_level + i;
2604 else
2605 p[i] = '>';
2606 if (first_level + i == level)
2607 break;
2608 }
2609 }
2610 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002611 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612}
2613#endif /* FEAT_FOLDING */
2614
2615/*
2616 * Display line "lnum" of window 'wp' on the screen.
2617 * Start at row "startrow", stop when "endrow" is reached.
2618 * wp->w_virtcol needs to be valid.
2619 *
2620 * Return the number of last row the line occupies.
2621 */
2622 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002623win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 win_T *wp;
2625 linenr_T lnum;
2626 int startrow;
2627 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002628 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 int col; /* visual column on screen */
2631 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2632 int c = 0; /* init for GCC */
2633 long vcol = 0; /* virtual column (for tabs) */
2634 long vcol_prev = -1; /* "vcol" of previous character */
2635 char_u *line; /* current line */
2636 char_u *ptr; /* current position in "line" */
2637 int row; /* row in the window, excl w_winrow */
2638 int screen_row; /* row on the screen, incl w_winrow */
2639
2640 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2641 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002642 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 int c_extra = NUL; /* extra chars, all the same */
2644 int extra_attr = 0; /* attributes when n_extra != 0 */
2645 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2646 displaying lcs_eol at end-of-line */
2647 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2648 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2649
2650 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2651 int saved_n_extra = 0;
2652 char_u *saved_p_extra = NULL;
2653 int saved_c_extra = 0;
2654 int saved_char_attr = 0;
2655
2656 int n_attr = 0; /* chars with special attr */
2657 int saved_attr2 = 0; /* char_attr saved for n_attr */
2658 int n_attr3 = 0; /* chars with overruling special attr */
2659 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2660
2661 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2662
2663 int fromcol, tocol; /* start/end of inverting */
2664 int fromcol_prev = -2; /* start of inverting after cursor */
2665 int noinvcur = FALSE; /* don't invert the cursor */
2666#ifdef FEAT_VISUAL
2667 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002668 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669#endif
2670 pos_T pos;
2671 long v;
2672
2673 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002674 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2676 in this line */
2677 int attr = 0; /* attributes for area highlighting */
2678 int area_attr = 0; /* attributes desired by highlighting */
2679 int search_attr = 0; /* attributes desired by 'hlsearch' */
2680#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002681 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 int syntax_attr = 0; /* attributes desired by syntax */
2683 int has_syntax = FALSE; /* this buffer has syntax highl. */
2684 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002685 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002686 int draw_color_col = FALSE; /* highlight colorcolumn */
2687 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002688#endif
2689#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002690 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002691# define SPWORDLEN 150
2692 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002693 int nextlinecol = 0; /* column where nextline[] starts */
2694 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002695 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002696 int spell_attr = 0; /* attributes desired by spelling */
2697 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002698 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2699 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002700 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002701 static int cap_col = -1; /* column to check for Cap word */
2702 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002703 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#endif
2705 int extra_check; /* has syntax or linebreak */
2706#ifdef FEAT_MBYTE
2707 int multi_attr = 0; /* attributes desired by multibyte */
2708 int mb_l = 1; /* multi-byte byte length */
2709 int mb_c = 0; /* decoded multi-byte character */
2710 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002711 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712#endif
2713#ifdef FEAT_DIFF
2714 int filler_lines; /* nr of filler lines to be drawn */
2715 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002716 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 int change_start = MAXCOL; /* first col of changed area */
2718 int change_end = -1; /* last col of changed area */
2719#endif
2720 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2721#ifdef FEAT_LINEBREAK
2722 int need_showbreak = FALSE;
2723#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002724#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2725 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002727 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#endif
2729#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002730 matchitem_T *cur; /* points to the match list */
2731 match_T *shl; /* points to search_hl or a match */
2732 int shl_flag; /* flag to indicate whether search_hl
2733 has been processed or not */
2734 int prevcol_hl_flag; /* flag to indicate whether prevcol
2735 equals startcol of search_hl or one
2736 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737#endif
2738#ifdef FEAT_ARABIC
2739 int prev_c = 0; /* previous Arabic character */
2740 int prev_c1 = 0; /* first composing char for prev_c */
2741#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002742#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002743 int did_line_attr = 0;
2744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 /* draw_state: items that are drawn in sequence: */
2747#define WL_START 0 /* nothing done yet */
2748#ifdef FEAT_CMDWIN
2749# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2750#else
2751# define WL_CMDLINE WL_START
2752#endif
2753#ifdef FEAT_FOLDING
2754# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2755#else
2756# define WL_FOLD WL_CMDLINE
2757#endif
2758#ifdef FEAT_SIGNS
2759# define WL_SIGN WL_FOLD + 1 /* column for signs */
2760#else
2761# define WL_SIGN WL_FOLD /* column for signs */
2762#endif
2763#define WL_NR WL_SIGN + 1 /* line number */
2764#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2765# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2766#else
2767# define WL_SBR WL_NR
2768#endif
2769#define WL_LINE WL_SBR + 1 /* text in the line */
2770 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002771#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 int feedback_col = 0;
2773 int feedback_old_attr = -1;
2774#endif
2775
Bram Moolenaar860cae12010-06-05 23:22:07 +02002776#ifdef FEAT_CONCEAL
2777 int syntax_flags = 0;
2778 int conceal_attr = hl_attr(HLF_CONCEAL);
2779 int first_conceal = (wp->w_p_conceal != 3);
2780 int is_concealing = FALSE;
2781 int boguscols = 0; /* nonexistent columns added to force
2782 wrapping */
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002783 int vcol_off = 0; /* offset for concealed characters */
2784# define VCOL_HLC (vcol - vcol_off)
2785#else
2786# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 if (startrow > endrow) /* past the end already! */
2790 return startrow;
2791
2792 row = startrow;
2793 screen_row = row + W_WINROW(wp);
2794
2795 /*
2796 * To speed up the loop below, set extra_check when there is linebreak,
2797 * trailing white space and/or syntax processing to be done.
2798 */
2799#ifdef FEAT_LINEBREAK
2800 extra_check = wp->w_p_lbr;
2801#else
2802 extra_check = 0;
2803#endif
2804#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002805 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
2807 /* Prepare for syntax highlighting in this line. When there is an
2808 * error, stop syntax highlighting. */
2809 save_did_emsg = did_emsg;
2810 did_emsg = FALSE;
2811 syntax_start(wp, lnum);
2812 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002813 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 else
2815 {
2816 did_emsg = save_did_emsg;
2817 has_syntax = TRUE;
2818 extra_check = TRUE;
2819 }
2820 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002821
2822 /* Check for columns to display for 'colorcolumn'. */
2823 color_cols = wp->w_p_cc_cols;
2824 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002825 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002826#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002827
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002828#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002829 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002830 && *wp->w_s->b_p_spl != NUL
2831 && wp->w_s->b_langp.ga_len > 0
2832 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002833 {
2834 /* Prepare for spell checking. */
2835 has_spell = TRUE;
2836 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002837
2838 /* Get the start of the next line, so that words that wrap to the next
2839 * line are found too: "et<line-break>al.".
2840 * Trick: skip a few chars for C/shell/Vim comments */
2841 nextline[SPWORDLEN] = NUL;
2842 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2843 {
2844 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2845 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2846 }
2847
2848 /* When a word wrapped from the previous line the start of the current
2849 * line is valid. */
2850 if (lnum == checked_lnum)
2851 cur_checked_col = checked_col;
2852 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002853
2854 /* When there was a sentence end in the previous line may require a
2855 * word starting with capital in this line. In line 1 always check
2856 * the first word. */
2857 if (lnum != capcol_lnum)
2858 cap_col = -1;
2859 if (lnum == 1)
2860 cap_col = 0;
2861 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863#endif
2864
2865 /*
2866 * handle visual active in this window
2867 */
2868 fromcol = -10;
2869 tocol = MAXCOL;
2870#ifdef FEAT_VISUAL
2871 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2872 {
2873 /* Visual is after curwin->w_cursor */
2874 if (ltoreq(curwin->w_cursor, VIsual))
2875 {
2876 top = &curwin->w_cursor;
2877 bot = &VIsual;
2878 }
2879 else /* Visual is before curwin->w_cursor */
2880 {
2881 top = &VIsual;
2882 bot = &curwin->w_cursor;
2883 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002884 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 if (VIsual_mode == Ctrl_V) /* block mode */
2886 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002887 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
2889 fromcol = wp->w_old_cursor_fcol;
2890 tocol = wp->w_old_cursor_lcol;
2891 }
2892 }
2893 else /* non-block mode */
2894 {
2895 if (lnum > top->lnum && lnum <= bot->lnum)
2896 fromcol = 0;
2897 else if (lnum == top->lnum)
2898 {
2899 if (VIsual_mode == 'V') /* linewise */
2900 fromcol = 0;
2901 else
2902 {
2903 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2904 if (gchar_pos(top) == NUL)
2905 tocol = fromcol + 1;
2906 }
2907 }
2908 if (VIsual_mode != 'V' && lnum == bot->lnum)
2909 {
2910 if (*p_sel == 'e' && bot->col == 0
2911#ifdef FEAT_VIRTUALEDIT
2912 && bot->coladd == 0
2913#endif
2914 )
2915 {
2916 fromcol = -10;
2917 tocol = MAXCOL;
2918 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002919 else if (bot->col == MAXCOL)
2920 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 else
2922 {
2923 pos = *bot;
2924 if (*p_sel == 'e')
2925 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2926 else
2927 {
2928 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2929 ++tocol;
2930 }
2931 }
2932 }
2933 }
2934
2935#ifndef MSDOS
2936 /* Check if the character under the cursor should not be inverted */
2937 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2938# ifdef FEAT_GUI
2939 && !gui.in_use
2940# endif
2941 )
2942 noinvcur = TRUE;
2943#endif
2944
2945 /* if inverting in this line set area_highlighting */
2946 if (fromcol >= 0)
2947 {
2948 area_highlighting = TRUE;
2949 attr = hl_attr(HLF_V);
2950#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2951 if (clip_star.available && !clip_star.owned && clip_isautosel())
2952 attr = hl_attr(HLF_VNC);
2953#endif
2954 }
2955 }
2956
2957 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002958 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 */
2960 else
2961#endif /* FEAT_VISUAL */
2962 if (highlight_match
2963 && wp == curwin
2964 && lnum >= curwin->w_cursor.lnum
2965 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2966 {
2967 if (lnum == curwin->w_cursor.lnum)
2968 getvcol(curwin, &(curwin->w_cursor),
2969 (colnr_T *)&fromcol, NULL, NULL);
2970 else
2971 fromcol = 0;
2972 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2973 {
2974 pos.lnum = lnum;
2975 pos.col = search_match_endcol;
2976 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2977 }
2978 else
2979 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00002980 /* do at least one character; happens when past end of line */
2981 if (fromcol == tocol)
2982 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 area_highlighting = TRUE;
2984 attr = hl_attr(HLF_I);
2985 }
2986
2987#ifdef FEAT_DIFF
2988 filler_lines = diff_check(wp, lnum);
2989 if (filler_lines < 0)
2990 {
2991 if (filler_lines == -1)
2992 {
2993 if (diff_find_change(wp, lnum, &change_start, &change_end))
2994 diff_hlf = HLF_ADD; /* added line */
2995 else if (change_start == 0)
2996 diff_hlf = HLF_TXD; /* changed text */
2997 else
2998 diff_hlf = HLF_CHD; /* changed line */
2999 }
3000 else
3001 diff_hlf = HLF_ADD; /* added line */
3002 filler_lines = 0;
3003 area_highlighting = TRUE;
3004 }
3005 if (lnum == wp->w_topline)
3006 filler_lines = wp->w_topfill;
3007 filler_todo = filler_lines;
3008#endif
3009
3010#ifdef LINE_ATTR
3011# ifdef FEAT_SIGNS
3012 /* If this line has a sign with line highlighting set line_attr. */
3013 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3014 if (v != 0)
3015 line_attr = sign_get_attr((int)v, TRUE);
3016# endif
3017# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3018 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003019 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 line_attr = hl_attr(HLF_L);
3021# endif
3022 if (line_attr != 0)
3023 area_highlighting = TRUE;
3024#endif
3025
3026 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3027 ptr = line;
3028
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003029#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003030 if (has_spell)
3031 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003032 /* For checking first word with a capital skip white space. */
3033 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003034 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003035
Bram Moolenaar30abd282005-06-22 22:35:10 +00003036 /* To be able to spell-check over line boundaries copy the end of the
3037 * current line into nextline[]. Above the start of the next line was
3038 * copied to nextline[SPWORDLEN]. */
3039 if (nextline[SPWORDLEN] == NUL)
3040 {
3041 /* No next line or it is empty. */
3042 nextlinecol = MAXCOL;
3043 nextline_idx = 0;
3044 }
3045 else
3046 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003047 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003048 if (v < SPWORDLEN)
3049 {
3050 /* Short line, use it completely and append the start of the
3051 * next line. */
3052 nextlinecol = 0;
3053 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003054 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003055 nextline_idx = v + 1;
3056 }
3057 else
3058 {
3059 /* Long line, use only the last SPWORDLEN bytes. */
3060 nextlinecol = v - SPWORDLEN;
3061 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3062 nextline_idx = SPWORDLEN + 1;
3063 }
3064 }
3065 }
3066#endif
3067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 /* find start of trailing whitespace */
3069 if (wp->w_p_list && lcs_trail)
3070 {
3071 trailcol = (colnr_T)STRLEN(ptr);
3072 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3073 --trailcol;
3074 trailcol += (colnr_T) (ptr - line);
3075 extra_check = TRUE;
3076 }
3077
3078 /*
3079 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3080 * first character to be displayed.
3081 */
3082 if (wp->w_p_wrap)
3083 v = wp->w_skipcol;
3084 else
3085 v = wp->w_leftcol;
3086 if (v > 0)
3087 {
3088#ifdef FEAT_MBYTE
3089 char_u *prev_ptr = ptr;
3090#endif
3091 while (vcol < v && *ptr != NUL)
3092 {
3093 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3094 vcol += c;
3095#ifdef FEAT_MBYTE
3096 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003098 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
3100
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003101#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3102 /* When:
3103 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003104 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003105 * - 'virtualedit' is set, or
3106 * - the visual mode is active,
3107 * the end of the line may be before the start of the displayed part.
3108 */
3109 if (vcol < v && (
3110# ifdef FEAT_SYN_HL
3111 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003112 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003113# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3114 ||
3115# endif
3116# endif
3117# ifdef FEAT_VIRTUALEDIT
3118 virtual_active()
3119# ifdef FEAT_VISUAL
3120 ||
3121# endif
3122# endif
3123# ifdef FEAT_VISUAL
3124 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3125# endif
3126 ))
3127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130#endif
3131
3132 /* Handle a character that's not completely on the screen: Put ptr at
3133 * that character but skip the first few screen characters. */
3134 if (vcol > v)
3135 {
3136 vcol -= c;
3137#ifdef FEAT_MBYTE
3138 ptr = prev_ptr;
3139#else
3140 --ptr;
3141#endif
3142 n_skip = v - vcol;
3143 }
3144
3145 /*
3146 * Adjust for when the inverted text is before the screen,
3147 * and when the start of the inverted text is before the screen.
3148 */
3149 if (tocol <= vcol)
3150 fromcol = 0;
3151 else if (fromcol >= 0 && fromcol < vcol)
3152 fromcol = vcol;
3153
3154#ifdef FEAT_LINEBREAK
3155 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3156 if (wp->w_p_wrap)
3157 need_showbreak = TRUE;
3158#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003159#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003160 /* When spell checking a word we need to figure out the start of the
3161 * word and if it's badly spelled or not. */
3162 if (has_spell)
3163 {
3164 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003165 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003166 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003167
3168 pos = wp->w_cursor;
3169 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003170 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003171 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003172
3173 /* spell_move_to() may call ml_get() and make "line" invalid */
3174 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3175 ptr = line + linecol;
3176
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003177 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003178 {
3179 /* no bad word found at line start, don't check until end of a
3180 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003181 spell_hlf = HLF_COUNT;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003182 word_end = (int)(spell_to_word_end(ptr, wp)
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003183 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003184 }
3185 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003186 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003187 /* bad word found, use attributes until end of word */
3188 word_end = wp->w_cursor.col + len + 1;
3189
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003190 /* Turn index into actual attributes. */
3191 if (spell_hlf != HLF_COUNT)
3192 spell_attr = highlight_attr[spell_hlf];
3193 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003194 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003195
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003196# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003197 /* Need to restart syntax highlighting for this line. */
3198 if (has_syntax)
3199 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003200# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003201 }
3202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204
3205 /*
3206 * Correct highlighting for cursor that can't be disabled.
3207 * Avoids having to check this for each character.
3208 */
3209 if (fromcol >= 0)
3210 {
3211 if (noinvcur)
3212 {
3213 if ((colnr_T)fromcol == wp->w_virtcol)
3214 {
3215 /* highlighting starts at cursor, let it start just after the
3216 * cursor */
3217 fromcol_prev = fromcol;
3218 fromcol = -1;
3219 }
3220 else if ((colnr_T)fromcol < wp->w_virtcol)
3221 /* restart highlighting after the cursor */
3222 fromcol_prev = wp->w_virtcol;
3223 }
3224 if (fromcol >= tocol)
3225 fromcol = -1;
3226 }
3227
3228#ifdef FEAT_SEARCH_EXTRA
3229 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003230 * Handle highlighting the last used search pattern and matches.
3231 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003233 cur = wp->w_match_head;
3234 shl_flag = FALSE;
3235 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003237 if (shl_flag == FALSE)
3238 {
3239 shl = &search_hl;
3240 shl_flag = TRUE;
3241 }
3242 else
3243 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003244 shl->startcol = MAXCOL;
3245 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 shl->attr_cur = 0;
3247 if (shl->rm.regprog != NULL)
3248 {
3249 v = (long)(ptr - line);
3250 next_search_hl(wp, shl, lnum, (colnr_T)v);
3251
3252 /* Need to get the line again, a multi-line regexp may have made it
3253 * invalid. */
3254 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3255 ptr = line + v;
3256
3257 if (shl->lnum != 0 && shl->lnum <= lnum)
3258 {
3259 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003260 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003262 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3264 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003265 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003267 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003269 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003272 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003273 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 else
3275#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003276 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003278 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
3280 shl->attr_cur = shl->attr;
3281 search_attr = shl->attr;
3282 }
3283 area_highlighting = TRUE;
3284 }
3285 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003286 if (shl != &search_hl && cur != NULL)
3287 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
3289#endif
3290
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003291#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003292 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3293 * active, because it's not clear what is selected then. */
3294 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003295 {
3296 line_attr = hl_attr(HLF_CUL);
3297 area_highlighting = TRUE;
3298 }
3299#endif
3300
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003301 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 col = 0;
3303#ifdef FEAT_RIGHTLEFT
3304 if (wp->w_p_rl)
3305 {
3306 /* Rightleft window: process the text in the normal direction, but put
3307 * it in current_ScreenLine[] from right to left. Start at the
3308 * rightmost column of the window. */
3309 col = W_WIDTH(wp) - 1;
3310 off += col;
3311 }
3312#endif
3313
3314 /*
3315 * Repeat for the whole displayed line.
3316 */
3317 for (;;)
3318 {
3319 /* Skip this quickly when working on the text. */
3320 if (draw_state != WL_LINE)
3321 {
3322#ifdef FEAT_CMDWIN
3323 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3324 {
3325 draw_state = WL_CMDLINE;
3326 if (cmdwin_type != 0 && wp == curwin)
3327 {
3328 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003330 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 char_attr = hl_attr(HLF_AT);
3332 }
3333 }
3334#endif
3335
3336#ifdef FEAT_FOLDING
3337 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3338 {
3339 draw_state = WL_FOLD;
3340 if (wp->w_p_fdc > 0)
3341 {
3342 /* Draw the 'foldcolumn'. */
3343 fill_foldcolumn(extra, wp, FALSE, lnum);
3344 n_extra = wp->w_p_fdc;
3345 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003346 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 c_extra = NUL;
3348 char_attr = hl_attr(HLF_FC);
3349 }
3350 }
3351#endif
3352
3353#ifdef FEAT_SIGNS
3354 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3355 {
3356 draw_state = WL_SIGN;
3357 /* Show the sign column when there are any signs in this
3358 * buffer or when using Netbeans. */
3359 if (draw_signcolumn(wp)
3360# ifdef FEAT_DIFF
3361 && filler_todo <= 0
3362# endif
3363 )
3364 {
3365 int_u text_sign;
3366# ifdef FEAT_SIGN_ICONS
3367 int_u icon_sign;
3368# endif
3369
3370 /* Draw two cells with the sign value or blank. */
3371 c_extra = ' ';
3372 char_attr = hl_attr(HLF_SC);
3373 n_extra = 2;
3374
3375 if (row == startrow)
3376 {
3377 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3378 SIGN_TEXT);
3379# ifdef FEAT_SIGN_ICONS
3380 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3381 SIGN_ICON);
3382 if (gui.in_use && icon_sign != 0)
3383 {
3384 /* Use the image in this position. */
3385 c_extra = SIGN_BYTE;
3386# ifdef FEAT_NETBEANS_INTG
3387 if (buf_signcount(wp->w_buffer, lnum) > 1)
3388 c_extra = MULTISIGN_BYTE;
3389# endif
3390 char_attr = icon_sign;
3391 }
3392 else
3393# endif
3394 if (text_sign != 0)
3395 {
3396 p_extra = sign_get_text(text_sign);
3397 if (p_extra != NULL)
3398 {
3399 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003400 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402 char_attr = sign_get_attr(text_sign, FALSE);
3403 }
3404 }
3405 }
3406 }
3407#endif
3408
3409 if (draw_state == WL_NR - 1 && n_extra == 0)
3410 {
3411 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003412 /* Display the absolute or relative line number. After the
3413 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3414 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 && (row == startrow
3416#ifdef FEAT_DIFF
3417 + filler_lines
3418#endif
3419 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3420 {
3421 /* Draw the line number (empty space after wrapping). */
3422 if (row == startrow
3423#ifdef FEAT_DIFF
3424 + filler_lines
3425#endif
3426 )
3427 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003428 long num;
3429
3430 if (wp->w_p_nu)
3431 /* 'number' */
3432 num = (long)lnum;
3433 else
3434 /* 'relativenumber', don't use negative numbers */
3435 num = (long)abs((int)get_cursor_rel_lnum(wp,
3436 lnum));
3437
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003438 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003439 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (wp->w_skipcol > 0)
3441 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3442 *p_extra = '-';
3443#ifdef FEAT_RIGHTLEFT
3444 if (wp->w_p_rl) /* reverse line numbers */
3445 rl_mirror(extra);
3446#endif
3447 p_extra = extra;
3448 c_extra = NUL;
3449 }
3450 else
3451 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003452 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003454#ifdef FEAT_SYN_HL
3455 /* When 'cursorline' is set highlight the line number of
3456 * the current line differently. */
3457 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3458 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
3461 }
3462
3463#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3464 if (draw_state == WL_SBR - 1 && n_extra == 0)
3465 {
3466 draw_state = WL_SBR;
3467# ifdef FEAT_DIFF
3468 if (filler_todo > 0)
3469 {
3470 /* Draw "deleted" diff line(s). */
3471 if (char2cells(fill_diff) > 1)
3472 c_extra = '-';
3473 else
3474 c_extra = fill_diff;
3475# ifdef FEAT_RIGHTLEFT
3476 if (wp->w_p_rl)
3477 n_extra = col + 1;
3478 else
3479# endif
3480 n_extra = W_WIDTH(wp) - col;
3481 char_attr = hl_attr(HLF_DED);
3482 }
3483# endif
3484# ifdef FEAT_LINEBREAK
3485 if (*p_sbr != NUL && need_showbreak)
3486 {
3487 /* Draw 'showbreak' at the start of each broken line. */
3488 p_extra = p_sbr;
3489 c_extra = NUL;
3490 n_extra = (int)STRLEN(p_sbr);
3491 char_attr = hl_attr(HLF_AT);
3492 need_showbreak = FALSE;
3493 /* Correct end of highlighted area for 'showbreak',
3494 * required when 'linebreak' is also set. */
3495 if (tocol == vcol)
3496 tocol += n_extra;
3497 }
3498# endif
3499 }
3500#endif
3501
3502 if (draw_state == WL_LINE - 1 && n_extra == 0)
3503 {
3504 draw_state = WL_LINE;
3505 if (saved_n_extra)
3506 {
3507 /* Continue item from end of wrapped line. */
3508 n_extra = saved_n_extra;
3509 c_extra = saved_c_extra;
3510 p_extra = saved_p_extra;
3511 char_attr = saved_char_attr;
3512 }
3513 else
3514 char_attr = 0;
3515 }
3516 }
3517
3518 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003519 if (dollar_vcol != 0 && wp == curwin
3520 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521#ifdef FEAT_DIFF
3522 && filler_todo <= 0
3523#endif
3524 )
3525 {
3526 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3527 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003528 /* Pretend we have finished updating the window. Except when
3529 * 'cursorcolumn' is set. */
3530#ifdef FEAT_SYN_HL
3531 if (wp->w_p_cuc)
3532 row = wp->w_cline_row + wp->w_cline_height;
3533 else
3534#endif
3535 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 break;
3537 }
3538
3539 if (draw_state == WL_LINE && area_highlighting)
3540 {
3541 /* handle Visual or match highlighting in this line */
3542 if (vcol == fromcol
3543#ifdef FEAT_MBYTE
3544 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3545 && (*mb_ptr2cells)(ptr) > 1)
3546#endif
3547 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003548 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 && vcol < tocol))
3550 area_attr = attr; /* start highlighting */
3551 else if (area_attr != 0
3552 && (vcol == tocol
3553 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556#ifdef FEAT_SEARCH_EXTRA
3557 if (!n_extra)
3558 {
3559 /*
3560 * Check for start/end of search pattern match.
3561 * After end, check for start/end of next match.
3562 * When another match, have to check for start again.
3563 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003564 * Do this for 'search_hl' and the match list (ordered by
3565 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003567 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003568 cur = wp->w_match_head;
3569 shl_flag = FALSE;
3570 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003572 if (shl_flag == FALSE
3573 && ((cur != NULL
3574 && cur->priority > SEARCH_HL_PRIORITY)
3575 || cur == NULL))
3576 {
3577 shl = &search_hl;
3578 shl_flag = TRUE;
3579 }
3580 else
3581 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 while (shl->rm.regprog != NULL)
3583 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003584 if (shl->startcol != MAXCOL
3585 && v >= (long)shl->startcol
3586 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 shl->attr_cur = shl->attr;
3589 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003590 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 shl->attr_cur = 0;
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 next_search_hl(wp, shl, lnum, (colnr_T)v);
3595
3596 /* Need to get the line again, a multi-line regexp
3597 * may have made it invalid. */
3598 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3599 ptr = line + v;
3600
3601 if (shl->lnum == lnum)
3602 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003603 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003605 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003607 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003609 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
3611 /* highlight empty match, try again after
3612 * it */
3613#ifdef FEAT_MBYTE
3614 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003615 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003616 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 else
3618#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003619 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
3621
3622 /* Loop to check if the match starts at the
3623 * current position */
3624 continue;
3625 }
3626 }
3627 break;
3628 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003629 if (shl != &search_hl && cur != NULL)
3630 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003632
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003633 /* Use attributes from match with highest priority among
3634 * 'search_hl' and the match list. */
3635 search_attr = search_hl.attr_cur;
3636 cur = wp->w_match_head;
3637 shl_flag = FALSE;
3638 while (cur != NULL || shl_flag == FALSE)
3639 {
3640 if (shl_flag == FALSE
3641 && ((cur != NULL
3642 && cur->priority > SEARCH_HL_PRIORITY)
3643 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003644 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003645 shl = &search_hl;
3646 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003647 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003648 else
3649 shl = &cur->hl;
3650 if (shl->attr_cur != 0)
3651 search_attr = shl->attr_cur;
3652 if (shl != &search_hl && cur != NULL)
3653 cur = cur->next;
3654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656#endif
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003659 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003661 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3662 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003664 if (diff_hlf == HLF_TXD && ptr - line > change_end
3665 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003667 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003670
3671 /* Decide which of the highlight attributes to use. */
3672 attr_pri = TRUE;
3673 if (area_attr != 0)
3674 char_attr = area_attr;
3675 else if (search_attr != 0)
3676 char_attr = search_attr;
3677#ifdef LINE_ATTR
3678 /* Use line_attr when not in the Visual or 'incsearch' area
3679 * (area_attr may be 0 when "noinvcur" is set). */
3680 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003681 || vcol < fromcol || vcol_prev < fromcol_prev
3682 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003683 char_attr = line_attr;
3684#endif
3685 else
3686 {
3687 attr_pri = FALSE;
3688#ifdef FEAT_SYN_HL
3689 if (has_syntax)
3690 char_attr = syntax_attr;
3691 else
3692#endif
3693 char_attr = 0;
3694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696
3697 /*
3698 * Get the next character to put on the screen.
3699 */
3700 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003701 * The "p_extra" points to the extra stuff that is inserted to
3702 * represent special characters (non-printable stuff) and other
3703 * things. When all characters are the same, c_extra is used.
3704 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3705 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3707 */
3708 if (n_extra > 0)
3709 {
3710 if (c_extra != NUL)
3711 {
3712 c = c_extra;
3713#ifdef FEAT_MBYTE
3714 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3715 if (enc_utf8 && (*mb_char2len)(c) > 1)
3716 {
3717 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003718 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003719 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721 else
3722 mb_utf8 = FALSE;
3723#endif
3724 }
3725 else
3726 {
3727 c = *p_extra;
3728#ifdef FEAT_MBYTE
3729 if (has_mbyte)
3730 {
3731 mb_c = c;
3732 if (enc_utf8)
3733 {
3734 /* If the UTF-8 character is more than one byte:
3735 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003736 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 mb_utf8 = FALSE;
3738 if (mb_l > n_extra)
3739 mb_l = 1;
3740 else if (mb_l > 1)
3741 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003742 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003744 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746 }
3747 else
3748 {
3749 /* if this is a DBCS character, put it in "mb_c" */
3750 mb_l = MB_BYTE2LEN(c);
3751 if (mb_l >= n_extra)
3752 mb_l = 1;
3753 else if (mb_l > 1)
3754 mb_c = (c << 8) + p_extra[1];
3755 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003756 if (mb_l == 0) /* at the NUL at end-of-line */
3757 mb_l = 1;
3758
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 /* If a double-width char doesn't fit display a '>' in the
3760 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003761 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762# ifdef FEAT_RIGHTLEFT
3763 wp->w_p_rl ? (col <= 0) :
3764# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003765 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 && (*mb_char2cells)(mb_c) == 2)
3767 {
3768 c = '>';
3769 mb_c = c;
3770 mb_l = 1;
3771 mb_utf8 = FALSE;
3772 multi_attr = hl_attr(HLF_AT);
3773 /* put the pointer back to output the double-width
3774 * character at the start of the next line. */
3775 ++n_extra;
3776 --p_extra;
3777 }
3778 else
3779 {
3780 n_extra -= mb_l - 1;
3781 p_extra += mb_l - 1;
3782 }
3783 }
3784#endif
3785 ++p_extra;
3786 }
3787 --n_extra;
3788 }
3789 else
3790 {
3791 /*
3792 * Get a character from the line itself.
3793 */
3794 c = *ptr;
3795#ifdef FEAT_MBYTE
3796 if (has_mbyte)
3797 {
3798 mb_c = c;
3799 if (enc_utf8)
3800 {
3801 /* If the UTF-8 character is more than one byte: Decode it
3802 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003803 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 mb_utf8 = FALSE;
3805 if (mb_l > 1)
3806 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003807 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 /* Overlong encoded ASCII or ASCII with composing char
3809 * is displayed normally, except a NUL. */
3810 if (mb_c < 0x80)
3811 c = mb_c;
3812 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003813
3814 /* At start of the line we can have a composing char.
3815 * Draw it as a space with a composing char. */
3816 if (utf_iscomposing(mb_c))
3817 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003818 int i;
3819
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003820 for (i = Screen_mco - 1; i > 0; --i)
3821 u8cc[i] = u8cc[i - 1];
3822 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003823 mb_c = ' ';
3824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 }
3826
3827 if ((mb_l == 1 && c >= 0x80)
3828 || (mb_l >= 1 && mb_c == 0)
3829 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003830# ifdef UNICODE16
3831 || mb_c >= 0x10000
3832# endif
3833 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
3835 /*
3836 * Illegal UTF-8 byte: display as <xx>.
3837 * Non-BMP character : display as ? or fullwidth ?.
3838 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003839# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
3843 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003844# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 if (wp->w_p_rl) /* reverse */
3846 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003849# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 else if (utf_char2cells(mb_c) != 2)
3851 STRCPY(extra, "?");
3852 else
3853 /* 0xff1f in UTF-8: full-width '?' */
3854 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
3857 p_extra = extra;
3858 c = *p_extra;
3859 mb_c = mb_ptr2char_adv(&p_extra);
3860 mb_utf8 = (c >= 0x80);
3861 n_extra = (int)STRLEN(p_extra);
3862 c_extra = NUL;
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 }
3870 else if (mb_l == 0) /* at the NUL at end-of-line */
3871 mb_l = 1;
3872#ifdef FEAT_ARABIC
3873 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3874 {
3875 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003876 int pc, pc1, nc;
3877 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
3879 /* The idea of what is the previous and next
3880 * character depends on 'rightleft'. */
3881 if (wp->w_p_rl)
3882 {
3883 pc = prev_c;
3884 pc1 = prev_c1;
3885 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003886 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
3888 else
3889 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003890 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003892 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 prev_c = mb_c;
3895
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003896 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898 else
3899 prev_c = mb_c;
3900#endif
3901 }
3902 else /* enc_dbcs */
3903 {
3904 mb_l = MB_BYTE2LEN(c);
3905 if (mb_l == 0) /* at the NUL at end-of-line */
3906 mb_l = 1;
3907 else if (mb_l > 1)
3908 {
3909 /* We assume a second byte below 32 is illegal.
3910 * Hopefully this is OK for all double-byte encodings!
3911 */
3912 if (ptr[1] >= 32)
3913 mb_c = (c << 8) + ptr[1];
3914 else
3915 {
3916 if (ptr[1] == NUL)
3917 {
3918 /* head byte at end of line */
3919 mb_l = 1;
3920 transchar_nonprint(extra, c);
3921 }
3922 else
3923 {
3924 /* illegal tail byte */
3925 mb_l = 2;
3926 STRCPY(extra, "XX");
3927 }
3928 p_extra = extra;
3929 n_extra = (int)STRLEN(extra) - 1;
3930 c_extra = NUL;
3931 c = *p_extra++;
3932 if (area_attr == 0 && search_attr == 0)
3933 {
3934 n_attr = n_extra + 1;
3935 extra_attr = hl_attr(HLF_8);
3936 saved_attr2 = char_attr; /* save current attr */
3937 }
3938 mb_c = c;
3939 }
3940 }
3941 }
3942 /* If a double-width char doesn't fit display a '>' in the
3943 * last column; the character is displayed at the start of the
3944 * next line. */
3945 if ((
3946# ifdef FEAT_RIGHTLEFT
3947 wp->w_p_rl ? (col <= 0) :
3948# endif
3949 (col >= W_WIDTH(wp) - 1))
3950 && (*mb_char2cells)(mb_c) == 2)
3951 {
3952 c = '>';
3953 mb_c = c;
3954 mb_utf8 = FALSE;
3955 mb_l = 1;
3956 multi_attr = hl_attr(HLF_AT);
3957 /* Put pointer back so that the character will be
3958 * displayed at the start of the next line. */
3959 --ptr;
3960 }
3961 else if (*ptr != NUL)
3962 ptr += mb_l - 1;
3963
3964 /* If a double-width char doesn't fit at the left side display
3965 * a '<' in the first column. */
3966 if (n_skip > 0 && mb_l > 1)
3967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003969 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 c = ' ';
3971 if (area_attr == 0 && search_attr == 0)
3972 {
3973 n_attr = n_extra + 1;
3974 extra_attr = hl_attr(HLF_AT);
3975 saved_attr2 = char_attr; /* save current attr */
3976 }
3977 mb_c = c;
3978 mb_utf8 = FALSE;
3979 mb_l = 1;
3980 }
3981
3982 }
3983#endif
3984 ++ptr;
3985
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003986 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003987 if (wp->w_p_list && (c == 160
3988#ifdef FEAT_MBYTE
3989 || (mb_utf8 && mb_c == 160)
3990#endif
3991 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003992 {
3993 c = lcs_nbsp;
3994 if (area_attr == 0 && search_attr == 0)
3995 {
3996 n_attr = 1;
3997 extra_attr = hl_attr(HLF_8);
3998 saved_attr2 = char_attr; /* save current attr */
3999 }
4000#ifdef FEAT_MBYTE
4001 mb_c = c;
4002 if (enc_utf8 && (*mb_char2len)(c) > 1)
4003 {
4004 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004005 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004006 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004007 }
4008 else
4009 mb_utf8 = FALSE;
4010#endif
4011 }
4012
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (extra_check)
4014 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004015#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004016 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004017#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004018
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004019#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 /* Get syntax attribute, unless still at the start of the line
4021 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004022 v = (long)(ptr - line);
4023 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
4025 /* Get the syntax attribute for the character. If there
4026 * is an error, disable syntax highlighting. */
4027 save_did_emsg = did_emsg;
4028 did_emsg = FALSE;
4029
Bram Moolenaar217ad922005-03-20 22:37:15 +00004030 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004031# ifdef FEAT_CONCEAL
4032 &syntax_flags,
4033# else
4034 NULL,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004035# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004036# ifdef FEAT_SPELL
4037 has_spell ? &can_spell :
4038# endif
4039 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004042 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004043 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004044 has_syntax = FALSE;
4045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 else
4047 did_emsg = save_did_emsg;
4048
4049 /* Need to get the line again, a multi-line regexp may
4050 * have made it invalid. */
4051 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4052 ptr = line + v;
4053
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004054 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004056 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004057 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004059#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004060
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004061#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004062 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004063 * Only do this when there is no syntax highlighting, the
4064 * @Spell cluster is not used or the current syntax item
4065 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004066 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004067 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004068 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004069# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004070 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004071 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004072# endif
4073 if (c != 0 && (
4074# ifdef FEAT_SYN_HL
4075 !has_syntax ||
4076# endif
4077 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004078 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004079 char_u *prev_ptr, *p;
4080 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004081 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004082# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004083 if (has_mbyte)
4084 {
4085 prev_ptr = ptr - mb_l;
4086 v -= mb_l - 1;
4087 }
4088 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004089# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004090 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004091
4092 /* Use nextline[] if possible, it has the start of the
4093 * next line concatenated. */
4094 if ((prev_ptr - line) - nextlinecol >= 0)
4095 p = nextline + (prev_ptr - line) - nextlinecol;
4096 else
4097 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004098 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004099 len = spell_check(wp, p, &spell_hlf, &cap_col,
4100 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004101 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004102
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004103 /* In Insert mode only highlight a word that
4104 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004105 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004106 && (State & INSERT) != 0
4107 && wp->w_cursor.lnum == lnum
4108 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004109 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004110 && wp->w_cursor.col < (colnr_T)word_end)
4111 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004112 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004113 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004114 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004115
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004116 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004117 && (p - nextline) + len > nextline_idx)
4118 {
4119 /* Remember that the good word continues at the
4120 * start of the next line. */
4121 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004122 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004123 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004124
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004125 /* Turn index into actual attributes. */
4126 if (spell_hlf != HLF_COUNT)
4127 spell_attr = highlight_attr[spell_hlf];
4128
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004129 if (cap_col > 0)
4130 {
4131 if (p != prev_ptr
4132 && (p - nextline) + cap_col >= nextline_idx)
4133 {
4134 /* Remember that the word in the next line
4135 * must start with a capital. */
4136 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004137 cap_col = (int)((p - nextline) + cap_col
4138 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004139 }
4140 else
4141 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004142 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004143 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004144 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004145 }
4146 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004147 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004148 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004149 char_attr = hl_combine_attr(char_attr, spell_attr);
4150 else
4151 char_attr = hl_combine_attr(spell_attr, char_attr);
4152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#endif
4154#ifdef FEAT_LINEBREAK
4155 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004156 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 */
4158 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004159 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
4161 n_extra = win_lbr_chartabsize(wp, ptr - (
4162# ifdef FEAT_MBYTE
4163 has_mbyte ? mb_l :
4164# endif
4165 1), (colnr_T)vcol, NULL) - 1;
4166 c_extra = ' ';
4167 if (vim_iswhite(c))
4168 c = ' ';
4169 }
4170#endif
4171
4172 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4173 {
4174 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004175 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
4177 n_attr = 1;
4178 extra_attr = hl_attr(HLF_8);
4179 saved_attr2 = char_attr; /* save current attr */
4180 }
4181#ifdef FEAT_MBYTE
4182 mb_c = c;
4183 if (enc_utf8 && (*mb_char2len)(c) > 1)
4184 {
4185 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004186 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004187 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189 else
4190 mb_utf8 = FALSE;
4191#endif
4192 }
4193 }
4194
4195 /*
4196 * Handling of non-printable characters.
4197 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004198 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 {
4200 /*
4201 * when getting a character from the file, we may have to
4202 * turn it into something else on the way to putting it
4203 * into "ScreenLines".
4204 */
4205 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4206 {
4207 /* tab amount depends on current column */
4208 n_extra = (int)wp->w_buffer->b_p_ts
4209 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4210#ifdef FEAT_MBYTE
4211 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4212#endif
4213 if (wp->w_p_list)
4214 {
4215 c = lcs_tab1;
4216 c_extra = lcs_tab2;
4217 n_attr = n_extra + 1;
4218 extra_attr = hl_attr(HLF_8);
4219 saved_attr2 = char_attr; /* save current attr */
4220#ifdef FEAT_MBYTE
4221 mb_c = c;
4222 if (enc_utf8 && (*mb_char2len)(c) > 1)
4223 {
4224 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004225 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004226 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228#endif
4229 }
4230 else
4231 {
4232 c_extra = ' ';
4233 c = ' ';
4234 }
4235 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004236 else if (c == NUL
4237 && ((wp->w_p_list && lcs_eol > 0)
4238 || ((fromcol >= 0 || fromcol_prev >= 0)
4239 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004240#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004241 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004242#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004243 && (
4244# ifdef FEAT_RIGHTLEFT
4245 wp->w_p_rl ? (col >= 0) :
4246# endif
4247 (col < W_WIDTH(wp)))
4248 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004249 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004250 && (colnr_T)vcol == wp->w_virtcol)))
4251 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004253 /* Display a '$' after the line or highlight an extra
4254 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4256 /* For a diff line the highlighting continues after the
4257 * "$". */
4258 if (
4259# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004260 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261# ifdef LINE_ATTR
4262 &&
4263# endif
4264# endif
4265# ifdef LINE_ATTR
4266 line_attr == 0
4267# endif
4268 )
4269#endif
4270 {
4271#ifdef FEAT_VIRTUALEDIT
4272 /* In virtualedit, visual selections may extend
4273 * beyond end of line. */
4274 if (area_highlighting && virtual_active()
4275 && tocol != MAXCOL && vcol < tocol)
4276 n_extra = 0;
4277 else
4278#endif
4279 {
4280 p_extra = at_end_str;
4281 n_extra = 1;
4282 c_extra = NUL;
4283 }
4284 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004285 if (wp->w_p_list)
4286 c = lcs_eol;
4287 else
4288 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 lcs_eol_one = -1;
4290 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004291 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
4293 extra_attr = hl_attr(HLF_AT);
4294 n_attr = 1;
4295 }
4296#ifdef FEAT_MBYTE
4297 mb_c = c;
4298 if (enc_utf8 && (*mb_char2len)(c) > 1)
4299 {
4300 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004301 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004302 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 else
4305 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4306#endif
4307 }
4308 else if (c != NUL)
4309 {
4310 p_extra = transchar(c);
4311#ifdef FEAT_RIGHTLEFT
4312 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4313 rl_mirror(p_extra); /* reverse "<12>" */
4314#endif
4315 n_extra = byte2cells(c) - 1;
4316 c_extra = NUL;
4317 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004318 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 n_attr = n_extra + 1;
4321 extra_attr = hl_attr(HLF_8);
4322 saved_attr2 = char_attr; /* save current attr */
4323 }
4324#ifdef FEAT_MBYTE
4325 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4326#endif
4327 }
4328#ifdef FEAT_VIRTUALEDIT
4329 else if (VIsual_active
4330 && (VIsual_mode == Ctrl_V
4331 || VIsual_mode == 'v')
4332 && virtual_active()
4333 && tocol != MAXCOL
4334 && vcol < tocol
4335 && (
4336# ifdef FEAT_RIGHTLEFT
4337 wp->w_p_rl ? (col >= 0) :
4338# endif
4339 (col < W_WIDTH(wp))))
4340 {
4341 c = ' ';
4342 --ptr; /* put it back at the NUL */
4343 }
4344#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004345#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 else if ((
4347# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004348 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 ) && (
4352# ifdef FEAT_RIGHTLEFT
4353 wp->w_p_rl ? (col >= 0) :
4354# endif
4355 (col < W_WIDTH(wp))))
4356 {
4357 /* Highlight until the right side of the window */
4358 c = ' ';
4359 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004360
4361 /* Remember we do the char for line highlighting. */
4362 ++did_line_attr;
4363
4364 /* don't do search HL for the rest of the line */
4365 if (line_attr != 0 && char_attr == search_attr && col > 0)
4366 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367# ifdef FEAT_DIFF
4368 if (diff_hlf == HLF_TXD)
4369 {
4370 diff_hlf = HLF_CHD;
4371 if (attr == 0 || char_attr != attr)
4372 char_attr = hl_attr(diff_hlf);
4373 }
4374# endif
4375 }
4376#endif
4377 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004378
4379#ifdef FEAT_CONCEAL
4380 if ( wp->w_p_conceal
Bram Moolenaare667c952010-07-05 22:57:59 +02004381 && !area_highlighting
4382 && (lnum != wp->w_cursor.lnum
4383 || curwin != wp || wp->w_buffer->b_p_ma == FALSE)
4384 && (syntax_flags & HL_CONCEAL) != 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004385
4386 {
4387 char_attr = conceal_attr;
4388 if (first_conceal
4389 && (syn_get_sub_char() != NUL || wp->w_p_conceal == 1))
4390 {
4391 if (syn_get_sub_char() != NUL)
4392 c = syn_get_sub_char();
4393 else if (lcs_conceal != NUL)
4394 c = lcs_conceal;
4395 else
4396 c = ' ';
4397
4398 first_conceal = FALSE;
4399
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004400 if (n_extra > 0)
4401 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004402 vcol += n_extra;
4403 if (wp->w_p_wrap && n_extra > 0)
4404 {
4405# ifdef FEAT_RIGHTLEFT
4406 if (wp->w_p_rl)
4407 {
4408 col -= n_extra;
4409 boguscols -= n_extra;
4410 }
4411 else
4412# endif
4413 {
4414 boguscols += n_extra;
4415 col += n_extra;
4416 }
4417 }
4418 n_extra = 0;
4419 n_attr = 0;
4420 }
4421 else if (n_skip == 0)
4422 {
4423 is_concealing = TRUE;
4424 n_skip = 1;
4425 }
4426# ifdef FEAT_MBYTE
4427 mb_c = c;
4428 if (enc_utf8 && (*mb_char2len)(c) > 1)
4429 {
4430 mb_utf8 = TRUE;
4431 u8cc[0] = 0;
4432 c = 0xc0;
4433 }
4434 else
4435 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4436# endif
4437 }
4438 else
4439 {
4440 first_conceal = (wp->w_p_conceal != 3);
4441 is_concealing = FALSE;
4442 }
4443#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445
4446 /* Don't override visual selection highlighting. */
4447 if (n_attr > 0
4448 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004449 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 char_attr = extra_attr;
4451
Bram Moolenaar81695252004-12-29 20:58:21 +00004452#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 /* XIM don't send preedit_start and preedit_end, but they send
4454 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4455 * im_is_preediting() here. */
4456 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004457 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 && (State & INSERT)
4459 && !p_imdisable
4460 && im_is_preediting()
4461 && draw_state == WL_LINE)
4462 {
4463 colnr_T tcol;
4464
4465 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004466 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 else
4468 tcol = preedit_end_col;
4469 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4470 {
4471 if (feedback_old_attr < 0)
4472 {
4473 feedback_col = 0;
4474 feedback_old_attr = char_attr;
4475 }
4476 char_attr = im_get_feedback_attr(feedback_col);
4477 if (char_attr < 0)
4478 char_attr = feedback_old_attr;
4479 feedback_col++;
4480 }
4481 else if (feedback_old_attr >= 0)
4482 {
4483 char_attr = feedback_old_attr;
4484 feedback_old_attr = -1;
4485 feedback_col = 0;
4486 }
4487 }
4488#endif
4489 /*
4490 * Handle the case where we are in column 0 but not on the first
4491 * character of the line and the user wants us to show us a
4492 * special character (via 'listchars' option "precedes:<char>".
4493 */
4494 if (lcs_prec_todo != NUL
4495 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4496#ifdef FEAT_DIFF
4497 && filler_todo <= 0
4498#endif
4499 && draw_state > WL_NR
4500 && c != NUL)
4501 {
4502 c = lcs_prec;
4503 lcs_prec_todo = NUL;
4504#ifdef FEAT_MBYTE
4505 mb_c = c;
4506 if (enc_utf8 && (*mb_char2len)(c) > 1)
4507 {
4508 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004509 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004510 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
4512 else
4513 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4514#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004515 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 saved_attr3 = char_attr; /* save current attr */
4518 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4519 n_attr3 = 1;
4520 }
4521 }
4522
4523 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004524 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004526 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004527#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004528 || did_line_attr == 1
4529#endif
4530 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004532#ifdef FEAT_SEARCH_EXTRA
4533 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004534
4535 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004536 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004537 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004538#endif
4539
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004540 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 * highlight match at end of line. If it's beyond the last
4542 * char on the screen, just overwrite that one (tricky!) Not
4543 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004544#ifdef FEAT_SEARCH_EXTRA
4545 prevcol_hl_flag = FALSE;
4546 if (prevcol == (long)search_hl.startcol)
4547 prevcol_hl_flag = TRUE;
4548 else
4549 {
4550 cur = wp->w_match_head;
4551 while (cur != NULL)
4552 {
4553 if (prevcol == (long)cur->hl.startcol)
4554 {
4555 prevcol_hl_flag = TRUE;
4556 break;
4557 }
4558 cur = cur->next;
4559 }
4560 }
4561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004563 && ((area_attr != 0 && vcol == fromcol
4564#ifdef FEAT_VISUAL
4565 && (VIsual_mode != Ctrl_V
4566 || lnum == VIsual.lnum
4567 || lnum == curwin->w_cursor.lnum)
4568#endif
4569 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570#ifdef FEAT_SEARCH_EXTRA
4571 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004572 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004573# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004574 && did_line_attr <= 1
4575# endif
4576 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577#endif
4578 ))
4579 {
4580 int n = 0;
4581
4582#ifdef FEAT_RIGHTLEFT
4583 if (wp->w_p_rl)
4584 {
4585 if (col < 0)
4586 n = 1;
4587 }
4588 else
4589#endif
4590 {
4591 if (col >= W_WIDTH(wp))
4592 n = -1;
4593 }
4594 if (n != 0)
4595 {
4596 /* At the window boundary, highlight the last character
4597 * instead (better than nothing). */
4598 off += n;
4599 col += n;
4600 }
4601 else
4602 {
4603 /* Add a blank character to highlight. */
4604 ScreenLines[off] = ' ';
4605#ifdef FEAT_MBYTE
4606 if (enc_utf8)
4607 ScreenLinesUC[off] = 0;
4608#endif
4609 }
4610#ifdef FEAT_SEARCH_EXTRA
4611 if (area_attr == 0)
4612 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004613 /* Use attributes from match with highest priority among
4614 * 'search_hl' and the match list. */
4615 char_attr = search_hl.attr;
4616 cur = wp->w_match_head;
4617 shl_flag = FALSE;
4618 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004619 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004620 if (shl_flag == FALSE
4621 && ((cur != NULL
4622 && cur->priority > SEARCH_HL_PRIORITY)
4623 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004624 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004625 shl = &search_hl;
4626 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004627 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004628 else
4629 shl = &cur->hl;
4630 if ((ptr - line) - 1 == (long)shl->startcol)
4631 char_attr = shl->attr;
4632 if (shl != &search_hl && cur != NULL)
4633 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636#endif
4637 ScreenAttrs[off] = char_attr;
4638#ifdef FEAT_RIGHTLEFT
4639 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004640 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004642 --off;
4643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 else
4645#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004648 ++off;
4649 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004650 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004651#ifdef FEAT_SYN_HL
4652 eol_hl_off = 1;
4653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
Bram Moolenaar91170f82006-05-05 21:15:17 +00004657 /*
4658 * At end of the text line.
4659 */
4660 if (c == NUL)
4661 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004662#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004663 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4664 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004665 {
4666 /* highlight last char after line */
4667 --col;
4668 --off;
4669 --vcol;
4670 }
4671
Bram Moolenaar1a384422010-07-14 19:53:30 +02004672 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004673 if (wp->w_p_wrap)
4674 v = wp->w_skipcol;
4675 else
4676 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004677
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004678 /* check if line ends before left margin */
4679 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004680 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004681#ifdef FEAT_CONCEAL
4682 /* Get rid of the boguscols now, we want to draw until the right
4683 * edge for 'cursorcolumn'. */
4684 col -= boguscols;
4685 boguscols = 0;
4686#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004687
4688 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004689 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004690
4691 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004692 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4693 && (int)wp->w_virtcol <
4694 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004695 && lnum != wp->w_cursor.lnum)
4696 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004697# ifdef FEAT_RIGHTLEFT
4698 && !wp->w_p_rl
4699# endif
4700 )
4701 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004702 int rightmost_vcol = 0;
4703 int i;
4704
4705 if (wp->w_p_cuc)
4706 rightmost_vcol = wp->w_virtcol;
4707 if (draw_color_col)
4708 /* determine rightmost colorcolumn to possibly draw */
4709 for (i = 0; color_cols[i] >= 0; ++i)
4710 if (rightmost_vcol < color_cols[i])
4711 rightmost_vcol = color_cols[i];
4712
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004713 while (col < W_WIDTH(wp))
4714 {
4715 ScreenLines[off] = ' ';
4716#ifdef FEAT_MBYTE
4717 if (enc_utf8)
4718 ScreenLinesUC[off] = 0;
4719#endif
4720 ++col;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004721 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004722 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004723 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004724 ScreenAttrs[off++] = hl_attr(HLF_MC);
4725 else
4726 ScreenAttrs[off++] = 0;
4727
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004728 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004729 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004730
4731 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004732 draw_color_col = advance_color_col(VCOL_HLC,
4733 &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004734
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004735 ++vcol;
4736 }
4737 }
4738#endif
4739
Bram Moolenaar860cae12010-06-05 23:22:07 +02004740 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4741 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 row++;
4743
4744 /*
4745 * Update w_cline_height and w_cline_folded if the cursor line was
4746 * updated (saves a call to plines() later).
4747 */
4748 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4749 {
4750 curwin->w_cline_row = startrow;
4751 curwin->w_cline_height = row - startrow;
4752#ifdef FEAT_FOLDING
4753 curwin->w_cline_folded = FALSE;
4754#endif
4755 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4756 }
4757
4758 break;
4759 }
4760
4761 /* line continues beyond line end */
4762 if (lcs_ext
4763 && !wp->w_p_wrap
4764#ifdef FEAT_DIFF
4765 && filler_todo <= 0
4766#endif
4767 && (
4768#ifdef FEAT_RIGHTLEFT
4769 wp->w_p_rl ? col == 0 :
4770#endif
4771 col == W_WIDTH(wp) - 1)
4772 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004773 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4775 {
4776 c = lcs_ext;
4777 char_attr = hl_attr(HLF_AT);
4778#ifdef FEAT_MBYTE
4779 mb_c = c;
4780 if (enc_utf8 && (*mb_char2len)(c) > 1)
4781 {
4782 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004783 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004784 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786 else
4787 mb_utf8 = FALSE;
4788#endif
4789 }
4790
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004791#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004792 /* advance to the next 'colorcolumn' */
4793 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004794 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004795
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004796 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004797 * highlight the cursor position itself.
4798 * Also highlight the 'colorcolumn' if it is different than
4799 * 'cursorcolumn' */
4800 vcol_save_attr = -1;
4801 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004802 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004803 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004804 && lnum != wp->w_cursor.lnum)
4805 {
4806 vcol_save_attr = char_attr;
4807 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4808 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004809 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004810 {
4811 vcol_save_attr = char_attr;
4812 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4813 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004814 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004815#endif
4816
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 /*
4818 * Store character to be displayed.
4819 * Skip characters that are left of the screen for 'nowrap'.
4820 */
4821 vcol_prev = vcol;
4822 if (draw_state < WL_LINE || n_skip <= 0)
4823 {
4824 /*
4825 * Store the character.
4826 */
4827#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4828 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4829 {
4830 /* A double-wide character is: put first halve in left cell. */
4831 --off;
4832 --col;
4833 }
4834#endif
4835 ScreenLines[off] = c;
4836#ifdef FEAT_MBYTE
4837 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004838 {
4839 if ((mb_c & 0xff00) == 0x8e00)
4840 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else if (enc_utf8)
4844 {
4845 if (mb_utf8)
4846 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004847 int i;
4848
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004850 if ((c & 0xff) == 0)
4851 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004852 for (i = 0; i < Screen_mco; ++i)
4853 {
4854 ScreenLinesC[i][off] = u8cc[i];
4855 if (u8cc[i] == 0)
4856 break;
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
4859 else
4860 ScreenLinesUC[off] = 0;
4861 }
4862 if (multi_attr)
4863 {
4864 ScreenAttrs[off] = multi_attr;
4865 multi_attr = 0;
4866 }
4867 else
4868#endif
4869 ScreenAttrs[off] = char_attr;
4870
4871#ifdef FEAT_MBYTE
4872 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4873 {
4874 /* Need to fill two screen columns. */
4875 ++off;
4876 ++col;
4877 if (enc_utf8)
4878 /* UTF-8: Put a 0 in the second screen char. */
4879 ScreenLines[off] = 0;
4880 else
4881 /* DBCS: Put second byte in the second screen char. */
4882 ScreenLines[off] = mb_c & 0xff;
4883 ++vcol;
4884 /* When "tocol" is halfway a character, set it to the end of
4885 * the character, otherwise highlighting won't stop. */
4886 if (tocol == vcol)
4887 ++tocol;
4888#ifdef FEAT_RIGHTLEFT
4889 if (wp->w_p_rl)
4890 {
4891 /* now it's time to backup one cell */
4892 --off;
4893 --col;
4894 }
4895#endif
4896 }
4897#endif
4898#ifdef FEAT_RIGHTLEFT
4899 if (wp->w_p_rl)
4900 {
4901 --off;
4902 --col;
4903 }
4904 else
4905#endif
4906 {
4907 ++off;
4908 ++col;
4909 }
4910 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004911#ifdef FEAT_CONCEAL
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004912 else if (wp->w_p_conceal > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004913 {
4914 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004915 ++vcol_off;
4916 if (n_extra > 0)
4917 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004918 if (wp->w_p_wrap)
4919 {
4920 /*
4921 * Special voodoo required if 'wrap' is on.
4922 *
4923 * Advance the column indicator to force the line
4924 * drawing to wrap early. This will make the line
4925 * take up the same screen space when parts are concealed,
4926 * so that cursor line computations aren't messed up.
4927 *
4928 * To avoid the fictitious advance of 'col' causing
4929 * trailing junk to be written out of the screen line
4930 * we are building, 'boguscols' keeps track of the number
4931 * of bad columns we have advanced.
4932 */
4933 if (n_extra > 0)
4934 {
4935 vcol += n_extra;
4936# ifdef FEAT_RIGHTLEFT
4937 if (wp->w_p_rl)
4938 {
4939 col -= n_extra;
4940 boguscols -= n_extra;
4941 }
4942 else
4943# endif
4944 {
4945 col += n_extra;
4946 boguscols += n_extra;
4947 }
4948 n_extra = 0;
4949 n_attr = 0;
4950 }
4951
4952
4953# ifdef FEAT_MBYTE
4954 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4955 {
4956 /* Need to fill two screen columns. */
4957# ifdef FEAT_RIGHTLEFT
4958 if (wp->w_p_rl)
4959 {
4960 --boguscols;
4961 --col;
4962 }
4963 else
4964# endif
4965 {
4966 ++boguscols;
4967 ++col;
4968 }
4969 }
4970# endif
4971
4972# ifdef FEAT_RIGHTLEFT
4973 if (wp->w_p_rl)
4974 {
4975 --boguscols;
4976 --col;
4977 }
4978 else
4979# endif
4980 {
4981 ++boguscols;
4982 ++col;
4983 }
4984 }
4985 else
4986 {
4987 if (n_extra > 0)
4988 {
4989 vcol += n_extra;
4990 n_extra = 0;
4991 n_attr = 0;
4992 }
4993 }
4994
4995 }
4996#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 else
4998 --n_skip;
4999
Bram Moolenaar64486672010-05-16 15:46:46 +02005000 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5001 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005002 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003#ifdef FEAT_DIFF
5004 && filler_todo <= 0
5005#endif
5006 )
5007 ++vcol;
5008
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005009#ifdef FEAT_SYN_HL
5010 if (vcol_save_attr >= 0)
5011 char_attr = vcol_save_attr;
5012#endif
5013
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 /* restore attributes after "predeces" in 'listchars' */
5015 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5016 char_attr = saved_attr3;
5017
5018 /* restore attributes after last 'listchars' or 'number' char */
5019 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5020 char_attr = saved_attr2;
5021
5022 /*
5023 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005024 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 */
5026 if ((
5027#ifdef FEAT_RIGHTLEFT
5028 wp->w_p_rl ? (col < 0) :
5029#endif
5030 (col >= W_WIDTH(wp)))
5031 && (*ptr != NUL
5032#ifdef FEAT_DIFF
5033 || filler_todo > 0
5034#endif
5035 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
5036 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5037 )
5038 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005039#ifdef FEAT_CONCEAL
5040 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5041 (int)W_WIDTH(wp), wp->w_p_rl);
5042 boguscols = 0;
5043#else
5044 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5045 (int)W_WIDTH(wp), wp->w_p_rl);
5046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 ++row;
5048 ++screen_row;
5049
5050 /* When not wrapping and finished diff lines, or when displayed
5051 * '$' and highlighting until last column, break here. */
5052 if ((!wp->w_p_wrap
5053#ifdef FEAT_DIFF
5054 && filler_todo <= 0
5055#endif
5056 ) || lcs_eol_one == -1)
5057 break;
5058
5059 /* When the window is too narrow draw all "@" lines. */
5060 if (draw_state != WL_LINE
5061#ifdef FEAT_DIFF
5062 && filler_todo <= 0
5063#endif
5064 )
5065 {
5066 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5067#ifdef FEAT_VERTSPLIT
5068 draw_vsep_win(wp, row);
5069#endif
5070 row = endrow;
5071 }
5072
5073 /* When line got too long for screen break here. */
5074 if (row == endrow)
5075 {
5076 ++row;
5077 break;
5078 }
5079
5080 if (screen_cur_row == screen_row - 1
5081#ifdef FEAT_DIFF
5082 && filler_todo <= 0
5083#endif
5084 && W_WIDTH(wp) == Columns)
5085 {
5086 /* Remember that the line wraps, used for modeless copy. */
5087 LineWraps[screen_row - 1] = TRUE;
5088
5089 /*
5090 * Special trick to make copy/paste of wrapped lines work with
5091 * xterm/screen: write an extra character beyond the end of
5092 * the line. This will work with all terminal types
5093 * (regardless of the xn,am settings).
5094 * Only do this on a fast tty.
5095 * Only do this if the cursor is on the current line
5096 * (something has been written in it).
5097 * Don't do this for the GUI.
5098 * Don't do this for double-width characters.
5099 * Don't do this for a window not at the right screen border.
5100 */
5101 if (p_tf
5102#ifdef FEAT_GUI
5103 && !gui.in_use
5104#endif
5105#ifdef FEAT_MBYTE
5106 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005107 && ((*mb_off2cells)(LineOffset[screen_row],
5108 LineOffset[screen_row] + screen_Columns)
5109 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005111 + (int)Columns - 2,
5112 LineOffset[screen_row] + screen_Columns)
5113 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114#endif
5115 )
5116 {
5117 /* First make sure we are at the end of the screen line,
5118 * then output the same character again to let the
5119 * terminal know about the wrap. If the terminal doesn't
5120 * auto-wrap, we overwrite the character. */
5121 if (screen_cur_col != W_WIDTH(wp))
5122 screen_char(LineOffset[screen_row - 1]
5123 + (unsigned)Columns - 1,
5124 screen_row - 1, (int)(Columns - 1));
5125
5126#ifdef FEAT_MBYTE
5127 /* When there is a multi-byte character, just output a
5128 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005129 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5130 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 out_char(' ');
5132 else
5133#endif
5134 out_char(ScreenLines[LineOffset[screen_row - 1]
5135 + (Columns - 1)]);
5136 /* force a redraw of the first char on the next line */
5137 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5138 screen_start(); /* don't know where cursor is now */
5139 }
5140 }
5141
5142 col = 0;
5143 off = (unsigned)(current_ScreenLine - ScreenLines);
5144#ifdef FEAT_RIGHTLEFT
5145 if (wp->w_p_rl)
5146 {
5147 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5148 off += col;
5149 }
5150#endif
5151
5152 /* reset the drawing state for the start of a wrapped line */
5153 draw_state = WL_START;
5154 saved_n_extra = n_extra;
5155 saved_p_extra = p_extra;
5156 saved_c_extra = c_extra;
5157 saved_char_attr = char_attr;
5158 n_extra = 0;
5159 lcs_prec_todo = lcs_prec;
5160#ifdef FEAT_LINEBREAK
5161# ifdef FEAT_DIFF
5162 if (filler_todo <= 0)
5163# endif
5164 need_showbreak = TRUE;
5165#endif
5166#ifdef FEAT_DIFF
5167 --filler_todo;
5168 /* When the filler lines are actually below the last line of the
5169 * file, don't draw the line itself, break here. */
5170 if (filler_todo == 0 && wp->w_botfill)
5171 break;
5172#endif
5173 }
5174
5175 } /* for every character in the line */
5176
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005177#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005178 /* After an empty line check first word for capital. */
5179 if (*skipwhite(line) == NUL)
5180 {
5181 capcol_lnum = lnum + 1;
5182 cap_col = 0;
5183 }
5184#endif
5185
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 return row;
5187}
5188
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005189#ifdef FEAT_MBYTE
5190static int comp_char_differs __ARGS((int, int));
5191
5192/*
5193 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005194 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005195 */
5196 static int
5197comp_char_differs(off_from, off_to)
5198 int off_from;
5199 int off_to;
5200{
5201 int i;
5202
5203 for (i = 0; i < Screen_mco; ++i)
5204 {
5205 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5206 return TRUE;
5207 if (ScreenLinesC[i][off_from] == 0)
5208 break;
5209 }
5210 return FALSE;
5211}
5212#endif
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214/*
5215 * Check whether the given character needs redrawing:
5216 * - the (first byte of the) character is different
5217 * - the attributes are different
5218 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005219 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 */
5221 static int
5222char_needs_redraw(off_from, off_to, cols)
5223 int off_from;
5224 int off_to;
5225 int cols;
5226{
5227 if (cols > 0
5228 && ((ScreenLines[off_from] != ScreenLines[off_to]
5229 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5230
5231#ifdef FEAT_MBYTE
5232 || (enc_dbcs != 0
5233 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5234 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5235 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5236 : (cols > 1 && ScreenLines[off_from + 1]
5237 != ScreenLines[off_to + 1])))
5238 || (enc_utf8
5239 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5240 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005241 && comp_char_differs(off_from, off_to))
5242 || (cols > 1 && ScreenLines[off_from + 1]
5243 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
5245 ))
5246 return TRUE;
5247 return FALSE;
5248}
5249
5250/*
5251 * Move one "cooked" screen line to the screen, but only the characters that
5252 * have actually changed. Handle insert/delete character.
5253 * "coloff" gives the first column on the screen for this line.
5254 * "endcol" gives the columns where valid characters are.
5255 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5256 * needs to be cleared, negative otherwise.
5257 * "rlflag" is TRUE in a rightleft window:
5258 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5259 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5260 */
5261 static void
5262screen_line(row, coloff, endcol, clear_width
5263#ifdef FEAT_RIGHTLEFT
5264 , rlflag
5265#endif
5266 )
5267 int row;
5268 int coloff;
5269 int endcol;
5270 int clear_width;
5271#ifdef FEAT_RIGHTLEFT
5272 int rlflag;
5273#endif
5274{
5275 unsigned off_from;
5276 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005277#ifdef FEAT_MBYTE
5278 unsigned max_off_from;
5279 unsigned max_off_to;
5280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 int col = 0;
5282#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5283 int hl;
5284#endif
5285 int force = FALSE; /* force update rest of the line */
5286 int redraw_this /* bool: does character need redraw? */
5287#ifdef FEAT_GUI
5288 = TRUE /* For GUI when while-loop empty */
5289#endif
5290 ;
5291 int redraw_next; /* redraw_this for next character */
5292#ifdef FEAT_MBYTE
5293 int clear_next = FALSE;
5294 int char_cells; /* 1: normal char */
5295 /* 2: occupies two display cells */
5296# define CHAR_CELLS char_cells
5297#else
5298# define CHAR_CELLS 1
5299#endif
5300
5301# ifdef FEAT_CLIPBOARD
5302 clip_may_clear_selection(row, row);
5303# endif
5304
5305 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5306 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005307#ifdef FEAT_MBYTE
5308 max_off_from = off_from + screen_Columns;
5309 max_off_to = LineOffset[row] + screen_Columns;
5310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
5312#ifdef FEAT_RIGHTLEFT
5313 if (rlflag)
5314 {
5315 /* Clear rest first, because it's left of the text. */
5316 if (clear_width > 0)
5317 {
5318 while (col <= endcol && ScreenLines[off_to] == ' '
5319 && ScreenAttrs[off_to] == 0
5320# ifdef FEAT_MBYTE
5321 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5322# endif
5323 )
5324 {
5325 ++off_to;
5326 ++col;
5327 }
5328 if (col <= endcol)
5329 screen_fill(row, row + 1, col + coloff,
5330 endcol + coloff + 1, ' ', ' ', 0);
5331 }
5332 col = endcol + 1;
5333 off_to = LineOffset[row] + col + coloff;
5334 off_from += col;
5335 endcol = (clear_width > 0 ? clear_width : -clear_width);
5336 }
5337#endif /* FEAT_RIGHTLEFT */
5338
5339 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5340
5341 while (col < endcol)
5342 {
5343#ifdef FEAT_MBYTE
5344 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005345 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 else
5347 char_cells = 1;
5348#endif
5349
5350 redraw_this = redraw_next;
5351 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5352 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5353
5354#ifdef FEAT_GUI
5355 /* If the next character was bold, then redraw the current character to
5356 * remove any pixels that might have spilt over into us. This only
5357 * happens in the GUI.
5358 */
5359 if (redraw_next && gui.in_use)
5360 {
5361 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005362 if (hl > HL_ALL)
5363 hl = syn_attr2attr(hl);
5364 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 redraw_this = TRUE;
5366 }
5367#endif
5368
5369 if (redraw_this)
5370 {
5371 /*
5372 * Special handling when 'xs' termcap flag set (hpterm):
5373 * Attributes for characters are stored at the position where the
5374 * cursor is when writing the highlighting code. The
5375 * start-highlighting code must be written with the cursor on the
5376 * first highlighted character. The stop-highlighting code must
5377 * be written with the cursor just after the last highlighted
5378 * character.
5379 * Overwriting a character doesn't remove it's highlighting. Need
5380 * to clear the rest of the line, and force redrawing it
5381 * completely.
5382 */
5383 if ( p_wiv
5384 && !force
5385#ifdef FEAT_GUI
5386 && !gui.in_use
5387#endif
5388 && ScreenAttrs[off_to] != 0
5389 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5390 {
5391 /*
5392 * Need to remove highlighting attributes here.
5393 */
5394 windgoto(row, col + coloff);
5395 out_str(T_CE); /* clear rest of this screen line */
5396 screen_start(); /* don't know where cursor is now */
5397 force = TRUE; /* force redraw of rest of the line */
5398 redraw_next = TRUE; /* or else next char would miss out */
5399
5400 /*
5401 * If the previous character was highlighted, need to stop
5402 * highlighting at this character.
5403 */
5404 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5405 {
5406 screen_attr = ScreenAttrs[off_to - 1];
5407 term_windgoto(row, col + coloff);
5408 screen_stop_highlight();
5409 }
5410 else
5411 screen_attr = 0; /* highlighting has stopped */
5412 }
5413#ifdef FEAT_MBYTE
5414 if (enc_dbcs != 0)
5415 {
5416 /* Check if overwriting a double-byte with a single-byte or
5417 * the other way around requires another character to be
5418 * redrawn. For UTF-8 this isn't needed, because comparing
5419 * ScreenLinesUC[] is sufficient. */
5420 if (char_cells == 1
5421 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005422 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {
5424 /* Writing a single-cell character over a double-cell
5425 * character: need to redraw the next cell. */
5426 ScreenLines[off_to + 1] = 0;
5427 redraw_next = TRUE;
5428 }
5429 else if (char_cells == 2
5430 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005431 && (*mb_off2cells)(off_to, max_off_to) == 1
5432 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
5434 /* Writing the second half of a double-cell character over
5435 * a double-cell character: need to redraw the second
5436 * cell. */
5437 ScreenLines[off_to + 2] = 0;
5438 redraw_next = TRUE;
5439 }
5440
5441 if (enc_dbcs == DBCS_JPNU)
5442 ScreenLines2[off_to] = ScreenLines2[off_from];
5443 }
5444 /* When writing a single-width character over a double-width
5445 * character and at the end of the redrawn text, need to clear out
5446 * the right halve of the old character.
5447 * Also required when writing the right halve of a double-width
5448 * char over the left halve of an existing one. */
5449 if (has_mbyte && col + char_cells == endcol
5450 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005451 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005453 && (*mb_off2cells)(off_to, max_off_to) == 1
5454 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 clear_next = TRUE;
5456#endif
5457
5458 ScreenLines[off_to] = ScreenLines[off_from];
5459#ifdef FEAT_MBYTE
5460 if (enc_utf8)
5461 {
5462 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5463 if (ScreenLinesUC[off_from] != 0)
5464 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005465 int i;
5466
5467 for (i = 0; i < Screen_mco; ++i)
5468 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 }
5470 }
5471 if (char_cells == 2)
5472 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5473#endif
5474
5475#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005476 /* The bold trick makes a single column of pixels appear in the
5477 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 * character should be redrawn too. This happens for our own GUI
5479 * and for some xterms. */
5480 if (
5481# ifdef FEAT_GUI
5482 gui.in_use
5483# endif
5484# if defined(FEAT_GUI) && defined(UNIX)
5485 ||
5486# endif
5487# ifdef UNIX
5488 term_is_xterm
5489# endif
5490 )
5491 {
5492 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005493 if (hl > HL_ALL)
5494 hl = syn_attr2attr(hl);
5495 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 redraw_next = TRUE;
5497 }
5498#endif
5499 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5500#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005501 /* For simplicity set the attributes of second half of a
5502 * double-wide character equal to the first half. */
5503 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005505
5506 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 else
5509#endif
5510 screen_char(off_to, row, col + coloff);
5511 }
5512 else if ( p_wiv
5513#ifdef FEAT_GUI
5514 && !gui.in_use
5515#endif
5516 && col + coloff > 0)
5517 {
5518 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5519 {
5520 /*
5521 * Don't output stop-highlight when moving the cursor, it will
5522 * stop the highlighting when it should continue.
5523 */
5524 screen_attr = 0;
5525 }
5526 else if (screen_attr != 0)
5527 screen_stop_highlight();
5528 }
5529
5530 off_to += CHAR_CELLS;
5531 off_from += CHAR_CELLS;
5532 col += CHAR_CELLS;
5533 }
5534
5535#ifdef FEAT_MBYTE
5536 if (clear_next)
5537 {
5538 /* Clear the second half of a double-wide character of which the left
5539 * half was overwritten with a single-wide character. */
5540 ScreenLines[off_to] = ' ';
5541 if (enc_utf8)
5542 ScreenLinesUC[off_to] = 0;
5543 screen_char(off_to, row, col + coloff);
5544 }
5545#endif
5546
5547 if (clear_width > 0
5548#ifdef FEAT_RIGHTLEFT
5549 && !rlflag
5550#endif
5551 )
5552 {
5553#ifdef FEAT_GUI
5554 int startCol = col;
5555#endif
5556
5557 /* blank out the rest of the line */
5558 while (col < clear_width && ScreenLines[off_to] == ' '
5559 && ScreenAttrs[off_to] == 0
5560#ifdef FEAT_MBYTE
5561 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5562#endif
5563 )
5564 {
5565 ++off_to;
5566 ++col;
5567 }
5568 if (col < clear_width)
5569 {
5570#ifdef FEAT_GUI
5571 /*
5572 * In the GUI, clearing the rest of the line may leave pixels
5573 * behind if the first character cleared was bold. Some bold
5574 * fonts spill over the left. In this case we redraw the previous
5575 * character too. If we didn't skip any blanks above, then we
5576 * only redraw if the character wasn't already redrawn anyway.
5577 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005578 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
5580 hl = ScreenAttrs[off_to];
5581 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005582 {
5583 int prev_cells = 1;
5584# ifdef FEAT_MBYTE
5585 if (enc_utf8)
5586 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5587 * that its width is 2. */
5588 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5589 else if (enc_dbcs != 0)
5590 {
5591 /* find previous character by counting from first
5592 * column and get its width. */
5593 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005594 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005595
5596 while (off < off_to)
5597 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005598 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005599 off += prev_cells;
5600 }
5601 }
5602
5603 if (enc_dbcs != 0 && prev_cells > 1)
5604 screen_char_2(off_to - prev_cells, row,
5605 col + coloff - prev_cells);
5606 else
5607# endif
5608 screen_char(off_to - prev_cells, row,
5609 col + coloff - prev_cells);
5610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
5612#endif
5613 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5614 ' ', ' ', 0);
5615#ifdef FEAT_VERTSPLIT
5616 off_to += clear_width - col;
5617 col = clear_width;
5618#endif
5619 }
5620 }
5621
5622 if (clear_width > 0)
5623 {
5624#ifdef FEAT_VERTSPLIT
5625 /* For a window that's left of another, draw the separator char. */
5626 if (col + coloff < Columns)
5627 {
5628 int c;
5629
5630 c = fillchar_vsep(&hl);
5631 if (ScreenLines[off_to] != c
5632# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005633 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5634 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635# endif
5636 || ScreenAttrs[off_to] != hl)
5637 {
5638 ScreenLines[off_to] = c;
5639 ScreenAttrs[off_to] = hl;
5640# ifdef FEAT_MBYTE
5641 if (enc_utf8)
5642 {
5643 if (c >= 0x80)
5644 {
5645 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005646 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
5648 else
5649 ScreenLinesUC[off_to] = 0;
5650 }
5651# endif
5652 screen_char(off_to, row, col + coloff);
5653 }
5654 }
5655 else
5656#endif
5657 LineWraps[row] = FALSE;
5658 }
5659}
5660
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005661#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005663 * Mirror text "str" for right-left displaying.
5664 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005666 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667rl_mirror(str)
5668 char_u *str;
5669{
5670 char_u *p1, *p2;
5671 int t;
5672
5673 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5674 {
5675 t = *p1;
5676 *p1 = *p2;
5677 *p2 = t;
5678 }
5679}
5680#endif
5681
5682#if defined(FEAT_WINDOWS) || defined(PROTO)
5683/*
5684 * mark all status lines for redraw; used after first :cd
5685 */
5686 void
5687status_redraw_all()
5688{
5689 win_T *wp;
5690
5691 for (wp = firstwin; wp; wp = wp->w_next)
5692 if (wp->w_status_height)
5693 {
5694 wp->w_redr_status = TRUE;
5695 redraw_later(VALID);
5696 }
5697}
5698
5699/*
5700 * mark all status lines of the current buffer for redraw
5701 */
5702 void
5703status_redraw_curbuf()
5704{
5705 win_T *wp;
5706
5707 for (wp = firstwin; wp; wp = wp->w_next)
5708 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5709 {
5710 wp->w_redr_status = TRUE;
5711 redraw_later(VALID);
5712 }
5713}
5714
5715/*
5716 * Redraw all status lines that need to be redrawn.
5717 */
5718 void
5719redraw_statuslines()
5720{
5721 win_T *wp;
5722
5723 for (wp = firstwin; wp; wp = wp->w_next)
5724 if (wp->w_redr_status)
5725 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005726 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005727 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728}
5729#endif
5730
5731#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5732/*
5733 * Redraw all status lines at the bottom of frame "frp".
5734 */
5735 void
5736win_redraw_last_status(frp)
5737 frame_T *frp;
5738{
5739 if (frp->fr_layout == FR_LEAF)
5740 frp->fr_win->w_redr_status = TRUE;
5741 else if (frp->fr_layout == FR_ROW)
5742 {
5743 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5744 win_redraw_last_status(frp);
5745 }
5746 else /* frp->fr_layout == FR_COL */
5747 {
5748 frp = frp->fr_child;
5749 while (frp->fr_next != NULL)
5750 frp = frp->fr_next;
5751 win_redraw_last_status(frp);
5752 }
5753}
5754#endif
5755
5756#ifdef FEAT_VERTSPLIT
5757/*
5758 * Draw the verticap separator right of window "wp" starting with line "row".
5759 */
5760 static void
5761draw_vsep_win(wp, row)
5762 win_T *wp;
5763 int row;
5764{
5765 int hl;
5766 int c;
5767
5768 if (wp->w_vsep_width)
5769 {
5770 /* draw the vertical separator right of this window */
5771 c = fillchar_vsep(&hl);
5772 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5773 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5774 c, ' ', hl);
5775 }
5776}
5777#endif
5778
5779#ifdef FEAT_WILDMENU
5780static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005781static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
5783/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005784 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 */
5786 static int
5787status_match_len(xp, s)
5788 expand_T *xp;
5789 char_u *s;
5790{
5791 int len = 0;
5792
5793#ifdef FEAT_MENU
5794 int emenu = (xp->xp_context == EXPAND_MENUS
5795 || xp->xp_context == EXPAND_MENUNAMES);
5796
5797 /* Check for menu separators - replace with '|'. */
5798 if (emenu && menu_is_separator(s))
5799 return 1;
5800#endif
5801
5802 while (*s != NUL)
5803 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005804 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005805 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005806 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
5808
5809 return len;
5810}
5811
5812/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005813 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005814 * These are backslashes used for escaping. Do show backslashes in help tags.
5815 */
5816 static int
5817skip_status_match_char(xp, s)
5818 expand_T *xp;
5819 char_u *s;
5820{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005821 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005822#ifdef FEAT_MENU
5823 || ((xp->xp_context == EXPAND_MENUS
5824 || xp->xp_context == EXPAND_MENUNAMES)
5825 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5826#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005827 )
5828 {
5829#ifndef BACKSLASH_IN_FILENAME
5830 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5831 return 2;
5832#endif
5833 return 1;
5834 }
5835 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005836}
5837
5838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * Show wildchar matches in the status line.
5840 * Show at least the "match" item.
5841 * We start at item 'first_match' in the list and show all matches that fit.
5842 *
5843 * If inversion is possible we use it. Else '=' characters are used.
5844 */
5845 void
5846win_redr_status_matches(xp, num_matches, matches, match, showtail)
5847 expand_T *xp;
5848 int num_matches;
5849 char_u **matches; /* list of matches */
5850 int match;
5851 int showtail;
5852{
5853#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5854 int row;
5855 char_u *buf;
5856 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005857 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 int fillchar;
5859 int attr;
5860 int i;
5861 int highlight = TRUE;
5862 char_u *selstart = NULL;
5863 int selstart_col = 0;
5864 char_u *selend = NULL;
5865 static int first_match = 0;
5866 int add_left = FALSE;
5867 char_u *s;
5868#ifdef FEAT_MENU
5869 int emenu;
5870#endif
5871#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5872 int l;
5873#endif
5874
5875 if (matches == NULL) /* interrupted completion? */
5876 return;
5877
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005878#ifdef FEAT_MBYTE
5879 if (has_mbyte)
5880 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5881 else
5882#endif
5883 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 if (buf == NULL)
5885 return;
5886
5887 if (match == -1) /* don't show match but original text */
5888 {
5889 match = 0;
5890 highlight = FALSE;
5891 }
5892 /* count 1 for the ending ">" */
5893 clen = status_match_len(xp, L_MATCH(match)) + 3;
5894 if (match == 0)
5895 first_match = 0;
5896 else if (match < first_match)
5897 {
5898 /* jumping left, as far as we can go */
5899 first_match = match;
5900 add_left = TRUE;
5901 }
5902 else
5903 {
5904 /* check if match fits on the screen */
5905 for (i = first_match; i < match; ++i)
5906 clen += status_match_len(xp, L_MATCH(i)) + 2;
5907 if (first_match > 0)
5908 clen += 2;
5909 /* jumping right, put match at the left */
5910 if ((long)clen > Columns)
5911 {
5912 first_match = match;
5913 /* if showing the last match, we can add some on the left */
5914 clen = 2;
5915 for (i = match; i < num_matches; ++i)
5916 {
5917 clen += status_match_len(xp, L_MATCH(i)) + 2;
5918 if ((long)clen >= Columns)
5919 break;
5920 }
5921 if (i == num_matches)
5922 add_left = TRUE;
5923 }
5924 }
5925 if (add_left)
5926 while (first_match > 0)
5927 {
5928 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5929 if ((long)clen >= Columns)
5930 break;
5931 --first_match;
5932 }
5933
5934 fillchar = fillchar_status(&attr, TRUE);
5935
5936 if (first_match == 0)
5937 {
5938 *buf = NUL;
5939 len = 0;
5940 }
5941 else
5942 {
5943 STRCPY(buf, "< ");
5944 len = 2;
5945 }
5946 clen = len;
5947
5948 i = first_match;
5949 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5950 {
5951 if (i == match)
5952 {
5953 selstart = buf + len;
5954 selstart_col = clen;
5955 }
5956
5957 s = L_MATCH(i);
5958 /* Check for menu separators - replace with '|' */
5959#ifdef FEAT_MENU
5960 emenu = (xp->xp_context == EXPAND_MENUS
5961 || xp->xp_context == EXPAND_MENUNAMES);
5962 if (emenu && menu_is_separator(s))
5963 {
5964 STRCPY(buf + len, transchar('|'));
5965 l = (int)STRLEN(buf + len);
5966 len += l;
5967 clen += l;
5968 }
5969 else
5970#endif
5971 for ( ; *s != NUL; ++s)
5972 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005973 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 clen += ptr2cells(s);
5975#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005976 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {
5978 STRNCPY(buf + len, s, l);
5979 s += l - 1;
5980 len += l;
5981 }
5982 else
5983#endif
5984 {
5985 STRCPY(buf + len, transchar_byte(*s));
5986 len += (int)STRLEN(buf + len);
5987 }
5988 }
5989 if (i == match)
5990 selend = buf + len;
5991
5992 *(buf + len++) = ' ';
5993 *(buf + len++) = ' ';
5994 clen += 2;
5995 if (++i == num_matches)
5996 break;
5997 }
5998
5999 if (i != num_matches)
6000 {
6001 *(buf + len++) = '>';
6002 ++clen;
6003 }
6004
6005 buf[len] = NUL;
6006
6007 row = cmdline_row - 1;
6008 if (row >= 0)
6009 {
6010 if (wild_menu_showing == 0)
6011 {
6012 if (msg_scrolled > 0)
6013 {
6014 /* Put the wildmenu just above the command line. If there is
6015 * no room, scroll the screen one line up. */
6016 if (cmdline_row == Rows - 1)
6017 {
6018 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6019 ++msg_scrolled;
6020 }
6021 else
6022 {
6023 ++cmdline_row;
6024 ++row;
6025 }
6026 wild_menu_showing = WM_SCROLLED;
6027 }
6028 else
6029 {
6030 /* Create status line if needed by setting 'laststatus' to 2.
6031 * Set 'winminheight' to zero to avoid that the window is
6032 * resized. */
6033 if (lastwin->w_status_height == 0)
6034 {
6035 save_p_ls = p_ls;
6036 save_p_wmh = p_wmh;
6037 p_ls = 2;
6038 p_wmh = 0;
6039 last_status(FALSE);
6040 }
6041 wild_menu_showing = WM_SHOWN;
6042 }
6043 }
6044
6045 screen_puts(buf, row, 0, attr);
6046 if (selstart != NULL && highlight)
6047 {
6048 *selend = NUL;
6049 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6050 }
6051
6052 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6053 }
6054
6055#ifdef FEAT_VERTSPLIT
6056 win_redraw_last_status(topframe);
6057#else
6058 lastwin->w_redr_status = TRUE;
6059#endif
6060 vim_free(buf);
6061}
6062#endif
6063
6064#if defined(FEAT_WINDOWS) || defined(PROTO)
6065/*
6066 * Redraw the status line of window wp.
6067 *
6068 * If inversion is possible we use it. Else '=' characters are used.
6069 */
6070 void
6071win_redr_status(wp)
6072 win_T *wp;
6073{
6074 int row;
6075 char_u *p;
6076 int len;
6077 int fillchar;
6078 int attr;
6079 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006080 static int busy = FALSE;
6081
6082 /* It's possible to get here recursively when 'statusline' (indirectly)
6083 * invokes ":redrawstatus". Simply ignore the call then. */
6084 if (busy)
6085 return;
6086 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
6088 wp->w_redr_status = FALSE;
6089 if (wp->w_status_height == 0)
6090 {
6091 /* no status line, can only be last window */
6092 redraw_cmdline = TRUE;
6093 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006094 else if (!redrawing()
6095#ifdef FEAT_INS_EXPAND
6096 /* don't update status line when popup menu is visible and may be
6097 * drawn over it */
6098 || pum_visible()
6099#endif
6100 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 {
6102 /* Don't redraw right now, do it later. */
6103 wp->w_redr_status = TRUE;
6104 }
6105#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006106 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006109 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 }
6111#endif
6112 else
6113 {
6114 fillchar = fillchar_status(&attr, wp == curwin);
6115
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006116 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 p = NameBuff;
6118 len = (int)STRLEN(p);
6119
6120 if (wp->w_buffer->b_help
6121#ifdef FEAT_QUICKFIX
6122 || wp->w_p_pvw
6123#endif
6124 || bufIsChanged(wp->w_buffer)
6125 || wp->w_buffer->b_p_ro)
6126 *(p + len++) = ' ';
6127 if (wp->w_buffer->b_help)
6128 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006129 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 len += (int)STRLEN(p + len);
6131 }
6132#ifdef FEAT_QUICKFIX
6133 if (wp->w_p_pvw)
6134 {
6135 STRCPY(p + len, _("[Preview]"));
6136 len += (int)STRLEN(p + len);
6137 }
6138#endif
6139 if (bufIsChanged(wp->w_buffer))
6140 {
6141 STRCPY(p + len, "[+]");
6142 len += 3;
6143 }
6144 if (wp->w_buffer->b_p_ro)
6145 {
6146 STRCPY(p + len, "[RO]");
6147 len += 4;
6148 }
6149
6150#ifndef FEAT_VERTSPLIT
6151 this_ru_col = ru_col;
6152 if (this_ru_col < (Columns + 1) / 2)
6153 this_ru_col = (Columns + 1) / 2;
6154#else
6155 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6156 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6157 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6158 if (this_ru_col <= 1)
6159 {
6160 p = (char_u *)"<"; /* No room for file name! */
6161 len = 1;
6162 }
6163 else
6164#endif
6165#ifdef FEAT_MBYTE
6166 if (has_mbyte)
6167 {
6168 int clen = 0, i;
6169
6170 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006171 clen = mb_string2cells(p, -1);
6172
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 /* Find first character that will fit.
6174 * Going from start to end is much faster for DBCS. */
6175 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006176 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 clen -= (*mb_ptr2cells)(p + i);
6178 len = clen;
6179 if (i > 0)
6180 {
6181 p = p + i - 1;
6182 *p = '<';
6183 ++len;
6184 }
6185
6186 }
6187 else
6188#endif
6189 if (len > this_ru_col - 1)
6190 {
6191 p += len - (this_ru_col - 1);
6192 *p = '<';
6193 len = this_ru_col - 1;
6194 }
6195
6196 row = W_WINROW(wp) + wp->w_height;
6197 screen_puts(p, row, W_WINCOL(wp), attr);
6198 screen_fill(row, row + 1, len + W_WINCOL(wp),
6199 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6200
6201 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6202 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6203 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6204 - 1 + W_WINCOL(wp)), attr);
6205
6206#ifdef FEAT_CMDL_INFO
6207 win_redr_ruler(wp, TRUE);
6208#endif
6209 }
6210
6211#ifdef FEAT_VERTSPLIT
6212 /*
6213 * May need to draw the character below the vertical separator.
6214 */
6215 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6216 {
6217 if (stl_connected(wp))
6218 fillchar = fillchar_status(&attr, wp == curwin);
6219 else
6220 fillchar = fillchar_vsep(&attr);
6221 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6222 attr);
6223 }
6224#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006225 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226}
6227
Bram Moolenaar238a5642006-02-21 22:12:05 +00006228#ifdef FEAT_STL_OPT
6229/*
6230 * Redraw the status line according to 'statusline' and take care of any
6231 * errors encountered.
6232 */
6233 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006234redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006235 win_T *wp;
6236{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006237 static int entered = FALSE;
6238 int save_called_emsg = called_emsg;
6239
6240 /* When called recursively return. This can happen when the statusline
6241 * contains an expression that triggers a redraw. */
6242 if (entered)
6243 return;
6244 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006245
6246 called_emsg = FALSE;
6247 win_redr_custom(wp, FALSE);
6248 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006249 {
6250 /* When there is an error disable the statusline, otherwise the
6251 * display is messed up with errors and a redraw triggers the problem
6252 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006253 set_string_option_direct((char_u *)"statusline", -1,
6254 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006255 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006256 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006257 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006258 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006259}
6260#endif
6261
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262# ifdef FEAT_VERTSPLIT
6263/*
6264 * Return TRUE if the status line of window "wp" is connected to the status
6265 * line of the window right of it. If not, then it's a vertical separator.
6266 * Only call if (wp->w_vsep_width != 0).
6267 */
6268 int
6269stl_connected(wp)
6270 win_T *wp;
6271{
6272 frame_T *fr;
6273
6274 fr = wp->w_frame;
6275 while (fr->fr_parent != NULL)
6276 {
6277 if (fr->fr_parent->fr_layout == FR_COL)
6278 {
6279 if (fr->fr_next != NULL)
6280 break;
6281 }
6282 else
6283 {
6284 if (fr->fr_next != NULL)
6285 return TRUE;
6286 }
6287 fr = fr->fr_parent;
6288 }
6289 return FALSE;
6290}
6291# endif
6292
6293#endif /* FEAT_WINDOWS */
6294
6295#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6296/*
6297 * Get the value to show for the language mappings, active 'keymap'.
6298 */
6299 int
6300get_keymap_str(wp, buf, len)
6301 win_T *wp;
6302 char_u *buf; /* buffer for the result */
6303 int len; /* length of buffer */
6304{
6305 char_u *p;
6306
6307 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6308 return FALSE;
6309
6310 {
6311#ifdef FEAT_EVAL
6312 buf_T *old_curbuf = curbuf;
6313 win_T *old_curwin = curwin;
6314 char_u *s;
6315
6316 curbuf = wp->w_buffer;
6317 curwin = wp;
6318 STRCPY(buf, "b:keymap_name"); /* must be writable */
6319 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006320 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 --emsg_skip;
6322 curbuf = old_curbuf;
6323 curwin = old_curwin;
6324 if (p == NULL || *p == NUL)
6325#endif
6326 {
6327#ifdef FEAT_KEYMAP
6328 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6329 p = wp->w_buffer->b_p_keymap;
6330 else
6331#endif
6332 p = (char_u *)"lang";
6333 }
6334 if ((int)(STRLEN(p) + 3) < len)
6335 sprintf((char *)buf, "<%s>", p);
6336 else
6337 buf[0] = NUL;
6338#ifdef FEAT_EVAL
6339 vim_free(s);
6340#endif
6341 }
6342 return buf[0] != NUL;
6343}
6344#endif
6345
6346#if defined(FEAT_STL_OPT) || defined(PROTO)
6347/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006348 * Redraw the status line or ruler of window "wp".
6349 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 */
6351 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006352win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006354 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
6356 int attr;
6357 int curattr;
6358 int row;
6359 int col = 0;
6360 int maxwidth;
6361 int width;
6362 int n;
6363 int len;
6364 int fillchar;
6365 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006366 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006368 struct stl_hlrec hltab[STL_MAX_ITEM];
6369 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006370 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
6372 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006373 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006375 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006376 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006377 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006378 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006379 attr = hl_attr(HLF_TPF);
6380 maxwidth = Columns;
6381# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006382 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006385 else
6386 {
6387 row = W_WINROW(wp) + wp->w_height;
6388 fillchar = fillchar_status(&attr, wp == curwin);
6389 maxwidth = W_WIDTH(wp);
6390
6391 if (draw_ruler)
6392 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006393 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006394 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006395 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006396 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006397 if (*++stl == '-')
6398 stl++;
6399 if (atoi((char *)stl))
6400 while (VIM_ISDIGIT(*stl))
6401 stl++;
6402 if (*stl++ != '(')
6403 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006404 }
6405#ifdef FEAT_VERTSPLIT
6406 col = ru_col - (Columns - W_WIDTH(wp));
6407 if (col < (W_WIDTH(wp) + 1) / 2)
6408 col = (W_WIDTH(wp) + 1) / 2;
6409#else
6410 col = ru_col;
6411 if (col > (Columns + 1) / 2)
6412 col = (Columns + 1) / 2;
6413#endif
6414 maxwidth = W_WIDTH(wp) - col;
6415#ifdef FEAT_WINDOWS
6416 if (!wp->w_status_height)
6417#endif
6418 {
6419 row = Rows - 1;
6420 --maxwidth; /* writing in last column may cause scrolling */
6421 fillchar = ' ';
6422 attr = 0;
6423 }
6424
6425# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006426 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006427# endif
6428 }
6429 else
6430 {
6431 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006432 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006433 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006434 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006435# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006436 use_sandbox = was_set_insecurely((char_u *)"statusline",
6437 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006438# endif
6439 }
6440
6441#ifdef FEAT_VERTSPLIT
6442 col += W_WINCOL(wp);
6443#endif
6444 }
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (maxwidth <= 0)
6447 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448
Bram Moolenaar362f3562009-11-03 16:20:34 +00006449 /* Make a copy, because the statusline may include a function call that
6450 * might change the option value and free the memory. */
6451 stl = vim_strsave(stl);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006452 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6453 buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006454 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006455 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006456 vim_free(stl);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006457 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006459 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 {
6461#ifdef FEAT_MBYTE
6462 len += (*mb_char2bytes)(fillchar, buf + len);
6463#else
6464 buf[len++] = fillchar;
6465#endif
6466 ++width;
6467 }
6468 buf[len] = NUL;
6469
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006470 /*
6471 * Draw each snippet with the specified highlighting.
6472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 curattr = attr;
6474 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006475 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006477 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 screen_puts_len(p, len, row, col, curattr);
6479 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006480 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006482 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006484 else if (hltab[n].userhl < 0)
6485 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006487 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006488 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489#endif
6490 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006491 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 }
6493 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006494
6495 if (wp == NULL)
6496 {
6497 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6498 col = 0;
6499 len = 0;
6500 p = buf;
6501 fillchar = 0;
6502 for (n = 0; tabtab[n].start != NULL; n++)
6503 {
6504 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6505 while (col < len)
6506 TabPageIdxs[col++] = fillchar;
6507 p = tabtab[n].start;
6508 fillchar = tabtab[n].userhl;
6509 }
6510 while (col < Columns)
6511 TabPageIdxs[col++] = fillchar;
6512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513}
6514
6515#endif /* FEAT_STL_OPT */
6516
6517/*
6518 * Output a single character directly to the screen and update ScreenLines.
6519 */
6520 void
6521screen_putchar(c, row, col, attr)
6522 int c;
6523 int row, col;
6524 int attr;
6525{
6526#ifdef FEAT_MBYTE
6527 char_u buf[MB_MAXBYTES + 1];
6528
6529 buf[(*mb_char2bytes)(c, buf)] = NUL;
6530#else
6531 char_u buf[2];
6532
6533 buf[0] = c;
6534 buf[1] = NUL;
6535#endif
6536 screen_puts(buf, row, col, attr);
6537}
6538
6539/*
6540 * Get a single character directly from ScreenLines into "bytes[]".
6541 * Also return its attribute in *attrp;
6542 */
6543 void
6544screen_getbytes(row, col, bytes, attrp)
6545 int row, col;
6546 char_u *bytes;
6547 int *attrp;
6548{
6549 unsigned off;
6550
6551 /* safety check */
6552 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6553 {
6554 off = LineOffset[row] + col;
6555 *attrp = ScreenAttrs[off];
6556 bytes[0] = ScreenLines[off];
6557 bytes[1] = NUL;
6558
6559#ifdef FEAT_MBYTE
6560 if (enc_utf8 && ScreenLinesUC[off] != 0)
6561 bytes[utfc_char2bytes(off, bytes)] = NUL;
6562 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6563 {
6564 bytes[0] = ScreenLines[off];
6565 bytes[1] = ScreenLines2[off];
6566 bytes[2] = NUL;
6567 }
6568 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6569 {
6570 bytes[1] = ScreenLines[off + 1];
6571 bytes[2] = NUL;
6572 }
6573#endif
6574 }
6575}
6576
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006577#ifdef FEAT_MBYTE
6578static int screen_comp_differs __ARGS((int, int*));
6579
6580/*
6581 * Return TRUE if composing characters for screen posn "off" differs from
6582 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006583 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006584 */
6585 static int
6586screen_comp_differs(off, u8cc)
6587 int off;
6588 int *u8cc;
6589{
6590 int i;
6591
6592 for (i = 0; i < Screen_mco; ++i)
6593 {
6594 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6595 return TRUE;
6596 if (u8cc[i] == 0)
6597 break;
6598 }
6599 return FALSE;
6600}
6601#endif
6602
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603/*
6604 * Put string '*text' on the screen at position 'row' and 'col', with
6605 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6606 * Note: only outputs within one row, message is truncated at screen boundary!
6607 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6608 */
6609 void
6610screen_puts(text, row, col, attr)
6611 char_u *text;
6612 int row;
6613 int col;
6614 int attr;
6615{
6616 screen_puts_len(text, -1, row, col, attr);
6617}
6618
6619/*
6620 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6621 * a NUL.
6622 */
6623 void
6624screen_puts_len(text, len, row, col, attr)
6625 char_u *text;
6626 int len;
6627 int row;
6628 int col;
6629 int attr;
6630{
6631 unsigned off;
6632 char_u *ptr = text;
6633 int c;
6634#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006635 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 int mbyte_blen = 1;
6637 int mbyte_cells = 1;
6638 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006639 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 int clear_next_cell = FALSE;
6641# ifdef FEAT_ARABIC
6642 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006643 int pc, nc, nc1;
6644 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645# endif
6646#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006647#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6648 int force_redraw_this;
6649 int force_redraw_next = FALSE;
6650#endif
6651 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6654 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006655 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
Bram Moolenaarc236c162008-07-13 17:41:49 +00006657#ifdef FEAT_MBYTE
6658 /* When drawing over the right halve of a double-wide char clear out the
6659 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006660 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006661# ifdef FEAT_GUI
6662 && !gui.in_use
6663# endif
6664 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006665 {
6666 ScreenLines[off - 1] = ' ';
6667 ScreenAttrs[off - 1] = 0;
6668 if (enc_utf8)
6669 {
6670 ScreenLinesUC[off - 1] = 0;
6671 ScreenLinesC[0][off - 1] = 0;
6672 }
6673 /* redraw the previous cell, make it empty */
6674 screen_char(off - 1, row, col - 1);
6675 /* force the cell at "col" to be redrawn */
6676 force_redraw_next = TRUE;
6677 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006678#endif
6679
Bram Moolenaar367329b2007-08-30 11:53:22 +00006680#ifdef FEAT_MBYTE
6681 max_off = LineOffset[row] + screen_Columns;
6682#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006683 while (col < screen_Columns
6684 && (len < 0 || (int)(ptr - text) < len)
6685 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
6687 c = *ptr;
6688#ifdef FEAT_MBYTE
6689 /* check if this is the first byte of a multibyte */
6690 if (has_mbyte)
6691 {
6692 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006693 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006695 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6697 mbyte_cells = 1;
6698 else if (enc_dbcs != 0)
6699 mbyte_cells = mbyte_blen;
6700 else /* enc_utf8 */
6701 {
6702 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006703 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 (int)((text + len) - ptr));
6705 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006706 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006708# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 /* Non-BMP character: display as ? or fullwidth ?. */
6710 if (u8c >= 0x10000)
6711 {
6712 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6713 if (attr == 0)
6714 attr = hl_attr(HLF_8);
6715 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717# ifdef FEAT_ARABIC
6718 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6719 {
6720 /* Do Arabic shaping. */
6721 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6722 {
6723 /* Past end of string to be displayed. */
6724 nc = NUL;
6725 nc1 = NUL;
6726 }
6727 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006728 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006729 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6730 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006731 nc1 = pcc[0];
6732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 pc = prev_c;
6734 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006735 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
6737 else
6738 prev_c = u8c;
6739# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006740 if (col + mbyte_cells > screen_Columns)
6741 {
6742 /* Only 1 cell left, but character requires 2 cells:
6743 * display a '>' in the last column to avoid wrapping. */
6744 c = '>';
6745 mbyte_cells = 1;
6746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
6748 }
6749#endif
6750
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006751#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6752 force_redraw_this = force_redraw_next;
6753 force_redraw_next = FALSE;
6754#endif
6755
6756 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757#ifdef FEAT_MBYTE
6758 || (mbyte_cells == 2
6759 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6760 || (enc_dbcs == DBCS_JPNU
6761 && c == 0x8e
6762 && ScreenLines2[off] != ptr[1])
6763 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006764 && (ScreenLinesUC[off] !=
6765 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6766 || (ScreenLinesUC[off] != 0
6767 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768#endif
6769 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006770 || exmode_active;
6771
6772 if (need_redraw
6773#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6774 || force_redraw_this
6775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 )
6777 {
6778#if defined(FEAT_GUI) || defined(UNIX)
6779 /* The bold trick makes a single row of pixels appear in the next
6780 * character. When a bold character is removed, the next
6781 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006782 * and for some xterms. */
6783 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784# ifdef FEAT_GUI
6785 gui.in_use
6786# endif
6787# if defined(FEAT_GUI) && defined(UNIX)
6788 ||
6789# endif
6790# ifdef UNIX
6791 term_is_xterm
6792# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006793 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006795 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006797 if (n > HL_ALL)
6798 n = syn_attr2attr(n);
6799 if (n & HL_BOLD)
6800 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 }
6802#endif
6803#ifdef FEAT_MBYTE
6804 /* When at the end of the text and overwriting a two-cell
6805 * character with a one-cell character, need to clear the next
6806 * cell. Also when overwriting the left halve of a two-cell char
6807 * with the right halve of a two-cell char. Do this only once
6808 * (mb_off2cells() may return 2 on the right halve). */
6809 if (clear_next_cell)
6810 clear_next_cell = FALSE;
6811 else if (has_mbyte
6812 && (len < 0 ? ptr[mbyte_blen] == NUL
6813 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006814 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006816 && (*mb_off2cells)(off, max_off) == 1
6817 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 clear_next_cell = TRUE;
6819
6820 /* Make sure we never leave a second byte of a double-byte behind,
6821 * it confuses mb_off2cells(). */
6822 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006823 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006825 && (*mb_off2cells)(off, max_off) == 1
6826 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 ScreenLines[off + mbyte_blen] = 0;
6828#endif
6829 ScreenLines[off] = c;
6830 ScreenAttrs[off] = attr;
6831#ifdef FEAT_MBYTE
6832 if (enc_utf8)
6833 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006834 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 ScreenLinesUC[off] = 0;
6836 else
6837 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006838 int i;
6839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006841 for (i = 0; i < Screen_mco; ++i)
6842 {
6843 ScreenLinesC[i][off] = u8cc[i];
6844 if (u8cc[i] == 0)
6845 break;
6846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
6848 if (mbyte_cells == 2)
6849 {
6850 ScreenLines[off + 1] = 0;
6851 ScreenAttrs[off + 1] = attr;
6852 }
6853 screen_char(off, row, col);
6854 }
6855 else if (mbyte_cells == 2)
6856 {
6857 ScreenLines[off + 1] = ptr[1];
6858 ScreenAttrs[off + 1] = attr;
6859 screen_char_2(off, row, col);
6860 }
6861 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6862 {
6863 ScreenLines2[off] = ptr[1];
6864 screen_char(off, row, col);
6865 }
6866 else
6867#endif
6868 screen_char(off, row, col);
6869 }
6870#ifdef FEAT_MBYTE
6871 if (has_mbyte)
6872 {
6873 off += mbyte_cells;
6874 col += mbyte_cells;
6875 ptr += mbyte_blen;
6876 if (clear_next_cell)
6877 ptr = (char_u *)" ";
6878 }
6879 else
6880#endif
6881 {
6882 ++off;
6883 ++col;
6884 ++ptr;
6885 }
6886 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006887
6888#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6889 /* If we detected the next character needs to be redrawn, but the text
6890 * doesn't extend up to there, update the character here. */
6891 if (force_redraw_next && col < screen_Columns)
6892 {
6893# ifdef FEAT_MBYTE
6894 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6895 screen_char_2(off, row, col);
6896 else
6897# endif
6898 screen_char(off, row, col);
6899 }
6900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901}
6902
6903#ifdef FEAT_SEARCH_EXTRA
6904/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006905 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 */
6907 static void
6908start_search_hl()
6909{
6910 if (p_hls && !no_hlsearch)
6911 {
6912 last_pat_prog(&search_hl.rm);
6913 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006914# ifdef FEAT_RELTIME
6915 /* Set the time limit to 'redrawtime'. */
6916 profile_setlimit(p_rdt, &search_hl.tm);
6917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 }
6919}
6920
6921/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006922 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 */
6924 static void
6925end_search_hl()
6926{
6927 if (search_hl.rm.regprog != NULL)
6928 {
6929 vim_free(search_hl.rm.regprog);
6930 search_hl.rm.regprog = NULL;
6931 }
6932}
6933
6934/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02006935 * Init for calling prepare_search_hl().
6936 */
6937 static void
6938init_search_hl(wp)
6939 win_T *wp;
6940{
6941 matchitem_T *cur;
6942
6943 /* Setup for match and 'hlsearch' highlighting. Disable any previous
6944 * match */
6945 cur = wp->w_match_head;
6946 while (cur != NULL)
6947 {
6948 cur->hl.rm = cur->match;
6949 if (cur->hlg_id == 0)
6950 cur->hl.attr = 0;
6951 else
6952 cur->hl.attr = syn_id2attr(cur->hlg_id);
6953 cur->hl.buf = wp->w_buffer;
6954 cur->hl.lnum = 0;
6955 cur->hl.first_lnum = 0;
6956# ifdef FEAT_RELTIME
6957 /* Set the time limit to 'redrawtime'. */
6958 profile_setlimit(p_rdt, &(cur->hl.tm));
6959# endif
6960 cur = cur->next;
6961 }
6962 search_hl.buf = wp->w_buffer;
6963 search_hl.lnum = 0;
6964 search_hl.first_lnum = 0;
6965 /* time limit is set at the toplevel, for all windows */
6966}
6967
6968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 * Advance to the match in window "wp" line "lnum" or past it.
6970 */
6971 static void
6972prepare_search_hl(wp, lnum)
6973 win_T *wp;
6974 linenr_T lnum;
6975{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006976 matchitem_T *cur; /* points to the match list */
6977 match_T *shl; /* points to search_hl or a match */
6978 int shl_flag; /* flag to indicate whether search_hl
6979 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 int n;
6981
6982 /*
6983 * When using a multi-line pattern, start searching at the top
6984 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006985 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006987 cur = wp->w_match_head;
6988 shl_flag = FALSE;
6989 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006991 if (shl_flag == FALSE)
6992 {
6993 shl = &search_hl;
6994 shl_flag = TRUE;
6995 }
6996 else
6997 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (shl->rm.regprog != NULL
6999 && shl->lnum == 0
7000 && re_multiline(shl->rm.regprog))
7001 {
7002 if (shl->first_lnum == 0)
7003 {
7004# ifdef FEAT_FOLDING
7005 for (shl->first_lnum = lnum;
7006 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7007 if (hasFoldingWin(wp, shl->first_lnum - 1,
7008 NULL, NULL, TRUE, NULL))
7009 break;
7010# else
7011 shl->first_lnum = wp->w_topline;
7012# endif
7013 }
7014 n = 0;
7015 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7016 {
7017 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7018 if (shl->lnum != 0)
7019 {
7020 shl->first_lnum = shl->lnum
7021 + shl->rm.endpos[0].lnum
7022 - shl->rm.startpos[0].lnum;
7023 n = shl->rm.endpos[0].col;
7024 }
7025 else
7026 {
7027 ++shl->first_lnum;
7028 n = 0;
7029 }
7030 }
7031 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007032 if (shl != &search_hl && cur != NULL)
7033 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 }
7035}
7036
7037/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007038 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 * Uses shl->buf.
7040 * Sets shl->lnum and shl->rm contents.
7041 * Note: Assumes a previous match is always before "lnum", unless
7042 * shl->lnum is zero.
7043 * Careful: Any pointers for buffer lines will become invalid.
7044 */
7045 static void
7046next_search_hl(win, shl, lnum, mincol)
7047 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007048 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 linenr_T lnum;
7050 colnr_T mincol; /* minimal column for a match */
7051{
7052 linenr_T l;
7053 colnr_T matchcol;
7054 long nmatched;
7055
7056 if (shl->lnum != 0)
7057 {
7058 /* Check for three situations:
7059 * 1. If the "lnum" is below a previous match, start a new search.
7060 * 2. If the previous match includes "mincol", use it.
7061 * 3. Continue after the previous match.
7062 */
7063 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7064 if (lnum > l)
7065 shl->lnum = 0;
7066 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7067 return;
7068 }
7069
7070 /*
7071 * Repeat searching for a match until one is found that includes "mincol"
7072 * or none is found in this line.
7073 */
7074 called_emsg = FALSE;
7075 for (;;)
7076 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007077#ifdef FEAT_RELTIME
7078 /* Stop searching after passing the time limit. */
7079 if (profile_passed_limit(&(shl->tm)))
7080 {
7081 shl->lnum = 0; /* no match found in time */
7082 break;
7083 }
7084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 /* Three situations:
7086 * 1. No useful previous match: search from start of line.
7087 * 2. Not Vi compatible or empty match: continue at next character.
7088 * Break the loop if this is beyond the end of the line.
7089 * 3. Vi compatible searching: continue at end of previous match.
7090 */
7091 if (shl->lnum == 0)
7092 matchcol = 0;
7093 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7094 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007095 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007097 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007098
7099 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007100 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007101 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007103 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 shl->lnum = 0;
7105 break;
7106 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007107#ifdef FEAT_MBYTE
7108 if (has_mbyte)
7109 matchcol += mb_ptr2len(ml);
7110 else
7111#endif
7112 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 }
7114 else
7115 matchcol = shl->rm.endpos[0].col;
7116
7117 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007118 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7119#ifdef FEAT_RELTIME
7120 &(shl->tm)
7121#else
7122 NULL
7123#endif
7124 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 if (called_emsg)
7126 {
7127 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007128 if (shl == &search_hl)
7129 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007130 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007131 vim_free(shl->rm.regprog);
7132 no_hlsearch = TRUE;
7133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007135 shl->lnum = 0;
7136 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 break;
7138 }
7139 if (nmatched == 0)
7140 {
7141 shl->lnum = 0; /* no match found */
7142 break;
7143 }
7144 if (shl->rm.startpos[0].lnum > 0
7145 || shl->rm.startpos[0].col >= mincol
7146 || nmatched > 1
7147 || shl->rm.endpos[0].col > mincol)
7148 {
7149 shl->lnum += shl->rm.startpos[0].lnum;
7150 break; /* useful match found */
7151 }
7152 }
7153}
7154#endif
7155
7156 static void
7157screen_start_highlight(attr)
7158 int attr;
7159{
7160 attrentry_T *aep = NULL;
7161
7162 screen_attr = attr;
7163 if (full_screen
7164#ifdef WIN3264
7165 && termcap_active
7166#endif
7167 )
7168 {
7169#ifdef FEAT_GUI
7170 if (gui.in_use)
7171 {
7172 char buf[20];
7173
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007174 /* The GUI handles this internally. */
7175 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 OUT_STR(buf);
7177 }
7178 else
7179#endif
7180 {
7181 if (attr > HL_ALL) /* special HL attr. */
7182 {
7183 if (t_colors > 1)
7184 aep = syn_cterm_attr2entry(attr);
7185 else
7186 aep = syn_term_attr2entry(attr);
7187 if (aep == NULL) /* did ":syntax clear" */
7188 attr = 0;
7189 else
7190 attr = aep->ae_attr;
7191 }
7192 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7193 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007194 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7195 && cterm_normal_fg_bold)
7196 /* If the Normal FG color has BOLD attribute and the new HL
7197 * has a FG color defined, clear BOLD. */
7198 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7200 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007201 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7202 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 out_str(T_US);
7204 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7205 out_str(T_CZH);
7206 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7207 out_str(T_MR);
7208
7209 /*
7210 * Output the color or start string after bold etc., in case the
7211 * bold etc. override the color setting.
7212 */
7213 if (aep != NULL)
7214 {
7215 if (t_colors > 1)
7216 {
7217 if (aep->ae_u.cterm.fg_color)
7218 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7219 if (aep->ae_u.cterm.bg_color)
7220 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7221 }
7222 else
7223 {
7224 if (aep->ae_u.term.start != NULL)
7225 out_str(aep->ae_u.term.start);
7226 }
7227 }
7228 }
7229 }
7230}
7231
7232 void
7233screen_stop_highlight()
7234{
7235 int do_ME = FALSE; /* output T_ME code */
7236
7237 if (screen_attr != 0
7238#ifdef WIN3264
7239 && termcap_active
7240#endif
7241 )
7242 {
7243#ifdef FEAT_GUI
7244 if (gui.in_use)
7245 {
7246 char buf[20];
7247
7248 /* use internal GUI code */
7249 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7250 OUT_STR(buf);
7251 }
7252 else
7253#endif
7254 {
7255 if (screen_attr > HL_ALL) /* special HL attr. */
7256 {
7257 attrentry_T *aep;
7258
7259 if (t_colors > 1)
7260 {
7261 /*
7262 * Assume that t_me restores the original colors!
7263 */
7264 aep = syn_cterm_attr2entry(screen_attr);
7265 if (aep != NULL && (aep->ae_u.cterm.fg_color
7266 || aep->ae_u.cterm.bg_color))
7267 do_ME = TRUE;
7268 }
7269 else
7270 {
7271 aep = syn_term_attr2entry(screen_attr);
7272 if (aep != NULL && aep->ae_u.term.stop != NULL)
7273 {
7274 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7275 do_ME = TRUE;
7276 else
7277 out_str(aep->ae_u.term.stop);
7278 }
7279 }
7280 if (aep == NULL) /* did ":syntax clear" */
7281 screen_attr = 0;
7282 else
7283 screen_attr = aep->ae_attr;
7284 }
7285
7286 /*
7287 * Often all ending-codes are equal to T_ME. Avoid outputting the
7288 * same sequence several times.
7289 */
7290 if (screen_attr & HL_STANDOUT)
7291 {
7292 if (STRCMP(T_SE, T_ME) == 0)
7293 do_ME = TRUE;
7294 else
7295 out_str(T_SE);
7296 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007297 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
7299 if (STRCMP(T_UE, T_ME) == 0)
7300 do_ME = TRUE;
7301 else
7302 out_str(T_UE);
7303 }
7304 if (screen_attr & HL_ITALIC)
7305 {
7306 if (STRCMP(T_CZR, T_ME) == 0)
7307 do_ME = TRUE;
7308 else
7309 out_str(T_CZR);
7310 }
7311 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7312 out_str(T_ME);
7313
7314 if (t_colors > 1)
7315 {
7316 /* set Normal cterm colors */
7317 if (cterm_normal_fg_color != 0)
7318 term_fg_color(cterm_normal_fg_color - 1);
7319 if (cterm_normal_bg_color != 0)
7320 term_bg_color(cterm_normal_bg_color - 1);
7321 if (cterm_normal_fg_bold)
7322 out_str(T_MD);
7323 }
7324 }
7325 }
7326 screen_attr = 0;
7327}
7328
7329/*
7330 * Reset the colors for a cterm. Used when leaving Vim.
7331 * The machine specific code may override this again.
7332 */
7333 void
7334reset_cterm_colors()
7335{
7336 if (t_colors > 1)
7337 {
7338 /* set Normal cterm colors */
7339 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7340 {
7341 out_str(T_OP);
7342 screen_attr = -1;
7343 }
7344 if (cterm_normal_fg_bold)
7345 {
7346 out_str(T_ME);
7347 screen_attr = -1;
7348 }
7349 }
7350}
7351
7352/*
7353 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7354 * using the attributes from ScreenAttrs["off"].
7355 */
7356 static void
7357screen_char(off, row, col)
7358 unsigned off;
7359 int row;
7360 int col;
7361{
7362 int attr;
7363
7364 /* Check for illegal values, just in case (could happen just after
7365 * resizing). */
7366 if (row >= screen_Rows || col >= screen_Columns)
7367 return;
7368
7369 /* Outputting the last character on the screen may scrollup the screen.
7370 * Don't to it! Mark the character invalid (update it when scrolled up) */
7371 if (row == screen_Rows - 1 && col == screen_Columns - 1
7372#ifdef FEAT_RIGHTLEFT
7373 /* account for first command-line character in rightleft mode */
7374 && !cmdmsg_rl
7375#endif
7376 )
7377 {
7378 ScreenAttrs[off] = (sattr_T)-1;
7379 return;
7380 }
7381
7382 /*
7383 * Stop highlighting first, so it's easier to move the cursor.
7384 */
7385#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7386 if (screen_char_attr != 0)
7387 attr = screen_char_attr;
7388 else
7389#endif
7390 attr = ScreenAttrs[off];
7391 if (screen_attr != attr)
7392 screen_stop_highlight();
7393
7394 windgoto(row, col);
7395
7396 if (screen_attr != attr)
7397 screen_start_highlight(attr);
7398
7399#ifdef FEAT_MBYTE
7400 if (enc_utf8 && ScreenLinesUC[off] != 0)
7401 {
7402 char_u buf[MB_MAXBYTES + 1];
7403
7404 /* Convert UTF-8 character to bytes and write it. */
7405
7406 buf[utfc_char2bytes(off, buf)] = NUL;
7407
7408 out_str(buf);
7409 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7410 ++screen_cur_col;
7411 }
7412 else
7413#endif
7414 {
7415#ifdef FEAT_MBYTE
7416 out_flush_check();
7417#endif
7418 out_char(ScreenLines[off]);
7419#ifdef FEAT_MBYTE
7420 /* double-byte character in single-width cell */
7421 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7422 out_char(ScreenLines2[off]);
7423#endif
7424 }
7425
7426 screen_cur_col++;
7427}
7428
7429#ifdef FEAT_MBYTE
7430
7431/*
7432 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7433 * on the screen at position 'row' and 'col'.
7434 * The attributes of the first byte is used for all. This is required to
7435 * output the two bytes of a double-byte character with nothing in between.
7436 */
7437 static void
7438screen_char_2(off, row, col)
7439 unsigned off;
7440 int row;
7441 int col;
7442{
7443 /* Check for illegal values (could be wrong when screen was resized). */
7444 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7445 return;
7446
7447 /* Outputting the last character on the screen may scrollup the screen.
7448 * Don't to it! Mark the character invalid (update it when scrolled up) */
7449 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7450 {
7451 ScreenAttrs[off] = (sattr_T)-1;
7452 return;
7453 }
7454
7455 /* Output the first byte normally (positions the cursor), then write the
7456 * second byte directly. */
7457 screen_char(off, row, col);
7458 out_char(ScreenLines[off + 1]);
7459 ++screen_cur_col;
7460}
7461#endif
7462
7463#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7464/*
7465 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7466 * This uses the contents of ScreenLines[] and doesn't change it.
7467 */
7468 void
7469screen_draw_rectangle(row, col, height, width, invert)
7470 int row;
7471 int col;
7472 int height;
7473 int width;
7474 int invert;
7475{
7476 int r, c;
7477 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007478#ifdef FEAT_MBYTE
7479 int max_off;
7480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007482 /* Can't use ScreenLines unless initialized */
7483 if (ScreenLines == NULL)
7484 return;
7485
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 if (invert)
7487 screen_char_attr = HL_INVERSE;
7488 for (r = row; r < row + height; ++r)
7489 {
7490 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007491#ifdef FEAT_MBYTE
7492 max_off = off + screen_Columns;
7493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 for (c = col; c < col + width; ++c)
7495 {
7496#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007497 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 {
7499 screen_char_2(off + c, r, c);
7500 ++c;
7501 }
7502 else
7503#endif
7504 {
7505 screen_char(off + c, r, c);
7506#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007507 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 ++c;
7509#endif
7510 }
7511 }
7512 }
7513 screen_char_attr = 0;
7514}
7515#endif
7516
7517#ifdef FEAT_VERTSPLIT
7518/*
7519 * Redraw the characters for a vertically split window.
7520 */
7521 static void
7522redraw_block(row, end, wp)
7523 int row;
7524 int end;
7525 win_T *wp;
7526{
7527 int col;
7528 int width;
7529
7530# ifdef FEAT_CLIPBOARD
7531 clip_may_clear_selection(row, end - 1);
7532# endif
7533
7534 if (wp == NULL)
7535 {
7536 col = 0;
7537 width = Columns;
7538 }
7539 else
7540 {
7541 col = wp->w_wincol;
7542 width = wp->w_width;
7543 }
7544 screen_draw_rectangle(row, col, end - row, width, FALSE);
7545}
7546#endif
7547
7548/*
7549 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7550 * with character 'c1' in first column followed by 'c2' in the other columns.
7551 * Use attributes 'attr'.
7552 */
7553 void
7554screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7555 int start_row, end_row;
7556 int start_col, end_col;
7557 int c1, c2;
7558 int attr;
7559{
7560 int row;
7561 int col;
7562 int off;
7563 int end_off;
7564 int did_delete;
7565 int c;
7566 int norm_term;
7567#if defined(FEAT_GUI) || defined(UNIX)
7568 int force_next = FALSE;
7569#endif
7570
7571 if (end_row > screen_Rows) /* safety check */
7572 end_row = screen_Rows;
7573 if (end_col > screen_Columns) /* safety check */
7574 end_col = screen_Columns;
7575 if (ScreenLines == NULL
7576 || start_row >= end_row
7577 || start_col >= end_col) /* nothing to do */
7578 return;
7579
7580 /* it's a "normal" terminal when not in a GUI or cterm */
7581 norm_term = (
7582#ifdef FEAT_GUI
7583 !gui.in_use &&
7584#endif
7585 t_colors <= 1);
7586 for (row = start_row; row < end_row; ++row)
7587 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007588#ifdef FEAT_MBYTE
7589 if (has_mbyte
7590# ifdef FEAT_GUI
7591 && !gui.in_use
7592# endif
7593 )
7594 {
7595 /* When drawing over the right halve of a double-wide char clear
7596 * out the left halve. When drawing over the left halve of a
7597 * double wide-char clear out the right halve. Only needed in a
7598 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007599 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007600 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007601 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007602 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007603 }
7604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 /*
7606 * Try to use delete-line termcap code, when no attributes or in a
7607 * "normal" terminal, where a bold/italic space is just a
7608 * space.
7609 */
7610 did_delete = FALSE;
7611 if (c2 == ' '
7612 && end_col == Columns
7613 && can_clear(T_CE)
7614 && (attr == 0
7615 || (norm_term
7616 && attr <= HL_ALL
7617 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7618 {
7619 /*
7620 * check if we really need to clear something
7621 */
7622 col = start_col;
7623 if (c1 != ' ') /* don't clear first char */
7624 ++col;
7625
7626 off = LineOffset[row] + col;
7627 end_off = LineOffset[row] + end_col;
7628
7629 /* skip blanks (used often, keep it fast!) */
7630#ifdef FEAT_MBYTE
7631 if (enc_utf8)
7632 while (off < end_off && ScreenLines[off] == ' '
7633 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7634 ++off;
7635 else
7636#endif
7637 while (off < end_off && ScreenLines[off] == ' '
7638 && ScreenAttrs[off] == 0)
7639 ++off;
7640 if (off < end_off) /* something to be cleared */
7641 {
7642 col = off - LineOffset[row];
7643 screen_stop_highlight();
7644 term_windgoto(row, col);/* clear rest of this screen line */
7645 out_str(T_CE);
7646 screen_start(); /* don't know where cursor is now */
7647 col = end_col - col;
7648 while (col--) /* clear chars in ScreenLines */
7649 {
7650 ScreenLines[off] = ' ';
7651#ifdef FEAT_MBYTE
7652 if (enc_utf8)
7653 ScreenLinesUC[off] = 0;
7654#endif
7655 ScreenAttrs[off] = 0;
7656 ++off;
7657 }
7658 }
7659 did_delete = TRUE; /* the chars are cleared now */
7660 }
7661
7662 off = LineOffset[row] + start_col;
7663 c = c1;
7664 for (col = start_col; col < end_col; ++col)
7665 {
7666 if (ScreenLines[off] != c
7667#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007668 || (enc_utf8 && (int)ScreenLinesUC[off]
7669 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670#endif
7671 || ScreenAttrs[off] != attr
7672#if defined(FEAT_GUI) || defined(UNIX)
7673 || force_next
7674#endif
7675 )
7676 {
7677#if defined(FEAT_GUI) || defined(UNIX)
7678 /* The bold trick may make a single row of pixels appear in
7679 * the next character. When a bold character is removed, the
7680 * next character should be redrawn too. This happens for our
7681 * own GUI and for some xterms. */
7682 if (
7683# ifdef FEAT_GUI
7684 gui.in_use
7685# endif
7686# if defined(FEAT_GUI) && defined(UNIX)
7687 ||
7688# endif
7689# ifdef UNIX
7690 term_is_xterm
7691# endif
7692 )
7693 {
7694 if (ScreenLines[off] != ' '
7695 && (ScreenAttrs[off] > HL_ALL
7696 || ScreenAttrs[off] & HL_BOLD))
7697 force_next = TRUE;
7698 else
7699 force_next = FALSE;
7700 }
7701#endif
7702 ScreenLines[off] = c;
7703#ifdef FEAT_MBYTE
7704 if (enc_utf8)
7705 {
7706 if (c >= 0x80)
7707 {
7708 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007709 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 }
7711 else
7712 ScreenLinesUC[off] = 0;
7713 }
7714#endif
7715 ScreenAttrs[off] = attr;
7716 if (!did_delete || c != ' ')
7717 screen_char(off, row, col);
7718 }
7719 ++off;
7720 if (col == start_col)
7721 {
7722 if (did_delete)
7723 break;
7724 c = c2;
7725 }
7726 }
7727 if (end_col == Columns)
7728 LineWraps[row] = FALSE;
7729 if (row == Rows - 1) /* overwritten the command line */
7730 {
7731 redraw_cmdline = TRUE;
7732 if (c1 == ' ' && c2 == ' ')
7733 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007734 if (start_col == 0)
7735 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 }
7737 }
7738}
7739
7740/*
7741 * Check if there should be a delay. Used before clearing or redrawing the
7742 * screen or the command line.
7743 */
7744 void
7745check_for_delay(check_msg_scroll)
7746 int check_msg_scroll;
7747{
7748 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7749 && !did_wait_return
7750 && emsg_silent == 0)
7751 {
7752 out_flush();
7753 ui_delay(1000L, TRUE);
7754 emsg_on_display = FALSE;
7755 if (check_msg_scroll)
7756 msg_scroll = FALSE;
7757 }
7758}
7759
7760/*
7761 * screen_valid - allocate screen buffers if size changed
7762 * If "clear" is TRUE: clear screen if it has been resized.
7763 * Returns TRUE if there is a valid screen to write to.
7764 * Returns FALSE when starting up and screen not initialized yet.
7765 */
7766 int
7767screen_valid(clear)
7768 int clear;
7769{
7770 screenalloc(clear); /* allocate screen buffers if size changed */
7771 return (ScreenLines != NULL);
7772}
7773
7774/*
7775 * Resize the shell to Rows and Columns.
7776 * Allocate ScreenLines[] and associated items.
7777 *
7778 * There may be some time between setting Rows and Columns and (re)allocating
7779 * ScreenLines[]. This happens when starting up and when (manually) changing
7780 * the shell size. Always use screen_Rows and screen_Columns to access items
7781 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7782 * final size of the shell is needed.
7783 */
7784 void
7785screenalloc(clear)
7786 int clear;
7787{
7788 int new_row, old_row;
7789#ifdef FEAT_GUI
7790 int old_Rows;
7791#endif
7792 win_T *wp;
7793 int outofmem = FALSE;
7794 int len;
7795 schar_T *new_ScreenLines;
7796#ifdef FEAT_MBYTE
7797 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007798 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007800 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801#endif
7802 sattr_T *new_ScreenAttrs;
7803 unsigned *new_LineOffset;
7804 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007805#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007806 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007807 tabpage_T *tp;
7808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007810 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007811#ifdef FEAT_AUTOCMD
7812 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007814retry:
7815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 /*
7817 * Allocation of the screen buffers is done only when the size changes and
7818 * when Rows and Columns have been set and we have started doing full
7819 * screen stuff.
7820 */
7821 if ((ScreenLines != NULL
7822 && Rows == screen_Rows
7823 && Columns == screen_Columns
7824#ifdef FEAT_MBYTE
7825 && enc_utf8 == (ScreenLinesUC != NULL)
7826 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007827 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828#endif
7829 )
7830 || Rows == 0
7831 || Columns == 0
7832 || (!full_screen && ScreenLines == NULL))
7833 return;
7834
7835 /*
7836 * It's possible that we produce an out-of-memory message below, which
7837 * will cause this function to be called again. To break the loop, just
7838 * return here.
7839 */
7840 if (entered)
7841 return;
7842 entered = TRUE;
7843
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007844 /*
7845 * Note that the window sizes are updated before reallocating the arrays,
7846 * thus we must not redraw here!
7847 */
7848 ++RedrawingDisabled;
7849
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 win_new_shellsize(); /* fit the windows in the new sized shell */
7851
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 comp_col(); /* recompute columns for shown command and ruler */
7853
7854 /*
7855 * We're changing the size of the screen.
7856 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7857 * - Move lines from the old arrays into the new arrays, clear extra
7858 * lines (unless the screen is going to be cleared).
7859 * - Free the old arrays.
7860 *
7861 * If anything fails, make ScreenLines NULL, so we don't do anything!
7862 * Continuing with the old ScreenLines may result in a crash, because the
7863 * size is wrong.
7864 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007865 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007867#ifdef FEAT_AUTOCMD
7868 if (aucmd_win != NULL)
7869 win_free_lsize(aucmd_win);
7870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871
7872 new_ScreenLines = (schar_T *)lalloc((long_u)(
7873 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7874#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007875 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 if (enc_utf8)
7877 {
7878 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7879 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007880 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007881 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7883 }
7884 if (enc_dbcs == DBCS_JPNU)
7885 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7886 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7887#endif
7888 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7889 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7890 new_LineOffset = (unsigned *)lalloc((long_u)(
7891 Rows * sizeof(unsigned)), FALSE);
7892 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007893#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007894 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007897 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {
7899 if (win_alloc_lines(wp) == FAIL)
7900 {
7901 outofmem = TRUE;
7902#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007903 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904#endif
7905 }
7906 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007907#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007908 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7909 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007910 outofmem = TRUE;
7911#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007912#ifdef FEAT_WINDOWS
7913give_up:
7914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007916#ifdef FEAT_MBYTE
7917 for (i = 0; i < p_mco; ++i)
7918 if (new_ScreenLinesC[i] == NULL)
7919 break;
7920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 if (new_ScreenLines == NULL
7922#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007923 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7925#endif
7926 || new_ScreenAttrs == NULL
7927 || new_LineOffset == NULL
7928 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007929#ifdef FEAT_WINDOWS
7930 || new_TabPageIdxs == NULL
7931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 || outofmem)
7933 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007934 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007935 {
7936 /* guess the size */
7937 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7938
7939 /* Remember we did this to avoid getting outofmem messages over
7940 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007941 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 vim_free(new_ScreenLines);
7944 new_ScreenLines = NULL;
7945#ifdef FEAT_MBYTE
7946 vim_free(new_ScreenLinesUC);
7947 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007948 for (i = 0; i < p_mco; ++i)
7949 {
7950 vim_free(new_ScreenLinesC[i]);
7951 new_ScreenLinesC[i] = NULL;
7952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 vim_free(new_ScreenLines2);
7954 new_ScreenLines2 = NULL;
7955#endif
7956 vim_free(new_ScreenAttrs);
7957 new_ScreenAttrs = NULL;
7958 vim_free(new_LineOffset);
7959 new_LineOffset = NULL;
7960 vim_free(new_LineWraps);
7961 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007962#ifdef FEAT_WINDOWS
7963 vim_free(new_TabPageIdxs);
7964 new_TabPageIdxs = NULL;
7965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 }
7967 else
7968 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007969 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007970
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 for (new_row = 0; new_row < Rows; ++new_row)
7972 {
7973 new_LineOffset[new_row] = new_row * Columns;
7974 new_LineWraps[new_row] = FALSE;
7975
7976 /*
7977 * If the screen is not going to be cleared, copy as much as
7978 * possible from the old screen to the new one and clear the rest
7979 * (used when resizing the window at the "--more--" prompt or when
7980 * executing an external command, for the GUI).
7981 */
7982 if (!clear)
7983 {
7984 (void)vim_memset(new_ScreenLines + new_row * Columns,
7985 ' ', (size_t)Columns * sizeof(schar_T));
7986#ifdef FEAT_MBYTE
7987 if (enc_utf8)
7988 {
7989 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7990 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007991 for (i = 0; i < p_mco; ++i)
7992 (void)vim_memset(new_ScreenLinesC[i]
7993 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 0, (size_t)Columns * sizeof(u8char_T));
7995 }
7996 if (enc_dbcs == DBCS_JPNU)
7997 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7998 0, (size_t)Columns * sizeof(schar_T));
7999#endif
8000 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8001 0, (size_t)Columns * sizeof(sattr_T));
8002 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008003 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {
8005 if (screen_Columns < Columns)
8006 len = screen_Columns;
8007 else
8008 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008009#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008010 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008011 * may be invalid now. Also when p_mco changes. */
8012 if (!(enc_utf8 && ScreenLinesUC == NULL)
8013 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008014#endif
8015 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8016 ScreenLines + LineOffset[old_row],
8017 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008019 if (enc_utf8 && ScreenLinesUC != NULL
8020 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {
8022 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8023 ScreenLinesUC + LineOffset[old_row],
8024 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008025 for (i = 0; i < p_mco; ++i)
8026 mch_memmove(new_ScreenLinesC[i]
8027 + new_LineOffset[new_row],
8028 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 (size_t)len * sizeof(u8char_T));
8030 }
8031 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8032 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8033 ScreenLines2 + LineOffset[old_row],
8034 (size_t)len * sizeof(schar_T));
8035#endif
8036 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8037 ScreenAttrs + LineOffset[old_row],
8038 (size_t)len * sizeof(sattr_T));
8039 }
8040 }
8041 }
8042 /* Use the last line of the screen for the current line. */
8043 current_ScreenLine = new_ScreenLines + Rows * Columns;
8044 }
8045
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008046 free_screenlines();
8047
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 ScreenLines = new_ScreenLines;
8049#ifdef FEAT_MBYTE
8050 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008051 for (i = 0; i < p_mco; ++i)
8052 ScreenLinesC[i] = new_ScreenLinesC[i];
8053 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 ScreenLines2 = new_ScreenLines2;
8055#endif
8056 ScreenAttrs = new_ScreenAttrs;
8057 LineOffset = new_LineOffset;
8058 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008059#ifdef FEAT_WINDOWS
8060 TabPageIdxs = new_TabPageIdxs;
8061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
8063 /* It's important that screen_Rows and screen_Columns reflect the actual
8064 * size of ScreenLines[]. Set them before calling anything. */
8065#ifdef FEAT_GUI
8066 old_Rows = screen_Rows;
8067#endif
8068 screen_Rows = Rows;
8069 screen_Columns = Columns;
8070
8071 must_redraw = CLEAR; /* need to clear the screen later */
8072 if (clear)
8073 screenclear2();
8074
8075#ifdef FEAT_GUI
8076 else if (gui.in_use
8077 && !gui.starting
8078 && ScreenLines != NULL
8079 && old_Rows != Rows)
8080 {
8081 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8082 /*
8083 * Adjust the position of the cursor, for when executing an external
8084 * command.
8085 */
8086 if (msg_row >= Rows) /* Rows got smaller */
8087 msg_row = Rows - 1; /* put cursor at last row */
8088 else if (Rows > old_Rows) /* Rows got bigger */
8089 msg_row += Rows - old_Rows; /* put cursor in same place */
8090 if (msg_col >= Columns) /* Columns got smaller */
8091 msg_col = Columns - 1; /* put cursor at last column */
8092 }
8093#endif
8094
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008096 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008097
8098#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008099 /*
8100 * Do not apply autocommands more than 3 times to avoid an endless loop
8101 * in case applying autocommands always changes Rows or Columns.
8102 */
8103 if (starting == 0 && ++retry_count <= 3)
8104 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008105 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008106 /* In rare cases, autocommands may have altered Rows or Columns,
8107 * jump back to check if we need to allocate the screen again. */
8108 goto retry;
8109 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111}
8112
8113 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008114free_screenlines()
8115{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008116#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008117 int i;
8118
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008119 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008120 for (i = 0; i < Screen_mco; ++i)
8121 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008122 vim_free(ScreenLines2);
8123#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008124 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008125 vim_free(ScreenAttrs);
8126 vim_free(LineOffset);
8127 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008128#ifdef FEAT_WINDOWS
8129 vim_free(TabPageIdxs);
8130#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008131}
8132
8133 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134screenclear()
8135{
8136 check_for_delay(FALSE);
8137 screenalloc(FALSE); /* allocate screen buffers if size changed */
8138 screenclear2(); /* clear the screen */
8139}
8140
8141 static void
8142screenclear2()
8143{
8144 int i;
8145
8146 if (starting == NO_SCREEN || ScreenLines == NULL
8147#ifdef FEAT_GUI
8148 || (gui.in_use && gui.starting)
8149#endif
8150 )
8151 return;
8152
8153#ifdef FEAT_GUI
8154 if (!gui.in_use)
8155#endif
8156 screen_attr = -1; /* force setting the Normal colors */
8157 screen_stop_highlight(); /* don't want highlighting here */
8158
8159#ifdef FEAT_CLIPBOARD
8160 /* disable selection without redrawing it */
8161 clip_scroll_selection(9999);
8162#endif
8163
8164 /* blank out ScreenLines */
8165 for (i = 0; i < Rows; ++i)
8166 {
8167 lineclear(LineOffset[i], (int)Columns);
8168 LineWraps[i] = FALSE;
8169 }
8170
8171 if (can_clear(T_CL))
8172 {
8173 out_str(T_CL); /* clear the display */
8174 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008175 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
8177 else
8178 {
8179 /* can't clear the screen, mark all chars with invalid attributes */
8180 for (i = 0; i < Rows; ++i)
8181 lineinvalid(LineOffset[i], (int)Columns);
8182 clear_cmdline = TRUE;
8183 }
8184
8185 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8186
8187 win_rest_invalid(firstwin);
8188 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008189#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008190 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (must_redraw == CLEAR) /* no need to clear again */
8193 must_redraw = NOT_VALID;
8194 compute_cmdrow();
8195 msg_row = cmdline_row; /* put cursor on last line for messages */
8196 msg_col = 0;
8197 screen_start(); /* don't know where cursor is now */
8198 msg_scrolled = 0; /* can't scroll back */
8199 msg_didany = FALSE;
8200 msg_didout = FALSE;
8201}
8202
8203/*
8204 * Clear one line in ScreenLines.
8205 */
8206 static void
8207lineclear(off, width)
8208 unsigned off;
8209 int width;
8210{
8211 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8212#ifdef FEAT_MBYTE
8213 if (enc_utf8)
8214 (void)vim_memset(ScreenLinesUC + off, 0,
8215 (size_t)width * sizeof(u8char_T));
8216#endif
8217 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8218}
8219
8220/*
8221 * Mark one line in ScreenLines invalid by setting the attributes to an
8222 * invalid value.
8223 */
8224 static void
8225lineinvalid(off, width)
8226 unsigned off;
8227 int width;
8228{
8229 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8230}
8231
8232#ifdef FEAT_VERTSPLIT
8233/*
8234 * Copy part of a Screenline for vertically split window "wp".
8235 */
8236 static void
8237linecopy(to, from, wp)
8238 int to;
8239 int from;
8240 win_T *wp;
8241{
8242 unsigned off_to = LineOffset[to] + wp->w_wincol;
8243 unsigned off_from = LineOffset[from] + wp->w_wincol;
8244
8245 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8246 wp->w_width * sizeof(schar_T));
8247# ifdef FEAT_MBYTE
8248 if (enc_utf8)
8249 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008250 int i;
8251
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8253 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008254 for (i = 0; i < p_mco; ++i)
8255 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8256 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 }
8258 if (enc_dbcs == DBCS_JPNU)
8259 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8260 wp->w_width * sizeof(schar_T));
8261# endif
8262 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8263 wp->w_width * sizeof(sattr_T));
8264}
8265#endif
8266
8267/*
8268 * Return TRUE if clearing with term string "p" would work.
8269 * It can't work when the string is empty or it won't set the right background.
8270 */
8271 int
8272can_clear(p)
8273 char_u *p;
8274{
8275 return (*p != NUL && (t_colors <= 1
8276#ifdef FEAT_GUI
8277 || gui.in_use
8278#endif
8279 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8280}
8281
8282/*
8283 * Reset cursor position. Use whenever cursor was moved because of outputting
8284 * something directly to the screen (shell commands) or a terminal control
8285 * code.
8286 */
8287 void
8288screen_start()
8289{
8290 screen_cur_row = screen_cur_col = 9999;
8291}
8292
8293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 * Move the cursor to position "row","col" in the screen.
8295 * This tries to find the most efficient way to move, minimizing the number of
8296 * characters sent to the terminal.
8297 */
8298 void
8299windgoto(row, col)
8300 int row;
8301 int col;
8302{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008303 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 int i;
8305 int plan;
8306 int cost;
8307 int wouldbe_col;
8308 int noinvcurs;
8309 char_u *bs;
8310 int goto_cost;
8311 int attr;
8312
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008313#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8315
8316#define PLAN_LE 1
8317#define PLAN_CR 2
8318#define PLAN_NL 3
8319#define PLAN_WRITE 4
8320 /* Can't use ScreenLines unless initialized */
8321 if (ScreenLines == NULL)
8322 return;
8323
8324 if (col != screen_cur_col || row != screen_cur_row)
8325 {
8326 /* Check for valid position. */
8327 if (row < 0) /* window without text lines? */
8328 row = 0;
8329 if (row >= screen_Rows)
8330 row = screen_Rows - 1;
8331 if (col >= screen_Columns)
8332 col = screen_Columns - 1;
8333
8334 /* check if no cursor movement is allowed in highlight mode */
8335 if (screen_attr && *T_MS == NUL)
8336 noinvcurs = HIGHL_COST;
8337 else
8338 noinvcurs = 0;
8339 goto_cost = GOTO_COST + noinvcurs;
8340
8341 /*
8342 * Plan how to do the positioning:
8343 * 1. Use CR to move it to column 0, same row.
8344 * 2. Use T_LE to move it a few columns to the left.
8345 * 3. Use NL to move a few lines down, column 0.
8346 * 4. Move a few columns to the right with T_ND or by writing chars.
8347 *
8348 * Don't do this if the cursor went beyond the last column, the cursor
8349 * position is unknown then (some terminals wrap, some don't )
8350 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008351 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 * characters to move the cursor to the right.
8353 */
8354 if (row >= screen_cur_row && screen_cur_col < Columns)
8355 {
8356 /*
8357 * If the cursor is in the same row, bigger col, we can use CR
8358 * or T_LE.
8359 */
8360 bs = NULL; /* init for GCC */
8361 attr = screen_attr;
8362 if (row == screen_cur_row && col < screen_cur_col)
8363 {
8364 /* "le" is preferred over "bc", because "bc" is obsolete */
8365 if (*T_LE)
8366 bs = T_LE; /* "cursor left" */
8367 else
8368 bs = T_BC; /* "backspace character (old) */
8369 if (*bs)
8370 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8371 else
8372 cost = 999;
8373 if (col + 1 < cost) /* using CR is less characters */
8374 {
8375 plan = PLAN_CR;
8376 wouldbe_col = 0;
8377 cost = 1; /* CR is just one character */
8378 }
8379 else
8380 {
8381 plan = PLAN_LE;
8382 wouldbe_col = col;
8383 }
8384 if (noinvcurs) /* will stop highlighting */
8385 {
8386 cost += noinvcurs;
8387 attr = 0;
8388 }
8389 }
8390
8391 /*
8392 * If the cursor is above where we want to be, we can use CR LF.
8393 */
8394 else if (row > screen_cur_row)
8395 {
8396 plan = PLAN_NL;
8397 wouldbe_col = 0;
8398 cost = (row - screen_cur_row) * 2; /* CR LF */
8399 if (noinvcurs) /* will stop highlighting */
8400 {
8401 cost += noinvcurs;
8402 attr = 0;
8403 }
8404 }
8405
8406 /*
8407 * If the cursor is in the same row, smaller col, just use write.
8408 */
8409 else
8410 {
8411 plan = PLAN_WRITE;
8412 wouldbe_col = screen_cur_col;
8413 cost = 0;
8414 }
8415
8416 /*
8417 * Check if any characters that need to be written have the
8418 * correct attributes. Also avoid UTF-8 characters.
8419 */
8420 i = col - wouldbe_col;
8421 if (i > 0)
8422 cost += i;
8423 if (cost < goto_cost && i > 0)
8424 {
8425 /*
8426 * Check if the attributes are correct without additionally
8427 * stopping highlighting.
8428 */
8429 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8430 while (i && *p++ == attr)
8431 --i;
8432 if (i != 0)
8433 {
8434 /*
8435 * Try if it works when highlighting is stopped here.
8436 */
8437 if (*--p == 0)
8438 {
8439 cost += noinvcurs;
8440 while (i && *p++ == 0)
8441 --i;
8442 }
8443 if (i != 0)
8444 cost = 999; /* different attributes, don't do it */
8445 }
8446#ifdef FEAT_MBYTE
8447 if (enc_utf8)
8448 {
8449 /* Don't use an UTF-8 char for positioning, it's slow. */
8450 for (i = wouldbe_col; i < col; ++i)
8451 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8452 {
8453 cost = 999;
8454 break;
8455 }
8456 }
8457#endif
8458 }
8459
8460 /*
8461 * We can do it without term_windgoto()!
8462 */
8463 if (cost < goto_cost)
8464 {
8465 if (plan == PLAN_LE)
8466 {
8467 if (noinvcurs)
8468 screen_stop_highlight();
8469 while (screen_cur_col > col)
8470 {
8471 out_str(bs);
8472 --screen_cur_col;
8473 }
8474 }
8475 else if (plan == PLAN_CR)
8476 {
8477 if (noinvcurs)
8478 screen_stop_highlight();
8479 out_char('\r');
8480 screen_cur_col = 0;
8481 }
8482 else if (plan == PLAN_NL)
8483 {
8484 if (noinvcurs)
8485 screen_stop_highlight();
8486 while (screen_cur_row < row)
8487 {
8488 out_char('\n');
8489 ++screen_cur_row;
8490 }
8491 screen_cur_col = 0;
8492 }
8493
8494 i = col - screen_cur_col;
8495 if (i > 0)
8496 {
8497 /*
8498 * Use cursor-right if it's one character only. Avoids
8499 * removing a line of pixels from the last bold char, when
8500 * using the bold trick in the GUI.
8501 */
8502 if (T_ND[0] != NUL && T_ND[1] == NUL)
8503 {
8504 while (i-- > 0)
8505 out_char(*T_ND);
8506 }
8507 else
8508 {
8509 int off;
8510
8511 off = LineOffset[row] + screen_cur_col;
8512 while (i-- > 0)
8513 {
8514 if (ScreenAttrs[off] != screen_attr)
8515 screen_stop_highlight();
8516#ifdef FEAT_MBYTE
8517 out_flush_check();
8518#endif
8519 out_char(ScreenLines[off]);
8520#ifdef FEAT_MBYTE
8521 if (enc_dbcs == DBCS_JPNU
8522 && ScreenLines[off] == 0x8e)
8523 out_char(ScreenLines2[off]);
8524#endif
8525 ++off;
8526 }
8527 }
8528 }
8529 }
8530 }
8531 else
8532 cost = 999;
8533
8534 if (cost >= goto_cost)
8535 {
8536 if (noinvcurs)
8537 screen_stop_highlight();
8538 if (row == screen_cur_row && (col > screen_cur_col) &&
8539 *T_CRI != NUL)
8540 term_cursor_right(col - screen_cur_col);
8541 else
8542 term_windgoto(row, col);
8543 }
8544 screen_cur_row = row;
8545 screen_cur_col = col;
8546 }
8547}
8548
8549/*
8550 * Set cursor to its position in the current window.
8551 */
8552 void
8553setcursor()
8554{
8555 if (redrawing())
8556 {
8557 validate_cursor();
8558 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8559 W_WINCOL(curwin) + (
8560#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008561 /* With 'rightleft' set and the cursor on a double-wide
8562 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8564# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008565 (has_mbyte
8566 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8567 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568# endif
8569 1)) :
8570#endif
8571 curwin->w_wcol));
8572 }
8573}
8574
8575
8576/*
8577 * insert 'line_count' lines at 'row' in window 'wp'
8578 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8579 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8580 * scrolling.
8581 * Returns FAIL if the lines are not inserted, OK for success.
8582 */
8583 int
8584win_ins_lines(wp, row, line_count, invalid, mayclear)
8585 win_T *wp;
8586 int row;
8587 int line_count;
8588 int invalid;
8589 int mayclear;
8590{
8591 int did_delete;
8592 int nextrow;
8593 int lastrow;
8594 int retval;
8595
8596 if (invalid)
8597 wp->w_lines_valid = 0;
8598
8599 if (wp->w_height < 5)
8600 return FAIL;
8601
8602 if (line_count > wp->w_height - row)
8603 line_count = wp->w_height - row;
8604
8605 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8606 if (retval != MAYBE)
8607 return retval;
8608
8609 /*
8610 * If there is a next window or a status line, we first try to delete the
8611 * lines at the bottom to avoid messing what is after the window.
8612 * If this fails and there are following windows, don't do anything to avoid
8613 * messing up those windows, better just redraw.
8614 */
8615 did_delete = FALSE;
8616#ifdef FEAT_WINDOWS
8617 if (wp->w_next != NULL || wp->w_status_height)
8618 {
8619 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8620 line_count, (int)Rows, FALSE, NULL) == OK)
8621 did_delete = TRUE;
8622 else if (wp->w_next)
8623 return FAIL;
8624 }
8625#endif
8626 /*
8627 * if no lines deleted, blank the lines that will end up below the window
8628 */
8629 if (!did_delete)
8630 {
8631#ifdef FEAT_WINDOWS
8632 wp->w_redr_status = TRUE;
8633#endif
8634 redraw_cmdline = TRUE;
8635 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8636 lastrow = nextrow + line_count;
8637 if (lastrow > Rows)
8638 lastrow = Rows;
8639 screen_fill(nextrow - line_count, lastrow - line_count,
8640 W_WINCOL(wp), (int)W_ENDCOL(wp),
8641 ' ', ' ', 0);
8642 }
8643
8644 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8645 == FAIL)
8646 {
8647 /* deletion will have messed up other windows */
8648 if (did_delete)
8649 {
8650#ifdef FEAT_WINDOWS
8651 wp->w_redr_status = TRUE;
8652#endif
8653 win_rest_invalid(W_NEXT(wp));
8654 }
8655 return FAIL;
8656 }
8657
8658 return OK;
8659}
8660
8661/*
8662 * delete "line_count" window lines at "row" in window "wp"
8663 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8664 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8665 * scrolling
8666 * Return OK for success, FAIL if the lines are not deleted.
8667 */
8668 int
8669win_del_lines(wp, row, line_count, invalid, mayclear)
8670 win_T *wp;
8671 int row;
8672 int line_count;
8673 int invalid;
8674 int mayclear;
8675{
8676 int retval;
8677
8678 if (invalid)
8679 wp->w_lines_valid = 0;
8680
8681 if (line_count > wp->w_height - row)
8682 line_count = wp->w_height - row;
8683
8684 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8685 if (retval != MAYBE)
8686 return retval;
8687
8688 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8689 (int)Rows, FALSE, NULL) == FAIL)
8690 return FAIL;
8691
8692#ifdef FEAT_WINDOWS
8693 /*
8694 * If there are windows or status lines below, try to put them at the
8695 * correct place. If we can't do that, they have to be redrawn.
8696 */
8697 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8698 {
8699 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8700 line_count, (int)Rows, NULL) == FAIL)
8701 {
8702 wp->w_redr_status = TRUE;
8703 win_rest_invalid(wp->w_next);
8704 }
8705 }
8706 /*
8707 * If this is the last window and there is no status line, redraw the
8708 * command line later.
8709 */
8710 else
8711#endif
8712 redraw_cmdline = TRUE;
8713 return OK;
8714}
8715
8716/*
8717 * Common code for win_ins_lines() and win_del_lines().
8718 * Returns OK or FAIL when the work has been done.
8719 * Returns MAYBE when not finished yet.
8720 */
8721 static int
8722win_do_lines(wp, row, line_count, mayclear, del)
8723 win_T *wp;
8724 int row;
8725 int line_count;
8726 int mayclear;
8727 int del;
8728{
8729 int retval;
8730
8731 if (!redrawing() || line_count <= 0)
8732 return FAIL;
8733
8734 /* only a few lines left: redraw is faster */
8735 if (mayclear && Rows - line_count < 5
8736#ifdef FEAT_VERTSPLIT
8737 && wp->w_width == Columns
8738#endif
8739 )
8740 {
8741 screenclear(); /* will set wp->w_lines_valid to 0 */
8742 return FAIL;
8743 }
8744
8745 /*
8746 * Delete all remaining lines
8747 */
8748 if (row + line_count >= wp->w_height)
8749 {
8750 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8751 W_WINCOL(wp), (int)W_ENDCOL(wp),
8752 ' ', ' ', 0);
8753 return OK;
8754 }
8755
8756 /*
8757 * when scrolling, the message on the command line should be cleared,
8758 * otherwise it will stay there forever.
8759 */
8760 clear_cmdline = TRUE;
8761
8762 /*
8763 * If the terminal can set a scroll region, use that.
8764 * Always do this in a vertically split window. This will redraw from
8765 * ScreenLines[] when t_CV isn't defined. That's faster than using
8766 * win_line().
8767 * Don't use a scroll region when we are going to redraw the text, writing
8768 * a character in the lower right corner of the scroll region causes a
8769 * scroll-up in the DJGPP version.
8770 */
8771 if (scroll_region
8772#ifdef FEAT_VERTSPLIT
8773 || W_WIDTH(wp) != Columns
8774#endif
8775 )
8776 {
8777#ifdef FEAT_VERTSPLIT
8778 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8779#endif
8780 scroll_region_set(wp, row);
8781 if (del)
8782 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8783 wp->w_height - row, FALSE, wp);
8784 else
8785 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8786 wp->w_height - row, wp);
8787#ifdef FEAT_VERTSPLIT
8788 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8789#endif
8790 scroll_region_reset();
8791 return retval;
8792 }
8793
8794#ifdef FEAT_WINDOWS
8795 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8796 return FAIL;
8797#endif
8798
8799 return MAYBE;
8800}
8801
8802/*
8803 * window 'wp' and everything after it is messed up, mark it for redraw
8804 */
8805 static void
8806win_rest_invalid(wp)
8807 win_T *wp;
8808{
8809#ifdef FEAT_WINDOWS
8810 while (wp != NULL)
8811#else
8812 if (wp != NULL)
8813#endif
8814 {
8815 redraw_win_later(wp, NOT_VALID);
8816#ifdef FEAT_WINDOWS
8817 wp->w_redr_status = TRUE;
8818 wp = wp->w_next;
8819#endif
8820 }
8821 redraw_cmdline = TRUE;
8822}
8823
8824/*
8825 * The rest of the routines in this file perform screen manipulations. The
8826 * given operation is performed physically on the screen. The corresponding
8827 * change is also made to the internal screen image. In this way, the editor
8828 * anticipates the effect of editing changes on the appearance of the screen.
8829 * That way, when we call screenupdate a complete redraw isn't usually
8830 * necessary. Another advantage is that we can keep adding code to anticipate
8831 * screen changes, and in the meantime, everything still works.
8832 */
8833
8834/*
8835 * types for inserting or deleting lines
8836 */
8837#define USE_T_CAL 1
8838#define USE_T_CDL 2
8839#define USE_T_AL 3
8840#define USE_T_CE 4
8841#define USE_T_DL 5
8842#define USE_T_SR 6
8843#define USE_NL 7
8844#define USE_T_CD 8
8845#define USE_REDRAW 9
8846
8847/*
8848 * insert lines on the screen and update ScreenLines[]
8849 * 'end' is the line after the scrolled part. Normally it is Rows.
8850 * When scrolling region used 'off' is the offset from the top for the region.
8851 * 'row' and 'end' are relative to the start of the region.
8852 *
8853 * return FAIL for failure, OK for success.
8854 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008855 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856screen_ins_lines(off, row, line_count, end, wp)
8857 int off;
8858 int row;
8859 int line_count;
8860 int end;
8861 win_T *wp; /* NULL or window to use width from */
8862{
8863 int i;
8864 int j;
8865 unsigned temp;
8866 int cursor_row;
8867 int type;
8868 int result_empty;
8869 int can_ce = can_clear(T_CE);
8870
8871 /*
8872 * FAIL if
8873 * - there is no valid screen
8874 * - the screen has to be redrawn completely
8875 * - the line count is less than one
8876 * - the line count is more than 'ttyscroll'
8877 */
8878 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8879 return FAIL;
8880
8881 /*
8882 * There are seven ways to insert lines:
8883 * 0. When in a vertically split window and t_CV isn't set, redraw the
8884 * characters from ScreenLines[].
8885 * 1. Use T_CD (clear to end of display) if it exists and the result of
8886 * the insert is just empty lines
8887 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8888 * present or line_count > 1. It looks better if we do all the inserts
8889 * at once.
8890 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8891 * insert is just empty lines and T_CE is not present or line_count >
8892 * 1.
8893 * 4. Use T_AL (insert line) if it exists.
8894 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8895 * just empty lines.
8896 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8897 * just empty lines.
8898 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8899 * the 'da' flag is not set or we have clear line capability.
8900 * 8. redraw the characters from ScreenLines[].
8901 *
8902 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8903 * the scrollbar for the window. It does have insert line, use that if it
8904 * exists.
8905 */
8906 result_empty = (row + line_count >= end);
8907#ifdef FEAT_VERTSPLIT
8908 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8909 type = USE_REDRAW;
8910 else
8911#endif
8912 if (can_clear(T_CD) && result_empty)
8913 type = USE_T_CD;
8914 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8915 type = USE_T_CAL;
8916 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8917 type = USE_T_CDL;
8918 else if (*T_AL != NUL)
8919 type = USE_T_AL;
8920 else if (can_ce && result_empty)
8921 type = USE_T_CE;
8922 else if (*T_DL != NUL && result_empty)
8923 type = USE_T_DL;
8924 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8925 type = USE_T_SR;
8926 else
8927 return FAIL;
8928
8929 /*
8930 * For clearing the lines screen_del_lines() is used. This will also take
8931 * care of t_db if necessary.
8932 */
8933 if (type == USE_T_CD || type == USE_T_CDL ||
8934 type == USE_T_CE || type == USE_T_DL)
8935 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8936
8937 /*
8938 * If text is retained below the screen, first clear or delete as many
8939 * lines at the bottom of the window as are about to be inserted so that
8940 * the deleted lines won't later surface during a screen_del_lines.
8941 */
8942 if (*T_DB)
8943 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8944
8945#ifdef FEAT_CLIPBOARD
8946 /* Remove a modeless selection when inserting lines halfway the screen
8947 * or not the full width of the screen. */
8948 if (off + row > 0
8949# ifdef FEAT_VERTSPLIT
8950 || (wp != NULL && wp->w_width != Columns)
8951# endif
8952 )
8953 clip_clear_selection();
8954 else
8955 clip_scroll_selection(-line_count);
8956#endif
8957
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958#ifdef FEAT_GUI
8959 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8960 * scrolling is actually carried out. */
8961 gui_dont_update_cursor();
8962#endif
8963
8964 if (*T_CCS != NUL) /* cursor relative to region */
8965 cursor_row = row;
8966 else
8967 cursor_row = row + off;
8968
8969 /*
8970 * Shift LineOffset[] line_count down to reflect the inserted lines.
8971 * Clear the inserted lines in ScreenLines[].
8972 */
8973 row += off;
8974 end += off;
8975 for (i = 0; i < line_count; ++i)
8976 {
8977#ifdef FEAT_VERTSPLIT
8978 if (wp != NULL && wp->w_width != Columns)
8979 {
8980 /* need to copy part of a line */
8981 j = end - 1 - i;
8982 while ((j -= line_count) >= row)
8983 linecopy(j + line_count, j, wp);
8984 j += line_count;
8985 if (can_clear((char_u *)" "))
8986 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8987 else
8988 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8989 LineWraps[j] = FALSE;
8990 }
8991 else
8992#endif
8993 {
8994 j = end - 1 - i;
8995 temp = LineOffset[j];
8996 while ((j -= line_count) >= row)
8997 {
8998 LineOffset[j + line_count] = LineOffset[j];
8999 LineWraps[j + line_count] = LineWraps[j];
9000 }
9001 LineOffset[j + line_count] = temp;
9002 LineWraps[j + line_count] = FALSE;
9003 if (can_clear((char_u *)" "))
9004 lineclear(temp, (int)Columns);
9005 else
9006 lineinvalid(temp, (int)Columns);
9007 }
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009
9010 screen_stop_highlight();
9011 windgoto(cursor_row, 0);
9012
9013#ifdef FEAT_VERTSPLIT
9014 /* redraw the characters */
9015 if (type == USE_REDRAW)
9016 redraw_block(row, end, wp);
9017 else
9018#endif
9019 if (type == USE_T_CAL)
9020 {
9021 term_append_lines(line_count);
9022 screen_start(); /* don't know where cursor is now */
9023 }
9024 else
9025 {
9026 for (i = 0; i < line_count; i++)
9027 {
9028 if (type == USE_T_AL)
9029 {
9030 if (i && cursor_row != 0)
9031 windgoto(cursor_row, 0);
9032 out_str(T_AL);
9033 }
9034 else /* type == USE_T_SR */
9035 out_str(T_SR);
9036 screen_start(); /* don't know where cursor is now */
9037 }
9038 }
9039
9040 /*
9041 * With scroll-reverse and 'da' flag set we need to clear the lines that
9042 * have been scrolled down into the region.
9043 */
9044 if (type == USE_T_SR && *T_DA)
9045 {
9046 for (i = 0; i < line_count; ++i)
9047 {
9048 windgoto(off + i, 0);
9049 out_str(T_CE);
9050 screen_start(); /* don't know where cursor is now */
9051 }
9052 }
9053
9054#ifdef FEAT_GUI
9055 gui_can_update_cursor();
9056 if (gui.in_use)
9057 out_flush(); /* always flush after a scroll */
9058#endif
9059 return OK;
9060}
9061
9062/*
9063 * delete lines on the screen and update ScreenLines[]
9064 * 'end' is the line after the scrolled part. Normally it is Rows.
9065 * When scrolling region used 'off' is the offset from the top for the region.
9066 * 'row' and 'end' are relative to the start of the region.
9067 *
9068 * Return OK for success, FAIL if the lines are not deleted.
9069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 int
9071screen_del_lines(off, row, line_count, end, force, wp)
9072 int off;
9073 int row;
9074 int line_count;
9075 int end;
9076 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009077 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
9079 int j;
9080 int i;
9081 unsigned temp;
9082 int cursor_row;
9083 int cursor_end;
9084 int result_empty; /* result is empty until end of region */
9085 int can_delete; /* deleting line codes can be used */
9086 int type;
9087
9088 /*
9089 * FAIL if
9090 * - there is no valid screen
9091 * - the screen has to be redrawn completely
9092 * - the line count is less than one
9093 * - the line count is more than 'ttyscroll'
9094 */
9095 if (!screen_valid(TRUE) || line_count <= 0 ||
9096 (!force && line_count > p_ttyscroll))
9097 return FAIL;
9098
9099 /*
9100 * Check if the rest of the current region will become empty.
9101 */
9102 result_empty = row + line_count >= end;
9103
9104 /*
9105 * We can delete lines only when 'db' flag not set or when 'ce' option
9106 * available.
9107 */
9108 can_delete = (*T_DB == NUL || can_clear(T_CE));
9109
9110 /*
9111 * There are six ways to delete lines:
9112 * 0. When in a vertically split window and t_CV isn't set, redraw the
9113 * characters from ScreenLines[].
9114 * 1. Use T_CD if it exists and the result is empty.
9115 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9116 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9117 * none of the other ways work.
9118 * 4. Use T_CE (erase line) if the result is empty.
9119 * 5. Use T_DL (delete line) if it exists.
9120 * 6. redraw the characters from ScreenLines[].
9121 */
9122#ifdef FEAT_VERTSPLIT
9123 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9124 type = USE_REDRAW;
9125 else
9126#endif
9127 if (can_clear(T_CD) && result_empty)
9128 type = USE_T_CD;
9129#if defined(__BEOS__) && defined(BEOS_DR8)
9130 /*
9131 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9132 * its internal termcap... this works okay for tests which test *T_DB !=
9133 * NUL. It has the disadvantage that the user cannot use any :set t_*
9134 * command to get T_DB (back) to empty_option, only :set term=... will do
9135 * the trick...
9136 * Anyway, this hack will hopefully go away with the next OS release.
9137 * (Olaf Seibert)
9138 */
9139 else if (row == 0 && T_DB == empty_option
9140 && (line_count == 1 || *T_CDL == NUL))
9141#else
9142 else if (row == 0 && (
9143#ifndef AMIGA
9144 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9145 * up, so use delete-line command */
9146 line_count == 1 ||
9147#endif
9148 *T_CDL == NUL))
9149#endif
9150 type = USE_NL;
9151 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9152 type = USE_T_CDL;
9153 else if (can_clear(T_CE) && result_empty
9154#ifdef FEAT_VERTSPLIT
9155 && (wp == NULL || wp->w_width == Columns)
9156#endif
9157 )
9158 type = USE_T_CE;
9159 else if (*T_DL != NUL && can_delete)
9160 type = USE_T_DL;
9161 else if (*T_CDL != NUL && can_delete)
9162 type = USE_T_CDL;
9163 else
9164 return FAIL;
9165
9166#ifdef FEAT_CLIPBOARD
9167 /* Remove a modeless selection when deleting lines halfway the screen or
9168 * not the full width of the screen. */
9169 if (off + row > 0
9170# ifdef FEAT_VERTSPLIT
9171 || (wp != NULL && wp->w_width != Columns)
9172# endif
9173 )
9174 clip_clear_selection();
9175 else
9176 clip_scroll_selection(line_count);
9177#endif
9178
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179#ifdef FEAT_GUI
9180 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9181 * scrolling is actually carried out. */
9182 gui_dont_update_cursor();
9183#endif
9184
9185 if (*T_CCS != NUL) /* cursor relative to region */
9186 {
9187 cursor_row = row;
9188 cursor_end = end;
9189 }
9190 else
9191 {
9192 cursor_row = row + off;
9193 cursor_end = end + off;
9194 }
9195
9196 /*
9197 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9198 * Clear the inserted lines in ScreenLines[].
9199 */
9200 row += off;
9201 end += off;
9202 for (i = 0; i < line_count; ++i)
9203 {
9204#ifdef FEAT_VERTSPLIT
9205 if (wp != NULL && wp->w_width != Columns)
9206 {
9207 /* need to copy part of a line */
9208 j = row + i;
9209 while ((j += line_count) <= end - 1)
9210 linecopy(j - line_count, j, wp);
9211 j -= line_count;
9212 if (can_clear((char_u *)" "))
9213 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9214 else
9215 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9216 LineWraps[j] = FALSE;
9217 }
9218 else
9219#endif
9220 {
9221 /* whole width, moving the line pointers is faster */
9222 j = row + i;
9223 temp = LineOffset[j];
9224 while ((j += line_count) <= end - 1)
9225 {
9226 LineOffset[j - line_count] = LineOffset[j];
9227 LineWraps[j - line_count] = LineWraps[j];
9228 }
9229 LineOffset[j - line_count] = temp;
9230 LineWraps[j - line_count] = FALSE;
9231 if (can_clear((char_u *)" "))
9232 lineclear(temp, (int)Columns);
9233 else
9234 lineinvalid(temp, (int)Columns);
9235 }
9236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237
9238 screen_stop_highlight();
9239
9240#ifdef FEAT_VERTSPLIT
9241 /* redraw the characters */
9242 if (type == USE_REDRAW)
9243 redraw_block(row, end, wp);
9244 else
9245#endif
9246 if (type == USE_T_CD) /* delete the lines */
9247 {
9248 windgoto(cursor_row, 0);
9249 out_str(T_CD);
9250 screen_start(); /* don't know where cursor is now */
9251 }
9252 else if (type == USE_T_CDL)
9253 {
9254 windgoto(cursor_row, 0);
9255 term_delete_lines(line_count);
9256 screen_start(); /* don't know where cursor is now */
9257 }
9258 /*
9259 * Deleting lines at top of the screen or scroll region: Just scroll
9260 * the whole screen (scroll region) up by outputting newlines on the
9261 * last line.
9262 */
9263 else if (type == USE_NL)
9264 {
9265 windgoto(cursor_end - 1, 0);
9266 for (i = line_count; --i >= 0; )
9267 out_char('\n'); /* cursor will remain on same line */
9268 }
9269 else
9270 {
9271 for (i = line_count; --i >= 0; )
9272 {
9273 if (type == USE_T_DL)
9274 {
9275 windgoto(cursor_row, 0);
9276 out_str(T_DL); /* delete a line */
9277 }
9278 else /* type == USE_T_CE */
9279 {
9280 windgoto(cursor_row + i, 0);
9281 out_str(T_CE); /* erase a line */
9282 }
9283 screen_start(); /* don't know where cursor is now */
9284 }
9285 }
9286
9287 /*
9288 * If the 'db' flag is set, we need to clear the lines that have been
9289 * scrolled up at the bottom of the region.
9290 */
9291 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9292 {
9293 for (i = line_count; i > 0; --i)
9294 {
9295 windgoto(cursor_end - i, 0);
9296 out_str(T_CE); /* erase a line */
9297 screen_start(); /* don't know where cursor is now */
9298 }
9299 }
9300
9301#ifdef FEAT_GUI
9302 gui_can_update_cursor();
9303 if (gui.in_use)
9304 out_flush(); /* always flush after a scroll */
9305#endif
9306
9307 return OK;
9308}
9309
9310/*
9311 * show the current mode and ruler
9312 *
9313 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9314 * If clear_cmdline is FALSE there may be a message there that needs to be
9315 * cleared only if a mode is shown.
9316 * Return the length of the message (0 if no message).
9317 */
9318 int
9319showmode()
9320{
9321 int need_clear;
9322 int length = 0;
9323 int do_mode;
9324 int attr;
9325 int nwr_save;
9326#ifdef FEAT_INS_EXPAND
9327 int sub_attr;
9328#endif
9329
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009330 do_mode = ((p_smd && msg_silent == 0)
9331 && ((State & INSERT)
9332 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333#ifdef FEAT_VISUAL
9334 || VIsual_active
9335#endif
9336 ));
9337 if (do_mode || Recording)
9338 {
9339 /*
9340 * Don't show mode right now, when not redrawing or inside a mapping.
9341 * Call char_avail() only when we are going to show something, because
9342 * it takes a bit of time.
9343 */
9344 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9345 {
9346 redraw_cmdline = TRUE; /* show mode later */
9347 return 0;
9348 }
9349
9350 nwr_save = need_wait_return;
9351
9352 /* wait a bit before overwriting an important message */
9353 check_for_delay(FALSE);
9354
9355 /* if the cmdline is more than one line high, erase top lines */
9356 need_clear = clear_cmdline;
9357 if (clear_cmdline && cmdline_row < Rows - 1)
9358 msg_clr_cmdline(); /* will reset clear_cmdline */
9359
9360 /* Position on the last line in the window, column 0 */
9361 msg_pos_mode();
9362 cursor_off();
9363 attr = hl_attr(HLF_CM); /* Highlight mode */
9364 if (do_mode)
9365 {
9366 MSG_PUTS_ATTR("--", attr);
9367#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009368# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 if (xic != NULL && im_get_status() && !p_imdisable
9370 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009371# else
9372 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009373# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009374 preedit_get_status()
9375# else
9376 im_get_status()
9377# endif
9378 )
9379# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009380# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 MSG_PUTS_ATTR(" IM", attr);
9382# else
9383 MSG_PUTS_ATTR(" XIM", attr);
9384# endif
9385#endif
9386#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9387 if (gui.in_use)
9388 {
9389 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009390 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 }
9392#endif
9393#ifdef FEAT_INS_EXPAND
9394 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9395 {
9396 /* These messages can get long, avoid a wrap in a narrow
9397 * window. Prefer showing edit_submode_extra. */
9398 length = (Rows - msg_row) * Columns - 3;
9399 if (edit_submode_extra != NULL)
9400 length -= vim_strsize(edit_submode_extra);
9401 if (length > 0)
9402 {
9403 if (edit_submode_pre != NULL)
9404 length -= vim_strsize(edit_submode_pre);
9405 if (length - vim_strsize(edit_submode) > 0)
9406 {
9407 if (edit_submode_pre != NULL)
9408 msg_puts_attr(edit_submode_pre, attr);
9409 msg_puts_attr(edit_submode, attr);
9410 }
9411 if (edit_submode_extra != NULL)
9412 {
9413 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9414 if ((int)edit_submode_highl < (int)HLF_COUNT)
9415 sub_attr = hl_attr(edit_submode_highl);
9416 else
9417 sub_attr = attr;
9418 msg_puts_attr(edit_submode_extra, sub_attr);
9419 }
9420 }
9421 length = 0;
9422 }
9423 else
9424#endif
9425 {
9426#ifdef FEAT_VREPLACE
9427 if (State & VREPLACE_FLAG)
9428 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9429 else
9430#endif
9431 if (State & REPLACE_FLAG)
9432 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9433 else if (State & INSERT)
9434 {
9435#ifdef FEAT_RIGHTLEFT
9436 if (p_ri)
9437 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9438#endif
9439 MSG_PUTS_ATTR(_(" INSERT"), attr);
9440 }
9441 else if (restart_edit == 'I')
9442 MSG_PUTS_ATTR(_(" (insert)"), attr);
9443 else if (restart_edit == 'R')
9444 MSG_PUTS_ATTR(_(" (replace)"), attr);
9445 else if (restart_edit == 'V')
9446 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9447#ifdef FEAT_RIGHTLEFT
9448 if (p_hkmap)
9449 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9450# ifdef FEAT_FKMAP
9451 if (p_fkmap)
9452 MSG_PUTS_ATTR(farsi_text_5, attr);
9453# endif
9454#endif
9455#ifdef FEAT_KEYMAP
9456 if (State & LANGMAP)
9457 {
9458# ifdef FEAT_ARABIC
9459 if (curwin->w_p_arab)
9460 MSG_PUTS_ATTR(_(" Arabic"), attr);
9461 else
9462# endif
9463 MSG_PUTS_ATTR(_(" (lang)"), attr);
9464 }
9465#endif
9466 if ((State & INSERT) && p_paste)
9467 MSG_PUTS_ATTR(_(" (paste)"), attr);
9468
9469#ifdef FEAT_VISUAL
9470 if (VIsual_active)
9471 {
9472 char *p;
9473
9474 /* Don't concatenate separate words to avoid translation
9475 * problems. */
9476 switch ((VIsual_select ? 4 : 0)
9477 + (VIsual_mode == Ctrl_V) * 2
9478 + (VIsual_mode == 'V'))
9479 {
9480 case 0: p = N_(" VISUAL"); break;
9481 case 1: p = N_(" VISUAL LINE"); break;
9482 case 2: p = N_(" VISUAL BLOCK"); break;
9483 case 4: p = N_(" SELECT"); break;
9484 case 5: p = N_(" SELECT LINE"); break;
9485 default: p = N_(" SELECT BLOCK"); break;
9486 }
9487 MSG_PUTS_ATTR(_(p), attr);
9488 }
9489#endif
9490 MSG_PUTS_ATTR(" --", attr);
9491 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009492
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 need_clear = TRUE;
9494 }
9495 if (Recording
9496#ifdef FEAT_INS_EXPAND
9497 && edit_submode == NULL /* otherwise it gets too long */
9498#endif
9499 )
9500 {
9501 MSG_PUTS_ATTR(_("recording"), attr);
9502 need_clear = TRUE;
9503 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009504
9505 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (need_clear || clear_cmdline)
9507 msg_clr_eos();
9508 msg_didout = FALSE; /* overwrite this message */
9509 length = msg_col;
9510 msg_col = 0;
9511 need_wait_return = nwr_save; /* never ask for hit-return for this */
9512 }
9513 else if (clear_cmdline && msg_silent == 0)
9514 /* Clear the whole command line. Will reset "clear_cmdline". */
9515 msg_clr_cmdline();
9516
9517#ifdef FEAT_CMDL_INFO
9518# ifdef FEAT_VISUAL
9519 /* In Visual mode the size of the selected area must be redrawn. */
9520 if (VIsual_active)
9521 clear_showcmd();
9522# endif
9523
9524 /* If the last window has no status line, the ruler is after the mode
9525 * message and must be redrawn */
9526 if (redrawing()
9527# ifdef FEAT_WINDOWS
9528 && lastwin->w_status_height == 0
9529# endif
9530 )
9531 win_redr_ruler(lastwin, TRUE);
9532#endif
9533 redraw_cmdline = FALSE;
9534 clear_cmdline = FALSE;
9535
9536 return length;
9537}
9538
9539/*
9540 * Position for a mode message.
9541 */
9542 static void
9543msg_pos_mode()
9544{
9545 msg_col = 0;
9546 msg_row = Rows - 1;
9547}
9548
9549/*
9550 * Delete mode message. Used when ESC is typed which is expected to end
9551 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009552 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 */
9554 void
9555unshowmode(force)
9556 int force;
9557{
9558 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009559 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 */
9561 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9562 redraw_cmdline = TRUE; /* delete mode later */
9563 else
9564 {
9565 msg_pos_mode();
9566 if (Recording)
9567 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9568 msg_clr_eos();
9569 }
9570}
9571
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009572#if defined(FEAT_WINDOWS)
9573/*
9574 * Draw the tab pages line at the top of the Vim window.
9575 */
9576 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009577draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009578{
9579 int tabcount = 0;
9580 tabpage_T *tp;
9581 int tabwidth;
9582 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009583 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009584 int attr;
9585 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009586 win_T *cwp;
9587 int wincount;
9588 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009589 int c;
9590 int len;
9591 int attr_sel = hl_attr(HLF_TPS);
9592 int attr_nosel = hl_attr(HLF_TP);
9593 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009594 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009595 int room;
9596 int use_sep_chars = (t_colors < 8
9597#ifdef FEAT_GUI
9598 && !gui.in_use
9599#endif
9600 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009601
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009602 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009603
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009604#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009605 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009606 if (gui_use_tabline())
9607 {
9608 gui_update_tabline();
9609 return;
9610 }
9611#endif
9612
9613 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009614 return;
9615
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009616#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009617
9618 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9619 for (scol = 0; scol < Columns; ++scol)
9620 TabPageIdxs[scol] = 0;
9621
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009622 /* Use the 'tabline' option if it's set. */
9623 if (*p_tal != NUL)
9624 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009625 int save_called_emsg = called_emsg;
9626
9627 /* Check for an error. If there is one we would loop in redrawing the
9628 * screen. Avoid that by making 'tabline' empty. */
9629 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009630 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009631 if (called_emsg)
9632 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009633 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009634 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009635 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009636 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009637#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009638 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009639 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9640 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009641
Bram Moolenaar238a5642006-02-21 22:12:05 +00009642 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9643 if (tabwidth < 6)
9644 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009645
Bram Moolenaar238a5642006-02-21 22:12:05 +00009646 attr = attr_nosel;
9647 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009648 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009649 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9650 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009651 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009652 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009653
Bram Moolenaar238a5642006-02-21 22:12:05 +00009654 if (tp->tp_topframe == topframe)
9655 attr = attr_sel;
9656 if (use_sep_chars && col > 0)
9657 screen_putchar('|', 0, col++, attr);
9658
9659 if (tp->tp_topframe != topframe)
9660 attr = attr_nosel;
9661
9662 screen_putchar(' ', 0, col++, attr);
9663
9664 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009665 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009666 cwp = curwin;
9667 wp = firstwin;
9668 }
9669 else
9670 {
9671 cwp = tp->tp_curwin;
9672 wp = tp->tp_firstwin;
9673 }
9674
9675 modified = FALSE;
9676 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9677 if (bufIsChanged(wp->w_buffer))
9678 modified = TRUE;
9679 if (modified || wincount > 1)
9680 {
9681 if (wincount > 1)
9682 {
9683 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009684 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009685 if (col + len >= Columns - 3)
9686 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009687 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009688#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009689 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009690#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009691 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009692#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009693 );
9694 col += len;
9695 }
9696 if (modified)
9697 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9698 screen_putchar(' ', 0, col++, attr);
9699 }
9700
9701 room = scol - col + tabwidth - 1;
9702 if (room > 0)
9703 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009704 /* Get buffer name in NameBuff[] */
9705 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009706 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009707 len = vim_strsize(NameBuff);
9708 p = NameBuff;
9709#ifdef FEAT_MBYTE
9710 if (has_mbyte)
9711 while (len > room)
9712 {
9713 len -= ptr2cells(p);
9714 mb_ptr_adv(p);
9715 }
9716 else
9717#endif
9718 if (len > room)
9719 {
9720 p += len - room;
9721 len = room;
9722 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009723 if (len > Columns - col - 1)
9724 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009725
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009726 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009727 col += len;
9728 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009729 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009730
9731 /* Store the tab page number in TabPageIdxs[], so that
9732 * jump_to_mouse() knows where each one is. */
9733 ++tabcount;
9734 while (scol < col)
9735 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009736 }
9737
Bram Moolenaar238a5642006-02-21 22:12:05 +00009738 if (use_sep_chars)
9739 c = '_';
9740 else
9741 c = ' ';
9742 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009743
9744 /* Put an "X" for closing the current tab if there are several. */
9745 if (first_tabpage->tp_next != NULL)
9746 {
9747 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9748 TabPageIdxs[Columns - 1] = -999;
9749 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009750 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009751
9752 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9753 * set. */
9754 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009755}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009756
9757/*
9758 * Get buffer name for "buf" into NameBuff[].
9759 * Takes care of special buffer names and translates special characters.
9760 */
9761 void
9762get_trans_bufname(buf)
9763 buf_T *buf;
9764{
9765 if (buf_spname(buf) != NULL)
9766 STRCPY(NameBuff, buf_spname(buf));
9767 else
9768 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9769 trans_characters(NameBuff, MAXPATHL);
9770}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009771#endif
9772
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9774/*
9775 * Get the character to use in a status line. Get its attributes in "*attr".
9776 */
9777 static int
9778fillchar_status(attr, is_curwin)
9779 int *attr;
9780 int is_curwin;
9781{
9782 int fill;
9783 if (is_curwin)
9784 {
9785 *attr = hl_attr(HLF_S);
9786 fill = fill_stl;
9787 }
9788 else
9789 {
9790 *attr = hl_attr(HLF_SNC);
9791 fill = fill_stlnc;
9792 }
9793 /* Use fill when there is highlighting, and highlighting of current
9794 * window differs, or the fillchars differ, or this is not the
9795 * current window */
9796 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9797 || !is_curwin || firstwin == lastwin)
9798 || (fill_stl != fill_stlnc)))
9799 return fill;
9800 if (is_curwin)
9801 return '^';
9802 return '=';
9803}
9804#endif
9805
9806#ifdef FEAT_VERTSPLIT
9807/*
9808 * Get the character to use in a separator between vertically split windows.
9809 * Get its attributes in "*attr".
9810 */
9811 static int
9812fillchar_vsep(attr)
9813 int *attr;
9814{
9815 *attr = hl_attr(HLF_C);
9816 if (*attr == 0 && fill_vert == ' ')
9817 return '|';
9818 else
9819 return fill_vert;
9820}
9821#endif
9822
9823/*
9824 * Return TRUE if redrawing should currently be done.
9825 */
9826 int
9827redrawing()
9828{
9829 return (!RedrawingDisabled
9830 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9831}
9832
9833/*
9834 * Return TRUE if printing messages should currently be done.
9835 */
9836 int
9837messaging()
9838{
9839 return (!(p_lz && char_avail() && !KeyTyped));
9840}
9841
9842/*
9843 * Show current status info in ruler and various other places
9844 * If always is FALSE, only show ruler if position has changed.
9845 */
9846 void
9847showruler(always)
9848 int always;
9849{
9850 if (!always && !redrawing())
9851 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009852#ifdef FEAT_INS_EXPAND
9853 if (pum_visible())
9854 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009855# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009856 /* Don't redraw right now, do it later. */
9857 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009858# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009859 return;
9860 }
9861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009863 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009864 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009865 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 else
9868#endif
9869#ifdef FEAT_CMDL_INFO
9870 win_redr_ruler(curwin, always);
9871#endif
9872
9873#ifdef FEAT_TITLE
9874 if (need_maketitle
9875# ifdef FEAT_STL_OPT
9876 || (p_icon && (stl_syntax & STL_IN_ICON))
9877 || (p_title && (stl_syntax & STL_IN_TITLE))
9878# endif
9879 )
9880 maketitle();
9881#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009882#ifdef FEAT_WINDOWS
9883 /* Redraw the tab pages line if needed. */
9884 if (redraw_tabline)
9885 draw_tabline();
9886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887}
9888
9889#ifdef FEAT_CMDL_INFO
9890 static void
9891win_redr_ruler(wp, always)
9892 win_T *wp;
9893 int always;
9894{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009895#define RULER_BUF_LEN 70
9896 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 int row;
9898 int fillchar;
9899 int attr;
9900 int empty_line = FALSE;
9901 colnr_T virtcol;
9902 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009903 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 int o;
9905#ifdef FEAT_VERTSPLIT
9906 int this_ru_col;
9907 int off = 0;
9908 int width = Columns;
9909# define WITH_OFF(x) x
9910# define WITH_WIDTH(x) x
9911#else
9912# define WITH_OFF(x) 0
9913# define WITH_WIDTH(x) Columns
9914# define this_ru_col ru_col
9915#endif
9916
9917 /* If 'ruler' off or redrawing disabled, don't do anything */
9918 if (!p_ru)
9919 return;
9920
9921 /*
9922 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9923 * after deleting lines, before cursor.lnum is corrected.
9924 */
9925 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9926 return;
9927
9928#ifdef FEAT_INS_EXPAND
9929 /* Don't draw the ruler while doing insert-completion, it might overwrite
9930 * the (long) mode message. */
9931# ifdef FEAT_WINDOWS
9932 if (wp == lastwin && lastwin->w_status_height == 0)
9933# endif
9934 if (edit_submode != NULL)
9935 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009936 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9937 if (pum_visible())
9938 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939#endif
9940
9941#ifdef FEAT_STL_OPT
9942 if (*p_ruf)
9943 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009944 int save_called_emsg = called_emsg;
9945
9946 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009948 if (called_emsg)
9949 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009950 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009951 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 return;
9953 }
9954#endif
9955
9956 /*
9957 * Check if not in Insert mode and the line is empty (will show "0-1").
9958 */
9959 if (!(State & INSERT)
9960 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9961 empty_line = TRUE;
9962
9963 /*
9964 * Only draw the ruler when something changed.
9965 */
9966 validate_virtcol_win(wp);
9967 if ( redraw_cmdline
9968 || always
9969 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9970 || wp->w_cursor.col != wp->w_ru_cursor.col
9971 || wp->w_virtcol != wp->w_ru_virtcol
9972#ifdef FEAT_VIRTUALEDIT
9973 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9974#endif
9975 || wp->w_topline != wp->w_ru_topline
9976 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9977#ifdef FEAT_DIFF
9978 || wp->w_topfill != wp->w_ru_topfill
9979#endif
9980 || empty_line != wp->w_ru_empty)
9981 {
9982 cursor_off();
9983#ifdef FEAT_WINDOWS
9984 if (wp->w_status_height)
9985 {
9986 row = W_WINROW(wp) + wp->w_height;
9987 fillchar = fillchar_status(&attr, wp == curwin);
9988# ifdef FEAT_VERTSPLIT
9989 off = W_WINCOL(wp);
9990 width = W_WIDTH(wp);
9991# endif
9992 }
9993 else
9994#endif
9995 {
9996 row = Rows - 1;
9997 fillchar = ' ';
9998 attr = 0;
9999#ifdef FEAT_VERTSPLIT
10000 width = Columns;
10001 off = 0;
10002#endif
10003 }
10004
10005 /* In list mode virtcol needs to be recomputed */
10006 virtcol = wp->w_virtcol;
10007 if (wp->w_p_list && lcs_tab1 == NUL)
10008 {
10009 wp->w_p_list = FALSE;
10010 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10011 wp->w_p_list = TRUE;
10012 }
10013
10014 /*
10015 * Some sprintfs return the length, some return a pointer.
10016 * To avoid portability problems we use strlen() here.
10017 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010018 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10020 ? 0L
10021 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010022 len = STRLEN(buffer);
10023 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10025 (int)virtcol + 1);
10026
10027 /*
10028 * Add a "50%" if there is room for it.
10029 * On the last line, don't print in the last column (scrolls the
10030 * screen up on some terminals).
10031 */
10032 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010033 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 o = i + vim_strsize(buffer + i + 1);
10035#ifdef FEAT_WINDOWS
10036 if (wp->w_status_height == 0) /* can't use last char of screen */
10037#endif
10038 ++o;
10039#ifdef FEAT_VERTSPLIT
10040 this_ru_col = ru_col - (Columns - width);
10041 if (this_ru_col < 0)
10042 this_ru_col = 0;
10043#endif
10044 /* Never use more than half the window/screen width, leave the other
10045 * half for the filename. */
10046 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10047 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10048 if (this_ru_col + o < WITH_WIDTH(width))
10049 {
10050 while (this_ru_col + o < WITH_WIDTH(width))
10051 {
10052#ifdef FEAT_MBYTE
10053 if (has_mbyte)
10054 i += (*mb_char2bytes)(fillchar, buffer + i);
10055 else
10056#endif
10057 buffer[i++] = fillchar;
10058 ++o;
10059 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010060 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 }
10062 /* Truncate at window boundary. */
10063#ifdef FEAT_MBYTE
10064 if (has_mbyte)
10065 {
10066 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010067 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
10069 o += (*mb_ptr2cells)(buffer + i);
10070 if (this_ru_col + o > WITH_WIDTH(width))
10071 {
10072 buffer[i] = NUL;
10073 break;
10074 }
10075 }
10076 }
10077 else
10078#endif
10079 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10080 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10081
10082 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10083 i = redraw_cmdline;
10084 screen_fill(row, row + 1,
10085 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10086 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10087 fillchar, fillchar, attr);
10088 /* don't redraw the cmdline because of showing the ruler */
10089 redraw_cmdline = i;
10090 wp->w_ru_cursor = wp->w_cursor;
10091 wp->w_ru_virtcol = wp->w_virtcol;
10092 wp->w_ru_empty = empty_line;
10093 wp->w_ru_topline = wp->w_topline;
10094 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10095#ifdef FEAT_DIFF
10096 wp->w_ru_topfill = wp->w_topfill;
10097#endif
10098 }
10099}
10100#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010101
10102#if defined(FEAT_LINEBREAK) || defined(PROTO)
10103/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010104 * Return the width of the 'number' and 'relativenumber' column.
10105 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010106 * Otherwise it depends on 'numberwidth' and the line count.
10107 */
10108 int
10109number_width(wp)
10110 win_T *wp;
10111{
10112 int n;
10113 linenr_T lnum;
10114
Bram Moolenaar64486672010-05-16 15:46:46 +020010115 if (wp->w_p_nu)
10116 /* 'number' */
10117 lnum = wp->w_buffer->b_ml.ml_line_count;
10118 else
10119 /* 'relativenumber' */
10120 lnum = wp->w_height;
10121
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010122 if (lnum == wp->w_nrwidth_line_count)
10123 return wp->w_nrwidth_width;
10124 wp->w_nrwidth_line_count = lnum;
10125
10126 n = 0;
10127 do
10128 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010129 lnum /= 10;
10130 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010131 } while (lnum > 0);
10132
10133 /* 'numberwidth' gives the minimal width plus one */
10134 if (n < wp->w_p_nuw - 1)
10135 n = wp->w_p_nuw - 1;
10136
10137 wp->w_nrwidth_width = n;
10138 return n;
10139}
10140#endif