blob: ffbd1c34e5caf7719334d9c13a604a64ccd7b1a7 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
92/*
93 * The attributes that are actually active for writing to the screen.
94 */
95static int screen_attr = 0;
96
97/*
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
100 */
101static int screen_cur_row, screen_cur_col; /* last known cursor position */
102
103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106
107#ifdef FEAT_FOLDING
108static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109#endif
110
111/*
112 * Buffer for one screen line (characters and attributes).
113 */
114static schar_T *current_ScreenLine;
115
116static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000117static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_FOLDING
119static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000123static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125#ifdef FEAT_RIGHTLEFT
126static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128#else
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_VERTSPLIT
133static void draw_vsep_win __ARGS((win_T *wp, int row));
134#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000135#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000136static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000139#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void start_search_hl __ARGS((void));
141static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200142static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
144static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
145#endif
146static void screen_start_highlight __ARGS((int attr));
147static void screen_char __ARGS((unsigned off, int row, int col));
148#ifdef FEAT_MBYTE
149static void screen_char_2 __ARGS((unsigned off, int row, int col));
150#endif
151static void screenclear2 __ARGS((void));
152static void lineclear __ARGS((unsigned off, int width));
153static void lineinvalid __ARGS((unsigned off, int width));
154#ifdef FEAT_VERTSPLIT
155static void linecopy __ARGS((int to, int from, win_T *wp));
156static void redraw_block __ARGS((int row, int end, win_T *wp));
157#endif
158static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
159static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000162static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
165static int fillchar_status __ARGS((int *attr, int is_curwin));
166#endif
167#ifdef FEAT_VERTSPLIT
168static int fillchar_vsep __ARGS((int *attr));
169#endif
170#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000171static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_CMDL_INFO
174static void win_redr_ruler __ARGS((win_T *wp, int always));
175#endif
176
177#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
178/* Ugly global: overrule attribute used by screen_char() */
179static int screen_char_attr = 0;
180#endif
181
182/*
183 * Redraw the current window later, with update_screen(type).
184 * Set must_redraw only if not already set to a higher value.
185 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
186 */
187 void
188redraw_later(type)
189 int type;
190{
191 redraw_win_later(curwin, type);
192}
193
194 void
195redraw_win_later(wp, type)
196 win_T *wp;
197 int type;
198{
199 if (wp->w_redr_type < type)
200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
214redraw_later_clear()
215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
232redraw_all_later(type)
233 int type;
234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
247redraw_curbuf_later(type)
248 int type;
249{
250 redraw_buf_later(curbuf, type);
251}
252
253 void
254redraw_buf_later(buf, type)
255 buf_T *buf;
256 int type;
257{
258 win_T *wp;
259
260 FOR_ALL_WINDOWS(wp)
261 {
262 if (wp->w_buffer == buf)
263 redraw_win_later(wp, type);
264 }
265}
266
267/*
268 * Changed something in the current window, at buffer line "lnum", that
269 * requires that line and possibly other lines to be redrawn.
270 * Used when entering/leaving Insert mode with the cursor on a folded line.
271 * Used to remove the "$" from a change command.
272 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
273 * may become invalid and the whole window will have to be redrawn.
274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 void
276redrawWinline(lnum, invalid)
277 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000278 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280#ifdef FEAT_FOLDING
281 int i;
282#endif
283
284 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
285 curwin->w_redraw_top = lnum;
286 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
287 curwin->w_redraw_bot = lnum;
288 redraw_later(VALID);
289
290#ifdef FEAT_FOLDING
291 if (invalid)
292 {
293 /* A w_lines[] entry for this lnum has become invalid. */
294 i = find_wl_entry(curwin, lnum);
295 if (i >= 0)
296 curwin->w_lines[i].wl_valid = FALSE;
297 }
298#endif
299}
300
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200301#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200302 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * update all windows that are editing the current buffer
305 */
306 void
307update_curbuf(type)
308 int type;
309{
310 redraw_curbuf_later(type);
311 update_screen(type);
312}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * update_screen()
317 *
318 * Based on the current value of curwin->w_topline, transfer a screenfull
319 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
320 */
321 void
322update_screen(type)
323 int type;
324{
325 win_T *wp;
326 static int did_intro = FALSE;
327#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
328 int did_one;
329#endif
330
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000331 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 if (!screen_valid(TRUE))
333 return;
334
335 if (must_redraw)
336 {
337 if (type < must_redraw) /* use maximal type */
338 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000339
340 /* must_redraw is reset here, so that when we run into some weird
341 * reason to redraw while busy redrawing (e.g., asynchronous
342 * scrolling), or update_topline() in win_update() will cause a
343 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 must_redraw = 0;
345 }
346
347 /* Need to update w_lines[]. */
348 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
349 type = NOT_VALID;
350
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000351 /* Postpone the redrawing when it's not needed and when being called
352 * recursively. */
353 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 redraw_later(type); /* remember type for next time */
356 must_redraw = type;
357 if (type > INVERTED_ALL)
358 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
359 return;
360 }
361
362 updating_screen = TRUE;
363#ifdef FEAT_SYN_HL
364 ++display_tick; /* let syntax code know we're in a next round of
365 * display updating */
366#endif
367
368 /*
369 * if the screen was scrolled up when displaying a message, scroll it down
370 */
371 if (msg_scrolled)
372 {
373 clear_cmdline = TRUE;
374 if (msg_scrolled > Rows - 5) /* clearing is faster */
375 type = CLEAR;
376 else if (type != CLEAR)
377 {
378 check_for_delay(FALSE);
379 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
380 type = CLEAR;
381 FOR_ALL_WINDOWS(wp)
382 {
383 if (W_WINROW(wp) < msg_scrolled)
384 {
385 if (W_WINROW(wp) + wp->w_height > msg_scrolled
386 && wp->w_redr_type < REDRAW_TOP
387 && wp->w_lines_valid > 0
388 && wp->w_topline == wp->w_lines[0].wl_lnum)
389 {
390 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
391 wp->w_redr_type = REDRAW_TOP;
392 }
393 else
394 {
395 wp->w_redr_type = NOT_VALID;
396#ifdef FEAT_WINDOWS
397 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
398 <= msg_scrolled)
399 wp->w_redr_status = TRUE;
400#endif
401 }
402 }
403 }
404 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000406 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409 msg_scrolled = 0;
410 need_wait_return = FALSE;
411 }
412
413 /* reset cmdline_row now (may have been changed temporarily) */
414 compute_cmdrow();
415
416 /* Check for changed highlighting */
417 if (need_highlight_changed)
418 highlight_changed();
419
420 if (type == CLEAR) /* first clear screen */
421 {
422 screenclear(); /* will reset clear_cmdline */
423 type = NOT_VALID;
424 }
425
426 if (clear_cmdline) /* going to clear cmdline (done below) */
427 check_for_delay(FALSE);
428
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000429#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200430 /* Force redraw when width of 'number' or 'relativenumber' column
431 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
434 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 curwin->w_redr_type = NOT_VALID;
436#endif
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 /*
439 * Only start redrawing if there is really something to do.
440 */
441 if (type == INVERTED)
442 update_curswant();
443 if (curwin->w_redr_type < type
444 && !((type == VALID
445 && curwin->w_lines[0].wl_valid
446#ifdef FEAT_DIFF
447 && curwin->w_topfill == curwin->w_old_topfill
448 && curwin->w_botfill == curwin->w_old_botfill
449#endif
450 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
451#ifdef FEAT_VISUAL
452 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000453 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
455 && curwin->w_old_visual_mode == VIsual_mode
456 && (curwin->w_valid & VALID_VIRTCOL)
457 && curwin->w_old_curswant == curwin->w_curswant)
458#endif
459 ))
460 curwin->w_redr_type = type;
461
Bram Moolenaar5a305422006-04-28 22:38:25 +0000462#ifdef FEAT_WINDOWS
463 /* Redraw the tab pages line if needed. */
464 if (redraw_tabline || type >= NOT_VALID)
465 draw_tabline();
466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#ifdef FEAT_SYN_HL
469 /*
470 * Correct stored syntax highlighting info for changes in each displayed
471 * buffer. Each buffer must only be done once.
472 */
473 FOR_ALL_WINDOWS(wp)
474 {
475 if (wp->w_buffer->b_mod_set)
476 {
477# ifdef FEAT_WINDOWS
478 win_T *wwp;
479
480 /* Check if we already did this buffer. */
481 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
482 if (wwp->w_buffer == wp->w_buffer)
483 break;
484# endif
485 if (
486# ifdef FEAT_WINDOWS
487 wwp == wp &&
488# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200489 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 syn_stack_apply_changes(wp->w_buffer);
491 }
492 }
493#endif
494
495 /*
496 * Go from top to bottom through the windows, redrawing the ones that need
497 * it.
498 */
499#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
500 did_one = FALSE;
501#endif
502#ifdef FEAT_SEARCH_EXTRA
503 search_hl.rm.regprog = NULL;
504#endif
505 FOR_ALL_WINDOWS(wp)
506 {
507 if (wp->w_redr_type != 0)
508 {
509 cursor_off();
510#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
511 if (!did_one)
512 {
513 did_one = TRUE;
514# ifdef FEAT_SEARCH_EXTRA
515 start_search_hl();
516# endif
517# ifdef FEAT_CLIPBOARD
518 /* When Visual area changed, may have to update selection. */
519 if (clip_star.available && clip_isautosel())
520 clip_update_selection();
521# endif
522#ifdef FEAT_GUI
523 /* Remove the cursor before starting to do anything, because
524 * scrolling may make it difficult to redraw the text under
525 * it. */
526 if (gui.in_use)
527 gui_undraw_cursor();
528#endif
529 }
530#endif
531 win_update(wp);
532 }
533
534#ifdef FEAT_WINDOWS
535 /* redraw status line after the window to minimize cursor movement */
536 if (wp->w_redr_status)
537 {
538 cursor_off();
539 win_redr_status(wp);
540 }
541#endif
542 }
543#if defined(FEAT_SEARCH_EXTRA)
544 end_search_hl();
545#endif
546
547#ifdef FEAT_WINDOWS
548 /* Reset b_mod_set flags. Going through all windows is probably faster
549 * than going through all buffers (there could be many buffers). */
550 for (wp = firstwin; wp != NULL; wp = wp->w_next)
551 wp->w_buffer->b_mod_set = FALSE;
552#else
553 curbuf->b_mod_set = FALSE;
554#endif
555
556 updating_screen = FALSE;
557#ifdef FEAT_GUI
558 gui_may_resize_shell();
559#endif
560
561 /* Clear or redraw the command line. Done last, because scrolling may
562 * mess up the command line. */
563 if (clear_cmdline || redraw_cmdline)
564 showmode();
565
566 /* May put up an introductory message when not editing a file */
567 if (!did_intro && bufempty()
568 && curbuf->b_fname == NULL
569#ifdef FEAT_WINDOWS
570 && firstwin->w_next == NULL
571#endif
572 && vim_strchr(p_shm, SHM_INTRO) == NULL)
573 intro_message(FALSE);
574 did_intro = TRUE;
575
576#ifdef FEAT_GUI
577 /* Redraw the cursor and update the scrollbars when all screen updating is
578 * done. */
579 if (gui.in_use)
580 {
581 out_flush(); /* required before updating the cursor */
582 if (did_one)
583 gui_update_cursor(FALSE, FALSE);
584 gui_update_scrollbars(FALSE);
585 }
586#endif
587}
588
Bram Moolenaar860cae12010-06-05 23:22:07 +0200589#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200590/*
591 * Return TRUE if the cursor line in window "wp" may be concealed, according
592 * to the 'concealcursor' option.
593 */
594 int
595conceal_cursor_line(wp)
596 win_T *wp;
597{
598 int c;
599
600 if (*wp->w_p_cocu == NUL)
601 return FALSE;
602 if (get_real_state() & VISUAL)
603 c = 'v';
604 else if (State & INSERT)
605 c = 'i';
606 else if (State & NORMAL)
607 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200608 else if (State & CMDLINE)
609 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200610 else
611 return FALSE;
612 return vim_strchr(wp->w_p_cocu, c) != NULL;
613}
614
615/*
616 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
617 */
618 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200619conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200620{
621 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
622 {
623 need_cursor_line_redraw = TRUE;
624 /* Need to recompute cursor column, e.g., when starting Visual mode
625 * without concealing. */
626 curs_columns(TRUE);
627 }
628}
629
Bram Moolenaar860cae12010-06-05 23:22:07 +0200630 void
631update_single_line(wp, lnum)
632 win_T *wp;
633 linenr_T lnum;
634{
635 int row;
636 int j;
637
638 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200639 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 {
641# ifdef FEAT_GUI
642 /* Remove the cursor before starting to do anything, because scrolling
643 * may make it difficult to redraw the text under it. */
644 if (gui.in_use)
645 gui_undraw_cursor();
646# endif
647 row = 0;
648 for (j = 0; j < wp->w_lines_valid; ++j)
649 {
650 if (lnum == wp->w_lines[j].wl_lnum)
651 {
652 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200653# ifdef FEAT_SEARCH_EXTRA
654 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200655 start_search_hl();
656 prepare_search_hl(wp, lnum);
657# endif
658 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
659# if defined(FEAT_SEARCH_EXTRA)
660 end_search_hl();
661# endif
662 break;
663 }
664 row += wp->w_lines[j].wl_size;
665 }
666# ifdef FEAT_GUI
667 /* Redraw the cursor */
668 if (gui.in_use)
669 {
670 out_flush(); /* required before updating the cursor */
671 gui_update_cursor(FALSE, FALSE);
672 }
673# endif
674 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200675 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200676}
677#endif
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
680static void update_prepare __ARGS((void));
681static void update_finish __ARGS((void));
682
683/*
684 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000685 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 */
687 static void
688update_prepare()
689{
690 cursor_off();
691 updating_screen = TRUE;
692#ifdef FEAT_GUI
693 /* Remove the cursor before starting to do anything, because scrolling may
694 * make it difficult to redraw the text under it. */
695 if (gui.in_use)
696 gui_undraw_cursor();
697#endif
698#ifdef FEAT_SEARCH_EXTRA
699 start_search_hl();
700#endif
701}
702
703/*
704 * Finish updating one or more windows.
705 */
706 static void
707update_finish()
708{
709 if (redraw_cmdline)
710 showmode();
711
712# ifdef FEAT_SEARCH_EXTRA
713 end_search_hl();
714# endif
715
716 updating_screen = FALSE;
717
718# ifdef FEAT_GUI
719 gui_may_resize_shell();
720
721 /* Redraw the cursor and update the scrollbars when all screen updating is
722 * done. */
723 if (gui.in_use)
724 {
725 out_flush(); /* required before updating the cursor */
726 gui_update_cursor(FALSE, FALSE);
727 gui_update_scrollbars(FALSE);
728 }
729# endif
730}
731#endif
732
733#if defined(FEAT_SIGNS) || defined(PROTO)
734 void
735update_debug_sign(buf, lnum)
736 buf_T *buf;
737 linenr_T lnum;
738{
739 win_T *wp;
740 int doit = FALSE;
741
742# ifdef FEAT_FOLDING
743 win_foldinfo.fi_level = 0;
744# endif
745
746 /* update/delete a specific mark */
747 FOR_ALL_WINDOWS(wp)
748 {
749 if (buf != NULL && lnum > 0)
750 {
751 if (wp->w_buffer == buf && lnum >= wp->w_topline
752 && lnum < wp->w_botline)
753 {
754 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
755 wp->w_redraw_top = lnum;
756 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
757 wp->w_redraw_bot = lnum;
758 redraw_win_later(wp, VALID);
759 }
760 }
761 else
762 redraw_win_later(wp, VALID);
763 if (wp->w_redr_type != 0)
764 doit = TRUE;
765 }
766
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100767 /* Return when there is nothing to do, screen updating is already
768 * happening (recursive call) or still starting up. */
769 if (!doit || updating_screen
770#ifdef FEAT_GUI
771 || gui.starting
772#endif
773 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 return;
775
776 /* update all windows that need updating */
777 update_prepare();
778
779# ifdef FEAT_WINDOWS
780 for (wp = firstwin; wp; wp = wp->w_next)
781 {
782 if (wp->w_redr_type != 0)
783 win_update(wp);
784 if (wp->w_redr_status)
785 win_redr_status(wp);
786 }
787# else
788 if (curwin->w_redr_type != 0)
789 win_update(curwin);
790# endif
791
792 update_finish();
793}
794#endif
795
796
797#if defined(FEAT_GUI) || defined(PROTO)
798/*
799 * Update a single window, its status line and maybe the command line msg.
800 * Used for the GUI scrollbar.
801 */
802 void
803updateWindow(wp)
804 win_T *wp;
805{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000806 /* return if already busy updating */
807 if (updating_screen)
808 return;
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 update_prepare();
811
812#ifdef FEAT_CLIPBOARD
813 /* When Visual area changed, may have to update selection. */
814 if (clip_star.available && clip_isautosel())
815 clip_update_selection();
816#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000821 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000822 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000823 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (wp->w_redr_status
826# ifdef FEAT_CMDL_INFO
827 || p_ru
828# endif
829# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000830 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831# endif
832 )
833 win_redr_status(wp);
834#endif
835
836 update_finish();
837}
838#endif
839
840/*
841 * Update a single window.
842 *
843 * This may cause the windows below it also to be redrawn (when clearing the
844 * screen or scrolling lines).
845 *
846 * How the window is redrawn depends on wp->w_redr_type. Each type also
847 * implies the one below it.
848 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000849 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
851 * INVERTED redraw the changed part of the Visual area
852 * INVERTED_ALL redraw the whole Visual area
853 * VALID 1. scroll up/down to adjust for a changed w_topline
854 * 2. update lines at the top when scrolled down
855 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000856 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 * b_mod_top and b_mod_bot.
858 * - if wp->w_redraw_top non-zero, redraw lines between
859 * wp->w_redraw_top and wp->w_redr_bot.
860 * - continue redrawing when syntax status is invalid.
861 * 4. if scrolled up, update lines at the bottom.
862 * This results in three areas that may need updating:
863 * top: from first row to top_end (when scrolled down)
864 * mid: from mid_start to mid_end (update inversion or changed text)
865 * bot: from bot_start to last row (when scrolled up)
866 */
867 static void
868win_update(wp)
869 win_T *wp;
870{
871 buf_T *buf = wp->w_buffer;
872 int type;
873 int top_end = 0; /* Below last row of the top area that needs
874 updating. 0 when no top area updating. */
875 int mid_start = 999;/* first row of the mid area that needs
876 updating. 999 when no mid area updating. */
877 int mid_end = 0; /* Below last row of the mid area that needs
878 updating. 0 when no mid area updating. */
879 int bot_start = 999;/* first row of the bot area that needs
880 updating. 999 when no bot area updating */
881#ifdef FEAT_VISUAL
882 int scrolled_down = FALSE; /* TRUE when scrolled down when
883 w_topline got smaller a bit */
884#endif
885#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000886 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int top_to_mod = FALSE; /* redraw above mod_top */
888#endif
889
890 int row; /* current window row to display */
891 linenr_T lnum; /* current buffer lnum to display */
892 int idx; /* current index in w_lines[] */
893 int srow; /* starting row of the current line */
894
895 int eof = FALSE; /* if TRUE, we hit the end of the file */
896 int didline = FALSE; /* if TRUE, we finished the last line */
897 int i;
898 long j;
899 static int recursive = FALSE; /* being called recursively */
900 int old_botline = wp->w_botline;
901#ifdef FEAT_FOLDING
902 long fold_count;
903#endif
904#ifdef FEAT_SYN_HL
905 /* remember what happened to the previous line, to know if
906 * check_visual_highlight() can be used */
907#define DID_NONE 1 /* didn't update a line */
908#define DID_LINE 2 /* updated a normal line */
909#define DID_FOLD 3 /* updated a folded line */
910 int did_update = DID_NONE;
911 linenr_T syntax_last_parsed = 0; /* last parsed text line */
912#endif
913 linenr_T mod_top = 0;
914 linenr_T mod_bot = 0;
915#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
916 int save_got_int;
917#endif
918
919 type = wp->w_redr_type;
920
921 if (type == NOT_VALID)
922 {
923#ifdef FEAT_WINDOWS
924 wp->w_redr_status = TRUE;
925#endif
926 wp->w_lines_valid = 0;
927 }
928
929 /* Window is zero-height: nothing to draw. */
930 if (wp->w_height == 0)
931 {
932 wp->w_redr_type = 0;
933 return;
934 }
935
936#ifdef FEAT_VERTSPLIT
937 /* Window is zero-width: Only need to draw the separator. */
938 if (wp->w_width == 0)
939 {
940 /* draw the vertical separator right of this window */
941 draw_vsep_win(wp, 0);
942 wp->w_redr_type = 0;
943 return;
944 }
945#endif
946
947#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200948 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#endif
950
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000951#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200952 /* Force redraw when width of 'number' or 'relativenumber' column
953 * changes. */
954 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000955 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000956 {
957 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000958 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000959 }
960 else
961#endif
962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
964 {
965 /*
966 * When there are both inserted/deleted lines and specific lines to be
967 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
968 * everything (only happens when redrawing is off for while).
969 */
970 type = NOT_VALID;
971 }
972 else
973 {
974 /*
975 * Set mod_top to the first line that needs displaying because of
976 * changes. Set mod_bot to the first line after the changes.
977 */
978 mod_top = wp->w_redraw_top;
979 if (wp->w_redraw_bot != 0)
980 mod_bot = wp->w_redraw_bot + 1;
981 else
982 mod_bot = 0;
983 wp->w_redraw_top = 0; /* reset for next time */
984 wp->w_redraw_bot = 0;
985 if (buf->b_mod_set)
986 {
987 if (mod_top == 0 || mod_top > buf->b_mod_top)
988 {
989 mod_top = buf->b_mod_top;
990#ifdef FEAT_SYN_HL
991 /* Need to redraw lines above the change that may be included
992 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200993 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200995 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (mod_top < 1)
997 mod_top = 1;
998 }
999#endif
1000 }
1001 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1002 mod_bot = buf->b_mod_bot;
1003
1004#ifdef FEAT_SEARCH_EXTRA
1005 /* When 'hlsearch' is on and using a multi-line search pattern, a
1006 * change in one line may make the Search highlighting in a
1007 * previous line invalid. Simple solution: redraw all visible
1008 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001009 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001011 if (search_hl.rm.regprog != NULL
1012 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001014 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001015 {
1016 cur = wp->w_match_head;
1017 while (cur != NULL)
1018 {
1019 if (cur->match.regprog != NULL
1020 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001021 {
1022 top_to_mod = TRUE;
1023 break;
1024 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001025 cur = cur->next;
1026 }
1027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#endif
1029 }
1030#ifdef FEAT_FOLDING
1031 if (mod_top != 0 && hasAnyFolding(wp))
1032 {
1033 linenr_T lnumt, lnumb;
1034
1035 /*
1036 * A change in a line can cause lines above it to become folded or
1037 * unfolded. Find the top most buffer line that may be affected.
1038 * If the line was previously folded and displayed, get the first
1039 * line of that fold. If the line is folded now, get the first
1040 * folded line. Use the minimum of these two.
1041 */
1042
1043 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1044 * the line below it. If there is no valid entry, use w_topline.
1045 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1046 * to this line. If there is no valid entry, use MAXLNUM. */
1047 lnumt = wp->w_topline;
1048 lnumb = MAXLNUM;
1049 for (i = 0; i < wp->w_lines_valid; ++i)
1050 if (wp->w_lines[i].wl_valid)
1051 {
1052 if (wp->w_lines[i].wl_lastlnum < mod_top)
1053 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1054 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1055 {
1056 lnumb = wp->w_lines[i].wl_lnum;
1057 /* When there is a fold column it might need updating
1058 * in the next line ("J" just above an open fold). */
1059 if (wp->w_p_fdc > 0)
1060 ++lnumb;
1061 }
1062 }
1063
1064 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1065 if (mod_top > lnumt)
1066 mod_top = lnumt;
1067
1068 /* Now do the same for the bottom line (one above mod_bot). */
1069 --mod_bot;
1070 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1071 ++mod_bot;
1072 if (mod_bot < lnumb)
1073 mod_bot = lnumb;
1074 }
1075#endif
1076
1077 /* When a change starts above w_topline and the end is below
1078 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001079 * If the end of the change is above w_topline: do like no change was
1080 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (mod_top != 0 && mod_top < wp->w_topline)
1082 {
1083 if (mod_bot > wp->w_topline)
1084 mod_top = wp->w_topline;
1085#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001086 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 top_end = 1;
1088#endif
1089 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001090
1091 /* When line numbers are displayed need to redraw all lines below
1092 * inserted/deleted lines. */
1093 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1094 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096
1097 /*
1098 * When only displaying the lines at the top, set top_end. Used when
1099 * window has scrolled down for msg_scrolled.
1100 */
1101 if (type == REDRAW_TOP)
1102 {
1103 j = 0;
1104 for (i = 0; i < wp->w_lines_valid; ++i)
1105 {
1106 j += wp->w_lines[i].wl_size;
1107 if (j >= wp->w_upd_rows)
1108 {
1109 top_end = j;
1110 break;
1111 }
1112 }
1113 if (top_end == 0)
1114 /* not found (cannot happen?): redraw everything */
1115 type = NOT_VALID;
1116 else
1117 /* top area defined, the rest is VALID */
1118 type = VALID;
1119 }
1120
Bram Moolenaar367329b2007-08-30 11:53:22 +00001121 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001122 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1123 * non-zero and thus not FALSE) will indicate that screenclear() was not
1124 * called. */
1125 if (screen_cleared)
1126 screen_cleared = MAYBE;
1127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 /*
1129 * If there are no changes on the screen that require a complete redraw,
1130 * handle three cases:
1131 * 1: we are off the top of the screen by a few lines: scroll down
1132 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1133 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1134 * w_lines[] that needs updating.
1135 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001136 if ((type == VALID || type == SOME_VALID
1137 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef FEAT_DIFF
1139 && !wp->w_botfill && !wp->w_old_botfill
1140#endif
1141 )
1142 {
1143 if (mod_top != 0 && wp->w_topline == mod_top)
1144 {
1145 /*
1146 * w_topline is the first changed line, the scrolling will be done
1147 * further down.
1148 */
1149 }
1150 else if (wp->w_lines[0].wl_valid
1151 && (wp->w_topline < wp->w_lines[0].wl_lnum
1152#ifdef FEAT_DIFF
1153 || (wp->w_topline == wp->w_lines[0].wl_lnum
1154 && wp->w_topfill > wp->w_old_topfill)
1155#endif
1156 ))
1157 {
1158 /*
1159 * New topline is above old topline: May scroll down.
1160 */
1161#ifdef FEAT_FOLDING
1162 if (hasAnyFolding(wp))
1163 {
1164 linenr_T ln;
1165
1166 /* count the number of lines we are off, counting a sequence
1167 * of folded lines as one */
1168 j = 0;
1169 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1170 {
1171 ++j;
1172 if (j >= wp->w_height - 2)
1173 break;
1174 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1175 }
1176 }
1177 else
1178#endif
1179 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1180 if (j < wp->w_height - 2) /* not too far off */
1181 {
1182 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1183#ifdef FEAT_DIFF
1184 /* insert extra lines for previously invisible filler lines */
1185 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1186 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1187 - wp->w_old_topfill;
1188#endif
1189 if (i < wp->w_height - 2) /* less than a screen off */
1190 {
1191 /*
1192 * Try to insert the correct number of lines.
1193 * If not the last window, delete the lines at the bottom.
1194 * win_ins_lines may fail when the terminal can't do it.
1195 */
1196 if (i > 0)
1197 check_for_delay(FALSE);
1198 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1199 {
1200 if (wp->w_lines_valid != 0)
1201 {
1202 /* Need to update rows that are new, stop at the
1203 * first one that scrolled down. */
1204 top_end = i;
1205#ifdef FEAT_VISUAL
1206 scrolled_down = TRUE;
1207#endif
1208
1209 /* Move the entries that were scrolled, disable
1210 * the entries for the lines to be redrawn. */
1211 if ((wp->w_lines_valid += j) > wp->w_height)
1212 wp->w_lines_valid = wp->w_height;
1213 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1214 wp->w_lines[idx] = wp->w_lines[idx - j];
1215 while (idx >= 0)
1216 wp->w_lines[idx--].wl_valid = FALSE;
1217 }
1218 }
1219 else
1220 mid_start = 0; /* redraw all lines */
1221 }
1222 else
1223 mid_start = 0; /* redraw all lines */
1224 }
1225 else
1226 mid_start = 0; /* redraw all lines */
1227 }
1228 else
1229 {
1230 /*
1231 * New topline is at or below old topline: May scroll up.
1232 * When topline didn't change, find first entry in w_lines[] that
1233 * needs updating.
1234 */
1235
1236 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1237 j = -1;
1238 row = 0;
1239 for (i = 0; i < wp->w_lines_valid; i++)
1240 {
1241 if (wp->w_lines[i].wl_valid
1242 && wp->w_lines[i].wl_lnum == wp->w_topline)
1243 {
1244 j = i;
1245 break;
1246 }
1247 row += wp->w_lines[i].wl_size;
1248 }
1249 if (j == -1)
1250 {
1251 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1252 * lines */
1253 mid_start = 0;
1254 }
1255 else
1256 {
1257 /*
1258 * Try to delete the correct number of lines.
1259 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1260 */
1261#ifdef FEAT_DIFF
1262 /* If the topline didn't change, delete old filler lines,
1263 * otherwise delete filler lines of the new topline... */
1264 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1265 row += wp->w_old_topfill;
1266 else
1267 row += diff_check_fill(wp, wp->w_topline);
1268 /* ... but don't delete new filler lines. */
1269 row -= wp->w_topfill;
1270#endif
1271 if (row > 0)
1272 {
1273 check_for_delay(FALSE);
1274 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1275 bot_start = wp->w_height - row;
1276 else
1277 mid_start = 0; /* redraw all lines */
1278 }
1279 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1280 {
1281 /*
1282 * Skip the lines (below the deleted lines) that are still
1283 * valid and don't need redrawing. Copy their info
1284 * upwards, to compensate for the deleted lines. Set
1285 * bot_start to the first row that needs redrawing.
1286 */
1287 bot_start = 0;
1288 idx = 0;
1289 for (;;)
1290 {
1291 wp->w_lines[idx] = wp->w_lines[j];
1292 /* stop at line that didn't fit, unless it is still
1293 * valid (no lines deleted) */
1294 if (row > 0 && bot_start + row
1295 + (int)wp->w_lines[j].wl_size > wp->w_height)
1296 {
1297 wp->w_lines_valid = idx + 1;
1298 break;
1299 }
1300 bot_start += wp->w_lines[idx++].wl_size;
1301
1302 /* stop at the last valid entry in w_lines[].wl_size */
1303 if (++j >= wp->w_lines_valid)
1304 {
1305 wp->w_lines_valid = idx;
1306 break;
1307 }
1308 }
1309#ifdef FEAT_DIFF
1310 /* Correct the first entry for filler lines at the top
1311 * when it won't get updated below. */
1312 if (wp->w_p_diff && bot_start > 0)
1313 wp->w_lines[0].wl_size =
1314 plines_win_nofill(wp, wp->w_topline, TRUE)
1315 + wp->w_topfill;
1316#endif
1317 }
1318 }
1319 }
1320
1321 /* When starting redraw in the first line, redraw all lines. When
1322 * there is only one window it's probably faster to clear the screen
1323 * first. */
1324 if (mid_start == 0)
1325 {
1326 mid_end = wp->w_height;
1327 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001328 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001329 /* Clear the screen when it was not done by win_del_lines() or
1330 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1331 * then. */
1332 if (screen_cleared != TRUE)
1333 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001334#ifdef FEAT_WINDOWS
1335 /* The screen was cleared, redraw the tab pages line. */
1336 if (redraw_tabline)
1337 draw_tabline();
1338#endif
1339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001341
1342 /* When win_del_lines() or win_ins_lines() caused the screen to be
1343 * cleared (only happens for the first window) or when screenclear()
1344 * was called directly above, "must_redraw" will have been set to
1345 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1346 if (screen_cleared == TRUE)
1347 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 else
1350 {
1351 /* Not VALID or INVERTED: redraw all lines. */
1352 mid_start = 0;
1353 mid_end = wp->w_height;
1354 }
1355
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001356 if (type == SOME_VALID)
1357 {
1358 /* SOME_VALID: redraw all lines. */
1359 mid_start = 0;
1360 mid_end = wp->w_height;
1361 type = NOT_VALID;
1362 }
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#ifdef FEAT_VISUAL
1365 /* check if we are updating or removing the inverted part */
1366 if ((VIsual_active && buf == curwin->w_buffer)
1367 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1368 {
1369 linenr_T from, to;
1370
1371 if (VIsual_active)
1372 {
1373 if (VIsual_active
1374 && (VIsual_mode != wp->w_old_visual_mode
1375 || type == INVERTED_ALL))
1376 {
1377 /*
1378 * If the type of Visual selection changed, redraw the whole
1379 * selection. Also when the ownership of the X selection is
1380 * gained or lost.
1381 */
1382 if (curwin->w_cursor.lnum < VIsual.lnum)
1383 {
1384 from = curwin->w_cursor.lnum;
1385 to = VIsual.lnum;
1386 }
1387 else
1388 {
1389 from = VIsual.lnum;
1390 to = curwin->w_cursor.lnum;
1391 }
1392 /* redraw more when the cursor moved as well */
1393 if (wp->w_old_cursor_lnum < from)
1394 from = wp->w_old_cursor_lnum;
1395 if (wp->w_old_cursor_lnum > to)
1396 to = wp->w_old_cursor_lnum;
1397 if (wp->w_old_visual_lnum < from)
1398 from = wp->w_old_visual_lnum;
1399 if (wp->w_old_visual_lnum > to)
1400 to = wp->w_old_visual_lnum;
1401 }
1402 else
1403 {
1404 /*
1405 * Find the line numbers that need to be updated: The lines
1406 * between the old cursor position and the current cursor
1407 * position. Also check if the Visual position changed.
1408 */
1409 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1410 {
1411 from = curwin->w_cursor.lnum;
1412 to = wp->w_old_cursor_lnum;
1413 }
1414 else
1415 {
1416 from = wp->w_old_cursor_lnum;
1417 to = curwin->w_cursor.lnum;
1418 if (from == 0) /* Visual mode just started */
1419 from = to;
1420 }
1421
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001422 if (VIsual.lnum != wp->w_old_visual_lnum
1423 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 {
1425 if (wp->w_old_visual_lnum < from
1426 && wp->w_old_visual_lnum != 0)
1427 from = wp->w_old_visual_lnum;
1428 if (wp->w_old_visual_lnum > to)
1429 to = wp->w_old_visual_lnum;
1430 if (VIsual.lnum < from)
1431 from = VIsual.lnum;
1432 if (VIsual.lnum > to)
1433 to = VIsual.lnum;
1434 }
1435 }
1436
1437 /*
1438 * If in block mode and changed column or curwin->w_curswant:
1439 * update all lines.
1440 * First compute the actual start and end column.
1441 */
1442 if (VIsual_mode == Ctrl_V)
1443 {
1444 colnr_T fromc, toc;
1445
1446 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1447 ++toc;
1448 if (curwin->w_curswant == MAXCOL)
1449 toc = MAXCOL;
1450
1451 if (fromc != wp->w_old_cursor_fcol
1452 || toc != wp->w_old_cursor_lcol)
1453 {
1454 if (from > VIsual.lnum)
1455 from = VIsual.lnum;
1456 if (to < VIsual.lnum)
1457 to = VIsual.lnum;
1458 }
1459 wp->w_old_cursor_fcol = fromc;
1460 wp->w_old_cursor_lcol = toc;
1461 }
1462 }
1463 else
1464 {
1465 /* Use the line numbers of the old Visual area. */
1466 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1467 {
1468 from = wp->w_old_cursor_lnum;
1469 to = wp->w_old_visual_lnum;
1470 }
1471 else
1472 {
1473 from = wp->w_old_visual_lnum;
1474 to = wp->w_old_cursor_lnum;
1475 }
1476 }
1477
1478 /*
1479 * There is no need to update lines above the top of the window.
1480 */
1481 if (from < wp->w_topline)
1482 from = wp->w_topline;
1483
1484 /*
1485 * If we know the value of w_botline, use it to restrict the update to
1486 * the lines that are visible in the window.
1487 */
1488 if (wp->w_valid & VALID_BOTLINE)
1489 {
1490 if (from >= wp->w_botline)
1491 from = wp->w_botline - 1;
1492 if (to >= wp->w_botline)
1493 to = wp->w_botline - 1;
1494 }
1495
1496 /*
1497 * Find the minimal part to be updated.
1498 * Watch out for scrolling that made entries in w_lines[] invalid.
1499 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1500 * top_end; need to redraw from top_end to the "to" line.
1501 * A middle mouse click with a Visual selection may change the text
1502 * above the Visual area and reset wl_valid, do count these for
1503 * mid_end (in srow).
1504 */
1505 if (mid_start > 0)
1506 {
1507 lnum = wp->w_topline;
1508 idx = 0;
1509 srow = 0;
1510 if (scrolled_down)
1511 mid_start = top_end;
1512 else
1513 mid_start = 0;
1514 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1515 {
1516 if (wp->w_lines[idx].wl_valid)
1517 mid_start += wp->w_lines[idx].wl_size;
1518 else if (!scrolled_down)
1519 srow += wp->w_lines[idx].wl_size;
1520 ++idx;
1521# ifdef FEAT_FOLDING
1522 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1523 lnum = wp->w_lines[idx].wl_lnum;
1524 else
1525# endif
1526 ++lnum;
1527 }
1528 srow += mid_start;
1529 mid_end = wp->w_height;
1530 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1531 {
1532 if (wp->w_lines[idx].wl_valid
1533 && wp->w_lines[idx].wl_lnum >= to + 1)
1534 {
1535 /* Only update until first row of this line */
1536 mid_end = srow;
1537 break;
1538 }
1539 srow += wp->w_lines[idx].wl_size;
1540 }
1541 }
1542 }
1543
1544 if (VIsual_active && buf == curwin->w_buffer)
1545 {
1546 wp->w_old_visual_mode = VIsual_mode;
1547 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1548 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001549 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 wp->w_old_curswant = curwin->w_curswant;
1551 }
1552 else
1553 {
1554 wp->w_old_visual_mode = 0;
1555 wp->w_old_cursor_lnum = 0;
1556 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001557 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559#endif /* FEAT_VISUAL */
1560
1561#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1562 /* reset got_int, otherwise regexp won't work */
1563 save_got_int = got_int;
1564 got_int = 0;
1565#endif
1566#ifdef FEAT_FOLDING
1567 win_foldinfo.fi_level = 0;
1568#endif
1569
1570 /*
1571 * Update all the window rows.
1572 */
1573 idx = 0; /* first entry in w_lines[].wl_size */
1574 row = 0;
1575 srow = 0;
1576 lnum = wp->w_topline; /* first line shown in window */
1577 for (;;)
1578 {
1579 /* stop updating when reached the end of the window (check for _past_
1580 * the end of the window is at the end of the loop) */
1581 if (row == wp->w_height)
1582 {
1583 didline = TRUE;
1584 break;
1585 }
1586
1587 /* stop updating when hit the end of the file */
1588 if (lnum > buf->b_ml.ml_line_count)
1589 {
1590 eof = TRUE;
1591 break;
1592 }
1593
1594 /* Remember the starting row of the line that is going to be dealt
1595 * with. It is used further down when the line doesn't fit. */
1596 srow = row;
1597
1598 /*
1599 * Update a line when it is in an area that needs updating, when it
1600 * has changes or w_lines[idx] is invalid.
1601 * bot_start may be halfway a wrapped line after using
1602 * win_del_lines(), check if the current line includes it.
1603 * When syntax folding is being used, the saved syntax states will
1604 * already have been updated, we can't see where the syntax state is
1605 * the same again, just update until the end of the window.
1606 */
1607 if (row < top_end
1608 || (row >= mid_start && row < mid_end)
1609#ifdef FEAT_SEARCH_EXTRA
1610 || top_to_mod
1611#endif
1612 || idx >= wp->w_lines_valid
1613 || (row + wp->w_lines[idx].wl_size > bot_start)
1614 || (mod_top != 0
1615 && (lnum == mod_top
1616 || (lnum >= mod_top
1617 && (lnum < mod_bot
1618#ifdef FEAT_SYN_HL
1619 || did_update == DID_FOLD
1620 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001621 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 && (
1623# ifdef FEAT_FOLDING
1624 (foldmethodIsSyntax(wp)
1625 && hasAnyFolding(wp)) ||
1626# endif
1627 syntax_check_changed(lnum)))
1628#endif
1629 )))))
1630 {
1631#ifdef FEAT_SEARCH_EXTRA
1632 if (lnum == mod_top)
1633 top_to_mod = FALSE;
1634#endif
1635
1636 /*
1637 * When at start of changed lines: May scroll following lines
1638 * up or down to minimize redrawing.
1639 * Don't do this when the change continues until the end.
1640 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1641 */
1642 if (lnum == mod_top
1643 && mod_bot != MAXLNUM
1644 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1645 {
1646 int old_rows = 0;
1647 int new_rows = 0;
1648 int xtra_rows;
1649 linenr_T l;
1650
1651 /* Count the old number of window rows, using w_lines[], which
1652 * should still contain the sizes for the lines as they are
1653 * currently displayed. */
1654 for (i = idx; i < wp->w_lines_valid; ++i)
1655 {
1656 /* Only valid lines have a meaningful wl_lnum. Invalid
1657 * lines are part of the changed area. */
1658 if (wp->w_lines[i].wl_valid
1659 && wp->w_lines[i].wl_lnum == mod_bot)
1660 break;
1661 old_rows += wp->w_lines[i].wl_size;
1662#ifdef FEAT_FOLDING
1663 if (wp->w_lines[i].wl_valid
1664 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1665 {
1666 /* Must have found the last valid entry above mod_bot.
1667 * Add following invalid entries. */
1668 ++i;
1669 while (i < wp->w_lines_valid
1670 && !wp->w_lines[i].wl_valid)
1671 old_rows += wp->w_lines[i++].wl_size;
1672 break;
1673 }
1674#endif
1675 }
1676
1677 if (i >= wp->w_lines_valid)
1678 {
1679 /* We can't find a valid line below the changed lines,
1680 * need to redraw until the end of the window.
1681 * Inserting/deleting lines has no use. */
1682 bot_start = 0;
1683 }
1684 else
1685 {
1686 /* Able to count old number of rows: Count new window
1687 * rows, and may insert/delete lines */
1688 j = idx;
1689 for (l = lnum; l < mod_bot; ++l)
1690 {
1691#ifdef FEAT_FOLDING
1692 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1693 ++new_rows;
1694 else
1695#endif
1696#ifdef FEAT_DIFF
1697 if (l == wp->w_topline)
1698 new_rows += plines_win_nofill(wp, l, TRUE)
1699 + wp->w_topfill;
1700 else
1701#endif
1702 new_rows += plines_win(wp, l, TRUE);
1703 ++j;
1704 if (new_rows > wp->w_height - row - 2)
1705 {
1706 /* it's getting too much, must redraw the rest */
1707 new_rows = 9999;
1708 break;
1709 }
1710 }
1711 xtra_rows = new_rows - old_rows;
1712 if (xtra_rows < 0)
1713 {
1714 /* May scroll text up. If there is not enough
1715 * remaining text or scrolling fails, must redraw the
1716 * rest. If scrolling works, must redraw the text
1717 * below the scrolled text. */
1718 if (row - xtra_rows >= wp->w_height - 2)
1719 mod_bot = MAXLNUM;
1720 else
1721 {
1722 check_for_delay(FALSE);
1723 if (win_del_lines(wp, row,
1724 -xtra_rows, FALSE, FALSE) == FAIL)
1725 mod_bot = MAXLNUM;
1726 else
1727 bot_start = wp->w_height + xtra_rows;
1728 }
1729 }
1730 else if (xtra_rows > 0)
1731 {
1732 /* May scroll text down. If there is not enough
1733 * remaining text of scrolling fails, must redraw the
1734 * rest. */
1735 if (row + xtra_rows >= wp->w_height - 2)
1736 mod_bot = MAXLNUM;
1737 else
1738 {
1739 check_for_delay(FALSE);
1740 if (win_ins_lines(wp, row + old_rows,
1741 xtra_rows, FALSE, FALSE) == FAIL)
1742 mod_bot = MAXLNUM;
1743 else if (top_end > row + old_rows)
1744 /* Scrolled the part at the top that requires
1745 * updating down. */
1746 top_end += xtra_rows;
1747 }
1748 }
1749
1750 /* When not updating the rest, may need to move w_lines[]
1751 * entries. */
1752 if (mod_bot != MAXLNUM && i != j)
1753 {
1754 if (j < i)
1755 {
1756 int x = row + new_rows;
1757
1758 /* move entries in w_lines[] upwards */
1759 for (;;)
1760 {
1761 /* stop at last valid entry in w_lines[] */
1762 if (i >= wp->w_lines_valid)
1763 {
1764 wp->w_lines_valid = j;
1765 break;
1766 }
1767 wp->w_lines[j] = wp->w_lines[i];
1768 /* stop at a line that won't fit */
1769 if (x + (int)wp->w_lines[j].wl_size
1770 > wp->w_height)
1771 {
1772 wp->w_lines_valid = j + 1;
1773 break;
1774 }
1775 x += wp->w_lines[j++].wl_size;
1776 ++i;
1777 }
1778 if (bot_start > x)
1779 bot_start = x;
1780 }
1781 else /* j > i */
1782 {
1783 /* move entries in w_lines[] downwards */
1784 j -= i;
1785 wp->w_lines_valid += j;
1786 if (wp->w_lines_valid > wp->w_height)
1787 wp->w_lines_valid = wp->w_height;
1788 for (i = wp->w_lines_valid; i - j >= idx; --i)
1789 wp->w_lines[i] = wp->w_lines[i - j];
1790
1791 /* The w_lines[] entries for inserted lines are
1792 * now invalid, but wl_size may be used above.
1793 * Reset to zero. */
1794 while (i >= idx)
1795 {
1796 wp->w_lines[i].wl_size = 0;
1797 wp->w_lines[i--].wl_valid = FALSE;
1798 }
1799 }
1800 }
1801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
1803
1804#ifdef FEAT_FOLDING
1805 /*
1806 * When lines are folded, display one line for all of them.
1807 * Otherwise, display normally (can be several display lines when
1808 * 'wrap' is on).
1809 */
1810 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1811 if (fold_count != 0)
1812 {
1813 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1814 ++row;
1815 --fold_count;
1816 wp->w_lines[idx].wl_folded = TRUE;
1817 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1818# ifdef FEAT_SYN_HL
1819 did_update = DID_FOLD;
1820# endif
1821 }
1822 else
1823#endif
1824 if (idx < wp->w_lines_valid
1825 && wp->w_lines[idx].wl_valid
1826 && wp->w_lines[idx].wl_lnum == lnum
1827 && lnum > wp->w_topline
1828 && !(dy_flags & DY_LASTLINE)
1829 && srow + wp->w_lines[idx].wl_size > wp->w_height
1830#ifdef FEAT_DIFF
1831 && diff_check_fill(wp, lnum) == 0
1832#endif
1833 )
1834 {
1835 /* This line is not going to fit. Don't draw anything here,
1836 * will draw "@ " lines below. */
1837 row = wp->w_height + 1;
1838 }
1839 else
1840 {
1841#ifdef FEAT_SEARCH_EXTRA
1842 prepare_search_hl(wp, lnum);
1843#endif
1844#ifdef FEAT_SYN_HL
1845 /* Let the syntax stuff know we skipped a few lines. */
1846 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001847 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 syntax_end_parsing(syntax_last_parsed + 1);
1849#endif
1850
1851 /*
1852 * Display one line.
1853 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001854 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856#ifdef FEAT_FOLDING
1857 wp->w_lines[idx].wl_folded = FALSE;
1858 wp->w_lines[idx].wl_lastlnum = lnum;
1859#endif
1860#ifdef FEAT_SYN_HL
1861 did_update = DID_LINE;
1862 syntax_last_parsed = lnum;
1863#endif
1864 }
1865
1866 wp->w_lines[idx].wl_lnum = lnum;
1867 wp->w_lines[idx].wl_valid = TRUE;
1868 if (row > wp->w_height) /* past end of screen */
1869 {
1870 /* we may need the size of that too long line later on */
1871 if (dollar_vcol == 0)
1872 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1873 ++idx;
1874 break;
1875 }
1876 if (dollar_vcol == 0)
1877 wp->w_lines[idx].wl_size = row - srow;
1878 ++idx;
1879#ifdef FEAT_FOLDING
1880 lnum += fold_count + 1;
1881#else
1882 ++lnum;
1883#endif
1884 }
1885 else
1886 {
1887 /* This line does not need updating, advance to the next one */
1888 row += wp->w_lines[idx++].wl_size;
1889 if (row > wp->w_height) /* past end of screen */
1890 break;
1891#ifdef FEAT_FOLDING
1892 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1893#else
1894 ++lnum;
1895#endif
1896#ifdef FEAT_SYN_HL
1897 did_update = DID_NONE;
1898#endif
1899 }
1900
1901 if (lnum > buf->b_ml.ml_line_count)
1902 {
1903 eof = TRUE;
1904 break;
1905 }
1906 }
1907 /*
1908 * End of loop over all window lines.
1909 */
1910
1911
1912 if (idx > wp->w_lines_valid)
1913 wp->w_lines_valid = idx;
1914
1915#ifdef FEAT_SYN_HL
1916 /*
1917 * Let the syntax stuff know we stop parsing here.
1918 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001919 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 syntax_end_parsing(syntax_last_parsed + 1);
1921#endif
1922
1923 /*
1924 * If we didn't hit the end of the file, and we didn't finish the last
1925 * line we were working on, then the line didn't fit.
1926 */
1927 wp->w_empty_rows = 0;
1928#ifdef FEAT_DIFF
1929 wp->w_filler_rows = 0;
1930#endif
1931 if (!eof && !didline)
1932 {
1933 if (lnum == wp->w_topline)
1934 {
1935 /*
1936 * Single line that does not fit!
1937 * Don't overwrite it, it can be edited.
1938 */
1939 wp->w_botline = lnum + 1;
1940 }
1941#ifdef FEAT_DIFF
1942 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1943 {
1944 /* Window ends in filler lines. */
1945 wp->w_botline = lnum;
1946 wp->w_filler_rows = wp->w_height - srow;
1947 }
1948#endif
1949 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1950 {
1951 /*
1952 * Last line isn't finished: Display "@@@" at the end.
1953 */
1954 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1955 W_WINROW(wp) + wp->w_height,
1956 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1957 '@', '@', hl_attr(HLF_AT));
1958 set_empty_rows(wp, srow);
1959 wp->w_botline = lnum;
1960 }
1961 else
1962 {
1963 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1964 wp->w_botline = lnum;
1965 }
1966 }
1967 else
1968 {
1969#ifdef FEAT_VERTSPLIT
1970 draw_vsep_win(wp, row);
1971#endif
1972 if (eof) /* we hit the end of the file */
1973 {
1974 wp->w_botline = buf->b_ml.ml_line_count + 1;
1975#ifdef FEAT_DIFF
1976 j = diff_check_fill(wp, wp->w_botline);
1977 if (j > 0 && !wp->w_botfill)
1978 {
1979 /*
1980 * Display filler lines at the end of the file
1981 */
1982 if (char2cells(fill_diff) > 1)
1983 i = '-';
1984 else
1985 i = fill_diff;
1986 if (row + j > wp->w_height)
1987 j = wp->w_height - row;
1988 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1989 row += j;
1990 }
1991#endif
1992 }
1993 else if (dollar_vcol == 0)
1994 wp->w_botline = lnum;
1995
1996 /* make sure the rest of the screen is blank */
1997 /* put '~'s on rows that aren't part of the file. */
1998 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1999 }
2000
2001 /* Reset the type of redrawing required, the window has been updated. */
2002 wp->w_redr_type = 0;
2003#ifdef FEAT_DIFF
2004 wp->w_old_topfill = wp->w_topfill;
2005 wp->w_old_botfill = wp->w_botfill;
2006#endif
2007
2008 if (dollar_vcol == 0)
2009 {
2010 /*
2011 * There is a trick with w_botline. If we invalidate it on each
2012 * change that might modify it, this will cause a lot of expensive
2013 * calls to plines() in update_topline() each time. Therefore the
2014 * value of w_botline is often approximated, and this value is used to
2015 * compute the value of w_topline. If the value of w_botline was
2016 * wrong, check that the value of w_topline is correct (cursor is on
2017 * the visible part of the text). If it's not, we need to redraw
2018 * again. Mostly this just means scrolling up a few lines, so it
2019 * doesn't look too bad. Only do this for the current window (where
2020 * changes are relevant).
2021 */
2022 wp->w_valid |= VALID_BOTLINE;
2023 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2024 {
2025 recursive = TRUE;
2026 curwin->w_valid &= ~VALID_TOPLINE;
2027 update_topline(); /* may invalidate w_botline again */
2028 if (must_redraw != 0)
2029 {
2030 /* Don't update for changes in buffer again. */
2031 i = curbuf->b_mod_set;
2032 curbuf->b_mod_set = FALSE;
2033 win_update(curwin);
2034 must_redraw = 0;
2035 curbuf->b_mod_set = i;
2036 }
2037 recursive = FALSE;
2038 }
2039 }
2040
2041#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2042 /* restore got_int, unless CTRL-C was hit while redrawing */
2043 if (!got_int)
2044 got_int = save_got_int;
2045#endif
2046}
2047
2048#ifdef FEAT_SIGNS
2049static int draw_signcolumn __ARGS((win_T *wp));
2050
2051/*
2052 * Return TRUE when window "wp" has a column to draw signs in.
2053 */
2054 static int
2055draw_signcolumn(wp)
2056 win_T *wp;
2057{
2058 return (wp->w_buffer->b_signlist != NULL
2059# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002060 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061# endif
2062 );
2063}
2064#endif
2065
2066/*
2067 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2068 * as the filler character.
2069 */
2070 static void
2071win_draw_end(wp, c1, c2, row, endrow, hl)
2072 win_T *wp;
2073 int c1;
2074 int c2;
2075 int row;
2076 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002077 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078{
2079#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2080 int n = 0;
2081# define FDC_OFF n
2082#else
2083# define FDC_OFF 0
2084#endif
2085
2086#ifdef FEAT_RIGHTLEFT
2087 if (wp->w_p_rl)
2088 {
2089 /* No check for cmdline window: should never be right-left. */
2090# ifdef FEAT_FOLDING
2091 n = wp->w_p_fdc;
2092
2093 if (n > 0)
2094 {
2095 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002096 if (n > W_WIDTH(wp))
2097 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2099 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2100 ' ', ' ', hl_attr(HLF_FC));
2101 }
2102# endif
2103# ifdef FEAT_SIGNS
2104 if (draw_signcolumn(wp))
2105 {
2106 int nn = n + 2;
2107
2108 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002109 if (nn > W_WIDTH(wp))
2110 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2112 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2113 ' ', ' ', hl_attr(HLF_SC));
2114 n = nn;
2115 }
2116# endif
2117 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2118 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2119 c2, c2, hl_attr(hl));
2120 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2121 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2122 c1, c2, hl_attr(hl));
2123 }
2124 else
2125#endif
2126 {
2127#ifdef FEAT_CMDWIN
2128 if (cmdwin_type != 0 && wp == curwin)
2129 {
2130 /* draw the cmdline character in the leftmost column */
2131 n = 1;
2132 if (n > wp->w_width)
2133 n = wp->w_width;
2134 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2135 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2136 cmdwin_type, ' ', hl_attr(HLF_AT));
2137 }
2138#endif
2139#ifdef FEAT_FOLDING
2140 if (wp->w_p_fdc > 0)
2141 {
2142 int nn = n + wp->w_p_fdc;
2143
2144 /* draw the fold column at the left */
2145 if (nn > W_WIDTH(wp))
2146 nn = W_WIDTH(wp);
2147 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2148 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2149 ' ', ' ', hl_attr(HLF_FC));
2150 n = nn;
2151 }
2152#endif
2153#ifdef FEAT_SIGNS
2154 if (draw_signcolumn(wp))
2155 {
2156 int nn = n + 2;
2157
2158 /* draw the sign column after the fold column */
2159 if (nn > W_WIDTH(wp))
2160 nn = W_WIDTH(wp);
2161 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2162 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2163 ' ', ' ', hl_attr(HLF_SC));
2164 n = nn;
2165 }
2166#endif
2167 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2168 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2169 c1, c2, hl_attr(hl));
2170 }
2171 set_empty_rows(wp, row);
2172}
2173
Bram Moolenaar1a384422010-07-14 19:53:30 +02002174#ifdef FEAT_SYN_HL
2175static int advance_color_col __ARGS((int vcol, int **color_cols));
2176
2177/*
2178 * Advance **color_cols and return TRUE when there are columns to draw.
2179 */
2180 static int
2181advance_color_col(vcol, color_cols)
2182 int vcol;
2183 int **color_cols;
2184{
2185 while (**color_cols >= 0 && vcol > **color_cols)
2186 ++*color_cols;
2187 return (**color_cols >= 0);
2188}
2189#endif
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#ifdef FEAT_FOLDING
2192/*
2193 * Display one folded line.
2194 */
2195 static void
2196fold_line(wp, fold_count, foldinfo, lnum, row)
2197 win_T *wp;
2198 long fold_count;
2199 foldinfo_T *foldinfo;
2200 linenr_T lnum;
2201 int row;
2202{
2203 char_u buf[51];
2204 pos_T *top, *bot;
2205 linenr_T lnume = lnum + fold_count - 1;
2206 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002207 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 int col;
2210 int txtcol;
2211 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002212 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214 /* Build the fold line:
2215 * 1. Add the cmdwin_type for the command-line window
2216 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002217 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 * 4. Compose the text
2219 * 5. Add the text
2220 * 6. set highlighting for the Visual area an other text
2221 */
2222 col = 0;
2223
2224 /*
2225 * 1. Add the cmdwin_type for the command-line window
2226 * Ignores 'rightleft', this window is never right-left.
2227 */
2228#ifdef FEAT_CMDWIN
2229 if (cmdwin_type != 0 && wp == curwin)
2230 {
2231 ScreenLines[off] = cmdwin_type;
2232 ScreenAttrs[off] = hl_attr(HLF_AT);
2233#ifdef FEAT_MBYTE
2234 if (enc_utf8)
2235 ScreenLinesUC[off] = 0;
2236#endif
2237 ++col;
2238 }
2239#endif
2240
2241 /*
2242 * 2. Add the 'foldcolumn'
2243 */
2244 fdc = wp->w_p_fdc;
2245 if (fdc > W_WIDTH(wp) - col)
2246 fdc = W_WIDTH(wp) - col;
2247 if (fdc > 0)
2248 {
2249 fill_foldcolumn(buf, wp, TRUE, lnum);
2250#ifdef FEAT_RIGHTLEFT
2251 if (wp->w_p_rl)
2252 {
2253 int i;
2254
2255 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2256 hl_attr(HLF_FC));
2257 /* reverse the fold column */
2258 for (i = 0; i < fdc; ++i)
2259 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2260 }
2261 else
2262#endif
2263 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2264 col += fdc;
2265 }
2266
2267#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002268# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2269 for (ri = 0; ri < l; ++ri) \
2270 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2271 else \
2272 for (ri = 0; ri < l; ++ri) \
2273 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002275# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2276 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#endif
2278
Bram Moolenaar64486672010-05-16 15:46:46 +02002279 /* Set all attributes of the 'number' or 'relativenumber' column and the
2280 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002281 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283#ifdef FEAT_SIGNS
2284 /* If signs are being displayed, add two spaces. */
2285 if (draw_signcolumn(wp))
2286 {
2287 len = W_WIDTH(wp) - col;
2288 if (len > 0)
2289 {
2290 if (len > 2)
2291 len = 2;
2292# ifdef FEAT_RIGHTLEFT
2293 if (wp->w_p_rl)
2294 /* the line number isn't reversed */
2295 copy_text_attr(off + W_WIDTH(wp) - len - col,
2296 (char_u *)" ", len, hl_attr(HLF_FL));
2297 else
2298# endif
2299 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2300 col += len;
2301 }
2302 }
2303#endif
2304
2305 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002306 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002308 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 len = W_WIDTH(wp) - col;
2311 if (len > 0)
2312 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002313 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002314 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002315
2316 if (len > w + 1)
2317 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002318
2319 if (wp->w_p_nu)
2320 /* 'number' */
2321 num = (long)lnum;
2322 else
2323 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002324 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02002325
2326 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327#ifdef FEAT_RIGHTLEFT
2328 if (wp->w_p_rl)
2329 /* the line number isn't reversed */
2330 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2331 hl_attr(HLF_FL));
2332 else
2333#endif
2334 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2335 col += len;
2336 }
2337 }
2338
2339 /*
2340 * 4. Compose the folded-line string with 'foldtext', if set.
2341 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002342 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344 txtcol = col; /* remember where text starts */
2345
2346 /*
2347 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2348 * Right-left text is put in columns 0 - number-col, normal text is put
2349 * in columns number-col - window-width.
2350 */
2351#ifdef FEAT_MBYTE
2352 if (has_mbyte)
2353 {
2354 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002355 int u8c, u8cc[MAX_MCO];
2356 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int idx;
2358 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002359 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360# ifdef FEAT_ARABIC
2361 int prev_c = 0; /* previous Arabic character */
2362 int prev_c1 = 0; /* first composing char for prev_c */
2363# endif
2364
2365# ifdef FEAT_RIGHTLEFT
2366 if (wp->w_p_rl)
2367 idx = off;
2368 else
2369# endif
2370 idx = off + col;
2371
2372 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2373 for (p = text; *p != NUL; )
2374 {
2375 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002376 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (col + cells > W_WIDTH(wp)
2378# ifdef FEAT_RIGHTLEFT
2379 - (wp->w_p_rl ? col : 0)
2380# endif
2381 )
2382 break;
2383 ScreenLines[idx] = *p;
2384 if (enc_utf8)
2385 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002386 u8c = utfc_ptr2char(p, u8cc);
2387 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
2389 ScreenLinesUC[idx] = 0;
2390#ifdef FEAT_ARABIC
2391 prev_c = u8c;
2392#endif
2393 }
2394 else
2395 {
2396#ifdef FEAT_ARABIC
2397 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2398 {
2399 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002400 int pc, pc1, nc;
2401 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 int firstbyte = *p;
2403
2404 /* The idea of what is the previous and next
2405 * character depends on 'rightleft'. */
2406 if (wp->w_p_rl)
2407 {
2408 pc = prev_c;
2409 pc1 = prev_c1;
2410 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002411 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413 else
2414 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002415 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002417 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
2419 prev_c = u8c;
2420
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002421 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 pc, pc1, nc);
2423 ScreenLines[idx] = firstbyte;
2424 }
2425 else
2426 prev_c = u8c;
2427#endif
2428 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002429#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (u8c >= 0x10000)
2431 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2432 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002435 for (i = 0; i < Screen_mco; ++i)
2436 {
2437 ScreenLinesC[i][idx] = u8cc[i];
2438 if (u8cc[i] == 0)
2439 break;
2440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
2442 if (cells > 1)
2443 ScreenLines[idx + 1] = 0;
2444 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002445 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2446 /* double-byte single width character */
2447 ScreenLines2[idx] = p[1];
2448 else if (cells > 1)
2449 /* double-width character */
2450 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 col += cells;
2452 idx += cells;
2453 p += c_len;
2454 }
2455 }
2456 else
2457#endif
2458 {
2459 len = (int)STRLEN(text);
2460 if (len > W_WIDTH(wp) - col)
2461 len = W_WIDTH(wp) - col;
2462 if (len > 0)
2463 {
2464#ifdef FEAT_RIGHTLEFT
2465 if (wp->w_p_rl)
2466 STRNCPY(current_ScreenLine, text, len);
2467 else
2468#endif
2469 STRNCPY(current_ScreenLine + col, text, len);
2470 col += len;
2471 }
2472 }
2473
2474 /* Fill the rest of the line with the fold filler */
2475#ifdef FEAT_RIGHTLEFT
2476 if (wp->w_p_rl)
2477 col -= txtcol;
2478#endif
2479 while (col < W_WIDTH(wp)
2480#ifdef FEAT_RIGHTLEFT
2481 - (wp->w_p_rl ? txtcol : 0)
2482#endif
2483 )
2484 {
2485#ifdef FEAT_MBYTE
2486 if (enc_utf8)
2487 {
2488 if (fill_fold >= 0x80)
2489 {
2490 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002491 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493 else
2494 ScreenLinesUC[off + col] = 0;
2495 }
2496#endif
2497 ScreenLines[off + col++] = fill_fold;
2498 }
2499
2500 if (text != buf)
2501 vim_free(text);
2502
2503 /*
2504 * 6. set highlighting for the Visual area an other text.
2505 * If all folded lines are in the Visual area, highlight the line.
2506 */
2507#ifdef FEAT_VISUAL
2508 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2509 {
2510 if (ltoreq(curwin->w_cursor, VIsual))
2511 {
2512 /* Visual is after curwin->w_cursor */
2513 top = &curwin->w_cursor;
2514 bot = &VIsual;
2515 }
2516 else
2517 {
2518 /* Visual is before curwin->w_cursor */
2519 top = &VIsual;
2520 bot = &curwin->w_cursor;
2521 }
2522 if (lnum >= top->lnum
2523 && lnume <= bot->lnum
2524 && (VIsual_mode != 'v'
2525 || ((lnum > top->lnum
2526 || (lnum == top->lnum
2527 && top->col == 0))
2528 && (lnume < bot->lnum
2529 || (lnume == bot->lnum
2530 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002531 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 if (VIsual_mode == Ctrl_V)
2534 {
2535 /* Visual block mode: highlight the chars part of the block */
2536 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2537 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002538 if (wp->w_old_cursor_lcol != MAXCOL
2539 && wp->w_old_cursor_lcol + txtcol
2540 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 len = wp->w_old_cursor_lcol;
2542 else
2543 len = W_WIDTH(wp) - txtcol;
2544 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002545 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547 }
2548 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002551 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554 }
2555#endif
2556
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002557#ifdef FEAT_SYN_HL
2558 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002559 if (wp->w_p_cuc)
2560 {
2561 txtcol += wp->w_virtcol;
2562 if (wp->w_p_wrap)
2563 txtcol -= wp->w_skipcol;
2564 else
2565 txtcol -= wp->w_leftcol;
2566 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2567 ScreenAttrs[off + txtcol] = hl_combine_attr(
2568 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2569 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
2572 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2573 (int)W_WIDTH(wp), FALSE);
2574
2575 /*
2576 * Update w_cline_height and w_cline_folded if the cursor line was
2577 * updated (saves a call to plines() later).
2578 */
2579 if (wp == curwin
2580 && lnum <= curwin->w_cursor.lnum
2581 && lnume >= curwin->w_cursor.lnum)
2582 {
2583 curwin->w_cline_row = row;
2584 curwin->w_cline_height = 1;
2585 curwin->w_cline_folded = TRUE;
2586 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2587 }
2588}
2589
2590/*
2591 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2592 */
2593 static void
2594copy_text_attr(off, buf, len, attr)
2595 int off;
2596 char_u *buf;
2597 int len;
2598 int attr;
2599{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002600 int i;
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 mch_memmove(ScreenLines + off, buf, (size_t)len);
2603# ifdef FEAT_MBYTE
2604 if (enc_utf8)
2605 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2606# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002607 for (i = 0; i < len; ++i)
2608 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609}
2610
2611/*
2612 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002613 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 */
2615 static void
2616fill_foldcolumn(p, wp, closed, lnum)
2617 char_u *p;
2618 win_T *wp;
2619 int closed; /* TRUE of FALSE */
2620 linenr_T lnum; /* current line number */
2621{
2622 int i = 0;
2623 int level;
2624 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002625 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 /* Init to all spaces. */
2628 copy_spaces(p, (size_t)wp->w_p_fdc);
2629
2630 level = win_foldinfo.fi_level;
2631 if (level > 0)
2632 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002633 /* If there is only one column put more info in it. */
2634 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 /* If the column is too narrow, we start at the lowest level that
2637 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002638 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (first_level < 1)
2640 first_level = 1;
2641
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002642 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 {
2644 if (win_foldinfo.fi_lnum == lnum
2645 && first_level + i >= win_foldinfo.fi_low_level)
2646 p[i] = '-';
2647 else if (first_level == 1)
2648 p[i] = '|';
2649 else if (first_level + i <= 9)
2650 p[i] = '0' + first_level + i;
2651 else
2652 p[i] = '>';
2653 if (first_level + i == level)
2654 break;
2655 }
2656 }
2657 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002658 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659}
2660#endif /* FEAT_FOLDING */
2661
2662/*
2663 * Display line "lnum" of window 'wp' on the screen.
2664 * Start at row "startrow", stop when "endrow" is reached.
2665 * wp->w_virtcol needs to be valid.
2666 *
2667 * Return the number of last row the line occupies.
2668 */
2669 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002670win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 win_T *wp;
2672 linenr_T lnum;
2673 int startrow;
2674 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002675 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
2677 int col; /* visual column on screen */
2678 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2679 int c = 0; /* init for GCC */
2680 long vcol = 0; /* virtual column (for tabs) */
2681 long vcol_prev = -1; /* "vcol" of previous character */
2682 char_u *line; /* current line */
2683 char_u *ptr; /* current position in "line" */
2684 int row; /* row in the window, excl w_winrow */
2685 int screen_row; /* row on the screen, incl w_winrow */
2686
2687 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2688 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002689 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 int c_extra = NUL; /* extra chars, all the same */
2691 int extra_attr = 0; /* attributes when n_extra != 0 */
2692 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2693 displaying lcs_eol at end-of-line */
2694 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2695 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2696
2697 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2698 int saved_n_extra = 0;
2699 char_u *saved_p_extra = NULL;
2700 int saved_c_extra = 0;
2701 int saved_char_attr = 0;
2702
2703 int n_attr = 0; /* chars with special attr */
2704 int saved_attr2 = 0; /* char_attr saved for n_attr */
2705 int n_attr3 = 0; /* chars with overruling special attr */
2706 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2707
2708 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2709
2710 int fromcol, tocol; /* start/end of inverting */
2711 int fromcol_prev = -2; /* start of inverting after cursor */
2712 int noinvcur = FALSE; /* don't invert the cursor */
2713#ifdef FEAT_VISUAL
2714 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002715 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#endif
2717 pos_T pos;
2718 long v;
2719
2720 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002721 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2723 in this line */
2724 int attr = 0; /* attributes for area highlighting */
2725 int area_attr = 0; /* attributes desired by highlighting */
2726 int search_attr = 0; /* attributes desired by 'hlsearch' */
2727#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002728 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 int syntax_attr = 0; /* attributes desired by syntax */
2730 int has_syntax = FALSE; /* this buffer has syntax highl. */
2731 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002732 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002733 int draw_color_col = FALSE; /* highlight colorcolumn */
2734 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002735#endif
2736#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002737 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002738# define SPWORDLEN 150
2739 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002740 int nextlinecol = 0; /* column where nextline[] starts */
2741 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002742 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002743 int spell_attr = 0; /* attributes desired by spelling */
2744 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002745 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2746 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002747 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002748 static int cap_col = -1; /* column to check for Cap word */
2749 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002750 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#endif
2752 int extra_check; /* has syntax or linebreak */
2753#ifdef FEAT_MBYTE
2754 int multi_attr = 0; /* attributes desired by multibyte */
2755 int mb_l = 1; /* multi-byte byte length */
2756 int mb_c = 0; /* decoded multi-byte character */
2757 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002758 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759#endif
2760#ifdef FEAT_DIFF
2761 int filler_lines; /* nr of filler lines to be drawn */
2762 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002763 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 int change_start = MAXCOL; /* first col of changed area */
2765 int change_end = -1; /* last col of changed area */
2766#endif
2767 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2768#ifdef FEAT_LINEBREAK
2769 int need_showbreak = FALSE;
2770#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002771#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2772 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002774 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775#endif
2776#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002777 matchitem_T *cur; /* points to the match list */
2778 match_T *shl; /* points to search_hl or a match */
2779 int shl_flag; /* flag to indicate whether search_hl
2780 has been processed or not */
2781 int prevcol_hl_flag; /* flag to indicate whether prevcol
2782 equals startcol of search_hl or one
2783 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784#endif
2785#ifdef FEAT_ARABIC
2786 int prev_c = 0; /* previous Arabic character */
2787 int prev_c1 = 0; /* first composing char for prev_c */
2788#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002789#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002790 int did_line_attr = 0;
2791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
2793 /* draw_state: items that are drawn in sequence: */
2794#define WL_START 0 /* nothing done yet */
2795#ifdef FEAT_CMDWIN
2796# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2797#else
2798# define WL_CMDLINE WL_START
2799#endif
2800#ifdef FEAT_FOLDING
2801# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2802#else
2803# define WL_FOLD WL_CMDLINE
2804#endif
2805#ifdef FEAT_SIGNS
2806# define WL_SIGN WL_FOLD + 1 /* column for signs */
2807#else
2808# define WL_SIGN WL_FOLD /* column for signs */
2809#endif
2810#define WL_NR WL_SIGN + 1 /* line number */
2811#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2812# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2813#else
2814# define WL_SBR WL_NR
2815#endif
2816#define WL_LINE WL_SBR + 1 /* text in the line */
2817 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002818#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 int feedback_col = 0;
2820 int feedback_old_attr = -1;
2821#endif
2822
Bram Moolenaar860cae12010-06-05 23:22:07 +02002823#ifdef FEAT_CONCEAL
2824 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002825 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002826 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002827 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002828 int is_concealing = FALSE;
2829 int boguscols = 0; /* nonexistent columns added to force
2830 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002831 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002832 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002833# define VCOL_HLC (vcol - vcol_off)
2834#else
2835# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 if (startrow > endrow) /* past the end already! */
2839 return startrow;
2840
2841 row = startrow;
2842 screen_row = row + W_WINROW(wp);
2843
2844 /*
2845 * To speed up the loop below, set extra_check when there is linebreak,
2846 * trailing white space and/or syntax processing to be done.
2847 */
2848#ifdef FEAT_LINEBREAK
2849 extra_check = wp->w_p_lbr;
2850#else
2851 extra_check = 0;
2852#endif
2853#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002854 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
2856 /* Prepare for syntax highlighting in this line. When there is an
2857 * error, stop syntax highlighting. */
2858 save_did_emsg = did_emsg;
2859 did_emsg = FALSE;
2860 syntax_start(wp, lnum);
2861 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002862 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 else
2864 {
2865 did_emsg = save_did_emsg;
2866 has_syntax = TRUE;
2867 extra_check = TRUE;
2868 }
2869 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002870
2871 /* Check for columns to display for 'colorcolumn'. */
2872 color_cols = wp->w_p_cc_cols;
2873 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002874 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002875#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002876
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002877#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002878 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002879 && *wp->w_s->b_p_spl != NUL
2880 && wp->w_s->b_langp.ga_len > 0
2881 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002882 {
2883 /* Prepare for spell checking. */
2884 has_spell = TRUE;
2885 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002886
2887 /* Get the start of the next line, so that words that wrap to the next
2888 * line are found too: "et<line-break>al.".
2889 * Trick: skip a few chars for C/shell/Vim comments */
2890 nextline[SPWORDLEN] = NUL;
2891 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2892 {
2893 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2894 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2895 }
2896
2897 /* When a word wrapped from the previous line the start of the current
2898 * line is valid. */
2899 if (lnum == checked_lnum)
2900 cur_checked_col = checked_col;
2901 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002902
2903 /* When there was a sentence end in the previous line may require a
2904 * word starting with capital in this line. In line 1 always check
2905 * the first word. */
2906 if (lnum != capcol_lnum)
2907 cap_col = -1;
2908 if (lnum == 1)
2909 cap_col = 0;
2910 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#endif
2913
2914 /*
2915 * handle visual active in this window
2916 */
2917 fromcol = -10;
2918 tocol = MAXCOL;
2919#ifdef FEAT_VISUAL
2920 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2921 {
2922 /* Visual is after curwin->w_cursor */
2923 if (ltoreq(curwin->w_cursor, VIsual))
2924 {
2925 top = &curwin->w_cursor;
2926 bot = &VIsual;
2927 }
2928 else /* Visual is before curwin->w_cursor */
2929 {
2930 top = &VIsual;
2931 bot = &curwin->w_cursor;
2932 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002933 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 if (VIsual_mode == Ctrl_V) /* block mode */
2935 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002936 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 {
2938 fromcol = wp->w_old_cursor_fcol;
2939 tocol = wp->w_old_cursor_lcol;
2940 }
2941 }
2942 else /* non-block mode */
2943 {
2944 if (lnum > top->lnum && lnum <= bot->lnum)
2945 fromcol = 0;
2946 else if (lnum == top->lnum)
2947 {
2948 if (VIsual_mode == 'V') /* linewise */
2949 fromcol = 0;
2950 else
2951 {
2952 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2953 if (gchar_pos(top) == NUL)
2954 tocol = fromcol + 1;
2955 }
2956 }
2957 if (VIsual_mode != 'V' && lnum == bot->lnum)
2958 {
2959 if (*p_sel == 'e' && bot->col == 0
2960#ifdef FEAT_VIRTUALEDIT
2961 && bot->coladd == 0
2962#endif
2963 )
2964 {
2965 fromcol = -10;
2966 tocol = MAXCOL;
2967 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002968 else if (bot->col == MAXCOL)
2969 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 else
2971 {
2972 pos = *bot;
2973 if (*p_sel == 'e')
2974 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2975 else
2976 {
2977 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2978 ++tocol;
2979 }
2980 }
2981 }
2982 }
2983
2984#ifndef MSDOS
2985 /* Check if the character under the cursor should not be inverted */
2986 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2987# ifdef FEAT_GUI
2988 && !gui.in_use
2989# endif
2990 )
2991 noinvcur = TRUE;
2992#endif
2993
2994 /* if inverting in this line set area_highlighting */
2995 if (fromcol >= 0)
2996 {
2997 area_highlighting = TRUE;
2998 attr = hl_attr(HLF_V);
2999#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
3000 if (clip_star.available && !clip_star.owned && clip_isautosel())
3001 attr = hl_attr(HLF_VNC);
3002#endif
3003 }
3004 }
3005
3006 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003007 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
3009 else
3010#endif /* FEAT_VISUAL */
3011 if (highlight_match
3012 && wp == curwin
3013 && lnum >= curwin->w_cursor.lnum
3014 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3015 {
3016 if (lnum == curwin->w_cursor.lnum)
3017 getvcol(curwin, &(curwin->w_cursor),
3018 (colnr_T *)&fromcol, NULL, NULL);
3019 else
3020 fromcol = 0;
3021 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3022 {
3023 pos.lnum = lnum;
3024 pos.col = search_match_endcol;
3025 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3026 }
3027 else
3028 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003029 /* do at least one character; happens when past end of line */
3030 if (fromcol == tocol)
3031 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 area_highlighting = TRUE;
3033 attr = hl_attr(HLF_I);
3034 }
3035
3036#ifdef FEAT_DIFF
3037 filler_lines = diff_check(wp, lnum);
3038 if (filler_lines < 0)
3039 {
3040 if (filler_lines == -1)
3041 {
3042 if (diff_find_change(wp, lnum, &change_start, &change_end))
3043 diff_hlf = HLF_ADD; /* added line */
3044 else if (change_start == 0)
3045 diff_hlf = HLF_TXD; /* changed text */
3046 else
3047 diff_hlf = HLF_CHD; /* changed line */
3048 }
3049 else
3050 diff_hlf = HLF_ADD; /* added line */
3051 filler_lines = 0;
3052 area_highlighting = TRUE;
3053 }
3054 if (lnum == wp->w_topline)
3055 filler_lines = wp->w_topfill;
3056 filler_todo = filler_lines;
3057#endif
3058
3059#ifdef LINE_ATTR
3060# ifdef FEAT_SIGNS
3061 /* If this line has a sign with line highlighting set line_attr. */
3062 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3063 if (v != 0)
3064 line_attr = sign_get_attr((int)v, TRUE);
3065# endif
3066# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3067 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003068 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 line_attr = hl_attr(HLF_L);
3070# endif
3071 if (line_attr != 0)
3072 area_highlighting = TRUE;
3073#endif
3074
3075 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3076 ptr = line;
3077
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003078#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003079 if (has_spell)
3080 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003081 /* For checking first word with a capital skip white space. */
3082 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003083 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003084
Bram Moolenaar30abd282005-06-22 22:35:10 +00003085 /* To be able to spell-check over line boundaries copy the end of the
3086 * current line into nextline[]. Above the start of the next line was
3087 * copied to nextline[SPWORDLEN]. */
3088 if (nextline[SPWORDLEN] == NUL)
3089 {
3090 /* No next line or it is empty. */
3091 nextlinecol = MAXCOL;
3092 nextline_idx = 0;
3093 }
3094 else
3095 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003096 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003097 if (v < SPWORDLEN)
3098 {
3099 /* Short line, use it completely and append the start of the
3100 * next line. */
3101 nextlinecol = 0;
3102 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003103 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003104 nextline_idx = v + 1;
3105 }
3106 else
3107 {
3108 /* Long line, use only the last SPWORDLEN bytes. */
3109 nextlinecol = v - SPWORDLEN;
3110 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3111 nextline_idx = SPWORDLEN + 1;
3112 }
3113 }
3114 }
3115#endif
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 /* find start of trailing whitespace */
3118 if (wp->w_p_list && lcs_trail)
3119 {
3120 trailcol = (colnr_T)STRLEN(ptr);
3121 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3122 --trailcol;
3123 trailcol += (colnr_T) (ptr - line);
3124 extra_check = TRUE;
3125 }
3126
3127 /*
3128 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3129 * first character to be displayed.
3130 */
3131 if (wp->w_p_wrap)
3132 v = wp->w_skipcol;
3133 else
3134 v = wp->w_leftcol;
3135 if (v > 0)
3136 {
3137#ifdef FEAT_MBYTE
3138 char_u *prev_ptr = ptr;
3139#endif
3140 while (vcol < v && *ptr != NUL)
3141 {
3142 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3143 vcol += c;
3144#ifdef FEAT_MBYTE
3145 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003147 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
3149
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003150#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3151 /* When:
3152 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003153 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003154 * - 'virtualedit' is set, or
3155 * - the visual mode is active,
3156 * the end of the line may be before the start of the displayed part.
3157 */
3158 if (vcol < v && (
3159# ifdef FEAT_SYN_HL
3160 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003161 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003162# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3163 ||
3164# endif
3165# endif
3166# ifdef FEAT_VIRTUALEDIT
3167 virtual_active()
3168# ifdef FEAT_VISUAL
3169 ||
3170# endif
3171# endif
3172# ifdef FEAT_VISUAL
3173 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3174# endif
3175 ))
3176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179#endif
3180
3181 /* Handle a character that's not completely on the screen: Put ptr at
3182 * that character but skip the first few screen characters. */
3183 if (vcol > v)
3184 {
3185 vcol -= c;
3186#ifdef FEAT_MBYTE
3187 ptr = prev_ptr;
3188#else
3189 --ptr;
3190#endif
3191 n_skip = v - vcol;
3192 }
3193
3194 /*
3195 * Adjust for when the inverted text is before the screen,
3196 * and when the start of the inverted text is before the screen.
3197 */
3198 if (tocol <= vcol)
3199 fromcol = 0;
3200 else if (fromcol >= 0 && fromcol < vcol)
3201 fromcol = vcol;
3202
3203#ifdef FEAT_LINEBREAK
3204 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3205 if (wp->w_p_wrap)
3206 need_showbreak = TRUE;
3207#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003208#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003209 /* When spell checking a word we need to figure out the start of the
3210 * word and if it's badly spelled or not. */
3211 if (has_spell)
3212 {
3213 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003214 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003215 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003216
3217 pos = wp->w_cursor;
3218 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003219 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003220 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003221
3222 /* spell_move_to() may call ml_get() and make "line" invalid */
3223 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3224 ptr = line + linecol;
3225
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003226 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003227 {
3228 /* no bad word found at line start, don't check until end of a
3229 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003230 spell_hlf = HLF_COUNT;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003231 word_end = (int)(spell_to_word_end(ptr, wp)
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003232 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003233 }
3234 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003235 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003236 /* bad word found, use attributes until end of word */
3237 word_end = wp->w_cursor.col + len + 1;
3238
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003239 /* Turn index into actual attributes. */
3240 if (spell_hlf != HLF_COUNT)
3241 spell_attr = highlight_attr[spell_hlf];
3242 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003243 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003244
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003245# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003246 /* Need to restart syntax highlighting for this line. */
3247 if (has_syntax)
3248 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003249# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003250 }
3251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
3253
3254 /*
3255 * Correct highlighting for cursor that can't be disabled.
3256 * Avoids having to check this for each character.
3257 */
3258 if (fromcol >= 0)
3259 {
3260 if (noinvcur)
3261 {
3262 if ((colnr_T)fromcol == wp->w_virtcol)
3263 {
3264 /* highlighting starts at cursor, let it start just after the
3265 * cursor */
3266 fromcol_prev = fromcol;
3267 fromcol = -1;
3268 }
3269 else if ((colnr_T)fromcol < wp->w_virtcol)
3270 /* restart highlighting after the cursor */
3271 fromcol_prev = wp->w_virtcol;
3272 }
3273 if (fromcol >= tocol)
3274 fromcol = -1;
3275 }
3276
3277#ifdef FEAT_SEARCH_EXTRA
3278 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003279 * Handle highlighting the last used search pattern and matches.
3280 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003282 cur = wp->w_match_head;
3283 shl_flag = FALSE;
3284 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003286 if (shl_flag == FALSE)
3287 {
3288 shl = &search_hl;
3289 shl_flag = TRUE;
3290 }
3291 else
3292 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003293 shl->startcol = MAXCOL;
3294 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 shl->attr_cur = 0;
3296 if (shl->rm.regprog != NULL)
3297 {
3298 v = (long)(ptr - line);
3299 next_search_hl(wp, shl, lnum, (colnr_T)v);
3300
3301 /* Need to get the line again, a multi-line regexp may have made it
3302 * invalid. */
3303 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3304 ptr = line + v;
3305
3306 if (shl->lnum != 0 && shl->lnum <= lnum)
3307 {
3308 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003309 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003311 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3313 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003314 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003316 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003318 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
3320#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003321 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003322 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 else
3324#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003325 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003327 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
3329 shl->attr_cur = shl->attr;
3330 search_attr = shl->attr;
3331 }
3332 area_highlighting = TRUE;
3333 }
3334 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003335 if (shl != &search_hl && cur != NULL)
3336 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338#endif
3339
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003340#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003341 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3342 * active, because it's not clear what is selected then. */
3343 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003344 {
3345 line_attr = hl_attr(HLF_CUL);
3346 area_highlighting = TRUE;
3347 }
3348#endif
3349
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003350 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 col = 0;
3352#ifdef FEAT_RIGHTLEFT
3353 if (wp->w_p_rl)
3354 {
3355 /* Rightleft window: process the text in the normal direction, but put
3356 * it in current_ScreenLine[] from right to left. Start at the
3357 * rightmost column of the window. */
3358 col = W_WIDTH(wp) - 1;
3359 off += col;
3360 }
3361#endif
3362
3363 /*
3364 * Repeat for the whole displayed line.
3365 */
3366 for (;;)
3367 {
3368 /* Skip this quickly when working on the text. */
3369 if (draw_state != WL_LINE)
3370 {
3371#ifdef FEAT_CMDWIN
3372 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3373 {
3374 draw_state = WL_CMDLINE;
3375 if (cmdwin_type != 0 && wp == curwin)
3376 {
3377 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003379 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 char_attr = hl_attr(HLF_AT);
3381 }
3382 }
3383#endif
3384
3385#ifdef FEAT_FOLDING
3386 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3387 {
3388 draw_state = WL_FOLD;
3389 if (wp->w_p_fdc > 0)
3390 {
3391 /* Draw the 'foldcolumn'. */
3392 fill_foldcolumn(extra, wp, FALSE, lnum);
3393 n_extra = wp->w_p_fdc;
3394 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003395 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 c_extra = NUL;
3397 char_attr = hl_attr(HLF_FC);
3398 }
3399 }
3400#endif
3401
3402#ifdef FEAT_SIGNS
3403 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3404 {
3405 draw_state = WL_SIGN;
3406 /* Show the sign column when there are any signs in this
3407 * buffer or when using Netbeans. */
3408 if (draw_signcolumn(wp)
3409# ifdef FEAT_DIFF
3410 && filler_todo <= 0
3411# endif
3412 )
3413 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003414 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003416 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417# endif
3418
3419 /* Draw two cells with the sign value or blank. */
3420 c_extra = ' ';
3421 char_attr = hl_attr(HLF_SC);
3422 n_extra = 2;
3423
3424 if (row == startrow)
3425 {
3426 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3427 SIGN_TEXT);
3428# ifdef FEAT_SIGN_ICONS
3429 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3430 SIGN_ICON);
3431 if (gui.in_use && icon_sign != 0)
3432 {
3433 /* Use the image in this position. */
3434 c_extra = SIGN_BYTE;
3435# ifdef FEAT_NETBEANS_INTG
3436 if (buf_signcount(wp->w_buffer, lnum) > 1)
3437 c_extra = MULTISIGN_BYTE;
3438# endif
3439 char_attr = icon_sign;
3440 }
3441 else
3442# endif
3443 if (text_sign != 0)
3444 {
3445 p_extra = sign_get_text(text_sign);
3446 if (p_extra != NULL)
3447 {
3448 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003449 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
3451 char_attr = sign_get_attr(text_sign, FALSE);
3452 }
3453 }
3454 }
3455 }
3456#endif
3457
3458 if (draw_state == WL_NR - 1 && n_extra == 0)
3459 {
3460 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003461 /* Display the absolute or relative line number. After the
3462 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3463 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 && (row == startrow
3465#ifdef FEAT_DIFF
3466 + filler_lines
3467#endif
3468 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3469 {
3470 /* Draw the line number (empty space after wrapping). */
3471 if (row == startrow
3472#ifdef FEAT_DIFF
3473 + filler_lines
3474#endif
3475 )
3476 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003477 long num;
3478
3479 if (wp->w_p_nu)
3480 /* 'number' */
3481 num = (long)lnum;
3482 else
3483 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003484 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02003485
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003486 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003487 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (wp->w_skipcol > 0)
3489 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3490 *p_extra = '-';
3491#ifdef FEAT_RIGHTLEFT
3492 if (wp->w_p_rl) /* reverse line numbers */
3493 rl_mirror(extra);
3494#endif
3495 p_extra = extra;
3496 c_extra = NUL;
3497 }
3498 else
3499 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003500 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003502#ifdef FEAT_SYN_HL
3503 /* When 'cursorline' is set highlight the line number of
3504 * the current line differently. */
3505 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3506 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509 }
3510
3511#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3512 if (draw_state == WL_SBR - 1 && n_extra == 0)
3513 {
3514 draw_state = WL_SBR;
3515# ifdef FEAT_DIFF
3516 if (filler_todo > 0)
3517 {
3518 /* Draw "deleted" diff line(s). */
3519 if (char2cells(fill_diff) > 1)
3520 c_extra = '-';
3521 else
3522 c_extra = fill_diff;
3523# ifdef FEAT_RIGHTLEFT
3524 if (wp->w_p_rl)
3525 n_extra = col + 1;
3526 else
3527# endif
3528 n_extra = W_WIDTH(wp) - col;
3529 char_attr = hl_attr(HLF_DED);
3530 }
3531# endif
3532# ifdef FEAT_LINEBREAK
3533 if (*p_sbr != NUL && need_showbreak)
3534 {
3535 /* Draw 'showbreak' at the start of each broken line. */
3536 p_extra = p_sbr;
3537 c_extra = NUL;
3538 n_extra = (int)STRLEN(p_sbr);
3539 char_attr = hl_attr(HLF_AT);
3540 need_showbreak = FALSE;
3541 /* Correct end of highlighted area for 'showbreak',
3542 * required when 'linebreak' is also set. */
3543 if (tocol == vcol)
3544 tocol += n_extra;
3545 }
3546# endif
3547 }
3548#endif
3549
3550 if (draw_state == WL_LINE - 1 && n_extra == 0)
3551 {
3552 draw_state = WL_LINE;
3553 if (saved_n_extra)
3554 {
3555 /* Continue item from end of wrapped line. */
3556 n_extra = saved_n_extra;
3557 c_extra = saved_c_extra;
3558 p_extra = saved_p_extra;
3559 char_attr = saved_char_attr;
3560 }
3561 else
3562 char_attr = 0;
3563 }
3564 }
3565
3566 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003567 if (dollar_vcol != 0 && wp == curwin
3568 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569#ifdef FEAT_DIFF
3570 && filler_todo <= 0
3571#endif
3572 )
3573 {
3574 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3575 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003576 /* Pretend we have finished updating the window. Except when
3577 * 'cursorcolumn' is set. */
3578#ifdef FEAT_SYN_HL
3579 if (wp->w_p_cuc)
3580 row = wp->w_cline_row + wp->w_cline_height;
3581 else
3582#endif
3583 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 break;
3585 }
3586
3587 if (draw_state == WL_LINE && area_highlighting)
3588 {
3589 /* handle Visual or match highlighting in this line */
3590 if (vcol == fromcol
3591#ifdef FEAT_MBYTE
3592 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3593 && (*mb_ptr2cells)(ptr) > 1)
3594#endif
3595 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003596 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 && vcol < tocol))
3598 area_attr = attr; /* start highlighting */
3599 else if (area_attr != 0
3600 && (vcol == tocol
3601 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
3604#ifdef FEAT_SEARCH_EXTRA
3605 if (!n_extra)
3606 {
3607 /*
3608 * Check for start/end of search pattern match.
3609 * After end, check for start/end of next match.
3610 * When another match, have to check for start again.
3611 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003612 * Do this for 'search_hl' and the match list (ordered by
3613 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003615 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003616 cur = wp->w_match_head;
3617 shl_flag = FALSE;
3618 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003620 if (shl_flag == FALSE
3621 && ((cur != NULL
3622 && cur->priority > SEARCH_HL_PRIORITY)
3623 || cur == NULL))
3624 {
3625 shl = &search_hl;
3626 shl_flag = TRUE;
3627 }
3628 else
3629 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 while (shl->rm.regprog != NULL)
3631 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003632 if (shl->startcol != MAXCOL
3633 && v >= (long)shl->startcol
3634 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
3636 shl->attr_cur = shl->attr;
3637 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003638 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
3640 shl->attr_cur = 0;
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 next_search_hl(wp, shl, lnum, (colnr_T)v);
3643
3644 /* Need to get the line again, a multi-line regexp
3645 * may have made it invalid. */
3646 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3647 ptr = line + v;
3648
3649 if (shl->lnum == lnum)
3650 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003651 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003653 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003655 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003657 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 /* highlight empty match, try again after
3660 * it */
3661#ifdef FEAT_MBYTE
3662 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003663 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003664 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 else
3666#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003667 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669
3670 /* Loop to check if the match starts at the
3671 * current position */
3672 continue;
3673 }
3674 }
3675 break;
3676 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003677 if (shl != &search_hl && cur != NULL)
3678 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003680
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003681 /* Use attributes from match with highest priority among
3682 * 'search_hl' and the match list. */
3683 search_attr = search_hl.attr_cur;
3684 cur = wp->w_match_head;
3685 shl_flag = FALSE;
3686 while (cur != NULL || shl_flag == FALSE)
3687 {
3688 if (shl_flag == FALSE
3689 && ((cur != NULL
3690 && cur->priority > SEARCH_HL_PRIORITY)
3691 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003692 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003693 shl = &search_hl;
3694 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003695 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003696 else
3697 shl = &cur->hl;
3698 if (shl->attr_cur != 0)
3699 search_attr = shl->attr_cur;
3700 if (shl != &search_hl && cur != NULL)
3701 cur = cur->next;
3702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704#endif
3705
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003707 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003709 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3710 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003712 if (diff_hlf == HLF_TXD && ptr - line > change_end
3713 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003715 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003718
3719 /* Decide which of the highlight attributes to use. */
3720 attr_pri = TRUE;
3721 if (area_attr != 0)
3722 char_attr = area_attr;
3723 else if (search_attr != 0)
3724 char_attr = search_attr;
3725#ifdef LINE_ATTR
3726 /* Use line_attr when not in the Visual or 'incsearch' area
3727 * (area_attr may be 0 when "noinvcur" is set). */
3728 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003729 || vcol < fromcol || vcol_prev < fromcol_prev
3730 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003731 char_attr = line_attr;
3732#endif
3733 else
3734 {
3735 attr_pri = FALSE;
3736#ifdef FEAT_SYN_HL
3737 if (has_syntax)
3738 char_attr = syntax_attr;
3739 else
3740#endif
3741 char_attr = 0;
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744
3745 /*
3746 * Get the next character to put on the screen.
3747 */
3748 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003749 * The "p_extra" points to the extra stuff that is inserted to
3750 * represent special characters (non-printable stuff) and other
3751 * things. When all characters are the same, c_extra is used.
3752 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3753 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3755 */
3756 if (n_extra > 0)
3757 {
3758 if (c_extra != NUL)
3759 {
3760 c = c_extra;
3761#ifdef FEAT_MBYTE
3762 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3763 if (enc_utf8 && (*mb_char2len)(c) > 1)
3764 {
3765 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003766 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003767 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 else
3770 mb_utf8 = FALSE;
3771#endif
3772 }
3773 else
3774 {
3775 c = *p_extra;
3776#ifdef FEAT_MBYTE
3777 if (has_mbyte)
3778 {
3779 mb_c = c;
3780 if (enc_utf8)
3781 {
3782 /* If the UTF-8 character is more than one byte:
3783 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003784 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 mb_utf8 = FALSE;
3786 if (mb_l > n_extra)
3787 mb_l = 1;
3788 else if (mb_l > 1)
3789 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003790 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003792 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 }
3795 else
3796 {
3797 /* if this is a DBCS character, put it in "mb_c" */
3798 mb_l = MB_BYTE2LEN(c);
3799 if (mb_l >= n_extra)
3800 mb_l = 1;
3801 else if (mb_l > 1)
3802 mb_c = (c << 8) + p_extra[1];
3803 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003804 if (mb_l == 0) /* at the NUL at end-of-line */
3805 mb_l = 1;
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 /* If a double-width char doesn't fit display a '>' in the
3808 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003809 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810# ifdef FEAT_RIGHTLEFT
3811 wp->w_p_rl ? (col <= 0) :
3812# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003813 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 && (*mb_char2cells)(mb_c) == 2)
3815 {
3816 c = '>';
3817 mb_c = c;
3818 mb_l = 1;
3819 mb_utf8 = FALSE;
3820 multi_attr = hl_attr(HLF_AT);
3821 /* put the pointer back to output the double-width
3822 * character at the start of the next line. */
3823 ++n_extra;
3824 --p_extra;
3825 }
3826 else
3827 {
3828 n_extra -= mb_l - 1;
3829 p_extra += mb_l - 1;
3830 }
3831 }
3832#endif
3833 ++p_extra;
3834 }
3835 --n_extra;
3836 }
3837 else
3838 {
3839 /*
3840 * Get a character from the line itself.
3841 */
3842 c = *ptr;
3843#ifdef FEAT_MBYTE
3844 if (has_mbyte)
3845 {
3846 mb_c = c;
3847 if (enc_utf8)
3848 {
3849 /* If the UTF-8 character is more than one byte: Decode it
3850 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003851 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 mb_utf8 = FALSE;
3853 if (mb_l > 1)
3854 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003855 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 /* Overlong encoded ASCII or ASCII with composing char
3857 * is displayed normally, except a NUL. */
3858 if (mb_c < 0x80)
3859 c = mb_c;
3860 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003861
3862 /* At start of the line we can have a composing char.
3863 * Draw it as a space with a composing char. */
3864 if (utf_iscomposing(mb_c))
3865 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003866 int i;
3867
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003868 for (i = Screen_mco - 1; i > 0; --i)
3869 u8cc[i] = u8cc[i - 1];
3870 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003871 mb_c = ' ';
3872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 }
3874
3875 if ((mb_l == 1 && c >= 0x80)
3876 || (mb_l >= 1 && mb_c == 0)
3877 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003878# ifdef UNICODE16
3879 || mb_c >= 0x10000
3880# endif
3881 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 /*
3884 * Illegal UTF-8 byte: display as <xx>.
3885 * Non-BMP character : display as ? or fullwidth ?.
3886 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003887# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
3891 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003892# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (wp->w_p_rl) /* reverse */
3894 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003897# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else if (utf_char2cells(mb_c) != 2)
3899 STRCPY(extra, "?");
3900 else
3901 /* 0xff1f in UTF-8: full-width '?' */
3902 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904
3905 p_extra = extra;
3906 c = *p_extra;
3907 mb_c = mb_ptr2char_adv(&p_extra);
3908 mb_utf8 = (c >= 0x80);
3909 n_extra = (int)STRLEN(p_extra);
3910 c_extra = NUL;
3911 if (area_attr == 0 && search_attr == 0)
3912 {
3913 n_attr = n_extra + 1;
3914 extra_attr = hl_attr(HLF_8);
3915 saved_attr2 = char_attr; /* save current attr */
3916 }
3917 }
3918 else if (mb_l == 0) /* at the NUL at end-of-line */
3919 mb_l = 1;
3920#ifdef FEAT_ARABIC
3921 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3922 {
3923 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003924 int pc, pc1, nc;
3925 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 /* The idea of what is the previous and next
3928 * character depends on 'rightleft'. */
3929 if (wp->w_p_rl)
3930 {
3931 pc = prev_c;
3932 pc1 = prev_c1;
3933 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003934 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936 else
3937 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003938 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003940 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942 prev_c = mb_c;
3943
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003944 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 }
3946 else
3947 prev_c = mb_c;
3948#endif
3949 }
3950 else /* enc_dbcs */
3951 {
3952 mb_l = MB_BYTE2LEN(c);
3953 if (mb_l == 0) /* at the NUL at end-of-line */
3954 mb_l = 1;
3955 else if (mb_l > 1)
3956 {
3957 /* We assume a second byte below 32 is illegal.
3958 * Hopefully this is OK for all double-byte encodings!
3959 */
3960 if (ptr[1] >= 32)
3961 mb_c = (c << 8) + ptr[1];
3962 else
3963 {
3964 if (ptr[1] == NUL)
3965 {
3966 /* head byte at end of line */
3967 mb_l = 1;
3968 transchar_nonprint(extra, c);
3969 }
3970 else
3971 {
3972 /* illegal tail byte */
3973 mb_l = 2;
3974 STRCPY(extra, "XX");
3975 }
3976 p_extra = extra;
3977 n_extra = (int)STRLEN(extra) - 1;
3978 c_extra = NUL;
3979 c = *p_extra++;
3980 if (area_attr == 0 && search_attr == 0)
3981 {
3982 n_attr = n_extra + 1;
3983 extra_attr = hl_attr(HLF_8);
3984 saved_attr2 = char_attr; /* save current attr */
3985 }
3986 mb_c = c;
3987 }
3988 }
3989 }
3990 /* If a double-width char doesn't fit display a '>' in the
3991 * last column; the character is displayed at the start of the
3992 * next line. */
3993 if ((
3994# ifdef FEAT_RIGHTLEFT
3995 wp->w_p_rl ? (col <= 0) :
3996# endif
3997 (col >= W_WIDTH(wp) - 1))
3998 && (*mb_char2cells)(mb_c) == 2)
3999 {
4000 c = '>';
4001 mb_c = c;
4002 mb_utf8 = FALSE;
4003 mb_l = 1;
4004 multi_attr = hl_attr(HLF_AT);
4005 /* Put pointer back so that the character will be
4006 * displayed at the start of the next line. */
4007 --ptr;
4008 }
4009 else if (*ptr != NUL)
4010 ptr += mb_l - 1;
4011
4012 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004013 * a '<' in the first column. Don't do this for unprintable
4014 * charactes. */
4015 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004018 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 c = ' ';
4020 if (area_attr == 0 && search_attr == 0)
4021 {
4022 n_attr = n_extra + 1;
4023 extra_attr = hl_attr(HLF_AT);
4024 saved_attr2 = char_attr; /* save current attr */
4025 }
4026 mb_c = c;
4027 mb_utf8 = FALSE;
4028 mb_l = 1;
4029 }
4030
4031 }
4032#endif
4033 ++ptr;
4034
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004035 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004036 if (wp->w_p_list && (c == 160
4037#ifdef FEAT_MBYTE
4038 || (mb_utf8 && mb_c == 160)
4039#endif
4040 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004041 {
4042 c = lcs_nbsp;
4043 if (area_attr == 0 && search_attr == 0)
4044 {
4045 n_attr = 1;
4046 extra_attr = hl_attr(HLF_8);
4047 saved_attr2 = char_attr; /* save current attr */
4048 }
4049#ifdef FEAT_MBYTE
4050 mb_c = c;
4051 if (enc_utf8 && (*mb_char2len)(c) > 1)
4052 {
4053 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004054 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004055 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004056 }
4057 else
4058 mb_utf8 = FALSE;
4059#endif
4060 }
4061
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (extra_check)
4063 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004064#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004065 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004066#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004067
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004068#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 /* Get syntax attribute, unless still at the start of the line
4070 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004071 v = (long)(ptr - line);
4072 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
4074 /* Get the syntax attribute for the character. If there
4075 * is an error, disable syntax highlighting. */
4076 save_did_emsg = did_emsg;
4077 did_emsg = FALSE;
4078
Bram Moolenaar217ad922005-03-20 22:37:15 +00004079 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004080# ifdef FEAT_SPELL
4081 has_spell ? &can_spell :
4082# endif
4083 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004086 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004087 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004088 has_syntax = FALSE;
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 else
4091 did_emsg = save_did_emsg;
4092
4093 /* Need to get the line again, a multi-line regexp may
4094 * have made it invalid. */
4095 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4096 ptr = line + v;
4097
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004098 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004100 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004101 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004102# ifdef FEAT_CONCEAL
4103 /* no concealing past the end of the line, it interferes
4104 * with line highlighting */
4105 if (c == NUL)
4106 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004107 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004108 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004111#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004112
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004113#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004114 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004115 * Only do this when there is no syntax highlighting, the
4116 * @Spell cluster is not used or the current syntax item
4117 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004118 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004119 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004120 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004121# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004122 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004123 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004124# endif
4125 if (c != 0 && (
4126# ifdef FEAT_SYN_HL
4127 !has_syntax ||
4128# endif
4129 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004130 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004131 char_u *prev_ptr, *p;
4132 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004133 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004134# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004135 if (has_mbyte)
4136 {
4137 prev_ptr = ptr - mb_l;
4138 v -= mb_l - 1;
4139 }
4140 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004141# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004142 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004143
4144 /* Use nextline[] if possible, it has the start of the
4145 * next line concatenated. */
4146 if ((prev_ptr - line) - nextlinecol >= 0)
4147 p = nextline + (prev_ptr - line) - nextlinecol;
4148 else
4149 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004150 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004151 len = spell_check(wp, p, &spell_hlf, &cap_col,
4152 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004153 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004154
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004155 /* In Insert mode only highlight a word that
4156 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004157 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004158 && (State & INSERT) != 0
4159 && wp->w_cursor.lnum == lnum
4160 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004161 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004162 && wp->w_cursor.col < (colnr_T)word_end)
4163 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004164 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004165 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004166 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004167
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004168 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004169 && (p - nextline) + len > nextline_idx)
4170 {
4171 /* Remember that the good word continues at the
4172 * start of the next line. */
4173 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004174 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004175 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004176
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004177 /* Turn index into actual attributes. */
4178 if (spell_hlf != HLF_COUNT)
4179 spell_attr = highlight_attr[spell_hlf];
4180
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004181 if (cap_col > 0)
4182 {
4183 if (p != prev_ptr
4184 && (p - nextline) + cap_col >= nextline_idx)
4185 {
4186 /* Remember that the word in the next line
4187 * must start with a capital. */
4188 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004189 cap_col = (int)((p - nextline) + cap_col
4190 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004191 }
4192 else
4193 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004194 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004195 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004196 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004197 }
4198 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004199 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004200 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004201 char_attr = hl_combine_attr(char_attr, spell_attr);
4202 else
4203 char_attr = hl_combine_attr(spell_attr, char_attr);
4204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205#endif
4206#ifdef FEAT_LINEBREAK
4207 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004208 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 */
4210 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004211 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
4213 n_extra = win_lbr_chartabsize(wp, ptr - (
4214# ifdef FEAT_MBYTE
4215 has_mbyte ? mb_l :
4216# endif
4217 1), (colnr_T)vcol, NULL) - 1;
4218 c_extra = ' ';
4219 if (vim_iswhite(c))
4220 c = ' ';
4221 }
4222#endif
4223
4224 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4225 {
4226 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004227 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 n_attr = 1;
4230 extra_attr = hl_attr(HLF_8);
4231 saved_attr2 = char_attr; /* save current attr */
4232 }
4233#ifdef FEAT_MBYTE
4234 mb_c = c;
4235 if (enc_utf8 && (*mb_char2len)(c) > 1)
4236 {
4237 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004238 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004239 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 else
4242 mb_utf8 = FALSE;
4243#endif
4244 }
4245 }
4246
4247 /*
4248 * Handling of non-printable characters.
4249 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004250 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 /*
4253 * when getting a character from the file, we may have to
4254 * turn it into something else on the way to putting it
4255 * into "ScreenLines".
4256 */
4257 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4258 {
4259 /* tab amount depends on current column */
4260 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar8a20b0f2011-08-10 14:32:39 +02004261 - VCOL_HLC % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#ifdef FEAT_MBYTE
4263 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4264#endif
4265 if (wp->w_p_list)
4266 {
4267 c = lcs_tab1;
4268 c_extra = lcs_tab2;
4269 n_attr = n_extra + 1;
4270 extra_attr = hl_attr(HLF_8);
4271 saved_attr2 = char_attr; /* save current attr */
4272#ifdef FEAT_MBYTE
4273 mb_c = c;
4274 if (enc_utf8 && (*mb_char2len)(c) > 1)
4275 {
4276 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004277 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004278 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280#endif
4281 }
4282 else
4283 {
4284 c_extra = ' ';
4285 c = ' ';
4286 }
4287 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004288 else if (c == NUL
4289 && ((wp->w_p_list && lcs_eol > 0)
4290 || ((fromcol >= 0 || fromcol_prev >= 0)
4291 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004292#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004293 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004294#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004295 && (
4296# ifdef FEAT_RIGHTLEFT
4297 wp->w_p_rl ? (col >= 0) :
4298# endif
4299 (col < W_WIDTH(wp)))
4300 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004301 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004302 && (colnr_T)vcol == wp->w_virtcol)))
4303 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004305 /* Display a '$' after the line or highlight an extra
4306 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4308 /* For a diff line the highlighting continues after the
4309 * "$". */
4310 if (
4311# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004312 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313# ifdef LINE_ATTR
4314 &&
4315# endif
4316# endif
4317# ifdef LINE_ATTR
4318 line_attr == 0
4319# endif
4320 )
4321#endif
4322 {
4323#ifdef FEAT_VIRTUALEDIT
4324 /* In virtualedit, visual selections may extend
4325 * beyond end of line. */
4326 if (area_highlighting && virtual_active()
4327 && tocol != MAXCOL && vcol < tocol)
4328 n_extra = 0;
4329 else
4330#endif
4331 {
4332 p_extra = at_end_str;
4333 n_extra = 1;
4334 c_extra = NUL;
4335 }
4336 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004337 if (wp->w_p_list)
4338 c = lcs_eol;
4339 else
4340 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 lcs_eol_one = -1;
4342 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004343 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
4345 extra_attr = hl_attr(HLF_AT);
4346 n_attr = 1;
4347 }
4348#ifdef FEAT_MBYTE
4349 mb_c = c;
4350 if (enc_utf8 && (*mb_char2len)(c) > 1)
4351 {
4352 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004353 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004354 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 else
4357 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4358#endif
4359 }
4360 else if (c != NUL)
4361 {
4362 p_extra = transchar(c);
4363#ifdef FEAT_RIGHTLEFT
4364 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4365 rl_mirror(p_extra); /* reverse "<12>" */
4366#endif
4367 n_extra = byte2cells(c) - 1;
4368 c_extra = NUL;
4369 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004370 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
4372 n_attr = n_extra + 1;
4373 extra_attr = hl_attr(HLF_8);
4374 saved_attr2 = char_attr; /* save current attr */
4375 }
4376#ifdef FEAT_MBYTE
4377 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4378#endif
4379 }
4380#ifdef FEAT_VIRTUALEDIT
4381 else if (VIsual_active
4382 && (VIsual_mode == Ctrl_V
4383 || VIsual_mode == 'v')
4384 && virtual_active()
4385 && tocol != MAXCOL
4386 && vcol < tocol
4387 && (
4388# ifdef FEAT_RIGHTLEFT
4389 wp->w_p_rl ? (col >= 0) :
4390# endif
4391 (col < W_WIDTH(wp))))
4392 {
4393 c = ' ';
4394 --ptr; /* put it back at the NUL */
4395 }
4396#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004397#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else if ((
4399# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004400 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 ) && (
4404# ifdef FEAT_RIGHTLEFT
4405 wp->w_p_rl ? (col >= 0) :
4406# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004407 (col
4408# ifdef FEAT_CONCEAL
4409 - boguscols
4410# endif
4411 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
4413 /* Highlight until the right side of the window */
4414 c = ' ';
4415 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004416
4417 /* Remember we do the char for line highlighting. */
4418 ++did_line_attr;
4419
4420 /* don't do search HL for the rest of the line */
4421 if (line_attr != 0 && char_attr == search_attr && col > 0)
4422 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423# ifdef FEAT_DIFF
4424 if (diff_hlf == HLF_TXD)
4425 {
4426 diff_hlf = HLF_CHD;
4427 if (attr == 0 || char_attr != attr)
4428 char_attr = hl_attr(diff_hlf);
4429 }
4430# endif
4431 }
4432#endif
4433 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004434
4435#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004436 if ( wp->w_p_cole > 0
4437 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4438 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004439 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004440 && !(lnum_in_visual_area
4441 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004442 {
4443 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004444 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004445 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4446 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004447 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004448 /* First time at this concealed item: display one
4449 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004450 if (syn_get_sub_char() != NUL)
4451 c = syn_get_sub_char();
4452 else if (lcs_conceal != NUL)
4453 c = lcs_conceal;
4454 else
4455 c = ' ';
4456
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004457 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004458
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004459 if (n_extra > 0)
4460 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004461 vcol += n_extra;
4462 if (wp->w_p_wrap && n_extra > 0)
4463 {
4464# ifdef FEAT_RIGHTLEFT
4465 if (wp->w_p_rl)
4466 {
4467 col -= n_extra;
4468 boguscols -= n_extra;
4469 }
4470 else
4471# endif
4472 {
4473 boguscols += n_extra;
4474 col += n_extra;
4475 }
4476 }
4477 n_extra = 0;
4478 n_attr = 0;
4479 }
4480 else if (n_skip == 0)
4481 {
4482 is_concealing = TRUE;
4483 n_skip = 1;
4484 }
4485# ifdef FEAT_MBYTE
4486 mb_c = c;
4487 if (enc_utf8 && (*mb_char2len)(c) > 1)
4488 {
4489 mb_utf8 = TRUE;
4490 u8cc[0] = 0;
4491 c = 0xc0;
4492 }
4493 else
4494 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4495# endif
4496 }
4497 else
4498 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004499 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004500 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004501 }
4502#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004505#ifdef FEAT_CONCEAL
4506 /* In the cursor line and we may be concealing characters: correct
4507 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004508 if (!did_wcol && draw_state == WL_LINE
4509 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004510 && conceal_cursor_line(wp)
4511 && (int)wp->w_virtcol <= vcol + n_skip)
4512 {
4513 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004514 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004515 did_wcol = TRUE;
4516 }
4517#endif
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /* Don't override visual selection highlighting. */
4520 if (n_attr > 0
4521 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004522 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 char_attr = extra_attr;
4524
Bram Moolenaar81695252004-12-29 20:58:21 +00004525#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 /* XIM don't send preedit_start and preedit_end, but they send
4527 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4528 * im_is_preediting() here. */
4529 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004530 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 && (State & INSERT)
4532 && !p_imdisable
4533 && im_is_preediting()
4534 && draw_state == WL_LINE)
4535 {
4536 colnr_T tcol;
4537
4538 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004539 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 else
4541 tcol = preedit_end_col;
4542 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4543 {
4544 if (feedback_old_attr < 0)
4545 {
4546 feedback_col = 0;
4547 feedback_old_attr = char_attr;
4548 }
4549 char_attr = im_get_feedback_attr(feedback_col);
4550 if (char_attr < 0)
4551 char_attr = feedback_old_attr;
4552 feedback_col++;
4553 }
4554 else if (feedback_old_attr >= 0)
4555 {
4556 char_attr = feedback_old_attr;
4557 feedback_old_attr = -1;
4558 feedback_col = 0;
4559 }
4560 }
4561#endif
4562 /*
4563 * Handle the case where we are in column 0 but not on the first
4564 * character of the line and the user wants us to show us a
4565 * special character (via 'listchars' option "precedes:<char>".
4566 */
4567 if (lcs_prec_todo != NUL
4568 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4569#ifdef FEAT_DIFF
4570 && filler_todo <= 0
4571#endif
4572 && draw_state > WL_NR
4573 && c != NUL)
4574 {
4575 c = lcs_prec;
4576 lcs_prec_todo = NUL;
4577#ifdef FEAT_MBYTE
4578 mb_c = c;
4579 if (enc_utf8 && (*mb_char2len)(c) > 1)
4580 {
4581 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004582 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004583 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 else
4586 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4587#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004588 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {
4590 saved_attr3 = char_attr; /* save current attr */
4591 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4592 n_attr3 = 1;
4593 }
4594 }
4595
4596 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004597 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004599 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004600#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004601 || did_line_attr == 1
4602#endif
4603 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004605#ifdef FEAT_SEARCH_EXTRA
4606 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004607
4608 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004609 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004610 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004611#endif
4612
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004613 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 * highlight match at end of line. If it's beyond the last
4615 * char on the screen, just overwrite that one (tricky!) Not
4616 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004617#ifdef FEAT_SEARCH_EXTRA
4618 prevcol_hl_flag = FALSE;
4619 if (prevcol == (long)search_hl.startcol)
4620 prevcol_hl_flag = TRUE;
4621 else
4622 {
4623 cur = wp->w_match_head;
4624 while (cur != NULL)
4625 {
4626 if (prevcol == (long)cur->hl.startcol)
4627 {
4628 prevcol_hl_flag = TRUE;
4629 break;
4630 }
4631 cur = cur->next;
4632 }
4633 }
4634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004636 && ((area_attr != 0 && vcol == fromcol
4637#ifdef FEAT_VISUAL
4638 && (VIsual_mode != Ctrl_V
4639 || lnum == VIsual.lnum
4640 || lnum == curwin->w_cursor.lnum)
4641#endif
4642 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#ifdef FEAT_SEARCH_EXTRA
4644 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004645 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004646# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004647 && did_line_attr <= 1
4648# endif
4649 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650#endif
4651 ))
4652 {
4653 int n = 0;
4654
4655#ifdef FEAT_RIGHTLEFT
4656 if (wp->w_p_rl)
4657 {
4658 if (col < 0)
4659 n = 1;
4660 }
4661 else
4662#endif
4663 {
4664 if (col >= W_WIDTH(wp))
4665 n = -1;
4666 }
4667 if (n != 0)
4668 {
4669 /* At the window boundary, highlight the last character
4670 * instead (better than nothing). */
4671 off += n;
4672 col += n;
4673 }
4674 else
4675 {
4676 /* Add a blank character to highlight. */
4677 ScreenLines[off] = ' ';
4678#ifdef FEAT_MBYTE
4679 if (enc_utf8)
4680 ScreenLinesUC[off] = 0;
4681#endif
4682 }
4683#ifdef FEAT_SEARCH_EXTRA
4684 if (area_attr == 0)
4685 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004686 /* Use attributes from match with highest priority among
4687 * 'search_hl' and the match list. */
4688 char_attr = search_hl.attr;
4689 cur = wp->w_match_head;
4690 shl_flag = FALSE;
4691 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004692 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004693 if (shl_flag == FALSE
4694 && ((cur != NULL
4695 && cur->priority > SEARCH_HL_PRIORITY)
4696 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004697 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004698 shl = &search_hl;
4699 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004700 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004701 else
4702 shl = &cur->hl;
4703 if ((ptr - line) - 1 == (long)shl->startcol)
4704 char_attr = shl->attr;
4705 if (shl != &search_hl && cur != NULL)
4706 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709#endif
4710 ScreenAttrs[off] = char_attr;
4711#ifdef FEAT_RIGHTLEFT
4712 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004715 --off;
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 else
4718#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004721 ++off;
4722 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004723 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004724#ifdef FEAT_SYN_HL
4725 eol_hl_off = 1;
4726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
Bram Moolenaar91170f82006-05-05 21:15:17 +00004730 /*
4731 * At end of the text line.
4732 */
4733 if (c == NUL)
4734 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004735#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004736 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4737 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004738 {
4739 /* highlight last char after line */
4740 --col;
4741 --off;
4742 --vcol;
4743 }
4744
Bram Moolenaar1a384422010-07-14 19:53:30 +02004745 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004746 if (wp->w_p_wrap)
4747 v = wp->w_skipcol;
4748 else
4749 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004750
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004751 /* check if line ends before left margin */
4752 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004753 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004754#ifdef FEAT_CONCEAL
4755 /* Get rid of the boguscols now, we want to draw until the right
4756 * edge for 'cursorcolumn'. */
4757 col -= boguscols;
4758 boguscols = 0;
4759#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004760
4761 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004762 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004763
4764 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004765 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4766 && (int)wp->w_virtcol <
4767 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004768 && lnum != wp->w_cursor.lnum)
4769 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004770# ifdef FEAT_RIGHTLEFT
4771 && !wp->w_p_rl
4772# endif
4773 )
4774 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004775 int rightmost_vcol = 0;
4776 int i;
4777
4778 if (wp->w_p_cuc)
4779 rightmost_vcol = wp->w_virtcol;
4780 if (draw_color_col)
4781 /* determine rightmost colorcolumn to possibly draw */
4782 for (i = 0; color_cols[i] >= 0; ++i)
4783 if (rightmost_vcol < color_cols[i])
4784 rightmost_vcol = color_cols[i];
4785
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004786 while (col < W_WIDTH(wp))
4787 {
4788 ScreenLines[off] = ' ';
4789#ifdef FEAT_MBYTE
4790 if (enc_utf8)
4791 ScreenLinesUC[off] = 0;
4792#endif
4793 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004794 if (draw_color_col)
4795 draw_color_col = advance_color_col(VCOL_HLC,
4796 &color_cols);
4797
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004798 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004799 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004800 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004801 ScreenAttrs[off++] = hl_attr(HLF_MC);
4802 else
4803 ScreenAttrs[off++] = 0;
4804
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004805 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004806 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004807
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004808 ++vcol;
4809 }
4810 }
4811#endif
4812
Bram Moolenaar860cae12010-06-05 23:22:07 +02004813 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4814 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 row++;
4816
4817 /*
4818 * Update w_cline_height and w_cline_folded if the cursor line was
4819 * updated (saves a call to plines() later).
4820 */
4821 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4822 {
4823 curwin->w_cline_row = startrow;
4824 curwin->w_cline_height = row - startrow;
4825#ifdef FEAT_FOLDING
4826 curwin->w_cline_folded = FALSE;
4827#endif
4828 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4829 }
4830
4831 break;
4832 }
4833
4834 /* line continues beyond line end */
4835 if (lcs_ext
4836 && !wp->w_p_wrap
4837#ifdef FEAT_DIFF
4838 && filler_todo <= 0
4839#endif
4840 && (
4841#ifdef FEAT_RIGHTLEFT
4842 wp->w_p_rl ? col == 0 :
4843#endif
4844 col == W_WIDTH(wp) - 1)
4845 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004846 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4848 {
4849 c = lcs_ext;
4850 char_attr = hl_attr(HLF_AT);
4851#ifdef FEAT_MBYTE
4852 mb_c = c;
4853 if (enc_utf8 && (*mb_char2len)(c) > 1)
4854 {
4855 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004856 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004857 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
4859 else
4860 mb_utf8 = FALSE;
4861#endif
4862 }
4863
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004864#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004865 /* advance to the next 'colorcolumn' */
4866 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004867 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004868
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004869 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004870 * highlight the cursor position itself.
4871 * Also highlight the 'colorcolumn' if it is different than
4872 * 'cursorcolumn' */
4873 vcol_save_attr = -1;
4874 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004875 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004876 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004877 && lnum != wp->w_cursor.lnum)
4878 {
4879 vcol_save_attr = char_attr;
4880 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4881 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004882 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004883 {
4884 vcol_save_attr = char_attr;
4885 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4886 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004887 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004888#endif
4889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 /*
4891 * Store character to be displayed.
4892 * Skip characters that are left of the screen for 'nowrap'.
4893 */
4894 vcol_prev = vcol;
4895 if (draw_state < WL_LINE || n_skip <= 0)
4896 {
4897 /*
4898 * Store the character.
4899 */
4900#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4901 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4902 {
4903 /* A double-wide character is: put first halve in left cell. */
4904 --off;
4905 --col;
4906 }
4907#endif
4908 ScreenLines[off] = c;
4909#ifdef FEAT_MBYTE
4910 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004911 {
4912 if ((mb_c & 0xff00) == 0x8e00)
4913 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else if (enc_utf8)
4917 {
4918 if (mb_utf8)
4919 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004920 int i;
4921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004923 if ((c & 0xff) == 0)
4924 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004925 for (i = 0; i < Screen_mco; ++i)
4926 {
4927 ScreenLinesC[i][off] = u8cc[i];
4928 if (u8cc[i] == 0)
4929 break;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 else
4933 ScreenLinesUC[off] = 0;
4934 }
4935 if (multi_attr)
4936 {
4937 ScreenAttrs[off] = multi_attr;
4938 multi_attr = 0;
4939 }
4940 else
4941#endif
4942 ScreenAttrs[off] = char_attr;
4943
4944#ifdef FEAT_MBYTE
4945 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4946 {
4947 /* Need to fill two screen columns. */
4948 ++off;
4949 ++col;
4950 if (enc_utf8)
4951 /* UTF-8: Put a 0 in the second screen char. */
4952 ScreenLines[off] = 0;
4953 else
4954 /* DBCS: Put second byte in the second screen char. */
4955 ScreenLines[off] = mb_c & 0xff;
4956 ++vcol;
4957 /* When "tocol" is halfway a character, set it to the end of
4958 * the character, otherwise highlighting won't stop. */
4959 if (tocol == vcol)
4960 ++tocol;
4961#ifdef FEAT_RIGHTLEFT
4962 if (wp->w_p_rl)
4963 {
4964 /* now it's time to backup one cell */
4965 --off;
4966 --col;
4967 }
4968#endif
4969 }
4970#endif
4971#ifdef FEAT_RIGHTLEFT
4972 if (wp->w_p_rl)
4973 {
4974 --off;
4975 --col;
4976 }
4977 else
4978#endif
4979 {
4980 ++off;
4981 ++col;
4982 }
4983 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004984#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004985 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004986 {
4987 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004988 ++vcol_off;
4989 if (n_extra > 0)
4990 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004991 if (wp->w_p_wrap)
4992 {
4993 /*
4994 * Special voodoo required if 'wrap' is on.
4995 *
4996 * Advance the column indicator to force the line
4997 * drawing to wrap early. This will make the line
4998 * take up the same screen space when parts are concealed,
4999 * so that cursor line computations aren't messed up.
5000 *
5001 * To avoid the fictitious advance of 'col' causing
5002 * trailing junk to be written out of the screen line
5003 * we are building, 'boguscols' keeps track of the number
5004 * of bad columns we have advanced.
5005 */
5006 if (n_extra > 0)
5007 {
5008 vcol += n_extra;
5009# ifdef FEAT_RIGHTLEFT
5010 if (wp->w_p_rl)
5011 {
5012 col -= n_extra;
5013 boguscols -= n_extra;
5014 }
5015 else
5016# endif
5017 {
5018 col += n_extra;
5019 boguscols += n_extra;
5020 }
5021 n_extra = 0;
5022 n_attr = 0;
5023 }
5024
5025
5026# ifdef FEAT_MBYTE
5027 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5028 {
5029 /* Need to fill two screen columns. */
5030# ifdef FEAT_RIGHTLEFT
5031 if (wp->w_p_rl)
5032 {
5033 --boguscols;
5034 --col;
5035 }
5036 else
5037# endif
5038 {
5039 ++boguscols;
5040 ++col;
5041 }
5042 }
5043# endif
5044
5045# ifdef FEAT_RIGHTLEFT
5046 if (wp->w_p_rl)
5047 {
5048 --boguscols;
5049 --col;
5050 }
5051 else
5052# endif
5053 {
5054 ++boguscols;
5055 ++col;
5056 }
5057 }
5058 else
5059 {
5060 if (n_extra > 0)
5061 {
5062 vcol += n_extra;
5063 n_extra = 0;
5064 n_attr = 0;
5065 }
5066 }
5067
5068 }
5069#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 else
5071 --n_skip;
5072
Bram Moolenaar64486672010-05-16 15:46:46 +02005073 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5074 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005075 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076#ifdef FEAT_DIFF
5077 && filler_todo <= 0
5078#endif
5079 )
5080 ++vcol;
5081
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005082#ifdef FEAT_SYN_HL
5083 if (vcol_save_attr >= 0)
5084 char_attr = vcol_save_attr;
5085#endif
5086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 /* restore attributes after "predeces" in 'listchars' */
5088 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5089 char_attr = saved_attr3;
5090
5091 /* restore attributes after last 'listchars' or 'number' char */
5092 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5093 char_attr = saved_attr2;
5094
5095 /*
5096 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005097 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 */
5099 if ((
5100#ifdef FEAT_RIGHTLEFT
5101 wp->w_p_rl ? (col < 0) :
5102#endif
5103 (col >= W_WIDTH(wp)))
5104 && (*ptr != NUL
5105#ifdef FEAT_DIFF
5106 || filler_todo > 0
5107#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005108 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5110 )
5111 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005112#ifdef FEAT_CONCEAL
5113 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5114 (int)W_WIDTH(wp), wp->w_p_rl);
5115 boguscols = 0;
5116#else
5117 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5118 (int)W_WIDTH(wp), wp->w_p_rl);
5119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 ++row;
5121 ++screen_row;
5122
5123 /* When not wrapping and finished diff lines, or when displayed
5124 * '$' and highlighting until last column, break here. */
5125 if ((!wp->w_p_wrap
5126#ifdef FEAT_DIFF
5127 && filler_todo <= 0
5128#endif
5129 ) || lcs_eol_one == -1)
5130 break;
5131
5132 /* When the window is too narrow draw all "@" lines. */
5133 if (draw_state != WL_LINE
5134#ifdef FEAT_DIFF
5135 && filler_todo <= 0
5136#endif
5137 )
5138 {
5139 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5140#ifdef FEAT_VERTSPLIT
5141 draw_vsep_win(wp, row);
5142#endif
5143 row = endrow;
5144 }
5145
5146 /* When line got too long for screen break here. */
5147 if (row == endrow)
5148 {
5149 ++row;
5150 break;
5151 }
5152
5153 if (screen_cur_row == screen_row - 1
5154#ifdef FEAT_DIFF
5155 && filler_todo <= 0
5156#endif
5157 && W_WIDTH(wp) == Columns)
5158 {
5159 /* Remember that the line wraps, used for modeless copy. */
5160 LineWraps[screen_row - 1] = TRUE;
5161
5162 /*
5163 * Special trick to make copy/paste of wrapped lines work with
5164 * xterm/screen: write an extra character beyond the end of
5165 * the line. This will work with all terminal types
5166 * (regardless of the xn,am settings).
5167 * Only do this on a fast tty.
5168 * Only do this if the cursor is on the current line
5169 * (something has been written in it).
5170 * Don't do this for the GUI.
5171 * Don't do this for double-width characters.
5172 * Don't do this for a window not at the right screen border.
5173 */
5174 if (p_tf
5175#ifdef FEAT_GUI
5176 && !gui.in_use
5177#endif
5178#ifdef FEAT_MBYTE
5179 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005180 && ((*mb_off2cells)(LineOffset[screen_row],
5181 LineOffset[screen_row] + screen_Columns)
5182 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005184 + (int)Columns - 2,
5185 LineOffset[screen_row] + screen_Columns)
5186 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187#endif
5188 )
5189 {
5190 /* First make sure we are at the end of the screen line,
5191 * then output the same character again to let the
5192 * terminal know about the wrap. If the terminal doesn't
5193 * auto-wrap, we overwrite the character. */
5194 if (screen_cur_col != W_WIDTH(wp))
5195 screen_char(LineOffset[screen_row - 1]
5196 + (unsigned)Columns - 1,
5197 screen_row - 1, (int)(Columns - 1));
5198
5199#ifdef FEAT_MBYTE
5200 /* When there is a multi-byte character, just output a
5201 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005202 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5203 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 out_char(' ');
5205 else
5206#endif
5207 out_char(ScreenLines[LineOffset[screen_row - 1]
5208 + (Columns - 1)]);
5209 /* force a redraw of the first char on the next line */
5210 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5211 screen_start(); /* don't know where cursor is now */
5212 }
5213 }
5214
5215 col = 0;
5216 off = (unsigned)(current_ScreenLine - ScreenLines);
5217#ifdef FEAT_RIGHTLEFT
5218 if (wp->w_p_rl)
5219 {
5220 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5221 off += col;
5222 }
5223#endif
5224
5225 /* reset the drawing state for the start of a wrapped line */
5226 draw_state = WL_START;
5227 saved_n_extra = n_extra;
5228 saved_p_extra = p_extra;
5229 saved_c_extra = c_extra;
5230 saved_char_attr = char_attr;
5231 n_extra = 0;
5232 lcs_prec_todo = lcs_prec;
5233#ifdef FEAT_LINEBREAK
5234# ifdef FEAT_DIFF
5235 if (filler_todo <= 0)
5236# endif
5237 need_showbreak = TRUE;
5238#endif
5239#ifdef FEAT_DIFF
5240 --filler_todo;
5241 /* When the filler lines are actually below the last line of the
5242 * file, don't draw the line itself, break here. */
5243 if (filler_todo == 0 && wp->w_botfill)
5244 break;
5245#endif
5246 }
5247
5248 } /* for every character in the line */
5249
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005250#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005251 /* After an empty line check first word for capital. */
5252 if (*skipwhite(line) == NUL)
5253 {
5254 capcol_lnum = lnum + 1;
5255 cap_col = 0;
5256 }
5257#endif
5258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return row;
5260}
5261
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005262#ifdef FEAT_MBYTE
5263static int comp_char_differs __ARGS((int, int));
5264
5265/*
5266 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005267 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005268 */
5269 static int
5270comp_char_differs(off_from, off_to)
5271 int off_from;
5272 int off_to;
5273{
5274 int i;
5275
5276 for (i = 0; i < Screen_mco; ++i)
5277 {
5278 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5279 return TRUE;
5280 if (ScreenLinesC[i][off_from] == 0)
5281 break;
5282 }
5283 return FALSE;
5284}
5285#endif
5286
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287/*
5288 * Check whether the given character needs redrawing:
5289 * - the (first byte of the) character is different
5290 * - the attributes are different
5291 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005292 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 */
5294 static int
5295char_needs_redraw(off_from, off_to, cols)
5296 int off_from;
5297 int off_to;
5298 int cols;
5299{
5300 if (cols > 0
5301 && ((ScreenLines[off_from] != ScreenLines[off_to]
5302 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5303
5304#ifdef FEAT_MBYTE
5305 || (enc_dbcs != 0
5306 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5307 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5308 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5309 : (cols > 1 && ScreenLines[off_from + 1]
5310 != ScreenLines[off_to + 1])))
5311 || (enc_utf8
5312 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5313 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005314 && comp_char_differs(off_from, off_to))
5315 || (cols > 1 && ScreenLines[off_from + 1]
5316 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#endif
5318 ))
5319 return TRUE;
5320 return FALSE;
5321}
5322
5323/*
5324 * Move one "cooked" screen line to the screen, but only the characters that
5325 * have actually changed. Handle insert/delete character.
5326 * "coloff" gives the first column on the screen for this line.
5327 * "endcol" gives the columns where valid characters are.
5328 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5329 * needs to be cleared, negative otherwise.
5330 * "rlflag" is TRUE in a rightleft window:
5331 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5332 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5333 */
5334 static void
5335screen_line(row, coloff, endcol, clear_width
5336#ifdef FEAT_RIGHTLEFT
5337 , rlflag
5338#endif
5339 )
5340 int row;
5341 int coloff;
5342 int endcol;
5343 int clear_width;
5344#ifdef FEAT_RIGHTLEFT
5345 int rlflag;
5346#endif
5347{
5348 unsigned off_from;
5349 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005350#ifdef FEAT_MBYTE
5351 unsigned max_off_from;
5352 unsigned max_off_to;
5353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 int col = 0;
5355#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5356 int hl;
5357#endif
5358 int force = FALSE; /* force update rest of the line */
5359 int redraw_this /* bool: does character need redraw? */
5360#ifdef FEAT_GUI
5361 = TRUE /* For GUI when while-loop empty */
5362#endif
5363 ;
5364 int redraw_next; /* redraw_this for next character */
5365#ifdef FEAT_MBYTE
5366 int clear_next = FALSE;
5367 int char_cells; /* 1: normal char */
5368 /* 2: occupies two display cells */
5369# define CHAR_CELLS char_cells
5370#else
5371# define CHAR_CELLS 1
5372#endif
5373
5374# ifdef FEAT_CLIPBOARD
5375 clip_may_clear_selection(row, row);
5376# endif
5377
5378 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5379 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005380#ifdef FEAT_MBYTE
5381 max_off_from = off_from + screen_Columns;
5382 max_off_to = LineOffset[row] + screen_Columns;
5383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385#ifdef FEAT_RIGHTLEFT
5386 if (rlflag)
5387 {
5388 /* Clear rest first, because it's left of the text. */
5389 if (clear_width > 0)
5390 {
5391 while (col <= endcol && ScreenLines[off_to] == ' '
5392 && ScreenAttrs[off_to] == 0
5393# ifdef FEAT_MBYTE
5394 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5395# endif
5396 )
5397 {
5398 ++off_to;
5399 ++col;
5400 }
5401 if (col <= endcol)
5402 screen_fill(row, row + 1, col + coloff,
5403 endcol + coloff + 1, ' ', ' ', 0);
5404 }
5405 col = endcol + 1;
5406 off_to = LineOffset[row] + col + coloff;
5407 off_from += col;
5408 endcol = (clear_width > 0 ? clear_width : -clear_width);
5409 }
5410#endif /* FEAT_RIGHTLEFT */
5411
5412 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5413
5414 while (col < endcol)
5415 {
5416#ifdef FEAT_MBYTE
5417 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005418 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 else
5420 char_cells = 1;
5421#endif
5422
5423 redraw_this = redraw_next;
5424 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5425 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5426
5427#ifdef FEAT_GUI
5428 /* If the next character was bold, then redraw the current character to
5429 * remove any pixels that might have spilt over into us. This only
5430 * happens in the GUI.
5431 */
5432 if (redraw_next && gui.in_use)
5433 {
5434 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005435 if (hl > HL_ALL)
5436 hl = syn_attr2attr(hl);
5437 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 redraw_this = TRUE;
5439 }
5440#endif
5441
5442 if (redraw_this)
5443 {
5444 /*
5445 * Special handling when 'xs' termcap flag set (hpterm):
5446 * Attributes for characters are stored at the position where the
5447 * cursor is when writing the highlighting code. The
5448 * start-highlighting code must be written with the cursor on the
5449 * first highlighted character. The stop-highlighting code must
5450 * be written with the cursor just after the last highlighted
5451 * character.
5452 * Overwriting a character doesn't remove it's highlighting. Need
5453 * to clear the rest of the line, and force redrawing it
5454 * completely.
5455 */
5456 if ( p_wiv
5457 && !force
5458#ifdef FEAT_GUI
5459 && !gui.in_use
5460#endif
5461 && ScreenAttrs[off_to] != 0
5462 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5463 {
5464 /*
5465 * Need to remove highlighting attributes here.
5466 */
5467 windgoto(row, col + coloff);
5468 out_str(T_CE); /* clear rest of this screen line */
5469 screen_start(); /* don't know where cursor is now */
5470 force = TRUE; /* force redraw of rest of the line */
5471 redraw_next = TRUE; /* or else next char would miss out */
5472
5473 /*
5474 * If the previous character was highlighted, need to stop
5475 * highlighting at this character.
5476 */
5477 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5478 {
5479 screen_attr = ScreenAttrs[off_to - 1];
5480 term_windgoto(row, col + coloff);
5481 screen_stop_highlight();
5482 }
5483 else
5484 screen_attr = 0; /* highlighting has stopped */
5485 }
5486#ifdef FEAT_MBYTE
5487 if (enc_dbcs != 0)
5488 {
5489 /* Check if overwriting a double-byte with a single-byte or
5490 * the other way around requires another character to be
5491 * redrawn. For UTF-8 this isn't needed, because comparing
5492 * ScreenLinesUC[] is sufficient. */
5493 if (char_cells == 1
5494 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005495 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
5497 /* Writing a single-cell character over a double-cell
5498 * character: need to redraw the next cell. */
5499 ScreenLines[off_to + 1] = 0;
5500 redraw_next = TRUE;
5501 }
5502 else if (char_cells == 2
5503 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005504 && (*mb_off2cells)(off_to, max_off_to) == 1
5505 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 /* Writing the second half of a double-cell character over
5508 * a double-cell character: need to redraw the second
5509 * cell. */
5510 ScreenLines[off_to + 2] = 0;
5511 redraw_next = TRUE;
5512 }
5513
5514 if (enc_dbcs == DBCS_JPNU)
5515 ScreenLines2[off_to] = ScreenLines2[off_from];
5516 }
5517 /* When writing a single-width character over a double-width
5518 * character and at the end of the redrawn text, need to clear out
5519 * the right halve of the old character.
5520 * Also required when writing the right halve of a double-width
5521 * char over the left halve of an existing one. */
5522 if (has_mbyte && col + char_cells == endcol
5523 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005524 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005526 && (*mb_off2cells)(off_to, max_off_to) == 1
5527 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 clear_next = TRUE;
5529#endif
5530
5531 ScreenLines[off_to] = ScreenLines[off_from];
5532#ifdef FEAT_MBYTE
5533 if (enc_utf8)
5534 {
5535 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5536 if (ScreenLinesUC[off_from] != 0)
5537 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005538 int i;
5539
5540 for (i = 0; i < Screen_mco; ++i)
5541 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543 }
5544 if (char_cells == 2)
5545 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5546#endif
5547
5548#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005549 /* The bold trick makes a single column of pixels appear in the
5550 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 * character should be redrawn too. This happens for our own GUI
5552 * and for some xterms. */
5553 if (
5554# ifdef FEAT_GUI
5555 gui.in_use
5556# endif
5557# if defined(FEAT_GUI) && defined(UNIX)
5558 ||
5559# endif
5560# ifdef UNIX
5561 term_is_xterm
5562# endif
5563 )
5564 {
5565 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005566 if (hl > HL_ALL)
5567 hl = syn_attr2attr(hl);
5568 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 redraw_next = TRUE;
5570 }
5571#endif
5572 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5573#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005574 /* For simplicity set the attributes of second half of a
5575 * double-wide character equal to the first half. */
5576 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005578
5579 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 else
5582#endif
5583 screen_char(off_to, row, col + coloff);
5584 }
5585 else if ( p_wiv
5586#ifdef FEAT_GUI
5587 && !gui.in_use
5588#endif
5589 && col + coloff > 0)
5590 {
5591 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5592 {
5593 /*
5594 * Don't output stop-highlight when moving the cursor, it will
5595 * stop the highlighting when it should continue.
5596 */
5597 screen_attr = 0;
5598 }
5599 else if (screen_attr != 0)
5600 screen_stop_highlight();
5601 }
5602
5603 off_to += CHAR_CELLS;
5604 off_from += CHAR_CELLS;
5605 col += CHAR_CELLS;
5606 }
5607
5608#ifdef FEAT_MBYTE
5609 if (clear_next)
5610 {
5611 /* Clear the second half of a double-wide character of which the left
5612 * half was overwritten with a single-wide character. */
5613 ScreenLines[off_to] = ' ';
5614 if (enc_utf8)
5615 ScreenLinesUC[off_to] = 0;
5616 screen_char(off_to, row, col + coloff);
5617 }
5618#endif
5619
5620 if (clear_width > 0
5621#ifdef FEAT_RIGHTLEFT
5622 && !rlflag
5623#endif
5624 )
5625 {
5626#ifdef FEAT_GUI
5627 int startCol = col;
5628#endif
5629
5630 /* blank out the rest of the line */
5631 while (col < clear_width && ScreenLines[off_to] == ' '
5632 && ScreenAttrs[off_to] == 0
5633#ifdef FEAT_MBYTE
5634 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5635#endif
5636 )
5637 {
5638 ++off_to;
5639 ++col;
5640 }
5641 if (col < clear_width)
5642 {
5643#ifdef FEAT_GUI
5644 /*
5645 * In the GUI, clearing the rest of the line may leave pixels
5646 * behind if the first character cleared was bold. Some bold
5647 * fonts spill over the left. In this case we redraw the previous
5648 * character too. If we didn't skip any blanks above, then we
5649 * only redraw if the character wasn't already redrawn anyway.
5650 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005651 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 hl = ScreenAttrs[off_to];
5654 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005655 {
5656 int prev_cells = 1;
5657# ifdef FEAT_MBYTE
5658 if (enc_utf8)
5659 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5660 * that its width is 2. */
5661 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5662 else if (enc_dbcs != 0)
5663 {
5664 /* find previous character by counting from first
5665 * column and get its width. */
5666 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005667 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005668
5669 while (off < off_to)
5670 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005671 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005672 off += prev_cells;
5673 }
5674 }
5675
5676 if (enc_dbcs != 0 && prev_cells > 1)
5677 screen_char_2(off_to - prev_cells, row,
5678 col + coloff - prev_cells);
5679 else
5680# endif
5681 screen_char(off_to - prev_cells, row,
5682 col + coloff - prev_cells);
5683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685#endif
5686 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5687 ' ', ' ', 0);
5688#ifdef FEAT_VERTSPLIT
5689 off_to += clear_width - col;
5690 col = clear_width;
5691#endif
5692 }
5693 }
5694
5695 if (clear_width > 0)
5696 {
5697#ifdef FEAT_VERTSPLIT
5698 /* For a window that's left of another, draw the separator char. */
5699 if (col + coloff < Columns)
5700 {
5701 int c;
5702
5703 c = fillchar_vsep(&hl);
5704 if (ScreenLines[off_to] != c
5705# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005706 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5707 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708# endif
5709 || ScreenAttrs[off_to] != hl)
5710 {
5711 ScreenLines[off_to] = c;
5712 ScreenAttrs[off_to] = hl;
5713# ifdef FEAT_MBYTE
5714 if (enc_utf8)
5715 {
5716 if (c >= 0x80)
5717 {
5718 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005719 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
5721 else
5722 ScreenLinesUC[off_to] = 0;
5723 }
5724# endif
5725 screen_char(off_to, row, col + coloff);
5726 }
5727 }
5728 else
5729#endif
5730 LineWraps[row] = FALSE;
5731 }
5732}
5733
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005734#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005736 * Mirror text "str" for right-left displaying.
5737 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005739 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740rl_mirror(str)
5741 char_u *str;
5742{
5743 char_u *p1, *p2;
5744 int t;
5745
5746 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5747 {
5748 t = *p1;
5749 *p1 = *p2;
5750 *p2 = t;
5751 }
5752}
5753#endif
5754
5755#if defined(FEAT_WINDOWS) || defined(PROTO)
5756/*
5757 * mark all status lines for redraw; used after first :cd
5758 */
5759 void
5760status_redraw_all()
5761{
5762 win_T *wp;
5763
5764 for (wp = firstwin; wp; wp = wp->w_next)
5765 if (wp->w_status_height)
5766 {
5767 wp->w_redr_status = TRUE;
5768 redraw_later(VALID);
5769 }
5770}
5771
5772/*
5773 * mark all status lines of the current buffer for redraw
5774 */
5775 void
5776status_redraw_curbuf()
5777{
5778 win_T *wp;
5779
5780 for (wp = firstwin; wp; wp = wp->w_next)
5781 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5782 {
5783 wp->w_redr_status = TRUE;
5784 redraw_later(VALID);
5785 }
5786}
5787
5788/*
5789 * Redraw all status lines that need to be redrawn.
5790 */
5791 void
5792redraw_statuslines()
5793{
5794 win_T *wp;
5795
5796 for (wp = firstwin; wp; wp = wp->w_next)
5797 if (wp->w_redr_status)
5798 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005799 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005800 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801}
5802#endif
5803
5804#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5805/*
5806 * Redraw all status lines at the bottom of frame "frp".
5807 */
5808 void
5809win_redraw_last_status(frp)
5810 frame_T *frp;
5811{
5812 if (frp->fr_layout == FR_LEAF)
5813 frp->fr_win->w_redr_status = TRUE;
5814 else if (frp->fr_layout == FR_ROW)
5815 {
5816 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5817 win_redraw_last_status(frp);
5818 }
5819 else /* frp->fr_layout == FR_COL */
5820 {
5821 frp = frp->fr_child;
5822 while (frp->fr_next != NULL)
5823 frp = frp->fr_next;
5824 win_redraw_last_status(frp);
5825 }
5826}
5827#endif
5828
5829#ifdef FEAT_VERTSPLIT
5830/*
5831 * Draw the verticap separator right of window "wp" starting with line "row".
5832 */
5833 static void
5834draw_vsep_win(wp, row)
5835 win_T *wp;
5836 int row;
5837{
5838 int hl;
5839 int c;
5840
5841 if (wp->w_vsep_width)
5842 {
5843 /* draw the vertical separator right of this window */
5844 c = fillchar_vsep(&hl);
5845 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5846 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5847 c, ' ', hl);
5848 }
5849}
5850#endif
5851
5852#ifdef FEAT_WILDMENU
5853static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005854static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
5856/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005857 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 */
5859 static int
5860status_match_len(xp, s)
5861 expand_T *xp;
5862 char_u *s;
5863{
5864 int len = 0;
5865
5866#ifdef FEAT_MENU
5867 int emenu = (xp->xp_context == EXPAND_MENUS
5868 || xp->xp_context == EXPAND_MENUNAMES);
5869
5870 /* Check for menu separators - replace with '|'. */
5871 if (emenu && menu_is_separator(s))
5872 return 1;
5873#endif
5874
5875 while (*s != NUL)
5876 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005877 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005878 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005879 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 }
5881
5882 return len;
5883}
5884
5885/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005886 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005887 * These are backslashes used for escaping. Do show backslashes in help tags.
5888 */
5889 static int
5890skip_status_match_char(xp, s)
5891 expand_T *xp;
5892 char_u *s;
5893{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005894 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005895#ifdef FEAT_MENU
5896 || ((xp->xp_context == EXPAND_MENUS
5897 || xp->xp_context == EXPAND_MENUNAMES)
5898 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5899#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005900 )
5901 {
5902#ifndef BACKSLASH_IN_FILENAME
5903 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5904 return 2;
5905#endif
5906 return 1;
5907 }
5908 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005909}
5910
5911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 * Show wildchar matches in the status line.
5913 * Show at least the "match" item.
5914 * We start at item 'first_match' in the list and show all matches that fit.
5915 *
5916 * If inversion is possible we use it. Else '=' characters are used.
5917 */
5918 void
5919win_redr_status_matches(xp, num_matches, matches, match, showtail)
5920 expand_T *xp;
5921 int num_matches;
5922 char_u **matches; /* list of matches */
5923 int match;
5924 int showtail;
5925{
5926#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5927 int row;
5928 char_u *buf;
5929 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005930 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 int fillchar;
5932 int attr;
5933 int i;
5934 int highlight = TRUE;
5935 char_u *selstart = NULL;
5936 int selstart_col = 0;
5937 char_u *selend = NULL;
5938 static int first_match = 0;
5939 int add_left = FALSE;
5940 char_u *s;
5941#ifdef FEAT_MENU
5942 int emenu;
5943#endif
5944#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5945 int l;
5946#endif
5947
5948 if (matches == NULL) /* interrupted completion? */
5949 return;
5950
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005951#ifdef FEAT_MBYTE
5952 if (has_mbyte)
5953 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5954 else
5955#endif
5956 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (buf == NULL)
5958 return;
5959
5960 if (match == -1) /* don't show match but original text */
5961 {
5962 match = 0;
5963 highlight = FALSE;
5964 }
5965 /* count 1 for the ending ">" */
5966 clen = status_match_len(xp, L_MATCH(match)) + 3;
5967 if (match == 0)
5968 first_match = 0;
5969 else if (match < first_match)
5970 {
5971 /* jumping left, as far as we can go */
5972 first_match = match;
5973 add_left = TRUE;
5974 }
5975 else
5976 {
5977 /* check if match fits on the screen */
5978 for (i = first_match; i < match; ++i)
5979 clen += status_match_len(xp, L_MATCH(i)) + 2;
5980 if (first_match > 0)
5981 clen += 2;
5982 /* jumping right, put match at the left */
5983 if ((long)clen > Columns)
5984 {
5985 first_match = match;
5986 /* if showing the last match, we can add some on the left */
5987 clen = 2;
5988 for (i = match; i < num_matches; ++i)
5989 {
5990 clen += status_match_len(xp, L_MATCH(i)) + 2;
5991 if ((long)clen >= Columns)
5992 break;
5993 }
5994 if (i == num_matches)
5995 add_left = TRUE;
5996 }
5997 }
5998 if (add_left)
5999 while (first_match > 0)
6000 {
6001 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6002 if ((long)clen >= Columns)
6003 break;
6004 --first_match;
6005 }
6006
6007 fillchar = fillchar_status(&attr, TRUE);
6008
6009 if (first_match == 0)
6010 {
6011 *buf = NUL;
6012 len = 0;
6013 }
6014 else
6015 {
6016 STRCPY(buf, "< ");
6017 len = 2;
6018 }
6019 clen = len;
6020
6021 i = first_match;
6022 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6023 {
6024 if (i == match)
6025 {
6026 selstart = buf + len;
6027 selstart_col = clen;
6028 }
6029
6030 s = L_MATCH(i);
6031 /* Check for menu separators - replace with '|' */
6032#ifdef FEAT_MENU
6033 emenu = (xp->xp_context == EXPAND_MENUS
6034 || xp->xp_context == EXPAND_MENUNAMES);
6035 if (emenu && menu_is_separator(s))
6036 {
6037 STRCPY(buf + len, transchar('|'));
6038 l = (int)STRLEN(buf + len);
6039 len += l;
6040 clen += l;
6041 }
6042 else
6043#endif
6044 for ( ; *s != NUL; ++s)
6045 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006046 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 clen += ptr2cells(s);
6048#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006049 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 {
6051 STRNCPY(buf + len, s, l);
6052 s += l - 1;
6053 len += l;
6054 }
6055 else
6056#endif
6057 {
6058 STRCPY(buf + len, transchar_byte(*s));
6059 len += (int)STRLEN(buf + len);
6060 }
6061 }
6062 if (i == match)
6063 selend = buf + len;
6064
6065 *(buf + len++) = ' ';
6066 *(buf + len++) = ' ';
6067 clen += 2;
6068 if (++i == num_matches)
6069 break;
6070 }
6071
6072 if (i != num_matches)
6073 {
6074 *(buf + len++) = '>';
6075 ++clen;
6076 }
6077
6078 buf[len] = NUL;
6079
6080 row = cmdline_row - 1;
6081 if (row >= 0)
6082 {
6083 if (wild_menu_showing == 0)
6084 {
6085 if (msg_scrolled > 0)
6086 {
6087 /* Put the wildmenu just above the command line. If there is
6088 * no room, scroll the screen one line up. */
6089 if (cmdline_row == Rows - 1)
6090 {
6091 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6092 ++msg_scrolled;
6093 }
6094 else
6095 {
6096 ++cmdline_row;
6097 ++row;
6098 }
6099 wild_menu_showing = WM_SCROLLED;
6100 }
6101 else
6102 {
6103 /* Create status line if needed by setting 'laststatus' to 2.
6104 * Set 'winminheight' to zero to avoid that the window is
6105 * resized. */
6106 if (lastwin->w_status_height == 0)
6107 {
6108 save_p_ls = p_ls;
6109 save_p_wmh = p_wmh;
6110 p_ls = 2;
6111 p_wmh = 0;
6112 last_status(FALSE);
6113 }
6114 wild_menu_showing = WM_SHOWN;
6115 }
6116 }
6117
6118 screen_puts(buf, row, 0, attr);
6119 if (selstart != NULL && highlight)
6120 {
6121 *selend = NUL;
6122 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6123 }
6124
6125 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6126 }
6127
6128#ifdef FEAT_VERTSPLIT
6129 win_redraw_last_status(topframe);
6130#else
6131 lastwin->w_redr_status = TRUE;
6132#endif
6133 vim_free(buf);
6134}
6135#endif
6136
6137#if defined(FEAT_WINDOWS) || defined(PROTO)
6138/*
6139 * Redraw the status line of window wp.
6140 *
6141 * If inversion is possible we use it. Else '=' characters are used.
6142 */
6143 void
6144win_redr_status(wp)
6145 win_T *wp;
6146{
6147 int row;
6148 char_u *p;
6149 int len;
6150 int fillchar;
6151 int attr;
6152 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006153 static int busy = FALSE;
6154
6155 /* It's possible to get here recursively when 'statusline' (indirectly)
6156 * invokes ":redrawstatus". Simply ignore the call then. */
6157 if (busy)
6158 return;
6159 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 wp->w_redr_status = FALSE;
6162 if (wp->w_status_height == 0)
6163 {
6164 /* no status line, can only be last window */
6165 redraw_cmdline = TRUE;
6166 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006167 else if (!redrawing()
6168#ifdef FEAT_INS_EXPAND
6169 /* don't update status line when popup menu is visible and may be
6170 * drawn over it */
6171 || pum_visible()
6172#endif
6173 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 {
6175 /* Don't redraw right now, do it later. */
6176 wp->w_redr_status = TRUE;
6177 }
6178#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006179 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {
6181 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006182 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184#endif
6185 else
6186 {
6187 fillchar = fillchar_status(&attr, wp == curwin);
6188
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006189 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 p = NameBuff;
6191 len = (int)STRLEN(p);
6192
6193 if (wp->w_buffer->b_help
6194#ifdef FEAT_QUICKFIX
6195 || wp->w_p_pvw
6196#endif
6197 || bufIsChanged(wp->w_buffer)
6198 || wp->w_buffer->b_p_ro)
6199 *(p + len++) = ' ';
6200 if (wp->w_buffer->b_help)
6201 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006202 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 len += (int)STRLEN(p + len);
6204 }
6205#ifdef FEAT_QUICKFIX
6206 if (wp->w_p_pvw)
6207 {
6208 STRCPY(p + len, _("[Preview]"));
6209 len += (int)STRLEN(p + len);
6210 }
6211#endif
6212 if (bufIsChanged(wp->w_buffer))
6213 {
6214 STRCPY(p + len, "[+]");
6215 len += 3;
6216 }
6217 if (wp->w_buffer->b_p_ro)
6218 {
6219 STRCPY(p + len, "[RO]");
6220 len += 4;
6221 }
6222
6223#ifndef FEAT_VERTSPLIT
6224 this_ru_col = ru_col;
6225 if (this_ru_col < (Columns + 1) / 2)
6226 this_ru_col = (Columns + 1) / 2;
6227#else
6228 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6229 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6230 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6231 if (this_ru_col <= 1)
6232 {
6233 p = (char_u *)"<"; /* No room for file name! */
6234 len = 1;
6235 }
6236 else
6237#endif
6238#ifdef FEAT_MBYTE
6239 if (has_mbyte)
6240 {
6241 int clen = 0, i;
6242
6243 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006244 clen = mb_string2cells(p, -1);
6245
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 /* Find first character that will fit.
6247 * Going from start to end is much faster for DBCS. */
6248 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006249 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 clen -= (*mb_ptr2cells)(p + i);
6251 len = clen;
6252 if (i > 0)
6253 {
6254 p = p + i - 1;
6255 *p = '<';
6256 ++len;
6257 }
6258
6259 }
6260 else
6261#endif
6262 if (len > this_ru_col - 1)
6263 {
6264 p += len - (this_ru_col - 1);
6265 *p = '<';
6266 len = this_ru_col - 1;
6267 }
6268
6269 row = W_WINROW(wp) + wp->w_height;
6270 screen_puts(p, row, W_WINCOL(wp), attr);
6271 screen_fill(row, row + 1, len + W_WINCOL(wp),
6272 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6273
6274 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6275 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6276 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6277 - 1 + W_WINCOL(wp)), attr);
6278
6279#ifdef FEAT_CMDL_INFO
6280 win_redr_ruler(wp, TRUE);
6281#endif
6282 }
6283
6284#ifdef FEAT_VERTSPLIT
6285 /*
6286 * May need to draw the character below the vertical separator.
6287 */
6288 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6289 {
6290 if (stl_connected(wp))
6291 fillchar = fillchar_status(&attr, wp == curwin);
6292 else
6293 fillchar = fillchar_vsep(&attr);
6294 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6295 attr);
6296 }
6297#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006298 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299}
6300
Bram Moolenaar238a5642006-02-21 22:12:05 +00006301#ifdef FEAT_STL_OPT
6302/*
6303 * Redraw the status line according to 'statusline' and take care of any
6304 * errors encountered.
6305 */
6306 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006307redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006308 win_T *wp;
6309{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006310 static int entered = FALSE;
6311 int save_called_emsg = called_emsg;
6312
6313 /* When called recursively return. This can happen when the statusline
6314 * contains an expression that triggers a redraw. */
6315 if (entered)
6316 return;
6317 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006318
6319 called_emsg = FALSE;
6320 win_redr_custom(wp, FALSE);
6321 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006322 {
6323 /* When there is an error disable the statusline, otherwise the
6324 * display is messed up with errors and a redraw triggers the problem
6325 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006326 set_string_option_direct((char_u *)"statusline", -1,
6327 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006328 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006329 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006330 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006331 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006332}
6333#endif
6334
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335# ifdef FEAT_VERTSPLIT
6336/*
6337 * Return TRUE if the status line of window "wp" is connected to the status
6338 * line of the window right of it. If not, then it's a vertical separator.
6339 * Only call if (wp->w_vsep_width != 0).
6340 */
6341 int
6342stl_connected(wp)
6343 win_T *wp;
6344{
6345 frame_T *fr;
6346
6347 fr = wp->w_frame;
6348 while (fr->fr_parent != NULL)
6349 {
6350 if (fr->fr_parent->fr_layout == FR_COL)
6351 {
6352 if (fr->fr_next != NULL)
6353 break;
6354 }
6355 else
6356 {
6357 if (fr->fr_next != NULL)
6358 return TRUE;
6359 }
6360 fr = fr->fr_parent;
6361 }
6362 return FALSE;
6363}
6364# endif
6365
6366#endif /* FEAT_WINDOWS */
6367
6368#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6369/*
6370 * Get the value to show for the language mappings, active 'keymap'.
6371 */
6372 int
6373get_keymap_str(wp, buf, len)
6374 win_T *wp;
6375 char_u *buf; /* buffer for the result */
6376 int len; /* length of buffer */
6377{
6378 char_u *p;
6379
6380 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6381 return FALSE;
6382
6383 {
6384#ifdef FEAT_EVAL
6385 buf_T *old_curbuf = curbuf;
6386 win_T *old_curwin = curwin;
6387 char_u *s;
6388
6389 curbuf = wp->w_buffer;
6390 curwin = wp;
6391 STRCPY(buf, "b:keymap_name"); /* must be writable */
6392 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006393 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 --emsg_skip;
6395 curbuf = old_curbuf;
6396 curwin = old_curwin;
6397 if (p == NULL || *p == NUL)
6398#endif
6399 {
6400#ifdef FEAT_KEYMAP
6401 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6402 p = wp->w_buffer->b_p_keymap;
6403 else
6404#endif
6405 p = (char_u *)"lang";
6406 }
6407 if ((int)(STRLEN(p) + 3) < len)
6408 sprintf((char *)buf, "<%s>", p);
6409 else
6410 buf[0] = NUL;
6411#ifdef FEAT_EVAL
6412 vim_free(s);
6413#endif
6414 }
6415 return buf[0] != NUL;
6416}
6417#endif
6418
6419#if defined(FEAT_STL_OPT) || defined(PROTO)
6420/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006421 * Redraw the status line or ruler of window "wp".
6422 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 */
6424 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006425win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006427 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428{
6429 int attr;
6430 int curattr;
6431 int row;
6432 int col = 0;
6433 int maxwidth;
6434 int width;
6435 int n;
6436 int len;
6437 int fillchar;
6438 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006439 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006441 struct stl_hlrec hltab[STL_MAX_ITEM];
6442 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006443 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006444 win_T *ewp;
6445 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446
6447 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006448 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006450 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006451 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006452 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006453 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006454 attr = hl_attr(HLF_TPF);
6455 maxwidth = Columns;
6456# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006457 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006460 else
6461 {
6462 row = W_WINROW(wp) + wp->w_height;
6463 fillchar = fillchar_status(&attr, wp == curwin);
6464 maxwidth = W_WIDTH(wp);
6465
6466 if (draw_ruler)
6467 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006468 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006469 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006470 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006471 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006472 if (*++stl == '-')
6473 stl++;
6474 if (atoi((char *)stl))
6475 while (VIM_ISDIGIT(*stl))
6476 stl++;
6477 if (*stl++ != '(')
6478 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006479 }
6480#ifdef FEAT_VERTSPLIT
6481 col = ru_col - (Columns - W_WIDTH(wp));
6482 if (col < (W_WIDTH(wp) + 1) / 2)
6483 col = (W_WIDTH(wp) + 1) / 2;
6484#else
6485 col = ru_col;
6486 if (col > (Columns + 1) / 2)
6487 col = (Columns + 1) / 2;
6488#endif
6489 maxwidth = W_WIDTH(wp) - col;
6490#ifdef FEAT_WINDOWS
6491 if (!wp->w_status_height)
6492#endif
6493 {
6494 row = Rows - 1;
6495 --maxwidth; /* writing in last column may cause scrolling */
6496 fillchar = ' ';
6497 attr = 0;
6498 }
6499
6500# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006501 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006502# endif
6503 }
6504 else
6505 {
6506 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006507 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006508 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006509 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006510# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006511 use_sandbox = was_set_insecurely((char_u *)"statusline",
6512 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006513# endif
6514 }
6515
6516#ifdef FEAT_VERTSPLIT
6517 col += W_WINCOL(wp);
6518#endif
6519 }
6520
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 if (maxwidth <= 0)
6522 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
Bram Moolenaar61452852011-02-01 18:01:11 +01006524 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6525 * the cursor away and back. */
6526 ewp = wp == NULL ? curwin : wp;
6527 p_crb_save = ewp->w_p_crb;
6528 ewp->w_p_crb = FALSE;
6529
Bram Moolenaar362f3562009-11-03 16:20:34 +00006530 /* Make a copy, because the statusline may include a function call that
6531 * might change the option value and free the memory. */
6532 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006533 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006534 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006535 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006536 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006537 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006539 /* Make all characters printable. */
6540 p = transstr(buf);
6541 if (p != NULL)
6542 {
6543 vim_strncpy(buf, p, sizeof(buf) - 1);
6544 vim_free(p);
6545 }
6546
6547 /* fill up with "fillchar" */
6548 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006549 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
6551#ifdef FEAT_MBYTE
6552 len += (*mb_char2bytes)(fillchar, buf + len);
6553#else
6554 buf[len++] = fillchar;
6555#endif
6556 ++width;
6557 }
6558 buf[len] = NUL;
6559
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006560 /*
6561 * Draw each snippet with the specified highlighting.
6562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 curattr = attr;
6564 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006565 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006567 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 screen_puts_len(p, len, row, col, curattr);
6569 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006570 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006572 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006574 else if (hltab[n].userhl < 0)
6575 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006577 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006578 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579#endif
6580 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006581 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 }
6583 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006584
6585 if (wp == NULL)
6586 {
6587 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6588 col = 0;
6589 len = 0;
6590 p = buf;
6591 fillchar = 0;
6592 for (n = 0; tabtab[n].start != NULL; n++)
6593 {
6594 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6595 while (col < len)
6596 TabPageIdxs[col++] = fillchar;
6597 p = tabtab[n].start;
6598 fillchar = tabtab[n].userhl;
6599 }
6600 while (col < Columns)
6601 TabPageIdxs[col++] = fillchar;
6602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603}
6604
6605#endif /* FEAT_STL_OPT */
6606
6607/*
6608 * Output a single character directly to the screen and update ScreenLines.
6609 */
6610 void
6611screen_putchar(c, row, col, attr)
6612 int c;
6613 int row, col;
6614 int attr;
6615{
6616#ifdef FEAT_MBYTE
6617 char_u buf[MB_MAXBYTES + 1];
6618
6619 buf[(*mb_char2bytes)(c, buf)] = NUL;
6620#else
6621 char_u buf[2];
6622
6623 buf[0] = c;
6624 buf[1] = NUL;
6625#endif
6626 screen_puts(buf, row, col, attr);
6627}
6628
6629/*
6630 * Get a single character directly from ScreenLines into "bytes[]".
6631 * Also return its attribute in *attrp;
6632 */
6633 void
6634screen_getbytes(row, col, bytes, attrp)
6635 int row, col;
6636 char_u *bytes;
6637 int *attrp;
6638{
6639 unsigned off;
6640
6641 /* safety check */
6642 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6643 {
6644 off = LineOffset[row] + col;
6645 *attrp = ScreenAttrs[off];
6646 bytes[0] = ScreenLines[off];
6647 bytes[1] = NUL;
6648
6649#ifdef FEAT_MBYTE
6650 if (enc_utf8 && ScreenLinesUC[off] != 0)
6651 bytes[utfc_char2bytes(off, bytes)] = NUL;
6652 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6653 {
6654 bytes[0] = ScreenLines[off];
6655 bytes[1] = ScreenLines2[off];
6656 bytes[2] = NUL;
6657 }
6658 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6659 {
6660 bytes[1] = ScreenLines[off + 1];
6661 bytes[2] = NUL;
6662 }
6663#endif
6664 }
6665}
6666
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006667#ifdef FEAT_MBYTE
6668static int screen_comp_differs __ARGS((int, int*));
6669
6670/*
6671 * Return TRUE if composing characters for screen posn "off" differs from
6672 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006673 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006674 */
6675 static int
6676screen_comp_differs(off, u8cc)
6677 int off;
6678 int *u8cc;
6679{
6680 int i;
6681
6682 for (i = 0; i < Screen_mco; ++i)
6683 {
6684 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6685 return TRUE;
6686 if (u8cc[i] == 0)
6687 break;
6688 }
6689 return FALSE;
6690}
6691#endif
6692
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693/*
6694 * Put string '*text' on the screen at position 'row' and 'col', with
6695 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6696 * Note: only outputs within one row, message is truncated at screen boundary!
6697 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6698 */
6699 void
6700screen_puts(text, row, col, attr)
6701 char_u *text;
6702 int row;
6703 int col;
6704 int attr;
6705{
6706 screen_puts_len(text, -1, row, col, attr);
6707}
6708
6709/*
6710 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6711 * a NUL.
6712 */
6713 void
6714screen_puts_len(text, len, row, col, attr)
6715 char_u *text;
6716 int len;
6717 int row;
6718 int col;
6719 int attr;
6720{
6721 unsigned off;
6722 char_u *ptr = text;
6723 int c;
6724#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006725 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 int mbyte_blen = 1;
6727 int mbyte_cells = 1;
6728 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006729 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 int clear_next_cell = FALSE;
6731# ifdef FEAT_ARABIC
6732 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006733 int pc, nc, nc1;
6734 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735# endif
6736#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006737#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6738 int force_redraw_this;
6739 int force_redraw_next = FALSE;
6740#endif
6741 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
6743 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6744 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006745 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746
Bram Moolenaarc236c162008-07-13 17:41:49 +00006747#ifdef FEAT_MBYTE
6748 /* When drawing over the right halve of a double-wide char clear out the
6749 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006750 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006751# ifdef FEAT_GUI
6752 && !gui.in_use
6753# endif
6754 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006755 {
6756 ScreenLines[off - 1] = ' ';
6757 ScreenAttrs[off - 1] = 0;
6758 if (enc_utf8)
6759 {
6760 ScreenLinesUC[off - 1] = 0;
6761 ScreenLinesC[0][off - 1] = 0;
6762 }
6763 /* redraw the previous cell, make it empty */
6764 screen_char(off - 1, row, col - 1);
6765 /* force the cell at "col" to be redrawn */
6766 force_redraw_next = TRUE;
6767 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006768#endif
6769
Bram Moolenaar367329b2007-08-30 11:53:22 +00006770#ifdef FEAT_MBYTE
6771 max_off = LineOffset[row] + screen_Columns;
6772#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006773 while (col < screen_Columns
6774 && (len < 0 || (int)(ptr - text) < len)
6775 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
6777 c = *ptr;
6778#ifdef FEAT_MBYTE
6779 /* check if this is the first byte of a multibyte */
6780 if (has_mbyte)
6781 {
6782 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006783 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006785 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6787 mbyte_cells = 1;
6788 else if (enc_dbcs != 0)
6789 mbyte_cells = mbyte_blen;
6790 else /* enc_utf8 */
6791 {
6792 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006793 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 (int)((text + len) - ptr));
6795 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006796 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006798# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 /* Non-BMP character: display as ? or fullwidth ?. */
6800 if (u8c >= 0x10000)
6801 {
6802 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6803 if (attr == 0)
6804 attr = hl_attr(HLF_8);
6805 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807# ifdef FEAT_ARABIC
6808 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6809 {
6810 /* Do Arabic shaping. */
6811 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6812 {
6813 /* Past end of string to be displayed. */
6814 nc = NUL;
6815 nc1 = NUL;
6816 }
6817 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006818 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006819 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6820 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006821 nc1 = pcc[0];
6822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 pc = prev_c;
6824 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006825 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 }
6827 else
6828 prev_c = u8c;
6829# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006830 if (col + mbyte_cells > screen_Columns)
6831 {
6832 /* Only 1 cell left, but character requires 2 cells:
6833 * display a '>' in the last column to avoid wrapping. */
6834 c = '>';
6835 mbyte_cells = 1;
6836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
6838 }
6839#endif
6840
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006841#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6842 force_redraw_this = force_redraw_next;
6843 force_redraw_next = FALSE;
6844#endif
6845
6846 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847#ifdef FEAT_MBYTE
6848 || (mbyte_cells == 2
6849 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6850 || (enc_dbcs == DBCS_JPNU
6851 && c == 0x8e
6852 && ScreenLines2[off] != ptr[1])
6853 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006854 && (ScreenLinesUC[off] !=
6855 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6856 || (ScreenLinesUC[off] != 0
6857 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858#endif
6859 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006860 || exmode_active;
6861
6862 if (need_redraw
6863#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6864 || force_redraw_this
6865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 )
6867 {
6868#if defined(FEAT_GUI) || defined(UNIX)
6869 /* The bold trick makes a single row of pixels appear in the next
6870 * character. When a bold character is removed, the next
6871 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006872 * and for some xterms. */
6873 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874# ifdef FEAT_GUI
6875 gui.in_use
6876# endif
6877# if defined(FEAT_GUI) && defined(UNIX)
6878 ||
6879# endif
6880# ifdef UNIX
6881 term_is_xterm
6882# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006883 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006885 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006887 if (n > HL_ALL)
6888 n = syn_attr2attr(n);
6889 if (n & HL_BOLD)
6890 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892#endif
6893#ifdef FEAT_MBYTE
6894 /* When at the end of the text and overwriting a two-cell
6895 * character with a one-cell character, need to clear the next
6896 * cell. Also when overwriting the left halve of a two-cell char
6897 * with the right halve of a two-cell char. Do this only once
6898 * (mb_off2cells() may return 2 on the right halve). */
6899 if (clear_next_cell)
6900 clear_next_cell = FALSE;
6901 else if (has_mbyte
6902 && (len < 0 ? ptr[mbyte_blen] == NUL
6903 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006904 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006906 && (*mb_off2cells)(off, max_off) == 1
6907 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 clear_next_cell = TRUE;
6909
6910 /* Make sure we never leave a second byte of a double-byte behind,
6911 * it confuses mb_off2cells(). */
6912 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006913 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006915 && (*mb_off2cells)(off, max_off) == 1
6916 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 ScreenLines[off + mbyte_blen] = 0;
6918#endif
6919 ScreenLines[off] = c;
6920 ScreenAttrs[off] = attr;
6921#ifdef FEAT_MBYTE
6922 if (enc_utf8)
6923 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006924 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 ScreenLinesUC[off] = 0;
6926 else
6927 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006928 int i;
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006931 for (i = 0; i < Screen_mco; ++i)
6932 {
6933 ScreenLinesC[i][off] = u8cc[i];
6934 if (u8cc[i] == 0)
6935 break;
6936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
6938 if (mbyte_cells == 2)
6939 {
6940 ScreenLines[off + 1] = 0;
6941 ScreenAttrs[off + 1] = attr;
6942 }
6943 screen_char(off, row, col);
6944 }
6945 else if (mbyte_cells == 2)
6946 {
6947 ScreenLines[off + 1] = ptr[1];
6948 ScreenAttrs[off + 1] = attr;
6949 screen_char_2(off, row, col);
6950 }
6951 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6952 {
6953 ScreenLines2[off] = ptr[1];
6954 screen_char(off, row, col);
6955 }
6956 else
6957#endif
6958 screen_char(off, row, col);
6959 }
6960#ifdef FEAT_MBYTE
6961 if (has_mbyte)
6962 {
6963 off += mbyte_cells;
6964 col += mbyte_cells;
6965 ptr += mbyte_blen;
6966 if (clear_next_cell)
6967 ptr = (char_u *)" ";
6968 }
6969 else
6970#endif
6971 {
6972 ++off;
6973 ++col;
6974 ++ptr;
6975 }
6976 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006977
6978#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6979 /* If we detected the next character needs to be redrawn, but the text
6980 * doesn't extend up to there, update the character here. */
6981 if (force_redraw_next && col < screen_Columns)
6982 {
6983# ifdef FEAT_MBYTE
6984 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6985 screen_char_2(off, row, col);
6986 else
6987# endif
6988 screen_char(off, row, col);
6989 }
6990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991}
6992
6993#ifdef FEAT_SEARCH_EXTRA
6994/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006995 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 */
6997 static void
6998start_search_hl()
6999{
7000 if (p_hls && !no_hlsearch)
7001 {
7002 last_pat_prog(&search_hl.rm);
7003 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007004# ifdef FEAT_RELTIME
7005 /* Set the time limit to 'redrawtime'. */
7006 profile_setlimit(p_rdt, &search_hl.tm);
7007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
7009}
7010
7011/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007012 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 */
7014 static void
7015end_search_hl()
7016{
7017 if (search_hl.rm.regprog != NULL)
7018 {
7019 vim_free(search_hl.rm.regprog);
7020 search_hl.rm.regprog = NULL;
7021 }
7022}
7023
7024/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007025 * Init for calling prepare_search_hl().
7026 */
7027 static void
7028init_search_hl(wp)
7029 win_T *wp;
7030{
7031 matchitem_T *cur;
7032
7033 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7034 * match */
7035 cur = wp->w_match_head;
7036 while (cur != NULL)
7037 {
7038 cur->hl.rm = cur->match;
7039 if (cur->hlg_id == 0)
7040 cur->hl.attr = 0;
7041 else
7042 cur->hl.attr = syn_id2attr(cur->hlg_id);
7043 cur->hl.buf = wp->w_buffer;
7044 cur->hl.lnum = 0;
7045 cur->hl.first_lnum = 0;
7046# ifdef FEAT_RELTIME
7047 /* Set the time limit to 'redrawtime'. */
7048 profile_setlimit(p_rdt, &(cur->hl.tm));
7049# endif
7050 cur = cur->next;
7051 }
7052 search_hl.buf = wp->w_buffer;
7053 search_hl.lnum = 0;
7054 search_hl.first_lnum = 0;
7055 /* time limit is set at the toplevel, for all windows */
7056}
7057
7058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 * Advance to the match in window "wp" line "lnum" or past it.
7060 */
7061 static void
7062prepare_search_hl(wp, lnum)
7063 win_T *wp;
7064 linenr_T lnum;
7065{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007066 matchitem_T *cur; /* points to the match list */
7067 match_T *shl; /* points to search_hl or a match */
7068 int shl_flag; /* flag to indicate whether search_hl
7069 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 int n;
7071
7072 /*
7073 * When using a multi-line pattern, start searching at the top
7074 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007075 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007077 cur = wp->w_match_head;
7078 shl_flag = FALSE;
7079 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007081 if (shl_flag == FALSE)
7082 {
7083 shl = &search_hl;
7084 shl_flag = TRUE;
7085 }
7086 else
7087 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (shl->rm.regprog != NULL
7089 && shl->lnum == 0
7090 && re_multiline(shl->rm.regprog))
7091 {
7092 if (shl->first_lnum == 0)
7093 {
7094# ifdef FEAT_FOLDING
7095 for (shl->first_lnum = lnum;
7096 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7097 if (hasFoldingWin(wp, shl->first_lnum - 1,
7098 NULL, NULL, TRUE, NULL))
7099 break;
7100# else
7101 shl->first_lnum = wp->w_topline;
7102# endif
7103 }
7104 n = 0;
7105 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7106 {
7107 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7108 if (shl->lnum != 0)
7109 {
7110 shl->first_lnum = shl->lnum
7111 + shl->rm.endpos[0].lnum
7112 - shl->rm.startpos[0].lnum;
7113 n = shl->rm.endpos[0].col;
7114 }
7115 else
7116 {
7117 ++shl->first_lnum;
7118 n = 0;
7119 }
7120 }
7121 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007122 if (shl != &search_hl && cur != NULL)
7123 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 }
7125}
7126
7127/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007128 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 * Uses shl->buf.
7130 * Sets shl->lnum and shl->rm contents.
7131 * Note: Assumes a previous match is always before "lnum", unless
7132 * shl->lnum is zero.
7133 * Careful: Any pointers for buffer lines will become invalid.
7134 */
7135 static void
7136next_search_hl(win, shl, lnum, mincol)
7137 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007138 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 linenr_T lnum;
7140 colnr_T mincol; /* minimal column for a match */
7141{
7142 linenr_T l;
7143 colnr_T matchcol;
7144 long nmatched;
7145
7146 if (shl->lnum != 0)
7147 {
7148 /* Check for three situations:
7149 * 1. If the "lnum" is below a previous match, start a new search.
7150 * 2. If the previous match includes "mincol", use it.
7151 * 3. Continue after the previous match.
7152 */
7153 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7154 if (lnum > l)
7155 shl->lnum = 0;
7156 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7157 return;
7158 }
7159
7160 /*
7161 * Repeat searching for a match until one is found that includes "mincol"
7162 * or none is found in this line.
7163 */
7164 called_emsg = FALSE;
7165 for (;;)
7166 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007167#ifdef FEAT_RELTIME
7168 /* Stop searching after passing the time limit. */
7169 if (profile_passed_limit(&(shl->tm)))
7170 {
7171 shl->lnum = 0; /* no match found in time */
7172 break;
7173 }
7174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 /* Three situations:
7176 * 1. No useful previous match: search from start of line.
7177 * 2. Not Vi compatible or empty match: continue at next character.
7178 * Break the loop if this is beyond the end of the line.
7179 * 3. Vi compatible searching: continue at end of previous match.
7180 */
7181 if (shl->lnum == 0)
7182 matchcol = 0;
7183 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7184 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007185 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007187 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007188
7189 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007190 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007191 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007193 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 shl->lnum = 0;
7195 break;
7196 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007197#ifdef FEAT_MBYTE
7198 if (has_mbyte)
7199 matchcol += mb_ptr2len(ml);
7200 else
7201#endif
7202 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 }
7204 else
7205 matchcol = shl->rm.endpos[0].col;
7206
7207 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007208 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7209#ifdef FEAT_RELTIME
7210 &(shl->tm)
7211#else
7212 NULL
7213#endif
7214 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007215 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
7217 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007218 if (shl == &search_hl)
7219 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007220 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007221 vim_free(shl->rm.regprog);
7222 no_hlsearch = TRUE;
7223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007225 shl->lnum = 0;
7226 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 break;
7228 }
7229 if (nmatched == 0)
7230 {
7231 shl->lnum = 0; /* no match found */
7232 break;
7233 }
7234 if (shl->rm.startpos[0].lnum > 0
7235 || shl->rm.startpos[0].col >= mincol
7236 || nmatched > 1
7237 || shl->rm.endpos[0].col > mincol)
7238 {
7239 shl->lnum += shl->rm.startpos[0].lnum;
7240 break; /* useful match found */
7241 }
7242 }
7243}
7244#endif
7245
7246 static void
7247screen_start_highlight(attr)
7248 int attr;
7249{
7250 attrentry_T *aep = NULL;
7251
7252 screen_attr = attr;
7253 if (full_screen
7254#ifdef WIN3264
7255 && termcap_active
7256#endif
7257 )
7258 {
7259#ifdef FEAT_GUI
7260 if (gui.in_use)
7261 {
7262 char buf[20];
7263
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007264 /* The GUI handles this internally. */
7265 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 OUT_STR(buf);
7267 }
7268 else
7269#endif
7270 {
7271 if (attr > HL_ALL) /* special HL attr. */
7272 {
7273 if (t_colors > 1)
7274 aep = syn_cterm_attr2entry(attr);
7275 else
7276 aep = syn_term_attr2entry(attr);
7277 if (aep == NULL) /* did ":syntax clear" */
7278 attr = 0;
7279 else
7280 attr = aep->ae_attr;
7281 }
7282 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7283 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007284 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7285 && cterm_normal_fg_bold)
7286 /* If the Normal FG color has BOLD attribute and the new HL
7287 * has a FG color defined, clear BOLD. */
7288 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7290 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007291 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7292 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 out_str(T_US);
7294 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7295 out_str(T_CZH);
7296 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7297 out_str(T_MR);
7298
7299 /*
7300 * Output the color or start string after bold etc., in case the
7301 * bold etc. override the color setting.
7302 */
7303 if (aep != NULL)
7304 {
7305 if (t_colors > 1)
7306 {
7307 if (aep->ae_u.cterm.fg_color)
7308 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7309 if (aep->ae_u.cterm.bg_color)
7310 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7311 }
7312 else
7313 {
7314 if (aep->ae_u.term.start != NULL)
7315 out_str(aep->ae_u.term.start);
7316 }
7317 }
7318 }
7319 }
7320}
7321
7322 void
7323screen_stop_highlight()
7324{
7325 int do_ME = FALSE; /* output T_ME code */
7326
7327 if (screen_attr != 0
7328#ifdef WIN3264
7329 && termcap_active
7330#endif
7331 )
7332 {
7333#ifdef FEAT_GUI
7334 if (gui.in_use)
7335 {
7336 char buf[20];
7337
7338 /* use internal GUI code */
7339 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7340 OUT_STR(buf);
7341 }
7342 else
7343#endif
7344 {
7345 if (screen_attr > HL_ALL) /* special HL attr. */
7346 {
7347 attrentry_T *aep;
7348
7349 if (t_colors > 1)
7350 {
7351 /*
7352 * Assume that t_me restores the original colors!
7353 */
7354 aep = syn_cterm_attr2entry(screen_attr);
7355 if (aep != NULL && (aep->ae_u.cterm.fg_color
7356 || aep->ae_u.cterm.bg_color))
7357 do_ME = TRUE;
7358 }
7359 else
7360 {
7361 aep = syn_term_attr2entry(screen_attr);
7362 if (aep != NULL && aep->ae_u.term.stop != NULL)
7363 {
7364 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7365 do_ME = TRUE;
7366 else
7367 out_str(aep->ae_u.term.stop);
7368 }
7369 }
7370 if (aep == NULL) /* did ":syntax clear" */
7371 screen_attr = 0;
7372 else
7373 screen_attr = aep->ae_attr;
7374 }
7375
7376 /*
7377 * Often all ending-codes are equal to T_ME. Avoid outputting the
7378 * same sequence several times.
7379 */
7380 if (screen_attr & HL_STANDOUT)
7381 {
7382 if (STRCMP(T_SE, T_ME) == 0)
7383 do_ME = TRUE;
7384 else
7385 out_str(T_SE);
7386 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007387 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 {
7389 if (STRCMP(T_UE, T_ME) == 0)
7390 do_ME = TRUE;
7391 else
7392 out_str(T_UE);
7393 }
7394 if (screen_attr & HL_ITALIC)
7395 {
7396 if (STRCMP(T_CZR, T_ME) == 0)
7397 do_ME = TRUE;
7398 else
7399 out_str(T_CZR);
7400 }
7401 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7402 out_str(T_ME);
7403
7404 if (t_colors > 1)
7405 {
7406 /* set Normal cterm colors */
7407 if (cterm_normal_fg_color != 0)
7408 term_fg_color(cterm_normal_fg_color - 1);
7409 if (cterm_normal_bg_color != 0)
7410 term_bg_color(cterm_normal_bg_color - 1);
7411 if (cterm_normal_fg_bold)
7412 out_str(T_MD);
7413 }
7414 }
7415 }
7416 screen_attr = 0;
7417}
7418
7419/*
7420 * Reset the colors for a cterm. Used when leaving Vim.
7421 * The machine specific code may override this again.
7422 */
7423 void
7424reset_cterm_colors()
7425{
7426 if (t_colors > 1)
7427 {
7428 /* set Normal cterm colors */
7429 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7430 {
7431 out_str(T_OP);
7432 screen_attr = -1;
7433 }
7434 if (cterm_normal_fg_bold)
7435 {
7436 out_str(T_ME);
7437 screen_attr = -1;
7438 }
7439 }
7440}
7441
7442/*
7443 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7444 * using the attributes from ScreenAttrs["off"].
7445 */
7446 static void
7447screen_char(off, row, col)
7448 unsigned off;
7449 int row;
7450 int col;
7451{
7452 int attr;
7453
7454 /* Check for illegal values, just in case (could happen just after
7455 * resizing). */
7456 if (row >= screen_Rows || col >= screen_Columns)
7457 return;
7458
7459 /* Outputting the last character on the screen may scrollup the screen.
7460 * Don't to it! Mark the character invalid (update it when scrolled up) */
7461 if (row == screen_Rows - 1 && col == screen_Columns - 1
7462#ifdef FEAT_RIGHTLEFT
7463 /* account for first command-line character in rightleft mode */
7464 && !cmdmsg_rl
7465#endif
7466 )
7467 {
7468 ScreenAttrs[off] = (sattr_T)-1;
7469 return;
7470 }
7471
7472 /*
7473 * Stop highlighting first, so it's easier to move the cursor.
7474 */
7475#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7476 if (screen_char_attr != 0)
7477 attr = screen_char_attr;
7478 else
7479#endif
7480 attr = ScreenAttrs[off];
7481 if (screen_attr != attr)
7482 screen_stop_highlight();
7483
7484 windgoto(row, col);
7485
7486 if (screen_attr != attr)
7487 screen_start_highlight(attr);
7488
7489#ifdef FEAT_MBYTE
7490 if (enc_utf8 && ScreenLinesUC[off] != 0)
7491 {
7492 char_u buf[MB_MAXBYTES + 1];
7493
7494 /* Convert UTF-8 character to bytes and write it. */
7495
7496 buf[utfc_char2bytes(off, buf)] = NUL;
7497
7498 out_str(buf);
7499 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7500 ++screen_cur_col;
7501 }
7502 else
7503#endif
7504 {
7505#ifdef FEAT_MBYTE
7506 out_flush_check();
7507#endif
7508 out_char(ScreenLines[off]);
7509#ifdef FEAT_MBYTE
7510 /* double-byte character in single-width cell */
7511 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7512 out_char(ScreenLines2[off]);
7513#endif
7514 }
7515
7516 screen_cur_col++;
7517}
7518
7519#ifdef FEAT_MBYTE
7520
7521/*
7522 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7523 * on the screen at position 'row' and 'col'.
7524 * The attributes of the first byte is used for all. This is required to
7525 * output the two bytes of a double-byte character with nothing in between.
7526 */
7527 static void
7528screen_char_2(off, row, col)
7529 unsigned off;
7530 int row;
7531 int col;
7532{
7533 /* Check for illegal values (could be wrong when screen was resized). */
7534 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7535 return;
7536
7537 /* Outputting the last character on the screen may scrollup the screen.
7538 * Don't to it! Mark the character invalid (update it when scrolled up) */
7539 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7540 {
7541 ScreenAttrs[off] = (sattr_T)-1;
7542 return;
7543 }
7544
7545 /* Output the first byte normally (positions the cursor), then write the
7546 * second byte directly. */
7547 screen_char(off, row, col);
7548 out_char(ScreenLines[off + 1]);
7549 ++screen_cur_col;
7550}
7551#endif
7552
7553#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7554/*
7555 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7556 * This uses the contents of ScreenLines[] and doesn't change it.
7557 */
7558 void
7559screen_draw_rectangle(row, col, height, width, invert)
7560 int row;
7561 int col;
7562 int height;
7563 int width;
7564 int invert;
7565{
7566 int r, c;
7567 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007568#ifdef FEAT_MBYTE
7569 int max_off;
7570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007572 /* Can't use ScreenLines unless initialized */
7573 if (ScreenLines == NULL)
7574 return;
7575
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 if (invert)
7577 screen_char_attr = HL_INVERSE;
7578 for (r = row; r < row + height; ++r)
7579 {
7580 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007581#ifdef FEAT_MBYTE
7582 max_off = off + screen_Columns;
7583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 for (c = col; c < col + width; ++c)
7585 {
7586#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007587 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 {
7589 screen_char_2(off + c, r, c);
7590 ++c;
7591 }
7592 else
7593#endif
7594 {
7595 screen_char(off + c, r, c);
7596#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007597 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 ++c;
7599#endif
7600 }
7601 }
7602 }
7603 screen_char_attr = 0;
7604}
7605#endif
7606
7607#ifdef FEAT_VERTSPLIT
7608/*
7609 * Redraw the characters for a vertically split window.
7610 */
7611 static void
7612redraw_block(row, end, wp)
7613 int row;
7614 int end;
7615 win_T *wp;
7616{
7617 int col;
7618 int width;
7619
7620# ifdef FEAT_CLIPBOARD
7621 clip_may_clear_selection(row, end - 1);
7622# endif
7623
7624 if (wp == NULL)
7625 {
7626 col = 0;
7627 width = Columns;
7628 }
7629 else
7630 {
7631 col = wp->w_wincol;
7632 width = wp->w_width;
7633 }
7634 screen_draw_rectangle(row, col, end - row, width, FALSE);
7635}
7636#endif
7637
7638/*
7639 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7640 * with character 'c1' in first column followed by 'c2' in the other columns.
7641 * Use attributes 'attr'.
7642 */
7643 void
7644screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7645 int start_row, end_row;
7646 int start_col, end_col;
7647 int c1, c2;
7648 int attr;
7649{
7650 int row;
7651 int col;
7652 int off;
7653 int end_off;
7654 int did_delete;
7655 int c;
7656 int norm_term;
7657#if defined(FEAT_GUI) || defined(UNIX)
7658 int force_next = FALSE;
7659#endif
7660
7661 if (end_row > screen_Rows) /* safety check */
7662 end_row = screen_Rows;
7663 if (end_col > screen_Columns) /* safety check */
7664 end_col = screen_Columns;
7665 if (ScreenLines == NULL
7666 || start_row >= end_row
7667 || start_col >= end_col) /* nothing to do */
7668 return;
7669
7670 /* it's a "normal" terminal when not in a GUI or cterm */
7671 norm_term = (
7672#ifdef FEAT_GUI
7673 !gui.in_use &&
7674#endif
7675 t_colors <= 1);
7676 for (row = start_row; row < end_row; ++row)
7677 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007678#ifdef FEAT_MBYTE
7679 if (has_mbyte
7680# ifdef FEAT_GUI
7681 && !gui.in_use
7682# endif
7683 )
7684 {
7685 /* When drawing over the right halve of a double-wide char clear
7686 * out the left halve. When drawing over the left halve of a
7687 * double wide-char clear out the right halve. Only needed in a
7688 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007689 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007690 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007691 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007692 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007693 }
7694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 /*
7696 * Try to use delete-line termcap code, when no attributes or in a
7697 * "normal" terminal, where a bold/italic space is just a
7698 * space.
7699 */
7700 did_delete = FALSE;
7701 if (c2 == ' '
7702 && end_col == Columns
7703 && can_clear(T_CE)
7704 && (attr == 0
7705 || (norm_term
7706 && attr <= HL_ALL
7707 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7708 {
7709 /*
7710 * check if we really need to clear something
7711 */
7712 col = start_col;
7713 if (c1 != ' ') /* don't clear first char */
7714 ++col;
7715
7716 off = LineOffset[row] + col;
7717 end_off = LineOffset[row] + end_col;
7718
7719 /* skip blanks (used often, keep it fast!) */
7720#ifdef FEAT_MBYTE
7721 if (enc_utf8)
7722 while (off < end_off && ScreenLines[off] == ' '
7723 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7724 ++off;
7725 else
7726#endif
7727 while (off < end_off && ScreenLines[off] == ' '
7728 && ScreenAttrs[off] == 0)
7729 ++off;
7730 if (off < end_off) /* something to be cleared */
7731 {
7732 col = off - LineOffset[row];
7733 screen_stop_highlight();
7734 term_windgoto(row, col);/* clear rest of this screen line */
7735 out_str(T_CE);
7736 screen_start(); /* don't know where cursor is now */
7737 col = end_col - col;
7738 while (col--) /* clear chars in ScreenLines */
7739 {
7740 ScreenLines[off] = ' ';
7741#ifdef FEAT_MBYTE
7742 if (enc_utf8)
7743 ScreenLinesUC[off] = 0;
7744#endif
7745 ScreenAttrs[off] = 0;
7746 ++off;
7747 }
7748 }
7749 did_delete = TRUE; /* the chars are cleared now */
7750 }
7751
7752 off = LineOffset[row] + start_col;
7753 c = c1;
7754 for (col = start_col; col < end_col; ++col)
7755 {
7756 if (ScreenLines[off] != c
7757#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007758 || (enc_utf8 && (int)ScreenLinesUC[off]
7759 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760#endif
7761 || ScreenAttrs[off] != attr
7762#if defined(FEAT_GUI) || defined(UNIX)
7763 || force_next
7764#endif
7765 )
7766 {
7767#if defined(FEAT_GUI) || defined(UNIX)
7768 /* The bold trick may make a single row of pixels appear in
7769 * the next character. When a bold character is removed, the
7770 * next character should be redrawn too. This happens for our
7771 * own GUI and for some xterms. */
7772 if (
7773# ifdef FEAT_GUI
7774 gui.in_use
7775# endif
7776# if defined(FEAT_GUI) && defined(UNIX)
7777 ||
7778# endif
7779# ifdef UNIX
7780 term_is_xterm
7781# endif
7782 )
7783 {
7784 if (ScreenLines[off] != ' '
7785 && (ScreenAttrs[off] > HL_ALL
7786 || ScreenAttrs[off] & HL_BOLD))
7787 force_next = TRUE;
7788 else
7789 force_next = FALSE;
7790 }
7791#endif
7792 ScreenLines[off] = c;
7793#ifdef FEAT_MBYTE
7794 if (enc_utf8)
7795 {
7796 if (c >= 0x80)
7797 {
7798 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007799 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 }
7801 else
7802 ScreenLinesUC[off] = 0;
7803 }
7804#endif
7805 ScreenAttrs[off] = attr;
7806 if (!did_delete || c != ' ')
7807 screen_char(off, row, col);
7808 }
7809 ++off;
7810 if (col == start_col)
7811 {
7812 if (did_delete)
7813 break;
7814 c = c2;
7815 }
7816 }
7817 if (end_col == Columns)
7818 LineWraps[row] = FALSE;
7819 if (row == Rows - 1) /* overwritten the command line */
7820 {
7821 redraw_cmdline = TRUE;
7822 if (c1 == ' ' && c2 == ' ')
7823 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007824 if (start_col == 0)
7825 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
7827 }
7828}
7829
7830/*
7831 * Check if there should be a delay. Used before clearing or redrawing the
7832 * screen or the command line.
7833 */
7834 void
7835check_for_delay(check_msg_scroll)
7836 int check_msg_scroll;
7837{
7838 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7839 && !did_wait_return
7840 && emsg_silent == 0)
7841 {
7842 out_flush();
7843 ui_delay(1000L, TRUE);
7844 emsg_on_display = FALSE;
7845 if (check_msg_scroll)
7846 msg_scroll = FALSE;
7847 }
7848}
7849
7850/*
7851 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007852 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 * Returns TRUE if there is a valid screen to write to.
7854 * Returns FALSE when starting up and screen not initialized yet.
7855 */
7856 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007857screen_valid(doclear)
7858 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007860 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 return (ScreenLines != NULL);
7862}
7863
7864/*
7865 * Resize the shell to Rows and Columns.
7866 * Allocate ScreenLines[] and associated items.
7867 *
7868 * There may be some time between setting Rows and Columns and (re)allocating
7869 * ScreenLines[]. This happens when starting up and when (manually) changing
7870 * the shell size. Always use screen_Rows and screen_Columns to access items
7871 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7872 * final size of the shell is needed.
7873 */
7874 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01007875screenalloc(doclear)
7876 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
7878 int new_row, old_row;
7879#ifdef FEAT_GUI
7880 int old_Rows;
7881#endif
7882 win_T *wp;
7883 int outofmem = FALSE;
7884 int len;
7885 schar_T *new_ScreenLines;
7886#ifdef FEAT_MBYTE
7887 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007888 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007890 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891#endif
7892 sattr_T *new_ScreenAttrs;
7893 unsigned *new_LineOffset;
7894 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007895#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007896 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007897 tabpage_T *tp;
7898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007900 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007901#ifdef FEAT_AUTOCMD
7902 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007904retry:
7905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 /*
7907 * Allocation of the screen buffers is done only when the size changes and
7908 * when Rows and Columns have been set and we have started doing full
7909 * screen stuff.
7910 */
7911 if ((ScreenLines != NULL
7912 && Rows == screen_Rows
7913 && Columns == screen_Columns
7914#ifdef FEAT_MBYTE
7915 && enc_utf8 == (ScreenLinesUC != NULL)
7916 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007917 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918#endif
7919 )
7920 || Rows == 0
7921 || Columns == 0
7922 || (!full_screen && ScreenLines == NULL))
7923 return;
7924
7925 /*
7926 * It's possible that we produce an out-of-memory message below, which
7927 * will cause this function to be called again. To break the loop, just
7928 * return here.
7929 */
7930 if (entered)
7931 return;
7932 entered = TRUE;
7933
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007934 /*
7935 * Note that the window sizes are updated before reallocating the arrays,
7936 * thus we must not redraw here!
7937 */
7938 ++RedrawingDisabled;
7939
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 win_new_shellsize(); /* fit the windows in the new sized shell */
7941
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 comp_col(); /* recompute columns for shown command and ruler */
7943
7944 /*
7945 * We're changing the size of the screen.
7946 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7947 * - Move lines from the old arrays into the new arrays, clear extra
7948 * lines (unless the screen is going to be cleared).
7949 * - Free the old arrays.
7950 *
7951 * If anything fails, make ScreenLines NULL, so we don't do anything!
7952 * Continuing with the old ScreenLines may result in a crash, because the
7953 * size is wrong.
7954 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007955 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007957#ifdef FEAT_AUTOCMD
7958 if (aucmd_win != NULL)
7959 win_free_lsize(aucmd_win);
7960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961
7962 new_ScreenLines = (schar_T *)lalloc((long_u)(
7963 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7964#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007965 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 if (enc_utf8)
7967 {
7968 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7969 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007970 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007971 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7973 }
7974 if (enc_dbcs == DBCS_JPNU)
7975 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7976 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7977#endif
7978 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7979 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7980 new_LineOffset = (unsigned *)lalloc((long_u)(
7981 Rows * sizeof(unsigned)), FALSE);
7982 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007983#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007984 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007987 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {
7989 if (win_alloc_lines(wp) == FAIL)
7990 {
7991 outofmem = TRUE;
7992#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007993 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994#endif
7995 }
7996 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007997#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007998 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7999 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008000 outofmem = TRUE;
8001#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008002#ifdef FEAT_WINDOWS
8003give_up:
8004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008006#ifdef FEAT_MBYTE
8007 for (i = 0; i < p_mco; ++i)
8008 if (new_ScreenLinesC[i] == NULL)
8009 break;
8010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 if (new_ScreenLines == NULL
8012#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008013 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8015#endif
8016 || new_ScreenAttrs == NULL
8017 || new_LineOffset == NULL
8018 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008019#ifdef FEAT_WINDOWS
8020 || new_TabPageIdxs == NULL
8021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 || outofmem)
8023 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008024 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008025 {
8026 /* guess the size */
8027 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8028
8029 /* Remember we did this to avoid getting outofmem messages over
8030 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008031 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 vim_free(new_ScreenLines);
8034 new_ScreenLines = NULL;
8035#ifdef FEAT_MBYTE
8036 vim_free(new_ScreenLinesUC);
8037 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008038 for (i = 0; i < p_mco; ++i)
8039 {
8040 vim_free(new_ScreenLinesC[i]);
8041 new_ScreenLinesC[i] = NULL;
8042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 vim_free(new_ScreenLines2);
8044 new_ScreenLines2 = NULL;
8045#endif
8046 vim_free(new_ScreenAttrs);
8047 new_ScreenAttrs = NULL;
8048 vim_free(new_LineOffset);
8049 new_LineOffset = NULL;
8050 vim_free(new_LineWraps);
8051 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008052#ifdef FEAT_WINDOWS
8053 vim_free(new_TabPageIdxs);
8054 new_TabPageIdxs = NULL;
8055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 }
8057 else
8058 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008059 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008060
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 for (new_row = 0; new_row < Rows; ++new_row)
8062 {
8063 new_LineOffset[new_row] = new_row * Columns;
8064 new_LineWraps[new_row] = FALSE;
8065
8066 /*
8067 * If the screen is not going to be cleared, copy as much as
8068 * possible from the old screen to the new one and clear the rest
8069 * (used when resizing the window at the "--more--" prompt or when
8070 * executing an external command, for the GUI).
8071 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008072 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {
8074 (void)vim_memset(new_ScreenLines + new_row * Columns,
8075 ' ', (size_t)Columns * sizeof(schar_T));
8076#ifdef FEAT_MBYTE
8077 if (enc_utf8)
8078 {
8079 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8080 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008081 for (i = 0; i < p_mco; ++i)
8082 (void)vim_memset(new_ScreenLinesC[i]
8083 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 0, (size_t)Columns * sizeof(u8char_T));
8085 }
8086 if (enc_dbcs == DBCS_JPNU)
8087 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8088 0, (size_t)Columns * sizeof(schar_T));
8089#endif
8090 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8091 0, (size_t)Columns * sizeof(sattr_T));
8092 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008093 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {
8095 if (screen_Columns < Columns)
8096 len = screen_Columns;
8097 else
8098 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008099#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008100 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008101 * may be invalid now. Also when p_mco changes. */
8102 if (!(enc_utf8 && ScreenLinesUC == NULL)
8103 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008104#endif
8105 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8106 ScreenLines + LineOffset[old_row],
8107 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008109 if (enc_utf8 && ScreenLinesUC != NULL
8110 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {
8112 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8113 ScreenLinesUC + LineOffset[old_row],
8114 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008115 for (i = 0; i < p_mco; ++i)
8116 mch_memmove(new_ScreenLinesC[i]
8117 + new_LineOffset[new_row],
8118 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 (size_t)len * sizeof(u8char_T));
8120 }
8121 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8122 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8123 ScreenLines2 + LineOffset[old_row],
8124 (size_t)len * sizeof(schar_T));
8125#endif
8126 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8127 ScreenAttrs + LineOffset[old_row],
8128 (size_t)len * sizeof(sattr_T));
8129 }
8130 }
8131 }
8132 /* Use the last line of the screen for the current line. */
8133 current_ScreenLine = new_ScreenLines + Rows * Columns;
8134 }
8135
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008136 free_screenlines();
8137
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 ScreenLines = new_ScreenLines;
8139#ifdef FEAT_MBYTE
8140 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008141 for (i = 0; i < p_mco; ++i)
8142 ScreenLinesC[i] = new_ScreenLinesC[i];
8143 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 ScreenLines2 = new_ScreenLines2;
8145#endif
8146 ScreenAttrs = new_ScreenAttrs;
8147 LineOffset = new_LineOffset;
8148 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008149#ifdef FEAT_WINDOWS
8150 TabPageIdxs = new_TabPageIdxs;
8151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152
8153 /* It's important that screen_Rows and screen_Columns reflect the actual
8154 * size of ScreenLines[]. Set them before calling anything. */
8155#ifdef FEAT_GUI
8156 old_Rows = screen_Rows;
8157#endif
8158 screen_Rows = Rows;
8159 screen_Columns = Columns;
8160
8161 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008162 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 screenclear2();
8164
8165#ifdef FEAT_GUI
8166 else if (gui.in_use
8167 && !gui.starting
8168 && ScreenLines != NULL
8169 && old_Rows != Rows)
8170 {
8171 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8172 /*
8173 * Adjust the position of the cursor, for when executing an external
8174 * command.
8175 */
8176 if (msg_row >= Rows) /* Rows got smaller */
8177 msg_row = Rows - 1; /* put cursor at last row */
8178 else if (Rows > old_Rows) /* Rows got bigger */
8179 msg_row += Rows - old_Rows; /* put cursor in same place */
8180 if (msg_col >= Columns) /* Columns got smaller */
8181 msg_col = Columns - 1; /* put cursor at last column */
8182 }
8183#endif
8184
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008186 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008187
8188#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008189 /*
8190 * Do not apply autocommands more than 3 times to avoid an endless loop
8191 * in case applying autocommands always changes Rows or Columns.
8192 */
8193 if (starting == 0 && ++retry_count <= 3)
8194 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008195 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008196 /* In rare cases, autocommands may have altered Rows or Columns,
8197 * jump back to check if we need to allocate the screen again. */
8198 goto retry;
8199 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201}
8202
8203 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008204free_screenlines()
8205{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008206#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008207 int i;
8208
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008209 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008210 for (i = 0; i < Screen_mco; ++i)
8211 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008212 vim_free(ScreenLines2);
8213#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008214 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008215 vim_free(ScreenAttrs);
8216 vim_free(LineOffset);
8217 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008218#ifdef FEAT_WINDOWS
8219 vim_free(TabPageIdxs);
8220#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008221}
8222
8223 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224screenclear()
8225{
8226 check_for_delay(FALSE);
8227 screenalloc(FALSE); /* allocate screen buffers if size changed */
8228 screenclear2(); /* clear the screen */
8229}
8230
8231 static void
8232screenclear2()
8233{
8234 int i;
8235
8236 if (starting == NO_SCREEN || ScreenLines == NULL
8237#ifdef FEAT_GUI
8238 || (gui.in_use && gui.starting)
8239#endif
8240 )
8241 return;
8242
8243#ifdef FEAT_GUI
8244 if (!gui.in_use)
8245#endif
8246 screen_attr = -1; /* force setting the Normal colors */
8247 screen_stop_highlight(); /* don't want highlighting here */
8248
8249#ifdef FEAT_CLIPBOARD
8250 /* disable selection without redrawing it */
8251 clip_scroll_selection(9999);
8252#endif
8253
8254 /* blank out ScreenLines */
8255 for (i = 0; i < Rows; ++i)
8256 {
8257 lineclear(LineOffset[i], (int)Columns);
8258 LineWraps[i] = FALSE;
8259 }
8260
8261 if (can_clear(T_CL))
8262 {
8263 out_str(T_CL); /* clear the display */
8264 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008265 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 }
8267 else
8268 {
8269 /* can't clear the screen, mark all chars with invalid attributes */
8270 for (i = 0; i < Rows; ++i)
8271 lineinvalid(LineOffset[i], (int)Columns);
8272 clear_cmdline = TRUE;
8273 }
8274
8275 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8276
8277 win_rest_invalid(firstwin);
8278 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008279#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008280 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 if (must_redraw == CLEAR) /* no need to clear again */
8283 must_redraw = NOT_VALID;
8284 compute_cmdrow();
8285 msg_row = cmdline_row; /* put cursor on last line for messages */
8286 msg_col = 0;
8287 screen_start(); /* don't know where cursor is now */
8288 msg_scrolled = 0; /* can't scroll back */
8289 msg_didany = FALSE;
8290 msg_didout = FALSE;
8291}
8292
8293/*
8294 * Clear one line in ScreenLines.
8295 */
8296 static void
8297lineclear(off, width)
8298 unsigned off;
8299 int width;
8300{
8301 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8302#ifdef FEAT_MBYTE
8303 if (enc_utf8)
8304 (void)vim_memset(ScreenLinesUC + off, 0,
8305 (size_t)width * sizeof(u8char_T));
8306#endif
8307 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8308}
8309
8310/*
8311 * Mark one line in ScreenLines invalid by setting the attributes to an
8312 * invalid value.
8313 */
8314 static void
8315lineinvalid(off, width)
8316 unsigned off;
8317 int width;
8318{
8319 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8320}
8321
8322#ifdef FEAT_VERTSPLIT
8323/*
8324 * Copy part of a Screenline for vertically split window "wp".
8325 */
8326 static void
8327linecopy(to, from, wp)
8328 int to;
8329 int from;
8330 win_T *wp;
8331{
8332 unsigned off_to = LineOffset[to] + wp->w_wincol;
8333 unsigned off_from = LineOffset[from] + wp->w_wincol;
8334
8335 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8336 wp->w_width * sizeof(schar_T));
8337# ifdef FEAT_MBYTE
8338 if (enc_utf8)
8339 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008340 int i;
8341
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8343 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008344 for (i = 0; i < p_mco; ++i)
8345 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8346 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 }
8348 if (enc_dbcs == DBCS_JPNU)
8349 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8350 wp->w_width * sizeof(schar_T));
8351# endif
8352 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8353 wp->w_width * sizeof(sattr_T));
8354}
8355#endif
8356
8357/*
8358 * Return TRUE if clearing with term string "p" would work.
8359 * It can't work when the string is empty or it won't set the right background.
8360 */
8361 int
8362can_clear(p)
8363 char_u *p;
8364{
8365 return (*p != NUL && (t_colors <= 1
8366#ifdef FEAT_GUI
8367 || gui.in_use
8368#endif
8369 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8370}
8371
8372/*
8373 * Reset cursor position. Use whenever cursor was moved because of outputting
8374 * something directly to the screen (shell commands) or a terminal control
8375 * code.
8376 */
8377 void
8378screen_start()
8379{
8380 screen_cur_row = screen_cur_col = 9999;
8381}
8382
8383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 * Move the cursor to position "row","col" in the screen.
8385 * This tries to find the most efficient way to move, minimizing the number of
8386 * characters sent to the terminal.
8387 */
8388 void
8389windgoto(row, col)
8390 int row;
8391 int col;
8392{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008393 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 int i;
8395 int plan;
8396 int cost;
8397 int wouldbe_col;
8398 int noinvcurs;
8399 char_u *bs;
8400 int goto_cost;
8401 int attr;
8402
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008403#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8405
8406#define PLAN_LE 1
8407#define PLAN_CR 2
8408#define PLAN_NL 3
8409#define PLAN_WRITE 4
8410 /* Can't use ScreenLines unless initialized */
8411 if (ScreenLines == NULL)
8412 return;
8413
8414 if (col != screen_cur_col || row != screen_cur_row)
8415 {
8416 /* Check for valid position. */
8417 if (row < 0) /* window without text lines? */
8418 row = 0;
8419 if (row >= screen_Rows)
8420 row = screen_Rows - 1;
8421 if (col >= screen_Columns)
8422 col = screen_Columns - 1;
8423
8424 /* check if no cursor movement is allowed in highlight mode */
8425 if (screen_attr && *T_MS == NUL)
8426 noinvcurs = HIGHL_COST;
8427 else
8428 noinvcurs = 0;
8429 goto_cost = GOTO_COST + noinvcurs;
8430
8431 /*
8432 * Plan how to do the positioning:
8433 * 1. Use CR to move it to column 0, same row.
8434 * 2. Use T_LE to move it a few columns to the left.
8435 * 3. Use NL to move a few lines down, column 0.
8436 * 4. Move a few columns to the right with T_ND or by writing chars.
8437 *
8438 * Don't do this if the cursor went beyond the last column, the cursor
8439 * position is unknown then (some terminals wrap, some don't )
8440 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008441 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 * characters to move the cursor to the right.
8443 */
8444 if (row >= screen_cur_row && screen_cur_col < Columns)
8445 {
8446 /*
8447 * If the cursor is in the same row, bigger col, we can use CR
8448 * or T_LE.
8449 */
8450 bs = NULL; /* init for GCC */
8451 attr = screen_attr;
8452 if (row == screen_cur_row && col < screen_cur_col)
8453 {
8454 /* "le" is preferred over "bc", because "bc" is obsolete */
8455 if (*T_LE)
8456 bs = T_LE; /* "cursor left" */
8457 else
8458 bs = T_BC; /* "backspace character (old) */
8459 if (*bs)
8460 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8461 else
8462 cost = 999;
8463 if (col + 1 < cost) /* using CR is less characters */
8464 {
8465 plan = PLAN_CR;
8466 wouldbe_col = 0;
8467 cost = 1; /* CR is just one character */
8468 }
8469 else
8470 {
8471 plan = PLAN_LE;
8472 wouldbe_col = col;
8473 }
8474 if (noinvcurs) /* will stop highlighting */
8475 {
8476 cost += noinvcurs;
8477 attr = 0;
8478 }
8479 }
8480
8481 /*
8482 * If the cursor is above where we want to be, we can use CR LF.
8483 */
8484 else if (row > screen_cur_row)
8485 {
8486 plan = PLAN_NL;
8487 wouldbe_col = 0;
8488 cost = (row - screen_cur_row) * 2; /* CR LF */
8489 if (noinvcurs) /* will stop highlighting */
8490 {
8491 cost += noinvcurs;
8492 attr = 0;
8493 }
8494 }
8495
8496 /*
8497 * If the cursor is in the same row, smaller col, just use write.
8498 */
8499 else
8500 {
8501 plan = PLAN_WRITE;
8502 wouldbe_col = screen_cur_col;
8503 cost = 0;
8504 }
8505
8506 /*
8507 * Check if any characters that need to be written have the
8508 * correct attributes. Also avoid UTF-8 characters.
8509 */
8510 i = col - wouldbe_col;
8511 if (i > 0)
8512 cost += i;
8513 if (cost < goto_cost && i > 0)
8514 {
8515 /*
8516 * Check if the attributes are correct without additionally
8517 * stopping highlighting.
8518 */
8519 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8520 while (i && *p++ == attr)
8521 --i;
8522 if (i != 0)
8523 {
8524 /*
8525 * Try if it works when highlighting is stopped here.
8526 */
8527 if (*--p == 0)
8528 {
8529 cost += noinvcurs;
8530 while (i && *p++ == 0)
8531 --i;
8532 }
8533 if (i != 0)
8534 cost = 999; /* different attributes, don't do it */
8535 }
8536#ifdef FEAT_MBYTE
8537 if (enc_utf8)
8538 {
8539 /* Don't use an UTF-8 char for positioning, it's slow. */
8540 for (i = wouldbe_col; i < col; ++i)
8541 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8542 {
8543 cost = 999;
8544 break;
8545 }
8546 }
8547#endif
8548 }
8549
8550 /*
8551 * We can do it without term_windgoto()!
8552 */
8553 if (cost < goto_cost)
8554 {
8555 if (plan == PLAN_LE)
8556 {
8557 if (noinvcurs)
8558 screen_stop_highlight();
8559 while (screen_cur_col > col)
8560 {
8561 out_str(bs);
8562 --screen_cur_col;
8563 }
8564 }
8565 else if (plan == PLAN_CR)
8566 {
8567 if (noinvcurs)
8568 screen_stop_highlight();
8569 out_char('\r');
8570 screen_cur_col = 0;
8571 }
8572 else if (plan == PLAN_NL)
8573 {
8574 if (noinvcurs)
8575 screen_stop_highlight();
8576 while (screen_cur_row < row)
8577 {
8578 out_char('\n');
8579 ++screen_cur_row;
8580 }
8581 screen_cur_col = 0;
8582 }
8583
8584 i = col - screen_cur_col;
8585 if (i > 0)
8586 {
8587 /*
8588 * Use cursor-right if it's one character only. Avoids
8589 * removing a line of pixels from the last bold char, when
8590 * using the bold trick in the GUI.
8591 */
8592 if (T_ND[0] != NUL && T_ND[1] == NUL)
8593 {
8594 while (i-- > 0)
8595 out_char(*T_ND);
8596 }
8597 else
8598 {
8599 int off;
8600
8601 off = LineOffset[row] + screen_cur_col;
8602 while (i-- > 0)
8603 {
8604 if (ScreenAttrs[off] != screen_attr)
8605 screen_stop_highlight();
8606#ifdef FEAT_MBYTE
8607 out_flush_check();
8608#endif
8609 out_char(ScreenLines[off]);
8610#ifdef FEAT_MBYTE
8611 if (enc_dbcs == DBCS_JPNU
8612 && ScreenLines[off] == 0x8e)
8613 out_char(ScreenLines2[off]);
8614#endif
8615 ++off;
8616 }
8617 }
8618 }
8619 }
8620 }
8621 else
8622 cost = 999;
8623
8624 if (cost >= goto_cost)
8625 {
8626 if (noinvcurs)
8627 screen_stop_highlight();
8628 if (row == screen_cur_row && (col > screen_cur_col) &&
8629 *T_CRI != NUL)
8630 term_cursor_right(col - screen_cur_col);
8631 else
8632 term_windgoto(row, col);
8633 }
8634 screen_cur_row = row;
8635 screen_cur_col = col;
8636 }
8637}
8638
8639/*
8640 * Set cursor to its position in the current window.
8641 */
8642 void
8643setcursor()
8644{
8645 if (redrawing())
8646 {
8647 validate_cursor();
8648 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8649 W_WINCOL(curwin) + (
8650#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008651 /* With 'rightleft' set and the cursor on a double-wide
8652 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8654# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008655 (has_mbyte
8656 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8657 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658# endif
8659 1)) :
8660#endif
8661 curwin->w_wcol));
8662 }
8663}
8664
8665
8666/*
8667 * insert 'line_count' lines at 'row' in window 'wp'
8668 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8669 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8670 * scrolling.
8671 * Returns FAIL if the lines are not inserted, OK for success.
8672 */
8673 int
8674win_ins_lines(wp, row, line_count, invalid, mayclear)
8675 win_T *wp;
8676 int row;
8677 int line_count;
8678 int invalid;
8679 int mayclear;
8680{
8681 int did_delete;
8682 int nextrow;
8683 int lastrow;
8684 int retval;
8685
8686 if (invalid)
8687 wp->w_lines_valid = 0;
8688
8689 if (wp->w_height < 5)
8690 return FAIL;
8691
8692 if (line_count > wp->w_height - row)
8693 line_count = wp->w_height - row;
8694
8695 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8696 if (retval != MAYBE)
8697 return retval;
8698
8699 /*
8700 * If there is a next window or a status line, we first try to delete the
8701 * lines at the bottom to avoid messing what is after the window.
8702 * If this fails and there are following windows, don't do anything to avoid
8703 * messing up those windows, better just redraw.
8704 */
8705 did_delete = FALSE;
8706#ifdef FEAT_WINDOWS
8707 if (wp->w_next != NULL || wp->w_status_height)
8708 {
8709 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8710 line_count, (int)Rows, FALSE, NULL) == OK)
8711 did_delete = TRUE;
8712 else if (wp->w_next)
8713 return FAIL;
8714 }
8715#endif
8716 /*
8717 * if no lines deleted, blank the lines that will end up below the window
8718 */
8719 if (!did_delete)
8720 {
8721#ifdef FEAT_WINDOWS
8722 wp->w_redr_status = TRUE;
8723#endif
8724 redraw_cmdline = TRUE;
8725 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8726 lastrow = nextrow + line_count;
8727 if (lastrow > Rows)
8728 lastrow = Rows;
8729 screen_fill(nextrow - line_count, lastrow - line_count,
8730 W_WINCOL(wp), (int)W_ENDCOL(wp),
8731 ' ', ' ', 0);
8732 }
8733
8734 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8735 == FAIL)
8736 {
8737 /* deletion will have messed up other windows */
8738 if (did_delete)
8739 {
8740#ifdef FEAT_WINDOWS
8741 wp->w_redr_status = TRUE;
8742#endif
8743 win_rest_invalid(W_NEXT(wp));
8744 }
8745 return FAIL;
8746 }
8747
8748 return OK;
8749}
8750
8751/*
8752 * delete "line_count" window lines at "row" in window "wp"
8753 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8754 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8755 * scrolling
8756 * Return OK for success, FAIL if the lines are not deleted.
8757 */
8758 int
8759win_del_lines(wp, row, line_count, invalid, mayclear)
8760 win_T *wp;
8761 int row;
8762 int line_count;
8763 int invalid;
8764 int mayclear;
8765{
8766 int retval;
8767
8768 if (invalid)
8769 wp->w_lines_valid = 0;
8770
8771 if (line_count > wp->w_height - row)
8772 line_count = wp->w_height - row;
8773
8774 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8775 if (retval != MAYBE)
8776 return retval;
8777
8778 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8779 (int)Rows, FALSE, NULL) == FAIL)
8780 return FAIL;
8781
8782#ifdef FEAT_WINDOWS
8783 /*
8784 * If there are windows or status lines below, try to put them at the
8785 * correct place. If we can't do that, they have to be redrawn.
8786 */
8787 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8788 {
8789 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8790 line_count, (int)Rows, NULL) == FAIL)
8791 {
8792 wp->w_redr_status = TRUE;
8793 win_rest_invalid(wp->w_next);
8794 }
8795 }
8796 /*
8797 * If this is the last window and there is no status line, redraw the
8798 * command line later.
8799 */
8800 else
8801#endif
8802 redraw_cmdline = TRUE;
8803 return OK;
8804}
8805
8806/*
8807 * Common code for win_ins_lines() and win_del_lines().
8808 * Returns OK or FAIL when the work has been done.
8809 * Returns MAYBE when not finished yet.
8810 */
8811 static int
8812win_do_lines(wp, row, line_count, mayclear, del)
8813 win_T *wp;
8814 int row;
8815 int line_count;
8816 int mayclear;
8817 int del;
8818{
8819 int retval;
8820
8821 if (!redrawing() || line_count <= 0)
8822 return FAIL;
8823
8824 /* only a few lines left: redraw is faster */
8825 if (mayclear && Rows - line_count < 5
8826#ifdef FEAT_VERTSPLIT
8827 && wp->w_width == Columns
8828#endif
8829 )
8830 {
8831 screenclear(); /* will set wp->w_lines_valid to 0 */
8832 return FAIL;
8833 }
8834
8835 /*
8836 * Delete all remaining lines
8837 */
8838 if (row + line_count >= wp->w_height)
8839 {
8840 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8841 W_WINCOL(wp), (int)W_ENDCOL(wp),
8842 ' ', ' ', 0);
8843 return OK;
8844 }
8845
8846 /*
8847 * when scrolling, the message on the command line should be cleared,
8848 * otherwise it will stay there forever.
8849 */
8850 clear_cmdline = TRUE;
8851
8852 /*
8853 * If the terminal can set a scroll region, use that.
8854 * Always do this in a vertically split window. This will redraw from
8855 * ScreenLines[] when t_CV isn't defined. That's faster than using
8856 * win_line().
8857 * Don't use a scroll region when we are going to redraw the text, writing
8858 * a character in the lower right corner of the scroll region causes a
8859 * scroll-up in the DJGPP version.
8860 */
8861 if (scroll_region
8862#ifdef FEAT_VERTSPLIT
8863 || W_WIDTH(wp) != Columns
8864#endif
8865 )
8866 {
8867#ifdef FEAT_VERTSPLIT
8868 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8869#endif
8870 scroll_region_set(wp, row);
8871 if (del)
8872 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8873 wp->w_height - row, FALSE, wp);
8874 else
8875 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8876 wp->w_height - row, wp);
8877#ifdef FEAT_VERTSPLIT
8878 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8879#endif
8880 scroll_region_reset();
8881 return retval;
8882 }
8883
8884#ifdef FEAT_WINDOWS
8885 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8886 return FAIL;
8887#endif
8888
8889 return MAYBE;
8890}
8891
8892/*
8893 * window 'wp' and everything after it is messed up, mark it for redraw
8894 */
8895 static void
8896win_rest_invalid(wp)
8897 win_T *wp;
8898{
8899#ifdef FEAT_WINDOWS
8900 while (wp != NULL)
8901#else
8902 if (wp != NULL)
8903#endif
8904 {
8905 redraw_win_later(wp, NOT_VALID);
8906#ifdef FEAT_WINDOWS
8907 wp->w_redr_status = TRUE;
8908 wp = wp->w_next;
8909#endif
8910 }
8911 redraw_cmdline = TRUE;
8912}
8913
8914/*
8915 * The rest of the routines in this file perform screen manipulations. The
8916 * given operation is performed physically on the screen. The corresponding
8917 * change is also made to the internal screen image. In this way, the editor
8918 * anticipates the effect of editing changes on the appearance of the screen.
8919 * That way, when we call screenupdate a complete redraw isn't usually
8920 * necessary. Another advantage is that we can keep adding code to anticipate
8921 * screen changes, and in the meantime, everything still works.
8922 */
8923
8924/*
8925 * types for inserting or deleting lines
8926 */
8927#define USE_T_CAL 1
8928#define USE_T_CDL 2
8929#define USE_T_AL 3
8930#define USE_T_CE 4
8931#define USE_T_DL 5
8932#define USE_T_SR 6
8933#define USE_NL 7
8934#define USE_T_CD 8
8935#define USE_REDRAW 9
8936
8937/*
8938 * insert lines on the screen and update ScreenLines[]
8939 * 'end' is the line after the scrolled part. Normally it is Rows.
8940 * When scrolling region used 'off' is the offset from the top for the region.
8941 * 'row' and 'end' are relative to the start of the region.
8942 *
8943 * return FAIL for failure, OK for success.
8944 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008945 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946screen_ins_lines(off, row, line_count, end, wp)
8947 int off;
8948 int row;
8949 int line_count;
8950 int end;
8951 win_T *wp; /* NULL or window to use width from */
8952{
8953 int i;
8954 int j;
8955 unsigned temp;
8956 int cursor_row;
8957 int type;
8958 int result_empty;
8959 int can_ce = can_clear(T_CE);
8960
8961 /*
8962 * FAIL if
8963 * - there is no valid screen
8964 * - the screen has to be redrawn completely
8965 * - the line count is less than one
8966 * - the line count is more than 'ttyscroll'
8967 */
8968 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8969 return FAIL;
8970
8971 /*
8972 * There are seven ways to insert lines:
8973 * 0. When in a vertically split window and t_CV isn't set, redraw the
8974 * characters from ScreenLines[].
8975 * 1. Use T_CD (clear to end of display) if it exists and the result of
8976 * the insert is just empty lines
8977 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8978 * present or line_count > 1. It looks better if we do all the inserts
8979 * at once.
8980 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8981 * insert is just empty lines and T_CE is not present or line_count >
8982 * 1.
8983 * 4. Use T_AL (insert line) if it exists.
8984 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8985 * just empty lines.
8986 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8987 * just empty lines.
8988 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8989 * the 'da' flag is not set or we have clear line capability.
8990 * 8. redraw the characters from ScreenLines[].
8991 *
8992 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8993 * the scrollbar for the window. It does have insert line, use that if it
8994 * exists.
8995 */
8996 result_empty = (row + line_count >= end);
8997#ifdef FEAT_VERTSPLIT
8998 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8999 type = USE_REDRAW;
9000 else
9001#endif
9002 if (can_clear(T_CD) && result_empty)
9003 type = USE_T_CD;
9004 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9005 type = USE_T_CAL;
9006 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9007 type = USE_T_CDL;
9008 else if (*T_AL != NUL)
9009 type = USE_T_AL;
9010 else if (can_ce && result_empty)
9011 type = USE_T_CE;
9012 else if (*T_DL != NUL && result_empty)
9013 type = USE_T_DL;
9014 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9015 type = USE_T_SR;
9016 else
9017 return FAIL;
9018
9019 /*
9020 * For clearing the lines screen_del_lines() is used. This will also take
9021 * care of t_db if necessary.
9022 */
9023 if (type == USE_T_CD || type == USE_T_CDL ||
9024 type == USE_T_CE || type == USE_T_DL)
9025 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9026
9027 /*
9028 * If text is retained below the screen, first clear or delete as many
9029 * lines at the bottom of the window as are about to be inserted so that
9030 * the deleted lines won't later surface during a screen_del_lines.
9031 */
9032 if (*T_DB)
9033 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9034
9035#ifdef FEAT_CLIPBOARD
9036 /* Remove a modeless selection when inserting lines halfway the screen
9037 * or not the full width of the screen. */
9038 if (off + row > 0
9039# ifdef FEAT_VERTSPLIT
9040 || (wp != NULL && wp->w_width != Columns)
9041# endif
9042 )
9043 clip_clear_selection();
9044 else
9045 clip_scroll_selection(-line_count);
9046#endif
9047
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048#ifdef FEAT_GUI
9049 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9050 * scrolling is actually carried out. */
9051 gui_dont_update_cursor();
9052#endif
9053
9054 if (*T_CCS != NUL) /* cursor relative to region */
9055 cursor_row = row;
9056 else
9057 cursor_row = row + off;
9058
9059 /*
9060 * Shift LineOffset[] line_count down to reflect the inserted lines.
9061 * Clear the inserted lines in ScreenLines[].
9062 */
9063 row += off;
9064 end += off;
9065 for (i = 0; i < line_count; ++i)
9066 {
9067#ifdef FEAT_VERTSPLIT
9068 if (wp != NULL && wp->w_width != Columns)
9069 {
9070 /* need to copy part of a line */
9071 j = end - 1 - i;
9072 while ((j -= line_count) >= row)
9073 linecopy(j + line_count, j, wp);
9074 j += line_count;
9075 if (can_clear((char_u *)" "))
9076 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9077 else
9078 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9079 LineWraps[j] = FALSE;
9080 }
9081 else
9082#endif
9083 {
9084 j = end - 1 - i;
9085 temp = LineOffset[j];
9086 while ((j -= line_count) >= row)
9087 {
9088 LineOffset[j + line_count] = LineOffset[j];
9089 LineWraps[j + line_count] = LineWraps[j];
9090 }
9091 LineOffset[j + line_count] = temp;
9092 LineWraps[j + line_count] = FALSE;
9093 if (can_clear((char_u *)" "))
9094 lineclear(temp, (int)Columns);
9095 else
9096 lineinvalid(temp, (int)Columns);
9097 }
9098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099
9100 screen_stop_highlight();
9101 windgoto(cursor_row, 0);
9102
9103#ifdef FEAT_VERTSPLIT
9104 /* redraw the characters */
9105 if (type == USE_REDRAW)
9106 redraw_block(row, end, wp);
9107 else
9108#endif
9109 if (type == USE_T_CAL)
9110 {
9111 term_append_lines(line_count);
9112 screen_start(); /* don't know where cursor is now */
9113 }
9114 else
9115 {
9116 for (i = 0; i < line_count; i++)
9117 {
9118 if (type == USE_T_AL)
9119 {
9120 if (i && cursor_row != 0)
9121 windgoto(cursor_row, 0);
9122 out_str(T_AL);
9123 }
9124 else /* type == USE_T_SR */
9125 out_str(T_SR);
9126 screen_start(); /* don't know where cursor is now */
9127 }
9128 }
9129
9130 /*
9131 * With scroll-reverse and 'da' flag set we need to clear the lines that
9132 * have been scrolled down into the region.
9133 */
9134 if (type == USE_T_SR && *T_DA)
9135 {
9136 for (i = 0; i < line_count; ++i)
9137 {
9138 windgoto(off + i, 0);
9139 out_str(T_CE);
9140 screen_start(); /* don't know where cursor is now */
9141 }
9142 }
9143
9144#ifdef FEAT_GUI
9145 gui_can_update_cursor();
9146 if (gui.in_use)
9147 out_flush(); /* always flush after a scroll */
9148#endif
9149 return OK;
9150}
9151
9152/*
9153 * delete lines on the screen and update ScreenLines[]
9154 * 'end' is the line after the scrolled part. Normally it is Rows.
9155 * When scrolling region used 'off' is the offset from the top for the region.
9156 * 'row' and 'end' are relative to the start of the region.
9157 *
9158 * Return OK for success, FAIL if the lines are not deleted.
9159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 int
9161screen_del_lines(off, row, line_count, end, force, wp)
9162 int off;
9163 int row;
9164 int line_count;
9165 int end;
9166 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009167 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169 int j;
9170 int i;
9171 unsigned temp;
9172 int cursor_row;
9173 int cursor_end;
9174 int result_empty; /* result is empty until end of region */
9175 int can_delete; /* deleting line codes can be used */
9176 int type;
9177
9178 /*
9179 * FAIL if
9180 * - there is no valid screen
9181 * - the screen has to be redrawn completely
9182 * - the line count is less than one
9183 * - the line count is more than 'ttyscroll'
9184 */
9185 if (!screen_valid(TRUE) || line_count <= 0 ||
9186 (!force && line_count > p_ttyscroll))
9187 return FAIL;
9188
9189 /*
9190 * Check if the rest of the current region will become empty.
9191 */
9192 result_empty = row + line_count >= end;
9193
9194 /*
9195 * We can delete lines only when 'db' flag not set or when 'ce' option
9196 * available.
9197 */
9198 can_delete = (*T_DB == NUL || can_clear(T_CE));
9199
9200 /*
9201 * There are six ways to delete lines:
9202 * 0. When in a vertically split window and t_CV isn't set, redraw the
9203 * characters from ScreenLines[].
9204 * 1. Use T_CD if it exists and the result is empty.
9205 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9206 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9207 * none of the other ways work.
9208 * 4. Use T_CE (erase line) if the result is empty.
9209 * 5. Use T_DL (delete line) if it exists.
9210 * 6. redraw the characters from ScreenLines[].
9211 */
9212#ifdef FEAT_VERTSPLIT
9213 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9214 type = USE_REDRAW;
9215 else
9216#endif
9217 if (can_clear(T_CD) && result_empty)
9218 type = USE_T_CD;
9219#if defined(__BEOS__) && defined(BEOS_DR8)
9220 /*
9221 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9222 * its internal termcap... this works okay for tests which test *T_DB !=
9223 * NUL. It has the disadvantage that the user cannot use any :set t_*
9224 * command to get T_DB (back) to empty_option, only :set term=... will do
9225 * the trick...
9226 * Anyway, this hack will hopefully go away with the next OS release.
9227 * (Olaf Seibert)
9228 */
9229 else if (row == 0 && T_DB == empty_option
9230 && (line_count == 1 || *T_CDL == NUL))
9231#else
9232 else if (row == 0 && (
9233#ifndef AMIGA
9234 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9235 * up, so use delete-line command */
9236 line_count == 1 ||
9237#endif
9238 *T_CDL == NUL))
9239#endif
9240 type = USE_NL;
9241 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9242 type = USE_T_CDL;
9243 else if (can_clear(T_CE) && result_empty
9244#ifdef FEAT_VERTSPLIT
9245 && (wp == NULL || wp->w_width == Columns)
9246#endif
9247 )
9248 type = USE_T_CE;
9249 else if (*T_DL != NUL && can_delete)
9250 type = USE_T_DL;
9251 else if (*T_CDL != NUL && can_delete)
9252 type = USE_T_CDL;
9253 else
9254 return FAIL;
9255
9256#ifdef FEAT_CLIPBOARD
9257 /* Remove a modeless selection when deleting lines halfway the screen or
9258 * not the full width of the screen. */
9259 if (off + row > 0
9260# ifdef FEAT_VERTSPLIT
9261 || (wp != NULL && wp->w_width != Columns)
9262# endif
9263 )
9264 clip_clear_selection();
9265 else
9266 clip_scroll_selection(line_count);
9267#endif
9268
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269#ifdef FEAT_GUI
9270 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9271 * scrolling is actually carried out. */
9272 gui_dont_update_cursor();
9273#endif
9274
9275 if (*T_CCS != NUL) /* cursor relative to region */
9276 {
9277 cursor_row = row;
9278 cursor_end = end;
9279 }
9280 else
9281 {
9282 cursor_row = row + off;
9283 cursor_end = end + off;
9284 }
9285
9286 /*
9287 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9288 * Clear the inserted lines in ScreenLines[].
9289 */
9290 row += off;
9291 end += off;
9292 for (i = 0; i < line_count; ++i)
9293 {
9294#ifdef FEAT_VERTSPLIT
9295 if (wp != NULL && wp->w_width != Columns)
9296 {
9297 /* need to copy part of a line */
9298 j = row + i;
9299 while ((j += line_count) <= end - 1)
9300 linecopy(j - line_count, j, wp);
9301 j -= line_count;
9302 if (can_clear((char_u *)" "))
9303 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9304 else
9305 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9306 LineWraps[j] = FALSE;
9307 }
9308 else
9309#endif
9310 {
9311 /* whole width, moving the line pointers is faster */
9312 j = row + i;
9313 temp = LineOffset[j];
9314 while ((j += line_count) <= end - 1)
9315 {
9316 LineOffset[j - line_count] = LineOffset[j];
9317 LineWraps[j - line_count] = LineWraps[j];
9318 }
9319 LineOffset[j - line_count] = temp;
9320 LineWraps[j - line_count] = FALSE;
9321 if (can_clear((char_u *)" "))
9322 lineclear(temp, (int)Columns);
9323 else
9324 lineinvalid(temp, (int)Columns);
9325 }
9326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327
9328 screen_stop_highlight();
9329
9330#ifdef FEAT_VERTSPLIT
9331 /* redraw the characters */
9332 if (type == USE_REDRAW)
9333 redraw_block(row, end, wp);
9334 else
9335#endif
9336 if (type == USE_T_CD) /* delete the lines */
9337 {
9338 windgoto(cursor_row, 0);
9339 out_str(T_CD);
9340 screen_start(); /* don't know where cursor is now */
9341 }
9342 else if (type == USE_T_CDL)
9343 {
9344 windgoto(cursor_row, 0);
9345 term_delete_lines(line_count);
9346 screen_start(); /* don't know where cursor is now */
9347 }
9348 /*
9349 * Deleting lines at top of the screen or scroll region: Just scroll
9350 * the whole screen (scroll region) up by outputting newlines on the
9351 * last line.
9352 */
9353 else if (type == USE_NL)
9354 {
9355 windgoto(cursor_end - 1, 0);
9356 for (i = line_count; --i >= 0; )
9357 out_char('\n'); /* cursor will remain on same line */
9358 }
9359 else
9360 {
9361 for (i = line_count; --i >= 0; )
9362 {
9363 if (type == USE_T_DL)
9364 {
9365 windgoto(cursor_row, 0);
9366 out_str(T_DL); /* delete a line */
9367 }
9368 else /* type == USE_T_CE */
9369 {
9370 windgoto(cursor_row + i, 0);
9371 out_str(T_CE); /* erase a line */
9372 }
9373 screen_start(); /* don't know where cursor is now */
9374 }
9375 }
9376
9377 /*
9378 * If the 'db' flag is set, we need to clear the lines that have been
9379 * scrolled up at the bottom of the region.
9380 */
9381 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9382 {
9383 for (i = line_count; i > 0; --i)
9384 {
9385 windgoto(cursor_end - i, 0);
9386 out_str(T_CE); /* erase a line */
9387 screen_start(); /* don't know where cursor is now */
9388 }
9389 }
9390
9391#ifdef FEAT_GUI
9392 gui_can_update_cursor();
9393 if (gui.in_use)
9394 out_flush(); /* always flush after a scroll */
9395#endif
9396
9397 return OK;
9398}
9399
9400/*
9401 * show the current mode and ruler
9402 *
9403 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9404 * If clear_cmdline is FALSE there may be a message there that needs to be
9405 * cleared only if a mode is shown.
9406 * Return the length of the message (0 if no message).
9407 */
9408 int
9409showmode()
9410{
9411 int need_clear;
9412 int length = 0;
9413 int do_mode;
9414 int attr;
9415 int nwr_save;
9416#ifdef FEAT_INS_EXPAND
9417 int sub_attr;
9418#endif
9419
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009420 do_mode = ((p_smd && msg_silent == 0)
9421 && ((State & INSERT)
9422 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423#ifdef FEAT_VISUAL
9424 || VIsual_active
9425#endif
9426 ));
9427 if (do_mode || Recording)
9428 {
9429 /*
9430 * Don't show mode right now, when not redrawing or inside a mapping.
9431 * Call char_avail() only when we are going to show something, because
9432 * it takes a bit of time.
9433 */
9434 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9435 {
9436 redraw_cmdline = TRUE; /* show mode later */
9437 return 0;
9438 }
9439
9440 nwr_save = need_wait_return;
9441
9442 /* wait a bit before overwriting an important message */
9443 check_for_delay(FALSE);
9444
9445 /* if the cmdline is more than one line high, erase top lines */
9446 need_clear = clear_cmdline;
9447 if (clear_cmdline && cmdline_row < Rows - 1)
9448 msg_clr_cmdline(); /* will reset clear_cmdline */
9449
9450 /* Position on the last line in the window, column 0 */
9451 msg_pos_mode();
9452 cursor_off();
9453 attr = hl_attr(HLF_CM); /* Highlight mode */
9454 if (do_mode)
9455 {
9456 MSG_PUTS_ATTR("--", attr);
9457#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009458 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009459# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009460 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009461# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009462 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009463# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009464 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009465# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 MSG_PUTS_ATTR(" IM", attr);
9467# else
9468 MSG_PUTS_ATTR(" XIM", attr);
9469# endif
9470#endif
9471#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9472 if (gui.in_use)
9473 {
9474 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009475 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 }
9477#endif
9478#ifdef FEAT_INS_EXPAND
9479 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9480 {
9481 /* These messages can get long, avoid a wrap in a narrow
9482 * window. Prefer showing edit_submode_extra. */
9483 length = (Rows - msg_row) * Columns - 3;
9484 if (edit_submode_extra != NULL)
9485 length -= vim_strsize(edit_submode_extra);
9486 if (length > 0)
9487 {
9488 if (edit_submode_pre != NULL)
9489 length -= vim_strsize(edit_submode_pre);
9490 if (length - vim_strsize(edit_submode) > 0)
9491 {
9492 if (edit_submode_pre != NULL)
9493 msg_puts_attr(edit_submode_pre, attr);
9494 msg_puts_attr(edit_submode, attr);
9495 }
9496 if (edit_submode_extra != NULL)
9497 {
9498 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9499 if ((int)edit_submode_highl < (int)HLF_COUNT)
9500 sub_attr = hl_attr(edit_submode_highl);
9501 else
9502 sub_attr = attr;
9503 msg_puts_attr(edit_submode_extra, sub_attr);
9504 }
9505 }
9506 length = 0;
9507 }
9508 else
9509#endif
9510 {
9511#ifdef FEAT_VREPLACE
9512 if (State & VREPLACE_FLAG)
9513 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9514 else
9515#endif
9516 if (State & REPLACE_FLAG)
9517 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9518 else if (State & INSERT)
9519 {
9520#ifdef FEAT_RIGHTLEFT
9521 if (p_ri)
9522 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9523#endif
9524 MSG_PUTS_ATTR(_(" INSERT"), attr);
9525 }
9526 else if (restart_edit == 'I')
9527 MSG_PUTS_ATTR(_(" (insert)"), attr);
9528 else if (restart_edit == 'R')
9529 MSG_PUTS_ATTR(_(" (replace)"), attr);
9530 else if (restart_edit == 'V')
9531 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9532#ifdef FEAT_RIGHTLEFT
9533 if (p_hkmap)
9534 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9535# ifdef FEAT_FKMAP
9536 if (p_fkmap)
9537 MSG_PUTS_ATTR(farsi_text_5, attr);
9538# endif
9539#endif
9540#ifdef FEAT_KEYMAP
9541 if (State & LANGMAP)
9542 {
9543# ifdef FEAT_ARABIC
9544 if (curwin->w_p_arab)
9545 MSG_PUTS_ATTR(_(" Arabic"), attr);
9546 else
9547# endif
9548 MSG_PUTS_ATTR(_(" (lang)"), attr);
9549 }
9550#endif
9551 if ((State & INSERT) && p_paste)
9552 MSG_PUTS_ATTR(_(" (paste)"), attr);
9553
9554#ifdef FEAT_VISUAL
9555 if (VIsual_active)
9556 {
9557 char *p;
9558
9559 /* Don't concatenate separate words to avoid translation
9560 * problems. */
9561 switch ((VIsual_select ? 4 : 0)
9562 + (VIsual_mode == Ctrl_V) * 2
9563 + (VIsual_mode == 'V'))
9564 {
9565 case 0: p = N_(" VISUAL"); break;
9566 case 1: p = N_(" VISUAL LINE"); break;
9567 case 2: p = N_(" VISUAL BLOCK"); break;
9568 case 4: p = N_(" SELECT"); break;
9569 case 5: p = N_(" SELECT LINE"); break;
9570 default: p = N_(" SELECT BLOCK"); break;
9571 }
9572 MSG_PUTS_ATTR(_(p), attr);
9573 }
9574#endif
9575 MSG_PUTS_ATTR(" --", attr);
9576 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009577
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 need_clear = TRUE;
9579 }
9580 if (Recording
9581#ifdef FEAT_INS_EXPAND
9582 && edit_submode == NULL /* otherwise it gets too long */
9583#endif
9584 )
9585 {
9586 MSG_PUTS_ATTR(_("recording"), attr);
9587 need_clear = TRUE;
9588 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009589
9590 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 if (need_clear || clear_cmdline)
9592 msg_clr_eos();
9593 msg_didout = FALSE; /* overwrite this message */
9594 length = msg_col;
9595 msg_col = 0;
9596 need_wait_return = nwr_save; /* never ask for hit-return for this */
9597 }
9598 else if (clear_cmdline && msg_silent == 0)
9599 /* Clear the whole command line. Will reset "clear_cmdline". */
9600 msg_clr_cmdline();
9601
9602#ifdef FEAT_CMDL_INFO
9603# ifdef FEAT_VISUAL
9604 /* In Visual mode the size of the selected area must be redrawn. */
9605 if (VIsual_active)
9606 clear_showcmd();
9607# endif
9608
9609 /* If the last window has no status line, the ruler is after the mode
9610 * message and must be redrawn */
9611 if (redrawing()
9612# ifdef FEAT_WINDOWS
9613 && lastwin->w_status_height == 0
9614# endif
9615 )
9616 win_redr_ruler(lastwin, TRUE);
9617#endif
9618 redraw_cmdline = FALSE;
9619 clear_cmdline = FALSE;
9620
9621 return length;
9622}
9623
9624/*
9625 * Position for a mode message.
9626 */
9627 static void
9628msg_pos_mode()
9629{
9630 msg_col = 0;
9631 msg_row = Rows - 1;
9632}
9633
9634/*
9635 * Delete mode message. Used when ESC is typed which is expected to end
9636 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009637 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 */
9639 void
9640unshowmode(force)
9641 int force;
9642{
9643 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009644 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 */
9646 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9647 redraw_cmdline = TRUE; /* delete mode later */
9648 else
9649 {
9650 msg_pos_mode();
9651 if (Recording)
9652 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9653 msg_clr_eos();
9654 }
9655}
9656
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009657#if defined(FEAT_WINDOWS)
9658/*
9659 * Draw the tab pages line at the top of the Vim window.
9660 */
9661 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009662draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009663{
9664 int tabcount = 0;
9665 tabpage_T *tp;
9666 int tabwidth;
9667 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009668 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009669 int attr;
9670 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009671 win_T *cwp;
9672 int wincount;
9673 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009674 int c;
9675 int len;
9676 int attr_sel = hl_attr(HLF_TPS);
9677 int attr_nosel = hl_attr(HLF_TP);
9678 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009679 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009680 int room;
9681 int use_sep_chars = (t_colors < 8
9682#ifdef FEAT_GUI
9683 && !gui.in_use
9684#endif
9685 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009686
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009687 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009688
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009689#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009690 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009691 if (gui_use_tabline())
9692 {
9693 gui_update_tabline();
9694 return;
9695 }
9696#endif
9697
9698 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009699 return;
9700
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009701#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009702
9703 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9704 for (scol = 0; scol < Columns; ++scol)
9705 TabPageIdxs[scol] = 0;
9706
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009707 /* Use the 'tabline' option if it's set. */
9708 if (*p_tal != NUL)
9709 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009710 int save_called_emsg = called_emsg;
9711
9712 /* Check for an error. If there is one we would loop in redrawing the
9713 * screen. Avoid that by making 'tabline' empty. */
9714 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009715 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009716 if (called_emsg)
9717 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009718 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009719 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009720 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009721 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009722#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009723 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009724 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9725 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009726
Bram Moolenaar238a5642006-02-21 22:12:05 +00009727 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9728 if (tabwidth < 6)
9729 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009730
Bram Moolenaar238a5642006-02-21 22:12:05 +00009731 attr = attr_nosel;
9732 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009733 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009734 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9735 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009736 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009737 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009738
Bram Moolenaar238a5642006-02-21 22:12:05 +00009739 if (tp->tp_topframe == topframe)
9740 attr = attr_sel;
9741 if (use_sep_chars && col > 0)
9742 screen_putchar('|', 0, col++, attr);
9743
9744 if (tp->tp_topframe != topframe)
9745 attr = attr_nosel;
9746
9747 screen_putchar(' ', 0, col++, attr);
9748
9749 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009750 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009751 cwp = curwin;
9752 wp = firstwin;
9753 }
9754 else
9755 {
9756 cwp = tp->tp_curwin;
9757 wp = tp->tp_firstwin;
9758 }
9759
9760 modified = FALSE;
9761 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9762 if (bufIsChanged(wp->w_buffer))
9763 modified = TRUE;
9764 if (modified || wincount > 1)
9765 {
9766 if (wincount > 1)
9767 {
9768 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009769 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009770 if (col + len >= Columns - 3)
9771 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009772 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009773#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009774 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009775#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009776 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009777#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009778 );
9779 col += len;
9780 }
9781 if (modified)
9782 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9783 screen_putchar(' ', 0, col++, attr);
9784 }
9785
9786 room = scol - col + tabwidth - 1;
9787 if (room > 0)
9788 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009789 /* Get buffer name in NameBuff[] */
9790 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009791 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009792 len = vim_strsize(NameBuff);
9793 p = NameBuff;
9794#ifdef FEAT_MBYTE
9795 if (has_mbyte)
9796 while (len > room)
9797 {
9798 len -= ptr2cells(p);
9799 mb_ptr_adv(p);
9800 }
9801 else
9802#endif
9803 if (len > room)
9804 {
9805 p += len - room;
9806 len = room;
9807 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009808 if (len > Columns - col - 1)
9809 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009810
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009811 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009812 col += len;
9813 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009814 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009815
9816 /* Store the tab page number in TabPageIdxs[], so that
9817 * jump_to_mouse() knows where each one is. */
9818 ++tabcount;
9819 while (scol < col)
9820 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009821 }
9822
Bram Moolenaar238a5642006-02-21 22:12:05 +00009823 if (use_sep_chars)
9824 c = '_';
9825 else
9826 c = ' ';
9827 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009828
9829 /* Put an "X" for closing the current tab if there are several. */
9830 if (first_tabpage->tp_next != NULL)
9831 {
9832 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9833 TabPageIdxs[Columns - 1] = -999;
9834 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009835 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009836
9837 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9838 * set. */
9839 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009840}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009841
9842/*
9843 * Get buffer name for "buf" into NameBuff[].
9844 * Takes care of special buffer names and translates special characters.
9845 */
9846 void
9847get_trans_bufname(buf)
9848 buf_T *buf;
9849{
9850 if (buf_spname(buf) != NULL)
9851 STRCPY(NameBuff, buf_spname(buf));
9852 else
9853 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9854 trans_characters(NameBuff, MAXPATHL);
9855}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009856#endif
9857
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9859/*
9860 * Get the character to use in a status line. Get its attributes in "*attr".
9861 */
9862 static int
9863fillchar_status(attr, is_curwin)
9864 int *attr;
9865 int is_curwin;
9866{
9867 int fill;
9868 if (is_curwin)
9869 {
9870 *attr = hl_attr(HLF_S);
9871 fill = fill_stl;
9872 }
9873 else
9874 {
9875 *attr = hl_attr(HLF_SNC);
9876 fill = fill_stlnc;
9877 }
9878 /* Use fill when there is highlighting, and highlighting of current
9879 * window differs, or the fillchars differ, or this is not the
9880 * current window */
9881 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9882 || !is_curwin || firstwin == lastwin)
9883 || (fill_stl != fill_stlnc)))
9884 return fill;
9885 if (is_curwin)
9886 return '^';
9887 return '=';
9888}
9889#endif
9890
9891#ifdef FEAT_VERTSPLIT
9892/*
9893 * Get the character to use in a separator between vertically split windows.
9894 * Get its attributes in "*attr".
9895 */
9896 static int
9897fillchar_vsep(attr)
9898 int *attr;
9899{
9900 *attr = hl_attr(HLF_C);
9901 if (*attr == 0 && fill_vert == ' ')
9902 return '|';
9903 else
9904 return fill_vert;
9905}
9906#endif
9907
9908/*
9909 * Return TRUE if redrawing should currently be done.
9910 */
9911 int
9912redrawing()
9913{
9914 return (!RedrawingDisabled
9915 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9916}
9917
9918/*
9919 * Return TRUE if printing messages should currently be done.
9920 */
9921 int
9922messaging()
9923{
9924 return (!(p_lz && char_avail() && !KeyTyped));
9925}
9926
9927/*
9928 * Show current status info in ruler and various other places
9929 * If always is FALSE, only show ruler if position has changed.
9930 */
9931 void
9932showruler(always)
9933 int always;
9934{
9935 if (!always && !redrawing())
9936 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009937#ifdef FEAT_INS_EXPAND
9938 if (pum_visible())
9939 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009940# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009941 /* Don't redraw right now, do it later. */
9942 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009943# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009944 return;
9945 }
9946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009948 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009949 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009950 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 else
9953#endif
9954#ifdef FEAT_CMDL_INFO
9955 win_redr_ruler(curwin, always);
9956#endif
9957
9958#ifdef FEAT_TITLE
9959 if (need_maketitle
9960# ifdef FEAT_STL_OPT
9961 || (p_icon && (stl_syntax & STL_IN_ICON))
9962 || (p_title && (stl_syntax & STL_IN_TITLE))
9963# endif
9964 )
9965 maketitle();
9966#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009967#ifdef FEAT_WINDOWS
9968 /* Redraw the tab pages line if needed. */
9969 if (redraw_tabline)
9970 draw_tabline();
9971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972}
9973
9974#ifdef FEAT_CMDL_INFO
9975 static void
9976win_redr_ruler(wp, always)
9977 win_T *wp;
9978 int always;
9979{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009980#define RULER_BUF_LEN 70
9981 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 int row;
9983 int fillchar;
9984 int attr;
9985 int empty_line = FALSE;
9986 colnr_T virtcol;
9987 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009988 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 int o;
9990#ifdef FEAT_VERTSPLIT
9991 int this_ru_col;
9992 int off = 0;
9993 int width = Columns;
9994# define WITH_OFF(x) x
9995# define WITH_WIDTH(x) x
9996#else
9997# define WITH_OFF(x) 0
9998# define WITH_WIDTH(x) Columns
9999# define this_ru_col ru_col
10000#endif
10001
10002 /* If 'ruler' off or redrawing disabled, don't do anything */
10003 if (!p_ru)
10004 return;
10005
10006 /*
10007 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10008 * after deleting lines, before cursor.lnum is corrected.
10009 */
10010 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10011 return;
10012
10013#ifdef FEAT_INS_EXPAND
10014 /* Don't draw the ruler while doing insert-completion, it might overwrite
10015 * the (long) mode message. */
10016# ifdef FEAT_WINDOWS
10017 if (wp == lastwin && lastwin->w_status_height == 0)
10018# endif
10019 if (edit_submode != NULL)
10020 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010021 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10022 if (pum_visible())
10023 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024#endif
10025
10026#ifdef FEAT_STL_OPT
10027 if (*p_ruf)
10028 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010029 int save_called_emsg = called_emsg;
10030
10031 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010033 if (called_emsg)
10034 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010035 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010036 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 return;
10038 }
10039#endif
10040
10041 /*
10042 * Check if not in Insert mode and the line is empty (will show "0-1").
10043 */
10044 if (!(State & INSERT)
10045 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10046 empty_line = TRUE;
10047
10048 /*
10049 * Only draw the ruler when something changed.
10050 */
10051 validate_virtcol_win(wp);
10052 if ( redraw_cmdline
10053 || always
10054 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10055 || wp->w_cursor.col != wp->w_ru_cursor.col
10056 || wp->w_virtcol != wp->w_ru_virtcol
10057#ifdef FEAT_VIRTUALEDIT
10058 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10059#endif
10060 || wp->w_topline != wp->w_ru_topline
10061 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10062#ifdef FEAT_DIFF
10063 || wp->w_topfill != wp->w_ru_topfill
10064#endif
10065 || empty_line != wp->w_ru_empty)
10066 {
10067 cursor_off();
10068#ifdef FEAT_WINDOWS
10069 if (wp->w_status_height)
10070 {
10071 row = W_WINROW(wp) + wp->w_height;
10072 fillchar = fillchar_status(&attr, wp == curwin);
10073# ifdef FEAT_VERTSPLIT
10074 off = W_WINCOL(wp);
10075 width = W_WIDTH(wp);
10076# endif
10077 }
10078 else
10079#endif
10080 {
10081 row = Rows - 1;
10082 fillchar = ' ';
10083 attr = 0;
10084#ifdef FEAT_VERTSPLIT
10085 width = Columns;
10086 off = 0;
10087#endif
10088 }
10089
10090 /* In list mode virtcol needs to be recomputed */
10091 virtcol = wp->w_virtcol;
10092 if (wp->w_p_list && lcs_tab1 == NUL)
10093 {
10094 wp->w_p_list = FALSE;
10095 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10096 wp->w_p_list = TRUE;
10097 }
10098
10099 /*
10100 * Some sprintfs return the length, some return a pointer.
10101 * To avoid portability problems we use strlen() here.
10102 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010103 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10105 ? 0L
10106 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010107 len = STRLEN(buffer);
10108 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10110 (int)virtcol + 1);
10111
10112 /*
10113 * Add a "50%" if there is room for it.
10114 * On the last line, don't print in the last column (scrolls the
10115 * screen up on some terminals).
10116 */
10117 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010118 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 o = i + vim_strsize(buffer + i + 1);
10120#ifdef FEAT_WINDOWS
10121 if (wp->w_status_height == 0) /* can't use last char of screen */
10122#endif
10123 ++o;
10124#ifdef FEAT_VERTSPLIT
10125 this_ru_col = ru_col - (Columns - width);
10126 if (this_ru_col < 0)
10127 this_ru_col = 0;
10128#endif
10129 /* Never use more than half the window/screen width, leave the other
10130 * half for the filename. */
10131 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10132 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10133 if (this_ru_col + o < WITH_WIDTH(width))
10134 {
10135 while (this_ru_col + o < WITH_WIDTH(width))
10136 {
10137#ifdef FEAT_MBYTE
10138 if (has_mbyte)
10139 i += (*mb_char2bytes)(fillchar, buffer + i);
10140 else
10141#endif
10142 buffer[i++] = fillchar;
10143 ++o;
10144 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010145 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 }
10147 /* Truncate at window boundary. */
10148#ifdef FEAT_MBYTE
10149 if (has_mbyte)
10150 {
10151 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010152 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 {
10154 o += (*mb_ptr2cells)(buffer + i);
10155 if (this_ru_col + o > WITH_WIDTH(width))
10156 {
10157 buffer[i] = NUL;
10158 break;
10159 }
10160 }
10161 }
10162 else
10163#endif
10164 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10165 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10166
10167 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10168 i = redraw_cmdline;
10169 screen_fill(row, row + 1,
10170 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10171 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10172 fillchar, fillchar, attr);
10173 /* don't redraw the cmdline because of showing the ruler */
10174 redraw_cmdline = i;
10175 wp->w_ru_cursor = wp->w_cursor;
10176 wp->w_ru_virtcol = wp->w_virtcol;
10177 wp->w_ru_empty = empty_line;
10178 wp->w_ru_topline = wp->w_topline;
10179 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10180#ifdef FEAT_DIFF
10181 wp->w_ru_topfill = wp->w_topfill;
10182#endif
10183 }
10184}
10185#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010186
10187#if defined(FEAT_LINEBREAK) || defined(PROTO)
10188/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010189 * Return the width of the 'number' and 'relativenumber' column.
10190 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010191 * Otherwise it depends on 'numberwidth' and the line count.
10192 */
10193 int
10194number_width(wp)
10195 win_T *wp;
10196{
10197 int n;
10198 linenr_T lnum;
10199
Bram Moolenaar64486672010-05-16 15:46:46 +020010200 if (wp->w_p_nu)
10201 /* 'number' */
10202 lnum = wp->w_buffer->b_ml.ml_line_count;
10203 else
10204 /* 'relativenumber' */
10205 lnum = wp->w_height;
10206
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010207 if (lnum == wp->w_nrwidth_line_count)
10208 return wp->w_nrwidth_width;
10209 wp->w_nrwidth_line_count = lnum;
10210
10211 n = 0;
10212 do
10213 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010214 lnum /= 10;
10215 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010216 } while (lnum > 0);
10217
10218 /* 'numberwidth' gives the minimal width plus one */
10219 if (n < wp->w_p_nuw - 1)
10220 n = wp->w_p_nuw - 1;
10221
10222 wp->w_nrwidth_width = n;
10223 return n;
10224}
10225#endif