blob: 0a2b9a941fd83bdceb34d5c5124fe07c46851434 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
92/*
93 * The attributes that are actually active for writing to the screen.
94 */
95static int screen_attr = 0;
96
97/*
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
100 */
101static int screen_cur_row, screen_cur_col; /* last known cursor position */
102
103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106
107#ifdef FEAT_FOLDING
108static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109#endif
110
111/*
112 * Buffer for one screen line (characters and attributes).
113 */
114static schar_T *current_ScreenLine;
115
116static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000117static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_FOLDING
119static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000123static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125#ifdef FEAT_RIGHTLEFT
126static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128#else
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_VERTSPLIT
133static void draw_vsep_win __ARGS((win_T *wp, int row));
134#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000135#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000136static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000139#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void start_search_hl __ARGS((void));
141static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200142static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
144static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
145#endif
146static void screen_start_highlight __ARGS((int attr));
147static void screen_char __ARGS((unsigned off, int row, int col));
148#ifdef FEAT_MBYTE
149static void screen_char_2 __ARGS((unsigned off, int row, int col));
150#endif
151static void screenclear2 __ARGS((void));
152static void lineclear __ARGS((unsigned off, int width));
153static void lineinvalid __ARGS((unsigned off, int width));
154#ifdef FEAT_VERTSPLIT
155static void linecopy __ARGS((int to, int from, win_T *wp));
156static void redraw_block __ARGS((int row, int end, win_T *wp));
157#endif
158static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
159static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000162static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
165static int fillchar_status __ARGS((int *attr, int is_curwin));
166#endif
167#ifdef FEAT_VERTSPLIT
168static int fillchar_vsep __ARGS((int *attr));
169#endif
170#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000171static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_CMDL_INFO
174static void win_redr_ruler __ARGS((win_T *wp, int always));
175#endif
176
177#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
178/* Ugly global: overrule attribute used by screen_char() */
179static int screen_char_attr = 0;
180#endif
181
182/*
183 * Redraw the current window later, with update_screen(type).
184 * Set must_redraw only if not already set to a higher value.
185 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
186 */
187 void
188redraw_later(type)
189 int type;
190{
191 redraw_win_later(curwin, type);
192}
193
194 void
195redraw_win_later(wp, type)
196 win_T *wp;
197 int type;
198{
199 if (wp->w_redr_type < type)
200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
214redraw_later_clear()
215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
232redraw_all_later(type)
233 int type;
234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
247redraw_curbuf_later(type)
248 int type;
249{
250 redraw_buf_later(curbuf, type);
251}
252
253 void
254redraw_buf_later(buf, type)
255 buf_T *buf;
256 int type;
257{
258 win_T *wp;
259
260 FOR_ALL_WINDOWS(wp)
261 {
262 if (wp->w_buffer == buf)
263 redraw_win_later(wp, type);
264 }
265}
266
267/*
268 * Changed something in the current window, at buffer line "lnum", that
269 * requires that line and possibly other lines to be redrawn.
270 * Used when entering/leaving Insert mode with the cursor on a folded line.
271 * Used to remove the "$" from a change command.
272 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
273 * may become invalid and the whole window will have to be redrawn.
274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 void
276redrawWinline(lnum, invalid)
277 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000278 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280#ifdef FEAT_FOLDING
281 int i;
282#endif
283
284 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
285 curwin->w_redraw_top = lnum;
286 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
287 curwin->w_redraw_bot = lnum;
288 redraw_later(VALID);
289
290#ifdef FEAT_FOLDING
291 if (invalid)
292 {
293 /* A w_lines[] entry for this lnum has become invalid. */
294 i = find_wl_entry(curwin, lnum);
295 if (i >= 0)
296 curwin->w_lines[i].wl_valid = FALSE;
297 }
298#endif
299}
300
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200301#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200302 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * update all windows that are editing the current buffer
305 */
306 void
307update_curbuf(type)
308 int type;
309{
310 redraw_curbuf_later(type);
311 update_screen(type);
312}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * update_screen()
317 *
318 * Based on the current value of curwin->w_topline, transfer a screenfull
319 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
320 */
321 void
322update_screen(type)
323 int type;
324{
325 win_T *wp;
326 static int did_intro = FALSE;
327#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
328 int did_one;
329#endif
330
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000331 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 if (!screen_valid(TRUE))
333 return;
334
335 if (must_redraw)
336 {
337 if (type < must_redraw) /* use maximal type */
338 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000339
340 /* must_redraw is reset here, so that when we run into some weird
341 * reason to redraw while busy redrawing (e.g., asynchronous
342 * scrolling), or update_topline() in win_update() will cause a
343 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 must_redraw = 0;
345 }
346
347 /* Need to update w_lines[]. */
348 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
349 type = NOT_VALID;
350
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000351 /* Postpone the redrawing when it's not needed and when being called
352 * recursively. */
353 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 redraw_later(type); /* remember type for next time */
356 must_redraw = type;
357 if (type > INVERTED_ALL)
358 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
359 return;
360 }
361
362 updating_screen = TRUE;
363#ifdef FEAT_SYN_HL
364 ++display_tick; /* let syntax code know we're in a next round of
365 * display updating */
366#endif
367
368 /*
369 * if the screen was scrolled up when displaying a message, scroll it down
370 */
371 if (msg_scrolled)
372 {
373 clear_cmdline = TRUE;
374 if (msg_scrolled > Rows - 5) /* clearing is faster */
375 type = CLEAR;
376 else if (type != CLEAR)
377 {
378 check_for_delay(FALSE);
379 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
380 type = CLEAR;
381 FOR_ALL_WINDOWS(wp)
382 {
383 if (W_WINROW(wp) < msg_scrolled)
384 {
385 if (W_WINROW(wp) + wp->w_height > msg_scrolled
386 && wp->w_redr_type < REDRAW_TOP
387 && wp->w_lines_valid > 0
388 && wp->w_topline == wp->w_lines[0].wl_lnum)
389 {
390 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
391 wp->w_redr_type = REDRAW_TOP;
392 }
393 else
394 {
395 wp->w_redr_type = NOT_VALID;
396#ifdef FEAT_WINDOWS
397 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
398 <= msg_scrolled)
399 wp->w_redr_status = TRUE;
400#endif
401 }
402 }
403 }
404 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000406 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409 msg_scrolled = 0;
410 need_wait_return = FALSE;
411 }
412
413 /* reset cmdline_row now (may have been changed temporarily) */
414 compute_cmdrow();
415
416 /* Check for changed highlighting */
417 if (need_highlight_changed)
418 highlight_changed();
419
420 if (type == CLEAR) /* first clear screen */
421 {
422 screenclear(); /* will reset clear_cmdline */
423 type = NOT_VALID;
424 }
425
426 if (clear_cmdline) /* going to clear cmdline (done below) */
427 check_for_delay(FALSE);
428
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000429#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200430 /* Force redraw when width of 'number' or 'relativenumber' column
431 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
434 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 curwin->w_redr_type = NOT_VALID;
436#endif
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 /*
439 * Only start redrawing if there is really something to do.
440 */
441 if (type == INVERTED)
442 update_curswant();
443 if (curwin->w_redr_type < type
444 && !((type == VALID
445 && curwin->w_lines[0].wl_valid
446#ifdef FEAT_DIFF
447 && curwin->w_topfill == curwin->w_old_topfill
448 && curwin->w_botfill == curwin->w_old_botfill
449#endif
450 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
451#ifdef FEAT_VISUAL
452 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000453 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
455 && curwin->w_old_visual_mode == VIsual_mode
456 && (curwin->w_valid & VALID_VIRTCOL)
457 && curwin->w_old_curswant == curwin->w_curswant)
458#endif
459 ))
460 curwin->w_redr_type = type;
461
Bram Moolenaar5a305422006-04-28 22:38:25 +0000462#ifdef FEAT_WINDOWS
463 /* Redraw the tab pages line if needed. */
464 if (redraw_tabline || type >= NOT_VALID)
465 draw_tabline();
466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#ifdef FEAT_SYN_HL
469 /*
470 * Correct stored syntax highlighting info for changes in each displayed
471 * buffer. Each buffer must only be done once.
472 */
473 FOR_ALL_WINDOWS(wp)
474 {
475 if (wp->w_buffer->b_mod_set)
476 {
477# ifdef FEAT_WINDOWS
478 win_T *wwp;
479
480 /* Check if we already did this buffer. */
481 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
482 if (wwp->w_buffer == wp->w_buffer)
483 break;
484# endif
485 if (
486# ifdef FEAT_WINDOWS
487 wwp == wp &&
488# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200489 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 syn_stack_apply_changes(wp->w_buffer);
491 }
492 }
493#endif
494
495 /*
496 * Go from top to bottom through the windows, redrawing the ones that need
497 * it.
498 */
499#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
500 did_one = FALSE;
501#endif
502#ifdef FEAT_SEARCH_EXTRA
503 search_hl.rm.regprog = NULL;
504#endif
505 FOR_ALL_WINDOWS(wp)
506 {
507 if (wp->w_redr_type != 0)
508 {
509 cursor_off();
510#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
511 if (!did_one)
512 {
513 did_one = TRUE;
514# ifdef FEAT_SEARCH_EXTRA
515 start_search_hl();
516# endif
517# ifdef FEAT_CLIPBOARD
518 /* When Visual area changed, may have to update selection. */
519 if (clip_star.available && clip_isautosel())
520 clip_update_selection();
521# endif
522#ifdef FEAT_GUI
523 /* Remove the cursor before starting to do anything, because
524 * scrolling may make it difficult to redraw the text under
525 * it. */
526 if (gui.in_use)
527 gui_undraw_cursor();
528#endif
529 }
530#endif
531 win_update(wp);
532 }
533
534#ifdef FEAT_WINDOWS
535 /* redraw status line after the window to minimize cursor movement */
536 if (wp->w_redr_status)
537 {
538 cursor_off();
539 win_redr_status(wp);
540 }
541#endif
542 }
543#if defined(FEAT_SEARCH_EXTRA)
544 end_search_hl();
545#endif
546
547#ifdef FEAT_WINDOWS
548 /* Reset b_mod_set flags. Going through all windows is probably faster
549 * than going through all buffers (there could be many buffers). */
550 for (wp = firstwin; wp != NULL; wp = wp->w_next)
551 wp->w_buffer->b_mod_set = FALSE;
552#else
553 curbuf->b_mod_set = FALSE;
554#endif
555
556 updating_screen = FALSE;
557#ifdef FEAT_GUI
558 gui_may_resize_shell();
559#endif
560
561 /* Clear or redraw the command line. Done last, because scrolling may
562 * mess up the command line. */
563 if (clear_cmdline || redraw_cmdline)
564 showmode();
565
566 /* May put up an introductory message when not editing a file */
567 if (!did_intro && bufempty()
568 && curbuf->b_fname == NULL
569#ifdef FEAT_WINDOWS
570 && firstwin->w_next == NULL
571#endif
572 && vim_strchr(p_shm, SHM_INTRO) == NULL)
573 intro_message(FALSE);
574 did_intro = TRUE;
575
576#ifdef FEAT_GUI
577 /* Redraw the cursor and update the scrollbars when all screen updating is
578 * done. */
579 if (gui.in_use)
580 {
581 out_flush(); /* required before updating the cursor */
582 if (did_one)
583 gui_update_cursor(FALSE, FALSE);
584 gui_update_scrollbars(FALSE);
585 }
586#endif
587}
588
Bram Moolenaar860cae12010-06-05 23:22:07 +0200589#if defined(FEAT_CONCEAL) || defined(PROTO)
590 void
591update_single_line(wp, lnum)
592 win_T *wp;
593 linenr_T lnum;
594{
595 int row;
596 int j;
597
598 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200599 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200600 {
601# ifdef FEAT_GUI
602 /* Remove the cursor before starting to do anything, because scrolling
603 * may make it difficult to redraw the text under it. */
604 if (gui.in_use)
605 gui_undraw_cursor();
606# endif
607 row = 0;
608 for (j = 0; j < wp->w_lines_valid; ++j)
609 {
610 if (lnum == wp->w_lines[j].wl_lnum)
611 {
612 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200613# ifdef FEAT_SEARCH_EXTRA
614 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200615 start_search_hl();
616 prepare_search_hl(wp, lnum);
617# endif
618 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
619# if defined(FEAT_SEARCH_EXTRA)
620 end_search_hl();
621# endif
622 break;
623 }
624 row += wp->w_lines[j].wl_size;
625 }
626# ifdef FEAT_GUI
627 /* Redraw the cursor */
628 if (gui.in_use)
629 {
630 out_flush(); /* required before updating the cursor */
631 gui_update_cursor(FALSE, FALSE);
632 }
633# endif
634 }
635}
636#endif
637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
639static void update_prepare __ARGS((void));
640static void update_finish __ARGS((void));
641
642/*
643 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000644 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 */
646 static void
647update_prepare()
648{
649 cursor_off();
650 updating_screen = TRUE;
651#ifdef FEAT_GUI
652 /* Remove the cursor before starting to do anything, because scrolling may
653 * make it difficult to redraw the text under it. */
654 if (gui.in_use)
655 gui_undraw_cursor();
656#endif
657#ifdef FEAT_SEARCH_EXTRA
658 start_search_hl();
659#endif
660}
661
662/*
663 * Finish updating one or more windows.
664 */
665 static void
666update_finish()
667{
668 if (redraw_cmdline)
669 showmode();
670
671# ifdef FEAT_SEARCH_EXTRA
672 end_search_hl();
673# endif
674
675 updating_screen = FALSE;
676
677# ifdef FEAT_GUI
678 gui_may_resize_shell();
679
680 /* Redraw the cursor and update the scrollbars when all screen updating is
681 * done. */
682 if (gui.in_use)
683 {
684 out_flush(); /* required before updating the cursor */
685 gui_update_cursor(FALSE, FALSE);
686 gui_update_scrollbars(FALSE);
687 }
688# endif
689}
690#endif
691
692#if defined(FEAT_SIGNS) || defined(PROTO)
693 void
694update_debug_sign(buf, lnum)
695 buf_T *buf;
696 linenr_T lnum;
697{
698 win_T *wp;
699 int doit = FALSE;
700
701# ifdef FEAT_FOLDING
702 win_foldinfo.fi_level = 0;
703# endif
704
705 /* update/delete a specific mark */
706 FOR_ALL_WINDOWS(wp)
707 {
708 if (buf != NULL && lnum > 0)
709 {
710 if (wp->w_buffer == buf && lnum >= wp->w_topline
711 && lnum < wp->w_botline)
712 {
713 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
714 wp->w_redraw_top = lnum;
715 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
716 wp->w_redraw_bot = lnum;
717 redraw_win_later(wp, VALID);
718 }
719 }
720 else
721 redraw_win_later(wp, VALID);
722 if (wp->w_redr_type != 0)
723 doit = TRUE;
724 }
725
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000726 /* Return when there is nothing to do or screen updating already
727 * happening. */
728 if (!doit || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 return;
730
731 /* update all windows that need updating */
732 update_prepare();
733
734# ifdef FEAT_WINDOWS
735 for (wp = firstwin; wp; wp = wp->w_next)
736 {
737 if (wp->w_redr_type != 0)
738 win_update(wp);
739 if (wp->w_redr_status)
740 win_redr_status(wp);
741 }
742# else
743 if (curwin->w_redr_type != 0)
744 win_update(curwin);
745# endif
746
747 update_finish();
748}
749#endif
750
751
752#if defined(FEAT_GUI) || defined(PROTO)
753/*
754 * Update a single window, its status line and maybe the command line msg.
755 * Used for the GUI scrollbar.
756 */
757 void
758updateWindow(wp)
759 win_T *wp;
760{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000761 /* return if already busy updating */
762 if (updating_screen)
763 return;
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 update_prepare();
766
767#ifdef FEAT_CLIPBOARD
768 /* When Visual area changed, may have to update selection. */
769 if (clip_star.available && clip_isautosel())
770 clip_update_selection();
771#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000776 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000777 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000778 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (wp->w_redr_status
781# ifdef FEAT_CMDL_INFO
782 || p_ru
783# endif
784# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000785 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786# endif
787 )
788 win_redr_status(wp);
789#endif
790
791 update_finish();
792}
793#endif
794
795/*
796 * Update a single window.
797 *
798 * This may cause the windows below it also to be redrawn (when clearing the
799 * screen or scrolling lines).
800 *
801 * How the window is redrawn depends on wp->w_redr_type. Each type also
802 * implies the one below it.
803 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000804 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
806 * INVERTED redraw the changed part of the Visual area
807 * INVERTED_ALL redraw the whole Visual area
808 * VALID 1. scroll up/down to adjust for a changed w_topline
809 * 2. update lines at the top when scrolled down
810 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000811 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 * b_mod_top and b_mod_bot.
813 * - if wp->w_redraw_top non-zero, redraw lines between
814 * wp->w_redraw_top and wp->w_redr_bot.
815 * - continue redrawing when syntax status is invalid.
816 * 4. if scrolled up, update lines at the bottom.
817 * This results in three areas that may need updating:
818 * top: from first row to top_end (when scrolled down)
819 * mid: from mid_start to mid_end (update inversion or changed text)
820 * bot: from bot_start to last row (when scrolled up)
821 */
822 static void
823win_update(wp)
824 win_T *wp;
825{
826 buf_T *buf = wp->w_buffer;
827 int type;
828 int top_end = 0; /* Below last row of the top area that needs
829 updating. 0 when no top area updating. */
830 int mid_start = 999;/* first row of the mid area that needs
831 updating. 999 when no mid area updating. */
832 int mid_end = 0; /* Below last row of the mid area that needs
833 updating. 0 when no mid area updating. */
834 int bot_start = 999;/* first row of the bot area that needs
835 updating. 999 when no bot area updating */
836#ifdef FEAT_VISUAL
837 int scrolled_down = FALSE; /* TRUE when scrolled down when
838 w_topline got smaller a bit */
839#endif
840#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000841 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 int top_to_mod = FALSE; /* redraw above mod_top */
843#endif
844
845 int row; /* current window row to display */
846 linenr_T lnum; /* current buffer lnum to display */
847 int idx; /* current index in w_lines[] */
848 int srow; /* starting row of the current line */
849
850 int eof = FALSE; /* if TRUE, we hit the end of the file */
851 int didline = FALSE; /* if TRUE, we finished the last line */
852 int i;
853 long j;
854 static int recursive = FALSE; /* being called recursively */
855 int old_botline = wp->w_botline;
856#ifdef FEAT_FOLDING
857 long fold_count;
858#endif
859#ifdef FEAT_SYN_HL
860 /* remember what happened to the previous line, to know if
861 * check_visual_highlight() can be used */
862#define DID_NONE 1 /* didn't update a line */
863#define DID_LINE 2 /* updated a normal line */
864#define DID_FOLD 3 /* updated a folded line */
865 int did_update = DID_NONE;
866 linenr_T syntax_last_parsed = 0; /* last parsed text line */
867#endif
868 linenr_T mod_top = 0;
869 linenr_T mod_bot = 0;
870#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
871 int save_got_int;
872#endif
873
874 type = wp->w_redr_type;
875
876 if (type == NOT_VALID)
877 {
878#ifdef FEAT_WINDOWS
879 wp->w_redr_status = TRUE;
880#endif
881 wp->w_lines_valid = 0;
882 }
883
884 /* Window is zero-height: nothing to draw. */
885 if (wp->w_height == 0)
886 {
887 wp->w_redr_type = 0;
888 return;
889 }
890
891#ifdef FEAT_VERTSPLIT
892 /* Window is zero-width: Only need to draw the separator. */
893 if (wp->w_width == 0)
894 {
895 /* draw the vertical separator right of this window */
896 draw_vsep_win(wp, 0);
897 wp->w_redr_type = 0;
898 return;
899 }
900#endif
901
902#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200903 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904#endif
905
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000906#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200907 /* Force redraw when width of 'number' or 'relativenumber' column
908 * changes. */
909 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000910 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000911 {
912 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000913 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000914 }
915 else
916#endif
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
919 {
920 /*
921 * When there are both inserted/deleted lines and specific lines to be
922 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
923 * everything (only happens when redrawing is off for while).
924 */
925 type = NOT_VALID;
926 }
927 else
928 {
929 /*
930 * Set mod_top to the first line that needs displaying because of
931 * changes. Set mod_bot to the first line after the changes.
932 */
933 mod_top = wp->w_redraw_top;
934 if (wp->w_redraw_bot != 0)
935 mod_bot = wp->w_redraw_bot + 1;
936 else
937 mod_bot = 0;
938 wp->w_redraw_top = 0; /* reset for next time */
939 wp->w_redraw_bot = 0;
940 if (buf->b_mod_set)
941 {
942 if (mod_top == 0 || mod_top > buf->b_mod_top)
943 {
944 mod_top = buf->b_mod_top;
945#ifdef FEAT_SYN_HL
946 /* Need to redraw lines above the change that may be included
947 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200950 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (mod_top < 1)
952 mod_top = 1;
953 }
954#endif
955 }
956 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
957 mod_bot = buf->b_mod_bot;
958
959#ifdef FEAT_SEARCH_EXTRA
960 /* When 'hlsearch' is on and using a multi-line search pattern, a
961 * change in one line may make the Search highlighting in a
962 * previous line invalid. Simple solution: redraw all visible
963 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000964 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000966 if (search_hl.rm.regprog != NULL
967 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000969 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000970 {
971 cur = wp->w_match_head;
972 while (cur != NULL)
973 {
974 if (cur->match.regprog != NULL
975 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000976 {
977 top_to_mod = TRUE;
978 break;
979 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000980 cur = cur->next;
981 }
982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#endif
984 }
985#ifdef FEAT_FOLDING
986 if (mod_top != 0 && hasAnyFolding(wp))
987 {
988 linenr_T lnumt, lnumb;
989
990 /*
991 * A change in a line can cause lines above it to become folded or
992 * unfolded. Find the top most buffer line that may be affected.
993 * If the line was previously folded and displayed, get the first
994 * line of that fold. If the line is folded now, get the first
995 * folded line. Use the minimum of these two.
996 */
997
998 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
999 * the line below it. If there is no valid entry, use w_topline.
1000 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1001 * to this line. If there is no valid entry, use MAXLNUM. */
1002 lnumt = wp->w_topline;
1003 lnumb = MAXLNUM;
1004 for (i = 0; i < wp->w_lines_valid; ++i)
1005 if (wp->w_lines[i].wl_valid)
1006 {
1007 if (wp->w_lines[i].wl_lastlnum < mod_top)
1008 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1009 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1010 {
1011 lnumb = wp->w_lines[i].wl_lnum;
1012 /* When there is a fold column it might need updating
1013 * in the next line ("J" just above an open fold). */
1014 if (wp->w_p_fdc > 0)
1015 ++lnumb;
1016 }
1017 }
1018
1019 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1020 if (mod_top > lnumt)
1021 mod_top = lnumt;
1022
1023 /* Now do the same for the bottom line (one above mod_bot). */
1024 --mod_bot;
1025 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1026 ++mod_bot;
1027 if (mod_bot < lnumb)
1028 mod_bot = lnumb;
1029 }
1030#endif
1031
1032 /* When a change starts above w_topline and the end is below
1033 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001034 * If the end of the change is above w_topline: do like no change was
1035 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (mod_top != 0 && mod_top < wp->w_topline)
1037 {
1038 if (mod_bot > wp->w_topline)
1039 mod_top = wp->w_topline;
1040#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001041 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 top_end = 1;
1043#endif
1044 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001045
1046 /* When line numbers are displayed need to redraw all lines below
1047 * inserted/deleted lines. */
1048 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1049 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 }
1051
1052 /*
1053 * When only displaying the lines at the top, set top_end. Used when
1054 * window has scrolled down for msg_scrolled.
1055 */
1056 if (type == REDRAW_TOP)
1057 {
1058 j = 0;
1059 for (i = 0; i < wp->w_lines_valid; ++i)
1060 {
1061 j += wp->w_lines[i].wl_size;
1062 if (j >= wp->w_upd_rows)
1063 {
1064 top_end = j;
1065 break;
1066 }
1067 }
1068 if (top_end == 0)
1069 /* not found (cannot happen?): redraw everything */
1070 type = NOT_VALID;
1071 else
1072 /* top area defined, the rest is VALID */
1073 type = VALID;
1074 }
1075
Bram Moolenaar367329b2007-08-30 11:53:22 +00001076 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001077 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1078 * non-zero and thus not FALSE) will indicate that screenclear() was not
1079 * called. */
1080 if (screen_cleared)
1081 screen_cleared = MAYBE;
1082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 /*
1084 * If there are no changes on the screen that require a complete redraw,
1085 * handle three cases:
1086 * 1: we are off the top of the screen by a few lines: scroll down
1087 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1088 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1089 * w_lines[] that needs updating.
1090 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001091 if ((type == VALID || type == SOME_VALID
1092 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#ifdef FEAT_DIFF
1094 && !wp->w_botfill && !wp->w_old_botfill
1095#endif
1096 )
1097 {
1098 if (mod_top != 0 && wp->w_topline == mod_top)
1099 {
1100 /*
1101 * w_topline is the first changed line, the scrolling will be done
1102 * further down.
1103 */
1104 }
1105 else if (wp->w_lines[0].wl_valid
1106 && (wp->w_topline < wp->w_lines[0].wl_lnum
1107#ifdef FEAT_DIFF
1108 || (wp->w_topline == wp->w_lines[0].wl_lnum
1109 && wp->w_topfill > wp->w_old_topfill)
1110#endif
1111 ))
1112 {
1113 /*
1114 * New topline is above old topline: May scroll down.
1115 */
1116#ifdef FEAT_FOLDING
1117 if (hasAnyFolding(wp))
1118 {
1119 linenr_T ln;
1120
1121 /* count the number of lines we are off, counting a sequence
1122 * of folded lines as one */
1123 j = 0;
1124 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1125 {
1126 ++j;
1127 if (j >= wp->w_height - 2)
1128 break;
1129 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1130 }
1131 }
1132 else
1133#endif
1134 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1135 if (j < wp->w_height - 2) /* not too far off */
1136 {
1137 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1138#ifdef FEAT_DIFF
1139 /* insert extra lines for previously invisible filler lines */
1140 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1141 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1142 - wp->w_old_topfill;
1143#endif
1144 if (i < wp->w_height - 2) /* less than a screen off */
1145 {
1146 /*
1147 * Try to insert the correct number of lines.
1148 * If not the last window, delete the lines at the bottom.
1149 * win_ins_lines may fail when the terminal can't do it.
1150 */
1151 if (i > 0)
1152 check_for_delay(FALSE);
1153 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1154 {
1155 if (wp->w_lines_valid != 0)
1156 {
1157 /* Need to update rows that are new, stop at the
1158 * first one that scrolled down. */
1159 top_end = i;
1160#ifdef FEAT_VISUAL
1161 scrolled_down = TRUE;
1162#endif
1163
1164 /* Move the entries that were scrolled, disable
1165 * the entries for the lines to be redrawn. */
1166 if ((wp->w_lines_valid += j) > wp->w_height)
1167 wp->w_lines_valid = wp->w_height;
1168 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1169 wp->w_lines[idx] = wp->w_lines[idx - j];
1170 while (idx >= 0)
1171 wp->w_lines[idx--].wl_valid = FALSE;
1172 }
1173 }
1174 else
1175 mid_start = 0; /* redraw all lines */
1176 }
1177 else
1178 mid_start = 0; /* redraw all lines */
1179 }
1180 else
1181 mid_start = 0; /* redraw all lines */
1182 }
1183 else
1184 {
1185 /*
1186 * New topline is at or below old topline: May scroll up.
1187 * When topline didn't change, find first entry in w_lines[] that
1188 * needs updating.
1189 */
1190
1191 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1192 j = -1;
1193 row = 0;
1194 for (i = 0; i < wp->w_lines_valid; i++)
1195 {
1196 if (wp->w_lines[i].wl_valid
1197 && wp->w_lines[i].wl_lnum == wp->w_topline)
1198 {
1199 j = i;
1200 break;
1201 }
1202 row += wp->w_lines[i].wl_size;
1203 }
1204 if (j == -1)
1205 {
1206 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1207 * lines */
1208 mid_start = 0;
1209 }
1210 else
1211 {
1212 /*
1213 * Try to delete the correct number of lines.
1214 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1215 */
1216#ifdef FEAT_DIFF
1217 /* If the topline didn't change, delete old filler lines,
1218 * otherwise delete filler lines of the new topline... */
1219 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1220 row += wp->w_old_topfill;
1221 else
1222 row += diff_check_fill(wp, wp->w_topline);
1223 /* ... but don't delete new filler lines. */
1224 row -= wp->w_topfill;
1225#endif
1226 if (row > 0)
1227 {
1228 check_for_delay(FALSE);
1229 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1230 bot_start = wp->w_height - row;
1231 else
1232 mid_start = 0; /* redraw all lines */
1233 }
1234 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1235 {
1236 /*
1237 * Skip the lines (below the deleted lines) that are still
1238 * valid and don't need redrawing. Copy their info
1239 * upwards, to compensate for the deleted lines. Set
1240 * bot_start to the first row that needs redrawing.
1241 */
1242 bot_start = 0;
1243 idx = 0;
1244 for (;;)
1245 {
1246 wp->w_lines[idx] = wp->w_lines[j];
1247 /* stop at line that didn't fit, unless it is still
1248 * valid (no lines deleted) */
1249 if (row > 0 && bot_start + row
1250 + (int)wp->w_lines[j].wl_size > wp->w_height)
1251 {
1252 wp->w_lines_valid = idx + 1;
1253 break;
1254 }
1255 bot_start += wp->w_lines[idx++].wl_size;
1256
1257 /* stop at the last valid entry in w_lines[].wl_size */
1258 if (++j >= wp->w_lines_valid)
1259 {
1260 wp->w_lines_valid = idx;
1261 break;
1262 }
1263 }
1264#ifdef FEAT_DIFF
1265 /* Correct the first entry for filler lines at the top
1266 * when it won't get updated below. */
1267 if (wp->w_p_diff && bot_start > 0)
1268 wp->w_lines[0].wl_size =
1269 plines_win_nofill(wp, wp->w_topline, TRUE)
1270 + wp->w_topfill;
1271#endif
1272 }
1273 }
1274 }
1275
1276 /* When starting redraw in the first line, redraw all lines. When
1277 * there is only one window it's probably faster to clear the screen
1278 * first. */
1279 if (mid_start == 0)
1280 {
1281 mid_end = wp->w_height;
1282 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001283 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001284 /* Clear the screen when it was not done by win_del_lines() or
1285 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1286 * then. */
1287 if (screen_cleared != TRUE)
1288 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001289#ifdef FEAT_WINDOWS
1290 /* The screen was cleared, redraw the tab pages line. */
1291 if (redraw_tabline)
1292 draw_tabline();
1293#endif
1294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001296
1297 /* When win_del_lines() or win_ins_lines() caused the screen to be
1298 * cleared (only happens for the first window) or when screenclear()
1299 * was called directly above, "must_redraw" will have been set to
1300 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1301 if (screen_cleared == TRUE)
1302 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 else
1305 {
1306 /* Not VALID or INVERTED: redraw all lines. */
1307 mid_start = 0;
1308 mid_end = wp->w_height;
1309 }
1310
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001311 if (type == SOME_VALID)
1312 {
1313 /* SOME_VALID: redraw all lines. */
1314 mid_start = 0;
1315 mid_end = wp->w_height;
1316 type = NOT_VALID;
1317 }
1318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319#ifdef FEAT_VISUAL
1320 /* check if we are updating or removing the inverted part */
1321 if ((VIsual_active && buf == curwin->w_buffer)
1322 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1323 {
1324 linenr_T from, to;
1325
1326 if (VIsual_active)
1327 {
1328 if (VIsual_active
1329 && (VIsual_mode != wp->w_old_visual_mode
1330 || type == INVERTED_ALL))
1331 {
1332 /*
1333 * If the type of Visual selection changed, redraw the whole
1334 * selection. Also when the ownership of the X selection is
1335 * gained or lost.
1336 */
1337 if (curwin->w_cursor.lnum < VIsual.lnum)
1338 {
1339 from = curwin->w_cursor.lnum;
1340 to = VIsual.lnum;
1341 }
1342 else
1343 {
1344 from = VIsual.lnum;
1345 to = curwin->w_cursor.lnum;
1346 }
1347 /* redraw more when the cursor moved as well */
1348 if (wp->w_old_cursor_lnum < from)
1349 from = wp->w_old_cursor_lnum;
1350 if (wp->w_old_cursor_lnum > to)
1351 to = wp->w_old_cursor_lnum;
1352 if (wp->w_old_visual_lnum < from)
1353 from = wp->w_old_visual_lnum;
1354 if (wp->w_old_visual_lnum > to)
1355 to = wp->w_old_visual_lnum;
1356 }
1357 else
1358 {
1359 /*
1360 * Find the line numbers that need to be updated: The lines
1361 * between the old cursor position and the current cursor
1362 * position. Also check if the Visual position changed.
1363 */
1364 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1365 {
1366 from = curwin->w_cursor.lnum;
1367 to = wp->w_old_cursor_lnum;
1368 }
1369 else
1370 {
1371 from = wp->w_old_cursor_lnum;
1372 to = curwin->w_cursor.lnum;
1373 if (from == 0) /* Visual mode just started */
1374 from = to;
1375 }
1376
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001377 if (VIsual.lnum != wp->w_old_visual_lnum
1378 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
1380 if (wp->w_old_visual_lnum < from
1381 && wp->w_old_visual_lnum != 0)
1382 from = wp->w_old_visual_lnum;
1383 if (wp->w_old_visual_lnum > to)
1384 to = wp->w_old_visual_lnum;
1385 if (VIsual.lnum < from)
1386 from = VIsual.lnum;
1387 if (VIsual.lnum > to)
1388 to = VIsual.lnum;
1389 }
1390 }
1391
1392 /*
1393 * If in block mode and changed column or curwin->w_curswant:
1394 * update all lines.
1395 * First compute the actual start and end column.
1396 */
1397 if (VIsual_mode == Ctrl_V)
1398 {
1399 colnr_T fromc, toc;
1400
1401 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1402 ++toc;
1403 if (curwin->w_curswant == MAXCOL)
1404 toc = MAXCOL;
1405
1406 if (fromc != wp->w_old_cursor_fcol
1407 || toc != wp->w_old_cursor_lcol)
1408 {
1409 if (from > VIsual.lnum)
1410 from = VIsual.lnum;
1411 if (to < VIsual.lnum)
1412 to = VIsual.lnum;
1413 }
1414 wp->w_old_cursor_fcol = fromc;
1415 wp->w_old_cursor_lcol = toc;
1416 }
1417 }
1418 else
1419 {
1420 /* Use the line numbers of the old Visual area. */
1421 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1422 {
1423 from = wp->w_old_cursor_lnum;
1424 to = wp->w_old_visual_lnum;
1425 }
1426 else
1427 {
1428 from = wp->w_old_visual_lnum;
1429 to = wp->w_old_cursor_lnum;
1430 }
1431 }
1432
1433 /*
1434 * There is no need to update lines above the top of the window.
1435 */
1436 if (from < wp->w_topline)
1437 from = wp->w_topline;
1438
1439 /*
1440 * If we know the value of w_botline, use it to restrict the update to
1441 * the lines that are visible in the window.
1442 */
1443 if (wp->w_valid & VALID_BOTLINE)
1444 {
1445 if (from >= wp->w_botline)
1446 from = wp->w_botline - 1;
1447 if (to >= wp->w_botline)
1448 to = wp->w_botline - 1;
1449 }
1450
1451 /*
1452 * Find the minimal part to be updated.
1453 * Watch out for scrolling that made entries in w_lines[] invalid.
1454 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1455 * top_end; need to redraw from top_end to the "to" line.
1456 * A middle mouse click with a Visual selection may change the text
1457 * above the Visual area and reset wl_valid, do count these for
1458 * mid_end (in srow).
1459 */
1460 if (mid_start > 0)
1461 {
1462 lnum = wp->w_topline;
1463 idx = 0;
1464 srow = 0;
1465 if (scrolled_down)
1466 mid_start = top_end;
1467 else
1468 mid_start = 0;
1469 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1470 {
1471 if (wp->w_lines[idx].wl_valid)
1472 mid_start += wp->w_lines[idx].wl_size;
1473 else if (!scrolled_down)
1474 srow += wp->w_lines[idx].wl_size;
1475 ++idx;
1476# ifdef FEAT_FOLDING
1477 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1478 lnum = wp->w_lines[idx].wl_lnum;
1479 else
1480# endif
1481 ++lnum;
1482 }
1483 srow += mid_start;
1484 mid_end = wp->w_height;
1485 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1486 {
1487 if (wp->w_lines[idx].wl_valid
1488 && wp->w_lines[idx].wl_lnum >= to + 1)
1489 {
1490 /* Only update until first row of this line */
1491 mid_end = srow;
1492 break;
1493 }
1494 srow += wp->w_lines[idx].wl_size;
1495 }
1496 }
1497 }
1498
1499 if (VIsual_active && buf == curwin->w_buffer)
1500 {
1501 wp->w_old_visual_mode = VIsual_mode;
1502 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1503 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001504 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 wp->w_old_curswant = curwin->w_curswant;
1506 }
1507 else
1508 {
1509 wp->w_old_visual_mode = 0;
1510 wp->w_old_cursor_lnum = 0;
1511 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001512 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514#endif /* FEAT_VISUAL */
1515
1516#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1517 /* reset got_int, otherwise regexp won't work */
1518 save_got_int = got_int;
1519 got_int = 0;
1520#endif
1521#ifdef FEAT_FOLDING
1522 win_foldinfo.fi_level = 0;
1523#endif
1524
1525 /*
1526 * Update all the window rows.
1527 */
1528 idx = 0; /* first entry in w_lines[].wl_size */
1529 row = 0;
1530 srow = 0;
1531 lnum = wp->w_topline; /* first line shown in window */
1532 for (;;)
1533 {
1534 /* stop updating when reached the end of the window (check for _past_
1535 * the end of the window is at the end of the loop) */
1536 if (row == wp->w_height)
1537 {
1538 didline = TRUE;
1539 break;
1540 }
1541
1542 /* stop updating when hit the end of the file */
1543 if (lnum > buf->b_ml.ml_line_count)
1544 {
1545 eof = TRUE;
1546 break;
1547 }
1548
1549 /* Remember the starting row of the line that is going to be dealt
1550 * with. It is used further down when the line doesn't fit. */
1551 srow = row;
1552
1553 /*
1554 * Update a line when it is in an area that needs updating, when it
1555 * has changes or w_lines[idx] is invalid.
1556 * bot_start may be halfway a wrapped line after using
1557 * win_del_lines(), check if the current line includes it.
1558 * When syntax folding is being used, the saved syntax states will
1559 * already have been updated, we can't see where the syntax state is
1560 * the same again, just update until the end of the window.
1561 */
1562 if (row < top_end
1563 || (row >= mid_start && row < mid_end)
1564#ifdef FEAT_SEARCH_EXTRA
1565 || top_to_mod
1566#endif
1567 || idx >= wp->w_lines_valid
1568 || (row + wp->w_lines[idx].wl_size > bot_start)
1569 || (mod_top != 0
1570 && (lnum == mod_top
1571 || (lnum >= mod_top
1572 && (lnum < mod_bot
1573#ifdef FEAT_SYN_HL
1574 || did_update == DID_FOLD
1575 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001576 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 && (
1578# ifdef FEAT_FOLDING
1579 (foldmethodIsSyntax(wp)
1580 && hasAnyFolding(wp)) ||
1581# endif
1582 syntax_check_changed(lnum)))
1583#endif
1584 )))))
1585 {
1586#ifdef FEAT_SEARCH_EXTRA
1587 if (lnum == mod_top)
1588 top_to_mod = FALSE;
1589#endif
1590
1591 /*
1592 * When at start of changed lines: May scroll following lines
1593 * up or down to minimize redrawing.
1594 * Don't do this when the change continues until the end.
1595 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1596 */
1597 if (lnum == mod_top
1598 && mod_bot != MAXLNUM
1599 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1600 {
1601 int old_rows = 0;
1602 int new_rows = 0;
1603 int xtra_rows;
1604 linenr_T l;
1605
1606 /* Count the old number of window rows, using w_lines[], which
1607 * should still contain the sizes for the lines as they are
1608 * currently displayed. */
1609 for (i = idx; i < wp->w_lines_valid; ++i)
1610 {
1611 /* Only valid lines have a meaningful wl_lnum. Invalid
1612 * lines are part of the changed area. */
1613 if (wp->w_lines[i].wl_valid
1614 && wp->w_lines[i].wl_lnum == mod_bot)
1615 break;
1616 old_rows += wp->w_lines[i].wl_size;
1617#ifdef FEAT_FOLDING
1618 if (wp->w_lines[i].wl_valid
1619 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1620 {
1621 /* Must have found the last valid entry above mod_bot.
1622 * Add following invalid entries. */
1623 ++i;
1624 while (i < wp->w_lines_valid
1625 && !wp->w_lines[i].wl_valid)
1626 old_rows += wp->w_lines[i++].wl_size;
1627 break;
1628 }
1629#endif
1630 }
1631
1632 if (i >= wp->w_lines_valid)
1633 {
1634 /* We can't find a valid line below the changed lines,
1635 * need to redraw until the end of the window.
1636 * Inserting/deleting lines has no use. */
1637 bot_start = 0;
1638 }
1639 else
1640 {
1641 /* Able to count old number of rows: Count new window
1642 * rows, and may insert/delete lines */
1643 j = idx;
1644 for (l = lnum; l < mod_bot; ++l)
1645 {
1646#ifdef FEAT_FOLDING
1647 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1648 ++new_rows;
1649 else
1650#endif
1651#ifdef FEAT_DIFF
1652 if (l == wp->w_topline)
1653 new_rows += plines_win_nofill(wp, l, TRUE)
1654 + wp->w_topfill;
1655 else
1656#endif
1657 new_rows += plines_win(wp, l, TRUE);
1658 ++j;
1659 if (new_rows > wp->w_height - row - 2)
1660 {
1661 /* it's getting too much, must redraw the rest */
1662 new_rows = 9999;
1663 break;
1664 }
1665 }
1666 xtra_rows = new_rows - old_rows;
1667 if (xtra_rows < 0)
1668 {
1669 /* May scroll text up. If there is not enough
1670 * remaining text or scrolling fails, must redraw the
1671 * rest. If scrolling works, must redraw the text
1672 * below the scrolled text. */
1673 if (row - xtra_rows >= wp->w_height - 2)
1674 mod_bot = MAXLNUM;
1675 else
1676 {
1677 check_for_delay(FALSE);
1678 if (win_del_lines(wp, row,
1679 -xtra_rows, FALSE, FALSE) == FAIL)
1680 mod_bot = MAXLNUM;
1681 else
1682 bot_start = wp->w_height + xtra_rows;
1683 }
1684 }
1685 else if (xtra_rows > 0)
1686 {
1687 /* May scroll text down. If there is not enough
1688 * remaining text of scrolling fails, must redraw the
1689 * rest. */
1690 if (row + xtra_rows >= wp->w_height - 2)
1691 mod_bot = MAXLNUM;
1692 else
1693 {
1694 check_for_delay(FALSE);
1695 if (win_ins_lines(wp, row + old_rows,
1696 xtra_rows, FALSE, FALSE) == FAIL)
1697 mod_bot = MAXLNUM;
1698 else if (top_end > row + old_rows)
1699 /* Scrolled the part at the top that requires
1700 * updating down. */
1701 top_end += xtra_rows;
1702 }
1703 }
1704
1705 /* When not updating the rest, may need to move w_lines[]
1706 * entries. */
1707 if (mod_bot != MAXLNUM && i != j)
1708 {
1709 if (j < i)
1710 {
1711 int x = row + new_rows;
1712
1713 /* move entries in w_lines[] upwards */
1714 for (;;)
1715 {
1716 /* stop at last valid entry in w_lines[] */
1717 if (i >= wp->w_lines_valid)
1718 {
1719 wp->w_lines_valid = j;
1720 break;
1721 }
1722 wp->w_lines[j] = wp->w_lines[i];
1723 /* stop at a line that won't fit */
1724 if (x + (int)wp->w_lines[j].wl_size
1725 > wp->w_height)
1726 {
1727 wp->w_lines_valid = j + 1;
1728 break;
1729 }
1730 x += wp->w_lines[j++].wl_size;
1731 ++i;
1732 }
1733 if (bot_start > x)
1734 bot_start = x;
1735 }
1736 else /* j > i */
1737 {
1738 /* move entries in w_lines[] downwards */
1739 j -= i;
1740 wp->w_lines_valid += j;
1741 if (wp->w_lines_valid > wp->w_height)
1742 wp->w_lines_valid = wp->w_height;
1743 for (i = wp->w_lines_valid; i - j >= idx; --i)
1744 wp->w_lines[i] = wp->w_lines[i - j];
1745
1746 /* The w_lines[] entries for inserted lines are
1747 * now invalid, but wl_size may be used above.
1748 * Reset to zero. */
1749 while (i >= idx)
1750 {
1751 wp->w_lines[i].wl_size = 0;
1752 wp->w_lines[i--].wl_valid = FALSE;
1753 }
1754 }
1755 }
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758
1759#ifdef FEAT_FOLDING
1760 /*
1761 * When lines are folded, display one line for all of them.
1762 * Otherwise, display normally (can be several display lines when
1763 * 'wrap' is on).
1764 */
1765 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1766 if (fold_count != 0)
1767 {
1768 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1769 ++row;
1770 --fold_count;
1771 wp->w_lines[idx].wl_folded = TRUE;
1772 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1773# ifdef FEAT_SYN_HL
1774 did_update = DID_FOLD;
1775# endif
1776 }
1777 else
1778#endif
1779 if (idx < wp->w_lines_valid
1780 && wp->w_lines[idx].wl_valid
1781 && wp->w_lines[idx].wl_lnum == lnum
1782 && lnum > wp->w_topline
1783 && !(dy_flags & DY_LASTLINE)
1784 && srow + wp->w_lines[idx].wl_size > wp->w_height
1785#ifdef FEAT_DIFF
1786 && diff_check_fill(wp, lnum) == 0
1787#endif
1788 )
1789 {
1790 /* This line is not going to fit. Don't draw anything here,
1791 * will draw "@ " lines below. */
1792 row = wp->w_height + 1;
1793 }
1794 else
1795 {
1796#ifdef FEAT_SEARCH_EXTRA
1797 prepare_search_hl(wp, lnum);
1798#endif
1799#ifdef FEAT_SYN_HL
1800 /* Let the syntax stuff know we skipped a few lines. */
1801 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001802 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 syntax_end_parsing(syntax_last_parsed + 1);
1804#endif
1805
1806 /*
1807 * Display one line.
1808 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001809 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811#ifdef FEAT_FOLDING
1812 wp->w_lines[idx].wl_folded = FALSE;
1813 wp->w_lines[idx].wl_lastlnum = lnum;
1814#endif
1815#ifdef FEAT_SYN_HL
1816 did_update = DID_LINE;
1817 syntax_last_parsed = lnum;
1818#endif
1819 }
1820
1821 wp->w_lines[idx].wl_lnum = lnum;
1822 wp->w_lines[idx].wl_valid = TRUE;
1823 if (row > wp->w_height) /* past end of screen */
1824 {
1825 /* we may need the size of that too long line later on */
1826 if (dollar_vcol == 0)
1827 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1828 ++idx;
1829 break;
1830 }
1831 if (dollar_vcol == 0)
1832 wp->w_lines[idx].wl_size = row - srow;
1833 ++idx;
1834#ifdef FEAT_FOLDING
1835 lnum += fold_count + 1;
1836#else
1837 ++lnum;
1838#endif
1839 }
1840 else
1841 {
1842 /* This line does not need updating, advance to the next one */
1843 row += wp->w_lines[idx++].wl_size;
1844 if (row > wp->w_height) /* past end of screen */
1845 break;
1846#ifdef FEAT_FOLDING
1847 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1848#else
1849 ++lnum;
1850#endif
1851#ifdef FEAT_SYN_HL
1852 did_update = DID_NONE;
1853#endif
1854 }
1855
1856 if (lnum > buf->b_ml.ml_line_count)
1857 {
1858 eof = TRUE;
1859 break;
1860 }
1861 }
1862 /*
1863 * End of loop over all window lines.
1864 */
1865
1866
1867 if (idx > wp->w_lines_valid)
1868 wp->w_lines_valid = idx;
1869
1870#ifdef FEAT_SYN_HL
1871 /*
1872 * Let the syntax stuff know we stop parsing here.
1873 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001874 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 syntax_end_parsing(syntax_last_parsed + 1);
1876#endif
1877
1878 /*
1879 * If we didn't hit the end of the file, and we didn't finish the last
1880 * line we were working on, then the line didn't fit.
1881 */
1882 wp->w_empty_rows = 0;
1883#ifdef FEAT_DIFF
1884 wp->w_filler_rows = 0;
1885#endif
1886 if (!eof && !didline)
1887 {
1888 if (lnum == wp->w_topline)
1889 {
1890 /*
1891 * Single line that does not fit!
1892 * Don't overwrite it, it can be edited.
1893 */
1894 wp->w_botline = lnum + 1;
1895 }
1896#ifdef FEAT_DIFF
1897 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1898 {
1899 /* Window ends in filler lines. */
1900 wp->w_botline = lnum;
1901 wp->w_filler_rows = wp->w_height - srow;
1902 }
1903#endif
1904 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1905 {
1906 /*
1907 * Last line isn't finished: Display "@@@" at the end.
1908 */
1909 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1910 W_WINROW(wp) + wp->w_height,
1911 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1912 '@', '@', hl_attr(HLF_AT));
1913 set_empty_rows(wp, srow);
1914 wp->w_botline = lnum;
1915 }
1916 else
1917 {
1918 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1919 wp->w_botline = lnum;
1920 }
1921 }
1922 else
1923 {
1924#ifdef FEAT_VERTSPLIT
1925 draw_vsep_win(wp, row);
1926#endif
1927 if (eof) /* we hit the end of the file */
1928 {
1929 wp->w_botline = buf->b_ml.ml_line_count + 1;
1930#ifdef FEAT_DIFF
1931 j = diff_check_fill(wp, wp->w_botline);
1932 if (j > 0 && !wp->w_botfill)
1933 {
1934 /*
1935 * Display filler lines at the end of the file
1936 */
1937 if (char2cells(fill_diff) > 1)
1938 i = '-';
1939 else
1940 i = fill_diff;
1941 if (row + j > wp->w_height)
1942 j = wp->w_height - row;
1943 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1944 row += j;
1945 }
1946#endif
1947 }
1948 else if (dollar_vcol == 0)
1949 wp->w_botline = lnum;
1950
1951 /* make sure the rest of the screen is blank */
1952 /* put '~'s on rows that aren't part of the file. */
1953 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1954 }
1955
1956 /* Reset the type of redrawing required, the window has been updated. */
1957 wp->w_redr_type = 0;
1958#ifdef FEAT_DIFF
1959 wp->w_old_topfill = wp->w_topfill;
1960 wp->w_old_botfill = wp->w_botfill;
1961#endif
1962
1963 if (dollar_vcol == 0)
1964 {
1965 /*
1966 * There is a trick with w_botline. If we invalidate it on each
1967 * change that might modify it, this will cause a lot of expensive
1968 * calls to plines() in update_topline() each time. Therefore the
1969 * value of w_botline is often approximated, and this value is used to
1970 * compute the value of w_topline. If the value of w_botline was
1971 * wrong, check that the value of w_topline is correct (cursor is on
1972 * the visible part of the text). If it's not, we need to redraw
1973 * again. Mostly this just means scrolling up a few lines, so it
1974 * doesn't look too bad. Only do this for the current window (where
1975 * changes are relevant).
1976 */
1977 wp->w_valid |= VALID_BOTLINE;
1978 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1979 {
1980 recursive = TRUE;
1981 curwin->w_valid &= ~VALID_TOPLINE;
1982 update_topline(); /* may invalidate w_botline again */
1983 if (must_redraw != 0)
1984 {
1985 /* Don't update for changes in buffer again. */
1986 i = curbuf->b_mod_set;
1987 curbuf->b_mod_set = FALSE;
1988 win_update(curwin);
1989 must_redraw = 0;
1990 curbuf->b_mod_set = i;
1991 }
1992 recursive = FALSE;
1993 }
1994 }
1995
1996#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1997 /* restore got_int, unless CTRL-C was hit while redrawing */
1998 if (!got_int)
1999 got_int = save_got_int;
2000#endif
2001}
2002
2003#ifdef FEAT_SIGNS
2004static int draw_signcolumn __ARGS((win_T *wp));
2005
2006/*
2007 * Return TRUE when window "wp" has a column to draw signs in.
2008 */
2009 static int
2010draw_signcolumn(wp)
2011 win_T *wp;
2012{
2013 return (wp->w_buffer->b_signlist != NULL
2014# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002015 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016# endif
2017 );
2018}
2019#endif
2020
2021/*
2022 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2023 * as the filler character.
2024 */
2025 static void
2026win_draw_end(wp, c1, c2, row, endrow, hl)
2027 win_T *wp;
2028 int c1;
2029 int c2;
2030 int row;
2031 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002032 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033{
2034#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2035 int n = 0;
2036# define FDC_OFF n
2037#else
2038# define FDC_OFF 0
2039#endif
2040
2041#ifdef FEAT_RIGHTLEFT
2042 if (wp->w_p_rl)
2043 {
2044 /* No check for cmdline window: should never be right-left. */
2045# ifdef FEAT_FOLDING
2046 n = wp->w_p_fdc;
2047
2048 if (n > 0)
2049 {
2050 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002051 if (n > W_WIDTH(wp))
2052 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2054 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2055 ' ', ' ', hl_attr(HLF_FC));
2056 }
2057# endif
2058# ifdef FEAT_SIGNS
2059 if (draw_signcolumn(wp))
2060 {
2061 int nn = n + 2;
2062
2063 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002064 if (nn > W_WIDTH(wp))
2065 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2067 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2068 ' ', ' ', hl_attr(HLF_SC));
2069 n = nn;
2070 }
2071# endif
2072 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2073 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2074 c2, c2, hl_attr(hl));
2075 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2076 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2077 c1, c2, hl_attr(hl));
2078 }
2079 else
2080#endif
2081 {
2082#ifdef FEAT_CMDWIN
2083 if (cmdwin_type != 0 && wp == curwin)
2084 {
2085 /* draw the cmdline character in the leftmost column */
2086 n = 1;
2087 if (n > wp->w_width)
2088 n = wp->w_width;
2089 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2090 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2091 cmdwin_type, ' ', hl_attr(HLF_AT));
2092 }
2093#endif
2094#ifdef FEAT_FOLDING
2095 if (wp->w_p_fdc > 0)
2096 {
2097 int nn = n + wp->w_p_fdc;
2098
2099 /* draw the fold column at the left */
2100 if (nn > W_WIDTH(wp))
2101 nn = W_WIDTH(wp);
2102 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2103 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2104 ' ', ' ', hl_attr(HLF_FC));
2105 n = nn;
2106 }
2107#endif
2108#ifdef FEAT_SIGNS
2109 if (draw_signcolumn(wp))
2110 {
2111 int nn = n + 2;
2112
2113 /* draw the sign column after the fold column */
2114 if (nn > W_WIDTH(wp))
2115 nn = W_WIDTH(wp);
2116 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2117 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2118 ' ', ' ', hl_attr(HLF_SC));
2119 n = nn;
2120 }
2121#endif
2122 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2123 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2124 c1, c2, hl_attr(hl));
2125 }
2126 set_empty_rows(wp, row);
2127}
2128
Bram Moolenaar1a384422010-07-14 19:53:30 +02002129#ifdef FEAT_SYN_HL
2130static int advance_color_col __ARGS((int vcol, int **color_cols));
2131
2132/*
2133 * Advance **color_cols and return TRUE when there are columns to draw.
2134 */
2135 static int
2136advance_color_col(vcol, color_cols)
2137 int vcol;
2138 int **color_cols;
2139{
2140 while (**color_cols >= 0 && vcol > **color_cols)
2141 ++*color_cols;
2142 return (**color_cols >= 0);
2143}
2144#endif
2145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146#ifdef FEAT_FOLDING
2147/*
2148 * Display one folded line.
2149 */
2150 static void
2151fold_line(wp, fold_count, foldinfo, lnum, row)
2152 win_T *wp;
2153 long fold_count;
2154 foldinfo_T *foldinfo;
2155 linenr_T lnum;
2156 int row;
2157{
2158 char_u buf[51];
2159 pos_T *top, *bot;
2160 linenr_T lnume = lnum + fold_count - 1;
2161 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002162 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 int col;
2165 int txtcol;
2166 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002167 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169 /* Build the fold line:
2170 * 1. Add the cmdwin_type for the command-line window
2171 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002172 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 * 4. Compose the text
2174 * 5. Add the text
2175 * 6. set highlighting for the Visual area an other text
2176 */
2177 col = 0;
2178
2179 /*
2180 * 1. Add the cmdwin_type for the command-line window
2181 * Ignores 'rightleft', this window is never right-left.
2182 */
2183#ifdef FEAT_CMDWIN
2184 if (cmdwin_type != 0 && wp == curwin)
2185 {
2186 ScreenLines[off] = cmdwin_type;
2187 ScreenAttrs[off] = hl_attr(HLF_AT);
2188#ifdef FEAT_MBYTE
2189 if (enc_utf8)
2190 ScreenLinesUC[off] = 0;
2191#endif
2192 ++col;
2193 }
2194#endif
2195
2196 /*
2197 * 2. Add the 'foldcolumn'
2198 */
2199 fdc = wp->w_p_fdc;
2200 if (fdc > W_WIDTH(wp) - col)
2201 fdc = W_WIDTH(wp) - col;
2202 if (fdc > 0)
2203 {
2204 fill_foldcolumn(buf, wp, TRUE, lnum);
2205#ifdef FEAT_RIGHTLEFT
2206 if (wp->w_p_rl)
2207 {
2208 int i;
2209
2210 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2211 hl_attr(HLF_FC));
2212 /* reverse the fold column */
2213 for (i = 0; i < fdc; ++i)
2214 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2215 }
2216 else
2217#endif
2218 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2219 col += fdc;
2220 }
2221
2222#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002223# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2224 for (ri = 0; ri < l; ++ri) \
2225 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2226 else \
2227 for (ri = 0; ri < l; ++ri) \
2228 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002230# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2231 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232#endif
2233
Bram Moolenaar64486672010-05-16 15:46:46 +02002234 /* Set all attributes of the 'number' or 'relativenumber' column and the
2235 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002236 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238#ifdef FEAT_SIGNS
2239 /* If signs are being displayed, add two spaces. */
2240 if (draw_signcolumn(wp))
2241 {
2242 len = W_WIDTH(wp) - col;
2243 if (len > 0)
2244 {
2245 if (len > 2)
2246 len = 2;
2247# ifdef FEAT_RIGHTLEFT
2248 if (wp->w_p_rl)
2249 /* the line number isn't reversed */
2250 copy_text_attr(off + W_WIDTH(wp) - len - col,
2251 (char_u *)" ", len, hl_attr(HLF_FL));
2252 else
2253# endif
2254 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2255 col += len;
2256 }
2257 }
2258#endif
2259
2260 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002261 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002263 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 len = W_WIDTH(wp) - col;
2266 if (len > 0)
2267 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002268 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002269 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002270
2271 if (len > w + 1)
2272 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002273
2274 if (wp->w_p_nu)
2275 /* 'number' */
2276 num = (long)lnum;
2277 else
2278 /* 'relativenumber', don't use negative numbers */
2279 num = (long)abs((int)get_cursor_rel_lnum(wp, lnum));
2280
2281 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282#ifdef FEAT_RIGHTLEFT
2283 if (wp->w_p_rl)
2284 /* the line number isn't reversed */
2285 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2286 hl_attr(HLF_FL));
2287 else
2288#endif
2289 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2290 col += len;
2291 }
2292 }
2293
2294 /*
2295 * 4. Compose the folded-line string with 'foldtext', if set.
2296 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002297 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 txtcol = col; /* remember where text starts */
2300
2301 /*
2302 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2303 * Right-left text is put in columns 0 - number-col, normal text is put
2304 * in columns number-col - window-width.
2305 */
2306#ifdef FEAT_MBYTE
2307 if (has_mbyte)
2308 {
2309 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002310 int u8c, u8cc[MAX_MCO];
2311 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 int idx;
2313 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002314 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315# ifdef FEAT_ARABIC
2316 int prev_c = 0; /* previous Arabic character */
2317 int prev_c1 = 0; /* first composing char for prev_c */
2318# endif
2319
2320# ifdef FEAT_RIGHTLEFT
2321 if (wp->w_p_rl)
2322 idx = off;
2323 else
2324# endif
2325 idx = off + col;
2326
2327 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2328 for (p = text; *p != NUL; )
2329 {
2330 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002331 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (col + cells > W_WIDTH(wp)
2333# ifdef FEAT_RIGHTLEFT
2334 - (wp->w_p_rl ? col : 0)
2335# endif
2336 )
2337 break;
2338 ScreenLines[idx] = *p;
2339 if (enc_utf8)
2340 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002341 u8c = utfc_ptr2char(p, u8cc);
2342 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 ScreenLinesUC[idx] = 0;
2345#ifdef FEAT_ARABIC
2346 prev_c = u8c;
2347#endif
2348 }
2349 else
2350 {
2351#ifdef FEAT_ARABIC
2352 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2353 {
2354 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002355 int pc, pc1, nc;
2356 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int firstbyte = *p;
2358
2359 /* The idea of what is the previous and next
2360 * character depends on 'rightleft'. */
2361 if (wp->w_p_rl)
2362 {
2363 pc = prev_c;
2364 pc1 = prev_c1;
2365 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002366 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 else
2369 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002370 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002372 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374 prev_c = u8c;
2375
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002376 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 pc, pc1, nc);
2378 ScreenLines[idx] = firstbyte;
2379 }
2380 else
2381 prev_c = u8c;
2382#endif
2383 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002384#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (u8c >= 0x10000)
2386 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2387 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002390 for (i = 0; i < Screen_mco; ++i)
2391 {
2392 ScreenLinesC[i][idx] = u8cc[i];
2393 if (u8cc[i] == 0)
2394 break;
2395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 if (cells > 1)
2398 ScreenLines[idx + 1] = 0;
2399 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002400 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2401 /* double-byte single width character */
2402 ScreenLines2[idx] = p[1];
2403 else if (cells > 1)
2404 /* double-width character */
2405 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 col += cells;
2407 idx += cells;
2408 p += c_len;
2409 }
2410 }
2411 else
2412#endif
2413 {
2414 len = (int)STRLEN(text);
2415 if (len > W_WIDTH(wp) - col)
2416 len = W_WIDTH(wp) - col;
2417 if (len > 0)
2418 {
2419#ifdef FEAT_RIGHTLEFT
2420 if (wp->w_p_rl)
2421 STRNCPY(current_ScreenLine, text, len);
2422 else
2423#endif
2424 STRNCPY(current_ScreenLine + col, text, len);
2425 col += len;
2426 }
2427 }
2428
2429 /* Fill the rest of the line with the fold filler */
2430#ifdef FEAT_RIGHTLEFT
2431 if (wp->w_p_rl)
2432 col -= txtcol;
2433#endif
2434 while (col < W_WIDTH(wp)
2435#ifdef FEAT_RIGHTLEFT
2436 - (wp->w_p_rl ? txtcol : 0)
2437#endif
2438 )
2439 {
2440#ifdef FEAT_MBYTE
2441 if (enc_utf8)
2442 {
2443 if (fill_fold >= 0x80)
2444 {
2445 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002446 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448 else
2449 ScreenLinesUC[off + col] = 0;
2450 }
2451#endif
2452 ScreenLines[off + col++] = fill_fold;
2453 }
2454
2455 if (text != buf)
2456 vim_free(text);
2457
2458 /*
2459 * 6. set highlighting for the Visual area an other text.
2460 * If all folded lines are in the Visual area, highlight the line.
2461 */
2462#ifdef FEAT_VISUAL
2463 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2464 {
2465 if (ltoreq(curwin->w_cursor, VIsual))
2466 {
2467 /* Visual is after curwin->w_cursor */
2468 top = &curwin->w_cursor;
2469 bot = &VIsual;
2470 }
2471 else
2472 {
2473 /* Visual is before curwin->w_cursor */
2474 top = &VIsual;
2475 bot = &curwin->w_cursor;
2476 }
2477 if (lnum >= top->lnum
2478 && lnume <= bot->lnum
2479 && (VIsual_mode != 'v'
2480 || ((lnum > top->lnum
2481 || (lnum == top->lnum
2482 && top->col == 0))
2483 && (lnume < bot->lnum
2484 || (lnume == bot->lnum
2485 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002486 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 if (VIsual_mode == Ctrl_V)
2489 {
2490 /* Visual block mode: highlight the chars part of the block */
2491 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2492 {
2493 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2494 len = wp->w_old_cursor_lcol;
2495 else
2496 len = W_WIDTH(wp) - txtcol;
2497 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002498 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 }
2501 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002504 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
2507 }
2508#endif
2509
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002510#ifdef FEAT_SYN_HL
2511 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002512 if (wp->w_p_cuc)
2513 {
2514 txtcol += wp->w_virtcol;
2515 if (wp->w_p_wrap)
2516 txtcol -= wp->w_skipcol;
2517 else
2518 txtcol -= wp->w_leftcol;
2519 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2520 ScreenAttrs[off + txtcol] = hl_combine_attr(
2521 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2522 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
2525 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2526 (int)W_WIDTH(wp), FALSE);
2527
2528 /*
2529 * Update w_cline_height and w_cline_folded if the cursor line was
2530 * updated (saves a call to plines() later).
2531 */
2532 if (wp == curwin
2533 && lnum <= curwin->w_cursor.lnum
2534 && lnume >= curwin->w_cursor.lnum)
2535 {
2536 curwin->w_cline_row = row;
2537 curwin->w_cline_height = 1;
2538 curwin->w_cline_folded = TRUE;
2539 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2540 }
2541}
2542
2543/*
2544 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2545 */
2546 static void
2547copy_text_attr(off, buf, len, attr)
2548 int off;
2549 char_u *buf;
2550 int len;
2551 int attr;
2552{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002553 int i;
2554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 mch_memmove(ScreenLines + off, buf, (size_t)len);
2556# ifdef FEAT_MBYTE
2557 if (enc_utf8)
2558 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2559# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002560 for (i = 0; i < len; ++i)
2561 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562}
2563
2564/*
2565 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002566 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
2568 static void
2569fill_foldcolumn(p, wp, closed, lnum)
2570 char_u *p;
2571 win_T *wp;
2572 int closed; /* TRUE of FALSE */
2573 linenr_T lnum; /* current line number */
2574{
2575 int i = 0;
2576 int level;
2577 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002578 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
2580 /* Init to all spaces. */
2581 copy_spaces(p, (size_t)wp->w_p_fdc);
2582
2583 level = win_foldinfo.fi_level;
2584 if (level > 0)
2585 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002586 /* If there is only one column put more info in it. */
2587 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 /* If the column is too narrow, we start at the lowest level that
2590 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002591 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (first_level < 1)
2593 first_level = 1;
2594
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002595 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 if (win_foldinfo.fi_lnum == lnum
2598 && first_level + i >= win_foldinfo.fi_low_level)
2599 p[i] = '-';
2600 else if (first_level == 1)
2601 p[i] = '|';
2602 else if (first_level + i <= 9)
2603 p[i] = '0' + first_level + i;
2604 else
2605 p[i] = '>';
2606 if (first_level + i == level)
2607 break;
2608 }
2609 }
2610 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002611 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612}
2613#endif /* FEAT_FOLDING */
2614
2615/*
2616 * Display line "lnum" of window 'wp' on the screen.
2617 * Start at row "startrow", stop when "endrow" is reached.
2618 * wp->w_virtcol needs to be valid.
2619 *
2620 * Return the number of last row the line occupies.
2621 */
2622 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002623win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 win_T *wp;
2625 linenr_T lnum;
2626 int startrow;
2627 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002628 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 int col; /* visual column on screen */
2631 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2632 int c = 0; /* init for GCC */
2633 long vcol = 0; /* virtual column (for tabs) */
2634 long vcol_prev = -1; /* "vcol" of previous character */
2635 char_u *line; /* current line */
2636 char_u *ptr; /* current position in "line" */
2637 int row; /* row in the window, excl w_winrow */
2638 int screen_row; /* row on the screen, incl w_winrow */
2639
2640 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2641 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002642 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 int c_extra = NUL; /* extra chars, all the same */
2644 int extra_attr = 0; /* attributes when n_extra != 0 */
2645 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2646 displaying lcs_eol at end-of-line */
2647 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2648 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2649
2650 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2651 int saved_n_extra = 0;
2652 char_u *saved_p_extra = NULL;
2653 int saved_c_extra = 0;
2654 int saved_char_attr = 0;
2655
2656 int n_attr = 0; /* chars with special attr */
2657 int saved_attr2 = 0; /* char_attr saved for n_attr */
2658 int n_attr3 = 0; /* chars with overruling special attr */
2659 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2660
2661 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2662
2663 int fromcol, tocol; /* start/end of inverting */
2664 int fromcol_prev = -2; /* start of inverting after cursor */
2665 int noinvcur = FALSE; /* don't invert the cursor */
2666#ifdef FEAT_VISUAL
2667 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002668 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669#endif
2670 pos_T pos;
2671 long v;
2672
2673 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002674 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2676 in this line */
2677 int attr = 0; /* attributes for area highlighting */
2678 int area_attr = 0; /* attributes desired by highlighting */
2679 int search_attr = 0; /* attributes desired by 'hlsearch' */
2680#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002681 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 int syntax_attr = 0; /* attributes desired by syntax */
2683 int has_syntax = FALSE; /* this buffer has syntax highl. */
2684 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002685 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002686 int draw_color_col = FALSE; /* highlight colorcolumn */
2687 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002688#endif
2689#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002690 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002691# define SPWORDLEN 150
2692 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002693 int nextlinecol = 0; /* column where nextline[] starts */
2694 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002695 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002696 int spell_attr = 0; /* attributes desired by spelling */
2697 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002698 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2699 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002700 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002701 static int cap_col = -1; /* column to check for Cap word */
2702 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002703 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#endif
2705 int extra_check; /* has syntax or linebreak */
2706#ifdef FEAT_MBYTE
2707 int multi_attr = 0; /* attributes desired by multibyte */
2708 int mb_l = 1; /* multi-byte byte length */
2709 int mb_c = 0; /* decoded multi-byte character */
2710 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002711 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712#endif
2713#ifdef FEAT_DIFF
2714 int filler_lines; /* nr of filler lines to be drawn */
2715 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002716 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 int change_start = MAXCOL; /* first col of changed area */
2718 int change_end = -1; /* last col of changed area */
2719#endif
2720 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2721#ifdef FEAT_LINEBREAK
2722 int need_showbreak = FALSE;
2723#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002724#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2725 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002727 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#endif
2729#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002730 matchitem_T *cur; /* points to the match list */
2731 match_T *shl; /* points to search_hl or a match */
2732 int shl_flag; /* flag to indicate whether search_hl
2733 has been processed or not */
2734 int prevcol_hl_flag; /* flag to indicate whether prevcol
2735 equals startcol of search_hl or one
2736 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737#endif
2738#ifdef FEAT_ARABIC
2739 int prev_c = 0; /* previous Arabic character */
2740 int prev_c1 = 0; /* first composing char for prev_c */
2741#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002742#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002743 int did_line_attr = 0;
2744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 /* draw_state: items that are drawn in sequence: */
2747#define WL_START 0 /* nothing done yet */
2748#ifdef FEAT_CMDWIN
2749# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2750#else
2751# define WL_CMDLINE WL_START
2752#endif
2753#ifdef FEAT_FOLDING
2754# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2755#else
2756# define WL_FOLD WL_CMDLINE
2757#endif
2758#ifdef FEAT_SIGNS
2759# define WL_SIGN WL_FOLD + 1 /* column for signs */
2760#else
2761# define WL_SIGN WL_FOLD /* column for signs */
2762#endif
2763#define WL_NR WL_SIGN + 1 /* line number */
2764#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2765# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2766#else
2767# define WL_SBR WL_NR
2768#endif
2769#define WL_LINE WL_SBR + 1 /* text in the line */
2770 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002771#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 int feedback_col = 0;
2773 int feedback_old_attr = -1;
2774#endif
2775
Bram Moolenaar860cae12010-06-05 23:22:07 +02002776#ifdef FEAT_CONCEAL
2777 int syntax_flags = 0;
2778 int conceal_attr = hl_attr(HLF_CONCEAL);
2779 int first_conceal = (wp->w_p_conceal != 3);
2780 int is_concealing = FALSE;
2781 int boguscols = 0; /* nonexistent columns added to force
2782 wrapping */
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 if (startrow > endrow) /* past the end already! */
2786 return startrow;
2787
2788 row = startrow;
2789 screen_row = row + W_WINROW(wp);
2790
2791 /*
2792 * To speed up the loop below, set extra_check when there is linebreak,
2793 * trailing white space and/or syntax processing to be done.
2794 */
2795#ifdef FEAT_LINEBREAK
2796 extra_check = wp->w_p_lbr;
2797#else
2798 extra_check = 0;
2799#endif
2800#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002801 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 {
2803 /* Prepare for syntax highlighting in this line. When there is an
2804 * error, stop syntax highlighting. */
2805 save_did_emsg = did_emsg;
2806 did_emsg = FALSE;
2807 syntax_start(wp, lnum);
2808 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002809 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 else
2811 {
2812 did_emsg = save_did_emsg;
2813 has_syntax = TRUE;
2814 extra_check = TRUE;
2815 }
2816 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002817
2818 /* Check for columns to display for 'colorcolumn'. */
2819 color_cols = wp->w_p_cc_cols;
2820 if (color_cols != NULL)
2821 draw_color_col = advance_color_col(vcol, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002822#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002823
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002824#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002825 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002826 && *wp->w_s->b_p_spl != NUL
2827 && wp->w_s->b_langp.ga_len > 0
2828 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002829 {
2830 /* Prepare for spell checking. */
2831 has_spell = TRUE;
2832 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002833
2834 /* Get the start of the next line, so that words that wrap to the next
2835 * line are found too: "et<line-break>al.".
2836 * Trick: skip a few chars for C/shell/Vim comments */
2837 nextline[SPWORDLEN] = NUL;
2838 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2839 {
2840 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2841 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2842 }
2843
2844 /* When a word wrapped from the previous line the start of the current
2845 * line is valid. */
2846 if (lnum == checked_lnum)
2847 cur_checked_col = checked_col;
2848 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002849
2850 /* When there was a sentence end in the previous line may require a
2851 * word starting with capital in this line. In line 1 always check
2852 * the first word. */
2853 if (lnum != capcol_lnum)
2854 cap_col = -1;
2855 if (lnum == 1)
2856 cap_col = 0;
2857 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859#endif
2860
2861 /*
2862 * handle visual active in this window
2863 */
2864 fromcol = -10;
2865 tocol = MAXCOL;
2866#ifdef FEAT_VISUAL
2867 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2868 {
2869 /* Visual is after curwin->w_cursor */
2870 if (ltoreq(curwin->w_cursor, VIsual))
2871 {
2872 top = &curwin->w_cursor;
2873 bot = &VIsual;
2874 }
2875 else /* Visual is before curwin->w_cursor */
2876 {
2877 top = &VIsual;
2878 bot = &curwin->w_cursor;
2879 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002880 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (VIsual_mode == Ctrl_V) /* block mode */
2882 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002883 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 fromcol = wp->w_old_cursor_fcol;
2886 tocol = wp->w_old_cursor_lcol;
2887 }
2888 }
2889 else /* non-block mode */
2890 {
2891 if (lnum > top->lnum && lnum <= bot->lnum)
2892 fromcol = 0;
2893 else if (lnum == top->lnum)
2894 {
2895 if (VIsual_mode == 'V') /* linewise */
2896 fromcol = 0;
2897 else
2898 {
2899 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2900 if (gchar_pos(top) == NUL)
2901 tocol = fromcol + 1;
2902 }
2903 }
2904 if (VIsual_mode != 'V' && lnum == bot->lnum)
2905 {
2906 if (*p_sel == 'e' && bot->col == 0
2907#ifdef FEAT_VIRTUALEDIT
2908 && bot->coladd == 0
2909#endif
2910 )
2911 {
2912 fromcol = -10;
2913 tocol = MAXCOL;
2914 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002915 else if (bot->col == MAXCOL)
2916 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 else
2918 {
2919 pos = *bot;
2920 if (*p_sel == 'e')
2921 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2922 else
2923 {
2924 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2925 ++tocol;
2926 }
2927 }
2928 }
2929 }
2930
2931#ifndef MSDOS
2932 /* Check if the character under the cursor should not be inverted */
2933 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2934# ifdef FEAT_GUI
2935 && !gui.in_use
2936# endif
2937 )
2938 noinvcur = TRUE;
2939#endif
2940
2941 /* if inverting in this line set area_highlighting */
2942 if (fromcol >= 0)
2943 {
2944 area_highlighting = TRUE;
2945 attr = hl_attr(HLF_V);
2946#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2947 if (clip_star.available && !clip_star.owned && clip_isautosel())
2948 attr = hl_attr(HLF_VNC);
2949#endif
2950 }
2951 }
2952
2953 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002954 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
2956 else
2957#endif /* FEAT_VISUAL */
2958 if (highlight_match
2959 && wp == curwin
2960 && lnum >= curwin->w_cursor.lnum
2961 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2962 {
2963 if (lnum == curwin->w_cursor.lnum)
2964 getvcol(curwin, &(curwin->w_cursor),
2965 (colnr_T *)&fromcol, NULL, NULL);
2966 else
2967 fromcol = 0;
2968 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2969 {
2970 pos.lnum = lnum;
2971 pos.col = search_match_endcol;
2972 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2973 }
2974 else
2975 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00002976 /* do at least one character; happens when past end of line */
2977 if (fromcol == tocol)
2978 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 area_highlighting = TRUE;
2980 attr = hl_attr(HLF_I);
2981 }
2982
2983#ifdef FEAT_DIFF
2984 filler_lines = diff_check(wp, lnum);
2985 if (filler_lines < 0)
2986 {
2987 if (filler_lines == -1)
2988 {
2989 if (diff_find_change(wp, lnum, &change_start, &change_end))
2990 diff_hlf = HLF_ADD; /* added line */
2991 else if (change_start == 0)
2992 diff_hlf = HLF_TXD; /* changed text */
2993 else
2994 diff_hlf = HLF_CHD; /* changed line */
2995 }
2996 else
2997 diff_hlf = HLF_ADD; /* added line */
2998 filler_lines = 0;
2999 area_highlighting = TRUE;
3000 }
3001 if (lnum == wp->w_topline)
3002 filler_lines = wp->w_topfill;
3003 filler_todo = filler_lines;
3004#endif
3005
3006#ifdef LINE_ATTR
3007# ifdef FEAT_SIGNS
3008 /* If this line has a sign with line highlighting set line_attr. */
3009 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3010 if (v != 0)
3011 line_attr = sign_get_attr((int)v, TRUE);
3012# endif
3013# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3014 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003015 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 line_attr = hl_attr(HLF_L);
3017# endif
3018 if (line_attr != 0)
3019 area_highlighting = TRUE;
3020#endif
3021
3022 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3023 ptr = line;
3024
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003025#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003026 if (has_spell)
3027 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003028 /* For checking first word with a capital skip white space. */
3029 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003030 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003031
Bram Moolenaar30abd282005-06-22 22:35:10 +00003032 /* To be able to spell-check over line boundaries copy the end of the
3033 * current line into nextline[]. Above the start of the next line was
3034 * copied to nextline[SPWORDLEN]. */
3035 if (nextline[SPWORDLEN] == NUL)
3036 {
3037 /* No next line or it is empty. */
3038 nextlinecol = MAXCOL;
3039 nextline_idx = 0;
3040 }
3041 else
3042 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003043 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003044 if (v < SPWORDLEN)
3045 {
3046 /* Short line, use it completely and append the start of the
3047 * next line. */
3048 nextlinecol = 0;
3049 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003050 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003051 nextline_idx = v + 1;
3052 }
3053 else
3054 {
3055 /* Long line, use only the last SPWORDLEN bytes. */
3056 nextlinecol = v - SPWORDLEN;
3057 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3058 nextline_idx = SPWORDLEN + 1;
3059 }
3060 }
3061 }
3062#endif
3063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 /* find start of trailing whitespace */
3065 if (wp->w_p_list && lcs_trail)
3066 {
3067 trailcol = (colnr_T)STRLEN(ptr);
3068 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3069 --trailcol;
3070 trailcol += (colnr_T) (ptr - line);
3071 extra_check = TRUE;
3072 }
3073
3074 /*
3075 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3076 * first character to be displayed.
3077 */
3078 if (wp->w_p_wrap)
3079 v = wp->w_skipcol;
3080 else
3081 v = wp->w_leftcol;
3082 if (v > 0)
3083 {
3084#ifdef FEAT_MBYTE
3085 char_u *prev_ptr = ptr;
3086#endif
3087 while (vcol < v && *ptr != NUL)
3088 {
3089 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3090 vcol += c;
3091#ifdef FEAT_MBYTE
3092 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003094 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 }
3096
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003097#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3098 /* When:
3099 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003100 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003101 * - 'virtualedit' is set, or
3102 * - the visual mode is active,
3103 * the end of the line may be before the start of the displayed part.
3104 */
3105 if (vcol < v && (
3106# ifdef FEAT_SYN_HL
3107 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003108 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003109# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3110 ||
3111# endif
3112# endif
3113# ifdef FEAT_VIRTUALEDIT
3114 virtual_active()
3115# ifdef FEAT_VISUAL
3116 ||
3117# endif
3118# endif
3119# ifdef FEAT_VISUAL
3120 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3121# endif
3122 ))
3123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#endif
3127
3128 /* Handle a character that's not completely on the screen: Put ptr at
3129 * that character but skip the first few screen characters. */
3130 if (vcol > v)
3131 {
3132 vcol -= c;
3133#ifdef FEAT_MBYTE
3134 ptr = prev_ptr;
3135#else
3136 --ptr;
3137#endif
3138 n_skip = v - vcol;
3139 }
3140
3141 /*
3142 * Adjust for when the inverted text is before the screen,
3143 * and when the start of the inverted text is before the screen.
3144 */
3145 if (tocol <= vcol)
3146 fromcol = 0;
3147 else if (fromcol >= 0 && fromcol < vcol)
3148 fromcol = vcol;
3149
3150#ifdef FEAT_LINEBREAK
3151 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3152 if (wp->w_p_wrap)
3153 need_showbreak = TRUE;
3154#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003155#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003156 /* When spell checking a word we need to figure out the start of the
3157 * word and if it's badly spelled or not. */
3158 if (has_spell)
3159 {
3160 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003161 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003162 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003163
3164 pos = wp->w_cursor;
3165 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003166 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003167 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003168
3169 /* spell_move_to() may call ml_get() and make "line" invalid */
3170 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3171 ptr = line + linecol;
3172
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003173 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003174 {
3175 /* no bad word found at line start, don't check until end of a
3176 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003177 spell_hlf = HLF_COUNT;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003178 word_end = (int)(spell_to_word_end(ptr, wp)
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003179 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003180 }
3181 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003182 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003183 /* bad word found, use attributes until end of word */
3184 word_end = wp->w_cursor.col + len + 1;
3185
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003186 /* Turn index into actual attributes. */
3187 if (spell_hlf != HLF_COUNT)
3188 spell_attr = highlight_attr[spell_hlf];
3189 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003190 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003191
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003192# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003193 /* Need to restart syntax highlighting for this line. */
3194 if (has_syntax)
3195 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003196# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003197 }
3198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200
3201 /*
3202 * Correct highlighting for cursor that can't be disabled.
3203 * Avoids having to check this for each character.
3204 */
3205 if (fromcol >= 0)
3206 {
3207 if (noinvcur)
3208 {
3209 if ((colnr_T)fromcol == wp->w_virtcol)
3210 {
3211 /* highlighting starts at cursor, let it start just after the
3212 * cursor */
3213 fromcol_prev = fromcol;
3214 fromcol = -1;
3215 }
3216 else if ((colnr_T)fromcol < wp->w_virtcol)
3217 /* restart highlighting after the cursor */
3218 fromcol_prev = wp->w_virtcol;
3219 }
3220 if (fromcol >= tocol)
3221 fromcol = -1;
3222 }
3223
3224#ifdef FEAT_SEARCH_EXTRA
3225 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003226 * Handle highlighting the last used search pattern and matches.
3227 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003229 cur = wp->w_match_head;
3230 shl_flag = FALSE;
3231 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003233 if (shl_flag == FALSE)
3234 {
3235 shl = &search_hl;
3236 shl_flag = TRUE;
3237 }
3238 else
3239 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003240 shl->startcol = MAXCOL;
3241 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 shl->attr_cur = 0;
3243 if (shl->rm.regprog != NULL)
3244 {
3245 v = (long)(ptr - line);
3246 next_search_hl(wp, shl, lnum, (colnr_T)v);
3247
3248 /* Need to get the line again, a multi-line regexp may have made it
3249 * invalid. */
3250 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3251 ptr = line + v;
3252
3253 if (shl->lnum != 0 && shl->lnum <= lnum)
3254 {
3255 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003256 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003258 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3260 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003261 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003263 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003265 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003268 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003269 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 else
3271#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003272 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003274 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
3276 shl->attr_cur = shl->attr;
3277 search_attr = shl->attr;
3278 }
3279 area_highlighting = TRUE;
3280 }
3281 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003282 if (shl != &search_hl && cur != NULL)
3283 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
3285#endif
3286
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003287#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003288 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3289 * active, because it's not clear what is selected then. */
3290 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003291 {
3292 line_attr = hl_attr(HLF_CUL);
3293 area_highlighting = TRUE;
3294 }
3295#endif
3296
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003297 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 col = 0;
3299#ifdef FEAT_RIGHTLEFT
3300 if (wp->w_p_rl)
3301 {
3302 /* Rightleft window: process the text in the normal direction, but put
3303 * it in current_ScreenLine[] from right to left. Start at the
3304 * rightmost column of the window. */
3305 col = W_WIDTH(wp) - 1;
3306 off += col;
3307 }
3308#endif
3309
3310 /*
3311 * Repeat for the whole displayed line.
3312 */
3313 for (;;)
3314 {
3315 /* Skip this quickly when working on the text. */
3316 if (draw_state != WL_LINE)
3317 {
3318#ifdef FEAT_CMDWIN
3319 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3320 {
3321 draw_state = WL_CMDLINE;
3322 if (cmdwin_type != 0 && wp == curwin)
3323 {
3324 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003326 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 char_attr = hl_attr(HLF_AT);
3328 }
3329 }
3330#endif
3331
3332#ifdef FEAT_FOLDING
3333 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3334 {
3335 draw_state = WL_FOLD;
3336 if (wp->w_p_fdc > 0)
3337 {
3338 /* Draw the 'foldcolumn'. */
3339 fill_foldcolumn(extra, wp, FALSE, lnum);
3340 n_extra = wp->w_p_fdc;
3341 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003342 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 c_extra = NUL;
3344 char_attr = hl_attr(HLF_FC);
3345 }
3346 }
3347#endif
3348
3349#ifdef FEAT_SIGNS
3350 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3351 {
3352 draw_state = WL_SIGN;
3353 /* Show the sign column when there are any signs in this
3354 * buffer or when using Netbeans. */
3355 if (draw_signcolumn(wp)
3356# ifdef FEAT_DIFF
3357 && filler_todo <= 0
3358# endif
3359 )
3360 {
3361 int_u text_sign;
3362# ifdef FEAT_SIGN_ICONS
3363 int_u icon_sign;
3364# endif
3365
3366 /* Draw two cells with the sign value or blank. */
3367 c_extra = ' ';
3368 char_attr = hl_attr(HLF_SC);
3369 n_extra = 2;
3370
3371 if (row == startrow)
3372 {
3373 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3374 SIGN_TEXT);
3375# ifdef FEAT_SIGN_ICONS
3376 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3377 SIGN_ICON);
3378 if (gui.in_use && icon_sign != 0)
3379 {
3380 /* Use the image in this position. */
3381 c_extra = SIGN_BYTE;
3382# ifdef FEAT_NETBEANS_INTG
3383 if (buf_signcount(wp->w_buffer, lnum) > 1)
3384 c_extra = MULTISIGN_BYTE;
3385# endif
3386 char_attr = icon_sign;
3387 }
3388 else
3389# endif
3390 if (text_sign != 0)
3391 {
3392 p_extra = sign_get_text(text_sign);
3393 if (p_extra != NULL)
3394 {
3395 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003396 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 char_attr = sign_get_attr(text_sign, FALSE);
3399 }
3400 }
3401 }
3402 }
3403#endif
3404
3405 if (draw_state == WL_NR - 1 && n_extra == 0)
3406 {
3407 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003408 /* Display the absolute or relative line number. After the
3409 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3410 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 && (row == startrow
3412#ifdef FEAT_DIFF
3413 + filler_lines
3414#endif
3415 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3416 {
3417 /* Draw the line number (empty space after wrapping). */
3418 if (row == startrow
3419#ifdef FEAT_DIFF
3420 + filler_lines
3421#endif
3422 )
3423 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003424 long num;
3425
3426 if (wp->w_p_nu)
3427 /* 'number' */
3428 num = (long)lnum;
3429 else
3430 /* 'relativenumber', don't use negative numbers */
3431 num = (long)abs((int)get_cursor_rel_lnum(wp,
3432 lnum));
3433
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003434 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003435 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (wp->w_skipcol > 0)
3437 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3438 *p_extra = '-';
3439#ifdef FEAT_RIGHTLEFT
3440 if (wp->w_p_rl) /* reverse line numbers */
3441 rl_mirror(extra);
3442#endif
3443 p_extra = extra;
3444 c_extra = NUL;
3445 }
3446 else
3447 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003448 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003450#ifdef FEAT_SYN_HL
3451 /* When 'cursorline' is set highlight the line number of
3452 * the current line differently. */
3453 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3454 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
3457 }
3458
3459#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3460 if (draw_state == WL_SBR - 1 && n_extra == 0)
3461 {
3462 draw_state = WL_SBR;
3463# ifdef FEAT_DIFF
3464 if (filler_todo > 0)
3465 {
3466 /* Draw "deleted" diff line(s). */
3467 if (char2cells(fill_diff) > 1)
3468 c_extra = '-';
3469 else
3470 c_extra = fill_diff;
3471# ifdef FEAT_RIGHTLEFT
3472 if (wp->w_p_rl)
3473 n_extra = col + 1;
3474 else
3475# endif
3476 n_extra = W_WIDTH(wp) - col;
3477 char_attr = hl_attr(HLF_DED);
3478 }
3479# endif
3480# ifdef FEAT_LINEBREAK
3481 if (*p_sbr != NUL && need_showbreak)
3482 {
3483 /* Draw 'showbreak' at the start of each broken line. */
3484 p_extra = p_sbr;
3485 c_extra = NUL;
3486 n_extra = (int)STRLEN(p_sbr);
3487 char_attr = hl_attr(HLF_AT);
3488 need_showbreak = FALSE;
3489 /* Correct end of highlighted area for 'showbreak',
3490 * required when 'linebreak' is also set. */
3491 if (tocol == vcol)
3492 tocol += n_extra;
3493 }
3494# endif
3495 }
3496#endif
3497
3498 if (draw_state == WL_LINE - 1 && n_extra == 0)
3499 {
3500 draw_state = WL_LINE;
3501 if (saved_n_extra)
3502 {
3503 /* Continue item from end of wrapped line. */
3504 n_extra = saved_n_extra;
3505 c_extra = saved_c_extra;
3506 p_extra = saved_p_extra;
3507 char_attr = saved_char_attr;
3508 }
3509 else
3510 char_attr = 0;
3511 }
3512 }
3513
3514 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003515 if (dollar_vcol != 0 && wp == curwin
3516 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517#ifdef FEAT_DIFF
3518 && filler_todo <= 0
3519#endif
3520 )
3521 {
3522 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3523 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003524 /* Pretend we have finished updating the window. Except when
3525 * 'cursorcolumn' is set. */
3526#ifdef FEAT_SYN_HL
3527 if (wp->w_p_cuc)
3528 row = wp->w_cline_row + wp->w_cline_height;
3529 else
3530#endif
3531 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 break;
3533 }
3534
3535 if (draw_state == WL_LINE && area_highlighting)
3536 {
3537 /* handle Visual or match highlighting in this line */
3538 if (vcol == fromcol
3539#ifdef FEAT_MBYTE
3540 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3541 && (*mb_ptr2cells)(ptr) > 1)
3542#endif
3543 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003544 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 && vcol < tocol))
3546 area_attr = attr; /* start highlighting */
3547 else if (area_attr != 0
3548 && (vcol == tocol
3549 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552#ifdef FEAT_SEARCH_EXTRA
3553 if (!n_extra)
3554 {
3555 /*
3556 * Check for start/end of search pattern match.
3557 * After end, check for start/end of next match.
3558 * When another match, have to check for start again.
3559 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003560 * Do this for 'search_hl' and the match list (ordered by
3561 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003563 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003564 cur = wp->w_match_head;
3565 shl_flag = FALSE;
3566 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003568 if (shl_flag == FALSE
3569 && ((cur != NULL
3570 && cur->priority > SEARCH_HL_PRIORITY)
3571 || cur == NULL))
3572 {
3573 shl = &search_hl;
3574 shl_flag = TRUE;
3575 }
3576 else
3577 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 while (shl->rm.regprog != NULL)
3579 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003580 if (shl->startcol != MAXCOL
3581 && v >= (long)shl->startcol
3582 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 shl->attr_cur = shl->attr;
3585 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003586 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 shl->attr_cur = 0;
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 next_search_hl(wp, shl, lnum, (colnr_T)v);
3591
3592 /* Need to get the line again, a multi-line regexp
3593 * may have made it invalid. */
3594 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3595 ptr = line + v;
3596
3597 if (shl->lnum == lnum)
3598 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003599 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003601 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003603 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003605 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 /* highlight empty match, try again after
3608 * it */
3609#ifdef FEAT_MBYTE
3610 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003611 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003612 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 else
3614#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003615 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617
3618 /* Loop to check if the match starts at the
3619 * current position */
3620 continue;
3621 }
3622 }
3623 break;
3624 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003625 if (shl != &search_hl && cur != NULL)
3626 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003628
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003629 /* Use attributes from match with highest priority among
3630 * 'search_hl' and the match list. */
3631 search_attr = search_hl.attr_cur;
3632 cur = wp->w_match_head;
3633 shl_flag = FALSE;
3634 while (cur != NULL || shl_flag == FALSE)
3635 {
3636 if (shl_flag == FALSE
3637 && ((cur != NULL
3638 && cur->priority > SEARCH_HL_PRIORITY)
3639 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003640 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003641 shl = &search_hl;
3642 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003643 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003644 else
3645 shl = &cur->hl;
3646 if (shl->attr_cur != 0)
3647 search_attr = shl->attr_cur;
3648 if (shl != &search_hl && cur != NULL)
3649 cur = cur->next;
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652#endif
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003655 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003657 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3658 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003660 if (diff_hlf == HLF_TXD && ptr - line > change_end
3661 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003663 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003666
3667 /* Decide which of the highlight attributes to use. */
3668 attr_pri = TRUE;
3669 if (area_attr != 0)
3670 char_attr = area_attr;
3671 else if (search_attr != 0)
3672 char_attr = search_attr;
3673#ifdef LINE_ATTR
3674 /* Use line_attr when not in the Visual or 'incsearch' area
3675 * (area_attr may be 0 when "noinvcur" is set). */
3676 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003677 || vcol < fromcol || vcol_prev < fromcol_prev
3678 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003679 char_attr = line_attr;
3680#endif
3681 else
3682 {
3683 attr_pri = FALSE;
3684#ifdef FEAT_SYN_HL
3685 if (has_syntax)
3686 char_attr = syntax_attr;
3687 else
3688#endif
3689 char_attr = 0;
3690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692
3693 /*
3694 * Get the next character to put on the screen.
3695 */
3696 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003697 * The "p_extra" points to the extra stuff that is inserted to
3698 * represent special characters (non-printable stuff) and other
3699 * things. When all characters are the same, c_extra is used.
3700 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3701 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3703 */
3704 if (n_extra > 0)
3705 {
3706 if (c_extra != NUL)
3707 {
3708 c = c_extra;
3709#ifdef FEAT_MBYTE
3710 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3711 if (enc_utf8 && (*mb_char2len)(c) > 1)
3712 {
3713 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003714 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003715 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 else
3718 mb_utf8 = FALSE;
3719#endif
3720 }
3721 else
3722 {
3723 c = *p_extra;
3724#ifdef FEAT_MBYTE
3725 if (has_mbyte)
3726 {
3727 mb_c = c;
3728 if (enc_utf8)
3729 {
3730 /* If the UTF-8 character is more than one byte:
3731 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003732 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 mb_utf8 = FALSE;
3734 if (mb_l > n_extra)
3735 mb_l = 1;
3736 else if (mb_l > 1)
3737 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003738 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003740 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742 }
3743 else
3744 {
3745 /* if this is a DBCS character, put it in "mb_c" */
3746 mb_l = MB_BYTE2LEN(c);
3747 if (mb_l >= n_extra)
3748 mb_l = 1;
3749 else if (mb_l > 1)
3750 mb_c = (c << 8) + p_extra[1];
3751 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003752 if (mb_l == 0) /* at the NUL at end-of-line */
3753 mb_l = 1;
3754
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 /* If a double-width char doesn't fit display a '>' in the
3756 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003757 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758# ifdef FEAT_RIGHTLEFT
3759 wp->w_p_rl ? (col <= 0) :
3760# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003761 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 && (*mb_char2cells)(mb_c) == 2)
3763 {
3764 c = '>';
3765 mb_c = c;
3766 mb_l = 1;
3767 mb_utf8 = FALSE;
3768 multi_attr = hl_attr(HLF_AT);
3769 /* put the pointer back to output the double-width
3770 * character at the start of the next line. */
3771 ++n_extra;
3772 --p_extra;
3773 }
3774 else
3775 {
3776 n_extra -= mb_l - 1;
3777 p_extra += mb_l - 1;
3778 }
3779 }
3780#endif
3781 ++p_extra;
3782 }
3783 --n_extra;
3784 }
3785 else
3786 {
3787 /*
3788 * Get a character from the line itself.
3789 */
3790 c = *ptr;
3791#ifdef FEAT_MBYTE
3792 if (has_mbyte)
3793 {
3794 mb_c = c;
3795 if (enc_utf8)
3796 {
3797 /* If the UTF-8 character is more than one byte: Decode it
3798 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003799 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 mb_utf8 = FALSE;
3801 if (mb_l > 1)
3802 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003803 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 /* Overlong encoded ASCII or ASCII with composing char
3805 * is displayed normally, except a NUL. */
3806 if (mb_c < 0x80)
3807 c = mb_c;
3808 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003809
3810 /* At start of the line we can have a composing char.
3811 * Draw it as a space with a composing char. */
3812 if (utf_iscomposing(mb_c))
3813 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003814 int i;
3815
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003816 for (i = Screen_mco - 1; i > 0; --i)
3817 u8cc[i] = u8cc[i - 1];
3818 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003819 mb_c = ' ';
3820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822
3823 if ((mb_l == 1 && c >= 0x80)
3824 || (mb_l >= 1 && mb_c == 0)
3825 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003826# ifdef UNICODE16
3827 || mb_c >= 0x10000
3828# endif
3829 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
3831 /*
3832 * Illegal UTF-8 byte: display as <xx>.
3833 * Non-BMP character : display as ? or fullwidth ?.
3834 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003835# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
3839 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003840# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (wp->w_p_rl) /* reverse */
3842 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003845# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 else if (utf_char2cells(mb_c) != 2)
3847 STRCPY(extra, "?");
3848 else
3849 /* 0xff1f in UTF-8: full-width '?' */
3850 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
3853 p_extra = extra;
3854 c = *p_extra;
3855 mb_c = mb_ptr2char_adv(&p_extra);
3856 mb_utf8 = (c >= 0x80);
3857 n_extra = (int)STRLEN(p_extra);
3858 c_extra = NUL;
3859 if (area_attr == 0 && search_attr == 0)
3860 {
3861 n_attr = n_extra + 1;
3862 extra_attr = hl_attr(HLF_8);
3863 saved_attr2 = char_attr; /* save current attr */
3864 }
3865 }
3866 else if (mb_l == 0) /* at the NUL at end-of-line */
3867 mb_l = 1;
3868#ifdef FEAT_ARABIC
3869 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3870 {
3871 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003872 int pc, pc1, nc;
3873 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874
3875 /* The idea of what is the previous and next
3876 * character depends on 'rightleft'. */
3877 if (wp->w_p_rl)
3878 {
3879 pc = prev_c;
3880 pc1 = prev_c1;
3881 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003882 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884 else
3885 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003886 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003888 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890 prev_c = mb_c;
3891
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003892 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 else
3895 prev_c = mb_c;
3896#endif
3897 }
3898 else /* enc_dbcs */
3899 {
3900 mb_l = MB_BYTE2LEN(c);
3901 if (mb_l == 0) /* at the NUL at end-of-line */
3902 mb_l = 1;
3903 else if (mb_l > 1)
3904 {
3905 /* We assume a second byte below 32 is illegal.
3906 * Hopefully this is OK for all double-byte encodings!
3907 */
3908 if (ptr[1] >= 32)
3909 mb_c = (c << 8) + ptr[1];
3910 else
3911 {
3912 if (ptr[1] == NUL)
3913 {
3914 /* head byte at end of line */
3915 mb_l = 1;
3916 transchar_nonprint(extra, c);
3917 }
3918 else
3919 {
3920 /* illegal tail byte */
3921 mb_l = 2;
3922 STRCPY(extra, "XX");
3923 }
3924 p_extra = extra;
3925 n_extra = (int)STRLEN(extra) - 1;
3926 c_extra = NUL;
3927 c = *p_extra++;
3928 if (area_attr == 0 && search_attr == 0)
3929 {
3930 n_attr = n_extra + 1;
3931 extra_attr = hl_attr(HLF_8);
3932 saved_attr2 = char_attr; /* save current attr */
3933 }
3934 mb_c = c;
3935 }
3936 }
3937 }
3938 /* If a double-width char doesn't fit display a '>' in the
3939 * last column; the character is displayed at the start of the
3940 * next line. */
3941 if ((
3942# ifdef FEAT_RIGHTLEFT
3943 wp->w_p_rl ? (col <= 0) :
3944# endif
3945 (col >= W_WIDTH(wp) - 1))
3946 && (*mb_char2cells)(mb_c) == 2)
3947 {
3948 c = '>';
3949 mb_c = c;
3950 mb_utf8 = FALSE;
3951 mb_l = 1;
3952 multi_attr = hl_attr(HLF_AT);
3953 /* Put pointer back so that the character will be
3954 * displayed at the start of the next line. */
3955 --ptr;
3956 }
3957 else if (*ptr != NUL)
3958 ptr += mb_l - 1;
3959
3960 /* If a double-width char doesn't fit at the left side display
3961 * a '<' in the first column. */
3962 if (n_skip > 0 && mb_l > 1)
3963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003965 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 c = ' ';
3967 if (area_attr == 0 && search_attr == 0)
3968 {
3969 n_attr = n_extra + 1;
3970 extra_attr = hl_attr(HLF_AT);
3971 saved_attr2 = char_attr; /* save current attr */
3972 }
3973 mb_c = c;
3974 mb_utf8 = FALSE;
3975 mb_l = 1;
3976 }
3977
3978 }
3979#endif
3980 ++ptr;
3981
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003982 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003983 if (wp->w_p_list && (c == 160
3984#ifdef FEAT_MBYTE
3985 || (mb_utf8 && mb_c == 160)
3986#endif
3987 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003988 {
3989 c = lcs_nbsp;
3990 if (area_attr == 0 && search_attr == 0)
3991 {
3992 n_attr = 1;
3993 extra_attr = hl_attr(HLF_8);
3994 saved_attr2 = char_attr; /* save current attr */
3995 }
3996#ifdef FEAT_MBYTE
3997 mb_c = c;
3998 if (enc_utf8 && (*mb_char2len)(c) > 1)
3999 {
4000 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004001 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004003 }
4004 else
4005 mb_utf8 = FALSE;
4006#endif
4007 }
4008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (extra_check)
4010 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004011#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004012 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004013#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004014
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004015#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 /* Get syntax attribute, unless still at the start of the line
4017 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004018 v = (long)(ptr - line);
4019 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
4021 /* Get the syntax attribute for the character. If there
4022 * is an error, disable syntax highlighting. */
4023 save_did_emsg = did_emsg;
4024 did_emsg = FALSE;
4025
Bram Moolenaar217ad922005-03-20 22:37:15 +00004026 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004027# ifdef FEAT_CONCEAL
4028 &syntax_flags,
4029# else
4030 NULL,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004031# endif
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 Moolenaar071d4272004-06-13 20:20:40 +00004054 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004055#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004056
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004057#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004058 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004059 * Only do this when there is no syntax highlighting, the
4060 * @Spell cluster is not used or the current syntax item
4061 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004062 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004063 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004064 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004065# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004066 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004067 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004068# endif
4069 if (c != 0 && (
4070# ifdef FEAT_SYN_HL
4071 !has_syntax ||
4072# endif
4073 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004074 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004075 char_u *prev_ptr, *p;
4076 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004077 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004078# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004079 if (has_mbyte)
4080 {
4081 prev_ptr = ptr - mb_l;
4082 v -= mb_l - 1;
4083 }
4084 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004085# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004086 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004087
4088 /* Use nextline[] if possible, it has the start of the
4089 * next line concatenated. */
4090 if ((prev_ptr - line) - nextlinecol >= 0)
4091 p = nextline + (prev_ptr - line) - nextlinecol;
4092 else
4093 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004094 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004095 len = spell_check(wp, p, &spell_hlf, &cap_col,
4096 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004097 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004098
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004099 /* In Insert mode only highlight a word that
4100 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004101 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004102 && (State & INSERT) != 0
4103 && wp->w_cursor.lnum == lnum
4104 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004105 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004106 && wp->w_cursor.col < (colnr_T)word_end)
4107 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004108 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004109 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004110 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004111
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004112 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004113 && (p - nextline) + len > nextline_idx)
4114 {
4115 /* Remember that the good word continues at the
4116 * start of the next line. */
4117 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004118 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004119 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004120
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004121 /* Turn index into actual attributes. */
4122 if (spell_hlf != HLF_COUNT)
4123 spell_attr = highlight_attr[spell_hlf];
4124
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004125 if (cap_col > 0)
4126 {
4127 if (p != prev_ptr
4128 && (p - nextline) + cap_col >= nextline_idx)
4129 {
4130 /* Remember that the word in the next line
4131 * must start with a capital. */
4132 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004133 cap_col = (int)((p - nextline) + cap_col
4134 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004135 }
4136 else
4137 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004138 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004139 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004140 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004141 }
4142 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004143 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004144 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004145 char_attr = hl_combine_attr(char_attr, spell_attr);
4146 else
4147 char_attr = hl_combine_attr(spell_attr, char_attr);
4148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149#endif
4150#ifdef FEAT_LINEBREAK
4151 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004152 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 */
4154 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004155 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
4157 n_extra = win_lbr_chartabsize(wp, ptr - (
4158# ifdef FEAT_MBYTE
4159 has_mbyte ? mb_l :
4160# endif
4161 1), (colnr_T)vcol, NULL) - 1;
4162 c_extra = ' ';
4163 if (vim_iswhite(c))
4164 c = ' ';
4165 }
4166#endif
4167
4168 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4169 {
4170 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004171 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
4173 n_attr = 1;
4174 extra_attr = hl_attr(HLF_8);
4175 saved_attr2 = char_attr; /* save current attr */
4176 }
4177#ifdef FEAT_MBYTE
4178 mb_c = c;
4179 if (enc_utf8 && (*mb_char2len)(c) > 1)
4180 {
4181 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004182 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004183 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185 else
4186 mb_utf8 = FALSE;
4187#endif
4188 }
4189 }
4190
4191 /*
4192 * Handling of non-printable characters.
4193 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004194 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
4196 /*
4197 * when getting a character from the file, we may have to
4198 * turn it into something else on the way to putting it
4199 * into "ScreenLines".
4200 */
4201 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4202 {
4203 /* tab amount depends on current column */
4204 n_extra = (int)wp->w_buffer->b_p_ts
4205 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4206#ifdef FEAT_MBYTE
4207 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4208#endif
4209 if (wp->w_p_list)
4210 {
4211 c = lcs_tab1;
4212 c_extra = lcs_tab2;
4213 n_attr = n_extra + 1;
4214 extra_attr = hl_attr(HLF_8);
4215 saved_attr2 = char_attr; /* save current attr */
4216#ifdef FEAT_MBYTE
4217 mb_c = c;
4218 if (enc_utf8 && (*mb_char2len)(c) > 1)
4219 {
4220 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004221 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004222 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224#endif
4225 }
4226 else
4227 {
4228 c_extra = ' ';
4229 c = ' ';
4230 }
4231 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004232 else if (c == NUL
4233 && ((wp->w_p_list && lcs_eol > 0)
4234 || ((fromcol >= 0 || fromcol_prev >= 0)
4235 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004236#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004237 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004238#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004239 && (
4240# ifdef FEAT_RIGHTLEFT
4241 wp->w_p_rl ? (col >= 0) :
4242# endif
4243 (col < W_WIDTH(wp)))
4244 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004245 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004246 && (colnr_T)vcol == wp->w_virtcol)))
4247 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004249 /* Display a '$' after the line or highlight an extra
4250 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4252 /* For a diff line the highlighting continues after the
4253 * "$". */
4254 if (
4255# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004256 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257# ifdef LINE_ATTR
4258 &&
4259# endif
4260# endif
4261# ifdef LINE_ATTR
4262 line_attr == 0
4263# endif
4264 )
4265#endif
4266 {
4267#ifdef FEAT_VIRTUALEDIT
4268 /* In virtualedit, visual selections may extend
4269 * beyond end of line. */
4270 if (area_highlighting && virtual_active()
4271 && tocol != MAXCOL && vcol < tocol)
4272 n_extra = 0;
4273 else
4274#endif
4275 {
4276 p_extra = at_end_str;
4277 n_extra = 1;
4278 c_extra = NUL;
4279 }
4280 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004281 if (wp->w_p_list)
4282 c = lcs_eol;
4283 else
4284 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 lcs_eol_one = -1;
4286 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004287 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 extra_attr = hl_attr(HLF_AT);
4290 n_attr = 1;
4291 }
4292#ifdef FEAT_MBYTE
4293 mb_c = c;
4294 if (enc_utf8 && (*mb_char2len)(c) > 1)
4295 {
4296 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004297 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004298 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 else
4301 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4302#endif
4303 }
4304 else if (c != NUL)
4305 {
4306 p_extra = transchar(c);
4307#ifdef FEAT_RIGHTLEFT
4308 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4309 rl_mirror(p_extra); /* reverse "<12>" */
4310#endif
4311 n_extra = byte2cells(c) - 1;
4312 c_extra = NUL;
4313 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004314 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 n_attr = n_extra + 1;
4317 extra_attr = hl_attr(HLF_8);
4318 saved_attr2 = char_attr; /* save current attr */
4319 }
4320#ifdef FEAT_MBYTE
4321 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4322#endif
4323 }
4324#ifdef FEAT_VIRTUALEDIT
4325 else if (VIsual_active
4326 && (VIsual_mode == Ctrl_V
4327 || VIsual_mode == 'v')
4328 && virtual_active()
4329 && tocol != MAXCOL
4330 && vcol < tocol
4331 && (
4332# ifdef FEAT_RIGHTLEFT
4333 wp->w_p_rl ? (col >= 0) :
4334# endif
4335 (col < W_WIDTH(wp))))
4336 {
4337 c = ' ';
4338 --ptr; /* put it back at the NUL */
4339 }
4340#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004341#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 else if ((
4343# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004344 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 ) && (
4348# ifdef FEAT_RIGHTLEFT
4349 wp->w_p_rl ? (col >= 0) :
4350# endif
4351 (col < W_WIDTH(wp))))
4352 {
4353 /* Highlight until the right side of the window */
4354 c = ' ';
4355 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004356
4357 /* Remember we do the char for line highlighting. */
4358 ++did_line_attr;
4359
4360 /* don't do search HL for the rest of the line */
4361 if (line_attr != 0 && char_attr == search_attr && col > 0)
4362 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363# ifdef FEAT_DIFF
4364 if (diff_hlf == HLF_TXD)
4365 {
4366 diff_hlf = HLF_CHD;
4367 if (attr == 0 || char_attr != attr)
4368 char_attr = hl_attr(diff_hlf);
4369 }
4370# endif
4371 }
4372#endif
4373 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004374
4375#ifdef FEAT_CONCEAL
4376 if ( wp->w_p_conceal
Bram Moolenaare667c952010-07-05 22:57:59 +02004377 && !area_highlighting
4378 && (lnum != wp->w_cursor.lnum
4379 || curwin != wp || wp->w_buffer->b_p_ma == FALSE)
4380 && (syntax_flags & HL_CONCEAL) != 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004381
4382 {
4383 char_attr = conceal_attr;
4384 if (first_conceal
4385 && (syn_get_sub_char() != NUL || wp->w_p_conceal == 1))
4386 {
4387 if (syn_get_sub_char() != NUL)
4388 c = syn_get_sub_char();
4389 else if (lcs_conceal != NUL)
4390 c = lcs_conceal;
4391 else
4392 c = ' ';
4393
4394 first_conceal = FALSE;
4395
4396# ifdef FEAT_HLCOLUMN
4397 if (hlc > 0 && n_extra > 0)
4398 hlc += n_extra;
4399# endif
4400 vcol += n_extra;
4401 if (wp->w_p_wrap && n_extra > 0)
4402 {
4403# ifdef FEAT_RIGHTLEFT
4404 if (wp->w_p_rl)
4405 {
4406 col -= n_extra;
4407 boguscols -= n_extra;
4408 }
4409 else
4410# endif
4411 {
4412 boguscols += n_extra;
4413 col += n_extra;
4414 }
4415 }
4416 n_extra = 0;
4417 n_attr = 0;
4418 }
4419 else if (n_skip == 0)
4420 {
4421 is_concealing = TRUE;
4422 n_skip = 1;
4423 }
4424# ifdef FEAT_MBYTE
4425 mb_c = c;
4426 if (enc_utf8 && (*mb_char2len)(c) > 1)
4427 {
4428 mb_utf8 = TRUE;
4429 u8cc[0] = 0;
4430 c = 0xc0;
4431 }
4432 else
4433 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4434# endif
4435 }
4436 else
4437 {
4438 first_conceal = (wp->w_p_conceal != 3);
4439 is_concealing = FALSE;
4440 }
4441#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
4444 /* Don't override visual selection highlighting. */
4445 if (n_attr > 0
4446 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004447 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 char_attr = extra_attr;
4449
Bram Moolenaar81695252004-12-29 20:58:21 +00004450#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 /* XIM don't send preedit_start and preedit_end, but they send
4452 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4453 * im_is_preediting() here. */
4454 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004455 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 && (State & INSERT)
4457 && !p_imdisable
4458 && im_is_preediting()
4459 && draw_state == WL_LINE)
4460 {
4461 colnr_T tcol;
4462
4463 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004464 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 else
4466 tcol = preedit_end_col;
4467 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4468 {
4469 if (feedback_old_attr < 0)
4470 {
4471 feedback_col = 0;
4472 feedback_old_attr = char_attr;
4473 }
4474 char_attr = im_get_feedback_attr(feedback_col);
4475 if (char_attr < 0)
4476 char_attr = feedback_old_attr;
4477 feedback_col++;
4478 }
4479 else if (feedback_old_attr >= 0)
4480 {
4481 char_attr = feedback_old_attr;
4482 feedback_old_attr = -1;
4483 feedback_col = 0;
4484 }
4485 }
4486#endif
4487 /*
4488 * Handle the case where we are in column 0 but not on the first
4489 * character of the line and the user wants us to show us a
4490 * special character (via 'listchars' option "precedes:<char>".
4491 */
4492 if (lcs_prec_todo != NUL
4493 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4494#ifdef FEAT_DIFF
4495 && filler_todo <= 0
4496#endif
4497 && draw_state > WL_NR
4498 && c != NUL)
4499 {
4500 c = lcs_prec;
4501 lcs_prec_todo = NUL;
4502#ifdef FEAT_MBYTE
4503 mb_c = c;
4504 if (enc_utf8 && (*mb_char2len)(c) > 1)
4505 {
4506 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004507 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004508 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510 else
4511 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4512#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004513 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
4515 saved_attr3 = char_attr; /* save current attr */
4516 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4517 n_attr3 = 1;
4518 }
4519 }
4520
4521 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004522 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004524 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004525#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004526 || did_line_attr == 1
4527#endif
4528 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004530#ifdef FEAT_SEARCH_EXTRA
4531 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004532
4533 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004534 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004535 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004536#endif
4537
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 /* invert at least one char, used for Visual and empty line or
4539 * highlight match at end of line. If it's beyond the last
4540 * char on the screen, just overwrite that one (tricky!) Not
4541 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004542#ifdef FEAT_SEARCH_EXTRA
4543 prevcol_hl_flag = FALSE;
4544 if (prevcol == (long)search_hl.startcol)
4545 prevcol_hl_flag = TRUE;
4546 else
4547 {
4548 cur = wp->w_match_head;
4549 while (cur != NULL)
4550 {
4551 if (prevcol == (long)cur->hl.startcol)
4552 {
4553 prevcol_hl_flag = TRUE;
4554 break;
4555 }
4556 cur = cur->next;
4557 }
4558 }
4559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004561 && ((area_attr != 0 && vcol == fromcol
4562#ifdef FEAT_VISUAL
4563 && (VIsual_mode != Ctrl_V
4564 || lnum == VIsual.lnum
4565 || lnum == curwin->w_cursor.lnum)
4566#endif
4567 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568#ifdef FEAT_SEARCH_EXTRA
4569 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004570 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004571# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004572 && did_line_attr <= 1
4573# endif
4574 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575#endif
4576 ))
4577 {
4578 int n = 0;
4579
4580#ifdef FEAT_RIGHTLEFT
4581 if (wp->w_p_rl)
4582 {
4583 if (col < 0)
4584 n = 1;
4585 }
4586 else
4587#endif
4588 {
4589 if (col >= W_WIDTH(wp))
4590 n = -1;
4591 }
4592 if (n != 0)
4593 {
4594 /* At the window boundary, highlight the last character
4595 * instead (better than nothing). */
4596 off += n;
4597 col += n;
4598 }
4599 else
4600 {
4601 /* Add a blank character to highlight. */
4602 ScreenLines[off] = ' ';
4603#ifdef FEAT_MBYTE
4604 if (enc_utf8)
4605 ScreenLinesUC[off] = 0;
4606#endif
4607 }
4608#ifdef FEAT_SEARCH_EXTRA
4609 if (area_attr == 0)
4610 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004611 /* Use attributes from match with highest priority among
4612 * 'search_hl' and the match list. */
4613 char_attr = search_hl.attr;
4614 cur = wp->w_match_head;
4615 shl_flag = FALSE;
4616 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004617 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004618 if (shl_flag == FALSE
4619 && ((cur != NULL
4620 && cur->priority > SEARCH_HL_PRIORITY)
4621 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004622 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004623 shl = &search_hl;
4624 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004625 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004626 else
4627 shl = &cur->hl;
4628 if ((ptr - line) - 1 == (long)shl->startcol)
4629 char_attr = shl->attr;
4630 if (shl != &search_hl && cur != NULL)
4631 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
4634#endif
4635 ScreenAttrs[off] = char_attr;
4636#ifdef FEAT_RIGHTLEFT
4637 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004640 --off;
4641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 else
4643#endif
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 Moolenaar600dddc2006-03-12 22:05:10 +00004648 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004649#ifdef FEAT_SYN_HL
4650 eol_hl_off = 1;
4651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
Bram Moolenaar91170f82006-05-05 21:15:17 +00004655 /*
4656 * At end of the text line.
4657 */
4658 if (c == NUL)
4659 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004660#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004661 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4662 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004663 {
4664 /* highlight last char after line */
4665 --col;
4666 --off;
4667 --vcol;
4668 }
4669
Bram Moolenaar1a384422010-07-14 19:53:30 +02004670 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004671 if (wp->w_p_wrap)
4672 v = wp->w_skipcol;
4673 else
4674 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004675
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004676 /* check if line ends before left margin */
4677 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004678 vcol = v + col - win_col_off(wp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004679
4680 if (draw_color_col)
4681 draw_color_col = advance_color_col(vcol, &color_cols);
4682
4683 if (((wp->w_p_cuc
4684 && (int)wp->w_virtcol >= vcol - eol_hl_off
4685 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004686 + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004687 && lnum != wp->w_cursor.lnum)
4688 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004689# ifdef FEAT_RIGHTLEFT
4690 && !wp->w_p_rl
4691# endif
4692 )
4693 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004694 int rightmost_vcol = 0;
4695 int i;
4696
4697 if (wp->w_p_cuc)
4698 rightmost_vcol = wp->w_virtcol;
4699 if (draw_color_col)
4700 /* determine rightmost colorcolumn to possibly draw */
4701 for (i = 0; color_cols[i] >= 0; ++i)
4702 if (rightmost_vcol < color_cols[i])
4703 rightmost_vcol = color_cols[i];
4704
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004705 while (col < W_WIDTH(wp))
4706 {
4707 ScreenLines[off] = ' ';
4708#ifdef FEAT_MBYTE
4709 if (enc_utf8)
4710 ScreenLinesUC[off] = 0;
4711#endif
4712 ++col;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004713 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol)
4714 ScreenAttrs[off++] = hl_attr(HLF_CUC);
4715 else if (draw_color_col && vcol == *color_cols)
4716 ScreenAttrs[off++] = hl_attr(HLF_MC);
4717 else
4718 ScreenAttrs[off++] = 0;
4719
4720 if (vcol >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004721 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004722
4723 if (draw_color_col)
4724 draw_color_col = advance_color_col(vcol, &color_cols);
4725
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004726 ++vcol;
4727 }
4728 }
4729#endif
4730
Bram Moolenaar860cae12010-06-05 23:22:07 +02004731#ifdef FEAT_CONCEAL
4732 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
4733 (int)W_WIDTH(wp), wp->w_p_rl);
4734 boguscols = 0;
4735#else
4736 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4737 (int)W_WIDTH(wp), wp->w_p_rl);
4738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 row++;
4740
4741 /*
4742 * Update w_cline_height and w_cline_folded if the cursor line was
4743 * updated (saves a call to plines() later).
4744 */
4745 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4746 {
4747 curwin->w_cline_row = startrow;
4748 curwin->w_cline_height = row - startrow;
4749#ifdef FEAT_FOLDING
4750 curwin->w_cline_folded = FALSE;
4751#endif
4752 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4753 }
4754
4755 break;
4756 }
4757
4758 /* line continues beyond line end */
4759 if (lcs_ext
4760 && !wp->w_p_wrap
4761#ifdef FEAT_DIFF
4762 && filler_todo <= 0
4763#endif
4764 && (
4765#ifdef FEAT_RIGHTLEFT
4766 wp->w_p_rl ? col == 0 :
4767#endif
4768 col == W_WIDTH(wp) - 1)
4769 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004770 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4772 {
4773 c = lcs_ext;
4774 char_attr = hl_attr(HLF_AT);
4775#ifdef FEAT_MBYTE
4776 mb_c = c;
4777 if (enc_utf8 && (*mb_char2len)(c) > 1)
4778 {
4779 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004780 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004781 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 else
4784 mb_utf8 = FALSE;
4785#endif
4786 }
4787
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004788#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004789 /* advance to the next 'colorcolumn' */
4790 if (draw_color_col)
4791 draw_color_col = advance_color_col(vcol, &color_cols);
4792
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004793 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004794 * highlight the cursor position itself.
4795 * Also highlight the 'colorcolumn' if it is different than
4796 * 'cursorcolumn' */
4797 vcol_save_attr = -1;
4798 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004799 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004800 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
4801 && lnum != wp->w_cursor.lnum)
4802 {
4803 vcol_save_attr = char_attr;
4804 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4805 }
4806 else if (draw_color_col && vcol == *color_cols)
4807 {
4808 vcol_save_attr = char_attr;
4809 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4810 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004811 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004812#endif
4813
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 /*
4815 * Store character to be displayed.
4816 * Skip characters that are left of the screen for 'nowrap'.
4817 */
4818 vcol_prev = vcol;
4819 if (draw_state < WL_LINE || n_skip <= 0)
4820 {
4821 /*
4822 * Store the character.
4823 */
4824#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4825 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4826 {
4827 /* A double-wide character is: put first halve in left cell. */
4828 --off;
4829 --col;
4830 }
4831#endif
4832 ScreenLines[off] = c;
4833#ifdef FEAT_MBYTE
4834 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004835 {
4836 if ((mb_c & 0xff00) == 0x8e00)
4837 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 else if (enc_utf8)
4841 {
4842 if (mb_utf8)
4843 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004844 int i;
4845
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004847 if ((c & 0xff) == 0)
4848 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004849 for (i = 0; i < Screen_mco; ++i)
4850 {
4851 ScreenLinesC[i][off] = u8cc[i];
4852 if (u8cc[i] == 0)
4853 break;
4854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 else
4857 ScreenLinesUC[off] = 0;
4858 }
4859 if (multi_attr)
4860 {
4861 ScreenAttrs[off] = multi_attr;
4862 multi_attr = 0;
4863 }
4864 else
4865#endif
4866 ScreenAttrs[off] = char_attr;
4867
4868#ifdef FEAT_MBYTE
4869 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4870 {
4871 /* Need to fill two screen columns. */
4872 ++off;
4873 ++col;
4874 if (enc_utf8)
4875 /* UTF-8: Put a 0 in the second screen char. */
4876 ScreenLines[off] = 0;
4877 else
4878 /* DBCS: Put second byte in the second screen char. */
4879 ScreenLines[off] = mb_c & 0xff;
4880 ++vcol;
4881 /* When "tocol" is halfway a character, set it to the end of
4882 * the character, otherwise highlighting won't stop. */
4883 if (tocol == vcol)
4884 ++tocol;
4885#ifdef FEAT_RIGHTLEFT
4886 if (wp->w_p_rl)
4887 {
4888 /* now it's time to backup one cell */
4889 --off;
4890 --col;
4891 }
4892#endif
4893 }
4894#endif
4895#ifdef FEAT_RIGHTLEFT
4896 if (wp->w_p_rl)
4897 {
4898 --off;
4899 --col;
4900 }
4901 else
4902#endif
4903 {
4904 ++off;
4905 ++col;
4906 }
4907 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004908#ifdef FEAT_CONCEAL
4909 else if (wp->w_p_conceal && is_concealing)
4910 {
4911 --n_skip;
4912# ifdef FEAT_HLCOLUMN
4913 if (hlc)
4914 {
4915 ++hlc;
4916 if (n_extra > 0)
4917 hlc += n_extra;
4918 }
4919# endif
4920 if (wp->w_p_wrap)
4921 {
4922 /*
4923 * Special voodoo required if 'wrap' is on.
4924 *
4925 * Advance the column indicator to force the line
4926 * drawing to wrap early. This will make the line
4927 * take up the same screen space when parts are concealed,
4928 * so that cursor line computations aren't messed up.
4929 *
4930 * To avoid the fictitious advance of 'col' causing
4931 * trailing junk to be written out of the screen line
4932 * we are building, 'boguscols' keeps track of the number
4933 * of bad columns we have advanced.
4934 */
4935 if (n_extra > 0)
4936 {
4937 vcol += n_extra;
4938# ifdef FEAT_RIGHTLEFT
4939 if (wp->w_p_rl)
4940 {
4941 col -= n_extra;
4942 boguscols -= n_extra;
4943 }
4944 else
4945# endif
4946 {
4947 col += n_extra;
4948 boguscols += n_extra;
4949 }
4950 n_extra = 0;
4951 n_attr = 0;
4952 }
4953
4954
4955# ifdef FEAT_MBYTE
4956 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4957 {
4958 /* Need to fill two screen columns. */
4959# ifdef FEAT_RIGHTLEFT
4960 if (wp->w_p_rl)
4961 {
4962 --boguscols;
4963 --col;
4964 }
4965 else
4966# endif
4967 {
4968 ++boguscols;
4969 ++col;
4970 }
4971 }
4972# endif
4973
4974# ifdef FEAT_RIGHTLEFT
4975 if (wp->w_p_rl)
4976 {
4977 --boguscols;
4978 --col;
4979 }
4980 else
4981# endif
4982 {
4983 ++boguscols;
4984 ++col;
4985 }
4986 }
4987 else
4988 {
4989 if (n_extra > 0)
4990 {
4991 vcol += n_extra;
4992 n_extra = 0;
4993 n_attr = 0;
4994 }
4995 }
4996
4997 }
4998#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 else
5000 --n_skip;
5001
Bram Moolenaar64486672010-05-16 15:46:46 +02005002 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5003 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005004 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005#ifdef FEAT_DIFF
5006 && filler_todo <= 0
5007#endif
5008 )
5009 ++vcol;
5010
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005011#ifdef FEAT_SYN_HL
5012 if (vcol_save_attr >= 0)
5013 char_attr = vcol_save_attr;
5014#endif
5015
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 /* restore attributes after "predeces" in 'listchars' */
5017 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5018 char_attr = saved_attr3;
5019
5020 /* restore attributes after last 'listchars' or 'number' char */
5021 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5022 char_attr = saved_attr2;
5023
5024 /*
5025 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005026 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 */
5028 if ((
5029#ifdef FEAT_RIGHTLEFT
5030 wp->w_p_rl ? (col < 0) :
5031#endif
5032 (col >= W_WIDTH(wp)))
5033 && (*ptr != NUL
5034#ifdef FEAT_DIFF
5035 || filler_todo > 0
5036#endif
5037 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
5038 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5039 )
5040 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005041#ifdef FEAT_CONCEAL
5042 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5043 (int)W_WIDTH(wp), wp->w_p_rl);
5044 boguscols = 0;
5045#else
5046 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5047 (int)W_WIDTH(wp), wp->w_p_rl);
5048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 ++row;
5050 ++screen_row;
5051
5052 /* When not wrapping and finished diff lines, or when displayed
5053 * '$' and highlighting until last column, break here. */
5054 if ((!wp->w_p_wrap
5055#ifdef FEAT_DIFF
5056 && filler_todo <= 0
5057#endif
5058 ) || lcs_eol_one == -1)
5059 break;
5060
5061 /* When the window is too narrow draw all "@" lines. */
5062 if (draw_state != WL_LINE
5063#ifdef FEAT_DIFF
5064 && filler_todo <= 0
5065#endif
5066 )
5067 {
5068 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5069#ifdef FEAT_VERTSPLIT
5070 draw_vsep_win(wp, row);
5071#endif
5072 row = endrow;
5073 }
5074
5075 /* When line got too long for screen break here. */
5076 if (row == endrow)
5077 {
5078 ++row;
5079 break;
5080 }
5081
5082 if (screen_cur_row == screen_row - 1
5083#ifdef FEAT_DIFF
5084 && filler_todo <= 0
5085#endif
5086 && W_WIDTH(wp) == Columns)
5087 {
5088 /* Remember that the line wraps, used for modeless copy. */
5089 LineWraps[screen_row - 1] = TRUE;
5090
5091 /*
5092 * Special trick to make copy/paste of wrapped lines work with
5093 * xterm/screen: write an extra character beyond the end of
5094 * the line. This will work with all terminal types
5095 * (regardless of the xn,am settings).
5096 * Only do this on a fast tty.
5097 * Only do this if the cursor is on the current line
5098 * (something has been written in it).
5099 * Don't do this for the GUI.
5100 * Don't do this for double-width characters.
5101 * Don't do this for a window not at the right screen border.
5102 */
5103 if (p_tf
5104#ifdef FEAT_GUI
5105 && !gui.in_use
5106#endif
5107#ifdef FEAT_MBYTE
5108 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005109 && ((*mb_off2cells)(LineOffset[screen_row],
5110 LineOffset[screen_row] + screen_Columns)
5111 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005113 + (int)Columns - 2,
5114 LineOffset[screen_row] + screen_Columns)
5115 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116#endif
5117 )
5118 {
5119 /* First make sure we are at the end of the screen line,
5120 * then output the same character again to let the
5121 * terminal know about the wrap. If the terminal doesn't
5122 * auto-wrap, we overwrite the character. */
5123 if (screen_cur_col != W_WIDTH(wp))
5124 screen_char(LineOffset[screen_row - 1]
5125 + (unsigned)Columns - 1,
5126 screen_row - 1, (int)(Columns - 1));
5127
5128#ifdef FEAT_MBYTE
5129 /* When there is a multi-byte character, just output a
5130 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005131 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5132 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 out_char(' ');
5134 else
5135#endif
5136 out_char(ScreenLines[LineOffset[screen_row - 1]
5137 + (Columns - 1)]);
5138 /* force a redraw of the first char on the next line */
5139 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5140 screen_start(); /* don't know where cursor is now */
5141 }
5142 }
5143
5144 col = 0;
5145 off = (unsigned)(current_ScreenLine - ScreenLines);
5146#ifdef FEAT_RIGHTLEFT
5147 if (wp->w_p_rl)
5148 {
5149 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5150 off += col;
5151 }
5152#endif
5153
5154 /* reset the drawing state for the start of a wrapped line */
5155 draw_state = WL_START;
5156 saved_n_extra = n_extra;
5157 saved_p_extra = p_extra;
5158 saved_c_extra = c_extra;
5159 saved_char_attr = char_attr;
5160 n_extra = 0;
5161 lcs_prec_todo = lcs_prec;
5162#ifdef FEAT_LINEBREAK
5163# ifdef FEAT_DIFF
5164 if (filler_todo <= 0)
5165# endif
5166 need_showbreak = TRUE;
5167#endif
5168#ifdef FEAT_DIFF
5169 --filler_todo;
5170 /* When the filler lines are actually below the last line of the
5171 * file, don't draw the line itself, break here. */
5172 if (filler_todo == 0 && wp->w_botfill)
5173 break;
5174#endif
5175 }
5176
5177 } /* for every character in the line */
5178
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005179#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005180 /* After an empty line check first word for capital. */
5181 if (*skipwhite(line) == NUL)
5182 {
5183 capcol_lnum = lnum + 1;
5184 cap_col = 0;
5185 }
5186#endif
5187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 return row;
5189}
5190
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005191#ifdef FEAT_MBYTE
5192static int comp_char_differs __ARGS((int, int));
5193
5194/*
5195 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005196 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005197 */
5198 static int
5199comp_char_differs(off_from, off_to)
5200 int off_from;
5201 int off_to;
5202{
5203 int i;
5204
5205 for (i = 0; i < Screen_mco; ++i)
5206 {
5207 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5208 return TRUE;
5209 if (ScreenLinesC[i][off_from] == 0)
5210 break;
5211 }
5212 return FALSE;
5213}
5214#endif
5215
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216/*
5217 * Check whether the given character needs redrawing:
5218 * - the (first byte of the) character is different
5219 * - the attributes are different
5220 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005221 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 */
5223 static int
5224char_needs_redraw(off_from, off_to, cols)
5225 int off_from;
5226 int off_to;
5227 int cols;
5228{
5229 if (cols > 0
5230 && ((ScreenLines[off_from] != ScreenLines[off_to]
5231 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5232
5233#ifdef FEAT_MBYTE
5234 || (enc_dbcs != 0
5235 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5236 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5237 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5238 : (cols > 1 && ScreenLines[off_from + 1]
5239 != ScreenLines[off_to + 1])))
5240 || (enc_utf8
5241 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5242 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005243 && comp_char_differs(off_from, off_to))
5244 || (cols > 1 && ScreenLines[off_from + 1]
5245 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#endif
5247 ))
5248 return TRUE;
5249 return FALSE;
5250}
5251
5252/*
5253 * Move one "cooked" screen line to the screen, but only the characters that
5254 * have actually changed. Handle insert/delete character.
5255 * "coloff" gives the first column on the screen for this line.
5256 * "endcol" gives the columns where valid characters are.
5257 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5258 * needs to be cleared, negative otherwise.
5259 * "rlflag" is TRUE in a rightleft window:
5260 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5261 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5262 */
5263 static void
5264screen_line(row, coloff, endcol, clear_width
5265#ifdef FEAT_RIGHTLEFT
5266 , rlflag
5267#endif
5268 )
5269 int row;
5270 int coloff;
5271 int endcol;
5272 int clear_width;
5273#ifdef FEAT_RIGHTLEFT
5274 int rlflag;
5275#endif
5276{
5277 unsigned off_from;
5278 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005279#ifdef FEAT_MBYTE
5280 unsigned max_off_from;
5281 unsigned max_off_to;
5282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 int col = 0;
5284#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5285 int hl;
5286#endif
5287 int force = FALSE; /* force update rest of the line */
5288 int redraw_this /* bool: does character need redraw? */
5289#ifdef FEAT_GUI
5290 = TRUE /* For GUI when while-loop empty */
5291#endif
5292 ;
5293 int redraw_next; /* redraw_this for next character */
5294#ifdef FEAT_MBYTE
5295 int clear_next = FALSE;
5296 int char_cells; /* 1: normal char */
5297 /* 2: occupies two display cells */
5298# define CHAR_CELLS char_cells
5299#else
5300# define CHAR_CELLS 1
5301#endif
5302
5303# ifdef FEAT_CLIPBOARD
5304 clip_may_clear_selection(row, row);
5305# endif
5306
5307 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5308 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005309#ifdef FEAT_MBYTE
5310 max_off_from = off_from + screen_Columns;
5311 max_off_to = LineOffset[row] + screen_Columns;
5312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314#ifdef FEAT_RIGHTLEFT
5315 if (rlflag)
5316 {
5317 /* Clear rest first, because it's left of the text. */
5318 if (clear_width > 0)
5319 {
5320 while (col <= endcol && ScreenLines[off_to] == ' '
5321 && ScreenAttrs[off_to] == 0
5322# ifdef FEAT_MBYTE
5323 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5324# endif
5325 )
5326 {
5327 ++off_to;
5328 ++col;
5329 }
5330 if (col <= endcol)
5331 screen_fill(row, row + 1, col + coloff,
5332 endcol + coloff + 1, ' ', ' ', 0);
5333 }
5334 col = endcol + 1;
5335 off_to = LineOffset[row] + col + coloff;
5336 off_from += col;
5337 endcol = (clear_width > 0 ? clear_width : -clear_width);
5338 }
5339#endif /* FEAT_RIGHTLEFT */
5340
5341 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5342
5343 while (col < endcol)
5344 {
5345#ifdef FEAT_MBYTE
5346 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005347 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 else
5349 char_cells = 1;
5350#endif
5351
5352 redraw_this = redraw_next;
5353 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5354 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5355
5356#ifdef FEAT_GUI
5357 /* If the next character was bold, then redraw the current character to
5358 * remove any pixels that might have spilt over into us. This only
5359 * happens in the GUI.
5360 */
5361 if (redraw_next && gui.in_use)
5362 {
5363 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005364 if (hl > HL_ALL)
5365 hl = syn_attr2attr(hl);
5366 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 redraw_this = TRUE;
5368 }
5369#endif
5370
5371 if (redraw_this)
5372 {
5373 /*
5374 * Special handling when 'xs' termcap flag set (hpterm):
5375 * Attributes for characters are stored at the position where the
5376 * cursor is when writing the highlighting code. The
5377 * start-highlighting code must be written with the cursor on the
5378 * first highlighted character. The stop-highlighting code must
5379 * be written with the cursor just after the last highlighted
5380 * character.
5381 * Overwriting a character doesn't remove it's highlighting. Need
5382 * to clear the rest of the line, and force redrawing it
5383 * completely.
5384 */
5385 if ( p_wiv
5386 && !force
5387#ifdef FEAT_GUI
5388 && !gui.in_use
5389#endif
5390 && ScreenAttrs[off_to] != 0
5391 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5392 {
5393 /*
5394 * Need to remove highlighting attributes here.
5395 */
5396 windgoto(row, col + coloff);
5397 out_str(T_CE); /* clear rest of this screen line */
5398 screen_start(); /* don't know where cursor is now */
5399 force = TRUE; /* force redraw of rest of the line */
5400 redraw_next = TRUE; /* or else next char would miss out */
5401
5402 /*
5403 * If the previous character was highlighted, need to stop
5404 * highlighting at this character.
5405 */
5406 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5407 {
5408 screen_attr = ScreenAttrs[off_to - 1];
5409 term_windgoto(row, col + coloff);
5410 screen_stop_highlight();
5411 }
5412 else
5413 screen_attr = 0; /* highlighting has stopped */
5414 }
5415#ifdef FEAT_MBYTE
5416 if (enc_dbcs != 0)
5417 {
5418 /* Check if overwriting a double-byte with a single-byte or
5419 * the other way around requires another character to be
5420 * redrawn. For UTF-8 this isn't needed, because comparing
5421 * ScreenLinesUC[] is sufficient. */
5422 if (char_cells == 1
5423 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005424 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
5426 /* Writing a single-cell character over a double-cell
5427 * character: need to redraw the next cell. */
5428 ScreenLines[off_to + 1] = 0;
5429 redraw_next = TRUE;
5430 }
5431 else if (char_cells == 2
5432 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005433 && (*mb_off2cells)(off_to, max_off_to) == 1
5434 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
5436 /* Writing the second half of a double-cell character over
5437 * a double-cell character: need to redraw the second
5438 * cell. */
5439 ScreenLines[off_to + 2] = 0;
5440 redraw_next = TRUE;
5441 }
5442
5443 if (enc_dbcs == DBCS_JPNU)
5444 ScreenLines2[off_to] = ScreenLines2[off_from];
5445 }
5446 /* When writing a single-width character over a double-width
5447 * character and at the end of the redrawn text, need to clear out
5448 * the right halve of the old character.
5449 * Also required when writing the right halve of a double-width
5450 * char over the left halve of an existing one. */
5451 if (has_mbyte && col + char_cells == endcol
5452 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005453 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005455 && (*mb_off2cells)(off_to, max_off_to) == 1
5456 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 clear_next = TRUE;
5458#endif
5459
5460 ScreenLines[off_to] = ScreenLines[off_from];
5461#ifdef FEAT_MBYTE
5462 if (enc_utf8)
5463 {
5464 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5465 if (ScreenLinesUC[off_from] != 0)
5466 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005467 int i;
5468
5469 for (i = 0; i < Screen_mco; ++i)
5470 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 }
5473 if (char_cells == 2)
5474 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5475#endif
5476
5477#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005478 /* The bold trick makes a single column of pixels appear in the
5479 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 * character should be redrawn too. This happens for our own GUI
5481 * and for some xterms. */
5482 if (
5483# ifdef FEAT_GUI
5484 gui.in_use
5485# endif
5486# if defined(FEAT_GUI) && defined(UNIX)
5487 ||
5488# endif
5489# ifdef UNIX
5490 term_is_xterm
5491# endif
5492 )
5493 {
5494 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005495 if (hl > HL_ALL)
5496 hl = syn_attr2attr(hl);
5497 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 redraw_next = TRUE;
5499 }
5500#endif
5501 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5502#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005503 /* For simplicity set the attributes of second half of a
5504 * double-wide character equal to the first half. */
5505 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005507
5508 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 else
5511#endif
5512 screen_char(off_to, row, col + coloff);
5513 }
5514 else if ( p_wiv
5515#ifdef FEAT_GUI
5516 && !gui.in_use
5517#endif
5518 && col + coloff > 0)
5519 {
5520 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5521 {
5522 /*
5523 * Don't output stop-highlight when moving the cursor, it will
5524 * stop the highlighting when it should continue.
5525 */
5526 screen_attr = 0;
5527 }
5528 else if (screen_attr != 0)
5529 screen_stop_highlight();
5530 }
5531
5532 off_to += CHAR_CELLS;
5533 off_from += CHAR_CELLS;
5534 col += CHAR_CELLS;
5535 }
5536
5537#ifdef FEAT_MBYTE
5538 if (clear_next)
5539 {
5540 /* Clear the second half of a double-wide character of which the left
5541 * half was overwritten with a single-wide character. */
5542 ScreenLines[off_to] = ' ';
5543 if (enc_utf8)
5544 ScreenLinesUC[off_to] = 0;
5545 screen_char(off_to, row, col + coloff);
5546 }
5547#endif
5548
5549 if (clear_width > 0
5550#ifdef FEAT_RIGHTLEFT
5551 && !rlflag
5552#endif
5553 )
5554 {
5555#ifdef FEAT_GUI
5556 int startCol = col;
5557#endif
5558
5559 /* blank out the rest of the line */
5560 while (col < clear_width && ScreenLines[off_to] == ' '
5561 && ScreenAttrs[off_to] == 0
5562#ifdef FEAT_MBYTE
5563 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5564#endif
5565 )
5566 {
5567 ++off_to;
5568 ++col;
5569 }
5570 if (col < clear_width)
5571 {
5572#ifdef FEAT_GUI
5573 /*
5574 * In the GUI, clearing the rest of the line may leave pixels
5575 * behind if the first character cleared was bold. Some bold
5576 * fonts spill over the left. In this case we redraw the previous
5577 * character too. If we didn't skip any blanks above, then we
5578 * only redraw if the character wasn't already redrawn anyway.
5579 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005580 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
5582 hl = ScreenAttrs[off_to];
5583 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005584 {
5585 int prev_cells = 1;
5586# ifdef FEAT_MBYTE
5587 if (enc_utf8)
5588 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5589 * that its width is 2. */
5590 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5591 else if (enc_dbcs != 0)
5592 {
5593 /* find previous character by counting from first
5594 * column and get its width. */
5595 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005596 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005597
5598 while (off < off_to)
5599 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005600 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005601 off += prev_cells;
5602 }
5603 }
5604
5605 if (enc_dbcs != 0 && prev_cells > 1)
5606 screen_char_2(off_to - prev_cells, row,
5607 col + coloff - prev_cells);
5608 else
5609# endif
5610 screen_char(off_to - prev_cells, row,
5611 col + coloff - prev_cells);
5612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614#endif
5615 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5616 ' ', ' ', 0);
5617#ifdef FEAT_VERTSPLIT
5618 off_to += clear_width - col;
5619 col = clear_width;
5620#endif
5621 }
5622 }
5623
5624 if (clear_width > 0)
5625 {
5626#ifdef FEAT_VERTSPLIT
5627 /* For a window that's left of another, draw the separator char. */
5628 if (col + coloff < Columns)
5629 {
5630 int c;
5631
5632 c = fillchar_vsep(&hl);
5633 if (ScreenLines[off_to] != c
5634# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005635 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5636 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637# endif
5638 || ScreenAttrs[off_to] != hl)
5639 {
5640 ScreenLines[off_to] = c;
5641 ScreenAttrs[off_to] = hl;
5642# ifdef FEAT_MBYTE
5643 if (enc_utf8)
5644 {
5645 if (c >= 0x80)
5646 {
5647 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005648 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 }
5650 else
5651 ScreenLinesUC[off_to] = 0;
5652 }
5653# endif
5654 screen_char(off_to, row, col + coloff);
5655 }
5656 }
5657 else
5658#endif
5659 LineWraps[row] = FALSE;
5660 }
5661}
5662
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005663#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005665 * Mirror text "str" for right-left displaying.
5666 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005668 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669rl_mirror(str)
5670 char_u *str;
5671{
5672 char_u *p1, *p2;
5673 int t;
5674
5675 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5676 {
5677 t = *p1;
5678 *p1 = *p2;
5679 *p2 = t;
5680 }
5681}
5682#endif
5683
5684#if defined(FEAT_WINDOWS) || defined(PROTO)
5685/*
5686 * mark all status lines for redraw; used after first :cd
5687 */
5688 void
5689status_redraw_all()
5690{
5691 win_T *wp;
5692
5693 for (wp = firstwin; wp; wp = wp->w_next)
5694 if (wp->w_status_height)
5695 {
5696 wp->w_redr_status = TRUE;
5697 redraw_later(VALID);
5698 }
5699}
5700
5701/*
5702 * mark all status lines of the current buffer for redraw
5703 */
5704 void
5705status_redraw_curbuf()
5706{
5707 win_T *wp;
5708
5709 for (wp = firstwin; wp; wp = wp->w_next)
5710 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5711 {
5712 wp->w_redr_status = TRUE;
5713 redraw_later(VALID);
5714 }
5715}
5716
5717/*
5718 * Redraw all status lines that need to be redrawn.
5719 */
5720 void
5721redraw_statuslines()
5722{
5723 win_T *wp;
5724
5725 for (wp = firstwin; wp; wp = wp->w_next)
5726 if (wp->w_redr_status)
5727 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005728 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005729 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730}
5731#endif
5732
5733#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5734/*
5735 * Redraw all status lines at the bottom of frame "frp".
5736 */
5737 void
5738win_redraw_last_status(frp)
5739 frame_T *frp;
5740{
5741 if (frp->fr_layout == FR_LEAF)
5742 frp->fr_win->w_redr_status = TRUE;
5743 else if (frp->fr_layout == FR_ROW)
5744 {
5745 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5746 win_redraw_last_status(frp);
5747 }
5748 else /* frp->fr_layout == FR_COL */
5749 {
5750 frp = frp->fr_child;
5751 while (frp->fr_next != NULL)
5752 frp = frp->fr_next;
5753 win_redraw_last_status(frp);
5754 }
5755}
5756#endif
5757
5758#ifdef FEAT_VERTSPLIT
5759/*
5760 * Draw the verticap separator right of window "wp" starting with line "row".
5761 */
5762 static void
5763draw_vsep_win(wp, row)
5764 win_T *wp;
5765 int row;
5766{
5767 int hl;
5768 int c;
5769
5770 if (wp->w_vsep_width)
5771 {
5772 /* draw the vertical separator right of this window */
5773 c = fillchar_vsep(&hl);
5774 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5775 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5776 c, ' ', hl);
5777 }
5778}
5779#endif
5780
5781#ifdef FEAT_WILDMENU
5782static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005783static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005786 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 */
5788 static int
5789status_match_len(xp, s)
5790 expand_T *xp;
5791 char_u *s;
5792{
5793 int len = 0;
5794
5795#ifdef FEAT_MENU
5796 int emenu = (xp->xp_context == EXPAND_MENUS
5797 || xp->xp_context == EXPAND_MENUNAMES);
5798
5799 /* Check for menu separators - replace with '|'. */
5800 if (emenu && menu_is_separator(s))
5801 return 1;
5802#endif
5803
5804 while (*s != NUL)
5805 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005806 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005807 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005808 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 }
5810
5811 return len;
5812}
5813
5814/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005815 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005816 * These are backslashes used for escaping. Do show backslashes in help tags.
5817 */
5818 static int
5819skip_status_match_char(xp, s)
5820 expand_T *xp;
5821 char_u *s;
5822{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005823 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005824#ifdef FEAT_MENU
5825 || ((xp->xp_context == EXPAND_MENUS
5826 || xp->xp_context == EXPAND_MENUNAMES)
5827 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5828#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005829 )
5830 {
5831#ifndef BACKSLASH_IN_FILENAME
5832 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5833 return 2;
5834#endif
5835 return 1;
5836 }
5837 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005838}
5839
5840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 * Show wildchar matches in the status line.
5842 * Show at least the "match" item.
5843 * We start at item 'first_match' in the list and show all matches that fit.
5844 *
5845 * If inversion is possible we use it. Else '=' characters are used.
5846 */
5847 void
5848win_redr_status_matches(xp, num_matches, matches, match, showtail)
5849 expand_T *xp;
5850 int num_matches;
5851 char_u **matches; /* list of matches */
5852 int match;
5853 int showtail;
5854{
5855#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5856 int row;
5857 char_u *buf;
5858 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005859 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 int fillchar;
5861 int attr;
5862 int i;
5863 int highlight = TRUE;
5864 char_u *selstart = NULL;
5865 int selstart_col = 0;
5866 char_u *selend = NULL;
5867 static int first_match = 0;
5868 int add_left = FALSE;
5869 char_u *s;
5870#ifdef FEAT_MENU
5871 int emenu;
5872#endif
5873#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5874 int l;
5875#endif
5876
5877 if (matches == NULL) /* interrupted completion? */
5878 return;
5879
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005880#ifdef FEAT_MBYTE
5881 if (has_mbyte)
5882 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5883 else
5884#endif
5885 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (buf == NULL)
5887 return;
5888
5889 if (match == -1) /* don't show match but original text */
5890 {
5891 match = 0;
5892 highlight = FALSE;
5893 }
5894 /* count 1 for the ending ">" */
5895 clen = status_match_len(xp, L_MATCH(match)) + 3;
5896 if (match == 0)
5897 first_match = 0;
5898 else if (match < first_match)
5899 {
5900 /* jumping left, as far as we can go */
5901 first_match = match;
5902 add_left = TRUE;
5903 }
5904 else
5905 {
5906 /* check if match fits on the screen */
5907 for (i = first_match; i < match; ++i)
5908 clen += status_match_len(xp, L_MATCH(i)) + 2;
5909 if (first_match > 0)
5910 clen += 2;
5911 /* jumping right, put match at the left */
5912 if ((long)clen > Columns)
5913 {
5914 first_match = match;
5915 /* if showing the last match, we can add some on the left */
5916 clen = 2;
5917 for (i = match; i < num_matches; ++i)
5918 {
5919 clen += status_match_len(xp, L_MATCH(i)) + 2;
5920 if ((long)clen >= Columns)
5921 break;
5922 }
5923 if (i == num_matches)
5924 add_left = TRUE;
5925 }
5926 }
5927 if (add_left)
5928 while (first_match > 0)
5929 {
5930 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5931 if ((long)clen >= Columns)
5932 break;
5933 --first_match;
5934 }
5935
5936 fillchar = fillchar_status(&attr, TRUE);
5937
5938 if (first_match == 0)
5939 {
5940 *buf = NUL;
5941 len = 0;
5942 }
5943 else
5944 {
5945 STRCPY(buf, "< ");
5946 len = 2;
5947 }
5948 clen = len;
5949
5950 i = first_match;
5951 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5952 {
5953 if (i == match)
5954 {
5955 selstart = buf + len;
5956 selstart_col = clen;
5957 }
5958
5959 s = L_MATCH(i);
5960 /* Check for menu separators - replace with '|' */
5961#ifdef FEAT_MENU
5962 emenu = (xp->xp_context == EXPAND_MENUS
5963 || xp->xp_context == EXPAND_MENUNAMES);
5964 if (emenu && menu_is_separator(s))
5965 {
5966 STRCPY(buf + len, transchar('|'));
5967 l = (int)STRLEN(buf + len);
5968 len += l;
5969 clen += l;
5970 }
5971 else
5972#endif
5973 for ( ; *s != NUL; ++s)
5974 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005975 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 clen += ptr2cells(s);
5977#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005978 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 STRNCPY(buf + len, s, l);
5981 s += l - 1;
5982 len += l;
5983 }
5984 else
5985#endif
5986 {
5987 STRCPY(buf + len, transchar_byte(*s));
5988 len += (int)STRLEN(buf + len);
5989 }
5990 }
5991 if (i == match)
5992 selend = buf + len;
5993
5994 *(buf + len++) = ' ';
5995 *(buf + len++) = ' ';
5996 clen += 2;
5997 if (++i == num_matches)
5998 break;
5999 }
6000
6001 if (i != num_matches)
6002 {
6003 *(buf + len++) = '>';
6004 ++clen;
6005 }
6006
6007 buf[len] = NUL;
6008
6009 row = cmdline_row - 1;
6010 if (row >= 0)
6011 {
6012 if (wild_menu_showing == 0)
6013 {
6014 if (msg_scrolled > 0)
6015 {
6016 /* Put the wildmenu just above the command line. If there is
6017 * no room, scroll the screen one line up. */
6018 if (cmdline_row == Rows - 1)
6019 {
6020 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6021 ++msg_scrolled;
6022 }
6023 else
6024 {
6025 ++cmdline_row;
6026 ++row;
6027 }
6028 wild_menu_showing = WM_SCROLLED;
6029 }
6030 else
6031 {
6032 /* Create status line if needed by setting 'laststatus' to 2.
6033 * Set 'winminheight' to zero to avoid that the window is
6034 * resized. */
6035 if (lastwin->w_status_height == 0)
6036 {
6037 save_p_ls = p_ls;
6038 save_p_wmh = p_wmh;
6039 p_ls = 2;
6040 p_wmh = 0;
6041 last_status(FALSE);
6042 }
6043 wild_menu_showing = WM_SHOWN;
6044 }
6045 }
6046
6047 screen_puts(buf, row, 0, attr);
6048 if (selstart != NULL && highlight)
6049 {
6050 *selend = NUL;
6051 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6052 }
6053
6054 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6055 }
6056
6057#ifdef FEAT_VERTSPLIT
6058 win_redraw_last_status(topframe);
6059#else
6060 lastwin->w_redr_status = TRUE;
6061#endif
6062 vim_free(buf);
6063}
6064#endif
6065
6066#if defined(FEAT_WINDOWS) || defined(PROTO)
6067/*
6068 * Redraw the status line of window wp.
6069 *
6070 * If inversion is possible we use it. Else '=' characters are used.
6071 */
6072 void
6073win_redr_status(wp)
6074 win_T *wp;
6075{
6076 int row;
6077 char_u *p;
6078 int len;
6079 int fillchar;
6080 int attr;
6081 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006082 static int busy = FALSE;
6083
6084 /* It's possible to get here recursively when 'statusline' (indirectly)
6085 * invokes ":redrawstatus". Simply ignore the call then. */
6086 if (busy)
6087 return;
6088 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089
6090 wp->w_redr_status = FALSE;
6091 if (wp->w_status_height == 0)
6092 {
6093 /* no status line, can only be last window */
6094 redraw_cmdline = TRUE;
6095 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006096 else if (!redrawing()
6097#ifdef FEAT_INS_EXPAND
6098 /* don't update status line when popup menu is visible and may be
6099 * drawn over it */
6100 || pum_visible()
6101#endif
6102 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 {
6104 /* Don't redraw right now, do it later. */
6105 wp->w_redr_status = TRUE;
6106 }
6107#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006108 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 {
6110 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006111 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 }
6113#endif
6114 else
6115 {
6116 fillchar = fillchar_status(&attr, wp == curwin);
6117
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006118 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 p = NameBuff;
6120 len = (int)STRLEN(p);
6121
6122 if (wp->w_buffer->b_help
6123#ifdef FEAT_QUICKFIX
6124 || wp->w_p_pvw
6125#endif
6126 || bufIsChanged(wp->w_buffer)
6127 || wp->w_buffer->b_p_ro)
6128 *(p + len++) = ' ';
6129 if (wp->w_buffer->b_help)
6130 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006131 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 len += (int)STRLEN(p + len);
6133 }
6134#ifdef FEAT_QUICKFIX
6135 if (wp->w_p_pvw)
6136 {
6137 STRCPY(p + len, _("[Preview]"));
6138 len += (int)STRLEN(p + len);
6139 }
6140#endif
6141 if (bufIsChanged(wp->w_buffer))
6142 {
6143 STRCPY(p + len, "[+]");
6144 len += 3;
6145 }
6146 if (wp->w_buffer->b_p_ro)
6147 {
6148 STRCPY(p + len, "[RO]");
6149 len += 4;
6150 }
6151
6152#ifndef FEAT_VERTSPLIT
6153 this_ru_col = ru_col;
6154 if (this_ru_col < (Columns + 1) / 2)
6155 this_ru_col = (Columns + 1) / 2;
6156#else
6157 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6158 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6159 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6160 if (this_ru_col <= 1)
6161 {
6162 p = (char_u *)"<"; /* No room for file name! */
6163 len = 1;
6164 }
6165 else
6166#endif
6167#ifdef FEAT_MBYTE
6168 if (has_mbyte)
6169 {
6170 int clen = 0, i;
6171
6172 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006173 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 clen += (*mb_ptr2cells)(p + i);
6175 /* Find first character that will fit.
6176 * Going from start to end is much faster for DBCS. */
6177 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006178 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 clen -= (*mb_ptr2cells)(p + i);
6180 len = clen;
6181 if (i > 0)
6182 {
6183 p = p + i - 1;
6184 *p = '<';
6185 ++len;
6186 }
6187
6188 }
6189 else
6190#endif
6191 if (len > this_ru_col - 1)
6192 {
6193 p += len - (this_ru_col - 1);
6194 *p = '<';
6195 len = this_ru_col - 1;
6196 }
6197
6198 row = W_WINROW(wp) + wp->w_height;
6199 screen_puts(p, row, W_WINCOL(wp), attr);
6200 screen_fill(row, row + 1, len + W_WINCOL(wp),
6201 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6202
6203 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6204 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6205 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6206 - 1 + W_WINCOL(wp)), attr);
6207
6208#ifdef FEAT_CMDL_INFO
6209 win_redr_ruler(wp, TRUE);
6210#endif
6211 }
6212
6213#ifdef FEAT_VERTSPLIT
6214 /*
6215 * May need to draw the character below the vertical separator.
6216 */
6217 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6218 {
6219 if (stl_connected(wp))
6220 fillchar = fillchar_status(&attr, wp == curwin);
6221 else
6222 fillchar = fillchar_vsep(&attr);
6223 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6224 attr);
6225 }
6226#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006227 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228}
6229
Bram Moolenaar238a5642006-02-21 22:12:05 +00006230#ifdef FEAT_STL_OPT
6231/*
6232 * Redraw the status line according to 'statusline' and take care of any
6233 * errors encountered.
6234 */
6235 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006236redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006237 win_T *wp;
6238{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006239 static int entered = FALSE;
6240 int save_called_emsg = called_emsg;
6241
6242 /* When called recursively return. This can happen when the statusline
6243 * contains an expression that triggers a redraw. */
6244 if (entered)
6245 return;
6246 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006247
6248 called_emsg = FALSE;
6249 win_redr_custom(wp, FALSE);
6250 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006251 {
6252 /* When there is an error disable the statusline, otherwise the
6253 * display is messed up with errors and a redraw triggers the problem
6254 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006255 set_string_option_direct((char_u *)"statusline", -1,
6256 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006257 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006258 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006259 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006260 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006261}
6262#endif
6263
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264# ifdef FEAT_VERTSPLIT
6265/*
6266 * Return TRUE if the status line of window "wp" is connected to the status
6267 * line of the window right of it. If not, then it's a vertical separator.
6268 * Only call if (wp->w_vsep_width != 0).
6269 */
6270 int
6271stl_connected(wp)
6272 win_T *wp;
6273{
6274 frame_T *fr;
6275
6276 fr = wp->w_frame;
6277 while (fr->fr_parent != NULL)
6278 {
6279 if (fr->fr_parent->fr_layout == FR_COL)
6280 {
6281 if (fr->fr_next != NULL)
6282 break;
6283 }
6284 else
6285 {
6286 if (fr->fr_next != NULL)
6287 return TRUE;
6288 }
6289 fr = fr->fr_parent;
6290 }
6291 return FALSE;
6292}
6293# endif
6294
6295#endif /* FEAT_WINDOWS */
6296
6297#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6298/*
6299 * Get the value to show for the language mappings, active 'keymap'.
6300 */
6301 int
6302get_keymap_str(wp, buf, len)
6303 win_T *wp;
6304 char_u *buf; /* buffer for the result */
6305 int len; /* length of buffer */
6306{
6307 char_u *p;
6308
6309 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6310 return FALSE;
6311
6312 {
6313#ifdef FEAT_EVAL
6314 buf_T *old_curbuf = curbuf;
6315 win_T *old_curwin = curwin;
6316 char_u *s;
6317
6318 curbuf = wp->w_buffer;
6319 curwin = wp;
6320 STRCPY(buf, "b:keymap_name"); /* must be writable */
6321 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006322 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 --emsg_skip;
6324 curbuf = old_curbuf;
6325 curwin = old_curwin;
6326 if (p == NULL || *p == NUL)
6327#endif
6328 {
6329#ifdef FEAT_KEYMAP
6330 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6331 p = wp->w_buffer->b_p_keymap;
6332 else
6333#endif
6334 p = (char_u *)"lang";
6335 }
6336 if ((int)(STRLEN(p) + 3) < len)
6337 sprintf((char *)buf, "<%s>", p);
6338 else
6339 buf[0] = NUL;
6340#ifdef FEAT_EVAL
6341 vim_free(s);
6342#endif
6343 }
6344 return buf[0] != NUL;
6345}
6346#endif
6347
6348#if defined(FEAT_STL_OPT) || defined(PROTO)
6349/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006350 * Redraw the status line or ruler of window "wp".
6351 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 */
6353 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006354win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006356 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 int attr;
6359 int curattr;
6360 int row;
6361 int col = 0;
6362 int maxwidth;
6363 int width;
6364 int n;
6365 int len;
6366 int fillchar;
6367 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006368 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006370 struct stl_hlrec hltab[STL_MAX_ITEM];
6371 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006372 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
6374 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006375 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006377 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006378 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006379 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006380 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006381 attr = hl_attr(HLF_TPF);
6382 maxwidth = Columns;
6383# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006384 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006387 else
6388 {
6389 row = W_WINROW(wp) + wp->w_height;
6390 fillchar = fillchar_status(&attr, wp == curwin);
6391 maxwidth = W_WIDTH(wp);
6392
6393 if (draw_ruler)
6394 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006395 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006396 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006397 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006398 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006399 if (*++stl == '-')
6400 stl++;
6401 if (atoi((char *)stl))
6402 while (VIM_ISDIGIT(*stl))
6403 stl++;
6404 if (*stl++ != '(')
6405 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006406 }
6407#ifdef FEAT_VERTSPLIT
6408 col = ru_col - (Columns - W_WIDTH(wp));
6409 if (col < (W_WIDTH(wp) + 1) / 2)
6410 col = (W_WIDTH(wp) + 1) / 2;
6411#else
6412 col = ru_col;
6413 if (col > (Columns + 1) / 2)
6414 col = (Columns + 1) / 2;
6415#endif
6416 maxwidth = W_WIDTH(wp) - col;
6417#ifdef FEAT_WINDOWS
6418 if (!wp->w_status_height)
6419#endif
6420 {
6421 row = Rows - 1;
6422 --maxwidth; /* writing in last column may cause scrolling */
6423 fillchar = ' ';
6424 attr = 0;
6425 }
6426
6427# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006428 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006429# endif
6430 }
6431 else
6432 {
6433 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006434 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006435 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006436 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006437# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006438 use_sandbox = was_set_insecurely((char_u *)"statusline",
6439 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006440# endif
6441 }
6442
6443#ifdef FEAT_VERTSPLIT
6444 col += W_WINCOL(wp);
6445#endif
6446 }
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (maxwidth <= 0)
6449 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
Bram Moolenaar362f3562009-11-03 16:20:34 +00006451 /* Make a copy, because the statusline may include a function call that
6452 * might change the option value and free the memory. */
6453 stl = vim_strsave(stl);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006454 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6455 buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006456 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006457 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006458 vim_free(stl);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006459 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006461 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 {
6463#ifdef FEAT_MBYTE
6464 len += (*mb_char2bytes)(fillchar, buf + len);
6465#else
6466 buf[len++] = fillchar;
6467#endif
6468 ++width;
6469 }
6470 buf[len] = NUL;
6471
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006472 /*
6473 * Draw each snippet with the specified highlighting.
6474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 curattr = attr;
6476 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006477 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006479 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 screen_puts_len(p, len, row, col, curattr);
6481 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006482 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006484 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006486 else if (hltab[n].userhl < 0)
6487 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006489 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006490 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491#endif
6492 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006493 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 }
6495 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006496
6497 if (wp == NULL)
6498 {
6499 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6500 col = 0;
6501 len = 0;
6502 p = buf;
6503 fillchar = 0;
6504 for (n = 0; tabtab[n].start != NULL; n++)
6505 {
6506 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6507 while (col < len)
6508 TabPageIdxs[col++] = fillchar;
6509 p = tabtab[n].start;
6510 fillchar = tabtab[n].userhl;
6511 }
6512 while (col < Columns)
6513 TabPageIdxs[col++] = fillchar;
6514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515}
6516
6517#endif /* FEAT_STL_OPT */
6518
6519/*
6520 * Output a single character directly to the screen and update ScreenLines.
6521 */
6522 void
6523screen_putchar(c, row, col, attr)
6524 int c;
6525 int row, col;
6526 int attr;
6527{
6528#ifdef FEAT_MBYTE
6529 char_u buf[MB_MAXBYTES + 1];
6530
6531 buf[(*mb_char2bytes)(c, buf)] = NUL;
6532#else
6533 char_u buf[2];
6534
6535 buf[0] = c;
6536 buf[1] = NUL;
6537#endif
6538 screen_puts(buf, row, col, attr);
6539}
6540
6541/*
6542 * Get a single character directly from ScreenLines into "bytes[]".
6543 * Also return its attribute in *attrp;
6544 */
6545 void
6546screen_getbytes(row, col, bytes, attrp)
6547 int row, col;
6548 char_u *bytes;
6549 int *attrp;
6550{
6551 unsigned off;
6552
6553 /* safety check */
6554 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6555 {
6556 off = LineOffset[row] + col;
6557 *attrp = ScreenAttrs[off];
6558 bytes[0] = ScreenLines[off];
6559 bytes[1] = NUL;
6560
6561#ifdef FEAT_MBYTE
6562 if (enc_utf8 && ScreenLinesUC[off] != 0)
6563 bytes[utfc_char2bytes(off, bytes)] = NUL;
6564 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6565 {
6566 bytes[0] = ScreenLines[off];
6567 bytes[1] = ScreenLines2[off];
6568 bytes[2] = NUL;
6569 }
6570 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6571 {
6572 bytes[1] = ScreenLines[off + 1];
6573 bytes[2] = NUL;
6574 }
6575#endif
6576 }
6577}
6578
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006579#ifdef FEAT_MBYTE
6580static int screen_comp_differs __ARGS((int, int*));
6581
6582/*
6583 * Return TRUE if composing characters for screen posn "off" differs from
6584 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006585 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006586 */
6587 static int
6588screen_comp_differs(off, u8cc)
6589 int off;
6590 int *u8cc;
6591{
6592 int i;
6593
6594 for (i = 0; i < Screen_mco; ++i)
6595 {
6596 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6597 return TRUE;
6598 if (u8cc[i] == 0)
6599 break;
6600 }
6601 return FALSE;
6602}
6603#endif
6604
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605/*
6606 * Put string '*text' on the screen at position 'row' and 'col', with
6607 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6608 * Note: only outputs within one row, message is truncated at screen boundary!
6609 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6610 */
6611 void
6612screen_puts(text, row, col, attr)
6613 char_u *text;
6614 int row;
6615 int col;
6616 int attr;
6617{
6618 screen_puts_len(text, -1, row, col, attr);
6619}
6620
6621/*
6622 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6623 * a NUL.
6624 */
6625 void
6626screen_puts_len(text, len, row, col, attr)
6627 char_u *text;
6628 int len;
6629 int row;
6630 int col;
6631 int attr;
6632{
6633 unsigned off;
6634 char_u *ptr = text;
6635 int c;
6636#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006637 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 int mbyte_blen = 1;
6639 int mbyte_cells = 1;
6640 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006641 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 int clear_next_cell = FALSE;
6643# ifdef FEAT_ARABIC
6644 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006645 int pc, nc, nc1;
6646 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647# endif
6648#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006649#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6650 int force_redraw_this;
6651 int force_redraw_next = FALSE;
6652#endif
6653 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654
6655 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6656 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006657 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
Bram Moolenaarc236c162008-07-13 17:41:49 +00006659#ifdef FEAT_MBYTE
6660 /* When drawing over the right halve of a double-wide char clear out the
6661 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006662 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006663# ifdef FEAT_GUI
6664 && !gui.in_use
6665# endif
6666 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006667 {
6668 ScreenLines[off - 1] = ' ';
6669 ScreenAttrs[off - 1] = 0;
6670 if (enc_utf8)
6671 {
6672 ScreenLinesUC[off - 1] = 0;
6673 ScreenLinesC[0][off - 1] = 0;
6674 }
6675 /* redraw the previous cell, make it empty */
6676 screen_char(off - 1, row, col - 1);
6677 /* force the cell at "col" to be redrawn */
6678 force_redraw_next = TRUE;
6679 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006680#endif
6681
Bram Moolenaar367329b2007-08-30 11:53:22 +00006682#ifdef FEAT_MBYTE
6683 max_off = LineOffset[row] + screen_Columns;
6684#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006685 while (col < screen_Columns
6686 && (len < 0 || (int)(ptr - text) < len)
6687 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
6689 c = *ptr;
6690#ifdef FEAT_MBYTE
6691 /* check if this is the first byte of a multibyte */
6692 if (has_mbyte)
6693 {
6694 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006695 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006697 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6699 mbyte_cells = 1;
6700 else if (enc_dbcs != 0)
6701 mbyte_cells = mbyte_blen;
6702 else /* enc_utf8 */
6703 {
6704 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006705 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 (int)((text + len) - ptr));
6707 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006708 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006710# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 /* Non-BMP character: display as ? or fullwidth ?. */
6712 if (u8c >= 0x10000)
6713 {
6714 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6715 if (attr == 0)
6716 attr = hl_attr(HLF_8);
6717 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719# ifdef FEAT_ARABIC
6720 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6721 {
6722 /* Do Arabic shaping. */
6723 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6724 {
6725 /* Past end of string to be displayed. */
6726 nc = NUL;
6727 nc1 = NUL;
6728 }
6729 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006730 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006731 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6732 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006733 nc1 = pcc[0];
6734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 pc = prev_c;
6736 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006737 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 }
6739 else
6740 prev_c = u8c;
6741# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006742 if (col + mbyte_cells > screen_Columns)
6743 {
6744 /* Only 1 cell left, but character requires 2 cells:
6745 * display a '>' in the last column to avoid wrapping. */
6746 c = '>';
6747 mbyte_cells = 1;
6748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
6750 }
6751#endif
6752
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006753#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6754 force_redraw_this = force_redraw_next;
6755 force_redraw_next = FALSE;
6756#endif
6757
6758 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759#ifdef FEAT_MBYTE
6760 || (mbyte_cells == 2
6761 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6762 || (enc_dbcs == DBCS_JPNU
6763 && c == 0x8e
6764 && ScreenLines2[off] != ptr[1])
6765 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006766 && (ScreenLinesUC[off] !=
6767 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6768 || (ScreenLinesUC[off] != 0
6769 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770#endif
6771 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006772 || exmode_active;
6773
6774 if (need_redraw
6775#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6776 || force_redraw_this
6777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 )
6779 {
6780#if defined(FEAT_GUI) || defined(UNIX)
6781 /* The bold trick makes a single row of pixels appear in the next
6782 * character. When a bold character is removed, the next
6783 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006784 * and for some xterms. */
6785 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786# ifdef FEAT_GUI
6787 gui.in_use
6788# endif
6789# if defined(FEAT_GUI) && defined(UNIX)
6790 ||
6791# endif
6792# ifdef UNIX
6793 term_is_xterm
6794# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006795 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006797 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006799 if (n > HL_ALL)
6800 n = syn_attr2attr(n);
6801 if (n & HL_BOLD)
6802 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
6804#endif
6805#ifdef FEAT_MBYTE
6806 /* When at the end of the text and overwriting a two-cell
6807 * character with a one-cell character, need to clear the next
6808 * cell. Also when overwriting the left halve of a two-cell char
6809 * with the right halve of a two-cell char. Do this only once
6810 * (mb_off2cells() may return 2 on the right halve). */
6811 if (clear_next_cell)
6812 clear_next_cell = FALSE;
6813 else if (has_mbyte
6814 && (len < 0 ? ptr[mbyte_blen] == NUL
6815 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006816 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006818 && (*mb_off2cells)(off, max_off) == 1
6819 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 clear_next_cell = TRUE;
6821
6822 /* Make sure we never leave a second byte of a double-byte behind,
6823 * it confuses mb_off2cells(). */
6824 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006825 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006827 && (*mb_off2cells)(off, max_off) == 1
6828 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 ScreenLines[off + mbyte_blen] = 0;
6830#endif
6831 ScreenLines[off] = c;
6832 ScreenAttrs[off] = attr;
6833#ifdef FEAT_MBYTE
6834 if (enc_utf8)
6835 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006836 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 ScreenLinesUC[off] = 0;
6838 else
6839 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006840 int i;
6841
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006843 for (i = 0; i < Screen_mco; ++i)
6844 {
6845 ScreenLinesC[i][off] = u8cc[i];
6846 if (u8cc[i] == 0)
6847 break;
6848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 }
6850 if (mbyte_cells == 2)
6851 {
6852 ScreenLines[off + 1] = 0;
6853 ScreenAttrs[off + 1] = attr;
6854 }
6855 screen_char(off, row, col);
6856 }
6857 else if (mbyte_cells == 2)
6858 {
6859 ScreenLines[off + 1] = ptr[1];
6860 ScreenAttrs[off + 1] = attr;
6861 screen_char_2(off, row, col);
6862 }
6863 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6864 {
6865 ScreenLines2[off] = ptr[1];
6866 screen_char(off, row, col);
6867 }
6868 else
6869#endif
6870 screen_char(off, row, col);
6871 }
6872#ifdef FEAT_MBYTE
6873 if (has_mbyte)
6874 {
6875 off += mbyte_cells;
6876 col += mbyte_cells;
6877 ptr += mbyte_blen;
6878 if (clear_next_cell)
6879 ptr = (char_u *)" ";
6880 }
6881 else
6882#endif
6883 {
6884 ++off;
6885 ++col;
6886 ++ptr;
6887 }
6888 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006889
6890#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6891 /* If we detected the next character needs to be redrawn, but the text
6892 * doesn't extend up to there, update the character here. */
6893 if (force_redraw_next && col < screen_Columns)
6894 {
6895# ifdef FEAT_MBYTE
6896 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6897 screen_char_2(off, row, col);
6898 else
6899# endif
6900 screen_char(off, row, col);
6901 }
6902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903}
6904
6905#ifdef FEAT_SEARCH_EXTRA
6906/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006907 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 */
6909 static void
6910start_search_hl()
6911{
6912 if (p_hls && !no_hlsearch)
6913 {
6914 last_pat_prog(&search_hl.rm);
6915 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006916# ifdef FEAT_RELTIME
6917 /* Set the time limit to 'redrawtime'. */
6918 profile_setlimit(p_rdt, &search_hl.tm);
6919# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 }
6921}
6922
6923/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006924 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 */
6926 static void
6927end_search_hl()
6928{
6929 if (search_hl.rm.regprog != NULL)
6930 {
6931 vim_free(search_hl.rm.regprog);
6932 search_hl.rm.regprog = NULL;
6933 }
6934}
6935
6936/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02006937 * Init for calling prepare_search_hl().
6938 */
6939 static void
6940init_search_hl(wp)
6941 win_T *wp;
6942{
6943 matchitem_T *cur;
6944
6945 /* Setup for match and 'hlsearch' highlighting. Disable any previous
6946 * match */
6947 cur = wp->w_match_head;
6948 while (cur != NULL)
6949 {
6950 cur->hl.rm = cur->match;
6951 if (cur->hlg_id == 0)
6952 cur->hl.attr = 0;
6953 else
6954 cur->hl.attr = syn_id2attr(cur->hlg_id);
6955 cur->hl.buf = wp->w_buffer;
6956 cur->hl.lnum = 0;
6957 cur->hl.first_lnum = 0;
6958# ifdef FEAT_RELTIME
6959 /* Set the time limit to 'redrawtime'. */
6960 profile_setlimit(p_rdt, &(cur->hl.tm));
6961# endif
6962 cur = cur->next;
6963 }
6964 search_hl.buf = wp->w_buffer;
6965 search_hl.lnum = 0;
6966 search_hl.first_lnum = 0;
6967 /* time limit is set at the toplevel, for all windows */
6968}
6969
6970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 * Advance to the match in window "wp" line "lnum" or past it.
6972 */
6973 static void
6974prepare_search_hl(wp, lnum)
6975 win_T *wp;
6976 linenr_T lnum;
6977{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006978 matchitem_T *cur; /* points to the match list */
6979 match_T *shl; /* points to search_hl or a match */
6980 int shl_flag; /* flag to indicate whether search_hl
6981 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 int n;
6983
6984 /*
6985 * When using a multi-line pattern, start searching at the top
6986 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006987 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006989 cur = wp->w_match_head;
6990 shl_flag = FALSE;
6991 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006993 if (shl_flag == FALSE)
6994 {
6995 shl = &search_hl;
6996 shl_flag = TRUE;
6997 }
6998 else
6999 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (shl->rm.regprog != NULL
7001 && shl->lnum == 0
7002 && re_multiline(shl->rm.regprog))
7003 {
7004 if (shl->first_lnum == 0)
7005 {
7006# ifdef FEAT_FOLDING
7007 for (shl->first_lnum = lnum;
7008 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7009 if (hasFoldingWin(wp, shl->first_lnum - 1,
7010 NULL, NULL, TRUE, NULL))
7011 break;
7012# else
7013 shl->first_lnum = wp->w_topline;
7014# endif
7015 }
7016 n = 0;
7017 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7018 {
7019 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7020 if (shl->lnum != 0)
7021 {
7022 shl->first_lnum = shl->lnum
7023 + shl->rm.endpos[0].lnum
7024 - shl->rm.startpos[0].lnum;
7025 n = shl->rm.endpos[0].col;
7026 }
7027 else
7028 {
7029 ++shl->first_lnum;
7030 n = 0;
7031 }
7032 }
7033 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007034 if (shl != &search_hl && cur != NULL)
7035 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037}
7038
7039/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007040 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 * Uses shl->buf.
7042 * Sets shl->lnum and shl->rm contents.
7043 * Note: Assumes a previous match is always before "lnum", unless
7044 * shl->lnum is zero.
7045 * Careful: Any pointers for buffer lines will become invalid.
7046 */
7047 static void
7048next_search_hl(win, shl, lnum, mincol)
7049 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007050 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 linenr_T lnum;
7052 colnr_T mincol; /* minimal column for a match */
7053{
7054 linenr_T l;
7055 colnr_T matchcol;
7056 long nmatched;
7057
7058 if (shl->lnum != 0)
7059 {
7060 /* Check for three situations:
7061 * 1. If the "lnum" is below a previous match, start a new search.
7062 * 2. If the previous match includes "mincol", use it.
7063 * 3. Continue after the previous match.
7064 */
7065 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7066 if (lnum > l)
7067 shl->lnum = 0;
7068 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7069 return;
7070 }
7071
7072 /*
7073 * Repeat searching for a match until one is found that includes "mincol"
7074 * or none is found in this line.
7075 */
7076 called_emsg = FALSE;
7077 for (;;)
7078 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007079#ifdef FEAT_RELTIME
7080 /* Stop searching after passing the time limit. */
7081 if (profile_passed_limit(&(shl->tm)))
7082 {
7083 shl->lnum = 0; /* no match found in time */
7084 break;
7085 }
7086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 /* Three situations:
7088 * 1. No useful previous match: search from start of line.
7089 * 2. Not Vi compatible or empty match: continue at next character.
7090 * Break the loop if this is beyond the end of the line.
7091 * 3. Vi compatible searching: continue at end of previous match.
7092 */
7093 if (shl->lnum == 0)
7094 matchcol = 0;
7095 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7096 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007097 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007099 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007100
7101 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007102 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007103 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007105 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 shl->lnum = 0;
7107 break;
7108 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007109#ifdef FEAT_MBYTE
7110 if (has_mbyte)
7111 matchcol += mb_ptr2len(ml);
7112 else
7113#endif
7114 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
7116 else
7117 matchcol = shl->rm.endpos[0].col;
7118
7119 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007120 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7121#ifdef FEAT_RELTIME
7122 &(shl->tm)
7123#else
7124 NULL
7125#endif
7126 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 if (called_emsg)
7128 {
7129 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007130 if (shl == &search_hl)
7131 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007132 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007133 vim_free(shl->rm.regprog);
7134 no_hlsearch = TRUE;
7135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007137 shl->lnum = 0;
7138 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 break;
7140 }
7141 if (nmatched == 0)
7142 {
7143 shl->lnum = 0; /* no match found */
7144 break;
7145 }
7146 if (shl->rm.startpos[0].lnum > 0
7147 || shl->rm.startpos[0].col >= mincol
7148 || nmatched > 1
7149 || shl->rm.endpos[0].col > mincol)
7150 {
7151 shl->lnum += shl->rm.startpos[0].lnum;
7152 break; /* useful match found */
7153 }
7154 }
7155}
7156#endif
7157
7158 static void
7159screen_start_highlight(attr)
7160 int attr;
7161{
7162 attrentry_T *aep = NULL;
7163
7164 screen_attr = attr;
7165 if (full_screen
7166#ifdef WIN3264
7167 && termcap_active
7168#endif
7169 )
7170 {
7171#ifdef FEAT_GUI
7172 if (gui.in_use)
7173 {
7174 char buf[20];
7175
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007176 /* The GUI handles this internally. */
7177 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 OUT_STR(buf);
7179 }
7180 else
7181#endif
7182 {
7183 if (attr > HL_ALL) /* special HL attr. */
7184 {
7185 if (t_colors > 1)
7186 aep = syn_cterm_attr2entry(attr);
7187 else
7188 aep = syn_term_attr2entry(attr);
7189 if (aep == NULL) /* did ":syntax clear" */
7190 attr = 0;
7191 else
7192 attr = aep->ae_attr;
7193 }
7194 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7195 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007196 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7197 && cterm_normal_fg_bold)
7198 /* If the Normal FG color has BOLD attribute and the new HL
7199 * has a FG color defined, clear BOLD. */
7200 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7202 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007203 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7204 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 out_str(T_US);
7206 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7207 out_str(T_CZH);
7208 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7209 out_str(T_MR);
7210
7211 /*
7212 * Output the color or start string after bold etc., in case the
7213 * bold etc. override the color setting.
7214 */
7215 if (aep != NULL)
7216 {
7217 if (t_colors > 1)
7218 {
7219 if (aep->ae_u.cterm.fg_color)
7220 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7221 if (aep->ae_u.cterm.bg_color)
7222 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7223 }
7224 else
7225 {
7226 if (aep->ae_u.term.start != NULL)
7227 out_str(aep->ae_u.term.start);
7228 }
7229 }
7230 }
7231 }
7232}
7233
7234 void
7235screen_stop_highlight()
7236{
7237 int do_ME = FALSE; /* output T_ME code */
7238
7239 if (screen_attr != 0
7240#ifdef WIN3264
7241 && termcap_active
7242#endif
7243 )
7244 {
7245#ifdef FEAT_GUI
7246 if (gui.in_use)
7247 {
7248 char buf[20];
7249
7250 /* use internal GUI code */
7251 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7252 OUT_STR(buf);
7253 }
7254 else
7255#endif
7256 {
7257 if (screen_attr > HL_ALL) /* special HL attr. */
7258 {
7259 attrentry_T *aep;
7260
7261 if (t_colors > 1)
7262 {
7263 /*
7264 * Assume that t_me restores the original colors!
7265 */
7266 aep = syn_cterm_attr2entry(screen_attr);
7267 if (aep != NULL && (aep->ae_u.cterm.fg_color
7268 || aep->ae_u.cterm.bg_color))
7269 do_ME = TRUE;
7270 }
7271 else
7272 {
7273 aep = syn_term_attr2entry(screen_attr);
7274 if (aep != NULL && aep->ae_u.term.stop != NULL)
7275 {
7276 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7277 do_ME = TRUE;
7278 else
7279 out_str(aep->ae_u.term.stop);
7280 }
7281 }
7282 if (aep == NULL) /* did ":syntax clear" */
7283 screen_attr = 0;
7284 else
7285 screen_attr = aep->ae_attr;
7286 }
7287
7288 /*
7289 * Often all ending-codes are equal to T_ME. Avoid outputting the
7290 * same sequence several times.
7291 */
7292 if (screen_attr & HL_STANDOUT)
7293 {
7294 if (STRCMP(T_SE, T_ME) == 0)
7295 do_ME = TRUE;
7296 else
7297 out_str(T_SE);
7298 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007299 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
7301 if (STRCMP(T_UE, T_ME) == 0)
7302 do_ME = TRUE;
7303 else
7304 out_str(T_UE);
7305 }
7306 if (screen_attr & HL_ITALIC)
7307 {
7308 if (STRCMP(T_CZR, T_ME) == 0)
7309 do_ME = TRUE;
7310 else
7311 out_str(T_CZR);
7312 }
7313 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7314 out_str(T_ME);
7315
7316 if (t_colors > 1)
7317 {
7318 /* set Normal cterm colors */
7319 if (cterm_normal_fg_color != 0)
7320 term_fg_color(cterm_normal_fg_color - 1);
7321 if (cterm_normal_bg_color != 0)
7322 term_bg_color(cterm_normal_bg_color - 1);
7323 if (cterm_normal_fg_bold)
7324 out_str(T_MD);
7325 }
7326 }
7327 }
7328 screen_attr = 0;
7329}
7330
7331/*
7332 * Reset the colors for a cterm. Used when leaving Vim.
7333 * The machine specific code may override this again.
7334 */
7335 void
7336reset_cterm_colors()
7337{
7338 if (t_colors > 1)
7339 {
7340 /* set Normal cterm colors */
7341 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7342 {
7343 out_str(T_OP);
7344 screen_attr = -1;
7345 }
7346 if (cterm_normal_fg_bold)
7347 {
7348 out_str(T_ME);
7349 screen_attr = -1;
7350 }
7351 }
7352}
7353
7354/*
7355 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7356 * using the attributes from ScreenAttrs["off"].
7357 */
7358 static void
7359screen_char(off, row, col)
7360 unsigned off;
7361 int row;
7362 int col;
7363{
7364 int attr;
7365
7366 /* Check for illegal values, just in case (could happen just after
7367 * resizing). */
7368 if (row >= screen_Rows || col >= screen_Columns)
7369 return;
7370
7371 /* Outputting the last character on the screen may scrollup the screen.
7372 * Don't to it! Mark the character invalid (update it when scrolled up) */
7373 if (row == screen_Rows - 1 && col == screen_Columns - 1
7374#ifdef FEAT_RIGHTLEFT
7375 /* account for first command-line character in rightleft mode */
7376 && !cmdmsg_rl
7377#endif
7378 )
7379 {
7380 ScreenAttrs[off] = (sattr_T)-1;
7381 return;
7382 }
7383
7384 /*
7385 * Stop highlighting first, so it's easier to move the cursor.
7386 */
7387#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7388 if (screen_char_attr != 0)
7389 attr = screen_char_attr;
7390 else
7391#endif
7392 attr = ScreenAttrs[off];
7393 if (screen_attr != attr)
7394 screen_stop_highlight();
7395
7396 windgoto(row, col);
7397
7398 if (screen_attr != attr)
7399 screen_start_highlight(attr);
7400
7401#ifdef FEAT_MBYTE
7402 if (enc_utf8 && ScreenLinesUC[off] != 0)
7403 {
7404 char_u buf[MB_MAXBYTES + 1];
7405
7406 /* Convert UTF-8 character to bytes and write it. */
7407
7408 buf[utfc_char2bytes(off, buf)] = NUL;
7409
7410 out_str(buf);
7411 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7412 ++screen_cur_col;
7413 }
7414 else
7415#endif
7416 {
7417#ifdef FEAT_MBYTE
7418 out_flush_check();
7419#endif
7420 out_char(ScreenLines[off]);
7421#ifdef FEAT_MBYTE
7422 /* double-byte character in single-width cell */
7423 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7424 out_char(ScreenLines2[off]);
7425#endif
7426 }
7427
7428 screen_cur_col++;
7429}
7430
7431#ifdef FEAT_MBYTE
7432
7433/*
7434 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7435 * on the screen at position 'row' and 'col'.
7436 * The attributes of the first byte is used for all. This is required to
7437 * output the two bytes of a double-byte character with nothing in between.
7438 */
7439 static void
7440screen_char_2(off, row, col)
7441 unsigned off;
7442 int row;
7443 int col;
7444{
7445 /* Check for illegal values (could be wrong when screen was resized). */
7446 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7447 return;
7448
7449 /* Outputting the last character on the screen may scrollup the screen.
7450 * Don't to it! Mark the character invalid (update it when scrolled up) */
7451 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7452 {
7453 ScreenAttrs[off] = (sattr_T)-1;
7454 return;
7455 }
7456
7457 /* Output the first byte normally (positions the cursor), then write the
7458 * second byte directly. */
7459 screen_char(off, row, col);
7460 out_char(ScreenLines[off + 1]);
7461 ++screen_cur_col;
7462}
7463#endif
7464
7465#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7466/*
7467 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7468 * This uses the contents of ScreenLines[] and doesn't change it.
7469 */
7470 void
7471screen_draw_rectangle(row, col, height, width, invert)
7472 int row;
7473 int col;
7474 int height;
7475 int width;
7476 int invert;
7477{
7478 int r, c;
7479 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007480#ifdef FEAT_MBYTE
7481 int max_off;
7482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007484 /* Can't use ScreenLines unless initialized */
7485 if (ScreenLines == NULL)
7486 return;
7487
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 if (invert)
7489 screen_char_attr = HL_INVERSE;
7490 for (r = row; r < row + height; ++r)
7491 {
7492 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007493#ifdef FEAT_MBYTE
7494 max_off = off + screen_Columns;
7495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 for (c = col; c < col + width; ++c)
7497 {
7498#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007499 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 {
7501 screen_char_2(off + c, r, c);
7502 ++c;
7503 }
7504 else
7505#endif
7506 {
7507 screen_char(off + c, r, c);
7508#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007509 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 ++c;
7511#endif
7512 }
7513 }
7514 }
7515 screen_char_attr = 0;
7516}
7517#endif
7518
7519#ifdef FEAT_VERTSPLIT
7520/*
7521 * Redraw the characters for a vertically split window.
7522 */
7523 static void
7524redraw_block(row, end, wp)
7525 int row;
7526 int end;
7527 win_T *wp;
7528{
7529 int col;
7530 int width;
7531
7532# ifdef FEAT_CLIPBOARD
7533 clip_may_clear_selection(row, end - 1);
7534# endif
7535
7536 if (wp == NULL)
7537 {
7538 col = 0;
7539 width = Columns;
7540 }
7541 else
7542 {
7543 col = wp->w_wincol;
7544 width = wp->w_width;
7545 }
7546 screen_draw_rectangle(row, col, end - row, width, FALSE);
7547}
7548#endif
7549
7550/*
7551 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7552 * with character 'c1' in first column followed by 'c2' in the other columns.
7553 * Use attributes 'attr'.
7554 */
7555 void
7556screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7557 int start_row, end_row;
7558 int start_col, end_col;
7559 int c1, c2;
7560 int attr;
7561{
7562 int row;
7563 int col;
7564 int off;
7565 int end_off;
7566 int did_delete;
7567 int c;
7568 int norm_term;
7569#if defined(FEAT_GUI) || defined(UNIX)
7570 int force_next = FALSE;
7571#endif
7572
7573 if (end_row > screen_Rows) /* safety check */
7574 end_row = screen_Rows;
7575 if (end_col > screen_Columns) /* safety check */
7576 end_col = screen_Columns;
7577 if (ScreenLines == NULL
7578 || start_row >= end_row
7579 || start_col >= end_col) /* nothing to do */
7580 return;
7581
7582 /* it's a "normal" terminal when not in a GUI or cterm */
7583 norm_term = (
7584#ifdef FEAT_GUI
7585 !gui.in_use &&
7586#endif
7587 t_colors <= 1);
7588 for (row = start_row; row < end_row; ++row)
7589 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007590#ifdef FEAT_MBYTE
7591 if (has_mbyte
7592# ifdef FEAT_GUI
7593 && !gui.in_use
7594# endif
7595 )
7596 {
7597 /* When drawing over the right halve of a double-wide char clear
7598 * out the left halve. When drawing over the left halve of a
7599 * double wide-char clear out the right halve. Only needed in a
7600 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007601 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007602 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007603 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007604 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007605 }
7606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 /*
7608 * Try to use delete-line termcap code, when no attributes or in a
7609 * "normal" terminal, where a bold/italic space is just a
7610 * space.
7611 */
7612 did_delete = FALSE;
7613 if (c2 == ' '
7614 && end_col == Columns
7615 && can_clear(T_CE)
7616 && (attr == 0
7617 || (norm_term
7618 && attr <= HL_ALL
7619 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7620 {
7621 /*
7622 * check if we really need to clear something
7623 */
7624 col = start_col;
7625 if (c1 != ' ') /* don't clear first char */
7626 ++col;
7627
7628 off = LineOffset[row] + col;
7629 end_off = LineOffset[row] + end_col;
7630
7631 /* skip blanks (used often, keep it fast!) */
7632#ifdef FEAT_MBYTE
7633 if (enc_utf8)
7634 while (off < end_off && ScreenLines[off] == ' '
7635 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7636 ++off;
7637 else
7638#endif
7639 while (off < end_off && ScreenLines[off] == ' '
7640 && ScreenAttrs[off] == 0)
7641 ++off;
7642 if (off < end_off) /* something to be cleared */
7643 {
7644 col = off - LineOffset[row];
7645 screen_stop_highlight();
7646 term_windgoto(row, col);/* clear rest of this screen line */
7647 out_str(T_CE);
7648 screen_start(); /* don't know where cursor is now */
7649 col = end_col - col;
7650 while (col--) /* clear chars in ScreenLines */
7651 {
7652 ScreenLines[off] = ' ';
7653#ifdef FEAT_MBYTE
7654 if (enc_utf8)
7655 ScreenLinesUC[off] = 0;
7656#endif
7657 ScreenAttrs[off] = 0;
7658 ++off;
7659 }
7660 }
7661 did_delete = TRUE; /* the chars are cleared now */
7662 }
7663
7664 off = LineOffset[row] + start_col;
7665 c = c1;
7666 for (col = start_col; col < end_col; ++col)
7667 {
7668 if (ScreenLines[off] != c
7669#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007670 || (enc_utf8 && (int)ScreenLinesUC[off]
7671 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672#endif
7673 || ScreenAttrs[off] != attr
7674#if defined(FEAT_GUI) || defined(UNIX)
7675 || force_next
7676#endif
7677 )
7678 {
7679#if defined(FEAT_GUI) || defined(UNIX)
7680 /* The bold trick may make a single row of pixels appear in
7681 * the next character. When a bold character is removed, the
7682 * next character should be redrawn too. This happens for our
7683 * own GUI and for some xterms. */
7684 if (
7685# ifdef FEAT_GUI
7686 gui.in_use
7687# endif
7688# if defined(FEAT_GUI) && defined(UNIX)
7689 ||
7690# endif
7691# ifdef UNIX
7692 term_is_xterm
7693# endif
7694 )
7695 {
7696 if (ScreenLines[off] != ' '
7697 && (ScreenAttrs[off] > HL_ALL
7698 || ScreenAttrs[off] & HL_BOLD))
7699 force_next = TRUE;
7700 else
7701 force_next = FALSE;
7702 }
7703#endif
7704 ScreenLines[off] = c;
7705#ifdef FEAT_MBYTE
7706 if (enc_utf8)
7707 {
7708 if (c >= 0x80)
7709 {
7710 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007711 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
7713 else
7714 ScreenLinesUC[off] = 0;
7715 }
7716#endif
7717 ScreenAttrs[off] = attr;
7718 if (!did_delete || c != ' ')
7719 screen_char(off, row, col);
7720 }
7721 ++off;
7722 if (col == start_col)
7723 {
7724 if (did_delete)
7725 break;
7726 c = c2;
7727 }
7728 }
7729 if (end_col == Columns)
7730 LineWraps[row] = FALSE;
7731 if (row == Rows - 1) /* overwritten the command line */
7732 {
7733 redraw_cmdline = TRUE;
7734 if (c1 == ' ' && c2 == ' ')
7735 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007736 if (start_col == 0)
7737 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739 }
7740}
7741
7742/*
7743 * Check if there should be a delay. Used before clearing or redrawing the
7744 * screen or the command line.
7745 */
7746 void
7747check_for_delay(check_msg_scroll)
7748 int check_msg_scroll;
7749{
7750 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7751 && !did_wait_return
7752 && emsg_silent == 0)
7753 {
7754 out_flush();
7755 ui_delay(1000L, TRUE);
7756 emsg_on_display = FALSE;
7757 if (check_msg_scroll)
7758 msg_scroll = FALSE;
7759 }
7760}
7761
7762/*
7763 * screen_valid - allocate screen buffers if size changed
7764 * If "clear" is TRUE: clear screen if it has been resized.
7765 * Returns TRUE if there is a valid screen to write to.
7766 * Returns FALSE when starting up and screen not initialized yet.
7767 */
7768 int
7769screen_valid(clear)
7770 int clear;
7771{
7772 screenalloc(clear); /* allocate screen buffers if size changed */
7773 return (ScreenLines != NULL);
7774}
7775
7776/*
7777 * Resize the shell to Rows and Columns.
7778 * Allocate ScreenLines[] and associated items.
7779 *
7780 * There may be some time between setting Rows and Columns and (re)allocating
7781 * ScreenLines[]. This happens when starting up and when (manually) changing
7782 * the shell size. Always use screen_Rows and screen_Columns to access items
7783 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7784 * final size of the shell is needed.
7785 */
7786 void
7787screenalloc(clear)
7788 int clear;
7789{
7790 int new_row, old_row;
7791#ifdef FEAT_GUI
7792 int old_Rows;
7793#endif
7794 win_T *wp;
7795 int outofmem = FALSE;
7796 int len;
7797 schar_T *new_ScreenLines;
7798#ifdef FEAT_MBYTE
7799 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007800 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007802 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803#endif
7804 sattr_T *new_ScreenAttrs;
7805 unsigned *new_LineOffset;
7806 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007807#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007808 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007809 tabpage_T *tp;
7810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007812 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007813#ifdef FEAT_AUTOCMD
7814 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007816retry:
7817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 /*
7819 * Allocation of the screen buffers is done only when the size changes and
7820 * when Rows and Columns have been set and we have started doing full
7821 * screen stuff.
7822 */
7823 if ((ScreenLines != NULL
7824 && Rows == screen_Rows
7825 && Columns == screen_Columns
7826#ifdef FEAT_MBYTE
7827 && enc_utf8 == (ScreenLinesUC != NULL)
7828 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007829 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830#endif
7831 )
7832 || Rows == 0
7833 || Columns == 0
7834 || (!full_screen && ScreenLines == NULL))
7835 return;
7836
7837 /*
7838 * It's possible that we produce an out-of-memory message below, which
7839 * will cause this function to be called again. To break the loop, just
7840 * return here.
7841 */
7842 if (entered)
7843 return;
7844 entered = TRUE;
7845
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007846 /*
7847 * Note that the window sizes are updated before reallocating the arrays,
7848 * thus we must not redraw here!
7849 */
7850 ++RedrawingDisabled;
7851
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 win_new_shellsize(); /* fit the windows in the new sized shell */
7853
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 comp_col(); /* recompute columns for shown command and ruler */
7855
7856 /*
7857 * We're changing the size of the screen.
7858 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7859 * - Move lines from the old arrays into the new arrays, clear extra
7860 * lines (unless the screen is going to be cleared).
7861 * - Free the old arrays.
7862 *
7863 * If anything fails, make ScreenLines NULL, so we don't do anything!
7864 * Continuing with the old ScreenLines may result in a crash, because the
7865 * size is wrong.
7866 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007867 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007869#ifdef FEAT_AUTOCMD
7870 if (aucmd_win != NULL)
7871 win_free_lsize(aucmd_win);
7872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873
7874 new_ScreenLines = (schar_T *)lalloc((long_u)(
7875 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7876#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007877 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 if (enc_utf8)
7879 {
7880 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7881 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007882 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007883 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7885 }
7886 if (enc_dbcs == DBCS_JPNU)
7887 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7888 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7889#endif
7890 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7891 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7892 new_LineOffset = (unsigned *)lalloc((long_u)(
7893 Rows * sizeof(unsigned)), FALSE);
7894 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007895#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007896 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007899 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {
7901 if (win_alloc_lines(wp) == FAIL)
7902 {
7903 outofmem = TRUE;
7904#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007905 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906#endif
7907 }
7908 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007909#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007910 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7911 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007912 outofmem = TRUE;
7913#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007914#ifdef FEAT_WINDOWS
7915give_up:
7916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007918#ifdef FEAT_MBYTE
7919 for (i = 0; i < p_mco; ++i)
7920 if (new_ScreenLinesC[i] == NULL)
7921 break;
7922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 if (new_ScreenLines == NULL
7924#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007925 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7927#endif
7928 || new_ScreenAttrs == NULL
7929 || new_LineOffset == NULL
7930 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007931#ifdef FEAT_WINDOWS
7932 || new_TabPageIdxs == NULL
7933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 || outofmem)
7935 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007936 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007937 {
7938 /* guess the size */
7939 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7940
7941 /* Remember we did this to avoid getting outofmem messages over
7942 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007943 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 vim_free(new_ScreenLines);
7946 new_ScreenLines = NULL;
7947#ifdef FEAT_MBYTE
7948 vim_free(new_ScreenLinesUC);
7949 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007950 for (i = 0; i < p_mco; ++i)
7951 {
7952 vim_free(new_ScreenLinesC[i]);
7953 new_ScreenLinesC[i] = NULL;
7954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 vim_free(new_ScreenLines2);
7956 new_ScreenLines2 = NULL;
7957#endif
7958 vim_free(new_ScreenAttrs);
7959 new_ScreenAttrs = NULL;
7960 vim_free(new_LineOffset);
7961 new_LineOffset = NULL;
7962 vim_free(new_LineWraps);
7963 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007964#ifdef FEAT_WINDOWS
7965 vim_free(new_TabPageIdxs);
7966 new_TabPageIdxs = NULL;
7967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 }
7969 else
7970 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007971 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007972
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 for (new_row = 0; new_row < Rows; ++new_row)
7974 {
7975 new_LineOffset[new_row] = new_row * Columns;
7976 new_LineWraps[new_row] = FALSE;
7977
7978 /*
7979 * If the screen is not going to be cleared, copy as much as
7980 * possible from the old screen to the new one and clear the rest
7981 * (used when resizing the window at the "--more--" prompt or when
7982 * executing an external command, for the GUI).
7983 */
7984 if (!clear)
7985 {
7986 (void)vim_memset(new_ScreenLines + new_row * Columns,
7987 ' ', (size_t)Columns * sizeof(schar_T));
7988#ifdef FEAT_MBYTE
7989 if (enc_utf8)
7990 {
7991 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7992 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007993 for (i = 0; i < p_mco; ++i)
7994 (void)vim_memset(new_ScreenLinesC[i]
7995 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 0, (size_t)Columns * sizeof(u8char_T));
7997 }
7998 if (enc_dbcs == DBCS_JPNU)
7999 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8000 0, (size_t)Columns * sizeof(schar_T));
8001#endif
8002 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8003 0, (size_t)Columns * sizeof(sattr_T));
8004 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008005 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {
8007 if (screen_Columns < Columns)
8008 len = screen_Columns;
8009 else
8010 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008011#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008012 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008013 * may be invalid now. Also when p_mco changes. */
8014 if (!(enc_utf8 && ScreenLinesUC == NULL)
8015 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008016#endif
8017 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8018 ScreenLines + LineOffset[old_row],
8019 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008021 if (enc_utf8 && ScreenLinesUC != NULL
8022 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {
8024 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8025 ScreenLinesUC + LineOffset[old_row],
8026 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008027 for (i = 0; i < p_mco; ++i)
8028 mch_memmove(new_ScreenLinesC[i]
8029 + new_LineOffset[new_row],
8030 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 (size_t)len * sizeof(u8char_T));
8032 }
8033 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8034 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8035 ScreenLines2 + LineOffset[old_row],
8036 (size_t)len * sizeof(schar_T));
8037#endif
8038 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8039 ScreenAttrs + LineOffset[old_row],
8040 (size_t)len * sizeof(sattr_T));
8041 }
8042 }
8043 }
8044 /* Use the last line of the screen for the current line. */
8045 current_ScreenLine = new_ScreenLines + Rows * Columns;
8046 }
8047
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008048 free_screenlines();
8049
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 ScreenLines = new_ScreenLines;
8051#ifdef FEAT_MBYTE
8052 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008053 for (i = 0; i < p_mco; ++i)
8054 ScreenLinesC[i] = new_ScreenLinesC[i];
8055 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 ScreenLines2 = new_ScreenLines2;
8057#endif
8058 ScreenAttrs = new_ScreenAttrs;
8059 LineOffset = new_LineOffset;
8060 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008061#ifdef FEAT_WINDOWS
8062 TabPageIdxs = new_TabPageIdxs;
8063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064
8065 /* It's important that screen_Rows and screen_Columns reflect the actual
8066 * size of ScreenLines[]. Set them before calling anything. */
8067#ifdef FEAT_GUI
8068 old_Rows = screen_Rows;
8069#endif
8070 screen_Rows = Rows;
8071 screen_Columns = Columns;
8072
8073 must_redraw = CLEAR; /* need to clear the screen later */
8074 if (clear)
8075 screenclear2();
8076
8077#ifdef FEAT_GUI
8078 else if (gui.in_use
8079 && !gui.starting
8080 && ScreenLines != NULL
8081 && old_Rows != Rows)
8082 {
8083 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8084 /*
8085 * Adjust the position of the cursor, for when executing an external
8086 * command.
8087 */
8088 if (msg_row >= Rows) /* Rows got smaller */
8089 msg_row = Rows - 1; /* put cursor at last row */
8090 else if (Rows > old_Rows) /* Rows got bigger */
8091 msg_row += Rows - old_Rows; /* put cursor in same place */
8092 if (msg_col >= Columns) /* Columns got smaller */
8093 msg_col = Columns - 1; /* put cursor at last column */
8094 }
8095#endif
8096
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008098 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008099
8100#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008101 /*
8102 * Do not apply autocommands more than 3 times to avoid an endless loop
8103 * in case applying autocommands always changes Rows or Columns.
8104 */
8105 if (starting == 0 && ++retry_count <= 3)
8106 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008107 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008108 /* In rare cases, autocommands may have altered Rows or Columns,
8109 * jump back to check if we need to allocate the screen again. */
8110 goto retry;
8111 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113}
8114
8115 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008116free_screenlines()
8117{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008118#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008119 int i;
8120
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008121 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008122 for (i = 0; i < Screen_mco; ++i)
8123 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008124 vim_free(ScreenLines2);
8125#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008126 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008127 vim_free(ScreenAttrs);
8128 vim_free(LineOffset);
8129 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008130#ifdef FEAT_WINDOWS
8131 vim_free(TabPageIdxs);
8132#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008133}
8134
8135 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136screenclear()
8137{
8138 check_for_delay(FALSE);
8139 screenalloc(FALSE); /* allocate screen buffers if size changed */
8140 screenclear2(); /* clear the screen */
8141}
8142
8143 static void
8144screenclear2()
8145{
8146 int i;
8147
8148 if (starting == NO_SCREEN || ScreenLines == NULL
8149#ifdef FEAT_GUI
8150 || (gui.in_use && gui.starting)
8151#endif
8152 )
8153 return;
8154
8155#ifdef FEAT_GUI
8156 if (!gui.in_use)
8157#endif
8158 screen_attr = -1; /* force setting the Normal colors */
8159 screen_stop_highlight(); /* don't want highlighting here */
8160
8161#ifdef FEAT_CLIPBOARD
8162 /* disable selection without redrawing it */
8163 clip_scroll_selection(9999);
8164#endif
8165
8166 /* blank out ScreenLines */
8167 for (i = 0; i < Rows; ++i)
8168 {
8169 lineclear(LineOffset[i], (int)Columns);
8170 LineWraps[i] = FALSE;
8171 }
8172
8173 if (can_clear(T_CL))
8174 {
8175 out_str(T_CL); /* clear the display */
8176 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008177 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
8179 else
8180 {
8181 /* can't clear the screen, mark all chars with invalid attributes */
8182 for (i = 0; i < Rows; ++i)
8183 lineinvalid(LineOffset[i], (int)Columns);
8184 clear_cmdline = TRUE;
8185 }
8186
8187 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8188
8189 win_rest_invalid(firstwin);
8190 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008191#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008192 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 if (must_redraw == CLEAR) /* no need to clear again */
8195 must_redraw = NOT_VALID;
8196 compute_cmdrow();
8197 msg_row = cmdline_row; /* put cursor on last line for messages */
8198 msg_col = 0;
8199 screen_start(); /* don't know where cursor is now */
8200 msg_scrolled = 0; /* can't scroll back */
8201 msg_didany = FALSE;
8202 msg_didout = FALSE;
8203}
8204
8205/*
8206 * Clear one line in ScreenLines.
8207 */
8208 static void
8209lineclear(off, width)
8210 unsigned off;
8211 int width;
8212{
8213 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8214#ifdef FEAT_MBYTE
8215 if (enc_utf8)
8216 (void)vim_memset(ScreenLinesUC + off, 0,
8217 (size_t)width * sizeof(u8char_T));
8218#endif
8219 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8220}
8221
8222/*
8223 * Mark one line in ScreenLines invalid by setting the attributes to an
8224 * invalid value.
8225 */
8226 static void
8227lineinvalid(off, width)
8228 unsigned off;
8229 int width;
8230{
8231 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8232}
8233
8234#ifdef FEAT_VERTSPLIT
8235/*
8236 * Copy part of a Screenline for vertically split window "wp".
8237 */
8238 static void
8239linecopy(to, from, wp)
8240 int to;
8241 int from;
8242 win_T *wp;
8243{
8244 unsigned off_to = LineOffset[to] + wp->w_wincol;
8245 unsigned off_from = LineOffset[from] + wp->w_wincol;
8246
8247 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8248 wp->w_width * sizeof(schar_T));
8249# ifdef FEAT_MBYTE
8250 if (enc_utf8)
8251 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008252 int i;
8253
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8255 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008256 for (i = 0; i < p_mco; ++i)
8257 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8258 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 }
8260 if (enc_dbcs == DBCS_JPNU)
8261 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8262 wp->w_width * sizeof(schar_T));
8263# endif
8264 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8265 wp->w_width * sizeof(sattr_T));
8266}
8267#endif
8268
8269/*
8270 * Return TRUE if clearing with term string "p" would work.
8271 * It can't work when the string is empty or it won't set the right background.
8272 */
8273 int
8274can_clear(p)
8275 char_u *p;
8276{
8277 return (*p != NUL && (t_colors <= 1
8278#ifdef FEAT_GUI
8279 || gui.in_use
8280#endif
8281 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8282}
8283
8284/*
8285 * Reset cursor position. Use whenever cursor was moved because of outputting
8286 * something directly to the screen (shell commands) or a terminal control
8287 * code.
8288 */
8289 void
8290screen_start()
8291{
8292 screen_cur_row = screen_cur_col = 9999;
8293}
8294
8295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 * Move the cursor to position "row","col" in the screen.
8297 * This tries to find the most efficient way to move, minimizing the number of
8298 * characters sent to the terminal.
8299 */
8300 void
8301windgoto(row, col)
8302 int row;
8303 int col;
8304{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008305 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 int i;
8307 int plan;
8308 int cost;
8309 int wouldbe_col;
8310 int noinvcurs;
8311 char_u *bs;
8312 int goto_cost;
8313 int attr;
8314
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008315#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8317
8318#define PLAN_LE 1
8319#define PLAN_CR 2
8320#define PLAN_NL 3
8321#define PLAN_WRITE 4
8322 /* Can't use ScreenLines unless initialized */
8323 if (ScreenLines == NULL)
8324 return;
8325
8326 if (col != screen_cur_col || row != screen_cur_row)
8327 {
8328 /* Check for valid position. */
8329 if (row < 0) /* window without text lines? */
8330 row = 0;
8331 if (row >= screen_Rows)
8332 row = screen_Rows - 1;
8333 if (col >= screen_Columns)
8334 col = screen_Columns - 1;
8335
8336 /* check if no cursor movement is allowed in highlight mode */
8337 if (screen_attr && *T_MS == NUL)
8338 noinvcurs = HIGHL_COST;
8339 else
8340 noinvcurs = 0;
8341 goto_cost = GOTO_COST + noinvcurs;
8342
8343 /*
8344 * Plan how to do the positioning:
8345 * 1. Use CR to move it to column 0, same row.
8346 * 2. Use T_LE to move it a few columns to the left.
8347 * 3. Use NL to move a few lines down, column 0.
8348 * 4. Move a few columns to the right with T_ND or by writing chars.
8349 *
8350 * Don't do this if the cursor went beyond the last column, the cursor
8351 * position is unknown then (some terminals wrap, some don't )
8352 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008353 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 * characters to move the cursor to the right.
8355 */
8356 if (row >= screen_cur_row && screen_cur_col < Columns)
8357 {
8358 /*
8359 * If the cursor is in the same row, bigger col, we can use CR
8360 * or T_LE.
8361 */
8362 bs = NULL; /* init for GCC */
8363 attr = screen_attr;
8364 if (row == screen_cur_row && col < screen_cur_col)
8365 {
8366 /* "le" is preferred over "bc", because "bc" is obsolete */
8367 if (*T_LE)
8368 bs = T_LE; /* "cursor left" */
8369 else
8370 bs = T_BC; /* "backspace character (old) */
8371 if (*bs)
8372 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8373 else
8374 cost = 999;
8375 if (col + 1 < cost) /* using CR is less characters */
8376 {
8377 plan = PLAN_CR;
8378 wouldbe_col = 0;
8379 cost = 1; /* CR is just one character */
8380 }
8381 else
8382 {
8383 plan = PLAN_LE;
8384 wouldbe_col = col;
8385 }
8386 if (noinvcurs) /* will stop highlighting */
8387 {
8388 cost += noinvcurs;
8389 attr = 0;
8390 }
8391 }
8392
8393 /*
8394 * If the cursor is above where we want to be, we can use CR LF.
8395 */
8396 else if (row > screen_cur_row)
8397 {
8398 plan = PLAN_NL;
8399 wouldbe_col = 0;
8400 cost = (row - screen_cur_row) * 2; /* CR LF */
8401 if (noinvcurs) /* will stop highlighting */
8402 {
8403 cost += noinvcurs;
8404 attr = 0;
8405 }
8406 }
8407
8408 /*
8409 * If the cursor is in the same row, smaller col, just use write.
8410 */
8411 else
8412 {
8413 plan = PLAN_WRITE;
8414 wouldbe_col = screen_cur_col;
8415 cost = 0;
8416 }
8417
8418 /*
8419 * Check if any characters that need to be written have the
8420 * correct attributes. Also avoid UTF-8 characters.
8421 */
8422 i = col - wouldbe_col;
8423 if (i > 0)
8424 cost += i;
8425 if (cost < goto_cost && i > 0)
8426 {
8427 /*
8428 * Check if the attributes are correct without additionally
8429 * stopping highlighting.
8430 */
8431 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8432 while (i && *p++ == attr)
8433 --i;
8434 if (i != 0)
8435 {
8436 /*
8437 * Try if it works when highlighting is stopped here.
8438 */
8439 if (*--p == 0)
8440 {
8441 cost += noinvcurs;
8442 while (i && *p++ == 0)
8443 --i;
8444 }
8445 if (i != 0)
8446 cost = 999; /* different attributes, don't do it */
8447 }
8448#ifdef FEAT_MBYTE
8449 if (enc_utf8)
8450 {
8451 /* Don't use an UTF-8 char for positioning, it's slow. */
8452 for (i = wouldbe_col; i < col; ++i)
8453 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8454 {
8455 cost = 999;
8456 break;
8457 }
8458 }
8459#endif
8460 }
8461
8462 /*
8463 * We can do it without term_windgoto()!
8464 */
8465 if (cost < goto_cost)
8466 {
8467 if (plan == PLAN_LE)
8468 {
8469 if (noinvcurs)
8470 screen_stop_highlight();
8471 while (screen_cur_col > col)
8472 {
8473 out_str(bs);
8474 --screen_cur_col;
8475 }
8476 }
8477 else if (plan == PLAN_CR)
8478 {
8479 if (noinvcurs)
8480 screen_stop_highlight();
8481 out_char('\r');
8482 screen_cur_col = 0;
8483 }
8484 else if (plan == PLAN_NL)
8485 {
8486 if (noinvcurs)
8487 screen_stop_highlight();
8488 while (screen_cur_row < row)
8489 {
8490 out_char('\n');
8491 ++screen_cur_row;
8492 }
8493 screen_cur_col = 0;
8494 }
8495
8496 i = col - screen_cur_col;
8497 if (i > 0)
8498 {
8499 /*
8500 * Use cursor-right if it's one character only. Avoids
8501 * removing a line of pixels from the last bold char, when
8502 * using the bold trick in the GUI.
8503 */
8504 if (T_ND[0] != NUL && T_ND[1] == NUL)
8505 {
8506 while (i-- > 0)
8507 out_char(*T_ND);
8508 }
8509 else
8510 {
8511 int off;
8512
8513 off = LineOffset[row] + screen_cur_col;
8514 while (i-- > 0)
8515 {
8516 if (ScreenAttrs[off] != screen_attr)
8517 screen_stop_highlight();
8518#ifdef FEAT_MBYTE
8519 out_flush_check();
8520#endif
8521 out_char(ScreenLines[off]);
8522#ifdef FEAT_MBYTE
8523 if (enc_dbcs == DBCS_JPNU
8524 && ScreenLines[off] == 0x8e)
8525 out_char(ScreenLines2[off]);
8526#endif
8527 ++off;
8528 }
8529 }
8530 }
8531 }
8532 }
8533 else
8534 cost = 999;
8535
8536 if (cost >= goto_cost)
8537 {
8538 if (noinvcurs)
8539 screen_stop_highlight();
8540 if (row == screen_cur_row && (col > screen_cur_col) &&
8541 *T_CRI != NUL)
8542 term_cursor_right(col - screen_cur_col);
8543 else
8544 term_windgoto(row, col);
8545 }
8546 screen_cur_row = row;
8547 screen_cur_col = col;
8548 }
8549}
8550
8551/*
8552 * Set cursor to its position in the current window.
8553 */
8554 void
8555setcursor()
8556{
8557 if (redrawing())
8558 {
8559 validate_cursor();
8560 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8561 W_WINCOL(curwin) + (
8562#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008563 /* With 'rightleft' set and the cursor on a double-wide
8564 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8566# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008567 (has_mbyte
8568 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8569 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570# endif
8571 1)) :
8572#endif
8573 curwin->w_wcol));
8574 }
8575}
8576
8577
8578/*
8579 * insert 'line_count' lines at 'row' in window 'wp'
8580 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8581 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8582 * scrolling.
8583 * Returns FAIL if the lines are not inserted, OK for success.
8584 */
8585 int
8586win_ins_lines(wp, row, line_count, invalid, mayclear)
8587 win_T *wp;
8588 int row;
8589 int line_count;
8590 int invalid;
8591 int mayclear;
8592{
8593 int did_delete;
8594 int nextrow;
8595 int lastrow;
8596 int retval;
8597
8598 if (invalid)
8599 wp->w_lines_valid = 0;
8600
8601 if (wp->w_height < 5)
8602 return FAIL;
8603
8604 if (line_count > wp->w_height - row)
8605 line_count = wp->w_height - row;
8606
8607 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8608 if (retval != MAYBE)
8609 return retval;
8610
8611 /*
8612 * If there is a next window or a status line, we first try to delete the
8613 * lines at the bottom to avoid messing what is after the window.
8614 * If this fails and there are following windows, don't do anything to avoid
8615 * messing up those windows, better just redraw.
8616 */
8617 did_delete = FALSE;
8618#ifdef FEAT_WINDOWS
8619 if (wp->w_next != NULL || wp->w_status_height)
8620 {
8621 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8622 line_count, (int)Rows, FALSE, NULL) == OK)
8623 did_delete = TRUE;
8624 else if (wp->w_next)
8625 return FAIL;
8626 }
8627#endif
8628 /*
8629 * if no lines deleted, blank the lines that will end up below the window
8630 */
8631 if (!did_delete)
8632 {
8633#ifdef FEAT_WINDOWS
8634 wp->w_redr_status = TRUE;
8635#endif
8636 redraw_cmdline = TRUE;
8637 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8638 lastrow = nextrow + line_count;
8639 if (lastrow > Rows)
8640 lastrow = Rows;
8641 screen_fill(nextrow - line_count, lastrow - line_count,
8642 W_WINCOL(wp), (int)W_ENDCOL(wp),
8643 ' ', ' ', 0);
8644 }
8645
8646 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8647 == FAIL)
8648 {
8649 /* deletion will have messed up other windows */
8650 if (did_delete)
8651 {
8652#ifdef FEAT_WINDOWS
8653 wp->w_redr_status = TRUE;
8654#endif
8655 win_rest_invalid(W_NEXT(wp));
8656 }
8657 return FAIL;
8658 }
8659
8660 return OK;
8661}
8662
8663/*
8664 * delete "line_count" window lines at "row" in window "wp"
8665 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8666 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8667 * scrolling
8668 * Return OK for success, FAIL if the lines are not deleted.
8669 */
8670 int
8671win_del_lines(wp, row, line_count, invalid, mayclear)
8672 win_T *wp;
8673 int row;
8674 int line_count;
8675 int invalid;
8676 int mayclear;
8677{
8678 int retval;
8679
8680 if (invalid)
8681 wp->w_lines_valid = 0;
8682
8683 if (line_count > wp->w_height - row)
8684 line_count = wp->w_height - row;
8685
8686 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8687 if (retval != MAYBE)
8688 return retval;
8689
8690 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8691 (int)Rows, FALSE, NULL) == FAIL)
8692 return FAIL;
8693
8694#ifdef FEAT_WINDOWS
8695 /*
8696 * If there are windows or status lines below, try to put them at the
8697 * correct place. If we can't do that, they have to be redrawn.
8698 */
8699 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8700 {
8701 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8702 line_count, (int)Rows, NULL) == FAIL)
8703 {
8704 wp->w_redr_status = TRUE;
8705 win_rest_invalid(wp->w_next);
8706 }
8707 }
8708 /*
8709 * If this is the last window and there is no status line, redraw the
8710 * command line later.
8711 */
8712 else
8713#endif
8714 redraw_cmdline = TRUE;
8715 return OK;
8716}
8717
8718/*
8719 * Common code for win_ins_lines() and win_del_lines().
8720 * Returns OK or FAIL when the work has been done.
8721 * Returns MAYBE when not finished yet.
8722 */
8723 static int
8724win_do_lines(wp, row, line_count, mayclear, del)
8725 win_T *wp;
8726 int row;
8727 int line_count;
8728 int mayclear;
8729 int del;
8730{
8731 int retval;
8732
8733 if (!redrawing() || line_count <= 0)
8734 return FAIL;
8735
8736 /* only a few lines left: redraw is faster */
8737 if (mayclear && Rows - line_count < 5
8738#ifdef FEAT_VERTSPLIT
8739 && wp->w_width == Columns
8740#endif
8741 )
8742 {
8743 screenclear(); /* will set wp->w_lines_valid to 0 */
8744 return FAIL;
8745 }
8746
8747 /*
8748 * Delete all remaining lines
8749 */
8750 if (row + line_count >= wp->w_height)
8751 {
8752 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8753 W_WINCOL(wp), (int)W_ENDCOL(wp),
8754 ' ', ' ', 0);
8755 return OK;
8756 }
8757
8758 /*
8759 * when scrolling, the message on the command line should be cleared,
8760 * otherwise it will stay there forever.
8761 */
8762 clear_cmdline = TRUE;
8763
8764 /*
8765 * If the terminal can set a scroll region, use that.
8766 * Always do this in a vertically split window. This will redraw from
8767 * ScreenLines[] when t_CV isn't defined. That's faster than using
8768 * win_line().
8769 * Don't use a scroll region when we are going to redraw the text, writing
8770 * a character in the lower right corner of the scroll region causes a
8771 * scroll-up in the DJGPP version.
8772 */
8773 if (scroll_region
8774#ifdef FEAT_VERTSPLIT
8775 || W_WIDTH(wp) != Columns
8776#endif
8777 )
8778 {
8779#ifdef FEAT_VERTSPLIT
8780 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8781#endif
8782 scroll_region_set(wp, row);
8783 if (del)
8784 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8785 wp->w_height - row, FALSE, wp);
8786 else
8787 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8788 wp->w_height - row, wp);
8789#ifdef FEAT_VERTSPLIT
8790 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8791#endif
8792 scroll_region_reset();
8793 return retval;
8794 }
8795
8796#ifdef FEAT_WINDOWS
8797 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8798 return FAIL;
8799#endif
8800
8801 return MAYBE;
8802}
8803
8804/*
8805 * window 'wp' and everything after it is messed up, mark it for redraw
8806 */
8807 static void
8808win_rest_invalid(wp)
8809 win_T *wp;
8810{
8811#ifdef FEAT_WINDOWS
8812 while (wp != NULL)
8813#else
8814 if (wp != NULL)
8815#endif
8816 {
8817 redraw_win_later(wp, NOT_VALID);
8818#ifdef FEAT_WINDOWS
8819 wp->w_redr_status = TRUE;
8820 wp = wp->w_next;
8821#endif
8822 }
8823 redraw_cmdline = TRUE;
8824}
8825
8826/*
8827 * The rest of the routines in this file perform screen manipulations. The
8828 * given operation is performed physically on the screen. The corresponding
8829 * change is also made to the internal screen image. In this way, the editor
8830 * anticipates the effect of editing changes on the appearance of the screen.
8831 * That way, when we call screenupdate a complete redraw isn't usually
8832 * necessary. Another advantage is that we can keep adding code to anticipate
8833 * screen changes, and in the meantime, everything still works.
8834 */
8835
8836/*
8837 * types for inserting or deleting lines
8838 */
8839#define USE_T_CAL 1
8840#define USE_T_CDL 2
8841#define USE_T_AL 3
8842#define USE_T_CE 4
8843#define USE_T_DL 5
8844#define USE_T_SR 6
8845#define USE_NL 7
8846#define USE_T_CD 8
8847#define USE_REDRAW 9
8848
8849/*
8850 * insert lines on the screen and update ScreenLines[]
8851 * 'end' is the line after the scrolled part. Normally it is Rows.
8852 * When scrolling region used 'off' is the offset from the top for the region.
8853 * 'row' and 'end' are relative to the start of the region.
8854 *
8855 * return FAIL for failure, OK for success.
8856 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008857 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858screen_ins_lines(off, row, line_count, end, wp)
8859 int off;
8860 int row;
8861 int line_count;
8862 int end;
8863 win_T *wp; /* NULL or window to use width from */
8864{
8865 int i;
8866 int j;
8867 unsigned temp;
8868 int cursor_row;
8869 int type;
8870 int result_empty;
8871 int can_ce = can_clear(T_CE);
8872
8873 /*
8874 * FAIL if
8875 * - there is no valid screen
8876 * - the screen has to be redrawn completely
8877 * - the line count is less than one
8878 * - the line count is more than 'ttyscroll'
8879 */
8880 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8881 return FAIL;
8882
8883 /*
8884 * There are seven ways to insert lines:
8885 * 0. When in a vertically split window and t_CV isn't set, redraw the
8886 * characters from ScreenLines[].
8887 * 1. Use T_CD (clear to end of display) if it exists and the result of
8888 * the insert is just empty lines
8889 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8890 * present or line_count > 1. It looks better if we do all the inserts
8891 * at once.
8892 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8893 * insert is just empty lines and T_CE is not present or line_count >
8894 * 1.
8895 * 4. Use T_AL (insert line) if it exists.
8896 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8897 * just empty lines.
8898 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8899 * just empty lines.
8900 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8901 * the 'da' flag is not set or we have clear line capability.
8902 * 8. redraw the characters from ScreenLines[].
8903 *
8904 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8905 * the scrollbar for the window. It does have insert line, use that if it
8906 * exists.
8907 */
8908 result_empty = (row + line_count >= end);
8909#ifdef FEAT_VERTSPLIT
8910 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8911 type = USE_REDRAW;
8912 else
8913#endif
8914 if (can_clear(T_CD) && result_empty)
8915 type = USE_T_CD;
8916 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8917 type = USE_T_CAL;
8918 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8919 type = USE_T_CDL;
8920 else if (*T_AL != NUL)
8921 type = USE_T_AL;
8922 else if (can_ce && result_empty)
8923 type = USE_T_CE;
8924 else if (*T_DL != NUL && result_empty)
8925 type = USE_T_DL;
8926 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8927 type = USE_T_SR;
8928 else
8929 return FAIL;
8930
8931 /*
8932 * For clearing the lines screen_del_lines() is used. This will also take
8933 * care of t_db if necessary.
8934 */
8935 if (type == USE_T_CD || type == USE_T_CDL ||
8936 type == USE_T_CE || type == USE_T_DL)
8937 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8938
8939 /*
8940 * If text is retained below the screen, first clear or delete as many
8941 * lines at the bottom of the window as are about to be inserted so that
8942 * the deleted lines won't later surface during a screen_del_lines.
8943 */
8944 if (*T_DB)
8945 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8946
8947#ifdef FEAT_CLIPBOARD
8948 /* Remove a modeless selection when inserting lines halfway the screen
8949 * or not the full width of the screen. */
8950 if (off + row > 0
8951# ifdef FEAT_VERTSPLIT
8952 || (wp != NULL && wp->w_width != Columns)
8953# endif
8954 )
8955 clip_clear_selection();
8956 else
8957 clip_scroll_selection(-line_count);
8958#endif
8959
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960#ifdef FEAT_GUI
8961 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8962 * scrolling is actually carried out. */
8963 gui_dont_update_cursor();
8964#endif
8965
8966 if (*T_CCS != NUL) /* cursor relative to region */
8967 cursor_row = row;
8968 else
8969 cursor_row = row + off;
8970
8971 /*
8972 * Shift LineOffset[] line_count down to reflect the inserted lines.
8973 * Clear the inserted lines in ScreenLines[].
8974 */
8975 row += off;
8976 end += off;
8977 for (i = 0; i < line_count; ++i)
8978 {
8979#ifdef FEAT_VERTSPLIT
8980 if (wp != NULL && wp->w_width != Columns)
8981 {
8982 /* need to copy part of a line */
8983 j = end - 1 - i;
8984 while ((j -= line_count) >= row)
8985 linecopy(j + line_count, j, wp);
8986 j += line_count;
8987 if (can_clear((char_u *)" "))
8988 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8989 else
8990 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8991 LineWraps[j] = FALSE;
8992 }
8993 else
8994#endif
8995 {
8996 j = end - 1 - i;
8997 temp = LineOffset[j];
8998 while ((j -= line_count) >= row)
8999 {
9000 LineOffset[j + line_count] = LineOffset[j];
9001 LineWraps[j + line_count] = LineWraps[j];
9002 }
9003 LineOffset[j + line_count] = temp;
9004 LineWraps[j + line_count] = FALSE;
9005 if (can_clear((char_u *)" "))
9006 lineclear(temp, (int)Columns);
9007 else
9008 lineinvalid(temp, (int)Columns);
9009 }
9010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011
9012 screen_stop_highlight();
9013 windgoto(cursor_row, 0);
9014
9015#ifdef FEAT_VERTSPLIT
9016 /* redraw the characters */
9017 if (type == USE_REDRAW)
9018 redraw_block(row, end, wp);
9019 else
9020#endif
9021 if (type == USE_T_CAL)
9022 {
9023 term_append_lines(line_count);
9024 screen_start(); /* don't know where cursor is now */
9025 }
9026 else
9027 {
9028 for (i = 0; i < line_count; i++)
9029 {
9030 if (type == USE_T_AL)
9031 {
9032 if (i && cursor_row != 0)
9033 windgoto(cursor_row, 0);
9034 out_str(T_AL);
9035 }
9036 else /* type == USE_T_SR */
9037 out_str(T_SR);
9038 screen_start(); /* don't know where cursor is now */
9039 }
9040 }
9041
9042 /*
9043 * With scroll-reverse and 'da' flag set we need to clear the lines that
9044 * have been scrolled down into the region.
9045 */
9046 if (type == USE_T_SR && *T_DA)
9047 {
9048 for (i = 0; i < line_count; ++i)
9049 {
9050 windgoto(off + i, 0);
9051 out_str(T_CE);
9052 screen_start(); /* don't know where cursor is now */
9053 }
9054 }
9055
9056#ifdef FEAT_GUI
9057 gui_can_update_cursor();
9058 if (gui.in_use)
9059 out_flush(); /* always flush after a scroll */
9060#endif
9061 return OK;
9062}
9063
9064/*
9065 * delete lines on the screen and update ScreenLines[]
9066 * 'end' is the line after the scrolled part. Normally it is Rows.
9067 * When scrolling region used 'off' is the offset from the top for the region.
9068 * 'row' and 'end' are relative to the start of the region.
9069 *
9070 * Return OK for success, FAIL if the lines are not deleted.
9071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 int
9073screen_del_lines(off, row, line_count, end, force, wp)
9074 int off;
9075 int row;
9076 int line_count;
9077 int end;
9078 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009079 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 int j;
9082 int i;
9083 unsigned temp;
9084 int cursor_row;
9085 int cursor_end;
9086 int result_empty; /* result is empty until end of region */
9087 int can_delete; /* deleting line codes can be used */
9088 int type;
9089
9090 /*
9091 * FAIL if
9092 * - there is no valid screen
9093 * - the screen has to be redrawn completely
9094 * - the line count is less than one
9095 * - the line count is more than 'ttyscroll'
9096 */
9097 if (!screen_valid(TRUE) || line_count <= 0 ||
9098 (!force && line_count > p_ttyscroll))
9099 return FAIL;
9100
9101 /*
9102 * Check if the rest of the current region will become empty.
9103 */
9104 result_empty = row + line_count >= end;
9105
9106 /*
9107 * We can delete lines only when 'db' flag not set or when 'ce' option
9108 * available.
9109 */
9110 can_delete = (*T_DB == NUL || can_clear(T_CE));
9111
9112 /*
9113 * There are six ways to delete lines:
9114 * 0. When in a vertically split window and t_CV isn't set, redraw the
9115 * characters from ScreenLines[].
9116 * 1. Use T_CD if it exists and the result is empty.
9117 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9118 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9119 * none of the other ways work.
9120 * 4. Use T_CE (erase line) if the result is empty.
9121 * 5. Use T_DL (delete line) if it exists.
9122 * 6. redraw the characters from ScreenLines[].
9123 */
9124#ifdef FEAT_VERTSPLIT
9125 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9126 type = USE_REDRAW;
9127 else
9128#endif
9129 if (can_clear(T_CD) && result_empty)
9130 type = USE_T_CD;
9131#if defined(__BEOS__) && defined(BEOS_DR8)
9132 /*
9133 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9134 * its internal termcap... this works okay for tests which test *T_DB !=
9135 * NUL. It has the disadvantage that the user cannot use any :set t_*
9136 * command to get T_DB (back) to empty_option, only :set term=... will do
9137 * the trick...
9138 * Anyway, this hack will hopefully go away with the next OS release.
9139 * (Olaf Seibert)
9140 */
9141 else if (row == 0 && T_DB == empty_option
9142 && (line_count == 1 || *T_CDL == NUL))
9143#else
9144 else if (row == 0 && (
9145#ifndef AMIGA
9146 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9147 * up, so use delete-line command */
9148 line_count == 1 ||
9149#endif
9150 *T_CDL == NUL))
9151#endif
9152 type = USE_NL;
9153 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9154 type = USE_T_CDL;
9155 else if (can_clear(T_CE) && result_empty
9156#ifdef FEAT_VERTSPLIT
9157 && (wp == NULL || wp->w_width == Columns)
9158#endif
9159 )
9160 type = USE_T_CE;
9161 else if (*T_DL != NUL && can_delete)
9162 type = USE_T_DL;
9163 else if (*T_CDL != NUL && can_delete)
9164 type = USE_T_CDL;
9165 else
9166 return FAIL;
9167
9168#ifdef FEAT_CLIPBOARD
9169 /* Remove a modeless selection when deleting lines halfway the screen or
9170 * not the full width of the screen. */
9171 if (off + row > 0
9172# ifdef FEAT_VERTSPLIT
9173 || (wp != NULL && wp->w_width != Columns)
9174# endif
9175 )
9176 clip_clear_selection();
9177 else
9178 clip_scroll_selection(line_count);
9179#endif
9180
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181#ifdef FEAT_GUI
9182 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9183 * scrolling is actually carried out. */
9184 gui_dont_update_cursor();
9185#endif
9186
9187 if (*T_CCS != NUL) /* cursor relative to region */
9188 {
9189 cursor_row = row;
9190 cursor_end = end;
9191 }
9192 else
9193 {
9194 cursor_row = row + off;
9195 cursor_end = end + off;
9196 }
9197
9198 /*
9199 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9200 * Clear the inserted lines in ScreenLines[].
9201 */
9202 row += off;
9203 end += off;
9204 for (i = 0; i < line_count; ++i)
9205 {
9206#ifdef FEAT_VERTSPLIT
9207 if (wp != NULL && wp->w_width != Columns)
9208 {
9209 /* need to copy part of a line */
9210 j = row + i;
9211 while ((j += line_count) <= end - 1)
9212 linecopy(j - line_count, j, wp);
9213 j -= line_count;
9214 if (can_clear((char_u *)" "))
9215 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9216 else
9217 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9218 LineWraps[j] = FALSE;
9219 }
9220 else
9221#endif
9222 {
9223 /* whole width, moving the line pointers is faster */
9224 j = row + i;
9225 temp = LineOffset[j];
9226 while ((j += line_count) <= end - 1)
9227 {
9228 LineOffset[j - line_count] = LineOffset[j];
9229 LineWraps[j - line_count] = LineWraps[j];
9230 }
9231 LineOffset[j - line_count] = temp;
9232 LineWraps[j - line_count] = FALSE;
9233 if (can_clear((char_u *)" "))
9234 lineclear(temp, (int)Columns);
9235 else
9236 lineinvalid(temp, (int)Columns);
9237 }
9238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239
9240 screen_stop_highlight();
9241
9242#ifdef FEAT_VERTSPLIT
9243 /* redraw the characters */
9244 if (type == USE_REDRAW)
9245 redraw_block(row, end, wp);
9246 else
9247#endif
9248 if (type == USE_T_CD) /* delete the lines */
9249 {
9250 windgoto(cursor_row, 0);
9251 out_str(T_CD);
9252 screen_start(); /* don't know where cursor is now */
9253 }
9254 else if (type == USE_T_CDL)
9255 {
9256 windgoto(cursor_row, 0);
9257 term_delete_lines(line_count);
9258 screen_start(); /* don't know where cursor is now */
9259 }
9260 /*
9261 * Deleting lines at top of the screen or scroll region: Just scroll
9262 * the whole screen (scroll region) up by outputting newlines on the
9263 * last line.
9264 */
9265 else if (type == USE_NL)
9266 {
9267 windgoto(cursor_end - 1, 0);
9268 for (i = line_count; --i >= 0; )
9269 out_char('\n'); /* cursor will remain on same line */
9270 }
9271 else
9272 {
9273 for (i = line_count; --i >= 0; )
9274 {
9275 if (type == USE_T_DL)
9276 {
9277 windgoto(cursor_row, 0);
9278 out_str(T_DL); /* delete a line */
9279 }
9280 else /* type == USE_T_CE */
9281 {
9282 windgoto(cursor_row + i, 0);
9283 out_str(T_CE); /* erase a line */
9284 }
9285 screen_start(); /* don't know where cursor is now */
9286 }
9287 }
9288
9289 /*
9290 * If the 'db' flag is set, we need to clear the lines that have been
9291 * scrolled up at the bottom of the region.
9292 */
9293 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9294 {
9295 for (i = line_count; i > 0; --i)
9296 {
9297 windgoto(cursor_end - i, 0);
9298 out_str(T_CE); /* erase a line */
9299 screen_start(); /* don't know where cursor is now */
9300 }
9301 }
9302
9303#ifdef FEAT_GUI
9304 gui_can_update_cursor();
9305 if (gui.in_use)
9306 out_flush(); /* always flush after a scroll */
9307#endif
9308
9309 return OK;
9310}
9311
9312/*
9313 * show the current mode and ruler
9314 *
9315 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9316 * If clear_cmdline is FALSE there may be a message there that needs to be
9317 * cleared only if a mode is shown.
9318 * Return the length of the message (0 if no message).
9319 */
9320 int
9321showmode()
9322{
9323 int need_clear;
9324 int length = 0;
9325 int do_mode;
9326 int attr;
9327 int nwr_save;
9328#ifdef FEAT_INS_EXPAND
9329 int sub_attr;
9330#endif
9331
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009332 do_mode = ((p_smd && msg_silent == 0)
9333 && ((State & INSERT)
9334 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335#ifdef FEAT_VISUAL
9336 || VIsual_active
9337#endif
9338 ));
9339 if (do_mode || Recording)
9340 {
9341 /*
9342 * Don't show mode right now, when not redrawing or inside a mapping.
9343 * Call char_avail() only when we are going to show something, because
9344 * it takes a bit of time.
9345 */
9346 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9347 {
9348 redraw_cmdline = TRUE; /* show mode later */
9349 return 0;
9350 }
9351
9352 nwr_save = need_wait_return;
9353
9354 /* wait a bit before overwriting an important message */
9355 check_for_delay(FALSE);
9356
9357 /* if the cmdline is more than one line high, erase top lines */
9358 need_clear = clear_cmdline;
9359 if (clear_cmdline && cmdline_row < Rows - 1)
9360 msg_clr_cmdline(); /* will reset clear_cmdline */
9361
9362 /* Position on the last line in the window, column 0 */
9363 msg_pos_mode();
9364 cursor_off();
9365 attr = hl_attr(HLF_CM); /* Highlight mode */
9366 if (do_mode)
9367 {
9368 MSG_PUTS_ATTR("--", attr);
9369#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009370# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 if (xic != NULL && im_get_status() && !p_imdisable
9372 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009373# else
9374 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009375# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009376 preedit_get_status()
9377# else
9378 im_get_status()
9379# endif
9380 )
9381# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009382# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 MSG_PUTS_ATTR(" IM", attr);
9384# else
9385 MSG_PUTS_ATTR(" XIM", attr);
9386# endif
9387#endif
9388#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9389 if (gui.in_use)
9390 {
9391 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009392 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 }
9394#endif
9395#ifdef FEAT_INS_EXPAND
9396 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9397 {
9398 /* These messages can get long, avoid a wrap in a narrow
9399 * window. Prefer showing edit_submode_extra. */
9400 length = (Rows - msg_row) * Columns - 3;
9401 if (edit_submode_extra != NULL)
9402 length -= vim_strsize(edit_submode_extra);
9403 if (length > 0)
9404 {
9405 if (edit_submode_pre != NULL)
9406 length -= vim_strsize(edit_submode_pre);
9407 if (length - vim_strsize(edit_submode) > 0)
9408 {
9409 if (edit_submode_pre != NULL)
9410 msg_puts_attr(edit_submode_pre, attr);
9411 msg_puts_attr(edit_submode, attr);
9412 }
9413 if (edit_submode_extra != NULL)
9414 {
9415 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9416 if ((int)edit_submode_highl < (int)HLF_COUNT)
9417 sub_attr = hl_attr(edit_submode_highl);
9418 else
9419 sub_attr = attr;
9420 msg_puts_attr(edit_submode_extra, sub_attr);
9421 }
9422 }
9423 length = 0;
9424 }
9425 else
9426#endif
9427 {
9428#ifdef FEAT_VREPLACE
9429 if (State & VREPLACE_FLAG)
9430 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9431 else
9432#endif
9433 if (State & REPLACE_FLAG)
9434 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9435 else if (State & INSERT)
9436 {
9437#ifdef FEAT_RIGHTLEFT
9438 if (p_ri)
9439 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9440#endif
9441 MSG_PUTS_ATTR(_(" INSERT"), attr);
9442 }
9443 else if (restart_edit == 'I')
9444 MSG_PUTS_ATTR(_(" (insert)"), attr);
9445 else if (restart_edit == 'R')
9446 MSG_PUTS_ATTR(_(" (replace)"), attr);
9447 else if (restart_edit == 'V')
9448 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9449#ifdef FEAT_RIGHTLEFT
9450 if (p_hkmap)
9451 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9452# ifdef FEAT_FKMAP
9453 if (p_fkmap)
9454 MSG_PUTS_ATTR(farsi_text_5, attr);
9455# endif
9456#endif
9457#ifdef FEAT_KEYMAP
9458 if (State & LANGMAP)
9459 {
9460# ifdef FEAT_ARABIC
9461 if (curwin->w_p_arab)
9462 MSG_PUTS_ATTR(_(" Arabic"), attr);
9463 else
9464# endif
9465 MSG_PUTS_ATTR(_(" (lang)"), attr);
9466 }
9467#endif
9468 if ((State & INSERT) && p_paste)
9469 MSG_PUTS_ATTR(_(" (paste)"), attr);
9470
9471#ifdef FEAT_VISUAL
9472 if (VIsual_active)
9473 {
9474 char *p;
9475
9476 /* Don't concatenate separate words to avoid translation
9477 * problems. */
9478 switch ((VIsual_select ? 4 : 0)
9479 + (VIsual_mode == Ctrl_V) * 2
9480 + (VIsual_mode == 'V'))
9481 {
9482 case 0: p = N_(" VISUAL"); break;
9483 case 1: p = N_(" VISUAL LINE"); break;
9484 case 2: p = N_(" VISUAL BLOCK"); break;
9485 case 4: p = N_(" SELECT"); break;
9486 case 5: p = N_(" SELECT LINE"); break;
9487 default: p = N_(" SELECT BLOCK"); break;
9488 }
9489 MSG_PUTS_ATTR(_(p), attr);
9490 }
9491#endif
9492 MSG_PUTS_ATTR(" --", attr);
9493 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009494
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 need_clear = TRUE;
9496 }
9497 if (Recording
9498#ifdef FEAT_INS_EXPAND
9499 && edit_submode == NULL /* otherwise it gets too long */
9500#endif
9501 )
9502 {
9503 MSG_PUTS_ATTR(_("recording"), attr);
9504 need_clear = TRUE;
9505 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009506
9507 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 if (need_clear || clear_cmdline)
9509 msg_clr_eos();
9510 msg_didout = FALSE; /* overwrite this message */
9511 length = msg_col;
9512 msg_col = 0;
9513 need_wait_return = nwr_save; /* never ask for hit-return for this */
9514 }
9515 else if (clear_cmdline && msg_silent == 0)
9516 /* Clear the whole command line. Will reset "clear_cmdline". */
9517 msg_clr_cmdline();
9518
9519#ifdef FEAT_CMDL_INFO
9520# ifdef FEAT_VISUAL
9521 /* In Visual mode the size of the selected area must be redrawn. */
9522 if (VIsual_active)
9523 clear_showcmd();
9524# endif
9525
9526 /* If the last window has no status line, the ruler is after the mode
9527 * message and must be redrawn */
9528 if (redrawing()
9529# ifdef FEAT_WINDOWS
9530 && lastwin->w_status_height == 0
9531# endif
9532 )
9533 win_redr_ruler(lastwin, TRUE);
9534#endif
9535 redraw_cmdline = FALSE;
9536 clear_cmdline = FALSE;
9537
9538 return length;
9539}
9540
9541/*
9542 * Position for a mode message.
9543 */
9544 static void
9545msg_pos_mode()
9546{
9547 msg_col = 0;
9548 msg_row = Rows - 1;
9549}
9550
9551/*
9552 * Delete mode message. Used when ESC is typed which is expected to end
9553 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009554 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 */
9556 void
9557unshowmode(force)
9558 int force;
9559{
9560 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009561 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 */
9563 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9564 redraw_cmdline = TRUE; /* delete mode later */
9565 else
9566 {
9567 msg_pos_mode();
9568 if (Recording)
9569 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9570 msg_clr_eos();
9571 }
9572}
9573
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009574#if defined(FEAT_WINDOWS)
9575/*
9576 * Draw the tab pages line at the top of the Vim window.
9577 */
9578 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009579draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009580{
9581 int tabcount = 0;
9582 tabpage_T *tp;
9583 int tabwidth;
9584 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009585 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009586 int attr;
9587 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009588 win_T *cwp;
9589 int wincount;
9590 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009591 int c;
9592 int len;
9593 int attr_sel = hl_attr(HLF_TPS);
9594 int attr_nosel = hl_attr(HLF_TP);
9595 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009596 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009597 int room;
9598 int use_sep_chars = (t_colors < 8
9599#ifdef FEAT_GUI
9600 && !gui.in_use
9601#endif
9602 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009603
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009604 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009605
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009606#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009607 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009608 if (gui_use_tabline())
9609 {
9610 gui_update_tabline();
9611 return;
9612 }
9613#endif
9614
9615 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009616 return;
9617
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009618#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009619
9620 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9621 for (scol = 0; scol < Columns; ++scol)
9622 TabPageIdxs[scol] = 0;
9623
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009624 /* Use the 'tabline' option if it's set. */
9625 if (*p_tal != NUL)
9626 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009627 int save_called_emsg = called_emsg;
9628
9629 /* Check for an error. If there is one we would loop in redrawing the
9630 * screen. Avoid that by making 'tabline' empty. */
9631 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009632 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009633 if (called_emsg)
9634 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009635 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009636 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009637 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009638 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009639#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009640 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009641 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9642 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009643
Bram Moolenaar238a5642006-02-21 22:12:05 +00009644 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9645 if (tabwidth < 6)
9646 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009647
Bram Moolenaar238a5642006-02-21 22:12:05 +00009648 attr = attr_nosel;
9649 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009650 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009651 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9652 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009653 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009654 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009655
Bram Moolenaar238a5642006-02-21 22:12:05 +00009656 if (tp->tp_topframe == topframe)
9657 attr = attr_sel;
9658 if (use_sep_chars && col > 0)
9659 screen_putchar('|', 0, col++, attr);
9660
9661 if (tp->tp_topframe != topframe)
9662 attr = attr_nosel;
9663
9664 screen_putchar(' ', 0, col++, attr);
9665
9666 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009667 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009668 cwp = curwin;
9669 wp = firstwin;
9670 }
9671 else
9672 {
9673 cwp = tp->tp_curwin;
9674 wp = tp->tp_firstwin;
9675 }
9676
9677 modified = FALSE;
9678 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9679 if (bufIsChanged(wp->w_buffer))
9680 modified = TRUE;
9681 if (modified || wincount > 1)
9682 {
9683 if (wincount > 1)
9684 {
9685 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009686 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009687 if (col + len >= Columns - 3)
9688 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009689 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009690#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009691 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009692#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009693 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009694#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009695 );
9696 col += len;
9697 }
9698 if (modified)
9699 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9700 screen_putchar(' ', 0, col++, attr);
9701 }
9702
9703 room = scol - col + tabwidth - 1;
9704 if (room > 0)
9705 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009706 /* Get buffer name in NameBuff[] */
9707 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009708 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009709 len = vim_strsize(NameBuff);
9710 p = NameBuff;
9711#ifdef FEAT_MBYTE
9712 if (has_mbyte)
9713 while (len > room)
9714 {
9715 len -= ptr2cells(p);
9716 mb_ptr_adv(p);
9717 }
9718 else
9719#endif
9720 if (len > room)
9721 {
9722 p += len - room;
9723 len = room;
9724 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009725 if (len > Columns - col - 1)
9726 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009727
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009728 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009729 col += len;
9730 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009731 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009732
9733 /* Store the tab page number in TabPageIdxs[], so that
9734 * jump_to_mouse() knows where each one is. */
9735 ++tabcount;
9736 while (scol < col)
9737 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009738 }
9739
Bram Moolenaar238a5642006-02-21 22:12:05 +00009740 if (use_sep_chars)
9741 c = '_';
9742 else
9743 c = ' ';
9744 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009745
9746 /* Put an "X" for closing the current tab if there are several. */
9747 if (first_tabpage->tp_next != NULL)
9748 {
9749 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9750 TabPageIdxs[Columns - 1] = -999;
9751 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009752 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009753
9754 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9755 * set. */
9756 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009757}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009758
9759/*
9760 * Get buffer name for "buf" into NameBuff[].
9761 * Takes care of special buffer names and translates special characters.
9762 */
9763 void
9764get_trans_bufname(buf)
9765 buf_T *buf;
9766{
9767 if (buf_spname(buf) != NULL)
9768 STRCPY(NameBuff, buf_spname(buf));
9769 else
9770 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9771 trans_characters(NameBuff, MAXPATHL);
9772}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009773#endif
9774
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9776/*
9777 * Get the character to use in a status line. Get its attributes in "*attr".
9778 */
9779 static int
9780fillchar_status(attr, is_curwin)
9781 int *attr;
9782 int is_curwin;
9783{
9784 int fill;
9785 if (is_curwin)
9786 {
9787 *attr = hl_attr(HLF_S);
9788 fill = fill_stl;
9789 }
9790 else
9791 {
9792 *attr = hl_attr(HLF_SNC);
9793 fill = fill_stlnc;
9794 }
9795 /* Use fill when there is highlighting, and highlighting of current
9796 * window differs, or the fillchars differ, or this is not the
9797 * current window */
9798 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9799 || !is_curwin || firstwin == lastwin)
9800 || (fill_stl != fill_stlnc)))
9801 return fill;
9802 if (is_curwin)
9803 return '^';
9804 return '=';
9805}
9806#endif
9807
9808#ifdef FEAT_VERTSPLIT
9809/*
9810 * Get the character to use in a separator between vertically split windows.
9811 * Get its attributes in "*attr".
9812 */
9813 static int
9814fillchar_vsep(attr)
9815 int *attr;
9816{
9817 *attr = hl_attr(HLF_C);
9818 if (*attr == 0 && fill_vert == ' ')
9819 return '|';
9820 else
9821 return fill_vert;
9822}
9823#endif
9824
9825/*
9826 * Return TRUE if redrawing should currently be done.
9827 */
9828 int
9829redrawing()
9830{
9831 return (!RedrawingDisabled
9832 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9833}
9834
9835/*
9836 * Return TRUE if printing messages should currently be done.
9837 */
9838 int
9839messaging()
9840{
9841 return (!(p_lz && char_avail() && !KeyTyped));
9842}
9843
9844/*
9845 * Show current status info in ruler and various other places
9846 * If always is FALSE, only show ruler if position has changed.
9847 */
9848 void
9849showruler(always)
9850 int always;
9851{
9852 if (!always && !redrawing())
9853 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009854#ifdef FEAT_INS_EXPAND
9855 if (pum_visible())
9856 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009857# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009858 /* Don't redraw right now, do it later. */
9859 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009860# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009861 return;
9862 }
9863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009865 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009866 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009867 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 else
9870#endif
9871#ifdef FEAT_CMDL_INFO
9872 win_redr_ruler(curwin, always);
9873#endif
9874
9875#ifdef FEAT_TITLE
9876 if (need_maketitle
9877# ifdef FEAT_STL_OPT
9878 || (p_icon && (stl_syntax & STL_IN_ICON))
9879 || (p_title && (stl_syntax & STL_IN_TITLE))
9880# endif
9881 )
9882 maketitle();
9883#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009884#ifdef FEAT_WINDOWS
9885 /* Redraw the tab pages line if needed. */
9886 if (redraw_tabline)
9887 draw_tabline();
9888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889}
9890
9891#ifdef FEAT_CMDL_INFO
9892 static void
9893win_redr_ruler(wp, always)
9894 win_T *wp;
9895 int always;
9896{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009897#define RULER_BUF_LEN 70
9898 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 int row;
9900 int fillchar;
9901 int attr;
9902 int empty_line = FALSE;
9903 colnr_T virtcol;
9904 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009905 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 int o;
9907#ifdef FEAT_VERTSPLIT
9908 int this_ru_col;
9909 int off = 0;
9910 int width = Columns;
9911# define WITH_OFF(x) x
9912# define WITH_WIDTH(x) x
9913#else
9914# define WITH_OFF(x) 0
9915# define WITH_WIDTH(x) Columns
9916# define this_ru_col ru_col
9917#endif
9918
9919 /* If 'ruler' off or redrawing disabled, don't do anything */
9920 if (!p_ru)
9921 return;
9922
9923 /*
9924 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9925 * after deleting lines, before cursor.lnum is corrected.
9926 */
9927 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9928 return;
9929
9930#ifdef FEAT_INS_EXPAND
9931 /* Don't draw the ruler while doing insert-completion, it might overwrite
9932 * the (long) mode message. */
9933# ifdef FEAT_WINDOWS
9934 if (wp == lastwin && lastwin->w_status_height == 0)
9935# endif
9936 if (edit_submode != NULL)
9937 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009938 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9939 if (pum_visible())
9940 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941#endif
9942
9943#ifdef FEAT_STL_OPT
9944 if (*p_ruf)
9945 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009946 int save_called_emsg = called_emsg;
9947
9948 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009950 if (called_emsg)
9951 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009952 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009953 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 return;
9955 }
9956#endif
9957
9958 /*
9959 * Check if not in Insert mode and the line is empty (will show "0-1").
9960 */
9961 if (!(State & INSERT)
9962 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9963 empty_line = TRUE;
9964
9965 /*
9966 * Only draw the ruler when something changed.
9967 */
9968 validate_virtcol_win(wp);
9969 if ( redraw_cmdline
9970 || always
9971 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9972 || wp->w_cursor.col != wp->w_ru_cursor.col
9973 || wp->w_virtcol != wp->w_ru_virtcol
9974#ifdef FEAT_VIRTUALEDIT
9975 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9976#endif
9977 || wp->w_topline != wp->w_ru_topline
9978 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9979#ifdef FEAT_DIFF
9980 || wp->w_topfill != wp->w_ru_topfill
9981#endif
9982 || empty_line != wp->w_ru_empty)
9983 {
9984 cursor_off();
9985#ifdef FEAT_WINDOWS
9986 if (wp->w_status_height)
9987 {
9988 row = W_WINROW(wp) + wp->w_height;
9989 fillchar = fillchar_status(&attr, wp == curwin);
9990# ifdef FEAT_VERTSPLIT
9991 off = W_WINCOL(wp);
9992 width = W_WIDTH(wp);
9993# endif
9994 }
9995 else
9996#endif
9997 {
9998 row = Rows - 1;
9999 fillchar = ' ';
10000 attr = 0;
10001#ifdef FEAT_VERTSPLIT
10002 width = Columns;
10003 off = 0;
10004#endif
10005 }
10006
10007 /* In list mode virtcol needs to be recomputed */
10008 virtcol = wp->w_virtcol;
10009 if (wp->w_p_list && lcs_tab1 == NUL)
10010 {
10011 wp->w_p_list = FALSE;
10012 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10013 wp->w_p_list = TRUE;
10014 }
10015
10016 /*
10017 * Some sprintfs return the length, some return a pointer.
10018 * To avoid portability problems we use strlen() here.
10019 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010020 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10022 ? 0L
10023 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010024 len = STRLEN(buffer);
10025 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10027 (int)virtcol + 1);
10028
10029 /*
10030 * Add a "50%" if there is room for it.
10031 * On the last line, don't print in the last column (scrolls the
10032 * screen up on some terminals).
10033 */
10034 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010035 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 o = i + vim_strsize(buffer + i + 1);
10037#ifdef FEAT_WINDOWS
10038 if (wp->w_status_height == 0) /* can't use last char of screen */
10039#endif
10040 ++o;
10041#ifdef FEAT_VERTSPLIT
10042 this_ru_col = ru_col - (Columns - width);
10043 if (this_ru_col < 0)
10044 this_ru_col = 0;
10045#endif
10046 /* Never use more than half the window/screen width, leave the other
10047 * half for the filename. */
10048 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10049 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10050 if (this_ru_col + o < WITH_WIDTH(width))
10051 {
10052 while (this_ru_col + o < WITH_WIDTH(width))
10053 {
10054#ifdef FEAT_MBYTE
10055 if (has_mbyte)
10056 i += (*mb_char2bytes)(fillchar, buffer + i);
10057 else
10058#endif
10059 buffer[i++] = fillchar;
10060 ++o;
10061 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010062 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 }
10064 /* Truncate at window boundary. */
10065#ifdef FEAT_MBYTE
10066 if (has_mbyte)
10067 {
10068 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010069 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
10071 o += (*mb_ptr2cells)(buffer + i);
10072 if (this_ru_col + o > WITH_WIDTH(width))
10073 {
10074 buffer[i] = NUL;
10075 break;
10076 }
10077 }
10078 }
10079 else
10080#endif
10081 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10082 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10083
10084 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10085 i = redraw_cmdline;
10086 screen_fill(row, row + 1,
10087 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10088 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10089 fillchar, fillchar, attr);
10090 /* don't redraw the cmdline because of showing the ruler */
10091 redraw_cmdline = i;
10092 wp->w_ru_cursor = wp->w_cursor;
10093 wp->w_ru_virtcol = wp->w_virtcol;
10094 wp->w_ru_empty = empty_line;
10095 wp->w_ru_topline = wp->w_topline;
10096 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10097#ifdef FEAT_DIFF
10098 wp->w_ru_topfill = wp->w_topfill;
10099#endif
10100 }
10101}
10102#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010103
10104#if defined(FEAT_LINEBREAK) || defined(PROTO)
10105/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010106 * Return the width of the 'number' and 'relativenumber' column.
10107 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010108 * Otherwise it depends on 'numberwidth' and the line count.
10109 */
10110 int
10111number_width(wp)
10112 win_T *wp;
10113{
10114 int n;
10115 linenr_T lnum;
10116
Bram Moolenaar64486672010-05-16 15:46:46 +020010117 if (wp->w_p_nu)
10118 /* 'number' */
10119 lnum = wp->w_buffer->b_ml.ml_line_count;
10120 else
10121 /* 'relativenumber' */
10122 lnum = wp->w_height;
10123
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010124 if (lnum == wp->w_nrwidth_line_count)
10125 return wp->w_nrwidth_width;
10126 wp->w_nrwidth_line_count = lnum;
10127
10128 n = 0;
10129 do
10130 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010131 lnum /= 10;
10132 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010133 } while (lnum > 0);
10134
10135 /* 'numberwidth' gives the minimal width plus one */
10136 if (n < wp->w_p_nuw - 1)
10137 n = wp->w_p_nuw - 1;
10138
10139 wp->w_nrwidth_width = n;
10140 return n;
10141}
10142#endif