blob: 6d2345cbe53102467a5cf1e2be24b5c86d6234e3 [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
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
112#endif
113
114/*
115 * Buffer for one screen line (characters and attributes).
116 */
117static schar_T *current_ScreenLine;
118
119static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000120static 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 +0000121#ifdef FEAT_FOLDING
122static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
123static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
124static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
125#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000126static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
128#ifdef FEAT_RIGHTLEFT
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
131#else
132static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
133# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifdef FEAT_VERTSPLIT
136static void draw_vsep_win __ARGS((win_T *wp, int row));
137#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000139static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000142#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void start_search_hl __ARGS((void));
144static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200145static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
147static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
148#endif
149static void screen_start_highlight __ARGS((int attr));
150static void screen_char __ARGS((unsigned off, int row, int col));
151#ifdef FEAT_MBYTE
152static void screen_char_2 __ARGS((unsigned off, int row, int col));
153#endif
154static void screenclear2 __ARGS((void));
155static void lineclear __ARGS((unsigned off, int width));
156static void lineinvalid __ARGS((unsigned off, int width));
157#ifdef FEAT_VERTSPLIT
158static void linecopy __ARGS((int to, int from, win_T *wp));
159static void redraw_block __ARGS((int row, int end, win_T *wp));
160#endif
161static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
162static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000164#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000165static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
168static int fillchar_status __ARGS((int *attr, int is_curwin));
169#endif
170#ifdef FEAT_VERTSPLIT
171static int fillchar_vsep __ARGS((int *attr));
172#endif
173#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000174static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_CMDL_INFO
177static void win_redr_ruler __ARGS((win_T *wp, int always));
178#endif
179
180#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
181/* Ugly global: overrule attribute used by screen_char() */
182static int screen_char_attr = 0;
183#endif
184
185/*
186 * Redraw the current window later, with update_screen(type).
187 * Set must_redraw only if not already set to a higher value.
188 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
189 */
190 void
191redraw_later(type)
192 int type;
193{
194 redraw_win_later(curwin, type);
195}
196
197 void
198redraw_win_later(wp, type)
199 win_T *wp;
200 int type;
201{
202 if (wp->w_redr_type < type)
203 {
204 wp->w_redr_type = type;
205 if (type >= NOT_VALID)
206 wp->w_lines_valid = 0;
207 if (must_redraw < type) /* must_redraw is the maximum of all windows */
208 must_redraw = type;
209 }
210}
211
212/*
213 * Force a complete redraw later. Also resets the highlighting. To be used
214 * after executing a shell command that messes up the screen.
215 */
216 void
217redraw_later_clear()
218{
219 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000220#ifdef FEAT_GUI
221 if (gui.in_use)
222 /* Use a code that will reset gui.highlight_mask in
223 * gui_stop_highlight(). */
224 screen_attr = HL_ALL + 1;
225 else
226#endif
227 /* Use attributes that is very unlikely to appear in text. */
228 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229}
230
231/*
232 * Mark all windows to be redrawn later.
233 */
234 void
235redraw_all_later(type)
236 int type;
237{
238 win_T *wp;
239
240 FOR_ALL_WINDOWS(wp)
241 {
242 redraw_win_later(wp, type);
243 }
244}
245
246/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000247 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 */
249 void
250redraw_curbuf_later(type)
251 int type;
252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
257redraw_buf_later(buf, type)
258 buf_T *buf;
259 int type;
260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
270/*
271 * Changed something in the current window, at buffer line "lnum", that
272 * requires that line and possibly other lines to be redrawn.
273 * Used when entering/leaving Insert mode with the cursor on a folded line.
274 * Used to remove the "$" from a change command.
275 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
276 * may become invalid and the whole window will have to be redrawn.
277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 void
279redrawWinline(lnum, invalid)
280 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000281 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282{
283#ifdef FEAT_FOLDING
284 int i;
285#endif
286
287 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
288 curwin->w_redraw_top = lnum;
289 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
290 curwin->w_redraw_bot = lnum;
291 redraw_later(VALID);
292
293#ifdef FEAT_FOLDING
294 if (invalid)
295 {
296 /* A w_lines[] entry for this lnum has become invalid. */
297 i = find_wl_entry(curwin, lnum);
298 if (i >= 0)
299 curwin->w_lines[i].wl_valid = FALSE;
300 }
301#endif
302}
303
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200304#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200305 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
307 * update all windows that are editing the current buffer
308 */
309 void
310update_curbuf(type)
311 int type;
312{
313 redraw_curbuf_later(type);
314 update_screen(type);
315}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
318/*
319 * update_screen()
320 *
321 * Based on the current value of curwin->w_topline, transfer a screenfull
322 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
323 */
324 void
325update_screen(type)
326 int type;
327{
328 win_T *wp;
329 static int did_intro = FALSE;
330#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
331 int did_one;
332#endif
333
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000334 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 if (!screen_valid(TRUE))
336 return;
337
338 if (must_redraw)
339 {
340 if (type < must_redraw) /* use maximal type */
341 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000342
343 /* must_redraw is reset here, so that when we run into some weird
344 * reason to redraw while busy redrawing (e.g., asynchronous
345 * scrolling), or update_topline() in win_update() will cause a
346 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 must_redraw = 0;
348 }
349
350 /* Need to update w_lines[]. */
351 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
352 type = NOT_VALID;
353
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000354 /* Postpone the redrawing when it's not needed and when being called
355 * recursively. */
356 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
358 redraw_later(type); /* remember type for next time */
359 must_redraw = type;
360 if (type > INVERTED_ALL)
361 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
362 return;
363 }
364
365 updating_screen = TRUE;
366#ifdef FEAT_SYN_HL
367 ++display_tick; /* let syntax code know we're in a next round of
368 * display updating */
369#endif
370
371 /*
372 * if the screen was scrolled up when displaying a message, scroll it down
373 */
374 if (msg_scrolled)
375 {
376 clear_cmdline = TRUE;
377 if (msg_scrolled > Rows - 5) /* clearing is faster */
378 type = CLEAR;
379 else if (type != CLEAR)
380 {
381 check_for_delay(FALSE);
382 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
383 type = CLEAR;
384 FOR_ALL_WINDOWS(wp)
385 {
386 if (W_WINROW(wp) < msg_scrolled)
387 {
388 if (W_WINROW(wp) + wp->w_height > msg_scrolled
389 && wp->w_redr_type < REDRAW_TOP
390 && wp->w_lines_valid > 0
391 && wp->w_topline == wp->w_lines[0].wl_lnum)
392 {
393 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
394 wp->w_redr_type = REDRAW_TOP;
395 }
396 else
397 {
398 wp->w_redr_type = NOT_VALID;
399#ifdef FEAT_WINDOWS
400 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
401 <= msg_scrolled)
402 wp->w_redr_status = TRUE;
403#endif
404 }
405 }
406 }
407 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000408#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000409 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 }
412 msg_scrolled = 0;
413 need_wait_return = FALSE;
414 }
415
416 /* reset cmdline_row now (may have been changed temporarily) */
417 compute_cmdrow();
418
419 /* Check for changed highlighting */
420 if (need_highlight_changed)
421 highlight_changed();
422
423 if (type == CLEAR) /* first clear screen */
424 {
425 screenclear(); /* will reset clear_cmdline */
426 type = NOT_VALID;
427 }
428
429 if (clear_cmdline) /* going to clear cmdline (done below) */
430 check_for_delay(FALSE);
431
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 /* Force redraw when width of 'number' or 'relativenumber' column
434 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200436 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
437 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000438 curwin->w_redr_type = NOT_VALID;
439#endif
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 /*
442 * Only start redrawing if there is really something to do.
443 */
444 if (type == INVERTED)
445 update_curswant();
446 if (curwin->w_redr_type < type
447 && !((type == VALID
448 && curwin->w_lines[0].wl_valid
449#ifdef FEAT_DIFF
450 && curwin->w_topfill == curwin->w_old_topfill
451 && curwin->w_botfill == curwin->w_old_botfill
452#endif
453 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
454#ifdef FEAT_VISUAL
455 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000456 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
458 && curwin->w_old_visual_mode == VIsual_mode
459 && (curwin->w_valid & VALID_VIRTCOL)
460 && curwin->w_old_curswant == curwin->w_curswant)
461#endif
462 ))
463 curwin->w_redr_type = type;
464
Bram Moolenaar5a305422006-04-28 22:38:25 +0000465#ifdef FEAT_WINDOWS
466 /* Redraw the tab pages line if needed. */
467 if (redraw_tabline || type >= NOT_VALID)
468 draw_tabline();
469#endif
470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471#ifdef FEAT_SYN_HL
472 /*
473 * Correct stored syntax highlighting info for changes in each displayed
474 * buffer. Each buffer must only be done once.
475 */
476 FOR_ALL_WINDOWS(wp)
477 {
478 if (wp->w_buffer->b_mod_set)
479 {
480# ifdef FEAT_WINDOWS
481 win_T *wwp;
482
483 /* Check if we already did this buffer. */
484 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
485 if (wwp->w_buffer == wp->w_buffer)
486 break;
487# endif
488 if (
489# ifdef FEAT_WINDOWS
490 wwp == wp &&
491# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200492 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 syn_stack_apply_changes(wp->w_buffer);
494 }
495 }
496#endif
497
498 /*
499 * Go from top to bottom through the windows, redrawing the ones that need
500 * it.
501 */
502#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
503 did_one = FALSE;
504#endif
505#ifdef FEAT_SEARCH_EXTRA
506 search_hl.rm.regprog = NULL;
507#endif
508 FOR_ALL_WINDOWS(wp)
509 {
510 if (wp->w_redr_type != 0)
511 {
512 cursor_off();
513#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
514 if (!did_one)
515 {
516 did_one = TRUE;
517# ifdef FEAT_SEARCH_EXTRA
518 start_search_hl();
519# endif
520# ifdef FEAT_CLIPBOARD
521 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200522 if (clip_star.available && clip_isautosel_star())
523 clip_update_selection(&clip_star);
524 if (clip_plus.available && clip_isautosel_plus())
525 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# endif
527#ifdef FEAT_GUI
528 /* Remove the cursor before starting to do anything, because
529 * scrolling may make it difficult to redraw the text under
530 * it. */
531 if (gui.in_use)
532 gui_undraw_cursor();
533#endif
534 }
535#endif
536 win_update(wp);
537 }
538
539#ifdef FEAT_WINDOWS
540 /* redraw status line after the window to minimize cursor movement */
541 if (wp->w_redr_status)
542 {
543 cursor_off();
544 win_redr_status(wp);
545 }
546#endif
547 }
548#if defined(FEAT_SEARCH_EXTRA)
549 end_search_hl();
550#endif
551
552#ifdef FEAT_WINDOWS
553 /* Reset b_mod_set flags. Going through all windows is probably faster
554 * than going through all buffers (there could be many buffers). */
555 for (wp = firstwin; wp != NULL; wp = wp->w_next)
556 wp->w_buffer->b_mod_set = FALSE;
557#else
558 curbuf->b_mod_set = FALSE;
559#endif
560
561 updating_screen = FALSE;
562#ifdef FEAT_GUI
563 gui_may_resize_shell();
564#endif
565
566 /* Clear or redraw the command line. Done last, because scrolling may
567 * mess up the command line. */
568 if (clear_cmdline || redraw_cmdline)
569 showmode();
570
571 /* May put up an introductory message when not editing a file */
572 if (!did_intro && bufempty()
573 && curbuf->b_fname == NULL
574#ifdef FEAT_WINDOWS
575 && firstwin->w_next == NULL
576#endif
577 && vim_strchr(p_shm, SHM_INTRO) == NULL)
578 intro_message(FALSE);
579 did_intro = TRUE;
580
581#ifdef FEAT_GUI
582 /* Redraw the cursor and update the scrollbars when all screen updating is
583 * done. */
584 if (gui.in_use)
585 {
586 out_flush(); /* required before updating the cursor */
587 if (did_one)
588 gui_update_cursor(FALSE, FALSE);
589 gui_update_scrollbars(FALSE);
590 }
591#endif
592}
593
Bram Moolenaar860cae12010-06-05 23:22:07 +0200594#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200595/*
596 * Return TRUE if the cursor line in window "wp" may be concealed, according
597 * to the 'concealcursor' option.
598 */
599 int
600conceal_cursor_line(wp)
601 win_T *wp;
602{
603 int c;
604
605 if (*wp->w_p_cocu == NUL)
606 return FALSE;
607 if (get_real_state() & VISUAL)
608 c = 'v';
609 else if (State & INSERT)
610 c = 'i';
611 else if (State & NORMAL)
612 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200613 else if (State & CMDLINE)
614 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200615 else
616 return FALSE;
617 return vim_strchr(wp->w_p_cocu, c) != NULL;
618}
619
620/*
621 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
622 */
623 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200624conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200625{
626 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
627 {
628 need_cursor_line_redraw = TRUE;
629 /* Need to recompute cursor column, e.g., when starting Visual mode
630 * without concealing. */
631 curs_columns(TRUE);
632 }
633}
634
Bram Moolenaar860cae12010-06-05 23:22:07 +0200635 void
636update_single_line(wp, lnum)
637 win_T *wp;
638 linenr_T lnum;
639{
640 int row;
641 int j;
642
643 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200644 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200645 {
646# ifdef FEAT_GUI
647 /* Remove the cursor before starting to do anything, because scrolling
648 * may make it difficult to redraw the text under it. */
649 if (gui.in_use)
650 gui_undraw_cursor();
651# endif
652 row = 0;
653 for (j = 0; j < wp->w_lines_valid; ++j)
654 {
655 if (lnum == wp->w_lines[j].wl_lnum)
656 {
657 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200658# ifdef FEAT_SEARCH_EXTRA
659 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200660 start_search_hl();
661 prepare_search_hl(wp, lnum);
662# endif
663 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
664# if defined(FEAT_SEARCH_EXTRA)
665 end_search_hl();
666# endif
667 break;
668 }
669 row += wp->w_lines[j].wl_size;
670 }
671# ifdef FEAT_GUI
672 /* Redraw the cursor */
673 if (gui.in_use)
674 {
675 out_flush(); /* required before updating the cursor */
676 gui_update_cursor(FALSE, FALSE);
677 }
678# endif
679 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200680 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200681}
682#endif
683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
685static void update_prepare __ARGS((void));
686static void update_finish __ARGS((void));
687
688/*
689 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000690 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 */
692 static void
693update_prepare()
694{
695 cursor_off();
696 updating_screen = TRUE;
697#ifdef FEAT_GUI
698 /* Remove the cursor before starting to do anything, because scrolling may
699 * make it difficult to redraw the text under it. */
700 if (gui.in_use)
701 gui_undraw_cursor();
702#endif
703#ifdef FEAT_SEARCH_EXTRA
704 start_search_hl();
705#endif
706}
707
708/*
709 * Finish updating one or more windows.
710 */
711 static void
712update_finish()
713{
714 if (redraw_cmdline)
715 showmode();
716
717# ifdef FEAT_SEARCH_EXTRA
718 end_search_hl();
719# endif
720
721 updating_screen = FALSE;
722
723# ifdef FEAT_GUI
724 gui_may_resize_shell();
725
726 /* Redraw the cursor and update the scrollbars when all screen updating is
727 * done. */
728 if (gui.in_use)
729 {
730 out_flush(); /* required before updating the cursor */
731 gui_update_cursor(FALSE, FALSE);
732 gui_update_scrollbars(FALSE);
733 }
734# endif
735}
736#endif
737
738#if defined(FEAT_SIGNS) || defined(PROTO)
739 void
740update_debug_sign(buf, lnum)
741 buf_T *buf;
742 linenr_T lnum;
743{
744 win_T *wp;
745 int doit = FALSE;
746
747# ifdef FEAT_FOLDING
748 win_foldinfo.fi_level = 0;
749# endif
750
751 /* update/delete a specific mark */
752 FOR_ALL_WINDOWS(wp)
753 {
754 if (buf != NULL && lnum > 0)
755 {
756 if (wp->w_buffer == buf && lnum >= wp->w_topline
757 && lnum < wp->w_botline)
758 {
759 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
760 wp->w_redraw_top = lnum;
761 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
762 wp->w_redraw_bot = lnum;
763 redraw_win_later(wp, VALID);
764 }
765 }
766 else
767 redraw_win_later(wp, VALID);
768 if (wp->w_redr_type != 0)
769 doit = TRUE;
770 }
771
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100772 /* Return when there is nothing to do, screen updating is already
773 * happening (recursive call) or still starting up. */
774 if (!doit || updating_screen
775#ifdef FEAT_GUI
776 || gui.starting
777#endif
778 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 return;
780
781 /* update all windows that need updating */
782 update_prepare();
783
784# ifdef FEAT_WINDOWS
785 for (wp = firstwin; wp; wp = wp->w_next)
786 {
787 if (wp->w_redr_type != 0)
788 win_update(wp);
789 if (wp->w_redr_status)
790 win_redr_status(wp);
791 }
792# else
793 if (curwin->w_redr_type != 0)
794 win_update(curwin);
795# endif
796
797 update_finish();
798}
799#endif
800
801
802#if defined(FEAT_GUI) || defined(PROTO)
803/*
804 * Update a single window, its status line and maybe the command line msg.
805 * Used for the GUI scrollbar.
806 */
807 void
808updateWindow(wp)
809 win_T *wp;
810{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000811 /* return if already busy updating */
812 if (updating_screen)
813 return;
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 update_prepare();
816
817#ifdef FEAT_CLIPBOARD
818 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200819 if (clip_star.available && clip_isautosel_star())
820 clip_update_selection(&clip_star);
821 if (clip_plus.available && clip_isautosel_plus())
822 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000828 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000829 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000830 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (wp->w_redr_status
833# ifdef FEAT_CMDL_INFO
834 || p_ru
835# endif
836# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000837 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838# endif
839 )
840 win_redr_status(wp);
841#endif
842
843 update_finish();
844}
845#endif
846
847/*
848 * Update a single window.
849 *
850 * This may cause the windows below it also to be redrawn (when clearing the
851 * screen or scrolling lines).
852 *
853 * How the window is redrawn depends on wp->w_redr_type. Each type also
854 * implies the one below it.
855 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000856 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
858 * INVERTED redraw the changed part of the Visual area
859 * INVERTED_ALL redraw the whole Visual area
860 * VALID 1. scroll up/down to adjust for a changed w_topline
861 * 2. update lines at the top when scrolled down
862 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000863 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * b_mod_top and b_mod_bot.
865 * - if wp->w_redraw_top non-zero, redraw lines between
866 * wp->w_redraw_top and wp->w_redr_bot.
867 * - continue redrawing when syntax status is invalid.
868 * 4. if scrolled up, update lines at the bottom.
869 * This results in three areas that may need updating:
870 * top: from first row to top_end (when scrolled down)
871 * mid: from mid_start to mid_end (update inversion or changed text)
872 * bot: from bot_start to last row (when scrolled up)
873 */
874 static void
875win_update(wp)
876 win_T *wp;
877{
878 buf_T *buf = wp->w_buffer;
879 int type;
880 int top_end = 0; /* Below last row of the top area that needs
881 updating. 0 when no top area updating. */
882 int mid_start = 999;/* first row of the mid area that needs
883 updating. 999 when no mid area updating. */
884 int mid_end = 0; /* Below last row of the mid area that needs
885 updating. 0 when no mid area updating. */
886 int bot_start = 999;/* first row of the bot area that needs
887 updating. 999 when no bot area updating */
888#ifdef FEAT_VISUAL
889 int scrolled_down = FALSE; /* TRUE when scrolled down when
890 w_topline got smaller a bit */
891#endif
892#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000893 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 int top_to_mod = FALSE; /* redraw above mod_top */
895#endif
896
897 int row; /* current window row to display */
898 linenr_T lnum; /* current buffer lnum to display */
899 int idx; /* current index in w_lines[] */
900 int srow; /* starting row of the current line */
901
902 int eof = FALSE; /* if TRUE, we hit the end of the file */
903 int didline = FALSE; /* if TRUE, we finished the last line */
904 int i;
905 long j;
906 static int recursive = FALSE; /* being called recursively */
907 int old_botline = wp->w_botline;
908#ifdef FEAT_FOLDING
909 long fold_count;
910#endif
911#ifdef FEAT_SYN_HL
912 /* remember what happened to the previous line, to know if
913 * check_visual_highlight() can be used */
914#define DID_NONE 1 /* didn't update a line */
915#define DID_LINE 2 /* updated a normal line */
916#define DID_FOLD 3 /* updated a folded line */
917 int did_update = DID_NONE;
918 linenr_T syntax_last_parsed = 0; /* last parsed text line */
919#endif
920 linenr_T mod_top = 0;
921 linenr_T mod_bot = 0;
922#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
923 int save_got_int;
924#endif
925
926 type = wp->w_redr_type;
927
928 if (type == NOT_VALID)
929 {
930#ifdef FEAT_WINDOWS
931 wp->w_redr_status = TRUE;
932#endif
933 wp->w_lines_valid = 0;
934 }
935
936 /* Window is zero-height: nothing to draw. */
937 if (wp->w_height == 0)
938 {
939 wp->w_redr_type = 0;
940 return;
941 }
942
943#ifdef FEAT_VERTSPLIT
944 /* Window is zero-width: Only need to draw the separator. */
945 if (wp->w_width == 0)
946 {
947 /* draw the vertical separator right of this window */
948 draw_vsep_win(wp, 0);
949 wp->w_redr_type = 0;
950 return;
951 }
952#endif
953
954#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200955 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956#endif
957
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000958#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200959 /* Force redraw when width of 'number' or 'relativenumber' column
960 * changes. */
961 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000962 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000963 {
964 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000965 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000966 }
967 else
968#endif
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
971 {
972 /*
973 * When there are both inserted/deleted lines and specific lines to be
974 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
975 * everything (only happens when redrawing is off for while).
976 */
977 type = NOT_VALID;
978 }
979 else
980 {
981 /*
982 * Set mod_top to the first line that needs displaying because of
983 * changes. Set mod_bot to the first line after the changes.
984 */
985 mod_top = wp->w_redraw_top;
986 if (wp->w_redraw_bot != 0)
987 mod_bot = wp->w_redraw_bot + 1;
988 else
989 mod_bot = 0;
990 wp->w_redraw_top = 0; /* reset for next time */
991 wp->w_redraw_bot = 0;
992 if (buf->b_mod_set)
993 {
994 if (mod_top == 0 || mod_top > buf->b_mod_top)
995 {
996 mod_top = buf->b_mod_top;
997#ifdef FEAT_SYN_HL
998 /* Need to redraw lines above the change that may be included
999 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001000 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001002 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (mod_top < 1)
1004 mod_top = 1;
1005 }
1006#endif
1007 }
1008 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1009 mod_bot = buf->b_mod_bot;
1010
1011#ifdef FEAT_SEARCH_EXTRA
1012 /* When 'hlsearch' is on and using a multi-line search pattern, a
1013 * change in one line may make the Search highlighting in a
1014 * previous line invalid. Simple solution: redraw all visible
1015 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001016 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001018 if (search_hl.rm.regprog != NULL
1019 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001021 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001022 {
1023 cur = wp->w_match_head;
1024 while (cur != NULL)
1025 {
1026 if (cur->match.regprog != NULL
1027 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001028 {
1029 top_to_mod = TRUE;
1030 break;
1031 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001032 cur = cur->next;
1033 }
1034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#endif
1036 }
1037#ifdef FEAT_FOLDING
1038 if (mod_top != 0 && hasAnyFolding(wp))
1039 {
1040 linenr_T lnumt, lnumb;
1041
1042 /*
1043 * A change in a line can cause lines above it to become folded or
1044 * unfolded. Find the top most buffer line that may be affected.
1045 * If the line was previously folded and displayed, get the first
1046 * line of that fold. If the line is folded now, get the first
1047 * folded line. Use the minimum of these two.
1048 */
1049
1050 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1051 * the line below it. If there is no valid entry, use w_topline.
1052 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1053 * to this line. If there is no valid entry, use MAXLNUM. */
1054 lnumt = wp->w_topline;
1055 lnumb = MAXLNUM;
1056 for (i = 0; i < wp->w_lines_valid; ++i)
1057 if (wp->w_lines[i].wl_valid)
1058 {
1059 if (wp->w_lines[i].wl_lastlnum < mod_top)
1060 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1061 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1062 {
1063 lnumb = wp->w_lines[i].wl_lnum;
1064 /* When there is a fold column it might need updating
1065 * in the next line ("J" just above an open fold). */
1066 if (wp->w_p_fdc > 0)
1067 ++lnumb;
1068 }
1069 }
1070
1071 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1072 if (mod_top > lnumt)
1073 mod_top = lnumt;
1074
1075 /* Now do the same for the bottom line (one above mod_bot). */
1076 --mod_bot;
1077 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1078 ++mod_bot;
1079 if (mod_bot < lnumb)
1080 mod_bot = lnumb;
1081 }
1082#endif
1083
1084 /* When a change starts above w_topline and the end is below
1085 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001086 * If the end of the change is above w_topline: do like no change was
1087 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (mod_top != 0 && mod_top < wp->w_topline)
1089 {
1090 if (mod_bot > wp->w_topline)
1091 mod_top = wp->w_topline;
1092#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001093 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 top_end = 1;
1095#endif
1096 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001097
1098 /* When line numbers are displayed need to redraw all lines below
1099 * inserted/deleted lines. */
1100 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1101 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 }
1103
1104 /*
1105 * When only displaying the lines at the top, set top_end. Used when
1106 * window has scrolled down for msg_scrolled.
1107 */
1108 if (type == REDRAW_TOP)
1109 {
1110 j = 0;
1111 for (i = 0; i < wp->w_lines_valid; ++i)
1112 {
1113 j += wp->w_lines[i].wl_size;
1114 if (j >= wp->w_upd_rows)
1115 {
1116 top_end = j;
1117 break;
1118 }
1119 }
1120 if (top_end == 0)
1121 /* not found (cannot happen?): redraw everything */
1122 type = NOT_VALID;
1123 else
1124 /* top area defined, the rest is VALID */
1125 type = VALID;
1126 }
1127
Bram Moolenaar367329b2007-08-30 11:53:22 +00001128 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001129 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1130 * non-zero and thus not FALSE) will indicate that screenclear() was not
1131 * called. */
1132 if (screen_cleared)
1133 screen_cleared = MAYBE;
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 /*
1136 * If there are no changes on the screen that require a complete redraw,
1137 * handle three cases:
1138 * 1: we are off the top of the screen by a few lines: scroll down
1139 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1140 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1141 * w_lines[] that needs updating.
1142 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001143 if ((type == VALID || type == SOME_VALID
1144 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#ifdef FEAT_DIFF
1146 && !wp->w_botfill && !wp->w_old_botfill
1147#endif
1148 )
1149 {
1150 if (mod_top != 0 && wp->w_topline == mod_top)
1151 {
1152 /*
1153 * w_topline is the first changed line, the scrolling will be done
1154 * further down.
1155 */
1156 }
1157 else if (wp->w_lines[0].wl_valid
1158 && (wp->w_topline < wp->w_lines[0].wl_lnum
1159#ifdef FEAT_DIFF
1160 || (wp->w_topline == wp->w_lines[0].wl_lnum
1161 && wp->w_topfill > wp->w_old_topfill)
1162#endif
1163 ))
1164 {
1165 /*
1166 * New topline is above old topline: May scroll down.
1167 */
1168#ifdef FEAT_FOLDING
1169 if (hasAnyFolding(wp))
1170 {
1171 linenr_T ln;
1172
1173 /* count the number of lines we are off, counting a sequence
1174 * of folded lines as one */
1175 j = 0;
1176 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1177 {
1178 ++j;
1179 if (j >= wp->w_height - 2)
1180 break;
1181 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1182 }
1183 }
1184 else
1185#endif
1186 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1187 if (j < wp->w_height - 2) /* not too far off */
1188 {
1189 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1190#ifdef FEAT_DIFF
1191 /* insert extra lines for previously invisible filler lines */
1192 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1193 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1194 - wp->w_old_topfill;
1195#endif
1196 if (i < wp->w_height - 2) /* less than a screen off */
1197 {
1198 /*
1199 * Try to insert the correct number of lines.
1200 * If not the last window, delete the lines at the bottom.
1201 * win_ins_lines may fail when the terminal can't do it.
1202 */
1203 if (i > 0)
1204 check_for_delay(FALSE);
1205 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1206 {
1207 if (wp->w_lines_valid != 0)
1208 {
1209 /* Need to update rows that are new, stop at the
1210 * first one that scrolled down. */
1211 top_end = i;
1212#ifdef FEAT_VISUAL
1213 scrolled_down = TRUE;
1214#endif
1215
1216 /* Move the entries that were scrolled, disable
1217 * the entries for the lines to be redrawn. */
1218 if ((wp->w_lines_valid += j) > wp->w_height)
1219 wp->w_lines_valid = wp->w_height;
1220 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1221 wp->w_lines[idx] = wp->w_lines[idx - j];
1222 while (idx >= 0)
1223 wp->w_lines[idx--].wl_valid = FALSE;
1224 }
1225 }
1226 else
1227 mid_start = 0; /* redraw all lines */
1228 }
1229 else
1230 mid_start = 0; /* redraw all lines */
1231 }
1232 else
1233 mid_start = 0; /* redraw all lines */
1234 }
1235 else
1236 {
1237 /*
1238 * New topline is at or below old topline: May scroll up.
1239 * When topline didn't change, find first entry in w_lines[] that
1240 * needs updating.
1241 */
1242
1243 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1244 j = -1;
1245 row = 0;
1246 for (i = 0; i < wp->w_lines_valid; i++)
1247 {
1248 if (wp->w_lines[i].wl_valid
1249 && wp->w_lines[i].wl_lnum == wp->w_topline)
1250 {
1251 j = i;
1252 break;
1253 }
1254 row += wp->w_lines[i].wl_size;
1255 }
1256 if (j == -1)
1257 {
1258 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1259 * lines */
1260 mid_start = 0;
1261 }
1262 else
1263 {
1264 /*
1265 * Try to delete the correct number of lines.
1266 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1267 */
1268#ifdef FEAT_DIFF
1269 /* If the topline didn't change, delete old filler lines,
1270 * otherwise delete filler lines of the new topline... */
1271 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1272 row += wp->w_old_topfill;
1273 else
1274 row += diff_check_fill(wp, wp->w_topline);
1275 /* ... but don't delete new filler lines. */
1276 row -= wp->w_topfill;
1277#endif
1278 if (row > 0)
1279 {
1280 check_for_delay(FALSE);
1281 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1282 bot_start = wp->w_height - row;
1283 else
1284 mid_start = 0; /* redraw all lines */
1285 }
1286 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1287 {
1288 /*
1289 * Skip the lines (below the deleted lines) that are still
1290 * valid and don't need redrawing. Copy their info
1291 * upwards, to compensate for the deleted lines. Set
1292 * bot_start to the first row that needs redrawing.
1293 */
1294 bot_start = 0;
1295 idx = 0;
1296 for (;;)
1297 {
1298 wp->w_lines[idx] = wp->w_lines[j];
1299 /* stop at line that didn't fit, unless it is still
1300 * valid (no lines deleted) */
1301 if (row > 0 && bot_start + row
1302 + (int)wp->w_lines[j].wl_size > wp->w_height)
1303 {
1304 wp->w_lines_valid = idx + 1;
1305 break;
1306 }
1307 bot_start += wp->w_lines[idx++].wl_size;
1308
1309 /* stop at the last valid entry in w_lines[].wl_size */
1310 if (++j >= wp->w_lines_valid)
1311 {
1312 wp->w_lines_valid = idx;
1313 break;
1314 }
1315 }
1316#ifdef FEAT_DIFF
1317 /* Correct the first entry for filler lines at the top
1318 * when it won't get updated below. */
1319 if (wp->w_p_diff && bot_start > 0)
1320 wp->w_lines[0].wl_size =
1321 plines_win_nofill(wp, wp->w_topline, TRUE)
1322 + wp->w_topfill;
1323#endif
1324 }
1325 }
1326 }
1327
1328 /* When starting redraw in the first line, redraw all lines. When
1329 * there is only one window it's probably faster to clear the screen
1330 * first. */
1331 if (mid_start == 0)
1332 {
1333 mid_end = wp->w_height;
1334 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001335 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001336 /* Clear the screen when it was not done by win_del_lines() or
1337 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1338 * then. */
1339 if (screen_cleared != TRUE)
1340 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001341#ifdef FEAT_WINDOWS
1342 /* The screen was cleared, redraw the tab pages line. */
1343 if (redraw_tabline)
1344 draw_tabline();
1345#endif
1346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001348
1349 /* When win_del_lines() or win_ins_lines() caused the screen to be
1350 * cleared (only happens for the first window) or when screenclear()
1351 * was called directly above, "must_redraw" will have been set to
1352 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1353 if (screen_cleared == TRUE)
1354 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 else
1357 {
1358 /* Not VALID or INVERTED: redraw all lines. */
1359 mid_start = 0;
1360 mid_end = wp->w_height;
1361 }
1362
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001363 if (type == SOME_VALID)
1364 {
1365 /* SOME_VALID: redraw all lines. */
1366 mid_start = 0;
1367 mid_end = wp->w_height;
1368 type = NOT_VALID;
1369 }
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371#ifdef FEAT_VISUAL
1372 /* check if we are updating or removing the inverted part */
1373 if ((VIsual_active && buf == curwin->w_buffer)
1374 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1375 {
1376 linenr_T from, to;
1377
1378 if (VIsual_active)
1379 {
1380 if (VIsual_active
1381 && (VIsual_mode != wp->w_old_visual_mode
1382 || type == INVERTED_ALL))
1383 {
1384 /*
1385 * If the type of Visual selection changed, redraw the whole
1386 * selection. Also when the ownership of the X selection is
1387 * gained or lost.
1388 */
1389 if (curwin->w_cursor.lnum < VIsual.lnum)
1390 {
1391 from = curwin->w_cursor.lnum;
1392 to = VIsual.lnum;
1393 }
1394 else
1395 {
1396 from = VIsual.lnum;
1397 to = curwin->w_cursor.lnum;
1398 }
1399 /* redraw more when the cursor moved as well */
1400 if (wp->w_old_cursor_lnum < from)
1401 from = wp->w_old_cursor_lnum;
1402 if (wp->w_old_cursor_lnum > to)
1403 to = wp->w_old_cursor_lnum;
1404 if (wp->w_old_visual_lnum < from)
1405 from = wp->w_old_visual_lnum;
1406 if (wp->w_old_visual_lnum > to)
1407 to = wp->w_old_visual_lnum;
1408 }
1409 else
1410 {
1411 /*
1412 * Find the line numbers that need to be updated: The lines
1413 * between the old cursor position and the current cursor
1414 * position. Also check if the Visual position changed.
1415 */
1416 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1417 {
1418 from = curwin->w_cursor.lnum;
1419 to = wp->w_old_cursor_lnum;
1420 }
1421 else
1422 {
1423 from = wp->w_old_cursor_lnum;
1424 to = curwin->w_cursor.lnum;
1425 if (from == 0) /* Visual mode just started */
1426 from = to;
1427 }
1428
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001429 if (VIsual.lnum != wp->w_old_visual_lnum
1430 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 {
1432 if (wp->w_old_visual_lnum < from
1433 && wp->w_old_visual_lnum != 0)
1434 from = wp->w_old_visual_lnum;
1435 if (wp->w_old_visual_lnum > to)
1436 to = wp->w_old_visual_lnum;
1437 if (VIsual.lnum < from)
1438 from = VIsual.lnum;
1439 if (VIsual.lnum > to)
1440 to = VIsual.lnum;
1441 }
1442 }
1443
1444 /*
1445 * If in block mode and changed column or curwin->w_curswant:
1446 * update all lines.
1447 * First compute the actual start and end column.
1448 */
1449 if (VIsual_mode == Ctrl_V)
1450 {
1451 colnr_T fromc, toc;
1452
1453 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1454 ++toc;
1455 if (curwin->w_curswant == MAXCOL)
1456 toc = MAXCOL;
1457
1458 if (fromc != wp->w_old_cursor_fcol
1459 || toc != wp->w_old_cursor_lcol)
1460 {
1461 if (from > VIsual.lnum)
1462 from = VIsual.lnum;
1463 if (to < VIsual.lnum)
1464 to = VIsual.lnum;
1465 }
1466 wp->w_old_cursor_fcol = fromc;
1467 wp->w_old_cursor_lcol = toc;
1468 }
1469 }
1470 else
1471 {
1472 /* Use the line numbers of the old Visual area. */
1473 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1474 {
1475 from = wp->w_old_cursor_lnum;
1476 to = wp->w_old_visual_lnum;
1477 }
1478 else
1479 {
1480 from = wp->w_old_visual_lnum;
1481 to = wp->w_old_cursor_lnum;
1482 }
1483 }
1484
1485 /*
1486 * There is no need to update lines above the top of the window.
1487 */
1488 if (from < wp->w_topline)
1489 from = wp->w_topline;
1490
1491 /*
1492 * If we know the value of w_botline, use it to restrict the update to
1493 * the lines that are visible in the window.
1494 */
1495 if (wp->w_valid & VALID_BOTLINE)
1496 {
1497 if (from >= wp->w_botline)
1498 from = wp->w_botline - 1;
1499 if (to >= wp->w_botline)
1500 to = wp->w_botline - 1;
1501 }
1502
1503 /*
1504 * Find the minimal part to be updated.
1505 * Watch out for scrolling that made entries in w_lines[] invalid.
1506 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1507 * top_end; need to redraw from top_end to the "to" line.
1508 * A middle mouse click with a Visual selection may change the text
1509 * above the Visual area and reset wl_valid, do count these for
1510 * mid_end (in srow).
1511 */
1512 if (mid_start > 0)
1513 {
1514 lnum = wp->w_topline;
1515 idx = 0;
1516 srow = 0;
1517 if (scrolled_down)
1518 mid_start = top_end;
1519 else
1520 mid_start = 0;
1521 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1522 {
1523 if (wp->w_lines[idx].wl_valid)
1524 mid_start += wp->w_lines[idx].wl_size;
1525 else if (!scrolled_down)
1526 srow += wp->w_lines[idx].wl_size;
1527 ++idx;
1528# ifdef FEAT_FOLDING
1529 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1530 lnum = wp->w_lines[idx].wl_lnum;
1531 else
1532# endif
1533 ++lnum;
1534 }
1535 srow += mid_start;
1536 mid_end = wp->w_height;
1537 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1538 {
1539 if (wp->w_lines[idx].wl_valid
1540 && wp->w_lines[idx].wl_lnum >= to + 1)
1541 {
1542 /* Only update until first row of this line */
1543 mid_end = srow;
1544 break;
1545 }
1546 srow += wp->w_lines[idx].wl_size;
1547 }
1548 }
1549 }
1550
1551 if (VIsual_active && buf == curwin->w_buffer)
1552 {
1553 wp->w_old_visual_mode = VIsual_mode;
1554 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1555 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001556 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 wp->w_old_curswant = curwin->w_curswant;
1558 }
1559 else
1560 {
1561 wp->w_old_visual_mode = 0;
1562 wp->w_old_cursor_lnum = 0;
1563 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001564 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566#endif /* FEAT_VISUAL */
1567
1568#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1569 /* reset got_int, otherwise regexp won't work */
1570 save_got_int = got_int;
1571 got_int = 0;
1572#endif
1573#ifdef FEAT_FOLDING
1574 win_foldinfo.fi_level = 0;
1575#endif
1576
1577 /*
1578 * Update all the window rows.
1579 */
1580 idx = 0; /* first entry in w_lines[].wl_size */
1581 row = 0;
1582 srow = 0;
1583 lnum = wp->w_topline; /* first line shown in window */
1584 for (;;)
1585 {
1586 /* stop updating when reached the end of the window (check for _past_
1587 * the end of the window is at the end of the loop) */
1588 if (row == wp->w_height)
1589 {
1590 didline = TRUE;
1591 break;
1592 }
1593
1594 /* stop updating when hit the end of the file */
1595 if (lnum > buf->b_ml.ml_line_count)
1596 {
1597 eof = TRUE;
1598 break;
1599 }
1600
1601 /* Remember the starting row of the line that is going to be dealt
1602 * with. It is used further down when the line doesn't fit. */
1603 srow = row;
1604
1605 /*
1606 * Update a line when it is in an area that needs updating, when it
1607 * has changes or w_lines[idx] is invalid.
1608 * bot_start may be halfway a wrapped line after using
1609 * win_del_lines(), check if the current line includes it.
1610 * When syntax folding is being used, the saved syntax states will
1611 * already have been updated, we can't see where the syntax state is
1612 * the same again, just update until the end of the window.
1613 */
1614 if (row < top_end
1615 || (row >= mid_start && row < mid_end)
1616#ifdef FEAT_SEARCH_EXTRA
1617 || top_to_mod
1618#endif
1619 || idx >= wp->w_lines_valid
1620 || (row + wp->w_lines[idx].wl_size > bot_start)
1621 || (mod_top != 0
1622 && (lnum == mod_top
1623 || (lnum >= mod_top
1624 && (lnum < mod_bot
1625#ifdef FEAT_SYN_HL
1626 || did_update == DID_FOLD
1627 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001628 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 && (
1630# ifdef FEAT_FOLDING
1631 (foldmethodIsSyntax(wp)
1632 && hasAnyFolding(wp)) ||
1633# endif
1634 syntax_check_changed(lnum)))
1635#endif
1636 )))))
1637 {
1638#ifdef FEAT_SEARCH_EXTRA
1639 if (lnum == mod_top)
1640 top_to_mod = FALSE;
1641#endif
1642
1643 /*
1644 * When at start of changed lines: May scroll following lines
1645 * up or down to minimize redrawing.
1646 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001647 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 */
1649 if (lnum == mod_top
1650 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001651 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
1653 int old_rows = 0;
1654 int new_rows = 0;
1655 int xtra_rows;
1656 linenr_T l;
1657
1658 /* Count the old number of window rows, using w_lines[], which
1659 * should still contain the sizes for the lines as they are
1660 * currently displayed. */
1661 for (i = idx; i < wp->w_lines_valid; ++i)
1662 {
1663 /* Only valid lines have a meaningful wl_lnum. Invalid
1664 * lines are part of the changed area. */
1665 if (wp->w_lines[i].wl_valid
1666 && wp->w_lines[i].wl_lnum == mod_bot)
1667 break;
1668 old_rows += wp->w_lines[i].wl_size;
1669#ifdef FEAT_FOLDING
1670 if (wp->w_lines[i].wl_valid
1671 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1672 {
1673 /* Must have found the last valid entry above mod_bot.
1674 * Add following invalid entries. */
1675 ++i;
1676 while (i < wp->w_lines_valid
1677 && !wp->w_lines[i].wl_valid)
1678 old_rows += wp->w_lines[i++].wl_size;
1679 break;
1680 }
1681#endif
1682 }
1683
1684 if (i >= wp->w_lines_valid)
1685 {
1686 /* We can't find a valid line below the changed lines,
1687 * need to redraw until the end of the window.
1688 * Inserting/deleting lines has no use. */
1689 bot_start = 0;
1690 }
1691 else
1692 {
1693 /* Able to count old number of rows: Count new window
1694 * rows, and may insert/delete lines */
1695 j = idx;
1696 for (l = lnum; l < mod_bot; ++l)
1697 {
1698#ifdef FEAT_FOLDING
1699 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1700 ++new_rows;
1701 else
1702#endif
1703#ifdef FEAT_DIFF
1704 if (l == wp->w_topline)
1705 new_rows += plines_win_nofill(wp, l, TRUE)
1706 + wp->w_topfill;
1707 else
1708#endif
1709 new_rows += plines_win(wp, l, TRUE);
1710 ++j;
1711 if (new_rows > wp->w_height - row - 2)
1712 {
1713 /* it's getting too much, must redraw the rest */
1714 new_rows = 9999;
1715 break;
1716 }
1717 }
1718 xtra_rows = new_rows - old_rows;
1719 if (xtra_rows < 0)
1720 {
1721 /* May scroll text up. If there is not enough
1722 * remaining text or scrolling fails, must redraw the
1723 * rest. If scrolling works, must redraw the text
1724 * below the scrolled text. */
1725 if (row - xtra_rows >= wp->w_height - 2)
1726 mod_bot = MAXLNUM;
1727 else
1728 {
1729 check_for_delay(FALSE);
1730 if (win_del_lines(wp, row,
1731 -xtra_rows, FALSE, FALSE) == FAIL)
1732 mod_bot = MAXLNUM;
1733 else
1734 bot_start = wp->w_height + xtra_rows;
1735 }
1736 }
1737 else if (xtra_rows > 0)
1738 {
1739 /* May scroll text down. If there is not enough
1740 * remaining text of scrolling fails, must redraw the
1741 * rest. */
1742 if (row + xtra_rows >= wp->w_height - 2)
1743 mod_bot = MAXLNUM;
1744 else
1745 {
1746 check_for_delay(FALSE);
1747 if (win_ins_lines(wp, row + old_rows,
1748 xtra_rows, FALSE, FALSE) == FAIL)
1749 mod_bot = MAXLNUM;
1750 else if (top_end > row + old_rows)
1751 /* Scrolled the part at the top that requires
1752 * updating down. */
1753 top_end += xtra_rows;
1754 }
1755 }
1756
1757 /* When not updating the rest, may need to move w_lines[]
1758 * entries. */
1759 if (mod_bot != MAXLNUM && i != j)
1760 {
1761 if (j < i)
1762 {
1763 int x = row + new_rows;
1764
1765 /* move entries in w_lines[] upwards */
1766 for (;;)
1767 {
1768 /* stop at last valid entry in w_lines[] */
1769 if (i >= wp->w_lines_valid)
1770 {
1771 wp->w_lines_valid = j;
1772 break;
1773 }
1774 wp->w_lines[j] = wp->w_lines[i];
1775 /* stop at a line that won't fit */
1776 if (x + (int)wp->w_lines[j].wl_size
1777 > wp->w_height)
1778 {
1779 wp->w_lines_valid = j + 1;
1780 break;
1781 }
1782 x += wp->w_lines[j++].wl_size;
1783 ++i;
1784 }
1785 if (bot_start > x)
1786 bot_start = x;
1787 }
1788 else /* j > i */
1789 {
1790 /* move entries in w_lines[] downwards */
1791 j -= i;
1792 wp->w_lines_valid += j;
1793 if (wp->w_lines_valid > wp->w_height)
1794 wp->w_lines_valid = wp->w_height;
1795 for (i = wp->w_lines_valid; i - j >= idx; --i)
1796 wp->w_lines[i] = wp->w_lines[i - j];
1797
1798 /* The w_lines[] entries for inserted lines are
1799 * now invalid, but wl_size may be used above.
1800 * Reset to zero. */
1801 while (i >= idx)
1802 {
1803 wp->w_lines[i].wl_size = 0;
1804 wp->w_lines[i--].wl_valid = FALSE;
1805 }
1806 }
1807 }
1808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
1810
1811#ifdef FEAT_FOLDING
1812 /*
1813 * When lines are folded, display one line for all of them.
1814 * Otherwise, display normally (can be several display lines when
1815 * 'wrap' is on).
1816 */
1817 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1818 if (fold_count != 0)
1819 {
1820 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1821 ++row;
1822 --fold_count;
1823 wp->w_lines[idx].wl_folded = TRUE;
1824 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1825# ifdef FEAT_SYN_HL
1826 did_update = DID_FOLD;
1827# endif
1828 }
1829 else
1830#endif
1831 if (idx < wp->w_lines_valid
1832 && wp->w_lines[idx].wl_valid
1833 && wp->w_lines[idx].wl_lnum == lnum
1834 && lnum > wp->w_topline
1835 && !(dy_flags & DY_LASTLINE)
1836 && srow + wp->w_lines[idx].wl_size > wp->w_height
1837#ifdef FEAT_DIFF
1838 && diff_check_fill(wp, lnum) == 0
1839#endif
1840 )
1841 {
1842 /* This line is not going to fit. Don't draw anything here,
1843 * will draw "@ " lines below. */
1844 row = wp->w_height + 1;
1845 }
1846 else
1847 {
1848#ifdef FEAT_SEARCH_EXTRA
1849 prepare_search_hl(wp, lnum);
1850#endif
1851#ifdef FEAT_SYN_HL
1852 /* Let the syntax stuff know we skipped a few lines. */
1853 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001854 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 syntax_end_parsing(syntax_last_parsed + 1);
1856#endif
1857
1858 /*
1859 * Display one line.
1860 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001861 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863#ifdef FEAT_FOLDING
1864 wp->w_lines[idx].wl_folded = FALSE;
1865 wp->w_lines[idx].wl_lastlnum = lnum;
1866#endif
1867#ifdef FEAT_SYN_HL
1868 did_update = DID_LINE;
1869 syntax_last_parsed = lnum;
1870#endif
1871 }
1872
1873 wp->w_lines[idx].wl_lnum = lnum;
1874 wp->w_lines[idx].wl_valid = TRUE;
1875 if (row > wp->w_height) /* past end of screen */
1876 {
1877 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001878 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1880 ++idx;
1881 break;
1882 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001883 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 wp->w_lines[idx].wl_size = row - srow;
1885 ++idx;
1886#ifdef FEAT_FOLDING
1887 lnum += fold_count + 1;
1888#else
1889 ++lnum;
1890#endif
1891 }
1892 else
1893 {
1894 /* This line does not need updating, advance to the next one */
1895 row += wp->w_lines[idx++].wl_size;
1896 if (row > wp->w_height) /* past end of screen */
1897 break;
1898#ifdef FEAT_FOLDING
1899 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1900#else
1901 ++lnum;
1902#endif
1903#ifdef FEAT_SYN_HL
1904 did_update = DID_NONE;
1905#endif
1906 }
1907
1908 if (lnum > buf->b_ml.ml_line_count)
1909 {
1910 eof = TRUE;
1911 break;
1912 }
1913 }
1914 /*
1915 * End of loop over all window lines.
1916 */
1917
1918
1919 if (idx > wp->w_lines_valid)
1920 wp->w_lines_valid = idx;
1921
1922#ifdef FEAT_SYN_HL
1923 /*
1924 * Let the syntax stuff know we stop parsing here.
1925 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001926 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 syntax_end_parsing(syntax_last_parsed + 1);
1928#endif
1929
1930 /*
1931 * If we didn't hit the end of the file, and we didn't finish the last
1932 * line we were working on, then the line didn't fit.
1933 */
1934 wp->w_empty_rows = 0;
1935#ifdef FEAT_DIFF
1936 wp->w_filler_rows = 0;
1937#endif
1938 if (!eof && !didline)
1939 {
1940 if (lnum == wp->w_topline)
1941 {
1942 /*
1943 * Single line that does not fit!
1944 * Don't overwrite it, it can be edited.
1945 */
1946 wp->w_botline = lnum + 1;
1947 }
1948#ifdef FEAT_DIFF
1949 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1950 {
1951 /* Window ends in filler lines. */
1952 wp->w_botline = lnum;
1953 wp->w_filler_rows = wp->w_height - srow;
1954 }
1955#endif
1956 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1957 {
1958 /*
1959 * Last line isn't finished: Display "@@@" at the end.
1960 */
1961 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1962 W_WINROW(wp) + wp->w_height,
1963 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1964 '@', '@', hl_attr(HLF_AT));
1965 set_empty_rows(wp, srow);
1966 wp->w_botline = lnum;
1967 }
1968 else
1969 {
1970 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1971 wp->w_botline = lnum;
1972 }
1973 }
1974 else
1975 {
1976#ifdef FEAT_VERTSPLIT
1977 draw_vsep_win(wp, row);
1978#endif
1979 if (eof) /* we hit the end of the file */
1980 {
1981 wp->w_botline = buf->b_ml.ml_line_count + 1;
1982#ifdef FEAT_DIFF
1983 j = diff_check_fill(wp, wp->w_botline);
1984 if (j > 0 && !wp->w_botfill)
1985 {
1986 /*
1987 * Display filler lines at the end of the file
1988 */
1989 if (char2cells(fill_diff) > 1)
1990 i = '-';
1991 else
1992 i = fill_diff;
1993 if (row + j > wp->w_height)
1994 j = wp->w_height - row;
1995 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1996 row += j;
1997 }
1998#endif
1999 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002000 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 wp->w_botline = lnum;
2002
2003 /* make sure the rest of the screen is blank */
2004 /* put '~'s on rows that aren't part of the file. */
2005 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2006 }
2007
2008 /* Reset the type of redrawing required, the window has been updated. */
2009 wp->w_redr_type = 0;
2010#ifdef FEAT_DIFF
2011 wp->w_old_topfill = wp->w_topfill;
2012 wp->w_old_botfill = wp->w_botfill;
2013#endif
2014
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002015 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
2017 /*
2018 * There is a trick with w_botline. If we invalidate it on each
2019 * change that might modify it, this will cause a lot of expensive
2020 * calls to plines() in update_topline() each time. Therefore the
2021 * value of w_botline is often approximated, and this value is used to
2022 * compute the value of w_topline. If the value of w_botline was
2023 * wrong, check that the value of w_topline is correct (cursor is on
2024 * the visible part of the text). If it's not, we need to redraw
2025 * again. Mostly this just means scrolling up a few lines, so it
2026 * doesn't look too bad. Only do this for the current window (where
2027 * changes are relevant).
2028 */
2029 wp->w_valid |= VALID_BOTLINE;
2030 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2031 {
2032 recursive = TRUE;
2033 curwin->w_valid &= ~VALID_TOPLINE;
2034 update_topline(); /* may invalidate w_botline again */
2035 if (must_redraw != 0)
2036 {
2037 /* Don't update for changes in buffer again. */
2038 i = curbuf->b_mod_set;
2039 curbuf->b_mod_set = FALSE;
2040 win_update(curwin);
2041 must_redraw = 0;
2042 curbuf->b_mod_set = i;
2043 }
2044 recursive = FALSE;
2045 }
2046 }
2047
2048#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2049 /* restore got_int, unless CTRL-C was hit while redrawing */
2050 if (!got_int)
2051 got_int = save_got_int;
2052#endif
2053}
2054
2055#ifdef FEAT_SIGNS
2056static int draw_signcolumn __ARGS((win_T *wp));
2057
2058/*
2059 * Return TRUE when window "wp" has a column to draw signs in.
2060 */
2061 static int
2062draw_signcolumn(wp)
2063 win_T *wp;
2064{
2065 return (wp->w_buffer->b_signlist != NULL
2066# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002067 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068# endif
2069 );
2070}
2071#endif
2072
2073/*
2074 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2075 * as the filler character.
2076 */
2077 static void
2078win_draw_end(wp, c1, c2, row, endrow, hl)
2079 win_T *wp;
2080 int c1;
2081 int c2;
2082 int row;
2083 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002084 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085{
2086#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2087 int n = 0;
2088# define FDC_OFF n
2089#else
2090# define FDC_OFF 0
2091#endif
2092
2093#ifdef FEAT_RIGHTLEFT
2094 if (wp->w_p_rl)
2095 {
2096 /* No check for cmdline window: should never be right-left. */
2097# ifdef FEAT_FOLDING
2098 n = wp->w_p_fdc;
2099
2100 if (n > 0)
2101 {
2102 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002103 if (n > W_WIDTH(wp))
2104 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2106 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2107 ' ', ' ', hl_attr(HLF_FC));
2108 }
2109# endif
2110# ifdef FEAT_SIGNS
2111 if (draw_signcolumn(wp))
2112 {
2113 int nn = n + 2;
2114
2115 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002116 if (nn > W_WIDTH(wp))
2117 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2119 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2120 ' ', ' ', hl_attr(HLF_SC));
2121 n = nn;
2122 }
2123# endif
2124 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2125 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2126 c2, c2, hl_attr(hl));
2127 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2128 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2129 c1, c2, hl_attr(hl));
2130 }
2131 else
2132#endif
2133 {
2134#ifdef FEAT_CMDWIN
2135 if (cmdwin_type != 0 && wp == curwin)
2136 {
2137 /* draw the cmdline character in the leftmost column */
2138 n = 1;
2139 if (n > wp->w_width)
2140 n = wp->w_width;
2141 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2142 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2143 cmdwin_type, ' ', hl_attr(HLF_AT));
2144 }
2145#endif
2146#ifdef FEAT_FOLDING
2147 if (wp->w_p_fdc > 0)
2148 {
2149 int nn = n + wp->w_p_fdc;
2150
2151 /* draw the fold column at the left */
2152 if (nn > W_WIDTH(wp))
2153 nn = W_WIDTH(wp);
2154 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2155 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2156 ' ', ' ', hl_attr(HLF_FC));
2157 n = nn;
2158 }
2159#endif
2160#ifdef FEAT_SIGNS
2161 if (draw_signcolumn(wp))
2162 {
2163 int nn = n + 2;
2164
2165 /* draw the sign column after the fold column */
2166 if (nn > W_WIDTH(wp))
2167 nn = W_WIDTH(wp);
2168 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2169 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2170 ' ', ' ', hl_attr(HLF_SC));
2171 n = nn;
2172 }
2173#endif
2174 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2175 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2176 c1, c2, hl_attr(hl));
2177 }
2178 set_empty_rows(wp, row);
2179}
2180
Bram Moolenaar1a384422010-07-14 19:53:30 +02002181#ifdef FEAT_SYN_HL
2182static int advance_color_col __ARGS((int vcol, int **color_cols));
2183
2184/*
2185 * Advance **color_cols and return TRUE when there are columns to draw.
2186 */
2187 static int
2188advance_color_col(vcol, color_cols)
2189 int vcol;
2190 int **color_cols;
2191{
2192 while (**color_cols >= 0 && vcol > **color_cols)
2193 ++*color_cols;
2194 return (**color_cols >= 0);
2195}
2196#endif
2197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#ifdef FEAT_FOLDING
2199/*
2200 * Display one folded line.
2201 */
2202 static void
2203fold_line(wp, fold_count, foldinfo, lnum, row)
2204 win_T *wp;
2205 long fold_count;
2206 foldinfo_T *foldinfo;
2207 linenr_T lnum;
2208 int row;
2209{
2210 char_u buf[51];
2211 pos_T *top, *bot;
2212 linenr_T lnume = lnum + fold_count - 1;
2213 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002214 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 int col;
2217 int txtcol;
2218 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002219 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
2221 /* Build the fold line:
2222 * 1. Add the cmdwin_type for the command-line window
2223 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002224 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 * 4. Compose the text
2226 * 5. Add the text
2227 * 6. set highlighting for the Visual area an other text
2228 */
2229 col = 0;
2230
2231 /*
2232 * 1. Add the cmdwin_type for the command-line window
2233 * Ignores 'rightleft', this window is never right-left.
2234 */
2235#ifdef FEAT_CMDWIN
2236 if (cmdwin_type != 0 && wp == curwin)
2237 {
2238 ScreenLines[off] = cmdwin_type;
2239 ScreenAttrs[off] = hl_attr(HLF_AT);
2240#ifdef FEAT_MBYTE
2241 if (enc_utf8)
2242 ScreenLinesUC[off] = 0;
2243#endif
2244 ++col;
2245 }
2246#endif
2247
2248 /*
2249 * 2. Add the 'foldcolumn'
2250 */
2251 fdc = wp->w_p_fdc;
2252 if (fdc > W_WIDTH(wp) - col)
2253 fdc = W_WIDTH(wp) - col;
2254 if (fdc > 0)
2255 {
2256 fill_foldcolumn(buf, wp, TRUE, lnum);
2257#ifdef FEAT_RIGHTLEFT
2258 if (wp->w_p_rl)
2259 {
2260 int i;
2261
2262 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2263 hl_attr(HLF_FC));
2264 /* reverse the fold column */
2265 for (i = 0; i < fdc; ++i)
2266 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2267 }
2268 else
2269#endif
2270 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2271 col += fdc;
2272 }
2273
2274#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002275# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2276 for (ri = 0; ri < l; ++ri) \
2277 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2278 else \
2279 for (ri = 0; ri < l; ++ri) \
2280 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002282# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2283 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#endif
2285
Bram Moolenaar64486672010-05-16 15:46:46 +02002286 /* Set all attributes of the 'number' or 'relativenumber' column and the
2287 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002288 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290#ifdef FEAT_SIGNS
2291 /* If signs are being displayed, add two spaces. */
2292 if (draw_signcolumn(wp))
2293 {
2294 len = W_WIDTH(wp) - col;
2295 if (len > 0)
2296 {
2297 if (len > 2)
2298 len = 2;
2299# ifdef FEAT_RIGHTLEFT
2300 if (wp->w_p_rl)
2301 /* the line number isn't reversed */
2302 copy_text_attr(off + W_WIDTH(wp) - len - col,
2303 (char_u *)" ", len, hl_attr(HLF_FL));
2304 else
2305# endif
2306 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2307 col += len;
2308 }
2309 }
2310#endif
2311
2312 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002313 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002315 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 len = W_WIDTH(wp) - col;
2318 if (len > 0)
2319 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002320 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002321 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01002322 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002323
2324 if (len > w + 1)
2325 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002326
2327 if (wp->w_p_nu)
2328 /* 'number' */
2329 num = (long)lnum;
2330 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002331 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002332 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002333 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar700e7342013-01-30 12:31:36 +01002334 if (num == 0)
2335 {
2336 num = lnum;
2337 fmt = "%-*ld ";
2338 }
2339 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002340
Bram Moolenaar700e7342013-01-30 12:31:36 +01002341 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342#ifdef FEAT_RIGHTLEFT
2343 if (wp->w_p_rl)
2344 /* the line number isn't reversed */
2345 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2346 hl_attr(HLF_FL));
2347 else
2348#endif
2349 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2350 col += len;
2351 }
2352 }
2353
2354 /*
2355 * 4. Compose the folded-line string with 'foldtext', if set.
2356 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002357 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 txtcol = col; /* remember where text starts */
2360
2361 /*
2362 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2363 * Right-left text is put in columns 0 - number-col, normal text is put
2364 * in columns number-col - window-width.
2365 */
2366#ifdef FEAT_MBYTE
2367 if (has_mbyte)
2368 {
2369 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002370 int u8c, u8cc[MAX_MCO];
2371 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 int idx;
2373 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002374 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375# ifdef FEAT_ARABIC
2376 int prev_c = 0; /* previous Arabic character */
2377 int prev_c1 = 0; /* first composing char for prev_c */
2378# endif
2379
2380# ifdef FEAT_RIGHTLEFT
2381 if (wp->w_p_rl)
2382 idx = off;
2383 else
2384# endif
2385 idx = off + col;
2386
2387 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2388 for (p = text; *p != NUL; )
2389 {
2390 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002391 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (col + cells > W_WIDTH(wp)
2393# ifdef FEAT_RIGHTLEFT
2394 - (wp->w_p_rl ? col : 0)
2395# endif
2396 )
2397 break;
2398 ScreenLines[idx] = *p;
2399 if (enc_utf8)
2400 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002401 u8c = utfc_ptr2char(p, u8cc);
2402 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
2404 ScreenLinesUC[idx] = 0;
2405#ifdef FEAT_ARABIC
2406 prev_c = u8c;
2407#endif
2408 }
2409 else
2410 {
2411#ifdef FEAT_ARABIC
2412 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2413 {
2414 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002415 int pc, pc1, nc;
2416 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 int firstbyte = *p;
2418
2419 /* The idea of what is the previous and next
2420 * character depends on 'rightleft'. */
2421 if (wp->w_p_rl)
2422 {
2423 pc = prev_c;
2424 pc1 = prev_c1;
2425 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002426 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 }
2428 else
2429 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002430 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002432 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 }
2434 prev_c = u8c;
2435
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002436 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 pc, pc1, nc);
2438 ScreenLines[idx] = firstbyte;
2439 }
2440 else
2441 prev_c = u8c;
2442#endif
2443 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002444#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 if (u8c >= 0x10000)
2446 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2447 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002450 for (i = 0; i < Screen_mco; ++i)
2451 {
2452 ScreenLinesC[i][idx] = u8cc[i];
2453 if (u8cc[i] == 0)
2454 break;
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457 if (cells > 1)
2458 ScreenLines[idx + 1] = 0;
2459 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002460 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2461 /* double-byte single width character */
2462 ScreenLines2[idx] = p[1];
2463 else if (cells > 1)
2464 /* double-width character */
2465 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 col += cells;
2467 idx += cells;
2468 p += c_len;
2469 }
2470 }
2471 else
2472#endif
2473 {
2474 len = (int)STRLEN(text);
2475 if (len > W_WIDTH(wp) - col)
2476 len = W_WIDTH(wp) - col;
2477 if (len > 0)
2478 {
2479#ifdef FEAT_RIGHTLEFT
2480 if (wp->w_p_rl)
2481 STRNCPY(current_ScreenLine, text, len);
2482 else
2483#endif
2484 STRNCPY(current_ScreenLine + col, text, len);
2485 col += len;
2486 }
2487 }
2488
2489 /* Fill the rest of the line with the fold filler */
2490#ifdef FEAT_RIGHTLEFT
2491 if (wp->w_p_rl)
2492 col -= txtcol;
2493#endif
2494 while (col < W_WIDTH(wp)
2495#ifdef FEAT_RIGHTLEFT
2496 - (wp->w_p_rl ? txtcol : 0)
2497#endif
2498 )
2499 {
2500#ifdef FEAT_MBYTE
2501 if (enc_utf8)
2502 {
2503 if (fill_fold >= 0x80)
2504 {
2505 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002506 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 }
2508 else
2509 ScreenLinesUC[off + col] = 0;
2510 }
2511#endif
2512 ScreenLines[off + col++] = fill_fold;
2513 }
2514
2515 if (text != buf)
2516 vim_free(text);
2517
2518 /*
2519 * 6. set highlighting for the Visual area an other text.
2520 * If all folded lines are in the Visual area, highlight the line.
2521 */
2522#ifdef FEAT_VISUAL
2523 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2524 {
2525 if (ltoreq(curwin->w_cursor, VIsual))
2526 {
2527 /* Visual is after curwin->w_cursor */
2528 top = &curwin->w_cursor;
2529 bot = &VIsual;
2530 }
2531 else
2532 {
2533 /* Visual is before curwin->w_cursor */
2534 top = &VIsual;
2535 bot = &curwin->w_cursor;
2536 }
2537 if (lnum >= top->lnum
2538 && lnume <= bot->lnum
2539 && (VIsual_mode != 'v'
2540 || ((lnum > top->lnum
2541 || (lnum == top->lnum
2542 && top->col == 0))
2543 && (lnume < bot->lnum
2544 || (lnume == bot->lnum
2545 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002546 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548 if (VIsual_mode == Ctrl_V)
2549 {
2550 /* Visual block mode: highlight the chars part of the block */
2551 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2552 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002553 if (wp->w_old_cursor_lcol != MAXCOL
2554 && wp->w_old_cursor_lcol + txtcol
2555 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 len = wp->w_old_cursor_lcol;
2557 else
2558 len = W_WIDTH(wp) - txtcol;
2559 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002560 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562 }
2563 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002566 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 }
2570#endif
2571
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002572#ifdef FEAT_SYN_HL
2573 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002574 if (wp->w_p_cuc)
2575 {
2576 txtcol += wp->w_virtcol;
2577 if (wp->w_p_wrap)
2578 txtcol -= wp->w_skipcol;
2579 else
2580 txtcol -= wp->w_leftcol;
2581 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2582 ScreenAttrs[off + txtcol] = hl_combine_attr(
2583 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2584 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2588 (int)W_WIDTH(wp), FALSE);
2589
2590 /*
2591 * Update w_cline_height and w_cline_folded if the cursor line was
2592 * updated (saves a call to plines() later).
2593 */
2594 if (wp == curwin
2595 && lnum <= curwin->w_cursor.lnum
2596 && lnume >= curwin->w_cursor.lnum)
2597 {
2598 curwin->w_cline_row = row;
2599 curwin->w_cline_height = 1;
2600 curwin->w_cline_folded = TRUE;
2601 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2602 }
2603}
2604
2605/*
2606 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2607 */
2608 static void
2609copy_text_attr(off, buf, len, attr)
2610 int off;
2611 char_u *buf;
2612 int len;
2613 int attr;
2614{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002615 int i;
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 mch_memmove(ScreenLines + off, buf, (size_t)len);
2618# ifdef FEAT_MBYTE
2619 if (enc_utf8)
2620 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2621# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002622 for (i = 0; i < len; ++i)
2623 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624}
2625
2626/*
2627 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002628 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
2630 static void
2631fill_foldcolumn(p, wp, closed, lnum)
2632 char_u *p;
2633 win_T *wp;
2634 int closed; /* TRUE of FALSE */
2635 linenr_T lnum; /* current line number */
2636{
2637 int i = 0;
2638 int level;
2639 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002640 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 /* Init to all spaces. */
2643 copy_spaces(p, (size_t)wp->w_p_fdc);
2644
2645 level = win_foldinfo.fi_level;
2646 if (level > 0)
2647 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002648 /* If there is only one column put more info in it. */
2649 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2650
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 /* If the column is too narrow, we start at the lowest level that
2652 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002653 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (first_level < 1)
2655 first_level = 1;
2656
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002657 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 if (win_foldinfo.fi_lnum == lnum
2660 && first_level + i >= win_foldinfo.fi_low_level)
2661 p[i] = '-';
2662 else if (first_level == 1)
2663 p[i] = '|';
2664 else if (first_level + i <= 9)
2665 p[i] = '0' + first_level + i;
2666 else
2667 p[i] = '>';
2668 if (first_level + i == level)
2669 break;
2670 }
2671 }
2672 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002673 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674}
2675#endif /* FEAT_FOLDING */
2676
2677/*
2678 * Display line "lnum" of window 'wp' on the screen.
2679 * Start at row "startrow", stop when "endrow" is reached.
2680 * wp->w_virtcol needs to be valid.
2681 *
2682 * Return the number of last row the line occupies.
2683 */
2684 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002685win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 win_T *wp;
2687 linenr_T lnum;
2688 int startrow;
2689 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002690 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 int col; /* visual column on screen */
2693 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2694 int c = 0; /* init for GCC */
2695 long vcol = 0; /* virtual column (for tabs) */
2696 long vcol_prev = -1; /* "vcol" of previous character */
2697 char_u *line; /* current line */
2698 char_u *ptr; /* current position in "line" */
2699 int row; /* row in the window, excl w_winrow */
2700 int screen_row; /* row on the screen, incl w_winrow */
2701
2702 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2703 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002704 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 int c_extra = NUL; /* extra chars, all the same */
2706 int extra_attr = 0; /* attributes when n_extra != 0 */
2707 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2708 displaying lcs_eol at end-of-line */
2709 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2710 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2711
2712 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2713 int saved_n_extra = 0;
2714 char_u *saved_p_extra = NULL;
2715 int saved_c_extra = 0;
2716 int saved_char_attr = 0;
2717
2718 int n_attr = 0; /* chars with special attr */
2719 int saved_attr2 = 0; /* char_attr saved for n_attr */
2720 int n_attr3 = 0; /* chars with overruling special attr */
2721 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2722
2723 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2724
2725 int fromcol, tocol; /* start/end of inverting */
2726 int fromcol_prev = -2; /* start of inverting after cursor */
2727 int noinvcur = FALSE; /* don't invert the cursor */
2728#ifdef FEAT_VISUAL
2729 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002730 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731#endif
2732 pos_T pos;
2733 long v;
2734
2735 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002736 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2738 in this line */
2739 int attr = 0; /* attributes for area highlighting */
2740 int area_attr = 0; /* attributes desired by highlighting */
2741 int search_attr = 0; /* attributes desired by 'hlsearch' */
2742#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002743 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 int syntax_attr = 0; /* attributes desired by syntax */
2745 int has_syntax = FALSE; /* this buffer has syntax highl. */
2746 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002747 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002748 int draw_color_col = FALSE; /* highlight colorcolumn */
2749 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002750#endif
2751#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002752 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002753# define SPWORDLEN 150
2754 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002755 int nextlinecol = 0; /* column where nextline[] starts */
2756 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002757 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002758 int spell_attr = 0; /* attributes desired by spelling */
2759 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002760 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2761 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002762 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002763 static int cap_col = -1; /* column to check for Cap word */
2764 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002765 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766#endif
2767 int extra_check; /* has syntax or linebreak */
2768#ifdef FEAT_MBYTE
2769 int multi_attr = 0; /* attributes desired by multibyte */
2770 int mb_l = 1; /* multi-byte byte length */
2771 int mb_c = 0; /* decoded multi-byte character */
2772 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002773 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774#endif
2775#ifdef FEAT_DIFF
2776 int filler_lines; /* nr of filler lines to be drawn */
2777 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002778 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 int change_start = MAXCOL; /* first col of changed area */
2780 int change_end = -1; /* last col of changed area */
2781#endif
2782 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2783#ifdef FEAT_LINEBREAK
2784 int need_showbreak = FALSE;
2785#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002786#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2787 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002789 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#endif
2791#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002792 matchitem_T *cur; /* points to the match list */
2793 match_T *shl; /* points to search_hl or a match */
2794 int shl_flag; /* flag to indicate whether search_hl
2795 has been processed or not */
2796 int prevcol_hl_flag; /* flag to indicate whether prevcol
2797 equals startcol of search_hl or one
2798 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#endif
2800#ifdef FEAT_ARABIC
2801 int prev_c = 0; /* previous Arabic character */
2802 int prev_c1 = 0; /* first composing char for prev_c */
2803#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002804#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002805 int did_line_attr = 0;
2806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808 /* draw_state: items that are drawn in sequence: */
2809#define WL_START 0 /* nothing done yet */
2810#ifdef FEAT_CMDWIN
2811# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2812#else
2813# define WL_CMDLINE WL_START
2814#endif
2815#ifdef FEAT_FOLDING
2816# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2817#else
2818# define WL_FOLD WL_CMDLINE
2819#endif
2820#ifdef FEAT_SIGNS
2821# define WL_SIGN WL_FOLD + 1 /* column for signs */
2822#else
2823# define WL_SIGN WL_FOLD /* column for signs */
2824#endif
2825#define WL_NR WL_SIGN + 1 /* line number */
2826#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2827# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2828#else
2829# define WL_SBR WL_NR
2830#endif
2831#define WL_LINE WL_SBR + 1 /* text in the line */
2832 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002833#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 int feedback_col = 0;
2835 int feedback_old_attr = -1;
2836#endif
2837
Bram Moolenaar860cae12010-06-05 23:22:07 +02002838#ifdef FEAT_CONCEAL
2839 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002840 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002841 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002842 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002843 int is_concealing = FALSE;
2844 int boguscols = 0; /* nonexistent columns added to force
2845 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002846 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002847 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002848# define VCOL_HLC (vcol - vcol_off)
2849#else
2850# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
2853 if (startrow > endrow) /* past the end already! */
2854 return startrow;
2855
2856 row = startrow;
2857 screen_row = row + W_WINROW(wp);
2858
2859 /*
2860 * To speed up the loop below, set extra_check when there is linebreak,
2861 * trailing white space and/or syntax processing to be done.
2862 */
2863#ifdef FEAT_LINEBREAK
2864 extra_check = wp->w_p_lbr;
2865#else
2866 extra_check = 0;
2867#endif
2868#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002869 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
2871 /* Prepare for syntax highlighting in this line. When there is an
2872 * error, stop syntax highlighting. */
2873 save_did_emsg = did_emsg;
2874 did_emsg = FALSE;
2875 syntax_start(wp, lnum);
2876 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002877 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 else
2879 {
2880 did_emsg = save_did_emsg;
2881 has_syntax = TRUE;
2882 extra_check = TRUE;
2883 }
2884 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002885
2886 /* Check for columns to display for 'colorcolumn'. */
2887 color_cols = wp->w_p_cc_cols;
2888 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002889 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002890#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002891
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002892#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002893 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002894 && *wp->w_s->b_p_spl != NUL
2895 && wp->w_s->b_langp.ga_len > 0
2896 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002897 {
2898 /* Prepare for spell checking. */
2899 has_spell = TRUE;
2900 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002901
2902 /* Get the start of the next line, so that words that wrap to the next
2903 * line are found too: "et<line-break>al.".
2904 * Trick: skip a few chars for C/shell/Vim comments */
2905 nextline[SPWORDLEN] = NUL;
2906 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2907 {
2908 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2909 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2910 }
2911
2912 /* When a word wrapped from the previous line the start of the current
2913 * line is valid. */
2914 if (lnum == checked_lnum)
2915 cur_checked_col = checked_col;
2916 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002917
2918 /* When there was a sentence end in the previous line may require a
2919 * word starting with capital in this line. In line 1 always check
2920 * the first word. */
2921 if (lnum != capcol_lnum)
2922 cap_col = -1;
2923 if (lnum == 1)
2924 cap_col = 0;
2925 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927#endif
2928
2929 /*
2930 * handle visual active in this window
2931 */
2932 fromcol = -10;
2933 tocol = MAXCOL;
2934#ifdef FEAT_VISUAL
2935 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2936 {
2937 /* Visual is after curwin->w_cursor */
2938 if (ltoreq(curwin->w_cursor, VIsual))
2939 {
2940 top = &curwin->w_cursor;
2941 bot = &VIsual;
2942 }
2943 else /* Visual is before curwin->w_cursor */
2944 {
2945 top = &VIsual;
2946 bot = &curwin->w_cursor;
2947 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002948 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 if (VIsual_mode == Ctrl_V) /* block mode */
2950 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002951 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
2953 fromcol = wp->w_old_cursor_fcol;
2954 tocol = wp->w_old_cursor_lcol;
2955 }
2956 }
2957 else /* non-block mode */
2958 {
2959 if (lnum > top->lnum && lnum <= bot->lnum)
2960 fromcol = 0;
2961 else if (lnum == top->lnum)
2962 {
2963 if (VIsual_mode == 'V') /* linewise */
2964 fromcol = 0;
2965 else
2966 {
2967 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2968 if (gchar_pos(top) == NUL)
2969 tocol = fromcol + 1;
2970 }
2971 }
2972 if (VIsual_mode != 'V' && lnum == bot->lnum)
2973 {
2974 if (*p_sel == 'e' && bot->col == 0
2975#ifdef FEAT_VIRTUALEDIT
2976 && bot->coladd == 0
2977#endif
2978 )
2979 {
2980 fromcol = -10;
2981 tocol = MAXCOL;
2982 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002983 else if (bot->col == MAXCOL)
2984 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 else
2986 {
2987 pos = *bot;
2988 if (*p_sel == 'e')
2989 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2990 else
2991 {
2992 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2993 ++tocol;
2994 }
2995 }
2996 }
2997 }
2998
2999#ifndef MSDOS
3000 /* Check if the character under the cursor should not be inverted */
3001 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3002# ifdef FEAT_GUI
3003 && !gui.in_use
3004# endif
3005 )
3006 noinvcur = TRUE;
3007#endif
3008
3009 /* if inverting in this line set area_highlighting */
3010 if (fromcol >= 0)
3011 {
3012 area_highlighting = TRUE;
3013 attr = hl_attr(HLF_V);
3014#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003015 if ((clip_star.available && !clip_star.owned
3016 && clip_isautosel_star())
3017 || (clip_plus.available && !clip_plus.owned
3018 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 attr = hl_attr(HLF_VNC);
3020#endif
3021 }
3022 }
3023
3024 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003025 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 */
3027 else
3028#endif /* FEAT_VISUAL */
3029 if (highlight_match
3030 && wp == curwin
3031 && lnum >= curwin->w_cursor.lnum
3032 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3033 {
3034 if (lnum == curwin->w_cursor.lnum)
3035 getvcol(curwin, &(curwin->w_cursor),
3036 (colnr_T *)&fromcol, NULL, NULL);
3037 else
3038 fromcol = 0;
3039 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3040 {
3041 pos.lnum = lnum;
3042 pos.col = search_match_endcol;
3043 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3044 }
3045 else
3046 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003047 /* do at least one character; happens when past end of line */
3048 if (fromcol == tocol)
3049 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 area_highlighting = TRUE;
3051 attr = hl_attr(HLF_I);
3052 }
3053
3054#ifdef FEAT_DIFF
3055 filler_lines = diff_check(wp, lnum);
3056 if (filler_lines < 0)
3057 {
3058 if (filler_lines == -1)
3059 {
3060 if (diff_find_change(wp, lnum, &change_start, &change_end))
3061 diff_hlf = HLF_ADD; /* added line */
3062 else if (change_start == 0)
3063 diff_hlf = HLF_TXD; /* changed text */
3064 else
3065 diff_hlf = HLF_CHD; /* changed line */
3066 }
3067 else
3068 diff_hlf = HLF_ADD; /* added line */
3069 filler_lines = 0;
3070 area_highlighting = TRUE;
3071 }
3072 if (lnum == wp->w_topline)
3073 filler_lines = wp->w_topfill;
3074 filler_todo = filler_lines;
3075#endif
3076
3077#ifdef LINE_ATTR
3078# ifdef FEAT_SIGNS
3079 /* If this line has a sign with line highlighting set line_attr. */
3080 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3081 if (v != 0)
3082 line_attr = sign_get_attr((int)v, TRUE);
3083# endif
3084# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3085 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003086 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 line_attr = hl_attr(HLF_L);
3088# endif
3089 if (line_attr != 0)
3090 area_highlighting = TRUE;
3091#endif
3092
3093 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3094 ptr = line;
3095
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003096#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003097 if (has_spell)
3098 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003099 /* For checking first word with a capital skip white space. */
3100 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003101 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003102
Bram Moolenaar30abd282005-06-22 22:35:10 +00003103 /* To be able to spell-check over line boundaries copy the end of the
3104 * current line into nextline[]. Above the start of the next line was
3105 * copied to nextline[SPWORDLEN]. */
3106 if (nextline[SPWORDLEN] == NUL)
3107 {
3108 /* No next line or it is empty. */
3109 nextlinecol = MAXCOL;
3110 nextline_idx = 0;
3111 }
3112 else
3113 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003114 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003115 if (v < SPWORDLEN)
3116 {
3117 /* Short line, use it completely and append the start of the
3118 * next line. */
3119 nextlinecol = 0;
3120 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003121 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003122 nextline_idx = v + 1;
3123 }
3124 else
3125 {
3126 /* Long line, use only the last SPWORDLEN bytes. */
3127 nextlinecol = v - SPWORDLEN;
3128 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3129 nextline_idx = SPWORDLEN + 1;
3130 }
3131 }
3132 }
3133#endif
3134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 /* find start of trailing whitespace */
3136 if (wp->w_p_list && lcs_trail)
3137 {
3138 trailcol = (colnr_T)STRLEN(ptr);
3139 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3140 --trailcol;
3141 trailcol += (colnr_T) (ptr - line);
3142 extra_check = TRUE;
3143 }
3144
3145 /*
3146 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3147 * first character to be displayed.
3148 */
3149 if (wp->w_p_wrap)
3150 v = wp->w_skipcol;
3151 else
3152 v = wp->w_leftcol;
3153 if (v > 0)
3154 {
3155#ifdef FEAT_MBYTE
3156 char_u *prev_ptr = ptr;
3157#endif
3158 while (vcol < v && *ptr != NUL)
3159 {
3160 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3161 vcol += c;
3162#ifdef FEAT_MBYTE
3163 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003165 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
3167
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003168#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3169 /* When:
3170 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003171 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003172 * - 'virtualedit' is set, or
3173 * - the visual mode is active,
3174 * the end of the line may be before the start of the displayed part.
3175 */
3176 if (vcol < v && (
3177# ifdef FEAT_SYN_HL
3178 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003179 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003180# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3181 ||
3182# endif
3183# endif
3184# ifdef FEAT_VIRTUALEDIT
3185 virtual_active()
3186# ifdef FEAT_VISUAL
3187 ||
3188# endif
3189# endif
3190# ifdef FEAT_VISUAL
3191 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3192# endif
3193 ))
3194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197#endif
3198
3199 /* Handle a character that's not completely on the screen: Put ptr at
3200 * that character but skip the first few screen characters. */
3201 if (vcol > v)
3202 {
3203 vcol -= c;
3204#ifdef FEAT_MBYTE
3205 ptr = prev_ptr;
3206#else
3207 --ptr;
3208#endif
3209 n_skip = v - vcol;
3210 }
3211
3212 /*
3213 * Adjust for when the inverted text is before the screen,
3214 * and when the start of the inverted text is before the screen.
3215 */
3216 if (tocol <= vcol)
3217 fromcol = 0;
3218 else if (fromcol >= 0 && fromcol < vcol)
3219 fromcol = vcol;
3220
3221#ifdef FEAT_LINEBREAK
3222 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3223 if (wp->w_p_wrap)
3224 need_showbreak = TRUE;
3225#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003226#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003227 /* When spell checking a word we need to figure out the start of the
3228 * word and if it's badly spelled or not. */
3229 if (has_spell)
3230 {
3231 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003232 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003233 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003234
3235 pos = wp->w_cursor;
3236 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003237 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003238 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003239
3240 /* spell_move_to() may call ml_get() and make "line" invalid */
3241 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3242 ptr = line + linecol;
3243
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003244 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003245 {
3246 /* no bad word found at line start, don't check until end of a
3247 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003248 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003249 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003250 }
3251 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003252 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003253 /* bad word found, use attributes until end of word */
3254 word_end = wp->w_cursor.col + len + 1;
3255
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003256 /* Turn index into actual attributes. */
3257 if (spell_hlf != HLF_COUNT)
3258 spell_attr = highlight_attr[spell_hlf];
3259 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003260 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003261
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003262# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003263 /* Need to restart syntax highlighting for this line. */
3264 if (has_syntax)
3265 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003266# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003267 }
3268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
3270
3271 /*
3272 * Correct highlighting for cursor that can't be disabled.
3273 * Avoids having to check this for each character.
3274 */
3275 if (fromcol >= 0)
3276 {
3277 if (noinvcur)
3278 {
3279 if ((colnr_T)fromcol == wp->w_virtcol)
3280 {
3281 /* highlighting starts at cursor, let it start just after the
3282 * cursor */
3283 fromcol_prev = fromcol;
3284 fromcol = -1;
3285 }
3286 else if ((colnr_T)fromcol < wp->w_virtcol)
3287 /* restart highlighting after the cursor */
3288 fromcol_prev = wp->w_virtcol;
3289 }
3290 if (fromcol >= tocol)
3291 fromcol = -1;
3292 }
3293
3294#ifdef FEAT_SEARCH_EXTRA
3295 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003296 * Handle highlighting the last used search pattern and matches.
3297 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003299 cur = wp->w_match_head;
3300 shl_flag = FALSE;
3301 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003303 if (shl_flag == FALSE)
3304 {
3305 shl = &search_hl;
3306 shl_flag = TRUE;
3307 }
3308 else
3309 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003310 shl->startcol = MAXCOL;
3311 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 shl->attr_cur = 0;
3313 if (shl->rm.regprog != NULL)
3314 {
3315 v = (long)(ptr - line);
3316 next_search_hl(wp, shl, lnum, (colnr_T)v);
3317
3318 /* Need to get the line again, a multi-line regexp may have made it
3319 * invalid. */
3320 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3321 ptr = line + v;
3322
3323 if (shl->lnum != 0 && shl->lnum <= lnum)
3324 {
3325 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003326 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003328 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3330 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003331 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003333 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003335 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
3337#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003338 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003339 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 else
3341#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003342 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003344 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
3346 shl->attr_cur = shl->attr;
3347 search_attr = shl->attr;
3348 }
3349 area_highlighting = TRUE;
3350 }
3351 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003352 if (shl != &search_hl && cur != NULL)
3353 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 }
3355#endif
3356
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003357#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003358 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3359 * active, because it's not clear what is selected then. */
3360 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003361 {
3362 line_attr = hl_attr(HLF_CUL);
3363 area_highlighting = TRUE;
3364 }
3365#endif
3366
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003367 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 col = 0;
3369#ifdef FEAT_RIGHTLEFT
3370 if (wp->w_p_rl)
3371 {
3372 /* Rightleft window: process the text in the normal direction, but put
3373 * it in current_ScreenLine[] from right to left. Start at the
3374 * rightmost column of the window. */
3375 col = W_WIDTH(wp) - 1;
3376 off += col;
3377 }
3378#endif
3379
3380 /*
3381 * Repeat for the whole displayed line.
3382 */
3383 for (;;)
3384 {
3385 /* Skip this quickly when working on the text. */
3386 if (draw_state != WL_LINE)
3387 {
3388#ifdef FEAT_CMDWIN
3389 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3390 {
3391 draw_state = WL_CMDLINE;
3392 if (cmdwin_type != 0 && wp == curwin)
3393 {
3394 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003396 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 char_attr = hl_attr(HLF_AT);
3398 }
3399 }
3400#endif
3401
3402#ifdef FEAT_FOLDING
3403 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3404 {
3405 draw_state = WL_FOLD;
3406 if (wp->w_p_fdc > 0)
3407 {
3408 /* Draw the 'foldcolumn'. */
3409 fill_foldcolumn(extra, wp, FALSE, lnum);
3410 n_extra = wp->w_p_fdc;
3411 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003412 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 c_extra = NUL;
3414 char_attr = hl_attr(HLF_FC);
3415 }
3416 }
3417#endif
3418
3419#ifdef FEAT_SIGNS
3420 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3421 {
3422 draw_state = WL_SIGN;
3423 /* Show the sign column when there are any signs in this
3424 * buffer or when using Netbeans. */
3425 if (draw_signcolumn(wp)
3426# ifdef FEAT_DIFF
3427 && filler_todo <= 0
3428# endif
3429 )
3430 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003431 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003433 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434# endif
3435
3436 /* Draw two cells with the sign value or blank. */
3437 c_extra = ' ';
3438 char_attr = hl_attr(HLF_SC);
3439 n_extra = 2;
3440
3441 if (row == startrow)
3442 {
3443 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3444 SIGN_TEXT);
3445# ifdef FEAT_SIGN_ICONS
3446 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3447 SIGN_ICON);
3448 if (gui.in_use && icon_sign != 0)
3449 {
3450 /* Use the image in this position. */
3451 c_extra = SIGN_BYTE;
3452# ifdef FEAT_NETBEANS_INTG
3453 if (buf_signcount(wp->w_buffer, lnum) > 1)
3454 c_extra = MULTISIGN_BYTE;
3455# endif
3456 char_attr = icon_sign;
3457 }
3458 else
3459# endif
3460 if (text_sign != 0)
3461 {
3462 p_extra = sign_get_text(text_sign);
3463 if (p_extra != NULL)
3464 {
3465 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003466 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
3468 char_attr = sign_get_attr(text_sign, FALSE);
3469 }
3470 }
3471 }
3472 }
3473#endif
3474
3475 if (draw_state == WL_NR - 1 && n_extra == 0)
3476 {
3477 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003478 /* Display the absolute or relative line number. After the
3479 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3480 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 && (row == startrow
3482#ifdef FEAT_DIFF
3483 + filler_lines
3484#endif
3485 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3486 {
3487 /* Draw the line number (empty space after wrapping). */
3488 if (row == startrow
3489#ifdef FEAT_DIFF
3490 + filler_lines
3491#endif
3492 )
3493 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003494 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003495 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003496
3497 if (wp->w_p_nu)
3498 /* 'number' */
3499 num = (long)lnum;
3500 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003501 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003502 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003503 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar700e7342013-01-30 12:31:36 +01003504 if (num == 0)
3505 {
3506 num = lnum;
3507 fmt = "%-*ld ";
3508 }
3509 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003510
Bram Moolenaar700e7342013-01-30 12:31:36 +01003511 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003512 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 if (wp->w_skipcol > 0)
3514 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3515 *p_extra = '-';
3516#ifdef FEAT_RIGHTLEFT
3517 if (wp->w_p_rl) /* reverse line numbers */
3518 rl_mirror(extra);
3519#endif
3520 p_extra = extra;
3521 c_extra = NUL;
3522 }
3523 else
3524 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003525 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003527#ifdef FEAT_SYN_HL
3528 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003529 * the current line differently.
3530 * TODO: Can we use CursorLine instead of CursorLineNr
3531 * when CursorLineNr isn't set? */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003532 if ((wp->w_p_cul || wp->w_p_rnu)
3533 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003534 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537 }
3538
3539#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3540 if (draw_state == WL_SBR - 1 && n_extra == 0)
3541 {
3542 draw_state = WL_SBR;
3543# ifdef FEAT_DIFF
3544 if (filler_todo > 0)
3545 {
3546 /* Draw "deleted" diff line(s). */
3547 if (char2cells(fill_diff) > 1)
3548 c_extra = '-';
3549 else
3550 c_extra = fill_diff;
3551# ifdef FEAT_RIGHTLEFT
3552 if (wp->w_p_rl)
3553 n_extra = col + 1;
3554 else
3555# endif
3556 n_extra = W_WIDTH(wp) - col;
3557 char_attr = hl_attr(HLF_DED);
3558 }
3559# endif
3560# ifdef FEAT_LINEBREAK
3561 if (*p_sbr != NUL && need_showbreak)
3562 {
3563 /* Draw 'showbreak' at the start of each broken line. */
3564 p_extra = p_sbr;
3565 c_extra = NUL;
3566 n_extra = (int)STRLEN(p_sbr);
3567 char_attr = hl_attr(HLF_AT);
3568 need_showbreak = FALSE;
3569 /* Correct end of highlighted area for 'showbreak',
3570 * required when 'linebreak' is also set. */
3571 if (tocol == vcol)
3572 tocol += n_extra;
3573 }
3574# endif
3575 }
3576#endif
3577
3578 if (draw_state == WL_LINE - 1 && n_extra == 0)
3579 {
3580 draw_state = WL_LINE;
3581 if (saved_n_extra)
3582 {
3583 /* Continue item from end of wrapped line. */
3584 n_extra = saved_n_extra;
3585 c_extra = saved_c_extra;
3586 p_extra = saved_p_extra;
3587 char_attr = saved_char_attr;
3588 }
3589 else
3590 char_attr = 0;
3591 }
3592 }
3593
3594 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003595 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003596 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597#ifdef FEAT_DIFF
3598 && filler_todo <= 0
3599#endif
3600 )
3601 {
3602 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3603 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003604 /* Pretend we have finished updating the window. Except when
3605 * 'cursorcolumn' is set. */
3606#ifdef FEAT_SYN_HL
3607 if (wp->w_p_cuc)
3608 row = wp->w_cline_row + wp->w_cline_height;
3609 else
3610#endif
3611 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 break;
3613 }
3614
3615 if (draw_state == WL_LINE && area_highlighting)
3616 {
3617 /* handle Visual or match highlighting in this line */
3618 if (vcol == fromcol
3619#ifdef FEAT_MBYTE
3620 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3621 && (*mb_ptr2cells)(ptr) > 1)
3622#endif
3623 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003624 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 && vcol < tocol))
3626 area_attr = attr; /* start highlighting */
3627 else if (area_attr != 0
3628 && (vcol == tocol
3629 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632#ifdef FEAT_SEARCH_EXTRA
3633 if (!n_extra)
3634 {
3635 /*
3636 * Check for start/end of search pattern match.
3637 * After end, check for start/end of next match.
3638 * When another match, have to check for start again.
3639 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003640 * Do this for 'search_hl' and the match list (ordered by
3641 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003643 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003644 cur = wp->w_match_head;
3645 shl_flag = FALSE;
3646 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003648 if (shl_flag == FALSE
3649 && ((cur != NULL
3650 && cur->priority > SEARCH_HL_PRIORITY)
3651 || cur == NULL))
3652 {
3653 shl = &search_hl;
3654 shl_flag = TRUE;
3655 }
3656 else
3657 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 while (shl->rm.regprog != NULL)
3659 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003660 if (shl->startcol != MAXCOL
3661 && v >= (long)shl->startcol
3662 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 shl->attr_cur = shl->attr;
3665 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003666 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
3668 shl->attr_cur = 0;
3669
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 next_search_hl(wp, shl, lnum, (colnr_T)v);
3671
3672 /* Need to get the line again, a multi-line regexp
3673 * may have made it invalid. */
3674 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3675 ptr = line + v;
3676
3677 if (shl->lnum == lnum)
3678 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003679 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003681 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003683 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003685 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
3687 /* highlight empty match, try again after
3688 * it */
3689#ifdef FEAT_MBYTE
3690 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003691 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003692 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 else
3694#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003695 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697
3698 /* Loop to check if the match starts at the
3699 * current position */
3700 continue;
3701 }
3702 }
3703 break;
3704 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003705 if (shl != &search_hl && cur != NULL)
3706 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003708
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003709 /* Use attributes from match with highest priority among
3710 * 'search_hl' and the match list. */
3711 search_attr = search_hl.attr_cur;
3712 cur = wp->w_match_head;
3713 shl_flag = FALSE;
3714 while (cur != NULL || shl_flag == FALSE)
3715 {
3716 if (shl_flag == FALSE
3717 && ((cur != NULL
3718 && cur->priority > SEARCH_HL_PRIORITY)
3719 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003720 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003721 shl = &search_hl;
3722 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003723 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003724 else
3725 shl = &cur->hl;
3726 if (shl->attr_cur != 0)
3727 search_attr = shl->attr_cur;
3728 if (shl != &search_hl && cur != NULL)
3729 cur = cur->next;
3730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732#endif
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003735 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003737 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3738 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003740 if (diff_hlf == HLF_TXD && ptr - line > change_end
3741 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003743 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003746
3747 /* Decide which of the highlight attributes to use. */
3748 attr_pri = TRUE;
3749 if (area_attr != 0)
3750 char_attr = area_attr;
3751 else if (search_attr != 0)
3752 char_attr = search_attr;
3753#ifdef LINE_ATTR
3754 /* Use line_attr when not in the Visual or 'incsearch' area
3755 * (area_attr may be 0 when "noinvcur" is set). */
3756 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003757 || vcol < fromcol || vcol_prev < fromcol_prev
3758 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003759 char_attr = line_attr;
3760#endif
3761 else
3762 {
3763 attr_pri = FALSE;
3764#ifdef FEAT_SYN_HL
3765 if (has_syntax)
3766 char_attr = syntax_attr;
3767 else
3768#endif
3769 char_attr = 0;
3770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772
3773 /*
3774 * Get the next character to put on the screen.
3775 */
3776 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003777 * The "p_extra" points to the extra stuff that is inserted to
3778 * represent special characters (non-printable stuff) and other
3779 * things. When all characters are the same, c_extra is used.
3780 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3781 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3783 */
3784 if (n_extra > 0)
3785 {
3786 if (c_extra != NUL)
3787 {
3788 c = c_extra;
3789#ifdef FEAT_MBYTE
3790 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3791 if (enc_utf8 && (*mb_char2len)(c) > 1)
3792 {
3793 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003794 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003795 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797 else
3798 mb_utf8 = FALSE;
3799#endif
3800 }
3801 else
3802 {
3803 c = *p_extra;
3804#ifdef FEAT_MBYTE
3805 if (has_mbyte)
3806 {
3807 mb_c = c;
3808 if (enc_utf8)
3809 {
3810 /* If the UTF-8 character is more than one byte:
3811 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003812 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 mb_utf8 = FALSE;
3814 if (mb_l > n_extra)
3815 mb_l = 1;
3816 else if (mb_l > 1)
3817 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003818 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003820 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 }
3823 else
3824 {
3825 /* if this is a DBCS character, put it in "mb_c" */
3826 mb_l = MB_BYTE2LEN(c);
3827 if (mb_l >= n_extra)
3828 mb_l = 1;
3829 else if (mb_l > 1)
3830 mb_c = (c << 8) + p_extra[1];
3831 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003832 if (mb_l == 0) /* at the NUL at end-of-line */
3833 mb_l = 1;
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 /* If a double-width char doesn't fit display a '>' in the
3836 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003837 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838# ifdef FEAT_RIGHTLEFT
3839 wp->w_p_rl ? (col <= 0) :
3840# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003841 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 && (*mb_char2cells)(mb_c) == 2)
3843 {
3844 c = '>';
3845 mb_c = c;
3846 mb_l = 1;
3847 mb_utf8 = FALSE;
3848 multi_attr = hl_attr(HLF_AT);
3849 /* put the pointer back to output the double-width
3850 * character at the start of the next line. */
3851 ++n_extra;
3852 --p_extra;
3853 }
3854 else
3855 {
3856 n_extra -= mb_l - 1;
3857 p_extra += mb_l - 1;
3858 }
3859 }
3860#endif
3861 ++p_extra;
3862 }
3863 --n_extra;
3864 }
3865 else
3866 {
3867 /*
3868 * Get a character from the line itself.
3869 */
3870 c = *ptr;
3871#ifdef FEAT_MBYTE
3872 if (has_mbyte)
3873 {
3874 mb_c = c;
3875 if (enc_utf8)
3876 {
3877 /* If the UTF-8 character is more than one byte: Decode it
3878 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003879 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 mb_utf8 = FALSE;
3881 if (mb_l > 1)
3882 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003883 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 /* Overlong encoded ASCII or ASCII with composing char
3885 * is displayed normally, except a NUL. */
3886 if (mb_c < 0x80)
3887 c = mb_c;
3888 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003889
3890 /* At start of the line we can have a composing char.
3891 * Draw it as a space with a composing char. */
3892 if (utf_iscomposing(mb_c))
3893 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003894 int i;
3895
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003896 for (i = Screen_mco - 1; i > 0; --i)
3897 u8cc[i] = u8cc[i - 1];
3898 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003899 mb_c = ' ';
3900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902
3903 if ((mb_l == 1 && c >= 0x80)
3904 || (mb_l >= 1 && mb_c == 0)
3905 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003906# ifdef UNICODE16
3907 || mb_c >= 0x10000
3908# endif
3909 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 {
3911 /*
3912 * Illegal UTF-8 byte: display as <xx>.
3913 * Non-BMP character : display as ? or fullwidth ?.
3914 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003915# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
3919 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003920# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 if (wp->w_p_rl) /* reverse */
3922 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003925# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 else if (utf_char2cells(mb_c) != 2)
3927 STRCPY(extra, "?");
3928 else
3929 /* 0xff1f in UTF-8: full-width '?' */
3930 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
3933 p_extra = extra;
3934 c = *p_extra;
3935 mb_c = mb_ptr2char_adv(&p_extra);
3936 mb_utf8 = (c >= 0x80);
3937 n_extra = (int)STRLEN(p_extra);
3938 c_extra = NUL;
3939 if (area_attr == 0 && search_attr == 0)
3940 {
3941 n_attr = n_extra + 1;
3942 extra_attr = hl_attr(HLF_8);
3943 saved_attr2 = char_attr; /* save current attr */
3944 }
3945 }
3946 else if (mb_l == 0) /* at the NUL at end-of-line */
3947 mb_l = 1;
3948#ifdef FEAT_ARABIC
3949 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3950 {
3951 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003952 int pc, pc1, nc;
3953 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
3955 /* The idea of what is the previous and next
3956 * character depends on 'rightleft'. */
3957 if (wp->w_p_rl)
3958 {
3959 pc = prev_c;
3960 pc1 = prev_c1;
3961 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003962 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 else
3965 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003966 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003968 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 prev_c = mb_c;
3971
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003972 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974 else
3975 prev_c = mb_c;
3976#endif
3977 }
3978 else /* enc_dbcs */
3979 {
3980 mb_l = MB_BYTE2LEN(c);
3981 if (mb_l == 0) /* at the NUL at end-of-line */
3982 mb_l = 1;
3983 else if (mb_l > 1)
3984 {
3985 /* We assume a second byte below 32 is illegal.
3986 * Hopefully this is OK for all double-byte encodings!
3987 */
3988 if (ptr[1] >= 32)
3989 mb_c = (c << 8) + ptr[1];
3990 else
3991 {
3992 if (ptr[1] == NUL)
3993 {
3994 /* head byte at end of line */
3995 mb_l = 1;
3996 transchar_nonprint(extra, c);
3997 }
3998 else
3999 {
4000 /* illegal tail byte */
4001 mb_l = 2;
4002 STRCPY(extra, "XX");
4003 }
4004 p_extra = extra;
4005 n_extra = (int)STRLEN(extra) - 1;
4006 c_extra = NUL;
4007 c = *p_extra++;
4008 if (area_attr == 0 && search_attr == 0)
4009 {
4010 n_attr = n_extra + 1;
4011 extra_attr = hl_attr(HLF_8);
4012 saved_attr2 = char_attr; /* save current attr */
4013 }
4014 mb_c = c;
4015 }
4016 }
4017 }
4018 /* If a double-width char doesn't fit display a '>' in the
4019 * last column; the character is displayed at the start of the
4020 * next line. */
4021 if ((
4022# ifdef FEAT_RIGHTLEFT
4023 wp->w_p_rl ? (col <= 0) :
4024# endif
4025 (col >= W_WIDTH(wp) - 1))
4026 && (*mb_char2cells)(mb_c) == 2)
4027 {
4028 c = '>';
4029 mb_c = c;
4030 mb_utf8 = FALSE;
4031 mb_l = 1;
4032 multi_attr = hl_attr(HLF_AT);
4033 /* Put pointer back so that the character will be
4034 * displayed at the start of the next line. */
4035 --ptr;
4036 }
4037 else if (*ptr != NUL)
4038 ptr += mb_l - 1;
4039
4040 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004041 * a '<' in the first column. Don't do this for unprintable
4042 * charactes. */
4043 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004046 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 c = ' ';
4048 if (area_attr == 0 && search_attr == 0)
4049 {
4050 n_attr = n_extra + 1;
4051 extra_attr = hl_attr(HLF_AT);
4052 saved_attr2 = char_attr; /* save current attr */
4053 }
4054 mb_c = c;
4055 mb_utf8 = FALSE;
4056 mb_l = 1;
4057 }
4058
4059 }
4060#endif
4061 ++ptr;
4062
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004063 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004064 if (wp->w_p_list && (c == 160
4065#ifdef FEAT_MBYTE
4066 || (mb_utf8 && mb_c == 160)
4067#endif
4068 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004069 {
4070 c = lcs_nbsp;
4071 if (area_attr == 0 && search_attr == 0)
4072 {
4073 n_attr = 1;
4074 extra_attr = hl_attr(HLF_8);
4075 saved_attr2 = char_attr; /* save current attr */
4076 }
4077#ifdef FEAT_MBYTE
4078 mb_c = c;
4079 if (enc_utf8 && (*mb_char2len)(c) > 1)
4080 {
4081 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004082 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004083 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004084 }
4085 else
4086 mb_utf8 = FALSE;
4087#endif
4088 }
4089
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 if (extra_check)
4091 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004092#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004093 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004094#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004095
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004096#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 /* Get syntax attribute, unless still at the start of the line
4098 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004099 v = (long)(ptr - line);
4100 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
4102 /* Get the syntax attribute for the character. If there
4103 * is an error, disable syntax highlighting. */
4104 save_did_emsg = did_emsg;
4105 did_emsg = FALSE;
4106
Bram Moolenaar217ad922005-03-20 22:37:15 +00004107 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004108# ifdef FEAT_SPELL
4109 has_spell ? &can_spell :
4110# endif
4111 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004114 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004115 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004116 has_syntax = FALSE;
4117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 else
4119 did_emsg = save_did_emsg;
4120
4121 /* Need to get the line again, a multi-line regexp may
4122 * have made it invalid. */
4123 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4124 ptr = line + v;
4125
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004126 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004128 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004129 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004130# ifdef FEAT_CONCEAL
4131 /* no concealing past the end of the line, it interferes
4132 * with line highlighting */
4133 if (c == NUL)
4134 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004135 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004136 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004137# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004139#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004140
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004141#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004142 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004143 * Only do this when there is no syntax highlighting, the
4144 * @Spell cluster is not used or the current syntax item
4145 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004146 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004147 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004148 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004149# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004150 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004151 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004152# endif
4153 if (c != 0 && (
4154# ifdef FEAT_SYN_HL
4155 !has_syntax ||
4156# endif
4157 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004158 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004159 char_u *prev_ptr, *p;
4160 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004161 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004162# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004163 if (has_mbyte)
4164 {
4165 prev_ptr = ptr - mb_l;
4166 v -= mb_l - 1;
4167 }
4168 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004169# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004170 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004171
4172 /* Use nextline[] if possible, it has the start of the
4173 * next line concatenated. */
4174 if ((prev_ptr - line) - nextlinecol >= 0)
4175 p = nextline + (prev_ptr - line) - nextlinecol;
4176 else
4177 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004178 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004179 len = spell_check(wp, p, &spell_hlf, &cap_col,
4180 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004181 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004182
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004183 /* In Insert mode only highlight a word that
4184 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004185 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004186 && (State & INSERT) != 0
4187 && wp->w_cursor.lnum == lnum
4188 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004189 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004190 && wp->w_cursor.col < (colnr_T)word_end)
4191 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004192 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004193 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004194 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004195
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004196 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004197 && (p - nextline) + len > nextline_idx)
4198 {
4199 /* Remember that the good word continues at the
4200 * start of the next line. */
4201 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004202 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004203 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004204
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004205 /* Turn index into actual attributes. */
4206 if (spell_hlf != HLF_COUNT)
4207 spell_attr = highlight_attr[spell_hlf];
4208
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004209 if (cap_col > 0)
4210 {
4211 if (p != prev_ptr
4212 && (p - nextline) + cap_col >= nextline_idx)
4213 {
4214 /* Remember that the word in the next line
4215 * must start with a capital. */
4216 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004217 cap_col = (int)((p - nextline) + cap_col
4218 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004219 }
4220 else
4221 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004222 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004223 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004224 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004225 }
4226 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004227 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004228 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004229 char_attr = hl_combine_attr(char_attr, spell_attr);
4230 else
4231 char_attr = hl_combine_attr(spell_attr, char_attr);
4232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#endif
4234#ifdef FEAT_LINEBREAK
4235 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004236 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 */
4238 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004239 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
4241 n_extra = win_lbr_chartabsize(wp, ptr - (
4242# ifdef FEAT_MBYTE
4243 has_mbyte ? mb_l :
4244# endif
4245 1), (colnr_T)vcol, NULL) - 1;
4246 c_extra = ' ';
4247 if (vim_iswhite(c))
4248 c = ' ';
4249 }
4250#endif
4251
4252 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4253 {
4254 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004255 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
4257 n_attr = 1;
4258 extra_attr = hl_attr(HLF_8);
4259 saved_attr2 = char_attr; /* save current attr */
4260 }
4261#ifdef FEAT_MBYTE
4262 mb_c = c;
4263 if (enc_utf8 && (*mb_char2len)(c) > 1)
4264 {
4265 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004266 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004267 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 else
4270 mb_utf8 = FALSE;
4271#endif
4272 }
4273 }
4274
4275 /*
4276 * Handling of non-printable characters.
4277 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004278 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
4280 /*
4281 * when getting a character from the file, we may have to
4282 * turn it into something else on the way to putting it
4283 * into "ScreenLines".
4284 */
4285 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4286 {
4287 /* tab amount depends on current column */
4288 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004289 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4290#ifdef FEAT_CONCEAL
4291 /* Tab alignment should be identical regardless of
4292 * 'conceallevel' value. So tab compensates of all
4293 * previous concealed characters, and thus resets vcol_off
4294 * and boguscols accumulated so far in the line. Note that
4295 * the tab can be longer than 'tabstop' when there
4296 * are concealed characters. */
4297 n_extra += vcol_off;
4298 vcol -= vcol_off;
4299 vcol_off = 0;
4300 col -= boguscols;
4301 boguscols = 0;
4302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#ifdef FEAT_MBYTE
4304 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4305#endif
4306 if (wp->w_p_list)
4307 {
4308 c = lcs_tab1;
4309 c_extra = lcs_tab2;
4310 n_attr = n_extra + 1;
4311 extra_attr = hl_attr(HLF_8);
4312 saved_attr2 = char_attr; /* save current attr */
4313#ifdef FEAT_MBYTE
4314 mb_c = c;
4315 if (enc_utf8 && (*mb_char2len)(c) > 1)
4316 {
4317 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004318 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004319 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321#endif
4322 }
4323 else
4324 {
4325 c_extra = ' ';
4326 c = ' ';
4327 }
4328 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004329 else if (c == NUL
4330 && ((wp->w_p_list && lcs_eol > 0)
4331 || ((fromcol >= 0 || fromcol_prev >= 0)
4332 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004333#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004334 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004335#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004336 && (
4337# ifdef FEAT_RIGHTLEFT
4338 wp->w_p_rl ? (col >= 0) :
4339# endif
4340 (col < W_WIDTH(wp)))
4341 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004342 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004343 && (colnr_T)vcol == wp->w_virtcol)))
4344 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004346 /* Display a '$' after the line or highlight an extra
4347 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4349 /* For a diff line the highlighting continues after the
4350 * "$". */
4351 if (
4352# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004353 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354# ifdef LINE_ATTR
4355 &&
4356# endif
4357# endif
4358# ifdef LINE_ATTR
4359 line_attr == 0
4360# endif
4361 )
4362#endif
4363 {
4364#ifdef FEAT_VIRTUALEDIT
4365 /* In virtualedit, visual selections may extend
4366 * beyond end of line. */
4367 if (area_highlighting && virtual_active()
4368 && tocol != MAXCOL && vcol < tocol)
4369 n_extra = 0;
4370 else
4371#endif
4372 {
4373 p_extra = at_end_str;
4374 n_extra = 1;
4375 c_extra = NUL;
4376 }
4377 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004378 if (wp->w_p_list)
4379 c = lcs_eol;
4380 else
4381 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 lcs_eol_one = -1;
4383 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004384 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 extra_attr = hl_attr(HLF_AT);
4387 n_attr = 1;
4388 }
4389#ifdef FEAT_MBYTE
4390 mb_c = c;
4391 if (enc_utf8 && (*mb_char2len)(c) > 1)
4392 {
4393 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004394 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004395 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397 else
4398 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4399#endif
4400 }
4401 else if (c != NUL)
4402 {
4403 p_extra = transchar(c);
4404#ifdef FEAT_RIGHTLEFT
4405 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4406 rl_mirror(p_extra); /* reverse "<12>" */
4407#endif
4408 n_extra = byte2cells(c) - 1;
4409 c_extra = NUL;
4410 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004411 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
4413 n_attr = n_extra + 1;
4414 extra_attr = hl_attr(HLF_8);
4415 saved_attr2 = char_attr; /* save current attr */
4416 }
4417#ifdef FEAT_MBYTE
4418 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4419#endif
4420 }
4421#ifdef FEAT_VIRTUALEDIT
4422 else if (VIsual_active
4423 && (VIsual_mode == Ctrl_V
4424 || VIsual_mode == 'v')
4425 && virtual_active()
4426 && tocol != MAXCOL
4427 && vcol < tocol
4428 && (
4429# ifdef FEAT_RIGHTLEFT
4430 wp->w_p_rl ? (col >= 0) :
4431# endif
4432 (col < W_WIDTH(wp))))
4433 {
4434 c = ' ';
4435 --ptr; /* put it back at the NUL */
4436 }
4437#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004438#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 else if ((
4440# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004441 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 ) && (
4445# ifdef FEAT_RIGHTLEFT
4446 wp->w_p_rl ? (col >= 0) :
4447# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004448 (col
4449# ifdef FEAT_CONCEAL
4450 - boguscols
4451# endif
4452 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
4454 /* Highlight until the right side of the window */
4455 c = ' ';
4456 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004457
4458 /* Remember we do the char for line highlighting. */
4459 ++did_line_attr;
4460
4461 /* don't do search HL for the rest of the line */
4462 if (line_attr != 0 && char_attr == search_attr && col > 0)
4463 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464# ifdef FEAT_DIFF
4465 if (diff_hlf == HLF_TXD)
4466 {
4467 diff_hlf = HLF_CHD;
4468 if (attr == 0 || char_attr != attr)
4469 char_attr = hl_attr(diff_hlf);
4470 }
4471# endif
4472 }
4473#endif
4474 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004475
4476#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004477 if ( wp->w_p_cole > 0
4478 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4479 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004480 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004481 && !(lnum_in_visual_area
4482 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004483 {
4484 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004485 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004486 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4487 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004488 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004489 /* First time at this concealed item: display one
4490 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004491 if (syn_get_sub_char() != NUL)
4492 c = syn_get_sub_char();
4493 else if (lcs_conceal != NUL)
4494 c = lcs_conceal;
4495 else
4496 c = ' ';
4497
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004498 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004499
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004500 if (n_extra > 0)
4501 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004502 vcol += n_extra;
4503 if (wp->w_p_wrap && n_extra > 0)
4504 {
4505# ifdef FEAT_RIGHTLEFT
4506 if (wp->w_p_rl)
4507 {
4508 col -= n_extra;
4509 boguscols -= n_extra;
4510 }
4511 else
4512# endif
4513 {
4514 boguscols += n_extra;
4515 col += n_extra;
4516 }
4517 }
4518 n_extra = 0;
4519 n_attr = 0;
4520 }
4521 else if (n_skip == 0)
4522 {
4523 is_concealing = TRUE;
4524 n_skip = 1;
4525 }
4526# ifdef FEAT_MBYTE
4527 mb_c = c;
4528 if (enc_utf8 && (*mb_char2len)(c) > 1)
4529 {
4530 mb_utf8 = TRUE;
4531 u8cc[0] = 0;
4532 c = 0xc0;
4533 }
4534 else
4535 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4536# endif
4537 }
4538 else
4539 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004540 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004541 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004542 }
4543#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004546#ifdef FEAT_CONCEAL
4547 /* In the cursor line and we may be concealing characters: correct
4548 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004549 if (!did_wcol && draw_state == WL_LINE
4550 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004551 && conceal_cursor_line(wp)
4552 && (int)wp->w_virtcol <= vcol + n_skip)
4553 {
4554 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004555 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004556 did_wcol = TRUE;
4557 }
4558#endif
4559
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 /* Don't override visual selection highlighting. */
4561 if (n_attr > 0
4562 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004563 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 char_attr = extra_attr;
4565
Bram Moolenaar81695252004-12-29 20:58:21 +00004566#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 /* XIM don't send preedit_start and preedit_end, but they send
4568 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4569 * im_is_preediting() here. */
4570 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004571 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 && (State & INSERT)
4573 && !p_imdisable
4574 && im_is_preediting()
4575 && draw_state == WL_LINE)
4576 {
4577 colnr_T tcol;
4578
4579 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004580 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 else
4582 tcol = preedit_end_col;
4583 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4584 {
4585 if (feedback_old_attr < 0)
4586 {
4587 feedback_col = 0;
4588 feedback_old_attr = char_attr;
4589 }
4590 char_attr = im_get_feedback_attr(feedback_col);
4591 if (char_attr < 0)
4592 char_attr = feedback_old_attr;
4593 feedback_col++;
4594 }
4595 else if (feedback_old_attr >= 0)
4596 {
4597 char_attr = feedback_old_attr;
4598 feedback_old_attr = -1;
4599 feedback_col = 0;
4600 }
4601 }
4602#endif
4603 /*
4604 * Handle the case where we are in column 0 but not on the first
4605 * character of the line and the user wants us to show us a
4606 * special character (via 'listchars' option "precedes:<char>".
4607 */
4608 if (lcs_prec_todo != NUL
4609 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4610#ifdef FEAT_DIFF
4611 && filler_todo <= 0
4612#endif
4613 && draw_state > WL_NR
4614 && c != NUL)
4615 {
4616 c = lcs_prec;
4617 lcs_prec_todo = NUL;
4618#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004619 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4620 {
4621 /* Double-width character being overwritten by the "precedes"
4622 * character, need to fill up half the character. */
4623 c_extra = MB_FILLER_CHAR;
4624 n_extra = 1;
4625 n_attr = 2;
4626 extra_attr = hl_attr(HLF_AT);
4627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 mb_c = c;
4629 if (enc_utf8 && (*mb_char2len)(c) > 1)
4630 {
4631 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004632 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004633 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635 else
4636 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4637#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004638 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
4640 saved_attr3 = char_attr; /* save current attr */
4641 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4642 n_attr3 = 1;
4643 }
4644 }
4645
4646 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004647 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004649 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004650#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004651 || did_line_attr == 1
4652#endif
4653 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004655#ifdef FEAT_SEARCH_EXTRA
4656 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004657
4658 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004659 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004660 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004661#endif
4662
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004663 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 * highlight match at end of line. If it's beyond the last
4665 * char on the screen, just overwrite that one (tricky!) Not
4666 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004667#ifdef FEAT_SEARCH_EXTRA
4668 prevcol_hl_flag = FALSE;
4669 if (prevcol == (long)search_hl.startcol)
4670 prevcol_hl_flag = TRUE;
4671 else
4672 {
4673 cur = wp->w_match_head;
4674 while (cur != NULL)
4675 {
4676 if (prevcol == (long)cur->hl.startcol)
4677 {
4678 prevcol_hl_flag = TRUE;
4679 break;
4680 }
4681 cur = cur->next;
4682 }
4683 }
4684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004686 && ((area_attr != 0 && vcol == fromcol
4687#ifdef FEAT_VISUAL
4688 && (VIsual_mode != Ctrl_V
4689 || lnum == VIsual.lnum
4690 || lnum == curwin->w_cursor.lnum)
4691#endif
4692 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#ifdef FEAT_SEARCH_EXTRA
4694 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004695 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004696# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004697 && did_line_attr <= 1
4698# endif
4699 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#endif
4701 ))
4702 {
4703 int n = 0;
4704
4705#ifdef FEAT_RIGHTLEFT
4706 if (wp->w_p_rl)
4707 {
4708 if (col < 0)
4709 n = 1;
4710 }
4711 else
4712#endif
4713 {
4714 if (col >= W_WIDTH(wp))
4715 n = -1;
4716 }
4717 if (n != 0)
4718 {
4719 /* At the window boundary, highlight the last character
4720 * instead (better than nothing). */
4721 off += n;
4722 col += n;
4723 }
4724 else
4725 {
4726 /* Add a blank character to highlight. */
4727 ScreenLines[off] = ' ';
4728#ifdef FEAT_MBYTE
4729 if (enc_utf8)
4730 ScreenLinesUC[off] = 0;
4731#endif
4732 }
4733#ifdef FEAT_SEARCH_EXTRA
4734 if (area_attr == 0)
4735 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004736 /* Use attributes from match with highest priority among
4737 * 'search_hl' and the match list. */
4738 char_attr = search_hl.attr;
4739 cur = wp->w_match_head;
4740 shl_flag = FALSE;
4741 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004742 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004743 if (shl_flag == FALSE
4744 && ((cur != NULL
4745 && cur->priority > SEARCH_HL_PRIORITY)
4746 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004747 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004748 shl = &search_hl;
4749 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004750 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004751 else
4752 shl = &cur->hl;
4753 if ((ptr - line) - 1 == (long)shl->startcol)
4754 char_attr = shl->attr;
4755 if (shl != &search_hl && cur != NULL)
4756 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
4759#endif
4760 ScreenAttrs[off] = char_attr;
4761#ifdef FEAT_RIGHTLEFT
4762 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004765 --off;
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 else
4768#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004771 ++off;
4772 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004773 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004774#ifdef FEAT_SYN_HL
4775 eol_hl_off = 1;
4776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
Bram Moolenaar91170f82006-05-05 21:15:17 +00004780 /*
4781 * At end of the text line.
4782 */
4783 if (c == NUL)
4784 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004785#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004786 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4787 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004788 {
4789 /* highlight last char after line */
4790 --col;
4791 --off;
4792 --vcol;
4793 }
4794
Bram Moolenaar1a384422010-07-14 19:53:30 +02004795 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004796 if (wp->w_p_wrap)
4797 v = wp->w_skipcol;
4798 else
4799 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004800
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004801 /* check if line ends before left margin */
4802 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004803 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004804#ifdef FEAT_CONCEAL
4805 /* Get rid of the boguscols now, we want to draw until the right
4806 * edge for 'cursorcolumn'. */
4807 col -= boguscols;
4808 boguscols = 0;
4809#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004810
4811 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004812 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004813
4814 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004815 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4816 && (int)wp->w_virtcol <
4817 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004818 && lnum != wp->w_cursor.lnum)
4819 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004820# ifdef FEAT_RIGHTLEFT
4821 && !wp->w_p_rl
4822# endif
4823 )
4824 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004825 int rightmost_vcol = 0;
4826 int i;
4827
4828 if (wp->w_p_cuc)
4829 rightmost_vcol = wp->w_virtcol;
4830 if (draw_color_col)
4831 /* determine rightmost colorcolumn to possibly draw */
4832 for (i = 0; color_cols[i] >= 0; ++i)
4833 if (rightmost_vcol < color_cols[i])
4834 rightmost_vcol = color_cols[i];
4835
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004836 while (col < W_WIDTH(wp))
4837 {
4838 ScreenLines[off] = ' ';
4839#ifdef FEAT_MBYTE
4840 if (enc_utf8)
4841 ScreenLinesUC[off] = 0;
4842#endif
4843 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004844 if (draw_color_col)
4845 draw_color_col = advance_color_col(VCOL_HLC,
4846 &color_cols);
4847
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004848 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004849 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004850 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004851 ScreenAttrs[off++] = hl_attr(HLF_MC);
4852 else
4853 ScreenAttrs[off++] = 0;
4854
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004855 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004856 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004857
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004858 ++vcol;
4859 }
4860 }
4861#endif
4862
Bram Moolenaar860cae12010-06-05 23:22:07 +02004863 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4864 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 row++;
4866
4867 /*
4868 * Update w_cline_height and w_cline_folded if the cursor line was
4869 * updated (saves a call to plines() later).
4870 */
4871 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4872 {
4873 curwin->w_cline_row = startrow;
4874 curwin->w_cline_height = row - startrow;
4875#ifdef FEAT_FOLDING
4876 curwin->w_cline_folded = FALSE;
4877#endif
4878 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4879 }
4880
4881 break;
4882 }
4883
4884 /* line continues beyond line end */
4885 if (lcs_ext
4886 && !wp->w_p_wrap
4887#ifdef FEAT_DIFF
4888 && filler_todo <= 0
4889#endif
4890 && (
4891#ifdef FEAT_RIGHTLEFT
4892 wp->w_p_rl ? col == 0 :
4893#endif
4894 col == W_WIDTH(wp) - 1)
4895 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004896 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4898 {
4899 c = lcs_ext;
4900 char_attr = hl_attr(HLF_AT);
4901#ifdef FEAT_MBYTE
4902 mb_c = c;
4903 if (enc_utf8 && (*mb_char2len)(c) > 1)
4904 {
4905 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004906 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004907 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 else
4910 mb_utf8 = FALSE;
4911#endif
4912 }
4913
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004914#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004915 /* advance to the next 'colorcolumn' */
4916 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004917 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004918
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004919 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004920 * highlight the cursor position itself.
4921 * Also highlight the 'colorcolumn' if it is different than
4922 * 'cursorcolumn' */
4923 vcol_save_attr = -1;
4924 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004925 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004926 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004927 && lnum != wp->w_cursor.lnum)
4928 {
4929 vcol_save_attr = char_attr;
4930 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4931 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004932 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004933 {
4934 vcol_save_attr = char_attr;
4935 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4936 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004937 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004938#endif
4939
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 /*
4941 * Store character to be displayed.
4942 * Skip characters that are left of the screen for 'nowrap'.
4943 */
4944 vcol_prev = vcol;
4945 if (draw_state < WL_LINE || n_skip <= 0)
4946 {
4947 /*
4948 * Store the character.
4949 */
4950#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4951 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4952 {
4953 /* A double-wide character is: put first halve in left cell. */
4954 --off;
4955 --col;
4956 }
4957#endif
4958 ScreenLines[off] = c;
4959#ifdef FEAT_MBYTE
4960 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004961 {
4962 if ((mb_c & 0xff00) == 0x8e00)
4963 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 else if (enc_utf8)
4967 {
4968 if (mb_utf8)
4969 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004970 int i;
4971
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004973 if ((c & 0xff) == 0)
4974 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004975 for (i = 0; i < Screen_mco; ++i)
4976 {
4977 ScreenLinesC[i][off] = u8cc[i];
4978 if (u8cc[i] == 0)
4979 break;
4980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 else
4983 ScreenLinesUC[off] = 0;
4984 }
4985 if (multi_attr)
4986 {
4987 ScreenAttrs[off] = multi_attr;
4988 multi_attr = 0;
4989 }
4990 else
4991#endif
4992 ScreenAttrs[off] = char_attr;
4993
4994#ifdef FEAT_MBYTE
4995 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4996 {
4997 /* Need to fill two screen columns. */
4998 ++off;
4999 ++col;
5000 if (enc_utf8)
5001 /* UTF-8: Put a 0 in the second screen char. */
5002 ScreenLines[off] = 0;
5003 else
5004 /* DBCS: Put second byte in the second screen char. */
5005 ScreenLines[off] = mb_c & 0xff;
5006 ++vcol;
5007 /* When "tocol" is halfway a character, set it to the end of
5008 * the character, otherwise highlighting won't stop. */
5009 if (tocol == vcol)
5010 ++tocol;
5011#ifdef FEAT_RIGHTLEFT
5012 if (wp->w_p_rl)
5013 {
5014 /* now it's time to backup one cell */
5015 --off;
5016 --col;
5017 }
5018#endif
5019 }
5020#endif
5021#ifdef FEAT_RIGHTLEFT
5022 if (wp->w_p_rl)
5023 {
5024 --off;
5025 --col;
5026 }
5027 else
5028#endif
5029 {
5030 ++off;
5031 ++col;
5032 }
5033 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005034#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005035 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005036 {
5037 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005038 ++vcol_off;
5039 if (n_extra > 0)
5040 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005041 if (wp->w_p_wrap)
5042 {
5043 /*
5044 * Special voodoo required if 'wrap' is on.
5045 *
5046 * Advance the column indicator to force the line
5047 * drawing to wrap early. This will make the line
5048 * take up the same screen space when parts are concealed,
5049 * so that cursor line computations aren't messed up.
5050 *
5051 * To avoid the fictitious advance of 'col' causing
5052 * trailing junk to be written out of the screen line
5053 * we are building, 'boguscols' keeps track of the number
5054 * of bad columns we have advanced.
5055 */
5056 if (n_extra > 0)
5057 {
5058 vcol += n_extra;
5059# ifdef FEAT_RIGHTLEFT
5060 if (wp->w_p_rl)
5061 {
5062 col -= n_extra;
5063 boguscols -= n_extra;
5064 }
5065 else
5066# endif
5067 {
5068 col += n_extra;
5069 boguscols += n_extra;
5070 }
5071 n_extra = 0;
5072 n_attr = 0;
5073 }
5074
5075
5076# ifdef FEAT_MBYTE
5077 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5078 {
5079 /* Need to fill two screen columns. */
5080# ifdef FEAT_RIGHTLEFT
5081 if (wp->w_p_rl)
5082 {
5083 --boguscols;
5084 --col;
5085 }
5086 else
5087# endif
5088 {
5089 ++boguscols;
5090 ++col;
5091 }
5092 }
5093# endif
5094
5095# ifdef FEAT_RIGHTLEFT
5096 if (wp->w_p_rl)
5097 {
5098 --boguscols;
5099 --col;
5100 }
5101 else
5102# endif
5103 {
5104 ++boguscols;
5105 ++col;
5106 }
5107 }
5108 else
5109 {
5110 if (n_extra > 0)
5111 {
5112 vcol += n_extra;
5113 n_extra = 0;
5114 n_attr = 0;
5115 }
5116 }
5117
5118 }
5119#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 else
5121 --n_skip;
5122
Bram Moolenaar64486672010-05-16 15:46:46 +02005123 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5124 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005125 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126#ifdef FEAT_DIFF
5127 && filler_todo <= 0
5128#endif
5129 )
5130 ++vcol;
5131
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005132#ifdef FEAT_SYN_HL
5133 if (vcol_save_attr >= 0)
5134 char_attr = vcol_save_attr;
5135#endif
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 /* restore attributes after "predeces" in 'listchars' */
5138 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5139 char_attr = saved_attr3;
5140
5141 /* restore attributes after last 'listchars' or 'number' char */
5142 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5143 char_attr = saved_attr2;
5144
5145 /*
5146 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005147 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 */
5149 if ((
5150#ifdef FEAT_RIGHTLEFT
5151 wp->w_p_rl ? (col < 0) :
5152#endif
5153 (col >= W_WIDTH(wp)))
5154 && (*ptr != NUL
5155#ifdef FEAT_DIFF
5156 || filler_todo > 0
5157#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005158 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5160 )
5161 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005162#ifdef FEAT_CONCEAL
5163 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5164 (int)W_WIDTH(wp), wp->w_p_rl);
5165 boguscols = 0;
5166#else
5167 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5168 (int)W_WIDTH(wp), wp->w_p_rl);
5169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 ++row;
5171 ++screen_row;
5172
5173 /* When not wrapping and finished diff lines, or when displayed
5174 * '$' and highlighting until last column, break here. */
5175 if ((!wp->w_p_wrap
5176#ifdef FEAT_DIFF
5177 && filler_todo <= 0
5178#endif
5179 ) || lcs_eol_one == -1)
5180 break;
5181
5182 /* When the window is too narrow draw all "@" lines. */
5183 if (draw_state != WL_LINE
5184#ifdef FEAT_DIFF
5185 && filler_todo <= 0
5186#endif
5187 )
5188 {
5189 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5190#ifdef FEAT_VERTSPLIT
5191 draw_vsep_win(wp, row);
5192#endif
5193 row = endrow;
5194 }
5195
5196 /* When line got too long for screen break here. */
5197 if (row == endrow)
5198 {
5199 ++row;
5200 break;
5201 }
5202
5203 if (screen_cur_row == screen_row - 1
5204#ifdef FEAT_DIFF
5205 && filler_todo <= 0
5206#endif
5207 && W_WIDTH(wp) == Columns)
5208 {
5209 /* Remember that the line wraps, used for modeless copy. */
5210 LineWraps[screen_row - 1] = TRUE;
5211
5212 /*
5213 * Special trick to make copy/paste of wrapped lines work with
5214 * xterm/screen: write an extra character beyond the end of
5215 * the line. This will work with all terminal types
5216 * (regardless of the xn,am settings).
5217 * Only do this on a fast tty.
5218 * Only do this if the cursor is on the current line
5219 * (something has been written in it).
5220 * Don't do this for the GUI.
5221 * Don't do this for double-width characters.
5222 * Don't do this for a window not at the right screen border.
5223 */
5224 if (p_tf
5225#ifdef FEAT_GUI
5226 && !gui.in_use
5227#endif
5228#ifdef FEAT_MBYTE
5229 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005230 && ((*mb_off2cells)(LineOffset[screen_row],
5231 LineOffset[screen_row] + screen_Columns)
5232 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005234 + (int)Columns - 2,
5235 LineOffset[screen_row] + screen_Columns)
5236 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237#endif
5238 )
5239 {
5240 /* First make sure we are at the end of the screen line,
5241 * then output the same character again to let the
5242 * terminal know about the wrap. If the terminal doesn't
5243 * auto-wrap, we overwrite the character. */
5244 if (screen_cur_col != W_WIDTH(wp))
5245 screen_char(LineOffset[screen_row - 1]
5246 + (unsigned)Columns - 1,
5247 screen_row - 1, (int)(Columns - 1));
5248
5249#ifdef FEAT_MBYTE
5250 /* When there is a multi-byte character, just output a
5251 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005252 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5253 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 out_char(' ');
5255 else
5256#endif
5257 out_char(ScreenLines[LineOffset[screen_row - 1]
5258 + (Columns - 1)]);
5259 /* force a redraw of the first char on the next line */
5260 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5261 screen_start(); /* don't know where cursor is now */
5262 }
5263 }
5264
5265 col = 0;
5266 off = (unsigned)(current_ScreenLine - ScreenLines);
5267#ifdef FEAT_RIGHTLEFT
5268 if (wp->w_p_rl)
5269 {
5270 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5271 off += col;
5272 }
5273#endif
5274
5275 /* reset the drawing state for the start of a wrapped line */
5276 draw_state = WL_START;
5277 saved_n_extra = n_extra;
5278 saved_p_extra = p_extra;
5279 saved_c_extra = c_extra;
5280 saved_char_attr = char_attr;
5281 n_extra = 0;
5282 lcs_prec_todo = lcs_prec;
5283#ifdef FEAT_LINEBREAK
5284# ifdef FEAT_DIFF
5285 if (filler_todo <= 0)
5286# endif
5287 need_showbreak = TRUE;
5288#endif
5289#ifdef FEAT_DIFF
5290 --filler_todo;
5291 /* When the filler lines are actually below the last line of the
5292 * file, don't draw the line itself, break here. */
5293 if (filler_todo == 0 && wp->w_botfill)
5294 break;
5295#endif
5296 }
5297
5298 } /* for every character in the line */
5299
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005300#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005301 /* After an empty line check first word for capital. */
5302 if (*skipwhite(line) == NUL)
5303 {
5304 capcol_lnum = lnum + 1;
5305 cap_col = 0;
5306 }
5307#endif
5308
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 return row;
5310}
5311
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005312#ifdef FEAT_MBYTE
5313static int comp_char_differs __ARGS((int, int));
5314
5315/*
5316 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005317 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005318 */
5319 static int
5320comp_char_differs(off_from, off_to)
5321 int off_from;
5322 int off_to;
5323{
5324 int i;
5325
5326 for (i = 0; i < Screen_mco; ++i)
5327 {
5328 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5329 return TRUE;
5330 if (ScreenLinesC[i][off_from] == 0)
5331 break;
5332 }
5333 return FALSE;
5334}
5335#endif
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337/*
5338 * Check whether the given character needs redrawing:
5339 * - the (first byte of the) character is different
5340 * - the attributes are different
5341 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005342 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 */
5344 static int
5345char_needs_redraw(off_from, off_to, cols)
5346 int off_from;
5347 int off_to;
5348 int cols;
5349{
5350 if (cols > 0
5351 && ((ScreenLines[off_from] != ScreenLines[off_to]
5352 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5353
5354#ifdef FEAT_MBYTE
5355 || (enc_dbcs != 0
5356 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5357 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5358 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5359 : (cols > 1 && ScreenLines[off_from + 1]
5360 != ScreenLines[off_to + 1])))
5361 || (enc_utf8
5362 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5363 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005364 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005365 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5366 && ScreenLines[off_from + 1]
5367 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#endif
5369 ))
5370 return TRUE;
5371 return FALSE;
5372}
5373
5374/*
5375 * Move one "cooked" screen line to the screen, but only the characters that
5376 * have actually changed. Handle insert/delete character.
5377 * "coloff" gives the first column on the screen for this line.
5378 * "endcol" gives the columns where valid characters are.
5379 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5380 * needs to be cleared, negative otherwise.
5381 * "rlflag" is TRUE in a rightleft window:
5382 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5383 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5384 */
5385 static void
5386screen_line(row, coloff, endcol, clear_width
5387#ifdef FEAT_RIGHTLEFT
5388 , rlflag
5389#endif
5390 )
5391 int row;
5392 int coloff;
5393 int endcol;
5394 int clear_width;
5395#ifdef FEAT_RIGHTLEFT
5396 int rlflag;
5397#endif
5398{
5399 unsigned off_from;
5400 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005401#ifdef FEAT_MBYTE
5402 unsigned max_off_from;
5403 unsigned max_off_to;
5404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 int col = 0;
5406#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5407 int hl;
5408#endif
5409 int force = FALSE; /* force update rest of the line */
5410 int redraw_this /* bool: does character need redraw? */
5411#ifdef FEAT_GUI
5412 = TRUE /* For GUI when while-loop empty */
5413#endif
5414 ;
5415 int redraw_next; /* redraw_this for next character */
5416#ifdef FEAT_MBYTE
5417 int clear_next = FALSE;
5418 int char_cells; /* 1: normal char */
5419 /* 2: occupies two display cells */
5420# define CHAR_CELLS char_cells
5421#else
5422# define CHAR_CELLS 1
5423#endif
5424
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005425 /* Check for illegal row and col, just in case. */
5426 if (row >= Rows)
5427 row = Rows - 1;
5428 if (endcol > Columns)
5429 endcol = Columns;
5430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431# ifdef FEAT_CLIPBOARD
5432 clip_may_clear_selection(row, row);
5433# endif
5434
5435 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5436 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005437#ifdef FEAT_MBYTE
5438 max_off_from = off_from + screen_Columns;
5439 max_off_to = LineOffset[row] + screen_Columns;
5440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442#ifdef FEAT_RIGHTLEFT
5443 if (rlflag)
5444 {
5445 /* Clear rest first, because it's left of the text. */
5446 if (clear_width > 0)
5447 {
5448 while (col <= endcol && ScreenLines[off_to] == ' '
5449 && ScreenAttrs[off_to] == 0
5450# ifdef FEAT_MBYTE
5451 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5452# endif
5453 )
5454 {
5455 ++off_to;
5456 ++col;
5457 }
5458 if (col <= endcol)
5459 screen_fill(row, row + 1, col + coloff,
5460 endcol + coloff + 1, ' ', ' ', 0);
5461 }
5462 col = endcol + 1;
5463 off_to = LineOffset[row] + col + coloff;
5464 off_from += col;
5465 endcol = (clear_width > 0 ? clear_width : -clear_width);
5466 }
5467#endif /* FEAT_RIGHTLEFT */
5468
5469 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5470
5471 while (col < endcol)
5472 {
5473#ifdef FEAT_MBYTE
5474 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005475 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 else
5477 char_cells = 1;
5478#endif
5479
5480 redraw_this = redraw_next;
5481 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5482 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5483
5484#ifdef FEAT_GUI
5485 /* If the next character was bold, then redraw the current character to
5486 * remove any pixels that might have spilt over into us. This only
5487 * happens in the GUI.
5488 */
5489 if (redraw_next && gui.in_use)
5490 {
5491 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005492 if (hl > HL_ALL)
5493 hl = syn_attr2attr(hl);
5494 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 redraw_this = TRUE;
5496 }
5497#endif
5498
5499 if (redraw_this)
5500 {
5501 /*
5502 * Special handling when 'xs' termcap flag set (hpterm):
5503 * Attributes for characters are stored at the position where the
5504 * cursor is when writing the highlighting code. The
5505 * start-highlighting code must be written with the cursor on the
5506 * first highlighted character. The stop-highlighting code must
5507 * be written with the cursor just after the last highlighted
5508 * character.
5509 * Overwriting a character doesn't remove it's highlighting. Need
5510 * to clear the rest of the line, and force redrawing it
5511 * completely.
5512 */
5513 if ( p_wiv
5514 && !force
5515#ifdef FEAT_GUI
5516 && !gui.in_use
5517#endif
5518 && ScreenAttrs[off_to] != 0
5519 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5520 {
5521 /*
5522 * Need to remove highlighting attributes here.
5523 */
5524 windgoto(row, col + coloff);
5525 out_str(T_CE); /* clear rest of this screen line */
5526 screen_start(); /* don't know where cursor is now */
5527 force = TRUE; /* force redraw of rest of the line */
5528 redraw_next = TRUE; /* or else next char would miss out */
5529
5530 /*
5531 * If the previous character was highlighted, need to stop
5532 * highlighting at this character.
5533 */
5534 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5535 {
5536 screen_attr = ScreenAttrs[off_to - 1];
5537 term_windgoto(row, col + coloff);
5538 screen_stop_highlight();
5539 }
5540 else
5541 screen_attr = 0; /* highlighting has stopped */
5542 }
5543#ifdef FEAT_MBYTE
5544 if (enc_dbcs != 0)
5545 {
5546 /* Check if overwriting a double-byte with a single-byte or
5547 * the other way around requires another character to be
5548 * redrawn. For UTF-8 this isn't needed, because comparing
5549 * ScreenLinesUC[] is sufficient. */
5550 if (char_cells == 1
5551 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005552 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
5554 /* Writing a single-cell character over a double-cell
5555 * character: need to redraw the next cell. */
5556 ScreenLines[off_to + 1] = 0;
5557 redraw_next = TRUE;
5558 }
5559 else if (char_cells == 2
5560 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005561 && (*mb_off2cells)(off_to, max_off_to) == 1
5562 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
5564 /* Writing the second half of a double-cell character over
5565 * a double-cell character: need to redraw the second
5566 * cell. */
5567 ScreenLines[off_to + 2] = 0;
5568 redraw_next = TRUE;
5569 }
5570
5571 if (enc_dbcs == DBCS_JPNU)
5572 ScreenLines2[off_to] = ScreenLines2[off_from];
5573 }
5574 /* When writing a single-width character over a double-width
5575 * character and at the end of the redrawn text, need to clear out
5576 * the right halve of the old character.
5577 * Also required when writing the right halve of a double-width
5578 * char over the left halve of an existing one. */
5579 if (has_mbyte && col + char_cells == endcol
5580 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005581 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005583 && (*mb_off2cells)(off_to, max_off_to) == 1
5584 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 clear_next = TRUE;
5586#endif
5587
5588 ScreenLines[off_to] = ScreenLines[off_from];
5589#ifdef FEAT_MBYTE
5590 if (enc_utf8)
5591 {
5592 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5593 if (ScreenLinesUC[off_from] != 0)
5594 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005595 int i;
5596
5597 for (i = 0; i < Screen_mco; ++i)
5598 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
5600 }
5601 if (char_cells == 2)
5602 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5603#endif
5604
5605#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005606 /* The bold trick makes a single column of pixels appear in the
5607 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * character should be redrawn too. This happens for our own GUI
5609 * and for some xterms. */
5610 if (
5611# ifdef FEAT_GUI
5612 gui.in_use
5613# endif
5614# if defined(FEAT_GUI) && defined(UNIX)
5615 ||
5616# endif
5617# ifdef UNIX
5618 term_is_xterm
5619# endif
5620 )
5621 {
5622 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005623 if (hl > HL_ALL)
5624 hl = syn_attr2attr(hl);
5625 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 redraw_next = TRUE;
5627 }
5628#endif
5629 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5630#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005631 /* For simplicity set the attributes of second half of a
5632 * double-wide character equal to the first half. */
5633 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005635
5636 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 else
5639#endif
5640 screen_char(off_to, row, col + coloff);
5641 }
5642 else if ( p_wiv
5643#ifdef FEAT_GUI
5644 && !gui.in_use
5645#endif
5646 && col + coloff > 0)
5647 {
5648 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5649 {
5650 /*
5651 * Don't output stop-highlight when moving the cursor, it will
5652 * stop the highlighting when it should continue.
5653 */
5654 screen_attr = 0;
5655 }
5656 else if (screen_attr != 0)
5657 screen_stop_highlight();
5658 }
5659
5660 off_to += CHAR_CELLS;
5661 off_from += CHAR_CELLS;
5662 col += CHAR_CELLS;
5663 }
5664
5665#ifdef FEAT_MBYTE
5666 if (clear_next)
5667 {
5668 /* Clear the second half of a double-wide character of which the left
5669 * half was overwritten with a single-wide character. */
5670 ScreenLines[off_to] = ' ';
5671 if (enc_utf8)
5672 ScreenLinesUC[off_to] = 0;
5673 screen_char(off_to, row, col + coloff);
5674 }
5675#endif
5676
5677 if (clear_width > 0
5678#ifdef FEAT_RIGHTLEFT
5679 && !rlflag
5680#endif
5681 )
5682 {
5683#ifdef FEAT_GUI
5684 int startCol = col;
5685#endif
5686
5687 /* blank out the rest of the line */
5688 while (col < clear_width && ScreenLines[off_to] == ' '
5689 && ScreenAttrs[off_to] == 0
5690#ifdef FEAT_MBYTE
5691 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5692#endif
5693 )
5694 {
5695 ++off_to;
5696 ++col;
5697 }
5698 if (col < clear_width)
5699 {
5700#ifdef FEAT_GUI
5701 /*
5702 * In the GUI, clearing the rest of the line may leave pixels
5703 * behind if the first character cleared was bold. Some bold
5704 * fonts spill over the left. In this case we redraw the previous
5705 * character too. If we didn't skip any blanks above, then we
5706 * only redraw if the character wasn't already redrawn anyway.
5707 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005708 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 {
5710 hl = ScreenAttrs[off_to];
5711 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005712 {
5713 int prev_cells = 1;
5714# ifdef FEAT_MBYTE
5715 if (enc_utf8)
5716 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5717 * that its width is 2. */
5718 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5719 else if (enc_dbcs != 0)
5720 {
5721 /* find previous character by counting from first
5722 * column and get its width. */
5723 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005724 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005725
5726 while (off < off_to)
5727 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005728 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005729 off += prev_cells;
5730 }
5731 }
5732
5733 if (enc_dbcs != 0 && prev_cells > 1)
5734 screen_char_2(off_to - prev_cells, row,
5735 col + coloff - prev_cells);
5736 else
5737# endif
5738 screen_char(off_to - prev_cells, row,
5739 col + coloff - prev_cells);
5740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
5742#endif
5743 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5744 ' ', ' ', 0);
5745#ifdef FEAT_VERTSPLIT
5746 off_to += clear_width - col;
5747 col = clear_width;
5748#endif
5749 }
5750 }
5751
5752 if (clear_width > 0)
5753 {
5754#ifdef FEAT_VERTSPLIT
5755 /* For a window that's left of another, draw the separator char. */
5756 if (col + coloff < Columns)
5757 {
5758 int c;
5759
5760 c = fillchar_vsep(&hl);
5761 if (ScreenLines[off_to] != c
5762# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005763 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5764 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765# endif
5766 || ScreenAttrs[off_to] != hl)
5767 {
5768 ScreenLines[off_to] = c;
5769 ScreenAttrs[off_to] = hl;
5770# ifdef FEAT_MBYTE
5771 if (enc_utf8)
5772 {
5773 if (c >= 0x80)
5774 {
5775 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005776 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778 else
5779 ScreenLinesUC[off_to] = 0;
5780 }
5781# endif
5782 screen_char(off_to, row, col + coloff);
5783 }
5784 }
5785 else
5786#endif
5787 LineWraps[row] = FALSE;
5788 }
5789}
5790
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005791#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005793 * Mirror text "str" for right-left displaying.
5794 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005796 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797rl_mirror(str)
5798 char_u *str;
5799{
5800 char_u *p1, *p2;
5801 int t;
5802
5803 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5804 {
5805 t = *p1;
5806 *p1 = *p2;
5807 *p2 = t;
5808 }
5809}
5810#endif
5811
5812#if defined(FEAT_WINDOWS) || defined(PROTO)
5813/*
5814 * mark all status lines for redraw; used after first :cd
5815 */
5816 void
5817status_redraw_all()
5818{
5819 win_T *wp;
5820
5821 for (wp = firstwin; wp; wp = wp->w_next)
5822 if (wp->w_status_height)
5823 {
5824 wp->w_redr_status = TRUE;
5825 redraw_later(VALID);
5826 }
5827}
5828
5829/*
5830 * mark all status lines of the current buffer for redraw
5831 */
5832 void
5833status_redraw_curbuf()
5834{
5835 win_T *wp;
5836
5837 for (wp = firstwin; wp; wp = wp->w_next)
5838 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5839 {
5840 wp->w_redr_status = TRUE;
5841 redraw_later(VALID);
5842 }
5843}
5844
5845/*
5846 * Redraw all status lines that need to be redrawn.
5847 */
5848 void
5849redraw_statuslines()
5850{
5851 win_T *wp;
5852
5853 for (wp = firstwin; wp; wp = wp->w_next)
5854 if (wp->w_redr_status)
5855 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005856 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005857 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858}
5859#endif
5860
5861#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5862/*
5863 * Redraw all status lines at the bottom of frame "frp".
5864 */
5865 void
5866win_redraw_last_status(frp)
5867 frame_T *frp;
5868{
5869 if (frp->fr_layout == FR_LEAF)
5870 frp->fr_win->w_redr_status = TRUE;
5871 else if (frp->fr_layout == FR_ROW)
5872 {
5873 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5874 win_redraw_last_status(frp);
5875 }
5876 else /* frp->fr_layout == FR_COL */
5877 {
5878 frp = frp->fr_child;
5879 while (frp->fr_next != NULL)
5880 frp = frp->fr_next;
5881 win_redraw_last_status(frp);
5882 }
5883}
5884#endif
5885
5886#ifdef FEAT_VERTSPLIT
5887/*
5888 * Draw the verticap separator right of window "wp" starting with line "row".
5889 */
5890 static void
5891draw_vsep_win(wp, row)
5892 win_T *wp;
5893 int row;
5894{
5895 int hl;
5896 int c;
5897
5898 if (wp->w_vsep_width)
5899 {
5900 /* draw the vertical separator right of this window */
5901 c = fillchar_vsep(&hl);
5902 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5903 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5904 c, ' ', hl);
5905 }
5906}
5907#endif
5908
5909#ifdef FEAT_WILDMENU
5910static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005911static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
5913/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005914 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 */
5916 static int
5917status_match_len(xp, s)
5918 expand_T *xp;
5919 char_u *s;
5920{
5921 int len = 0;
5922
5923#ifdef FEAT_MENU
5924 int emenu = (xp->xp_context == EXPAND_MENUS
5925 || xp->xp_context == EXPAND_MENUNAMES);
5926
5927 /* Check for menu separators - replace with '|'. */
5928 if (emenu && menu_is_separator(s))
5929 return 1;
5930#endif
5931
5932 while (*s != NUL)
5933 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005934 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005935 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005936 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938
5939 return len;
5940}
5941
5942/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005943 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005944 * These are backslashes used for escaping. Do show backslashes in help tags.
5945 */
5946 static int
5947skip_status_match_char(xp, s)
5948 expand_T *xp;
5949 char_u *s;
5950{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005951 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005952#ifdef FEAT_MENU
5953 || ((xp->xp_context == EXPAND_MENUS
5954 || xp->xp_context == EXPAND_MENUNAMES)
5955 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5956#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005957 )
5958 {
5959#ifndef BACKSLASH_IN_FILENAME
5960 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5961 return 2;
5962#endif
5963 return 1;
5964 }
5965 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005966}
5967
5968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 * Show wildchar matches in the status line.
5970 * Show at least the "match" item.
5971 * We start at item 'first_match' in the list and show all matches that fit.
5972 *
5973 * If inversion is possible we use it. Else '=' characters are used.
5974 */
5975 void
5976win_redr_status_matches(xp, num_matches, matches, match, showtail)
5977 expand_T *xp;
5978 int num_matches;
5979 char_u **matches; /* list of matches */
5980 int match;
5981 int showtail;
5982{
5983#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5984 int row;
5985 char_u *buf;
5986 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005987 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 int fillchar;
5989 int attr;
5990 int i;
5991 int highlight = TRUE;
5992 char_u *selstart = NULL;
5993 int selstart_col = 0;
5994 char_u *selend = NULL;
5995 static int first_match = 0;
5996 int add_left = FALSE;
5997 char_u *s;
5998#ifdef FEAT_MENU
5999 int emenu;
6000#endif
6001#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6002 int l;
6003#endif
6004
6005 if (matches == NULL) /* interrupted completion? */
6006 return;
6007
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006008#ifdef FEAT_MBYTE
6009 if (has_mbyte)
6010 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6011 else
6012#endif
6013 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 if (buf == NULL)
6015 return;
6016
6017 if (match == -1) /* don't show match but original text */
6018 {
6019 match = 0;
6020 highlight = FALSE;
6021 }
6022 /* count 1 for the ending ">" */
6023 clen = status_match_len(xp, L_MATCH(match)) + 3;
6024 if (match == 0)
6025 first_match = 0;
6026 else if (match < first_match)
6027 {
6028 /* jumping left, as far as we can go */
6029 first_match = match;
6030 add_left = TRUE;
6031 }
6032 else
6033 {
6034 /* check if match fits on the screen */
6035 for (i = first_match; i < match; ++i)
6036 clen += status_match_len(xp, L_MATCH(i)) + 2;
6037 if (first_match > 0)
6038 clen += 2;
6039 /* jumping right, put match at the left */
6040 if ((long)clen > Columns)
6041 {
6042 first_match = match;
6043 /* if showing the last match, we can add some on the left */
6044 clen = 2;
6045 for (i = match; i < num_matches; ++i)
6046 {
6047 clen += status_match_len(xp, L_MATCH(i)) + 2;
6048 if ((long)clen >= Columns)
6049 break;
6050 }
6051 if (i == num_matches)
6052 add_left = TRUE;
6053 }
6054 }
6055 if (add_left)
6056 while (first_match > 0)
6057 {
6058 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6059 if ((long)clen >= Columns)
6060 break;
6061 --first_match;
6062 }
6063
6064 fillchar = fillchar_status(&attr, TRUE);
6065
6066 if (first_match == 0)
6067 {
6068 *buf = NUL;
6069 len = 0;
6070 }
6071 else
6072 {
6073 STRCPY(buf, "< ");
6074 len = 2;
6075 }
6076 clen = len;
6077
6078 i = first_match;
6079 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6080 {
6081 if (i == match)
6082 {
6083 selstart = buf + len;
6084 selstart_col = clen;
6085 }
6086
6087 s = L_MATCH(i);
6088 /* Check for menu separators - replace with '|' */
6089#ifdef FEAT_MENU
6090 emenu = (xp->xp_context == EXPAND_MENUS
6091 || xp->xp_context == EXPAND_MENUNAMES);
6092 if (emenu && menu_is_separator(s))
6093 {
6094 STRCPY(buf + len, transchar('|'));
6095 l = (int)STRLEN(buf + len);
6096 len += l;
6097 clen += l;
6098 }
6099 else
6100#endif
6101 for ( ; *s != NUL; ++s)
6102 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006103 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 clen += ptr2cells(s);
6105#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006106 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 STRNCPY(buf + len, s, l);
6109 s += l - 1;
6110 len += l;
6111 }
6112 else
6113#endif
6114 {
6115 STRCPY(buf + len, transchar_byte(*s));
6116 len += (int)STRLEN(buf + len);
6117 }
6118 }
6119 if (i == match)
6120 selend = buf + len;
6121
6122 *(buf + len++) = ' ';
6123 *(buf + len++) = ' ';
6124 clen += 2;
6125 if (++i == num_matches)
6126 break;
6127 }
6128
6129 if (i != num_matches)
6130 {
6131 *(buf + len++) = '>';
6132 ++clen;
6133 }
6134
6135 buf[len] = NUL;
6136
6137 row = cmdline_row - 1;
6138 if (row >= 0)
6139 {
6140 if (wild_menu_showing == 0)
6141 {
6142 if (msg_scrolled > 0)
6143 {
6144 /* Put the wildmenu just above the command line. If there is
6145 * no room, scroll the screen one line up. */
6146 if (cmdline_row == Rows - 1)
6147 {
6148 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6149 ++msg_scrolled;
6150 }
6151 else
6152 {
6153 ++cmdline_row;
6154 ++row;
6155 }
6156 wild_menu_showing = WM_SCROLLED;
6157 }
6158 else
6159 {
6160 /* Create status line if needed by setting 'laststatus' to 2.
6161 * Set 'winminheight' to zero to avoid that the window is
6162 * resized. */
6163 if (lastwin->w_status_height == 0)
6164 {
6165 save_p_ls = p_ls;
6166 save_p_wmh = p_wmh;
6167 p_ls = 2;
6168 p_wmh = 0;
6169 last_status(FALSE);
6170 }
6171 wild_menu_showing = WM_SHOWN;
6172 }
6173 }
6174
6175 screen_puts(buf, row, 0, attr);
6176 if (selstart != NULL && highlight)
6177 {
6178 *selend = NUL;
6179 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6180 }
6181
6182 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6183 }
6184
6185#ifdef FEAT_VERTSPLIT
6186 win_redraw_last_status(topframe);
6187#else
6188 lastwin->w_redr_status = TRUE;
6189#endif
6190 vim_free(buf);
6191}
6192#endif
6193
6194#if defined(FEAT_WINDOWS) || defined(PROTO)
6195/*
6196 * Redraw the status line of window wp.
6197 *
6198 * If inversion is possible we use it. Else '=' characters are used.
6199 */
6200 void
6201win_redr_status(wp)
6202 win_T *wp;
6203{
6204 int row;
6205 char_u *p;
6206 int len;
6207 int fillchar;
6208 int attr;
6209 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006210 static int busy = FALSE;
6211
6212 /* It's possible to get here recursively when 'statusline' (indirectly)
6213 * invokes ":redrawstatus". Simply ignore the call then. */
6214 if (busy)
6215 return;
6216 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218 wp->w_redr_status = FALSE;
6219 if (wp->w_status_height == 0)
6220 {
6221 /* no status line, can only be last window */
6222 redraw_cmdline = TRUE;
6223 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006224 else if (!redrawing()
6225#ifdef FEAT_INS_EXPAND
6226 /* don't update status line when popup menu is visible and may be
6227 * drawn over it */
6228 || pum_visible()
6229#endif
6230 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 /* Don't redraw right now, do it later. */
6233 wp->w_redr_status = TRUE;
6234 }
6235#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006236 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006239 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
6241#endif
6242 else
6243 {
6244 fillchar = fillchar_status(&attr, wp == curwin);
6245
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006246 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 p = NameBuff;
6248 len = (int)STRLEN(p);
6249
6250 if (wp->w_buffer->b_help
6251#ifdef FEAT_QUICKFIX
6252 || wp->w_p_pvw
6253#endif
6254 || bufIsChanged(wp->w_buffer)
6255 || wp->w_buffer->b_p_ro)
6256 *(p + len++) = ' ';
6257 if (wp->w_buffer->b_help)
6258 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006259 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 len += (int)STRLEN(p + len);
6261 }
6262#ifdef FEAT_QUICKFIX
6263 if (wp->w_p_pvw)
6264 {
6265 STRCPY(p + len, _("[Preview]"));
6266 len += (int)STRLEN(p + len);
6267 }
6268#endif
6269 if (bufIsChanged(wp->w_buffer))
6270 {
6271 STRCPY(p + len, "[+]");
6272 len += 3;
6273 }
6274 if (wp->w_buffer->b_p_ro)
6275 {
6276 STRCPY(p + len, "[RO]");
6277 len += 4;
6278 }
6279
6280#ifndef FEAT_VERTSPLIT
6281 this_ru_col = ru_col;
6282 if (this_ru_col < (Columns + 1) / 2)
6283 this_ru_col = (Columns + 1) / 2;
6284#else
6285 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6286 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6287 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6288 if (this_ru_col <= 1)
6289 {
6290 p = (char_u *)"<"; /* No room for file name! */
6291 len = 1;
6292 }
6293 else
6294#endif
6295#ifdef FEAT_MBYTE
6296 if (has_mbyte)
6297 {
6298 int clen = 0, i;
6299
6300 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006301 clen = mb_string2cells(p, -1);
6302
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 /* Find first character that will fit.
6304 * Going from start to end is much faster for DBCS. */
6305 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006306 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 clen -= (*mb_ptr2cells)(p + i);
6308 len = clen;
6309 if (i > 0)
6310 {
6311 p = p + i - 1;
6312 *p = '<';
6313 ++len;
6314 }
6315
6316 }
6317 else
6318#endif
6319 if (len > this_ru_col - 1)
6320 {
6321 p += len - (this_ru_col - 1);
6322 *p = '<';
6323 len = this_ru_col - 1;
6324 }
6325
6326 row = W_WINROW(wp) + wp->w_height;
6327 screen_puts(p, row, W_WINCOL(wp), attr);
6328 screen_fill(row, row + 1, len + W_WINCOL(wp),
6329 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6330
6331 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6332 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6333 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6334 - 1 + W_WINCOL(wp)), attr);
6335
6336#ifdef FEAT_CMDL_INFO
6337 win_redr_ruler(wp, TRUE);
6338#endif
6339 }
6340
6341#ifdef FEAT_VERTSPLIT
6342 /*
6343 * May need to draw the character below the vertical separator.
6344 */
6345 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6346 {
6347 if (stl_connected(wp))
6348 fillchar = fillchar_status(&attr, wp == curwin);
6349 else
6350 fillchar = fillchar_vsep(&attr);
6351 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6352 attr);
6353 }
6354#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006355 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
Bram Moolenaar238a5642006-02-21 22:12:05 +00006358#ifdef FEAT_STL_OPT
6359/*
6360 * Redraw the status line according to 'statusline' and take care of any
6361 * errors encountered.
6362 */
6363 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006364redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006365 win_T *wp;
6366{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006367 static int entered = FALSE;
6368 int save_called_emsg = called_emsg;
6369
6370 /* When called recursively return. This can happen when the statusline
6371 * contains an expression that triggers a redraw. */
6372 if (entered)
6373 return;
6374 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006375
6376 called_emsg = FALSE;
6377 win_redr_custom(wp, FALSE);
6378 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006379 {
6380 /* When there is an error disable the statusline, otherwise the
6381 * display is messed up with errors and a redraw triggers the problem
6382 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006383 set_string_option_direct((char_u *)"statusline", -1,
6384 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006385 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006386 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006387 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006388 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006389}
6390#endif
6391
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392# ifdef FEAT_VERTSPLIT
6393/*
6394 * Return TRUE if the status line of window "wp" is connected to the status
6395 * line of the window right of it. If not, then it's a vertical separator.
6396 * Only call if (wp->w_vsep_width != 0).
6397 */
6398 int
6399stl_connected(wp)
6400 win_T *wp;
6401{
6402 frame_T *fr;
6403
6404 fr = wp->w_frame;
6405 while (fr->fr_parent != NULL)
6406 {
6407 if (fr->fr_parent->fr_layout == FR_COL)
6408 {
6409 if (fr->fr_next != NULL)
6410 break;
6411 }
6412 else
6413 {
6414 if (fr->fr_next != NULL)
6415 return TRUE;
6416 }
6417 fr = fr->fr_parent;
6418 }
6419 return FALSE;
6420}
6421# endif
6422
6423#endif /* FEAT_WINDOWS */
6424
6425#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6426/*
6427 * Get the value to show for the language mappings, active 'keymap'.
6428 */
6429 int
6430get_keymap_str(wp, buf, len)
6431 win_T *wp;
6432 char_u *buf; /* buffer for the result */
6433 int len; /* length of buffer */
6434{
6435 char_u *p;
6436
6437 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6438 return FALSE;
6439
6440 {
6441#ifdef FEAT_EVAL
6442 buf_T *old_curbuf = curbuf;
6443 win_T *old_curwin = curwin;
6444 char_u *s;
6445
6446 curbuf = wp->w_buffer;
6447 curwin = wp;
6448 STRCPY(buf, "b:keymap_name"); /* must be writable */
6449 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006450 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 --emsg_skip;
6452 curbuf = old_curbuf;
6453 curwin = old_curwin;
6454 if (p == NULL || *p == NUL)
6455#endif
6456 {
6457#ifdef FEAT_KEYMAP
6458 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6459 p = wp->w_buffer->b_p_keymap;
6460 else
6461#endif
6462 p = (char_u *)"lang";
6463 }
6464 if ((int)(STRLEN(p) + 3) < len)
6465 sprintf((char *)buf, "<%s>", p);
6466 else
6467 buf[0] = NUL;
6468#ifdef FEAT_EVAL
6469 vim_free(s);
6470#endif
6471 }
6472 return buf[0] != NUL;
6473}
6474#endif
6475
6476#if defined(FEAT_STL_OPT) || defined(PROTO)
6477/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006478 * Redraw the status line or ruler of window "wp".
6479 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 */
6481 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006482win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006484 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485{
6486 int attr;
6487 int curattr;
6488 int row;
6489 int col = 0;
6490 int maxwidth;
6491 int width;
6492 int n;
6493 int len;
6494 int fillchar;
6495 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006496 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006498 struct stl_hlrec hltab[STL_MAX_ITEM];
6499 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006500 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006501 win_T *ewp;
6502 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
6504 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006505 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006507 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006508 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006509 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006510 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006511 attr = hl_attr(HLF_TPF);
6512 maxwidth = Columns;
6513# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006514 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006517 else
6518 {
6519 row = W_WINROW(wp) + wp->w_height;
6520 fillchar = fillchar_status(&attr, wp == curwin);
6521 maxwidth = W_WIDTH(wp);
6522
6523 if (draw_ruler)
6524 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006525 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006526 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006527 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006528 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006529 if (*++stl == '-')
6530 stl++;
6531 if (atoi((char *)stl))
6532 while (VIM_ISDIGIT(*stl))
6533 stl++;
6534 if (*stl++ != '(')
6535 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006536 }
6537#ifdef FEAT_VERTSPLIT
6538 col = ru_col - (Columns - W_WIDTH(wp));
6539 if (col < (W_WIDTH(wp) + 1) / 2)
6540 col = (W_WIDTH(wp) + 1) / 2;
6541#else
6542 col = ru_col;
6543 if (col > (Columns + 1) / 2)
6544 col = (Columns + 1) / 2;
6545#endif
6546 maxwidth = W_WIDTH(wp) - col;
6547#ifdef FEAT_WINDOWS
6548 if (!wp->w_status_height)
6549#endif
6550 {
6551 row = Rows - 1;
6552 --maxwidth; /* writing in last column may cause scrolling */
6553 fillchar = ' ';
6554 attr = 0;
6555 }
6556
6557# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006558 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006559# endif
6560 }
6561 else
6562 {
6563 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006564 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006565 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006566 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006567# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006568 use_sandbox = was_set_insecurely((char_u *)"statusline",
6569 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006570# endif
6571 }
6572
6573#ifdef FEAT_VERTSPLIT
6574 col += W_WINCOL(wp);
6575#endif
6576 }
6577
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 if (maxwidth <= 0)
6579 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580
Bram Moolenaar61452852011-02-01 18:01:11 +01006581 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6582 * the cursor away and back. */
6583 ewp = wp == NULL ? curwin : wp;
6584 p_crb_save = ewp->w_p_crb;
6585 ewp->w_p_crb = FALSE;
6586
Bram Moolenaar362f3562009-11-03 16:20:34 +00006587 /* Make a copy, because the statusline may include a function call that
6588 * might change the option value and free the memory. */
6589 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006590 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006591 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006592 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006593 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006594 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006596 /* Make all characters printable. */
6597 p = transstr(buf);
6598 if (p != NULL)
6599 {
6600 vim_strncpy(buf, p, sizeof(buf) - 1);
6601 vim_free(p);
6602 }
6603
6604 /* fill up with "fillchar" */
6605 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006606 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 {
6608#ifdef FEAT_MBYTE
6609 len += (*mb_char2bytes)(fillchar, buf + len);
6610#else
6611 buf[len++] = fillchar;
6612#endif
6613 ++width;
6614 }
6615 buf[len] = NUL;
6616
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006617 /*
6618 * Draw each snippet with the specified highlighting.
6619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 curattr = attr;
6621 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006622 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006624 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 screen_puts_len(p, len, row, col, curattr);
6626 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006627 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006629 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006631 else if (hltab[n].userhl < 0)
6632 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006634 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006635 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636#endif
6637 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006638 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
6640 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006641
6642 if (wp == NULL)
6643 {
6644 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6645 col = 0;
6646 len = 0;
6647 p = buf;
6648 fillchar = 0;
6649 for (n = 0; tabtab[n].start != NULL; n++)
6650 {
6651 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6652 while (col < len)
6653 TabPageIdxs[col++] = fillchar;
6654 p = tabtab[n].start;
6655 fillchar = tabtab[n].userhl;
6656 }
6657 while (col < Columns)
6658 TabPageIdxs[col++] = fillchar;
6659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660}
6661
6662#endif /* FEAT_STL_OPT */
6663
6664/*
6665 * Output a single character directly to the screen and update ScreenLines.
6666 */
6667 void
6668screen_putchar(c, row, col, attr)
6669 int c;
6670 int row, col;
6671 int attr;
6672{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 char_u buf[MB_MAXBYTES + 1];
6674
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006675#ifdef FEAT_MBYTE
6676 if (has_mbyte)
6677 buf[(*mb_char2bytes)(c, buf)] = NUL;
6678 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006680 {
6681 buf[0] = c;
6682 buf[1] = NUL;
6683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 screen_puts(buf, row, col, attr);
6685}
6686
6687/*
6688 * Get a single character directly from ScreenLines into "bytes[]".
6689 * Also return its attribute in *attrp;
6690 */
6691 void
6692screen_getbytes(row, col, bytes, attrp)
6693 int row, col;
6694 char_u *bytes;
6695 int *attrp;
6696{
6697 unsigned off;
6698
6699 /* safety check */
6700 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6701 {
6702 off = LineOffset[row] + col;
6703 *attrp = ScreenAttrs[off];
6704 bytes[0] = ScreenLines[off];
6705 bytes[1] = NUL;
6706
6707#ifdef FEAT_MBYTE
6708 if (enc_utf8 && ScreenLinesUC[off] != 0)
6709 bytes[utfc_char2bytes(off, bytes)] = NUL;
6710 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6711 {
6712 bytes[0] = ScreenLines[off];
6713 bytes[1] = ScreenLines2[off];
6714 bytes[2] = NUL;
6715 }
6716 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6717 {
6718 bytes[1] = ScreenLines[off + 1];
6719 bytes[2] = NUL;
6720 }
6721#endif
6722 }
6723}
6724
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006725#ifdef FEAT_MBYTE
6726static int screen_comp_differs __ARGS((int, int*));
6727
6728/*
6729 * Return TRUE if composing characters for screen posn "off" differs from
6730 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006731 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006732 */
6733 static int
6734screen_comp_differs(off, u8cc)
6735 int off;
6736 int *u8cc;
6737{
6738 int i;
6739
6740 for (i = 0; i < Screen_mco; ++i)
6741 {
6742 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6743 return TRUE;
6744 if (u8cc[i] == 0)
6745 break;
6746 }
6747 return FALSE;
6748}
6749#endif
6750
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751/*
6752 * Put string '*text' on the screen at position 'row' and 'col', with
6753 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6754 * Note: only outputs within one row, message is truncated at screen boundary!
6755 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6756 */
6757 void
6758screen_puts(text, row, col, attr)
6759 char_u *text;
6760 int row;
6761 int col;
6762 int attr;
6763{
6764 screen_puts_len(text, -1, row, col, attr);
6765}
6766
6767/*
6768 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6769 * a NUL.
6770 */
6771 void
6772screen_puts_len(text, len, row, col, attr)
6773 char_u *text;
6774 int len;
6775 int row;
6776 int col;
6777 int attr;
6778{
6779 unsigned off;
6780 char_u *ptr = text;
6781 int c;
6782#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006783 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 int mbyte_blen = 1;
6785 int mbyte_cells = 1;
6786 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006787 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 int clear_next_cell = FALSE;
6789# ifdef FEAT_ARABIC
6790 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006791 int pc, nc, nc1;
6792 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793# endif
6794#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006795#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6796 int force_redraw_this;
6797 int force_redraw_next = FALSE;
6798#endif
6799 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
6801 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6802 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006803 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
Bram Moolenaarc236c162008-07-13 17:41:49 +00006805#ifdef FEAT_MBYTE
6806 /* When drawing over the right halve of a double-wide char clear out the
6807 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006808 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006809# ifdef FEAT_GUI
6810 && !gui.in_use
6811# endif
6812 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006813 {
6814 ScreenLines[off - 1] = ' ';
6815 ScreenAttrs[off - 1] = 0;
6816 if (enc_utf8)
6817 {
6818 ScreenLinesUC[off - 1] = 0;
6819 ScreenLinesC[0][off - 1] = 0;
6820 }
6821 /* redraw the previous cell, make it empty */
6822 screen_char(off - 1, row, col - 1);
6823 /* force the cell at "col" to be redrawn */
6824 force_redraw_next = TRUE;
6825 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006826#endif
6827
Bram Moolenaar367329b2007-08-30 11:53:22 +00006828#ifdef FEAT_MBYTE
6829 max_off = LineOffset[row] + screen_Columns;
6830#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006831 while (col < screen_Columns
6832 && (len < 0 || (int)(ptr - text) < len)
6833 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {
6835 c = *ptr;
6836#ifdef FEAT_MBYTE
6837 /* check if this is the first byte of a multibyte */
6838 if (has_mbyte)
6839 {
6840 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006841 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006843 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6845 mbyte_cells = 1;
6846 else if (enc_dbcs != 0)
6847 mbyte_cells = mbyte_blen;
6848 else /* enc_utf8 */
6849 {
6850 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006851 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 (int)((text + len) - ptr));
6853 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006854 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006856# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 /* Non-BMP character: display as ? or fullwidth ?. */
6858 if (u8c >= 0x10000)
6859 {
6860 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6861 if (attr == 0)
6862 attr = hl_attr(HLF_8);
6863 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865# ifdef FEAT_ARABIC
6866 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6867 {
6868 /* Do Arabic shaping. */
6869 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6870 {
6871 /* Past end of string to be displayed. */
6872 nc = NUL;
6873 nc1 = NUL;
6874 }
6875 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006876 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006877 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6878 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006879 nc1 = pcc[0];
6880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 pc = prev_c;
6882 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006883 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
6885 else
6886 prev_c = u8c;
6887# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006888 if (col + mbyte_cells > screen_Columns)
6889 {
6890 /* Only 1 cell left, but character requires 2 cells:
6891 * display a '>' in the last column to avoid wrapping. */
6892 c = '>';
6893 mbyte_cells = 1;
6894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 }
6896 }
6897#endif
6898
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006899#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6900 force_redraw_this = force_redraw_next;
6901 force_redraw_next = FALSE;
6902#endif
6903
6904 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905#ifdef FEAT_MBYTE
6906 || (mbyte_cells == 2
6907 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6908 || (enc_dbcs == DBCS_JPNU
6909 && c == 0x8e
6910 && ScreenLines2[off] != ptr[1])
6911 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006912 && (ScreenLinesUC[off] !=
6913 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6914 || (ScreenLinesUC[off] != 0
6915 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916#endif
6917 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006918 || exmode_active;
6919
6920 if (need_redraw
6921#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6922 || force_redraw_this
6923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 )
6925 {
6926#if defined(FEAT_GUI) || defined(UNIX)
6927 /* The bold trick makes a single row of pixels appear in the next
6928 * character. When a bold character is removed, the next
6929 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006930 * and for some xterms. */
6931 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932# ifdef FEAT_GUI
6933 gui.in_use
6934# endif
6935# if defined(FEAT_GUI) && defined(UNIX)
6936 ||
6937# endif
6938# ifdef UNIX
6939 term_is_xterm
6940# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006941 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006943 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006945 if (n > HL_ALL)
6946 n = syn_attr2attr(n);
6947 if (n & HL_BOLD)
6948 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 }
6950#endif
6951#ifdef FEAT_MBYTE
6952 /* When at the end of the text and overwriting a two-cell
6953 * character with a one-cell character, need to clear the next
6954 * cell. Also when overwriting the left halve of a two-cell char
6955 * with the right halve of a two-cell char. Do this only once
6956 * (mb_off2cells() may return 2 on the right halve). */
6957 if (clear_next_cell)
6958 clear_next_cell = FALSE;
6959 else if (has_mbyte
6960 && (len < 0 ? ptr[mbyte_blen] == NUL
6961 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006962 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006964 && (*mb_off2cells)(off, max_off) == 1
6965 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 clear_next_cell = TRUE;
6967
6968 /* Make sure we never leave a second byte of a double-byte behind,
6969 * it confuses mb_off2cells(). */
6970 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006971 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006973 && (*mb_off2cells)(off, max_off) == 1
6974 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 ScreenLines[off + mbyte_blen] = 0;
6976#endif
6977 ScreenLines[off] = c;
6978 ScreenAttrs[off] = attr;
6979#ifdef FEAT_MBYTE
6980 if (enc_utf8)
6981 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006982 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 ScreenLinesUC[off] = 0;
6984 else
6985 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006986 int i;
6987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006989 for (i = 0; i < Screen_mco; ++i)
6990 {
6991 ScreenLinesC[i][off] = u8cc[i];
6992 if (u8cc[i] == 0)
6993 break;
6994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
6996 if (mbyte_cells == 2)
6997 {
6998 ScreenLines[off + 1] = 0;
6999 ScreenAttrs[off + 1] = attr;
7000 }
7001 screen_char(off, row, col);
7002 }
7003 else if (mbyte_cells == 2)
7004 {
7005 ScreenLines[off + 1] = ptr[1];
7006 ScreenAttrs[off + 1] = attr;
7007 screen_char_2(off, row, col);
7008 }
7009 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7010 {
7011 ScreenLines2[off] = ptr[1];
7012 screen_char(off, row, col);
7013 }
7014 else
7015#endif
7016 screen_char(off, row, col);
7017 }
7018#ifdef FEAT_MBYTE
7019 if (has_mbyte)
7020 {
7021 off += mbyte_cells;
7022 col += mbyte_cells;
7023 ptr += mbyte_blen;
7024 if (clear_next_cell)
7025 ptr = (char_u *)" ";
7026 }
7027 else
7028#endif
7029 {
7030 ++off;
7031 ++col;
7032 ++ptr;
7033 }
7034 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007035
7036#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7037 /* If we detected the next character needs to be redrawn, but the text
7038 * doesn't extend up to there, update the character here. */
7039 if (force_redraw_next && col < screen_Columns)
7040 {
7041# ifdef FEAT_MBYTE
7042 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7043 screen_char_2(off, row, col);
7044 else
7045# endif
7046 screen_char(off, row, col);
7047 }
7048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049}
7050
7051#ifdef FEAT_SEARCH_EXTRA
7052/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007053 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 */
7055 static void
7056start_search_hl()
7057{
7058 if (p_hls && !no_hlsearch)
7059 {
7060 last_pat_prog(&search_hl.rm);
7061 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007062# ifdef FEAT_RELTIME
7063 /* Set the time limit to 'redrawtime'. */
7064 profile_setlimit(p_rdt, &search_hl.tm);
7065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
7067}
7068
7069/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007070 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 */
7072 static void
7073end_search_hl()
7074{
7075 if (search_hl.rm.regprog != NULL)
7076 {
7077 vim_free(search_hl.rm.regprog);
7078 search_hl.rm.regprog = NULL;
7079 }
7080}
7081
7082/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007083 * Init for calling prepare_search_hl().
7084 */
7085 static void
7086init_search_hl(wp)
7087 win_T *wp;
7088{
7089 matchitem_T *cur;
7090
7091 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7092 * match */
7093 cur = wp->w_match_head;
7094 while (cur != NULL)
7095 {
7096 cur->hl.rm = cur->match;
7097 if (cur->hlg_id == 0)
7098 cur->hl.attr = 0;
7099 else
7100 cur->hl.attr = syn_id2attr(cur->hlg_id);
7101 cur->hl.buf = wp->w_buffer;
7102 cur->hl.lnum = 0;
7103 cur->hl.first_lnum = 0;
7104# ifdef FEAT_RELTIME
7105 /* Set the time limit to 'redrawtime'. */
7106 profile_setlimit(p_rdt, &(cur->hl.tm));
7107# endif
7108 cur = cur->next;
7109 }
7110 search_hl.buf = wp->w_buffer;
7111 search_hl.lnum = 0;
7112 search_hl.first_lnum = 0;
7113 /* time limit is set at the toplevel, for all windows */
7114}
7115
7116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 * Advance to the match in window "wp" line "lnum" or past it.
7118 */
7119 static void
7120prepare_search_hl(wp, lnum)
7121 win_T *wp;
7122 linenr_T lnum;
7123{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007124 matchitem_T *cur; /* points to the match list */
7125 match_T *shl; /* points to search_hl or a match */
7126 int shl_flag; /* flag to indicate whether search_hl
7127 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 int n;
7129
7130 /*
7131 * When using a multi-line pattern, start searching at the top
7132 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007133 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007135 cur = wp->w_match_head;
7136 shl_flag = FALSE;
7137 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007139 if (shl_flag == FALSE)
7140 {
7141 shl = &search_hl;
7142 shl_flag = TRUE;
7143 }
7144 else
7145 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (shl->rm.regprog != NULL
7147 && shl->lnum == 0
7148 && re_multiline(shl->rm.regprog))
7149 {
7150 if (shl->first_lnum == 0)
7151 {
7152# ifdef FEAT_FOLDING
7153 for (shl->first_lnum = lnum;
7154 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7155 if (hasFoldingWin(wp, shl->first_lnum - 1,
7156 NULL, NULL, TRUE, NULL))
7157 break;
7158# else
7159 shl->first_lnum = wp->w_topline;
7160# endif
7161 }
7162 n = 0;
7163 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7164 {
7165 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7166 if (shl->lnum != 0)
7167 {
7168 shl->first_lnum = shl->lnum
7169 + shl->rm.endpos[0].lnum
7170 - shl->rm.startpos[0].lnum;
7171 n = shl->rm.endpos[0].col;
7172 }
7173 else
7174 {
7175 ++shl->first_lnum;
7176 n = 0;
7177 }
7178 }
7179 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007180 if (shl != &search_hl && cur != NULL)
7181 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 }
7183}
7184
7185/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007186 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 * Uses shl->buf.
7188 * Sets shl->lnum and shl->rm contents.
7189 * Note: Assumes a previous match is always before "lnum", unless
7190 * shl->lnum is zero.
7191 * Careful: Any pointers for buffer lines will become invalid.
7192 */
7193 static void
7194next_search_hl(win, shl, lnum, mincol)
7195 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007196 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 linenr_T lnum;
7198 colnr_T mincol; /* minimal column for a match */
7199{
7200 linenr_T l;
7201 colnr_T matchcol;
7202 long nmatched;
7203
7204 if (shl->lnum != 0)
7205 {
7206 /* Check for three situations:
7207 * 1. If the "lnum" is below a previous match, start a new search.
7208 * 2. If the previous match includes "mincol", use it.
7209 * 3. Continue after the previous match.
7210 */
7211 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7212 if (lnum > l)
7213 shl->lnum = 0;
7214 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7215 return;
7216 }
7217
7218 /*
7219 * Repeat searching for a match until one is found that includes "mincol"
7220 * or none is found in this line.
7221 */
7222 called_emsg = FALSE;
7223 for (;;)
7224 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007225#ifdef FEAT_RELTIME
7226 /* Stop searching after passing the time limit. */
7227 if (profile_passed_limit(&(shl->tm)))
7228 {
7229 shl->lnum = 0; /* no match found in time */
7230 break;
7231 }
7232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 /* Three situations:
7234 * 1. No useful previous match: search from start of line.
7235 * 2. Not Vi compatible or empty match: continue at next character.
7236 * Break the loop if this is beyond the end of the line.
7237 * 3. Vi compatible searching: continue at end of previous match.
7238 */
7239 if (shl->lnum == 0)
7240 matchcol = 0;
7241 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7242 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007243 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007245 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007246
7247 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007248 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007249 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007251 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 shl->lnum = 0;
7253 break;
7254 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007255#ifdef FEAT_MBYTE
7256 if (has_mbyte)
7257 matchcol += mb_ptr2len(ml);
7258 else
7259#endif
7260 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
7262 else
7263 matchcol = shl->rm.endpos[0].col;
7264
7265 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007266 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7267#ifdef FEAT_RELTIME
7268 &(shl->tm)
7269#else
7270 NULL
7271#endif
7272 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007273 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {
7275 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007276 if (shl == &search_hl)
7277 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007278 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007279 vim_free(shl->rm.regprog);
7280 no_hlsearch = TRUE;
7281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007283 shl->lnum = 0;
7284 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 break;
7286 }
7287 if (nmatched == 0)
7288 {
7289 shl->lnum = 0; /* no match found */
7290 break;
7291 }
7292 if (shl->rm.startpos[0].lnum > 0
7293 || shl->rm.startpos[0].col >= mincol
7294 || nmatched > 1
7295 || shl->rm.endpos[0].col > mincol)
7296 {
7297 shl->lnum += shl->rm.startpos[0].lnum;
7298 break; /* useful match found */
7299 }
7300 }
7301}
7302#endif
7303
7304 static void
7305screen_start_highlight(attr)
7306 int attr;
7307{
7308 attrentry_T *aep = NULL;
7309
7310 screen_attr = attr;
7311 if (full_screen
7312#ifdef WIN3264
7313 && termcap_active
7314#endif
7315 )
7316 {
7317#ifdef FEAT_GUI
7318 if (gui.in_use)
7319 {
7320 char buf[20];
7321
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007322 /* The GUI handles this internally. */
7323 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 OUT_STR(buf);
7325 }
7326 else
7327#endif
7328 {
7329 if (attr > HL_ALL) /* special HL attr. */
7330 {
7331 if (t_colors > 1)
7332 aep = syn_cterm_attr2entry(attr);
7333 else
7334 aep = syn_term_attr2entry(attr);
7335 if (aep == NULL) /* did ":syntax clear" */
7336 attr = 0;
7337 else
7338 attr = aep->ae_attr;
7339 }
7340 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7341 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007342 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7343 && cterm_normal_fg_bold)
7344 /* If the Normal FG color has BOLD attribute and the new HL
7345 * has a FG color defined, clear BOLD. */
7346 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7348 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007349 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7350 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 out_str(T_US);
7352 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7353 out_str(T_CZH);
7354 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7355 out_str(T_MR);
7356
7357 /*
7358 * Output the color or start string after bold etc., in case the
7359 * bold etc. override the color setting.
7360 */
7361 if (aep != NULL)
7362 {
7363 if (t_colors > 1)
7364 {
7365 if (aep->ae_u.cterm.fg_color)
7366 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7367 if (aep->ae_u.cterm.bg_color)
7368 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7369 }
7370 else
7371 {
7372 if (aep->ae_u.term.start != NULL)
7373 out_str(aep->ae_u.term.start);
7374 }
7375 }
7376 }
7377 }
7378}
7379
7380 void
7381screen_stop_highlight()
7382{
7383 int do_ME = FALSE; /* output T_ME code */
7384
7385 if (screen_attr != 0
7386#ifdef WIN3264
7387 && termcap_active
7388#endif
7389 )
7390 {
7391#ifdef FEAT_GUI
7392 if (gui.in_use)
7393 {
7394 char buf[20];
7395
7396 /* use internal GUI code */
7397 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7398 OUT_STR(buf);
7399 }
7400 else
7401#endif
7402 {
7403 if (screen_attr > HL_ALL) /* special HL attr. */
7404 {
7405 attrentry_T *aep;
7406
7407 if (t_colors > 1)
7408 {
7409 /*
7410 * Assume that t_me restores the original colors!
7411 */
7412 aep = syn_cterm_attr2entry(screen_attr);
7413 if (aep != NULL && (aep->ae_u.cterm.fg_color
7414 || aep->ae_u.cterm.bg_color))
7415 do_ME = TRUE;
7416 }
7417 else
7418 {
7419 aep = syn_term_attr2entry(screen_attr);
7420 if (aep != NULL && aep->ae_u.term.stop != NULL)
7421 {
7422 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7423 do_ME = TRUE;
7424 else
7425 out_str(aep->ae_u.term.stop);
7426 }
7427 }
7428 if (aep == NULL) /* did ":syntax clear" */
7429 screen_attr = 0;
7430 else
7431 screen_attr = aep->ae_attr;
7432 }
7433
7434 /*
7435 * Often all ending-codes are equal to T_ME. Avoid outputting the
7436 * same sequence several times.
7437 */
7438 if (screen_attr & HL_STANDOUT)
7439 {
7440 if (STRCMP(T_SE, T_ME) == 0)
7441 do_ME = TRUE;
7442 else
7443 out_str(T_SE);
7444 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007445 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
7447 if (STRCMP(T_UE, T_ME) == 0)
7448 do_ME = TRUE;
7449 else
7450 out_str(T_UE);
7451 }
7452 if (screen_attr & HL_ITALIC)
7453 {
7454 if (STRCMP(T_CZR, T_ME) == 0)
7455 do_ME = TRUE;
7456 else
7457 out_str(T_CZR);
7458 }
7459 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7460 out_str(T_ME);
7461
7462 if (t_colors > 1)
7463 {
7464 /* set Normal cterm colors */
7465 if (cterm_normal_fg_color != 0)
7466 term_fg_color(cterm_normal_fg_color - 1);
7467 if (cterm_normal_bg_color != 0)
7468 term_bg_color(cterm_normal_bg_color - 1);
7469 if (cterm_normal_fg_bold)
7470 out_str(T_MD);
7471 }
7472 }
7473 }
7474 screen_attr = 0;
7475}
7476
7477/*
7478 * Reset the colors for a cterm. Used when leaving Vim.
7479 * The machine specific code may override this again.
7480 */
7481 void
7482reset_cterm_colors()
7483{
7484 if (t_colors > 1)
7485 {
7486 /* set Normal cterm colors */
7487 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7488 {
7489 out_str(T_OP);
7490 screen_attr = -1;
7491 }
7492 if (cterm_normal_fg_bold)
7493 {
7494 out_str(T_ME);
7495 screen_attr = -1;
7496 }
7497 }
7498}
7499
7500/*
7501 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7502 * using the attributes from ScreenAttrs["off"].
7503 */
7504 static void
7505screen_char(off, row, col)
7506 unsigned off;
7507 int row;
7508 int col;
7509{
7510 int attr;
7511
7512 /* Check for illegal values, just in case (could happen just after
7513 * resizing). */
7514 if (row >= screen_Rows || col >= screen_Columns)
7515 return;
7516
7517 /* Outputting the last character on the screen may scrollup the screen.
7518 * Don't to it! Mark the character invalid (update it when scrolled up) */
7519 if (row == screen_Rows - 1 && col == screen_Columns - 1
7520#ifdef FEAT_RIGHTLEFT
7521 /* account for first command-line character in rightleft mode */
7522 && !cmdmsg_rl
7523#endif
7524 )
7525 {
7526 ScreenAttrs[off] = (sattr_T)-1;
7527 return;
7528 }
7529
7530 /*
7531 * Stop highlighting first, so it's easier to move the cursor.
7532 */
7533#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7534 if (screen_char_attr != 0)
7535 attr = screen_char_attr;
7536 else
7537#endif
7538 attr = ScreenAttrs[off];
7539 if (screen_attr != attr)
7540 screen_stop_highlight();
7541
7542 windgoto(row, col);
7543
7544 if (screen_attr != attr)
7545 screen_start_highlight(attr);
7546
7547#ifdef FEAT_MBYTE
7548 if (enc_utf8 && ScreenLinesUC[off] != 0)
7549 {
7550 char_u buf[MB_MAXBYTES + 1];
7551
7552 /* Convert UTF-8 character to bytes and write it. */
7553
7554 buf[utfc_char2bytes(off, buf)] = NUL;
7555
7556 out_str(buf);
7557 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7558 ++screen_cur_col;
7559 }
7560 else
7561#endif
7562 {
7563#ifdef FEAT_MBYTE
7564 out_flush_check();
7565#endif
7566 out_char(ScreenLines[off]);
7567#ifdef FEAT_MBYTE
7568 /* double-byte character in single-width cell */
7569 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7570 out_char(ScreenLines2[off]);
7571#endif
7572 }
7573
7574 screen_cur_col++;
7575}
7576
7577#ifdef FEAT_MBYTE
7578
7579/*
7580 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7581 * on the screen at position 'row' and 'col'.
7582 * The attributes of the first byte is used for all. This is required to
7583 * output the two bytes of a double-byte character with nothing in between.
7584 */
7585 static void
7586screen_char_2(off, row, col)
7587 unsigned off;
7588 int row;
7589 int col;
7590{
7591 /* Check for illegal values (could be wrong when screen was resized). */
7592 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7593 return;
7594
7595 /* Outputting the last character on the screen may scrollup the screen.
7596 * Don't to it! Mark the character invalid (update it when scrolled up) */
7597 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7598 {
7599 ScreenAttrs[off] = (sattr_T)-1;
7600 return;
7601 }
7602
7603 /* Output the first byte normally (positions the cursor), then write the
7604 * second byte directly. */
7605 screen_char(off, row, col);
7606 out_char(ScreenLines[off + 1]);
7607 ++screen_cur_col;
7608}
7609#endif
7610
7611#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7612/*
7613 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7614 * This uses the contents of ScreenLines[] and doesn't change it.
7615 */
7616 void
7617screen_draw_rectangle(row, col, height, width, invert)
7618 int row;
7619 int col;
7620 int height;
7621 int width;
7622 int invert;
7623{
7624 int r, c;
7625 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007626#ifdef FEAT_MBYTE
7627 int max_off;
7628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007630 /* Can't use ScreenLines unless initialized */
7631 if (ScreenLines == NULL)
7632 return;
7633
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 if (invert)
7635 screen_char_attr = HL_INVERSE;
7636 for (r = row; r < row + height; ++r)
7637 {
7638 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007639#ifdef FEAT_MBYTE
7640 max_off = off + screen_Columns;
7641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 for (c = col; c < col + width; ++c)
7643 {
7644#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007645 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {
7647 screen_char_2(off + c, r, c);
7648 ++c;
7649 }
7650 else
7651#endif
7652 {
7653 screen_char(off + c, r, c);
7654#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007655 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 ++c;
7657#endif
7658 }
7659 }
7660 }
7661 screen_char_attr = 0;
7662}
7663#endif
7664
7665#ifdef FEAT_VERTSPLIT
7666/*
7667 * Redraw the characters for a vertically split window.
7668 */
7669 static void
7670redraw_block(row, end, wp)
7671 int row;
7672 int end;
7673 win_T *wp;
7674{
7675 int col;
7676 int width;
7677
7678# ifdef FEAT_CLIPBOARD
7679 clip_may_clear_selection(row, end - 1);
7680# endif
7681
7682 if (wp == NULL)
7683 {
7684 col = 0;
7685 width = Columns;
7686 }
7687 else
7688 {
7689 col = wp->w_wincol;
7690 width = wp->w_width;
7691 }
7692 screen_draw_rectangle(row, col, end - row, width, FALSE);
7693}
7694#endif
7695
7696/*
7697 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7698 * with character 'c1' in first column followed by 'c2' in the other columns.
7699 * Use attributes 'attr'.
7700 */
7701 void
7702screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7703 int start_row, end_row;
7704 int start_col, end_col;
7705 int c1, c2;
7706 int attr;
7707{
7708 int row;
7709 int col;
7710 int off;
7711 int end_off;
7712 int did_delete;
7713 int c;
7714 int norm_term;
7715#if defined(FEAT_GUI) || defined(UNIX)
7716 int force_next = FALSE;
7717#endif
7718
7719 if (end_row > screen_Rows) /* safety check */
7720 end_row = screen_Rows;
7721 if (end_col > screen_Columns) /* safety check */
7722 end_col = screen_Columns;
7723 if (ScreenLines == NULL
7724 || start_row >= end_row
7725 || start_col >= end_col) /* nothing to do */
7726 return;
7727
7728 /* it's a "normal" terminal when not in a GUI or cterm */
7729 norm_term = (
7730#ifdef FEAT_GUI
7731 !gui.in_use &&
7732#endif
7733 t_colors <= 1);
7734 for (row = start_row; row < end_row; ++row)
7735 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007736#ifdef FEAT_MBYTE
7737 if (has_mbyte
7738# ifdef FEAT_GUI
7739 && !gui.in_use
7740# endif
7741 )
7742 {
7743 /* When drawing over the right halve of a double-wide char clear
7744 * out the left halve. When drawing over the left halve of a
7745 * double wide-char clear out the right halve. Only needed in a
7746 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007747 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007748 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007749 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007750 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007751 }
7752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 /*
7754 * Try to use delete-line termcap code, when no attributes or in a
7755 * "normal" terminal, where a bold/italic space is just a
7756 * space.
7757 */
7758 did_delete = FALSE;
7759 if (c2 == ' '
7760 && end_col == Columns
7761 && can_clear(T_CE)
7762 && (attr == 0
7763 || (norm_term
7764 && attr <= HL_ALL
7765 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7766 {
7767 /*
7768 * check if we really need to clear something
7769 */
7770 col = start_col;
7771 if (c1 != ' ') /* don't clear first char */
7772 ++col;
7773
7774 off = LineOffset[row] + col;
7775 end_off = LineOffset[row] + end_col;
7776
7777 /* skip blanks (used often, keep it fast!) */
7778#ifdef FEAT_MBYTE
7779 if (enc_utf8)
7780 while (off < end_off && ScreenLines[off] == ' '
7781 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7782 ++off;
7783 else
7784#endif
7785 while (off < end_off && ScreenLines[off] == ' '
7786 && ScreenAttrs[off] == 0)
7787 ++off;
7788 if (off < end_off) /* something to be cleared */
7789 {
7790 col = off - LineOffset[row];
7791 screen_stop_highlight();
7792 term_windgoto(row, col);/* clear rest of this screen line */
7793 out_str(T_CE);
7794 screen_start(); /* don't know where cursor is now */
7795 col = end_col - col;
7796 while (col--) /* clear chars in ScreenLines */
7797 {
7798 ScreenLines[off] = ' ';
7799#ifdef FEAT_MBYTE
7800 if (enc_utf8)
7801 ScreenLinesUC[off] = 0;
7802#endif
7803 ScreenAttrs[off] = 0;
7804 ++off;
7805 }
7806 }
7807 did_delete = TRUE; /* the chars are cleared now */
7808 }
7809
7810 off = LineOffset[row] + start_col;
7811 c = c1;
7812 for (col = start_col; col < end_col; ++col)
7813 {
7814 if (ScreenLines[off] != c
7815#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007816 || (enc_utf8 && (int)ScreenLinesUC[off]
7817 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818#endif
7819 || ScreenAttrs[off] != attr
7820#if defined(FEAT_GUI) || defined(UNIX)
7821 || force_next
7822#endif
7823 )
7824 {
7825#if defined(FEAT_GUI) || defined(UNIX)
7826 /* The bold trick may make a single row of pixels appear in
7827 * the next character. When a bold character is removed, the
7828 * next character should be redrawn too. This happens for our
7829 * own GUI and for some xterms. */
7830 if (
7831# ifdef FEAT_GUI
7832 gui.in_use
7833# endif
7834# if defined(FEAT_GUI) && defined(UNIX)
7835 ||
7836# endif
7837# ifdef UNIX
7838 term_is_xterm
7839# endif
7840 )
7841 {
7842 if (ScreenLines[off] != ' '
7843 && (ScreenAttrs[off] > HL_ALL
7844 || ScreenAttrs[off] & HL_BOLD))
7845 force_next = TRUE;
7846 else
7847 force_next = FALSE;
7848 }
7849#endif
7850 ScreenLines[off] = c;
7851#ifdef FEAT_MBYTE
7852 if (enc_utf8)
7853 {
7854 if (c >= 0x80)
7855 {
7856 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007857 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 }
7859 else
7860 ScreenLinesUC[off] = 0;
7861 }
7862#endif
7863 ScreenAttrs[off] = attr;
7864 if (!did_delete || c != ' ')
7865 screen_char(off, row, col);
7866 }
7867 ++off;
7868 if (col == start_col)
7869 {
7870 if (did_delete)
7871 break;
7872 c = c2;
7873 }
7874 }
7875 if (end_col == Columns)
7876 LineWraps[row] = FALSE;
7877 if (row == Rows - 1) /* overwritten the command line */
7878 {
7879 redraw_cmdline = TRUE;
7880 if (c1 == ' ' && c2 == ' ')
7881 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007882 if (start_col == 0)
7883 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 }
7885 }
7886}
7887
7888/*
7889 * Check if there should be a delay. Used before clearing or redrawing the
7890 * screen or the command line.
7891 */
7892 void
7893check_for_delay(check_msg_scroll)
7894 int check_msg_scroll;
7895{
7896 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7897 && !did_wait_return
7898 && emsg_silent == 0)
7899 {
7900 out_flush();
7901 ui_delay(1000L, TRUE);
7902 emsg_on_display = FALSE;
7903 if (check_msg_scroll)
7904 msg_scroll = FALSE;
7905 }
7906}
7907
7908/*
7909 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007910 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 * Returns TRUE if there is a valid screen to write to.
7912 * Returns FALSE when starting up and screen not initialized yet.
7913 */
7914 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007915screen_valid(doclear)
7916 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007918 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 return (ScreenLines != NULL);
7920}
7921
7922/*
7923 * Resize the shell to Rows and Columns.
7924 * Allocate ScreenLines[] and associated items.
7925 *
7926 * There may be some time between setting Rows and Columns and (re)allocating
7927 * ScreenLines[]. This happens when starting up and when (manually) changing
7928 * the shell size. Always use screen_Rows and screen_Columns to access items
7929 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7930 * final size of the shell is needed.
7931 */
7932 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007933screenalloc(doclear)
7934 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 int new_row, old_row;
7937#ifdef FEAT_GUI
7938 int old_Rows;
7939#endif
7940 win_T *wp;
7941 int outofmem = FALSE;
7942 int len;
7943 schar_T *new_ScreenLines;
7944#ifdef FEAT_MBYTE
7945 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007946 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007948 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949#endif
7950 sattr_T *new_ScreenAttrs;
7951 unsigned *new_LineOffset;
7952 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007953#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007954 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007955 tabpage_T *tp;
7956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007958 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007959#ifdef FEAT_AUTOCMD
7960 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007962retry:
7963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 /*
7965 * Allocation of the screen buffers is done only when the size changes and
7966 * when Rows and Columns have been set and we have started doing full
7967 * screen stuff.
7968 */
7969 if ((ScreenLines != NULL
7970 && Rows == screen_Rows
7971 && Columns == screen_Columns
7972#ifdef FEAT_MBYTE
7973 && enc_utf8 == (ScreenLinesUC != NULL)
7974 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007975 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976#endif
7977 )
7978 || Rows == 0
7979 || Columns == 0
7980 || (!full_screen && ScreenLines == NULL))
7981 return;
7982
7983 /*
7984 * It's possible that we produce an out-of-memory message below, which
7985 * will cause this function to be called again. To break the loop, just
7986 * return here.
7987 */
7988 if (entered)
7989 return;
7990 entered = TRUE;
7991
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007992 /*
7993 * Note that the window sizes are updated before reallocating the arrays,
7994 * thus we must not redraw here!
7995 */
7996 ++RedrawingDisabled;
7997
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 win_new_shellsize(); /* fit the windows in the new sized shell */
7999
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 comp_col(); /* recompute columns for shown command and ruler */
8001
8002 /*
8003 * We're changing the size of the screen.
8004 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8005 * - Move lines from the old arrays into the new arrays, clear extra
8006 * lines (unless the screen is going to be cleared).
8007 * - Free the old arrays.
8008 *
8009 * If anything fails, make ScreenLines NULL, so we don't do anything!
8010 * Continuing with the old ScreenLines may result in a crash, because the
8011 * size is wrong.
8012 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008013 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008015#ifdef FEAT_AUTOCMD
8016 if (aucmd_win != NULL)
8017 win_free_lsize(aucmd_win);
8018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020 new_ScreenLines = (schar_T *)lalloc((long_u)(
8021 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8022#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008023 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 if (enc_utf8)
8025 {
8026 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8027 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008028 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008029 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8031 }
8032 if (enc_dbcs == DBCS_JPNU)
8033 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8034 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8035#endif
8036 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8037 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8038 new_LineOffset = (unsigned *)lalloc((long_u)(
8039 Rows * sizeof(unsigned)), FALSE);
8040 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008041#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008042 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008045 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 {
8047 if (win_alloc_lines(wp) == FAIL)
8048 {
8049 outofmem = TRUE;
8050#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008051 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052#endif
8053 }
8054 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008055#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008056 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8057 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008058 outofmem = TRUE;
8059#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008060#ifdef FEAT_WINDOWS
8061give_up:
8062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008064#ifdef FEAT_MBYTE
8065 for (i = 0; i < p_mco; ++i)
8066 if (new_ScreenLinesC[i] == NULL)
8067 break;
8068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 if (new_ScreenLines == NULL
8070#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008071 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8073#endif
8074 || new_ScreenAttrs == NULL
8075 || new_LineOffset == NULL
8076 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008077#ifdef FEAT_WINDOWS
8078 || new_TabPageIdxs == NULL
8079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 || outofmem)
8081 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008082 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008083 {
8084 /* guess the size */
8085 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8086
8087 /* Remember we did this to avoid getting outofmem messages over
8088 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008089 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 vim_free(new_ScreenLines);
8092 new_ScreenLines = NULL;
8093#ifdef FEAT_MBYTE
8094 vim_free(new_ScreenLinesUC);
8095 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008096 for (i = 0; i < p_mco; ++i)
8097 {
8098 vim_free(new_ScreenLinesC[i]);
8099 new_ScreenLinesC[i] = NULL;
8100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 vim_free(new_ScreenLines2);
8102 new_ScreenLines2 = NULL;
8103#endif
8104 vim_free(new_ScreenAttrs);
8105 new_ScreenAttrs = NULL;
8106 vim_free(new_LineOffset);
8107 new_LineOffset = NULL;
8108 vim_free(new_LineWraps);
8109 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008110#ifdef FEAT_WINDOWS
8111 vim_free(new_TabPageIdxs);
8112 new_TabPageIdxs = NULL;
8113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 }
8115 else
8116 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008117 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008118
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 for (new_row = 0; new_row < Rows; ++new_row)
8120 {
8121 new_LineOffset[new_row] = new_row * Columns;
8122 new_LineWraps[new_row] = FALSE;
8123
8124 /*
8125 * If the screen is not going to be cleared, copy as much as
8126 * possible from the old screen to the new one and clear the rest
8127 * (used when resizing the window at the "--more--" prompt or when
8128 * executing an external command, for the GUI).
8129 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008130 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {
8132 (void)vim_memset(new_ScreenLines + new_row * Columns,
8133 ' ', (size_t)Columns * sizeof(schar_T));
8134#ifdef FEAT_MBYTE
8135 if (enc_utf8)
8136 {
8137 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8138 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008139 for (i = 0; i < p_mco; ++i)
8140 (void)vim_memset(new_ScreenLinesC[i]
8141 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 0, (size_t)Columns * sizeof(u8char_T));
8143 }
8144 if (enc_dbcs == DBCS_JPNU)
8145 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8146 0, (size_t)Columns * sizeof(schar_T));
8147#endif
8148 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8149 0, (size_t)Columns * sizeof(sattr_T));
8150 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008151 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {
8153 if (screen_Columns < Columns)
8154 len = screen_Columns;
8155 else
8156 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008157#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008158 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008159 * may be invalid now. Also when p_mco changes. */
8160 if (!(enc_utf8 && ScreenLinesUC == NULL)
8161 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008162#endif
8163 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8164 ScreenLines + LineOffset[old_row],
8165 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008167 if (enc_utf8 && ScreenLinesUC != NULL
8168 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {
8170 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8171 ScreenLinesUC + LineOffset[old_row],
8172 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008173 for (i = 0; i < p_mco; ++i)
8174 mch_memmove(new_ScreenLinesC[i]
8175 + new_LineOffset[new_row],
8176 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 (size_t)len * sizeof(u8char_T));
8178 }
8179 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8180 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8181 ScreenLines2 + LineOffset[old_row],
8182 (size_t)len * sizeof(schar_T));
8183#endif
8184 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8185 ScreenAttrs + LineOffset[old_row],
8186 (size_t)len * sizeof(sattr_T));
8187 }
8188 }
8189 }
8190 /* Use the last line of the screen for the current line. */
8191 current_ScreenLine = new_ScreenLines + Rows * Columns;
8192 }
8193
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008194 free_screenlines();
8195
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 ScreenLines = new_ScreenLines;
8197#ifdef FEAT_MBYTE
8198 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008199 for (i = 0; i < p_mco; ++i)
8200 ScreenLinesC[i] = new_ScreenLinesC[i];
8201 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 ScreenLines2 = new_ScreenLines2;
8203#endif
8204 ScreenAttrs = new_ScreenAttrs;
8205 LineOffset = new_LineOffset;
8206 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008207#ifdef FEAT_WINDOWS
8208 TabPageIdxs = new_TabPageIdxs;
8209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210
8211 /* It's important that screen_Rows and screen_Columns reflect the actual
8212 * size of ScreenLines[]. Set them before calling anything. */
8213#ifdef FEAT_GUI
8214 old_Rows = screen_Rows;
8215#endif
8216 screen_Rows = Rows;
8217 screen_Columns = Columns;
8218
8219 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008220 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 screenclear2();
8222
8223#ifdef FEAT_GUI
8224 else if (gui.in_use
8225 && !gui.starting
8226 && ScreenLines != NULL
8227 && old_Rows != Rows)
8228 {
8229 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8230 /*
8231 * Adjust the position of the cursor, for when executing an external
8232 * command.
8233 */
8234 if (msg_row >= Rows) /* Rows got smaller */
8235 msg_row = Rows - 1; /* put cursor at last row */
8236 else if (Rows > old_Rows) /* Rows got bigger */
8237 msg_row += Rows - old_Rows; /* put cursor in same place */
8238 if (msg_col >= Columns) /* Columns got smaller */
8239 msg_col = Columns - 1; /* put cursor at last column */
8240 }
8241#endif
8242
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008244 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008245
8246#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008247 /*
8248 * Do not apply autocommands more than 3 times to avoid an endless loop
8249 * in case applying autocommands always changes Rows or Columns.
8250 */
8251 if (starting == 0 && ++retry_count <= 3)
8252 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008253 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008254 /* In rare cases, autocommands may have altered Rows or Columns,
8255 * jump back to check if we need to allocate the screen again. */
8256 goto retry;
8257 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259}
8260
8261 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008262free_screenlines()
8263{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008264#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008265 int i;
8266
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008267 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008268 for (i = 0; i < Screen_mco; ++i)
8269 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008270 vim_free(ScreenLines2);
8271#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008272 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008273 vim_free(ScreenAttrs);
8274 vim_free(LineOffset);
8275 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008276#ifdef FEAT_WINDOWS
8277 vim_free(TabPageIdxs);
8278#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008279}
8280
8281 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282screenclear()
8283{
8284 check_for_delay(FALSE);
8285 screenalloc(FALSE); /* allocate screen buffers if size changed */
8286 screenclear2(); /* clear the screen */
8287}
8288
8289 static void
8290screenclear2()
8291{
8292 int i;
8293
8294 if (starting == NO_SCREEN || ScreenLines == NULL
8295#ifdef FEAT_GUI
8296 || (gui.in_use && gui.starting)
8297#endif
8298 )
8299 return;
8300
8301#ifdef FEAT_GUI
8302 if (!gui.in_use)
8303#endif
8304 screen_attr = -1; /* force setting the Normal colors */
8305 screen_stop_highlight(); /* don't want highlighting here */
8306
8307#ifdef FEAT_CLIPBOARD
8308 /* disable selection without redrawing it */
8309 clip_scroll_selection(9999);
8310#endif
8311
8312 /* blank out ScreenLines */
8313 for (i = 0; i < Rows; ++i)
8314 {
8315 lineclear(LineOffset[i], (int)Columns);
8316 LineWraps[i] = FALSE;
8317 }
8318
8319 if (can_clear(T_CL))
8320 {
8321 out_str(T_CL); /* clear the display */
8322 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008323 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325 else
8326 {
8327 /* can't clear the screen, mark all chars with invalid attributes */
8328 for (i = 0; i < Rows; ++i)
8329 lineinvalid(LineOffset[i], (int)Columns);
8330 clear_cmdline = TRUE;
8331 }
8332
8333 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8334
8335 win_rest_invalid(firstwin);
8336 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008337#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008338 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 if (must_redraw == CLEAR) /* no need to clear again */
8341 must_redraw = NOT_VALID;
8342 compute_cmdrow();
8343 msg_row = cmdline_row; /* put cursor on last line for messages */
8344 msg_col = 0;
8345 screen_start(); /* don't know where cursor is now */
8346 msg_scrolled = 0; /* can't scroll back */
8347 msg_didany = FALSE;
8348 msg_didout = FALSE;
8349}
8350
8351/*
8352 * Clear one line in ScreenLines.
8353 */
8354 static void
8355lineclear(off, width)
8356 unsigned off;
8357 int width;
8358{
8359 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8360#ifdef FEAT_MBYTE
8361 if (enc_utf8)
8362 (void)vim_memset(ScreenLinesUC + off, 0,
8363 (size_t)width * sizeof(u8char_T));
8364#endif
8365 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8366}
8367
8368/*
8369 * Mark one line in ScreenLines invalid by setting the attributes to an
8370 * invalid value.
8371 */
8372 static void
8373lineinvalid(off, width)
8374 unsigned off;
8375 int width;
8376{
8377 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8378}
8379
8380#ifdef FEAT_VERTSPLIT
8381/*
8382 * Copy part of a Screenline for vertically split window "wp".
8383 */
8384 static void
8385linecopy(to, from, wp)
8386 int to;
8387 int from;
8388 win_T *wp;
8389{
8390 unsigned off_to = LineOffset[to] + wp->w_wincol;
8391 unsigned off_from = LineOffset[from] + wp->w_wincol;
8392
8393 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8394 wp->w_width * sizeof(schar_T));
8395# ifdef FEAT_MBYTE
8396 if (enc_utf8)
8397 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008398 int i;
8399
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8401 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008402 for (i = 0; i < p_mco; ++i)
8403 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8404 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 }
8406 if (enc_dbcs == DBCS_JPNU)
8407 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8408 wp->w_width * sizeof(schar_T));
8409# endif
8410 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8411 wp->w_width * sizeof(sattr_T));
8412}
8413#endif
8414
8415/*
8416 * Return TRUE if clearing with term string "p" would work.
8417 * It can't work when the string is empty or it won't set the right background.
8418 */
8419 int
8420can_clear(p)
8421 char_u *p;
8422{
8423 return (*p != NUL && (t_colors <= 1
8424#ifdef FEAT_GUI
8425 || gui.in_use
8426#endif
8427 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8428}
8429
8430/*
8431 * Reset cursor position. Use whenever cursor was moved because of outputting
8432 * something directly to the screen (shell commands) or a terminal control
8433 * code.
8434 */
8435 void
8436screen_start()
8437{
8438 screen_cur_row = screen_cur_col = 9999;
8439}
8440
8441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 * Move the cursor to position "row","col" in the screen.
8443 * This tries to find the most efficient way to move, minimizing the number of
8444 * characters sent to the terminal.
8445 */
8446 void
8447windgoto(row, col)
8448 int row;
8449 int col;
8450{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008451 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 int i;
8453 int plan;
8454 int cost;
8455 int wouldbe_col;
8456 int noinvcurs;
8457 char_u *bs;
8458 int goto_cost;
8459 int attr;
8460
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008461#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8463
8464#define PLAN_LE 1
8465#define PLAN_CR 2
8466#define PLAN_NL 3
8467#define PLAN_WRITE 4
8468 /* Can't use ScreenLines unless initialized */
8469 if (ScreenLines == NULL)
8470 return;
8471
8472 if (col != screen_cur_col || row != screen_cur_row)
8473 {
8474 /* Check for valid position. */
8475 if (row < 0) /* window without text lines? */
8476 row = 0;
8477 if (row >= screen_Rows)
8478 row = screen_Rows - 1;
8479 if (col >= screen_Columns)
8480 col = screen_Columns - 1;
8481
8482 /* check if no cursor movement is allowed in highlight mode */
8483 if (screen_attr && *T_MS == NUL)
8484 noinvcurs = HIGHL_COST;
8485 else
8486 noinvcurs = 0;
8487 goto_cost = GOTO_COST + noinvcurs;
8488
8489 /*
8490 * Plan how to do the positioning:
8491 * 1. Use CR to move it to column 0, same row.
8492 * 2. Use T_LE to move it a few columns to the left.
8493 * 3. Use NL to move a few lines down, column 0.
8494 * 4. Move a few columns to the right with T_ND or by writing chars.
8495 *
8496 * Don't do this if the cursor went beyond the last column, the cursor
8497 * position is unknown then (some terminals wrap, some don't )
8498 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008499 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 * characters to move the cursor to the right.
8501 */
8502 if (row >= screen_cur_row && screen_cur_col < Columns)
8503 {
8504 /*
8505 * If the cursor is in the same row, bigger col, we can use CR
8506 * or T_LE.
8507 */
8508 bs = NULL; /* init for GCC */
8509 attr = screen_attr;
8510 if (row == screen_cur_row && col < screen_cur_col)
8511 {
8512 /* "le" is preferred over "bc", because "bc" is obsolete */
8513 if (*T_LE)
8514 bs = T_LE; /* "cursor left" */
8515 else
8516 bs = T_BC; /* "backspace character (old) */
8517 if (*bs)
8518 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8519 else
8520 cost = 999;
8521 if (col + 1 < cost) /* using CR is less characters */
8522 {
8523 plan = PLAN_CR;
8524 wouldbe_col = 0;
8525 cost = 1; /* CR is just one character */
8526 }
8527 else
8528 {
8529 plan = PLAN_LE;
8530 wouldbe_col = col;
8531 }
8532 if (noinvcurs) /* will stop highlighting */
8533 {
8534 cost += noinvcurs;
8535 attr = 0;
8536 }
8537 }
8538
8539 /*
8540 * If the cursor is above where we want to be, we can use CR LF.
8541 */
8542 else if (row > screen_cur_row)
8543 {
8544 plan = PLAN_NL;
8545 wouldbe_col = 0;
8546 cost = (row - screen_cur_row) * 2; /* CR LF */
8547 if (noinvcurs) /* will stop highlighting */
8548 {
8549 cost += noinvcurs;
8550 attr = 0;
8551 }
8552 }
8553
8554 /*
8555 * If the cursor is in the same row, smaller col, just use write.
8556 */
8557 else
8558 {
8559 plan = PLAN_WRITE;
8560 wouldbe_col = screen_cur_col;
8561 cost = 0;
8562 }
8563
8564 /*
8565 * Check if any characters that need to be written have the
8566 * correct attributes. Also avoid UTF-8 characters.
8567 */
8568 i = col - wouldbe_col;
8569 if (i > 0)
8570 cost += i;
8571 if (cost < goto_cost && i > 0)
8572 {
8573 /*
8574 * Check if the attributes are correct without additionally
8575 * stopping highlighting.
8576 */
8577 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8578 while (i && *p++ == attr)
8579 --i;
8580 if (i != 0)
8581 {
8582 /*
8583 * Try if it works when highlighting is stopped here.
8584 */
8585 if (*--p == 0)
8586 {
8587 cost += noinvcurs;
8588 while (i && *p++ == 0)
8589 --i;
8590 }
8591 if (i != 0)
8592 cost = 999; /* different attributes, don't do it */
8593 }
8594#ifdef FEAT_MBYTE
8595 if (enc_utf8)
8596 {
8597 /* Don't use an UTF-8 char for positioning, it's slow. */
8598 for (i = wouldbe_col; i < col; ++i)
8599 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8600 {
8601 cost = 999;
8602 break;
8603 }
8604 }
8605#endif
8606 }
8607
8608 /*
8609 * We can do it without term_windgoto()!
8610 */
8611 if (cost < goto_cost)
8612 {
8613 if (plan == PLAN_LE)
8614 {
8615 if (noinvcurs)
8616 screen_stop_highlight();
8617 while (screen_cur_col > col)
8618 {
8619 out_str(bs);
8620 --screen_cur_col;
8621 }
8622 }
8623 else if (plan == PLAN_CR)
8624 {
8625 if (noinvcurs)
8626 screen_stop_highlight();
8627 out_char('\r');
8628 screen_cur_col = 0;
8629 }
8630 else if (plan == PLAN_NL)
8631 {
8632 if (noinvcurs)
8633 screen_stop_highlight();
8634 while (screen_cur_row < row)
8635 {
8636 out_char('\n');
8637 ++screen_cur_row;
8638 }
8639 screen_cur_col = 0;
8640 }
8641
8642 i = col - screen_cur_col;
8643 if (i > 0)
8644 {
8645 /*
8646 * Use cursor-right if it's one character only. Avoids
8647 * removing a line of pixels from the last bold char, when
8648 * using the bold trick in the GUI.
8649 */
8650 if (T_ND[0] != NUL && T_ND[1] == NUL)
8651 {
8652 while (i-- > 0)
8653 out_char(*T_ND);
8654 }
8655 else
8656 {
8657 int off;
8658
8659 off = LineOffset[row] + screen_cur_col;
8660 while (i-- > 0)
8661 {
8662 if (ScreenAttrs[off] != screen_attr)
8663 screen_stop_highlight();
8664#ifdef FEAT_MBYTE
8665 out_flush_check();
8666#endif
8667 out_char(ScreenLines[off]);
8668#ifdef FEAT_MBYTE
8669 if (enc_dbcs == DBCS_JPNU
8670 && ScreenLines[off] == 0x8e)
8671 out_char(ScreenLines2[off]);
8672#endif
8673 ++off;
8674 }
8675 }
8676 }
8677 }
8678 }
8679 else
8680 cost = 999;
8681
8682 if (cost >= goto_cost)
8683 {
8684 if (noinvcurs)
8685 screen_stop_highlight();
8686 if (row == screen_cur_row && (col > screen_cur_col) &&
8687 *T_CRI != NUL)
8688 term_cursor_right(col - screen_cur_col);
8689 else
8690 term_windgoto(row, col);
8691 }
8692 screen_cur_row = row;
8693 screen_cur_col = col;
8694 }
8695}
8696
8697/*
8698 * Set cursor to its position in the current window.
8699 */
8700 void
8701setcursor()
8702{
8703 if (redrawing())
8704 {
8705 validate_cursor();
8706 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8707 W_WINCOL(curwin) + (
8708#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008709 /* With 'rightleft' set and the cursor on a double-wide
8710 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8712# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008713 (has_mbyte
8714 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8715 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716# endif
8717 1)) :
8718#endif
8719 curwin->w_wcol));
8720 }
8721}
8722
8723
8724/*
8725 * insert 'line_count' lines at 'row' in window 'wp'
8726 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8727 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8728 * scrolling.
8729 * Returns FAIL if the lines are not inserted, OK for success.
8730 */
8731 int
8732win_ins_lines(wp, row, line_count, invalid, mayclear)
8733 win_T *wp;
8734 int row;
8735 int line_count;
8736 int invalid;
8737 int mayclear;
8738{
8739 int did_delete;
8740 int nextrow;
8741 int lastrow;
8742 int retval;
8743
8744 if (invalid)
8745 wp->w_lines_valid = 0;
8746
8747 if (wp->w_height < 5)
8748 return FAIL;
8749
8750 if (line_count > wp->w_height - row)
8751 line_count = wp->w_height - row;
8752
8753 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8754 if (retval != MAYBE)
8755 return retval;
8756
8757 /*
8758 * If there is a next window or a status line, we first try to delete the
8759 * lines at the bottom to avoid messing what is after the window.
8760 * If this fails and there are following windows, don't do anything to avoid
8761 * messing up those windows, better just redraw.
8762 */
8763 did_delete = FALSE;
8764#ifdef FEAT_WINDOWS
8765 if (wp->w_next != NULL || wp->w_status_height)
8766 {
8767 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8768 line_count, (int)Rows, FALSE, NULL) == OK)
8769 did_delete = TRUE;
8770 else if (wp->w_next)
8771 return FAIL;
8772 }
8773#endif
8774 /*
8775 * if no lines deleted, blank the lines that will end up below the window
8776 */
8777 if (!did_delete)
8778 {
8779#ifdef FEAT_WINDOWS
8780 wp->w_redr_status = TRUE;
8781#endif
8782 redraw_cmdline = TRUE;
8783 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8784 lastrow = nextrow + line_count;
8785 if (lastrow > Rows)
8786 lastrow = Rows;
8787 screen_fill(nextrow - line_count, lastrow - line_count,
8788 W_WINCOL(wp), (int)W_ENDCOL(wp),
8789 ' ', ' ', 0);
8790 }
8791
8792 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8793 == FAIL)
8794 {
8795 /* deletion will have messed up other windows */
8796 if (did_delete)
8797 {
8798#ifdef FEAT_WINDOWS
8799 wp->w_redr_status = TRUE;
8800#endif
8801 win_rest_invalid(W_NEXT(wp));
8802 }
8803 return FAIL;
8804 }
8805
8806 return OK;
8807}
8808
8809/*
8810 * delete "line_count" window lines at "row" in window "wp"
8811 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8812 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8813 * scrolling
8814 * Return OK for success, FAIL if the lines are not deleted.
8815 */
8816 int
8817win_del_lines(wp, row, line_count, invalid, mayclear)
8818 win_T *wp;
8819 int row;
8820 int line_count;
8821 int invalid;
8822 int mayclear;
8823{
8824 int retval;
8825
8826 if (invalid)
8827 wp->w_lines_valid = 0;
8828
8829 if (line_count > wp->w_height - row)
8830 line_count = wp->w_height - row;
8831
8832 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8833 if (retval != MAYBE)
8834 return retval;
8835
8836 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8837 (int)Rows, FALSE, NULL) == FAIL)
8838 return FAIL;
8839
8840#ifdef FEAT_WINDOWS
8841 /*
8842 * If there are windows or status lines below, try to put them at the
8843 * correct place. If we can't do that, they have to be redrawn.
8844 */
8845 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8846 {
8847 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8848 line_count, (int)Rows, NULL) == FAIL)
8849 {
8850 wp->w_redr_status = TRUE;
8851 win_rest_invalid(wp->w_next);
8852 }
8853 }
8854 /*
8855 * If this is the last window and there is no status line, redraw the
8856 * command line later.
8857 */
8858 else
8859#endif
8860 redraw_cmdline = TRUE;
8861 return OK;
8862}
8863
8864/*
8865 * Common code for win_ins_lines() and win_del_lines().
8866 * Returns OK or FAIL when the work has been done.
8867 * Returns MAYBE when not finished yet.
8868 */
8869 static int
8870win_do_lines(wp, row, line_count, mayclear, del)
8871 win_T *wp;
8872 int row;
8873 int line_count;
8874 int mayclear;
8875 int del;
8876{
8877 int retval;
8878
8879 if (!redrawing() || line_count <= 0)
8880 return FAIL;
8881
8882 /* only a few lines left: redraw is faster */
8883 if (mayclear && Rows - line_count < 5
8884#ifdef FEAT_VERTSPLIT
8885 && wp->w_width == Columns
8886#endif
8887 )
8888 {
8889 screenclear(); /* will set wp->w_lines_valid to 0 */
8890 return FAIL;
8891 }
8892
8893 /*
8894 * Delete all remaining lines
8895 */
8896 if (row + line_count >= wp->w_height)
8897 {
8898 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8899 W_WINCOL(wp), (int)W_ENDCOL(wp),
8900 ' ', ' ', 0);
8901 return OK;
8902 }
8903
8904 /*
8905 * when scrolling, the message on the command line should be cleared,
8906 * otherwise it will stay there forever.
8907 */
8908 clear_cmdline = TRUE;
8909
8910 /*
8911 * If the terminal can set a scroll region, use that.
8912 * Always do this in a vertically split window. This will redraw from
8913 * ScreenLines[] when t_CV isn't defined. That's faster than using
8914 * win_line().
8915 * Don't use a scroll region when we are going to redraw the text, writing
8916 * a character in the lower right corner of the scroll region causes a
8917 * scroll-up in the DJGPP version.
8918 */
8919 if (scroll_region
8920#ifdef FEAT_VERTSPLIT
8921 || W_WIDTH(wp) != Columns
8922#endif
8923 )
8924 {
8925#ifdef FEAT_VERTSPLIT
8926 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8927#endif
8928 scroll_region_set(wp, row);
8929 if (del)
8930 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8931 wp->w_height - row, FALSE, wp);
8932 else
8933 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8934 wp->w_height - row, wp);
8935#ifdef FEAT_VERTSPLIT
8936 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8937#endif
8938 scroll_region_reset();
8939 return retval;
8940 }
8941
8942#ifdef FEAT_WINDOWS
8943 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8944 return FAIL;
8945#endif
8946
8947 return MAYBE;
8948}
8949
8950/*
8951 * window 'wp' and everything after it is messed up, mark it for redraw
8952 */
8953 static void
8954win_rest_invalid(wp)
8955 win_T *wp;
8956{
8957#ifdef FEAT_WINDOWS
8958 while (wp != NULL)
8959#else
8960 if (wp != NULL)
8961#endif
8962 {
8963 redraw_win_later(wp, NOT_VALID);
8964#ifdef FEAT_WINDOWS
8965 wp->w_redr_status = TRUE;
8966 wp = wp->w_next;
8967#endif
8968 }
8969 redraw_cmdline = TRUE;
8970}
8971
8972/*
8973 * The rest of the routines in this file perform screen manipulations. The
8974 * given operation is performed physically on the screen. The corresponding
8975 * change is also made to the internal screen image. In this way, the editor
8976 * anticipates the effect of editing changes on the appearance of the screen.
8977 * That way, when we call screenupdate a complete redraw isn't usually
8978 * necessary. Another advantage is that we can keep adding code to anticipate
8979 * screen changes, and in the meantime, everything still works.
8980 */
8981
8982/*
8983 * types for inserting or deleting lines
8984 */
8985#define USE_T_CAL 1
8986#define USE_T_CDL 2
8987#define USE_T_AL 3
8988#define USE_T_CE 4
8989#define USE_T_DL 5
8990#define USE_T_SR 6
8991#define USE_NL 7
8992#define USE_T_CD 8
8993#define USE_REDRAW 9
8994
8995/*
8996 * insert lines on the screen and update ScreenLines[]
8997 * 'end' is the line after the scrolled part. Normally it is Rows.
8998 * When scrolling region used 'off' is the offset from the top for the region.
8999 * 'row' and 'end' are relative to the start of the region.
9000 *
9001 * return FAIL for failure, OK for success.
9002 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009003 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004screen_ins_lines(off, row, line_count, end, wp)
9005 int off;
9006 int row;
9007 int line_count;
9008 int end;
9009 win_T *wp; /* NULL or window to use width from */
9010{
9011 int i;
9012 int j;
9013 unsigned temp;
9014 int cursor_row;
9015 int type;
9016 int result_empty;
9017 int can_ce = can_clear(T_CE);
9018
9019 /*
9020 * FAIL if
9021 * - there is no valid screen
9022 * - the screen has to be redrawn completely
9023 * - the line count is less than one
9024 * - the line count is more than 'ttyscroll'
9025 */
9026 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9027 return FAIL;
9028
9029 /*
9030 * There are seven ways to insert lines:
9031 * 0. When in a vertically split window and t_CV isn't set, redraw the
9032 * characters from ScreenLines[].
9033 * 1. Use T_CD (clear to end of display) if it exists and the result of
9034 * the insert is just empty lines
9035 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9036 * present or line_count > 1. It looks better if we do all the inserts
9037 * at once.
9038 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9039 * insert is just empty lines and T_CE is not present or line_count >
9040 * 1.
9041 * 4. Use T_AL (insert line) if it exists.
9042 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9043 * just empty lines.
9044 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9045 * just empty lines.
9046 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9047 * the 'da' flag is not set or we have clear line capability.
9048 * 8. redraw the characters from ScreenLines[].
9049 *
9050 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9051 * the scrollbar for the window. It does have insert line, use that if it
9052 * exists.
9053 */
9054 result_empty = (row + line_count >= end);
9055#ifdef FEAT_VERTSPLIT
9056 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9057 type = USE_REDRAW;
9058 else
9059#endif
9060 if (can_clear(T_CD) && result_empty)
9061 type = USE_T_CD;
9062 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9063 type = USE_T_CAL;
9064 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9065 type = USE_T_CDL;
9066 else if (*T_AL != NUL)
9067 type = USE_T_AL;
9068 else if (can_ce && result_empty)
9069 type = USE_T_CE;
9070 else if (*T_DL != NUL && result_empty)
9071 type = USE_T_DL;
9072 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9073 type = USE_T_SR;
9074 else
9075 return FAIL;
9076
9077 /*
9078 * For clearing the lines screen_del_lines() is used. This will also take
9079 * care of t_db if necessary.
9080 */
9081 if (type == USE_T_CD || type == USE_T_CDL ||
9082 type == USE_T_CE || type == USE_T_DL)
9083 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9084
9085 /*
9086 * If text is retained below the screen, first clear or delete as many
9087 * lines at the bottom of the window as are about to be inserted so that
9088 * the deleted lines won't later surface during a screen_del_lines.
9089 */
9090 if (*T_DB)
9091 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9092
9093#ifdef FEAT_CLIPBOARD
9094 /* Remove a modeless selection when inserting lines halfway the screen
9095 * or not the full width of the screen. */
9096 if (off + row > 0
9097# ifdef FEAT_VERTSPLIT
9098 || (wp != NULL && wp->w_width != Columns)
9099# endif
9100 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009101 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 else
9103 clip_scroll_selection(-line_count);
9104#endif
9105
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106#ifdef FEAT_GUI
9107 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9108 * scrolling is actually carried out. */
9109 gui_dont_update_cursor();
9110#endif
9111
9112 if (*T_CCS != NUL) /* cursor relative to region */
9113 cursor_row = row;
9114 else
9115 cursor_row = row + off;
9116
9117 /*
9118 * Shift LineOffset[] line_count down to reflect the inserted lines.
9119 * Clear the inserted lines in ScreenLines[].
9120 */
9121 row += off;
9122 end += off;
9123 for (i = 0; i < line_count; ++i)
9124 {
9125#ifdef FEAT_VERTSPLIT
9126 if (wp != NULL && wp->w_width != Columns)
9127 {
9128 /* need to copy part of a line */
9129 j = end - 1 - i;
9130 while ((j -= line_count) >= row)
9131 linecopy(j + line_count, j, wp);
9132 j += line_count;
9133 if (can_clear((char_u *)" "))
9134 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9135 else
9136 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9137 LineWraps[j] = FALSE;
9138 }
9139 else
9140#endif
9141 {
9142 j = end - 1 - i;
9143 temp = LineOffset[j];
9144 while ((j -= line_count) >= row)
9145 {
9146 LineOffset[j + line_count] = LineOffset[j];
9147 LineWraps[j + line_count] = LineWraps[j];
9148 }
9149 LineOffset[j + line_count] = temp;
9150 LineWraps[j + line_count] = FALSE;
9151 if (can_clear((char_u *)" "))
9152 lineclear(temp, (int)Columns);
9153 else
9154 lineinvalid(temp, (int)Columns);
9155 }
9156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157
9158 screen_stop_highlight();
9159 windgoto(cursor_row, 0);
9160
9161#ifdef FEAT_VERTSPLIT
9162 /* redraw the characters */
9163 if (type == USE_REDRAW)
9164 redraw_block(row, end, wp);
9165 else
9166#endif
9167 if (type == USE_T_CAL)
9168 {
9169 term_append_lines(line_count);
9170 screen_start(); /* don't know where cursor is now */
9171 }
9172 else
9173 {
9174 for (i = 0; i < line_count; i++)
9175 {
9176 if (type == USE_T_AL)
9177 {
9178 if (i && cursor_row != 0)
9179 windgoto(cursor_row, 0);
9180 out_str(T_AL);
9181 }
9182 else /* type == USE_T_SR */
9183 out_str(T_SR);
9184 screen_start(); /* don't know where cursor is now */
9185 }
9186 }
9187
9188 /*
9189 * With scroll-reverse and 'da' flag set we need to clear the lines that
9190 * have been scrolled down into the region.
9191 */
9192 if (type == USE_T_SR && *T_DA)
9193 {
9194 for (i = 0; i < line_count; ++i)
9195 {
9196 windgoto(off + i, 0);
9197 out_str(T_CE);
9198 screen_start(); /* don't know where cursor is now */
9199 }
9200 }
9201
9202#ifdef FEAT_GUI
9203 gui_can_update_cursor();
9204 if (gui.in_use)
9205 out_flush(); /* always flush after a scroll */
9206#endif
9207 return OK;
9208}
9209
9210/*
9211 * delete lines on the screen and update ScreenLines[]
9212 * 'end' is the line after the scrolled part. Normally it is Rows.
9213 * When scrolling region used 'off' is the offset from the top for the region.
9214 * 'row' and 'end' are relative to the start of the region.
9215 *
9216 * Return OK for success, FAIL if the lines are not deleted.
9217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 int
9219screen_del_lines(off, row, line_count, end, force, wp)
9220 int off;
9221 int row;
9222 int line_count;
9223 int end;
9224 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009225 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226{
9227 int j;
9228 int i;
9229 unsigned temp;
9230 int cursor_row;
9231 int cursor_end;
9232 int result_empty; /* result is empty until end of region */
9233 int can_delete; /* deleting line codes can be used */
9234 int type;
9235
9236 /*
9237 * FAIL if
9238 * - there is no valid screen
9239 * - the screen has to be redrawn completely
9240 * - the line count is less than one
9241 * - the line count is more than 'ttyscroll'
9242 */
9243 if (!screen_valid(TRUE) || line_count <= 0 ||
9244 (!force && line_count > p_ttyscroll))
9245 return FAIL;
9246
9247 /*
9248 * Check if the rest of the current region will become empty.
9249 */
9250 result_empty = row + line_count >= end;
9251
9252 /*
9253 * We can delete lines only when 'db' flag not set or when 'ce' option
9254 * available.
9255 */
9256 can_delete = (*T_DB == NUL || can_clear(T_CE));
9257
9258 /*
9259 * There are six ways to delete lines:
9260 * 0. When in a vertically split window and t_CV isn't set, redraw the
9261 * characters from ScreenLines[].
9262 * 1. Use T_CD if it exists and the result is empty.
9263 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9264 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9265 * none of the other ways work.
9266 * 4. Use T_CE (erase line) if the result is empty.
9267 * 5. Use T_DL (delete line) if it exists.
9268 * 6. redraw the characters from ScreenLines[].
9269 */
9270#ifdef FEAT_VERTSPLIT
9271 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9272 type = USE_REDRAW;
9273 else
9274#endif
9275 if (can_clear(T_CD) && result_empty)
9276 type = USE_T_CD;
9277#if defined(__BEOS__) && defined(BEOS_DR8)
9278 /*
9279 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9280 * its internal termcap... this works okay for tests which test *T_DB !=
9281 * NUL. It has the disadvantage that the user cannot use any :set t_*
9282 * command to get T_DB (back) to empty_option, only :set term=... will do
9283 * the trick...
9284 * Anyway, this hack will hopefully go away with the next OS release.
9285 * (Olaf Seibert)
9286 */
9287 else if (row == 0 && T_DB == empty_option
9288 && (line_count == 1 || *T_CDL == NUL))
9289#else
9290 else if (row == 0 && (
9291#ifndef AMIGA
9292 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9293 * up, so use delete-line command */
9294 line_count == 1 ||
9295#endif
9296 *T_CDL == NUL))
9297#endif
9298 type = USE_NL;
9299 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9300 type = USE_T_CDL;
9301 else if (can_clear(T_CE) && result_empty
9302#ifdef FEAT_VERTSPLIT
9303 && (wp == NULL || wp->w_width == Columns)
9304#endif
9305 )
9306 type = USE_T_CE;
9307 else if (*T_DL != NUL && can_delete)
9308 type = USE_T_DL;
9309 else if (*T_CDL != NUL && can_delete)
9310 type = USE_T_CDL;
9311 else
9312 return FAIL;
9313
9314#ifdef FEAT_CLIPBOARD
9315 /* Remove a modeless selection when deleting lines halfway the screen or
9316 * not the full width of the screen. */
9317 if (off + row > 0
9318# ifdef FEAT_VERTSPLIT
9319 || (wp != NULL && wp->w_width != Columns)
9320# endif
9321 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009322 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 else
9324 clip_scroll_selection(line_count);
9325#endif
9326
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327#ifdef FEAT_GUI
9328 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9329 * scrolling is actually carried out. */
9330 gui_dont_update_cursor();
9331#endif
9332
9333 if (*T_CCS != NUL) /* cursor relative to region */
9334 {
9335 cursor_row = row;
9336 cursor_end = end;
9337 }
9338 else
9339 {
9340 cursor_row = row + off;
9341 cursor_end = end + off;
9342 }
9343
9344 /*
9345 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9346 * Clear the inserted lines in ScreenLines[].
9347 */
9348 row += off;
9349 end += off;
9350 for (i = 0; i < line_count; ++i)
9351 {
9352#ifdef FEAT_VERTSPLIT
9353 if (wp != NULL && wp->w_width != Columns)
9354 {
9355 /* need to copy part of a line */
9356 j = row + i;
9357 while ((j += line_count) <= end - 1)
9358 linecopy(j - line_count, j, wp);
9359 j -= line_count;
9360 if (can_clear((char_u *)" "))
9361 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9362 else
9363 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9364 LineWraps[j] = FALSE;
9365 }
9366 else
9367#endif
9368 {
9369 /* whole width, moving the line pointers is faster */
9370 j = row + i;
9371 temp = LineOffset[j];
9372 while ((j += line_count) <= end - 1)
9373 {
9374 LineOffset[j - line_count] = LineOffset[j];
9375 LineWraps[j - line_count] = LineWraps[j];
9376 }
9377 LineOffset[j - line_count] = temp;
9378 LineWraps[j - line_count] = FALSE;
9379 if (can_clear((char_u *)" "))
9380 lineclear(temp, (int)Columns);
9381 else
9382 lineinvalid(temp, (int)Columns);
9383 }
9384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
9386 screen_stop_highlight();
9387
9388#ifdef FEAT_VERTSPLIT
9389 /* redraw the characters */
9390 if (type == USE_REDRAW)
9391 redraw_block(row, end, wp);
9392 else
9393#endif
9394 if (type == USE_T_CD) /* delete the lines */
9395 {
9396 windgoto(cursor_row, 0);
9397 out_str(T_CD);
9398 screen_start(); /* don't know where cursor is now */
9399 }
9400 else if (type == USE_T_CDL)
9401 {
9402 windgoto(cursor_row, 0);
9403 term_delete_lines(line_count);
9404 screen_start(); /* don't know where cursor is now */
9405 }
9406 /*
9407 * Deleting lines at top of the screen or scroll region: Just scroll
9408 * the whole screen (scroll region) up by outputting newlines on the
9409 * last line.
9410 */
9411 else if (type == USE_NL)
9412 {
9413 windgoto(cursor_end - 1, 0);
9414 for (i = line_count; --i >= 0; )
9415 out_char('\n'); /* cursor will remain on same line */
9416 }
9417 else
9418 {
9419 for (i = line_count; --i >= 0; )
9420 {
9421 if (type == USE_T_DL)
9422 {
9423 windgoto(cursor_row, 0);
9424 out_str(T_DL); /* delete a line */
9425 }
9426 else /* type == USE_T_CE */
9427 {
9428 windgoto(cursor_row + i, 0);
9429 out_str(T_CE); /* erase a line */
9430 }
9431 screen_start(); /* don't know where cursor is now */
9432 }
9433 }
9434
9435 /*
9436 * If the 'db' flag is set, we need to clear the lines that have been
9437 * scrolled up at the bottom of the region.
9438 */
9439 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9440 {
9441 for (i = line_count; i > 0; --i)
9442 {
9443 windgoto(cursor_end - i, 0);
9444 out_str(T_CE); /* erase a line */
9445 screen_start(); /* don't know where cursor is now */
9446 }
9447 }
9448
9449#ifdef FEAT_GUI
9450 gui_can_update_cursor();
9451 if (gui.in_use)
9452 out_flush(); /* always flush after a scroll */
9453#endif
9454
9455 return OK;
9456}
9457
9458/*
9459 * show the current mode and ruler
9460 *
9461 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9462 * If clear_cmdline is FALSE there may be a message there that needs to be
9463 * cleared only if a mode is shown.
9464 * Return the length of the message (0 if no message).
9465 */
9466 int
9467showmode()
9468{
9469 int need_clear;
9470 int length = 0;
9471 int do_mode;
9472 int attr;
9473 int nwr_save;
9474#ifdef FEAT_INS_EXPAND
9475 int sub_attr;
9476#endif
9477
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009478 do_mode = ((p_smd && msg_silent == 0)
9479 && ((State & INSERT)
9480 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481#ifdef FEAT_VISUAL
9482 || VIsual_active
9483#endif
9484 ));
9485 if (do_mode || Recording)
9486 {
9487 /*
9488 * Don't show mode right now, when not redrawing or inside a mapping.
9489 * Call char_avail() only when we are going to show something, because
9490 * it takes a bit of time.
9491 */
9492 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9493 {
9494 redraw_cmdline = TRUE; /* show mode later */
9495 return 0;
9496 }
9497
9498 nwr_save = need_wait_return;
9499
9500 /* wait a bit before overwriting an important message */
9501 check_for_delay(FALSE);
9502
9503 /* if the cmdline is more than one line high, erase top lines */
9504 need_clear = clear_cmdline;
9505 if (clear_cmdline && cmdline_row < Rows - 1)
9506 msg_clr_cmdline(); /* will reset clear_cmdline */
9507
9508 /* Position on the last line in the window, column 0 */
9509 msg_pos_mode();
9510 cursor_off();
9511 attr = hl_attr(HLF_CM); /* Highlight mode */
9512 if (do_mode)
9513 {
9514 MSG_PUTS_ATTR("--", attr);
9515#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009516 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009517# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009518 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009519# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009520 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009521# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009522 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009523# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 MSG_PUTS_ATTR(" IM", attr);
9525# else
9526 MSG_PUTS_ATTR(" XIM", attr);
9527# endif
9528#endif
9529#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9530 if (gui.in_use)
9531 {
9532 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009533 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 }
9535#endif
9536#ifdef FEAT_INS_EXPAND
9537 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9538 {
9539 /* These messages can get long, avoid a wrap in a narrow
9540 * window. Prefer showing edit_submode_extra. */
9541 length = (Rows - msg_row) * Columns - 3;
9542 if (edit_submode_extra != NULL)
9543 length -= vim_strsize(edit_submode_extra);
9544 if (length > 0)
9545 {
9546 if (edit_submode_pre != NULL)
9547 length -= vim_strsize(edit_submode_pre);
9548 if (length - vim_strsize(edit_submode) > 0)
9549 {
9550 if (edit_submode_pre != NULL)
9551 msg_puts_attr(edit_submode_pre, attr);
9552 msg_puts_attr(edit_submode, attr);
9553 }
9554 if (edit_submode_extra != NULL)
9555 {
9556 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9557 if ((int)edit_submode_highl < (int)HLF_COUNT)
9558 sub_attr = hl_attr(edit_submode_highl);
9559 else
9560 sub_attr = attr;
9561 msg_puts_attr(edit_submode_extra, sub_attr);
9562 }
9563 }
9564 length = 0;
9565 }
9566 else
9567#endif
9568 {
9569#ifdef FEAT_VREPLACE
9570 if (State & VREPLACE_FLAG)
9571 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9572 else
9573#endif
9574 if (State & REPLACE_FLAG)
9575 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9576 else if (State & INSERT)
9577 {
9578#ifdef FEAT_RIGHTLEFT
9579 if (p_ri)
9580 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9581#endif
9582 MSG_PUTS_ATTR(_(" INSERT"), attr);
9583 }
9584 else if (restart_edit == 'I')
9585 MSG_PUTS_ATTR(_(" (insert)"), attr);
9586 else if (restart_edit == 'R')
9587 MSG_PUTS_ATTR(_(" (replace)"), attr);
9588 else if (restart_edit == 'V')
9589 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9590#ifdef FEAT_RIGHTLEFT
9591 if (p_hkmap)
9592 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9593# ifdef FEAT_FKMAP
9594 if (p_fkmap)
9595 MSG_PUTS_ATTR(farsi_text_5, attr);
9596# endif
9597#endif
9598#ifdef FEAT_KEYMAP
9599 if (State & LANGMAP)
9600 {
9601# ifdef FEAT_ARABIC
9602 if (curwin->w_p_arab)
9603 MSG_PUTS_ATTR(_(" Arabic"), attr);
9604 else
9605# endif
9606 MSG_PUTS_ATTR(_(" (lang)"), attr);
9607 }
9608#endif
9609 if ((State & INSERT) && p_paste)
9610 MSG_PUTS_ATTR(_(" (paste)"), attr);
9611
9612#ifdef FEAT_VISUAL
9613 if (VIsual_active)
9614 {
9615 char *p;
9616
9617 /* Don't concatenate separate words to avoid translation
9618 * problems. */
9619 switch ((VIsual_select ? 4 : 0)
9620 + (VIsual_mode == Ctrl_V) * 2
9621 + (VIsual_mode == 'V'))
9622 {
9623 case 0: p = N_(" VISUAL"); break;
9624 case 1: p = N_(" VISUAL LINE"); break;
9625 case 2: p = N_(" VISUAL BLOCK"); break;
9626 case 4: p = N_(" SELECT"); break;
9627 case 5: p = N_(" SELECT LINE"); break;
9628 default: p = N_(" SELECT BLOCK"); break;
9629 }
9630 MSG_PUTS_ATTR(_(p), attr);
9631 }
9632#endif
9633 MSG_PUTS_ATTR(" --", attr);
9634 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009635
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 need_clear = TRUE;
9637 }
9638 if (Recording
9639#ifdef FEAT_INS_EXPAND
9640 && edit_submode == NULL /* otherwise it gets too long */
9641#endif
9642 )
9643 {
9644 MSG_PUTS_ATTR(_("recording"), attr);
9645 need_clear = TRUE;
9646 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009647
9648 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 if (need_clear || clear_cmdline)
9650 msg_clr_eos();
9651 msg_didout = FALSE; /* overwrite this message */
9652 length = msg_col;
9653 msg_col = 0;
9654 need_wait_return = nwr_save; /* never ask for hit-return for this */
9655 }
9656 else if (clear_cmdline && msg_silent == 0)
9657 /* Clear the whole command line. Will reset "clear_cmdline". */
9658 msg_clr_cmdline();
9659
9660#ifdef FEAT_CMDL_INFO
9661# ifdef FEAT_VISUAL
9662 /* In Visual mode the size of the selected area must be redrawn. */
9663 if (VIsual_active)
9664 clear_showcmd();
9665# endif
9666
9667 /* If the last window has no status line, the ruler is after the mode
9668 * message and must be redrawn */
9669 if (redrawing()
9670# ifdef FEAT_WINDOWS
9671 && lastwin->w_status_height == 0
9672# endif
9673 )
9674 win_redr_ruler(lastwin, TRUE);
9675#endif
9676 redraw_cmdline = FALSE;
9677 clear_cmdline = FALSE;
9678
9679 return length;
9680}
9681
9682/*
9683 * Position for a mode message.
9684 */
9685 static void
9686msg_pos_mode()
9687{
9688 msg_col = 0;
9689 msg_row = Rows - 1;
9690}
9691
9692/*
9693 * Delete mode message. Used when ESC is typed which is expected to end
9694 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009695 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 */
9697 void
9698unshowmode(force)
9699 int force;
9700{
9701 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009702 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 */
9704 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9705 redraw_cmdline = TRUE; /* delete mode later */
9706 else
9707 {
9708 msg_pos_mode();
9709 if (Recording)
9710 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9711 msg_clr_eos();
9712 }
9713}
9714
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009715#if defined(FEAT_WINDOWS)
9716/*
9717 * Draw the tab pages line at the top of the Vim window.
9718 */
9719 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009720draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009721{
9722 int tabcount = 0;
9723 tabpage_T *tp;
9724 int tabwidth;
9725 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009726 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009727 int attr;
9728 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009729 win_T *cwp;
9730 int wincount;
9731 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009732 int c;
9733 int len;
9734 int attr_sel = hl_attr(HLF_TPS);
9735 int attr_nosel = hl_attr(HLF_TP);
9736 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009737 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009738 int room;
9739 int use_sep_chars = (t_colors < 8
9740#ifdef FEAT_GUI
9741 && !gui.in_use
9742#endif
9743 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009744
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009745 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009746
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009747#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009748 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009749 if (gui_use_tabline())
9750 {
9751 gui_update_tabline();
9752 return;
9753 }
9754#endif
9755
9756 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009757 return;
9758
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009759#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009760
9761 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9762 for (scol = 0; scol < Columns; ++scol)
9763 TabPageIdxs[scol] = 0;
9764
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009765 /* Use the 'tabline' option if it's set. */
9766 if (*p_tal != NUL)
9767 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009768 int save_called_emsg = called_emsg;
9769
9770 /* Check for an error. If there is one we would loop in redrawing the
9771 * screen. Avoid that by making 'tabline' empty. */
9772 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009773 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009774 if (called_emsg)
9775 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009776 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009777 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009778 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009779 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009780#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009781 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009782 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9783 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009784
Bram Moolenaar238a5642006-02-21 22:12:05 +00009785 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9786 if (tabwidth < 6)
9787 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009788
Bram Moolenaar238a5642006-02-21 22:12:05 +00009789 attr = attr_nosel;
9790 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009791 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009792 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9793 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009794 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009795 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009796
Bram Moolenaar238a5642006-02-21 22:12:05 +00009797 if (tp->tp_topframe == topframe)
9798 attr = attr_sel;
9799 if (use_sep_chars && col > 0)
9800 screen_putchar('|', 0, col++, attr);
9801
9802 if (tp->tp_topframe != topframe)
9803 attr = attr_nosel;
9804
9805 screen_putchar(' ', 0, col++, attr);
9806
9807 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009808 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009809 cwp = curwin;
9810 wp = firstwin;
9811 }
9812 else
9813 {
9814 cwp = tp->tp_curwin;
9815 wp = tp->tp_firstwin;
9816 }
9817
9818 modified = FALSE;
9819 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9820 if (bufIsChanged(wp->w_buffer))
9821 modified = TRUE;
9822 if (modified || wincount > 1)
9823 {
9824 if (wincount > 1)
9825 {
9826 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009827 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009828 if (col + len >= Columns - 3)
9829 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009830 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009831#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009832 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009833#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009834 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009835#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009836 );
9837 col += len;
9838 }
9839 if (modified)
9840 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9841 screen_putchar(' ', 0, col++, attr);
9842 }
9843
9844 room = scol - col + tabwidth - 1;
9845 if (room > 0)
9846 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009847 /* Get buffer name in NameBuff[] */
9848 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009849 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009850 len = vim_strsize(NameBuff);
9851 p = NameBuff;
9852#ifdef FEAT_MBYTE
9853 if (has_mbyte)
9854 while (len > room)
9855 {
9856 len -= ptr2cells(p);
9857 mb_ptr_adv(p);
9858 }
9859 else
9860#endif
9861 if (len > room)
9862 {
9863 p += len - room;
9864 len = room;
9865 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009866 if (len > Columns - col - 1)
9867 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009868
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009869 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009870 col += len;
9871 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009872 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009873
9874 /* Store the tab page number in TabPageIdxs[], so that
9875 * jump_to_mouse() knows where each one is. */
9876 ++tabcount;
9877 while (scol < col)
9878 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009879 }
9880
Bram Moolenaar238a5642006-02-21 22:12:05 +00009881 if (use_sep_chars)
9882 c = '_';
9883 else
9884 c = ' ';
9885 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009886
9887 /* Put an "X" for closing the current tab if there are several. */
9888 if (first_tabpage->tp_next != NULL)
9889 {
9890 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9891 TabPageIdxs[Columns - 1] = -999;
9892 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009893 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009894
9895 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9896 * set. */
9897 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009898}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009899
9900/*
9901 * Get buffer name for "buf" into NameBuff[].
9902 * Takes care of special buffer names and translates special characters.
9903 */
9904 void
9905get_trans_bufname(buf)
9906 buf_T *buf;
9907{
9908 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02009909 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009910 else
9911 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9912 trans_characters(NameBuff, MAXPATHL);
9913}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009914#endif
9915
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9917/*
9918 * Get the character to use in a status line. Get its attributes in "*attr".
9919 */
9920 static int
9921fillchar_status(attr, is_curwin)
9922 int *attr;
9923 int is_curwin;
9924{
9925 int fill;
9926 if (is_curwin)
9927 {
9928 *attr = hl_attr(HLF_S);
9929 fill = fill_stl;
9930 }
9931 else
9932 {
9933 *attr = hl_attr(HLF_SNC);
9934 fill = fill_stlnc;
9935 }
9936 /* Use fill when there is highlighting, and highlighting of current
9937 * window differs, or the fillchars differ, or this is not the
9938 * current window */
9939 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9940 || !is_curwin || firstwin == lastwin)
9941 || (fill_stl != fill_stlnc)))
9942 return fill;
9943 if (is_curwin)
9944 return '^';
9945 return '=';
9946}
9947#endif
9948
9949#ifdef FEAT_VERTSPLIT
9950/*
9951 * Get the character to use in a separator between vertically split windows.
9952 * Get its attributes in "*attr".
9953 */
9954 static int
9955fillchar_vsep(attr)
9956 int *attr;
9957{
9958 *attr = hl_attr(HLF_C);
9959 if (*attr == 0 && fill_vert == ' ')
9960 return '|';
9961 else
9962 return fill_vert;
9963}
9964#endif
9965
9966/*
9967 * Return TRUE if redrawing should currently be done.
9968 */
9969 int
9970redrawing()
9971{
9972 return (!RedrawingDisabled
9973 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9974}
9975
9976/*
9977 * Return TRUE if printing messages should currently be done.
9978 */
9979 int
9980messaging()
9981{
9982 return (!(p_lz && char_avail() && !KeyTyped));
9983}
9984
9985/*
9986 * Show current status info in ruler and various other places
9987 * If always is FALSE, only show ruler if position has changed.
9988 */
9989 void
9990showruler(always)
9991 int always;
9992{
9993 if (!always && !redrawing())
9994 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009995#ifdef FEAT_INS_EXPAND
9996 if (pum_visible())
9997 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009998# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009999 /* Don't redraw right now, do it later. */
10000 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010001# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010002 return;
10003 }
10004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010006 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010007 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010008 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 else
10011#endif
10012#ifdef FEAT_CMDL_INFO
10013 win_redr_ruler(curwin, always);
10014#endif
10015
10016#ifdef FEAT_TITLE
10017 if (need_maketitle
10018# ifdef FEAT_STL_OPT
10019 || (p_icon && (stl_syntax & STL_IN_ICON))
10020 || (p_title && (stl_syntax & STL_IN_TITLE))
10021# endif
10022 )
10023 maketitle();
10024#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010025#ifdef FEAT_WINDOWS
10026 /* Redraw the tab pages line if needed. */
10027 if (redraw_tabline)
10028 draw_tabline();
10029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030}
10031
10032#ifdef FEAT_CMDL_INFO
10033 static void
10034win_redr_ruler(wp, always)
10035 win_T *wp;
10036 int always;
10037{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010038#define RULER_BUF_LEN 70
10039 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 int row;
10041 int fillchar;
10042 int attr;
10043 int empty_line = FALSE;
10044 colnr_T virtcol;
10045 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010046 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 int o;
10048#ifdef FEAT_VERTSPLIT
10049 int this_ru_col;
10050 int off = 0;
10051 int width = Columns;
10052# define WITH_OFF(x) x
10053# define WITH_WIDTH(x) x
10054#else
10055# define WITH_OFF(x) 0
10056# define WITH_WIDTH(x) Columns
10057# define this_ru_col ru_col
10058#endif
10059
10060 /* If 'ruler' off or redrawing disabled, don't do anything */
10061 if (!p_ru)
10062 return;
10063
10064 /*
10065 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10066 * after deleting lines, before cursor.lnum is corrected.
10067 */
10068 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10069 return;
10070
10071#ifdef FEAT_INS_EXPAND
10072 /* Don't draw the ruler while doing insert-completion, it might overwrite
10073 * the (long) mode message. */
10074# ifdef FEAT_WINDOWS
10075 if (wp == lastwin && lastwin->w_status_height == 0)
10076# endif
10077 if (edit_submode != NULL)
10078 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010079 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10080 if (pum_visible())
10081 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082#endif
10083
10084#ifdef FEAT_STL_OPT
10085 if (*p_ruf)
10086 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010087 int save_called_emsg = called_emsg;
10088
10089 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010091 if (called_emsg)
10092 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010093 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010094 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 return;
10096 }
10097#endif
10098
10099 /*
10100 * Check if not in Insert mode and the line is empty (will show "0-1").
10101 */
10102 if (!(State & INSERT)
10103 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10104 empty_line = TRUE;
10105
10106 /*
10107 * Only draw the ruler when something changed.
10108 */
10109 validate_virtcol_win(wp);
10110 if ( redraw_cmdline
10111 || always
10112 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10113 || wp->w_cursor.col != wp->w_ru_cursor.col
10114 || wp->w_virtcol != wp->w_ru_virtcol
10115#ifdef FEAT_VIRTUALEDIT
10116 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10117#endif
10118 || wp->w_topline != wp->w_ru_topline
10119 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10120#ifdef FEAT_DIFF
10121 || wp->w_topfill != wp->w_ru_topfill
10122#endif
10123 || empty_line != wp->w_ru_empty)
10124 {
10125 cursor_off();
10126#ifdef FEAT_WINDOWS
10127 if (wp->w_status_height)
10128 {
10129 row = W_WINROW(wp) + wp->w_height;
10130 fillchar = fillchar_status(&attr, wp == curwin);
10131# ifdef FEAT_VERTSPLIT
10132 off = W_WINCOL(wp);
10133 width = W_WIDTH(wp);
10134# endif
10135 }
10136 else
10137#endif
10138 {
10139 row = Rows - 1;
10140 fillchar = ' ';
10141 attr = 0;
10142#ifdef FEAT_VERTSPLIT
10143 width = Columns;
10144 off = 0;
10145#endif
10146 }
10147
10148 /* In list mode virtcol needs to be recomputed */
10149 virtcol = wp->w_virtcol;
10150 if (wp->w_p_list && lcs_tab1 == NUL)
10151 {
10152 wp->w_p_list = FALSE;
10153 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10154 wp->w_p_list = TRUE;
10155 }
10156
10157 /*
10158 * Some sprintfs return the length, some return a pointer.
10159 * To avoid portability problems we use strlen() here.
10160 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010161 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10163 ? 0L
10164 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010165 len = STRLEN(buffer);
10166 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10168 (int)virtcol + 1);
10169
10170 /*
10171 * Add a "50%" if there is room for it.
10172 * On the last line, don't print in the last column (scrolls the
10173 * screen up on some terminals).
10174 */
10175 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010176 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 o = i + vim_strsize(buffer + i + 1);
10178#ifdef FEAT_WINDOWS
10179 if (wp->w_status_height == 0) /* can't use last char of screen */
10180#endif
10181 ++o;
10182#ifdef FEAT_VERTSPLIT
10183 this_ru_col = ru_col - (Columns - width);
10184 if (this_ru_col < 0)
10185 this_ru_col = 0;
10186#endif
10187 /* Never use more than half the window/screen width, leave the other
10188 * half for the filename. */
10189 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10190 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10191 if (this_ru_col + o < WITH_WIDTH(width))
10192 {
10193 while (this_ru_col + o < WITH_WIDTH(width))
10194 {
10195#ifdef FEAT_MBYTE
10196 if (has_mbyte)
10197 i += (*mb_char2bytes)(fillchar, buffer + i);
10198 else
10199#endif
10200 buffer[i++] = fillchar;
10201 ++o;
10202 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010203 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 }
10205 /* Truncate at window boundary. */
10206#ifdef FEAT_MBYTE
10207 if (has_mbyte)
10208 {
10209 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010210 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 {
10212 o += (*mb_ptr2cells)(buffer + i);
10213 if (this_ru_col + o > WITH_WIDTH(width))
10214 {
10215 buffer[i] = NUL;
10216 break;
10217 }
10218 }
10219 }
10220 else
10221#endif
10222 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10223 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10224
10225 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10226 i = redraw_cmdline;
10227 screen_fill(row, row + 1,
10228 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10229 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10230 fillchar, fillchar, attr);
10231 /* don't redraw the cmdline because of showing the ruler */
10232 redraw_cmdline = i;
10233 wp->w_ru_cursor = wp->w_cursor;
10234 wp->w_ru_virtcol = wp->w_virtcol;
10235 wp->w_ru_empty = empty_line;
10236 wp->w_ru_topline = wp->w_topline;
10237 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10238#ifdef FEAT_DIFF
10239 wp->w_ru_topfill = wp->w_topfill;
10240#endif
10241 }
10242}
10243#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010244
10245#if defined(FEAT_LINEBREAK) || defined(PROTO)
10246/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010247 * Return the width of the 'number' and 'relativenumber' column.
10248 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010249 * Otherwise it depends on 'numberwidth' and the line count.
10250 */
10251 int
10252number_width(wp)
10253 win_T *wp;
10254{
10255 int n;
10256 linenr_T lnum;
10257
Bram Moolenaar700e7342013-01-30 12:31:36 +010010258 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010259
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010260 if (lnum == wp->w_nrwidth_line_count)
10261 return wp->w_nrwidth_width;
10262 wp->w_nrwidth_line_count = lnum;
10263
10264 n = 0;
10265 do
10266 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010267 lnum /= 10;
10268 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010269 } while (lnum > 0);
10270
10271 /* 'numberwidth' gives the minimal width plus one */
10272 if (n < wp->w_p_nuw - 1)
10273 n = wp->w_p_nuw - 1;
10274
10275 wp->w_nrwidth_width = n;
10276 return n;
10277}
10278#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010279
10280/*
10281 * Return the current cursor column. This is the actual position on the
10282 * screen. First column is 0.
10283 */
10284 int
10285screen_screencol()
10286{
10287 return screen_cur_col;
10288}
10289
10290/*
10291 * Return the current cursor row. This is the actual position on the screen.
10292 * First row is 0.
10293 */
10294 int
10295screen_screenrow()
10296{
10297 return screen_cur_row;
10298}