blob: 2c87e9b9ef0143e78d116b8f1b466badb455ba8d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
92/*
93 * The attributes that are actually active for writing to the screen.
94 */
95static int screen_attr = 0;
96
97/*
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
100 */
101static int screen_cur_row, screen_cur_col; /* last known cursor position */
102
103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106
107#ifdef FEAT_FOLDING
108static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109#endif
110
111/*
112 * Buffer for one screen line (characters and attributes).
113 */
114static schar_T *current_ScreenLine;
115
116static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000117static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_FOLDING
119static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000123static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125#ifdef FEAT_RIGHTLEFT
126static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128#else
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_VERTSPLIT
133static void draw_vsep_win __ARGS((win_T *wp, int row));
134#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000135#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000136static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000139#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void start_search_hl __ARGS((void));
141static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200142static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
144static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
145#endif
146static void screen_start_highlight __ARGS((int attr));
147static void screen_char __ARGS((unsigned off, int row, int col));
148#ifdef FEAT_MBYTE
149static void screen_char_2 __ARGS((unsigned off, int row, int col));
150#endif
151static void screenclear2 __ARGS((void));
152static void lineclear __ARGS((unsigned off, int width));
153static void lineinvalid __ARGS((unsigned off, int width));
154#ifdef FEAT_VERTSPLIT
155static void linecopy __ARGS((int to, int from, win_T *wp));
156static void redraw_block __ARGS((int row, int end, win_T *wp));
157#endif
158static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
159static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000162static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
165static int fillchar_status __ARGS((int *attr, int is_curwin));
166#endif
167#ifdef FEAT_VERTSPLIT
168static int fillchar_vsep __ARGS((int *attr));
169#endif
170#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000171static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_CMDL_INFO
174static void win_redr_ruler __ARGS((win_T *wp, int always));
175#endif
176
177#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
178/* Ugly global: overrule attribute used by screen_char() */
179static int screen_char_attr = 0;
180#endif
181
182/*
183 * Redraw the current window later, with update_screen(type).
184 * Set must_redraw only if not already set to a higher value.
185 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
186 */
187 void
188redraw_later(type)
189 int type;
190{
191 redraw_win_later(curwin, type);
192}
193
194 void
195redraw_win_later(wp, type)
196 win_T *wp;
197 int type;
198{
199 if (wp->w_redr_type < type)
200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
214redraw_later_clear()
215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
232redraw_all_later(type)
233 int type;
234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
247redraw_curbuf_later(type)
248 int type;
249{
250 redraw_buf_later(curbuf, type);
251}
252
253 void
254redraw_buf_later(buf, type)
255 buf_T *buf;
256 int type;
257{
258 win_T *wp;
259
260 FOR_ALL_WINDOWS(wp)
261 {
262 if (wp->w_buffer == buf)
263 redraw_win_later(wp, type);
264 }
265}
266
267/*
268 * Changed something in the current window, at buffer line "lnum", that
269 * requires that line and possibly other lines to be redrawn.
270 * Used when entering/leaving Insert mode with the cursor on a folded line.
271 * Used to remove the "$" from a change command.
272 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
273 * may become invalid and the whole window will have to be redrawn.
274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 void
276redrawWinline(lnum, invalid)
277 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000278 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280#ifdef FEAT_FOLDING
281 int i;
282#endif
283
284 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
285 curwin->w_redraw_top = lnum;
286 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
287 curwin->w_redraw_bot = lnum;
288 redraw_later(VALID);
289
290#ifdef FEAT_FOLDING
291 if (invalid)
292 {
293 /* A w_lines[] entry for this lnum has become invalid. */
294 i = find_wl_entry(curwin, lnum);
295 if (i >= 0)
296 curwin->w_lines[i].wl_valid = FALSE;
297 }
298#endif
299}
300
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200301#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200302 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * update all windows that are editing the current buffer
305 */
306 void
307update_curbuf(type)
308 int type;
309{
310 redraw_curbuf_later(type);
311 update_screen(type);
312}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * update_screen()
317 *
318 * Based on the current value of curwin->w_topline, transfer a screenfull
319 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
320 */
321 void
322update_screen(type)
323 int type;
324{
325 win_T *wp;
326 static int did_intro = FALSE;
327#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
328 int did_one;
329#endif
330
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000331 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 if (!screen_valid(TRUE))
333 return;
334
335 if (must_redraw)
336 {
337 if (type < must_redraw) /* use maximal type */
338 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000339
340 /* must_redraw is reset here, so that when we run into some weird
341 * reason to redraw while busy redrawing (e.g., asynchronous
342 * scrolling), or update_topline() in win_update() will cause a
343 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 must_redraw = 0;
345 }
346
347 /* Need to update w_lines[]. */
348 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
349 type = NOT_VALID;
350
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000351 /* Postpone the redrawing when it's not needed and when being called
352 * recursively. */
353 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 redraw_later(type); /* remember type for next time */
356 must_redraw = type;
357 if (type > INVERTED_ALL)
358 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
359 return;
360 }
361
362 updating_screen = TRUE;
363#ifdef FEAT_SYN_HL
364 ++display_tick; /* let syntax code know we're in a next round of
365 * display updating */
366#endif
367
368 /*
369 * if the screen was scrolled up when displaying a message, scroll it down
370 */
371 if (msg_scrolled)
372 {
373 clear_cmdline = TRUE;
374 if (msg_scrolled > Rows - 5) /* clearing is faster */
375 type = CLEAR;
376 else if (type != CLEAR)
377 {
378 check_for_delay(FALSE);
379 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
380 type = CLEAR;
381 FOR_ALL_WINDOWS(wp)
382 {
383 if (W_WINROW(wp) < msg_scrolled)
384 {
385 if (W_WINROW(wp) + wp->w_height > msg_scrolled
386 && wp->w_redr_type < REDRAW_TOP
387 && wp->w_lines_valid > 0
388 && wp->w_topline == wp->w_lines[0].wl_lnum)
389 {
390 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
391 wp->w_redr_type = REDRAW_TOP;
392 }
393 else
394 {
395 wp->w_redr_type = NOT_VALID;
396#ifdef FEAT_WINDOWS
397 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
398 <= msg_scrolled)
399 wp->w_redr_status = TRUE;
400#endif
401 }
402 }
403 }
404 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000406 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409 msg_scrolled = 0;
410 need_wait_return = FALSE;
411 }
412
413 /* reset cmdline_row now (may have been changed temporarily) */
414 compute_cmdrow();
415
416 /* Check for changed highlighting */
417 if (need_highlight_changed)
418 highlight_changed();
419
420 if (type == CLEAR) /* first clear screen */
421 {
422 screenclear(); /* will reset clear_cmdline */
423 type = NOT_VALID;
424 }
425
426 if (clear_cmdline) /* going to clear cmdline (done below) */
427 check_for_delay(FALSE);
428
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000429#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200430 /* Force redraw when width of 'number' or 'relativenumber' column
431 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
434 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 curwin->w_redr_type = NOT_VALID;
436#endif
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 /*
439 * Only start redrawing if there is really something to do.
440 */
441 if (type == INVERTED)
442 update_curswant();
443 if (curwin->w_redr_type < type
444 && !((type == VALID
445 && curwin->w_lines[0].wl_valid
446#ifdef FEAT_DIFF
447 && curwin->w_topfill == curwin->w_old_topfill
448 && curwin->w_botfill == curwin->w_old_botfill
449#endif
450 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
451#ifdef FEAT_VISUAL
452 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000453 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
455 && curwin->w_old_visual_mode == VIsual_mode
456 && (curwin->w_valid & VALID_VIRTCOL)
457 && curwin->w_old_curswant == curwin->w_curswant)
458#endif
459 ))
460 curwin->w_redr_type = type;
461
Bram Moolenaar5a305422006-04-28 22:38:25 +0000462#ifdef FEAT_WINDOWS
463 /* Redraw the tab pages line if needed. */
464 if (redraw_tabline || type >= NOT_VALID)
465 draw_tabline();
466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#ifdef FEAT_SYN_HL
469 /*
470 * Correct stored syntax highlighting info for changes in each displayed
471 * buffer. Each buffer must only be done once.
472 */
473 FOR_ALL_WINDOWS(wp)
474 {
475 if (wp->w_buffer->b_mod_set)
476 {
477# ifdef FEAT_WINDOWS
478 win_T *wwp;
479
480 /* Check if we already did this buffer. */
481 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
482 if (wwp->w_buffer == wp->w_buffer)
483 break;
484# endif
485 if (
486# ifdef FEAT_WINDOWS
487 wwp == wp &&
488# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200489 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 syn_stack_apply_changes(wp->w_buffer);
491 }
492 }
493#endif
494
495 /*
496 * Go from top to bottom through the windows, redrawing the ones that need
497 * it.
498 */
499#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
500 did_one = FALSE;
501#endif
502#ifdef FEAT_SEARCH_EXTRA
503 search_hl.rm.regprog = NULL;
504#endif
505 FOR_ALL_WINDOWS(wp)
506 {
507 if (wp->w_redr_type != 0)
508 {
509 cursor_off();
510#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
511 if (!did_one)
512 {
513 did_one = TRUE;
514# ifdef FEAT_SEARCH_EXTRA
515 start_search_hl();
516# endif
517# ifdef FEAT_CLIPBOARD
518 /* When Visual area changed, may have to update selection. */
519 if (clip_star.available && clip_isautosel())
520 clip_update_selection();
521# endif
522#ifdef FEAT_GUI
523 /* Remove the cursor before starting to do anything, because
524 * scrolling may make it difficult to redraw the text under
525 * it. */
526 if (gui.in_use)
527 gui_undraw_cursor();
528#endif
529 }
530#endif
531 win_update(wp);
532 }
533
534#ifdef FEAT_WINDOWS
535 /* redraw status line after the window to minimize cursor movement */
536 if (wp->w_redr_status)
537 {
538 cursor_off();
539 win_redr_status(wp);
540 }
541#endif
542 }
543#if defined(FEAT_SEARCH_EXTRA)
544 end_search_hl();
545#endif
546
547#ifdef FEAT_WINDOWS
548 /* Reset b_mod_set flags. Going through all windows is probably faster
549 * than going through all buffers (there could be many buffers). */
550 for (wp = firstwin; wp != NULL; wp = wp->w_next)
551 wp->w_buffer->b_mod_set = FALSE;
552#else
553 curbuf->b_mod_set = FALSE;
554#endif
555
556 updating_screen = FALSE;
557#ifdef FEAT_GUI
558 gui_may_resize_shell();
559#endif
560
561 /* Clear or redraw the command line. Done last, because scrolling may
562 * mess up the command line. */
563 if (clear_cmdline || redraw_cmdline)
564 showmode();
565
566 /* May put up an introductory message when not editing a file */
567 if (!did_intro && bufempty()
568 && curbuf->b_fname == NULL
569#ifdef FEAT_WINDOWS
570 && firstwin->w_next == NULL
571#endif
572 && vim_strchr(p_shm, SHM_INTRO) == NULL)
573 intro_message(FALSE);
574 did_intro = TRUE;
575
576#ifdef FEAT_GUI
577 /* Redraw the cursor and update the scrollbars when all screen updating is
578 * done. */
579 if (gui.in_use)
580 {
581 out_flush(); /* required before updating the cursor */
582 if (did_one)
583 gui_update_cursor(FALSE, FALSE);
584 gui_update_scrollbars(FALSE);
585 }
586#endif
587}
588
Bram Moolenaar860cae12010-06-05 23:22:07 +0200589#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200590/*
591 * Return TRUE if the cursor line in window "wp" may be concealed, according
592 * to the 'concealcursor' option.
593 */
594 int
595conceal_cursor_line(wp)
596 win_T *wp;
597{
598 int c;
599
600 if (*wp->w_p_cocu == NUL)
601 return FALSE;
602 if (get_real_state() & VISUAL)
603 c = 'v';
604 else if (State & INSERT)
605 c = 'i';
606 else if (State & NORMAL)
607 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200608 else if (State & CMDLINE)
609 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200610 else
611 return FALSE;
612 return vim_strchr(wp->w_p_cocu, c) != NULL;
613}
614
615/*
616 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
617 */
618 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200619conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200620{
621 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
622 {
623 need_cursor_line_redraw = TRUE;
624 /* Need to recompute cursor column, e.g., when starting Visual mode
625 * without concealing. */
626 curs_columns(TRUE);
627 }
628}
629
Bram Moolenaar860cae12010-06-05 23:22:07 +0200630 void
631update_single_line(wp, lnum)
632 win_T *wp;
633 linenr_T lnum;
634{
635 int row;
636 int j;
637
638 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200639 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 {
641# ifdef FEAT_GUI
642 /* Remove the cursor before starting to do anything, because scrolling
643 * may make it difficult to redraw the text under it. */
644 if (gui.in_use)
645 gui_undraw_cursor();
646# endif
647 row = 0;
648 for (j = 0; j < wp->w_lines_valid; ++j)
649 {
650 if (lnum == wp->w_lines[j].wl_lnum)
651 {
652 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200653# ifdef FEAT_SEARCH_EXTRA
654 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200655 start_search_hl();
656 prepare_search_hl(wp, lnum);
657# endif
658 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
659# if defined(FEAT_SEARCH_EXTRA)
660 end_search_hl();
661# endif
662 break;
663 }
664 row += wp->w_lines[j].wl_size;
665 }
666# ifdef FEAT_GUI
667 /* Redraw the cursor */
668 if (gui.in_use)
669 {
670 out_flush(); /* required before updating the cursor */
671 gui_update_cursor(FALSE, FALSE);
672 }
673# endif
674 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200675 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200676}
677#endif
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
680static void update_prepare __ARGS((void));
681static void update_finish __ARGS((void));
682
683/*
684 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000685 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 */
687 static void
688update_prepare()
689{
690 cursor_off();
691 updating_screen = TRUE;
692#ifdef FEAT_GUI
693 /* Remove the cursor before starting to do anything, because scrolling may
694 * make it difficult to redraw the text under it. */
695 if (gui.in_use)
696 gui_undraw_cursor();
697#endif
698#ifdef FEAT_SEARCH_EXTRA
699 start_search_hl();
700#endif
701}
702
703/*
704 * Finish updating one or more windows.
705 */
706 static void
707update_finish()
708{
709 if (redraw_cmdline)
710 showmode();
711
712# ifdef FEAT_SEARCH_EXTRA
713 end_search_hl();
714# endif
715
716 updating_screen = FALSE;
717
718# ifdef FEAT_GUI
719 gui_may_resize_shell();
720
721 /* Redraw the cursor and update the scrollbars when all screen updating is
722 * done. */
723 if (gui.in_use)
724 {
725 out_flush(); /* required before updating the cursor */
726 gui_update_cursor(FALSE, FALSE);
727 gui_update_scrollbars(FALSE);
728 }
729# endif
730}
731#endif
732
733#if defined(FEAT_SIGNS) || defined(PROTO)
734 void
735update_debug_sign(buf, lnum)
736 buf_T *buf;
737 linenr_T lnum;
738{
739 win_T *wp;
740 int doit = FALSE;
741
742# ifdef FEAT_FOLDING
743 win_foldinfo.fi_level = 0;
744# endif
745
746 /* update/delete a specific mark */
747 FOR_ALL_WINDOWS(wp)
748 {
749 if (buf != NULL && lnum > 0)
750 {
751 if (wp->w_buffer == buf && lnum >= wp->w_topline
752 && lnum < wp->w_botline)
753 {
754 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
755 wp->w_redraw_top = lnum;
756 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
757 wp->w_redraw_bot = lnum;
758 redraw_win_later(wp, VALID);
759 }
760 }
761 else
762 redraw_win_later(wp, VALID);
763 if (wp->w_redr_type != 0)
764 doit = TRUE;
765 }
766
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000767 /* Return when there is nothing to do or screen updating already
768 * happening. */
769 if (!doit || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 return;
771
772 /* update all windows that need updating */
773 update_prepare();
774
775# ifdef FEAT_WINDOWS
776 for (wp = firstwin; wp; wp = wp->w_next)
777 {
778 if (wp->w_redr_type != 0)
779 win_update(wp);
780 if (wp->w_redr_status)
781 win_redr_status(wp);
782 }
783# else
784 if (curwin->w_redr_type != 0)
785 win_update(curwin);
786# endif
787
788 update_finish();
789}
790#endif
791
792
793#if defined(FEAT_GUI) || defined(PROTO)
794/*
795 * Update a single window, its status line and maybe the command line msg.
796 * Used for the GUI scrollbar.
797 */
798 void
799updateWindow(wp)
800 win_T *wp;
801{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000802 /* return if already busy updating */
803 if (updating_screen)
804 return;
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 update_prepare();
807
808#ifdef FEAT_CLIPBOARD
809 /* When Visual area changed, may have to update selection. */
810 if (clip_star.available && clip_isautosel())
811 clip_update_selection();
812#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000817 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000818 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000819 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (wp->w_redr_status
822# ifdef FEAT_CMDL_INFO
823 || p_ru
824# endif
825# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000826 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827# endif
828 )
829 win_redr_status(wp);
830#endif
831
832 update_finish();
833}
834#endif
835
836/*
837 * Update a single window.
838 *
839 * This may cause the windows below it also to be redrawn (when clearing the
840 * screen or scrolling lines).
841 *
842 * How the window is redrawn depends on wp->w_redr_type. Each type also
843 * implies the one below it.
844 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000845 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
847 * INVERTED redraw the changed part of the Visual area
848 * INVERTED_ALL redraw the whole Visual area
849 * VALID 1. scroll up/down to adjust for a changed w_topline
850 * 2. update lines at the top when scrolled down
851 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000852 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 * b_mod_top and b_mod_bot.
854 * - if wp->w_redraw_top non-zero, redraw lines between
855 * wp->w_redraw_top and wp->w_redr_bot.
856 * - continue redrawing when syntax status is invalid.
857 * 4. if scrolled up, update lines at the bottom.
858 * This results in three areas that may need updating:
859 * top: from first row to top_end (when scrolled down)
860 * mid: from mid_start to mid_end (update inversion or changed text)
861 * bot: from bot_start to last row (when scrolled up)
862 */
863 static void
864win_update(wp)
865 win_T *wp;
866{
867 buf_T *buf = wp->w_buffer;
868 int type;
869 int top_end = 0; /* Below last row of the top area that needs
870 updating. 0 when no top area updating. */
871 int mid_start = 999;/* first row of the mid area that needs
872 updating. 999 when no mid area updating. */
873 int mid_end = 0; /* Below last row of the mid area that needs
874 updating. 0 when no mid area updating. */
875 int bot_start = 999;/* first row of the bot area that needs
876 updating. 999 when no bot area updating */
877#ifdef FEAT_VISUAL
878 int scrolled_down = FALSE; /* TRUE when scrolled down when
879 w_topline got smaller a bit */
880#endif
881#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000882 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 int top_to_mod = FALSE; /* redraw above mod_top */
884#endif
885
886 int row; /* current window row to display */
887 linenr_T lnum; /* current buffer lnum to display */
888 int idx; /* current index in w_lines[] */
889 int srow; /* starting row of the current line */
890
891 int eof = FALSE; /* if TRUE, we hit the end of the file */
892 int didline = FALSE; /* if TRUE, we finished the last line */
893 int i;
894 long j;
895 static int recursive = FALSE; /* being called recursively */
896 int old_botline = wp->w_botline;
897#ifdef FEAT_FOLDING
898 long fold_count;
899#endif
900#ifdef FEAT_SYN_HL
901 /* remember what happened to the previous line, to know if
902 * check_visual_highlight() can be used */
903#define DID_NONE 1 /* didn't update a line */
904#define DID_LINE 2 /* updated a normal line */
905#define DID_FOLD 3 /* updated a folded line */
906 int did_update = DID_NONE;
907 linenr_T syntax_last_parsed = 0; /* last parsed text line */
908#endif
909 linenr_T mod_top = 0;
910 linenr_T mod_bot = 0;
911#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
912 int save_got_int;
913#endif
914
915 type = wp->w_redr_type;
916
917 if (type == NOT_VALID)
918 {
919#ifdef FEAT_WINDOWS
920 wp->w_redr_status = TRUE;
921#endif
922 wp->w_lines_valid = 0;
923 }
924
925 /* Window is zero-height: nothing to draw. */
926 if (wp->w_height == 0)
927 {
928 wp->w_redr_type = 0;
929 return;
930 }
931
932#ifdef FEAT_VERTSPLIT
933 /* Window is zero-width: Only need to draw the separator. */
934 if (wp->w_width == 0)
935 {
936 /* draw the vertical separator right of this window */
937 draw_vsep_win(wp, 0);
938 wp->w_redr_type = 0;
939 return;
940 }
941#endif
942
943#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200944 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#endif
946
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000947#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200948 /* Force redraw when width of 'number' or 'relativenumber' column
949 * changes. */
950 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000951 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000952 {
953 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000954 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000955 }
956 else
957#endif
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
960 {
961 /*
962 * When there are both inserted/deleted lines and specific lines to be
963 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
964 * everything (only happens when redrawing is off for while).
965 */
966 type = NOT_VALID;
967 }
968 else
969 {
970 /*
971 * Set mod_top to the first line that needs displaying because of
972 * changes. Set mod_bot to the first line after the changes.
973 */
974 mod_top = wp->w_redraw_top;
975 if (wp->w_redraw_bot != 0)
976 mod_bot = wp->w_redraw_bot + 1;
977 else
978 mod_bot = 0;
979 wp->w_redraw_top = 0; /* reset for next time */
980 wp->w_redraw_bot = 0;
981 if (buf->b_mod_set)
982 {
983 if (mod_top == 0 || mod_top > buf->b_mod_top)
984 {
985 mod_top = buf->b_mod_top;
986#ifdef FEAT_SYN_HL
987 /* Need to redraw lines above the change that may be included
988 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200989 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200991 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (mod_top < 1)
993 mod_top = 1;
994 }
995#endif
996 }
997 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
998 mod_bot = buf->b_mod_bot;
999
1000#ifdef FEAT_SEARCH_EXTRA
1001 /* When 'hlsearch' is on and using a multi-line search pattern, a
1002 * change in one line may make the Search highlighting in a
1003 * previous line invalid. Simple solution: redraw all visible
1004 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001005 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001007 if (search_hl.rm.regprog != NULL
1008 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001010 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001011 {
1012 cur = wp->w_match_head;
1013 while (cur != NULL)
1014 {
1015 if (cur->match.regprog != NULL
1016 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001017 {
1018 top_to_mod = TRUE;
1019 break;
1020 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001021 cur = cur->next;
1022 }
1023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#endif
1025 }
1026#ifdef FEAT_FOLDING
1027 if (mod_top != 0 && hasAnyFolding(wp))
1028 {
1029 linenr_T lnumt, lnumb;
1030
1031 /*
1032 * A change in a line can cause lines above it to become folded or
1033 * unfolded. Find the top most buffer line that may be affected.
1034 * If the line was previously folded and displayed, get the first
1035 * line of that fold. If the line is folded now, get the first
1036 * folded line. Use the minimum of these two.
1037 */
1038
1039 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1040 * the line below it. If there is no valid entry, use w_topline.
1041 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1042 * to this line. If there is no valid entry, use MAXLNUM. */
1043 lnumt = wp->w_topline;
1044 lnumb = MAXLNUM;
1045 for (i = 0; i < wp->w_lines_valid; ++i)
1046 if (wp->w_lines[i].wl_valid)
1047 {
1048 if (wp->w_lines[i].wl_lastlnum < mod_top)
1049 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1050 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1051 {
1052 lnumb = wp->w_lines[i].wl_lnum;
1053 /* When there is a fold column it might need updating
1054 * in the next line ("J" just above an open fold). */
1055 if (wp->w_p_fdc > 0)
1056 ++lnumb;
1057 }
1058 }
1059
1060 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1061 if (mod_top > lnumt)
1062 mod_top = lnumt;
1063
1064 /* Now do the same for the bottom line (one above mod_bot). */
1065 --mod_bot;
1066 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1067 ++mod_bot;
1068 if (mod_bot < lnumb)
1069 mod_bot = lnumb;
1070 }
1071#endif
1072
1073 /* When a change starts above w_topline and the end is below
1074 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001075 * If the end of the change is above w_topline: do like no change was
1076 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (mod_top != 0 && mod_top < wp->w_topline)
1078 {
1079 if (mod_bot > wp->w_topline)
1080 mod_top = wp->w_topline;
1081#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001082 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 top_end = 1;
1084#endif
1085 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001086
1087 /* When line numbers are displayed need to redraw all lines below
1088 * inserted/deleted lines. */
1089 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1090 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092
1093 /*
1094 * When only displaying the lines at the top, set top_end. Used when
1095 * window has scrolled down for msg_scrolled.
1096 */
1097 if (type == REDRAW_TOP)
1098 {
1099 j = 0;
1100 for (i = 0; i < wp->w_lines_valid; ++i)
1101 {
1102 j += wp->w_lines[i].wl_size;
1103 if (j >= wp->w_upd_rows)
1104 {
1105 top_end = j;
1106 break;
1107 }
1108 }
1109 if (top_end == 0)
1110 /* not found (cannot happen?): redraw everything */
1111 type = NOT_VALID;
1112 else
1113 /* top area defined, the rest is VALID */
1114 type = VALID;
1115 }
1116
Bram Moolenaar367329b2007-08-30 11:53:22 +00001117 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001118 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1119 * non-zero and thus not FALSE) will indicate that screenclear() was not
1120 * called. */
1121 if (screen_cleared)
1122 screen_cleared = MAYBE;
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 /*
1125 * If there are no changes on the screen that require a complete redraw,
1126 * handle three cases:
1127 * 1: we are off the top of the screen by a few lines: scroll down
1128 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1129 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1130 * w_lines[] that needs updating.
1131 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001132 if ((type == VALID || type == SOME_VALID
1133 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#ifdef FEAT_DIFF
1135 && !wp->w_botfill && !wp->w_old_botfill
1136#endif
1137 )
1138 {
1139 if (mod_top != 0 && wp->w_topline == mod_top)
1140 {
1141 /*
1142 * w_topline is the first changed line, the scrolling will be done
1143 * further down.
1144 */
1145 }
1146 else if (wp->w_lines[0].wl_valid
1147 && (wp->w_topline < wp->w_lines[0].wl_lnum
1148#ifdef FEAT_DIFF
1149 || (wp->w_topline == wp->w_lines[0].wl_lnum
1150 && wp->w_topfill > wp->w_old_topfill)
1151#endif
1152 ))
1153 {
1154 /*
1155 * New topline is above old topline: May scroll down.
1156 */
1157#ifdef FEAT_FOLDING
1158 if (hasAnyFolding(wp))
1159 {
1160 linenr_T ln;
1161
1162 /* count the number of lines we are off, counting a sequence
1163 * of folded lines as one */
1164 j = 0;
1165 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1166 {
1167 ++j;
1168 if (j >= wp->w_height - 2)
1169 break;
1170 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1171 }
1172 }
1173 else
1174#endif
1175 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1176 if (j < wp->w_height - 2) /* not too far off */
1177 {
1178 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1179#ifdef FEAT_DIFF
1180 /* insert extra lines for previously invisible filler lines */
1181 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1182 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1183 - wp->w_old_topfill;
1184#endif
1185 if (i < wp->w_height - 2) /* less than a screen off */
1186 {
1187 /*
1188 * Try to insert the correct number of lines.
1189 * If not the last window, delete the lines at the bottom.
1190 * win_ins_lines may fail when the terminal can't do it.
1191 */
1192 if (i > 0)
1193 check_for_delay(FALSE);
1194 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1195 {
1196 if (wp->w_lines_valid != 0)
1197 {
1198 /* Need to update rows that are new, stop at the
1199 * first one that scrolled down. */
1200 top_end = i;
1201#ifdef FEAT_VISUAL
1202 scrolled_down = TRUE;
1203#endif
1204
1205 /* Move the entries that were scrolled, disable
1206 * the entries for the lines to be redrawn. */
1207 if ((wp->w_lines_valid += j) > wp->w_height)
1208 wp->w_lines_valid = wp->w_height;
1209 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1210 wp->w_lines[idx] = wp->w_lines[idx - j];
1211 while (idx >= 0)
1212 wp->w_lines[idx--].wl_valid = FALSE;
1213 }
1214 }
1215 else
1216 mid_start = 0; /* redraw all lines */
1217 }
1218 else
1219 mid_start = 0; /* redraw all lines */
1220 }
1221 else
1222 mid_start = 0; /* redraw all lines */
1223 }
1224 else
1225 {
1226 /*
1227 * New topline is at or below old topline: May scroll up.
1228 * When topline didn't change, find first entry in w_lines[] that
1229 * needs updating.
1230 */
1231
1232 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1233 j = -1;
1234 row = 0;
1235 for (i = 0; i < wp->w_lines_valid; i++)
1236 {
1237 if (wp->w_lines[i].wl_valid
1238 && wp->w_lines[i].wl_lnum == wp->w_topline)
1239 {
1240 j = i;
1241 break;
1242 }
1243 row += wp->w_lines[i].wl_size;
1244 }
1245 if (j == -1)
1246 {
1247 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1248 * lines */
1249 mid_start = 0;
1250 }
1251 else
1252 {
1253 /*
1254 * Try to delete the correct number of lines.
1255 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1256 */
1257#ifdef FEAT_DIFF
1258 /* If the topline didn't change, delete old filler lines,
1259 * otherwise delete filler lines of the new topline... */
1260 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1261 row += wp->w_old_topfill;
1262 else
1263 row += diff_check_fill(wp, wp->w_topline);
1264 /* ... but don't delete new filler lines. */
1265 row -= wp->w_topfill;
1266#endif
1267 if (row > 0)
1268 {
1269 check_for_delay(FALSE);
1270 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1271 bot_start = wp->w_height - row;
1272 else
1273 mid_start = 0; /* redraw all lines */
1274 }
1275 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1276 {
1277 /*
1278 * Skip the lines (below the deleted lines) that are still
1279 * valid and don't need redrawing. Copy their info
1280 * upwards, to compensate for the deleted lines. Set
1281 * bot_start to the first row that needs redrawing.
1282 */
1283 bot_start = 0;
1284 idx = 0;
1285 for (;;)
1286 {
1287 wp->w_lines[idx] = wp->w_lines[j];
1288 /* stop at line that didn't fit, unless it is still
1289 * valid (no lines deleted) */
1290 if (row > 0 && bot_start + row
1291 + (int)wp->w_lines[j].wl_size > wp->w_height)
1292 {
1293 wp->w_lines_valid = idx + 1;
1294 break;
1295 }
1296 bot_start += wp->w_lines[idx++].wl_size;
1297
1298 /* stop at the last valid entry in w_lines[].wl_size */
1299 if (++j >= wp->w_lines_valid)
1300 {
1301 wp->w_lines_valid = idx;
1302 break;
1303 }
1304 }
1305#ifdef FEAT_DIFF
1306 /* Correct the first entry for filler lines at the top
1307 * when it won't get updated below. */
1308 if (wp->w_p_diff && bot_start > 0)
1309 wp->w_lines[0].wl_size =
1310 plines_win_nofill(wp, wp->w_topline, TRUE)
1311 + wp->w_topfill;
1312#endif
1313 }
1314 }
1315 }
1316
1317 /* When starting redraw in the first line, redraw all lines. When
1318 * there is only one window it's probably faster to clear the screen
1319 * first. */
1320 if (mid_start == 0)
1321 {
1322 mid_end = wp->w_height;
1323 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001324 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001325 /* Clear the screen when it was not done by win_del_lines() or
1326 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1327 * then. */
1328 if (screen_cleared != TRUE)
1329 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001330#ifdef FEAT_WINDOWS
1331 /* The screen was cleared, redraw the tab pages line. */
1332 if (redraw_tabline)
1333 draw_tabline();
1334#endif
1335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001337
1338 /* When win_del_lines() or win_ins_lines() caused the screen to be
1339 * cleared (only happens for the first window) or when screenclear()
1340 * was called directly above, "must_redraw" will have been set to
1341 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1342 if (screen_cleared == TRUE)
1343 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345 else
1346 {
1347 /* Not VALID or INVERTED: redraw all lines. */
1348 mid_start = 0;
1349 mid_end = wp->w_height;
1350 }
1351
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001352 if (type == SOME_VALID)
1353 {
1354 /* SOME_VALID: redraw all lines. */
1355 mid_start = 0;
1356 mid_end = wp->w_height;
1357 type = NOT_VALID;
1358 }
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360#ifdef FEAT_VISUAL
1361 /* check if we are updating or removing the inverted part */
1362 if ((VIsual_active && buf == curwin->w_buffer)
1363 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1364 {
1365 linenr_T from, to;
1366
1367 if (VIsual_active)
1368 {
1369 if (VIsual_active
1370 && (VIsual_mode != wp->w_old_visual_mode
1371 || type == INVERTED_ALL))
1372 {
1373 /*
1374 * If the type of Visual selection changed, redraw the whole
1375 * selection. Also when the ownership of the X selection is
1376 * gained or lost.
1377 */
1378 if (curwin->w_cursor.lnum < VIsual.lnum)
1379 {
1380 from = curwin->w_cursor.lnum;
1381 to = VIsual.lnum;
1382 }
1383 else
1384 {
1385 from = VIsual.lnum;
1386 to = curwin->w_cursor.lnum;
1387 }
1388 /* redraw more when the cursor moved as well */
1389 if (wp->w_old_cursor_lnum < from)
1390 from = wp->w_old_cursor_lnum;
1391 if (wp->w_old_cursor_lnum > to)
1392 to = wp->w_old_cursor_lnum;
1393 if (wp->w_old_visual_lnum < from)
1394 from = wp->w_old_visual_lnum;
1395 if (wp->w_old_visual_lnum > to)
1396 to = wp->w_old_visual_lnum;
1397 }
1398 else
1399 {
1400 /*
1401 * Find the line numbers that need to be updated: The lines
1402 * between the old cursor position and the current cursor
1403 * position. Also check if the Visual position changed.
1404 */
1405 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1406 {
1407 from = curwin->w_cursor.lnum;
1408 to = wp->w_old_cursor_lnum;
1409 }
1410 else
1411 {
1412 from = wp->w_old_cursor_lnum;
1413 to = curwin->w_cursor.lnum;
1414 if (from == 0) /* Visual mode just started */
1415 from = to;
1416 }
1417
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001418 if (VIsual.lnum != wp->w_old_visual_lnum
1419 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 {
1421 if (wp->w_old_visual_lnum < from
1422 && wp->w_old_visual_lnum != 0)
1423 from = wp->w_old_visual_lnum;
1424 if (wp->w_old_visual_lnum > to)
1425 to = wp->w_old_visual_lnum;
1426 if (VIsual.lnum < from)
1427 from = VIsual.lnum;
1428 if (VIsual.lnum > to)
1429 to = VIsual.lnum;
1430 }
1431 }
1432
1433 /*
1434 * If in block mode and changed column or curwin->w_curswant:
1435 * update all lines.
1436 * First compute the actual start and end column.
1437 */
1438 if (VIsual_mode == Ctrl_V)
1439 {
1440 colnr_T fromc, toc;
1441
1442 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1443 ++toc;
1444 if (curwin->w_curswant == MAXCOL)
1445 toc = MAXCOL;
1446
1447 if (fromc != wp->w_old_cursor_fcol
1448 || toc != wp->w_old_cursor_lcol)
1449 {
1450 if (from > VIsual.lnum)
1451 from = VIsual.lnum;
1452 if (to < VIsual.lnum)
1453 to = VIsual.lnum;
1454 }
1455 wp->w_old_cursor_fcol = fromc;
1456 wp->w_old_cursor_lcol = toc;
1457 }
1458 }
1459 else
1460 {
1461 /* Use the line numbers of the old Visual area. */
1462 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1463 {
1464 from = wp->w_old_cursor_lnum;
1465 to = wp->w_old_visual_lnum;
1466 }
1467 else
1468 {
1469 from = wp->w_old_visual_lnum;
1470 to = wp->w_old_cursor_lnum;
1471 }
1472 }
1473
1474 /*
1475 * There is no need to update lines above the top of the window.
1476 */
1477 if (from < wp->w_topline)
1478 from = wp->w_topline;
1479
1480 /*
1481 * If we know the value of w_botline, use it to restrict the update to
1482 * the lines that are visible in the window.
1483 */
1484 if (wp->w_valid & VALID_BOTLINE)
1485 {
1486 if (from >= wp->w_botline)
1487 from = wp->w_botline - 1;
1488 if (to >= wp->w_botline)
1489 to = wp->w_botline - 1;
1490 }
1491
1492 /*
1493 * Find the minimal part to be updated.
1494 * Watch out for scrolling that made entries in w_lines[] invalid.
1495 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1496 * top_end; need to redraw from top_end to the "to" line.
1497 * A middle mouse click with a Visual selection may change the text
1498 * above the Visual area and reset wl_valid, do count these for
1499 * mid_end (in srow).
1500 */
1501 if (mid_start > 0)
1502 {
1503 lnum = wp->w_topline;
1504 idx = 0;
1505 srow = 0;
1506 if (scrolled_down)
1507 mid_start = top_end;
1508 else
1509 mid_start = 0;
1510 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1511 {
1512 if (wp->w_lines[idx].wl_valid)
1513 mid_start += wp->w_lines[idx].wl_size;
1514 else if (!scrolled_down)
1515 srow += wp->w_lines[idx].wl_size;
1516 ++idx;
1517# ifdef FEAT_FOLDING
1518 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1519 lnum = wp->w_lines[idx].wl_lnum;
1520 else
1521# endif
1522 ++lnum;
1523 }
1524 srow += mid_start;
1525 mid_end = wp->w_height;
1526 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1527 {
1528 if (wp->w_lines[idx].wl_valid
1529 && wp->w_lines[idx].wl_lnum >= to + 1)
1530 {
1531 /* Only update until first row of this line */
1532 mid_end = srow;
1533 break;
1534 }
1535 srow += wp->w_lines[idx].wl_size;
1536 }
1537 }
1538 }
1539
1540 if (VIsual_active && buf == curwin->w_buffer)
1541 {
1542 wp->w_old_visual_mode = VIsual_mode;
1543 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1544 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001545 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 wp->w_old_curswant = curwin->w_curswant;
1547 }
1548 else
1549 {
1550 wp->w_old_visual_mode = 0;
1551 wp->w_old_cursor_lnum = 0;
1552 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001553 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 }
1555#endif /* FEAT_VISUAL */
1556
1557#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1558 /* reset got_int, otherwise regexp won't work */
1559 save_got_int = got_int;
1560 got_int = 0;
1561#endif
1562#ifdef FEAT_FOLDING
1563 win_foldinfo.fi_level = 0;
1564#endif
1565
1566 /*
1567 * Update all the window rows.
1568 */
1569 idx = 0; /* first entry in w_lines[].wl_size */
1570 row = 0;
1571 srow = 0;
1572 lnum = wp->w_topline; /* first line shown in window */
1573 for (;;)
1574 {
1575 /* stop updating when reached the end of the window (check for _past_
1576 * the end of the window is at the end of the loop) */
1577 if (row == wp->w_height)
1578 {
1579 didline = TRUE;
1580 break;
1581 }
1582
1583 /* stop updating when hit the end of the file */
1584 if (lnum > buf->b_ml.ml_line_count)
1585 {
1586 eof = TRUE;
1587 break;
1588 }
1589
1590 /* Remember the starting row of the line that is going to be dealt
1591 * with. It is used further down when the line doesn't fit. */
1592 srow = row;
1593
1594 /*
1595 * Update a line when it is in an area that needs updating, when it
1596 * has changes or w_lines[idx] is invalid.
1597 * bot_start may be halfway a wrapped line after using
1598 * win_del_lines(), check if the current line includes it.
1599 * When syntax folding is being used, the saved syntax states will
1600 * already have been updated, we can't see where the syntax state is
1601 * the same again, just update until the end of the window.
1602 */
1603 if (row < top_end
1604 || (row >= mid_start && row < mid_end)
1605#ifdef FEAT_SEARCH_EXTRA
1606 || top_to_mod
1607#endif
1608 || idx >= wp->w_lines_valid
1609 || (row + wp->w_lines[idx].wl_size > bot_start)
1610 || (mod_top != 0
1611 && (lnum == mod_top
1612 || (lnum >= mod_top
1613 && (lnum < mod_bot
1614#ifdef FEAT_SYN_HL
1615 || did_update == DID_FOLD
1616 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001617 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 && (
1619# ifdef FEAT_FOLDING
1620 (foldmethodIsSyntax(wp)
1621 && hasAnyFolding(wp)) ||
1622# endif
1623 syntax_check_changed(lnum)))
1624#endif
1625 )))))
1626 {
1627#ifdef FEAT_SEARCH_EXTRA
1628 if (lnum == mod_top)
1629 top_to_mod = FALSE;
1630#endif
1631
1632 /*
1633 * When at start of changed lines: May scroll following lines
1634 * up or down to minimize redrawing.
1635 * Don't do this when the change continues until the end.
1636 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1637 */
1638 if (lnum == mod_top
1639 && mod_bot != MAXLNUM
1640 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1641 {
1642 int old_rows = 0;
1643 int new_rows = 0;
1644 int xtra_rows;
1645 linenr_T l;
1646
1647 /* Count the old number of window rows, using w_lines[], which
1648 * should still contain the sizes for the lines as they are
1649 * currently displayed. */
1650 for (i = idx; i < wp->w_lines_valid; ++i)
1651 {
1652 /* Only valid lines have a meaningful wl_lnum. Invalid
1653 * lines are part of the changed area. */
1654 if (wp->w_lines[i].wl_valid
1655 && wp->w_lines[i].wl_lnum == mod_bot)
1656 break;
1657 old_rows += wp->w_lines[i].wl_size;
1658#ifdef FEAT_FOLDING
1659 if (wp->w_lines[i].wl_valid
1660 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1661 {
1662 /* Must have found the last valid entry above mod_bot.
1663 * Add following invalid entries. */
1664 ++i;
1665 while (i < wp->w_lines_valid
1666 && !wp->w_lines[i].wl_valid)
1667 old_rows += wp->w_lines[i++].wl_size;
1668 break;
1669 }
1670#endif
1671 }
1672
1673 if (i >= wp->w_lines_valid)
1674 {
1675 /* We can't find a valid line below the changed lines,
1676 * need to redraw until the end of the window.
1677 * Inserting/deleting lines has no use. */
1678 bot_start = 0;
1679 }
1680 else
1681 {
1682 /* Able to count old number of rows: Count new window
1683 * rows, and may insert/delete lines */
1684 j = idx;
1685 for (l = lnum; l < mod_bot; ++l)
1686 {
1687#ifdef FEAT_FOLDING
1688 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1689 ++new_rows;
1690 else
1691#endif
1692#ifdef FEAT_DIFF
1693 if (l == wp->w_topline)
1694 new_rows += plines_win_nofill(wp, l, TRUE)
1695 + wp->w_topfill;
1696 else
1697#endif
1698 new_rows += plines_win(wp, l, TRUE);
1699 ++j;
1700 if (new_rows > wp->w_height - row - 2)
1701 {
1702 /* it's getting too much, must redraw the rest */
1703 new_rows = 9999;
1704 break;
1705 }
1706 }
1707 xtra_rows = new_rows - old_rows;
1708 if (xtra_rows < 0)
1709 {
1710 /* May scroll text up. If there is not enough
1711 * remaining text or scrolling fails, must redraw the
1712 * rest. If scrolling works, must redraw the text
1713 * below the scrolled text. */
1714 if (row - xtra_rows >= wp->w_height - 2)
1715 mod_bot = MAXLNUM;
1716 else
1717 {
1718 check_for_delay(FALSE);
1719 if (win_del_lines(wp, row,
1720 -xtra_rows, FALSE, FALSE) == FAIL)
1721 mod_bot = MAXLNUM;
1722 else
1723 bot_start = wp->w_height + xtra_rows;
1724 }
1725 }
1726 else if (xtra_rows > 0)
1727 {
1728 /* May scroll text down. If there is not enough
1729 * remaining text of scrolling fails, must redraw the
1730 * rest. */
1731 if (row + xtra_rows >= wp->w_height - 2)
1732 mod_bot = MAXLNUM;
1733 else
1734 {
1735 check_for_delay(FALSE);
1736 if (win_ins_lines(wp, row + old_rows,
1737 xtra_rows, FALSE, FALSE) == FAIL)
1738 mod_bot = MAXLNUM;
1739 else if (top_end > row + old_rows)
1740 /* Scrolled the part at the top that requires
1741 * updating down. */
1742 top_end += xtra_rows;
1743 }
1744 }
1745
1746 /* When not updating the rest, may need to move w_lines[]
1747 * entries. */
1748 if (mod_bot != MAXLNUM && i != j)
1749 {
1750 if (j < i)
1751 {
1752 int x = row + new_rows;
1753
1754 /* move entries in w_lines[] upwards */
1755 for (;;)
1756 {
1757 /* stop at last valid entry in w_lines[] */
1758 if (i >= wp->w_lines_valid)
1759 {
1760 wp->w_lines_valid = j;
1761 break;
1762 }
1763 wp->w_lines[j] = wp->w_lines[i];
1764 /* stop at a line that won't fit */
1765 if (x + (int)wp->w_lines[j].wl_size
1766 > wp->w_height)
1767 {
1768 wp->w_lines_valid = j + 1;
1769 break;
1770 }
1771 x += wp->w_lines[j++].wl_size;
1772 ++i;
1773 }
1774 if (bot_start > x)
1775 bot_start = x;
1776 }
1777 else /* j > i */
1778 {
1779 /* move entries in w_lines[] downwards */
1780 j -= i;
1781 wp->w_lines_valid += j;
1782 if (wp->w_lines_valid > wp->w_height)
1783 wp->w_lines_valid = wp->w_height;
1784 for (i = wp->w_lines_valid; i - j >= idx; --i)
1785 wp->w_lines[i] = wp->w_lines[i - j];
1786
1787 /* The w_lines[] entries for inserted lines are
1788 * now invalid, but wl_size may be used above.
1789 * Reset to zero. */
1790 while (i >= idx)
1791 {
1792 wp->w_lines[i].wl_size = 0;
1793 wp->w_lines[i--].wl_valid = FALSE;
1794 }
1795 }
1796 }
1797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 }
1799
1800#ifdef FEAT_FOLDING
1801 /*
1802 * When lines are folded, display one line for all of them.
1803 * Otherwise, display normally (can be several display lines when
1804 * 'wrap' is on).
1805 */
1806 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1807 if (fold_count != 0)
1808 {
1809 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1810 ++row;
1811 --fold_count;
1812 wp->w_lines[idx].wl_folded = TRUE;
1813 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1814# ifdef FEAT_SYN_HL
1815 did_update = DID_FOLD;
1816# endif
1817 }
1818 else
1819#endif
1820 if (idx < wp->w_lines_valid
1821 && wp->w_lines[idx].wl_valid
1822 && wp->w_lines[idx].wl_lnum == lnum
1823 && lnum > wp->w_topline
1824 && !(dy_flags & DY_LASTLINE)
1825 && srow + wp->w_lines[idx].wl_size > wp->w_height
1826#ifdef FEAT_DIFF
1827 && diff_check_fill(wp, lnum) == 0
1828#endif
1829 )
1830 {
1831 /* This line is not going to fit. Don't draw anything here,
1832 * will draw "@ " lines below. */
1833 row = wp->w_height + 1;
1834 }
1835 else
1836 {
1837#ifdef FEAT_SEARCH_EXTRA
1838 prepare_search_hl(wp, lnum);
1839#endif
1840#ifdef FEAT_SYN_HL
1841 /* Let the syntax stuff know we skipped a few lines. */
1842 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001843 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 syntax_end_parsing(syntax_last_parsed + 1);
1845#endif
1846
1847 /*
1848 * Display one line.
1849 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001850 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852#ifdef FEAT_FOLDING
1853 wp->w_lines[idx].wl_folded = FALSE;
1854 wp->w_lines[idx].wl_lastlnum = lnum;
1855#endif
1856#ifdef FEAT_SYN_HL
1857 did_update = DID_LINE;
1858 syntax_last_parsed = lnum;
1859#endif
1860 }
1861
1862 wp->w_lines[idx].wl_lnum = lnum;
1863 wp->w_lines[idx].wl_valid = TRUE;
1864 if (row > wp->w_height) /* past end of screen */
1865 {
1866 /* we may need the size of that too long line later on */
1867 if (dollar_vcol == 0)
1868 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1869 ++idx;
1870 break;
1871 }
1872 if (dollar_vcol == 0)
1873 wp->w_lines[idx].wl_size = row - srow;
1874 ++idx;
1875#ifdef FEAT_FOLDING
1876 lnum += fold_count + 1;
1877#else
1878 ++lnum;
1879#endif
1880 }
1881 else
1882 {
1883 /* This line does not need updating, advance to the next one */
1884 row += wp->w_lines[idx++].wl_size;
1885 if (row > wp->w_height) /* past end of screen */
1886 break;
1887#ifdef FEAT_FOLDING
1888 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1889#else
1890 ++lnum;
1891#endif
1892#ifdef FEAT_SYN_HL
1893 did_update = DID_NONE;
1894#endif
1895 }
1896
1897 if (lnum > buf->b_ml.ml_line_count)
1898 {
1899 eof = TRUE;
1900 break;
1901 }
1902 }
1903 /*
1904 * End of loop over all window lines.
1905 */
1906
1907
1908 if (idx > wp->w_lines_valid)
1909 wp->w_lines_valid = idx;
1910
1911#ifdef FEAT_SYN_HL
1912 /*
1913 * Let the syntax stuff know we stop parsing here.
1914 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001915 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 syntax_end_parsing(syntax_last_parsed + 1);
1917#endif
1918
1919 /*
1920 * If we didn't hit the end of the file, and we didn't finish the last
1921 * line we were working on, then the line didn't fit.
1922 */
1923 wp->w_empty_rows = 0;
1924#ifdef FEAT_DIFF
1925 wp->w_filler_rows = 0;
1926#endif
1927 if (!eof && !didline)
1928 {
1929 if (lnum == wp->w_topline)
1930 {
1931 /*
1932 * Single line that does not fit!
1933 * Don't overwrite it, it can be edited.
1934 */
1935 wp->w_botline = lnum + 1;
1936 }
1937#ifdef FEAT_DIFF
1938 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1939 {
1940 /* Window ends in filler lines. */
1941 wp->w_botline = lnum;
1942 wp->w_filler_rows = wp->w_height - srow;
1943 }
1944#endif
1945 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1946 {
1947 /*
1948 * Last line isn't finished: Display "@@@" at the end.
1949 */
1950 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1951 W_WINROW(wp) + wp->w_height,
1952 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1953 '@', '@', hl_attr(HLF_AT));
1954 set_empty_rows(wp, srow);
1955 wp->w_botline = lnum;
1956 }
1957 else
1958 {
1959 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1960 wp->w_botline = lnum;
1961 }
1962 }
1963 else
1964 {
1965#ifdef FEAT_VERTSPLIT
1966 draw_vsep_win(wp, row);
1967#endif
1968 if (eof) /* we hit the end of the file */
1969 {
1970 wp->w_botline = buf->b_ml.ml_line_count + 1;
1971#ifdef FEAT_DIFF
1972 j = diff_check_fill(wp, wp->w_botline);
1973 if (j > 0 && !wp->w_botfill)
1974 {
1975 /*
1976 * Display filler lines at the end of the file
1977 */
1978 if (char2cells(fill_diff) > 1)
1979 i = '-';
1980 else
1981 i = fill_diff;
1982 if (row + j > wp->w_height)
1983 j = wp->w_height - row;
1984 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1985 row += j;
1986 }
1987#endif
1988 }
1989 else if (dollar_vcol == 0)
1990 wp->w_botline = lnum;
1991
1992 /* make sure the rest of the screen is blank */
1993 /* put '~'s on rows that aren't part of the file. */
1994 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1995 }
1996
1997 /* Reset the type of redrawing required, the window has been updated. */
1998 wp->w_redr_type = 0;
1999#ifdef FEAT_DIFF
2000 wp->w_old_topfill = wp->w_topfill;
2001 wp->w_old_botfill = wp->w_botfill;
2002#endif
2003
2004 if (dollar_vcol == 0)
2005 {
2006 /*
2007 * There is a trick with w_botline. If we invalidate it on each
2008 * change that might modify it, this will cause a lot of expensive
2009 * calls to plines() in update_topline() each time. Therefore the
2010 * value of w_botline is often approximated, and this value is used to
2011 * compute the value of w_topline. If the value of w_botline was
2012 * wrong, check that the value of w_topline is correct (cursor is on
2013 * the visible part of the text). If it's not, we need to redraw
2014 * again. Mostly this just means scrolling up a few lines, so it
2015 * doesn't look too bad. Only do this for the current window (where
2016 * changes are relevant).
2017 */
2018 wp->w_valid |= VALID_BOTLINE;
2019 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2020 {
2021 recursive = TRUE;
2022 curwin->w_valid &= ~VALID_TOPLINE;
2023 update_topline(); /* may invalidate w_botline again */
2024 if (must_redraw != 0)
2025 {
2026 /* Don't update for changes in buffer again. */
2027 i = curbuf->b_mod_set;
2028 curbuf->b_mod_set = FALSE;
2029 win_update(curwin);
2030 must_redraw = 0;
2031 curbuf->b_mod_set = i;
2032 }
2033 recursive = FALSE;
2034 }
2035 }
2036
2037#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2038 /* restore got_int, unless CTRL-C was hit while redrawing */
2039 if (!got_int)
2040 got_int = save_got_int;
2041#endif
2042}
2043
2044#ifdef FEAT_SIGNS
2045static int draw_signcolumn __ARGS((win_T *wp));
2046
2047/*
2048 * Return TRUE when window "wp" has a column to draw signs in.
2049 */
2050 static int
2051draw_signcolumn(wp)
2052 win_T *wp;
2053{
2054 return (wp->w_buffer->b_signlist != NULL
2055# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002056 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057# endif
2058 );
2059}
2060#endif
2061
2062/*
2063 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2064 * as the filler character.
2065 */
2066 static void
2067win_draw_end(wp, c1, c2, row, endrow, hl)
2068 win_T *wp;
2069 int c1;
2070 int c2;
2071 int row;
2072 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002073 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2076 int n = 0;
2077# define FDC_OFF n
2078#else
2079# define FDC_OFF 0
2080#endif
2081
2082#ifdef FEAT_RIGHTLEFT
2083 if (wp->w_p_rl)
2084 {
2085 /* No check for cmdline window: should never be right-left. */
2086# ifdef FEAT_FOLDING
2087 n = wp->w_p_fdc;
2088
2089 if (n > 0)
2090 {
2091 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002092 if (n > W_WIDTH(wp))
2093 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2095 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2096 ' ', ' ', hl_attr(HLF_FC));
2097 }
2098# endif
2099# ifdef FEAT_SIGNS
2100 if (draw_signcolumn(wp))
2101 {
2102 int nn = n + 2;
2103
2104 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002105 if (nn > W_WIDTH(wp))
2106 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2108 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2109 ' ', ' ', hl_attr(HLF_SC));
2110 n = nn;
2111 }
2112# endif
2113 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2114 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2115 c2, c2, hl_attr(hl));
2116 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2117 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2118 c1, c2, hl_attr(hl));
2119 }
2120 else
2121#endif
2122 {
2123#ifdef FEAT_CMDWIN
2124 if (cmdwin_type != 0 && wp == curwin)
2125 {
2126 /* draw the cmdline character in the leftmost column */
2127 n = 1;
2128 if (n > wp->w_width)
2129 n = wp->w_width;
2130 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2131 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2132 cmdwin_type, ' ', hl_attr(HLF_AT));
2133 }
2134#endif
2135#ifdef FEAT_FOLDING
2136 if (wp->w_p_fdc > 0)
2137 {
2138 int nn = n + wp->w_p_fdc;
2139
2140 /* draw the fold column at the left */
2141 if (nn > W_WIDTH(wp))
2142 nn = W_WIDTH(wp);
2143 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2144 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2145 ' ', ' ', hl_attr(HLF_FC));
2146 n = nn;
2147 }
2148#endif
2149#ifdef FEAT_SIGNS
2150 if (draw_signcolumn(wp))
2151 {
2152 int nn = n + 2;
2153
2154 /* draw the sign column after the fold column */
2155 if (nn > W_WIDTH(wp))
2156 nn = W_WIDTH(wp);
2157 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2158 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2159 ' ', ' ', hl_attr(HLF_SC));
2160 n = nn;
2161 }
2162#endif
2163 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2164 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2165 c1, c2, hl_attr(hl));
2166 }
2167 set_empty_rows(wp, row);
2168}
2169
Bram Moolenaar1a384422010-07-14 19:53:30 +02002170#ifdef FEAT_SYN_HL
2171static int advance_color_col __ARGS((int vcol, int **color_cols));
2172
2173/*
2174 * Advance **color_cols and return TRUE when there are columns to draw.
2175 */
2176 static int
2177advance_color_col(vcol, color_cols)
2178 int vcol;
2179 int **color_cols;
2180{
2181 while (**color_cols >= 0 && vcol > **color_cols)
2182 ++*color_cols;
2183 return (**color_cols >= 0);
2184}
2185#endif
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#ifdef FEAT_FOLDING
2188/*
2189 * Display one folded line.
2190 */
2191 static void
2192fold_line(wp, fold_count, foldinfo, lnum, row)
2193 win_T *wp;
2194 long fold_count;
2195 foldinfo_T *foldinfo;
2196 linenr_T lnum;
2197 int row;
2198{
2199 char_u buf[51];
2200 pos_T *top, *bot;
2201 linenr_T lnume = lnum + fold_count - 1;
2202 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002203 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 int col;
2206 int txtcol;
2207 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002208 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210 /* Build the fold line:
2211 * 1. Add the cmdwin_type for the command-line window
2212 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002213 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 * 4. Compose the text
2215 * 5. Add the text
2216 * 6. set highlighting for the Visual area an other text
2217 */
2218 col = 0;
2219
2220 /*
2221 * 1. Add the cmdwin_type for the command-line window
2222 * Ignores 'rightleft', this window is never right-left.
2223 */
2224#ifdef FEAT_CMDWIN
2225 if (cmdwin_type != 0 && wp == curwin)
2226 {
2227 ScreenLines[off] = cmdwin_type;
2228 ScreenAttrs[off] = hl_attr(HLF_AT);
2229#ifdef FEAT_MBYTE
2230 if (enc_utf8)
2231 ScreenLinesUC[off] = 0;
2232#endif
2233 ++col;
2234 }
2235#endif
2236
2237 /*
2238 * 2. Add the 'foldcolumn'
2239 */
2240 fdc = wp->w_p_fdc;
2241 if (fdc > W_WIDTH(wp) - col)
2242 fdc = W_WIDTH(wp) - col;
2243 if (fdc > 0)
2244 {
2245 fill_foldcolumn(buf, wp, TRUE, lnum);
2246#ifdef FEAT_RIGHTLEFT
2247 if (wp->w_p_rl)
2248 {
2249 int i;
2250
2251 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2252 hl_attr(HLF_FC));
2253 /* reverse the fold column */
2254 for (i = 0; i < fdc; ++i)
2255 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2256 }
2257 else
2258#endif
2259 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2260 col += fdc;
2261 }
2262
2263#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002264# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2265 for (ri = 0; ri < l; ++ri) \
2266 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2267 else \
2268 for (ri = 0; ri < l; ++ri) \
2269 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002271# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2272 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273#endif
2274
Bram Moolenaar64486672010-05-16 15:46:46 +02002275 /* Set all attributes of the 'number' or 'relativenumber' column and the
2276 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002277 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
2279#ifdef FEAT_SIGNS
2280 /* If signs are being displayed, add two spaces. */
2281 if (draw_signcolumn(wp))
2282 {
2283 len = W_WIDTH(wp) - col;
2284 if (len > 0)
2285 {
2286 if (len > 2)
2287 len = 2;
2288# ifdef FEAT_RIGHTLEFT
2289 if (wp->w_p_rl)
2290 /* the line number isn't reversed */
2291 copy_text_attr(off + W_WIDTH(wp) - len - col,
2292 (char_u *)" ", len, hl_attr(HLF_FL));
2293 else
2294# endif
2295 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2296 col += len;
2297 }
2298 }
2299#endif
2300
2301 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002302 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002304 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 len = W_WIDTH(wp) - col;
2307 if (len > 0)
2308 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002309 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002310 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002311
2312 if (len > w + 1)
2313 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002314
2315 if (wp->w_p_nu)
2316 /* 'number' */
2317 num = (long)lnum;
2318 else
2319 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002320 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02002321
2322 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#ifdef FEAT_RIGHTLEFT
2324 if (wp->w_p_rl)
2325 /* the line number isn't reversed */
2326 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2327 hl_attr(HLF_FL));
2328 else
2329#endif
2330 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2331 col += len;
2332 }
2333 }
2334
2335 /*
2336 * 4. Compose the folded-line string with 'foldtext', if set.
2337 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002338 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
2340 txtcol = col; /* remember where text starts */
2341
2342 /*
2343 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2344 * Right-left text is put in columns 0 - number-col, normal text is put
2345 * in columns number-col - window-width.
2346 */
2347#ifdef FEAT_MBYTE
2348 if (has_mbyte)
2349 {
2350 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002351 int u8c, u8cc[MAX_MCO];
2352 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 int idx;
2354 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002355 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356# ifdef FEAT_ARABIC
2357 int prev_c = 0; /* previous Arabic character */
2358 int prev_c1 = 0; /* first composing char for prev_c */
2359# endif
2360
2361# ifdef FEAT_RIGHTLEFT
2362 if (wp->w_p_rl)
2363 idx = off;
2364 else
2365# endif
2366 idx = off + col;
2367
2368 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2369 for (p = text; *p != NUL; )
2370 {
2371 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002372 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (col + cells > W_WIDTH(wp)
2374# ifdef FEAT_RIGHTLEFT
2375 - (wp->w_p_rl ? col : 0)
2376# endif
2377 )
2378 break;
2379 ScreenLines[idx] = *p;
2380 if (enc_utf8)
2381 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002382 u8c = utfc_ptr2char(p, u8cc);
2383 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
2385 ScreenLinesUC[idx] = 0;
2386#ifdef FEAT_ARABIC
2387 prev_c = u8c;
2388#endif
2389 }
2390 else
2391 {
2392#ifdef FEAT_ARABIC
2393 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2394 {
2395 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002396 int pc, pc1, nc;
2397 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 int firstbyte = *p;
2399
2400 /* The idea of what is the previous and next
2401 * character depends on 'rightleft'. */
2402 if (wp->w_p_rl)
2403 {
2404 pc = prev_c;
2405 pc1 = prev_c1;
2406 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002407 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 else
2410 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002411 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002413 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415 prev_c = u8c;
2416
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002417 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 pc, pc1, nc);
2419 ScreenLines[idx] = firstbyte;
2420 }
2421 else
2422 prev_c = u8c;
2423#endif
2424 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002425#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (u8c >= 0x10000)
2427 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2428 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002431 for (i = 0; i < Screen_mco; ++i)
2432 {
2433 ScreenLinesC[i][idx] = u8cc[i];
2434 if (u8cc[i] == 0)
2435 break;
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
2438 if (cells > 1)
2439 ScreenLines[idx + 1] = 0;
2440 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002441 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2442 /* double-byte single width character */
2443 ScreenLines2[idx] = p[1];
2444 else if (cells > 1)
2445 /* double-width character */
2446 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 col += cells;
2448 idx += cells;
2449 p += c_len;
2450 }
2451 }
2452 else
2453#endif
2454 {
2455 len = (int)STRLEN(text);
2456 if (len > W_WIDTH(wp) - col)
2457 len = W_WIDTH(wp) - col;
2458 if (len > 0)
2459 {
2460#ifdef FEAT_RIGHTLEFT
2461 if (wp->w_p_rl)
2462 STRNCPY(current_ScreenLine, text, len);
2463 else
2464#endif
2465 STRNCPY(current_ScreenLine + col, text, len);
2466 col += len;
2467 }
2468 }
2469
2470 /* Fill the rest of the line with the fold filler */
2471#ifdef FEAT_RIGHTLEFT
2472 if (wp->w_p_rl)
2473 col -= txtcol;
2474#endif
2475 while (col < W_WIDTH(wp)
2476#ifdef FEAT_RIGHTLEFT
2477 - (wp->w_p_rl ? txtcol : 0)
2478#endif
2479 )
2480 {
2481#ifdef FEAT_MBYTE
2482 if (enc_utf8)
2483 {
2484 if (fill_fold >= 0x80)
2485 {
2486 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002487 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489 else
2490 ScreenLinesUC[off + col] = 0;
2491 }
2492#endif
2493 ScreenLines[off + col++] = fill_fold;
2494 }
2495
2496 if (text != buf)
2497 vim_free(text);
2498
2499 /*
2500 * 6. set highlighting for the Visual area an other text.
2501 * If all folded lines are in the Visual area, highlight the line.
2502 */
2503#ifdef FEAT_VISUAL
2504 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2505 {
2506 if (ltoreq(curwin->w_cursor, VIsual))
2507 {
2508 /* Visual is after curwin->w_cursor */
2509 top = &curwin->w_cursor;
2510 bot = &VIsual;
2511 }
2512 else
2513 {
2514 /* Visual is before curwin->w_cursor */
2515 top = &VIsual;
2516 bot = &curwin->w_cursor;
2517 }
2518 if (lnum >= top->lnum
2519 && lnume <= bot->lnum
2520 && (VIsual_mode != 'v'
2521 || ((lnum > top->lnum
2522 || (lnum == top->lnum
2523 && top->col == 0))
2524 && (lnume < bot->lnum
2525 || (lnume == bot->lnum
2526 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002527 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
2529 if (VIsual_mode == Ctrl_V)
2530 {
2531 /* Visual block mode: highlight the chars part of the block */
2532 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2533 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002534 if (wp->w_old_cursor_lcol != MAXCOL
2535 && wp->w_old_cursor_lcol + txtcol
2536 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 len = wp->w_old_cursor_lcol;
2538 else
2539 len = W_WIDTH(wp) - txtcol;
2540 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002541 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543 }
2544 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002547 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 }
2550 }
2551#endif
2552
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002553#ifdef FEAT_SYN_HL
2554 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002555 if (wp->w_p_cuc)
2556 {
2557 txtcol += wp->w_virtcol;
2558 if (wp->w_p_wrap)
2559 txtcol -= wp->w_skipcol;
2560 else
2561 txtcol -= wp->w_leftcol;
2562 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2563 ScreenAttrs[off + txtcol] = hl_combine_attr(
2564 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2565 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
2568 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2569 (int)W_WIDTH(wp), FALSE);
2570
2571 /*
2572 * Update w_cline_height and w_cline_folded if the cursor line was
2573 * updated (saves a call to plines() later).
2574 */
2575 if (wp == curwin
2576 && lnum <= curwin->w_cursor.lnum
2577 && lnume >= curwin->w_cursor.lnum)
2578 {
2579 curwin->w_cline_row = row;
2580 curwin->w_cline_height = 1;
2581 curwin->w_cline_folded = TRUE;
2582 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2583 }
2584}
2585
2586/*
2587 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2588 */
2589 static void
2590copy_text_attr(off, buf, len, attr)
2591 int off;
2592 char_u *buf;
2593 int len;
2594 int attr;
2595{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002596 int i;
2597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 mch_memmove(ScreenLines + off, buf, (size_t)len);
2599# ifdef FEAT_MBYTE
2600 if (enc_utf8)
2601 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2602# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002603 for (i = 0; i < len; ++i)
2604 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605}
2606
2607/*
2608 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002609 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
2611 static void
2612fill_foldcolumn(p, wp, closed, lnum)
2613 char_u *p;
2614 win_T *wp;
2615 int closed; /* TRUE of FALSE */
2616 linenr_T lnum; /* current line number */
2617{
2618 int i = 0;
2619 int level;
2620 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002621 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
2623 /* Init to all spaces. */
2624 copy_spaces(p, (size_t)wp->w_p_fdc);
2625
2626 level = win_foldinfo.fi_level;
2627 if (level > 0)
2628 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002629 /* If there is only one column put more info in it. */
2630 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 /* If the column is too narrow, we start at the lowest level that
2633 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002634 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (first_level < 1)
2636 first_level = 1;
2637
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002638 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 {
2640 if (win_foldinfo.fi_lnum == lnum
2641 && first_level + i >= win_foldinfo.fi_low_level)
2642 p[i] = '-';
2643 else if (first_level == 1)
2644 p[i] = '|';
2645 else if (first_level + i <= 9)
2646 p[i] = '0' + first_level + i;
2647 else
2648 p[i] = '>';
2649 if (first_level + i == level)
2650 break;
2651 }
2652 }
2653 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002654 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655}
2656#endif /* FEAT_FOLDING */
2657
2658/*
2659 * Display line "lnum" of window 'wp' on the screen.
2660 * Start at row "startrow", stop when "endrow" is reached.
2661 * wp->w_virtcol needs to be valid.
2662 *
2663 * Return the number of last row the line occupies.
2664 */
2665 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002666win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 win_T *wp;
2668 linenr_T lnum;
2669 int startrow;
2670 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002671 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
2673 int col; /* visual column on screen */
2674 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2675 int c = 0; /* init for GCC */
2676 long vcol = 0; /* virtual column (for tabs) */
2677 long vcol_prev = -1; /* "vcol" of previous character */
2678 char_u *line; /* current line */
2679 char_u *ptr; /* current position in "line" */
2680 int row; /* row in the window, excl w_winrow */
2681 int screen_row; /* row on the screen, incl w_winrow */
2682
2683 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2684 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002685 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 int c_extra = NUL; /* extra chars, all the same */
2687 int extra_attr = 0; /* attributes when n_extra != 0 */
2688 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2689 displaying lcs_eol at end-of-line */
2690 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2691 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2692
2693 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2694 int saved_n_extra = 0;
2695 char_u *saved_p_extra = NULL;
2696 int saved_c_extra = 0;
2697 int saved_char_attr = 0;
2698
2699 int n_attr = 0; /* chars with special attr */
2700 int saved_attr2 = 0; /* char_attr saved for n_attr */
2701 int n_attr3 = 0; /* chars with overruling special attr */
2702 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2703
2704 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2705
2706 int fromcol, tocol; /* start/end of inverting */
2707 int fromcol_prev = -2; /* start of inverting after cursor */
2708 int noinvcur = FALSE; /* don't invert the cursor */
2709#ifdef FEAT_VISUAL
2710 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002711 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712#endif
2713 pos_T pos;
2714 long v;
2715
2716 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002717 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2719 in this line */
2720 int attr = 0; /* attributes for area highlighting */
2721 int area_attr = 0; /* attributes desired by highlighting */
2722 int search_attr = 0; /* attributes desired by 'hlsearch' */
2723#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002724 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 int syntax_attr = 0; /* attributes desired by syntax */
2726 int has_syntax = FALSE; /* this buffer has syntax highl. */
2727 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002728 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002729 int draw_color_col = FALSE; /* highlight colorcolumn */
2730 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002731#endif
2732#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002733 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002734# define SPWORDLEN 150
2735 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002736 int nextlinecol = 0; /* column where nextline[] starts */
2737 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002738 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002739 int spell_attr = 0; /* attributes desired by spelling */
2740 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002741 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2742 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002743 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002744 static int cap_col = -1; /* column to check for Cap word */
2745 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002746 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#endif
2748 int extra_check; /* has syntax or linebreak */
2749#ifdef FEAT_MBYTE
2750 int multi_attr = 0; /* attributes desired by multibyte */
2751 int mb_l = 1; /* multi-byte byte length */
2752 int mb_c = 0; /* decoded multi-byte character */
2753 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002754 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755#endif
2756#ifdef FEAT_DIFF
2757 int filler_lines; /* nr of filler lines to be drawn */
2758 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002759 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 int change_start = MAXCOL; /* first col of changed area */
2761 int change_end = -1; /* last col of changed area */
2762#endif
2763 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2764#ifdef FEAT_LINEBREAK
2765 int need_showbreak = FALSE;
2766#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002767#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2768 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002770 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771#endif
2772#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002773 matchitem_T *cur; /* points to the match list */
2774 match_T *shl; /* points to search_hl or a match */
2775 int shl_flag; /* flag to indicate whether search_hl
2776 has been processed or not */
2777 int prevcol_hl_flag; /* flag to indicate whether prevcol
2778 equals startcol of search_hl or one
2779 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780#endif
2781#ifdef FEAT_ARABIC
2782 int prev_c = 0; /* previous Arabic character */
2783 int prev_c1 = 0; /* first composing char for prev_c */
2784#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002785#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002786 int did_line_attr = 0;
2787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 /* draw_state: items that are drawn in sequence: */
2790#define WL_START 0 /* nothing done yet */
2791#ifdef FEAT_CMDWIN
2792# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2793#else
2794# define WL_CMDLINE WL_START
2795#endif
2796#ifdef FEAT_FOLDING
2797# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2798#else
2799# define WL_FOLD WL_CMDLINE
2800#endif
2801#ifdef FEAT_SIGNS
2802# define WL_SIGN WL_FOLD + 1 /* column for signs */
2803#else
2804# define WL_SIGN WL_FOLD /* column for signs */
2805#endif
2806#define WL_NR WL_SIGN + 1 /* line number */
2807#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2808# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2809#else
2810# define WL_SBR WL_NR
2811#endif
2812#define WL_LINE WL_SBR + 1 /* text in the line */
2813 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002814#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 int feedback_col = 0;
2816 int feedback_old_attr = -1;
2817#endif
2818
Bram Moolenaar860cae12010-06-05 23:22:07 +02002819#ifdef FEAT_CONCEAL
2820 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002821 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002822 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002823 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002824 int is_concealing = FALSE;
2825 int boguscols = 0; /* nonexistent columns added to force
2826 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002827 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002828 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002829# define VCOL_HLC (vcol - vcol_off)
2830#else
2831# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
2834 if (startrow > endrow) /* past the end already! */
2835 return startrow;
2836
2837 row = startrow;
2838 screen_row = row + W_WINROW(wp);
2839
2840 /*
2841 * To speed up the loop below, set extra_check when there is linebreak,
2842 * trailing white space and/or syntax processing to be done.
2843 */
2844#ifdef FEAT_LINEBREAK
2845 extra_check = wp->w_p_lbr;
2846#else
2847 extra_check = 0;
2848#endif
2849#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002850 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 /* Prepare for syntax highlighting in this line. When there is an
2853 * error, stop syntax highlighting. */
2854 save_did_emsg = did_emsg;
2855 did_emsg = FALSE;
2856 syntax_start(wp, lnum);
2857 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002858 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 else
2860 {
2861 did_emsg = save_did_emsg;
2862 has_syntax = TRUE;
2863 extra_check = TRUE;
2864 }
2865 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002866
2867 /* Check for columns to display for 'colorcolumn'. */
2868 color_cols = wp->w_p_cc_cols;
2869 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002870 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002871#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002872
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002873#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002874 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002875 && *wp->w_s->b_p_spl != NUL
2876 && wp->w_s->b_langp.ga_len > 0
2877 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002878 {
2879 /* Prepare for spell checking. */
2880 has_spell = TRUE;
2881 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002882
2883 /* Get the start of the next line, so that words that wrap to the next
2884 * line are found too: "et<line-break>al.".
2885 * Trick: skip a few chars for C/shell/Vim comments */
2886 nextline[SPWORDLEN] = NUL;
2887 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2888 {
2889 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2890 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2891 }
2892
2893 /* When a word wrapped from the previous line the start of the current
2894 * line is valid. */
2895 if (lnum == checked_lnum)
2896 cur_checked_col = checked_col;
2897 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002898
2899 /* When there was a sentence end in the previous line may require a
2900 * word starting with capital in this line. In line 1 always check
2901 * the first word. */
2902 if (lnum != capcol_lnum)
2903 cap_col = -1;
2904 if (lnum == 1)
2905 cap_col = 0;
2906 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908#endif
2909
2910 /*
2911 * handle visual active in this window
2912 */
2913 fromcol = -10;
2914 tocol = MAXCOL;
2915#ifdef FEAT_VISUAL
2916 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2917 {
2918 /* Visual is after curwin->w_cursor */
2919 if (ltoreq(curwin->w_cursor, VIsual))
2920 {
2921 top = &curwin->w_cursor;
2922 bot = &VIsual;
2923 }
2924 else /* Visual is before curwin->w_cursor */
2925 {
2926 top = &VIsual;
2927 bot = &curwin->w_cursor;
2928 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002929 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (VIsual_mode == Ctrl_V) /* block mode */
2931 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002932 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 {
2934 fromcol = wp->w_old_cursor_fcol;
2935 tocol = wp->w_old_cursor_lcol;
2936 }
2937 }
2938 else /* non-block mode */
2939 {
2940 if (lnum > top->lnum && lnum <= bot->lnum)
2941 fromcol = 0;
2942 else if (lnum == top->lnum)
2943 {
2944 if (VIsual_mode == 'V') /* linewise */
2945 fromcol = 0;
2946 else
2947 {
2948 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2949 if (gchar_pos(top) == NUL)
2950 tocol = fromcol + 1;
2951 }
2952 }
2953 if (VIsual_mode != 'V' && lnum == bot->lnum)
2954 {
2955 if (*p_sel == 'e' && bot->col == 0
2956#ifdef FEAT_VIRTUALEDIT
2957 && bot->coladd == 0
2958#endif
2959 )
2960 {
2961 fromcol = -10;
2962 tocol = MAXCOL;
2963 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002964 else if (bot->col == MAXCOL)
2965 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 else
2967 {
2968 pos = *bot;
2969 if (*p_sel == 'e')
2970 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2971 else
2972 {
2973 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2974 ++tocol;
2975 }
2976 }
2977 }
2978 }
2979
2980#ifndef MSDOS
2981 /* Check if the character under the cursor should not be inverted */
2982 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2983# ifdef FEAT_GUI
2984 && !gui.in_use
2985# endif
2986 )
2987 noinvcur = TRUE;
2988#endif
2989
2990 /* if inverting in this line set area_highlighting */
2991 if (fromcol >= 0)
2992 {
2993 area_highlighting = TRUE;
2994 attr = hl_attr(HLF_V);
2995#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2996 if (clip_star.available && !clip_star.owned && clip_isautosel())
2997 attr = hl_attr(HLF_VNC);
2998#endif
2999 }
3000 }
3001
3002 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003003 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 */
3005 else
3006#endif /* FEAT_VISUAL */
3007 if (highlight_match
3008 && wp == curwin
3009 && lnum >= curwin->w_cursor.lnum
3010 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3011 {
3012 if (lnum == curwin->w_cursor.lnum)
3013 getvcol(curwin, &(curwin->w_cursor),
3014 (colnr_T *)&fromcol, NULL, NULL);
3015 else
3016 fromcol = 0;
3017 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3018 {
3019 pos.lnum = lnum;
3020 pos.col = search_match_endcol;
3021 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3022 }
3023 else
3024 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003025 /* do at least one character; happens when past end of line */
3026 if (fromcol == tocol)
3027 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 area_highlighting = TRUE;
3029 attr = hl_attr(HLF_I);
3030 }
3031
3032#ifdef FEAT_DIFF
3033 filler_lines = diff_check(wp, lnum);
3034 if (filler_lines < 0)
3035 {
3036 if (filler_lines == -1)
3037 {
3038 if (diff_find_change(wp, lnum, &change_start, &change_end))
3039 diff_hlf = HLF_ADD; /* added line */
3040 else if (change_start == 0)
3041 diff_hlf = HLF_TXD; /* changed text */
3042 else
3043 diff_hlf = HLF_CHD; /* changed line */
3044 }
3045 else
3046 diff_hlf = HLF_ADD; /* added line */
3047 filler_lines = 0;
3048 area_highlighting = TRUE;
3049 }
3050 if (lnum == wp->w_topline)
3051 filler_lines = wp->w_topfill;
3052 filler_todo = filler_lines;
3053#endif
3054
3055#ifdef LINE_ATTR
3056# ifdef FEAT_SIGNS
3057 /* If this line has a sign with line highlighting set line_attr. */
3058 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3059 if (v != 0)
3060 line_attr = sign_get_attr((int)v, TRUE);
3061# endif
3062# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3063 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003064 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 line_attr = hl_attr(HLF_L);
3066# endif
3067 if (line_attr != 0)
3068 area_highlighting = TRUE;
3069#endif
3070
3071 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3072 ptr = line;
3073
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003074#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003075 if (has_spell)
3076 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003077 /* For checking first word with a capital skip white space. */
3078 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003079 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003080
Bram Moolenaar30abd282005-06-22 22:35:10 +00003081 /* To be able to spell-check over line boundaries copy the end of the
3082 * current line into nextline[]. Above the start of the next line was
3083 * copied to nextline[SPWORDLEN]. */
3084 if (nextline[SPWORDLEN] == NUL)
3085 {
3086 /* No next line or it is empty. */
3087 nextlinecol = MAXCOL;
3088 nextline_idx = 0;
3089 }
3090 else
3091 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003092 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003093 if (v < SPWORDLEN)
3094 {
3095 /* Short line, use it completely and append the start of the
3096 * next line. */
3097 nextlinecol = 0;
3098 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003099 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003100 nextline_idx = v + 1;
3101 }
3102 else
3103 {
3104 /* Long line, use only the last SPWORDLEN bytes. */
3105 nextlinecol = v - SPWORDLEN;
3106 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3107 nextline_idx = SPWORDLEN + 1;
3108 }
3109 }
3110 }
3111#endif
3112
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 /* find start of trailing whitespace */
3114 if (wp->w_p_list && lcs_trail)
3115 {
3116 trailcol = (colnr_T)STRLEN(ptr);
3117 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3118 --trailcol;
3119 trailcol += (colnr_T) (ptr - line);
3120 extra_check = TRUE;
3121 }
3122
3123 /*
3124 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3125 * first character to be displayed.
3126 */
3127 if (wp->w_p_wrap)
3128 v = wp->w_skipcol;
3129 else
3130 v = wp->w_leftcol;
3131 if (v > 0)
3132 {
3133#ifdef FEAT_MBYTE
3134 char_u *prev_ptr = ptr;
3135#endif
3136 while (vcol < v && *ptr != NUL)
3137 {
3138 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3139 vcol += c;
3140#ifdef FEAT_MBYTE
3141 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003143 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 }
3145
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003146#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3147 /* When:
3148 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003149 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003150 * - 'virtualedit' is set, or
3151 * - the visual mode is active,
3152 * the end of the line may be before the start of the displayed part.
3153 */
3154 if (vcol < v && (
3155# ifdef FEAT_SYN_HL
3156 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003157 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003158# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3159 ||
3160# endif
3161# endif
3162# ifdef FEAT_VIRTUALEDIT
3163 virtual_active()
3164# ifdef FEAT_VISUAL
3165 ||
3166# endif
3167# endif
3168# ifdef FEAT_VISUAL
3169 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3170# endif
3171 ))
3172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175#endif
3176
3177 /* Handle a character that's not completely on the screen: Put ptr at
3178 * that character but skip the first few screen characters. */
3179 if (vcol > v)
3180 {
3181 vcol -= c;
3182#ifdef FEAT_MBYTE
3183 ptr = prev_ptr;
3184#else
3185 --ptr;
3186#endif
3187 n_skip = v - vcol;
3188 }
3189
3190 /*
3191 * Adjust for when the inverted text is before the screen,
3192 * and when the start of the inverted text is before the screen.
3193 */
3194 if (tocol <= vcol)
3195 fromcol = 0;
3196 else if (fromcol >= 0 && fromcol < vcol)
3197 fromcol = vcol;
3198
3199#ifdef FEAT_LINEBREAK
3200 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3201 if (wp->w_p_wrap)
3202 need_showbreak = TRUE;
3203#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003204#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003205 /* When spell checking a word we need to figure out the start of the
3206 * word and if it's badly spelled or not. */
3207 if (has_spell)
3208 {
3209 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003210 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003211 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003212
3213 pos = wp->w_cursor;
3214 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003215 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003216 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003217
3218 /* spell_move_to() may call ml_get() and make "line" invalid */
3219 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3220 ptr = line + linecol;
3221
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003222 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003223 {
3224 /* no bad word found at line start, don't check until end of a
3225 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003226 spell_hlf = HLF_COUNT;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003227 word_end = (int)(spell_to_word_end(ptr, wp)
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003228 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003229 }
3230 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003231 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003232 /* bad word found, use attributes until end of word */
3233 word_end = wp->w_cursor.col + len + 1;
3234
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003235 /* Turn index into actual attributes. */
3236 if (spell_hlf != HLF_COUNT)
3237 spell_attr = highlight_attr[spell_hlf];
3238 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003239 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003240
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003241# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003242 /* Need to restart syntax highlighting for this line. */
3243 if (has_syntax)
3244 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003245# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003246 }
3247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249
3250 /*
3251 * Correct highlighting for cursor that can't be disabled.
3252 * Avoids having to check this for each character.
3253 */
3254 if (fromcol >= 0)
3255 {
3256 if (noinvcur)
3257 {
3258 if ((colnr_T)fromcol == wp->w_virtcol)
3259 {
3260 /* highlighting starts at cursor, let it start just after the
3261 * cursor */
3262 fromcol_prev = fromcol;
3263 fromcol = -1;
3264 }
3265 else if ((colnr_T)fromcol < wp->w_virtcol)
3266 /* restart highlighting after the cursor */
3267 fromcol_prev = wp->w_virtcol;
3268 }
3269 if (fromcol >= tocol)
3270 fromcol = -1;
3271 }
3272
3273#ifdef FEAT_SEARCH_EXTRA
3274 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003275 * Handle highlighting the last used search pattern and matches.
3276 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003278 cur = wp->w_match_head;
3279 shl_flag = FALSE;
3280 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003282 if (shl_flag == FALSE)
3283 {
3284 shl = &search_hl;
3285 shl_flag = TRUE;
3286 }
3287 else
3288 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003289 shl->startcol = MAXCOL;
3290 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 shl->attr_cur = 0;
3292 if (shl->rm.regprog != NULL)
3293 {
3294 v = (long)(ptr - line);
3295 next_search_hl(wp, shl, lnum, (colnr_T)v);
3296
3297 /* Need to get the line again, a multi-line regexp may have made it
3298 * invalid. */
3299 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3300 ptr = line + v;
3301
3302 if (shl->lnum != 0 && shl->lnum <= lnum)
3303 {
3304 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003305 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003307 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3309 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003310 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003312 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003314 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
3316#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003317 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003318 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 else
3320#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003321 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003323 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 shl->attr_cur = shl->attr;
3326 search_attr = shl->attr;
3327 }
3328 area_highlighting = TRUE;
3329 }
3330 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003331 if (shl != &search_hl && cur != NULL)
3332 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334#endif
3335
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003336#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003337 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3338 * active, because it's not clear what is selected then. */
3339 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003340 {
3341 line_attr = hl_attr(HLF_CUL);
3342 area_highlighting = TRUE;
3343 }
3344#endif
3345
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003346 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 col = 0;
3348#ifdef FEAT_RIGHTLEFT
3349 if (wp->w_p_rl)
3350 {
3351 /* Rightleft window: process the text in the normal direction, but put
3352 * it in current_ScreenLine[] from right to left. Start at the
3353 * rightmost column of the window. */
3354 col = W_WIDTH(wp) - 1;
3355 off += col;
3356 }
3357#endif
3358
3359 /*
3360 * Repeat for the whole displayed line.
3361 */
3362 for (;;)
3363 {
3364 /* Skip this quickly when working on the text. */
3365 if (draw_state != WL_LINE)
3366 {
3367#ifdef FEAT_CMDWIN
3368 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3369 {
3370 draw_state = WL_CMDLINE;
3371 if (cmdwin_type != 0 && wp == curwin)
3372 {
3373 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003375 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 char_attr = hl_attr(HLF_AT);
3377 }
3378 }
3379#endif
3380
3381#ifdef FEAT_FOLDING
3382 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3383 {
3384 draw_state = WL_FOLD;
3385 if (wp->w_p_fdc > 0)
3386 {
3387 /* Draw the 'foldcolumn'. */
3388 fill_foldcolumn(extra, wp, FALSE, lnum);
3389 n_extra = wp->w_p_fdc;
3390 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003391 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 c_extra = NUL;
3393 char_attr = hl_attr(HLF_FC);
3394 }
3395 }
3396#endif
3397
3398#ifdef FEAT_SIGNS
3399 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3400 {
3401 draw_state = WL_SIGN;
3402 /* Show the sign column when there are any signs in this
3403 * buffer or when using Netbeans. */
3404 if (draw_signcolumn(wp)
3405# ifdef FEAT_DIFF
3406 && filler_todo <= 0
3407# endif
3408 )
3409 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003410 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003412 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413# endif
3414
3415 /* Draw two cells with the sign value or blank. */
3416 c_extra = ' ';
3417 char_attr = hl_attr(HLF_SC);
3418 n_extra = 2;
3419
3420 if (row == startrow)
3421 {
3422 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3423 SIGN_TEXT);
3424# ifdef FEAT_SIGN_ICONS
3425 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3426 SIGN_ICON);
3427 if (gui.in_use && icon_sign != 0)
3428 {
3429 /* Use the image in this position. */
3430 c_extra = SIGN_BYTE;
3431# ifdef FEAT_NETBEANS_INTG
3432 if (buf_signcount(wp->w_buffer, lnum) > 1)
3433 c_extra = MULTISIGN_BYTE;
3434# endif
3435 char_attr = icon_sign;
3436 }
3437 else
3438# endif
3439 if (text_sign != 0)
3440 {
3441 p_extra = sign_get_text(text_sign);
3442 if (p_extra != NULL)
3443 {
3444 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003445 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447 char_attr = sign_get_attr(text_sign, FALSE);
3448 }
3449 }
3450 }
3451 }
3452#endif
3453
3454 if (draw_state == WL_NR - 1 && n_extra == 0)
3455 {
3456 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003457 /* Display the absolute or relative line number. After the
3458 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3459 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 && (row == startrow
3461#ifdef FEAT_DIFF
3462 + filler_lines
3463#endif
3464 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3465 {
3466 /* Draw the line number (empty space after wrapping). */
3467 if (row == startrow
3468#ifdef FEAT_DIFF
3469 + filler_lines
3470#endif
3471 )
3472 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003473 long num;
3474
3475 if (wp->w_p_nu)
3476 /* 'number' */
3477 num = (long)lnum;
3478 else
3479 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003480 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02003481
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003482 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003483 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (wp->w_skipcol > 0)
3485 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3486 *p_extra = '-';
3487#ifdef FEAT_RIGHTLEFT
3488 if (wp->w_p_rl) /* reverse line numbers */
3489 rl_mirror(extra);
3490#endif
3491 p_extra = extra;
3492 c_extra = NUL;
3493 }
3494 else
3495 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003496 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003498#ifdef FEAT_SYN_HL
3499 /* When 'cursorline' is set highlight the line number of
3500 * the current line differently. */
3501 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3502 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505 }
3506
3507#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3508 if (draw_state == WL_SBR - 1 && n_extra == 0)
3509 {
3510 draw_state = WL_SBR;
3511# ifdef FEAT_DIFF
3512 if (filler_todo > 0)
3513 {
3514 /* Draw "deleted" diff line(s). */
3515 if (char2cells(fill_diff) > 1)
3516 c_extra = '-';
3517 else
3518 c_extra = fill_diff;
3519# ifdef FEAT_RIGHTLEFT
3520 if (wp->w_p_rl)
3521 n_extra = col + 1;
3522 else
3523# endif
3524 n_extra = W_WIDTH(wp) - col;
3525 char_attr = hl_attr(HLF_DED);
3526 }
3527# endif
3528# ifdef FEAT_LINEBREAK
3529 if (*p_sbr != NUL && need_showbreak)
3530 {
3531 /* Draw 'showbreak' at the start of each broken line. */
3532 p_extra = p_sbr;
3533 c_extra = NUL;
3534 n_extra = (int)STRLEN(p_sbr);
3535 char_attr = hl_attr(HLF_AT);
3536 need_showbreak = FALSE;
3537 /* Correct end of highlighted area for 'showbreak',
3538 * required when 'linebreak' is also set. */
3539 if (tocol == vcol)
3540 tocol += n_extra;
3541 }
3542# endif
3543 }
3544#endif
3545
3546 if (draw_state == WL_LINE - 1 && n_extra == 0)
3547 {
3548 draw_state = WL_LINE;
3549 if (saved_n_extra)
3550 {
3551 /* Continue item from end of wrapped line. */
3552 n_extra = saved_n_extra;
3553 c_extra = saved_c_extra;
3554 p_extra = saved_p_extra;
3555 char_attr = saved_char_attr;
3556 }
3557 else
3558 char_attr = 0;
3559 }
3560 }
3561
3562 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003563 if (dollar_vcol != 0 && wp == curwin
3564 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565#ifdef FEAT_DIFF
3566 && filler_todo <= 0
3567#endif
3568 )
3569 {
3570 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3571 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003572 /* Pretend we have finished updating the window. Except when
3573 * 'cursorcolumn' is set. */
3574#ifdef FEAT_SYN_HL
3575 if (wp->w_p_cuc)
3576 row = wp->w_cline_row + wp->w_cline_height;
3577 else
3578#endif
3579 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 break;
3581 }
3582
3583 if (draw_state == WL_LINE && area_highlighting)
3584 {
3585 /* handle Visual or match highlighting in this line */
3586 if (vcol == fromcol
3587#ifdef FEAT_MBYTE
3588 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3589 && (*mb_ptr2cells)(ptr) > 1)
3590#endif
3591 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003592 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 && vcol < tocol))
3594 area_attr = attr; /* start highlighting */
3595 else if (area_attr != 0
3596 && (vcol == tocol
3597 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
3600#ifdef FEAT_SEARCH_EXTRA
3601 if (!n_extra)
3602 {
3603 /*
3604 * Check for start/end of search pattern match.
3605 * After end, check for start/end of next match.
3606 * When another match, have to check for start again.
3607 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003608 * Do this for 'search_hl' and the match list (ordered by
3609 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003611 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003612 cur = wp->w_match_head;
3613 shl_flag = FALSE;
3614 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003616 if (shl_flag == FALSE
3617 && ((cur != NULL
3618 && cur->priority > SEARCH_HL_PRIORITY)
3619 || cur == NULL))
3620 {
3621 shl = &search_hl;
3622 shl_flag = TRUE;
3623 }
3624 else
3625 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 while (shl->rm.regprog != NULL)
3627 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003628 if (shl->startcol != MAXCOL
3629 && v >= (long)shl->startcol
3630 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
3632 shl->attr_cur = shl->attr;
3633 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003634 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
3636 shl->attr_cur = 0;
3637
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 next_search_hl(wp, shl, lnum, (colnr_T)v);
3639
3640 /* Need to get the line again, a multi-line regexp
3641 * may have made it invalid. */
3642 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3643 ptr = line + v;
3644
3645 if (shl->lnum == lnum)
3646 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003647 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003649 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003651 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003653 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
3655 /* highlight empty match, try again after
3656 * it */
3657#ifdef FEAT_MBYTE
3658 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003659 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003660 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 else
3662#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003663 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665
3666 /* Loop to check if the match starts at the
3667 * current position */
3668 continue;
3669 }
3670 }
3671 break;
3672 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003673 if (shl != &search_hl && cur != NULL)
3674 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003676
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003677 /* Use attributes from match with highest priority among
3678 * 'search_hl' and the match list. */
3679 search_attr = search_hl.attr_cur;
3680 cur = wp->w_match_head;
3681 shl_flag = FALSE;
3682 while (cur != NULL || shl_flag == FALSE)
3683 {
3684 if (shl_flag == FALSE
3685 && ((cur != NULL
3686 && cur->priority > SEARCH_HL_PRIORITY)
3687 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003688 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003689 shl = &search_hl;
3690 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003691 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003692 else
3693 shl = &cur->hl;
3694 if (shl->attr_cur != 0)
3695 search_attr = shl->attr_cur;
3696 if (shl != &search_hl && cur != NULL)
3697 cur = cur->next;
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700#endif
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003703 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003705 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3706 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003708 if (diff_hlf == HLF_TXD && ptr - line > change_end
3709 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003711 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
3713#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003714
3715 /* Decide which of the highlight attributes to use. */
3716 attr_pri = TRUE;
3717 if (area_attr != 0)
3718 char_attr = area_attr;
3719 else if (search_attr != 0)
3720 char_attr = search_attr;
3721#ifdef LINE_ATTR
3722 /* Use line_attr when not in the Visual or 'incsearch' area
3723 * (area_attr may be 0 when "noinvcur" is set). */
3724 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003725 || vcol < fromcol || vcol_prev < fromcol_prev
3726 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003727 char_attr = line_attr;
3728#endif
3729 else
3730 {
3731 attr_pri = FALSE;
3732#ifdef FEAT_SYN_HL
3733 if (has_syntax)
3734 char_attr = syntax_attr;
3735 else
3736#endif
3737 char_attr = 0;
3738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740
3741 /*
3742 * Get the next character to put on the screen.
3743 */
3744 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003745 * The "p_extra" points to the extra stuff that is inserted to
3746 * represent special characters (non-printable stuff) and other
3747 * things. When all characters are the same, c_extra is used.
3748 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3749 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3751 */
3752 if (n_extra > 0)
3753 {
3754 if (c_extra != NUL)
3755 {
3756 c = c_extra;
3757#ifdef FEAT_MBYTE
3758 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3759 if (enc_utf8 && (*mb_char2len)(c) > 1)
3760 {
3761 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003762 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003763 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
3765 else
3766 mb_utf8 = FALSE;
3767#endif
3768 }
3769 else
3770 {
3771 c = *p_extra;
3772#ifdef FEAT_MBYTE
3773 if (has_mbyte)
3774 {
3775 mb_c = c;
3776 if (enc_utf8)
3777 {
3778 /* If the UTF-8 character is more than one byte:
3779 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003780 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 mb_utf8 = FALSE;
3782 if (mb_l > n_extra)
3783 mb_l = 1;
3784 else if (mb_l > 1)
3785 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003786 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003788 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
3791 else
3792 {
3793 /* if this is a DBCS character, put it in "mb_c" */
3794 mb_l = MB_BYTE2LEN(c);
3795 if (mb_l >= n_extra)
3796 mb_l = 1;
3797 else if (mb_l > 1)
3798 mb_c = (c << 8) + p_extra[1];
3799 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003800 if (mb_l == 0) /* at the NUL at end-of-line */
3801 mb_l = 1;
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 /* If a double-width char doesn't fit display a '>' in the
3804 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003805 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806# ifdef FEAT_RIGHTLEFT
3807 wp->w_p_rl ? (col <= 0) :
3808# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003809 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 && (*mb_char2cells)(mb_c) == 2)
3811 {
3812 c = '>';
3813 mb_c = c;
3814 mb_l = 1;
3815 mb_utf8 = FALSE;
3816 multi_attr = hl_attr(HLF_AT);
3817 /* put the pointer back to output the double-width
3818 * character at the start of the next line. */
3819 ++n_extra;
3820 --p_extra;
3821 }
3822 else
3823 {
3824 n_extra -= mb_l - 1;
3825 p_extra += mb_l - 1;
3826 }
3827 }
3828#endif
3829 ++p_extra;
3830 }
3831 --n_extra;
3832 }
3833 else
3834 {
3835 /*
3836 * Get a character from the line itself.
3837 */
3838 c = *ptr;
3839#ifdef FEAT_MBYTE
3840 if (has_mbyte)
3841 {
3842 mb_c = c;
3843 if (enc_utf8)
3844 {
3845 /* If the UTF-8 character is more than one byte: Decode it
3846 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003847 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 mb_utf8 = FALSE;
3849 if (mb_l > 1)
3850 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003851 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 /* Overlong encoded ASCII or ASCII with composing char
3853 * is displayed normally, except a NUL. */
3854 if (mb_c < 0x80)
3855 c = mb_c;
3856 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003857
3858 /* At start of the line we can have a composing char.
3859 * Draw it as a space with a composing char. */
3860 if (utf_iscomposing(mb_c))
3861 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003862 int i;
3863
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003864 for (i = Screen_mco - 1; i > 0; --i)
3865 u8cc[i] = u8cc[i - 1];
3866 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003867 mb_c = ' ';
3868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870
3871 if ((mb_l == 1 && c >= 0x80)
3872 || (mb_l >= 1 && mb_c == 0)
3873 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003874# ifdef UNICODE16
3875 || mb_c >= 0x10000
3876# endif
3877 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
3879 /*
3880 * Illegal UTF-8 byte: display as <xx>.
3881 * Non-BMP character : display as ? or fullwidth ?.
3882 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003883# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
3887 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003888# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (wp->w_p_rl) /* reverse */
3890 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003893# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 else if (utf_char2cells(mb_c) != 2)
3895 STRCPY(extra, "?");
3896 else
3897 /* 0xff1f in UTF-8: full-width '?' */
3898 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
3901 p_extra = extra;
3902 c = *p_extra;
3903 mb_c = mb_ptr2char_adv(&p_extra);
3904 mb_utf8 = (c >= 0x80);
3905 n_extra = (int)STRLEN(p_extra);
3906 c_extra = NUL;
3907 if (area_attr == 0 && search_attr == 0)
3908 {
3909 n_attr = n_extra + 1;
3910 extra_attr = hl_attr(HLF_8);
3911 saved_attr2 = char_attr; /* save current attr */
3912 }
3913 }
3914 else if (mb_l == 0) /* at the NUL at end-of-line */
3915 mb_l = 1;
3916#ifdef FEAT_ARABIC
3917 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3918 {
3919 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003920 int pc, pc1, nc;
3921 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923 /* The idea of what is the previous and next
3924 * character depends on 'rightleft'. */
3925 if (wp->w_p_rl)
3926 {
3927 pc = prev_c;
3928 pc1 = prev_c1;
3929 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003930 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932 else
3933 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003934 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003936 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 prev_c = mb_c;
3939
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003940 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942 else
3943 prev_c = mb_c;
3944#endif
3945 }
3946 else /* enc_dbcs */
3947 {
3948 mb_l = MB_BYTE2LEN(c);
3949 if (mb_l == 0) /* at the NUL at end-of-line */
3950 mb_l = 1;
3951 else if (mb_l > 1)
3952 {
3953 /* We assume a second byte below 32 is illegal.
3954 * Hopefully this is OK for all double-byte encodings!
3955 */
3956 if (ptr[1] >= 32)
3957 mb_c = (c << 8) + ptr[1];
3958 else
3959 {
3960 if (ptr[1] == NUL)
3961 {
3962 /* head byte at end of line */
3963 mb_l = 1;
3964 transchar_nonprint(extra, c);
3965 }
3966 else
3967 {
3968 /* illegal tail byte */
3969 mb_l = 2;
3970 STRCPY(extra, "XX");
3971 }
3972 p_extra = extra;
3973 n_extra = (int)STRLEN(extra) - 1;
3974 c_extra = NUL;
3975 c = *p_extra++;
3976 if (area_attr == 0 && search_attr == 0)
3977 {
3978 n_attr = n_extra + 1;
3979 extra_attr = hl_attr(HLF_8);
3980 saved_attr2 = char_attr; /* save current attr */
3981 }
3982 mb_c = c;
3983 }
3984 }
3985 }
3986 /* If a double-width char doesn't fit display a '>' in the
3987 * last column; the character is displayed at the start of the
3988 * next line. */
3989 if ((
3990# ifdef FEAT_RIGHTLEFT
3991 wp->w_p_rl ? (col <= 0) :
3992# endif
3993 (col >= W_WIDTH(wp) - 1))
3994 && (*mb_char2cells)(mb_c) == 2)
3995 {
3996 c = '>';
3997 mb_c = c;
3998 mb_utf8 = FALSE;
3999 mb_l = 1;
4000 multi_attr = hl_attr(HLF_AT);
4001 /* Put pointer back so that the character will be
4002 * displayed at the start of the next line. */
4003 --ptr;
4004 }
4005 else if (*ptr != NUL)
4006 ptr += mb_l - 1;
4007
4008 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004009 * a '<' in the first column. Don't do this for unprintable
4010 * charactes. */
4011 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004014 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 c = ' ';
4016 if (area_attr == 0 && search_attr == 0)
4017 {
4018 n_attr = n_extra + 1;
4019 extra_attr = hl_attr(HLF_AT);
4020 saved_attr2 = char_attr; /* save current attr */
4021 }
4022 mb_c = c;
4023 mb_utf8 = FALSE;
4024 mb_l = 1;
4025 }
4026
4027 }
4028#endif
4029 ++ptr;
4030
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004031 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 if (wp->w_p_list && (c == 160
4033#ifdef FEAT_MBYTE
4034 || (mb_utf8 && mb_c == 160)
4035#endif
4036 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004037 {
4038 c = lcs_nbsp;
4039 if (area_attr == 0 && search_attr == 0)
4040 {
4041 n_attr = 1;
4042 extra_attr = hl_attr(HLF_8);
4043 saved_attr2 = char_attr; /* save current attr */
4044 }
4045#ifdef FEAT_MBYTE
4046 mb_c = c;
4047 if (enc_utf8 && (*mb_char2len)(c) > 1)
4048 {
4049 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004050 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004051 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004052 }
4053 else
4054 mb_utf8 = FALSE;
4055#endif
4056 }
4057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 if (extra_check)
4059 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004060#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004061 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004062#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004063
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004064#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 /* Get syntax attribute, unless still at the start of the line
4066 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004067 v = (long)(ptr - line);
4068 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
4070 /* Get the syntax attribute for the character. If there
4071 * is an error, disable syntax highlighting. */
4072 save_did_emsg = did_emsg;
4073 did_emsg = FALSE;
4074
Bram Moolenaar217ad922005-03-20 22:37:15 +00004075 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004076# ifdef FEAT_SPELL
4077 has_spell ? &can_spell :
4078# endif
4079 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004082 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004083 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004084 has_syntax = FALSE;
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
4087 did_emsg = save_did_emsg;
4088
4089 /* Need to get the line again, a multi-line regexp may
4090 * have made it invalid. */
4091 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4092 ptr = line + v;
4093
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004094 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004096 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004097 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004098# ifdef FEAT_CONCEAL
4099 /* no concealing past the end of the line, it interferes
4100 * with line highlighting */
4101 if (c == NUL)
4102 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004103 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004104 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004107#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004108
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004109#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004110 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004111 * Only do this when there is no syntax highlighting, the
4112 * @Spell cluster is not used or the current syntax item
4113 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004114 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004115 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004116 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004117# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004118 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004119 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004120# endif
4121 if (c != 0 && (
4122# ifdef FEAT_SYN_HL
4123 !has_syntax ||
4124# endif
4125 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004126 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004127 char_u *prev_ptr, *p;
4128 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004129 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004130# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004131 if (has_mbyte)
4132 {
4133 prev_ptr = ptr - mb_l;
4134 v -= mb_l - 1;
4135 }
4136 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004137# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004138 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004139
4140 /* Use nextline[] if possible, it has the start of the
4141 * next line concatenated. */
4142 if ((prev_ptr - line) - nextlinecol >= 0)
4143 p = nextline + (prev_ptr - line) - nextlinecol;
4144 else
4145 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004146 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004147 len = spell_check(wp, p, &spell_hlf, &cap_col,
4148 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004149 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004150
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004151 /* In Insert mode only highlight a word that
4152 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004153 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004154 && (State & INSERT) != 0
4155 && wp->w_cursor.lnum == lnum
4156 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004157 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004158 && wp->w_cursor.col < (colnr_T)word_end)
4159 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004160 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004161 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004162 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004163
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004164 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004165 && (p - nextline) + len > nextline_idx)
4166 {
4167 /* Remember that the good word continues at the
4168 * start of the next line. */
4169 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004170 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004171 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004172
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004173 /* Turn index into actual attributes. */
4174 if (spell_hlf != HLF_COUNT)
4175 spell_attr = highlight_attr[spell_hlf];
4176
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004177 if (cap_col > 0)
4178 {
4179 if (p != prev_ptr
4180 && (p - nextline) + cap_col >= nextline_idx)
4181 {
4182 /* Remember that the word in the next line
4183 * must start with a capital. */
4184 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004185 cap_col = (int)((p - nextline) + cap_col
4186 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004187 }
4188 else
4189 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004190 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004191 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004192 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004193 }
4194 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004195 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004196 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004197 char_attr = hl_combine_attr(char_attr, spell_attr);
4198 else
4199 char_attr = hl_combine_attr(spell_attr, char_attr);
4200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#endif
4202#ifdef FEAT_LINEBREAK
4203 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004204 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 */
4206 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004207 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
4209 n_extra = win_lbr_chartabsize(wp, ptr - (
4210# ifdef FEAT_MBYTE
4211 has_mbyte ? mb_l :
4212# endif
4213 1), (colnr_T)vcol, NULL) - 1;
4214 c_extra = ' ';
4215 if (vim_iswhite(c))
4216 c = ' ';
4217 }
4218#endif
4219
4220 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4221 {
4222 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004223 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
4225 n_attr = 1;
4226 extra_attr = hl_attr(HLF_8);
4227 saved_attr2 = char_attr; /* save current attr */
4228 }
4229#ifdef FEAT_MBYTE
4230 mb_c = c;
4231 if (enc_utf8 && (*mb_char2len)(c) > 1)
4232 {
4233 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004234 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004235 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237 else
4238 mb_utf8 = FALSE;
4239#endif
4240 }
4241 }
4242
4243 /*
4244 * Handling of non-printable characters.
4245 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004246 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
4248 /*
4249 * when getting a character from the file, we may have to
4250 * turn it into something else on the way to putting it
4251 * into "ScreenLines".
4252 */
4253 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4254 {
4255 /* tab amount depends on current column */
4256 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar8a20b0f2011-08-10 14:32:39 +02004257 - VCOL_HLC % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258#ifdef FEAT_MBYTE
4259 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4260#endif
4261 if (wp->w_p_list)
4262 {
4263 c = lcs_tab1;
4264 c_extra = lcs_tab2;
4265 n_attr = n_extra + 1;
4266 extra_attr = hl_attr(HLF_8);
4267 saved_attr2 = char_attr; /* save current attr */
4268#ifdef FEAT_MBYTE
4269 mb_c = c;
4270 if (enc_utf8 && (*mb_char2len)(c) > 1)
4271 {
4272 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004273 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004274 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276#endif
4277 }
4278 else
4279 {
4280 c_extra = ' ';
4281 c = ' ';
4282 }
4283 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004284 else if (c == NUL
4285 && ((wp->w_p_list && lcs_eol > 0)
4286 || ((fromcol >= 0 || fromcol_prev >= 0)
4287 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004288#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004289 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004290#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004291 && (
4292# ifdef FEAT_RIGHTLEFT
4293 wp->w_p_rl ? (col >= 0) :
4294# endif
4295 (col < W_WIDTH(wp)))
4296 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004297 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004298 && (colnr_T)vcol == wp->w_virtcol)))
4299 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004301 /* Display a '$' after the line or highlight an extra
4302 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4304 /* For a diff line the highlighting continues after the
4305 * "$". */
4306 if (
4307# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004308 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309# ifdef LINE_ATTR
4310 &&
4311# endif
4312# endif
4313# ifdef LINE_ATTR
4314 line_attr == 0
4315# endif
4316 )
4317#endif
4318 {
4319#ifdef FEAT_VIRTUALEDIT
4320 /* In virtualedit, visual selections may extend
4321 * beyond end of line. */
4322 if (area_highlighting && virtual_active()
4323 && tocol != MAXCOL && vcol < tocol)
4324 n_extra = 0;
4325 else
4326#endif
4327 {
4328 p_extra = at_end_str;
4329 n_extra = 1;
4330 c_extra = NUL;
4331 }
4332 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004333 if (wp->w_p_list)
4334 c = lcs_eol;
4335 else
4336 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 lcs_eol_one = -1;
4338 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004339 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 {
4341 extra_attr = hl_attr(HLF_AT);
4342 n_attr = 1;
4343 }
4344#ifdef FEAT_MBYTE
4345 mb_c = c;
4346 if (enc_utf8 && (*mb_char2len)(c) > 1)
4347 {
4348 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004349 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004350 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 else
4353 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4354#endif
4355 }
4356 else if (c != NUL)
4357 {
4358 p_extra = transchar(c);
4359#ifdef FEAT_RIGHTLEFT
4360 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4361 rl_mirror(p_extra); /* reverse "<12>" */
4362#endif
4363 n_extra = byte2cells(c) - 1;
4364 c_extra = NUL;
4365 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004366 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 {
4368 n_attr = n_extra + 1;
4369 extra_attr = hl_attr(HLF_8);
4370 saved_attr2 = char_attr; /* save current attr */
4371 }
4372#ifdef FEAT_MBYTE
4373 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4374#endif
4375 }
4376#ifdef FEAT_VIRTUALEDIT
4377 else if (VIsual_active
4378 && (VIsual_mode == Ctrl_V
4379 || VIsual_mode == 'v')
4380 && virtual_active()
4381 && tocol != MAXCOL
4382 && vcol < tocol
4383 && (
4384# ifdef FEAT_RIGHTLEFT
4385 wp->w_p_rl ? (col >= 0) :
4386# endif
4387 (col < W_WIDTH(wp))))
4388 {
4389 c = ' ';
4390 --ptr; /* put it back at the NUL */
4391 }
4392#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004393#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 else if ((
4395# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004396 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 ) && (
4400# ifdef FEAT_RIGHTLEFT
4401 wp->w_p_rl ? (col >= 0) :
4402# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004403 (col
4404# ifdef FEAT_CONCEAL
4405 - boguscols
4406# endif
4407 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
4409 /* Highlight until the right side of the window */
4410 c = ' ';
4411 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004412
4413 /* Remember we do the char for line highlighting. */
4414 ++did_line_attr;
4415
4416 /* don't do search HL for the rest of the line */
4417 if (line_attr != 0 && char_attr == search_attr && col > 0)
4418 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419# ifdef FEAT_DIFF
4420 if (diff_hlf == HLF_TXD)
4421 {
4422 diff_hlf = HLF_CHD;
4423 if (attr == 0 || char_attr != attr)
4424 char_attr = hl_attr(diff_hlf);
4425 }
4426# endif
4427 }
4428#endif
4429 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004430
4431#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004432 if ( wp->w_p_cole > 0
4433 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4434 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004435 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004436 && !(lnum_in_visual_area
4437 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004438 {
4439 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004440 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004441 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4442 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004443 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004444 /* First time at this concealed item: display one
4445 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004446 if (syn_get_sub_char() != NUL)
4447 c = syn_get_sub_char();
4448 else if (lcs_conceal != NUL)
4449 c = lcs_conceal;
4450 else
4451 c = ' ';
4452
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004453 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004454
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004455 if (n_extra > 0)
4456 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004457 vcol += n_extra;
4458 if (wp->w_p_wrap && n_extra > 0)
4459 {
4460# ifdef FEAT_RIGHTLEFT
4461 if (wp->w_p_rl)
4462 {
4463 col -= n_extra;
4464 boguscols -= n_extra;
4465 }
4466 else
4467# endif
4468 {
4469 boguscols += n_extra;
4470 col += n_extra;
4471 }
4472 }
4473 n_extra = 0;
4474 n_attr = 0;
4475 }
4476 else if (n_skip == 0)
4477 {
4478 is_concealing = TRUE;
4479 n_skip = 1;
4480 }
4481# ifdef FEAT_MBYTE
4482 mb_c = c;
4483 if (enc_utf8 && (*mb_char2len)(c) > 1)
4484 {
4485 mb_utf8 = TRUE;
4486 u8cc[0] = 0;
4487 c = 0xc0;
4488 }
4489 else
4490 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4491# endif
4492 }
4493 else
4494 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004495 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004496 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004497 }
4498#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004501#ifdef FEAT_CONCEAL
4502 /* In the cursor line and we may be concealing characters: correct
4503 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004504 if (!did_wcol && draw_state == WL_LINE
4505 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004506 && conceal_cursor_line(wp)
4507 && (int)wp->w_virtcol <= vcol + n_skip)
4508 {
4509 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004510 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004511 did_wcol = TRUE;
4512 }
4513#endif
4514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /* Don't override visual selection highlighting. */
4516 if (n_attr > 0
4517 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004518 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 char_attr = extra_attr;
4520
Bram Moolenaar81695252004-12-29 20:58:21 +00004521#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 /* XIM don't send preedit_start and preedit_end, but they send
4523 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4524 * im_is_preediting() here. */
4525 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004526 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 && (State & INSERT)
4528 && !p_imdisable
4529 && im_is_preediting()
4530 && draw_state == WL_LINE)
4531 {
4532 colnr_T tcol;
4533
4534 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004535 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 else
4537 tcol = preedit_end_col;
4538 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4539 {
4540 if (feedback_old_attr < 0)
4541 {
4542 feedback_col = 0;
4543 feedback_old_attr = char_attr;
4544 }
4545 char_attr = im_get_feedback_attr(feedback_col);
4546 if (char_attr < 0)
4547 char_attr = feedback_old_attr;
4548 feedback_col++;
4549 }
4550 else if (feedback_old_attr >= 0)
4551 {
4552 char_attr = feedback_old_attr;
4553 feedback_old_attr = -1;
4554 feedback_col = 0;
4555 }
4556 }
4557#endif
4558 /*
4559 * Handle the case where we are in column 0 but not on the first
4560 * character of the line and the user wants us to show us a
4561 * special character (via 'listchars' option "precedes:<char>".
4562 */
4563 if (lcs_prec_todo != NUL
4564 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4565#ifdef FEAT_DIFF
4566 && filler_todo <= 0
4567#endif
4568 && draw_state > WL_NR
4569 && c != NUL)
4570 {
4571 c = lcs_prec;
4572 lcs_prec_todo = NUL;
4573#ifdef FEAT_MBYTE
4574 mb_c = c;
4575 if (enc_utf8 && (*mb_char2len)(c) > 1)
4576 {
4577 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004578 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004579 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 else
4582 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4583#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004584 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 saved_attr3 = char_attr; /* save current attr */
4587 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4588 n_attr3 = 1;
4589 }
4590 }
4591
4592 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004593 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004595 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004596#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004597 || did_line_attr == 1
4598#endif
4599 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004601#ifdef FEAT_SEARCH_EXTRA
4602 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004603
4604 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004605 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004606 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004607#endif
4608
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004609 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 * highlight match at end of line. If it's beyond the last
4611 * char on the screen, just overwrite that one (tricky!) Not
4612 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004613#ifdef FEAT_SEARCH_EXTRA
4614 prevcol_hl_flag = FALSE;
4615 if (prevcol == (long)search_hl.startcol)
4616 prevcol_hl_flag = TRUE;
4617 else
4618 {
4619 cur = wp->w_match_head;
4620 while (cur != NULL)
4621 {
4622 if (prevcol == (long)cur->hl.startcol)
4623 {
4624 prevcol_hl_flag = TRUE;
4625 break;
4626 }
4627 cur = cur->next;
4628 }
4629 }
4630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004632 && ((area_attr != 0 && vcol == fromcol
4633#ifdef FEAT_VISUAL
4634 && (VIsual_mode != Ctrl_V
4635 || lnum == VIsual.lnum
4636 || lnum == curwin->w_cursor.lnum)
4637#endif
4638 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639#ifdef FEAT_SEARCH_EXTRA
4640 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004641 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004642# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004643 && did_line_attr <= 1
4644# endif
4645 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#endif
4647 ))
4648 {
4649 int n = 0;
4650
4651#ifdef FEAT_RIGHTLEFT
4652 if (wp->w_p_rl)
4653 {
4654 if (col < 0)
4655 n = 1;
4656 }
4657 else
4658#endif
4659 {
4660 if (col >= W_WIDTH(wp))
4661 n = -1;
4662 }
4663 if (n != 0)
4664 {
4665 /* At the window boundary, highlight the last character
4666 * instead (better than nothing). */
4667 off += n;
4668 col += n;
4669 }
4670 else
4671 {
4672 /* Add a blank character to highlight. */
4673 ScreenLines[off] = ' ';
4674#ifdef FEAT_MBYTE
4675 if (enc_utf8)
4676 ScreenLinesUC[off] = 0;
4677#endif
4678 }
4679#ifdef FEAT_SEARCH_EXTRA
4680 if (area_attr == 0)
4681 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004682 /* Use attributes from match with highest priority among
4683 * 'search_hl' and the match list. */
4684 char_attr = search_hl.attr;
4685 cur = wp->w_match_head;
4686 shl_flag = FALSE;
4687 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004688 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004689 if (shl_flag == FALSE
4690 && ((cur != NULL
4691 && cur->priority > SEARCH_HL_PRIORITY)
4692 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004693 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004694 shl = &search_hl;
4695 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004696 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004697 else
4698 shl = &cur->hl;
4699 if ((ptr - line) - 1 == (long)shl->startcol)
4700 char_attr = shl->attr;
4701 if (shl != &search_hl && cur != NULL)
4702 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705#endif
4706 ScreenAttrs[off] = char_attr;
4707#ifdef FEAT_RIGHTLEFT
4708 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004711 --off;
4712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 else
4714#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004717 ++off;
4718 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004719 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004720#ifdef FEAT_SYN_HL
4721 eol_hl_off = 1;
4722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaar91170f82006-05-05 21:15:17 +00004726 /*
4727 * At end of the text line.
4728 */
4729 if (c == NUL)
4730 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004731#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004732 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4733 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004734 {
4735 /* highlight last char after line */
4736 --col;
4737 --off;
4738 --vcol;
4739 }
4740
Bram Moolenaar1a384422010-07-14 19:53:30 +02004741 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004742 if (wp->w_p_wrap)
4743 v = wp->w_skipcol;
4744 else
4745 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004746
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004747 /* check if line ends before left margin */
4748 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004749 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004750#ifdef FEAT_CONCEAL
4751 /* Get rid of the boguscols now, we want to draw until the right
4752 * edge for 'cursorcolumn'. */
4753 col -= boguscols;
4754 boguscols = 0;
4755#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004756
4757 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004758 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004759
4760 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004761 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4762 && (int)wp->w_virtcol <
4763 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004764 && lnum != wp->w_cursor.lnum)
4765 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004766# ifdef FEAT_RIGHTLEFT
4767 && !wp->w_p_rl
4768# endif
4769 )
4770 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004771 int rightmost_vcol = 0;
4772 int i;
4773
4774 if (wp->w_p_cuc)
4775 rightmost_vcol = wp->w_virtcol;
4776 if (draw_color_col)
4777 /* determine rightmost colorcolumn to possibly draw */
4778 for (i = 0; color_cols[i] >= 0; ++i)
4779 if (rightmost_vcol < color_cols[i])
4780 rightmost_vcol = color_cols[i];
4781
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004782 while (col < W_WIDTH(wp))
4783 {
4784 ScreenLines[off] = ' ';
4785#ifdef FEAT_MBYTE
4786 if (enc_utf8)
4787 ScreenLinesUC[off] = 0;
4788#endif
4789 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004790 if (draw_color_col)
4791 draw_color_col = advance_color_col(VCOL_HLC,
4792 &color_cols);
4793
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004794 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004795 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004796 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004797 ScreenAttrs[off++] = hl_attr(HLF_MC);
4798 else
4799 ScreenAttrs[off++] = 0;
4800
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004801 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004802 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004803
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004804 ++vcol;
4805 }
4806 }
4807#endif
4808
Bram Moolenaar860cae12010-06-05 23:22:07 +02004809 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4810 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 row++;
4812
4813 /*
4814 * Update w_cline_height and w_cline_folded if the cursor line was
4815 * updated (saves a call to plines() later).
4816 */
4817 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4818 {
4819 curwin->w_cline_row = startrow;
4820 curwin->w_cline_height = row - startrow;
4821#ifdef FEAT_FOLDING
4822 curwin->w_cline_folded = FALSE;
4823#endif
4824 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4825 }
4826
4827 break;
4828 }
4829
4830 /* line continues beyond line end */
4831 if (lcs_ext
4832 && !wp->w_p_wrap
4833#ifdef FEAT_DIFF
4834 && filler_todo <= 0
4835#endif
4836 && (
4837#ifdef FEAT_RIGHTLEFT
4838 wp->w_p_rl ? col == 0 :
4839#endif
4840 col == W_WIDTH(wp) - 1)
4841 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004842 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4844 {
4845 c = lcs_ext;
4846 char_attr = hl_attr(HLF_AT);
4847#ifdef FEAT_MBYTE
4848 mb_c = c;
4849 if (enc_utf8 && (*mb_char2len)(c) > 1)
4850 {
4851 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004852 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004853 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 else
4856 mb_utf8 = FALSE;
4857#endif
4858 }
4859
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004860#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004861 /* advance to the next 'colorcolumn' */
4862 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004863 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004864
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004865 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004866 * highlight the cursor position itself.
4867 * Also highlight the 'colorcolumn' if it is different than
4868 * 'cursorcolumn' */
4869 vcol_save_attr = -1;
4870 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004871 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004872 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004873 && lnum != wp->w_cursor.lnum)
4874 {
4875 vcol_save_attr = char_attr;
4876 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4877 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004878 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004879 {
4880 vcol_save_attr = char_attr;
4881 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4882 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004883 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004884#endif
4885
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 /*
4887 * Store character to be displayed.
4888 * Skip characters that are left of the screen for 'nowrap'.
4889 */
4890 vcol_prev = vcol;
4891 if (draw_state < WL_LINE || n_skip <= 0)
4892 {
4893 /*
4894 * Store the character.
4895 */
4896#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4897 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4898 {
4899 /* A double-wide character is: put first halve in left cell. */
4900 --off;
4901 --col;
4902 }
4903#endif
4904 ScreenLines[off] = c;
4905#ifdef FEAT_MBYTE
4906 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004907 {
4908 if ((mb_c & 0xff00) == 0x8e00)
4909 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 else if (enc_utf8)
4913 {
4914 if (mb_utf8)
4915 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004916 int i;
4917
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004919 if ((c & 0xff) == 0)
4920 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004921 for (i = 0; i < Screen_mco; ++i)
4922 {
4923 ScreenLinesC[i][off] = u8cc[i];
4924 if (u8cc[i] == 0)
4925 break;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
4929 ScreenLinesUC[off] = 0;
4930 }
4931 if (multi_attr)
4932 {
4933 ScreenAttrs[off] = multi_attr;
4934 multi_attr = 0;
4935 }
4936 else
4937#endif
4938 ScreenAttrs[off] = char_attr;
4939
4940#ifdef FEAT_MBYTE
4941 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4942 {
4943 /* Need to fill two screen columns. */
4944 ++off;
4945 ++col;
4946 if (enc_utf8)
4947 /* UTF-8: Put a 0 in the second screen char. */
4948 ScreenLines[off] = 0;
4949 else
4950 /* DBCS: Put second byte in the second screen char. */
4951 ScreenLines[off] = mb_c & 0xff;
4952 ++vcol;
4953 /* When "tocol" is halfway a character, set it to the end of
4954 * the character, otherwise highlighting won't stop. */
4955 if (tocol == vcol)
4956 ++tocol;
4957#ifdef FEAT_RIGHTLEFT
4958 if (wp->w_p_rl)
4959 {
4960 /* now it's time to backup one cell */
4961 --off;
4962 --col;
4963 }
4964#endif
4965 }
4966#endif
4967#ifdef FEAT_RIGHTLEFT
4968 if (wp->w_p_rl)
4969 {
4970 --off;
4971 --col;
4972 }
4973 else
4974#endif
4975 {
4976 ++off;
4977 ++col;
4978 }
4979 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004980#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004981 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004982 {
4983 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004984 ++vcol_off;
4985 if (n_extra > 0)
4986 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004987 if (wp->w_p_wrap)
4988 {
4989 /*
4990 * Special voodoo required if 'wrap' is on.
4991 *
4992 * Advance the column indicator to force the line
4993 * drawing to wrap early. This will make the line
4994 * take up the same screen space when parts are concealed,
4995 * so that cursor line computations aren't messed up.
4996 *
4997 * To avoid the fictitious advance of 'col' causing
4998 * trailing junk to be written out of the screen line
4999 * we are building, 'boguscols' keeps track of the number
5000 * of bad columns we have advanced.
5001 */
5002 if (n_extra > 0)
5003 {
5004 vcol += n_extra;
5005# ifdef FEAT_RIGHTLEFT
5006 if (wp->w_p_rl)
5007 {
5008 col -= n_extra;
5009 boguscols -= n_extra;
5010 }
5011 else
5012# endif
5013 {
5014 col += n_extra;
5015 boguscols += n_extra;
5016 }
5017 n_extra = 0;
5018 n_attr = 0;
5019 }
5020
5021
5022# ifdef FEAT_MBYTE
5023 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5024 {
5025 /* Need to fill two screen columns. */
5026# ifdef FEAT_RIGHTLEFT
5027 if (wp->w_p_rl)
5028 {
5029 --boguscols;
5030 --col;
5031 }
5032 else
5033# endif
5034 {
5035 ++boguscols;
5036 ++col;
5037 }
5038 }
5039# endif
5040
5041# ifdef FEAT_RIGHTLEFT
5042 if (wp->w_p_rl)
5043 {
5044 --boguscols;
5045 --col;
5046 }
5047 else
5048# endif
5049 {
5050 ++boguscols;
5051 ++col;
5052 }
5053 }
5054 else
5055 {
5056 if (n_extra > 0)
5057 {
5058 vcol += n_extra;
5059 n_extra = 0;
5060 n_attr = 0;
5061 }
5062 }
5063
5064 }
5065#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 else
5067 --n_skip;
5068
Bram Moolenaar64486672010-05-16 15:46:46 +02005069 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5070 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005071 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072#ifdef FEAT_DIFF
5073 && filler_todo <= 0
5074#endif
5075 )
5076 ++vcol;
5077
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005078#ifdef FEAT_SYN_HL
5079 if (vcol_save_attr >= 0)
5080 char_attr = vcol_save_attr;
5081#endif
5082
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 /* restore attributes after "predeces" in 'listchars' */
5084 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5085 char_attr = saved_attr3;
5086
5087 /* restore attributes after last 'listchars' or 'number' char */
5088 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5089 char_attr = saved_attr2;
5090
5091 /*
5092 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005093 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 */
5095 if ((
5096#ifdef FEAT_RIGHTLEFT
5097 wp->w_p_rl ? (col < 0) :
5098#endif
5099 (col >= W_WIDTH(wp)))
5100 && (*ptr != NUL
5101#ifdef FEAT_DIFF
5102 || filler_todo > 0
5103#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005104 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5106 )
5107 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005108#ifdef FEAT_CONCEAL
5109 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5110 (int)W_WIDTH(wp), wp->w_p_rl);
5111 boguscols = 0;
5112#else
5113 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5114 (int)W_WIDTH(wp), wp->w_p_rl);
5115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 ++row;
5117 ++screen_row;
5118
5119 /* When not wrapping and finished diff lines, or when displayed
5120 * '$' and highlighting until last column, break here. */
5121 if ((!wp->w_p_wrap
5122#ifdef FEAT_DIFF
5123 && filler_todo <= 0
5124#endif
5125 ) || lcs_eol_one == -1)
5126 break;
5127
5128 /* When the window is too narrow draw all "@" lines. */
5129 if (draw_state != WL_LINE
5130#ifdef FEAT_DIFF
5131 && filler_todo <= 0
5132#endif
5133 )
5134 {
5135 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5136#ifdef FEAT_VERTSPLIT
5137 draw_vsep_win(wp, row);
5138#endif
5139 row = endrow;
5140 }
5141
5142 /* When line got too long for screen break here. */
5143 if (row == endrow)
5144 {
5145 ++row;
5146 break;
5147 }
5148
5149 if (screen_cur_row == screen_row - 1
5150#ifdef FEAT_DIFF
5151 && filler_todo <= 0
5152#endif
5153 && W_WIDTH(wp) == Columns)
5154 {
5155 /* Remember that the line wraps, used for modeless copy. */
5156 LineWraps[screen_row - 1] = TRUE;
5157
5158 /*
5159 * Special trick to make copy/paste of wrapped lines work with
5160 * xterm/screen: write an extra character beyond the end of
5161 * the line. This will work with all terminal types
5162 * (regardless of the xn,am settings).
5163 * Only do this on a fast tty.
5164 * Only do this if the cursor is on the current line
5165 * (something has been written in it).
5166 * Don't do this for the GUI.
5167 * Don't do this for double-width characters.
5168 * Don't do this for a window not at the right screen border.
5169 */
5170 if (p_tf
5171#ifdef FEAT_GUI
5172 && !gui.in_use
5173#endif
5174#ifdef FEAT_MBYTE
5175 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005176 && ((*mb_off2cells)(LineOffset[screen_row],
5177 LineOffset[screen_row] + screen_Columns)
5178 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005180 + (int)Columns - 2,
5181 LineOffset[screen_row] + screen_Columns)
5182 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#endif
5184 )
5185 {
5186 /* First make sure we are at the end of the screen line,
5187 * then output the same character again to let the
5188 * terminal know about the wrap. If the terminal doesn't
5189 * auto-wrap, we overwrite the character. */
5190 if (screen_cur_col != W_WIDTH(wp))
5191 screen_char(LineOffset[screen_row - 1]
5192 + (unsigned)Columns - 1,
5193 screen_row - 1, (int)(Columns - 1));
5194
5195#ifdef FEAT_MBYTE
5196 /* When there is a multi-byte character, just output a
5197 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005198 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5199 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 out_char(' ');
5201 else
5202#endif
5203 out_char(ScreenLines[LineOffset[screen_row - 1]
5204 + (Columns - 1)]);
5205 /* force a redraw of the first char on the next line */
5206 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5207 screen_start(); /* don't know where cursor is now */
5208 }
5209 }
5210
5211 col = 0;
5212 off = (unsigned)(current_ScreenLine - ScreenLines);
5213#ifdef FEAT_RIGHTLEFT
5214 if (wp->w_p_rl)
5215 {
5216 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5217 off += col;
5218 }
5219#endif
5220
5221 /* reset the drawing state for the start of a wrapped line */
5222 draw_state = WL_START;
5223 saved_n_extra = n_extra;
5224 saved_p_extra = p_extra;
5225 saved_c_extra = c_extra;
5226 saved_char_attr = char_attr;
5227 n_extra = 0;
5228 lcs_prec_todo = lcs_prec;
5229#ifdef FEAT_LINEBREAK
5230# ifdef FEAT_DIFF
5231 if (filler_todo <= 0)
5232# endif
5233 need_showbreak = TRUE;
5234#endif
5235#ifdef FEAT_DIFF
5236 --filler_todo;
5237 /* When the filler lines are actually below the last line of the
5238 * file, don't draw the line itself, break here. */
5239 if (filler_todo == 0 && wp->w_botfill)
5240 break;
5241#endif
5242 }
5243
5244 } /* for every character in the line */
5245
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005246#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005247 /* After an empty line check first word for capital. */
5248 if (*skipwhite(line) == NUL)
5249 {
5250 capcol_lnum = lnum + 1;
5251 cap_col = 0;
5252 }
5253#endif
5254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 return row;
5256}
5257
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005258#ifdef FEAT_MBYTE
5259static int comp_char_differs __ARGS((int, int));
5260
5261/*
5262 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005263 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005264 */
5265 static int
5266comp_char_differs(off_from, off_to)
5267 int off_from;
5268 int off_to;
5269{
5270 int i;
5271
5272 for (i = 0; i < Screen_mco; ++i)
5273 {
5274 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5275 return TRUE;
5276 if (ScreenLinesC[i][off_from] == 0)
5277 break;
5278 }
5279 return FALSE;
5280}
5281#endif
5282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283/*
5284 * Check whether the given character needs redrawing:
5285 * - the (first byte of the) character is different
5286 * - the attributes are different
5287 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005288 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 */
5290 static int
5291char_needs_redraw(off_from, off_to, cols)
5292 int off_from;
5293 int off_to;
5294 int cols;
5295{
5296 if (cols > 0
5297 && ((ScreenLines[off_from] != ScreenLines[off_to]
5298 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5299
5300#ifdef FEAT_MBYTE
5301 || (enc_dbcs != 0
5302 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5303 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5304 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5305 : (cols > 1 && ScreenLines[off_from + 1]
5306 != ScreenLines[off_to + 1])))
5307 || (enc_utf8
5308 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5309 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005310 && comp_char_differs(off_from, off_to))
5311 || (cols > 1 && ScreenLines[off_from + 1]
5312 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313#endif
5314 ))
5315 return TRUE;
5316 return FALSE;
5317}
5318
5319/*
5320 * Move one "cooked" screen line to the screen, but only the characters that
5321 * have actually changed. Handle insert/delete character.
5322 * "coloff" gives the first column on the screen for this line.
5323 * "endcol" gives the columns where valid characters are.
5324 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5325 * needs to be cleared, negative otherwise.
5326 * "rlflag" is TRUE in a rightleft window:
5327 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5328 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5329 */
5330 static void
5331screen_line(row, coloff, endcol, clear_width
5332#ifdef FEAT_RIGHTLEFT
5333 , rlflag
5334#endif
5335 )
5336 int row;
5337 int coloff;
5338 int endcol;
5339 int clear_width;
5340#ifdef FEAT_RIGHTLEFT
5341 int rlflag;
5342#endif
5343{
5344 unsigned off_from;
5345 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005346#ifdef FEAT_MBYTE
5347 unsigned max_off_from;
5348 unsigned max_off_to;
5349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 int col = 0;
5351#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5352 int hl;
5353#endif
5354 int force = FALSE; /* force update rest of the line */
5355 int redraw_this /* bool: does character need redraw? */
5356#ifdef FEAT_GUI
5357 = TRUE /* For GUI when while-loop empty */
5358#endif
5359 ;
5360 int redraw_next; /* redraw_this for next character */
5361#ifdef FEAT_MBYTE
5362 int clear_next = FALSE;
5363 int char_cells; /* 1: normal char */
5364 /* 2: occupies two display cells */
5365# define CHAR_CELLS char_cells
5366#else
5367# define CHAR_CELLS 1
5368#endif
5369
5370# ifdef FEAT_CLIPBOARD
5371 clip_may_clear_selection(row, row);
5372# endif
5373
5374 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5375 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005376#ifdef FEAT_MBYTE
5377 max_off_from = off_from + screen_Columns;
5378 max_off_to = LineOffset[row] + screen_Columns;
5379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
5381#ifdef FEAT_RIGHTLEFT
5382 if (rlflag)
5383 {
5384 /* Clear rest first, because it's left of the text. */
5385 if (clear_width > 0)
5386 {
5387 while (col <= endcol && ScreenLines[off_to] == ' '
5388 && ScreenAttrs[off_to] == 0
5389# ifdef FEAT_MBYTE
5390 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5391# endif
5392 )
5393 {
5394 ++off_to;
5395 ++col;
5396 }
5397 if (col <= endcol)
5398 screen_fill(row, row + 1, col + coloff,
5399 endcol + coloff + 1, ' ', ' ', 0);
5400 }
5401 col = endcol + 1;
5402 off_to = LineOffset[row] + col + coloff;
5403 off_from += col;
5404 endcol = (clear_width > 0 ? clear_width : -clear_width);
5405 }
5406#endif /* FEAT_RIGHTLEFT */
5407
5408 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5409
5410 while (col < endcol)
5411 {
5412#ifdef FEAT_MBYTE
5413 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005414 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 else
5416 char_cells = 1;
5417#endif
5418
5419 redraw_this = redraw_next;
5420 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5421 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5422
5423#ifdef FEAT_GUI
5424 /* If the next character was bold, then redraw the current character to
5425 * remove any pixels that might have spilt over into us. This only
5426 * happens in the GUI.
5427 */
5428 if (redraw_next && gui.in_use)
5429 {
5430 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005431 if (hl > HL_ALL)
5432 hl = syn_attr2attr(hl);
5433 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 redraw_this = TRUE;
5435 }
5436#endif
5437
5438 if (redraw_this)
5439 {
5440 /*
5441 * Special handling when 'xs' termcap flag set (hpterm):
5442 * Attributes for characters are stored at the position where the
5443 * cursor is when writing the highlighting code. The
5444 * start-highlighting code must be written with the cursor on the
5445 * first highlighted character. The stop-highlighting code must
5446 * be written with the cursor just after the last highlighted
5447 * character.
5448 * Overwriting a character doesn't remove it's highlighting. Need
5449 * to clear the rest of the line, and force redrawing it
5450 * completely.
5451 */
5452 if ( p_wiv
5453 && !force
5454#ifdef FEAT_GUI
5455 && !gui.in_use
5456#endif
5457 && ScreenAttrs[off_to] != 0
5458 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5459 {
5460 /*
5461 * Need to remove highlighting attributes here.
5462 */
5463 windgoto(row, col + coloff);
5464 out_str(T_CE); /* clear rest of this screen line */
5465 screen_start(); /* don't know where cursor is now */
5466 force = TRUE; /* force redraw of rest of the line */
5467 redraw_next = TRUE; /* or else next char would miss out */
5468
5469 /*
5470 * If the previous character was highlighted, need to stop
5471 * highlighting at this character.
5472 */
5473 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5474 {
5475 screen_attr = ScreenAttrs[off_to - 1];
5476 term_windgoto(row, col + coloff);
5477 screen_stop_highlight();
5478 }
5479 else
5480 screen_attr = 0; /* highlighting has stopped */
5481 }
5482#ifdef FEAT_MBYTE
5483 if (enc_dbcs != 0)
5484 {
5485 /* Check if overwriting a double-byte with a single-byte or
5486 * the other way around requires another character to be
5487 * redrawn. For UTF-8 this isn't needed, because comparing
5488 * ScreenLinesUC[] is sufficient. */
5489 if (char_cells == 1
5490 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005491 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {
5493 /* Writing a single-cell character over a double-cell
5494 * character: need to redraw the next cell. */
5495 ScreenLines[off_to + 1] = 0;
5496 redraw_next = TRUE;
5497 }
5498 else if (char_cells == 2
5499 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005500 && (*mb_off2cells)(off_to, max_off_to) == 1
5501 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 /* Writing the second half of a double-cell character over
5504 * a double-cell character: need to redraw the second
5505 * cell. */
5506 ScreenLines[off_to + 2] = 0;
5507 redraw_next = TRUE;
5508 }
5509
5510 if (enc_dbcs == DBCS_JPNU)
5511 ScreenLines2[off_to] = ScreenLines2[off_from];
5512 }
5513 /* When writing a single-width character over a double-width
5514 * character and at the end of the redrawn text, need to clear out
5515 * the right halve of the old character.
5516 * Also required when writing the right halve of a double-width
5517 * char over the left halve of an existing one. */
5518 if (has_mbyte && col + char_cells == endcol
5519 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005520 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005522 && (*mb_off2cells)(off_to, max_off_to) == 1
5523 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 clear_next = TRUE;
5525#endif
5526
5527 ScreenLines[off_to] = ScreenLines[off_from];
5528#ifdef FEAT_MBYTE
5529 if (enc_utf8)
5530 {
5531 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5532 if (ScreenLinesUC[off_from] != 0)
5533 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005534 int i;
5535
5536 for (i = 0; i < Screen_mco; ++i)
5537 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539 }
5540 if (char_cells == 2)
5541 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5542#endif
5543
5544#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005545 /* The bold trick makes a single column of pixels appear in the
5546 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 * character should be redrawn too. This happens for our own GUI
5548 * and for some xterms. */
5549 if (
5550# ifdef FEAT_GUI
5551 gui.in_use
5552# endif
5553# if defined(FEAT_GUI) && defined(UNIX)
5554 ||
5555# endif
5556# ifdef UNIX
5557 term_is_xterm
5558# endif
5559 )
5560 {
5561 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005562 if (hl > HL_ALL)
5563 hl = syn_attr2attr(hl);
5564 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 redraw_next = TRUE;
5566 }
5567#endif
5568 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5569#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005570 /* For simplicity set the attributes of second half of a
5571 * double-wide character equal to the first half. */
5572 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005574
5575 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 else
5578#endif
5579 screen_char(off_to, row, col + coloff);
5580 }
5581 else if ( p_wiv
5582#ifdef FEAT_GUI
5583 && !gui.in_use
5584#endif
5585 && col + coloff > 0)
5586 {
5587 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5588 {
5589 /*
5590 * Don't output stop-highlight when moving the cursor, it will
5591 * stop the highlighting when it should continue.
5592 */
5593 screen_attr = 0;
5594 }
5595 else if (screen_attr != 0)
5596 screen_stop_highlight();
5597 }
5598
5599 off_to += CHAR_CELLS;
5600 off_from += CHAR_CELLS;
5601 col += CHAR_CELLS;
5602 }
5603
5604#ifdef FEAT_MBYTE
5605 if (clear_next)
5606 {
5607 /* Clear the second half of a double-wide character of which the left
5608 * half was overwritten with a single-wide character. */
5609 ScreenLines[off_to] = ' ';
5610 if (enc_utf8)
5611 ScreenLinesUC[off_to] = 0;
5612 screen_char(off_to, row, col + coloff);
5613 }
5614#endif
5615
5616 if (clear_width > 0
5617#ifdef FEAT_RIGHTLEFT
5618 && !rlflag
5619#endif
5620 )
5621 {
5622#ifdef FEAT_GUI
5623 int startCol = col;
5624#endif
5625
5626 /* blank out the rest of the line */
5627 while (col < clear_width && ScreenLines[off_to] == ' '
5628 && ScreenAttrs[off_to] == 0
5629#ifdef FEAT_MBYTE
5630 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5631#endif
5632 )
5633 {
5634 ++off_to;
5635 ++col;
5636 }
5637 if (col < clear_width)
5638 {
5639#ifdef FEAT_GUI
5640 /*
5641 * In the GUI, clearing the rest of the line may leave pixels
5642 * behind if the first character cleared was bold. Some bold
5643 * fonts spill over the left. In this case we redraw the previous
5644 * character too. If we didn't skip any blanks above, then we
5645 * only redraw if the character wasn't already redrawn anyway.
5646 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005647 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
5649 hl = ScreenAttrs[off_to];
5650 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005651 {
5652 int prev_cells = 1;
5653# ifdef FEAT_MBYTE
5654 if (enc_utf8)
5655 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5656 * that its width is 2. */
5657 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5658 else if (enc_dbcs != 0)
5659 {
5660 /* find previous character by counting from first
5661 * column and get its width. */
5662 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005663 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005664
5665 while (off < off_to)
5666 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005667 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005668 off += prev_cells;
5669 }
5670 }
5671
5672 if (enc_dbcs != 0 && prev_cells > 1)
5673 screen_char_2(off_to - prev_cells, row,
5674 col + coloff - prev_cells);
5675 else
5676# endif
5677 screen_char(off_to - prev_cells, row,
5678 col + coloff - prev_cells);
5679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681#endif
5682 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5683 ' ', ' ', 0);
5684#ifdef FEAT_VERTSPLIT
5685 off_to += clear_width - col;
5686 col = clear_width;
5687#endif
5688 }
5689 }
5690
5691 if (clear_width > 0)
5692 {
5693#ifdef FEAT_VERTSPLIT
5694 /* For a window that's left of another, draw the separator char. */
5695 if (col + coloff < Columns)
5696 {
5697 int c;
5698
5699 c = fillchar_vsep(&hl);
5700 if (ScreenLines[off_to] != c
5701# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005702 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5703 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704# endif
5705 || ScreenAttrs[off_to] != hl)
5706 {
5707 ScreenLines[off_to] = c;
5708 ScreenAttrs[off_to] = hl;
5709# ifdef FEAT_MBYTE
5710 if (enc_utf8)
5711 {
5712 if (c >= 0x80)
5713 {
5714 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005715 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 else
5718 ScreenLinesUC[off_to] = 0;
5719 }
5720# endif
5721 screen_char(off_to, row, col + coloff);
5722 }
5723 }
5724 else
5725#endif
5726 LineWraps[row] = FALSE;
5727 }
5728}
5729
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005730#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005732 * Mirror text "str" for right-left displaying.
5733 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005735 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736rl_mirror(str)
5737 char_u *str;
5738{
5739 char_u *p1, *p2;
5740 int t;
5741
5742 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5743 {
5744 t = *p1;
5745 *p1 = *p2;
5746 *p2 = t;
5747 }
5748}
5749#endif
5750
5751#if defined(FEAT_WINDOWS) || defined(PROTO)
5752/*
5753 * mark all status lines for redraw; used after first :cd
5754 */
5755 void
5756status_redraw_all()
5757{
5758 win_T *wp;
5759
5760 for (wp = firstwin; wp; wp = wp->w_next)
5761 if (wp->w_status_height)
5762 {
5763 wp->w_redr_status = TRUE;
5764 redraw_later(VALID);
5765 }
5766}
5767
5768/*
5769 * mark all status lines of the current buffer for redraw
5770 */
5771 void
5772status_redraw_curbuf()
5773{
5774 win_T *wp;
5775
5776 for (wp = firstwin; wp; wp = wp->w_next)
5777 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5778 {
5779 wp->w_redr_status = TRUE;
5780 redraw_later(VALID);
5781 }
5782}
5783
5784/*
5785 * Redraw all status lines that need to be redrawn.
5786 */
5787 void
5788redraw_statuslines()
5789{
5790 win_T *wp;
5791
5792 for (wp = firstwin; wp; wp = wp->w_next)
5793 if (wp->w_redr_status)
5794 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005795 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005796 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797}
5798#endif
5799
5800#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5801/*
5802 * Redraw all status lines at the bottom of frame "frp".
5803 */
5804 void
5805win_redraw_last_status(frp)
5806 frame_T *frp;
5807{
5808 if (frp->fr_layout == FR_LEAF)
5809 frp->fr_win->w_redr_status = TRUE;
5810 else if (frp->fr_layout == FR_ROW)
5811 {
5812 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5813 win_redraw_last_status(frp);
5814 }
5815 else /* frp->fr_layout == FR_COL */
5816 {
5817 frp = frp->fr_child;
5818 while (frp->fr_next != NULL)
5819 frp = frp->fr_next;
5820 win_redraw_last_status(frp);
5821 }
5822}
5823#endif
5824
5825#ifdef FEAT_VERTSPLIT
5826/*
5827 * Draw the verticap separator right of window "wp" starting with line "row".
5828 */
5829 static void
5830draw_vsep_win(wp, row)
5831 win_T *wp;
5832 int row;
5833{
5834 int hl;
5835 int c;
5836
5837 if (wp->w_vsep_width)
5838 {
5839 /* draw the vertical separator right of this window */
5840 c = fillchar_vsep(&hl);
5841 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5842 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5843 c, ' ', hl);
5844 }
5845}
5846#endif
5847
5848#ifdef FEAT_WILDMENU
5849static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005850static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
5852/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005853 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 */
5855 static int
5856status_match_len(xp, s)
5857 expand_T *xp;
5858 char_u *s;
5859{
5860 int len = 0;
5861
5862#ifdef FEAT_MENU
5863 int emenu = (xp->xp_context == EXPAND_MENUS
5864 || xp->xp_context == EXPAND_MENUNAMES);
5865
5866 /* Check for menu separators - replace with '|'. */
5867 if (emenu && menu_is_separator(s))
5868 return 1;
5869#endif
5870
5871 while (*s != NUL)
5872 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005873 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005874 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005875 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
5877
5878 return len;
5879}
5880
5881/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005882 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005883 * These are backslashes used for escaping. Do show backslashes in help tags.
5884 */
5885 static int
5886skip_status_match_char(xp, s)
5887 expand_T *xp;
5888 char_u *s;
5889{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005890 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005891#ifdef FEAT_MENU
5892 || ((xp->xp_context == EXPAND_MENUS
5893 || xp->xp_context == EXPAND_MENUNAMES)
5894 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5895#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005896 )
5897 {
5898#ifndef BACKSLASH_IN_FILENAME
5899 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5900 return 2;
5901#endif
5902 return 1;
5903 }
5904 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005905}
5906
5907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 * Show wildchar matches in the status line.
5909 * Show at least the "match" item.
5910 * We start at item 'first_match' in the list and show all matches that fit.
5911 *
5912 * If inversion is possible we use it. Else '=' characters are used.
5913 */
5914 void
5915win_redr_status_matches(xp, num_matches, matches, match, showtail)
5916 expand_T *xp;
5917 int num_matches;
5918 char_u **matches; /* list of matches */
5919 int match;
5920 int showtail;
5921{
5922#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5923 int row;
5924 char_u *buf;
5925 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005926 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 int fillchar;
5928 int attr;
5929 int i;
5930 int highlight = TRUE;
5931 char_u *selstart = NULL;
5932 int selstart_col = 0;
5933 char_u *selend = NULL;
5934 static int first_match = 0;
5935 int add_left = FALSE;
5936 char_u *s;
5937#ifdef FEAT_MENU
5938 int emenu;
5939#endif
5940#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5941 int l;
5942#endif
5943
5944 if (matches == NULL) /* interrupted completion? */
5945 return;
5946
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005947#ifdef FEAT_MBYTE
5948 if (has_mbyte)
5949 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5950 else
5951#endif
5952 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 if (buf == NULL)
5954 return;
5955
5956 if (match == -1) /* don't show match but original text */
5957 {
5958 match = 0;
5959 highlight = FALSE;
5960 }
5961 /* count 1 for the ending ">" */
5962 clen = status_match_len(xp, L_MATCH(match)) + 3;
5963 if (match == 0)
5964 first_match = 0;
5965 else if (match < first_match)
5966 {
5967 /* jumping left, as far as we can go */
5968 first_match = match;
5969 add_left = TRUE;
5970 }
5971 else
5972 {
5973 /* check if match fits on the screen */
5974 for (i = first_match; i < match; ++i)
5975 clen += status_match_len(xp, L_MATCH(i)) + 2;
5976 if (first_match > 0)
5977 clen += 2;
5978 /* jumping right, put match at the left */
5979 if ((long)clen > Columns)
5980 {
5981 first_match = match;
5982 /* if showing the last match, we can add some on the left */
5983 clen = 2;
5984 for (i = match; i < num_matches; ++i)
5985 {
5986 clen += status_match_len(xp, L_MATCH(i)) + 2;
5987 if ((long)clen >= Columns)
5988 break;
5989 }
5990 if (i == num_matches)
5991 add_left = TRUE;
5992 }
5993 }
5994 if (add_left)
5995 while (first_match > 0)
5996 {
5997 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5998 if ((long)clen >= Columns)
5999 break;
6000 --first_match;
6001 }
6002
6003 fillchar = fillchar_status(&attr, TRUE);
6004
6005 if (first_match == 0)
6006 {
6007 *buf = NUL;
6008 len = 0;
6009 }
6010 else
6011 {
6012 STRCPY(buf, "< ");
6013 len = 2;
6014 }
6015 clen = len;
6016
6017 i = first_match;
6018 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6019 {
6020 if (i == match)
6021 {
6022 selstart = buf + len;
6023 selstart_col = clen;
6024 }
6025
6026 s = L_MATCH(i);
6027 /* Check for menu separators - replace with '|' */
6028#ifdef FEAT_MENU
6029 emenu = (xp->xp_context == EXPAND_MENUS
6030 || xp->xp_context == EXPAND_MENUNAMES);
6031 if (emenu && menu_is_separator(s))
6032 {
6033 STRCPY(buf + len, transchar('|'));
6034 l = (int)STRLEN(buf + len);
6035 len += l;
6036 clen += l;
6037 }
6038 else
6039#endif
6040 for ( ; *s != NUL; ++s)
6041 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006042 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 clen += ptr2cells(s);
6044#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006045 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {
6047 STRNCPY(buf + len, s, l);
6048 s += l - 1;
6049 len += l;
6050 }
6051 else
6052#endif
6053 {
6054 STRCPY(buf + len, transchar_byte(*s));
6055 len += (int)STRLEN(buf + len);
6056 }
6057 }
6058 if (i == match)
6059 selend = buf + len;
6060
6061 *(buf + len++) = ' ';
6062 *(buf + len++) = ' ';
6063 clen += 2;
6064 if (++i == num_matches)
6065 break;
6066 }
6067
6068 if (i != num_matches)
6069 {
6070 *(buf + len++) = '>';
6071 ++clen;
6072 }
6073
6074 buf[len] = NUL;
6075
6076 row = cmdline_row - 1;
6077 if (row >= 0)
6078 {
6079 if (wild_menu_showing == 0)
6080 {
6081 if (msg_scrolled > 0)
6082 {
6083 /* Put the wildmenu just above the command line. If there is
6084 * no room, scroll the screen one line up. */
6085 if (cmdline_row == Rows - 1)
6086 {
6087 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6088 ++msg_scrolled;
6089 }
6090 else
6091 {
6092 ++cmdline_row;
6093 ++row;
6094 }
6095 wild_menu_showing = WM_SCROLLED;
6096 }
6097 else
6098 {
6099 /* Create status line if needed by setting 'laststatus' to 2.
6100 * Set 'winminheight' to zero to avoid that the window is
6101 * resized. */
6102 if (lastwin->w_status_height == 0)
6103 {
6104 save_p_ls = p_ls;
6105 save_p_wmh = p_wmh;
6106 p_ls = 2;
6107 p_wmh = 0;
6108 last_status(FALSE);
6109 }
6110 wild_menu_showing = WM_SHOWN;
6111 }
6112 }
6113
6114 screen_puts(buf, row, 0, attr);
6115 if (selstart != NULL && highlight)
6116 {
6117 *selend = NUL;
6118 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6119 }
6120
6121 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6122 }
6123
6124#ifdef FEAT_VERTSPLIT
6125 win_redraw_last_status(topframe);
6126#else
6127 lastwin->w_redr_status = TRUE;
6128#endif
6129 vim_free(buf);
6130}
6131#endif
6132
6133#if defined(FEAT_WINDOWS) || defined(PROTO)
6134/*
6135 * Redraw the status line of window wp.
6136 *
6137 * If inversion is possible we use it. Else '=' characters are used.
6138 */
6139 void
6140win_redr_status(wp)
6141 win_T *wp;
6142{
6143 int row;
6144 char_u *p;
6145 int len;
6146 int fillchar;
6147 int attr;
6148 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006149 static int busy = FALSE;
6150
6151 /* It's possible to get here recursively when 'statusline' (indirectly)
6152 * invokes ":redrawstatus". Simply ignore the call then. */
6153 if (busy)
6154 return;
6155 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 wp->w_redr_status = FALSE;
6158 if (wp->w_status_height == 0)
6159 {
6160 /* no status line, can only be last window */
6161 redraw_cmdline = TRUE;
6162 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006163 else if (!redrawing()
6164#ifdef FEAT_INS_EXPAND
6165 /* don't update status line when popup menu is visible and may be
6166 * drawn over it */
6167 || pum_visible()
6168#endif
6169 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 {
6171 /* Don't redraw right now, do it later. */
6172 wp->w_redr_status = TRUE;
6173 }
6174#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006175 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 {
6177 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006178 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 }
6180#endif
6181 else
6182 {
6183 fillchar = fillchar_status(&attr, wp == curwin);
6184
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006185 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 p = NameBuff;
6187 len = (int)STRLEN(p);
6188
6189 if (wp->w_buffer->b_help
6190#ifdef FEAT_QUICKFIX
6191 || wp->w_p_pvw
6192#endif
6193 || bufIsChanged(wp->w_buffer)
6194 || wp->w_buffer->b_p_ro)
6195 *(p + len++) = ' ';
6196 if (wp->w_buffer->b_help)
6197 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006198 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 len += (int)STRLEN(p + len);
6200 }
6201#ifdef FEAT_QUICKFIX
6202 if (wp->w_p_pvw)
6203 {
6204 STRCPY(p + len, _("[Preview]"));
6205 len += (int)STRLEN(p + len);
6206 }
6207#endif
6208 if (bufIsChanged(wp->w_buffer))
6209 {
6210 STRCPY(p + len, "[+]");
6211 len += 3;
6212 }
6213 if (wp->w_buffer->b_p_ro)
6214 {
6215 STRCPY(p + len, "[RO]");
6216 len += 4;
6217 }
6218
6219#ifndef FEAT_VERTSPLIT
6220 this_ru_col = ru_col;
6221 if (this_ru_col < (Columns + 1) / 2)
6222 this_ru_col = (Columns + 1) / 2;
6223#else
6224 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6225 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6226 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6227 if (this_ru_col <= 1)
6228 {
6229 p = (char_u *)"<"; /* No room for file name! */
6230 len = 1;
6231 }
6232 else
6233#endif
6234#ifdef FEAT_MBYTE
6235 if (has_mbyte)
6236 {
6237 int clen = 0, i;
6238
6239 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006240 clen = mb_string2cells(p, -1);
6241
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 /* Find first character that will fit.
6243 * Going from start to end is much faster for DBCS. */
6244 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006245 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 clen -= (*mb_ptr2cells)(p + i);
6247 len = clen;
6248 if (i > 0)
6249 {
6250 p = p + i - 1;
6251 *p = '<';
6252 ++len;
6253 }
6254
6255 }
6256 else
6257#endif
6258 if (len > this_ru_col - 1)
6259 {
6260 p += len - (this_ru_col - 1);
6261 *p = '<';
6262 len = this_ru_col - 1;
6263 }
6264
6265 row = W_WINROW(wp) + wp->w_height;
6266 screen_puts(p, row, W_WINCOL(wp), attr);
6267 screen_fill(row, row + 1, len + W_WINCOL(wp),
6268 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6269
6270 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6271 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6272 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6273 - 1 + W_WINCOL(wp)), attr);
6274
6275#ifdef FEAT_CMDL_INFO
6276 win_redr_ruler(wp, TRUE);
6277#endif
6278 }
6279
6280#ifdef FEAT_VERTSPLIT
6281 /*
6282 * May need to draw the character below the vertical separator.
6283 */
6284 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6285 {
6286 if (stl_connected(wp))
6287 fillchar = fillchar_status(&attr, wp == curwin);
6288 else
6289 fillchar = fillchar_vsep(&attr);
6290 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6291 attr);
6292 }
6293#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006294 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295}
6296
Bram Moolenaar238a5642006-02-21 22:12:05 +00006297#ifdef FEAT_STL_OPT
6298/*
6299 * Redraw the status line according to 'statusline' and take care of any
6300 * errors encountered.
6301 */
6302 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006303redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006304 win_T *wp;
6305{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006306 static int entered = FALSE;
6307 int save_called_emsg = called_emsg;
6308
6309 /* When called recursively return. This can happen when the statusline
6310 * contains an expression that triggers a redraw. */
6311 if (entered)
6312 return;
6313 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006314
6315 called_emsg = FALSE;
6316 win_redr_custom(wp, FALSE);
6317 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006318 {
6319 /* When there is an error disable the statusline, otherwise the
6320 * display is messed up with errors and a redraw triggers the problem
6321 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006322 set_string_option_direct((char_u *)"statusline", -1,
6323 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006324 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006325 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006326 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006327 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006328}
6329#endif
6330
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331# ifdef FEAT_VERTSPLIT
6332/*
6333 * Return TRUE if the status line of window "wp" is connected to the status
6334 * line of the window right of it. If not, then it's a vertical separator.
6335 * Only call if (wp->w_vsep_width != 0).
6336 */
6337 int
6338stl_connected(wp)
6339 win_T *wp;
6340{
6341 frame_T *fr;
6342
6343 fr = wp->w_frame;
6344 while (fr->fr_parent != NULL)
6345 {
6346 if (fr->fr_parent->fr_layout == FR_COL)
6347 {
6348 if (fr->fr_next != NULL)
6349 break;
6350 }
6351 else
6352 {
6353 if (fr->fr_next != NULL)
6354 return TRUE;
6355 }
6356 fr = fr->fr_parent;
6357 }
6358 return FALSE;
6359}
6360# endif
6361
6362#endif /* FEAT_WINDOWS */
6363
6364#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6365/*
6366 * Get the value to show for the language mappings, active 'keymap'.
6367 */
6368 int
6369get_keymap_str(wp, buf, len)
6370 win_T *wp;
6371 char_u *buf; /* buffer for the result */
6372 int len; /* length of buffer */
6373{
6374 char_u *p;
6375
6376 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6377 return FALSE;
6378
6379 {
6380#ifdef FEAT_EVAL
6381 buf_T *old_curbuf = curbuf;
6382 win_T *old_curwin = curwin;
6383 char_u *s;
6384
6385 curbuf = wp->w_buffer;
6386 curwin = wp;
6387 STRCPY(buf, "b:keymap_name"); /* must be writable */
6388 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006389 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 --emsg_skip;
6391 curbuf = old_curbuf;
6392 curwin = old_curwin;
6393 if (p == NULL || *p == NUL)
6394#endif
6395 {
6396#ifdef FEAT_KEYMAP
6397 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6398 p = wp->w_buffer->b_p_keymap;
6399 else
6400#endif
6401 p = (char_u *)"lang";
6402 }
6403 if ((int)(STRLEN(p) + 3) < len)
6404 sprintf((char *)buf, "<%s>", p);
6405 else
6406 buf[0] = NUL;
6407#ifdef FEAT_EVAL
6408 vim_free(s);
6409#endif
6410 }
6411 return buf[0] != NUL;
6412}
6413#endif
6414
6415#if defined(FEAT_STL_OPT) || defined(PROTO)
6416/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006417 * Redraw the status line or ruler of window "wp".
6418 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 */
6420 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006421win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006423 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
6425 int attr;
6426 int curattr;
6427 int row;
6428 int col = 0;
6429 int maxwidth;
6430 int width;
6431 int n;
6432 int len;
6433 int fillchar;
6434 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006435 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006437 struct stl_hlrec hltab[STL_MAX_ITEM];
6438 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006439 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006440 win_T *ewp;
6441 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
6443 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006444 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006446 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006447 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006448 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006449 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006450 attr = hl_attr(HLF_TPF);
6451 maxwidth = Columns;
6452# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006453 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006456 else
6457 {
6458 row = W_WINROW(wp) + wp->w_height;
6459 fillchar = fillchar_status(&attr, wp == curwin);
6460 maxwidth = W_WIDTH(wp);
6461
6462 if (draw_ruler)
6463 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006464 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006465 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006466 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006467 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006468 if (*++stl == '-')
6469 stl++;
6470 if (atoi((char *)stl))
6471 while (VIM_ISDIGIT(*stl))
6472 stl++;
6473 if (*stl++ != '(')
6474 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006475 }
6476#ifdef FEAT_VERTSPLIT
6477 col = ru_col - (Columns - W_WIDTH(wp));
6478 if (col < (W_WIDTH(wp) + 1) / 2)
6479 col = (W_WIDTH(wp) + 1) / 2;
6480#else
6481 col = ru_col;
6482 if (col > (Columns + 1) / 2)
6483 col = (Columns + 1) / 2;
6484#endif
6485 maxwidth = W_WIDTH(wp) - col;
6486#ifdef FEAT_WINDOWS
6487 if (!wp->w_status_height)
6488#endif
6489 {
6490 row = Rows - 1;
6491 --maxwidth; /* writing in last column may cause scrolling */
6492 fillchar = ' ';
6493 attr = 0;
6494 }
6495
6496# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006497 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006498# endif
6499 }
6500 else
6501 {
6502 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006503 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006504 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006505 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006506# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006507 use_sandbox = was_set_insecurely((char_u *)"statusline",
6508 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006509# endif
6510 }
6511
6512#ifdef FEAT_VERTSPLIT
6513 col += W_WINCOL(wp);
6514#endif
6515 }
6516
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 if (maxwidth <= 0)
6518 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519
Bram Moolenaar61452852011-02-01 18:01:11 +01006520 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6521 * the cursor away and back. */
6522 ewp = wp == NULL ? curwin : wp;
6523 p_crb_save = ewp->w_p_crb;
6524 ewp->w_p_crb = FALSE;
6525
Bram Moolenaar362f3562009-11-03 16:20:34 +00006526 /* Make a copy, because the statusline may include a function call that
6527 * might change the option value and free the memory. */
6528 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006529 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006530 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006531 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006532 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006533 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006535 /* Make all characters printable. */
6536 p = transstr(buf);
6537 if (p != NULL)
6538 {
6539 vim_strncpy(buf, p, sizeof(buf) - 1);
6540 vim_free(p);
6541 }
6542
6543 /* fill up with "fillchar" */
6544 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006545 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {
6547#ifdef FEAT_MBYTE
6548 len += (*mb_char2bytes)(fillchar, buf + len);
6549#else
6550 buf[len++] = fillchar;
6551#endif
6552 ++width;
6553 }
6554 buf[len] = NUL;
6555
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006556 /*
6557 * Draw each snippet with the specified highlighting.
6558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 curattr = attr;
6560 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006561 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006563 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 screen_puts_len(p, len, row, col, curattr);
6565 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006566 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006568 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006570 else if (hltab[n].userhl < 0)
6571 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006573 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006574 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575#endif
6576 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006577 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 }
6579 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006580
6581 if (wp == NULL)
6582 {
6583 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6584 col = 0;
6585 len = 0;
6586 p = buf;
6587 fillchar = 0;
6588 for (n = 0; tabtab[n].start != NULL; n++)
6589 {
6590 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6591 while (col < len)
6592 TabPageIdxs[col++] = fillchar;
6593 p = tabtab[n].start;
6594 fillchar = tabtab[n].userhl;
6595 }
6596 while (col < Columns)
6597 TabPageIdxs[col++] = fillchar;
6598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599}
6600
6601#endif /* FEAT_STL_OPT */
6602
6603/*
6604 * Output a single character directly to the screen and update ScreenLines.
6605 */
6606 void
6607screen_putchar(c, row, col, attr)
6608 int c;
6609 int row, col;
6610 int attr;
6611{
6612#ifdef FEAT_MBYTE
6613 char_u buf[MB_MAXBYTES + 1];
6614
6615 buf[(*mb_char2bytes)(c, buf)] = NUL;
6616#else
6617 char_u buf[2];
6618
6619 buf[0] = c;
6620 buf[1] = NUL;
6621#endif
6622 screen_puts(buf, row, col, attr);
6623}
6624
6625/*
6626 * Get a single character directly from ScreenLines into "bytes[]".
6627 * Also return its attribute in *attrp;
6628 */
6629 void
6630screen_getbytes(row, col, bytes, attrp)
6631 int row, col;
6632 char_u *bytes;
6633 int *attrp;
6634{
6635 unsigned off;
6636
6637 /* safety check */
6638 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6639 {
6640 off = LineOffset[row] + col;
6641 *attrp = ScreenAttrs[off];
6642 bytes[0] = ScreenLines[off];
6643 bytes[1] = NUL;
6644
6645#ifdef FEAT_MBYTE
6646 if (enc_utf8 && ScreenLinesUC[off] != 0)
6647 bytes[utfc_char2bytes(off, bytes)] = NUL;
6648 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6649 {
6650 bytes[0] = ScreenLines[off];
6651 bytes[1] = ScreenLines2[off];
6652 bytes[2] = NUL;
6653 }
6654 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6655 {
6656 bytes[1] = ScreenLines[off + 1];
6657 bytes[2] = NUL;
6658 }
6659#endif
6660 }
6661}
6662
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006663#ifdef FEAT_MBYTE
6664static int screen_comp_differs __ARGS((int, int*));
6665
6666/*
6667 * Return TRUE if composing characters for screen posn "off" differs from
6668 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006669 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006670 */
6671 static int
6672screen_comp_differs(off, u8cc)
6673 int off;
6674 int *u8cc;
6675{
6676 int i;
6677
6678 for (i = 0; i < Screen_mco; ++i)
6679 {
6680 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6681 return TRUE;
6682 if (u8cc[i] == 0)
6683 break;
6684 }
6685 return FALSE;
6686}
6687#endif
6688
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689/*
6690 * Put string '*text' on the screen at position 'row' and 'col', with
6691 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6692 * Note: only outputs within one row, message is truncated at screen boundary!
6693 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6694 */
6695 void
6696screen_puts(text, row, col, attr)
6697 char_u *text;
6698 int row;
6699 int col;
6700 int attr;
6701{
6702 screen_puts_len(text, -1, row, col, attr);
6703}
6704
6705/*
6706 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6707 * a NUL.
6708 */
6709 void
6710screen_puts_len(text, len, row, col, attr)
6711 char_u *text;
6712 int len;
6713 int row;
6714 int col;
6715 int attr;
6716{
6717 unsigned off;
6718 char_u *ptr = text;
6719 int c;
6720#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006721 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 int mbyte_blen = 1;
6723 int mbyte_cells = 1;
6724 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006725 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 int clear_next_cell = FALSE;
6727# ifdef FEAT_ARABIC
6728 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006729 int pc, nc, nc1;
6730 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731# endif
6732#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006733#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6734 int force_redraw_this;
6735 int force_redraw_next = FALSE;
6736#endif
6737 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
6739 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6740 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006741 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
Bram Moolenaarc236c162008-07-13 17:41:49 +00006743#ifdef FEAT_MBYTE
6744 /* When drawing over the right halve of a double-wide char clear out the
6745 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006746 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006747# ifdef FEAT_GUI
6748 && !gui.in_use
6749# endif
6750 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006751 {
6752 ScreenLines[off - 1] = ' ';
6753 ScreenAttrs[off - 1] = 0;
6754 if (enc_utf8)
6755 {
6756 ScreenLinesUC[off - 1] = 0;
6757 ScreenLinesC[0][off - 1] = 0;
6758 }
6759 /* redraw the previous cell, make it empty */
6760 screen_char(off - 1, row, col - 1);
6761 /* force the cell at "col" to be redrawn */
6762 force_redraw_next = TRUE;
6763 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006764#endif
6765
Bram Moolenaar367329b2007-08-30 11:53:22 +00006766#ifdef FEAT_MBYTE
6767 max_off = LineOffset[row] + screen_Columns;
6768#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006769 while (col < screen_Columns
6770 && (len < 0 || (int)(ptr - text) < len)
6771 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
6773 c = *ptr;
6774#ifdef FEAT_MBYTE
6775 /* check if this is the first byte of a multibyte */
6776 if (has_mbyte)
6777 {
6778 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006779 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006781 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6783 mbyte_cells = 1;
6784 else if (enc_dbcs != 0)
6785 mbyte_cells = mbyte_blen;
6786 else /* enc_utf8 */
6787 {
6788 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006789 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 (int)((text + len) - ptr));
6791 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006792 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006794# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 /* Non-BMP character: display as ? or fullwidth ?. */
6796 if (u8c >= 0x10000)
6797 {
6798 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6799 if (attr == 0)
6800 attr = hl_attr(HLF_8);
6801 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803# ifdef FEAT_ARABIC
6804 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6805 {
6806 /* Do Arabic shaping. */
6807 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6808 {
6809 /* Past end of string to be displayed. */
6810 nc = NUL;
6811 nc1 = NUL;
6812 }
6813 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006814 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006815 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6816 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006817 nc1 = pcc[0];
6818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 pc = prev_c;
6820 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006821 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 }
6823 else
6824 prev_c = u8c;
6825# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006826 if (col + mbyte_cells > screen_Columns)
6827 {
6828 /* Only 1 cell left, but character requires 2 cells:
6829 * display a '>' in the last column to avoid wrapping. */
6830 c = '>';
6831 mbyte_cells = 1;
6832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
6834 }
6835#endif
6836
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006837#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6838 force_redraw_this = force_redraw_next;
6839 force_redraw_next = FALSE;
6840#endif
6841
6842 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843#ifdef FEAT_MBYTE
6844 || (mbyte_cells == 2
6845 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6846 || (enc_dbcs == DBCS_JPNU
6847 && c == 0x8e
6848 && ScreenLines2[off] != ptr[1])
6849 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006850 && (ScreenLinesUC[off] !=
6851 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6852 || (ScreenLinesUC[off] != 0
6853 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854#endif
6855 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006856 || exmode_active;
6857
6858 if (need_redraw
6859#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6860 || force_redraw_this
6861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 )
6863 {
6864#if defined(FEAT_GUI) || defined(UNIX)
6865 /* The bold trick makes a single row of pixels appear in the next
6866 * character. When a bold character is removed, the next
6867 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006868 * and for some xterms. */
6869 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870# ifdef FEAT_GUI
6871 gui.in_use
6872# endif
6873# if defined(FEAT_GUI) && defined(UNIX)
6874 ||
6875# endif
6876# ifdef UNIX
6877 term_is_xterm
6878# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006879 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006881 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006883 if (n > HL_ALL)
6884 n = syn_attr2attr(n);
6885 if (n & HL_BOLD)
6886 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888#endif
6889#ifdef FEAT_MBYTE
6890 /* When at the end of the text and overwriting a two-cell
6891 * character with a one-cell character, need to clear the next
6892 * cell. Also when overwriting the left halve of a two-cell char
6893 * with the right halve of a two-cell char. Do this only once
6894 * (mb_off2cells() may return 2 on the right halve). */
6895 if (clear_next_cell)
6896 clear_next_cell = FALSE;
6897 else if (has_mbyte
6898 && (len < 0 ? ptr[mbyte_blen] == NUL
6899 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006900 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006902 && (*mb_off2cells)(off, max_off) == 1
6903 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 clear_next_cell = TRUE;
6905
6906 /* Make sure we never leave a second byte of a double-byte behind,
6907 * it confuses mb_off2cells(). */
6908 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006909 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006911 && (*mb_off2cells)(off, max_off) == 1
6912 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 ScreenLines[off + mbyte_blen] = 0;
6914#endif
6915 ScreenLines[off] = c;
6916 ScreenAttrs[off] = attr;
6917#ifdef FEAT_MBYTE
6918 if (enc_utf8)
6919 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006920 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 ScreenLinesUC[off] = 0;
6922 else
6923 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006924 int i;
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006927 for (i = 0; i < Screen_mco; ++i)
6928 {
6929 ScreenLinesC[i][off] = u8cc[i];
6930 if (u8cc[i] == 0)
6931 break;
6932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 }
6934 if (mbyte_cells == 2)
6935 {
6936 ScreenLines[off + 1] = 0;
6937 ScreenAttrs[off + 1] = attr;
6938 }
6939 screen_char(off, row, col);
6940 }
6941 else if (mbyte_cells == 2)
6942 {
6943 ScreenLines[off + 1] = ptr[1];
6944 ScreenAttrs[off + 1] = attr;
6945 screen_char_2(off, row, col);
6946 }
6947 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6948 {
6949 ScreenLines2[off] = ptr[1];
6950 screen_char(off, row, col);
6951 }
6952 else
6953#endif
6954 screen_char(off, row, col);
6955 }
6956#ifdef FEAT_MBYTE
6957 if (has_mbyte)
6958 {
6959 off += mbyte_cells;
6960 col += mbyte_cells;
6961 ptr += mbyte_blen;
6962 if (clear_next_cell)
6963 ptr = (char_u *)" ";
6964 }
6965 else
6966#endif
6967 {
6968 ++off;
6969 ++col;
6970 ++ptr;
6971 }
6972 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006973
6974#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6975 /* If we detected the next character needs to be redrawn, but the text
6976 * doesn't extend up to there, update the character here. */
6977 if (force_redraw_next && col < screen_Columns)
6978 {
6979# ifdef FEAT_MBYTE
6980 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6981 screen_char_2(off, row, col);
6982 else
6983# endif
6984 screen_char(off, row, col);
6985 }
6986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987}
6988
6989#ifdef FEAT_SEARCH_EXTRA
6990/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006991 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 */
6993 static void
6994start_search_hl()
6995{
6996 if (p_hls && !no_hlsearch)
6997 {
6998 last_pat_prog(&search_hl.rm);
6999 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007000# ifdef FEAT_RELTIME
7001 /* Set the time limit to 'redrawtime'. */
7002 profile_setlimit(p_rdt, &search_hl.tm);
7003# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 }
7005}
7006
7007/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007008 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 */
7010 static void
7011end_search_hl()
7012{
7013 if (search_hl.rm.regprog != NULL)
7014 {
7015 vim_free(search_hl.rm.regprog);
7016 search_hl.rm.regprog = NULL;
7017 }
7018}
7019
7020/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007021 * Init for calling prepare_search_hl().
7022 */
7023 static void
7024init_search_hl(wp)
7025 win_T *wp;
7026{
7027 matchitem_T *cur;
7028
7029 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7030 * match */
7031 cur = wp->w_match_head;
7032 while (cur != NULL)
7033 {
7034 cur->hl.rm = cur->match;
7035 if (cur->hlg_id == 0)
7036 cur->hl.attr = 0;
7037 else
7038 cur->hl.attr = syn_id2attr(cur->hlg_id);
7039 cur->hl.buf = wp->w_buffer;
7040 cur->hl.lnum = 0;
7041 cur->hl.first_lnum = 0;
7042# ifdef FEAT_RELTIME
7043 /* Set the time limit to 'redrawtime'. */
7044 profile_setlimit(p_rdt, &(cur->hl.tm));
7045# endif
7046 cur = cur->next;
7047 }
7048 search_hl.buf = wp->w_buffer;
7049 search_hl.lnum = 0;
7050 search_hl.first_lnum = 0;
7051 /* time limit is set at the toplevel, for all windows */
7052}
7053
7054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 * Advance to the match in window "wp" line "lnum" or past it.
7056 */
7057 static void
7058prepare_search_hl(wp, lnum)
7059 win_T *wp;
7060 linenr_T lnum;
7061{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007062 matchitem_T *cur; /* points to the match list */
7063 match_T *shl; /* points to search_hl or a match */
7064 int shl_flag; /* flag to indicate whether search_hl
7065 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 int n;
7067
7068 /*
7069 * When using a multi-line pattern, start searching at the top
7070 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007071 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007073 cur = wp->w_match_head;
7074 shl_flag = FALSE;
7075 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007077 if (shl_flag == FALSE)
7078 {
7079 shl = &search_hl;
7080 shl_flag = TRUE;
7081 }
7082 else
7083 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 if (shl->rm.regprog != NULL
7085 && shl->lnum == 0
7086 && re_multiline(shl->rm.regprog))
7087 {
7088 if (shl->first_lnum == 0)
7089 {
7090# ifdef FEAT_FOLDING
7091 for (shl->first_lnum = lnum;
7092 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7093 if (hasFoldingWin(wp, shl->first_lnum - 1,
7094 NULL, NULL, TRUE, NULL))
7095 break;
7096# else
7097 shl->first_lnum = wp->w_topline;
7098# endif
7099 }
7100 n = 0;
7101 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7102 {
7103 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7104 if (shl->lnum != 0)
7105 {
7106 shl->first_lnum = shl->lnum
7107 + shl->rm.endpos[0].lnum
7108 - shl->rm.startpos[0].lnum;
7109 n = shl->rm.endpos[0].col;
7110 }
7111 else
7112 {
7113 ++shl->first_lnum;
7114 n = 0;
7115 }
7116 }
7117 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007118 if (shl != &search_hl && cur != NULL)
7119 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 }
7121}
7122
7123/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007124 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 * Uses shl->buf.
7126 * Sets shl->lnum and shl->rm contents.
7127 * Note: Assumes a previous match is always before "lnum", unless
7128 * shl->lnum is zero.
7129 * Careful: Any pointers for buffer lines will become invalid.
7130 */
7131 static void
7132next_search_hl(win, shl, lnum, mincol)
7133 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007134 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 linenr_T lnum;
7136 colnr_T mincol; /* minimal column for a match */
7137{
7138 linenr_T l;
7139 colnr_T matchcol;
7140 long nmatched;
7141
7142 if (shl->lnum != 0)
7143 {
7144 /* Check for three situations:
7145 * 1. If the "lnum" is below a previous match, start a new search.
7146 * 2. If the previous match includes "mincol", use it.
7147 * 3. Continue after the previous match.
7148 */
7149 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7150 if (lnum > l)
7151 shl->lnum = 0;
7152 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7153 return;
7154 }
7155
7156 /*
7157 * Repeat searching for a match until one is found that includes "mincol"
7158 * or none is found in this line.
7159 */
7160 called_emsg = FALSE;
7161 for (;;)
7162 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007163#ifdef FEAT_RELTIME
7164 /* Stop searching after passing the time limit. */
7165 if (profile_passed_limit(&(shl->tm)))
7166 {
7167 shl->lnum = 0; /* no match found in time */
7168 break;
7169 }
7170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 /* Three situations:
7172 * 1. No useful previous match: search from start of line.
7173 * 2. Not Vi compatible or empty match: continue at next character.
7174 * Break the loop if this is beyond the end of the line.
7175 * 3. Vi compatible searching: continue at end of previous match.
7176 */
7177 if (shl->lnum == 0)
7178 matchcol = 0;
7179 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7180 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007181 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007183 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007184
7185 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007186 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007187 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007189 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 shl->lnum = 0;
7191 break;
7192 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007193#ifdef FEAT_MBYTE
7194 if (has_mbyte)
7195 matchcol += mb_ptr2len(ml);
7196 else
7197#endif
7198 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 }
7200 else
7201 matchcol = shl->rm.endpos[0].col;
7202
7203 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007204 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7205#ifdef FEAT_RELTIME
7206 &(shl->tm)
7207#else
7208 NULL
7209#endif
7210 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007211 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {
7213 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007214 if (shl == &search_hl)
7215 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007216 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007217 vim_free(shl->rm.regprog);
7218 no_hlsearch = TRUE;
7219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007221 shl->lnum = 0;
7222 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 break;
7224 }
7225 if (nmatched == 0)
7226 {
7227 shl->lnum = 0; /* no match found */
7228 break;
7229 }
7230 if (shl->rm.startpos[0].lnum > 0
7231 || shl->rm.startpos[0].col >= mincol
7232 || nmatched > 1
7233 || shl->rm.endpos[0].col > mincol)
7234 {
7235 shl->lnum += shl->rm.startpos[0].lnum;
7236 break; /* useful match found */
7237 }
7238 }
7239}
7240#endif
7241
7242 static void
7243screen_start_highlight(attr)
7244 int attr;
7245{
7246 attrentry_T *aep = NULL;
7247
7248 screen_attr = attr;
7249 if (full_screen
7250#ifdef WIN3264
7251 && termcap_active
7252#endif
7253 )
7254 {
7255#ifdef FEAT_GUI
7256 if (gui.in_use)
7257 {
7258 char buf[20];
7259
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007260 /* The GUI handles this internally. */
7261 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 OUT_STR(buf);
7263 }
7264 else
7265#endif
7266 {
7267 if (attr > HL_ALL) /* special HL attr. */
7268 {
7269 if (t_colors > 1)
7270 aep = syn_cterm_attr2entry(attr);
7271 else
7272 aep = syn_term_attr2entry(attr);
7273 if (aep == NULL) /* did ":syntax clear" */
7274 attr = 0;
7275 else
7276 attr = aep->ae_attr;
7277 }
7278 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7279 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007280 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7281 && cterm_normal_fg_bold)
7282 /* If the Normal FG color has BOLD attribute and the new HL
7283 * has a FG color defined, clear BOLD. */
7284 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7286 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007287 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7288 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 out_str(T_US);
7290 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7291 out_str(T_CZH);
7292 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7293 out_str(T_MR);
7294
7295 /*
7296 * Output the color or start string after bold etc., in case the
7297 * bold etc. override the color setting.
7298 */
7299 if (aep != NULL)
7300 {
7301 if (t_colors > 1)
7302 {
7303 if (aep->ae_u.cterm.fg_color)
7304 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7305 if (aep->ae_u.cterm.bg_color)
7306 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7307 }
7308 else
7309 {
7310 if (aep->ae_u.term.start != NULL)
7311 out_str(aep->ae_u.term.start);
7312 }
7313 }
7314 }
7315 }
7316}
7317
7318 void
7319screen_stop_highlight()
7320{
7321 int do_ME = FALSE; /* output T_ME code */
7322
7323 if (screen_attr != 0
7324#ifdef WIN3264
7325 && termcap_active
7326#endif
7327 )
7328 {
7329#ifdef FEAT_GUI
7330 if (gui.in_use)
7331 {
7332 char buf[20];
7333
7334 /* use internal GUI code */
7335 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7336 OUT_STR(buf);
7337 }
7338 else
7339#endif
7340 {
7341 if (screen_attr > HL_ALL) /* special HL attr. */
7342 {
7343 attrentry_T *aep;
7344
7345 if (t_colors > 1)
7346 {
7347 /*
7348 * Assume that t_me restores the original colors!
7349 */
7350 aep = syn_cterm_attr2entry(screen_attr);
7351 if (aep != NULL && (aep->ae_u.cterm.fg_color
7352 || aep->ae_u.cterm.bg_color))
7353 do_ME = TRUE;
7354 }
7355 else
7356 {
7357 aep = syn_term_attr2entry(screen_attr);
7358 if (aep != NULL && aep->ae_u.term.stop != NULL)
7359 {
7360 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7361 do_ME = TRUE;
7362 else
7363 out_str(aep->ae_u.term.stop);
7364 }
7365 }
7366 if (aep == NULL) /* did ":syntax clear" */
7367 screen_attr = 0;
7368 else
7369 screen_attr = aep->ae_attr;
7370 }
7371
7372 /*
7373 * Often all ending-codes are equal to T_ME. Avoid outputting the
7374 * same sequence several times.
7375 */
7376 if (screen_attr & HL_STANDOUT)
7377 {
7378 if (STRCMP(T_SE, T_ME) == 0)
7379 do_ME = TRUE;
7380 else
7381 out_str(T_SE);
7382 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007383 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
7385 if (STRCMP(T_UE, T_ME) == 0)
7386 do_ME = TRUE;
7387 else
7388 out_str(T_UE);
7389 }
7390 if (screen_attr & HL_ITALIC)
7391 {
7392 if (STRCMP(T_CZR, T_ME) == 0)
7393 do_ME = TRUE;
7394 else
7395 out_str(T_CZR);
7396 }
7397 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7398 out_str(T_ME);
7399
7400 if (t_colors > 1)
7401 {
7402 /* set Normal cterm colors */
7403 if (cterm_normal_fg_color != 0)
7404 term_fg_color(cterm_normal_fg_color - 1);
7405 if (cterm_normal_bg_color != 0)
7406 term_bg_color(cterm_normal_bg_color - 1);
7407 if (cterm_normal_fg_bold)
7408 out_str(T_MD);
7409 }
7410 }
7411 }
7412 screen_attr = 0;
7413}
7414
7415/*
7416 * Reset the colors for a cterm. Used when leaving Vim.
7417 * The machine specific code may override this again.
7418 */
7419 void
7420reset_cterm_colors()
7421{
7422 if (t_colors > 1)
7423 {
7424 /* set Normal cterm colors */
7425 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7426 {
7427 out_str(T_OP);
7428 screen_attr = -1;
7429 }
7430 if (cterm_normal_fg_bold)
7431 {
7432 out_str(T_ME);
7433 screen_attr = -1;
7434 }
7435 }
7436}
7437
7438/*
7439 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7440 * using the attributes from ScreenAttrs["off"].
7441 */
7442 static void
7443screen_char(off, row, col)
7444 unsigned off;
7445 int row;
7446 int col;
7447{
7448 int attr;
7449
7450 /* Check for illegal values, just in case (could happen just after
7451 * resizing). */
7452 if (row >= screen_Rows || col >= screen_Columns)
7453 return;
7454
7455 /* Outputting the last character on the screen may scrollup the screen.
7456 * Don't to it! Mark the character invalid (update it when scrolled up) */
7457 if (row == screen_Rows - 1 && col == screen_Columns - 1
7458#ifdef FEAT_RIGHTLEFT
7459 /* account for first command-line character in rightleft mode */
7460 && !cmdmsg_rl
7461#endif
7462 )
7463 {
7464 ScreenAttrs[off] = (sattr_T)-1;
7465 return;
7466 }
7467
7468 /*
7469 * Stop highlighting first, so it's easier to move the cursor.
7470 */
7471#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7472 if (screen_char_attr != 0)
7473 attr = screen_char_attr;
7474 else
7475#endif
7476 attr = ScreenAttrs[off];
7477 if (screen_attr != attr)
7478 screen_stop_highlight();
7479
7480 windgoto(row, col);
7481
7482 if (screen_attr != attr)
7483 screen_start_highlight(attr);
7484
7485#ifdef FEAT_MBYTE
7486 if (enc_utf8 && ScreenLinesUC[off] != 0)
7487 {
7488 char_u buf[MB_MAXBYTES + 1];
7489
7490 /* Convert UTF-8 character to bytes and write it. */
7491
7492 buf[utfc_char2bytes(off, buf)] = NUL;
7493
7494 out_str(buf);
7495 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7496 ++screen_cur_col;
7497 }
7498 else
7499#endif
7500 {
7501#ifdef FEAT_MBYTE
7502 out_flush_check();
7503#endif
7504 out_char(ScreenLines[off]);
7505#ifdef FEAT_MBYTE
7506 /* double-byte character in single-width cell */
7507 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7508 out_char(ScreenLines2[off]);
7509#endif
7510 }
7511
7512 screen_cur_col++;
7513}
7514
7515#ifdef FEAT_MBYTE
7516
7517/*
7518 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7519 * on the screen at position 'row' and 'col'.
7520 * The attributes of the first byte is used for all. This is required to
7521 * output the two bytes of a double-byte character with nothing in between.
7522 */
7523 static void
7524screen_char_2(off, row, col)
7525 unsigned off;
7526 int row;
7527 int col;
7528{
7529 /* Check for illegal values (could be wrong when screen was resized). */
7530 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7531 return;
7532
7533 /* Outputting the last character on the screen may scrollup the screen.
7534 * Don't to it! Mark the character invalid (update it when scrolled up) */
7535 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7536 {
7537 ScreenAttrs[off] = (sattr_T)-1;
7538 return;
7539 }
7540
7541 /* Output the first byte normally (positions the cursor), then write the
7542 * second byte directly. */
7543 screen_char(off, row, col);
7544 out_char(ScreenLines[off + 1]);
7545 ++screen_cur_col;
7546}
7547#endif
7548
7549#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7550/*
7551 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7552 * This uses the contents of ScreenLines[] and doesn't change it.
7553 */
7554 void
7555screen_draw_rectangle(row, col, height, width, invert)
7556 int row;
7557 int col;
7558 int height;
7559 int width;
7560 int invert;
7561{
7562 int r, c;
7563 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007564#ifdef FEAT_MBYTE
7565 int max_off;
7566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007568 /* Can't use ScreenLines unless initialized */
7569 if (ScreenLines == NULL)
7570 return;
7571
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 if (invert)
7573 screen_char_attr = HL_INVERSE;
7574 for (r = row; r < row + height; ++r)
7575 {
7576 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007577#ifdef FEAT_MBYTE
7578 max_off = off + screen_Columns;
7579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 for (c = col; c < col + width; ++c)
7581 {
7582#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007583 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 {
7585 screen_char_2(off + c, r, c);
7586 ++c;
7587 }
7588 else
7589#endif
7590 {
7591 screen_char(off + c, r, c);
7592#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007593 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 ++c;
7595#endif
7596 }
7597 }
7598 }
7599 screen_char_attr = 0;
7600}
7601#endif
7602
7603#ifdef FEAT_VERTSPLIT
7604/*
7605 * Redraw the characters for a vertically split window.
7606 */
7607 static void
7608redraw_block(row, end, wp)
7609 int row;
7610 int end;
7611 win_T *wp;
7612{
7613 int col;
7614 int width;
7615
7616# ifdef FEAT_CLIPBOARD
7617 clip_may_clear_selection(row, end - 1);
7618# endif
7619
7620 if (wp == NULL)
7621 {
7622 col = 0;
7623 width = Columns;
7624 }
7625 else
7626 {
7627 col = wp->w_wincol;
7628 width = wp->w_width;
7629 }
7630 screen_draw_rectangle(row, col, end - row, width, FALSE);
7631}
7632#endif
7633
7634/*
7635 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7636 * with character 'c1' in first column followed by 'c2' in the other columns.
7637 * Use attributes 'attr'.
7638 */
7639 void
7640screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7641 int start_row, end_row;
7642 int start_col, end_col;
7643 int c1, c2;
7644 int attr;
7645{
7646 int row;
7647 int col;
7648 int off;
7649 int end_off;
7650 int did_delete;
7651 int c;
7652 int norm_term;
7653#if defined(FEAT_GUI) || defined(UNIX)
7654 int force_next = FALSE;
7655#endif
7656
7657 if (end_row > screen_Rows) /* safety check */
7658 end_row = screen_Rows;
7659 if (end_col > screen_Columns) /* safety check */
7660 end_col = screen_Columns;
7661 if (ScreenLines == NULL
7662 || start_row >= end_row
7663 || start_col >= end_col) /* nothing to do */
7664 return;
7665
7666 /* it's a "normal" terminal when not in a GUI or cterm */
7667 norm_term = (
7668#ifdef FEAT_GUI
7669 !gui.in_use &&
7670#endif
7671 t_colors <= 1);
7672 for (row = start_row; row < end_row; ++row)
7673 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007674#ifdef FEAT_MBYTE
7675 if (has_mbyte
7676# ifdef FEAT_GUI
7677 && !gui.in_use
7678# endif
7679 )
7680 {
7681 /* When drawing over the right halve of a double-wide char clear
7682 * out the left halve. When drawing over the left halve of a
7683 * double wide-char clear out the right halve. Only needed in a
7684 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007685 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007686 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007687 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007688 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007689 }
7690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 /*
7692 * Try to use delete-line termcap code, when no attributes or in a
7693 * "normal" terminal, where a bold/italic space is just a
7694 * space.
7695 */
7696 did_delete = FALSE;
7697 if (c2 == ' '
7698 && end_col == Columns
7699 && can_clear(T_CE)
7700 && (attr == 0
7701 || (norm_term
7702 && attr <= HL_ALL
7703 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7704 {
7705 /*
7706 * check if we really need to clear something
7707 */
7708 col = start_col;
7709 if (c1 != ' ') /* don't clear first char */
7710 ++col;
7711
7712 off = LineOffset[row] + col;
7713 end_off = LineOffset[row] + end_col;
7714
7715 /* skip blanks (used often, keep it fast!) */
7716#ifdef FEAT_MBYTE
7717 if (enc_utf8)
7718 while (off < end_off && ScreenLines[off] == ' '
7719 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7720 ++off;
7721 else
7722#endif
7723 while (off < end_off && ScreenLines[off] == ' '
7724 && ScreenAttrs[off] == 0)
7725 ++off;
7726 if (off < end_off) /* something to be cleared */
7727 {
7728 col = off - LineOffset[row];
7729 screen_stop_highlight();
7730 term_windgoto(row, col);/* clear rest of this screen line */
7731 out_str(T_CE);
7732 screen_start(); /* don't know where cursor is now */
7733 col = end_col - col;
7734 while (col--) /* clear chars in ScreenLines */
7735 {
7736 ScreenLines[off] = ' ';
7737#ifdef FEAT_MBYTE
7738 if (enc_utf8)
7739 ScreenLinesUC[off] = 0;
7740#endif
7741 ScreenAttrs[off] = 0;
7742 ++off;
7743 }
7744 }
7745 did_delete = TRUE; /* the chars are cleared now */
7746 }
7747
7748 off = LineOffset[row] + start_col;
7749 c = c1;
7750 for (col = start_col; col < end_col; ++col)
7751 {
7752 if (ScreenLines[off] != c
7753#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007754 || (enc_utf8 && (int)ScreenLinesUC[off]
7755 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756#endif
7757 || ScreenAttrs[off] != attr
7758#if defined(FEAT_GUI) || defined(UNIX)
7759 || force_next
7760#endif
7761 )
7762 {
7763#if defined(FEAT_GUI) || defined(UNIX)
7764 /* The bold trick may make a single row of pixels appear in
7765 * the next character. When a bold character is removed, the
7766 * next character should be redrawn too. This happens for our
7767 * own GUI and for some xterms. */
7768 if (
7769# ifdef FEAT_GUI
7770 gui.in_use
7771# endif
7772# if defined(FEAT_GUI) && defined(UNIX)
7773 ||
7774# endif
7775# ifdef UNIX
7776 term_is_xterm
7777# endif
7778 )
7779 {
7780 if (ScreenLines[off] != ' '
7781 && (ScreenAttrs[off] > HL_ALL
7782 || ScreenAttrs[off] & HL_BOLD))
7783 force_next = TRUE;
7784 else
7785 force_next = FALSE;
7786 }
7787#endif
7788 ScreenLines[off] = c;
7789#ifdef FEAT_MBYTE
7790 if (enc_utf8)
7791 {
7792 if (c >= 0x80)
7793 {
7794 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007795 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 }
7797 else
7798 ScreenLinesUC[off] = 0;
7799 }
7800#endif
7801 ScreenAttrs[off] = attr;
7802 if (!did_delete || c != ' ')
7803 screen_char(off, row, col);
7804 }
7805 ++off;
7806 if (col == start_col)
7807 {
7808 if (did_delete)
7809 break;
7810 c = c2;
7811 }
7812 }
7813 if (end_col == Columns)
7814 LineWraps[row] = FALSE;
7815 if (row == Rows - 1) /* overwritten the command line */
7816 {
7817 redraw_cmdline = TRUE;
7818 if (c1 == ' ' && c2 == ' ')
7819 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007820 if (start_col == 0)
7821 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 }
7823 }
7824}
7825
7826/*
7827 * Check if there should be a delay. Used before clearing or redrawing the
7828 * screen or the command line.
7829 */
7830 void
7831check_for_delay(check_msg_scroll)
7832 int check_msg_scroll;
7833{
7834 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7835 && !did_wait_return
7836 && emsg_silent == 0)
7837 {
7838 out_flush();
7839 ui_delay(1000L, TRUE);
7840 emsg_on_display = FALSE;
7841 if (check_msg_scroll)
7842 msg_scroll = FALSE;
7843 }
7844}
7845
7846/*
7847 * screen_valid - allocate screen buffers if size changed
7848 * If "clear" is TRUE: clear screen if it has been resized.
7849 * Returns TRUE if there is a valid screen to write to.
7850 * Returns FALSE when starting up and screen not initialized yet.
7851 */
7852 int
7853screen_valid(clear)
7854 int clear;
7855{
7856 screenalloc(clear); /* allocate screen buffers if size changed */
7857 return (ScreenLines != NULL);
7858}
7859
7860/*
7861 * Resize the shell to Rows and Columns.
7862 * Allocate ScreenLines[] and associated items.
7863 *
7864 * There may be some time between setting Rows and Columns and (re)allocating
7865 * ScreenLines[]. This happens when starting up and when (manually) changing
7866 * the shell size. Always use screen_Rows and screen_Columns to access items
7867 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7868 * final size of the shell is needed.
7869 */
7870 void
7871screenalloc(clear)
7872 int clear;
7873{
7874 int new_row, old_row;
7875#ifdef FEAT_GUI
7876 int old_Rows;
7877#endif
7878 win_T *wp;
7879 int outofmem = FALSE;
7880 int len;
7881 schar_T *new_ScreenLines;
7882#ifdef FEAT_MBYTE
7883 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007884 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007886 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887#endif
7888 sattr_T *new_ScreenAttrs;
7889 unsigned *new_LineOffset;
7890 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007891#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007892 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007893 tabpage_T *tp;
7894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007896 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007897#ifdef FEAT_AUTOCMD
7898 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007900retry:
7901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 /*
7903 * Allocation of the screen buffers is done only when the size changes and
7904 * when Rows and Columns have been set and we have started doing full
7905 * screen stuff.
7906 */
7907 if ((ScreenLines != NULL
7908 && Rows == screen_Rows
7909 && Columns == screen_Columns
7910#ifdef FEAT_MBYTE
7911 && enc_utf8 == (ScreenLinesUC != NULL)
7912 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007913 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914#endif
7915 )
7916 || Rows == 0
7917 || Columns == 0
7918 || (!full_screen && ScreenLines == NULL))
7919 return;
7920
7921 /*
7922 * It's possible that we produce an out-of-memory message below, which
7923 * will cause this function to be called again. To break the loop, just
7924 * return here.
7925 */
7926 if (entered)
7927 return;
7928 entered = TRUE;
7929
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007930 /*
7931 * Note that the window sizes are updated before reallocating the arrays,
7932 * thus we must not redraw here!
7933 */
7934 ++RedrawingDisabled;
7935
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 win_new_shellsize(); /* fit the windows in the new sized shell */
7937
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 comp_col(); /* recompute columns for shown command and ruler */
7939
7940 /*
7941 * We're changing the size of the screen.
7942 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7943 * - Move lines from the old arrays into the new arrays, clear extra
7944 * lines (unless the screen is going to be cleared).
7945 * - Free the old arrays.
7946 *
7947 * If anything fails, make ScreenLines NULL, so we don't do anything!
7948 * Continuing with the old ScreenLines may result in a crash, because the
7949 * size is wrong.
7950 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007951 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007953#ifdef FEAT_AUTOCMD
7954 if (aucmd_win != NULL)
7955 win_free_lsize(aucmd_win);
7956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957
7958 new_ScreenLines = (schar_T *)lalloc((long_u)(
7959 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7960#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007961 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (enc_utf8)
7963 {
7964 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7965 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007966 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007967 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7969 }
7970 if (enc_dbcs == DBCS_JPNU)
7971 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7972 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7973#endif
7974 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7975 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7976 new_LineOffset = (unsigned *)lalloc((long_u)(
7977 Rows * sizeof(unsigned)), FALSE);
7978 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007979#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007980 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007983 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {
7985 if (win_alloc_lines(wp) == FAIL)
7986 {
7987 outofmem = TRUE;
7988#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007989 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990#endif
7991 }
7992 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007993#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007994 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7995 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007996 outofmem = TRUE;
7997#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007998#ifdef FEAT_WINDOWS
7999give_up:
8000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008002#ifdef FEAT_MBYTE
8003 for (i = 0; i < p_mco; ++i)
8004 if (new_ScreenLinesC[i] == NULL)
8005 break;
8006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (new_ScreenLines == NULL
8008#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008009 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8011#endif
8012 || new_ScreenAttrs == NULL
8013 || new_LineOffset == NULL
8014 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008015#ifdef FEAT_WINDOWS
8016 || new_TabPageIdxs == NULL
8017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 || outofmem)
8019 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008020 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008021 {
8022 /* guess the size */
8023 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8024
8025 /* Remember we did this to avoid getting outofmem messages over
8026 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008027 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 vim_free(new_ScreenLines);
8030 new_ScreenLines = NULL;
8031#ifdef FEAT_MBYTE
8032 vim_free(new_ScreenLinesUC);
8033 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008034 for (i = 0; i < p_mco; ++i)
8035 {
8036 vim_free(new_ScreenLinesC[i]);
8037 new_ScreenLinesC[i] = NULL;
8038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 vim_free(new_ScreenLines2);
8040 new_ScreenLines2 = NULL;
8041#endif
8042 vim_free(new_ScreenAttrs);
8043 new_ScreenAttrs = NULL;
8044 vim_free(new_LineOffset);
8045 new_LineOffset = NULL;
8046 vim_free(new_LineWraps);
8047 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008048#ifdef FEAT_WINDOWS
8049 vim_free(new_TabPageIdxs);
8050 new_TabPageIdxs = NULL;
8051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 else
8054 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008055 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008056
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 for (new_row = 0; new_row < Rows; ++new_row)
8058 {
8059 new_LineOffset[new_row] = new_row * Columns;
8060 new_LineWraps[new_row] = FALSE;
8061
8062 /*
8063 * If the screen is not going to be cleared, copy as much as
8064 * possible from the old screen to the new one and clear the rest
8065 * (used when resizing the window at the "--more--" prompt or when
8066 * executing an external command, for the GUI).
8067 */
8068 if (!clear)
8069 {
8070 (void)vim_memset(new_ScreenLines + new_row * Columns,
8071 ' ', (size_t)Columns * sizeof(schar_T));
8072#ifdef FEAT_MBYTE
8073 if (enc_utf8)
8074 {
8075 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8076 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008077 for (i = 0; i < p_mco; ++i)
8078 (void)vim_memset(new_ScreenLinesC[i]
8079 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 0, (size_t)Columns * sizeof(u8char_T));
8081 }
8082 if (enc_dbcs == DBCS_JPNU)
8083 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8084 0, (size_t)Columns * sizeof(schar_T));
8085#endif
8086 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8087 0, (size_t)Columns * sizeof(sattr_T));
8088 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008089 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
8091 if (screen_Columns < Columns)
8092 len = screen_Columns;
8093 else
8094 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008095#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008096 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008097 * may be invalid now. Also when p_mco changes. */
8098 if (!(enc_utf8 && ScreenLinesUC == NULL)
8099 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008100#endif
8101 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8102 ScreenLines + LineOffset[old_row],
8103 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008105 if (enc_utf8 && ScreenLinesUC != NULL
8106 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {
8108 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8109 ScreenLinesUC + LineOffset[old_row],
8110 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008111 for (i = 0; i < p_mco; ++i)
8112 mch_memmove(new_ScreenLinesC[i]
8113 + new_LineOffset[new_row],
8114 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 (size_t)len * sizeof(u8char_T));
8116 }
8117 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8118 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8119 ScreenLines2 + LineOffset[old_row],
8120 (size_t)len * sizeof(schar_T));
8121#endif
8122 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8123 ScreenAttrs + LineOffset[old_row],
8124 (size_t)len * sizeof(sattr_T));
8125 }
8126 }
8127 }
8128 /* Use the last line of the screen for the current line. */
8129 current_ScreenLine = new_ScreenLines + Rows * Columns;
8130 }
8131
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008132 free_screenlines();
8133
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 ScreenLines = new_ScreenLines;
8135#ifdef FEAT_MBYTE
8136 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008137 for (i = 0; i < p_mco; ++i)
8138 ScreenLinesC[i] = new_ScreenLinesC[i];
8139 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 ScreenLines2 = new_ScreenLines2;
8141#endif
8142 ScreenAttrs = new_ScreenAttrs;
8143 LineOffset = new_LineOffset;
8144 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008145#ifdef FEAT_WINDOWS
8146 TabPageIdxs = new_TabPageIdxs;
8147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148
8149 /* It's important that screen_Rows and screen_Columns reflect the actual
8150 * size of ScreenLines[]. Set them before calling anything. */
8151#ifdef FEAT_GUI
8152 old_Rows = screen_Rows;
8153#endif
8154 screen_Rows = Rows;
8155 screen_Columns = Columns;
8156
8157 must_redraw = CLEAR; /* need to clear the screen later */
8158 if (clear)
8159 screenclear2();
8160
8161#ifdef FEAT_GUI
8162 else if (gui.in_use
8163 && !gui.starting
8164 && ScreenLines != NULL
8165 && old_Rows != Rows)
8166 {
8167 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8168 /*
8169 * Adjust the position of the cursor, for when executing an external
8170 * command.
8171 */
8172 if (msg_row >= Rows) /* Rows got smaller */
8173 msg_row = Rows - 1; /* put cursor at last row */
8174 else if (Rows > old_Rows) /* Rows got bigger */
8175 msg_row += Rows - old_Rows; /* put cursor in same place */
8176 if (msg_col >= Columns) /* Columns got smaller */
8177 msg_col = Columns - 1; /* put cursor at last column */
8178 }
8179#endif
8180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008182 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008183
8184#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008185 /*
8186 * Do not apply autocommands more than 3 times to avoid an endless loop
8187 * in case applying autocommands always changes Rows or Columns.
8188 */
8189 if (starting == 0 && ++retry_count <= 3)
8190 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008191 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008192 /* In rare cases, autocommands may have altered Rows or Columns,
8193 * jump back to check if we need to allocate the screen again. */
8194 goto retry;
8195 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197}
8198
8199 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008200free_screenlines()
8201{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008202#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008203 int i;
8204
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008205 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008206 for (i = 0; i < Screen_mco; ++i)
8207 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008208 vim_free(ScreenLines2);
8209#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008210 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008211 vim_free(ScreenAttrs);
8212 vim_free(LineOffset);
8213 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008214#ifdef FEAT_WINDOWS
8215 vim_free(TabPageIdxs);
8216#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008217}
8218
8219 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220screenclear()
8221{
8222 check_for_delay(FALSE);
8223 screenalloc(FALSE); /* allocate screen buffers if size changed */
8224 screenclear2(); /* clear the screen */
8225}
8226
8227 static void
8228screenclear2()
8229{
8230 int i;
8231
8232 if (starting == NO_SCREEN || ScreenLines == NULL
8233#ifdef FEAT_GUI
8234 || (gui.in_use && gui.starting)
8235#endif
8236 )
8237 return;
8238
8239#ifdef FEAT_GUI
8240 if (!gui.in_use)
8241#endif
8242 screen_attr = -1; /* force setting the Normal colors */
8243 screen_stop_highlight(); /* don't want highlighting here */
8244
8245#ifdef FEAT_CLIPBOARD
8246 /* disable selection without redrawing it */
8247 clip_scroll_selection(9999);
8248#endif
8249
8250 /* blank out ScreenLines */
8251 for (i = 0; i < Rows; ++i)
8252 {
8253 lineclear(LineOffset[i], (int)Columns);
8254 LineWraps[i] = FALSE;
8255 }
8256
8257 if (can_clear(T_CL))
8258 {
8259 out_str(T_CL); /* clear the display */
8260 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008261 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 }
8263 else
8264 {
8265 /* can't clear the screen, mark all chars with invalid attributes */
8266 for (i = 0; i < Rows; ++i)
8267 lineinvalid(LineOffset[i], (int)Columns);
8268 clear_cmdline = TRUE;
8269 }
8270
8271 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8272
8273 win_rest_invalid(firstwin);
8274 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008275#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008276 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 if (must_redraw == CLEAR) /* no need to clear again */
8279 must_redraw = NOT_VALID;
8280 compute_cmdrow();
8281 msg_row = cmdline_row; /* put cursor on last line for messages */
8282 msg_col = 0;
8283 screen_start(); /* don't know where cursor is now */
8284 msg_scrolled = 0; /* can't scroll back */
8285 msg_didany = FALSE;
8286 msg_didout = FALSE;
8287}
8288
8289/*
8290 * Clear one line in ScreenLines.
8291 */
8292 static void
8293lineclear(off, width)
8294 unsigned off;
8295 int width;
8296{
8297 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8298#ifdef FEAT_MBYTE
8299 if (enc_utf8)
8300 (void)vim_memset(ScreenLinesUC + off, 0,
8301 (size_t)width * sizeof(u8char_T));
8302#endif
8303 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8304}
8305
8306/*
8307 * Mark one line in ScreenLines invalid by setting the attributes to an
8308 * invalid value.
8309 */
8310 static void
8311lineinvalid(off, width)
8312 unsigned off;
8313 int width;
8314{
8315 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8316}
8317
8318#ifdef FEAT_VERTSPLIT
8319/*
8320 * Copy part of a Screenline for vertically split window "wp".
8321 */
8322 static void
8323linecopy(to, from, wp)
8324 int to;
8325 int from;
8326 win_T *wp;
8327{
8328 unsigned off_to = LineOffset[to] + wp->w_wincol;
8329 unsigned off_from = LineOffset[from] + wp->w_wincol;
8330
8331 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8332 wp->w_width * sizeof(schar_T));
8333# ifdef FEAT_MBYTE
8334 if (enc_utf8)
8335 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008336 int i;
8337
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8339 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008340 for (i = 0; i < p_mco; ++i)
8341 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8342 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 }
8344 if (enc_dbcs == DBCS_JPNU)
8345 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8346 wp->w_width * sizeof(schar_T));
8347# endif
8348 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8349 wp->w_width * sizeof(sattr_T));
8350}
8351#endif
8352
8353/*
8354 * Return TRUE if clearing with term string "p" would work.
8355 * It can't work when the string is empty or it won't set the right background.
8356 */
8357 int
8358can_clear(p)
8359 char_u *p;
8360{
8361 return (*p != NUL && (t_colors <= 1
8362#ifdef FEAT_GUI
8363 || gui.in_use
8364#endif
8365 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8366}
8367
8368/*
8369 * Reset cursor position. Use whenever cursor was moved because of outputting
8370 * something directly to the screen (shell commands) or a terminal control
8371 * code.
8372 */
8373 void
8374screen_start()
8375{
8376 screen_cur_row = screen_cur_col = 9999;
8377}
8378
8379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 * Move the cursor to position "row","col" in the screen.
8381 * This tries to find the most efficient way to move, minimizing the number of
8382 * characters sent to the terminal.
8383 */
8384 void
8385windgoto(row, col)
8386 int row;
8387 int col;
8388{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008389 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 int i;
8391 int plan;
8392 int cost;
8393 int wouldbe_col;
8394 int noinvcurs;
8395 char_u *bs;
8396 int goto_cost;
8397 int attr;
8398
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008399#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8401
8402#define PLAN_LE 1
8403#define PLAN_CR 2
8404#define PLAN_NL 3
8405#define PLAN_WRITE 4
8406 /* Can't use ScreenLines unless initialized */
8407 if (ScreenLines == NULL)
8408 return;
8409
8410 if (col != screen_cur_col || row != screen_cur_row)
8411 {
8412 /* Check for valid position. */
8413 if (row < 0) /* window without text lines? */
8414 row = 0;
8415 if (row >= screen_Rows)
8416 row = screen_Rows - 1;
8417 if (col >= screen_Columns)
8418 col = screen_Columns - 1;
8419
8420 /* check if no cursor movement is allowed in highlight mode */
8421 if (screen_attr && *T_MS == NUL)
8422 noinvcurs = HIGHL_COST;
8423 else
8424 noinvcurs = 0;
8425 goto_cost = GOTO_COST + noinvcurs;
8426
8427 /*
8428 * Plan how to do the positioning:
8429 * 1. Use CR to move it to column 0, same row.
8430 * 2. Use T_LE to move it a few columns to the left.
8431 * 3. Use NL to move a few lines down, column 0.
8432 * 4. Move a few columns to the right with T_ND or by writing chars.
8433 *
8434 * Don't do this if the cursor went beyond the last column, the cursor
8435 * position is unknown then (some terminals wrap, some don't )
8436 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008437 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 * characters to move the cursor to the right.
8439 */
8440 if (row >= screen_cur_row && screen_cur_col < Columns)
8441 {
8442 /*
8443 * If the cursor is in the same row, bigger col, we can use CR
8444 * or T_LE.
8445 */
8446 bs = NULL; /* init for GCC */
8447 attr = screen_attr;
8448 if (row == screen_cur_row && col < screen_cur_col)
8449 {
8450 /* "le" is preferred over "bc", because "bc" is obsolete */
8451 if (*T_LE)
8452 bs = T_LE; /* "cursor left" */
8453 else
8454 bs = T_BC; /* "backspace character (old) */
8455 if (*bs)
8456 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8457 else
8458 cost = 999;
8459 if (col + 1 < cost) /* using CR is less characters */
8460 {
8461 plan = PLAN_CR;
8462 wouldbe_col = 0;
8463 cost = 1; /* CR is just one character */
8464 }
8465 else
8466 {
8467 plan = PLAN_LE;
8468 wouldbe_col = col;
8469 }
8470 if (noinvcurs) /* will stop highlighting */
8471 {
8472 cost += noinvcurs;
8473 attr = 0;
8474 }
8475 }
8476
8477 /*
8478 * If the cursor is above where we want to be, we can use CR LF.
8479 */
8480 else if (row > screen_cur_row)
8481 {
8482 plan = PLAN_NL;
8483 wouldbe_col = 0;
8484 cost = (row - screen_cur_row) * 2; /* CR LF */
8485 if (noinvcurs) /* will stop highlighting */
8486 {
8487 cost += noinvcurs;
8488 attr = 0;
8489 }
8490 }
8491
8492 /*
8493 * If the cursor is in the same row, smaller col, just use write.
8494 */
8495 else
8496 {
8497 plan = PLAN_WRITE;
8498 wouldbe_col = screen_cur_col;
8499 cost = 0;
8500 }
8501
8502 /*
8503 * Check if any characters that need to be written have the
8504 * correct attributes. Also avoid UTF-8 characters.
8505 */
8506 i = col - wouldbe_col;
8507 if (i > 0)
8508 cost += i;
8509 if (cost < goto_cost && i > 0)
8510 {
8511 /*
8512 * Check if the attributes are correct without additionally
8513 * stopping highlighting.
8514 */
8515 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8516 while (i && *p++ == attr)
8517 --i;
8518 if (i != 0)
8519 {
8520 /*
8521 * Try if it works when highlighting is stopped here.
8522 */
8523 if (*--p == 0)
8524 {
8525 cost += noinvcurs;
8526 while (i && *p++ == 0)
8527 --i;
8528 }
8529 if (i != 0)
8530 cost = 999; /* different attributes, don't do it */
8531 }
8532#ifdef FEAT_MBYTE
8533 if (enc_utf8)
8534 {
8535 /* Don't use an UTF-8 char for positioning, it's slow. */
8536 for (i = wouldbe_col; i < col; ++i)
8537 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8538 {
8539 cost = 999;
8540 break;
8541 }
8542 }
8543#endif
8544 }
8545
8546 /*
8547 * We can do it without term_windgoto()!
8548 */
8549 if (cost < goto_cost)
8550 {
8551 if (plan == PLAN_LE)
8552 {
8553 if (noinvcurs)
8554 screen_stop_highlight();
8555 while (screen_cur_col > col)
8556 {
8557 out_str(bs);
8558 --screen_cur_col;
8559 }
8560 }
8561 else if (plan == PLAN_CR)
8562 {
8563 if (noinvcurs)
8564 screen_stop_highlight();
8565 out_char('\r');
8566 screen_cur_col = 0;
8567 }
8568 else if (plan == PLAN_NL)
8569 {
8570 if (noinvcurs)
8571 screen_stop_highlight();
8572 while (screen_cur_row < row)
8573 {
8574 out_char('\n');
8575 ++screen_cur_row;
8576 }
8577 screen_cur_col = 0;
8578 }
8579
8580 i = col - screen_cur_col;
8581 if (i > 0)
8582 {
8583 /*
8584 * Use cursor-right if it's one character only. Avoids
8585 * removing a line of pixels from the last bold char, when
8586 * using the bold trick in the GUI.
8587 */
8588 if (T_ND[0] != NUL && T_ND[1] == NUL)
8589 {
8590 while (i-- > 0)
8591 out_char(*T_ND);
8592 }
8593 else
8594 {
8595 int off;
8596
8597 off = LineOffset[row] + screen_cur_col;
8598 while (i-- > 0)
8599 {
8600 if (ScreenAttrs[off] != screen_attr)
8601 screen_stop_highlight();
8602#ifdef FEAT_MBYTE
8603 out_flush_check();
8604#endif
8605 out_char(ScreenLines[off]);
8606#ifdef FEAT_MBYTE
8607 if (enc_dbcs == DBCS_JPNU
8608 && ScreenLines[off] == 0x8e)
8609 out_char(ScreenLines2[off]);
8610#endif
8611 ++off;
8612 }
8613 }
8614 }
8615 }
8616 }
8617 else
8618 cost = 999;
8619
8620 if (cost >= goto_cost)
8621 {
8622 if (noinvcurs)
8623 screen_stop_highlight();
8624 if (row == screen_cur_row && (col > screen_cur_col) &&
8625 *T_CRI != NUL)
8626 term_cursor_right(col - screen_cur_col);
8627 else
8628 term_windgoto(row, col);
8629 }
8630 screen_cur_row = row;
8631 screen_cur_col = col;
8632 }
8633}
8634
8635/*
8636 * Set cursor to its position in the current window.
8637 */
8638 void
8639setcursor()
8640{
8641 if (redrawing())
8642 {
8643 validate_cursor();
8644 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8645 W_WINCOL(curwin) + (
8646#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008647 /* With 'rightleft' set and the cursor on a double-wide
8648 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8650# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008651 (has_mbyte
8652 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8653 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654# endif
8655 1)) :
8656#endif
8657 curwin->w_wcol));
8658 }
8659}
8660
8661
8662/*
8663 * insert 'line_count' lines at 'row' in window 'wp'
8664 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8665 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8666 * scrolling.
8667 * Returns FAIL if the lines are not inserted, OK for success.
8668 */
8669 int
8670win_ins_lines(wp, row, line_count, invalid, mayclear)
8671 win_T *wp;
8672 int row;
8673 int line_count;
8674 int invalid;
8675 int mayclear;
8676{
8677 int did_delete;
8678 int nextrow;
8679 int lastrow;
8680 int retval;
8681
8682 if (invalid)
8683 wp->w_lines_valid = 0;
8684
8685 if (wp->w_height < 5)
8686 return FAIL;
8687
8688 if (line_count > wp->w_height - row)
8689 line_count = wp->w_height - row;
8690
8691 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8692 if (retval != MAYBE)
8693 return retval;
8694
8695 /*
8696 * If there is a next window or a status line, we first try to delete the
8697 * lines at the bottom to avoid messing what is after the window.
8698 * If this fails and there are following windows, don't do anything to avoid
8699 * messing up those windows, better just redraw.
8700 */
8701 did_delete = FALSE;
8702#ifdef FEAT_WINDOWS
8703 if (wp->w_next != NULL || wp->w_status_height)
8704 {
8705 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8706 line_count, (int)Rows, FALSE, NULL) == OK)
8707 did_delete = TRUE;
8708 else if (wp->w_next)
8709 return FAIL;
8710 }
8711#endif
8712 /*
8713 * if no lines deleted, blank the lines that will end up below the window
8714 */
8715 if (!did_delete)
8716 {
8717#ifdef FEAT_WINDOWS
8718 wp->w_redr_status = TRUE;
8719#endif
8720 redraw_cmdline = TRUE;
8721 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8722 lastrow = nextrow + line_count;
8723 if (lastrow > Rows)
8724 lastrow = Rows;
8725 screen_fill(nextrow - line_count, lastrow - line_count,
8726 W_WINCOL(wp), (int)W_ENDCOL(wp),
8727 ' ', ' ', 0);
8728 }
8729
8730 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8731 == FAIL)
8732 {
8733 /* deletion will have messed up other windows */
8734 if (did_delete)
8735 {
8736#ifdef FEAT_WINDOWS
8737 wp->w_redr_status = TRUE;
8738#endif
8739 win_rest_invalid(W_NEXT(wp));
8740 }
8741 return FAIL;
8742 }
8743
8744 return OK;
8745}
8746
8747/*
8748 * delete "line_count" window lines at "row" in window "wp"
8749 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8750 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8751 * scrolling
8752 * Return OK for success, FAIL if the lines are not deleted.
8753 */
8754 int
8755win_del_lines(wp, row, line_count, invalid, mayclear)
8756 win_T *wp;
8757 int row;
8758 int line_count;
8759 int invalid;
8760 int mayclear;
8761{
8762 int retval;
8763
8764 if (invalid)
8765 wp->w_lines_valid = 0;
8766
8767 if (line_count > wp->w_height - row)
8768 line_count = wp->w_height - row;
8769
8770 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8771 if (retval != MAYBE)
8772 return retval;
8773
8774 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8775 (int)Rows, FALSE, NULL) == FAIL)
8776 return FAIL;
8777
8778#ifdef FEAT_WINDOWS
8779 /*
8780 * If there are windows or status lines below, try to put them at the
8781 * correct place. If we can't do that, they have to be redrawn.
8782 */
8783 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8784 {
8785 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8786 line_count, (int)Rows, NULL) == FAIL)
8787 {
8788 wp->w_redr_status = TRUE;
8789 win_rest_invalid(wp->w_next);
8790 }
8791 }
8792 /*
8793 * If this is the last window and there is no status line, redraw the
8794 * command line later.
8795 */
8796 else
8797#endif
8798 redraw_cmdline = TRUE;
8799 return OK;
8800}
8801
8802/*
8803 * Common code for win_ins_lines() and win_del_lines().
8804 * Returns OK or FAIL when the work has been done.
8805 * Returns MAYBE when not finished yet.
8806 */
8807 static int
8808win_do_lines(wp, row, line_count, mayclear, del)
8809 win_T *wp;
8810 int row;
8811 int line_count;
8812 int mayclear;
8813 int del;
8814{
8815 int retval;
8816
8817 if (!redrawing() || line_count <= 0)
8818 return FAIL;
8819
8820 /* only a few lines left: redraw is faster */
8821 if (mayclear && Rows - line_count < 5
8822#ifdef FEAT_VERTSPLIT
8823 && wp->w_width == Columns
8824#endif
8825 )
8826 {
8827 screenclear(); /* will set wp->w_lines_valid to 0 */
8828 return FAIL;
8829 }
8830
8831 /*
8832 * Delete all remaining lines
8833 */
8834 if (row + line_count >= wp->w_height)
8835 {
8836 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8837 W_WINCOL(wp), (int)W_ENDCOL(wp),
8838 ' ', ' ', 0);
8839 return OK;
8840 }
8841
8842 /*
8843 * when scrolling, the message on the command line should be cleared,
8844 * otherwise it will stay there forever.
8845 */
8846 clear_cmdline = TRUE;
8847
8848 /*
8849 * If the terminal can set a scroll region, use that.
8850 * Always do this in a vertically split window. This will redraw from
8851 * ScreenLines[] when t_CV isn't defined. That's faster than using
8852 * win_line().
8853 * Don't use a scroll region when we are going to redraw the text, writing
8854 * a character in the lower right corner of the scroll region causes a
8855 * scroll-up in the DJGPP version.
8856 */
8857 if (scroll_region
8858#ifdef FEAT_VERTSPLIT
8859 || W_WIDTH(wp) != Columns
8860#endif
8861 )
8862 {
8863#ifdef FEAT_VERTSPLIT
8864 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8865#endif
8866 scroll_region_set(wp, row);
8867 if (del)
8868 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8869 wp->w_height - row, FALSE, wp);
8870 else
8871 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8872 wp->w_height - row, wp);
8873#ifdef FEAT_VERTSPLIT
8874 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8875#endif
8876 scroll_region_reset();
8877 return retval;
8878 }
8879
8880#ifdef FEAT_WINDOWS
8881 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8882 return FAIL;
8883#endif
8884
8885 return MAYBE;
8886}
8887
8888/*
8889 * window 'wp' and everything after it is messed up, mark it for redraw
8890 */
8891 static void
8892win_rest_invalid(wp)
8893 win_T *wp;
8894{
8895#ifdef FEAT_WINDOWS
8896 while (wp != NULL)
8897#else
8898 if (wp != NULL)
8899#endif
8900 {
8901 redraw_win_later(wp, NOT_VALID);
8902#ifdef FEAT_WINDOWS
8903 wp->w_redr_status = TRUE;
8904 wp = wp->w_next;
8905#endif
8906 }
8907 redraw_cmdline = TRUE;
8908}
8909
8910/*
8911 * The rest of the routines in this file perform screen manipulations. The
8912 * given operation is performed physically on the screen. The corresponding
8913 * change is also made to the internal screen image. In this way, the editor
8914 * anticipates the effect of editing changes on the appearance of the screen.
8915 * That way, when we call screenupdate a complete redraw isn't usually
8916 * necessary. Another advantage is that we can keep adding code to anticipate
8917 * screen changes, and in the meantime, everything still works.
8918 */
8919
8920/*
8921 * types for inserting or deleting lines
8922 */
8923#define USE_T_CAL 1
8924#define USE_T_CDL 2
8925#define USE_T_AL 3
8926#define USE_T_CE 4
8927#define USE_T_DL 5
8928#define USE_T_SR 6
8929#define USE_NL 7
8930#define USE_T_CD 8
8931#define USE_REDRAW 9
8932
8933/*
8934 * insert lines on the screen and update ScreenLines[]
8935 * 'end' is the line after the scrolled part. Normally it is Rows.
8936 * When scrolling region used 'off' is the offset from the top for the region.
8937 * 'row' and 'end' are relative to the start of the region.
8938 *
8939 * return FAIL for failure, OK for success.
8940 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008941 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942screen_ins_lines(off, row, line_count, end, wp)
8943 int off;
8944 int row;
8945 int line_count;
8946 int end;
8947 win_T *wp; /* NULL or window to use width from */
8948{
8949 int i;
8950 int j;
8951 unsigned temp;
8952 int cursor_row;
8953 int type;
8954 int result_empty;
8955 int can_ce = can_clear(T_CE);
8956
8957 /*
8958 * FAIL if
8959 * - there is no valid screen
8960 * - the screen has to be redrawn completely
8961 * - the line count is less than one
8962 * - the line count is more than 'ttyscroll'
8963 */
8964 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8965 return FAIL;
8966
8967 /*
8968 * There are seven ways to insert lines:
8969 * 0. When in a vertically split window and t_CV isn't set, redraw the
8970 * characters from ScreenLines[].
8971 * 1. Use T_CD (clear to end of display) if it exists and the result of
8972 * the insert is just empty lines
8973 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8974 * present or line_count > 1. It looks better if we do all the inserts
8975 * at once.
8976 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8977 * insert is just empty lines and T_CE is not present or line_count >
8978 * 1.
8979 * 4. Use T_AL (insert line) if it exists.
8980 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8981 * just empty lines.
8982 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8983 * just empty lines.
8984 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8985 * the 'da' flag is not set or we have clear line capability.
8986 * 8. redraw the characters from ScreenLines[].
8987 *
8988 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8989 * the scrollbar for the window. It does have insert line, use that if it
8990 * exists.
8991 */
8992 result_empty = (row + line_count >= end);
8993#ifdef FEAT_VERTSPLIT
8994 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8995 type = USE_REDRAW;
8996 else
8997#endif
8998 if (can_clear(T_CD) && result_empty)
8999 type = USE_T_CD;
9000 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9001 type = USE_T_CAL;
9002 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9003 type = USE_T_CDL;
9004 else if (*T_AL != NUL)
9005 type = USE_T_AL;
9006 else if (can_ce && result_empty)
9007 type = USE_T_CE;
9008 else if (*T_DL != NUL && result_empty)
9009 type = USE_T_DL;
9010 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9011 type = USE_T_SR;
9012 else
9013 return FAIL;
9014
9015 /*
9016 * For clearing the lines screen_del_lines() is used. This will also take
9017 * care of t_db if necessary.
9018 */
9019 if (type == USE_T_CD || type == USE_T_CDL ||
9020 type == USE_T_CE || type == USE_T_DL)
9021 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9022
9023 /*
9024 * If text is retained below the screen, first clear or delete as many
9025 * lines at the bottom of the window as are about to be inserted so that
9026 * the deleted lines won't later surface during a screen_del_lines.
9027 */
9028 if (*T_DB)
9029 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9030
9031#ifdef FEAT_CLIPBOARD
9032 /* Remove a modeless selection when inserting lines halfway the screen
9033 * or not the full width of the screen. */
9034 if (off + row > 0
9035# ifdef FEAT_VERTSPLIT
9036 || (wp != NULL && wp->w_width != Columns)
9037# endif
9038 )
9039 clip_clear_selection();
9040 else
9041 clip_scroll_selection(-line_count);
9042#endif
9043
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044#ifdef FEAT_GUI
9045 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9046 * scrolling is actually carried out. */
9047 gui_dont_update_cursor();
9048#endif
9049
9050 if (*T_CCS != NUL) /* cursor relative to region */
9051 cursor_row = row;
9052 else
9053 cursor_row = row + off;
9054
9055 /*
9056 * Shift LineOffset[] line_count down to reflect the inserted lines.
9057 * Clear the inserted lines in ScreenLines[].
9058 */
9059 row += off;
9060 end += off;
9061 for (i = 0; i < line_count; ++i)
9062 {
9063#ifdef FEAT_VERTSPLIT
9064 if (wp != NULL && wp->w_width != Columns)
9065 {
9066 /* need to copy part of a line */
9067 j = end - 1 - i;
9068 while ((j -= line_count) >= row)
9069 linecopy(j + line_count, j, wp);
9070 j += line_count;
9071 if (can_clear((char_u *)" "))
9072 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9073 else
9074 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9075 LineWraps[j] = FALSE;
9076 }
9077 else
9078#endif
9079 {
9080 j = end - 1 - i;
9081 temp = LineOffset[j];
9082 while ((j -= line_count) >= row)
9083 {
9084 LineOffset[j + line_count] = LineOffset[j];
9085 LineWraps[j + line_count] = LineWraps[j];
9086 }
9087 LineOffset[j + line_count] = temp;
9088 LineWraps[j + line_count] = FALSE;
9089 if (can_clear((char_u *)" "))
9090 lineclear(temp, (int)Columns);
9091 else
9092 lineinvalid(temp, (int)Columns);
9093 }
9094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095
9096 screen_stop_highlight();
9097 windgoto(cursor_row, 0);
9098
9099#ifdef FEAT_VERTSPLIT
9100 /* redraw the characters */
9101 if (type == USE_REDRAW)
9102 redraw_block(row, end, wp);
9103 else
9104#endif
9105 if (type == USE_T_CAL)
9106 {
9107 term_append_lines(line_count);
9108 screen_start(); /* don't know where cursor is now */
9109 }
9110 else
9111 {
9112 for (i = 0; i < line_count; i++)
9113 {
9114 if (type == USE_T_AL)
9115 {
9116 if (i && cursor_row != 0)
9117 windgoto(cursor_row, 0);
9118 out_str(T_AL);
9119 }
9120 else /* type == USE_T_SR */
9121 out_str(T_SR);
9122 screen_start(); /* don't know where cursor is now */
9123 }
9124 }
9125
9126 /*
9127 * With scroll-reverse and 'da' flag set we need to clear the lines that
9128 * have been scrolled down into the region.
9129 */
9130 if (type == USE_T_SR && *T_DA)
9131 {
9132 for (i = 0; i < line_count; ++i)
9133 {
9134 windgoto(off + i, 0);
9135 out_str(T_CE);
9136 screen_start(); /* don't know where cursor is now */
9137 }
9138 }
9139
9140#ifdef FEAT_GUI
9141 gui_can_update_cursor();
9142 if (gui.in_use)
9143 out_flush(); /* always flush after a scroll */
9144#endif
9145 return OK;
9146}
9147
9148/*
9149 * delete lines on the screen and update ScreenLines[]
9150 * 'end' is the line after the scrolled part. Normally it is Rows.
9151 * When scrolling region used 'off' is the offset from the top for the region.
9152 * 'row' and 'end' are relative to the start of the region.
9153 *
9154 * Return OK for success, FAIL if the lines are not deleted.
9155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 int
9157screen_del_lines(off, row, line_count, end, force, wp)
9158 int off;
9159 int row;
9160 int line_count;
9161 int end;
9162 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009163 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165 int j;
9166 int i;
9167 unsigned temp;
9168 int cursor_row;
9169 int cursor_end;
9170 int result_empty; /* result is empty until end of region */
9171 int can_delete; /* deleting line codes can be used */
9172 int type;
9173
9174 /*
9175 * FAIL if
9176 * - there is no valid screen
9177 * - the screen has to be redrawn completely
9178 * - the line count is less than one
9179 * - the line count is more than 'ttyscroll'
9180 */
9181 if (!screen_valid(TRUE) || line_count <= 0 ||
9182 (!force && line_count > p_ttyscroll))
9183 return FAIL;
9184
9185 /*
9186 * Check if the rest of the current region will become empty.
9187 */
9188 result_empty = row + line_count >= end;
9189
9190 /*
9191 * We can delete lines only when 'db' flag not set or when 'ce' option
9192 * available.
9193 */
9194 can_delete = (*T_DB == NUL || can_clear(T_CE));
9195
9196 /*
9197 * There are six ways to delete lines:
9198 * 0. When in a vertically split window and t_CV isn't set, redraw the
9199 * characters from ScreenLines[].
9200 * 1. Use T_CD if it exists and the result is empty.
9201 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9202 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9203 * none of the other ways work.
9204 * 4. Use T_CE (erase line) if the result is empty.
9205 * 5. Use T_DL (delete line) if it exists.
9206 * 6. redraw the characters from ScreenLines[].
9207 */
9208#ifdef FEAT_VERTSPLIT
9209 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9210 type = USE_REDRAW;
9211 else
9212#endif
9213 if (can_clear(T_CD) && result_empty)
9214 type = USE_T_CD;
9215#if defined(__BEOS__) && defined(BEOS_DR8)
9216 /*
9217 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9218 * its internal termcap... this works okay for tests which test *T_DB !=
9219 * NUL. It has the disadvantage that the user cannot use any :set t_*
9220 * command to get T_DB (back) to empty_option, only :set term=... will do
9221 * the trick...
9222 * Anyway, this hack will hopefully go away with the next OS release.
9223 * (Olaf Seibert)
9224 */
9225 else if (row == 0 && T_DB == empty_option
9226 && (line_count == 1 || *T_CDL == NUL))
9227#else
9228 else if (row == 0 && (
9229#ifndef AMIGA
9230 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9231 * up, so use delete-line command */
9232 line_count == 1 ||
9233#endif
9234 *T_CDL == NUL))
9235#endif
9236 type = USE_NL;
9237 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9238 type = USE_T_CDL;
9239 else if (can_clear(T_CE) && result_empty
9240#ifdef FEAT_VERTSPLIT
9241 && (wp == NULL || wp->w_width == Columns)
9242#endif
9243 )
9244 type = USE_T_CE;
9245 else if (*T_DL != NUL && can_delete)
9246 type = USE_T_DL;
9247 else if (*T_CDL != NUL && can_delete)
9248 type = USE_T_CDL;
9249 else
9250 return FAIL;
9251
9252#ifdef FEAT_CLIPBOARD
9253 /* Remove a modeless selection when deleting lines halfway the screen or
9254 * not the full width of the screen. */
9255 if (off + row > 0
9256# ifdef FEAT_VERTSPLIT
9257 || (wp != NULL && wp->w_width != Columns)
9258# endif
9259 )
9260 clip_clear_selection();
9261 else
9262 clip_scroll_selection(line_count);
9263#endif
9264
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265#ifdef FEAT_GUI
9266 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9267 * scrolling is actually carried out. */
9268 gui_dont_update_cursor();
9269#endif
9270
9271 if (*T_CCS != NUL) /* cursor relative to region */
9272 {
9273 cursor_row = row;
9274 cursor_end = end;
9275 }
9276 else
9277 {
9278 cursor_row = row + off;
9279 cursor_end = end + off;
9280 }
9281
9282 /*
9283 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9284 * Clear the inserted lines in ScreenLines[].
9285 */
9286 row += off;
9287 end += off;
9288 for (i = 0; i < line_count; ++i)
9289 {
9290#ifdef FEAT_VERTSPLIT
9291 if (wp != NULL && wp->w_width != Columns)
9292 {
9293 /* need to copy part of a line */
9294 j = row + i;
9295 while ((j += line_count) <= end - 1)
9296 linecopy(j - line_count, j, wp);
9297 j -= line_count;
9298 if (can_clear((char_u *)" "))
9299 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9300 else
9301 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9302 LineWraps[j] = FALSE;
9303 }
9304 else
9305#endif
9306 {
9307 /* whole width, moving the line pointers is faster */
9308 j = row + i;
9309 temp = LineOffset[j];
9310 while ((j += line_count) <= end - 1)
9311 {
9312 LineOffset[j - line_count] = LineOffset[j];
9313 LineWraps[j - line_count] = LineWraps[j];
9314 }
9315 LineOffset[j - line_count] = temp;
9316 LineWraps[j - line_count] = FALSE;
9317 if (can_clear((char_u *)" "))
9318 lineclear(temp, (int)Columns);
9319 else
9320 lineinvalid(temp, (int)Columns);
9321 }
9322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323
9324 screen_stop_highlight();
9325
9326#ifdef FEAT_VERTSPLIT
9327 /* redraw the characters */
9328 if (type == USE_REDRAW)
9329 redraw_block(row, end, wp);
9330 else
9331#endif
9332 if (type == USE_T_CD) /* delete the lines */
9333 {
9334 windgoto(cursor_row, 0);
9335 out_str(T_CD);
9336 screen_start(); /* don't know where cursor is now */
9337 }
9338 else if (type == USE_T_CDL)
9339 {
9340 windgoto(cursor_row, 0);
9341 term_delete_lines(line_count);
9342 screen_start(); /* don't know where cursor is now */
9343 }
9344 /*
9345 * Deleting lines at top of the screen or scroll region: Just scroll
9346 * the whole screen (scroll region) up by outputting newlines on the
9347 * last line.
9348 */
9349 else if (type == USE_NL)
9350 {
9351 windgoto(cursor_end - 1, 0);
9352 for (i = line_count; --i >= 0; )
9353 out_char('\n'); /* cursor will remain on same line */
9354 }
9355 else
9356 {
9357 for (i = line_count; --i >= 0; )
9358 {
9359 if (type == USE_T_DL)
9360 {
9361 windgoto(cursor_row, 0);
9362 out_str(T_DL); /* delete a line */
9363 }
9364 else /* type == USE_T_CE */
9365 {
9366 windgoto(cursor_row + i, 0);
9367 out_str(T_CE); /* erase a line */
9368 }
9369 screen_start(); /* don't know where cursor is now */
9370 }
9371 }
9372
9373 /*
9374 * If the 'db' flag is set, we need to clear the lines that have been
9375 * scrolled up at the bottom of the region.
9376 */
9377 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9378 {
9379 for (i = line_count; i > 0; --i)
9380 {
9381 windgoto(cursor_end - i, 0);
9382 out_str(T_CE); /* erase a line */
9383 screen_start(); /* don't know where cursor is now */
9384 }
9385 }
9386
9387#ifdef FEAT_GUI
9388 gui_can_update_cursor();
9389 if (gui.in_use)
9390 out_flush(); /* always flush after a scroll */
9391#endif
9392
9393 return OK;
9394}
9395
9396/*
9397 * show the current mode and ruler
9398 *
9399 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9400 * If clear_cmdline is FALSE there may be a message there that needs to be
9401 * cleared only if a mode is shown.
9402 * Return the length of the message (0 if no message).
9403 */
9404 int
9405showmode()
9406{
9407 int need_clear;
9408 int length = 0;
9409 int do_mode;
9410 int attr;
9411 int nwr_save;
9412#ifdef FEAT_INS_EXPAND
9413 int sub_attr;
9414#endif
9415
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009416 do_mode = ((p_smd && msg_silent == 0)
9417 && ((State & INSERT)
9418 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419#ifdef FEAT_VISUAL
9420 || VIsual_active
9421#endif
9422 ));
9423 if (do_mode || Recording)
9424 {
9425 /*
9426 * Don't show mode right now, when not redrawing or inside a mapping.
9427 * Call char_avail() only when we are going to show something, because
9428 * it takes a bit of time.
9429 */
9430 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9431 {
9432 redraw_cmdline = TRUE; /* show mode later */
9433 return 0;
9434 }
9435
9436 nwr_save = need_wait_return;
9437
9438 /* wait a bit before overwriting an important message */
9439 check_for_delay(FALSE);
9440
9441 /* if the cmdline is more than one line high, erase top lines */
9442 need_clear = clear_cmdline;
9443 if (clear_cmdline && cmdline_row < Rows - 1)
9444 msg_clr_cmdline(); /* will reset clear_cmdline */
9445
9446 /* Position on the last line in the window, column 0 */
9447 msg_pos_mode();
9448 cursor_off();
9449 attr = hl_attr(HLF_CM); /* Highlight mode */
9450 if (do_mode)
9451 {
9452 MSG_PUTS_ATTR("--", attr);
9453#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009454 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009455# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009456 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009457# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009458 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009459# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009460 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009461# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 MSG_PUTS_ATTR(" IM", attr);
9463# else
9464 MSG_PUTS_ATTR(" XIM", attr);
9465# endif
9466#endif
9467#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9468 if (gui.in_use)
9469 {
9470 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009471 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 }
9473#endif
9474#ifdef FEAT_INS_EXPAND
9475 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9476 {
9477 /* These messages can get long, avoid a wrap in a narrow
9478 * window. Prefer showing edit_submode_extra. */
9479 length = (Rows - msg_row) * Columns - 3;
9480 if (edit_submode_extra != NULL)
9481 length -= vim_strsize(edit_submode_extra);
9482 if (length > 0)
9483 {
9484 if (edit_submode_pre != NULL)
9485 length -= vim_strsize(edit_submode_pre);
9486 if (length - vim_strsize(edit_submode) > 0)
9487 {
9488 if (edit_submode_pre != NULL)
9489 msg_puts_attr(edit_submode_pre, attr);
9490 msg_puts_attr(edit_submode, attr);
9491 }
9492 if (edit_submode_extra != NULL)
9493 {
9494 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9495 if ((int)edit_submode_highl < (int)HLF_COUNT)
9496 sub_attr = hl_attr(edit_submode_highl);
9497 else
9498 sub_attr = attr;
9499 msg_puts_attr(edit_submode_extra, sub_attr);
9500 }
9501 }
9502 length = 0;
9503 }
9504 else
9505#endif
9506 {
9507#ifdef FEAT_VREPLACE
9508 if (State & VREPLACE_FLAG)
9509 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9510 else
9511#endif
9512 if (State & REPLACE_FLAG)
9513 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9514 else if (State & INSERT)
9515 {
9516#ifdef FEAT_RIGHTLEFT
9517 if (p_ri)
9518 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9519#endif
9520 MSG_PUTS_ATTR(_(" INSERT"), attr);
9521 }
9522 else if (restart_edit == 'I')
9523 MSG_PUTS_ATTR(_(" (insert)"), attr);
9524 else if (restart_edit == 'R')
9525 MSG_PUTS_ATTR(_(" (replace)"), attr);
9526 else if (restart_edit == 'V')
9527 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9528#ifdef FEAT_RIGHTLEFT
9529 if (p_hkmap)
9530 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9531# ifdef FEAT_FKMAP
9532 if (p_fkmap)
9533 MSG_PUTS_ATTR(farsi_text_5, attr);
9534# endif
9535#endif
9536#ifdef FEAT_KEYMAP
9537 if (State & LANGMAP)
9538 {
9539# ifdef FEAT_ARABIC
9540 if (curwin->w_p_arab)
9541 MSG_PUTS_ATTR(_(" Arabic"), attr);
9542 else
9543# endif
9544 MSG_PUTS_ATTR(_(" (lang)"), attr);
9545 }
9546#endif
9547 if ((State & INSERT) && p_paste)
9548 MSG_PUTS_ATTR(_(" (paste)"), attr);
9549
9550#ifdef FEAT_VISUAL
9551 if (VIsual_active)
9552 {
9553 char *p;
9554
9555 /* Don't concatenate separate words to avoid translation
9556 * problems. */
9557 switch ((VIsual_select ? 4 : 0)
9558 + (VIsual_mode == Ctrl_V) * 2
9559 + (VIsual_mode == 'V'))
9560 {
9561 case 0: p = N_(" VISUAL"); break;
9562 case 1: p = N_(" VISUAL LINE"); break;
9563 case 2: p = N_(" VISUAL BLOCK"); break;
9564 case 4: p = N_(" SELECT"); break;
9565 case 5: p = N_(" SELECT LINE"); break;
9566 default: p = N_(" SELECT BLOCK"); break;
9567 }
9568 MSG_PUTS_ATTR(_(p), attr);
9569 }
9570#endif
9571 MSG_PUTS_ATTR(" --", attr);
9572 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009573
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 need_clear = TRUE;
9575 }
9576 if (Recording
9577#ifdef FEAT_INS_EXPAND
9578 && edit_submode == NULL /* otherwise it gets too long */
9579#endif
9580 )
9581 {
9582 MSG_PUTS_ATTR(_("recording"), attr);
9583 need_clear = TRUE;
9584 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009585
9586 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 if (need_clear || clear_cmdline)
9588 msg_clr_eos();
9589 msg_didout = FALSE; /* overwrite this message */
9590 length = msg_col;
9591 msg_col = 0;
9592 need_wait_return = nwr_save; /* never ask for hit-return for this */
9593 }
9594 else if (clear_cmdline && msg_silent == 0)
9595 /* Clear the whole command line. Will reset "clear_cmdline". */
9596 msg_clr_cmdline();
9597
9598#ifdef FEAT_CMDL_INFO
9599# ifdef FEAT_VISUAL
9600 /* In Visual mode the size of the selected area must be redrawn. */
9601 if (VIsual_active)
9602 clear_showcmd();
9603# endif
9604
9605 /* If the last window has no status line, the ruler is after the mode
9606 * message and must be redrawn */
9607 if (redrawing()
9608# ifdef FEAT_WINDOWS
9609 && lastwin->w_status_height == 0
9610# endif
9611 )
9612 win_redr_ruler(lastwin, TRUE);
9613#endif
9614 redraw_cmdline = FALSE;
9615 clear_cmdline = FALSE;
9616
9617 return length;
9618}
9619
9620/*
9621 * Position for a mode message.
9622 */
9623 static void
9624msg_pos_mode()
9625{
9626 msg_col = 0;
9627 msg_row = Rows - 1;
9628}
9629
9630/*
9631 * Delete mode message. Used when ESC is typed which is expected to end
9632 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009633 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 */
9635 void
9636unshowmode(force)
9637 int force;
9638{
9639 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009640 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 */
9642 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9643 redraw_cmdline = TRUE; /* delete mode later */
9644 else
9645 {
9646 msg_pos_mode();
9647 if (Recording)
9648 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9649 msg_clr_eos();
9650 }
9651}
9652
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009653#if defined(FEAT_WINDOWS)
9654/*
9655 * Draw the tab pages line at the top of the Vim window.
9656 */
9657 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009658draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009659{
9660 int tabcount = 0;
9661 tabpage_T *tp;
9662 int tabwidth;
9663 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009664 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009665 int attr;
9666 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009667 win_T *cwp;
9668 int wincount;
9669 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009670 int c;
9671 int len;
9672 int attr_sel = hl_attr(HLF_TPS);
9673 int attr_nosel = hl_attr(HLF_TP);
9674 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009675 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009676 int room;
9677 int use_sep_chars = (t_colors < 8
9678#ifdef FEAT_GUI
9679 && !gui.in_use
9680#endif
9681 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009682
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009683 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009684
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009685#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009686 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009687 if (gui_use_tabline())
9688 {
9689 gui_update_tabline();
9690 return;
9691 }
9692#endif
9693
9694 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009695 return;
9696
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009697#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009698
9699 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9700 for (scol = 0; scol < Columns; ++scol)
9701 TabPageIdxs[scol] = 0;
9702
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009703 /* Use the 'tabline' option if it's set. */
9704 if (*p_tal != NUL)
9705 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009706 int save_called_emsg = called_emsg;
9707
9708 /* Check for an error. If there is one we would loop in redrawing the
9709 * screen. Avoid that by making 'tabline' empty. */
9710 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009711 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009712 if (called_emsg)
9713 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009714 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009715 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009716 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009717 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009718#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009719 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009720 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9721 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009722
Bram Moolenaar238a5642006-02-21 22:12:05 +00009723 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9724 if (tabwidth < 6)
9725 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009726
Bram Moolenaar238a5642006-02-21 22:12:05 +00009727 attr = attr_nosel;
9728 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009729 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009730 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9731 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009732 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009733 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009734
Bram Moolenaar238a5642006-02-21 22:12:05 +00009735 if (tp->tp_topframe == topframe)
9736 attr = attr_sel;
9737 if (use_sep_chars && col > 0)
9738 screen_putchar('|', 0, col++, attr);
9739
9740 if (tp->tp_topframe != topframe)
9741 attr = attr_nosel;
9742
9743 screen_putchar(' ', 0, col++, attr);
9744
9745 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009746 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009747 cwp = curwin;
9748 wp = firstwin;
9749 }
9750 else
9751 {
9752 cwp = tp->tp_curwin;
9753 wp = tp->tp_firstwin;
9754 }
9755
9756 modified = FALSE;
9757 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9758 if (bufIsChanged(wp->w_buffer))
9759 modified = TRUE;
9760 if (modified || wincount > 1)
9761 {
9762 if (wincount > 1)
9763 {
9764 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009765 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009766 if (col + len >= Columns - 3)
9767 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009768 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009769#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009770 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009771#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009772 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009773#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009774 );
9775 col += len;
9776 }
9777 if (modified)
9778 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9779 screen_putchar(' ', 0, col++, attr);
9780 }
9781
9782 room = scol - col + tabwidth - 1;
9783 if (room > 0)
9784 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009785 /* Get buffer name in NameBuff[] */
9786 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009787 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009788 len = vim_strsize(NameBuff);
9789 p = NameBuff;
9790#ifdef FEAT_MBYTE
9791 if (has_mbyte)
9792 while (len > room)
9793 {
9794 len -= ptr2cells(p);
9795 mb_ptr_adv(p);
9796 }
9797 else
9798#endif
9799 if (len > room)
9800 {
9801 p += len - room;
9802 len = room;
9803 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009804 if (len > Columns - col - 1)
9805 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009806
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009807 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009808 col += len;
9809 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009810 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009811
9812 /* Store the tab page number in TabPageIdxs[], so that
9813 * jump_to_mouse() knows where each one is. */
9814 ++tabcount;
9815 while (scol < col)
9816 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009817 }
9818
Bram Moolenaar238a5642006-02-21 22:12:05 +00009819 if (use_sep_chars)
9820 c = '_';
9821 else
9822 c = ' ';
9823 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009824
9825 /* Put an "X" for closing the current tab if there are several. */
9826 if (first_tabpage->tp_next != NULL)
9827 {
9828 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9829 TabPageIdxs[Columns - 1] = -999;
9830 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009831 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009832
9833 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9834 * set. */
9835 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009836}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009837
9838/*
9839 * Get buffer name for "buf" into NameBuff[].
9840 * Takes care of special buffer names and translates special characters.
9841 */
9842 void
9843get_trans_bufname(buf)
9844 buf_T *buf;
9845{
9846 if (buf_spname(buf) != NULL)
9847 STRCPY(NameBuff, buf_spname(buf));
9848 else
9849 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9850 trans_characters(NameBuff, MAXPATHL);
9851}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009852#endif
9853
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9855/*
9856 * Get the character to use in a status line. Get its attributes in "*attr".
9857 */
9858 static int
9859fillchar_status(attr, is_curwin)
9860 int *attr;
9861 int is_curwin;
9862{
9863 int fill;
9864 if (is_curwin)
9865 {
9866 *attr = hl_attr(HLF_S);
9867 fill = fill_stl;
9868 }
9869 else
9870 {
9871 *attr = hl_attr(HLF_SNC);
9872 fill = fill_stlnc;
9873 }
9874 /* Use fill when there is highlighting, and highlighting of current
9875 * window differs, or the fillchars differ, or this is not the
9876 * current window */
9877 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9878 || !is_curwin || firstwin == lastwin)
9879 || (fill_stl != fill_stlnc)))
9880 return fill;
9881 if (is_curwin)
9882 return '^';
9883 return '=';
9884}
9885#endif
9886
9887#ifdef FEAT_VERTSPLIT
9888/*
9889 * Get the character to use in a separator between vertically split windows.
9890 * Get its attributes in "*attr".
9891 */
9892 static int
9893fillchar_vsep(attr)
9894 int *attr;
9895{
9896 *attr = hl_attr(HLF_C);
9897 if (*attr == 0 && fill_vert == ' ')
9898 return '|';
9899 else
9900 return fill_vert;
9901}
9902#endif
9903
9904/*
9905 * Return TRUE if redrawing should currently be done.
9906 */
9907 int
9908redrawing()
9909{
9910 return (!RedrawingDisabled
9911 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9912}
9913
9914/*
9915 * Return TRUE if printing messages should currently be done.
9916 */
9917 int
9918messaging()
9919{
9920 return (!(p_lz && char_avail() && !KeyTyped));
9921}
9922
9923/*
9924 * Show current status info in ruler and various other places
9925 * If always is FALSE, only show ruler if position has changed.
9926 */
9927 void
9928showruler(always)
9929 int always;
9930{
9931 if (!always && !redrawing())
9932 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009933#ifdef FEAT_INS_EXPAND
9934 if (pum_visible())
9935 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009936# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009937 /* Don't redraw right now, do it later. */
9938 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009939# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009940 return;
9941 }
9942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009944 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009945 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009946 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 else
9949#endif
9950#ifdef FEAT_CMDL_INFO
9951 win_redr_ruler(curwin, always);
9952#endif
9953
9954#ifdef FEAT_TITLE
9955 if (need_maketitle
9956# ifdef FEAT_STL_OPT
9957 || (p_icon && (stl_syntax & STL_IN_ICON))
9958 || (p_title && (stl_syntax & STL_IN_TITLE))
9959# endif
9960 )
9961 maketitle();
9962#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009963#ifdef FEAT_WINDOWS
9964 /* Redraw the tab pages line if needed. */
9965 if (redraw_tabline)
9966 draw_tabline();
9967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968}
9969
9970#ifdef FEAT_CMDL_INFO
9971 static void
9972win_redr_ruler(wp, always)
9973 win_T *wp;
9974 int always;
9975{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009976#define RULER_BUF_LEN 70
9977 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 int row;
9979 int fillchar;
9980 int attr;
9981 int empty_line = FALSE;
9982 colnr_T virtcol;
9983 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009984 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 int o;
9986#ifdef FEAT_VERTSPLIT
9987 int this_ru_col;
9988 int off = 0;
9989 int width = Columns;
9990# define WITH_OFF(x) x
9991# define WITH_WIDTH(x) x
9992#else
9993# define WITH_OFF(x) 0
9994# define WITH_WIDTH(x) Columns
9995# define this_ru_col ru_col
9996#endif
9997
9998 /* If 'ruler' off or redrawing disabled, don't do anything */
9999 if (!p_ru)
10000 return;
10001
10002 /*
10003 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10004 * after deleting lines, before cursor.lnum is corrected.
10005 */
10006 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10007 return;
10008
10009#ifdef FEAT_INS_EXPAND
10010 /* Don't draw the ruler while doing insert-completion, it might overwrite
10011 * the (long) mode message. */
10012# ifdef FEAT_WINDOWS
10013 if (wp == lastwin && lastwin->w_status_height == 0)
10014# endif
10015 if (edit_submode != NULL)
10016 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010017 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10018 if (pum_visible())
10019 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020#endif
10021
10022#ifdef FEAT_STL_OPT
10023 if (*p_ruf)
10024 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010025 int save_called_emsg = called_emsg;
10026
10027 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010029 if (called_emsg)
10030 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010031 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010032 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 return;
10034 }
10035#endif
10036
10037 /*
10038 * Check if not in Insert mode and the line is empty (will show "0-1").
10039 */
10040 if (!(State & INSERT)
10041 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10042 empty_line = TRUE;
10043
10044 /*
10045 * Only draw the ruler when something changed.
10046 */
10047 validate_virtcol_win(wp);
10048 if ( redraw_cmdline
10049 || always
10050 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10051 || wp->w_cursor.col != wp->w_ru_cursor.col
10052 || wp->w_virtcol != wp->w_ru_virtcol
10053#ifdef FEAT_VIRTUALEDIT
10054 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10055#endif
10056 || wp->w_topline != wp->w_ru_topline
10057 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10058#ifdef FEAT_DIFF
10059 || wp->w_topfill != wp->w_ru_topfill
10060#endif
10061 || empty_line != wp->w_ru_empty)
10062 {
10063 cursor_off();
10064#ifdef FEAT_WINDOWS
10065 if (wp->w_status_height)
10066 {
10067 row = W_WINROW(wp) + wp->w_height;
10068 fillchar = fillchar_status(&attr, wp == curwin);
10069# ifdef FEAT_VERTSPLIT
10070 off = W_WINCOL(wp);
10071 width = W_WIDTH(wp);
10072# endif
10073 }
10074 else
10075#endif
10076 {
10077 row = Rows - 1;
10078 fillchar = ' ';
10079 attr = 0;
10080#ifdef FEAT_VERTSPLIT
10081 width = Columns;
10082 off = 0;
10083#endif
10084 }
10085
10086 /* In list mode virtcol needs to be recomputed */
10087 virtcol = wp->w_virtcol;
10088 if (wp->w_p_list && lcs_tab1 == NUL)
10089 {
10090 wp->w_p_list = FALSE;
10091 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10092 wp->w_p_list = TRUE;
10093 }
10094
10095 /*
10096 * Some sprintfs return the length, some return a pointer.
10097 * To avoid portability problems we use strlen() here.
10098 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010099 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10101 ? 0L
10102 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010103 len = STRLEN(buffer);
10104 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10106 (int)virtcol + 1);
10107
10108 /*
10109 * Add a "50%" if there is room for it.
10110 * On the last line, don't print in the last column (scrolls the
10111 * screen up on some terminals).
10112 */
10113 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010114 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 o = i + vim_strsize(buffer + i + 1);
10116#ifdef FEAT_WINDOWS
10117 if (wp->w_status_height == 0) /* can't use last char of screen */
10118#endif
10119 ++o;
10120#ifdef FEAT_VERTSPLIT
10121 this_ru_col = ru_col - (Columns - width);
10122 if (this_ru_col < 0)
10123 this_ru_col = 0;
10124#endif
10125 /* Never use more than half the window/screen width, leave the other
10126 * half for the filename. */
10127 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10128 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10129 if (this_ru_col + o < WITH_WIDTH(width))
10130 {
10131 while (this_ru_col + o < WITH_WIDTH(width))
10132 {
10133#ifdef FEAT_MBYTE
10134 if (has_mbyte)
10135 i += (*mb_char2bytes)(fillchar, buffer + i);
10136 else
10137#endif
10138 buffer[i++] = fillchar;
10139 ++o;
10140 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010141 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 }
10143 /* Truncate at window boundary. */
10144#ifdef FEAT_MBYTE
10145 if (has_mbyte)
10146 {
10147 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010148 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 {
10150 o += (*mb_ptr2cells)(buffer + i);
10151 if (this_ru_col + o > WITH_WIDTH(width))
10152 {
10153 buffer[i] = NUL;
10154 break;
10155 }
10156 }
10157 }
10158 else
10159#endif
10160 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10161 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10162
10163 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10164 i = redraw_cmdline;
10165 screen_fill(row, row + 1,
10166 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10167 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10168 fillchar, fillchar, attr);
10169 /* don't redraw the cmdline because of showing the ruler */
10170 redraw_cmdline = i;
10171 wp->w_ru_cursor = wp->w_cursor;
10172 wp->w_ru_virtcol = wp->w_virtcol;
10173 wp->w_ru_empty = empty_line;
10174 wp->w_ru_topline = wp->w_topline;
10175 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10176#ifdef FEAT_DIFF
10177 wp->w_ru_topfill = wp->w_topfill;
10178#endif
10179 }
10180}
10181#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010182
10183#if defined(FEAT_LINEBREAK) || defined(PROTO)
10184/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010185 * Return the width of the 'number' and 'relativenumber' column.
10186 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010187 * Otherwise it depends on 'numberwidth' and the line count.
10188 */
10189 int
10190number_width(wp)
10191 win_T *wp;
10192{
10193 int n;
10194 linenr_T lnum;
10195
Bram Moolenaar64486672010-05-16 15:46:46 +020010196 if (wp->w_p_nu)
10197 /* 'number' */
10198 lnum = wp->w_buffer->b_ml.ml_line_count;
10199 else
10200 /* 'relativenumber' */
10201 lnum = wp->w_height;
10202
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010203 if (lnum == wp->w_nrwidth_line_count)
10204 return wp->w_nrwidth_width;
10205 wp->w_nrwidth_line_count = lnum;
10206
10207 n = 0;
10208 do
10209 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010210 lnum /= 10;
10211 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010212 } while (lnum > 0);
10213
10214 /* 'numberwidth' gives the minimal width plus one */
10215 if (n < wp->w_p_nuw - 1)
10216 n = wp->w_p_nuw - 1;
10217
10218 wp->w_nrwidth_width = n;
10219 return n;
10220}
10221#endif