blob: 71eb16fca595b50d0eaeebf6d0a758fd5a41657a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
92/*
93 * The attributes that are actually active for writing to the screen.
94 */
95static int screen_attr = 0;
96
97/*
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
100 */
101static int screen_cur_row, screen_cur_col; /* last known cursor position */
102
103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106
107#ifdef FEAT_FOLDING
108static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109#endif
110
111/*
112 * Buffer for one screen line (characters and attributes).
113 */
114static schar_T *current_ScreenLine;
115
116static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000117static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_FOLDING
119static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000123static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125#ifdef FEAT_RIGHTLEFT
126static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128#else
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_VERTSPLIT
133static void draw_vsep_win __ARGS((win_T *wp, int row));
134#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000135#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000136static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000139#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void start_search_hl __ARGS((void));
141static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200142static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
144static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
145#endif
146static void screen_start_highlight __ARGS((int attr));
147static void screen_char __ARGS((unsigned off, int row, int col));
148#ifdef FEAT_MBYTE
149static void screen_char_2 __ARGS((unsigned off, int row, int col));
150#endif
151static void screenclear2 __ARGS((void));
152static void lineclear __ARGS((unsigned off, int width));
153static void lineinvalid __ARGS((unsigned off, int width));
154#ifdef FEAT_VERTSPLIT
155static void linecopy __ARGS((int to, int from, win_T *wp));
156static void redraw_block __ARGS((int row, int end, win_T *wp));
157#endif
158static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
159static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000162static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
165static int fillchar_status __ARGS((int *attr, int is_curwin));
166#endif
167#ifdef FEAT_VERTSPLIT
168static int fillchar_vsep __ARGS((int *attr));
169#endif
170#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000171static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_CMDL_INFO
174static void win_redr_ruler __ARGS((win_T *wp, int always));
175#endif
176
177#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
178/* Ugly global: overrule attribute used by screen_char() */
179static int screen_char_attr = 0;
180#endif
181
182/*
183 * Redraw the current window later, with update_screen(type).
184 * Set must_redraw only if not already set to a higher value.
185 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
186 */
187 void
188redraw_later(type)
189 int type;
190{
191 redraw_win_later(curwin, type);
192}
193
194 void
195redraw_win_later(wp, type)
196 win_T *wp;
197 int type;
198{
199 if (wp->w_redr_type < type)
200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
214redraw_later_clear()
215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
232redraw_all_later(type)
233 int type;
234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
247redraw_curbuf_later(type)
248 int type;
249{
250 redraw_buf_later(curbuf, type);
251}
252
253 void
254redraw_buf_later(buf, type)
255 buf_T *buf;
256 int type;
257{
258 win_T *wp;
259
260 FOR_ALL_WINDOWS(wp)
261 {
262 if (wp->w_buffer == buf)
263 redraw_win_later(wp, type);
264 }
265}
266
267/*
268 * Changed something in the current window, at buffer line "lnum", that
269 * requires that line and possibly other lines to be redrawn.
270 * Used when entering/leaving Insert mode with the cursor on a folded line.
271 * Used to remove the "$" from a change command.
272 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
273 * may become invalid and the whole window will have to be redrawn.
274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 void
276redrawWinline(lnum, invalid)
277 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000278 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280#ifdef FEAT_FOLDING
281 int i;
282#endif
283
284 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
285 curwin->w_redraw_top = lnum;
286 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
287 curwin->w_redraw_bot = lnum;
288 redraw_later(VALID);
289
290#ifdef FEAT_FOLDING
291 if (invalid)
292 {
293 /* A w_lines[] entry for this lnum has become invalid. */
294 i = find_wl_entry(curwin, lnum);
295 if (i >= 0)
296 curwin->w_lines[i].wl_valid = FALSE;
297 }
298#endif
299}
300
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200301#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200302 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * update all windows that are editing the current buffer
305 */
306 void
307update_curbuf(type)
308 int type;
309{
310 redraw_curbuf_later(type);
311 update_screen(type);
312}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * update_screen()
317 *
318 * Based on the current value of curwin->w_topline, transfer a screenfull
319 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
320 */
321 void
322update_screen(type)
323 int type;
324{
325 win_T *wp;
326 static int did_intro = FALSE;
327#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
328 int did_one;
329#endif
330
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000331 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 if (!screen_valid(TRUE))
333 return;
334
335 if (must_redraw)
336 {
337 if (type < must_redraw) /* use maximal type */
338 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000339
340 /* must_redraw is reset here, so that when we run into some weird
341 * reason to redraw while busy redrawing (e.g., asynchronous
342 * scrolling), or update_topline() in win_update() will cause a
343 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 must_redraw = 0;
345 }
346
347 /* Need to update w_lines[]. */
348 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
349 type = NOT_VALID;
350
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000351 /* Postpone the redrawing when it's not needed and when being called
352 * recursively. */
353 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 redraw_later(type); /* remember type for next time */
356 must_redraw = type;
357 if (type > INVERTED_ALL)
358 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
359 return;
360 }
361
362 updating_screen = TRUE;
363#ifdef FEAT_SYN_HL
364 ++display_tick; /* let syntax code know we're in a next round of
365 * display updating */
366#endif
367
368 /*
369 * if the screen was scrolled up when displaying a message, scroll it down
370 */
371 if (msg_scrolled)
372 {
373 clear_cmdline = TRUE;
374 if (msg_scrolled > Rows - 5) /* clearing is faster */
375 type = CLEAR;
376 else if (type != CLEAR)
377 {
378 check_for_delay(FALSE);
379 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
380 type = CLEAR;
381 FOR_ALL_WINDOWS(wp)
382 {
383 if (W_WINROW(wp) < msg_scrolled)
384 {
385 if (W_WINROW(wp) + wp->w_height > msg_scrolled
386 && wp->w_redr_type < REDRAW_TOP
387 && wp->w_lines_valid > 0
388 && wp->w_topline == wp->w_lines[0].wl_lnum)
389 {
390 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
391 wp->w_redr_type = REDRAW_TOP;
392 }
393 else
394 {
395 wp->w_redr_type = NOT_VALID;
396#ifdef FEAT_WINDOWS
397 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
398 <= msg_scrolled)
399 wp->w_redr_status = TRUE;
400#endif
401 }
402 }
403 }
404 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000406 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409 msg_scrolled = 0;
410 need_wait_return = FALSE;
411 }
412
413 /* reset cmdline_row now (may have been changed temporarily) */
414 compute_cmdrow();
415
416 /* Check for changed highlighting */
417 if (need_highlight_changed)
418 highlight_changed();
419
420 if (type == CLEAR) /* first clear screen */
421 {
422 screenclear(); /* will reset clear_cmdline */
423 type = NOT_VALID;
424 }
425
426 if (clear_cmdline) /* going to clear cmdline (done below) */
427 check_for_delay(FALSE);
428
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000429#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200430 /* Force redraw when width of 'number' or 'relativenumber' column
431 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
434 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 curwin->w_redr_type = NOT_VALID;
436#endif
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 /*
439 * Only start redrawing if there is really something to do.
440 */
441 if (type == INVERTED)
442 update_curswant();
443 if (curwin->w_redr_type < type
444 && !((type == VALID
445 && curwin->w_lines[0].wl_valid
446#ifdef FEAT_DIFF
447 && curwin->w_topfill == curwin->w_old_topfill
448 && curwin->w_botfill == curwin->w_old_botfill
449#endif
450 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
451#ifdef FEAT_VISUAL
452 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000453 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
455 && curwin->w_old_visual_mode == VIsual_mode
456 && (curwin->w_valid & VALID_VIRTCOL)
457 && curwin->w_old_curswant == curwin->w_curswant)
458#endif
459 ))
460 curwin->w_redr_type = type;
461
Bram Moolenaar5a305422006-04-28 22:38:25 +0000462#ifdef FEAT_WINDOWS
463 /* Redraw the tab pages line if needed. */
464 if (redraw_tabline || type >= NOT_VALID)
465 draw_tabline();
466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#ifdef FEAT_SYN_HL
469 /*
470 * Correct stored syntax highlighting info for changes in each displayed
471 * buffer. Each buffer must only be done once.
472 */
473 FOR_ALL_WINDOWS(wp)
474 {
475 if (wp->w_buffer->b_mod_set)
476 {
477# ifdef FEAT_WINDOWS
478 win_T *wwp;
479
480 /* Check if we already did this buffer. */
481 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
482 if (wwp->w_buffer == wp->w_buffer)
483 break;
484# endif
485 if (
486# ifdef FEAT_WINDOWS
487 wwp == wp &&
488# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200489 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 syn_stack_apply_changes(wp->w_buffer);
491 }
492 }
493#endif
494
495 /*
496 * Go from top to bottom through the windows, redrawing the ones that need
497 * it.
498 */
499#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
500 did_one = FALSE;
501#endif
502#ifdef FEAT_SEARCH_EXTRA
503 search_hl.rm.regprog = NULL;
504#endif
505 FOR_ALL_WINDOWS(wp)
506 {
507 if (wp->w_redr_type != 0)
508 {
509 cursor_off();
510#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
511 if (!did_one)
512 {
513 did_one = TRUE;
514# ifdef FEAT_SEARCH_EXTRA
515 start_search_hl();
516# endif
517# ifdef FEAT_CLIPBOARD
518 /* When Visual area changed, may have to update selection. */
519 if (clip_star.available && clip_isautosel())
520 clip_update_selection();
521# endif
522#ifdef FEAT_GUI
523 /* Remove the cursor before starting to do anything, because
524 * scrolling may make it difficult to redraw the text under
525 * it. */
526 if (gui.in_use)
527 gui_undraw_cursor();
528#endif
529 }
530#endif
531 win_update(wp);
532 }
533
534#ifdef FEAT_WINDOWS
535 /* redraw status line after the window to minimize cursor movement */
536 if (wp->w_redr_status)
537 {
538 cursor_off();
539 win_redr_status(wp);
540 }
541#endif
542 }
543#if defined(FEAT_SEARCH_EXTRA)
544 end_search_hl();
545#endif
546
547#ifdef FEAT_WINDOWS
548 /* Reset b_mod_set flags. Going through all windows is probably faster
549 * than going through all buffers (there could be many buffers). */
550 for (wp = firstwin; wp != NULL; wp = wp->w_next)
551 wp->w_buffer->b_mod_set = FALSE;
552#else
553 curbuf->b_mod_set = FALSE;
554#endif
555
556 updating_screen = FALSE;
557#ifdef FEAT_GUI
558 gui_may_resize_shell();
559#endif
560
561 /* Clear or redraw the command line. Done last, because scrolling may
562 * mess up the command line. */
563 if (clear_cmdline || redraw_cmdline)
564 showmode();
565
566 /* May put up an introductory message when not editing a file */
567 if (!did_intro && bufempty()
568 && curbuf->b_fname == NULL
569#ifdef FEAT_WINDOWS
570 && firstwin->w_next == NULL
571#endif
572 && vim_strchr(p_shm, SHM_INTRO) == NULL)
573 intro_message(FALSE);
574 did_intro = TRUE;
575
576#ifdef FEAT_GUI
577 /* Redraw the cursor and update the scrollbars when all screen updating is
578 * done. */
579 if (gui.in_use)
580 {
581 out_flush(); /* required before updating the cursor */
582 if (did_one)
583 gui_update_cursor(FALSE, FALSE);
584 gui_update_scrollbars(FALSE);
585 }
586#endif
587}
588
Bram Moolenaar860cae12010-06-05 23:22:07 +0200589#if defined(FEAT_CONCEAL) || defined(PROTO)
590 void
591update_single_line(wp, lnum)
592 win_T *wp;
593 linenr_T lnum;
594{
595 int row;
596 int j;
597
598 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200599 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200600 {
601# ifdef FEAT_GUI
602 /* Remove the cursor before starting to do anything, because scrolling
603 * may make it difficult to redraw the text under it. */
604 if (gui.in_use)
605 gui_undraw_cursor();
606# endif
607 row = 0;
608 for (j = 0; j < wp->w_lines_valid; ++j)
609 {
610 if (lnum == wp->w_lines[j].wl_lnum)
611 {
612 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200613# ifdef FEAT_SEARCH_EXTRA
614 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200615 start_search_hl();
616 prepare_search_hl(wp, lnum);
617# endif
618 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
619# if defined(FEAT_SEARCH_EXTRA)
620 end_search_hl();
621# endif
622 break;
623 }
624 row += wp->w_lines[j].wl_size;
625 }
626# ifdef FEAT_GUI
627 /* Redraw the cursor */
628 if (gui.in_use)
629 {
630 out_flush(); /* required before updating the cursor */
631 gui_update_cursor(FALSE, FALSE);
632 }
633# endif
634 }
635}
636#endif
637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
639static void update_prepare __ARGS((void));
640static void update_finish __ARGS((void));
641
642/*
643 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000644 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 */
646 static void
647update_prepare()
648{
649 cursor_off();
650 updating_screen = TRUE;
651#ifdef FEAT_GUI
652 /* Remove the cursor before starting to do anything, because scrolling may
653 * make it difficult to redraw the text under it. */
654 if (gui.in_use)
655 gui_undraw_cursor();
656#endif
657#ifdef FEAT_SEARCH_EXTRA
658 start_search_hl();
659#endif
660}
661
662/*
663 * Finish updating one or more windows.
664 */
665 static void
666update_finish()
667{
668 if (redraw_cmdline)
669 showmode();
670
671# ifdef FEAT_SEARCH_EXTRA
672 end_search_hl();
673# endif
674
675 updating_screen = FALSE;
676
677# ifdef FEAT_GUI
678 gui_may_resize_shell();
679
680 /* Redraw the cursor and update the scrollbars when all screen updating is
681 * done. */
682 if (gui.in_use)
683 {
684 out_flush(); /* required before updating the cursor */
685 gui_update_cursor(FALSE, FALSE);
686 gui_update_scrollbars(FALSE);
687 }
688# endif
689}
690#endif
691
692#if defined(FEAT_SIGNS) || defined(PROTO)
693 void
694update_debug_sign(buf, lnum)
695 buf_T *buf;
696 linenr_T lnum;
697{
698 win_T *wp;
699 int doit = FALSE;
700
701# ifdef FEAT_FOLDING
702 win_foldinfo.fi_level = 0;
703# endif
704
705 /* update/delete a specific mark */
706 FOR_ALL_WINDOWS(wp)
707 {
708 if (buf != NULL && lnum > 0)
709 {
710 if (wp->w_buffer == buf && lnum >= wp->w_topline
711 && lnum < wp->w_botline)
712 {
713 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
714 wp->w_redraw_top = lnum;
715 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
716 wp->w_redraw_bot = lnum;
717 redraw_win_later(wp, VALID);
718 }
719 }
720 else
721 redraw_win_later(wp, VALID);
722 if (wp->w_redr_type != 0)
723 doit = TRUE;
724 }
725
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000726 /* Return when there is nothing to do or screen updating already
727 * happening. */
728 if (!doit || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 return;
730
731 /* update all windows that need updating */
732 update_prepare();
733
734# ifdef FEAT_WINDOWS
735 for (wp = firstwin; wp; wp = wp->w_next)
736 {
737 if (wp->w_redr_type != 0)
738 win_update(wp);
739 if (wp->w_redr_status)
740 win_redr_status(wp);
741 }
742# else
743 if (curwin->w_redr_type != 0)
744 win_update(curwin);
745# endif
746
747 update_finish();
748}
749#endif
750
751
752#if defined(FEAT_GUI) || defined(PROTO)
753/*
754 * Update a single window, its status line and maybe the command line msg.
755 * Used for the GUI scrollbar.
756 */
757 void
758updateWindow(wp)
759 win_T *wp;
760{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000761 /* return if already busy updating */
762 if (updating_screen)
763 return;
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 update_prepare();
766
767#ifdef FEAT_CLIPBOARD
768 /* When Visual area changed, may have to update selection. */
769 if (clip_star.available && clip_isautosel())
770 clip_update_selection();
771#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000776 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000777 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000778 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (wp->w_redr_status
781# ifdef FEAT_CMDL_INFO
782 || p_ru
783# endif
784# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000785 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# endif
787 )
788 win_redr_status(wp);
789#endif
790
791 update_finish();
792}
793#endif
794
795/*
796 * Update a single window.
797 *
798 * This may cause the windows below it also to be redrawn (when clearing the
799 * screen or scrolling lines).
800 *
801 * How the window is redrawn depends on wp->w_redr_type. Each type also
802 * implies the one below it.
803 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000804 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
806 * INVERTED redraw the changed part of the Visual area
807 * INVERTED_ALL redraw the whole Visual area
808 * VALID 1. scroll up/down to adjust for a changed w_topline
809 * 2. update lines at the top when scrolled down
810 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000811 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 * b_mod_top and b_mod_bot.
813 * - if wp->w_redraw_top non-zero, redraw lines between
814 * wp->w_redraw_top and wp->w_redr_bot.
815 * - continue redrawing when syntax status is invalid.
816 * 4. if scrolled up, update lines at the bottom.
817 * This results in three areas that may need updating:
818 * top: from first row to top_end (when scrolled down)
819 * mid: from mid_start to mid_end (update inversion or changed text)
820 * bot: from bot_start to last row (when scrolled up)
821 */
822 static void
823win_update(wp)
824 win_T *wp;
825{
826 buf_T *buf = wp->w_buffer;
827 int type;
828 int top_end = 0; /* Below last row of the top area that needs
829 updating. 0 when no top area updating. */
830 int mid_start = 999;/* first row of the mid area that needs
831 updating. 999 when no mid area updating. */
832 int mid_end = 0; /* Below last row of the mid area that needs
833 updating. 0 when no mid area updating. */
834 int bot_start = 999;/* first row of the bot area that needs
835 updating. 999 when no bot area updating */
836#ifdef FEAT_VISUAL
837 int scrolled_down = FALSE; /* TRUE when scrolled down when
838 w_topline got smaller a bit */
839#endif
840#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000841 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 int top_to_mod = FALSE; /* redraw above mod_top */
843#endif
844
845 int row; /* current window row to display */
846 linenr_T lnum; /* current buffer lnum to display */
847 int idx; /* current index in w_lines[] */
848 int srow; /* starting row of the current line */
849
850 int eof = FALSE; /* if TRUE, we hit the end of the file */
851 int didline = FALSE; /* if TRUE, we finished the last line */
852 int i;
853 long j;
854 static int recursive = FALSE; /* being called recursively */
855 int old_botline = wp->w_botline;
856#ifdef FEAT_FOLDING
857 long fold_count;
858#endif
859#ifdef FEAT_SYN_HL
860 /* remember what happened to the previous line, to know if
861 * check_visual_highlight() can be used */
862#define DID_NONE 1 /* didn't update a line */
863#define DID_LINE 2 /* updated a normal line */
864#define DID_FOLD 3 /* updated a folded line */
865 int did_update = DID_NONE;
866 linenr_T syntax_last_parsed = 0; /* last parsed text line */
867#endif
868 linenr_T mod_top = 0;
869 linenr_T mod_bot = 0;
870#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
871 int save_got_int;
872#endif
873
874 type = wp->w_redr_type;
875
876 if (type == NOT_VALID)
877 {
878#ifdef FEAT_WINDOWS
879 wp->w_redr_status = TRUE;
880#endif
881 wp->w_lines_valid = 0;
882 }
883
884 /* Window is zero-height: nothing to draw. */
885 if (wp->w_height == 0)
886 {
887 wp->w_redr_type = 0;
888 return;
889 }
890
891#ifdef FEAT_VERTSPLIT
892 /* Window is zero-width: Only need to draw the separator. */
893 if (wp->w_width == 0)
894 {
895 /* draw the vertical separator right of this window */
896 draw_vsep_win(wp, 0);
897 wp->w_redr_type = 0;
898 return;
899 }
900#endif
901
902#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200903 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000906#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200907 /* Force redraw when width of 'number' or 'relativenumber' column
908 * changes. */
909 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000910 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000911 {
912 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000913 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000914 }
915 else
916#endif
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
919 {
920 /*
921 * When there are both inserted/deleted lines and specific lines to be
922 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
923 * everything (only happens when redrawing is off for while).
924 */
925 type = NOT_VALID;
926 }
927 else
928 {
929 /*
930 * Set mod_top to the first line that needs displaying because of
931 * changes. Set mod_bot to the first line after the changes.
932 */
933 mod_top = wp->w_redraw_top;
934 if (wp->w_redraw_bot != 0)
935 mod_bot = wp->w_redraw_bot + 1;
936 else
937 mod_bot = 0;
938 wp->w_redraw_top = 0; /* reset for next time */
939 wp->w_redraw_bot = 0;
940 if (buf->b_mod_set)
941 {
942 if (mod_top == 0 || mod_top > buf->b_mod_top)
943 {
944 mod_top = buf->b_mod_top;
945#ifdef FEAT_SYN_HL
946 /* Need to redraw lines above the change that may be included
947 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200950 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (mod_top < 1)
952 mod_top = 1;
953 }
954#endif
955 }
956 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
957 mod_bot = buf->b_mod_bot;
958
959#ifdef FEAT_SEARCH_EXTRA
960 /* When 'hlsearch' is on and using a multi-line search pattern, a
961 * change in one line may make the Search highlighting in a
962 * previous line invalid. Simple solution: redraw all visible
963 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000964 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000966 if (search_hl.rm.regprog != NULL
967 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000969 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000970 {
971 cur = wp->w_match_head;
972 while (cur != NULL)
973 {
974 if (cur->match.regprog != NULL
975 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000976 {
977 top_to_mod = TRUE;
978 break;
979 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000980 cur = cur->next;
981 }
982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#endif
984 }
985#ifdef FEAT_FOLDING
986 if (mod_top != 0 && hasAnyFolding(wp))
987 {
988 linenr_T lnumt, lnumb;
989
990 /*
991 * A change in a line can cause lines above it to become folded or
992 * unfolded. Find the top most buffer line that may be affected.
993 * If the line was previously folded and displayed, get the first
994 * line of that fold. If the line is folded now, get the first
995 * folded line. Use the minimum of these two.
996 */
997
998 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
999 * the line below it. If there is no valid entry, use w_topline.
1000 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1001 * to this line. If there is no valid entry, use MAXLNUM. */
1002 lnumt = wp->w_topline;
1003 lnumb = MAXLNUM;
1004 for (i = 0; i < wp->w_lines_valid; ++i)
1005 if (wp->w_lines[i].wl_valid)
1006 {
1007 if (wp->w_lines[i].wl_lastlnum < mod_top)
1008 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1009 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1010 {
1011 lnumb = wp->w_lines[i].wl_lnum;
1012 /* When there is a fold column it might need updating
1013 * in the next line ("J" just above an open fold). */
1014 if (wp->w_p_fdc > 0)
1015 ++lnumb;
1016 }
1017 }
1018
1019 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1020 if (mod_top > lnumt)
1021 mod_top = lnumt;
1022
1023 /* Now do the same for the bottom line (one above mod_bot). */
1024 --mod_bot;
1025 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1026 ++mod_bot;
1027 if (mod_bot < lnumb)
1028 mod_bot = lnumb;
1029 }
1030#endif
1031
1032 /* When a change starts above w_topline and the end is below
1033 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001034 * If the end of the change is above w_topline: do like no change was
1035 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (mod_top != 0 && mod_top < wp->w_topline)
1037 {
1038 if (mod_bot > wp->w_topline)
1039 mod_top = wp->w_topline;
1040#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001041 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 top_end = 1;
1043#endif
1044 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001045
1046 /* When line numbers are displayed need to redraw all lines below
1047 * inserted/deleted lines. */
1048 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1049 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 }
1051
1052 /*
1053 * When only displaying the lines at the top, set top_end. Used when
1054 * window has scrolled down for msg_scrolled.
1055 */
1056 if (type == REDRAW_TOP)
1057 {
1058 j = 0;
1059 for (i = 0; i < wp->w_lines_valid; ++i)
1060 {
1061 j += wp->w_lines[i].wl_size;
1062 if (j >= wp->w_upd_rows)
1063 {
1064 top_end = j;
1065 break;
1066 }
1067 }
1068 if (top_end == 0)
1069 /* not found (cannot happen?): redraw everything */
1070 type = NOT_VALID;
1071 else
1072 /* top area defined, the rest is VALID */
1073 type = VALID;
1074 }
1075
Bram Moolenaar367329b2007-08-30 11:53:22 +00001076 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001077 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1078 * non-zero and thus not FALSE) will indicate that screenclear() was not
1079 * called. */
1080 if (screen_cleared)
1081 screen_cleared = MAYBE;
1082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 /*
1084 * If there are no changes on the screen that require a complete redraw,
1085 * handle three cases:
1086 * 1: we are off the top of the screen by a few lines: scroll down
1087 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1088 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1089 * w_lines[] that needs updating.
1090 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001091 if ((type == VALID || type == SOME_VALID
1092 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#ifdef FEAT_DIFF
1094 && !wp->w_botfill && !wp->w_old_botfill
1095#endif
1096 )
1097 {
1098 if (mod_top != 0 && wp->w_topline == mod_top)
1099 {
1100 /*
1101 * w_topline is the first changed line, the scrolling will be done
1102 * further down.
1103 */
1104 }
1105 else if (wp->w_lines[0].wl_valid
1106 && (wp->w_topline < wp->w_lines[0].wl_lnum
1107#ifdef FEAT_DIFF
1108 || (wp->w_topline == wp->w_lines[0].wl_lnum
1109 && wp->w_topfill > wp->w_old_topfill)
1110#endif
1111 ))
1112 {
1113 /*
1114 * New topline is above old topline: May scroll down.
1115 */
1116#ifdef FEAT_FOLDING
1117 if (hasAnyFolding(wp))
1118 {
1119 linenr_T ln;
1120
1121 /* count the number of lines we are off, counting a sequence
1122 * of folded lines as one */
1123 j = 0;
1124 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1125 {
1126 ++j;
1127 if (j >= wp->w_height - 2)
1128 break;
1129 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1130 }
1131 }
1132 else
1133#endif
1134 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1135 if (j < wp->w_height - 2) /* not too far off */
1136 {
1137 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1138#ifdef FEAT_DIFF
1139 /* insert extra lines for previously invisible filler lines */
1140 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1141 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1142 - wp->w_old_topfill;
1143#endif
1144 if (i < wp->w_height - 2) /* less than a screen off */
1145 {
1146 /*
1147 * Try to insert the correct number of lines.
1148 * If not the last window, delete the lines at the bottom.
1149 * win_ins_lines may fail when the terminal can't do it.
1150 */
1151 if (i > 0)
1152 check_for_delay(FALSE);
1153 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1154 {
1155 if (wp->w_lines_valid != 0)
1156 {
1157 /* Need to update rows that are new, stop at the
1158 * first one that scrolled down. */
1159 top_end = i;
1160#ifdef FEAT_VISUAL
1161 scrolled_down = TRUE;
1162#endif
1163
1164 /* Move the entries that were scrolled, disable
1165 * the entries for the lines to be redrawn. */
1166 if ((wp->w_lines_valid += j) > wp->w_height)
1167 wp->w_lines_valid = wp->w_height;
1168 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1169 wp->w_lines[idx] = wp->w_lines[idx - j];
1170 while (idx >= 0)
1171 wp->w_lines[idx--].wl_valid = FALSE;
1172 }
1173 }
1174 else
1175 mid_start = 0; /* redraw all lines */
1176 }
1177 else
1178 mid_start = 0; /* redraw all lines */
1179 }
1180 else
1181 mid_start = 0; /* redraw all lines */
1182 }
1183 else
1184 {
1185 /*
1186 * New topline is at or below old topline: May scroll up.
1187 * When topline didn't change, find first entry in w_lines[] that
1188 * needs updating.
1189 */
1190
1191 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1192 j = -1;
1193 row = 0;
1194 for (i = 0; i < wp->w_lines_valid; i++)
1195 {
1196 if (wp->w_lines[i].wl_valid
1197 && wp->w_lines[i].wl_lnum == wp->w_topline)
1198 {
1199 j = i;
1200 break;
1201 }
1202 row += wp->w_lines[i].wl_size;
1203 }
1204 if (j == -1)
1205 {
1206 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1207 * lines */
1208 mid_start = 0;
1209 }
1210 else
1211 {
1212 /*
1213 * Try to delete the correct number of lines.
1214 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1215 */
1216#ifdef FEAT_DIFF
1217 /* If the topline didn't change, delete old filler lines,
1218 * otherwise delete filler lines of the new topline... */
1219 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1220 row += wp->w_old_topfill;
1221 else
1222 row += diff_check_fill(wp, wp->w_topline);
1223 /* ... but don't delete new filler lines. */
1224 row -= wp->w_topfill;
1225#endif
1226 if (row > 0)
1227 {
1228 check_for_delay(FALSE);
1229 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1230 bot_start = wp->w_height - row;
1231 else
1232 mid_start = 0; /* redraw all lines */
1233 }
1234 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1235 {
1236 /*
1237 * Skip the lines (below the deleted lines) that are still
1238 * valid and don't need redrawing. Copy their info
1239 * upwards, to compensate for the deleted lines. Set
1240 * bot_start to the first row that needs redrawing.
1241 */
1242 bot_start = 0;
1243 idx = 0;
1244 for (;;)
1245 {
1246 wp->w_lines[idx] = wp->w_lines[j];
1247 /* stop at line that didn't fit, unless it is still
1248 * valid (no lines deleted) */
1249 if (row > 0 && bot_start + row
1250 + (int)wp->w_lines[j].wl_size > wp->w_height)
1251 {
1252 wp->w_lines_valid = idx + 1;
1253 break;
1254 }
1255 bot_start += wp->w_lines[idx++].wl_size;
1256
1257 /* stop at the last valid entry in w_lines[].wl_size */
1258 if (++j >= wp->w_lines_valid)
1259 {
1260 wp->w_lines_valid = idx;
1261 break;
1262 }
1263 }
1264#ifdef FEAT_DIFF
1265 /* Correct the first entry for filler lines at the top
1266 * when it won't get updated below. */
1267 if (wp->w_p_diff && bot_start > 0)
1268 wp->w_lines[0].wl_size =
1269 plines_win_nofill(wp, wp->w_topline, TRUE)
1270 + wp->w_topfill;
1271#endif
1272 }
1273 }
1274 }
1275
1276 /* When starting redraw in the first line, redraw all lines. When
1277 * there is only one window it's probably faster to clear the screen
1278 * first. */
1279 if (mid_start == 0)
1280 {
1281 mid_end = wp->w_height;
1282 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001283 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001284 /* Clear the screen when it was not done by win_del_lines() or
1285 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1286 * then. */
1287 if (screen_cleared != TRUE)
1288 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001289#ifdef FEAT_WINDOWS
1290 /* The screen was cleared, redraw the tab pages line. */
1291 if (redraw_tabline)
1292 draw_tabline();
1293#endif
1294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001296
1297 /* When win_del_lines() or win_ins_lines() caused the screen to be
1298 * cleared (only happens for the first window) or when screenclear()
1299 * was called directly above, "must_redraw" will have been set to
1300 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1301 if (screen_cleared == TRUE)
1302 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 else
1305 {
1306 /* Not VALID or INVERTED: redraw all lines. */
1307 mid_start = 0;
1308 mid_end = wp->w_height;
1309 }
1310
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001311 if (type == SOME_VALID)
1312 {
1313 /* SOME_VALID: redraw all lines. */
1314 mid_start = 0;
1315 mid_end = wp->w_height;
1316 type = NOT_VALID;
1317 }
1318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319#ifdef FEAT_VISUAL
1320 /* check if we are updating or removing the inverted part */
1321 if ((VIsual_active && buf == curwin->w_buffer)
1322 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1323 {
1324 linenr_T from, to;
1325
1326 if (VIsual_active)
1327 {
1328 if (VIsual_active
1329 && (VIsual_mode != wp->w_old_visual_mode
1330 || type == INVERTED_ALL))
1331 {
1332 /*
1333 * If the type of Visual selection changed, redraw the whole
1334 * selection. Also when the ownership of the X selection is
1335 * gained or lost.
1336 */
1337 if (curwin->w_cursor.lnum < VIsual.lnum)
1338 {
1339 from = curwin->w_cursor.lnum;
1340 to = VIsual.lnum;
1341 }
1342 else
1343 {
1344 from = VIsual.lnum;
1345 to = curwin->w_cursor.lnum;
1346 }
1347 /* redraw more when the cursor moved as well */
1348 if (wp->w_old_cursor_lnum < from)
1349 from = wp->w_old_cursor_lnum;
1350 if (wp->w_old_cursor_lnum > to)
1351 to = wp->w_old_cursor_lnum;
1352 if (wp->w_old_visual_lnum < from)
1353 from = wp->w_old_visual_lnum;
1354 if (wp->w_old_visual_lnum > to)
1355 to = wp->w_old_visual_lnum;
1356 }
1357 else
1358 {
1359 /*
1360 * Find the line numbers that need to be updated: The lines
1361 * between the old cursor position and the current cursor
1362 * position. Also check if the Visual position changed.
1363 */
1364 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1365 {
1366 from = curwin->w_cursor.lnum;
1367 to = wp->w_old_cursor_lnum;
1368 }
1369 else
1370 {
1371 from = wp->w_old_cursor_lnum;
1372 to = curwin->w_cursor.lnum;
1373 if (from == 0) /* Visual mode just started */
1374 from = to;
1375 }
1376
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001377 if (VIsual.lnum != wp->w_old_visual_lnum
1378 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
1380 if (wp->w_old_visual_lnum < from
1381 && wp->w_old_visual_lnum != 0)
1382 from = wp->w_old_visual_lnum;
1383 if (wp->w_old_visual_lnum > to)
1384 to = wp->w_old_visual_lnum;
1385 if (VIsual.lnum < from)
1386 from = VIsual.lnum;
1387 if (VIsual.lnum > to)
1388 to = VIsual.lnum;
1389 }
1390 }
1391
1392 /*
1393 * If in block mode and changed column or curwin->w_curswant:
1394 * update all lines.
1395 * First compute the actual start and end column.
1396 */
1397 if (VIsual_mode == Ctrl_V)
1398 {
1399 colnr_T fromc, toc;
1400
1401 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1402 ++toc;
1403 if (curwin->w_curswant == MAXCOL)
1404 toc = MAXCOL;
1405
1406 if (fromc != wp->w_old_cursor_fcol
1407 || toc != wp->w_old_cursor_lcol)
1408 {
1409 if (from > VIsual.lnum)
1410 from = VIsual.lnum;
1411 if (to < VIsual.lnum)
1412 to = VIsual.lnum;
1413 }
1414 wp->w_old_cursor_fcol = fromc;
1415 wp->w_old_cursor_lcol = toc;
1416 }
1417 }
1418 else
1419 {
1420 /* Use the line numbers of the old Visual area. */
1421 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1422 {
1423 from = wp->w_old_cursor_lnum;
1424 to = wp->w_old_visual_lnum;
1425 }
1426 else
1427 {
1428 from = wp->w_old_visual_lnum;
1429 to = wp->w_old_cursor_lnum;
1430 }
1431 }
1432
1433 /*
1434 * There is no need to update lines above the top of the window.
1435 */
1436 if (from < wp->w_topline)
1437 from = wp->w_topline;
1438
1439 /*
1440 * If we know the value of w_botline, use it to restrict the update to
1441 * the lines that are visible in the window.
1442 */
1443 if (wp->w_valid & VALID_BOTLINE)
1444 {
1445 if (from >= wp->w_botline)
1446 from = wp->w_botline - 1;
1447 if (to >= wp->w_botline)
1448 to = wp->w_botline - 1;
1449 }
1450
1451 /*
1452 * Find the minimal part to be updated.
1453 * Watch out for scrolling that made entries in w_lines[] invalid.
1454 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1455 * top_end; need to redraw from top_end to the "to" line.
1456 * A middle mouse click with a Visual selection may change the text
1457 * above the Visual area and reset wl_valid, do count these for
1458 * mid_end (in srow).
1459 */
1460 if (mid_start > 0)
1461 {
1462 lnum = wp->w_topline;
1463 idx = 0;
1464 srow = 0;
1465 if (scrolled_down)
1466 mid_start = top_end;
1467 else
1468 mid_start = 0;
1469 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1470 {
1471 if (wp->w_lines[idx].wl_valid)
1472 mid_start += wp->w_lines[idx].wl_size;
1473 else if (!scrolled_down)
1474 srow += wp->w_lines[idx].wl_size;
1475 ++idx;
1476# ifdef FEAT_FOLDING
1477 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1478 lnum = wp->w_lines[idx].wl_lnum;
1479 else
1480# endif
1481 ++lnum;
1482 }
1483 srow += mid_start;
1484 mid_end = wp->w_height;
1485 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1486 {
1487 if (wp->w_lines[idx].wl_valid
1488 && wp->w_lines[idx].wl_lnum >= to + 1)
1489 {
1490 /* Only update until first row of this line */
1491 mid_end = srow;
1492 break;
1493 }
1494 srow += wp->w_lines[idx].wl_size;
1495 }
1496 }
1497 }
1498
1499 if (VIsual_active && buf == curwin->w_buffer)
1500 {
1501 wp->w_old_visual_mode = VIsual_mode;
1502 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1503 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001504 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 wp->w_old_curswant = curwin->w_curswant;
1506 }
1507 else
1508 {
1509 wp->w_old_visual_mode = 0;
1510 wp->w_old_cursor_lnum = 0;
1511 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001512 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514#endif /* FEAT_VISUAL */
1515
1516#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1517 /* reset got_int, otherwise regexp won't work */
1518 save_got_int = got_int;
1519 got_int = 0;
1520#endif
1521#ifdef FEAT_FOLDING
1522 win_foldinfo.fi_level = 0;
1523#endif
1524
1525 /*
1526 * Update all the window rows.
1527 */
1528 idx = 0; /* first entry in w_lines[].wl_size */
1529 row = 0;
1530 srow = 0;
1531 lnum = wp->w_topline; /* first line shown in window */
1532 for (;;)
1533 {
1534 /* stop updating when reached the end of the window (check for _past_
1535 * the end of the window is at the end of the loop) */
1536 if (row == wp->w_height)
1537 {
1538 didline = TRUE;
1539 break;
1540 }
1541
1542 /* stop updating when hit the end of the file */
1543 if (lnum > buf->b_ml.ml_line_count)
1544 {
1545 eof = TRUE;
1546 break;
1547 }
1548
1549 /* Remember the starting row of the line that is going to be dealt
1550 * with. It is used further down when the line doesn't fit. */
1551 srow = row;
1552
1553 /*
1554 * Update a line when it is in an area that needs updating, when it
1555 * has changes or w_lines[idx] is invalid.
1556 * bot_start may be halfway a wrapped line after using
1557 * win_del_lines(), check if the current line includes it.
1558 * When syntax folding is being used, the saved syntax states will
1559 * already have been updated, we can't see where the syntax state is
1560 * the same again, just update until the end of the window.
1561 */
1562 if (row < top_end
1563 || (row >= mid_start && row < mid_end)
1564#ifdef FEAT_SEARCH_EXTRA
1565 || top_to_mod
1566#endif
1567 || idx >= wp->w_lines_valid
1568 || (row + wp->w_lines[idx].wl_size > bot_start)
1569 || (mod_top != 0
1570 && (lnum == mod_top
1571 || (lnum >= mod_top
1572 && (lnum < mod_bot
1573#ifdef FEAT_SYN_HL
1574 || did_update == DID_FOLD
1575 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001576 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 && (
1578# ifdef FEAT_FOLDING
1579 (foldmethodIsSyntax(wp)
1580 && hasAnyFolding(wp)) ||
1581# endif
1582 syntax_check_changed(lnum)))
1583#endif
1584 )))))
1585 {
1586#ifdef FEAT_SEARCH_EXTRA
1587 if (lnum == mod_top)
1588 top_to_mod = FALSE;
1589#endif
1590
1591 /*
1592 * When at start of changed lines: May scroll following lines
1593 * up or down to minimize redrawing.
1594 * Don't do this when the change continues until the end.
1595 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1596 */
1597 if (lnum == mod_top
1598 && mod_bot != MAXLNUM
1599 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1600 {
1601 int old_rows = 0;
1602 int new_rows = 0;
1603 int xtra_rows;
1604 linenr_T l;
1605
1606 /* Count the old number of window rows, using w_lines[], which
1607 * should still contain the sizes for the lines as they are
1608 * currently displayed. */
1609 for (i = idx; i < wp->w_lines_valid; ++i)
1610 {
1611 /* Only valid lines have a meaningful wl_lnum. Invalid
1612 * lines are part of the changed area. */
1613 if (wp->w_lines[i].wl_valid
1614 && wp->w_lines[i].wl_lnum == mod_bot)
1615 break;
1616 old_rows += wp->w_lines[i].wl_size;
1617#ifdef FEAT_FOLDING
1618 if (wp->w_lines[i].wl_valid
1619 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1620 {
1621 /* Must have found the last valid entry above mod_bot.
1622 * Add following invalid entries. */
1623 ++i;
1624 while (i < wp->w_lines_valid
1625 && !wp->w_lines[i].wl_valid)
1626 old_rows += wp->w_lines[i++].wl_size;
1627 break;
1628 }
1629#endif
1630 }
1631
1632 if (i >= wp->w_lines_valid)
1633 {
1634 /* We can't find a valid line below the changed lines,
1635 * need to redraw until the end of the window.
1636 * Inserting/deleting lines has no use. */
1637 bot_start = 0;
1638 }
1639 else
1640 {
1641 /* Able to count old number of rows: Count new window
1642 * rows, and may insert/delete lines */
1643 j = idx;
1644 for (l = lnum; l < mod_bot; ++l)
1645 {
1646#ifdef FEAT_FOLDING
1647 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1648 ++new_rows;
1649 else
1650#endif
1651#ifdef FEAT_DIFF
1652 if (l == wp->w_topline)
1653 new_rows += plines_win_nofill(wp, l, TRUE)
1654 + wp->w_topfill;
1655 else
1656#endif
1657 new_rows += plines_win(wp, l, TRUE);
1658 ++j;
1659 if (new_rows > wp->w_height - row - 2)
1660 {
1661 /* it's getting too much, must redraw the rest */
1662 new_rows = 9999;
1663 break;
1664 }
1665 }
1666 xtra_rows = new_rows - old_rows;
1667 if (xtra_rows < 0)
1668 {
1669 /* May scroll text up. If there is not enough
1670 * remaining text or scrolling fails, must redraw the
1671 * rest. If scrolling works, must redraw the text
1672 * below the scrolled text. */
1673 if (row - xtra_rows >= wp->w_height - 2)
1674 mod_bot = MAXLNUM;
1675 else
1676 {
1677 check_for_delay(FALSE);
1678 if (win_del_lines(wp, row,
1679 -xtra_rows, FALSE, FALSE) == FAIL)
1680 mod_bot = MAXLNUM;
1681 else
1682 bot_start = wp->w_height + xtra_rows;
1683 }
1684 }
1685 else if (xtra_rows > 0)
1686 {
1687 /* May scroll text down. If there is not enough
1688 * remaining text of scrolling fails, must redraw the
1689 * rest. */
1690 if (row + xtra_rows >= wp->w_height - 2)
1691 mod_bot = MAXLNUM;
1692 else
1693 {
1694 check_for_delay(FALSE);
1695 if (win_ins_lines(wp, row + old_rows,
1696 xtra_rows, FALSE, FALSE) == FAIL)
1697 mod_bot = MAXLNUM;
1698 else if (top_end > row + old_rows)
1699 /* Scrolled the part at the top that requires
1700 * updating down. */
1701 top_end += xtra_rows;
1702 }
1703 }
1704
1705 /* When not updating the rest, may need to move w_lines[]
1706 * entries. */
1707 if (mod_bot != MAXLNUM && i != j)
1708 {
1709 if (j < i)
1710 {
1711 int x = row + new_rows;
1712
1713 /* move entries in w_lines[] upwards */
1714 for (;;)
1715 {
1716 /* stop at last valid entry in w_lines[] */
1717 if (i >= wp->w_lines_valid)
1718 {
1719 wp->w_lines_valid = j;
1720 break;
1721 }
1722 wp->w_lines[j] = wp->w_lines[i];
1723 /* stop at a line that won't fit */
1724 if (x + (int)wp->w_lines[j].wl_size
1725 > wp->w_height)
1726 {
1727 wp->w_lines_valid = j + 1;
1728 break;
1729 }
1730 x += wp->w_lines[j++].wl_size;
1731 ++i;
1732 }
1733 if (bot_start > x)
1734 bot_start = x;
1735 }
1736 else /* j > i */
1737 {
1738 /* move entries in w_lines[] downwards */
1739 j -= i;
1740 wp->w_lines_valid += j;
1741 if (wp->w_lines_valid > wp->w_height)
1742 wp->w_lines_valid = wp->w_height;
1743 for (i = wp->w_lines_valid; i - j >= idx; --i)
1744 wp->w_lines[i] = wp->w_lines[i - j];
1745
1746 /* The w_lines[] entries for inserted lines are
1747 * now invalid, but wl_size may be used above.
1748 * Reset to zero. */
1749 while (i >= idx)
1750 {
1751 wp->w_lines[i].wl_size = 0;
1752 wp->w_lines[i--].wl_valid = FALSE;
1753 }
1754 }
1755 }
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758
1759#ifdef FEAT_FOLDING
1760 /*
1761 * When lines are folded, display one line for all of them.
1762 * Otherwise, display normally (can be several display lines when
1763 * 'wrap' is on).
1764 */
1765 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1766 if (fold_count != 0)
1767 {
1768 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1769 ++row;
1770 --fold_count;
1771 wp->w_lines[idx].wl_folded = TRUE;
1772 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1773# ifdef FEAT_SYN_HL
1774 did_update = DID_FOLD;
1775# endif
1776 }
1777 else
1778#endif
1779 if (idx < wp->w_lines_valid
1780 && wp->w_lines[idx].wl_valid
1781 && wp->w_lines[idx].wl_lnum == lnum
1782 && lnum > wp->w_topline
1783 && !(dy_flags & DY_LASTLINE)
1784 && srow + wp->w_lines[idx].wl_size > wp->w_height
1785#ifdef FEAT_DIFF
1786 && diff_check_fill(wp, lnum) == 0
1787#endif
1788 )
1789 {
1790 /* This line is not going to fit. Don't draw anything here,
1791 * will draw "@ " lines below. */
1792 row = wp->w_height + 1;
1793 }
1794 else
1795 {
1796#ifdef FEAT_SEARCH_EXTRA
1797 prepare_search_hl(wp, lnum);
1798#endif
1799#ifdef FEAT_SYN_HL
1800 /* Let the syntax stuff know we skipped a few lines. */
1801 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001802 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 syntax_end_parsing(syntax_last_parsed + 1);
1804#endif
1805
1806 /*
1807 * Display one line.
1808 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001809 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811#ifdef FEAT_FOLDING
1812 wp->w_lines[idx].wl_folded = FALSE;
1813 wp->w_lines[idx].wl_lastlnum = lnum;
1814#endif
1815#ifdef FEAT_SYN_HL
1816 did_update = DID_LINE;
1817 syntax_last_parsed = lnum;
1818#endif
1819 }
1820
1821 wp->w_lines[idx].wl_lnum = lnum;
1822 wp->w_lines[idx].wl_valid = TRUE;
1823 if (row > wp->w_height) /* past end of screen */
1824 {
1825 /* we may need the size of that too long line later on */
1826 if (dollar_vcol == 0)
1827 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1828 ++idx;
1829 break;
1830 }
1831 if (dollar_vcol == 0)
1832 wp->w_lines[idx].wl_size = row - srow;
1833 ++idx;
1834#ifdef FEAT_FOLDING
1835 lnum += fold_count + 1;
1836#else
1837 ++lnum;
1838#endif
1839 }
1840 else
1841 {
1842 /* This line does not need updating, advance to the next one */
1843 row += wp->w_lines[idx++].wl_size;
1844 if (row > wp->w_height) /* past end of screen */
1845 break;
1846#ifdef FEAT_FOLDING
1847 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1848#else
1849 ++lnum;
1850#endif
1851#ifdef FEAT_SYN_HL
1852 did_update = DID_NONE;
1853#endif
1854 }
1855
1856 if (lnum > buf->b_ml.ml_line_count)
1857 {
1858 eof = TRUE;
1859 break;
1860 }
1861 }
1862 /*
1863 * End of loop over all window lines.
1864 */
1865
1866
1867 if (idx > wp->w_lines_valid)
1868 wp->w_lines_valid = idx;
1869
1870#ifdef FEAT_SYN_HL
1871 /*
1872 * Let the syntax stuff know we stop parsing here.
1873 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001874 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 syntax_end_parsing(syntax_last_parsed + 1);
1876#endif
1877
1878 /*
1879 * If we didn't hit the end of the file, and we didn't finish the last
1880 * line we were working on, then the line didn't fit.
1881 */
1882 wp->w_empty_rows = 0;
1883#ifdef FEAT_DIFF
1884 wp->w_filler_rows = 0;
1885#endif
1886 if (!eof && !didline)
1887 {
1888 if (lnum == wp->w_topline)
1889 {
1890 /*
1891 * Single line that does not fit!
1892 * Don't overwrite it, it can be edited.
1893 */
1894 wp->w_botline = lnum + 1;
1895 }
1896#ifdef FEAT_DIFF
1897 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1898 {
1899 /* Window ends in filler lines. */
1900 wp->w_botline = lnum;
1901 wp->w_filler_rows = wp->w_height - srow;
1902 }
1903#endif
1904 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1905 {
1906 /*
1907 * Last line isn't finished: Display "@@@" at the end.
1908 */
1909 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1910 W_WINROW(wp) + wp->w_height,
1911 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1912 '@', '@', hl_attr(HLF_AT));
1913 set_empty_rows(wp, srow);
1914 wp->w_botline = lnum;
1915 }
1916 else
1917 {
1918 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1919 wp->w_botline = lnum;
1920 }
1921 }
1922 else
1923 {
1924#ifdef FEAT_VERTSPLIT
1925 draw_vsep_win(wp, row);
1926#endif
1927 if (eof) /* we hit the end of the file */
1928 {
1929 wp->w_botline = buf->b_ml.ml_line_count + 1;
1930#ifdef FEAT_DIFF
1931 j = diff_check_fill(wp, wp->w_botline);
1932 if (j > 0 && !wp->w_botfill)
1933 {
1934 /*
1935 * Display filler lines at the end of the file
1936 */
1937 if (char2cells(fill_diff) > 1)
1938 i = '-';
1939 else
1940 i = fill_diff;
1941 if (row + j > wp->w_height)
1942 j = wp->w_height - row;
1943 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1944 row += j;
1945 }
1946#endif
1947 }
1948 else if (dollar_vcol == 0)
1949 wp->w_botline = lnum;
1950
1951 /* make sure the rest of the screen is blank */
1952 /* put '~'s on rows that aren't part of the file. */
1953 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1954 }
1955
1956 /* Reset the type of redrawing required, the window has been updated. */
1957 wp->w_redr_type = 0;
1958#ifdef FEAT_DIFF
1959 wp->w_old_topfill = wp->w_topfill;
1960 wp->w_old_botfill = wp->w_botfill;
1961#endif
1962
1963 if (dollar_vcol == 0)
1964 {
1965 /*
1966 * There is a trick with w_botline. If we invalidate it on each
1967 * change that might modify it, this will cause a lot of expensive
1968 * calls to plines() in update_topline() each time. Therefore the
1969 * value of w_botline is often approximated, and this value is used to
1970 * compute the value of w_topline. If the value of w_botline was
1971 * wrong, check that the value of w_topline is correct (cursor is on
1972 * the visible part of the text). If it's not, we need to redraw
1973 * again. Mostly this just means scrolling up a few lines, so it
1974 * doesn't look too bad. Only do this for the current window (where
1975 * changes are relevant).
1976 */
1977 wp->w_valid |= VALID_BOTLINE;
1978 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1979 {
1980 recursive = TRUE;
1981 curwin->w_valid &= ~VALID_TOPLINE;
1982 update_topline(); /* may invalidate w_botline again */
1983 if (must_redraw != 0)
1984 {
1985 /* Don't update for changes in buffer again. */
1986 i = curbuf->b_mod_set;
1987 curbuf->b_mod_set = FALSE;
1988 win_update(curwin);
1989 must_redraw = 0;
1990 curbuf->b_mod_set = i;
1991 }
1992 recursive = FALSE;
1993 }
1994 }
1995
1996#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1997 /* restore got_int, unless CTRL-C was hit while redrawing */
1998 if (!got_int)
1999 got_int = save_got_int;
2000#endif
2001}
2002
2003#ifdef FEAT_SIGNS
2004static int draw_signcolumn __ARGS((win_T *wp));
2005
2006/*
2007 * Return TRUE when window "wp" has a column to draw signs in.
2008 */
2009 static int
2010draw_signcolumn(wp)
2011 win_T *wp;
2012{
2013 return (wp->w_buffer->b_signlist != NULL
2014# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002015 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016# endif
2017 );
2018}
2019#endif
2020
2021/*
2022 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2023 * as the filler character.
2024 */
2025 static void
2026win_draw_end(wp, c1, c2, row, endrow, hl)
2027 win_T *wp;
2028 int c1;
2029 int c2;
2030 int row;
2031 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002032 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
2034#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2035 int n = 0;
2036# define FDC_OFF n
2037#else
2038# define FDC_OFF 0
2039#endif
2040
2041#ifdef FEAT_RIGHTLEFT
2042 if (wp->w_p_rl)
2043 {
2044 /* No check for cmdline window: should never be right-left. */
2045# ifdef FEAT_FOLDING
2046 n = wp->w_p_fdc;
2047
2048 if (n > 0)
2049 {
2050 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002051 if (n > W_WIDTH(wp))
2052 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2054 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2055 ' ', ' ', hl_attr(HLF_FC));
2056 }
2057# endif
2058# ifdef FEAT_SIGNS
2059 if (draw_signcolumn(wp))
2060 {
2061 int nn = n + 2;
2062
2063 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002064 if (nn > W_WIDTH(wp))
2065 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2067 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2068 ' ', ' ', hl_attr(HLF_SC));
2069 n = nn;
2070 }
2071# endif
2072 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2073 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2074 c2, c2, hl_attr(hl));
2075 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2076 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2077 c1, c2, hl_attr(hl));
2078 }
2079 else
2080#endif
2081 {
2082#ifdef FEAT_CMDWIN
2083 if (cmdwin_type != 0 && wp == curwin)
2084 {
2085 /* draw the cmdline character in the leftmost column */
2086 n = 1;
2087 if (n > wp->w_width)
2088 n = wp->w_width;
2089 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2090 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2091 cmdwin_type, ' ', hl_attr(HLF_AT));
2092 }
2093#endif
2094#ifdef FEAT_FOLDING
2095 if (wp->w_p_fdc > 0)
2096 {
2097 int nn = n + wp->w_p_fdc;
2098
2099 /* draw the fold column at the left */
2100 if (nn > W_WIDTH(wp))
2101 nn = W_WIDTH(wp);
2102 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2103 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2104 ' ', ' ', hl_attr(HLF_FC));
2105 n = nn;
2106 }
2107#endif
2108#ifdef FEAT_SIGNS
2109 if (draw_signcolumn(wp))
2110 {
2111 int nn = n + 2;
2112
2113 /* draw the sign column after the fold column */
2114 if (nn > W_WIDTH(wp))
2115 nn = W_WIDTH(wp);
2116 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2117 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2118 ' ', ' ', hl_attr(HLF_SC));
2119 n = nn;
2120 }
2121#endif
2122 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2123 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2124 c1, c2, hl_attr(hl));
2125 }
2126 set_empty_rows(wp, row);
2127}
2128
Bram Moolenaar1a384422010-07-14 19:53:30 +02002129#ifdef FEAT_SYN_HL
2130static int advance_color_col __ARGS((int vcol, int **color_cols));
2131
2132/*
2133 * Advance **color_cols and return TRUE when there are columns to draw.
2134 */
2135 static int
2136advance_color_col(vcol, color_cols)
2137 int vcol;
2138 int **color_cols;
2139{
2140 while (**color_cols >= 0 && vcol > **color_cols)
2141 ++*color_cols;
2142 return (**color_cols >= 0);
2143}
2144#endif
2145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146#ifdef FEAT_FOLDING
2147/*
2148 * Display one folded line.
2149 */
2150 static void
2151fold_line(wp, fold_count, foldinfo, lnum, row)
2152 win_T *wp;
2153 long fold_count;
2154 foldinfo_T *foldinfo;
2155 linenr_T lnum;
2156 int row;
2157{
2158 char_u buf[51];
2159 pos_T *top, *bot;
2160 linenr_T lnume = lnum + fold_count - 1;
2161 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002162 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 int col;
2165 int txtcol;
2166 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002167 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169 /* Build the fold line:
2170 * 1. Add the cmdwin_type for the command-line window
2171 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002172 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 * 4. Compose the text
2174 * 5. Add the text
2175 * 6. set highlighting for the Visual area an other text
2176 */
2177 col = 0;
2178
2179 /*
2180 * 1. Add the cmdwin_type for the command-line window
2181 * Ignores 'rightleft', this window is never right-left.
2182 */
2183#ifdef FEAT_CMDWIN
2184 if (cmdwin_type != 0 && wp == curwin)
2185 {
2186 ScreenLines[off] = cmdwin_type;
2187 ScreenAttrs[off] = hl_attr(HLF_AT);
2188#ifdef FEAT_MBYTE
2189 if (enc_utf8)
2190 ScreenLinesUC[off] = 0;
2191#endif
2192 ++col;
2193 }
2194#endif
2195
2196 /*
2197 * 2. Add the 'foldcolumn'
2198 */
2199 fdc = wp->w_p_fdc;
2200 if (fdc > W_WIDTH(wp) - col)
2201 fdc = W_WIDTH(wp) - col;
2202 if (fdc > 0)
2203 {
2204 fill_foldcolumn(buf, wp, TRUE, lnum);
2205#ifdef FEAT_RIGHTLEFT
2206 if (wp->w_p_rl)
2207 {
2208 int i;
2209
2210 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2211 hl_attr(HLF_FC));
2212 /* reverse the fold column */
2213 for (i = 0; i < fdc; ++i)
2214 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2215 }
2216 else
2217#endif
2218 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2219 col += fdc;
2220 }
2221
2222#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002223# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2224 for (ri = 0; ri < l; ++ri) \
2225 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2226 else \
2227 for (ri = 0; ri < l; ++ri) \
2228 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002230# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2231 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#endif
2233
Bram Moolenaar64486672010-05-16 15:46:46 +02002234 /* Set all attributes of the 'number' or 'relativenumber' column and the
2235 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002236 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238#ifdef FEAT_SIGNS
2239 /* If signs are being displayed, add two spaces. */
2240 if (draw_signcolumn(wp))
2241 {
2242 len = W_WIDTH(wp) - col;
2243 if (len > 0)
2244 {
2245 if (len > 2)
2246 len = 2;
2247# ifdef FEAT_RIGHTLEFT
2248 if (wp->w_p_rl)
2249 /* the line number isn't reversed */
2250 copy_text_attr(off + W_WIDTH(wp) - len - col,
2251 (char_u *)" ", len, hl_attr(HLF_FL));
2252 else
2253# endif
2254 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2255 col += len;
2256 }
2257 }
2258#endif
2259
2260 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002261 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002263 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 len = W_WIDTH(wp) - col;
2266 if (len > 0)
2267 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002268 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002269 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002270
2271 if (len > w + 1)
2272 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002273
2274 if (wp->w_p_nu)
2275 /* 'number' */
2276 num = (long)lnum;
2277 else
2278 /* 'relativenumber', don't use negative numbers */
2279 num = (long)abs((int)get_cursor_rel_lnum(wp, lnum));
2280
2281 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282#ifdef FEAT_RIGHTLEFT
2283 if (wp->w_p_rl)
2284 /* the line number isn't reversed */
2285 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2286 hl_attr(HLF_FL));
2287 else
2288#endif
2289 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2290 col += len;
2291 }
2292 }
2293
2294 /*
2295 * 4. Compose the folded-line string with 'foldtext', if set.
2296 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002297 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 txtcol = col; /* remember where text starts */
2300
2301 /*
2302 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2303 * Right-left text is put in columns 0 - number-col, normal text is put
2304 * in columns number-col - window-width.
2305 */
2306#ifdef FEAT_MBYTE
2307 if (has_mbyte)
2308 {
2309 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002310 int u8c, u8cc[MAX_MCO];
2311 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 int idx;
2313 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002314 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315# ifdef FEAT_ARABIC
2316 int prev_c = 0; /* previous Arabic character */
2317 int prev_c1 = 0; /* first composing char for prev_c */
2318# endif
2319
2320# ifdef FEAT_RIGHTLEFT
2321 if (wp->w_p_rl)
2322 idx = off;
2323 else
2324# endif
2325 idx = off + col;
2326
2327 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2328 for (p = text; *p != NUL; )
2329 {
2330 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002331 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (col + cells > W_WIDTH(wp)
2333# ifdef FEAT_RIGHTLEFT
2334 - (wp->w_p_rl ? col : 0)
2335# endif
2336 )
2337 break;
2338 ScreenLines[idx] = *p;
2339 if (enc_utf8)
2340 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002341 u8c = utfc_ptr2char(p, u8cc);
2342 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 ScreenLinesUC[idx] = 0;
2345#ifdef FEAT_ARABIC
2346 prev_c = u8c;
2347#endif
2348 }
2349 else
2350 {
2351#ifdef FEAT_ARABIC
2352 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2353 {
2354 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002355 int pc, pc1, nc;
2356 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int firstbyte = *p;
2358
2359 /* The idea of what is the previous and next
2360 * character depends on 'rightleft'. */
2361 if (wp->w_p_rl)
2362 {
2363 pc = prev_c;
2364 pc1 = prev_c1;
2365 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002366 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 else
2369 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002370 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002372 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374 prev_c = u8c;
2375
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002376 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 pc, pc1, nc);
2378 ScreenLines[idx] = firstbyte;
2379 }
2380 else
2381 prev_c = u8c;
2382#endif
2383 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002384#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (u8c >= 0x10000)
2386 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2387 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002390 for (i = 0; i < Screen_mco; ++i)
2391 {
2392 ScreenLinesC[i][idx] = u8cc[i];
2393 if (u8cc[i] == 0)
2394 break;
2395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 if (cells > 1)
2398 ScreenLines[idx + 1] = 0;
2399 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002400 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2401 /* double-byte single width character */
2402 ScreenLines2[idx] = p[1];
2403 else if (cells > 1)
2404 /* double-width character */
2405 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 col += cells;
2407 idx += cells;
2408 p += c_len;
2409 }
2410 }
2411 else
2412#endif
2413 {
2414 len = (int)STRLEN(text);
2415 if (len > W_WIDTH(wp) - col)
2416 len = W_WIDTH(wp) - col;
2417 if (len > 0)
2418 {
2419#ifdef FEAT_RIGHTLEFT
2420 if (wp->w_p_rl)
2421 STRNCPY(current_ScreenLine, text, len);
2422 else
2423#endif
2424 STRNCPY(current_ScreenLine + col, text, len);
2425 col += len;
2426 }
2427 }
2428
2429 /* Fill the rest of the line with the fold filler */
2430#ifdef FEAT_RIGHTLEFT
2431 if (wp->w_p_rl)
2432 col -= txtcol;
2433#endif
2434 while (col < W_WIDTH(wp)
2435#ifdef FEAT_RIGHTLEFT
2436 - (wp->w_p_rl ? txtcol : 0)
2437#endif
2438 )
2439 {
2440#ifdef FEAT_MBYTE
2441 if (enc_utf8)
2442 {
2443 if (fill_fold >= 0x80)
2444 {
2445 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002446 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448 else
2449 ScreenLinesUC[off + col] = 0;
2450 }
2451#endif
2452 ScreenLines[off + col++] = fill_fold;
2453 }
2454
2455 if (text != buf)
2456 vim_free(text);
2457
2458 /*
2459 * 6. set highlighting for the Visual area an other text.
2460 * If all folded lines are in the Visual area, highlight the line.
2461 */
2462#ifdef FEAT_VISUAL
2463 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2464 {
2465 if (ltoreq(curwin->w_cursor, VIsual))
2466 {
2467 /* Visual is after curwin->w_cursor */
2468 top = &curwin->w_cursor;
2469 bot = &VIsual;
2470 }
2471 else
2472 {
2473 /* Visual is before curwin->w_cursor */
2474 top = &VIsual;
2475 bot = &curwin->w_cursor;
2476 }
2477 if (lnum >= top->lnum
2478 && lnume <= bot->lnum
2479 && (VIsual_mode != 'v'
2480 || ((lnum > top->lnum
2481 || (lnum == top->lnum
2482 && top->col == 0))
2483 && (lnume < bot->lnum
2484 || (lnume == bot->lnum
2485 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002486 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 if (VIsual_mode == Ctrl_V)
2489 {
2490 /* Visual block mode: highlight the chars part of the block */
2491 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2492 {
2493 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2494 len = wp->w_old_cursor_lcol;
2495 else
2496 len = W_WIDTH(wp) - txtcol;
2497 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002498 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 }
2501 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002504 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
2507 }
2508#endif
2509
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002510#ifdef FEAT_SYN_HL
2511 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002512 if (wp->w_p_cuc)
2513 {
2514 txtcol += wp->w_virtcol;
2515 if (wp->w_p_wrap)
2516 txtcol -= wp->w_skipcol;
2517 else
2518 txtcol -= wp->w_leftcol;
2519 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2520 ScreenAttrs[off + txtcol] = hl_combine_attr(
2521 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2522 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
2525 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2526 (int)W_WIDTH(wp), FALSE);
2527
2528 /*
2529 * Update w_cline_height and w_cline_folded if the cursor line was
2530 * updated (saves a call to plines() later).
2531 */
2532 if (wp == curwin
2533 && lnum <= curwin->w_cursor.lnum
2534 && lnume >= curwin->w_cursor.lnum)
2535 {
2536 curwin->w_cline_row = row;
2537 curwin->w_cline_height = 1;
2538 curwin->w_cline_folded = TRUE;
2539 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2540 }
2541}
2542
2543/*
2544 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2545 */
2546 static void
2547copy_text_attr(off, buf, len, attr)
2548 int off;
2549 char_u *buf;
2550 int len;
2551 int attr;
2552{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002553 int i;
2554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 mch_memmove(ScreenLines + off, buf, (size_t)len);
2556# ifdef FEAT_MBYTE
2557 if (enc_utf8)
2558 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2559# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002560 for (i = 0; i < len; ++i)
2561 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562}
2563
2564/*
2565 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002566 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
2568 static void
2569fill_foldcolumn(p, wp, closed, lnum)
2570 char_u *p;
2571 win_T *wp;
2572 int closed; /* TRUE of FALSE */
2573 linenr_T lnum; /* current line number */
2574{
2575 int i = 0;
2576 int level;
2577 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002578 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
2580 /* Init to all spaces. */
2581 copy_spaces(p, (size_t)wp->w_p_fdc);
2582
2583 level = win_foldinfo.fi_level;
2584 if (level > 0)
2585 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002586 /* If there is only one column put more info in it. */
2587 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 /* If the column is too narrow, we start at the lowest level that
2590 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002591 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (first_level < 1)
2593 first_level = 1;
2594
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002595 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 if (win_foldinfo.fi_lnum == lnum
2598 && first_level + i >= win_foldinfo.fi_low_level)
2599 p[i] = '-';
2600 else if (first_level == 1)
2601 p[i] = '|';
2602 else if (first_level + i <= 9)
2603 p[i] = '0' + first_level + i;
2604 else
2605 p[i] = '>';
2606 if (first_level + i == level)
2607 break;
2608 }
2609 }
2610 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002611 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612}
2613#endif /* FEAT_FOLDING */
2614
2615/*
2616 * Display line "lnum" of window 'wp' on the screen.
2617 * Start at row "startrow", stop when "endrow" is reached.
2618 * wp->w_virtcol needs to be valid.
2619 *
2620 * Return the number of last row the line occupies.
2621 */
2622 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002623win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 win_T *wp;
2625 linenr_T lnum;
2626 int startrow;
2627 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002628 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 int col; /* visual column on screen */
2631 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2632 int c = 0; /* init for GCC */
2633 long vcol = 0; /* virtual column (for tabs) */
2634 long vcol_prev = -1; /* "vcol" of previous character */
2635 char_u *line; /* current line */
2636 char_u *ptr; /* current position in "line" */
2637 int row; /* row in the window, excl w_winrow */
2638 int screen_row; /* row on the screen, incl w_winrow */
2639
2640 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2641 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002642 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 int c_extra = NUL; /* extra chars, all the same */
2644 int extra_attr = 0; /* attributes when n_extra != 0 */
2645 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2646 displaying lcs_eol at end-of-line */
2647 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2648 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2649
2650 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2651 int saved_n_extra = 0;
2652 char_u *saved_p_extra = NULL;
2653 int saved_c_extra = 0;
2654 int saved_char_attr = 0;
2655
2656 int n_attr = 0; /* chars with special attr */
2657 int saved_attr2 = 0; /* char_attr saved for n_attr */
2658 int n_attr3 = 0; /* chars with overruling special attr */
2659 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2660
2661 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2662
2663 int fromcol, tocol; /* start/end of inverting */
2664 int fromcol_prev = -2; /* start of inverting after cursor */
2665 int noinvcur = FALSE; /* don't invert the cursor */
2666#ifdef FEAT_VISUAL
2667 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002668 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669#endif
2670 pos_T pos;
2671 long v;
2672
2673 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002674 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2676 in this line */
2677 int attr = 0; /* attributes for area highlighting */
2678 int area_attr = 0; /* attributes desired by highlighting */
2679 int search_attr = 0; /* attributes desired by 'hlsearch' */
2680#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002681 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 int syntax_attr = 0; /* attributes desired by syntax */
2683 int has_syntax = FALSE; /* this buffer has syntax highl. */
2684 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002685 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002686 int draw_color_col = FALSE; /* highlight colorcolumn */
2687 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002688#endif
2689#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002690 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002691# define SPWORDLEN 150
2692 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002693 int nextlinecol = 0; /* column where nextline[] starts */
2694 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002695 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002696 int spell_attr = 0; /* attributes desired by spelling */
2697 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002698 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2699 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002700 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002701 static int cap_col = -1; /* column to check for Cap word */
2702 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002703 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#endif
2705 int extra_check; /* has syntax or linebreak */
2706#ifdef FEAT_MBYTE
2707 int multi_attr = 0; /* attributes desired by multibyte */
2708 int mb_l = 1; /* multi-byte byte length */
2709 int mb_c = 0; /* decoded multi-byte character */
2710 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002711 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712#endif
2713#ifdef FEAT_DIFF
2714 int filler_lines; /* nr of filler lines to be drawn */
2715 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002716 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 int change_start = MAXCOL; /* first col of changed area */
2718 int change_end = -1; /* last col of changed area */
2719#endif
2720 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2721#ifdef FEAT_LINEBREAK
2722 int need_showbreak = FALSE;
2723#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002724#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2725 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002727 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#endif
2729#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002730 matchitem_T *cur; /* points to the match list */
2731 match_T *shl; /* points to search_hl or a match */
2732 int shl_flag; /* flag to indicate whether search_hl
2733 has been processed or not */
2734 int prevcol_hl_flag; /* flag to indicate whether prevcol
2735 equals startcol of search_hl or one
2736 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737#endif
2738#ifdef FEAT_ARABIC
2739 int prev_c = 0; /* previous Arabic character */
2740 int prev_c1 = 0; /* first composing char for prev_c */
2741#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002742#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002743 int did_line_attr = 0;
2744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 /* draw_state: items that are drawn in sequence: */
2747#define WL_START 0 /* nothing done yet */
2748#ifdef FEAT_CMDWIN
2749# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2750#else
2751# define WL_CMDLINE WL_START
2752#endif
2753#ifdef FEAT_FOLDING
2754# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2755#else
2756# define WL_FOLD WL_CMDLINE
2757#endif
2758#ifdef FEAT_SIGNS
2759# define WL_SIGN WL_FOLD + 1 /* column for signs */
2760#else
2761# define WL_SIGN WL_FOLD /* column for signs */
2762#endif
2763#define WL_NR WL_SIGN + 1 /* line number */
2764#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2765# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2766#else
2767# define WL_SBR WL_NR
2768#endif
2769#define WL_LINE WL_SBR + 1 /* text in the line */
2770 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002771#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 int feedback_col = 0;
2773 int feedback_old_attr = -1;
2774#endif
2775
Bram Moolenaar860cae12010-06-05 23:22:07 +02002776#ifdef FEAT_CONCEAL
2777 int syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002778 int syntax_id = 0;
2779 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002780 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002781 int is_concealing = FALSE;
2782 int boguscols = 0; /* nonexistent columns added to force
2783 wrapping */
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002784 int vcol_off = 0; /* offset for concealed characters */
2785# define VCOL_HLC (vcol - vcol_off)
2786#else
2787# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
2790 if (startrow > endrow) /* past the end already! */
2791 return startrow;
2792
2793 row = startrow;
2794 screen_row = row + W_WINROW(wp);
2795
2796 /*
2797 * To speed up the loop below, set extra_check when there is linebreak,
2798 * trailing white space and/or syntax processing to be done.
2799 */
2800#ifdef FEAT_LINEBREAK
2801 extra_check = wp->w_p_lbr;
2802#else
2803 extra_check = 0;
2804#endif
2805#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002806 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
2808 /* Prepare for syntax highlighting in this line. When there is an
2809 * error, stop syntax highlighting. */
2810 save_did_emsg = did_emsg;
2811 did_emsg = FALSE;
2812 syntax_start(wp, lnum);
2813 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002814 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 else
2816 {
2817 did_emsg = save_did_emsg;
2818 has_syntax = TRUE;
2819 extra_check = TRUE;
2820 }
2821 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002822
2823 /* Check for columns to display for 'colorcolumn'. */
2824 color_cols = wp->w_p_cc_cols;
2825 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002826 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002827#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002828
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002829#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002830 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002831 && *wp->w_s->b_p_spl != NUL
2832 && wp->w_s->b_langp.ga_len > 0
2833 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002834 {
2835 /* Prepare for spell checking. */
2836 has_spell = TRUE;
2837 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002838
2839 /* Get the start of the next line, so that words that wrap to the next
2840 * line are found too: "et<line-break>al.".
2841 * Trick: skip a few chars for C/shell/Vim comments */
2842 nextline[SPWORDLEN] = NUL;
2843 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2844 {
2845 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2846 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2847 }
2848
2849 /* When a word wrapped from the previous line the start of the current
2850 * line is valid. */
2851 if (lnum == checked_lnum)
2852 cur_checked_col = checked_col;
2853 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002854
2855 /* When there was a sentence end in the previous line may require a
2856 * word starting with capital in this line. In line 1 always check
2857 * the first word. */
2858 if (lnum != capcol_lnum)
2859 cap_col = -1;
2860 if (lnum == 1)
2861 cap_col = 0;
2862 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864#endif
2865
2866 /*
2867 * handle visual active in this window
2868 */
2869 fromcol = -10;
2870 tocol = MAXCOL;
2871#ifdef FEAT_VISUAL
2872 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2873 {
2874 /* Visual is after curwin->w_cursor */
2875 if (ltoreq(curwin->w_cursor, VIsual))
2876 {
2877 top = &curwin->w_cursor;
2878 bot = &VIsual;
2879 }
2880 else /* Visual is before curwin->w_cursor */
2881 {
2882 top = &VIsual;
2883 bot = &curwin->w_cursor;
2884 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002885 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 if (VIsual_mode == Ctrl_V) /* block mode */
2887 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002888 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
2890 fromcol = wp->w_old_cursor_fcol;
2891 tocol = wp->w_old_cursor_lcol;
2892 }
2893 }
2894 else /* non-block mode */
2895 {
2896 if (lnum > top->lnum && lnum <= bot->lnum)
2897 fromcol = 0;
2898 else if (lnum == top->lnum)
2899 {
2900 if (VIsual_mode == 'V') /* linewise */
2901 fromcol = 0;
2902 else
2903 {
2904 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2905 if (gchar_pos(top) == NUL)
2906 tocol = fromcol + 1;
2907 }
2908 }
2909 if (VIsual_mode != 'V' && lnum == bot->lnum)
2910 {
2911 if (*p_sel == 'e' && bot->col == 0
2912#ifdef FEAT_VIRTUALEDIT
2913 && bot->coladd == 0
2914#endif
2915 )
2916 {
2917 fromcol = -10;
2918 tocol = MAXCOL;
2919 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002920 else if (bot->col == MAXCOL)
2921 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 else
2923 {
2924 pos = *bot;
2925 if (*p_sel == 'e')
2926 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2927 else
2928 {
2929 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2930 ++tocol;
2931 }
2932 }
2933 }
2934 }
2935
2936#ifndef MSDOS
2937 /* Check if the character under the cursor should not be inverted */
2938 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2939# ifdef FEAT_GUI
2940 && !gui.in_use
2941# endif
2942 )
2943 noinvcur = TRUE;
2944#endif
2945
2946 /* if inverting in this line set area_highlighting */
2947 if (fromcol >= 0)
2948 {
2949 area_highlighting = TRUE;
2950 attr = hl_attr(HLF_V);
2951#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2952 if (clip_star.available && !clip_star.owned && clip_isautosel())
2953 attr = hl_attr(HLF_VNC);
2954#endif
2955 }
2956 }
2957
2958 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002959 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 */
2961 else
2962#endif /* FEAT_VISUAL */
2963 if (highlight_match
2964 && wp == curwin
2965 && lnum >= curwin->w_cursor.lnum
2966 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2967 {
2968 if (lnum == curwin->w_cursor.lnum)
2969 getvcol(curwin, &(curwin->w_cursor),
2970 (colnr_T *)&fromcol, NULL, NULL);
2971 else
2972 fromcol = 0;
2973 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2974 {
2975 pos.lnum = lnum;
2976 pos.col = search_match_endcol;
2977 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2978 }
2979 else
2980 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00002981 /* do at least one character; happens when past end of line */
2982 if (fromcol == tocol)
2983 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 area_highlighting = TRUE;
2985 attr = hl_attr(HLF_I);
2986 }
2987
2988#ifdef FEAT_DIFF
2989 filler_lines = diff_check(wp, lnum);
2990 if (filler_lines < 0)
2991 {
2992 if (filler_lines == -1)
2993 {
2994 if (diff_find_change(wp, lnum, &change_start, &change_end))
2995 diff_hlf = HLF_ADD; /* added line */
2996 else if (change_start == 0)
2997 diff_hlf = HLF_TXD; /* changed text */
2998 else
2999 diff_hlf = HLF_CHD; /* changed line */
3000 }
3001 else
3002 diff_hlf = HLF_ADD; /* added line */
3003 filler_lines = 0;
3004 area_highlighting = TRUE;
3005 }
3006 if (lnum == wp->w_topline)
3007 filler_lines = wp->w_topfill;
3008 filler_todo = filler_lines;
3009#endif
3010
3011#ifdef LINE_ATTR
3012# ifdef FEAT_SIGNS
3013 /* If this line has a sign with line highlighting set line_attr. */
3014 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3015 if (v != 0)
3016 line_attr = sign_get_attr((int)v, TRUE);
3017# endif
3018# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3019 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003020 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 line_attr = hl_attr(HLF_L);
3022# endif
3023 if (line_attr != 0)
3024 area_highlighting = TRUE;
3025#endif
3026
3027 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3028 ptr = line;
3029
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003030#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003031 if (has_spell)
3032 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003033 /* For checking first word with a capital skip white space. */
3034 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003035 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003036
Bram Moolenaar30abd282005-06-22 22:35:10 +00003037 /* To be able to spell-check over line boundaries copy the end of the
3038 * current line into nextline[]. Above the start of the next line was
3039 * copied to nextline[SPWORDLEN]. */
3040 if (nextline[SPWORDLEN] == NUL)
3041 {
3042 /* No next line or it is empty. */
3043 nextlinecol = MAXCOL;
3044 nextline_idx = 0;
3045 }
3046 else
3047 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003048 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003049 if (v < SPWORDLEN)
3050 {
3051 /* Short line, use it completely and append the start of the
3052 * next line. */
3053 nextlinecol = 0;
3054 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003055 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003056 nextline_idx = v + 1;
3057 }
3058 else
3059 {
3060 /* Long line, use only the last SPWORDLEN bytes. */
3061 nextlinecol = v - SPWORDLEN;
3062 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3063 nextline_idx = SPWORDLEN + 1;
3064 }
3065 }
3066 }
3067#endif
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 /* find start of trailing whitespace */
3070 if (wp->w_p_list && lcs_trail)
3071 {
3072 trailcol = (colnr_T)STRLEN(ptr);
3073 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3074 --trailcol;
3075 trailcol += (colnr_T) (ptr - line);
3076 extra_check = TRUE;
3077 }
3078
3079 /*
3080 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3081 * first character to be displayed.
3082 */
3083 if (wp->w_p_wrap)
3084 v = wp->w_skipcol;
3085 else
3086 v = wp->w_leftcol;
3087 if (v > 0)
3088 {
3089#ifdef FEAT_MBYTE
3090 char_u *prev_ptr = ptr;
3091#endif
3092 while (vcol < v && *ptr != NUL)
3093 {
3094 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3095 vcol += c;
3096#ifdef FEAT_MBYTE
3097 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003099 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003102#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3103 /* When:
3104 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003105 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003106 * - 'virtualedit' is set, or
3107 * - the visual mode is active,
3108 * the end of the line may be before the start of the displayed part.
3109 */
3110 if (vcol < v && (
3111# ifdef FEAT_SYN_HL
3112 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003113 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003114# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3115 ||
3116# endif
3117# endif
3118# ifdef FEAT_VIRTUALEDIT
3119 virtual_active()
3120# ifdef FEAT_VISUAL
3121 ||
3122# endif
3123# endif
3124# ifdef FEAT_VISUAL
3125 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3126# endif
3127 ))
3128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#endif
3132
3133 /* Handle a character that's not completely on the screen: Put ptr at
3134 * that character but skip the first few screen characters. */
3135 if (vcol > v)
3136 {
3137 vcol -= c;
3138#ifdef FEAT_MBYTE
3139 ptr = prev_ptr;
3140#else
3141 --ptr;
3142#endif
3143 n_skip = v - vcol;
3144 }
3145
3146 /*
3147 * Adjust for when the inverted text is before the screen,
3148 * and when the start of the inverted text is before the screen.
3149 */
3150 if (tocol <= vcol)
3151 fromcol = 0;
3152 else if (fromcol >= 0 && fromcol < vcol)
3153 fromcol = vcol;
3154
3155#ifdef FEAT_LINEBREAK
3156 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3157 if (wp->w_p_wrap)
3158 need_showbreak = TRUE;
3159#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003160#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003161 /* When spell checking a word we need to figure out the start of the
3162 * word and if it's badly spelled or not. */
3163 if (has_spell)
3164 {
3165 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003166 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003167 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003168
3169 pos = wp->w_cursor;
3170 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003171 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003172 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003173
3174 /* spell_move_to() may call ml_get() and make "line" invalid */
3175 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3176 ptr = line + linecol;
3177
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003178 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003179 {
3180 /* no bad word found at line start, don't check until end of a
3181 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003182 spell_hlf = HLF_COUNT;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003183 word_end = (int)(spell_to_word_end(ptr, wp)
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003184 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003185 }
3186 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003187 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003188 /* bad word found, use attributes until end of word */
3189 word_end = wp->w_cursor.col + len + 1;
3190
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003191 /* Turn index into actual attributes. */
3192 if (spell_hlf != HLF_COUNT)
3193 spell_attr = highlight_attr[spell_hlf];
3194 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003195 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003196
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003197# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003198 /* Need to restart syntax highlighting for this line. */
3199 if (has_syntax)
3200 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003201# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003202 }
3203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205
3206 /*
3207 * Correct highlighting for cursor that can't be disabled.
3208 * Avoids having to check this for each character.
3209 */
3210 if (fromcol >= 0)
3211 {
3212 if (noinvcur)
3213 {
3214 if ((colnr_T)fromcol == wp->w_virtcol)
3215 {
3216 /* highlighting starts at cursor, let it start just after the
3217 * cursor */
3218 fromcol_prev = fromcol;
3219 fromcol = -1;
3220 }
3221 else if ((colnr_T)fromcol < wp->w_virtcol)
3222 /* restart highlighting after the cursor */
3223 fromcol_prev = wp->w_virtcol;
3224 }
3225 if (fromcol >= tocol)
3226 fromcol = -1;
3227 }
3228
3229#ifdef FEAT_SEARCH_EXTRA
3230 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003231 * Handle highlighting the last used search pattern and matches.
3232 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003234 cur = wp->w_match_head;
3235 shl_flag = FALSE;
3236 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003238 if (shl_flag == FALSE)
3239 {
3240 shl = &search_hl;
3241 shl_flag = TRUE;
3242 }
3243 else
3244 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003245 shl->startcol = MAXCOL;
3246 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 shl->attr_cur = 0;
3248 if (shl->rm.regprog != NULL)
3249 {
3250 v = (long)(ptr - line);
3251 next_search_hl(wp, shl, lnum, (colnr_T)v);
3252
3253 /* Need to get the line again, a multi-line regexp may have made it
3254 * invalid. */
3255 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3256 ptr = line + v;
3257
3258 if (shl->lnum != 0 && shl->lnum <= lnum)
3259 {
3260 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003261 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003263 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3265 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003266 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003268 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003270 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003273 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003274 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 else
3276#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003277 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003279 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 shl->attr_cur = shl->attr;
3282 search_attr = shl->attr;
3283 }
3284 area_highlighting = TRUE;
3285 }
3286 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003287 if (shl != &search_hl && cur != NULL)
3288 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290#endif
3291
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003292#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003293 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3294 * active, because it's not clear what is selected then. */
3295 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003296 {
3297 line_attr = hl_attr(HLF_CUL);
3298 area_highlighting = TRUE;
3299 }
3300#endif
3301
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003302 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 col = 0;
3304#ifdef FEAT_RIGHTLEFT
3305 if (wp->w_p_rl)
3306 {
3307 /* Rightleft window: process the text in the normal direction, but put
3308 * it in current_ScreenLine[] from right to left. Start at the
3309 * rightmost column of the window. */
3310 col = W_WIDTH(wp) - 1;
3311 off += col;
3312 }
3313#endif
3314
3315 /*
3316 * Repeat for the whole displayed line.
3317 */
3318 for (;;)
3319 {
3320 /* Skip this quickly when working on the text. */
3321 if (draw_state != WL_LINE)
3322 {
3323#ifdef FEAT_CMDWIN
3324 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3325 {
3326 draw_state = WL_CMDLINE;
3327 if (cmdwin_type != 0 && wp == curwin)
3328 {
3329 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003331 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 char_attr = hl_attr(HLF_AT);
3333 }
3334 }
3335#endif
3336
3337#ifdef FEAT_FOLDING
3338 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3339 {
3340 draw_state = WL_FOLD;
3341 if (wp->w_p_fdc > 0)
3342 {
3343 /* Draw the 'foldcolumn'. */
3344 fill_foldcolumn(extra, wp, FALSE, lnum);
3345 n_extra = wp->w_p_fdc;
3346 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003347 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 c_extra = NUL;
3349 char_attr = hl_attr(HLF_FC);
3350 }
3351 }
3352#endif
3353
3354#ifdef FEAT_SIGNS
3355 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3356 {
3357 draw_state = WL_SIGN;
3358 /* Show the sign column when there are any signs in this
3359 * buffer or when using Netbeans. */
3360 if (draw_signcolumn(wp)
3361# ifdef FEAT_DIFF
3362 && filler_todo <= 0
3363# endif
3364 )
3365 {
3366 int_u text_sign;
3367# ifdef FEAT_SIGN_ICONS
3368 int_u icon_sign;
3369# endif
3370
3371 /* Draw two cells with the sign value or blank. */
3372 c_extra = ' ';
3373 char_attr = hl_attr(HLF_SC);
3374 n_extra = 2;
3375
3376 if (row == startrow)
3377 {
3378 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3379 SIGN_TEXT);
3380# ifdef FEAT_SIGN_ICONS
3381 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3382 SIGN_ICON);
3383 if (gui.in_use && icon_sign != 0)
3384 {
3385 /* Use the image in this position. */
3386 c_extra = SIGN_BYTE;
3387# ifdef FEAT_NETBEANS_INTG
3388 if (buf_signcount(wp->w_buffer, lnum) > 1)
3389 c_extra = MULTISIGN_BYTE;
3390# endif
3391 char_attr = icon_sign;
3392 }
3393 else
3394# endif
3395 if (text_sign != 0)
3396 {
3397 p_extra = sign_get_text(text_sign);
3398 if (p_extra != NULL)
3399 {
3400 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003401 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403 char_attr = sign_get_attr(text_sign, FALSE);
3404 }
3405 }
3406 }
3407 }
3408#endif
3409
3410 if (draw_state == WL_NR - 1 && n_extra == 0)
3411 {
3412 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003413 /* Display the absolute or relative line number. After the
3414 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3415 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 && (row == startrow
3417#ifdef FEAT_DIFF
3418 + filler_lines
3419#endif
3420 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3421 {
3422 /* Draw the line number (empty space after wrapping). */
3423 if (row == startrow
3424#ifdef FEAT_DIFF
3425 + filler_lines
3426#endif
3427 )
3428 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003429 long num;
3430
3431 if (wp->w_p_nu)
3432 /* 'number' */
3433 num = (long)lnum;
3434 else
3435 /* 'relativenumber', don't use negative numbers */
3436 num = (long)abs((int)get_cursor_rel_lnum(wp,
3437 lnum));
3438
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003439 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003440 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 if (wp->w_skipcol > 0)
3442 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3443 *p_extra = '-';
3444#ifdef FEAT_RIGHTLEFT
3445 if (wp->w_p_rl) /* reverse line numbers */
3446 rl_mirror(extra);
3447#endif
3448 p_extra = extra;
3449 c_extra = NUL;
3450 }
3451 else
3452 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003453 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003455#ifdef FEAT_SYN_HL
3456 /* When 'cursorline' is set highlight the line number of
3457 * the current line differently. */
3458 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3459 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462 }
3463
3464#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3465 if (draw_state == WL_SBR - 1 && n_extra == 0)
3466 {
3467 draw_state = WL_SBR;
3468# ifdef FEAT_DIFF
3469 if (filler_todo > 0)
3470 {
3471 /* Draw "deleted" diff line(s). */
3472 if (char2cells(fill_diff) > 1)
3473 c_extra = '-';
3474 else
3475 c_extra = fill_diff;
3476# ifdef FEAT_RIGHTLEFT
3477 if (wp->w_p_rl)
3478 n_extra = col + 1;
3479 else
3480# endif
3481 n_extra = W_WIDTH(wp) - col;
3482 char_attr = hl_attr(HLF_DED);
3483 }
3484# endif
3485# ifdef FEAT_LINEBREAK
3486 if (*p_sbr != NUL && need_showbreak)
3487 {
3488 /* Draw 'showbreak' at the start of each broken line. */
3489 p_extra = p_sbr;
3490 c_extra = NUL;
3491 n_extra = (int)STRLEN(p_sbr);
3492 char_attr = hl_attr(HLF_AT);
3493 need_showbreak = FALSE;
3494 /* Correct end of highlighted area for 'showbreak',
3495 * required when 'linebreak' is also set. */
3496 if (tocol == vcol)
3497 tocol += n_extra;
3498 }
3499# endif
3500 }
3501#endif
3502
3503 if (draw_state == WL_LINE - 1 && n_extra == 0)
3504 {
3505 draw_state = WL_LINE;
3506 if (saved_n_extra)
3507 {
3508 /* Continue item from end of wrapped line. */
3509 n_extra = saved_n_extra;
3510 c_extra = saved_c_extra;
3511 p_extra = saved_p_extra;
3512 char_attr = saved_char_attr;
3513 }
3514 else
3515 char_attr = 0;
3516 }
3517 }
3518
3519 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003520 if (dollar_vcol != 0 && wp == curwin
3521 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#ifdef FEAT_DIFF
3523 && filler_todo <= 0
3524#endif
3525 )
3526 {
3527 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3528 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003529 /* Pretend we have finished updating the window. Except when
3530 * 'cursorcolumn' is set. */
3531#ifdef FEAT_SYN_HL
3532 if (wp->w_p_cuc)
3533 row = wp->w_cline_row + wp->w_cline_height;
3534 else
3535#endif
3536 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 break;
3538 }
3539
3540 if (draw_state == WL_LINE && area_highlighting)
3541 {
3542 /* handle Visual or match highlighting in this line */
3543 if (vcol == fromcol
3544#ifdef FEAT_MBYTE
3545 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3546 && (*mb_ptr2cells)(ptr) > 1)
3547#endif
3548 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003549 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 && vcol < tocol))
3551 area_attr = attr; /* start highlighting */
3552 else if (area_attr != 0
3553 && (vcol == tocol
3554 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
3557#ifdef FEAT_SEARCH_EXTRA
3558 if (!n_extra)
3559 {
3560 /*
3561 * Check for start/end of search pattern match.
3562 * After end, check for start/end of next match.
3563 * When another match, have to check for start again.
3564 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003565 * Do this for 'search_hl' and the match list (ordered by
3566 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003568 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003569 cur = wp->w_match_head;
3570 shl_flag = FALSE;
3571 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003573 if (shl_flag == FALSE
3574 && ((cur != NULL
3575 && cur->priority > SEARCH_HL_PRIORITY)
3576 || cur == NULL))
3577 {
3578 shl = &search_hl;
3579 shl_flag = TRUE;
3580 }
3581 else
3582 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 while (shl->rm.regprog != NULL)
3584 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003585 if (shl->startcol != MAXCOL
3586 && v >= (long)shl->startcol
3587 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 shl->attr_cur = shl->attr;
3590 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003591 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
3593 shl->attr_cur = 0;
3594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 next_search_hl(wp, shl, lnum, (colnr_T)v);
3596
3597 /* Need to get the line again, a multi-line regexp
3598 * may have made it invalid. */
3599 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3600 ptr = line + v;
3601
3602 if (shl->lnum == lnum)
3603 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003604 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003606 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003608 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003610 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
3612 /* highlight empty match, try again after
3613 * it */
3614#ifdef FEAT_MBYTE
3615 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003616 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003617 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 else
3619#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003620 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622
3623 /* Loop to check if the match starts at the
3624 * current position */
3625 continue;
3626 }
3627 }
3628 break;
3629 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003630 if (shl != &search_hl && cur != NULL)
3631 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003633
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003634 /* Use attributes from match with highest priority among
3635 * 'search_hl' and the match list. */
3636 search_attr = search_hl.attr_cur;
3637 cur = wp->w_match_head;
3638 shl_flag = FALSE;
3639 while (cur != NULL || shl_flag == FALSE)
3640 {
3641 if (shl_flag == FALSE
3642 && ((cur != NULL
3643 && cur->priority > SEARCH_HL_PRIORITY)
3644 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003645 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003646 shl = &search_hl;
3647 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003648 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003649 else
3650 shl = &cur->hl;
3651 if (shl->attr_cur != 0)
3652 search_attr = shl->attr_cur;
3653 if (shl != &search_hl && cur != NULL)
3654 cur = cur->next;
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657#endif
3658
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003660 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003662 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3663 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003665 if (diff_hlf == HLF_TXD && ptr - line > change_end
3666 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003668 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003671
3672 /* Decide which of the highlight attributes to use. */
3673 attr_pri = TRUE;
3674 if (area_attr != 0)
3675 char_attr = area_attr;
3676 else if (search_attr != 0)
3677 char_attr = search_attr;
3678#ifdef LINE_ATTR
3679 /* Use line_attr when not in the Visual or 'incsearch' area
3680 * (area_attr may be 0 when "noinvcur" is set). */
3681 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003682 || vcol < fromcol || vcol_prev < fromcol_prev
3683 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003684 char_attr = line_attr;
3685#endif
3686 else
3687 {
3688 attr_pri = FALSE;
3689#ifdef FEAT_SYN_HL
3690 if (has_syntax)
3691 char_attr = syntax_attr;
3692 else
3693#endif
3694 char_attr = 0;
3695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697
3698 /*
3699 * Get the next character to put on the screen.
3700 */
3701 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003702 * The "p_extra" points to the extra stuff that is inserted to
3703 * represent special characters (non-printable stuff) and other
3704 * things. When all characters are the same, c_extra is used.
3705 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3706 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3708 */
3709 if (n_extra > 0)
3710 {
3711 if (c_extra != NUL)
3712 {
3713 c = c_extra;
3714#ifdef FEAT_MBYTE
3715 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3716 if (enc_utf8 && (*mb_char2len)(c) > 1)
3717 {
3718 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003719 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003720 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 else
3723 mb_utf8 = FALSE;
3724#endif
3725 }
3726 else
3727 {
3728 c = *p_extra;
3729#ifdef FEAT_MBYTE
3730 if (has_mbyte)
3731 {
3732 mb_c = c;
3733 if (enc_utf8)
3734 {
3735 /* If the UTF-8 character is more than one byte:
3736 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003737 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 mb_utf8 = FALSE;
3739 if (mb_l > n_extra)
3740 mb_l = 1;
3741 else if (mb_l > 1)
3742 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003743 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003745 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747 }
3748 else
3749 {
3750 /* if this is a DBCS character, put it in "mb_c" */
3751 mb_l = MB_BYTE2LEN(c);
3752 if (mb_l >= n_extra)
3753 mb_l = 1;
3754 else if (mb_l > 1)
3755 mb_c = (c << 8) + p_extra[1];
3756 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003757 if (mb_l == 0) /* at the NUL at end-of-line */
3758 mb_l = 1;
3759
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 /* If a double-width char doesn't fit display a '>' in the
3761 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003762 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763# ifdef FEAT_RIGHTLEFT
3764 wp->w_p_rl ? (col <= 0) :
3765# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003766 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 && (*mb_char2cells)(mb_c) == 2)
3768 {
3769 c = '>';
3770 mb_c = c;
3771 mb_l = 1;
3772 mb_utf8 = FALSE;
3773 multi_attr = hl_attr(HLF_AT);
3774 /* put the pointer back to output the double-width
3775 * character at the start of the next line. */
3776 ++n_extra;
3777 --p_extra;
3778 }
3779 else
3780 {
3781 n_extra -= mb_l - 1;
3782 p_extra += mb_l - 1;
3783 }
3784 }
3785#endif
3786 ++p_extra;
3787 }
3788 --n_extra;
3789 }
3790 else
3791 {
3792 /*
3793 * Get a character from the line itself.
3794 */
3795 c = *ptr;
3796#ifdef FEAT_MBYTE
3797 if (has_mbyte)
3798 {
3799 mb_c = c;
3800 if (enc_utf8)
3801 {
3802 /* If the UTF-8 character is more than one byte: Decode it
3803 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003804 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 mb_utf8 = FALSE;
3806 if (mb_l > 1)
3807 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003808 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 /* Overlong encoded ASCII or ASCII with composing char
3810 * is displayed normally, except a NUL. */
3811 if (mb_c < 0x80)
3812 c = mb_c;
3813 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003814
3815 /* At start of the line we can have a composing char.
3816 * Draw it as a space with a composing char. */
3817 if (utf_iscomposing(mb_c))
3818 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003819 int i;
3820
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003821 for (i = Screen_mco - 1; i > 0; --i)
3822 u8cc[i] = u8cc[i - 1];
3823 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003824 mb_c = ' ';
3825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827
3828 if ((mb_l == 1 && c >= 0x80)
3829 || (mb_l >= 1 && mb_c == 0)
3830 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003831# ifdef UNICODE16
3832 || mb_c >= 0x10000
3833# endif
3834 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 {
3836 /*
3837 * Illegal UTF-8 byte: display as <xx>.
3838 * Non-BMP character : display as ? or fullwidth ?.
3839 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003840# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
3844 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003845# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (wp->w_p_rl) /* reverse */
3847 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003850# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 else if (utf_char2cells(mb_c) != 2)
3852 STRCPY(extra, "?");
3853 else
3854 /* 0xff1f in UTF-8: full-width '?' */
3855 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
3858 p_extra = extra;
3859 c = *p_extra;
3860 mb_c = mb_ptr2char_adv(&p_extra);
3861 mb_utf8 = (c >= 0x80);
3862 n_extra = (int)STRLEN(p_extra);
3863 c_extra = NUL;
3864 if (area_attr == 0 && search_attr == 0)
3865 {
3866 n_attr = n_extra + 1;
3867 extra_attr = hl_attr(HLF_8);
3868 saved_attr2 = char_attr; /* save current attr */
3869 }
3870 }
3871 else if (mb_l == 0) /* at the NUL at end-of-line */
3872 mb_l = 1;
3873#ifdef FEAT_ARABIC
3874 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3875 {
3876 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003877 int pc, pc1, nc;
3878 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 /* The idea of what is the previous and next
3881 * character depends on 'rightleft'. */
3882 if (wp->w_p_rl)
3883 {
3884 pc = prev_c;
3885 pc1 = prev_c1;
3886 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003887 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
3889 else
3890 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003891 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003893 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895 prev_c = mb_c;
3896
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003897 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899 else
3900 prev_c = mb_c;
3901#endif
3902 }
3903 else /* enc_dbcs */
3904 {
3905 mb_l = MB_BYTE2LEN(c);
3906 if (mb_l == 0) /* at the NUL at end-of-line */
3907 mb_l = 1;
3908 else if (mb_l > 1)
3909 {
3910 /* We assume a second byte below 32 is illegal.
3911 * Hopefully this is OK for all double-byte encodings!
3912 */
3913 if (ptr[1] >= 32)
3914 mb_c = (c << 8) + ptr[1];
3915 else
3916 {
3917 if (ptr[1] == NUL)
3918 {
3919 /* head byte at end of line */
3920 mb_l = 1;
3921 transchar_nonprint(extra, c);
3922 }
3923 else
3924 {
3925 /* illegal tail byte */
3926 mb_l = 2;
3927 STRCPY(extra, "XX");
3928 }
3929 p_extra = extra;
3930 n_extra = (int)STRLEN(extra) - 1;
3931 c_extra = NUL;
3932 c = *p_extra++;
3933 if (area_attr == 0 && search_attr == 0)
3934 {
3935 n_attr = n_extra + 1;
3936 extra_attr = hl_attr(HLF_8);
3937 saved_attr2 = char_attr; /* save current attr */
3938 }
3939 mb_c = c;
3940 }
3941 }
3942 }
3943 /* If a double-width char doesn't fit display a '>' in the
3944 * last column; the character is displayed at the start of the
3945 * next line. */
3946 if ((
3947# ifdef FEAT_RIGHTLEFT
3948 wp->w_p_rl ? (col <= 0) :
3949# endif
3950 (col >= W_WIDTH(wp) - 1))
3951 && (*mb_char2cells)(mb_c) == 2)
3952 {
3953 c = '>';
3954 mb_c = c;
3955 mb_utf8 = FALSE;
3956 mb_l = 1;
3957 multi_attr = hl_attr(HLF_AT);
3958 /* Put pointer back so that the character will be
3959 * displayed at the start of the next line. */
3960 --ptr;
3961 }
3962 else if (*ptr != NUL)
3963 ptr += mb_l - 1;
3964
3965 /* If a double-width char doesn't fit at the left side display
3966 * a '<' in the first column. */
3967 if (n_skip > 0 && mb_l > 1)
3968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003970 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 c = ' ';
3972 if (area_attr == 0 && search_attr == 0)
3973 {
3974 n_attr = n_extra + 1;
3975 extra_attr = hl_attr(HLF_AT);
3976 saved_attr2 = char_attr; /* save current attr */
3977 }
3978 mb_c = c;
3979 mb_utf8 = FALSE;
3980 mb_l = 1;
3981 }
3982
3983 }
3984#endif
3985 ++ptr;
3986
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003987 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003988 if (wp->w_p_list && (c == 160
3989#ifdef FEAT_MBYTE
3990 || (mb_utf8 && mb_c == 160)
3991#endif
3992 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003993 {
3994 c = lcs_nbsp;
3995 if (area_attr == 0 && search_attr == 0)
3996 {
3997 n_attr = 1;
3998 extra_attr = hl_attr(HLF_8);
3999 saved_attr2 = char_attr; /* save current attr */
4000 }
4001#ifdef FEAT_MBYTE
4002 mb_c = c;
4003 if (enc_utf8 && (*mb_char2len)(c) > 1)
4004 {
4005 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004006 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004007 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004008 }
4009 else
4010 mb_utf8 = FALSE;
4011#endif
4012 }
4013
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 if (extra_check)
4015 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004016#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004017 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004018#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004019
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004020#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 /* Get syntax attribute, unless still at the start of the line
4022 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004023 v = (long)(ptr - line);
4024 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 {
4026 /* Get the syntax attribute for the character. If there
4027 * is an error, disable syntax highlighting. */
4028 save_did_emsg = did_emsg;
4029 did_emsg = FALSE;
4030
Bram Moolenaar217ad922005-03-20 22:37:15 +00004031 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004032# ifdef FEAT_SPELL
4033 has_spell ? &can_spell :
4034# endif
4035 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
4037 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004038 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004039 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004040 has_syntax = FALSE;
4041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 else
4043 did_emsg = save_did_emsg;
4044
4045 /* Need to get the line again, a multi-line regexp may
4046 * have made it invalid. */
4047 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4048 ptr = line + v;
4049
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004050 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004052 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004053 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004054# ifdef FEAT_CONCEAL
4055 /* no concealing past the end of the line, it interferes
4056 * with line highlighting */
4057 if (c == NUL)
4058 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004059 else
4060 syntax_flags = get_syntax_info(&syntax_id);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004063#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004064
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004065#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004066 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004067 * Only do this when there is no syntax highlighting, the
4068 * @Spell cluster is not used or the current syntax item
4069 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004070 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004071 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004072 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004073# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004074 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004075 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004076# endif
4077 if (c != 0 && (
4078# ifdef FEAT_SYN_HL
4079 !has_syntax ||
4080# endif
4081 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004082 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004083 char_u *prev_ptr, *p;
4084 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004085 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004086# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004087 if (has_mbyte)
4088 {
4089 prev_ptr = ptr - mb_l;
4090 v -= mb_l - 1;
4091 }
4092 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004093# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004094 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004095
4096 /* Use nextline[] if possible, it has the start of the
4097 * next line concatenated. */
4098 if ((prev_ptr - line) - nextlinecol >= 0)
4099 p = nextline + (prev_ptr - line) - nextlinecol;
4100 else
4101 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004102 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004103 len = spell_check(wp, p, &spell_hlf, &cap_col,
4104 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004105 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004106
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004107 /* In Insert mode only highlight a word that
4108 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004109 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004110 && (State & INSERT) != 0
4111 && wp->w_cursor.lnum == lnum
4112 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004113 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004114 && wp->w_cursor.col < (colnr_T)word_end)
4115 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004116 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004117 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004118 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004119
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004120 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004121 && (p - nextline) + len > nextline_idx)
4122 {
4123 /* Remember that the good word continues at the
4124 * start of the next line. */
4125 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004126 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004127 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004128
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004129 /* Turn index into actual attributes. */
4130 if (spell_hlf != HLF_COUNT)
4131 spell_attr = highlight_attr[spell_hlf];
4132
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004133 if (cap_col > 0)
4134 {
4135 if (p != prev_ptr
4136 && (p - nextline) + cap_col >= nextline_idx)
4137 {
4138 /* Remember that the word in the next line
4139 * must start with a capital. */
4140 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004141 cap_col = (int)((p - nextline) + cap_col
4142 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004143 }
4144 else
4145 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004146 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004147 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004148 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004149 }
4150 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004151 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004152 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004153 char_attr = hl_combine_attr(char_attr, spell_attr);
4154 else
4155 char_attr = hl_combine_attr(spell_attr, char_attr);
4156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157#endif
4158#ifdef FEAT_LINEBREAK
4159 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004160 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 */
4162 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004163 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
4165 n_extra = win_lbr_chartabsize(wp, ptr - (
4166# ifdef FEAT_MBYTE
4167 has_mbyte ? mb_l :
4168# endif
4169 1), (colnr_T)vcol, NULL) - 1;
4170 c_extra = ' ';
4171 if (vim_iswhite(c))
4172 c = ' ';
4173 }
4174#endif
4175
4176 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4177 {
4178 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004179 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 n_attr = 1;
4182 extra_attr = hl_attr(HLF_8);
4183 saved_attr2 = char_attr; /* save current attr */
4184 }
4185#ifdef FEAT_MBYTE
4186 mb_c = c;
4187 if (enc_utf8 && (*mb_char2len)(c) > 1)
4188 {
4189 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004190 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004191 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 else
4194 mb_utf8 = FALSE;
4195#endif
4196 }
4197 }
4198
4199 /*
4200 * Handling of non-printable characters.
4201 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004202 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
4204 /*
4205 * when getting a character from the file, we may have to
4206 * turn it into something else on the way to putting it
4207 * into "ScreenLines".
4208 */
4209 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4210 {
4211 /* tab amount depends on current column */
4212 n_extra = (int)wp->w_buffer->b_p_ts
4213 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4214#ifdef FEAT_MBYTE
4215 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4216#endif
4217 if (wp->w_p_list)
4218 {
4219 c = lcs_tab1;
4220 c_extra = lcs_tab2;
4221 n_attr = n_extra + 1;
4222 extra_attr = hl_attr(HLF_8);
4223 saved_attr2 = char_attr; /* save current attr */
4224#ifdef FEAT_MBYTE
4225 mb_c = c;
4226 if (enc_utf8 && (*mb_char2len)(c) > 1)
4227 {
4228 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004229 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004230 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232#endif
4233 }
4234 else
4235 {
4236 c_extra = ' ';
4237 c = ' ';
4238 }
4239 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004240 else if (c == NUL
4241 && ((wp->w_p_list && lcs_eol > 0)
4242 || ((fromcol >= 0 || fromcol_prev >= 0)
4243 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004244#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004245 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004246#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004247 && (
4248# ifdef FEAT_RIGHTLEFT
4249 wp->w_p_rl ? (col >= 0) :
4250# endif
4251 (col < W_WIDTH(wp)))
4252 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004253 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004254 && (colnr_T)vcol == wp->w_virtcol)))
4255 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004257 /* Display a '$' after the line or highlight an extra
4258 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4260 /* For a diff line the highlighting continues after the
4261 * "$". */
4262 if (
4263# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004264 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265# ifdef LINE_ATTR
4266 &&
4267# endif
4268# endif
4269# ifdef LINE_ATTR
4270 line_attr == 0
4271# endif
4272 )
4273#endif
4274 {
4275#ifdef FEAT_VIRTUALEDIT
4276 /* In virtualedit, visual selections may extend
4277 * beyond end of line. */
4278 if (area_highlighting && virtual_active()
4279 && tocol != MAXCOL && vcol < tocol)
4280 n_extra = 0;
4281 else
4282#endif
4283 {
4284 p_extra = at_end_str;
4285 n_extra = 1;
4286 c_extra = NUL;
4287 }
4288 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004289 if (wp->w_p_list)
4290 c = lcs_eol;
4291 else
4292 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 lcs_eol_one = -1;
4294 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004295 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 {
4297 extra_attr = hl_attr(HLF_AT);
4298 n_attr = 1;
4299 }
4300#ifdef FEAT_MBYTE
4301 mb_c = c;
4302 if (enc_utf8 && (*mb_char2len)(c) > 1)
4303 {
4304 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004305 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004306 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308 else
4309 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4310#endif
4311 }
4312 else if (c != NUL)
4313 {
4314 p_extra = transchar(c);
4315#ifdef FEAT_RIGHTLEFT
4316 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4317 rl_mirror(p_extra); /* reverse "<12>" */
4318#endif
4319 n_extra = byte2cells(c) - 1;
4320 c_extra = NUL;
4321 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004322 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
4324 n_attr = n_extra + 1;
4325 extra_attr = hl_attr(HLF_8);
4326 saved_attr2 = char_attr; /* save current attr */
4327 }
4328#ifdef FEAT_MBYTE
4329 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4330#endif
4331 }
4332#ifdef FEAT_VIRTUALEDIT
4333 else if (VIsual_active
4334 && (VIsual_mode == Ctrl_V
4335 || VIsual_mode == 'v')
4336 && virtual_active()
4337 && tocol != MAXCOL
4338 && vcol < tocol
4339 && (
4340# ifdef FEAT_RIGHTLEFT
4341 wp->w_p_rl ? (col >= 0) :
4342# endif
4343 (col < W_WIDTH(wp))))
4344 {
4345 c = ' ';
4346 --ptr; /* put it back at the NUL */
4347 }
4348#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004349#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 else if ((
4351# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004352 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 ) && (
4356# ifdef FEAT_RIGHTLEFT
4357 wp->w_p_rl ? (col >= 0) :
4358# endif
4359 (col < W_WIDTH(wp))))
4360 {
4361 /* Highlight until the right side of the window */
4362 c = ' ';
4363 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004364
4365 /* Remember we do the char for line highlighting. */
4366 ++did_line_attr;
4367
4368 /* don't do search HL for the rest of the line */
4369 if (line_attr != 0 && char_attr == search_attr && col > 0)
4370 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371# ifdef FEAT_DIFF
4372 if (diff_hlf == HLF_TXD)
4373 {
4374 diff_hlf = HLF_CHD;
4375 if (attr == 0 || char_attr != attr)
4376 char_attr = hl_attr(diff_hlf);
4377 }
4378# endif
4379 }
4380#endif
4381 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004382
4383#ifdef FEAT_CONCEAL
Bram Moolenaara7781e02010-07-19 20:13:22 +02004384 if ( wp->w_p_conc > 0
4385 && (lnum != wp->w_cursor.lnum || curwin != wp)
4386 && (syntax_flags & HL_CONCEAL) != 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004387 {
4388 char_attr = conceal_attr;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004389 if (prev_syntax_id != syntax_id
4390 && (syn_get_sub_char() != NUL || wp->w_p_conc == 1)
4391 && wp->w_p_conc != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004392 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004393 /* First time at this concealed item: display one
4394 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004395 if (syn_get_sub_char() != NUL)
4396 c = syn_get_sub_char();
4397 else if (lcs_conceal != NUL)
4398 c = lcs_conceal;
4399 else
4400 c = ' ';
4401
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004402 prev_syntax_id = syntax_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004403
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004404 if (n_extra > 0)
4405 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004406 vcol += n_extra;
4407 if (wp->w_p_wrap && n_extra > 0)
4408 {
4409# ifdef FEAT_RIGHTLEFT
4410 if (wp->w_p_rl)
4411 {
4412 col -= n_extra;
4413 boguscols -= n_extra;
4414 }
4415 else
4416# endif
4417 {
4418 boguscols += n_extra;
4419 col += n_extra;
4420 }
4421 }
4422 n_extra = 0;
4423 n_attr = 0;
4424 }
4425 else if (n_skip == 0)
4426 {
4427 is_concealing = TRUE;
4428 n_skip = 1;
4429 }
4430# ifdef FEAT_MBYTE
4431 mb_c = c;
4432 if (enc_utf8 && (*mb_char2len)(c) > 1)
4433 {
4434 mb_utf8 = TRUE;
4435 u8cc[0] = 0;
4436 c = 0xc0;
4437 }
4438 else
4439 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4440# endif
4441 }
4442 else
4443 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004444 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004445 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004446 }
4447#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449
4450 /* Don't override visual selection highlighting. */
4451 if (n_attr > 0
4452 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004453 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 char_attr = extra_attr;
4455
Bram Moolenaar81695252004-12-29 20:58:21 +00004456#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 /* XIM don't send preedit_start and preedit_end, but they send
4458 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4459 * im_is_preediting() here. */
4460 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004461 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 && (State & INSERT)
4463 && !p_imdisable
4464 && im_is_preediting()
4465 && draw_state == WL_LINE)
4466 {
4467 colnr_T tcol;
4468
4469 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004470 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 else
4472 tcol = preedit_end_col;
4473 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4474 {
4475 if (feedback_old_attr < 0)
4476 {
4477 feedback_col = 0;
4478 feedback_old_attr = char_attr;
4479 }
4480 char_attr = im_get_feedback_attr(feedback_col);
4481 if (char_attr < 0)
4482 char_attr = feedback_old_attr;
4483 feedback_col++;
4484 }
4485 else if (feedback_old_attr >= 0)
4486 {
4487 char_attr = feedback_old_attr;
4488 feedback_old_attr = -1;
4489 feedback_col = 0;
4490 }
4491 }
4492#endif
4493 /*
4494 * Handle the case where we are in column 0 but not on the first
4495 * character of the line and the user wants us to show us a
4496 * special character (via 'listchars' option "precedes:<char>".
4497 */
4498 if (lcs_prec_todo != NUL
4499 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4500#ifdef FEAT_DIFF
4501 && filler_todo <= 0
4502#endif
4503 && draw_state > WL_NR
4504 && c != NUL)
4505 {
4506 c = lcs_prec;
4507 lcs_prec_todo = NUL;
4508#ifdef FEAT_MBYTE
4509 mb_c = c;
4510 if (enc_utf8 && (*mb_char2len)(c) > 1)
4511 {
4512 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004513 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004514 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516 else
4517 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4518#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004519 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
4521 saved_attr3 = char_attr; /* save current attr */
4522 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4523 n_attr3 = 1;
4524 }
4525 }
4526
4527 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004528 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004530 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004531#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004532 || did_line_attr == 1
4533#endif
4534 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004536#ifdef FEAT_SEARCH_EXTRA
4537 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004538
4539 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004540 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004541 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004542#endif
4543
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004544 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 * highlight match at end of line. If it's beyond the last
4546 * char on the screen, just overwrite that one (tricky!) Not
4547 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004548#ifdef FEAT_SEARCH_EXTRA
4549 prevcol_hl_flag = FALSE;
4550 if (prevcol == (long)search_hl.startcol)
4551 prevcol_hl_flag = TRUE;
4552 else
4553 {
4554 cur = wp->w_match_head;
4555 while (cur != NULL)
4556 {
4557 if (prevcol == (long)cur->hl.startcol)
4558 {
4559 prevcol_hl_flag = TRUE;
4560 break;
4561 }
4562 cur = cur->next;
4563 }
4564 }
4565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004567 && ((area_attr != 0 && vcol == fromcol
4568#ifdef FEAT_VISUAL
4569 && (VIsual_mode != Ctrl_V
4570 || lnum == VIsual.lnum
4571 || lnum == curwin->w_cursor.lnum)
4572#endif
4573 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574#ifdef FEAT_SEARCH_EXTRA
4575 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004576 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004577# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004578 && did_line_attr <= 1
4579# endif
4580 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581#endif
4582 ))
4583 {
4584 int n = 0;
4585
4586#ifdef FEAT_RIGHTLEFT
4587 if (wp->w_p_rl)
4588 {
4589 if (col < 0)
4590 n = 1;
4591 }
4592 else
4593#endif
4594 {
4595 if (col >= W_WIDTH(wp))
4596 n = -1;
4597 }
4598 if (n != 0)
4599 {
4600 /* At the window boundary, highlight the last character
4601 * instead (better than nothing). */
4602 off += n;
4603 col += n;
4604 }
4605 else
4606 {
4607 /* Add a blank character to highlight. */
4608 ScreenLines[off] = ' ';
4609#ifdef FEAT_MBYTE
4610 if (enc_utf8)
4611 ScreenLinesUC[off] = 0;
4612#endif
4613 }
4614#ifdef FEAT_SEARCH_EXTRA
4615 if (area_attr == 0)
4616 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004617 /* Use attributes from match with highest priority among
4618 * 'search_hl' and the match list. */
4619 char_attr = search_hl.attr;
4620 cur = wp->w_match_head;
4621 shl_flag = FALSE;
4622 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004623 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004624 if (shl_flag == FALSE
4625 && ((cur != NULL
4626 && cur->priority > SEARCH_HL_PRIORITY)
4627 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004628 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004629 shl = &search_hl;
4630 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004631 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004632 else
4633 shl = &cur->hl;
4634 if ((ptr - line) - 1 == (long)shl->startcol)
4635 char_attr = shl->attr;
4636 if (shl != &search_hl && cur != NULL)
4637 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640#endif
4641 ScreenAttrs[off] = char_attr;
4642#ifdef FEAT_RIGHTLEFT
4643 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004646 --off;
4647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 else
4649#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004652 ++off;
4653 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004654 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004655#ifdef FEAT_SYN_HL
4656 eol_hl_off = 1;
4657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
Bram Moolenaar91170f82006-05-05 21:15:17 +00004661 /*
4662 * At end of the text line.
4663 */
4664 if (c == NUL)
4665 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004666#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004667 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4668 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004669 {
4670 /* highlight last char after line */
4671 --col;
4672 --off;
4673 --vcol;
4674 }
4675
Bram Moolenaar1a384422010-07-14 19:53:30 +02004676 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004677 if (wp->w_p_wrap)
4678 v = wp->w_skipcol;
4679 else
4680 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004681
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004682 /* check if line ends before left margin */
4683 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004684 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004685#ifdef FEAT_CONCEAL
4686 /* Get rid of the boguscols now, we want to draw until the right
4687 * edge for 'cursorcolumn'. */
4688 col -= boguscols;
4689 boguscols = 0;
4690#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004691
4692 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004693 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004694
4695 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004696 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4697 && (int)wp->w_virtcol <
4698 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004699 && lnum != wp->w_cursor.lnum)
4700 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004701# ifdef FEAT_RIGHTLEFT
4702 && !wp->w_p_rl
4703# endif
4704 )
4705 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004706 int rightmost_vcol = 0;
4707 int i;
4708
4709 if (wp->w_p_cuc)
4710 rightmost_vcol = wp->w_virtcol;
4711 if (draw_color_col)
4712 /* determine rightmost colorcolumn to possibly draw */
4713 for (i = 0; color_cols[i] >= 0; ++i)
4714 if (rightmost_vcol < color_cols[i])
4715 rightmost_vcol = color_cols[i];
4716
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004717 while (col < W_WIDTH(wp))
4718 {
4719 ScreenLines[off] = ' ';
4720#ifdef FEAT_MBYTE
4721 if (enc_utf8)
4722 ScreenLinesUC[off] = 0;
4723#endif
4724 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004725 if (draw_color_col)
4726 draw_color_col = advance_color_col(VCOL_HLC,
4727 &color_cols);
4728
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004729 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004730 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004731 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004732 ScreenAttrs[off++] = hl_attr(HLF_MC);
4733 else
4734 ScreenAttrs[off++] = 0;
4735
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004736 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004737 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004738
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004739 ++vcol;
4740 }
4741 }
4742#endif
4743
Bram Moolenaar860cae12010-06-05 23:22:07 +02004744 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4745 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 row++;
4747
4748 /*
4749 * Update w_cline_height and w_cline_folded if the cursor line was
4750 * updated (saves a call to plines() later).
4751 */
4752 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4753 {
4754 curwin->w_cline_row = startrow;
4755 curwin->w_cline_height = row - startrow;
4756#ifdef FEAT_FOLDING
4757 curwin->w_cline_folded = FALSE;
4758#endif
4759 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4760 }
4761
4762 break;
4763 }
4764
4765 /* line continues beyond line end */
4766 if (lcs_ext
4767 && !wp->w_p_wrap
4768#ifdef FEAT_DIFF
4769 && filler_todo <= 0
4770#endif
4771 && (
4772#ifdef FEAT_RIGHTLEFT
4773 wp->w_p_rl ? col == 0 :
4774#endif
4775 col == W_WIDTH(wp) - 1)
4776 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004777 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4779 {
4780 c = lcs_ext;
4781 char_attr = hl_attr(HLF_AT);
4782#ifdef FEAT_MBYTE
4783 mb_c = c;
4784 if (enc_utf8 && (*mb_char2len)(c) > 1)
4785 {
4786 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004787 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004788 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 else
4791 mb_utf8 = FALSE;
4792#endif
4793 }
4794
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004795#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004796 /* advance to the next 'colorcolumn' */
4797 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004798 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004799
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004800 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004801 * highlight the cursor position itself.
4802 * Also highlight the 'colorcolumn' if it is different than
4803 * 'cursorcolumn' */
4804 vcol_save_attr = -1;
4805 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004806 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004807 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004808 && lnum != wp->w_cursor.lnum)
4809 {
4810 vcol_save_attr = char_attr;
4811 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4812 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004813 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004814 {
4815 vcol_save_attr = char_attr;
4816 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4817 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004818 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004819#endif
4820
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 /*
4822 * Store character to be displayed.
4823 * Skip characters that are left of the screen for 'nowrap'.
4824 */
4825 vcol_prev = vcol;
4826 if (draw_state < WL_LINE || n_skip <= 0)
4827 {
4828 /*
4829 * Store the character.
4830 */
4831#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4832 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4833 {
4834 /* A double-wide character is: put first halve in left cell. */
4835 --off;
4836 --col;
4837 }
4838#endif
4839 ScreenLines[off] = c;
4840#ifdef FEAT_MBYTE
4841 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004842 {
4843 if ((mb_c & 0xff00) == 0x8e00)
4844 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 else if (enc_utf8)
4848 {
4849 if (mb_utf8)
4850 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004851 int i;
4852
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004854 if ((c & 0xff) == 0)
4855 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004856 for (i = 0; i < Screen_mco; ++i)
4857 {
4858 ScreenLinesC[i][off] = u8cc[i];
4859 if (u8cc[i] == 0)
4860 break;
4861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
4863 else
4864 ScreenLinesUC[off] = 0;
4865 }
4866 if (multi_attr)
4867 {
4868 ScreenAttrs[off] = multi_attr;
4869 multi_attr = 0;
4870 }
4871 else
4872#endif
4873 ScreenAttrs[off] = char_attr;
4874
4875#ifdef FEAT_MBYTE
4876 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4877 {
4878 /* Need to fill two screen columns. */
4879 ++off;
4880 ++col;
4881 if (enc_utf8)
4882 /* UTF-8: Put a 0 in the second screen char. */
4883 ScreenLines[off] = 0;
4884 else
4885 /* DBCS: Put second byte in the second screen char. */
4886 ScreenLines[off] = mb_c & 0xff;
4887 ++vcol;
4888 /* When "tocol" is halfway a character, set it to the end of
4889 * the character, otherwise highlighting won't stop. */
4890 if (tocol == vcol)
4891 ++tocol;
4892#ifdef FEAT_RIGHTLEFT
4893 if (wp->w_p_rl)
4894 {
4895 /* now it's time to backup one cell */
4896 --off;
4897 --col;
4898 }
4899#endif
4900 }
4901#endif
4902#ifdef FEAT_RIGHTLEFT
4903 if (wp->w_p_rl)
4904 {
4905 --off;
4906 --col;
4907 }
4908 else
4909#endif
4910 {
4911 ++off;
4912 ++col;
4913 }
4914 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004915#ifdef FEAT_CONCEAL
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004916 else if (wp->w_p_conc > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004917 {
4918 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004919 ++vcol_off;
4920 if (n_extra > 0)
4921 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004922 if (wp->w_p_wrap)
4923 {
4924 /*
4925 * Special voodoo required if 'wrap' is on.
4926 *
4927 * Advance the column indicator to force the line
4928 * drawing to wrap early. This will make the line
4929 * take up the same screen space when parts are concealed,
4930 * so that cursor line computations aren't messed up.
4931 *
4932 * To avoid the fictitious advance of 'col' causing
4933 * trailing junk to be written out of the screen line
4934 * we are building, 'boguscols' keeps track of the number
4935 * of bad columns we have advanced.
4936 */
4937 if (n_extra > 0)
4938 {
4939 vcol += n_extra;
4940# ifdef FEAT_RIGHTLEFT
4941 if (wp->w_p_rl)
4942 {
4943 col -= n_extra;
4944 boguscols -= n_extra;
4945 }
4946 else
4947# endif
4948 {
4949 col += n_extra;
4950 boguscols += n_extra;
4951 }
4952 n_extra = 0;
4953 n_attr = 0;
4954 }
4955
4956
4957# ifdef FEAT_MBYTE
4958 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4959 {
4960 /* Need to fill two screen columns. */
4961# ifdef FEAT_RIGHTLEFT
4962 if (wp->w_p_rl)
4963 {
4964 --boguscols;
4965 --col;
4966 }
4967 else
4968# endif
4969 {
4970 ++boguscols;
4971 ++col;
4972 }
4973 }
4974# endif
4975
4976# ifdef FEAT_RIGHTLEFT
4977 if (wp->w_p_rl)
4978 {
4979 --boguscols;
4980 --col;
4981 }
4982 else
4983# endif
4984 {
4985 ++boguscols;
4986 ++col;
4987 }
4988 }
4989 else
4990 {
4991 if (n_extra > 0)
4992 {
4993 vcol += n_extra;
4994 n_extra = 0;
4995 n_attr = 0;
4996 }
4997 }
4998
4999 }
5000#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 else
5002 --n_skip;
5003
Bram Moolenaar64486672010-05-16 15:46:46 +02005004 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5005 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005006 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#ifdef FEAT_DIFF
5008 && filler_todo <= 0
5009#endif
5010 )
5011 ++vcol;
5012
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005013#ifdef FEAT_SYN_HL
5014 if (vcol_save_attr >= 0)
5015 char_attr = vcol_save_attr;
5016#endif
5017
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 /* restore attributes after "predeces" in 'listchars' */
5019 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5020 char_attr = saved_attr3;
5021
5022 /* restore attributes after last 'listchars' or 'number' char */
5023 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5024 char_attr = saved_attr2;
5025
5026 /*
5027 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005028 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 */
5030 if ((
5031#ifdef FEAT_RIGHTLEFT
5032 wp->w_p_rl ? (col < 0) :
5033#endif
5034 (col >= W_WIDTH(wp)))
5035 && (*ptr != NUL
5036#ifdef FEAT_DIFF
5037 || filler_todo > 0
5038#endif
5039 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
5040 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5041 )
5042 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005043#ifdef FEAT_CONCEAL
5044 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5045 (int)W_WIDTH(wp), wp->w_p_rl);
5046 boguscols = 0;
5047#else
5048 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5049 (int)W_WIDTH(wp), wp->w_p_rl);
5050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 ++row;
5052 ++screen_row;
5053
5054 /* When not wrapping and finished diff lines, or when displayed
5055 * '$' and highlighting until last column, break here. */
5056 if ((!wp->w_p_wrap
5057#ifdef FEAT_DIFF
5058 && filler_todo <= 0
5059#endif
5060 ) || lcs_eol_one == -1)
5061 break;
5062
5063 /* When the window is too narrow draw all "@" lines. */
5064 if (draw_state != WL_LINE
5065#ifdef FEAT_DIFF
5066 && filler_todo <= 0
5067#endif
5068 )
5069 {
5070 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5071#ifdef FEAT_VERTSPLIT
5072 draw_vsep_win(wp, row);
5073#endif
5074 row = endrow;
5075 }
5076
5077 /* When line got too long for screen break here. */
5078 if (row == endrow)
5079 {
5080 ++row;
5081 break;
5082 }
5083
5084 if (screen_cur_row == screen_row - 1
5085#ifdef FEAT_DIFF
5086 && filler_todo <= 0
5087#endif
5088 && W_WIDTH(wp) == Columns)
5089 {
5090 /* Remember that the line wraps, used for modeless copy. */
5091 LineWraps[screen_row - 1] = TRUE;
5092
5093 /*
5094 * Special trick to make copy/paste of wrapped lines work with
5095 * xterm/screen: write an extra character beyond the end of
5096 * the line. This will work with all terminal types
5097 * (regardless of the xn,am settings).
5098 * Only do this on a fast tty.
5099 * Only do this if the cursor is on the current line
5100 * (something has been written in it).
5101 * Don't do this for the GUI.
5102 * Don't do this for double-width characters.
5103 * Don't do this for a window not at the right screen border.
5104 */
5105 if (p_tf
5106#ifdef FEAT_GUI
5107 && !gui.in_use
5108#endif
5109#ifdef FEAT_MBYTE
5110 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005111 && ((*mb_off2cells)(LineOffset[screen_row],
5112 LineOffset[screen_row] + screen_Columns)
5113 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005115 + (int)Columns - 2,
5116 LineOffset[screen_row] + screen_Columns)
5117 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118#endif
5119 )
5120 {
5121 /* First make sure we are at the end of the screen line,
5122 * then output the same character again to let the
5123 * terminal know about the wrap. If the terminal doesn't
5124 * auto-wrap, we overwrite the character. */
5125 if (screen_cur_col != W_WIDTH(wp))
5126 screen_char(LineOffset[screen_row - 1]
5127 + (unsigned)Columns - 1,
5128 screen_row - 1, (int)(Columns - 1));
5129
5130#ifdef FEAT_MBYTE
5131 /* When there is a multi-byte character, just output a
5132 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005133 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5134 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 out_char(' ');
5136 else
5137#endif
5138 out_char(ScreenLines[LineOffset[screen_row - 1]
5139 + (Columns - 1)]);
5140 /* force a redraw of the first char on the next line */
5141 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5142 screen_start(); /* don't know where cursor is now */
5143 }
5144 }
5145
5146 col = 0;
5147 off = (unsigned)(current_ScreenLine - ScreenLines);
5148#ifdef FEAT_RIGHTLEFT
5149 if (wp->w_p_rl)
5150 {
5151 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5152 off += col;
5153 }
5154#endif
5155
5156 /* reset the drawing state for the start of a wrapped line */
5157 draw_state = WL_START;
5158 saved_n_extra = n_extra;
5159 saved_p_extra = p_extra;
5160 saved_c_extra = c_extra;
5161 saved_char_attr = char_attr;
5162 n_extra = 0;
5163 lcs_prec_todo = lcs_prec;
5164#ifdef FEAT_LINEBREAK
5165# ifdef FEAT_DIFF
5166 if (filler_todo <= 0)
5167# endif
5168 need_showbreak = TRUE;
5169#endif
5170#ifdef FEAT_DIFF
5171 --filler_todo;
5172 /* When the filler lines are actually below the last line of the
5173 * file, don't draw the line itself, break here. */
5174 if (filler_todo == 0 && wp->w_botfill)
5175 break;
5176#endif
5177 }
5178
5179 } /* for every character in the line */
5180
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005181#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005182 /* After an empty line check first word for capital. */
5183 if (*skipwhite(line) == NUL)
5184 {
5185 capcol_lnum = lnum + 1;
5186 cap_col = 0;
5187 }
5188#endif
5189
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 return row;
5191}
5192
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005193#ifdef FEAT_MBYTE
5194static int comp_char_differs __ARGS((int, int));
5195
5196/*
5197 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005198 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005199 */
5200 static int
5201comp_char_differs(off_from, off_to)
5202 int off_from;
5203 int off_to;
5204{
5205 int i;
5206
5207 for (i = 0; i < Screen_mco; ++i)
5208 {
5209 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5210 return TRUE;
5211 if (ScreenLinesC[i][off_from] == 0)
5212 break;
5213 }
5214 return FALSE;
5215}
5216#endif
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218/*
5219 * Check whether the given character needs redrawing:
5220 * - the (first byte of the) character is different
5221 * - the attributes are different
5222 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005223 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 */
5225 static int
5226char_needs_redraw(off_from, off_to, cols)
5227 int off_from;
5228 int off_to;
5229 int cols;
5230{
5231 if (cols > 0
5232 && ((ScreenLines[off_from] != ScreenLines[off_to]
5233 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5234
5235#ifdef FEAT_MBYTE
5236 || (enc_dbcs != 0
5237 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5238 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5239 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5240 : (cols > 1 && ScreenLines[off_from + 1]
5241 != ScreenLines[off_to + 1])))
5242 || (enc_utf8
5243 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5244 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005245 && comp_char_differs(off_from, off_to))
5246 || (cols > 1 && ScreenLines[off_from + 1]
5247 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248#endif
5249 ))
5250 return TRUE;
5251 return FALSE;
5252}
5253
5254/*
5255 * Move one "cooked" screen line to the screen, but only the characters that
5256 * have actually changed. Handle insert/delete character.
5257 * "coloff" gives the first column on the screen for this line.
5258 * "endcol" gives the columns where valid characters are.
5259 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5260 * needs to be cleared, negative otherwise.
5261 * "rlflag" is TRUE in a rightleft window:
5262 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5263 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5264 */
5265 static void
5266screen_line(row, coloff, endcol, clear_width
5267#ifdef FEAT_RIGHTLEFT
5268 , rlflag
5269#endif
5270 )
5271 int row;
5272 int coloff;
5273 int endcol;
5274 int clear_width;
5275#ifdef FEAT_RIGHTLEFT
5276 int rlflag;
5277#endif
5278{
5279 unsigned off_from;
5280 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005281#ifdef FEAT_MBYTE
5282 unsigned max_off_from;
5283 unsigned max_off_to;
5284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 int col = 0;
5286#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5287 int hl;
5288#endif
5289 int force = FALSE; /* force update rest of the line */
5290 int redraw_this /* bool: does character need redraw? */
5291#ifdef FEAT_GUI
5292 = TRUE /* For GUI when while-loop empty */
5293#endif
5294 ;
5295 int redraw_next; /* redraw_this for next character */
5296#ifdef FEAT_MBYTE
5297 int clear_next = FALSE;
5298 int char_cells; /* 1: normal char */
5299 /* 2: occupies two display cells */
5300# define CHAR_CELLS char_cells
5301#else
5302# define CHAR_CELLS 1
5303#endif
5304
5305# ifdef FEAT_CLIPBOARD
5306 clip_may_clear_selection(row, row);
5307# endif
5308
5309 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5310 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005311#ifdef FEAT_MBYTE
5312 max_off_from = off_from + screen_Columns;
5313 max_off_to = LineOffset[row] + screen_Columns;
5314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316#ifdef FEAT_RIGHTLEFT
5317 if (rlflag)
5318 {
5319 /* Clear rest first, because it's left of the text. */
5320 if (clear_width > 0)
5321 {
5322 while (col <= endcol && ScreenLines[off_to] == ' '
5323 && ScreenAttrs[off_to] == 0
5324# ifdef FEAT_MBYTE
5325 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5326# endif
5327 )
5328 {
5329 ++off_to;
5330 ++col;
5331 }
5332 if (col <= endcol)
5333 screen_fill(row, row + 1, col + coloff,
5334 endcol + coloff + 1, ' ', ' ', 0);
5335 }
5336 col = endcol + 1;
5337 off_to = LineOffset[row] + col + coloff;
5338 off_from += col;
5339 endcol = (clear_width > 0 ? clear_width : -clear_width);
5340 }
5341#endif /* FEAT_RIGHTLEFT */
5342
5343 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5344
5345 while (col < endcol)
5346 {
5347#ifdef FEAT_MBYTE
5348 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005349 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 else
5351 char_cells = 1;
5352#endif
5353
5354 redraw_this = redraw_next;
5355 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5356 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5357
5358#ifdef FEAT_GUI
5359 /* If the next character was bold, then redraw the current character to
5360 * remove any pixels that might have spilt over into us. This only
5361 * happens in the GUI.
5362 */
5363 if (redraw_next && gui.in_use)
5364 {
5365 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005366 if (hl > HL_ALL)
5367 hl = syn_attr2attr(hl);
5368 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 redraw_this = TRUE;
5370 }
5371#endif
5372
5373 if (redraw_this)
5374 {
5375 /*
5376 * Special handling when 'xs' termcap flag set (hpterm):
5377 * Attributes for characters are stored at the position where the
5378 * cursor is when writing the highlighting code. The
5379 * start-highlighting code must be written with the cursor on the
5380 * first highlighted character. The stop-highlighting code must
5381 * be written with the cursor just after the last highlighted
5382 * character.
5383 * Overwriting a character doesn't remove it's highlighting. Need
5384 * to clear the rest of the line, and force redrawing it
5385 * completely.
5386 */
5387 if ( p_wiv
5388 && !force
5389#ifdef FEAT_GUI
5390 && !gui.in_use
5391#endif
5392 && ScreenAttrs[off_to] != 0
5393 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5394 {
5395 /*
5396 * Need to remove highlighting attributes here.
5397 */
5398 windgoto(row, col + coloff);
5399 out_str(T_CE); /* clear rest of this screen line */
5400 screen_start(); /* don't know where cursor is now */
5401 force = TRUE; /* force redraw of rest of the line */
5402 redraw_next = TRUE; /* or else next char would miss out */
5403
5404 /*
5405 * If the previous character was highlighted, need to stop
5406 * highlighting at this character.
5407 */
5408 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5409 {
5410 screen_attr = ScreenAttrs[off_to - 1];
5411 term_windgoto(row, col + coloff);
5412 screen_stop_highlight();
5413 }
5414 else
5415 screen_attr = 0; /* highlighting has stopped */
5416 }
5417#ifdef FEAT_MBYTE
5418 if (enc_dbcs != 0)
5419 {
5420 /* Check if overwriting a double-byte with a single-byte or
5421 * the other way around requires another character to be
5422 * redrawn. For UTF-8 this isn't needed, because comparing
5423 * ScreenLinesUC[] is sufficient. */
5424 if (char_cells == 1
5425 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005426 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
5428 /* Writing a single-cell character over a double-cell
5429 * character: need to redraw the next cell. */
5430 ScreenLines[off_to + 1] = 0;
5431 redraw_next = TRUE;
5432 }
5433 else if (char_cells == 2
5434 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005435 && (*mb_off2cells)(off_to, max_off_to) == 1
5436 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {
5438 /* Writing the second half of a double-cell character over
5439 * a double-cell character: need to redraw the second
5440 * cell. */
5441 ScreenLines[off_to + 2] = 0;
5442 redraw_next = TRUE;
5443 }
5444
5445 if (enc_dbcs == DBCS_JPNU)
5446 ScreenLines2[off_to] = ScreenLines2[off_from];
5447 }
5448 /* When writing a single-width character over a double-width
5449 * character and at the end of the redrawn text, need to clear out
5450 * the right halve of the old character.
5451 * Also required when writing the right halve of a double-width
5452 * char over the left halve of an existing one. */
5453 if (has_mbyte && col + char_cells == endcol
5454 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005455 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005457 && (*mb_off2cells)(off_to, max_off_to) == 1
5458 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 clear_next = TRUE;
5460#endif
5461
5462 ScreenLines[off_to] = ScreenLines[off_from];
5463#ifdef FEAT_MBYTE
5464 if (enc_utf8)
5465 {
5466 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5467 if (ScreenLinesUC[off_from] != 0)
5468 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005469 int i;
5470
5471 for (i = 0; i < Screen_mco; ++i)
5472 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474 }
5475 if (char_cells == 2)
5476 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5477#endif
5478
5479#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005480 /* The bold trick makes a single column of pixels appear in the
5481 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 * character should be redrawn too. This happens for our own GUI
5483 * and for some xterms. */
5484 if (
5485# ifdef FEAT_GUI
5486 gui.in_use
5487# endif
5488# if defined(FEAT_GUI) && defined(UNIX)
5489 ||
5490# endif
5491# ifdef UNIX
5492 term_is_xterm
5493# endif
5494 )
5495 {
5496 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005497 if (hl > HL_ALL)
5498 hl = syn_attr2attr(hl);
5499 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 redraw_next = TRUE;
5501 }
5502#endif
5503 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5504#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005505 /* For simplicity set the attributes of second half of a
5506 * double-wide character equal to the first half. */
5507 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005509
5510 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 else
5513#endif
5514 screen_char(off_to, row, col + coloff);
5515 }
5516 else if ( p_wiv
5517#ifdef FEAT_GUI
5518 && !gui.in_use
5519#endif
5520 && col + coloff > 0)
5521 {
5522 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5523 {
5524 /*
5525 * Don't output stop-highlight when moving the cursor, it will
5526 * stop the highlighting when it should continue.
5527 */
5528 screen_attr = 0;
5529 }
5530 else if (screen_attr != 0)
5531 screen_stop_highlight();
5532 }
5533
5534 off_to += CHAR_CELLS;
5535 off_from += CHAR_CELLS;
5536 col += CHAR_CELLS;
5537 }
5538
5539#ifdef FEAT_MBYTE
5540 if (clear_next)
5541 {
5542 /* Clear the second half of a double-wide character of which the left
5543 * half was overwritten with a single-wide character. */
5544 ScreenLines[off_to] = ' ';
5545 if (enc_utf8)
5546 ScreenLinesUC[off_to] = 0;
5547 screen_char(off_to, row, col + coloff);
5548 }
5549#endif
5550
5551 if (clear_width > 0
5552#ifdef FEAT_RIGHTLEFT
5553 && !rlflag
5554#endif
5555 )
5556 {
5557#ifdef FEAT_GUI
5558 int startCol = col;
5559#endif
5560
5561 /* blank out the rest of the line */
5562 while (col < clear_width && ScreenLines[off_to] == ' '
5563 && ScreenAttrs[off_to] == 0
5564#ifdef FEAT_MBYTE
5565 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5566#endif
5567 )
5568 {
5569 ++off_to;
5570 ++col;
5571 }
5572 if (col < clear_width)
5573 {
5574#ifdef FEAT_GUI
5575 /*
5576 * In the GUI, clearing the rest of the line may leave pixels
5577 * behind if the first character cleared was bold. Some bold
5578 * fonts spill over the left. In this case we redraw the previous
5579 * character too. If we didn't skip any blanks above, then we
5580 * only redraw if the character wasn't already redrawn anyway.
5581 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005582 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
5584 hl = ScreenAttrs[off_to];
5585 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005586 {
5587 int prev_cells = 1;
5588# ifdef FEAT_MBYTE
5589 if (enc_utf8)
5590 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5591 * that its width is 2. */
5592 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5593 else if (enc_dbcs != 0)
5594 {
5595 /* find previous character by counting from first
5596 * column and get its width. */
5597 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005598 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005599
5600 while (off < off_to)
5601 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005602 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005603 off += prev_cells;
5604 }
5605 }
5606
5607 if (enc_dbcs != 0 && prev_cells > 1)
5608 screen_char_2(off_to - prev_cells, row,
5609 col + coloff - prev_cells);
5610 else
5611# endif
5612 screen_char(off_to - prev_cells, row,
5613 col + coloff - prev_cells);
5614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
5616#endif
5617 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5618 ' ', ' ', 0);
5619#ifdef FEAT_VERTSPLIT
5620 off_to += clear_width - col;
5621 col = clear_width;
5622#endif
5623 }
5624 }
5625
5626 if (clear_width > 0)
5627 {
5628#ifdef FEAT_VERTSPLIT
5629 /* For a window that's left of another, draw the separator char. */
5630 if (col + coloff < Columns)
5631 {
5632 int c;
5633
5634 c = fillchar_vsep(&hl);
5635 if (ScreenLines[off_to] != c
5636# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005637 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5638 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639# endif
5640 || ScreenAttrs[off_to] != hl)
5641 {
5642 ScreenLines[off_to] = c;
5643 ScreenAttrs[off_to] = hl;
5644# ifdef FEAT_MBYTE
5645 if (enc_utf8)
5646 {
5647 if (c >= 0x80)
5648 {
5649 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005650 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652 else
5653 ScreenLinesUC[off_to] = 0;
5654 }
5655# endif
5656 screen_char(off_to, row, col + coloff);
5657 }
5658 }
5659 else
5660#endif
5661 LineWraps[row] = FALSE;
5662 }
5663}
5664
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005665#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005667 * Mirror text "str" for right-left displaying.
5668 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005670 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671rl_mirror(str)
5672 char_u *str;
5673{
5674 char_u *p1, *p2;
5675 int t;
5676
5677 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5678 {
5679 t = *p1;
5680 *p1 = *p2;
5681 *p2 = t;
5682 }
5683}
5684#endif
5685
5686#if defined(FEAT_WINDOWS) || defined(PROTO)
5687/*
5688 * mark all status lines for redraw; used after first :cd
5689 */
5690 void
5691status_redraw_all()
5692{
5693 win_T *wp;
5694
5695 for (wp = firstwin; wp; wp = wp->w_next)
5696 if (wp->w_status_height)
5697 {
5698 wp->w_redr_status = TRUE;
5699 redraw_later(VALID);
5700 }
5701}
5702
5703/*
5704 * mark all status lines of the current buffer for redraw
5705 */
5706 void
5707status_redraw_curbuf()
5708{
5709 win_T *wp;
5710
5711 for (wp = firstwin; wp; wp = wp->w_next)
5712 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5713 {
5714 wp->w_redr_status = TRUE;
5715 redraw_later(VALID);
5716 }
5717}
5718
5719/*
5720 * Redraw all status lines that need to be redrawn.
5721 */
5722 void
5723redraw_statuslines()
5724{
5725 win_T *wp;
5726
5727 for (wp = firstwin; wp; wp = wp->w_next)
5728 if (wp->w_redr_status)
5729 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005730 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005731 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732}
5733#endif
5734
5735#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5736/*
5737 * Redraw all status lines at the bottom of frame "frp".
5738 */
5739 void
5740win_redraw_last_status(frp)
5741 frame_T *frp;
5742{
5743 if (frp->fr_layout == FR_LEAF)
5744 frp->fr_win->w_redr_status = TRUE;
5745 else if (frp->fr_layout == FR_ROW)
5746 {
5747 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5748 win_redraw_last_status(frp);
5749 }
5750 else /* frp->fr_layout == FR_COL */
5751 {
5752 frp = frp->fr_child;
5753 while (frp->fr_next != NULL)
5754 frp = frp->fr_next;
5755 win_redraw_last_status(frp);
5756 }
5757}
5758#endif
5759
5760#ifdef FEAT_VERTSPLIT
5761/*
5762 * Draw the verticap separator right of window "wp" starting with line "row".
5763 */
5764 static void
5765draw_vsep_win(wp, row)
5766 win_T *wp;
5767 int row;
5768{
5769 int hl;
5770 int c;
5771
5772 if (wp->w_vsep_width)
5773 {
5774 /* draw the vertical separator right of this window */
5775 c = fillchar_vsep(&hl);
5776 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5777 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5778 c, ' ', hl);
5779 }
5780}
5781#endif
5782
5783#ifdef FEAT_WILDMENU
5784static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005785static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005788 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 */
5790 static int
5791status_match_len(xp, s)
5792 expand_T *xp;
5793 char_u *s;
5794{
5795 int len = 0;
5796
5797#ifdef FEAT_MENU
5798 int emenu = (xp->xp_context == EXPAND_MENUS
5799 || xp->xp_context == EXPAND_MENUNAMES);
5800
5801 /* Check for menu separators - replace with '|'. */
5802 if (emenu && menu_is_separator(s))
5803 return 1;
5804#endif
5805
5806 while (*s != NUL)
5807 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005808 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005809 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005810 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
5812
5813 return len;
5814}
5815
5816/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005817 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005818 * These are backslashes used for escaping. Do show backslashes in help tags.
5819 */
5820 static int
5821skip_status_match_char(xp, s)
5822 expand_T *xp;
5823 char_u *s;
5824{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005825 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005826#ifdef FEAT_MENU
5827 || ((xp->xp_context == EXPAND_MENUS
5828 || xp->xp_context == EXPAND_MENUNAMES)
5829 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5830#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005831 )
5832 {
5833#ifndef BACKSLASH_IN_FILENAME
5834 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5835 return 2;
5836#endif
5837 return 1;
5838 }
5839 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005840}
5841
5842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 * Show wildchar matches in the status line.
5844 * Show at least the "match" item.
5845 * We start at item 'first_match' in the list and show all matches that fit.
5846 *
5847 * If inversion is possible we use it. Else '=' characters are used.
5848 */
5849 void
5850win_redr_status_matches(xp, num_matches, matches, match, showtail)
5851 expand_T *xp;
5852 int num_matches;
5853 char_u **matches; /* list of matches */
5854 int match;
5855 int showtail;
5856{
5857#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5858 int row;
5859 char_u *buf;
5860 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005861 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 int fillchar;
5863 int attr;
5864 int i;
5865 int highlight = TRUE;
5866 char_u *selstart = NULL;
5867 int selstart_col = 0;
5868 char_u *selend = NULL;
5869 static int first_match = 0;
5870 int add_left = FALSE;
5871 char_u *s;
5872#ifdef FEAT_MENU
5873 int emenu;
5874#endif
5875#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5876 int l;
5877#endif
5878
5879 if (matches == NULL) /* interrupted completion? */
5880 return;
5881
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005882#ifdef FEAT_MBYTE
5883 if (has_mbyte)
5884 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5885 else
5886#endif
5887 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 if (buf == NULL)
5889 return;
5890
5891 if (match == -1) /* don't show match but original text */
5892 {
5893 match = 0;
5894 highlight = FALSE;
5895 }
5896 /* count 1 for the ending ">" */
5897 clen = status_match_len(xp, L_MATCH(match)) + 3;
5898 if (match == 0)
5899 first_match = 0;
5900 else if (match < first_match)
5901 {
5902 /* jumping left, as far as we can go */
5903 first_match = match;
5904 add_left = TRUE;
5905 }
5906 else
5907 {
5908 /* check if match fits on the screen */
5909 for (i = first_match; i < match; ++i)
5910 clen += status_match_len(xp, L_MATCH(i)) + 2;
5911 if (first_match > 0)
5912 clen += 2;
5913 /* jumping right, put match at the left */
5914 if ((long)clen > Columns)
5915 {
5916 first_match = match;
5917 /* if showing the last match, we can add some on the left */
5918 clen = 2;
5919 for (i = match; i < num_matches; ++i)
5920 {
5921 clen += status_match_len(xp, L_MATCH(i)) + 2;
5922 if ((long)clen >= Columns)
5923 break;
5924 }
5925 if (i == num_matches)
5926 add_left = TRUE;
5927 }
5928 }
5929 if (add_left)
5930 while (first_match > 0)
5931 {
5932 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5933 if ((long)clen >= Columns)
5934 break;
5935 --first_match;
5936 }
5937
5938 fillchar = fillchar_status(&attr, TRUE);
5939
5940 if (first_match == 0)
5941 {
5942 *buf = NUL;
5943 len = 0;
5944 }
5945 else
5946 {
5947 STRCPY(buf, "< ");
5948 len = 2;
5949 }
5950 clen = len;
5951
5952 i = first_match;
5953 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5954 {
5955 if (i == match)
5956 {
5957 selstart = buf + len;
5958 selstart_col = clen;
5959 }
5960
5961 s = L_MATCH(i);
5962 /* Check for menu separators - replace with '|' */
5963#ifdef FEAT_MENU
5964 emenu = (xp->xp_context == EXPAND_MENUS
5965 || xp->xp_context == EXPAND_MENUNAMES);
5966 if (emenu && menu_is_separator(s))
5967 {
5968 STRCPY(buf + len, transchar('|'));
5969 l = (int)STRLEN(buf + len);
5970 len += l;
5971 clen += l;
5972 }
5973 else
5974#endif
5975 for ( ; *s != NUL; ++s)
5976 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005977 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 clen += ptr2cells(s);
5979#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005980 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {
5982 STRNCPY(buf + len, s, l);
5983 s += l - 1;
5984 len += l;
5985 }
5986 else
5987#endif
5988 {
5989 STRCPY(buf + len, transchar_byte(*s));
5990 len += (int)STRLEN(buf + len);
5991 }
5992 }
5993 if (i == match)
5994 selend = buf + len;
5995
5996 *(buf + len++) = ' ';
5997 *(buf + len++) = ' ';
5998 clen += 2;
5999 if (++i == num_matches)
6000 break;
6001 }
6002
6003 if (i != num_matches)
6004 {
6005 *(buf + len++) = '>';
6006 ++clen;
6007 }
6008
6009 buf[len] = NUL;
6010
6011 row = cmdline_row - 1;
6012 if (row >= 0)
6013 {
6014 if (wild_menu_showing == 0)
6015 {
6016 if (msg_scrolled > 0)
6017 {
6018 /* Put the wildmenu just above the command line. If there is
6019 * no room, scroll the screen one line up. */
6020 if (cmdline_row == Rows - 1)
6021 {
6022 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6023 ++msg_scrolled;
6024 }
6025 else
6026 {
6027 ++cmdline_row;
6028 ++row;
6029 }
6030 wild_menu_showing = WM_SCROLLED;
6031 }
6032 else
6033 {
6034 /* Create status line if needed by setting 'laststatus' to 2.
6035 * Set 'winminheight' to zero to avoid that the window is
6036 * resized. */
6037 if (lastwin->w_status_height == 0)
6038 {
6039 save_p_ls = p_ls;
6040 save_p_wmh = p_wmh;
6041 p_ls = 2;
6042 p_wmh = 0;
6043 last_status(FALSE);
6044 }
6045 wild_menu_showing = WM_SHOWN;
6046 }
6047 }
6048
6049 screen_puts(buf, row, 0, attr);
6050 if (selstart != NULL && highlight)
6051 {
6052 *selend = NUL;
6053 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6054 }
6055
6056 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6057 }
6058
6059#ifdef FEAT_VERTSPLIT
6060 win_redraw_last_status(topframe);
6061#else
6062 lastwin->w_redr_status = TRUE;
6063#endif
6064 vim_free(buf);
6065}
6066#endif
6067
6068#if defined(FEAT_WINDOWS) || defined(PROTO)
6069/*
6070 * Redraw the status line of window wp.
6071 *
6072 * If inversion is possible we use it. Else '=' characters are used.
6073 */
6074 void
6075win_redr_status(wp)
6076 win_T *wp;
6077{
6078 int row;
6079 char_u *p;
6080 int len;
6081 int fillchar;
6082 int attr;
6083 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006084 static int busy = FALSE;
6085
6086 /* It's possible to get here recursively when 'statusline' (indirectly)
6087 * invokes ":redrawstatus". Simply ignore the call then. */
6088 if (busy)
6089 return;
6090 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092 wp->w_redr_status = FALSE;
6093 if (wp->w_status_height == 0)
6094 {
6095 /* no status line, can only be last window */
6096 redraw_cmdline = TRUE;
6097 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006098 else if (!redrawing()
6099#ifdef FEAT_INS_EXPAND
6100 /* don't update status line when popup menu is visible and may be
6101 * drawn over it */
6102 || pum_visible()
6103#endif
6104 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 {
6106 /* Don't redraw right now, do it later. */
6107 wp->w_redr_status = TRUE;
6108 }
6109#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006110 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 {
6112 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006113 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 }
6115#endif
6116 else
6117 {
6118 fillchar = fillchar_status(&attr, wp == curwin);
6119
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006120 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 p = NameBuff;
6122 len = (int)STRLEN(p);
6123
6124 if (wp->w_buffer->b_help
6125#ifdef FEAT_QUICKFIX
6126 || wp->w_p_pvw
6127#endif
6128 || bufIsChanged(wp->w_buffer)
6129 || wp->w_buffer->b_p_ro)
6130 *(p + len++) = ' ';
6131 if (wp->w_buffer->b_help)
6132 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006133 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 len += (int)STRLEN(p + len);
6135 }
6136#ifdef FEAT_QUICKFIX
6137 if (wp->w_p_pvw)
6138 {
6139 STRCPY(p + len, _("[Preview]"));
6140 len += (int)STRLEN(p + len);
6141 }
6142#endif
6143 if (bufIsChanged(wp->w_buffer))
6144 {
6145 STRCPY(p + len, "[+]");
6146 len += 3;
6147 }
6148 if (wp->w_buffer->b_p_ro)
6149 {
6150 STRCPY(p + len, "[RO]");
6151 len += 4;
6152 }
6153
6154#ifndef FEAT_VERTSPLIT
6155 this_ru_col = ru_col;
6156 if (this_ru_col < (Columns + 1) / 2)
6157 this_ru_col = (Columns + 1) / 2;
6158#else
6159 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6160 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6161 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6162 if (this_ru_col <= 1)
6163 {
6164 p = (char_u *)"<"; /* No room for file name! */
6165 len = 1;
6166 }
6167 else
6168#endif
6169#ifdef FEAT_MBYTE
6170 if (has_mbyte)
6171 {
6172 int clen = 0, i;
6173
6174 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006175 clen = mb_string2cells(p, -1);
6176
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 /* Find first character that will fit.
6178 * Going from start to end is much faster for DBCS. */
6179 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006180 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 clen -= (*mb_ptr2cells)(p + i);
6182 len = clen;
6183 if (i > 0)
6184 {
6185 p = p + i - 1;
6186 *p = '<';
6187 ++len;
6188 }
6189
6190 }
6191 else
6192#endif
6193 if (len > this_ru_col - 1)
6194 {
6195 p += len - (this_ru_col - 1);
6196 *p = '<';
6197 len = this_ru_col - 1;
6198 }
6199
6200 row = W_WINROW(wp) + wp->w_height;
6201 screen_puts(p, row, W_WINCOL(wp), attr);
6202 screen_fill(row, row + 1, len + W_WINCOL(wp),
6203 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6204
6205 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6206 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6207 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6208 - 1 + W_WINCOL(wp)), attr);
6209
6210#ifdef FEAT_CMDL_INFO
6211 win_redr_ruler(wp, TRUE);
6212#endif
6213 }
6214
6215#ifdef FEAT_VERTSPLIT
6216 /*
6217 * May need to draw the character below the vertical separator.
6218 */
6219 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6220 {
6221 if (stl_connected(wp))
6222 fillchar = fillchar_status(&attr, wp == curwin);
6223 else
6224 fillchar = fillchar_vsep(&attr);
6225 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6226 attr);
6227 }
6228#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006229 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230}
6231
Bram Moolenaar238a5642006-02-21 22:12:05 +00006232#ifdef FEAT_STL_OPT
6233/*
6234 * Redraw the status line according to 'statusline' and take care of any
6235 * errors encountered.
6236 */
6237 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006238redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006239 win_T *wp;
6240{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006241 static int entered = FALSE;
6242 int save_called_emsg = called_emsg;
6243
6244 /* When called recursively return. This can happen when the statusline
6245 * contains an expression that triggers a redraw. */
6246 if (entered)
6247 return;
6248 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006249
6250 called_emsg = FALSE;
6251 win_redr_custom(wp, FALSE);
6252 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006253 {
6254 /* When there is an error disable the statusline, otherwise the
6255 * display is messed up with errors and a redraw triggers the problem
6256 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006257 set_string_option_direct((char_u *)"statusline", -1,
6258 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006259 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006260 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006261 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006262 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006263}
6264#endif
6265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266# ifdef FEAT_VERTSPLIT
6267/*
6268 * Return TRUE if the status line of window "wp" is connected to the status
6269 * line of the window right of it. If not, then it's a vertical separator.
6270 * Only call if (wp->w_vsep_width != 0).
6271 */
6272 int
6273stl_connected(wp)
6274 win_T *wp;
6275{
6276 frame_T *fr;
6277
6278 fr = wp->w_frame;
6279 while (fr->fr_parent != NULL)
6280 {
6281 if (fr->fr_parent->fr_layout == FR_COL)
6282 {
6283 if (fr->fr_next != NULL)
6284 break;
6285 }
6286 else
6287 {
6288 if (fr->fr_next != NULL)
6289 return TRUE;
6290 }
6291 fr = fr->fr_parent;
6292 }
6293 return FALSE;
6294}
6295# endif
6296
6297#endif /* FEAT_WINDOWS */
6298
6299#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6300/*
6301 * Get the value to show for the language mappings, active 'keymap'.
6302 */
6303 int
6304get_keymap_str(wp, buf, len)
6305 win_T *wp;
6306 char_u *buf; /* buffer for the result */
6307 int len; /* length of buffer */
6308{
6309 char_u *p;
6310
6311 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6312 return FALSE;
6313
6314 {
6315#ifdef FEAT_EVAL
6316 buf_T *old_curbuf = curbuf;
6317 win_T *old_curwin = curwin;
6318 char_u *s;
6319
6320 curbuf = wp->w_buffer;
6321 curwin = wp;
6322 STRCPY(buf, "b:keymap_name"); /* must be writable */
6323 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006324 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 --emsg_skip;
6326 curbuf = old_curbuf;
6327 curwin = old_curwin;
6328 if (p == NULL || *p == NUL)
6329#endif
6330 {
6331#ifdef FEAT_KEYMAP
6332 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6333 p = wp->w_buffer->b_p_keymap;
6334 else
6335#endif
6336 p = (char_u *)"lang";
6337 }
6338 if ((int)(STRLEN(p) + 3) < len)
6339 sprintf((char *)buf, "<%s>", p);
6340 else
6341 buf[0] = NUL;
6342#ifdef FEAT_EVAL
6343 vim_free(s);
6344#endif
6345 }
6346 return buf[0] != NUL;
6347}
6348#endif
6349
6350#if defined(FEAT_STL_OPT) || defined(PROTO)
6351/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006352 * Redraw the status line or ruler of window "wp".
6353 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 */
6355 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006356win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006358 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359{
6360 int attr;
6361 int curattr;
6362 int row;
6363 int col = 0;
6364 int maxwidth;
6365 int width;
6366 int n;
6367 int len;
6368 int fillchar;
6369 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006370 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006372 struct stl_hlrec hltab[STL_MAX_ITEM];
6373 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006374 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006377 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006379 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006380 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006381 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006382 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006383 attr = hl_attr(HLF_TPF);
6384 maxwidth = Columns;
6385# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006386 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006389 else
6390 {
6391 row = W_WINROW(wp) + wp->w_height;
6392 fillchar = fillchar_status(&attr, wp == curwin);
6393 maxwidth = W_WIDTH(wp);
6394
6395 if (draw_ruler)
6396 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006397 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006398 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006399 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006400 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006401 if (*++stl == '-')
6402 stl++;
6403 if (atoi((char *)stl))
6404 while (VIM_ISDIGIT(*stl))
6405 stl++;
6406 if (*stl++ != '(')
6407 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006408 }
6409#ifdef FEAT_VERTSPLIT
6410 col = ru_col - (Columns - W_WIDTH(wp));
6411 if (col < (W_WIDTH(wp) + 1) / 2)
6412 col = (W_WIDTH(wp) + 1) / 2;
6413#else
6414 col = ru_col;
6415 if (col > (Columns + 1) / 2)
6416 col = (Columns + 1) / 2;
6417#endif
6418 maxwidth = W_WIDTH(wp) - col;
6419#ifdef FEAT_WINDOWS
6420 if (!wp->w_status_height)
6421#endif
6422 {
6423 row = Rows - 1;
6424 --maxwidth; /* writing in last column may cause scrolling */
6425 fillchar = ' ';
6426 attr = 0;
6427 }
6428
6429# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006430 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006431# endif
6432 }
6433 else
6434 {
6435 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006436 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006437 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006438 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006439# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006440 use_sandbox = was_set_insecurely((char_u *)"statusline",
6441 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006442# endif
6443 }
6444
6445#ifdef FEAT_VERTSPLIT
6446 col += W_WINCOL(wp);
6447#endif
6448 }
6449
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 if (maxwidth <= 0)
6451 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
Bram Moolenaar362f3562009-11-03 16:20:34 +00006453 /* Make a copy, because the statusline may include a function call that
6454 * might change the option value and free the memory. */
6455 stl = vim_strsave(stl);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006456 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6457 buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006458 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006459 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006460 vim_free(stl);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006461 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006463 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465#ifdef FEAT_MBYTE
6466 len += (*mb_char2bytes)(fillchar, buf + len);
6467#else
6468 buf[len++] = fillchar;
6469#endif
6470 ++width;
6471 }
6472 buf[len] = NUL;
6473
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006474 /*
6475 * Draw each snippet with the specified highlighting.
6476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 curattr = attr;
6478 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006479 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006481 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 screen_puts_len(p, len, row, col, curattr);
6483 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006484 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006486 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006488 else if (hltab[n].userhl < 0)
6489 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006491 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006492 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493#endif
6494 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006495 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 }
6497 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006498
6499 if (wp == NULL)
6500 {
6501 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6502 col = 0;
6503 len = 0;
6504 p = buf;
6505 fillchar = 0;
6506 for (n = 0; tabtab[n].start != NULL; n++)
6507 {
6508 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6509 while (col < len)
6510 TabPageIdxs[col++] = fillchar;
6511 p = tabtab[n].start;
6512 fillchar = tabtab[n].userhl;
6513 }
6514 while (col < Columns)
6515 TabPageIdxs[col++] = fillchar;
6516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517}
6518
6519#endif /* FEAT_STL_OPT */
6520
6521/*
6522 * Output a single character directly to the screen and update ScreenLines.
6523 */
6524 void
6525screen_putchar(c, row, col, attr)
6526 int c;
6527 int row, col;
6528 int attr;
6529{
6530#ifdef FEAT_MBYTE
6531 char_u buf[MB_MAXBYTES + 1];
6532
6533 buf[(*mb_char2bytes)(c, buf)] = NUL;
6534#else
6535 char_u buf[2];
6536
6537 buf[0] = c;
6538 buf[1] = NUL;
6539#endif
6540 screen_puts(buf, row, col, attr);
6541}
6542
6543/*
6544 * Get a single character directly from ScreenLines into "bytes[]".
6545 * Also return its attribute in *attrp;
6546 */
6547 void
6548screen_getbytes(row, col, bytes, attrp)
6549 int row, col;
6550 char_u *bytes;
6551 int *attrp;
6552{
6553 unsigned off;
6554
6555 /* safety check */
6556 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6557 {
6558 off = LineOffset[row] + col;
6559 *attrp = ScreenAttrs[off];
6560 bytes[0] = ScreenLines[off];
6561 bytes[1] = NUL;
6562
6563#ifdef FEAT_MBYTE
6564 if (enc_utf8 && ScreenLinesUC[off] != 0)
6565 bytes[utfc_char2bytes(off, bytes)] = NUL;
6566 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6567 {
6568 bytes[0] = ScreenLines[off];
6569 bytes[1] = ScreenLines2[off];
6570 bytes[2] = NUL;
6571 }
6572 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6573 {
6574 bytes[1] = ScreenLines[off + 1];
6575 bytes[2] = NUL;
6576 }
6577#endif
6578 }
6579}
6580
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006581#ifdef FEAT_MBYTE
6582static int screen_comp_differs __ARGS((int, int*));
6583
6584/*
6585 * Return TRUE if composing characters for screen posn "off" differs from
6586 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006587 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006588 */
6589 static int
6590screen_comp_differs(off, u8cc)
6591 int off;
6592 int *u8cc;
6593{
6594 int i;
6595
6596 for (i = 0; i < Screen_mco; ++i)
6597 {
6598 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6599 return TRUE;
6600 if (u8cc[i] == 0)
6601 break;
6602 }
6603 return FALSE;
6604}
6605#endif
6606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607/*
6608 * Put string '*text' on the screen at position 'row' and 'col', with
6609 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6610 * Note: only outputs within one row, message is truncated at screen boundary!
6611 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6612 */
6613 void
6614screen_puts(text, row, col, attr)
6615 char_u *text;
6616 int row;
6617 int col;
6618 int attr;
6619{
6620 screen_puts_len(text, -1, row, col, attr);
6621}
6622
6623/*
6624 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6625 * a NUL.
6626 */
6627 void
6628screen_puts_len(text, len, row, col, attr)
6629 char_u *text;
6630 int len;
6631 int row;
6632 int col;
6633 int attr;
6634{
6635 unsigned off;
6636 char_u *ptr = text;
6637 int c;
6638#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006639 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 int mbyte_blen = 1;
6641 int mbyte_cells = 1;
6642 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006643 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 int clear_next_cell = FALSE;
6645# ifdef FEAT_ARABIC
6646 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006647 int pc, nc, nc1;
6648 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649# endif
6650#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006651#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6652 int force_redraw_this;
6653 int force_redraw_next = FALSE;
6654#endif
6655 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
6657 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6658 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006659 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660
Bram Moolenaarc236c162008-07-13 17:41:49 +00006661#ifdef FEAT_MBYTE
6662 /* When drawing over the right halve of a double-wide char clear out the
6663 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006664 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006665# ifdef FEAT_GUI
6666 && !gui.in_use
6667# endif
6668 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006669 {
6670 ScreenLines[off - 1] = ' ';
6671 ScreenAttrs[off - 1] = 0;
6672 if (enc_utf8)
6673 {
6674 ScreenLinesUC[off - 1] = 0;
6675 ScreenLinesC[0][off - 1] = 0;
6676 }
6677 /* redraw the previous cell, make it empty */
6678 screen_char(off - 1, row, col - 1);
6679 /* force the cell at "col" to be redrawn */
6680 force_redraw_next = TRUE;
6681 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006682#endif
6683
Bram Moolenaar367329b2007-08-30 11:53:22 +00006684#ifdef FEAT_MBYTE
6685 max_off = LineOffset[row] + screen_Columns;
6686#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006687 while (col < screen_Columns
6688 && (len < 0 || (int)(ptr - text) < len)
6689 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 {
6691 c = *ptr;
6692#ifdef FEAT_MBYTE
6693 /* check if this is the first byte of a multibyte */
6694 if (has_mbyte)
6695 {
6696 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006697 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006699 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6701 mbyte_cells = 1;
6702 else if (enc_dbcs != 0)
6703 mbyte_cells = mbyte_blen;
6704 else /* enc_utf8 */
6705 {
6706 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006707 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 (int)((text + len) - ptr));
6709 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006710 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006712# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 /* Non-BMP character: display as ? or fullwidth ?. */
6714 if (u8c >= 0x10000)
6715 {
6716 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6717 if (attr == 0)
6718 attr = hl_attr(HLF_8);
6719 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721# ifdef FEAT_ARABIC
6722 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6723 {
6724 /* Do Arabic shaping. */
6725 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6726 {
6727 /* Past end of string to be displayed. */
6728 nc = NUL;
6729 nc1 = NUL;
6730 }
6731 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006732 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006733 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6734 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006735 nc1 = pcc[0];
6736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 pc = prev_c;
6738 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006739 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
6741 else
6742 prev_c = u8c;
6743# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006744 if (col + mbyte_cells > screen_Columns)
6745 {
6746 /* Only 1 cell left, but character requires 2 cells:
6747 * display a '>' in the last column to avoid wrapping. */
6748 c = '>';
6749 mbyte_cells = 1;
6750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 }
6752 }
6753#endif
6754
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006755#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6756 force_redraw_this = force_redraw_next;
6757 force_redraw_next = FALSE;
6758#endif
6759
6760 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761#ifdef FEAT_MBYTE
6762 || (mbyte_cells == 2
6763 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6764 || (enc_dbcs == DBCS_JPNU
6765 && c == 0x8e
6766 && ScreenLines2[off] != ptr[1])
6767 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006768 && (ScreenLinesUC[off] !=
6769 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6770 || (ScreenLinesUC[off] != 0
6771 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772#endif
6773 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006774 || exmode_active;
6775
6776 if (need_redraw
6777#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6778 || force_redraw_this
6779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 )
6781 {
6782#if defined(FEAT_GUI) || defined(UNIX)
6783 /* The bold trick makes a single row of pixels appear in the next
6784 * character. When a bold character is removed, the next
6785 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006786 * and for some xterms. */
6787 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788# ifdef FEAT_GUI
6789 gui.in_use
6790# endif
6791# if defined(FEAT_GUI) && defined(UNIX)
6792 ||
6793# endif
6794# ifdef UNIX
6795 term_is_xterm
6796# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006797 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006799 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006801 if (n > HL_ALL)
6802 n = syn_attr2attr(n);
6803 if (n & HL_BOLD)
6804 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 }
6806#endif
6807#ifdef FEAT_MBYTE
6808 /* When at the end of the text and overwriting a two-cell
6809 * character with a one-cell character, need to clear the next
6810 * cell. Also when overwriting the left halve of a two-cell char
6811 * with the right halve of a two-cell char. Do this only once
6812 * (mb_off2cells() may return 2 on the right halve). */
6813 if (clear_next_cell)
6814 clear_next_cell = FALSE;
6815 else if (has_mbyte
6816 && (len < 0 ? ptr[mbyte_blen] == NUL
6817 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006818 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006820 && (*mb_off2cells)(off, max_off) == 1
6821 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 clear_next_cell = TRUE;
6823
6824 /* Make sure we never leave a second byte of a double-byte behind,
6825 * it confuses mb_off2cells(). */
6826 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006827 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006829 && (*mb_off2cells)(off, max_off) == 1
6830 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 ScreenLines[off + mbyte_blen] = 0;
6832#endif
6833 ScreenLines[off] = c;
6834 ScreenAttrs[off] = attr;
6835#ifdef FEAT_MBYTE
6836 if (enc_utf8)
6837 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006838 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 ScreenLinesUC[off] = 0;
6840 else
6841 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006842 int i;
6843
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006845 for (i = 0; i < Screen_mco; ++i)
6846 {
6847 ScreenLinesC[i][off] = u8cc[i];
6848 if (u8cc[i] == 0)
6849 break;
6850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 }
6852 if (mbyte_cells == 2)
6853 {
6854 ScreenLines[off + 1] = 0;
6855 ScreenAttrs[off + 1] = attr;
6856 }
6857 screen_char(off, row, col);
6858 }
6859 else if (mbyte_cells == 2)
6860 {
6861 ScreenLines[off + 1] = ptr[1];
6862 ScreenAttrs[off + 1] = attr;
6863 screen_char_2(off, row, col);
6864 }
6865 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6866 {
6867 ScreenLines2[off] = ptr[1];
6868 screen_char(off, row, col);
6869 }
6870 else
6871#endif
6872 screen_char(off, row, col);
6873 }
6874#ifdef FEAT_MBYTE
6875 if (has_mbyte)
6876 {
6877 off += mbyte_cells;
6878 col += mbyte_cells;
6879 ptr += mbyte_blen;
6880 if (clear_next_cell)
6881 ptr = (char_u *)" ";
6882 }
6883 else
6884#endif
6885 {
6886 ++off;
6887 ++col;
6888 ++ptr;
6889 }
6890 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006891
6892#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6893 /* If we detected the next character needs to be redrawn, but the text
6894 * doesn't extend up to there, update the character here. */
6895 if (force_redraw_next && col < screen_Columns)
6896 {
6897# ifdef FEAT_MBYTE
6898 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6899 screen_char_2(off, row, col);
6900 else
6901# endif
6902 screen_char(off, row, col);
6903 }
6904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905}
6906
6907#ifdef FEAT_SEARCH_EXTRA
6908/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006909 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 */
6911 static void
6912start_search_hl()
6913{
6914 if (p_hls && !no_hlsearch)
6915 {
6916 last_pat_prog(&search_hl.rm);
6917 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006918# ifdef FEAT_RELTIME
6919 /* Set the time limit to 'redrawtime'. */
6920 profile_setlimit(p_rdt, &search_hl.tm);
6921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 }
6923}
6924
6925/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006926 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 */
6928 static void
6929end_search_hl()
6930{
6931 if (search_hl.rm.regprog != NULL)
6932 {
6933 vim_free(search_hl.rm.regprog);
6934 search_hl.rm.regprog = NULL;
6935 }
6936}
6937
6938/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02006939 * Init for calling prepare_search_hl().
6940 */
6941 static void
6942init_search_hl(wp)
6943 win_T *wp;
6944{
6945 matchitem_T *cur;
6946
6947 /* Setup for match and 'hlsearch' highlighting. Disable any previous
6948 * match */
6949 cur = wp->w_match_head;
6950 while (cur != NULL)
6951 {
6952 cur->hl.rm = cur->match;
6953 if (cur->hlg_id == 0)
6954 cur->hl.attr = 0;
6955 else
6956 cur->hl.attr = syn_id2attr(cur->hlg_id);
6957 cur->hl.buf = wp->w_buffer;
6958 cur->hl.lnum = 0;
6959 cur->hl.first_lnum = 0;
6960# ifdef FEAT_RELTIME
6961 /* Set the time limit to 'redrawtime'. */
6962 profile_setlimit(p_rdt, &(cur->hl.tm));
6963# endif
6964 cur = cur->next;
6965 }
6966 search_hl.buf = wp->w_buffer;
6967 search_hl.lnum = 0;
6968 search_hl.first_lnum = 0;
6969 /* time limit is set at the toplevel, for all windows */
6970}
6971
6972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 * Advance to the match in window "wp" line "lnum" or past it.
6974 */
6975 static void
6976prepare_search_hl(wp, lnum)
6977 win_T *wp;
6978 linenr_T lnum;
6979{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006980 matchitem_T *cur; /* points to the match list */
6981 match_T *shl; /* points to search_hl or a match */
6982 int shl_flag; /* flag to indicate whether search_hl
6983 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 int n;
6985
6986 /*
6987 * When using a multi-line pattern, start searching at the top
6988 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006989 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006991 cur = wp->w_match_head;
6992 shl_flag = FALSE;
6993 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006995 if (shl_flag == FALSE)
6996 {
6997 shl = &search_hl;
6998 shl_flag = TRUE;
6999 }
7000 else
7001 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (shl->rm.regprog != NULL
7003 && shl->lnum == 0
7004 && re_multiline(shl->rm.regprog))
7005 {
7006 if (shl->first_lnum == 0)
7007 {
7008# ifdef FEAT_FOLDING
7009 for (shl->first_lnum = lnum;
7010 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7011 if (hasFoldingWin(wp, shl->first_lnum - 1,
7012 NULL, NULL, TRUE, NULL))
7013 break;
7014# else
7015 shl->first_lnum = wp->w_topline;
7016# endif
7017 }
7018 n = 0;
7019 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7020 {
7021 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7022 if (shl->lnum != 0)
7023 {
7024 shl->first_lnum = shl->lnum
7025 + shl->rm.endpos[0].lnum
7026 - shl->rm.startpos[0].lnum;
7027 n = shl->rm.endpos[0].col;
7028 }
7029 else
7030 {
7031 ++shl->first_lnum;
7032 n = 0;
7033 }
7034 }
7035 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007036 if (shl != &search_hl && cur != NULL)
7037 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039}
7040
7041/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007042 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 * Uses shl->buf.
7044 * Sets shl->lnum and shl->rm contents.
7045 * Note: Assumes a previous match is always before "lnum", unless
7046 * shl->lnum is zero.
7047 * Careful: Any pointers for buffer lines will become invalid.
7048 */
7049 static void
7050next_search_hl(win, shl, lnum, mincol)
7051 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007052 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 linenr_T lnum;
7054 colnr_T mincol; /* minimal column for a match */
7055{
7056 linenr_T l;
7057 colnr_T matchcol;
7058 long nmatched;
7059
7060 if (shl->lnum != 0)
7061 {
7062 /* Check for three situations:
7063 * 1. If the "lnum" is below a previous match, start a new search.
7064 * 2. If the previous match includes "mincol", use it.
7065 * 3. Continue after the previous match.
7066 */
7067 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7068 if (lnum > l)
7069 shl->lnum = 0;
7070 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7071 return;
7072 }
7073
7074 /*
7075 * Repeat searching for a match until one is found that includes "mincol"
7076 * or none is found in this line.
7077 */
7078 called_emsg = FALSE;
7079 for (;;)
7080 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007081#ifdef FEAT_RELTIME
7082 /* Stop searching after passing the time limit. */
7083 if (profile_passed_limit(&(shl->tm)))
7084 {
7085 shl->lnum = 0; /* no match found in time */
7086 break;
7087 }
7088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 /* Three situations:
7090 * 1. No useful previous match: search from start of line.
7091 * 2. Not Vi compatible or empty match: continue at next character.
7092 * Break the loop if this is beyond the end of the line.
7093 * 3. Vi compatible searching: continue at end of previous match.
7094 */
7095 if (shl->lnum == 0)
7096 matchcol = 0;
7097 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7098 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007099 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007101 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007102
7103 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007104 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007105 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007107 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 shl->lnum = 0;
7109 break;
7110 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007111#ifdef FEAT_MBYTE
7112 if (has_mbyte)
7113 matchcol += mb_ptr2len(ml);
7114 else
7115#endif
7116 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 }
7118 else
7119 matchcol = shl->rm.endpos[0].col;
7120
7121 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007122 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7123#ifdef FEAT_RELTIME
7124 &(shl->tm)
7125#else
7126 NULL
7127#endif
7128 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007129 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {
7131 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007132 if (shl == &search_hl)
7133 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007134 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007135 vim_free(shl->rm.regprog);
7136 no_hlsearch = TRUE;
7137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007139 shl->lnum = 0;
7140 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 break;
7142 }
7143 if (nmatched == 0)
7144 {
7145 shl->lnum = 0; /* no match found */
7146 break;
7147 }
7148 if (shl->rm.startpos[0].lnum > 0
7149 || shl->rm.startpos[0].col >= mincol
7150 || nmatched > 1
7151 || shl->rm.endpos[0].col > mincol)
7152 {
7153 shl->lnum += shl->rm.startpos[0].lnum;
7154 break; /* useful match found */
7155 }
7156 }
7157}
7158#endif
7159
7160 static void
7161screen_start_highlight(attr)
7162 int attr;
7163{
7164 attrentry_T *aep = NULL;
7165
7166 screen_attr = attr;
7167 if (full_screen
7168#ifdef WIN3264
7169 && termcap_active
7170#endif
7171 )
7172 {
7173#ifdef FEAT_GUI
7174 if (gui.in_use)
7175 {
7176 char buf[20];
7177
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007178 /* The GUI handles this internally. */
7179 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 OUT_STR(buf);
7181 }
7182 else
7183#endif
7184 {
7185 if (attr > HL_ALL) /* special HL attr. */
7186 {
7187 if (t_colors > 1)
7188 aep = syn_cterm_attr2entry(attr);
7189 else
7190 aep = syn_term_attr2entry(attr);
7191 if (aep == NULL) /* did ":syntax clear" */
7192 attr = 0;
7193 else
7194 attr = aep->ae_attr;
7195 }
7196 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7197 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007198 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7199 && cterm_normal_fg_bold)
7200 /* If the Normal FG color has BOLD attribute and the new HL
7201 * has a FG color defined, clear BOLD. */
7202 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7204 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007205 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7206 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 out_str(T_US);
7208 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7209 out_str(T_CZH);
7210 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7211 out_str(T_MR);
7212
7213 /*
7214 * Output the color or start string after bold etc., in case the
7215 * bold etc. override the color setting.
7216 */
7217 if (aep != NULL)
7218 {
7219 if (t_colors > 1)
7220 {
7221 if (aep->ae_u.cterm.fg_color)
7222 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7223 if (aep->ae_u.cterm.bg_color)
7224 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7225 }
7226 else
7227 {
7228 if (aep->ae_u.term.start != NULL)
7229 out_str(aep->ae_u.term.start);
7230 }
7231 }
7232 }
7233 }
7234}
7235
7236 void
7237screen_stop_highlight()
7238{
7239 int do_ME = FALSE; /* output T_ME code */
7240
7241 if (screen_attr != 0
7242#ifdef WIN3264
7243 && termcap_active
7244#endif
7245 )
7246 {
7247#ifdef FEAT_GUI
7248 if (gui.in_use)
7249 {
7250 char buf[20];
7251
7252 /* use internal GUI code */
7253 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7254 OUT_STR(buf);
7255 }
7256 else
7257#endif
7258 {
7259 if (screen_attr > HL_ALL) /* special HL attr. */
7260 {
7261 attrentry_T *aep;
7262
7263 if (t_colors > 1)
7264 {
7265 /*
7266 * Assume that t_me restores the original colors!
7267 */
7268 aep = syn_cterm_attr2entry(screen_attr);
7269 if (aep != NULL && (aep->ae_u.cterm.fg_color
7270 || aep->ae_u.cterm.bg_color))
7271 do_ME = TRUE;
7272 }
7273 else
7274 {
7275 aep = syn_term_attr2entry(screen_attr);
7276 if (aep != NULL && aep->ae_u.term.stop != NULL)
7277 {
7278 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7279 do_ME = TRUE;
7280 else
7281 out_str(aep->ae_u.term.stop);
7282 }
7283 }
7284 if (aep == NULL) /* did ":syntax clear" */
7285 screen_attr = 0;
7286 else
7287 screen_attr = aep->ae_attr;
7288 }
7289
7290 /*
7291 * Often all ending-codes are equal to T_ME. Avoid outputting the
7292 * same sequence several times.
7293 */
7294 if (screen_attr & HL_STANDOUT)
7295 {
7296 if (STRCMP(T_SE, T_ME) == 0)
7297 do_ME = TRUE;
7298 else
7299 out_str(T_SE);
7300 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007301 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
7303 if (STRCMP(T_UE, T_ME) == 0)
7304 do_ME = TRUE;
7305 else
7306 out_str(T_UE);
7307 }
7308 if (screen_attr & HL_ITALIC)
7309 {
7310 if (STRCMP(T_CZR, T_ME) == 0)
7311 do_ME = TRUE;
7312 else
7313 out_str(T_CZR);
7314 }
7315 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7316 out_str(T_ME);
7317
7318 if (t_colors > 1)
7319 {
7320 /* set Normal cterm colors */
7321 if (cterm_normal_fg_color != 0)
7322 term_fg_color(cterm_normal_fg_color - 1);
7323 if (cterm_normal_bg_color != 0)
7324 term_bg_color(cterm_normal_bg_color - 1);
7325 if (cterm_normal_fg_bold)
7326 out_str(T_MD);
7327 }
7328 }
7329 }
7330 screen_attr = 0;
7331}
7332
7333/*
7334 * Reset the colors for a cterm. Used when leaving Vim.
7335 * The machine specific code may override this again.
7336 */
7337 void
7338reset_cterm_colors()
7339{
7340 if (t_colors > 1)
7341 {
7342 /* set Normal cterm colors */
7343 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7344 {
7345 out_str(T_OP);
7346 screen_attr = -1;
7347 }
7348 if (cterm_normal_fg_bold)
7349 {
7350 out_str(T_ME);
7351 screen_attr = -1;
7352 }
7353 }
7354}
7355
7356/*
7357 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7358 * using the attributes from ScreenAttrs["off"].
7359 */
7360 static void
7361screen_char(off, row, col)
7362 unsigned off;
7363 int row;
7364 int col;
7365{
7366 int attr;
7367
7368 /* Check for illegal values, just in case (could happen just after
7369 * resizing). */
7370 if (row >= screen_Rows || col >= screen_Columns)
7371 return;
7372
7373 /* Outputting the last character on the screen may scrollup the screen.
7374 * Don't to it! Mark the character invalid (update it when scrolled up) */
7375 if (row == screen_Rows - 1 && col == screen_Columns - 1
7376#ifdef FEAT_RIGHTLEFT
7377 /* account for first command-line character in rightleft mode */
7378 && !cmdmsg_rl
7379#endif
7380 )
7381 {
7382 ScreenAttrs[off] = (sattr_T)-1;
7383 return;
7384 }
7385
7386 /*
7387 * Stop highlighting first, so it's easier to move the cursor.
7388 */
7389#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7390 if (screen_char_attr != 0)
7391 attr = screen_char_attr;
7392 else
7393#endif
7394 attr = ScreenAttrs[off];
7395 if (screen_attr != attr)
7396 screen_stop_highlight();
7397
7398 windgoto(row, col);
7399
7400 if (screen_attr != attr)
7401 screen_start_highlight(attr);
7402
7403#ifdef FEAT_MBYTE
7404 if (enc_utf8 && ScreenLinesUC[off] != 0)
7405 {
7406 char_u buf[MB_MAXBYTES + 1];
7407
7408 /* Convert UTF-8 character to bytes and write it. */
7409
7410 buf[utfc_char2bytes(off, buf)] = NUL;
7411
7412 out_str(buf);
7413 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7414 ++screen_cur_col;
7415 }
7416 else
7417#endif
7418 {
7419#ifdef FEAT_MBYTE
7420 out_flush_check();
7421#endif
7422 out_char(ScreenLines[off]);
7423#ifdef FEAT_MBYTE
7424 /* double-byte character in single-width cell */
7425 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7426 out_char(ScreenLines2[off]);
7427#endif
7428 }
7429
7430 screen_cur_col++;
7431}
7432
7433#ifdef FEAT_MBYTE
7434
7435/*
7436 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7437 * on the screen at position 'row' and 'col'.
7438 * The attributes of the first byte is used for all. This is required to
7439 * output the two bytes of a double-byte character with nothing in between.
7440 */
7441 static void
7442screen_char_2(off, row, col)
7443 unsigned off;
7444 int row;
7445 int col;
7446{
7447 /* Check for illegal values (could be wrong when screen was resized). */
7448 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7449 return;
7450
7451 /* Outputting the last character on the screen may scrollup the screen.
7452 * Don't to it! Mark the character invalid (update it when scrolled up) */
7453 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7454 {
7455 ScreenAttrs[off] = (sattr_T)-1;
7456 return;
7457 }
7458
7459 /* Output the first byte normally (positions the cursor), then write the
7460 * second byte directly. */
7461 screen_char(off, row, col);
7462 out_char(ScreenLines[off + 1]);
7463 ++screen_cur_col;
7464}
7465#endif
7466
7467#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7468/*
7469 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7470 * This uses the contents of ScreenLines[] and doesn't change it.
7471 */
7472 void
7473screen_draw_rectangle(row, col, height, width, invert)
7474 int row;
7475 int col;
7476 int height;
7477 int width;
7478 int invert;
7479{
7480 int r, c;
7481 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007482#ifdef FEAT_MBYTE
7483 int max_off;
7484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007486 /* Can't use ScreenLines unless initialized */
7487 if (ScreenLines == NULL)
7488 return;
7489
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 if (invert)
7491 screen_char_attr = HL_INVERSE;
7492 for (r = row; r < row + height; ++r)
7493 {
7494 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007495#ifdef FEAT_MBYTE
7496 max_off = off + screen_Columns;
7497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 for (c = col; c < col + width; ++c)
7499 {
7500#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007501 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
7503 screen_char_2(off + c, r, c);
7504 ++c;
7505 }
7506 else
7507#endif
7508 {
7509 screen_char(off + c, r, c);
7510#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007511 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 ++c;
7513#endif
7514 }
7515 }
7516 }
7517 screen_char_attr = 0;
7518}
7519#endif
7520
7521#ifdef FEAT_VERTSPLIT
7522/*
7523 * Redraw the characters for a vertically split window.
7524 */
7525 static void
7526redraw_block(row, end, wp)
7527 int row;
7528 int end;
7529 win_T *wp;
7530{
7531 int col;
7532 int width;
7533
7534# ifdef FEAT_CLIPBOARD
7535 clip_may_clear_selection(row, end - 1);
7536# endif
7537
7538 if (wp == NULL)
7539 {
7540 col = 0;
7541 width = Columns;
7542 }
7543 else
7544 {
7545 col = wp->w_wincol;
7546 width = wp->w_width;
7547 }
7548 screen_draw_rectangle(row, col, end - row, width, FALSE);
7549}
7550#endif
7551
7552/*
7553 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7554 * with character 'c1' in first column followed by 'c2' in the other columns.
7555 * Use attributes 'attr'.
7556 */
7557 void
7558screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7559 int start_row, end_row;
7560 int start_col, end_col;
7561 int c1, c2;
7562 int attr;
7563{
7564 int row;
7565 int col;
7566 int off;
7567 int end_off;
7568 int did_delete;
7569 int c;
7570 int norm_term;
7571#if defined(FEAT_GUI) || defined(UNIX)
7572 int force_next = FALSE;
7573#endif
7574
7575 if (end_row > screen_Rows) /* safety check */
7576 end_row = screen_Rows;
7577 if (end_col > screen_Columns) /* safety check */
7578 end_col = screen_Columns;
7579 if (ScreenLines == NULL
7580 || start_row >= end_row
7581 || start_col >= end_col) /* nothing to do */
7582 return;
7583
7584 /* it's a "normal" terminal when not in a GUI or cterm */
7585 norm_term = (
7586#ifdef FEAT_GUI
7587 !gui.in_use &&
7588#endif
7589 t_colors <= 1);
7590 for (row = start_row; row < end_row; ++row)
7591 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007592#ifdef FEAT_MBYTE
7593 if (has_mbyte
7594# ifdef FEAT_GUI
7595 && !gui.in_use
7596# endif
7597 )
7598 {
7599 /* When drawing over the right halve of a double-wide char clear
7600 * out the left halve. When drawing over the left halve of a
7601 * double wide-char clear out the right halve. Only needed in a
7602 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007603 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007604 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007605 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007606 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007607 }
7608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 /*
7610 * Try to use delete-line termcap code, when no attributes or in a
7611 * "normal" terminal, where a bold/italic space is just a
7612 * space.
7613 */
7614 did_delete = FALSE;
7615 if (c2 == ' '
7616 && end_col == Columns
7617 && can_clear(T_CE)
7618 && (attr == 0
7619 || (norm_term
7620 && attr <= HL_ALL
7621 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7622 {
7623 /*
7624 * check if we really need to clear something
7625 */
7626 col = start_col;
7627 if (c1 != ' ') /* don't clear first char */
7628 ++col;
7629
7630 off = LineOffset[row] + col;
7631 end_off = LineOffset[row] + end_col;
7632
7633 /* skip blanks (used often, keep it fast!) */
7634#ifdef FEAT_MBYTE
7635 if (enc_utf8)
7636 while (off < end_off && ScreenLines[off] == ' '
7637 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7638 ++off;
7639 else
7640#endif
7641 while (off < end_off && ScreenLines[off] == ' '
7642 && ScreenAttrs[off] == 0)
7643 ++off;
7644 if (off < end_off) /* something to be cleared */
7645 {
7646 col = off - LineOffset[row];
7647 screen_stop_highlight();
7648 term_windgoto(row, col);/* clear rest of this screen line */
7649 out_str(T_CE);
7650 screen_start(); /* don't know where cursor is now */
7651 col = end_col - col;
7652 while (col--) /* clear chars in ScreenLines */
7653 {
7654 ScreenLines[off] = ' ';
7655#ifdef FEAT_MBYTE
7656 if (enc_utf8)
7657 ScreenLinesUC[off] = 0;
7658#endif
7659 ScreenAttrs[off] = 0;
7660 ++off;
7661 }
7662 }
7663 did_delete = TRUE; /* the chars are cleared now */
7664 }
7665
7666 off = LineOffset[row] + start_col;
7667 c = c1;
7668 for (col = start_col; col < end_col; ++col)
7669 {
7670 if (ScreenLines[off] != c
7671#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007672 || (enc_utf8 && (int)ScreenLinesUC[off]
7673 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674#endif
7675 || ScreenAttrs[off] != attr
7676#if defined(FEAT_GUI) || defined(UNIX)
7677 || force_next
7678#endif
7679 )
7680 {
7681#if defined(FEAT_GUI) || defined(UNIX)
7682 /* The bold trick may make a single row of pixels appear in
7683 * the next character. When a bold character is removed, the
7684 * next character should be redrawn too. This happens for our
7685 * own GUI and for some xterms. */
7686 if (
7687# ifdef FEAT_GUI
7688 gui.in_use
7689# endif
7690# if defined(FEAT_GUI) && defined(UNIX)
7691 ||
7692# endif
7693# ifdef UNIX
7694 term_is_xterm
7695# endif
7696 )
7697 {
7698 if (ScreenLines[off] != ' '
7699 && (ScreenAttrs[off] > HL_ALL
7700 || ScreenAttrs[off] & HL_BOLD))
7701 force_next = TRUE;
7702 else
7703 force_next = FALSE;
7704 }
7705#endif
7706 ScreenLines[off] = c;
7707#ifdef FEAT_MBYTE
7708 if (enc_utf8)
7709 {
7710 if (c >= 0x80)
7711 {
7712 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007713 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715 else
7716 ScreenLinesUC[off] = 0;
7717 }
7718#endif
7719 ScreenAttrs[off] = attr;
7720 if (!did_delete || c != ' ')
7721 screen_char(off, row, col);
7722 }
7723 ++off;
7724 if (col == start_col)
7725 {
7726 if (did_delete)
7727 break;
7728 c = c2;
7729 }
7730 }
7731 if (end_col == Columns)
7732 LineWraps[row] = FALSE;
7733 if (row == Rows - 1) /* overwritten the command line */
7734 {
7735 redraw_cmdline = TRUE;
7736 if (c1 == ' ' && c2 == ' ')
7737 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007738 if (start_col == 0)
7739 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 }
7741 }
7742}
7743
7744/*
7745 * Check if there should be a delay. Used before clearing or redrawing the
7746 * screen or the command line.
7747 */
7748 void
7749check_for_delay(check_msg_scroll)
7750 int check_msg_scroll;
7751{
7752 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7753 && !did_wait_return
7754 && emsg_silent == 0)
7755 {
7756 out_flush();
7757 ui_delay(1000L, TRUE);
7758 emsg_on_display = FALSE;
7759 if (check_msg_scroll)
7760 msg_scroll = FALSE;
7761 }
7762}
7763
7764/*
7765 * screen_valid - allocate screen buffers if size changed
7766 * If "clear" is TRUE: clear screen if it has been resized.
7767 * Returns TRUE if there is a valid screen to write to.
7768 * Returns FALSE when starting up and screen not initialized yet.
7769 */
7770 int
7771screen_valid(clear)
7772 int clear;
7773{
7774 screenalloc(clear); /* allocate screen buffers if size changed */
7775 return (ScreenLines != NULL);
7776}
7777
7778/*
7779 * Resize the shell to Rows and Columns.
7780 * Allocate ScreenLines[] and associated items.
7781 *
7782 * There may be some time between setting Rows and Columns and (re)allocating
7783 * ScreenLines[]. This happens when starting up and when (manually) changing
7784 * the shell size. Always use screen_Rows and screen_Columns to access items
7785 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7786 * final size of the shell is needed.
7787 */
7788 void
7789screenalloc(clear)
7790 int clear;
7791{
7792 int new_row, old_row;
7793#ifdef FEAT_GUI
7794 int old_Rows;
7795#endif
7796 win_T *wp;
7797 int outofmem = FALSE;
7798 int len;
7799 schar_T *new_ScreenLines;
7800#ifdef FEAT_MBYTE
7801 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007802 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007804 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805#endif
7806 sattr_T *new_ScreenAttrs;
7807 unsigned *new_LineOffset;
7808 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007809#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007810 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007811 tabpage_T *tp;
7812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007814 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007815#ifdef FEAT_AUTOCMD
7816 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007818retry:
7819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 /*
7821 * Allocation of the screen buffers is done only when the size changes and
7822 * when Rows and Columns have been set and we have started doing full
7823 * screen stuff.
7824 */
7825 if ((ScreenLines != NULL
7826 && Rows == screen_Rows
7827 && Columns == screen_Columns
7828#ifdef FEAT_MBYTE
7829 && enc_utf8 == (ScreenLinesUC != NULL)
7830 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007831 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832#endif
7833 )
7834 || Rows == 0
7835 || Columns == 0
7836 || (!full_screen && ScreenLines == NULL))
7837 return;
7838
7839 /*
7840 * It's possible that we produce an out-of-memory message below, which
7841 * will cause this function to be called again. To break the loop, just
7842 * return here.
7843 */
7844 if (entered)
7845 return;
7846 entered = TRUE;
7847
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007848 /*
7849 * Note that the window sizes are updated before reallocating the arrays,
7850 * thus we must not redraw here!
7851 */
7852 ++RedrawingDisabled;
7853
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 win_new_shellsize(); /* fit the windows in the new sized shell */
7855
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 comp_col(); /* recompute columns for shown command and ruler */
7857
7858 /*
7859 * We're changing the size of the screen.
7860 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7861 * - Move lines from the old arrays into the new arrays, clear extra
7862 * lines (unless the screen is going to be cleared).
7863 * - Free the old arrays.
7864 *
7865 * If anything fails, make ScreenLines NULL, so we don't do anything!
7866 * Continuing with the old ScreenLines may result in a crash, because the
7867 * size is wrong.
7868 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007869 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007871#ifdef FEAT_AUTOCMD
7872 if (aucmd_win != NULL)
7873 win_free_lsize(aucmd_win);
7874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875
7876 new_ScreenLines = (schar_T *)lalloc((long_u)(
7877 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7878#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007879 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 if (enc_utf8)
7881 {
7882 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7883 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007884 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007885 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7887 }
7888 if (enc_dbcs == DBCS_JPNU)
7889 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7890 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7891#endif
7892 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7893 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7894 new_LineOffset = (unsigned *)lalloc((long_u)(
7895 Rows * sizeof(unsigned)), FALSE);
7896 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007897#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007898 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007901 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {
7903 if (win_alloc_lines(wp) == FAIL)
7904 {
7905 outofmem = TRUE;
7906#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007907 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908#endif
7909 }
7910 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007911#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007912 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7913 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007914 outofmem = TRUE;
7915#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007916#ifdef FEAT_WINDOWS
7917give_up:
7918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007920#ifdef FEAT_MBYTE
7921 for (i = 0; i < p_mco; ++i)
7922 if (new_ScreenLinesC[i] == NULL)
7923 break;
7924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 if (new_ScreenLines == NULL
7926#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007927 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7929#endif
7930 || new_ScreenAttrs == NULL
7931 || new_LineOffset == NULL
7932 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007933#ifdef FEAT_WINDOWS
7934 || new_TabPageIdxs == NULL
7935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 || outofmem)
7937 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007938 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007939 {
7940 /* guess the size */
7941 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7942
7943 /* Remember we did this to avoid getting outofmem messages over
7944 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007945 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 vim_free(new_ScreenLines);
7948 new_ScreenLines = NULL;
7949#ifdef FEAT_MBYTE
7950 vim_free(new_ScreenLinesUC);
7951 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007952 for (i = 0; i < p_mco; ++i)
7953 {
7954 vim_free(new_ScreenLinesC[i]);
7955 new_ScreenLinesC[i] = NULL;
7956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 vim_free(new_ScreenLines2);
7958 new_ScreenLines2 = NULL;
7959#endif
7960 vim_free(new_ScreenAttrs);
7961 new_ScreenAttrs = NULL;
7962 vim_free(new_LineOffset);
7963 new_LineOffset = NULL;
7964 vim_free(new_LineWraps);
7965 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007966#ifdef FEAT_WINDOWS
7967 vim_free(new_TabPageIdxs);
7968 new_TabPageIdxs = NULL;
7969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 }
7971 else
7972 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007973 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007974
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 for (new_row = 0; new_row < Rows; ++new_row)
7976 {
7977 new_LineOffset[new_row] = new_row * Columns;
7978 new_LineWraps[new_row] = FALSE;
7979
7980 /*
7981 * If the screen is not going to be cleared, copy as much as
7982 * possible from the old screen to the new one and clear the rest
7983 * (used when resizing the window at the "--more--" prompt or when
7984 * executing an external command, for the GUI).
7985 */
7986 if (!clear)
7987 {
7988 (void)vim_memset(new_ScreenLines + new_row * Columns,
7989 ' ', (size_t)Columns * sizeof(schar_T));
7990#ifdef FEAT_MBYTE
7991 if (enc_utf8)
7992 {
7993 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7994 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007995 for (i = 0; i < p_mco; ++i)
7996 (void)vim_memset(new_ScreenLinesC[i]
7997 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 0, (size_t)Columns * sizeof(u8char_T));
7999 }
8000 if (enc_dbcs == DBCS_JPNU)
8001 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8002 0, (size_t)Columns * sizeof(schar_T));
8003#endif
8004 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8005 0, (size_t)Columns * sizeof(sattr_T));
8006 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008007 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {
8009 if (screen_Columns < Columns)
8010 len = screen_Columns;
8011 else
8012 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008013#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008014 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008015 * may be invalid now. Also when p_mco changes. */
8016 if (!(enc_utf8 && ScreenLinesUC == NULL)
8017 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008018#endif
8019 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8020 ScreenLines + LineOffset[old_row],
8021 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008023 if (enc_utf8 && ScreenLinesUC != NULL
8024 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {
8026 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8027 ScreenLinesUC + LineOffset[old_row],
8028 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008029 for (i = 0; i < p_mco; ++i)
8030 mch_memmove(new_ScreenLinesC[i]
8031 + new_LineOffset[new_row],
8032 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 (size_t)len * sizeof(u8char_T));
8034 }
8035 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8036 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8037 ScreenLines2 + LineOffset[old_row],
8038 (size_t)len * sizeof(schar_T));
8039#endif
8040 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8041 ScreenAttrs + LineOffset[old_row],
8042 (size_t)len * sizeof(sattr_T));
8043 }
8044 }
8045 }
8046 /* Use the last line of the screen for the current line. */
8047 current_ScreenLine = new_ScreenLines + Rows * Columns;
8048 }
8049
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008050 free_screenlines();
8051
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 ScreenLines = new_ScreenLines;
8053#ifdef FEAT_MBYTE
8054 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008055 for (i = 0; i < p_mco; ++i)
8056 ScreenLinesC[i] = new_ScreenLinesC[i];
8057 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 ScreenLines2 = new_ScreenLines2;
8059#endif
8060 ScreenAttrs = new_ScreenAttrs;
8061 LineOffset = new_LineOffset;
8062 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008063#ifdef FEAT_WINDOWS
8064 TabPageIdxs = new_TabPageIdxs;
8065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067 /* It's important that screen_Rows and screen_Columns reflect the actual
8068 * size of ScreenLines[]. Set them before calling anything. */
8069#ifdef FEAT_GUI
8070 old_Rows = screen_Rows;
8071#endif
8072 screen_Rows = Rows;
8073 screen_Columns = Columns;
8074
8075 must_redraw = CLEAR; /* need to clear the screen later */
8076 if (clear)
8077 screenclear2();
8078
8079#ifdef FEAT_GUI
8080 else if (gui.in_use
8081 && !gui.starting
8082 && ScreenLines != NULL
8083 && old_Rows != Rows)
8084 {
8085 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8086 /*
8087 * Adjust the position of the cursor, for when executing an external
8088 * command.
8089 */
8090 if (msg_row >= Rows) /* Rows got smaller */
8091 msg_row = Rows - 1; /* put cursor at last row */
8092 else if (Rows > old_Rows) /* Rows got bigger */
8093 msg_row += Rows - old_Rows; /* put cursor in same place */
8094 if (msg_col >= Columns) /* Columns got smaller */
8095 msg_col = Columns - 1; /* put cursor at last column */
8096 }
8097#endif
8098
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008100 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008101
8102#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008103 /*
8104 * Do not apply autocommands more than 3 times to avoid an endless loop
8105 * in case applying autocommands always changes Rows or Columns.
8106 */
8107 if (starting == 0 && ++retry_count <= 3)
8108 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008109 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008110 /* In rare cases, autocommands may have altered Rows or Columns,
8111 * jump back to check if we need to allocate the screen again. */
8112 goto retry;
8113 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115}
8116
8117 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008118free_screenlines()
8119{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008120#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008121 int i;
8122
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008123 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008124 for (i = 0; i < Screen_mco; ++i)
8125 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008126 vim_free(ScreenLines2);
8127#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008128 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008129 vim_free(ScreenAttrs);
8130 vim_free(LineOffset);
8131 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008132#ifdef FEAT_WINDOWS
8133 vim_free(TabPageIdxs);
8134#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008135}
8136
8137 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138screenclear()
8139{
8140 check_for_delay(FALSE);
8141 screenalloc(FALSE); /* allocate screen buffers if size changed */
8142 screenclear2(); /* clear the screen */
8143}
8144
8145 static void
8146screenclear2()
8147{
8148 int i;
8149
8150 if (starting == NO_SCREEN || ScreenLines == NULL
8151#ifdef FEAT_GUI
8152 || (gui.in_use && gui.starting)
8153#endif
8154 )
8155 return;
8156
8157#ifdef FEAT_GUI
8158 if (!gui.in_use)
8159#endif
8160 screen_attr = -1; /* force setting the Normal colors */
8161 screen_stop_highlight(); /* don't want highlighting here */
8162
8163#ifdef FEAT_CLIPBOARD
8164 /* disable selection without redrawing it */
8165 clip_scroll_selection(9999);
8166#endif
8167
8168 /* blank out ScreenLines */
8169 for (i = 0; i < Rows; ++i)
8170 {
8171 lineclear(LineOffset[i], (int)Columns);
8172 LineWraps[i] = FALSE;
8173 }
8174
8175 if (can_clear(T_CL))
8176 {
8177 out_str(T_CL); /* clear the display */
8178 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008179 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 }
8181 else
8182 {
8183 /* can't clear the screen, mark all chars with invalid attributes */
8184 for (i = 0; i < Rows; ++i)
8185 lineinvalid(LineOffset[i], (int)Columns);
8186 clear_cmdline = TRUE;
8187 }
8188
8189 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8190
8191 win_rest_invalid(firstwin);
8192 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008193#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008194 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 if (must_redraw == CLEAR) /* no need to clear again */
8197 must_redraw = NOT_VALID;
8198 compute_cmdrow();
8199 msg_row = cmdline_row; /* put cursor on last line for messages */
8200 msg_col = 0;
8201 screen_start(); /* don't know where cursor is now */
8202 msg_scrolled = 0; /* can't scroll back */
8203 msg_didany = FALSE;
8204 msg_didout = FALSE;
8205}
8206
8207/*
8208 * Clear one line in ScreenLines.
8209 */
8210 static void
8211lineclear(off, width)
8212 unsigned off;
8213 int width;
8214{
8215 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8216#ifdef FEAT_MBYTE
8217 if (enc_utf8)
8218 (void)vim_memset(ScreenLinesUC + off, 0,
8219 (size_t)width * sizeof(u8char_T));
8220#endif
8221 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8222}
8223
8224/*
8225 * Mark one line in ScreenLines invalid by setting the attributes to an
8226 * invalid value.
8227 */
8228 static void
8229lineinvalid(off, width)
8230 unsigned off;
8231 int width;
8232{
8233 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8234}
8235
8236#ifdef FEAT_VERTSPLIT
8237/*
8238 * Copy part of a Screenline for vertically split window "wp".
8239 */
8240 static void
8241linecopy(to, from, wp)
8242 int to;
8243 int from;
8244 win_T *wp;
8245{
8246 unsigned off_to = LineOffset[to] + wp->w_wincol;
8247 unsigned off_from = LineOffset[from] + wp->w_wincol;
8248
8249 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8250 wp->w_width * sizeof(schar_T));
8251# ifdef FEAT_MBYTE
8252 if (enc_utf8)
8253 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008254 int i;
8255
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8257 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008258 for (i = 0; i < p_mco; ++i)
8259 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8260 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 }
8262 if (enc_dbcs == DBCS_JPNU)
8263 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8264 wp->w_width * sizeof(schar_T));
8265# endif
8266 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8267 wp->w_width * sizeof(sattr_T));
8268}
8269#endif
8270
8271/*
8272 * Return TRUE if clearing with term string "p" would work.
8273 * It can't work when the string is empty or it won't set the right background.
8274 */
8275 int
8276can_clear(p)
8277 char_u *p;
8278{
8279 return (*p != NUL && (t_colors <= 1
8280#ifdef FEAT_GUI
8281 || gui.in_use
8282#endif
8283 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8284}
8285
8286/*
8287 * Reset cursor position. Use whenever cursor was moved because of outputting
8288 * something directly to the screen (shell commands) or a terminal control
8289 * code.
8290 */
8291 void
8292screen_start()
8293{
8294 screen_cur_row = screen_cur_col = 9999;
8295}
8296
8297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 * Move the cursor to position "row","col" in the screen.
8299 * This tries to find the most efficient way to move, minimizing the number of
8300 * characters sent to the terminal.
8301 */
8302 void
8303windgoto(row, col)
8304 int row;
8305 int col;
8306{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008307 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 int i;
8309 int plan;
8310 int cost;
8311 int wouldbe_col;
8312 int noinvcurs;
8313 char_u *bs;
8314 int goto_cost;
8315 int attr;
8316
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008317#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8319
8320#define PLAN_LE 1
8321#define PLAN_CR 2
8322#define PLAN_NL 3
8323#define PLAN_WRITE 4
8324 /* Can't use ScreenLines unless initialized */
8325 if (ScreenLines == NULL)
8326 return;
8327
8328 if (col != screen_cur_col || row != screen_cur_row)
8329 {
8330 /* Check for valid position. */
8331 if (row < 0) /* window without text lines? */
8332 row = 0;
8333 if (row >= screen_Rows)
8334 row = screen_Rows - 1;
8335 if (col >= screen_Columns)
8336 col = screen_Columns - 1;
8337
8338 /* check if no cursor movement is allowed in highlight mode */
8339 if (screen_attr && *T_MS == NUL)
8340 noinvcurs = HIGHL_COST;
8341 else
8342 noinvcurs = 0;
8343 goto_cost = GOTO_COST + noinvcurs;
8344
8345 /*
8346 * Plan how to do the positioning:
8347 * 1. Use CR to move it to column 0, same row.
8348 * 2. Use T_LE to move it a few columns to the left.
8349 * 3. Use NL to move a few lines down, column 0.
8350 * 4. Move a few columns to the right with T_ND or by writing chars.
8351 *
8352 * Don't do this if the cursor went beyond the last column, the cursor
8353 * position is unknown then (some terminals wrap, some don't )
8354 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008355 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 * characters to move the cursor to the right.
8357 */
8358 if (row >= screen_cur_row && screen_cur_col < Columns)
8359 {
8360 /*
8361 * If the cursor is in the same row, bigger col, we can use CR
8362 * or T_LE.
8363 */
8364 bs = NULL; /* init for GCC */
8365 attr = screen_attr;
8366 if (row == screen_cur_row && col < screen_cur_col)
8367 {
8368 /* "le" is preferred over "bc", because "bc" is obsolete */
8369 if (*T_LE)
8370 bs = T_LE; /* "cursor left" */
8371 else
8372 bs = T_BC; /* "backspace character (old) */
8373 if (*bs)
8374 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8375 else
8376 cost = 999;
8377 if (col + 1 < cost) /* using CR is less characters */
8378 {
8379 plan = PLAN_CR;
8380 wouldbe_col = 0;
8381 cost = 1; /* CR is just one character */
8382 }
8383 else
8384 {
8385 plan = PLAN_LE;
8386 wouldbe_col = col;
8387 }
8388 if (noinvcurs) /* will stop highlighting */
8389 {
8390 cost += noinvcurs;
8391 attr = 0;
8392 }
8393 }
8394
8395 /*
8396 * If the cursor is above where we want to be, we can use CR LF.
8397 */
8398 else if (row > screen_cur_row)
8399 {
8400 plan = PLAN_NL;
8401 wouldbe_col = 0;
8402 cost = (row - screen_cur_row) * 2; /* CR LF */
8403 if (noinvcurs) /* will stop highlighting */
8404 {
8405 cost += noinvcurs;
8406 attr = 0;
8407 }
8408 }
8409
8410 /*
8411 * If the cursor is in the same row, smaller col, just use write.
8412 */
8413 else
8414 {
8415 plan = PLAN_WRITE;
8416 wouldbe_col = screen_cur_col;
8417 cost = 0;
8418 }
8419
8420 /*
8421 * Check if any characters that need to be written have the
8422 * correct attributes. Also avoid UTF-8 characters.
8423 */
8424 i = col - wouldbe_col;
8425 if (i > 0)
8426 cost += i;
8427 if (cost < goto_cost && i > 0)
8428 {
8429 /*
8430 * Check if the attributes are correct without additionally
8431 * stopping highlighting.
8432 */
8433 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8434 while (i && *p++ == attr)
8435 --i;
8436 if (i != 0)
8437 {
8438 /*
8439 * Try if it works when highlighting is stopped here.
8440 */
8441 if (*--p == 0)
8442 {
8443 cost += noinvcurs;
8444 while (i && *p++ == 0)
8445 --i;
8446 }
8447 if (i != 0)
8448 cost = 999; /* different attributes, don't do it */
8449 }
8450#ifdef FEAT_MBYTE
8451 if (enc_utf8)
8452 {
8453 /* Don't use an UTF-8 char for positioning, it's slow. */
8454 for (i = wouldbe_col; i < col; ++i)
8455 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8456 {
8457 cost = 999;
8458 break;
8459 }
8460 }
8461#endif
8462 }
8463
8464 /*
8465 * We can do it without term_windgoto()!
8466 */
8467 if (cost < goto_cost)
8468 {
8469 if (plan == PLAN_LE)
8470 {
8471 if (noinvcurs)
8472 screen_stop_highlight();
8473 while (screen_cur_col > col)
8474 {
8475 out_str(bs);
8476 --screen_cur_col;
8477 }
8478 }
8479 else if (plan == PLAN_CR)
8480 {
8481 if (noinvcurs)
8482 screen_stop_highlight();
8483 out_char('\r');
8484 screen_cur_col = 0;
8485 }
8486 else if (plan == PLAN_NL)
8487 {
8488 if (noinvcurs)
8489 screen_stop_highlight();
8490 while (screen_cur_row < row)
8491 {
8492 out_char('\n');
8493 ++screen_cur_row;
8494 }
8495 screen_cur_col = 0;
8496 }
8497
8498 i = col - screen_cur_col;
8499 if (i > 0)
8500 {
8501 /*
8502 * Use cursor-right if it's one character only. Avoids
8503 * removing a line of pixels from the last bold char, when
8504 * using the bold trick in the GUI.
8505 */
8506 if (T_ND[0] != NUL && T_ND[1] == NUL)
8507 {
8508 while (i-- > 0)
8509 out_char(*T_ND);
8510 }
8511 else
8512 {
8513 int off;
8514
8515 off = LineOffset[row] + screen_cur_col;
8516 while (i-- > 0)
8517 {
8518 if (ScreenAttrs[off] != screen_attr)
8519 screen_stop_highlight();
8520#ifdef FEAT_MBYTE
8521 out_flush_check();
8522#endif
8523 out_char(ScreenLines[off]);
8524#ifdef FEAT_MBYTE
8525 if (enc_dbcs == DBCS_JPNU
8526 && ScreenLines[off] == 0x8e)
8527 out_char(ScreenLines2[off]);
8528#endif
8529 ++off;
8530 }
8531 }
8532 }
8533 }
8534 }
8535 else
8536 cost = 999;
8537
8538 if (cost >= goto_cost)
8539 {
8540 if (noinvcurs)
8541 screen_stop_highlight();
8542 if (row == screen_cur_row && (col > screen_cur_col) &&
8543 *T_CRI != NUL)
8544 term_cursor_right(col - screen_cur_col);
8545 else
8546 term_windgoto(row, col);
8547 }
8548 screen_cur_row = row;
8549 screen_cur_col = col;
8550 }
8551}
8552
8553/*
8554 * Set cursor to its position in the current window.
8555 */
8556 void
8557setcursor()
8558{
8559 if (redrawing())
8560 {
8561 validate_cursor();
8562 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8563 W_WINCOL(curwin) + (
8564#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008565 /* With 'rightleft' set and the cursor on a double-wide
8566 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8568# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008569 (has_mbyte
8570 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8571 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572# endif
8573 1)) :
8574#endif
8575 curwin->w_wcol));
8576 }
8577}
8578
8579
8580/*
8581 * insert 'line_count' lines at 'row' in window 'wp'
8582 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8583 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8584 * scrolling.
8585 * Returns FAIL if the lines are not inserted, OK for success.
8586 */
8587 int
8588win_ins_lines(wp, row, line_count, invalid, mayclear)
8589 win_T *wp;
8590 int row;
8591 int line_count;
8592 int invalid;
8593 int mayclear;
8594{
8595 int did_delete;
8596 int nextrow;
8597 int lastrow;
8598 int retval;
8599
8600 if (invalid)
8601 wp->w_lines_valid = 0;
8602
8603 if (wp->w_height < 5)
8604 return FAIL;
8605
8606 if (line_count > wp->w_height - row)
8607 line_count = wp->w_height - row;
8608
8609 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8610 if (retval != MAYBE)
8611 return retval;
8612
8613 /*
8614 * If there is a next window or a status line, we first try to delete the
8615 * lines at the bottom to avoid messing what is after the window.
8616 * If this fails and there are following windows, don't do anything to avoid
8617 * messing up those windows, better just redraw.
8618 */
8619 did_delete = FALSE;
8620#ifdef FEAT_WINDOWS
8621 if (wp->w_next != NULL || wp->w_status_height)
8622 {
8623 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8624 line_count, (int)Rows, FALSE, NULL) == OK)
8625 did_delete = TRUE;
8626 else if (wp->w_next)
8627 return FAIL;
8628 }
8629#endif
8630 /*
8631 * if no lines deleted, blank the lines that will end up below the window
8632 */
8633 if (!did_delete)
8634 {
8635#ifdef FEAT_WINDOWS
8636 wp->w_redr_status = TRUE;
8637#endif
8638 redraw_cmdline = TRUE;
8639 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8640 lastrow = nextrow + line_count;
8641 if (lastrow > Rows)
8642 lastrow = Rows;
8643 screen_fill(nextrow - line_count, lastrow - line_count,
8644 W_WINCOL(wp), (int)W_ENDCOL(wp),
8645 ' ', ' ', 0);
8646 }
8647
8648 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8649 == FAIL)
8650 {
8651 /* deletion will have messed up other windows */
8652 if (did_delete)
8653 {
8654#ifdef FEAT_WINDOWS
8655 wp->w_redr_status = TRUE;
8656#endif
8657 win_rest_invalid(W_NEXT(wp));
8658 }
8659 return FAIL;
8660 }
8661
8662 return OK;
8663}
8664
8665/*
8666 * delete "line_count" window lines at "row" in window "wp"
8667 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8668 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8669 * scrolling
8670 * Return OK for success, FAIL if the lines are not deleted.
8671 */
8672 int
8673win_del_lines(wp, row, line_count, invalid, mayclear)
8674 win_T *wp;
8675 int row;
8676 int line_count;
8677 int invalid;
8678 int mayclear;
8679{
8680 int retval;
8681
8682 if (invalid)
8683 wp->w_lines_valid = 0;
8684
8685 if (line_count > wp->w_height - row)
8686 line_count = wp->w_height - row;
8687
8688 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8689 if (retval != MAYBE)
8690 return retval;
8691
8692 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8693 (int)Rows, FALSE, NULL) == FAIL)
8694 return FAIL;
8695
8696#ifdef FEAT_WINDOWS
8697 /*
8698 * If there are windows or status lines below, try to put them at the
8699 * correct place. If we can't do that, they have to be redrawn.
8700 */
8701 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8702 {
8703 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8704 line_count, (int)Rows, NULL) == FAIL)
8705 {
8706 wp->w_redr_status = TRUE;
8707 win_rest_invalid(wp->w_next);
8708 }
8709 }
8710 /*
8711 * If this is the last window and there is no status line, redraw the
8712 * command line later.
8713 */
8714 else
8715#endif
8716 redraw_cmdline = TRUE;
8717 return OK;
8718}
8719
8720/*
8721 * Common code for win_ins_lines() and win_del_lines().
8722 * Returns OK or FAIL when the work has been done.
8723 * Returns MAYBE when not finished yet.
8724 */
8725 static int
8726win_do_lines(wp, row, line_count, mayclear, del)
8727 win_T *wp;
8728 int row;
8729 int line_count;
8730 int mayclear;
8731 int del;
8732{
8733 int retval;
8734
8735 if (!redrawing() || line_count <= 0)
8736 return FAIL;
8737
8738 /* only a few lines left: redraw is faster */
8739 if (mayclear && Rows - line_count < 5
8740#ifdef FEAT_VERTSPLIT
8741 && wp->w_width == Columns
8742#endif
8743 )
8744 {
8745 screenclear(); /* will set wp->w_lines_valid to 0 */
8746 return FAIL;
8747 }
8748
8749 /*
8750 * Delete all remaining lines
8751 */
8752 if (row + line_count >= wp->w_height)
8753 {
8754 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8755 W_WINCOL(wp), (int)W_ENDCOL(wp),
8756 ' ', ' ', 0);
8757 return OK;
8758 }
8759
8760 /*
8761 * when scrolling, the message on the command line should be cleared,
8762 * otherwise it will stay there forever.
8763 */
8764 clear_cmdline = TRUE;
8765
8766 /*
8767 * If the terminal can set a scroll region, use that.
8768 * Always do this in a vertically split window. This will redraw from
8769 * ScreenLines[] when t_CV isn't defined. That's faster than using
8770 * win_line().
8771 * Don't use a scroll region when we are going to redraw the text, writing
8772 * a character in the lower right corner of the scroll region causes a
8773 * scroll-up in the DJGPP version.
8774 */
8775 if (scroll_region
8776#ifdef FEAT_VERTSPLIT
8777 || W_WIDTH(wp) != Columns
8778#endif
8779 )
8780 {
8781#ifdef FEAT_VERTSPLIT
8782 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8783#endif
8784 scroll_region_set(wp, row);
8785 if (del)
8786 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8787 wp->w_height - row, FALSE, wp);
8788 else
8789 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8790 wp->w_height - row, wp);
8791#ifdef FEAT_VERTSPLIT
8792 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8793#endif
8794 scroll_region_reset();
8795 return retval;
8796 }
8797
8798#ifdef FEAT_WINDOWS
8799 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8800 return FAIL;
8801#endif
8802
8803 return MAYBE;
8804}
8805
8806/*
8807 * window 'wp' and everything after it is messed up, mark it for redraw
8808 */
8809 static void
8810win_rest_invalid(wp)
8811 win_T *wp;
8812{
8813#ifdef FEAT_WINDOWS
8814 while (wp != NULL)
8815#else
8816 if (wp != NULL)
8817#endif
8818 {
8819 redraw_win_later(wp, NOT_VALID);
8820#ifdef FEAT_WINDOWS
8821 wp->w_redr_status = TRUE;
8822 wp = wp->w_next;
8823#endif
8824 }
8825 redraw_cmdline = TRUE;
8826}
8827
8828/*
8829 * The rest of the routines in this file perform screen manipulations. The
8830 * given operation is performed physically on the screen. The corresponding
8831 * change is also made to the internal screen image. In this way, the editor
8832 * anticipates the effect of editing changes on the appearance of the screen.
8833 * That way, when we call screenupdate a complete redraw isn't usually
8834 * necessary. Another advantage is that we can keep adding code to anticipate
8835 * screen changes, and in the meantime, everything still works.
8836 */
8837
8838/*
8839 * types for inserting or deleting lines
8840 */
8841#define USE_T_CAL 1
8842#define USE_T_CDL 2
8843#define USE_T_AL 3
8844#define USE_T_CE 4
8845#define USE_T_DL 5
8846#define USE_T_SR 6
8847#define USE_NL 7
8848#define USE_T_CD 8
8849#define USE_REDRAW 9
8850
8851/*
8852 * insert lines on the screen and update ScreenLines[]
8853 * 'end' is the line after the scrolled part. Normally it is Rows.
8854 * When scrolling region used 'off' is the offset from the top for the region.
8855 * 'row' and 'end' are relative to the start of the region.
8856 *
8857 * return FAIL for failure, OK for success.
8858 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008859 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860screen_ins_lines(off, row, line_count, end, wp)
8861 int off;
8862 int row;
8863 int line_count;
8864 int end;
8865 win_T *wp; /* NULL or window to use width from */
8866{
8867 int i;
8868 int j;
8869 unsigned temp;
8870 int cursor_row;
8871 int type;
8872 int result_empty;
8873 int can_ce = can_clear(T_CE);
8874
8875 /*
8876 * FAIL if
8877 * - there is no valid screen
8878 * - the screen has to be redrawn completely
8879 * - the line count is less than one
8880 * - the line count is more than 'ttyscroll'
8881 */
8882 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8883 return FAIL;
8884
8885 /*
8886 * There are seven ways to insert lines:
8887 * 0. When in a vertically split window and t_CV isn't set, redraw the
8888 * characters from ScreenLines[].
8889 * 1. Use T_CD (clear to end of display) if it exists and the result of
8890 * the insert is just empty lines
8891 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8892 * present or line_count > 1. It looks better if we do all the inserts
8893 * at once.
8894 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8895 * insert is just empty lines and T_CE is not present or line_count >
8896 * 1.
8897 * 4. Use T_AL (insert line) if it exists.
8898 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8899 * just empty lines.
8900 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8901 * just empty lines.
8902 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8903 * the 'da' flag is not set or we have clear line capability.
8904 * 8. redraw the characters from ScreenLines[].
8905 *
8906 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8907 * the scrollbar for the window. It does have insert line, use that if it
8908 * exists.
8909 */
8910 result_empty = (row + line_count >= end);
8911#ifdef FEAT_VERTSPLIT
8912 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8913 type = USE_REDRAW;
8914 else
8915#endif
8916 if (can_clear(T_CD) && result_empty)
8917 type = USE_T_CD;
8918 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8919 type = USE_T_CAL;
8920 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8921 type = USE_T_CDL;
8922 else if (*T_AL != NUL)
8923 type = USE_T_AL;
8924 else if (can_ce && result_empty)
8925 type = USE_T_CE;
8926 else if (*T_DL != NUL && result_empty)
8927 type = USE_T_DL;
8928 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8929 type = USE_T_SR;
8930 else
8931 return FAIL;
8932
8933 /*
8934 * For clearing the lines screen_del_lines() is used. This will also take
8935 * care of t_db if necessary.
8936 */
8937 if (type == USE_T_CD || type == USE_T_CDL ||
8938 type == USE_T_CE || type == USE_T_DL)
8939 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8940
8941 /*
8942 * If text is retained below the screen, first clear or delete as many
8943 * lines at the bottom of the window as are about to be inserted so that
8944 * the deleted lines won't later surface during a screen_del_lines.
8945 */
8946 if (*T_DB)
8947 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8948
8949#ifdef FEAT_CLIPBOARD
8950 /* Remove a modeless selection when inserting lines halfway the screen
8951 * or not the full width of the screen. */
8952 if (off + row > 0
8953# ifdef FEAT_VERTSPLIT
8954 || (wp != NULL && wp->w_width != Columns)
8955# endif
8956 )
8957 clip_clear_selection();
8958 else
8959 clip_scroll_selection(-line_count);
8960#endif
8961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962#ifdef FEAT_GUI
8963 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8964 * scrolling is actually carried out. */
8965 gui_dont_update_cursor();
8966#endif
8967
8968 if (*T_CCS != NUL) /* cursor relative to region */
8969 cursor_row = row;
8970 else
8971 cursor_row = row + off;
8972
8973 /*
8974 * Shift LineOffset[] line_count down to reflect the inserted lines.
8975 * Clear the inserted lines in ScreenLines[].
8976 */
8977 row += off;
8978 end += off;
8979 for (i = 0; i < line_count; ++i)
8980 {
8981#ifdef FEAT_VERTSPLIT
8982 if (wp != NULL && wp->w_width != Columns)
8983 {
8984 /* need to copy part of a line */
8985 j = end - 1 - i;
8986 while ((j -= line_count) >= row)
8987 linecopy(j + line_count, j, wp);
8988 j += line_count;
8989 if (can_clear((char_u *)" "))
8990 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8991 else
8992 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8993 LineWraps[j] = FALSE;
8994 }
8995 else
8996#endif
8997 {
8998 j = end - 1 - i;
8999 temp = LineOffset[j];
9000 while ((j -= line_count) >= row)
9001 {
9002 LineOffset[j + line_count] = LineOffset[j];
9003 LineWraps[j + line_count] = LineWraps[j];
9004 }
9005 LineOffset[j + line_count] = temp;
9006 LineWraps[j + line_count] = FALSE;
9007 if (can_clear((char_u *)" "))
9008 lineclear(temp, (int)Columns);
9009 else
9010 lineinvalid(temp, (int)Columns);
9011 }
9012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013
9014 screen_stop_highlight();
9015 windgoto(cursor_row, 0);
9016
9017#ifdef FEAT_VERTSPLIT
9018 /* redraw the characters */
9019 if (type == USE_REDRAW)
9020 redraw_block(row, end, wp);
9021 else
9022#endif
9023 if (type == USE_T_CAL)
9024 {
9025 term_append_lines(line_count);
9026 screen_start(); /* don't know where cursor is now */
9027 }
9028 else
9029 {
9030 for (i = 0; i < line_count; i++)
9031 {
9032 if (type == USE_T_AL)
9033 {
9034 if (i && cursor_row != 0)
9035 windgoto(cursor_row, 0);
9036 out_str(T_AL);
9037 }
9038 else /* type == USE_T_SR */
9039 out_str(T_SR);
9040 screen_start(); /* don't know where cursor is now */
9041 }
9042 }
9043
9044 /*
9045 * With scroll-reverse and 'da' flag set we need to clear the lines that
9046 * have been scrolled down into the region.
9047 */
9048 if (type == USE_T_SR && *T_DA)
9049 {
9050 for (i = 0; i < line_count; ++i)
9051 {
9052 windgoto(off + i, 0);
9053 out_str(T_CE);
9054 screen_start(); /* don't know where cursor is now */
9055 }
9056 }
9057
9058#ifdef FEAT_GUI
9059 gui_can_update_cursor();
9060 if (gui.in_use)
9061 out_flush(); /* always flush after a scroll */
9062#endif
9063 return OK;
9064}
9065
9066/*
9067 * delete lines on the screen and update ScreenLines[]
9068 * 'end' is the line after the scrolled part. Normally it is Rows.
9069 * When scrolling region used 'off' is the offset from the top for the region.
9070 * 'row' and 'end' are relative to the start of the region.
9071 *
9072 * Return OK for success, FAIL if the lines are not deleted.
9073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 int
9075screen_del_lines(off, row, line_count, end, force, wp)
9076 int off;
9077 int row;
9078 int line_count;
9079 int end;
9080 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009081 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
9083 int j;
9084 int i;
9085 unsigned temp;
9086 int cursor_row;
9087 int cursor_end;
9088 int result_empty; /* result is empty until end of region */
9089 int can_delete; /* deleting line codes can be used */
9090 int type;
9091
9092 /*
9093 * FAIL if
9094 * - there is no valid screen
9095 * - the screen has to be redrawn completely
9096 * - the line count is less than one
9097 * - the line count is more than 'ttyscroll'
9098 */
9099 if (!screen_valid(TRUE) || line_count <= 0 ||
9100 (!force && line_count > p_ttyscroll))
9101 return FAIL;
9102
9103 /*
9104 * Check if the rest of the current region will become empty.
9105 */
9106 result_empty = row + line_count >= end;
9107
9108 /*
9109 * We can delete lines only when 'db' flag not set or when 'ce' option
9110 * available.
9111 */
9112 can_delete = (*T_DB == NUL || can_clear(T_CE));
9113
9114 /*
9115 * There are six ways to delete lines:
9116 * 0. When in a vertically split window and t_CV isn't set, redraw the
9117 * characters from ScreenLines[].
9118 * 1. Use T_CD if it exists and the result is empty.
9119 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9120 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9121 * none of the other ways work.
9122 * 4. Use T_CE (erase line) if the result is empty.
9123 * 5. Use T_DL (delete line) if it exists.
9124 * 6. redraw the characters from ScreenLines[].
9125 */
9126#ifdef FEAT_VERTSPLIT
9127 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9128 type = USE_REDRAW;
9129 else
9130#endif
9131 if (can_clear(T_CD) && result_empty)
9132 type = USE_T_CD;
9133#if defined(__BEOS__) && defined(BEOS_DR8)
9134 /*
9135 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9136 * its internal termcap... this works okay for tests which test *T_DB !=
9137 * NUL. It has the disadvantage that the user cannot use any :set t_*
9138 * command to get T_DB (back) to empty_option, only :set term=... will do
9139 * the trick...
9140 * Anyway, this hack will hopefully go away with the next OS release.
9141 * (Olaf Seibert)
9142 */
9143 else if (row == 0 && T_DB == empty_option
9144 && (line_count == 1 || *T_CDL == NUL))
9145#else
9146 else if (row == 0 && (
9147#ifndef AMIGA
9148 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9149 * up, so use delete-line command */
9150 line_count == 1 ||
9151#endif
9152 *T_CDL == NUL))
9153#endif
9154 type = USE_NL;
9155 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9156 type = USE_T_CDL;
9157 else if (can_clear(T_CE) && result_empty
9158#ifdef FEAT_VERTSPLIT
9159 && (wp == NULL || wp->w_width == Columns)
9160#endif
9161 )
9162 type = USE_T_CE;
9163 else if (*T_DL != NUL && can_delete)
9164 type = USE_T_DL;
9165 else if (*T_CDL != NUL && can_delete)
9166 type = USE_T_CDL;
9167 else
9168 return FAIL;
9169
9170#ifdef FEAT_CLIPBOARD
9171 /* Remove a modeless selection when deleting lines halfway the screen or
9172 * not the full width of the screen. */
9173 if (off + row > 0
9174# ifdef FEAT_VERTSPLIT
9175 || (wp != NULL && wp->w_width != Columns)
9176# endif
9177 )
9178 clip_clear_selection();
9179 else
9180 clip_scroll_selection(line_count);
9181#endif
9182
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183#ifdef FEAT_GUI
9184 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9185 * scrolling is actually carried out. */
9186 gui_dont_update_cursor();
9187#endif
9188
9189 if (*T_CCS != NUL) /* cursor relative to region */
9190 {
9191 cursor_row = row;
9192 cursor_end = end;
9193 }
9194 else
9195 {
9196 cursor_row = row + off;
9197 cursor_end = end + off;
9198 }
9199
9200 /*
9201 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9202 * Clear the inserted lines in ScreenLines[].
9203 */
9204 row += off;
9205 end += off;
9206 for (i = 0; i < line_count; ++i)
9207 {
9208#ifdef FEAT_VERTSPLIT
9209 if (wp != NULL && wp->w_width != Columns)
9210 {
9211 /* need to copy part of a line */
9212 j = row + i;
9213 while ((j += line_count) <= end - 1)
9214 linecopy(j - line_count, j, wp);
9215 j -= line_count;
9216 if (can_clear((char_u *)" "))
9217 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9218 else
9219 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9220 LineWraps[j] = FALSE;
9221 }
9222 else
9223#endif
9224 {
9225 /* whole width, moving the line pointers is faster */
9226 j = row + i;
9227 temp = LineOffset[j];
9228 while ((j += line_count) <= end - 1)
9229 {
9230 LineOffset[j - line_count] = LineOffset[j];
9231 LineWraps[j - line_count] = LineWraps[j];
9232 }
9233 LineOffset[j - line_count] = temp;
9234 LineWraps[j - line_count] = FALSE;
9235 if (can_clear((char_u *)" "))
9236 lineclear(temp, (int)Columns);
9237 else
9238 lineinvalid(temp, (int)Columns);
9239 }
9240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
9242 screen_stop_highlight();
9243
9244#ifdef FEAT_VERTSPLIT
9245 /* redraw the characters */
9246 if (type == USE_REDRAW)
9247 redraw_block(row, end, wp);
9248 else
9249#endif
9250 if (type == USE_T_CD) /* delete the lines */
9251 {
9252 windgoto(cursor_row, 0);
9253 out_str(T_CD);
9254 screen_start(); /* don't know where cursor is now */
9255 }
9256 else if (type == USE_T_CDL)
9257 {
9258 windgoto(cursor_row, 0);
9259 term_delete_lines(line_count);
9260 screen_start(); /* don't know where cursor is now */
9261 }
9262 /*
9263 * Deleting lines at top of the screen or scroll region: Just scroll
9264 * the whole screen (scroll region) up by outputting newlines on the
9265 * last line.
9266 */
9267 else if (type == USE_NL)
9268 {
9269 windgoto(cursor_end - 1, 0);
9270 for (i = line_count; --i >= 0; )
9271 out_char('\n'); /* cursor will remain on same line */
9272 }
9273 else
9274 {
9275 for (i = line_count; --i >= 0; )
9276 {
9277 if (type == USE_T_DL)
9278 {
9279 windgoto(cursor_row, 0);
9280 out_str(T_DL); /* delete a line */
9281 }
9282 else /* type == USE_T_CE */
9283 {
9284 windgoto(cursor_row + i, 0);
9285 out_str(T_CE); /* erase a line */
9286 }
9287 screen_start(); /* don't know where cursor is now */
9288 }
9289 }
9290
9291 /*
9292 * If the 'db' flag is set, we need to clear the lines that have been
9293 * scrolled up at the bottom of the region.
9294 */
9295 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9296 {
9297 for (i = line_count; i > 0; --i)
9298 {
9299 windgoto(cursor_end - i, 0);
9300 out_str(T_CE); /* erase a line */
9301 screen_start(); /* don't know where cursor is now */
9302 }
9303 }
9304
9305#ifdef FEAT_GUI
9306 gui_can_update_cursor();
9307 if (gui.in_use)
9308 out_flush(); /* always flush after a scroll */
9309#endif
9310
9311 return OK;
9312}
9313
9314/*
9315 * show the current mode and ruler
9316 *
9317 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9318 * If clear_cmdline is FALSE there may be a message there that needs to be
9319 * cleared only if a mode is shown.
9320 * Return the length of the message (0 if no message).
9321 */
9322 int
9323showmode()
9324{
9325 int need_clear;
9326 int length = 0;
9327 int do_mode;
9328 int attr;
9329 int nwr_save;
9330#ifdef FEAT_INS_EXPAND
9331 int sub_attr;
9332#endif
9333
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009334 do_mode = ((p_smd && msg_silent == 0)
9335 && ((State & INSERT)
9336 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337#ifdef FEAT_VISUAL
9338 || VIsual_active
9339#endif
9340 ));
9341 if (do_mode || Recording)
9342 {
9343 /*
9344 * Don't show mode right now, when not redrawing or inside a mapping.
9345 * Call char_avail() only when we are going to show something, because
9346 * it takes a bit of time.
9347 */
9348 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9349 {
9350 redraw_cmdline = TRUE; /* show mode later */
9351 return 0;
9352 }
9353
9354 nwr_save = need_wait_return;
9355
9356 /* wait a bit before overwriting an important message */
9357 check_for_delay(FALSE);
9358
9359 /* if the cmdline is more than one line high, erase top lines */
9360 need_clear = clear_cmdline;
9361 if (clear_cmdline && cmdline_row < Rows - 1)
9362 msg_clr_cmdline(); /* will reset clear_cmdline */
9363
9364 /* Position on the last line in the window, column 0 */
9365 msg_pos_mode();
9366 cursor_off();
9367 attr = hl_attr(HLF_CM); /* Highlight mode */
9368 if (do_mode)
9369 {
9370 MSG_PUTS_ATTR("--", attr);
9371#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009372# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 if (xic != NULL && im_get_status() && !p_imdisable
9374 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009375# else
9376 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009377# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009378 preedit_get_status()
9379# else
9380 im_get_status()
9381# endif
9382 )
9383# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009384# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 MSG_PUTS_ATTR(" IM", attr);
9386# else
9387 MSG_PUTS_ATTR(" XIM", attr);
9388# endif
9389#endif
9390#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9391 if (gui.in_use)
9392 {
9393 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009394 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 }
9396#endif
9397#ifdef FEAT_INS_EXPAND
9398 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9399 {
9400 /* These messages can get long, avoid a wrap in a narrow
9401 * window. Prefer showing edit_submode_extra. */
9402 length = (Rows - msg_row) * Columns - 3;
9403 if (edit_submode_extra != NULL)
9404 length -= vim_strsize(edit_submode_extra);
9405 if (length > 0)
9406 {
9407 if (edit_submode_pre != NULL)
9408 length -= vim_strsize(edit_submode_pre);
9409 if (length - vim_strsize(edit_submode) > 0)
9410 {
9411 if (edit_submode_pre != NULL)
9412 msg_puts_attr(edit_submode_pre, attr);
9413 msg_puts_attr(edit_submode, attr);
9414 }
9415 if (edit_submode_extra != NULL)
9416 {
9417 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9418 if ((int)edit_submode_highl < (int)HLF_COUNT)
9419 sub_attr = hl_attr(edit_submode_highl);
9420 else
9421 sub_attr = attr;
9422 msg_puts_attr(edit_submode_extra, sub_attr);
9423 }
9424 }
9425 length = 0;
9426 }
9427 else
9428#endif
9429 {
9430#ifdef FEAT_VREPLACE
9431 if (State & VREPLACE_FLAG)
9432 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9433 else
9434#endif
9435 if (State & REPLACE_FLAG)
9436 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9437 else if (State & INSERT)
9438 {
9439#ifdef FEAT_RIGHTLEFT
9440 if (p_ri)
9441 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9442#endif
9443 MSG_PUTS_ATTR(_(" INSERT"), attr);
9444 }
9445 else if (restart_edit == 'I')
9446 MSG_PUTS_ATTR(_(" (insert)"), attr);
9447 else if (restart_edit == 'R')
9448 MSG_PUTS_ATTR(_(" (replace)"), attr);
9449 else if (restart_edit == 'V')
9450 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9451#ifdef FEAT_RIGHTLEFT
9452 if (p_hkmap)
9453 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9454# ifdef FEAT_FKMAP
9455 if (p_fkmap)
9456 MSG_PUTS_ATTR(farsi_text_5, attr);
9457# endif
9458#endif
9459#ifdef FEAT_KEYMAP
9460 if (State & LANGMAP)
9461 {
9462# ifdef FEAT_ARABIC
9463 if (curwin->w_p_arab)
9464 MSG_PUTS_ATTR(_(" Arabic"), attr);
9465 else
9466# endif
9467 MSG_PUTS_ATTR(_(" (lang)"), attr);
9468 }
9469#endif
9470 if ((State & INSERT) && p_paste)
9471 MSG_PUTS_ATTR(_(" (paste)"), attr);
9472
9473#ifdef FEAT_VISUAL
9474 if (VIsual_active)
9475 {
9476 char *p;
9477
9478 /* Don't concatenate separate words to avoid translation
9479 * problems. */
9480 switch ((VIsual_select ? 4 : 0)
9481 + (VIsual_mode == Ctrl_V) * 2
9482 + (VIsual_mode == 'V'))
9483 {
9484 case 0: p = N_(" VISUAL"); break;
9485 case 1: p = N_(" VISUAL LINE"); break;
9486 case 2: p = N_(" VISUAL BLOCK"); break;
9487 case 4: p = N_(" SELECT"); break;
9488 case 5: p = N_(" SELECT LINE"); break;
9489 default: p = N_(" SELECT BLOCK"); break;
9490 }
9491 MSG_PUTS_ATTR(_(p), attr);
9492 }
9493#endif
9494 MSG_PUTS_ATTR(" --", attr);
9495 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009496
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 need_clear = TRUE;
9498 }
9499 if (Recording
9500#ifdef FEAT_INS_EXPAND
9501 && edit_submode == NULL /* otherwise it gets too long */
9502#endif
9503 )
9504 {
9505 MSG_PUTS_ATTR(_("recording"), attr);
9506 need_clear = TRUE;
9507 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009508
9509 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 if (need_clear || clear_cmdline)
9511 msg_clr_eos();
9512 msg_didout = FALSE; /* overwrite this message */
9513 length = msg_col;
9514 msg_col = 0;
9515 need_wait_return = nwr_save; /* never ask for hit-return for this */
9516 }
9517 else if (clear_cmdline && msg_silent == 0)
9518 /* Clear the whole command line. Will reset "clear_cmdline". */
9519 msg_clr_cmdline();
9520
9521#ifdef FEAT_CMDL_INFO
9522# ifdef FEAT_VISUAL
9523 /* In Visual mode the size of the selected area must be redrawn. */
9524 if (VIsual_active)
9525 clear_showcmd();
9526# endif
9527
9528 /* If the last window has no status line, the ruler is after the mode
9529 * message and must be redrawn */
9530 if (redrawing()
9531# ifdef FEAT_WINDOWS
9532 && lastwin->w_status_height == 0
9533# endif
9534 )
9535 win_redr_ruler(lastwin, TRUE);
9536#endif
9537 redraw_cmdline = FALSE;
9538 clear_cmdline = FALSE;
9539
9540 return length;
9541}
9542
9543/*
9544 * Position for a mode message.
9545 */
9546 static void
9547msg_pos_mode()
9548{
9549 msg_col = 0;
9550 msg_row = Rows - 1;
9551}
9552
9553/*
9554 * Delete mode message. Used when ESC is typed which is expected to end
9555 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009556 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 */
9558 void
9559unshowmode(force)
9560 int force;
9561{
9562 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009563 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 */
9565 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9566 redraw_cmdline = TRUE; /* delete mode later */
9567 else
9568 {
9569 msg_pos_mode();
9570 if (Recording)
9571 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9572 msg_clr_eos();
9573 }
9574}
9575
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009576#if defined(FEAT_WINDOWS)
9577/*
9578 * Draw the tab pages line at the top of the Vim window.
9579 */
9580 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009581draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009582{
9583 int tabcount = 0;
9584 tabpage_T *tp;
9585 int tabwidth;
9586 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009587 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009588 int attr;
9589 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009590 win_T *cwp;
9591 int wincount;
9592 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009593 int c;
9594 int len;
9595 int attr_sel = hl_attr(HLF_TPS);
9596 int attr_nosel = hl_attr(HLF_TP);
9597 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009598 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009599 int room;
9600 int use_sep_chars = (t_colors < 8
9601#ifdef FEAT_GUI
9602 && !gui.in_use
9603#endif
9604 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009605
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009606 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009607
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009608#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009609 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009610 if (gui_use_tabline())
9611 {
9612 gui_update_tabline();
9613 return;
9614 }
9615#endif
9616
9617 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009618 return;
9619
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009620#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009621
9622 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9623 for (scol = 0; scol < Columns; ++scol)
9624 TabPageIdxs[scol] = 0;
9625
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009626 /* Use the 'tabline' option if it's set. */
9627 if (*p_tal != NUL)
9628 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009629 int save_called_emsg = called_emsg;
9630
9631 /* Check for an error. If there is one we would loop in redrawing the
9632 * screen. Avoid that by making 'tabline' empty. */
9633 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009634 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009635 if (called_emsg)
9636 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009637 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009638 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009639 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009640 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009641#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009642 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009643 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9644 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009645
Bram Moolenaar238a5642006-02-21 22:12:05 +00009646 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9647 if (tabwidth < 6)
9648 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009649
Bram Moolenaar238a5642006-02-21 22:12:05 +00009650 attr = attr_nosel;
9651 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009652 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009653 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9654 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009655 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009656 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009657
Bram Moolenaar238a5642006-02-21 22:12:05 +00009658 if (tp->tp_topframe == topframe)
9659 attr = attr_sel;
9660 if (use_sep_chars && col > 0)
9661 screen_putchar('|', 0, col++, attr);
9662
9663 if (tp->tp_topframe != topframe)
9664 attr = attr_nosel;
9665
9666 screen_putchar(' ', 0, col++, attr);
9667
9668 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009669 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009670 cwp = curwin;
9671 wp = firstwin;
9672 }
9673 else
9674 {
9675 cwp = tp->tp_curwin;
9676 wp = tp->tp_firstwin;
9677 }
9678
9679 modified = FALSE;
9680 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9681 if (bufIsChanged(wp->w_buffer))
9682 modified = TRUE;
9683 if (modified || wincount > 1)
9684 {
9685 if (wincount > 1)
9686 {
9687 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009688 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009689 if (col + len >= Columns - 3)
9690 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009691 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009692#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009693 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009694#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009695 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009696#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009697 );
9698 col += len;
9699 }
9700 if (modified)
9701 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9702 screen_putchar(' ', 0, col++, attr);
9703 }
9704
9705 room = scol - col + tabwidth - 1;
9706 if (room > 0)
9707 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009708 /* Get buffer name in NameBuff[] */
9709 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009710 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009711 len = vim_strsize(NameBuff);
9712 p = NameBuff;
9713#ifdef FEAT_MBYTE
9714 if (has_mbyte)
9715 while (len > room)
9716 {
9717 len -= ptr2cells(p);
9718 mb_ptr_adv(p);
9719 }
9720 else
9721#endif
9722 if (len > room)
9723 {
9724 p += len - room;
9725 len = room;
9726 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009727 if (len > Columns - col - 1)
9728 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009729
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009730 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009731 col += len;
9732 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009733 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009734
9735 /* Store the tab page number in TabPageIdxs[], so that
9736 * jump_to_mouse() knows where each one is. */
9737 ++tabcount;
9738 while (scol < col)
9739 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009740 }
9741
Bram Moolenaar238a5642006-02-21 22:12:05 +00009742 if (use_sep_chars)
9743 c = '_';
9744 else
9745 c = ' ';
9746 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009747
9748 /* Put an "X" for closing the current tab if there are several. */
9749 if (first_tabpage->tp_next != NULL)
9750 {
9751 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9752 TabPageIdxs[Columns - 1] = -999;
9753 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009754 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009755
9756 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9757 * set. */
9758 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009759}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009760
9761/*
9762 * Get buffer name for "buf" into NameBuff[].
9763 * Takes care of special buffer names and translates special characters.
9764 */
9765 void
9766get_trans_bufname(buf)
9767 buf_T *buf;
9768{
9769 if (buf_spname(buf) != NULL)
9770 STRCPY(NameBuff, buf_spname(buf));
9771 else
9772 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9773 trans_characters(NameBuff, MAXPATHL);
9774}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009775#endif
9776
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9778/*
9779 * Get the character to use in a status line. Get its attributes in "*attr".
9780 */
9781 static int
9782fillchar_status(attr, is_curwin)
9783 int *attr;
9784 int is_curwin;
9785{
9786 int fill;
9787 if (is_curwin)
9788 {
9789 *attr = hl_attr(HLF_S);
9790 fill = fill_stl;
9791 }
9792 else
9793 {
9794 *attr = hl_attr(HLF_SNC);
9795 fill = fill_stlnc;
9796 }
9797 /* Use fill when there is highlighting, and highlighting of current
9798 * window differs, or the fillchars differ, or this is not the
9799 * current window */
9800 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9801 || !is_curwin || firstwin == lastwin)
9802 || (fill_stl != fill_stlnc)))
9803 return fill;
9804 if (is_curwin)
9805 return '^';
9806 return '=';
9807}
9808#endif
9809
9810#ifdef FEAT_VERTSPLIT
9811/*
9812 * Get the character to use in a separator between vertically split windows.
9813 * Get its attributes in "*attr".
9814 */
9815 static int
9816fillchar_vsep(attr)
9817 int *attr;
9818{
9819 *attr = hl_attr(HLF_C);
9820 if (*attr == 0 && fill_vert == ' ')
9821 return '|';
9822 else
9823 return fill_vert;
9824}
9825#endif
9826
9827/*
9828 * Return TRUE if redrawing should currently be done.
9829 */
9830 int
9831redrawing()
9832{
9833 return (!RedrawingDisabled
9834 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9835}
9836
9837/*
9838 * Return TRUE if printing messages should currently be done.
9839 */
9840 int
9841messaging()
9842{
9843 return (!(p_lz && char_avail() && !KeyTyped));
9844}
9845
9846/*
9847 * Show current status info in ruler and various other places
9848 * If always is FALSE, only show ruler if position has changed.
9849 */
9850 void
9851showruler(always)
9852 int always;
9853{
9854 if (!always && !redrawing())
9855 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009856#ifdef FEAT_INS_EXPAND
9857 if (pum_visible())
9858 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009859# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009860 /* Don't redraw right now, do it later. */
9861 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009862# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009863 return;
9864 }
9865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009867 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009868 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009869 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 else
9872#endif
9873#ifdef FEAT_CMDL_INFO
9874 win_redr_ruler(curwin, always);
9875#endif
9876
9877#ifdef FEAT_TITLE
9878 if (need_maketitle
9879# ifdef FEAT_STL_OPT
9880 || (p_icon && (stl_syntax & STL_IN_ICON))
9881 || (p_title && (stl_syntax & STL_IN_TITLE))
9882# endif
9883 )
9884 maketitle();
9885#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009886#ifdef FEAT_WINDOWS
9887 /* Redraw the tab pages line if needed. */
9888 if (redraw_tabline)
9889 draw_tabline();
9890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891}
9892
9893#ifdef FEAT_CMDL_INFO
9894 static void
9895win_redr_ruler(wp, always)
9896 win_T *wp;
9897 int always;
9898{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009899#define RULER_BUF_LEN 70
9900 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 int row;
9902 int fillchar;
9903 int attr;
9904 int empty_line = FALSE;
9905 colnr_T virtcol;
9906 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009907 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 int o;
9909#ifdef FEAT_VERTSPLIT
9910 int this_ru_col;
9911 int off = 0;
9912 int width = Columns;
9913# define WITH_OFF(x) x
9914# define WITH_WIDTH(x) x
9915#else
9916# define WITH_OFF(x) 0
9917# define WITH_WIDTH(x) Columns
9918# define this_ru_col ru_col
9919#endif
9920
9921 /* If 'ruler' off or redrawing disabled, don't do anything */
9922 if (!p_ru)
9923 return;
9924
9925 /*
9926 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9927 * after deleting lines, before cursor.lnum is corrected.
9928 */
9929 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9930 return;
9931
9932#ifdef FEAT_INS_EXPAND
9933 /* Don't draw the ruler while doing insert-completion, it might overwrite
9934 * the (long) mode message. */
9935# ifdef FEAT_WINDOWS
9936 if (wp == lastwin && lastwin->w_status_height == 0)
9937# endif
9938 if (edit_submode != NULL)
9939 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009940 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9941 if (pum_visible())
9942 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943#endif
9944
9945#ifdef FEAT_STL_OPT
9946 if (*p_ruf)
9947 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009948 int save_called_emsg = called_emsg;
9949
9950 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009952 if (called_emsg)
9953 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009954 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009955 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 return;
9957 }
9958#endif
9959
9960 /*
9961 * Check if not in Insert mode and the line is empty (will show "0-1").
9962 */
9963 if (!(State & INSERT)
9964 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9965 empty_line = TRUE;
9966
9967 /*
9968 * Only draw the ruler when something changed.
9969 */
9970 validate_virtcol_win(wp);
9971 if ( redraw_cmdline
9972 || always
9973 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9974 || wp->w_cursor.col != wp->w_ru_cursor.col
9975 || wp->w_virtcol != wp->w_ru_virtcol
9976#ifdef FEAT_VIRTUALEDIT
9977 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9978#endif
9979 || wp->w_topline != wp->w_ru_topline
9980 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9981#ifdef FEAT_DIFF
9982 || wp->w_topfill != wp->w_ru_topfill
9983#endif
9984 || empty_line != wp->w_ru_empty)
9985 {
9986 cursor_off();
9987#ifdef FEAT_WINDOWS
9988 if (wp->w_status_height)
9989 {
9990 row = W_WINROW(wp) + wp->w_height;
9991 fillchar = fillchar_status(&attr, wp == curwin);
9992# ifdef FEAT_VERTSPLIT
9993 off = W_WINCOL(wp);
9994 width = W_WIDTH(wp);
9995# endif
9996 }
9997 else
9998#endif
9999 {
10000 row = Rows - 1;
10001 fillchar = ' ';
10002 attr = 0;
10003#ifdef FEAT_VERTSPLIT
10004 width = Columns;
10005 off = 0;
10006#endif
10007 }
10008
10009 /* In list mode virtcol needs to be recomputed */
10010 virtcol = wp->w_virtcol;
10011 if (wp->w_p_list && lcs_tab1 == NUL)
10012 {
10013 wp->w_p_list = FALSE;
10014 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10015 wp->w_p_list = TRUE;
10016 }
10017
10018 /*
10019 * Some sprintfs return the length, some return a pointer.
10020 * To avoid portability problems we use strlen() here.
10021 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010022 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10024 ? 0L
10025 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010026 len = STRLEN(buffer);
10027 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10029 (int)virtcol + 1);
10030
10031 /*
10032 * Add a "50%" if there is room for it.
10033 * On the last line, don't print in the last column (scrolls the
10034 * screen up on some terminals).
10035 */
10036 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010037 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 o = i + vim_strsize(buffer + i + 1);
10039#ifdef FEAT_WINDOWS
10040 if (wp->w_status_height == 0) /* can't use last char of screen */
10041#endif
10042 ++o;
10043#ifdef FEAT_VERTSPLIT
10044 this_ru_col = ru_col - (Columns - width);
10045 if (this_ru_col < 0)
10046 this_ru_col = 0;
10047#endif
10048 /* Never use more than half the window/screen width, leave the other
10049 * half for the filename. */
10050 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10051 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10052 if (this_ru_col + o < WITH_WIDTH(width))
10053 {
10054 while (this_ru_col + o < WITH_WIDTH(width))
10055 {
10056#ifdef FEAT_MBYTE
10057 if (has_mbyte)
10058 i += (*mb_char2bytes)(fillchar, buffer + i);
10059 else
10060#endif
10061 buffer[i++] = fillchar;
10062 ++o;
10063 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010064 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 }
10066 /* Truncate at window boundary. */
10067#ifdef FEAT_MBYTE
10068 if (has_mbyte)
10069 {
10070 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010071 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 {
10073 o += (*mb_ptr2cells)(buffer + i);
10074 if (this_ru_col + o > WITH_WIDTH(width))
10075 {
10076 buffer[i] = NUL;
10077 break;
10078 }
10079 }
10080 }
10081 else
10082#endif
10083 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10084 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10085
10086 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10087 i = redraw_cmdline;
10088 screen_fill(row, row + 1,
10089 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10090 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10091 fillchar, fillchar, attr);
10092 /* don't redraw the cmdline because of showing the ruler */
10093 redraw_cmdline = i;
10094 wp->w_ru_cursor = wp->w_cursor;
10095 wp->w_ru_virtcol = wp->w_virtcol;
10096 wp->w_ru_empty = empty_line;
10097 wp->w_ru_topline = wp->w_topline;
10098 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10099#ifdef FEAT_DIFF
10100 wp->w_ru_topfill = wp->w_topfill;
10101#endif
10102 }
10103}
10104#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010105
10106#if defined(FEAT_LINEBREAK) || defined(PROTO)
10107/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010108 * Return the width of the 'number' and 'relativenumber' column.
10109 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010110 * Otherwise it depends on 'numberwidth' and the line count.
10111 */
10112 int
10113number_width(wp)
10114 win_T *wp;
10115{
10116 int n;
10117 linenr_T lnum;
10118
Bram Moolenaar64486672010-05-16 15:46:46 +020010119 if (wp->w_p_nu)
10120 /* 'number' */
10121 lnum = wp->w_buffer->b_ml.ml_line_count;
10122 else
10123 /* 'relativenumber' */
10124 lnum = wp->w_height;
10125
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010126 if (lnum == wp->w_nrwidth_line_count)
10127 return wp->w_nrwidth_width;
10128 wp->w_nrwidth_line_count = lnum;
10129
10130 n = 0;
10131 do
10132 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010133 lnum /= 10;
10134 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010135 } while (lnum > 0);
10136
10137 /* 'numberwidth' gives the minimal width plus one */
10138 if (n < wp->w_p_nuw - 1)
10139 n = wp->w_p_nuw - 1;
10140
10141 wp->w_nrwidth_width = n;
10142 return n;
10143}
10144#endif