blob: b7f4f408edfa9c728eeaf40a25d7355c48cd2eac [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';
608 else
609 return FALSE;
610 return vim_strchr(wp->w_p_cocu, c) != NULL;
611}
612
613/*
614 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
615 */
616 void
617conceal_check_cursur_line_redraw()
618{
619 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
620 {
621 need_cursor_line_redraw = TRUE;
622 /* Need to recompute cursor column, e.g., when starting Visual mode
623 * without concealing. */
624 curs_columns(TRUE);
625 }
626}
627
Bram Moolenaar860cae12010-06-05 23:22:07 +0200628 void
629update_single_line(wp, lnum)
630 win_T *wp;
631 linenr_T lnum;
632{
633 int row;
634 int j;
635
636 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200637 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200638 {
639# ifdef FEAT_GUI
640 /* Remove the cursor before starting to do anything, because scrolling
641 * may make it difficult to redraw the text under it. */
642 if (gui.in_use)
643 gui_undraw_cursor();
644# endif
645 row = 0;
646 for (j = 0; j < wp->w_lines_valid; ++j)
647 {
648 if (lnum == wp->w_lines[j].wl_lnum)
649 {
650 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200651# ifdef FEAT_SEARCH_EXTRA
652 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200653 start_search_hl();
654 prepare_search_hl(wp, lnum);
655# endif
656 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
657# if defined(FEAT_SEARCH_EXTRA)
658 end_search_hl();
659# endif
660 break;
661 }
662 row += wp->w_lines[j].wl_size;
663 }
664# ifdef FEAT_GUI
665 /* Redraw the cursor */
666 if (gui.in_use)
667 {
668 out_flush(); /* required before updating the cursor */
669 gui_update_cursor(FALSE, FALSE);
670 }
671# endif
672 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200673 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200674}
675#endif
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
678static void update_prepare __ARGS((void));
679static void update_finish __ARGS((void));
680
681/*
682 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000683 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 */
685 static void
686update_prepare()
687{
688 cursor_off();
689 updating_screen = TRUE;
690#ifdef FEAT_GUI
691 /* Remove the cursor before starting to do anything, because scrolling may
692 * make it difficult to redraw the text under it. */
693 if (gui.in_use)
694 gui_undraw_cursor();
695#endif
696#ifdef FEAT_SEARCH_EXTRA
697 start_search_hl();
698#endif
699}
700
701/*
702 * Finish updating one or more windows.
703 */
704 static void
705update_finish()
706{
707 if (redraw_cmdline)
708 showmode();
709
710# ifdef FEAT_SEARCH_EXTRA
711 end_search_hl();
712# endif
713
714 updating_screen = FALSE;
715
716# ifdef FEAT_GUI
717 gui_may_resize_shell();
718
719 /* Redraw the cursor and update the scrollbars when all screen updating is
720 * done. */
721 if (gui.in_use)
722 {
723 out_flush(); /* required before updating the cursor */
724 gui_update_cursor(FALSE, FALSE);
725 gui_update_scrollbars(FALSE);
726 }
727# endif
728}
729#endif
730
731#if defined(FEAT_SIGNS) || defined(PROTO)
732 void
733update_debug_sign(buf, lnum)
734 buf_T *buf;
735 linenr_T lnum;
736{
737 win_T *wp;
738 int doit = FALSE;
739
740# ifdef FEAT_FOLDING
741 win_foldinfo.fi_level = 0;
742# endif
743
744 /* update/delete a specific mark */
745 FOR_ALL_WINDOWS(wp)
746 {
747 if (buf != NULL && lnum > 0)
748 {
749 if (wp->w_buffer == buf && lnum >= wp->w_topline
750 && lnum < wp->w_botline)
751 {
752 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
753 wp->w_redraw_top = lnum;
754 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
755 wp->w_redraw_bot = lnum;
756 redraw_win_later(wp, VALID);
757 }
758 }
759 else
760 redraw_win_later(wp, VALID);
761 if (wp->w_redr_type != 0)
762 doit = TRUE;
763 }
764
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000765 /* Return when there is nothing to do or screen updating already
766 * happening. */
767 if (!doit || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 return;
769
770 /* update all windows that need updating */
771 update_prepare();
772
773# ifdef FEAT_WINDOWS
774 for (wp = firstwin; wp; wp = wp->w_next)
775 {
776 if (wp->w_redr_type != 0)
777 win_update(wp);
778 if (wp->w_redr_status)
779 win_redr_status(wp);
780 }
781# else
782 if (curwin->w_redr_type != 0)
783 win_update(curwin);
784# endif
785
786 update_finish();
787}
788#endif
789
790
791#if defined(FEAT_GUI) || defined(PROTO)
792/*
793 * Update a single window, its status line and maybe the command line msg.
794 * Used for the GUI scrollbar.
795 */
796 void
797updateWindow(wp)
798 win_T *wp;
799{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000800 /* return if already busy updating */
801 if (updating_screen)
802 return;
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 update_prepare();
805
806#ifdef FEAT_CLIPBOARD
807 /* When Visual area changed, may have to update selection. */
808 if (clip_star.available && clip_isautosel())
809 clip_update_selection();
810#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000815 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000816 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000817 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (wp->w_redr_status
820# ifdef FEAT_CMDL_INFO
821 || p_ru
822# endif
823# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000824 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825# endif
826 )
827 win_redr_status(wp);
828#endif
829
830 update_finish();
831}
832#endif
833
834/*
835 * Update a single window.
836 *
837 * This may cause the windows below it also to be redrawn (when clearing the
838 * screen or scrolling lines).
839 *
840 * How the window is redrawn depends on wp->w_redr_type. Each type also
841 * implies the one below it.
842 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000843 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
845 * INVERTED redraw the changed part of the Visual area
846 * INVERTED_ALL redraw the whole Visual area
847 * VALID 1. scroll up/down to adjust for a changed w_topline
848 * 2. update lines at the top when scrolled down
849 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000850 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 * b_mod_top and b_mod_bot.
852 * - if wp->w_redraw_top non-zero, redraw lines between
853 * wp->w_redraw_top and wp->w_redr_bot.
854 * - continue redrawing when syntax status is invalid.
855 * 4. if scrolled up, update lines at the bottom.
856 * This results in three areas that may need updating:
857 * top: from first row to top_end (when scrolled down)
858 * mid: from mid_start to mid_end (update inversion or changed text)
859 * bot: from bot_start to last row (when scrolled up)
860 */
861 static void
862win_update(wp)
863 win_T *wp;
864{
865 buf_T *buf = wp->w_buffer;
866 int type;
867 int top_end = 0; /* Below last row of the top area that needs
868 updating. 0 when no top area updating. */
869 int mid_start = 999;/* first row of the mid area that needs
870 updating. 999 when no mid area updating. */
871 int mid_end = 0; /* Below last row of the mid area that needs
872 updating. 0 when no mid area updating. */
873 int bot_start = 999;/* first row of the bot area that needs
874 updating. 999 when no bot area updating */
875#ifdef FEAT_VISUAL
876 int scrolled_down = FALSE; /* TRUE when scrolled down when
877 w_topline got smaller a bit */
878#endif
879#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000880 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 int top_to_mod = FALSE; /* redraw above mod_top */
882#endif
883
884 int row; /* current window row to display */
885 linenr_T lnum; /* current buffer lnum to display */
886 int idx; /* current index in w_lines[] */
887 int srow; /* starting row of the current line */
888
889 int eof = FALSE; /* if TRUE, we hit the end of the file */
890 int didline = FALSE; /* if TRUE, we finished the last line */
891 int i;
892 long j;
893 static int recursive = FALSE; /* being called recursively */
894 int old_botline = wp->w_botline;
895#ifdef FEAT_FOLDING
896 long fold_count;
897#endif
898#ifdef FEAT_SYN_HL
899 /* remember what happened to the previous line, to know if
900 * check_visual_highlight() can be used */
901#define DID_NONE 1 /* didn't update a line */
902#define DID_LINE 2 /* updated a normal line */
903#define DID_FOLD 3 /* updated a folded line */
904 int did_update = DID_NONE;
905 linenr_T syntax_last_parsed = 0; /* last parsed text line */
906#endif
907 linenr_T mod_top = 0;
908 linenr_T mod_bot = 0;
909#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
910 int save_got_int;
911#endif
912
913 type = wp->w_redr_type;
914
915 if (type == NOT_VALID)
916 {
917#ifdef FEAT_WINDOWS
918 wp->w_redr_status = TRUE;
919#endif
920 wp->w_lines_valid = 0;
921 }
922
923 /* Window is zero-height: nothing to draw. */
924 if (wp->w_height == 0)
925 {
926 wp->w_redr_type = 0;
927 return;
928 }
929
930#ifdef FEAT_VERTSPLIT
931 /* Window is zero-width: Only need to draw the separator. */
932 if (wp->w_width == 0)
933 {
934 /* draw the vertical separator right of this window */
935 draw_vsep_win(wp, 0);
936 wp->w_redr_type = 0;
937 return;
938 }
939#endif
940
941#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200942 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943#endif
944
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000945#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200946 /* Force redraw when width of 'number' or 'relativenumber' column
947 * changes. */
948 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000949 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000950 {
951 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000952 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000953 }
954 else
955#endif
956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
958 {
959 /*
960 * When there are both inserted/deleted lines and specific lines to be
961 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
962 * everything (only happens when redrawing is off for while).
963 */
964 type = NOT_VALID;
965 }
966 else
967 {
968 /*
969 * Set mod_top to the first line that needs displaying because of
970 * changes. Set mod_bot to the first line after the changes.
971 */
972 mod_top = wp->w_redraw_top;
973 if (wp->w_redraw_bot != 0)
974 mod_bot = wp->w_redraw_bot + 1;
975 else
976 mod_bot = 0;
977 wp->w_redraw_top = 0; /* reset for next time */
978 wp->w_redraw_bot = 0;
979 if (buf->b_mod_set)
980 {
981 if (mod_top == 0 || mod_top > buf->b_mod_top)
982 {
983 mod_top = buf->b_mod_top;
984#ifdef FEAT_SYN_HL
985 /* Need to redraw lines above the change that may be included
986 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200987 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200989 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (mod_top < 1)
991 mod_top = 1;
992 }
993#endif
994 }
995 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
996 mod_bot = buf->b_mod_bot;
997
998#ifdef FEAT_SEARCH_EXTRA
999 /* When 'hlsearch' is on and using a multi-line search pattern, a
1000 * change in one line may make the Search highlighting in a
1001 * previous line invalid. Simple solution: redraw all visible
1002 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001003 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001005 if (search_hl.rm.regprog != NULL
1006 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001008 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001009 {
1010 cur = wp->w_match_head;
1011 while (cur != NULL)
1012 {
1013 if (cur->match.regprog != NULL
1014 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001015 {
1016 top_to_mod = TRUE;
1017 break;
1018 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001019 cur = cur->next;
1020 }
1021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022#endif
1023 }
1024#ifdef FEAT_FOLDING
1025 if (mod_top != 0 && hasAnyFolding(wp))
1026 {
1027 linenr_T lnumt, lnumb;
1028
1029 /*
1030 * A change in a line can cause lines above it to become folded or
1031 * unfolded. Find the top most buffer line that may be affected.
1032 * If the line was previously folded and displayed, get the first
1033 * line of that fold. If the line is folded now, get the first
1034 * folded line. Use the minimum of these two.
1035 */
1036
1037 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1038 * the line below it. If there is no valid entry, use w_topline.
1039 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1040 * to this line. If there is no valid entry, use MAXLNUM. */
1041 lnumt = wp->w_topline;
1042 lnumb = MAXLNUM;
1043 for (i = 0; i < wp->w_lines_valid; ++i)
1044 if (wp->w_lines[i].wl_valid)
1045 {
1046 if (wp->w_lines[i].wl_lastlnum < mod_top)
1047 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1048 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1049 {
1050 lnumb = wp->w_lines[i].wl_lnum;
1051 /* When there is a fold column it might need updating
1052 * in the next line ("J" just above an open fold). */
1053 if (wp->w_p_fdc > 0)
1054 ++lnumb;
1055 }
1056 }
1057
1058 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1059 if (mod_top > lnumt)
1060 mod_top = lnumt;
1061
1062 /* Now do the same for the bottom line (one above mod_bot). */
1063 --mod_bot;
1064 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1065 ++mod_bot;
1066 if (mod_bot < lnumb)
1067 mod_bot = lnumb;
1068 }
1069#endif
1070
1071 /* When a change starts above w_topline and the end is below
1072 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001073 * If the end of the change is above w_topline: do like no change was
1074 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 if (mod_top != 0 && mod_top < wp->w_topline)
1076 {
1077 if (mod_bot > wp->w_topline)
1078 mod_top = wp->w_topline;
1079#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001080 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 top_end = 1;
1082#endif
1083 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001084
1085 /* When line numbers are displayed need to redraw all lines below
1086 * inserted/deleted lines. */
1087 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1088 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090
1091 /*
1092 * When only displaying the lines at the top, set top_end. Used when
1093 * window has scrolled down for msg_scrolled.
1094 */
1095 if (type == REDRAW_TOP)
1096 {
1097 j = 0;
1098 for (i = 0; i < wp->w_lines_valid; ++i)
1099 {
1100 j += wp->w_lines[i].wl_size;
1101 if (j >= wp->w_upd_rows)
1102 {
1103 top_end = j;
1104 break;
1105 }
1106 }
1107 if (top_end == 0)
1108 /* not found (cannot happen?): redraw everything */
1109 type = NOT_VALID;
1110 else
1111 /* top area defined, the rest is VALID */
1112 type = VALID;
1113 }
1114
Bram Moolenaar367329b2007-08-30 11:53:22 +00001115 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001116 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1117 * non-zero and thus not FALSE) will indicate that screenclear() was not
1118 * called. */
1119 if (screen_cleared)
1120 screen_cleared = MAYBE;
1121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 /*
1123 * If there are no changes on the screen that require a complete redraw,
1124 * handle three cases:
1125 * 1: we are off the top of the screen by a few lines: scroll down
1126 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1127 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1128 * w_lines[] that needs updating.
1129 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001130 if ((type == VALID || type == SOME_VALID
1131 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132#ifdef FEAT_DIFF
1133 && !wp->w_botfill && !wp->w_old_botfill
1134#endif
1135 )
1136 {
1137 if (mod_top != 0 && wp->w_topline == mod_top)
1138 {
1139 /*
1140 * w_topline is the first changed line, the scrolling will be done
1141 * further down.
1142 */
1143 }
1144 else if (wp->w_lines[0].wl_valid
1145 && (wp->w_topline < wp->w_lines[0].wl_lnum
1146#ifdef FEAT_DIFF
1147 || (wp->w_topline == wp->w_lines[0].wl_lnum
1148 && wp->w_topfill > wp->w_old_topfill)
1149#endif
1150 ))
1151 {
1152 /*
1153 * New topline is above old topline: May scroll down.
1154 */
1155#ifdef FEAT_FOLDING
1156 if (hasAnyFolding(wp))
1157 {
1158 linenr_T ln;
1159
1160 /* count the number of lines we are off, counting a sequence
1161 * of folded lines as one */
1162 j = 0;
1163 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1164 {
1165 ++j;
1166 if (j >= wp->w_height - 2)
1167 break;
1168 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1169 }
1170 }
1171 else
1172#endif
1173 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1174 if (j < wp->w_height - 2) /* not too far off */
1175 {
1176 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1177#ifdef FEAT_DIFF
1178 /* insert extra lines for previously invisible filler lines */
1179 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1180 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1181 - wp->w_old_topfill;
1182#endif
1183 if (i < wp->w_height - 2) /* less than a screen off */
1184 {
1185 /*
1186 * Try to insert the correct number of lines.
1187 * If not the last window, delete the lines at the bottom.
1188 * win_ins_lines may fail when the terminal can't do it.
1189 */
1190 if (i > 0)
1191 check_for_delay(FALSE);
1192 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1193 {
1194 if (wp->w_lines_valid != 0)
1195 {
1196 /* Need to update rows that are new, stop at the
1197 * first one that scrolled down. */
1198 top_end = i;
1199#ifdef FEAT_VISUAL
1200 scrolled_down = TRUE;
1201#endif
1202
1203 /* Move the entries that were scrolled, disable
1204 * the entries for the lines to be redrawn. */
1205 if ((wp->w_lines_valid += j) > wp->w_height)
1206 wp->w_lines_valid = wp->w_height;
1207 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1208 wp->w_lines[idx] = wp->w_lines[idx - j];
1209 while (idx >= 0)
1210 wp->w_lines[idx--].wl_valid = FALSE;
1211 }
1212 }
1213 else
1214 mid_start = 0; /* redraw all lines */
1215 }
1216 else
1217 mid_start = 0; /* redraw all lines */
1218 }
1219 else
1220 mid_start = 0; /* redraw all lines */
1221 }
1222 else
1223 {
1224 /*
1225 * New topline is at or below old topline: May scroll up.
1226 * When topline didn't change, find first entry in w_lines[] that
1227 * needs updating.
1228 */
1229
1230 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1231 j = -1;
1232 row = 0;
1233 for (i = 0; i < wp->w_lines_valid; i++)
1234 {
1235 if (wp->w_lines[i].wl_valid
1236 && wp->w_lines[i].wl_lnum == wp->w_topline)
1237 {
1238 j = i;
1239 break;
1240 }
1241 row += wp->w_lines[i].wl_size;
1242 }
1243 if (j == -1)
1244 {
1245 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1246 * lines */
1247 mid_start = 0;
1248 }
1249 else
1250 {
1251 /*
1252 * Try to delete the correct number of lines.
1253 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1254 */
1255#ifdef FEAT_DIFF
1256 /* If the topline didn't change, delete old filler lines,
1257 * otherwise delete filler lines of the new topline... */
1258 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1259 row += wp->w_old_topfill;
1260 else
1261 row += diff_check_fill(wp, wp->w_topline);
1262 /* ... but don't delete new filler lines. */
1263 row -= wp->w_topfill;
1264#endif
1265 if (row > 0)
1266 {
1267 check_for_delay(FALSE);
1268 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1269 bot_start = wp->w_height - row;
1270 else
1271 mid_start = 0; /* redraw all lines */
1272 }
1273 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1274 {
1275 /*
1276 * Skip the lines (below the deleted lines) that are still
1277 * valid and don't need redrawing. Copy their info
1278 * upwards, to compensate for the deleted lines. Set
1279 * bot_start to the first row that needs redrawing.
1280 */
1281 bot_start = 0;
1282 idx = 0;
1283 for (;;)
1284 {
1285 wp->w_lines[idx] = wp->w_lines[j];
1286 /* stop at line that didn't fit, unless it is still
1287 * valid (no lines deleted) */
1288 if (row > 0 && bot_start + row
1289 + (int)wp->w_lines[j].wl_size > wp->w_height)
1290 {
1291 wp->w_lines_valid = idx + 1;
1292 break;
1293 }
1294 bot_start += wp->w_lines[idx++].wl_size;
1295
1296 /* stop at the last valid entry in w_lines[].wl_size */
1297 if (++j >= wp->w_lines_valid)
1298 {
1299 wp->w_lines_valid = idx;
1300 break;
1301 }
1302 }
1303#ifdef FEAT_DIFF
1304 /* Correct the first entry for filler lines at the top
1305 * when it won't get updated below. */
1306 if (wp->w_p_diff && bot_start > 0)
1307 wp->w_lines[0].wl_size =
1308 plines_win_nofill(wp, wp->w_topline, TRUE)
1309 + wp->w_topfill;
1310#endif
1311 }
1312 }
1313 }
1314
1315 /* When starting redraw in the first line, redraw all lines. When
1316 * there is only one window it's probably faster to clear the screen
1317 * first. */
1318 if (mid_start == 0)
1319 {
1320 mid_end = wp->w_height;
1321 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001322 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001323 /* Clear the screen when it was not done by win_del_lines() or
1324 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1325 * then. */
1326 if (screen_cleared != TRUE)
1327 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001328#ifdef FEAT_WINDOWS
1329 /* The screen was cleared, redraw the tab pages line. */
1330 if (redraw_tabline)
1331 draw_tabline();
1332#endif
1333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001335
1336 /* When win_del_lines() or win_ins_lines() caused the screen to be
1337 * cleared (only happens for the first window) or when screenclear()
1338 * was called directly above, "must_redraw" will have been set to
1339 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1340 if (screen_cleared == TRUE)
1341 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343 else
1344 {
1345 /* Not VALID or INVERTED: redraw all lines. */
1346 mid_start = 0;
1347 mid_end = wp->w_height;
1348 }
1349
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001350 if (type == SOME_VALID)
1351 {
1352 /* SOME_VALID: redraw all lines. */
1353 mid_start = 0;
1354 mid_end = wp->w_height;
1355 type = NOT_VALID;
1356 }
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358#ifdef FEAT_VISUAL
1359 /* check if we are updating or removing the inverted part */
1360 if ((VIsual_active && buf == curwin->w_buffer)
1361 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1362 {
1363 linenr_T from, to;
1364
1365 if (VIsual_active)
1366 {
1367 if (VIsual_active
1368 && (VIsual_mode != wp->w_old_visual_mode
1369 || type == INVERTED_ALL))
1370 {
1371 /*
1372 * If the type of Visual selection changed, redraw the whole
1373 * selection. Also when the ownership of the X selection is
1374 * gained or lost.
1375 */
1376 if (curwin->w_cursor.lnum < VIsual.lnum)
1377 {
1378 from = curwin->w_cursor.lnum;
1379 to = VIsual.lnum;
1380 }
1381 else
1382 {
1383 from = VIsual.lnum;
1384 to = curwin->w_cursor.lnum;
1385 }
1386 /* redraw more when the cursor moved as well */
1387 if (wp->w_old_cursor_lnum < from)
1388 from = wp->w_old_cursor_lnum;
1389 if (wp->w_old_cursor_lnum > to)
1390 to = wp->w_old_cursor_lnum;
1391 if (wp->w_old_visual_lnum < from)
1392 from = wp->w_old_visual_lnum;
1393 if (wp->w_old_visual_lnum > to)
1394 to = wp->w_old_visual_lnum;
1395 }
1396 else
1397 {
1398 /*
1399 * Find the line numbers that need to be updated: The lines
1400 * between the old cursor position and the current cursor
1401 * position. Also check if the Visual position changed.
1402 */
1403 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1404 {
1405 from = curwin->w_cursor.lnum;
1406 to = wp->w_old_cursor_lnum;
1407 }
1408 else
1409 {
1410 from = wp->w_old_cursor_lnum;
1411 to = curwin->w_cursor.lnum;
1412 if (from == 0) /* Visual mode just started */
1413 from = to;
1414 }
1415
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001416 if (VIsual.lnum != wp->w_old_visual_lnum
1417 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
1419 if (wp->w_old_visual_lnum < from
1420 && wp->w_old_visual_lnum != 0)
1421 from = wp->w_old_visual_lnum;
1422 if (wp->w_old_visual_lnum > to)
1423 to = wp->w_old_visual_lnum;
1424 if (VIsual.lnum < from)
1425 from = VIsual.lnum;
1426 if (VIsual.lnum > to)
1427 to = VIsual.lnum;
1428 }
1429 }
1430
1431 /*
1432 * If in block mode and changed column or curwin->w_curswant:
1433 * update all lines.
1434 * First compute the actual start and end column.
1435 */
1436 if (VIsual_mode == Ctrl_V)
1437 {
1438 colnr_T fromc, toc;
1439
1440 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1441 ++toc;
1442 if (curwin->w_curswant == MAXCOL)
1443 toc = MAXCOL;
1444
1445 if (fromc != wp->w_old_cursor_fcol
1446 || toc != wp->w_old_cursor_lcol)
1447 {
1448 if (from > VIsual.lnum)
1449 from = VIsual.lnum;
1450 if (to < VIsual.lnum)
1451 to = VIsual.lnum;
1452 }
1453 wp->w_old_cursor_fcol = fromc;
1454 wp->w_old_cursor_lcol = toc;
1455 }
1456 }
1457 else
1458 {
1459 /* Use the line numbers of the old Visual area. */
1460 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1461 {
1462 from = wp->w_old_cursor_lnum;
1463 to = wp->w_old_visual_lnum;
1464 }
1465 else
1466 {
1467 from = wp->w_old_visual_lnum;
1468 to = wp->w_old_cursor_lnum;
1469 }
1470 }
1471
1472 /*
1473 * There is no need to update lines above the top of the window.
1474 */
1475 if (from < wp->w_topline)
1476 from = wp->w_topline;
1477
1478 /*
1479 * If we know the value of w_botline, use it to restrict the update to
1480 * the lines that are visible in the window.
1481 */
1482 if (wp->w_valid & VALID_BOTLINE)
1483 {
1484 if (from >= wp->w_botline)
1485 from = wp->w_botline - 1;
1486 if (to >= wp->w_botline)
1487 to = wp->w_botline - 1;
1488 }
1489
1490 /*
1491 * Find the minimal part to be updated.
1492 * Watch out for scrolling that made entries in w_lines[] invalid.
1493 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1494 * top_end; need to redraw from top_end to the "to" line.
1495 * A middle mouse click with a Visual selection may change the text
1496 * above the Visual area and reset wl_valid, do count these for
1497 * mid_end (in srow).
1498 */
1499 if (mid_start > 0)
1500 {
1501 lnum = wp->w_topline;
1502 idx = 0;
1503 srow = 0;
1504 if (scrolled_down)
1505 mid_start = top_end;
1506 else
1507 mid_start = 0;
1508 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1509 {
1510 if (wp->w_lines[idx].wl_valid)
1511 mid_start += wp->w_lines[idx].wl_size;
1512 else if (!scrolled_down)
1513 srow += wp->w_lines[idx].wl_size;
1514 ++idx;
1515# ifdef FEAT_FOLDING
1516 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1517 lnum = wp->w_lines[idx].wl_lnum;
1518 else
1519# endif
1520 ++lnum;
1521 }
1522 srow += mid_start;
1523 mid_end = wp->w_height;
1524 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1525 {
1526 if (wp->w_lines[idx].wl_valid
1527 && wp->w_lines[idx].wl_lnum >= to + 1)
1528 {
1529 /* Only update until first row of this line */
1530 mid_end = srow;
1531 break;
1532 }
1533 srow += wp->w_lines[idx].wl_size;
1534 }
1535 }
1536 }
1537
1538 if (VIsual_active && buf == curwin->w_buffer)
1539 {
1540 wp->w_old_visual_mode = VIsual_mode;
1541 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1542 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001543 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 wp->w_old_curswant = curwin->w_curswant;
1545 }
1546 else
1547 {
1548 wp->w_old_visual_mode = 0;
1549 wp->w_old_cursor_lnum = 0;
1550 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001551 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
1553#endif /* FEAT_VISUAL */
1554
1555#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1556 /* reset got_int, otherwise regexp won't work */
1557 save_got_int = got_int;
1558 got_int = 0;
1559#endif
1560#ifdef FEAT_FOLDING
1561 win_foldinfo.fi_level = 0;
1562#endif
1563
1564 /*
1565 * Update all the window rows.
1566 */
1567 idx = 0; /* first entry in w_lines[].wl_size */
1568 row = 0;
1569 srow = 0;
1570 lnum = wp->w_topline; /* first line shown in window */
1571 for (;;)
1572 {
1573 /* stop updating when reached the end of the window (check for _past_
1574 * the end of the window is at the end of the loop) */
1575 if (row == wp->w_height)
1576 {
1577 didline = TRUE;
1578 break;
1579 }
1580
1581 /* stop updating when hit the end of the file */
1582 if (lnum > buf->b_ml.ml_line_count)
1583 {
1584 eof = TRUE;
1585 break;
1586 }
1587
1588 /* Remember the starting row of the line that is going to be dealt
1589 * with. It is used further down when the line doesn't fit. */
1590 srow = row;
1591
1592 /*
1593 * Update a line when it is in an area that needs updating, when it
1594 * has changes or w_lines[idx] is invalid.
1595 * bot_start may be halfway a wrapped line after using
1596 * win_del_lines(), check if the current line includes it.
1597 * When syntax folding is being used, the saved syntax states will
1598 * already have been updated, we can't see where the syntax state is
1599 * the same again, just update until the end of the window.
1600 */
1601 if (row < top_end
1602 || (row >= mid_start && row < mid_end)
1603#ifdef FEAT_SEARCH_EXTRA
1604 || top_to_mod
1605#endif
1606 || idx >= wp->w_lines_valid
1607 || (row + wp->w_lines[idx].wl_size > bot_start)
1608 || (mod_top != 0
1609 && (lnum == mod_top
1610 || (lnum >= mod_top
1611 && (lnum < mod_bot
1612#ifdef FEAT_SYN_HL
1613 || did_update == DID_FOLD
1614 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001615 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 && (
1617# ifdef FEAT_FOLDING
1618 (foldmethodIsSyntax(wp)
1619 && hasAnyFolding(wp)) ||
1620# endif
1621 syntax_check_changed(lnum)))
1622#endif
1623 )))))
1624 {
1625#ifdef FEAT_SEARCH_EXTRA
1626 if (lnum == mod_top)
1627 top_to_mod = FALSE;
1628#endif
1629
1630 /*
1631 * When at start of changed lines: May scroll following lines
1632 * up or down to minimize redrawing.
1633 * Don't do this when the change continues until the end.
1634 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1635 */
1636 if (lnum == mod_top
1637 && mod_bot != MAXLNUM
1638 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1639 {
1640 int old_rows = 0;
1641 int new_rows = 0;
1642 int xtra_rows;
1643 linenr_T l;
1644
1645 /* Count the old number of window rows, using w_lines[], which
1646 * should still contain the sizes for the lines as they are
1647 * currently displayed. */
1648 for (i = idx; i < wp->w_lines_valid; ++i)
1649 {
1650 /* Only valid lines have a meaningful wl_lnum. Invalid
1651 * lines are part of the changed area. */
1652 if (wp->w_lines[i].wl_valid
1653 && wp->w_lines[i].wl_lnum == mod_bot)
1654 break;
1655 old_rows += wp->w_lines[i].wl_size;
1656#ifdef FEAT_FOLDING
1657 if (wp->w_lines[i].wl_valid
1658 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1659 {
1660 /* Must have found the last valid entry above mod_bot.
1661 * Add following invalid entries. */
1662 ++i;
1663 while (i < wp->w_lines_valid
1664 && !wp->w_lines[i].wl_valid)
1665 old_rows += wp->w_lines[i++].wl_size;
1666 break;
1667 }
1668#endif
1669 }
1670
1671 if (i >= wp->w_lines_valid)
1672 {
1673 /* We can't find a valid line below the changed lines,
1674 * need to redraw until the end of the window.
1675 * Inserting/deleting lines has no use. */
1676 bot_start = 0;
1677 }
1678 else
1679 {
1680 /* Able to count old number of rows: Count new window
1681 * rows, and may insert/delete lines */
1682 j = idx;
1683 for (l = lnum; l < mod_bot; ++l)
1684 {
1685#ifdef FEAT_FOLDING
1686 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1687 ++new_rows;
1688 else
1689#endif
1690#ifdef FEAT_DIFF
1691 if (l == wp->w_topline)
1692 new_rows += plines_win_nofill(wp, l, TRUE)
1693 + wp->w_topfill;
1694 else
1695#endif
1696 new_rows += plines_win(wp, l, TRUE);
1697 ++j;
1698 if (new_rows > wp->w_height - row - 2)
1699 {
1700 /* it's getting too much, must redraw the rest */
1701 new_rows = 9999;
1702 break;
1703 }
1704 }
1705 xtra_rows = new_rows - old_rows;
1706 if (xtra_rows < 0)
1707 {
1708 /* May scroll text up. If there is not enough
1709 * remaining text or scrolling fails, must redraw the
1710 * rest. If scrolling works, must redraw the text
1711 * below the scrolled text. */
1712 if (row - xtra_rows >= wp->w_height - 2)
1713 mod_bot = MAXLNUM;
1714 else
1715 {
1716 check_for_delay(FALSE);
1717 if (win_del_lines(wp, row,
1718 -xtra_rows, FALSE, FALSE) == FAIL)
1719 mod_bot = MAXLNUM;
1720 else
1721 bot_start = wp->w_height + xtra_rows;
1722 }
1723 }
1724 else if (xtra_rows > 0)
1725 {
1726 /* May scroll text down. If there is not enough
1727 * remaining text of scrolling fails, must redraw the
1728 * rest. */
1729 if (row + xtra_rows >= wp->w_height - 2)
1730 mod_bot = MAXLNUM;
1731 else
1732 {
1733 check_for_delay(FALSE);
1734 if (win_ins_lines(wp, row + old_rows,
1735 xtra_rows, FALSE, FALSE) == FAIL)
1736 mod_bot = MAXLNUM;
1737 else if (top_end > row + old_rows)
1738 /* Scrolled the part at the top that requires
1739 * updating down. */
1740 top_end += xtra_rows;
1741 }
1742 }
1743
1744 /* When not updating the rest, may need to move w_lines[]
1745 * entries. */
1746 if (mod_bot != MAXLNUM && i != j)
1747 {
1748 if (j < i)
1749 {
1750 int x = row + new_rows;
1751
1752 /* move entries in w_lines[] upwards */
1753 for (;;)
1754 {
1755 /* stop at last valid entry in w_lines[] */
1756 if (i >= wp->w_lines_valid)
1757 {
1758 wp->w_lines_valid = j;
1759 break;
1760 }
1761 wp->w_lines[j] = wp->w_lines[i];
1762 /* stop at a line that won't fit */
1763 if (x + (int)wp->w_lines[j].wl_size
1764 > wp->w_height)
1765 {
1766 wp->w_lines_valid = j + 1;
1767 break;
1768 }
1769 x += wp->w_lines[j++].wl_size;
1770 ++i;
1771 }
1772 if (bot_start > x)
1773 bot_start = x;
1774 }
1775 else /* j > i */
1776 {
1777 /* move entries in w_lines[] downwards */
1778 j -= i;
1779 wp->w_lines_valid += j;
1780 if (wp->w_lines_valid > wp->w_height)
1781 wp->w_lines_valid = wp->w_height;
1782 for (i = wp->w_lines_valid; i - j >= idx; --i)
1783 wp->w_lines[i] = wp->w_lines[i - j];
1784
1785 /* The w_lines[] entries for inserted lines are
1786 * now invalid, but wl_size may be used above.
1787 * Reset to zero. */
1788 while (i >= idx)
1789 {
1790 wp->w_lines[i].wl_size = 0;
1791 wp->w_lines[i--].wl_valid = FALSE;
1792 }
1793 }
1794 }
1795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
1797
1798#ifdef FEAT_FOLDING
1799 /*
1800 * When lines are folded, display one line for all of them.
1801 * Otherwise, display normally (can be several display lines when
1802 * 'wrap' is on).
1803 */
1804 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1805 if (fold_count != 0)
1806 {
1807 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1808 ++row;
1809 --fold_count;
1810 wp->w_lines[idx].wl_folded = TRUE;
1811 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1812# ifdef FEAT_SYN_HL
1813 did_update = DID_FOLD;
1814# endif
1815 }
1816 else
1817#endif
1818 if (idx < wp->w_lines_valid
1819 && wp->w_lines[idx].wl_valid
1820 && wp->w_lines[idx].wl_lnum == lnum
1821 && lnum > wp->w_topline
1822 && !(dy_flags & DY_LASTLINE)
1823 && srow + wp->w_lines[idx].wl_size > wp->w_height
1824#ifdef FEAT_DIFF
1825 && diff_check_fill(wp, lnum) == 0
1826#endif
1827 )
1828 {
1829 /* This line is not going to fit. Don't draw anything here,
1830 * will draw "@ " lines below. */
1831 row = wp->w_height + 1;
1832 }
1833 else
1834 {
1835#ifdef FEAT_SEARCH_EXTRA
1836 prepare_search_hl(wp, lnum);
1837#endif
1838#ifdef FEAT_SYN_HL
1839 /* Let the syntax stuff know we skipped a few lines. */
1840 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001841 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 syntax_end_parsing(syntax_last_parsed + 1);
1843#endif
1844
1845 /*
1846 * Display one line.
1847 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001848 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
1850#ifdef FEAT_FOLDING
1851 wp->w_lines[idx].wl_folded = FALSE;
1852 wp->w_lines[idx].wl_lastlnum = lnum;
1853#endif
1854#ifdef FEAT_SYN_HL
1855 did_update = DID_LINE;
1856 syntax_last_parsed = lnum;
1857#endif
1858 }
1859
1860 wp->w_lines[idx].wl_lnum = lnum;
1861 wp->w_lines[idx].wl_valid = TRUE;
1862 if (row > wp->w_height) /* past end of screen */
1863 {
1864 /* we may need the size of that too long line later on */
1865 if (dollar_vcol == 0)
1866 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1867 ++idx;
1868 break;
1869 }
1870 if (dollar_vcol == 0)
1871 wp->w_lines[idx].wl_size = row - srow;
1872 ++idx;
1873#ifdef FEAT_FOLDING
1874 lnum += fold_count + 1;
1875#else
1876 ++lnum;
1877#endif
1878 }
1879 else
1880 {
1881 /* This line does not need updating, advance to the next one */
1882 row += wp->w_lines[idx++].wl_size;
1883 if (row > wp->w_height) /* past end of screen */
1884 break;
1885#ifdef FEAT_FOLDING
1886 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1887#else
1888 ++lnum;
1889#endif
1890#ifdef FEAT_SYN_HL
1891 did_update = DID_NONE;
1892#endif
1893 }
1894
1895 if (lnum > buf->b_ml.ml_line_count)
1896 {
1897 eof = TRUE;
1898 break;
1899 }
1900 }
1901 /*
1902 * End of loop over all window lines.
1903 */
1904
1905
1906 if (idx > wp->w_lines_valid)
1907 wp->w_lines_valid = idx;
1908
1909#ifdef FEAT_SYN_HL
1910 /*
1911 * Let the syntax stuff know we stop parsing here.
1912 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001913 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 syntax_end_parsing(syntax_last_parsed + 1);
1915#endif
1916
1917 /*
1918 * If we didn't hit the end of the file, and we didn't finish the last
1919 * line we were working on, then the line didn't fit.
1920 */
1921 wp->w_empty_rows = 0;
1922#ifdef FEAT_DIFF
1923 wp->w_filler_rows = 0;
1924#endif
1925 if (!eof && !didline)
1926 {
1927 if (lnum == wp->w_topline)
1928 {
1929 /*
1930 * Single line that does not fit!
1931 * Don't overwrite it, it can be edited.
1932 */
1933 wp->w_botline = lnum + 1;
1934 }
1935#ifdef FEAT_DIFF
1936 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1937 {
1938 /* Window ends in filler lines. */
1939 wp->w_botline = lnum;
1940 wp->w_filler_rows = wp->w_height - srow;
1941 }
1942#endif
1943 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1944 {
1945 /*
1946 * Last line isn't finished: Display "@@@" at the end.
1947 */
1948 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1949 W_WINROW(wp) + wp->w_height,
1950 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1951 '@', '@', hl_attr(HLF_AT));
1952 set_empty_rows(wp, srow);
1953 wp->w_botline = lnum;
1954 }
1955 else
1956 {
1957 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1958 wp->w_botline = lnum;
1959 }
1960 }
1961 else
1962 {
1963#ifdef FEAT_VERTSPLIT
1964 draw_vsep_win(wp, row);
1965#endif
1966 if (eof) /* we hit the end of the file */
1967 {
1968 wp->w_botline = buf->b_ml.ml_line_count + 1;
1969#ifdef FEAT_DIFF
1970 j = diff_check_fill(wp, wp->w_botline);
1971 if (j > 0 && !wp->w_botfill)
1972 {
1973 /*
1974 * Display filler lines at the end of the file
1975 */
1976 if (char2cells(fill_diff) > 1)
1977 i = '-';
1978 else
1979 i = fill_diff;
1980 if (row + j > wp->w_height)
1981 j = wp->w_height - row;
1982 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1983 row += j;
1984 }
1985#endif
1986 }
1987 else if (dollar_vcol == 0)
1988 wp->w_botline = lnum;
1989
1990 /* make sure the rest of the screen is blank */
1991 /* put '~'s on rows that aren't part of the file. */
1992 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1993 }
1994
1995 /* Reset the type of redrawing required, the window has been updated. */
1996 wp->w_redr_type = 0;
1997#ifdef FEAT_DIFF
1998 wp->w_old_topfill = wp->w_topfill;
1999 wp->w_old_botfill = wp->w_botfill;
2000#endif
2001
2002 if (dollar_vcol == 0)
2003 {
2004 /*
2005 * There is a trick with w_botline. If we invalidate it on each
2006 * change that might modify it, this will cause a lot of expensive
2007 * calls to plines() in update_topline() each time. Therefore the
2008 * value of w_botline is often approximated, and this value is used to
2009 * compute the value of w_topline. If the value of w_botline was
2010 * wrong, check that the value of w_topline is correct (cursor is on
2011 * the visible part of the text). If it's not, we need to redraw
2012 * again. Mostly this just means scrolling up a few lines, so it
2013 * doesn't look too bad. Only do this for the current window (where
2014 * changes are relevant).
2015 */
2016 wp->w_valid |= VALID_BOTLINE;
2017 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2018 {
2019 recursive = TRUE;
2020 curwin->w_valid &= ~VALID_TOPLINE;
2021 update_topline(); /* may invalidate w_botline again */
2022 if (must_redraw != 0)
2023 {
2024 /* Don't update for changes in buffer again. */
2025 i = curbuf->b_mod_set;
2026 curbuf->b_mod_set = FALSE;
2027 win_update(curwin);
2028 must_redraw = 0;
2029 curbuf->b_mod_set = i;
2030 }
2031 recursive = FALSE;
2032 }
2033 }
2034
2035#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2036 /* restore got_int, unless CTRL-C was hit while redrawing */
2037 if (!got_int)
2038 got_int = save_got_int;
2039#endif
2040}
2041
2042#ifdef FEAT_SIGNS
2043static int draw_signcolumn __ARGS((win_T *wp));
2044
2045/*
2046 * Return TRUE when window "wp" has a column to draw signs in.
2047 */
2048 static int
2049draw_signcolumn(wp)
2050 win_T *wp;
2051{
2052 return (wp->w_buffer->b_signlist != NULL
2053# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002054 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055# endif
2056 );
2057}
2058#endif
2059
2060/*
2061 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2062 * as the filler character.
2063 */
2064 static void
2065win_draw_end(wp, c1, c2, row, endrow, hl)
2066 win_T *wp;
2067 int c1;
2068 int c2;
2069 int row;
2070 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002071 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072{
2073#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2074 int n = 0;
2075# define FDC_OFF n
2076#else
2077# define FDC_OFF 0
2078#endif
2079
2080#ifdef FEAT_RIGHTLEFT
2081 if (wp->w_p_rl)
2082 {
2083 /* No check for cmdline window: should never be right-left. */
2084# ifdef FEAT_FOLDING
2085 n = wp->w_p_fdc;
2086
2087 if (n > 0)
2088 {
2089 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002090 if (n > W_WIDTH(wp))
2091 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2093 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2094 ' ', ' ', hl_attr(HLF_FC));
2095 }
2096# endif
2097# ifdef FEAT_SIGNS
2098 if (draw_signcolumn(wp))
2099 {
2100 int nn = n + 2;
2101
2102 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002103 if (nn > W_WIDTH(wp))
2104 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2106 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2107 ' ', ' ', hl_attr(HLF_SC));
2108 n = nn;
2109 }
2110# endif
2111 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2112 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2113 c2, c2, hl_attr(hl));
2114 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2115 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2116 c1, c2, hl_attr(hl));
2117 }
2118 else
2119#endif
2120 {
2121#ifdef FEAT_CMDWIN
2122 if (cmdwin_type != 0 && wp == curwin)
2123 {
2124 /* draw the cmdline character in the leftmost column */
2125 n = 1;
2126 if (n > wp->w_width)
2127 n = wp->w_width;
2128 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2129 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2130 cmdwin_type, ' ', hl_attr(HLF_AT));
2131 }
2132#endif
2133#ifdef FEAT_FOLDING
2134 if (wp->w_p_fdc > 0)
2135 {
2136 int nn = n + wp->w_p_fdc;
2137
2138 /* draw the fold column at the left */
2139 if (nn > W_WIDTH(wp))
2140 nn = W_WIDTH(wp);
2141 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2142 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2143 ' ', ' ', hl_attr(HLF_FC));
2144 n = nn;
2145 }
2146#endif
2147#ifdef FEAT_SIGNS
2148 if (draw_signcolumn(wp))
2149 {
2150 int nn = n + 2;
2151
2152 /* draw the sign column after the fold column */
2153 if (nn > W_WIDTH(wp))
2154 nn = W_WIDTH(wp);
2155 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2156 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2157 ' ', ' ', hl_attr(HLF_SC));
2158 n = nn;
2159 }
2160#endif
2161 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2162 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2163 c1, c2, hl_attr(hl));
2164 }
2165 set_empty_rows(wp, row);
2166}
2167
Bram Moolenaar1a384422010-07-14 19:53:30 +02002168#ifdef FEAT_SYN_HL
2169static int advance_color_col __ARGS((int vcol, int **color_cols));
2170
2171/*
2172 * Advance **color_cols and return TRUE when there are columns to draw.
2173 */
2174 static int
2175advance_color_col(vcol, color_cols)
2176 int vcol;
2177 int **color_cols;
2178{
2179 while (**color_cols >= 0 && vcol > **color_cols)
2180 ++*color_cols;
2181 return (**color_cols >= 0);
2182}
2183#endif
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185#ifdef FEAT_FOLDING
2186/*
2187 * Display one folded line.
2188 */
2189 static void
2190fold_line(wp, fold_count, foldinfo, lnum, row)
2191 win_T *wp;
2192 long fold_count;
2193 foldinfo_T *foldinfo;
2194 linenr_T lnum;
2195 int row;
2196{
2197 char_u buf[51];
2198 pos_T *top, *bot;
2199 linenr_T lnume = lnum + fold_count - 1;
2200 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002201 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 int col;
2204 int txtcol;
2205 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002206 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
2208 /* Build the fold line:
2209 * 1. Add the cmdwin_type for the command-line window
2210 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002211 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 * 4. Compose the text
2213 * 5. Add the text
2214 * 6. set highlighting for the Visual area an other text
2215 */
2216 col = 0;
2217
2218 /*
2219 * 1. Add the cmdwin_type for the command-line window
2220 * Ignores 'rightleft', this window is never right-left.
2221 */
2222#ifdef FEAT_CMDWIN
2223 if (cmdwin_type != 0 && wp == curwin)
2224 {
2225 ScreenLines[off] = cmdwin_type;
2226 ScreenAttrs[off] = hl_attr(HLF_AT);
2227#ifdef FEAT_MBYTE
2228 if (enc_utf8)
2229 ScreenLinesUC[off] = 0;
2230#endif
2231 ++col;
2232 }
2233#endif
2234
2235 /*
2236 * 2. Add the 'foldcolumn'
2237 */
2238 fdc = wp->w_p_fdc;
2239 if (fdc > W_WIDTH(wp) - col)
2240 fdc = W_WIDTH(wp) - col;
2241 if (fdc > 0)
2242 {
2243 fill_foldcolumn(buf, wp, TRUE, lnum);
2244#ifdef FEAT_RIGHTLEFT
2245 if (wp->w_p_rl)
2246 {
2247 int i;
2248
2249 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2250 hl_attr(HLF_FC));
2251 /* reverse the fold column */
2252 for (i = 0; i < fdc; ++i)
2253 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2254 }
2255 else
2256#endif
2257 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2258 col += fdc;
2259 }
2260
2261#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002262# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2263 for (ri = 0; ri < l; ++ri) \
2264 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2265 else \
2266 for (ri = 0; ri < l; ++ri) \
2267 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002269# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2270 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271#endif
2272
Bram Moolenaar64486672010-05-16 15:46:46 +02002273 /* Set all attributes of the 'number' or 'relativenumber' column and the
2274 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002275 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276
2277#ifdef FEAT_SIGNS
2278 /* If signs are being displayed, add two spaces. */
2279 if (draw_signcolumn(wp))
2280 {
2281 len = W_WIDTH(wp) - col;
2282 if (len > 0)
2283 {
2284 if (len > 2)
2285 len = 2;
2286# ifdef FEAT_RIGHTLEFT
2287 if (wp->w_p_rl)
2288 /* the line number isn't reversed */
2289 copy_text_attr(off + W_WIDTH(wp) - len - col,
2290 (char_u *)" ", len, hl_attr(HLF_FL));
2291 else
2292# endif
2293 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2294 col += len;
2295 }
2296 }
2297#endif
2298
2299 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002300 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002302 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 len = W_WIDTH(wp) - col;
2305 if (len > 0)
2306 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002307 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002308 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002309
2310 if (len > w + 1)
2311 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002312
2313 if (wp->w_p_nu)
2314 /* 'number' */
2315 num = (long)lnum;
2316 else
2317 /* 'relativenumber', don't use negative numbers */
2318 num = (long)abs((int)get_cursor_rel_lnum(wp, lnum));
2319
2320 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#ifdef FEAT_RIGHTLEFT
2322 if (wp->w_p_rl)
2323 /* the line number isn't reversed */
2324 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2325 hl_attr(HLF_FL));
2326 else
2327#endif
2328 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2329 col += len;
2330 }
2331 }
2332
2333 /*
2334 * 4. Compose the folded-line string with 'foldtext', if set.
2335 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002336 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 txtcol = col; /* remember where text starts */
2339
2340 /*
2341 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2342 * Right-left text is put in columns 0 - number-col, normal text is put
2343 * in columns number-col - window-width.
2344 */
2345#ifdef FEAT_MBYTE
2346 if (has_mbyte)
2347 {
2348 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002349 int u8c, u8cc[MAX_MCO];
2350 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 int idx;
2352 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002353 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354# ifdef FEAT_ARABIC
2355 int prev_c = 0; /* previous Arabic character */
2356 int prev_c1 = 0; /* first composing char for prev_c */
2357# endif
2358
2359# ifdef FEAT_RIGHTLEFT
2360 if (wp->w_p_rl)
2361 idx = off;
2362 else
2363# endif
2364 idx = off + col;
2365
2366 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2367 for (p = text; *p != NUL; )
2368 {
2369 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002370 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (col + cells > W_WIDTH(wp)
2372# ifdef FEAT_RIGHTLEFT
2373 - (wp->w_p_rl ? col : 0)
2374# endif
2375 )
2376 break;
2377 ScreenLines[idx] = *p;
2378 if (enc_utf8)
2379 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002380 u8c = utfc_ptr2char(p, u8cc);
2381 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
2383 ScreenLinesUC[idx] = 0;
2384#ifdef FEAT_ARABIC
2385 prev_c = u8c;
2386#endif
2387 }
2388 else
2389 {
2390#ifdef FEAT_ARABIC
2391 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2392 {
2393 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002394 int pc, pc1, nc;
2395 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 int firstbyte = *p;
2397
2398 /* The idea of what is the previous and next
2399 * character depends on 'rightleft'. */
2400 if (wp->w_p_rl)
2401 {
2402 pc = prev_c;
2403 pc1 = prev_c1;
2404 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002405 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407 else
2408 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002409 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002411 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413 prev_c = u8c;
2414
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002415 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 pc, pc1, nc);
2417 ScreenLines[idx] = firstbyte;
2418 }
2419 else
2420 prev_c = u8c;
2421#endif
2422 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002423#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (u8c >= 0x10000)
2425 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2426 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002429 for (i = 0; i < Screen_mco; ++i)
2430 {
2431 ScreenLinesC[i][idx] = u8cc[i];
2432 if (u8cc[i] == 0)
2433 break;
2434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436 if (cells > 1)
2437 ScreenLines[idx + 1] = 0;
2438 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002439 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2440 /* double-byte single width character */
2441 ScreenLines2[idx] = p[1];
2442 else if (cells > 1)
2443 /* double-width character */
2444 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 col += cells;
2446 idx += cells;
2447 p += c_len;
2448 }
2449 }
2450 else
2451#endif
2452 {
2453 len = (int)STRLEN(text);
2454 if (len > W_WIDTH(wp) - col)
2455 len = W_WIDTH(wp) - col;
2456 if (len > 0)
2457 {
2458#ifdef FEAT_RIGHTLEFT
2459 if (wp->w_p_rl)
2460 STRNCPY(current_ScreenLine, text, len);
2461 else
2462#endif
2463 STRNCPY(current_ScreenLine + col, text, len);
2464 col += len;
2465 }
2466 }
2467
2468 /* Fill the rest of the line with the fold filler */
2469#ifdef FEAT_RIGHTLEFT
2470 if (wp->w_p_rl)
2471 col -= txtcol;
2472#endif
2473 while (col < W_WIDTH(wp)
2474#ifdef FEAT_RIGHTLEFT
2475 - (wp->w_p_rl ? txtcol : 0)
2476#endif
2477 )
2478 {
2479#ifdef FEAT_MBYTE
2480 if (enc_utf8)
2481 {
2482 if (fill_fold >= 0x80)
2483 {
2484 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002485 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487 else
2488 ScreenLinesUC[off + col] = 0;
2489 }
2490#endif
2491 ScreenLines[off + col++] = fill_fold;
2492 }
2493
2494 if (text != buf)
2495 vim_free(text);
2496
2497 /*
2498 * 6. set highlighting for the Visual area an other text.
2499 * If all folded lines are in the Visual area, highlight the line.
2500 */
2501#ifdef FEAT_VISUAL
2502 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2503 {
2504 if (ltoreq(curwin->w_cursor, VIsual))
2505 {
2506 /* Visual is after curwin->w_cursor */
2507 top = &curwin->w_cursor;
2508 bot = &VIsual;
2509 }
2510 else
2511 {
2512 /* Visual is before curwin->w_cursor */
2513 top = &VIsual;
2514 bot = &curwin->w_cursor;
2515 }
2516 if (lnum >= top->lnum
2517 && lnume <= bot->lnum
2518 && (VIsual_mode != 'v'
2519 || ((lnum > top->lnum
2520 || (lnum == top->lnum
2521 && top->col == 0))
2522 && (lnume < bot->lnum
2523 || (lnume == bot->lnum
2524 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002525 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 if (VIsual_mode == Ctrl_V)
2528 {
2529 /* Visual block mode: highlight the chars part of the block */
2530 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2531 {
2532 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2533 len = wp->w_old_cursor_lcol;
2534 else
2535 len = W_WIDTH(wp) - txtcol;
2536 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002537 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539 }
2540 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002543 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546 }
2547#endif
2548
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002549#ifdef FEAT_SYN_HL
2550 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002551 if (wp->w_p_cuc)
2552 {
2553 txtcol += wp->w_virtcol;
2554 if (wp->w_p_wrap)
2555 txtcol -= wp->w_skipcol;
2556 else
2557 txtcol -= wp->w_leftcol;
2558 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2559 ScreenAttrs[off + txtcol] = hl_combine_attr(
2560 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2561 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
2564 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2565 (int)W_WIDTH(wp), FALSE);
2566
2567 /*
2568 * Update w_cline_height and w_cline_folded if the cursor line was
2569 * updated (saves a call to plines() later).
2570 */
2571 if (wp == curwin
2572 && lnum <= curwin->w_cursor.lnum
2573 && lnume >= curwin->w_cursor.lnum)
2574 {
2575 curwin->w_cline_row = row;
2576 curwin->w_cline_height = 1;
2577 curwin->w_cline_folded = TRUE;
2578 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2579 }
2580}
2581
2582/*
2583 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2584 */
2585 static void
2586copy_text_attr(off, buf, len, attr)
2587 int off;
2588 char_u *buf;
2589 int len;
2590 int attr;
2591{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002592 int i;
2593
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 mch_memmove(ScreenLines + off, buf, (size_t)len);
2595# ifdef FEAT_MBYTE
2596 if (enc_utf8)
2597 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2598# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002599 for (i = 0; i < len; ++i)
2600 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601}
2602
2603/*
2604 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002605 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
2607 static void
2608fill_foldcolumn(p, wp, closed, lnum)
2609 char_u *p;
2610 win_T *wp;
2611 int closed; /* TRUE of FALSE */
2612 linenr_T lnum; /* current line number */
2613{
2614 int i = 0;
2615 int level;
2616 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002617 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 /* Init to all spaces. */
2620 copy_spaces(p, (size_t)wp->w_p_fdc);
2621
2622 level = win_foldinfo.fi_level;
2623 if (level > 0)
2624 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002625 /* If there is only one column put more info in it. */
2626 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 /* If the column is too narrow, we start at the lowest level that
2629 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002630 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (first_level < 1)
2632 first_level = 1;
2633
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002634 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 if (win_foldinfo.fi_lnum == lnum
2637 && first_level + i >= win_foldinfo.fi_low_level)
2638 p[i] = '-';
2639 else if (first_level == 1)
2640 p[i] = '|';
2641 else if (first_level + i <= 9)
2642 p[i] = '0' + first_level + i;
2643 else
2644 p[i] = '>';
2645 if (first_level + i == level)
2646 break;
2647 }
2648 }
2649 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002650 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651}
2652#endif /* FEAT_FOLDING */
2653
2654/*
2655 * Display line "lnum" of window 'wp' on the screen.
2656 * Start at row "startrow", stop when "endrow" is reached.
2657 * wp->w_virtcol needs to be valid.
2658 *
2659 * Return the number of last row the line occupies.
2660 */
2661 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002662win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 win_T *wp;
2664 linenr_T lnum;
2665 int startrow;
2666 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002667 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669 int col; /* visual column on screen */
2670 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2671 int c = 0; /* init for GCC */
2672 long vcol = 0; /* virtual column (for tabs) */
2673 long vcol_prev = -1; /* "vcol" of previous character */
2674 char_u *line; /* current line */
2675 char_u *ptr; /* current position in "line" */
2676 int row; /* row in the window, excl w_winrow */
2677 int screen_row; /* row on the screen, incl w_winrow */
2678
2679 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2680 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002681 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 int c_extra = NUL; /* extra chars, all the same */
2683 int extra_attr = 0; /* attributes when n_extra != 0 */
2684 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2685 displaying lcs_eol at end-of-line */
2686 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2687 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2688
2689 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2690 int saved_n_extra = 0;
2691 char_u *saved_p_extra = NULL;
2692 int saved_c_extra = 0;
2693 int saved_char_attr = 0;
2694
2695 int n_attr = 0; /* chars with special attr */
2696 int saved_attr2 = 0; /* char_attr saved for n_attr */
2697 int n_attr3 = 0; /* chars with overruling special attr */
2698 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2699
2700 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2701
2702 int fromcol, tocol; /* start/end of inverting */
2703 int fromcol_prev = -2; /* start of inverting after cursor */
2704 int noinvcur = FALSE; /* don't invert the cursor */
2705#ifdef FEAT_VISUAL
2706 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002707 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708#endif
2709 pos_T pos;
2710 long v;
2711
2712 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002713 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2715 in this line */
2716 int attr = 0; /* attributes for area highlighting */
2717 int area_attr = 0; /* attributes desired by highlighting */
2718 int search_attr = 0; /* attributes desired by 'hlsearch' */
2719#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002720 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 int syntax_attr = 0; /* attributes desired by syntax */
2722 int has_syntax = FALSE; /* this buffer has syntax highl. */
2723 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002724 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002725 int draw_color_col = FALSE; /* highlight colorcolumn */
2726 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002727#endif
2728#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002729 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002730# define SPWORDLEN 150
2731 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002732 int nextlinecol = 0; /* column where nextline[] starts */
2733 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002734 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002735 int spell_attr = 0; /* attributes desired by spelling */
2736 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002737 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2738 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002739 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002740 static int cap_col = -1; /* column to check for Cap word */
2741 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002742 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#endif
2744 int extra_check; /* has syntax or linebreak */
2745#ifdef FEAT_MBYTE
2746 int multi_attr = 0; /* attributes desired by multibyte */
2747 int mb_l = 1; /* multi-byte byte length */
2748 int mb_c = 0; /* decoded multi-byte character */
2749 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002750 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#endif
2752#ifdef FEAT_DIFF
2753 int filler_lines; /* nr of filler lines to be drawn */
2754 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002755 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 int change_start = MAXCOL; /* first col of changed area */
2757 int change_end = -1; /* last col of changed area */
2758#endif
2759 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2760#ifdef FEAT_LINEBREAK
2761 int need_showbreak = FALSE;
2762#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002763#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2764 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002766 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767#endif
2768#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002769 matchitem_T *cur; /* points to the match list */
2770 match_T *shl; /* points to search_hl or a match */
2771 int shl_flag; /* flag to indicate whether search_hl
2772 has been processed or not */
2773 int prevcol_hl_flag; /* flag to indicate whether prevcol
2774 equals startcol of search_hl or one
2775 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776#endif
2777#ifdef FEAT_ARABIC
2778 int prev_c = 0; /* previous Arabic character */
2779 int prev_c1 = 0; /* first composing char for prev_c */
2780#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002781#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002782 int did_line_attr = 0;
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 /* draw_state: items that are drawn in sequence: */
2786#define WL_START 0 /* nothing done yet */
2787#ifdef FEAT_CMDWIN
2788# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2789#else
2790# define WL_CMDLINE WL_START
2791#endif
2792#ifdef FEAT_FOLDING
2793# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2794#else
2795# define WL_FOLD WL_CMDLINE
2796#endif
2797#ifdef FEAT_SIGNS
2798# define WL_SIGN WL_FOLD + 1 /* column for signs */
2799#else
2800# define WL_SIGN WL_FOLD /* column for signs */
2801#endif
2802#define WL_NR WL_SIGN + 1 /* line number */
2803#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2804# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2805#else
2806# define WL_SBR WL_NR
2807#endif
2808#define WL_LINE WL_SBR + 1 /* text in the line */
2809 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002810#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 int feedback_col = 0;
2812 int feedback_old_attr = -1;
2813#endif
2814
Bram Moolenaar860cae12010-06-05 23:22:07 +02002815#ifdef FEAT_CONCEAL
2816 int syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002817 int syntax_id = 0;
2818 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002819 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002820 int is_concealing = FALSE;
2821 int boguscols = 0; /* nonexistent columns added to force
2822 wrapping */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002823 int vcol_off = 0; /* offset for concealed characters */
2824 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002825# define VCOL_HLC (vcol - vcol_off)
2826#else
2827# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830 if (startrow > endrow) /* past the end already! */
2831 return startrow;
2832
2833 row = startrow;
2834 screen_row = row + W_WINROW(wp);
2835
2836 /*
2837 * To speed up the loop below, set extra_check when there is linebreak,
2838 * trailing white space and/or syntax processing to be done.
2839 */
2840#ifdef FEAT_LINEBREAK
2841 extra_check = wp->w_p_lbr;
2842#else
2843 extra_check = 0;
2844#endif
2845#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002846 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
2848 /* Prepare for syntax highlighting in this line. When there is an
2849 * error, stop syntax highlighting. */
2850 save_did_emsg = did_emsg;
2851 did_emsg = FALSE;
2852 syntax_start(wp, lnum);
2853 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002854 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 else
2856 {
2857 did_emsg = save_did_emsg;
2858 has_syntax = TRUE;
2859 extra_check = TRUE;
2860 }
2861 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002862
2863 /* Check for columns to display for 'colorcolumn'. */
2864 color_cols = wp->w_p_cc_cols;
2865 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002866 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002867#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002868
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002869#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002870 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002871 && *wp->w_s->b_p_spl != NUL
2872 && wp->w_s->b_langp.ga_len > 0
2873 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002874 {
2875 /* Prepare for spell checking. */
2876 has_spell = TRUE;
2877 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002878
2879 /* Get the start of the next line, so that words that wrap to the next
2880 * line are found too: "et<line-break>al.".
2881 * Trick: skip a few chars for C/shell/Vim comments */
2882 nextline[SPWORDLEN] = NUL;
2883 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2884 {
2885 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2886 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2887 }
2888
2889 /* When a word wrapped from the previous line the start of the current
2890 * line is valid. */
2891 if (lnum == checked_lnum)
2892 cur_checked_col = checked_col;
2893 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002894
2895 /* When there was a sentence end in the previous line may require a
2896 * word starting with capital in this line. In line 1 always check
2897 * the first word. */
2898 if (lnum != capcol_lnum)
2899 cap_col = -1;
2900 if (lnum == 1)
2901 cap_col = 0;
2902 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904#endif
2905
2906 /*
2907 * handle visual active in this window
2908 */
2909 fromcol = -10;
2910 tocol = MAXCOL;
2911#ifdef FEAT_VISUAL
2912 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2913 {
2914 /* Visual is after curwin->w_cursor */
2915 if (ltoreq(curwin->w_cursor, VIsual))
2916 {
2917 top = &curwin->w_cursor;
2918 bot = &VIsual;
2919 }
2920 else /* Visual is before curwin->w_cursor */
2921 {
2922 top = &VIsual;
2923 bot = &curwin->w_cursor;
2924 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002925 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (VIsual_mode == Ctrl_V) /* block mode */
2927 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002928 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 {
2930 fromcol = wp->w_old_cursor_fcol;
2931 tocol = wp->w_old_cursor_lcol;
2932 }
2933 }
2934 else /* non-block mode */
2935 {
2936 if (lnum > top->lnum && lnum <= bot->lnum)
2937 fromcol = 0;
2938 else if (lnum == top->lnum)
2939 {
2940 if (VIsual_mode == 'V') /* linewise */
2941 fromcol = 0;
2942 else
2943 {
2944 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2945 if (gchar_pos(top) == NUL)
2946 tocol = fromcol + 1;
2947 }
2948 }
2949 if (VIsual_mode != 'V' && lnum == bot->lnum)
2950 {
2951 if (*p_sel == 'e' && bot->col == 0
2952#ifdef FEAT_VIRTUALEDIT
2953 && bot->coladd == 0
2954#endif
2955 )
2956 {
2957 fromcol = -10;
2958 tocol = MAXCOL;
2959 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002960 else if (bot->col == MAXCOL)
2961 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 else
2963 {
2964 pos = *bot;
2965 if (*p_sel == 'e')
2966 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2967 else
2968 {
2969 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2970 ++tocol;
2971 }
2972 }
2973 }
2974 }
2975
2976#ifndef MSDOS
2977 /* Check if the character under the cursor should not be inverted */
2978 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2979# ifdef FEAT_GUI
2980 && !gui.in_use
2981# endif
2982 )
2983 noinvcur = TRUE;
2984#endif
2985
2986 /* if inverting in this line set area_highlighting */
2987 if (fromcol >= 0)
2988 {
2989 area_highlighting = TRUE;
2990 attr = hl_attr(HLF_V);
2991#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2992 if (clip_star.available && !clip_star.owned && clip_isautosel())
2993 attr = hl_attr(HLF_VNC);
2994#endif
2995 }
2996 }
2997
2998 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002999 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 */
3001 else
3002#endif /* FEAT_VISUAL */
3003 if (highlight_match
3004 && wp == curwin
3005 && lnum >= curwin->w_cursor.lnum
3006 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3007 {
3008 if (lnum == curwin->w_cursor.lnum)
3009 getvcol(curwin, &(curwin->w_cursor),
3010 (colnr_T *)&fromcol, NULL, NULL);
3011 else
3012 fromcol = 0;
3013 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3014 {
3015 pos.lnum = lnum;
3016 pos.col = search_match_endcol;
3017 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3018 }
3019 else
3020 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003021 /* do at least one character; happens when past end of line */
3022 if (fromcol == tocol)
3023 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 area_highlighting = TRUE;
3025 attr = hl_attr(HLF_I);
3026 }
3027
3028#ifdef FEAT_DIFF
3029 filler_lines = diff_check(wp, lnum);
3030 if (filler_lines < 0)
3031 {
3032 if (filler_lines == -1)
3033 {
3034 if (diff_find_change(wp, lnum, &change_start, &change_end))
3035 diff_hlf = HLF_ADD; /* added line */
3036 else if (change_start == 0)
3037 diff_hlf = HLF_TXD; /* changed text */
3038 else
3039 diff_hlf = HLF_CHD; /* changed line */
3040 }
3041 else
3042 diff_hlf = HLF_ADD; /* added line */
3043 filler_lines = 0;
3044 area_highlighting = TRUE;
3045 }
3046 if (lnum == wp->w_topline)
3047 filler_lines = wp->w_topfill;
3048 filler_todo = filler_lines;
3049#endif
3050
3051#ifdef LINE_ATTR
3052# ifdef FEAT_SIGNS
3053 /* If this line has a sign with line highlighting set line_attr. */
3054 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3055 if (v != 0)
3056 line_attr = sign_get_attr((int)v, TRUE);
3057# endif
3058# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3059 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003060 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 line_attr = hl_attr(HLF_L);
3062# endif
3063 if (line_attr != 0)
3064 area_highlighting = TRUE;
3065#endif
3066
3067 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3068 ptr = line;
3069
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003070#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003071 if (has_spell)
3072 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003073 /* For checking first word with a capital skip white space. */
3074 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003075 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003076
Bram Moolenaar30abd282005-06-22 22:35:10 +00003077 /* To be able to spell-check over line boundaries copy the end of the
3078 * current line into nextline[]. Above the start of the next line was
3079 * copied to nextline[SPWORDLEN]. */
3080 if (nextline[SPWORDLEN] == NUL)
3081 {
3082 /* No next line or it is empty. */
3083 nextlinecol = MAXCOL;
3084 nextline_idx = 0;
3085 }
3086 else
3087 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003088 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003089 if (v < SPWORDLEN)
3090 {
3091 /* Short line, use it completely and append the start of the
3092 * next line. */
3093 nextlinecol = 0;
3094 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003095 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003096 nextline_idx = v + 1;
3097 }
3098 else
3099 {
3100 /* Long line, use only the last SPWORDLEN bytes. */
3101 nextlinecol = v - SPWORDLEN;
3102 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3103 nextline_idx = SPWORDLEN + 1;
3104 }
3105 }
3106 }
3107#endif
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 /* find start of trailing whitespace */
3110 if (wp->w_p_list && lcs_trail)
3111 {
3112 trailcol = (colnr_T)STRLEN(ptr);
3113 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3114 --trailcol;
3115 trailcol += (colnr_T) (ptr - line);
3116 extra_check = TRUE;
3117 }
3118
3119 /*
3120 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3121 * first character to be displayed.
3122 */
3123 if (wp->w_p_wrap)
3124 v = wp->w_skipcol;
3125 else
3126 v = wp->w_leftcol;
3127 if (v > 0)
3128 {
3129#ifdef FEAT_MBYTE
3130 char_u *prev_ptr = ptr;
3131#endif
3132 while (vcol < v && *ptr != NUL)
3133 {
3134 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3135 vcol += c;
3136#ifdef FEAT_MBYTE
3137 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003139 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 }
3141
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003142#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3143 /* When:
3144 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003145 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003146 * - 'virtualedit' is set, or
3147 * - the visual mode is active,
3148 * the end of the line may be before the start of the displayed part.
3149 */
3150 if (vcol < v && (
3151# ifdef FEAT_SYN_HL
3152 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003153 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003154# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3155 ||
3156# endif
3157# endif
3158# ifdef FEAT_VIRTUALEDIT
3159 virtual_active()
3160# ifdef FEAT_VISUAL
3161 ||
3162# endif
3163# endif
3164# ifdef FEAT_VISUAL
3165 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3166# endif
3167 ))
3168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#endif
3172
3173 /* Handle a character that's not completely on the screen: Put ptr at
3174 * that character but skip the first few screen characters. */
3175 if (vcol > v)
3176 {
3177 vcol -= c;
3178#ifdef FEAT_MBYTE
3179 ptr = prev_ptr;
3180#else
3181 --ptr;
3182#endif
3183 n_skip = v - vcol;
3184 }
3185
3186 /*
3187 * Adjust for when the inverted text is before the screen,
3188 * and when the start of the inverted text is before the screen.
3189 */
3190 if (tocol <= vcol)
3191 fromcol = 0;
3192 else if (fromcol >= 0 && fromcol < vcol)
3193 fromcol = vcol;
3194
3195#ifdef FEAT_LINEBREAK
3196 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3197 if (wp->w_p_wrap)
3198 need_showbreak = TRUE;
3199#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003200#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003201 /* When spell checking a word we need to figure out the start of the
3202 * word and if it's badly spelled or not. */
3203 if (has_spell)
3204 {
3205 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003206 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003207 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003208
3209 pos = wp->w_cursor;
3210 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003211 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003212 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003213
3214 /* spell_move_to() may call ml_get() and make "line" invalid */
3215 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3216 ptr = line + linecol;
3217
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003218 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003219 {
3220 /* no bad word found at line start, don't check until end of a
3221 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003222 spell_hlf = HLF_COUNT;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003223 word_end = (int)(spell_to_word_end(ptr, wp)
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003224 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003225 }
3226 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003227 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003228 /* bad word found, use attributes until end of word */
3229 word_end = wp->w_cursor.col + len + 1;
3230
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003231 /* Turn index into actual attributes. */
3232 if (spell_hlf != HLF_COUNT)
3233 spell_attr = highlight_attr[spell_hlf];
3234 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003235 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003236
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003237# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003238 /* Need to restart syntax highlighting for this line. */
3239 if (has_syntax)
3240 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003241# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003242 }
3243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
3245
3246 /*
3247 * Correct highlighting for cursor that can't be disabled.
3248 * Avoids having to check this for each character.
3249 */
3250 if (fromcol >= 0)
3251 {
3252 if (noinvcur)
3253 {
3254 if ((colnr_T)fromcol == wp->w_virtcol)
3255 {
3256 /* highlighting starts at cursor, let it start just after the
3257 * cursor */
3258 fromcol_prev = fromcol;
3259 fromcol = -1;
3260 }
3261 else if ((colnr_T)fromcol < wp->w_virtcol)
3262 /* restart highlighting after the cursor */
3263 fromcol_prev = wp->w_virtcol;
3264 }
3265 if (fromcol >= tocol)
3266 fromcol = -1;
3267 }
3268
3269#ifdef FEAT_SEARCH_EXTRA
3270 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003271 * Handle highlighting the last used search pattern and matches.
3272 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003274 cur = wp->w_match_head;
3275 shl_flag = FALSE;
3276 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003278 if (shl_flag == FALSE)
3279 {
3280 shl = &search_hl;
3281 shl_flag = TRUE;
3282 }
3283 else
3284 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003285 shl->startcol = MAXCOL;
3286 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 shl->attr_cur = 0;
3288 if (shl->rm.regprog != NULL)
3289 {
3290 v = (long)(ptr - line);
3291 next_search_hl(wp, shl, lnum, (colnr_T)v);
3292
3293 /* Need to get the line again, a multi-line regexp may have made it
3294 * invalid. */
3295 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3296 ptr = line + v;
3297
3298 if (shl->lnum != 0 && shl->lnum <= lnum)
3299 {
3300 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003301 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003303 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3305 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003306 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003308 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003310 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003313 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003314 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 else
3316#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003317 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003319 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
3321 shl->attr_cur = shl->attr;
3322 search_attr = shl->attr;
3323 }
3324 area_highlighting = TRUE;
3325 }
3326 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003327 if (shl != &search_hl && cur != NULL)
3328 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
3330#endif
3331
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003332#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003333 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3334 * active, because it's not clear what is selected then. */
3335 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003336 {
3337 line_attr = hl_attr(HLF_CUL);
3338 area_highlighting = TRUE;
3339 }
3340#endif
3341
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003342 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 col = 0;
3344#ifdef FEAT_RIGHTLEFT
3345 if (wp->w_p_rl)
3346 {
3347 /* Rightleft window: process the text in the normal direction, but put
3348 * it in current_ScreenLine[] from right to left. Start at the
3349 * rightmost column of the window. */
3350 col = W_WIDTH(wp) - 1;
3351 off += col;
3352 }
3353#endif
3354
3355 /*
3356 * Repeat for the whole displayed line.
3357 */
3358 for (;;)
3359 {
3360 /* Skip this quickly when working on the text. */
3361 if (draw_state != WL_LINE)
3362 {
3363#ifdef FEAT_CMDWIN
3364 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3365 {
3366 draw_state = WL_CMDLINE;
3367 if (cmdwin_type != 0 && wp == curwin)
3368 {
3369 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003371 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 char_attr = hl_attr(HLF_AT);
3373 }
3374 }
3375#endif
3376
3377#ifdef FEAT_FOLDING
3378 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3379 {
3380 draw_state = WL_FOLD;
3381 if (wp->w_p_fdc > 0)
3382 {
3383 /* Draw the 'foldcolumn'. */
3384 fill_foldcolumn(extra, wp, FALSE, lnum);
3385 n_extra = wp->w_p_fdc;
3386 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003387 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 c_extra = NUL;
3389 char_attr = hl_attr(HLF_FC);
3390 }
3391 }
3392#endif
3393
3394#ifdef FEAT_SIGNS
3395 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3396 {
3397 draw_state = WL_SIGN;
3398 /* Show the sign column when there are any signs in this
3399 * buffer or when using Netbeans. */
3400 if (draw_signcolumn(wp)
3401# ifdef FEAT_DIFF
3402 && filler_todo <= 0
3403# endif
3404 )
3405 {
3406 int_u text_sign;
3407# ifdef FEAT_SIGN_ICONS
3408 int_u icon_sign;
3409# endif
3410
3411 /* Draw two cells with the sign value or blank. */
3412 c_extra = ' ';
3413 char_attr = hl_attr(HLF_SC);
3414 n_extra = 2;
3415
3416 if (row == startrow)
3417 {
3418 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3419 SIGN_TEXT);
3420# ifdef FEAT_SIGN_ICONS
3421 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3422 SIGN_ICON);
3423 if (gui.in_use && icon_sign != 0)
3424 {
3425 /* Use the image in this position. */
3426 c_extra = SIGN_BYTE;
3427# ifdef FEAT_NETBEANS_INTG
3428 if (buf_signcount(wp->w_buffer, lnum) > 1)
3429 c_extra = MULTISIGN_BYTE;
3430# endif
3431 char_attr = icon_sign;
3432 }
3433 else
3434# endif
3435 if (text_sign != 0)
3436 {
3437 p_extra = sign_get_text(text_sign);
3438 if (p_extra != NULL)
3439 {
3440 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003441 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
3443 char_attr = sign_get_attr(text_sign, FALSE);
3444 }
3445 }
3446 }
3447 }
3448#endif
3449
3450 if (draw_state == WL_NR - 1 && n_extra == 0)
3451 {
3452 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003453 /* Display the absolute or relative line number. After the
3454 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3455 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 && (row == startrow
3457#ifdef FEAT_DIFF
3458 + filler_lines
3459#endif
3460 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3461 {
3462 /* Draw the line number (empty space after wrapping). */
3463 if (row == startrow
3464#ifdef FEAT_DIFF
3465 + filler_lines
3466#endif
3467 )
3468 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003469 long num;
3470
3471 if (wp->w_p_nu)
3472 /* 'number' */
3473 num = (long)lnum;
3474 else
3475 /* 'relativenumber', don't use negative numbers */
3476 num = (long)abs((int)get_cursor_rel_lnum(wp,
3477 lnum));
3478
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003479 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003480 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (wp->w_skipcol > 0)
3482 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3483 *p_extra = '-';
3484#ifdef FEAT_RIGHTLEFT
3485 if (wp->w_p_rl) /* reverse line numbers */
3486 rl_mirror(extra);
3487#endif
3488 p_extra = extra;
3489 c_extra = NUL;
3490 }
3491 else
3492 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003493 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003495#ifdef FEAT_SYN_HL
3496 /* When 'cursorline' is set highlight the line number of
3497 * the current line differently. */
3498 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3499 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502 }
3503
3504#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3505 if (draw_state == WL_SBR - 1 && n_extra == 0)
3506 {
3507 draw_state = WL_SBR;
3508# ifdef FEAT_DIFF
3509 if (filler_todo > 0)
3510 {
3511 /* Draw "deleted" diff line(s). */
3512 if (char2cells(fill_diff) > 1)
3513 c_extra = '-';
3514 else
3515 c_extra = fill_diff;
3516# ifdef FEAT_RIGHTLEFT
3517 if (wp->w_p_rl)
3518 n_extra = col + 1;
3519 else
3520# endif
3521 n_extra = W_WIDTH(wp) - col;
3522 char_attr = hl_attr(HLF_DED);
3523 }
3524# endif
3525# ifdef FEAT_LINEBREAK
3526 if (*p_sbr != NUL && need_showbreak)
3527 {
3528 /* Draw 'showbreak' at the start of each broken line. */
3529 p_extra = p_sbr;
3530 c_extra = NUL;
3531 n_extra = (int)STRLEN(p_sbr);
3532 char_attr = hl_attr(HLF_AT);
3533 need_showbreak = FALSE;
3534 /* Correct end of highlighted area for 'showbreak',
3535 * required when 'linebreak' is also set. */
3536 if (tocol == vcol)
3537 tocol += n_extra;
3538 }
3539# endif
3540 }
3541#endif
3542
3543 if (draw_state == WL_LINE - 1 && n_extra == 0)
3544 {
3545 draw_state = WL_LINE;
3546 if (saved_n_extra)
3547 {
3548 /* Continue item from end of wrapped line. */
3549 n_extra = saved_n_extra;
3550 c_extra = saved_c_extra;
3551 p_extra = saved_p_extra;
3552 char_attr = saved_char_attr;
3553 }
3554 else
3555 char_attr = 0;
3556 }
3557 }
3558
3559 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003560 if (dollar_vcol != 0 && wp == curwin
3561 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#ifdef FEAT_DIFF
3563 && filler_todo <= 0
3564#endif
3565 )
3566 {
3567 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3568 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003569 /* Pretend we have finished updating the window. Except when
3570 * 'cursorcolumn' is set. */
3571#ifdef FEAT_SYN_HL
3572 if (wp->w_p_cuc)
3573 row = wp->w_cline_row + wp->w_cline_height;
3574 else
3575#endif
3576 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 break;
3578 }
3579
3580 if (draw_state == WL_LINE && area_highlighting)
3581 {
3582 /* handle Visual or match highlighting in this line */
3583 if (vcol == fromcol
3584#ifdef FEAT_MBYTE
3585 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3586 && (*mb_ptr2cells)(ptr) > 1)
3587#endif
3588 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003589 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 && vcol < tocol))
3591 area_attr = attr; /* start highlighting */
3592 else if (area_attr != 0
3593 && (vcol == tocol
3594 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597#ifdef FEAT_SEARCH_EXTRA
3598 if (!n_extra)
3599 {
3600 /*
3601 * Check for start/end of search pattern match.
3602 * After end, check for start/end of next match.
3603 * When another match, have to check for start again.
3604 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003605 * Do this for 'search_hl' and the match list (ordered by
3606 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003608 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003609 cur = wp->w_match_head;
3610 shl_flag = FALSE;
3611 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003613 if (shl_flag == FALSE
3614 && ((cur != NULL
3615 && cur->priority > SEARCH_HL_PRIORITY)
3616 || cur == NULL))
3617 {
3618 shl = &search_hl;
3619 shl_flag = TRUE;
3620 }
3621 else
3622 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 while (shl->rm.regprog != NULL)
3624 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003625 if (shl->startcol != MAXCOL
3626 && v >= (long)shl->startcol
3627 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
3629 shl->attr_cur = shl->attr;
3630 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003631 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
3633 shl->attr_cur = 0;
3634
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 next_search_hl(wp, shl, lnum, (colnr_T)v);
3636
3637 /* Need to get the line again, a multi-line regexp
3638 * may have made it invalid. */
3639 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3640 ptr = line + v;
3641
3642 if (shl->lnum == lnum)
3643 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003644 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003646 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003648 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003650 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 /* highlight empty match, try again after
3653 * it */
3654#ifdef FEAT_MBYTE
3655 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003656 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003657 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 else
3659#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003660 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
3662
3663 /* Loop to check if the match starts at the
3664 * current position */
3665 continue;
3666 }
3667 }
3668 break;
3669 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003670 if (shl != &search_hl && cur != NULL)
3671 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003673
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003674 /* Use attributes from match with highest priority among
3675 * 'search_hl' and the match list. */
3676 search_attr = search_hl.attr_cur;
3677 cur = wp->w_match_head;
3678 shl_flag = FALSE;
3679 while (cur != NULL || shl_flag == FALSE)
3680 {
3681 if (shl_flag == FALSE
3682 && ((cur != NULL
3683 && cur->priority > SEARCH_HL_PRIORITY)
3684 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003685 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003686 shl = &search_hl;
3687 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003688 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003689 else
3690 shl = &cur->hl;
3691 if (shl->attr_cur != 0)
3692 search_attr = shl->attr_cur;
3693 if (shl != &search_hl && cur != NULL)
3694 cur = cur->next;
3695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697#endif
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003700 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003702 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3703 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003705 if (diff_hlf == HLF_TXD && ptr - line > change_end
3706 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003708 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
3710#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003711
3712 /* Decide which of the highlight attributes to use. */
3713 attr_pri = TRUE;
3714 if (area_attr != 0)
3715 char_attr = area_attr;
3716 else if (search_attr != 0)
3717 char_attr = search_attr;
3718#ifdef LINE_ATTR
3719 /* Use line_attr when not in the Visual or 'incsearch' area
3720 * (area_attr may be 0 when "noinvcur" is set). */
3721 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003722 || vcol < fromcol || vcol_prev < fromcol_prev
3723 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003724 char_attr = line_attr;
3725#endif
3726 else
3727 {
3728 attr_pri = FALSE;
3729#ifdef FEAT_SYN_HL
3730 if (has_syntax)
3731 char_attr = syntax_attr;
3732 else
3733#endif
3734 char_attr = 0;
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
3738 /*
3739 * Get the next character to put on the screen.
3740 */
3741 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003742 * The "p_extra" points to the extra stuff that is inserted to
3743 * represent special characters (non-printable stuff) and other
3744 * things. When all characters are the same, c_extra is used.
3745 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3746 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3748 */
3749 if (n_extra > 0)
3750 {
3751 if (c_extra != NUL)
3752 {
3753 c = c_extra;
3754#ifdef FEAT_MBYTE
3755 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3756 if (enc_utf8 && (*mb_char2len)(c) > 1)
3757 {
3758 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003759 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003760 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 else
3763 mb_utf8 = FALSE;
3764#endif
3765 }
3766 else
3767 {
3768 c = *p_extra;
3769#ifdef FEAT_MBYTE
3770 if (has_mbyte)
3771 {
3772 mb_c = c;
3773 if (enc_utf8)
3774 {
3775 /* If the UTF-8 character is more than one byte:
3776 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003777 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 mb_utf8 = FALSE;
3779 if (mb_l > n_extra)
3780 mb_l = 1;
3781 else if (mb_l > 1)
3782 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003783 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003785 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787 }
3788 else
3789 {
3790 /* if this is a DBCS character, put it in "mb_c" */
3791 mb_l = MB_BYTE2LEN(c);
3792 if (mb_l >= n_extra)
3793 mb_l = 1;
3794 else if (mb_l > 1)
3795 mb_c = (c << 8) + p_extra[1];
3796 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003797 if (mb_l == 0) /* at the NUL at end-of-line */
3798 mb_l = 1;
3799
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 /* If a double-width char doesn't fit display a '>' in the
3801 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003802 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803# ifdef FEAT_RIGHTLEFT
3804 wp->w_p_rl ? (col <= 0) :
3805# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003806 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 && (*mb_char2cells)(mb_c) == 2)
3808 {
3809 c = '>';
3810 mb_c = c;
3811 mb_l = 1;
3812 mb_utf8 = FALSE;
3813 multi_attr = hl_attr(HLF_AT);
3814 /* put the pointer back to output the double-width
3815 * character at the start of the next line. */
3816 ++n_extra;
3817 --p_extra;
3818 }
3819 else
3820 {
3821 n_extra -= mb_l - 1;
3822 p_extra += mb_l - 1;
3823 }
3824 }
3825#endif
3826 ++p_extra;
3827 }
3828 --n_extra;
3829 }
3830 else
3831 {
3832 /*
3833 * Get a character from the line itself.
3834 */
3835 c = *ptr;
3836#ifdef FEAT_MBYTE
3837 if (has_mbyte)
3838 {
3839 mb_c = c;
3840 if (enc_utf8)
3841 {
3842 /* If the UTF-8 character is more than one byte: Decode it
3843 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003844 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 mb_utf8 = FALSE;
3846 if (mb_l > 1)
3847 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003848 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 /* Overlong encoded ASCII or ASCII with composing char
3850 * is displayed normally, except a NUL. */
3851 if (mb_c < 0x80)
3852 c = mb_c;
3853 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003854
3855 /* At start of the line we can have a composing char.
3856 * Draw it as a space with a composing char. */
3857 if (utf_iscomposing(mb_c))
3858 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003859 int i;
3860
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003861 for (i = Screen_mco - 1; i > 0; --i)
3862 u8cc[i] = u8cc[i - 1];
3863 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003864 mb_c = ' ';
3865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 }
3867
3868 if ((mb_l == 1 && c >= 0x80)
3869 || (mb_l >= 1 && mb_c == 0)
3870 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003871# ifdef UNICODE16
3872 || mb_c >= 0x10000
3873# endif
3874 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
3876 /*
3877 * Illegal UTF-8 byte: display as <xx>.
3878 * Non-BMP character : display as ? or fullwidth ?.
3879 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003880# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
3884 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003885# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if (wp->w_p_rl) /* reverse */
3887 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003890# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 else if (utf_char2cells(mb_c) != 2)
3892 STRCPY(extra, "?");
3893 else
3894 /* 0xff1f in UTF-8: full-width '?' */
3895 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
3898 p_extra = extra;
3899 c = *p_extra;
3900 mb_c = mb_ptr2char_adv(&p_extra);
3901 mb_utf8 = (c >= 0x80);
3902 n_extra = (int)STRLEN(p_extra);
3903 c_extra = NUL;
3904 if (area_attr == 0 && search_attr == 0)
3905 {
3906 n_attr = n_extra + 1;
3907 extra_attr = hl_attr(HLF_8);
3908 saved_attr2 = char_attr; /* save current attr */
3909 }
3910 }
3911 else if (mb_l == 0) /* at the NUL at end-of-line */
3912 mb_l = 1;
3913#ifdef FEAT_ARABIC
3914 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3915 {
3916 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003917 int pc, pc1, nc;
3918 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
3920 /* The idea of what is the previous and next
3921 * character depends on 'rightleft'. */
3922 if (wp->w_p_rl)
3923 {
3924 pc = prev_c;
3925 pc1 = prev_c1;
3926 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003927 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929 else
3930 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003931 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003933 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935 prev_c = mb_c;
3936
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003937 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
3939 else
3940 prev_c = mb_c;
3941#endif
3942 }
3943 else /* enc_dbcs */
3944 {
3945 mb_l = MB_BYTE2LEN(c);
3946 if (mb_l == 0) /* at the NUL at end-of-line */
3947 mb_l = 1;
3948 else if (mb_l > 1)
3949 {
3950 /* We assume a second byte below 32 is illegal.
3951 * Hopefully this is OK for all double-byte encodings!
3952 */
3953 if (ptr[1] >= 32)
3954 mb_c = (c << 8) + ptr[1];
3955 else
3956 {
3957 if (ptr[1] == NUL)
3958 {
3959 /* head byte at end of line */
3960 mb_l = 1;
3961 transchar_nonprint(extra, c);
3962 }
3963 else
3964 {
3965 /* illegal tail byte */
3966 mb_l = 2;
3967 STRCPY(extra, "XX");
3968 }
3969 p_extra = extra;
3970 n_extra = (int)STRLEN(extra) - 1;
3971 c_extra = NUL;
3972 c = *p_extra++;
3973 if (area_attr == 0 && search_attr == 0)
3974 {
3975 n_attr = n_extra + 1;
3976 extra_attr = hl_attr(HLF_8);
3977 saved_attr2 = char_attr; /* save current attr */
3978 }
3979 mb_c = c;
3980 }
3981 }
3982 }
3983 /* If a double-width char doesn't fit display a '>' in the
3984 * last column; the character is displayed at the start of the
3985 * next line. */
3986 if ((
3987# ifdef FEAT_RIGHTLEFT
3988 wp->w_p_rl ? (col <= 0) :
3989# endif
3990 (col >= W_WIDTH(wp) - 1))
3991 && (*mb_char2cells)(mb_c) == 2)
3992 {
3993 c = '>';
3994 mb_c = c;
3995 mb_utf8 = FALSE;
3996 mb_l = 1;
3997 multi_attr = hl_attr(HLF_AT);
3998 /* Put pointer back so that the character will be
3999 * displayed at the start of the next line. */
4000 --ptr;
4001 }
4002 else if (*ptr != NUL)
4003 ptr += mb_l - 1;
4004
4005 /* If a double-width char doesn't fit at the left side display
4006 * a '<' in the first column. */
4007 if (n_skip > 0 && mb_l > 1)
4008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004010 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 c = ' ';
4012 if (area_attr == 0 && search_attr == 0)
4013 {
4014 n_attr = n_extra + 1;
4015 extra_attr = hl_attr(HLF_AT);
4016 saved_attr2 = char_attr; /* save current attr */
4017 }
4018 mb_c = c;
4019 mb_utf8 = FALSE;
4020 mb_l = 1;
4021 }
4022
4023 }
4024#endif
4025 ++ptr;
4026
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004027 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028 if (wp->w_p_list && (c == 160
4029#ifdef FEAT_MBYTE
4030 || (mb_utf8 && mb_c == 160)
4031#endif
4032 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004033 {
4034 c = lcs_nbsp;
4035 if (area_attr == 0 && search_attr == 0)
4036 {
4037 n_attr = 1;
4038 extra_attr = hl_attr(HLF_8);
4039 saved_attr2 = char_attr; /* save current attr */
4040 }
4041#ifdef FEAT_MBYTE
4042 mb_c = c;
4043 if (enc_utf8 && (*mb_char2len)(c) > 1)
4044 {
4045 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004046 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004047 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004048 }
4049 else
4050 mb_utf8 = FALSE;
4051#endif
4052 }
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 if (extra_check)
4055 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004056#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004057 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004058#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004059
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004060#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 /* Get syntax attribute, unless still at the start of the line
4062 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004063 v = (long)(ptr - line);
4064 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
4066 /* Get the syntax attribute for the character. If there
4067 * is an error, disable syntax highlighting. */
4068 save_did_emsg = did_emsg;
4069 did_emsg = FALSE;
4070
Bram Moolenaar217ad922005-03-20 22:37:15 +00004071 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004072# ifdef FEAT_SPELL
4073 has_spell ? &can_spell :
4074# endif
4075 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004078 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004079 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004080 has_syntax = FALSE;
4081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 else
4083 did_emsg = save_did_emsg;
4084
4085 /* Need to get the line again, a multi-line regexp may
4086 * have made it invalid. */
4087 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4088 ptr = line + v;
4089
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004090 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004092 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004093 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004094# ifdef FEAT_CONCEAL
4095 /* no concealing past the end of the line, it interferes
4096 * with line highlighting */
4097 if (c == NUL)
4098 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004099 else
4100 syntax_flags = get_syntax_info(&syntax_id);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004103#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004104
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004105#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004106 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004107 * Only do this when there is no syntax highlighting, the
4108 * @Spell cluster is not used or the current syntax item
4109 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004110 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004111 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004112 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004113# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004114 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004115 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004116# endif
4117 if (c != 0 && (
4118# ifdef FEAT_SYN_HL
4119 !has_syntax ||
4120# endif
4121 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004122 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004123 char_u *prev_ptr, *p;
4124 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004125 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004126# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004127 if (has_mbyte)
4128 {
4129 prev_ptr = ptr - mb_l;
4130 v -= mb_l - 1;
4131 }
4132 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004133# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004134 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004135
4136 /* Use nextline[] if possible, it has the start of the
4137 * next line concatenated. */
4138 if ((prev_ptr - line) - nextlinecol >= 0)
4139 p = nextline + (prev_ptr - line) - nextlinecol;
4140 else
4141 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004142 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004143 len = spell_check(wp, p, &spell_hlf, &cap_col,
4144 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004145 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004146
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004147 /* In Insert mode only highlight a word that
4148 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004149 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004150 && (State & INSERT) != 0
4151 && wp->w_cursor.lnum == lnum
4152 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004153 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004154 && wp->w_cursor.col < (colnr_T)word_end)
4155 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004156 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004157 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004158 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004159
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004160 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004161 && (p - nextline) + len > nextline_idx)
4162 {
4163 /* Remember that the good word continues at the
4164 * start of the next line. */
4165 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004166 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004167 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004168
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004169 /* Turn index into actual attributes. */
4170 if (spell_hlf != HLF_COUNT)
4171 spell_attr = highlight_attr[spell_hlf];
4172
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004173 if (cap_col > 0)
4174 {
4175 if (p != prev_ptr
4176 && (p - nextline) + cap_col >= nextline_idx)
4177 {
4178 /* Remember that the word in the next line
4179 * must start with a capital. */
4180 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004181 cap_col = (int)((p - nextline) + cap_col
4182 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004183 }
4184 else
4185 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004186 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004187 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004188 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004189 }
4190 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004191 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004192 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004193 char_attr = hl_combine_attr(char_attr, spell_attr);
4194 else
4195 char_attr = hl_combine_attr(spell_attr, char_attr);
4196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197#endif
4198#ifdef FEAT_LINEBREAK
4199 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004200 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 */
4202 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004203 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
4205 n_extra = win_lbr_chartabsize(wp, ptr - (
4206# ifdef FEAT_MBYTE
4207 has_mbyte ? mb_l :
4208# endif
4209 1), (colnr_T)vcol, NULL) - 1;
4210 c_extra = ' ';
4211 if (vim_iswhite(c))
4212 c = ' ';
4213 }
4214#endif
4215
4216 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4217 {
4218 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004219 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
4221 n_attr = 1;
4222 extra_attr = hl_attr(HLF_8);
4223 saved_attr2 = char_attr; /* save current attr */
4224 }
4225#ifdef FEAT_MBYTE
4226 mb_c = c;
4227 if (enc_utf8 && (*mb_char2len)(c) > 1)
4228 {
4229 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004230 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004231 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 else
4234 mb_utf8 = FALSE;
4235#endif
4236 }
4237 }
4238
4239 /*
4240 * Handling of non-printable characters.
4241 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004242 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
4244 /*
4245 * when getting a character from the file, we may have to
4246 * turn it into something else on the way to putting it
4247 * into "ScreenLines".
4248 */
4249 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4250 {
4251 /* tab amount depends on current column */
4252 n_extra = (int)wp->w_buffer->b_p_ts
4253 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4254#ifdef FEAT_MBYTE
4255 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4256#endif
4257 if (wp->w_p_list)
4258 {
4259 c = lcs_tab1;
4260 c_extra = lcs_tab2;
4261 n_attr = n_extra + 1;
4262 extra_attr = hl_attr(HLF_8);
4263 saved_attr2 = char_attr; /* save current attr */
4264#ifdef FEAT_MBYTE
4265 mb_c = c;
4266 if (enc_utf8 && (*mb_char2len)(c) > 1)
4267 {
4268 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004269 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004270 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272#endif
4273 }
4274 else
4275 {
4276 c_extra = ' ';
4277 c = ' ';
4278 }
4279 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004280 else if (c == NUL
4281 && ((wp->w_p_list && lcs_eol > 0)
4282 || ((fromcol >= 0 || fromcol_prev >= 0)
4283 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004284#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004285 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004286#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004287 && (
4288# ifdef FEAT_RIGHTLEFT
4289 wp->w_p_rl ? (col >= 0) :
4290# endif
4291 (col < W_WIDTH(wp)))
4292 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004293 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004294 && (colnr_T)vcol == wp->w_virtcol)))
4295 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004297 /* Display a '$' after the line or highlight an extra
4298 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4300 /* For a diff line the highlighting continues after the
4301 * "$". */
4302 if (
4303# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004304 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305# ifdef LINE_ATTR
4306 &&
4307# endif
4308# endif
4309# ifdef LINE_ATTR
4310 line_attr == 0
4311# endif
4312 )
4313#endif
4314 {
4315#ifdef FEAT_VIRTUALEDIT
4316 /* In virtualedit, visual selections may extend
4317 * beyond end of line. */
4318 if (area_highlighting && virtual_active()
4319 && tocol != MAXCOL && vcol < tocol)
4320 n_extra = 0;
4321 else
4322#endif
4323 {
4324 p_extra = at_end_str;
4325 n_extra = 1;
4326 c_extra = NUL;
4327 }
4328 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004329 if (wp->w_p_list)
4330 c = lcs_eol;
4331 else
4332 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 lcs_eol_one = -1;
4334 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004335 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
4337 extra_attr = hl_attr(HLF_AT);
4338 n_attr = 1;
4339 }
4340#ifdef FEAT_MBYTE
4341 mb_c = c;
4342 if (enc_utf8 && (*mb_char2len)(c) > 1)
4343 {
4344 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004345 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004346 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 else
4349 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4350#endif
4351 }
4352 else if (c != NUL)
4353 {
4354 p_extra = transchar(c);
4355#ifdef FEAT_RIGHTLEFT
4356 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4357 rl_mirror(p_extra); /* reverse "<12>" */
4358#endif
4359 n_extra = byte2cells(c) - 1;
4360 c_extra = NUL;
4361 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004362 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
4364 n_attr = n_extra + 1;
4365 extra_attr = hl_attr(HLF_8);
4366 saved_attr2 = char_attr; /* save current attr */
4367 }
4368#ifdef FEAT_MBYTE
4369 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4370#endif
4371 }
4372#ifdef FEAT_VIRTUALEDIT
4373 else if (VIsual_active
4374 && (VIsual_mode == Ctrl_V
4375 || VIsual_mode == 'v')
4376 && virtual_active()
4377 && tocol != MAXCOL
4378 && vcol < tocol
4379 && (
4380# ifdef FEAT_RIGHTLEFT
4381 wp->w_p_rl ? (col >= 0) :
4382# endif
4383 (col < W_WIDTH(wp))))
4384 {
4385 c = ' ';
4386 --ptr; /* put it back at the NUL */
4387 }
4388#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004389#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 else if ((
4391# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004392 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 ) && (
4396# ifdef FEAT_RIGHTLEFT
4397 wp->w_p_rl ? (col >= 0) :
4398# endif
4399 (col < W_WIDTH(wp))))
4400 {
4401 /* Highlight until the right side of the window */
4402 c = ' ';
4403 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004404
4405 /* Remember we do the char for line highlighting. */
4406 ++did_line_attr;
4407
4408 /* don't do search HL for the rest of the line */
4409 if (line_attr != 0 && char_attr == search_attr && col > 0)
4410 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411# ifdef FEAT_DIFF
4412 if (diff_hlf == HLF_TXD)
4413 {
4414 diff_hlf = HLF_CHD;
4415 if (attr == 0 || char_attr != attr)
4416 char_attr = hl_attr(diff_hlf);
4417 }
4418# endif
4419 }
4420#endif
4421 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004422
4423#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004424 if ( wp->w_p_cole > 0
4425 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4426 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004427 && (syntax_flags & HL_CONCEAL) != 0
4428 && !lnum_in_visual_area)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004429 {
4430 char_attr = conceal_attr;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004431 if (prev_syntax_id != syntax_id
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004432 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4433 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004434 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004435 /* First time at this concealed item: display one
4436 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004437 if (syn_get_sub_char() != NUL)
4438 c = syn_get_sub_char();
4439 else if (lcs_conceal != NUL)
4440 c = lcs_conceal;
4441 else
4442 c = ' ';
4443
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004444 prev_syntax_id = syntax_id;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004445
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004446 if (n_extra > 0)
4447 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004448 vcol += n_extra;
4449 if (wp->w_p_wrap && n_extra > 0)
4450 {
4451# ifdef FEAT_RIGHTLEFT
4452 if (wp->w_p_rl)
4453 {
4454 col -= n_extra;
4455 boguscols -= n_extra;
4456 }
4457 else
4458# endif
4459 {
4460 boguscols += n_extra;
4461 col += n_extra;
4462 }
4463 }
4464 n_extra = 0;
4465 n_attr = 0;
4466 }
4467 else if (n_skip == 0)
4468 {
4469 is_concealing = TRUE;
4470 n_skip = 1;
4471 }
4472# ifdef FEAT_MBYTE
4473 mb_c = c;
4474 if (enc_utf8 && (*mb_char2len)(c) > 1)
4475 {
4476 mb_utf8 = TRUE;
4477 u8cc[0] = 0;
4478 c = 0xc0;
4479 }
4480 else
4481 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4482# endif
4483 }
4484 else
4485 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004486 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004487 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004488 }
4489#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004492#ifdef FEAT_CONCEAL
4493 /* In the cursor line and we may be concealing characters: correct
4494 * the cursor column when we reach its position. */
4495 if (!did_wcol && wp == curwin && lnum == wp->w_cursor.lnum
4496 && conceal_cursor_line(wp)
4497 && (int)wp->w_virtcol <= vcol + n_skip)
4498 {
4499 wp->w_wcol = col - boguscols;
4500 did_wcol = TRUE;
4501 }
4502#endif
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 /* Don't override visual selection highlighting. */
4505 if (n_attr > 0
4506 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004507 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 char_attr = extra_attr;
4509
Bram Moolenaar81695252004-12-29 20:58:21 +00004510#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 /* XIM don't send preedit_start and preedit_end, but they send
4512 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4513 * im_is_preediting() here. */
4514 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004515 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 && (State & INSERT)
4517 && !p_imdisable
4518 && im_is_preediting()
4519 && draw_state == WL_LINE)
4520 {
4521 colnr_T tcol;
4522
4523 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004524 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 else
4526 tcol = preedit_end_col;
4527 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4528 {
4529 if (feedback_old_attr < 0)
4530 {
4531 feedback_col = 0;
4532 feedback_old_attr = char_attr;
4533 }
4534 char_attr = im_get_feedback_attr(feedback_col);
4535 if (char_attr < 0)
4536 char_attr = feedback_old_attr;
4537 feedback_col++;
4538 }
4539 else if (feedback_old_attr >= 0)
4540 {
4541 char_attr = feedback_old_attr;
4542 feedback_old_attr = -1;
4543 feedback_col = 0;
4544 }
4545 }
4546#endif
4547 /*
4548 * Handle the case where we are in column 0 but not on the first
4549 * character of the line and the user wants us to show us a
4550 * special character (via 'listchars' option "precedes:<char>".
4551 */
4552 if (lcs_prec_todo != NUL
4553 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4554#ifdef FEAT_DIFF
4555 && filler_todo <= 0
4556#endif
4557 && draw_state > WL_NR
4558 && c != NUL)
4559 {
4560 c = lcs_prec;
4561 lcs_prec_todo = NUL;
4562#ifdef FEAT_MBYTE
4563 mb_c = c;
4564 if (enc_utf8 && (*mb_char2len)(c) > 1)
4565 {
4566 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004567 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004568 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 else
4571 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4572#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004573 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
4575 saved_attr3 = char_attr; /* save current attr */
4576 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4577 n_attr3 = 1;
4578 }
4579 }
4580
4581 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004582 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004584 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004585#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004586 || did_line_attr == 1
4587#endif
4588 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004590#ifdef FEAT_SEARCH_EXTRA
4591 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004592
4593 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004594 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004595 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004596#endif
4597
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004598 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 * highlight match at end of line. If it's beyond the last
4600 * char on the screen, just overwrite that one (tricky!) Not
4601 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004602#ifdef FEAT_SEARCH_EXTRA
4603 prevcol_hl_flag = FALSE;
4604 if (prevcol == (long)search_hl.startcol)
4605 prevcol_hl_flag = TRUE;
4606 else
4607 {
4608 cur = wp->w_match_head;
4609 while (cur != NULL)
4610 {
4611 if (prevcol == (long)cur->hl.startcol)
4612 {
4613 prevcol_hl_flag = TRUE;
4614 break;
4615 }
4616 cur = cur->next;
4617 }
4618 }
4619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004621 && ((area_attr != 0 && vcol == fromcol
4622#ifdef FEAT_VISUAL
4623 && (VIsual_mode != Ctrl_V
4624 || lnum == VIsual.lnum
4625 || lnum == curwin->w_cursor.lnum)
4626#endif
4627 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628#ifdef FEAT_SEARCH_EXTRA
4629 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004630 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004631# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004632 && did_line_attr <= 1
4633# endif
4634 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635#endif
4636 ))
4637 {
4638 int n = 0;
4639
4640#ifdef FEAT_RIGHTLEFT
4641 if (wp->w_p_rl)
4642 {
4643 if (col < 0)
4644 n = 1;
4645 }
4646 else
4647#endif
4648 {
4649 if (col >= W_WIDTH(wp))
4650 n = -1;
4651 }
4652 if (n != 0)
4653 {
4654 /* At the window boundary, highlight the last character
4655 * instead (better than nothing). */
4656 off += n;
4657 col += n;
4658 }
4659 else
4660 {
4661 /* Add a blank character to highlight. */
4662 ScreenLines[off] = ' ';
4663#ifdef FEAT_MBYTE
4664 if (enc_utf8)
4665 ScreenLinesUC[off] = 0;
4666#endif
4667 }
4668#ifdef FEAT_SEARCH_EXTRA
4669 if (area_attr == 0)
4670 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004671 /* Use attributes from match with highest priority among
4672 * 'search_hl' and the match list. */
4673 char_attr = search_hl.attr;
4674 cur = wp->w_match_head;
4675 shl_flag = FALSE;
4676 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004677 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004678 if (shl_flag == FALSE
4679 && ((cur != NULL
4680 && cur->priority > SEARCH_HL_PRIORITY)
4681 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004682 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004683 shl = &search_hl;
4684 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004685 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004686 else
4687 shl = &cur->hl;
4688 if ((ptr - line) - 1 == (long)shl->startcol)
4689 char_attr = shl->attr;
4690 if (shl != &search_hl && cur != NULL)
4691 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694#endif
4695 ScreenAttrs[off] = char_attr;
4696#ifdef FEAT_RIGHTLEFT
4697 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004700 --off;
4701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 else
4703#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004706 ++off;
4707 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004708 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004709#ifdef FEAT_SYN_HL
4710 eol_hl_off = 1;
4711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714
Bram Moolenaar91170f82006-05-05 21:15:17 +00004715 /*
4716 * At end of the text line.
4717 */
4718 if (c == NUL)
4719 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004720#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004721 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4722 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004723 {
4724 /* highlight last char after line */
4725 --col;
4726 --off;
4727 --vcol;
4728 }
4729
Bram Moolenaar1a384422010-07-14 19:53:30 +02004730 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004731 if (wp->w_p_wrap)
4732 v = wp->w_skipcol;
4733 else
4734 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004735
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004736 /* check if line ends before left margin */
4737 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004738 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004739#ifdef FEAT_CONCEAL
4740 /* Get rid of the boguscols now, we want to draw until the right
4741 * edge for 'cursorcolumn'. */
4742 col -= boguscols;
4743 boguscols = 0;
4744#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004745
4746 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004747 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004748
4749 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004750 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4751 && (int)wp->w_virtcol <
4752 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004753 && lnum != wp->w_cursor.lnum)
4754 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004755# ifdef FEAT_RIGHTLEFT
4756 && !wp->w_p_rl
4757# endif
4758 )
4759 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004760 int rightmost_vcol = 0;
4761 int i;
4762
4763 if (wp->w_p_cuc)
4764 rightmost_vcol = wp->w_virtcol;
4765 if (draw_color_col)
4766 /* determine rightmost colorcolumn to possibly draw */
4767 for (i = 0; color_cols[i] >= 0; ++i)
4768 if (rightmost_vcol < color_cols[i])
4769 rightmost_vcol = color_cols[i];
4770
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004771 while (col < W_WIDTH(wp))
4772 {
4773 ScreenLines[off] = ' ';
4774#ifdef FEAT_MBYTE
4775 if (enc_utf8)
4776 ScreenLinesUC[off] = 0;
4777#endif
4778 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004779 if (draw_color_col)
4780 draw_color_col = advance_color_col(VCOL_HLC,
4781 &color_cols);
4782
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004783 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004784 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004785 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004786 ScreenAttrs[off++] = hl_attr(HLF_MC);
4787 else
4788 ScreenAttrs[off++] = 0;
4789
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004790 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004791 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004792
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004793 ++vcol;
4794 }
4795 }
4796#endif
4797
Bram Moolenaar860cae12010-06-05 23:22:07 +02004798 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4799 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 row++;
4801
4802 /*
4803 * Update w_cline_height and w_cline_folded if the cursor line was
4804 * updated (saves a call to plines() later).
4805 */
4806 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4807 {
4808 curwin->w_cline_row = startrow;
4809 curwin->w_cline_height = row - startrow;
4810#ifdef FEAT_FOLDING
4811 curwin->w_cline_folded = FALSE;
4812#endif
4813 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4814 }
4815
4816 break;
4817 }
4818
4819 /* line continues beyond line end */
4820 if (lcs_ext
4821 && !wp->w_p_wrap
4822#ifdef FEAT_DIFF
4823 && filler_todo <= 0
4824#endif
4825 && (
4826#ifdef FEAT_RIGHTLEFT
4827 wp->w_p_rl ? col == 0 :
4828#endif
4829 col == W_WIDTH(wp) - 1)
4830 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004831 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4833 {
4834 c = lcs_ext;
4835 char_attr = hl_attr(HLF_AT);
4836#ifdef FEAT_MBYTE
4837 mb_c = c;
4838 if (enc_utf8 && (*mb_char2len)(c) > 1)
4839 {
4840 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004841 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004842 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
4844 else
4845 mb_utf8 = FALSE;
4846#endif
4847 }
4848
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004849#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004850 /* advance to the next 'colorcolumn' */
4851 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004852 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004853
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004854 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004855 * highlight the cursor position itself.
4856 * Also highlight the 'colorcolumn' if it is different than
4857 * 'cursorcolumn' */
4858 vcol_save_attr = -1;
4859 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004860 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004861 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004862 && lnum != wp->w_cursor.lnum)
4863 {
4864 vcol_save_attr = char_attr;
4865 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4866 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004867 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004868 {
4869 vcol_save_attr = char_attr;
4870 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4871 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004872 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004873#endif
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 /*
4876 * Store character to be displayed.
4877 * Skip characters that are left of the screen for 'nowrap'.
4878 */
4879 vcol_prev = vcol;
4880 if (draw_state < WL_LINE || n_skip <= 0)
4881 {
4882 /*
4883 * Store the character.
4884 */
4885#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4886 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4887 {
4888 /* A double-wide character is: put first halve in left cell. */
4889 --off;
4890 --col;
4891 }
4892#endif
4893 ScreenLines[off] = c;
4894#ifdef FEAT_MBYTE
4895 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004896 {
4897 if ((mb_c & 0xff00) == 0x8e00)
4898 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 else if (enc_utf8)
4902 {
4903 if (mb_utf8)
4904 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004905 int i;
4906
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004908 if ((c & 0xff) == 0)
4909 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004910 for (i = 0; i < Screen_mco; ++i)
4911 {
4912 ScreenLinesC[i][off] = u8cc[i];
4913 if (u8cc[i] == 0)
4914 break;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else
4918 ScreenLinesUC[off] = 0;
4919 }
4920 if (multi_attr)
4921 {
4922 ScreenAttrs[off] = multi_attr;
4923 multi_attr = 0;
4924 }
4925 else
4926#endif
4927 ScreenAttrs[off] = char_attr;
4928
4929#ifdef FEAT_MBYTE
4930 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4931 {
4932 /* Need to fill two screen columns. */
4933 ++off;
4934 ++col;
4935 if (enc_utf8)
4936 /* UTF-8: Put a 0 in the second screen char. */
4937 ScreenLines[off] = 0;
4938 else
4939 /* DBCS: Put second byte in the second screen char. */
4940 ScreenLines[off] = mb_c & 0xff;
4941 ++vcol;
4942 /* When "tocol" is halfway a character, set it to the end of
4943 * the character, otherwise highlighting won't stop. */
4944 if (tocol == vcol)
4945 ++tocol;
4946#ifdef FEAT_RIGHTLEFT
4947 if (wp->w_p_rl)
4948 {
4949 /* now it's time to backup one cell */
4950 --off;
4951 --col;
4952 }
4953#endif
4954 }
4955#endif
4956#ifdef FEAT_RIGHTLEFT
4957 if (wp->w_p_rl)
4958 {
4959 --off;
4960 --col;
4961 }
4962 else
4963#endif
4964 {
4965 ++off;
4966 ++col;
4967 }
4968 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004969#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004970 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004971 {
4972 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004973 ++vcol_off;
4974 if (n_extra > 0)
4975 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004976 if (wp->w_p_wrap)
4977 {
4978 /*
4979 * Special voodoo required if 'wrap' is on.
4980 *
4981 * Advance the column indicator to force the line
4982 * drawing to wrap early. This will make the line
4983 * take up the same screen space when parts are concealed,
4984 * so that cursor line computations aren't messed up.
4985 *
4986 * To avoid the fictitious advance of 'col' causing
4987 * trailing junk to be written out of the screen line
4988 * we are building, 'boguscols' keeps track of the number
4989 * of bad columns we have advanced.
4990 */
4991 if (n_extra > 0)
4992 {
4993 vcol += n_extra;
4994# ifdef FEAT_RIGHTLEFT
4995 if (wp->w_p_rl)
4996 {
4997 col -= n_extra;
4998 boguscols -= n_extra;
4999 }
5000 else
5001# endif
5002 {
5003 col += n_extra;
5004 boguscols += n_extra;
5005 }
5006 n_extra = 0;
5007 n_attr = 0;
5008 }
5009
5010
5011# ifdef FEAT_MBYTE
5012 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5013 {
5014 /* Need to fill two screen columns. */
5015# ifdef FEAT_RIGHTLEFT
5016 if (wp->w_p_rl)
5017 {
5018 --boguscols;
5019 --col;
5020 }
5021 else
5022# endif
5023 {
5024 ++boguscols;
5025 ++col;
5026 }
5027 }
5028# endif
5029
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 else
5044 {
5045 if (n_extra > 0)
5046 {
5047 vcol += n_extra;
5048 n_extra = 0;
5049 n_attr = 0;
5050 }
5051 }
5052
5053 }
5054#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 else
5056 --n_skip;
5057
Bram Moolenaar64486672010-05-16 15:46:46 +02005058 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5059 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005060 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061#ifdef FEAT_DIFF
5062 && filler_todo <= 0
5063#endif
5064 )
5065 ++vcol;
5066
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005067#ifdef FEAT_SYN_HL
5068 if (vcol_save_attr >= 0)
5069 char_attr = vcol_save_attr;
5070#endif
5071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 /* restore attributes after "predeces" in 'listchars' */
5073 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5074 char_attr = saved_attr3;
5075
5076 /* restore attributes after last 'listchars' or 'number' char */
5077 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5078 char_attr = saved_attr2;
5079
5080 /*
5081 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005082 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
5084 if ((
5085#ifdef FEAT_RIGHTLEFT
5086 wp->w_p_rl ? (col < 0) :
5087#endif
5088 (col >= W_WIDTH(wp)))
5089 && (*ptr != NUL
5090#ifdef FEAT_DIFF
5091 || filler_todo > 0
5092#endif
5093 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
5094 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5095 )
5096 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005097#ifdef FEAT_CONCEAL
5098 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5099 (int)W_WIDTH(wp), wp->w_p_rl);
5100 boguscols = 0;
5101#else
5102 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5103 (int)W_WIDTH(wp), wp->w_p_rl);
5104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 ++row;
5106 ++screen_row;
5107
5108 /* When not wrapping and finished diff lines, or when displayed
5109 * '$' and highlighting until last column, break here. */
5110 if ((!wp->w_p_wrap
5111#ifdef FEAT_DIFF
5112 && filler_todo <= 0
5113#endif
5114 ) || lcs_eol_one == -1)
5115 break;
5116
5117 /* When the window is too narrow draw all "@" lines. */
5118 if (draw_state != WL_LINE
5119#ifdef FEAT_DIFF
5120 && filler_todo <= 0
5121#endif
5122 )
5123 {
5124 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5125#ifdef FEAT_VERTSPLIT
5126 draw_vsep_win(wp, row);
5127#endif
5128 row = endrow;
5129 }
5130
5131 /* When line got too long for screen break here. */
5132 if (row == endrow)
5133 {
5134 ++row;
5135 break;
5136 }
5137
5138 if (screen_cur_row == screen_row - 1
5139#ifdef FEAT_DIFF
5140 && filler_todo <= 0
5141#endif
5142 && W_WIDTH(wp) == Columns)
5143 {
5144 /* Remember that the line wraps, used for modeless copy. */
5145 LineWraps[screen_row - 1] = TRUE;
5146
5147 /*
5148 * Special trick to make copy/paste of wrapped lines work with
5149 * xterm/screen: write an extra character beyond the end of
5150 * the line. This will work with all terminal types
5151 * (regardless of the xn,am settings).
5152 * Only do this on a fast tty.
5153 * Only do this if the cursor is on the current line
5154 * (something has been written in it).
5155 * Don't do this for the GUI.
5156 * Don't do this for double-width characters.
5157 * Don't do this for a window not at the right screen border.
5158 */
5159 if (p_tf
5160#ifdef FEAT_GUI
5161 && !gui.in_use
5162#endif
5163#ifdef FEAT_MBYTE
5164 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005165 && ((*mb_off2cells)(LineOffset[screen_row],
5166 LineOffset[screen_row] + screen_Columns)
5167 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005169 + (int)Columns - 2,
5170 LineOffset[screen_row] + screen_Columns)
5171 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172#endif
5173 )
5174 {
5175 /* First make sure we are at the end of the screen line,
5176 * then output the same character again to let the
5177 * terminal know about the wrap. If the terminal doesn't
5178 * auto-wrap, we overwrite the character. */
5179 if (screen_cur_col != W_WIDTH(wp))
5180 screen_char(LineOffset[screen_row - 1]
5181 + (unsigned)Columns - 1,
5182 screen_row - 1, (int)(Columns - 1));
5183
5184#ifdef FEAT_MBYTE
5185 /* When there is a multi-byte character, just output a
5186 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005187 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5188 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 out_char(' ');
5190 else
5191#endif
5192 out_char(ScreenLines[LineOffset[screen_row - 1]
5193 + (Columns - 1)]);
5194 /* force a redraw of the first char on the next line */
5195 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5196 screen_start(); /* don't know where cursor is now */
5197 }
5198 }
5199
5200 col = 0;
5201 off = (unsigned)(current_ScreenLine - ScreenLines);
5202#ifdef FEAT_RIGHTLEFT
5203 if (wp->w_p_rl)
5204 {
5205 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5206 off += col;
5207 }
5208#endif
5209
5210 /* reset the drawing state for the start of a wrapped line */
5211 draw_state = WL_START;
5212 saved_n_extra = n_extra;
5213 saved_p_extra = p_extra;
5214 saved_c_extra = c_extra;
5215 saved_char_attr = char_attr;
5216 n_extra = 0;
5217 lcs_prec_todo = lcs_prec;
5218#ifdef FEAT_LINEBREAK
5219# ifdef FEAT_DIFF
5220 if (filler_todo <= 0)
5221# endif
5222 need_showbreak = TRUE;
5223#endif
5224#ifdef FEAT_DIFF
5225 --filler_todo;
5226 /* When the filler lines are actually below the last line of the
5227 * file, don't draw the line itself, break here. */
5228 if (filler_todo == 0 && wp->w_botfill)
5229 break;
5230#endif
5231 }
5232
5233 } /* for every character in the line */
5234
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005235#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005236 /* After an empty line check first word for capital. */
5237 if (*skipwhite(line) == NUL)
5238 {
5239 capcol_lnum = lnum + 1;
5240 cap_col = 0;
5241 }
5242#endif
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 return row;
5245}
5246
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005247#ifdef FEAT_MBYTE
5248static int comp_char_differs __ARGS((int, int));
5249
5250/*
5251 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005252 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005253 */
5254 static int
5255comp_char_differs(off_from, off_to)
5256 int off_from;
5257 int off_to;
5258{
5259 int i;
5260
5261 for (i = 0; i < Screen_mco; ++i)
5262 {
5263 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5264 return TRUE;
5265 if (ScreenLinesC[i][off_from] == 0)
5266 break;
5267 }
5268 return FALSE;
5269}
5270#endif
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272/*
5273 * Check whether the given character needs redrawing:
5274 * - the (first byte of the) character is different
5275 * - the attributes are different
5276 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005277 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 */
5279 static int
5280char_needs_redraw(off_from, off_to, cols)
5281 int off_from;
5282 int off_to;
5283 int cols;
5284{
5285 if (cols > 0
5286 && ((ScreenLines[off_from] != ScreenLines[off_to]
5287 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5288
5289#ifdef FEAT_MBYTE
5290 || (enc_dbcs != 0
5291 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5292 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5293 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5294 : (cols > 1 && ScreenLines[off_from + 1]
5295 != ScreenLines[off_to + 1])))
5296 || (enc_utf8
5297 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5298 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005299 && comp_char_differs(off_from, off_to))
5300 || (cols > 1 && ScreenLines[off_from + 1]
5301 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302#endif
5303 ))
5304 return TRUE;
5305 return FALSE;
5306}
5307
5308/*
5309 * Move one "cooked" screen line to the screen, but only the characters that
5310 * have actually changed. Handle insert/delete character.
5311 * "coloff" gives the first column on the screen for this line.
5312 * "endcol" gives the columns where valid characters are.
5313 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5314 * needs to be cleared, negative otherwise.
5315 * "rlflag" is TRUE in a rightleft window:
5316 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5317 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5318 */
5319 static void
5320screen_line(row, coloff, endcol, clear_width
5321#ifdef FEAT_RIGHTLEFT
5322 , rlflag
5323#endif
5324 )
5325 int row;
5326 int coloff;
5327 int endcol;
5328 int clear_width;
5329#ifdef FEAT_RIGHTLEFT
5330 int rlflag;
5331#endif
5332{
5333 unsigned off_from;
5334 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005335#ifdef FEAT_MBYTE
5336 unsigned max_off_from;
5337 unsigned max_off_to;
5338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 int col = 0;
5340#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5341 int hl;
5342#endif
5343 int force = FALSE; /* force update rest of the line */
5344 int redraw_this /* bool: does character need redraw? */
5345#ifdef FEAT_GUI
5346 = TRUE /* For GUI when while-loop empty */
5347#endif
5348 ;
5349 int redraw_next; /* redraw_this for next character */
5350#ifdef FEAT_MBYTE
5351 int clear_next = FALSE;
5352 int char_cells; /* 1: normal char */
5353 /* 2: occupies two display cells */
5354# define CHAR_CELLS char_cells
5355#else
5356# define CHAR_CELLS 1
5357#endif
5358
5359# ifdef FEAT_CLIPBOARD
5360 clip_may_clear_selection(row, row);
5361# endif
5362
5363 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5364 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005365#ifdef FEAT_MBYTE
5366 max_off_from = off_from + screen_Columns;
5367 max_off_to = LineOffset[row] + screen_Columns;
5368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370#ifdef FEAT_RIGHTLEFT
5371 if (rlflag)
5372 {
5373 /* Clear rest first, because it's left of the text. */
5374 if (clear_width > 0)
5375 {
5376 while (col <= endcol && ScreenLines[off_to] == ' '
5377 && ScreenAttrs[off_to] == 0
5378# ifdef FEAT_MBYTE
5379 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5380# endif
5381 )
5382 {
5383 ++off_to;
5384 ++col;
5385 }
5386 if (col <= endcol)
5387 screen_fill(row, row + 1, col + coloff,
5388 endcol + coloff + 1, ' ', ' ', 0);
5389 }
5390 col = endcol + 1;
5391 off_to = LineOffset[row] + col + coloff;
5392 off_from += col;
5393 endcol = (clear_width > 0 ? clear_width : -clear_width);
5394 }
5395#endif /* FEAT_RIGHTLEFT */
5396
5397 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5398
5399 while (col < endcol)
5400 {
5401#ifdef FEAT_MBYTE
5402 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005403 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 else
5405 char_cells = 1;
5406#endif
5407
5408 redraw_this = redraw_next;
5409 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5410 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5411
5412#ifdef FEAT_GUI
5413 /* If the next character was bold, then redraw the current character to
5414 * remove any pixels that might have spilt over into us. This only
5415 * happens in the GUI.
5416 */
5417 if (redraw_next && gui.in_use)
5418 {
5419 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005420 if (hl > HL_ALL)
5421 hl = syn_attr2attr(hl);
5422 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 redraw_this = TRUE;
5424 }
5425#endif
5426
5427 if (redraw_this)
5428 {
5429 /*
5430 * Special handling when 'xs' termcap flag set (hpterm):
5431 * Attributes for characters are stored at the position where the
5432 * cursor is when writing the highlighting code. The
5433 * start-highlighting code must be written with the cursor on the
5434 * first highlighted character. The stop-highlighting code must
5435 * be written with the cursor just after the last highlighted
5436 * character.
5437 * Overwriting a character doesn't remove it's highlighting. Need
5438 * to clear the rest of the line, and force redrawing it
5439 * completely.
5440 */
5441 if ( p_wiv
5442 && !force
5443#ifdef FEAT_GUI
5444 && !gui.in_use
5445#endif
5446 && ScreenAttrs[off_to] != 0
5447 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5448 {
5449 /*
5450 * Need to remove highlighting attributes here.
5451 */
5452 windgoto(row, col + coloff);
5453 out_str(T_CE); /* clear rest of this screen line */
5454 screen_start(); /* don't know where cursor is now */
5455 force = TRUE; /* force redraw of rest of the line */
5456 redraw_next = TRUE; /* or else next char would miss out */
5457
5458 /*
5459 * If the previous character was highlighted, need to stop
5460 * highlighting at this character.
5461 */
5462 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5463 {
5464 screen_attr = ScreenAttrs[off_to - 1];
5465 term_windgoto(row, col + coloff);
5466 screen_stop_highlight();
5467 }
5468 else
5469 screen_attr = 0; /* highlighting has stopped */
5470 }
5471#ifdef FEAT_MBYTE
5472 if (enc_dbcs != 0)
5473 {
5474 /* Check if overwriting a double-byte with a single-byte or
5475 * the other way around requires another character to be
5476 * redrawn. For UTF-8 this isn't needed, because comparing
5477 * ScreenLinesUC[] is sufficient. */
5478 if (char_cells == 1
5479 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005480 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 /* Writing a single-cell character over a double-cell
5483 * character: need to redraw the next cell. */
5484 ScreenLines[off_to + 1] = 0;
5485 redraw_next = TRUE;
5486 }
5487 else if (char_cells == 2
5488 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005489 && (*mb_off2cells)(off_to, max_off_to) == 1
5490 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
5492 /* Writing the second half of a double-cell character over
5493 * a double-cell character: need to redraw the second
5494 * cell. */
5495 ScreenLines[off_to + 2] = 0;
5496 redraw_next = TRUE;
5497 }
5498
5499 if (enc_dbcs == DBCS_JPNU)
5500 ScreenLines2[off_to] = ScreenLines2[off_from];
5501 }
5502 /* When writing a single-width character over a double-width
5503 * character and at the end of the redrawn text, need to clear out
5504 * the right halve of the old character.
5505 * Also required when writing the right halve of a double-width
5506 * char over the left halve of an existing one. */
5507 if (has_mbyte && col + char_cells == endcol
5508 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005509 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005511 && (*mb_off2cells)(off_to, max_off_to) == 1
5512 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 clear_next = TRUE;
5514#endif
5515
5516 ScreenLines[off_to] = ScreenLines[off_from];
5517#ifdef FEAT_MBYTE
5518 if (enc_utf8)
5519 {
5520 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5521 if (ScreenLinesUC[off_from] != 0)
5522 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005523 int i;
5524
5525 for (i = 0; i < Screen_mco; ++i)
5526 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 }
5528 }
5529 if (char_cells == 2)
5530 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5531#endif
5532
5533#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005534 /* The bold trick makes a single column of pixels appear in the
5535 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 * character should be redrawn too. This happens for our own GUI
5537 * and for some xterms. */
5538 if (
5539# ifdef FEAT_GUI
5540 gui.in_use
5541# endif
5542# if defined(FEAT_GUI) && defined(UNIX)
5543 ||
5544# endif
5545# ifdef UNIX
5546 term_is_xterm
5547# endif
5548 )
5549 {
5550 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005551 if (hl > HL_ALL)
5552 hl = syn_attr2attr(hl);
5553 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 redraw_next = TRUE;
5555 }
5556#endif
5557 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5558#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005559 /* For simplicity set the attributes of second half of a
5560 * double-wide character equal to the first half. */
5561 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005563
5564 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 else
5567#endif
5568 screen_char(off_to, row, col + coloff);
5569 }
5570 else if ( p_wiv
5571#ifdef FEAT_GUI
5572 && !gui.in_use
5573#endif
5574 && col + coloff > 0)
5575 {
5576 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5577 {
5578 /*
5579 * Don't output stop-highlight when moving the cursor, it will
5580 * stop the highlighting when it should continue.
5581 */
5582 screen_attr = 0;
5583 }
5584 else if (screen_attr != 0)
5585 screen_stop_highlight();
5586 }
5587
5588 off_to += CHAR_CELLS;
5589 off_from += CHAR_CELLS;
5590 col += CHAR_CELLS;
5591 }
5592
5593#ifdef FEAT_MBYTE
5594 if (clear_next)
5595 {
5596 /* Clear the second half of a double-wide character of which the left
5597 * half was overwritten with a single-wide character. */
5598 ScreenLines[off_to] = ' ';
5599 if (enc_utf8)
5600 ScreenLinesUC[off_to] = 0;
5601 screen_char(off_to, row, col + coloff);
5602 }
5603#endif
5604
5605 if (clear_width > 0
5606#ifdef FEAT_RIGHTLEFT
5607 && !rlflag
5608#endif
5609 )
5610 {
5611#ifdef FEAT_GUI
5612 int startCol = col;
5613#endif
5614
5615 /* blank out the rest of the line */
5616 while (col < clear_width && ScreenLines[off_to] == ' '
5617 && ScreenAttrs[off_to] == 0
5618#ifdef FEAT_MBYTE
5619 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5620#endif
5621 )
5622 {
5623 ++off_to;
5624 ++col;
5625 }
5626 if (col < clear_width)
5627 {
5628#ifdef FEAT_GUI
5629 /*
5630 * In the GUI, clearing the rest of the line may leave pixels
5631 * behind if the first character cleared was bold. Some bold
5632 * fonts spill over the left. In this case we redraw the previous
5633 * character too. If we didn't skip any blanks above, then we
5634 * only redraw if the character wasn't already redrawn anyway.
5635 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005636 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
5638 hl = ScreenAttrs[off_to];
5639 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005640 {
5641 int prev_cells = 1;
5642# ifdef FEAT_MBYTE
5643 if (enc_utf8)
5644 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5645 * that its width is 2. */
5646 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5647 else if (enc_dbcs != 0)
5648 {
5649 /* find previous character by counting from first
5650 * column and get its width. */
5651 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005652 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005653
5654 while (off < off_to)
5655 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005656 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005657 off += prev_cells;
5658 }
5659 }
5660
5661 if (enc_dbcs != 0 && prev_cells > 1)
5662 screen_char_2(off_to - prev_cells, row,
5663 col + coloff - prev_cells);
5664 else
5665# endif
5666 screen_char(off_to - prev_cells, row,
5667 col + coloff - prev_cells);
5668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670#endif
5671 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5672 ' ', ' ', 0);
5673#ifdef FEAT_VERTSPLIT
5674 off_to += clear_width - col;
5675 col = clear_width;
5676#endif
5677 }
5678 }
5679
5680 if (clear_width > 0)
5681 {
5682#ifdef FEAT_VERTSPLIT
5683 /* For a window that's left of another, draw the separator char. */
5684 if (col + coloff < Columns)
5685 {
5686 int c;
5687
5688 c = fillchar_vsep(&hl);
5689 if (ScreenLines[off_to] != c
5690# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005691 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5692 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693# endif
5694 || ScreenAttrs[off_to] != hl)
5695 {
5696 ScreenLines[off_to] = c;
5697 ScreenAttrs[off_to] = hl;
5698# ifdef FEAT_MBYTE
5699 if (enc_utf8)
5700 {
5701 if (c >= 0x80)
5702 {
5703 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005704 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706 else
5707 ScreenLinesUC[off_to] = 0;
5708 }
5709# endif
5710 screen_char(off_to, row, col + coloff);
5711 }
5712 }
5713 else
5714#endif
5715 LineWraps[row] = FALSE;
5716 }
5717}
5718
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005719#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005721 * Mirror text "str" for right-left displaying.
5722 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005724 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725rl_mirror(str)
5726 char_u *str;
5727{
5728 char_u *p1, *p2;
5729 int t;
5730
5731 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5732 {
5733 t = *p1;
5734 *p1 = *p2;
5735 *p2 = t;
5736 }
5737}
5738#endif
5739
5740#if defined(FEAT_WINDOWS) || defined(PROTO)
5741/*
5742 * mark all status lines for redraw; used after first :cd
5743 */
5744 void
5745status_redraw_all()
5746{
5747 win_T *wp;
5748
5749 for (wp = firstwin; wp; wp = wp->w_next)
5750 if (wp->w_status_height)
5751 {
5752 wp->w_redr_status = TRUE;
5753 redraw_later(VALID);
5754 }
5755}
5756
5757/*
5758 * mark all status lines of the current buffer for redraw
5759 */
5760 void
5761status_redraw_curbuf()
5762{
5763 win_T *wp;
5764
5765 for (wp = firstwin; wp; wp = wp->w_next)
5766 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5767 {
5768 wp->w_redr_status = TRUE;
5769 redraw_later(VALID);
5770 }
5771}
5772
5773/*
5774 * Redraw all status lines that need to be redrawn.
5775 */
5776 void
5777redraw_statuslines()
5778{
5779 win_T *wp;
5780
5781 for (wp = firstwin; wp; wp = wp->w_next)
5782 if (wp->w_redr_status)
5783 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005784 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005785 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786}
5787#endif
5788
5789#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5790/*
5791 * Redraw all status lines at the bottom of frame "frp".
5792 */
5793 void
5794win_redraw_last_status(frp)
5795 frame_T *frp;
5796{
5797 if (frp->fr_layout == FR_LEAF)
5798 frp->fr_win->w_redr_status = TRUE;
5799 else if (frp->fr_layout == FR_ROW)
5800 {
5801 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5802 win_redraw_last_status(frp);
5803 }
5804 else /* frp->fr_layout == FR_COL */
5805 {
5806 frp = frp->fr_child;
5807 while (frp->fr_next != NULL)
5808 frp = frp->fr_next;
5809 win_redraw_last_status(frp);
5810 }
5811}
5812#endif
5813
5814#ifdef FEAT_VERTSPLIT
5815/*
5816 * Draw the verticap separator right of window "wp" starting with line "row".
5817 */
5818 static void
5819draw_vsep_win(wp, row)
5820 win_T *wp;
5821 int row;
5822{
5823 int hl;
5824 int c;
5825
5826 if (wp->w_vsep_width)
5827 {
5828 /* draw the vertical separator right of this window */
5829 c = fillchar_vsep(&hl);
5830 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5831 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5832 c, ' ', hl);
5833 }
5834}
5835#endif
5836
5837#ifdef FEAT_WILDMENU
5838static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005839static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
5841/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005842 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 */
5844 static int
5845status_match_len(xp, s)
5846 expand_T *xp;
5847 char_u *s;
5848{
5849 int len = 0;
5850
5851#ifdef FEAT_MENU
5852 int emenu = (xp->xp_context == EXPAND_MENUS
5853 || xp->xp_context == EXPAND_MENUNAMES);
5854
5855 /* Check for menu separators - replace with '|'. */
5856 if (emenu && menu_is_separator(s))
5857 return 1;
5858#endif
5859
5860 while (*s != NUL)
5861 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005862 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005863 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005864 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 }
5866
5867 return len;
5868}
5869
5870/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005871 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005872 * These are backslashes used for escaping. Do show backslashes in help tags.
5873 */
5874 static int
5875skip_status_match_char(xp, s)
5876 expand_T *xp;
5877 char_u *s;
5878{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005879 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005880#ifdef FEAT_MENU
5881 || ((xp->xp_context == EXPAND_MENUS
5882 || xp->xp_context == EXPAND_MENUNAMES)
5883 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5884#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005885 )
5886 {
5887#ifndef BACKSLASH_IN_FILENAME
5888 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5889 return 2;
5890#endif
5891 return 1;
5892 }
5893 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005894}
5895
5896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 * Show wildchar matches in the status line.
5898 * Show at least the "match" item.
5899 * We start at item 'first_match' in the list and show all matches that fit.
5900 *
5901 * If inversion is possible we use it. Else '=' characters are used.
5902 */
5903 void
5904win_redr_status_matches(xp, num_matches, matches, match, showtail)
5905 expand_T *xp;
5906 int num_matches;
5907 char_u **matches; /* list of matches */
5908 int match;
5909 int showtail;
5910{
5911#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5912 int row;
5913 char_u *buf;
5914 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005915 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 int fillchar;
5917 int attr;
5918 int i;
5919 int highlight = TRUE;
5920 char_u *selstart = NULL;
5921 int selstart_col = 0;
5922 char_u *selend = NULL;
5923 static int first_match = 0;
5924 int add_left = FALSE;
5925 char_u *s;
5926#ifdef FEAT_MENU
5927 int emenu;
5928#endif
5929#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5930 int l;
5931#endif
5932
5933 if (matches == NULL) /* interrupted completion? */
5934 return;
5935
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005936#ifdef FEAT_MBYTE
5937 if (has_mbyte)
5938 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5939 else
5940#endif
5941 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 if (buf == NULL)
5943 return;
5944
5945 if (match == -1) /* don't show match but original text */
5946 {
5947 match = 0;
5948 highlight = FALSE;
5949 }
5950 /* count 1 for the ending ">" */
5951 clen = status_match_len(xp, L_MATCH(match)) + 3;
5952 if (match == 0)
5953 first_match = 0;
5954 else if (match < first_match)
5955 {
5956 /* jumping left, as far as we can go */
5957 first_match = match;
5958 add_left = TRUE;
5959 }
5960 else
5961 {
5962 /* check if match fits on the screen */
5963 for (i = first_match; i < match; ++i)
5964 clen += status_match_len(xp, L_MATCH(i)) + 2;
5965 if (first_match > 0)
5966 clen += 2;
5967 /* jumping right, put match at the left */
5968 if ((long)clen > Columns)
5969 {
5970 first_match = match;
5971 /* if showing the last match, we can add some on the left */
5972 clen = 2;
5973 for (i = match; i < num_matches; ++i)
5974 {
5975 clen += status_match_len(xp, L_MATCH(i)) + 2;
5976 if ((long)clen >= Columns)
5977 break;
5978 }
5979 if (i == num_matches)
5980 add_left = TRUE;
5981 }
5982 }
5983 if (add_left)
5984 while (first_match > 0)
5985 {
5986 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5987 if ((long)clen >= Columns)
5988 break;
5989 --first_match;
5990 }
5991
5992 fillchar = fillchar_status(&attr, TRUE);
5993
5994 if (first_match == 0)
5995 {
5996 *buf = NUL;
5997 len = 0;
5998 }
5999 else
6000 {
6001 STRCPY(buf, "< ");
6002 len = 2;
6003 }
6004 clen = len;
6005
6006 i = first_match;
6007 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6008 {
6009 if (i == match)
6010 {
6011 selstart = buf + len;
6012 selstart_col = clen;
6013 }
6014
6015 s = L_MATCH(i);
6016 /* Check for menu separators - replace with '|' */
6017#ifdef FEAT_MENU
6018 emenu = (xp->xp_context == EXPAND_MENUS
6019 || xp->xp_context == EXPAND_MENUNAMES);
6020 if (emenu && menu_is_separator(s))
6021 {
6022 STRCPY(buf + len, transchar('|'));
6023 l = (int)STRLEN(buf + len);
6024 len += l;
6025 clen += l;
6026 }
6027 else
6028#endif
6029 for ( ; *s != NUL; ++s)
6030 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006031 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 clen += ptr2cells(s);
6033#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006034 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 {
6036 STRNCPY(buf + len, s, l);
6037 s += l - 1;
6038 len += l;
6039 }
6040 else
6041#endif
6042 {
6043 STRCPY(buf + len, transchar_byte(*s));
6044 len += (int)STRLEN(buf + len);
6045 }
6046 }
6047 if (i == match)
6048 selend = buf + len;
6049
6050 *(buf + len++) = ' ';
6051 *(buf + len++) = ' ';
6052 clen += 2;
6053 if (++i == num_matches)
6054 break;
6055 }
6056
6057 if (i != num_matches)
6058 {
6059 *(buf + len++) = '>';
6060 ++clen;
6061 }
6062
6063 buf[len] = NUL;
6064
6065 row = cmdline_row - 1;
6066 if (row >= 0)
6067 {
6068 if (wild_menu_showing == 0)
6069 {
6070 if (msg_scrolled > 0)
6071 {
6072 /* Put the wildmenu just above the command line. If there is
6073 * no room, scroll the screen one line up. */
6074 if (cmdline_row == Rows - 1)
6075 {
6076 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6077 ++msg_scrolled;
6078 }
6079 else
6080 {
6081 ++cmdline_row;
6082 ++row;
6083 }
6084 wild_menu_showing = WM_SCROLLED;
6085 }
6086 else
6087 {
6088 /* Create status line if needed by setting 'laststatus' to 2.
6089 * Set 'winminheight' to zero to avoid that the window is
6090 * resized. */
6091 if (lastwin->w_status_height == 0)
6092 {
6093 save_p_ls = p_ls;
6094 save_p_wmh = p_wmh;
6095 p_ls = 2;
6096 p_wmh = 0;
6097 last_status(FALSE);
6098 }
6099 wild_menu_showing = WM_SHOWN;
6100 }
6101 }
6102
6103 screen_puts(buf, row, 0, attr);
6104 if (selstart != NULL && highlight)
6105 {
6106 *selend = NUL;
6107 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6108 }
6109
6110 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6111 }
6112
6113#ifdef FEAT_VERTSPLIT
6114 win_redraw_last_status(topframe);
6115#else
6116 lastwin->w_redr_status = TRUE;
6117#endif
6118 vim_free(buf);
6119}
6120#endif
6121
6122#if defined(FEAT_WINDOWS) || defined(PROTO)
6123/*
6124 * Redraw the status line of window wp.
6125 *
6126 * If inversion is possible we use it. Else '=' characters are used.
6127 */
6128 void
6129win_redr_status(wp)
6130 win_T *wp;
6131{
6132 int row;
6133 char_u *p;
6134 int len;
6135 int fillchar;
6136 int attr;
6137 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006138 static int busy = FALSE;
6139
6140 /* It's possible to get here recursively when 'statusline' (indirectly)
6141 * invokes ":redrawstatus". Simply ignore the call then. */
6142 if (busy)
6143 return;
6144 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145
6146 wp->w_redr_status = FALSE;
6147 if (wp->w_status_height == 0)
6148 {
6149 /* no status line, can only be last window */
6150 redraw_cmdline = TRUE;
6151 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006152 else if (!redrawing()
6153#ifdef FEAT_INS_EXPAND
6154 /* don't update status line when popup menu is visible and may be
6155 * drawn over it */
6156 || pum_visible()
6157#endif
6158 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 {
6160 /* Don't redraw right now, do it later. */
6161 wp->w_redr_status = TRUE;
6162 }
6163#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006164 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 {
6166 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006167 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 }
6169#endif
6170 else
6171 {
6172 fillchar = fillchar_status(&attr, wp == curwin);
6173
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006174 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 p = NameBuff;
6176 len = (int)STRLEN(p);
6177
6178 if (wp->w_buffer->b_help
6179#ifdef FEAT_QUICKFIX
6180 || wp->w_p_pvw
6181#endif
6182 || bufIsChanged(wp->w_buffer)
6183 || wp->w_buffer->b_p_ro)
6184 *(p + len++) = ' ';
6185 if (wp->w_buffer->b_help)
6186 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006187 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 len += (int)STRLEN(p + len);
6189 }
6190#ifdef FEAT_QUICKFIX
6191 if (wp->w_p_pvw)
6192 {
6193 STRCPY(p + len, _("[Preview]"));
6194 len += (int)STRLEN(p + len);
6195 }
6196#endif
6197 if (bufIsChanged(wp->w_buffer))
6198 {
6199 STRCPY(p + len, "[+]");
6200 len += 3;
6201 }
6202 if (wp->w_buffer->b_p_ro)
6203 {
6204 STRCPY(p + len, "[RO]");
6205 len += 4;
6206 }
6207
6208#ifndef FEAT_VERTSPLIT
6209 this_ru_col = ru_col;
6210 if (this_ru_col < (Columns + 1) / 2)
6211 this_ru_col = (Columns + 1) / 2;
6212#else
6213 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6214 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6215 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6216 if (this_ru_col <= 1)
6217 {
6218 p = (char_u *)"<"; /* No room for file name! */
6219 len = 1;
6220 }
6221 else
6222#endif
6223#ifdef FEAT_MBYTE
6224 if (has_mbyte)
6225 {
6226 int clen = 0, i;
6227
6228 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006229 clen = mb_string2cells(p, -1);
6230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 /* Find first character that will fit.
6232 * Going from start to end is much faster for DBCS. */
6233 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006234 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 clen -= (*mb_ptr2cells)(p + i);
6236 len = clen;
6237 if (i > 0)
6238 {
6239 p = p + i - 1;
6240 *p = '<';
6241 ++len;
6242 }
6243
6244 }
6245 else
6246#endif
6247 if (len > this_ru_col - 1)
6248 {
6249 p += len - (this_ru_col - 1);
6250 *p = '<';
6251 len = this_ru_col - 1;
6252 }
6253
6254 row = W_WINROW(wp) + wp->w_height;
6255 screen_puts(p, row, W_WINCOL(wp), attr);
6256 screen_fill(row, row + 1, len + W_WINCOL(wp),
6257 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6258
6259 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6260 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6261 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6262 - 1 + W_WINCOL(wp)), attr);
6263
6264#ifdef FEAT_CMDL_INFO
6265 win_redr_ruler(wp, TRUE);
6266#endif
6267 }
6268
6269#ifdef FEAT_VERTSPLIT
6270 /*
6271 * May need to draw the character below the vertical separator.
6272 */
6273 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6274 {
6275 if (stl_connected(wp))
6276 fillchar = fillchar_status(&attr, wp == curwin);
6277 else
6278 fillchar = fillchar_vsep(&attr);
6279 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6280 attr);
6281 }
6282#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006283 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284}
6285
Bram Moolenaar238a5642006-02-21 22:12:05 +00006286#ifdef FEAT_STL_OPT
6287/*
6288 * Redraw the status line according to 'statusline' and take care of any
6289 * errors encountered.
6290 */
6291 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006292redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006293 win_T *wp;
6294{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006295 static int entered = FALSE;
6296 int save_called_emsg = called_emsg;
6297
6298 /* When called recursively return. This can happen when the statusline
6299 * contains an expression that triggers a redraw. */
6300 if (entered)
6301 return;
6302 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006303
6304 called_emsg = FALSE;
6305 win_redr_custom(wp, FALSE);
6306 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006307 {
6308 /* When there is an error disable the statusline, otherwise the
6309 * display is messed up with errors and a redraw triggers the problem
6310 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006311 set_string_option_direct((char_u *)"statusline", -1,
6312 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006313 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006314 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006315 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006316 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006317}
6318#endif
6319
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320# ifdef FEAT_VERTSPLIT
6321/*
6322 * Return TRUE if the status line of window "wp" is connected to the status
6323 * line of the window right of it. If not, then it's a vertical separator.
6324 * Only call if (wp->w_vsep_width != 0).
6325 */
6326 int
6327stl_connected(wp)
6328 win_T *wp;
6329{
6330 frame_T *fr;
6331
6332 fr = wp->w_frame;
6333 while (fr->fr_parent != NULL)
6334 {
6335 if (fr->fr_parent->fr_layout == FR_COL)
6336 {
6337 if (fr->fr_next != NULL)
6338 break;
6339 }
6340 else
6341 {
6342 if (fr->fr_next != NULL)
6343 return TRUE;
6344 }
6345 fr = fr->fr_parent;
6346 }
6347 return FALSE;
6348}
6349# endif
6350
6351#endif /* FEAT_WINDOWS */
6352
6353#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6354/*
6355 * Get the value to show for the language mappings, active 'keymap'.
6356 */
6357 int
6358get_keymap_str(wp, buf, len)
6359 win_T *wp;
6360 char_u *buf; /* buffer for the result */
6361 int len; /* length of buffer */
6362{
6363 char_u *p;
6364
6365 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6366 return FALSE;
6367
6368 {
6369#ifdef FEAT_EVAL
6370 buf_T *old_curbuf = curbuf;
6371 win_T *old_curwin = curwin;
6372 char_u *s;
6373
6374 curbuf = wp->w_buffer;
6375 curwin = wp;
6376 STRCPY(buf, "b:keymap_name"); /* must be writable */
6377 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006378 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 --emsg_skip;
6380 curbuf = old_curbuf;
6381 curwin = old_curwin;
6382 if (p == NULL || *p == NUL)
6383#endif
6384 {
6385#ifdef FEAT_KEYMAP
6386 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6387 p = wp->w_buffer->b_p_keymap;
6388 else
6389#endif
6390 p = (char_u *)"lang";
6391 }
6392 if ((int)(STRLEN(p) + 3) < len)
6393 sprintf((char *)buf, "<%s>", p);
6394 else
6395 buf[0] = NUL;
6396#ifdef FEAT_EVAL
6397 vim_free(s);
6398#endif
6399 }
6400 return buf[0] != NUL;
6401}
6402#endif
6403
6404#if defined(FEAT_STL_OPT) || defined(PROTO)
6405/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006406 * Redraw the status line or ruler of window "wp".
6407 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 */
6409 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006410win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006412 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 int attr;
6415 int curattr;
6416 int row;
6417 int col = 0;
6418 int maxwidth;
6419 int width;
6420 int n;
6421 int len;
6422 int fillchar;
6423 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006424 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006426 struct stl_hlrec hltab[STL_MAX_ITEM];
6427 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006428 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429
6430 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006431 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006433 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006434 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006435 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006436 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006437 attr = hl_attr(HLF_TPF);
6438 maxwidth = Columns;
6439# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006440 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006443 else
6444 {
6445 row = W_WINROW(wp) + wp->w_height;
6446 fillchar = fillchar_status(&attr, wp == curwin);
6447 maxwidth = W_WIDTH(wp);
6448
6449 if (draw_ruler)
6450 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006451 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006452 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006453 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006454 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006455 if (*++stl == '-')
6456 stl++;
6457 if (atoi((char *)stl))
6458 while (VIM_ISDIGIT(*stl))
6459 stl++;
6460 if (*stl++ != '(')
6461 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006462 }
6463#ifdef FEAT_VERTSPLIT
6464 col = ru_col - (Columns - W_WIDTH(wp));
6465 if (col < (W_WIDTH(wp) + 1) / 2)
6466 col = (W_WIDTH(wp) + 1) / 2;
6467#else
6468 col = ru_col;
6469 if (col > (Columns + 1) / 2)
6470 col = (Columns + 1) / 2;
6471#endif
6472 maxwidth = W_WIDTH(wp) - col;
6473#ifdef FEAT_WINDOWS
6474 if (!wp->w_status_height)
6475#endif
6476 {
6477 row = Rows - 1;
6478 --maxwidth; /* writing in last column may cause scrolling */
6479 fillchar = ' ';
6480 attr = 0;
6481 }
6482
6483# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006484 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006485# endif
6486 }
6487 else
6488 {
6489 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006490 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006491 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006492 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006493# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006494 use_sandbox = was_set_insecurely((char_u *)"statusline",
6495 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006496# endif
6497 }
6498
6499#ifdef FEAT_VERTSPLIT
6500 col += W_WINCOL(wp);
6501#endif
6502 }
6503
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 if (maxwidth <= 0)
6505 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
Bram Moolenaar362f3562009-11-03 16:20:34 +00006507 /* Make a copy, because the statusline may include a function call that
6508 * might change the option value and free the memory. */
6509 stl = vim_strsave(stl);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006510 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6511 buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006512 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006513 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006514 vim_free(stl);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006515 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006517 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 {
6519#ifdef FEAT_MBYTE
6520 len += (*mb_char2bytes)(fillchar, buf + len);
6521#else
6522 buf[len++] = fillchar;
6523#endif
6524 ++width;
6525 }
6526 buf[len] = NUL;
6527
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006528 /*
6529 * Draw each snippet with the specified highlighting.
6530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 curattr = attr;
6532 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006533 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006535 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 screen_puts_len(p, len, row, col, curattr);
6537 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006538 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006540 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006542 else if (hltab[n].userhl < 0)
6543 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006545 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006546 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547#endif
6548 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006549 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
6551 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006552
6553 if (wp == NULL)
6554 {
6555 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6556 col = 0;
6557 len = 0;
6558 p = buf;
6559 fillchar = 0;
6560 for (n = 0; tabtab[n].start != NULL; n++)
6561 {
6562 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6563 while (col < len)
6564 TabPageIdxs[col++] = fillchar;
6565 p = tabtab[n].start;
6566 fillchar = tabtab[n].userhl;
6567 }
6568 while (col < Columns)
6569 TabPageIdxs[col++] = fillchar;
6570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571}
6572
6573#endif /* FEAT_STL_OPT */
6574
6575/*
6576 * Output a single character directly to the screen and update ScreenLines.
6577 */
6578 void
6579screen_putchar(c, row, col, attr)
6580 int c;
6581 int row, col;
6582 int attr;
6583{
6584#ifdef FEAT_MBYTE
6585 char_u buf[MB_MAXBYTES + 1];
6586
6587 buf[(*mb_char2bytes)(c, buf)] = NUL;
6588#else
6589 char_u buf[2];
6590
6591 buf[0] = c;
6592 buf[1] = NUL;
6593#endif
6594 screen_puts(buf, row, col, attr);
6595}
6596
6597/*
6598 * Get a single character directly from ScreenLines into "bytes[]".
6599 * Also return its attribute in *attrp;
6600 */
6601 void
6602screen_getbytes(row, col, bytes, attrp)
6603 int row, col;
6604 char_u *bytes;
6605 int *attrp;
6606{
6607 unsigned off;
6608
6609 /* safety check */
6610 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6611 {
6612 off = LineOffset[row] + col;
6613 *attrp = ScreenAttrs[off];
6614 bytes[0] = ScreenLines[off];
6615 bytes[1] = NUL;
6616
6617#ifdef FEAT_MBYTE
6618 if (enc_utf8 && ScreenLinesUC[off] != 0)
6619 bytes[utfc_char2bytes(off, bytes)] = NUL;
6620 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6621 {
6622 bytes[0] = ScreenLines[off];
6623 bytes[1] = ScreenLines2[off];
6624 bytes[2] = NUL;
6625 }
6626 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6627 {
6628 bytes[1] = ScreenLines[off + 1];
6629 bytes[2] = NUL;
6630 }
6631#endif
6632 }
6633}
6634
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006635#ifdef FEAT_MBYTE
6636static int screen_comp_differs __ARGS((int, int*));
6637
6638/*
6639 * Return TRUE if composing characters for screen posn "off" differs from
6640 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006641 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006642 */
6643 static int
6644screen_comp_differs(off, u8cc)
6645 int off;
6646 int *u8cc;
6647{
6648 int i;
6649
6650 for (i = 0; i < Screen_mco; ++i)
6651 {
6652 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6653 return TRUE;
6654 if (u8cc[i] == 0)
6655 break;
6656 }
6657 return FALSE;
6658}
6659#endif
6660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661/*
6662 * Put string '*text' on the screen at position 'row' and 'col', with
6663 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6664 * Note: only outputs within one row, message is truncated at screen boundary!
6665 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6666 */
6667 void
6668screen_puts(text, row, col, attr)
6669 char_u *text;
6670 int row;
6671 int col;
6672 int attr;
6673{
6674 screen_puts_len(text, -1, row, col, attr);
6675}
6676
6677/*
6678 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6679 * a NUL.
6680 */
6681 void
6682screen_puts_len(text, len, row, col, attr)
6683 char_u *text;
6684 int len;
6685 int row;
6686 int col;
6687 int attr;
6688{
6689 unsigned off;
6690 char_u *ptr = text;
6691 int c;
6692#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006693 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 int mbyte_blen = 1;
6695 int mbyte_cells = 1;
6696 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006697 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 int clear_next_cell = FALSE;
6699# ifdef FEAT_ARABIC
6700 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006701 int pc, nc, nc1;
6702 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703# endif
6704#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006705#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6706 int force_redraw_this;
6707 int force_redraw_next = FALSE;
6708#endif
6709 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
6711 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6712 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006713 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
Bram Moolenaarc236c162008-07-13 17:41:49 +00006715#ifdef FEAT_MBYTE
6716 /* When drawing over the right halve of a double-wide char clear out the
6717 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006718 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006719# ifdef FEAT_GUI
6720 && !gui.in_use
6721# endif
6722 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006723 {
6724 ScreenLines[off - 1] = ' ';
6725 ScreenAttrs[off - 1] = 0;
6726 if (enc_utf8)
6727 {
6728 ScreenLinesUC[off - 1] = 0;
6729 ScreenLinesC[0][off - 1] = 0;
6730 }
6731 /* redraw the previous cell, make it empty */
6732 screen_char(off - 1, row, col - 1);
6733 /* force the cell at "col" to be redrawn */
6734 force_redraw_next = TRUE;
6735 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006736#endif
6737
Bram Moolenaar367329b2007-08-30 11:53:22 +00006738#ifdef FEAT_MBYTE
6739 max_off = LineOffset[row] + screen_Columns;
6740#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006741 while (col < screen_Columns
6742 && (len < 0 || (int)(ptr - text) < len)
6743 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 {
6745 c = *ptr;
6746#ifdef FEAT_MBYTE
6747 /* check if this is the first byte of a multibyte */
6748 if (has_mbyte)
6749 {
6750 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006751 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006753 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6755 mbyte_cells = 1;
6756 else if (enc_dbcs != 0)
6757 mbyte_cells = mbyte_blen;
6758 else /* enc_utf8 */
6759 {
6760 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006761 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 (int)((text + len) - ptr));
6763 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006764 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006766# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 /* Non-BMP character: display as ? or fullwidth ?. */
6768 if (u8c >= 0x10000)
6769 {
6770 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6771 if (attr == 0)
6772 attr = hl_attr(HLF_8);
6773 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775# ifdef FEAT_ARABIC
6776 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6777 {
6778 /* Do Arabic shaping. */
6779 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6780 {
6781 /* Past end of string to be displayed. */
6782 nc = NUL;
6783 nc1 = NUL;
6784 }
6785 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006786 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006787 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6788 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006789 nc1 = pcc[0];
6790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 pc = prev_c;
6792 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006793 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 }
6795 else
6796 prev_c = u8c;
6797# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006798 if (col + mbyte_cells > screen_Columns)
6799 {
6800 /* Only 1 cell left, but character requires 2 cells:
6801 * display a '>' in the last column to avoid wrapping. */
6802 c = '>';
6803 mbyte_cells = 1;
6804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 }
6806 }
6807#endif
6808
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006809#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6810 force_redraw_this = force_redraw_next;
6811 force_redraw_next = FALSE;
6812#endif
6813
6814 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815#ifdef FEAT_MBYTE
6816 || (mbyte_cells == 2
6817 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6818 || (enc_dbcs == DBCS_JPNU
6819 && c == 0x8e
6820 && ScreenLines2[off] != ptr[1])
6821 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006822 && (ScreenLinesUC[off] !=
6823 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6824 || (ScreenLinesUC[off] != 0
6825 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826#endif
6827 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006828 || exmode_active;
6829
6830 if (need_redraw
6831#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6832 || force_redraw_this
6833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 )
6835 {
6836#if defined(FEAT_GUI) || defined(UNIX)
6837 /* The bold trick makes a single row of pixels appear in the next
6838 * character. When a bold character is removed, the next
6839 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006840 * and for some xterms. */
6841 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842# ifdef FEAT_GUI
6843 gui.in_use
6844# endif
6845# if defined(FEAT_GUI) && defined(UNIX)
6846 ||
6847# endif
6848# ifdef UNIX
6849 term_is_xterm
6850# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006851 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006853 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006855 if (n > HL_ALL)
6856 n = syn_attr2attr(n);
6857 if (n & HL_BOLD)
6858 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 }
6860#endif
6861#ifdef FEAT_MBYTE
6862 /* When at the end of the text and overwriting a two-cell
6863 * character with a one-cell character, need to clear the next
6864 * cell. Also when overwriting the left halve of a two-cell char
6865 * with the right halve of a two-cell char. Do this only once
6866 * (mb_off2cells() may return 2 on the right halve). */
6867 if (clear_next_cell)
6868 clear_next_cell = FALSE;
6869 else if (has_mbyte
6870 && (len < 0 ? ptr[mbyte_blen] == NUL
6871 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006872 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006874 && (*mb_off2cells)(off, max_off) == 1
6875 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 clear_next_cell = TRUE;
6877
6878 /* Make sure we never leave a second byte of a double-byte behind,
6879 * it confuses mb_off2cells(). */
6880 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006881 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006883 && (*mb_off2cells)(off, max_off) == 1
6884 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 ScreenLines[off + mbyte_blen] = 0;
6886#endif
6887 ScreenLines[off] = c;
6888 ScreenAttrs[off] = attr;
6889#ifdef FEAT_MBYTE
6890 if (enc_utf8)
6891 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006892 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 ScreenLinesUC[off] = 0;
6894 else
6895 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006896 int i;
6897
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006899 for (i = 0; i < Screen_mco; ++i)
6900 {
6901 ScreenLinesC[i][off] = u8cc[i];
6902 if (u8cc[i] == 0)
6903 break;
6904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 }
6906 if (mbyte_cells == 2)
6907 {
6908 ScreenLines[off + 1] = 0;
6909 ScreenAttrs[off + 1] = attr;
6910 }
6911 screen_char(off, row, col);
6912 }
6913 else if (mbyte_cells == 2)
6914 {
6915 ScreenLines[off + 1] = ptr[1];
6916 ScreenAttrs[off + 1] = attr;
6917 screen_char_2(off, row, col);
6918 }
6919 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6920 {
6921 ScreenLines2[off] = ptr[1];
6922 screen_char(off, row, col);
6923 }
6924 else
6925#endif
6926 screen_char(off, row, col);
6927 }
6928#ifdef FEAT_MBYTE
6929 if (has_mbyte)
6930 {
6931 off += mbyte_cells;
6932 col += mbyte_cells;
6933 ptr += mbyte_blen;
6934 if (clear_next_cell)
6935 ptr = (char_u *)" ";
6936 }
6937 else
6938#endif
6939 {
6940 ++off;
6941 ++col;
6942 ++ptr;
6943 }
6944 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006945
6946#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6947 /* If we detected the next character needs to be redrawn, but the text
6948 * doesn't extend up to there, update the character here. */
6949 if (force_redraw_next && col < screen_Columns)
6950 {
6951# ifdef FEAT_MBYTE
6952 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6953 screen_char_2(off, row, col);
6954 else
6955# endif
6956 screen_char(off, row, col);
6957 }
6958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959}
6960
6961#ifdef FEAT_SEARCH_EXTRA
6962/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006963 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 */
6965 static void
6966start_search_hl()
6967{
6968 if (p_hls && !no_hlsearch)
6969 {
6970 last_pat_prog(&search_hl.rm);
6971 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006972# ifdef FEAT_RELTIME
6973 /* Set the time limit to 'redrawtime'. */
6974 profile_setlimit(p_rdt, &search_hl.tm);
6975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 }
6977}
6978
6979/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006980 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 */
6982 static void
6983end_search_hl()
6984{
6985 if (search_hl.rm.regprog != NULL)
6986 {
6987 vim_free(search_hl.rm.regprog);
6988 search_hl.rm.regprog = NULL;
6989 }
6990}
6991
6992/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02006993 * Init for calling prepare_search_hl().
6994 */
6995 static void
6996init_search_hl(wp)
6997 win_T *wp;
6998{
6999 matchitem_T *cur;
7000
7001 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7002 * match */
7003 cur = wp->w_match_head;
7004 while (cur != NULL)
7005 {
7006 cur->hl.rm = cur->match;
7007 if (cur->hlg_id == 0)
7008 cur->hl.attr = 0;
7009 else
7010 cur->hl.attr = syn_id2attr(cur->hlg_id);
7011 cur->hl.buf = wp->w_buffer;
7012 cur->hl.lnum = 0;
7013 cur->hl.first_lnum = 0;
7014# ifdef FEAT_RELTIME
7015 /* Set the time limit to 'redrawtime'. */
7016 profile_setlimit(p_rdt, &(cur->hl.tm));
7017# endif
7018 cur = cur->next;
7019 }
7020 search_hl.buf = wp->w_buffer;
7021 search_hl.lnum = 0;
7022 search_hl.first_lnum = 0;
7023 /* time limit is set at the toplevel, for all windows */
7024}
7025
7026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 * Advance to the match in window "wp" line "lnum" or past it.
7028 */
7029 static void
7030prepare_search_hl(wp, lnum)
7031 win_T *wp;
7032 linenr_T lnum;
7033{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007034 matchitem_T *cur; /* points to the match list */
7035 match_T *shl; /* points to search_hl or a match */
7036 int shl_flag; /* flag to indicate whether search_hl
7037 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 int n;
7039
7040 /*
7041 * When using a multi-line pattern, start searching at the top
7042 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007043 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007045 cur = wp->w_match_head;
7046 shl_flag = FALSE;
7047 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007049 if (shl_flag == FALSE)
7050 {
7051 shl = &search_hl;
7052 shl_flag = TRUE;
7053 }
7054 else
7055 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 if (shl->rm.regprog != NULL
7057 && shl->lnum == 0
7058 && re_multiline(shl->rm.regprog))
7059 {
7060 if (shl->first_lnum == 0)
7061 {
7062# ifdef FEAT_FOLDING
7063 for (shl->first_lnum = lnum;
7064 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7065 if (hasFoldingWin(wp, shl->first_lnum - 1,
7066 NULL, NULL, TRUE, NULL))
7067 break;
7068# else
7069 shl->first_lnum = wp->w_topline;
7070# endif
7071 }
7072 n = 0;
7073 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7074 {
7075 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7076 if (shl->lnum != 0)
7077 {
7078 shl->first_lnum = shl->lnum
7079 + shl->rm.endpos[0].lnum
7080 - shl->rm.startpos[0].lnum;
7081 n = shl->rm.endpos[0].col;
7082 }
7083 else
7084 {
7085 ++shl->first_lnum;
7086 n = 0;
7087 }
7088 }
7089 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007090 if (shl != &search_hl && cur != NULL)
7091 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 }
7093}
7094
7095/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007096 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 * Uses shl->buf.
7098 * Sets shl->lnum and shl->rm contents.
7099 * Note: Assumes a previous match is always before "lnum", unless
7100 * shl->lnum is zero.
7101 * Careful: Any pointers for buffer lines will become invalid.
7102 */
7103 static void
7104next_search_hl(win, shl, lnum, mincol)
7105 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007106 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 linenr_T lnum;
7108 colnr_T mincol; /* minimal column for a match */
7109{
7110 linenr_T l;
7111 colnr_T matchcol;
7112 long nmatched;
7113
7114 if (shl->lnum != 0)
7115 {
7116 /* Check for three situations:
7117 * 1. If the "lnum" is below a previous match, start a new search.
7118 * 2. If the previous match includes "mincol", use it.
7119 * 3. Continue after the previous match.
7120 */
7121 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7122 if (lnum > l)
7123 shl->lnum = 0;
7124 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7125 return;
7126 }
7127
7128 /*
7129 * Repeat searching for a match until one is found that includes "mincol"
7130 * or none is found in this line.
7131 */
7132 called_emsg = FALSE;
7133 for (;;)
7134 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007135#ifdef FEAT_RELTIME
7136 /* Stop searching after passing the time limit. */
7137 if (profile_passed_limit(&(shl->tm)))
7138 {
7139 shl->lnum = 0; /* no match found in time */
7140 break;
7141 }
7142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 /* Three situations:
7144 * 1. No useful previous match: search from start of line.
7145 * 2. Not Vi compatible or empty match: continue at next character.
7146 * Break the loop if this is beyond the end of the line.
7147 * 3. Vi compatible searching: continue at end of previous match.
7148 */
7149 if (shl->lnum == 0)
7150 matchcol = 0;
7151 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7152 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007153 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007155 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007156
7157 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007158 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007159 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007161 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 shl->lnum = 0;
7163 break;
7164 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007165#ifdef FEAT_MBYTE
7166 if (has_mbyte)
7167 matchcol += mb_ptr2len(ml);
7168 else
7169#endif
7170 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 }
7172 else
7173 matchcol = shl->rm.endpos[0].col;
7174
7175 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007176 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7177#ifdef FEAT_RELTIME
7178 &(shl->tm)
7179#else
7180 NULL
7181#endif
7182 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007183 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
7185 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007186 if (shl == &search_hl)
7187 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007188 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007189 vim_free(shl->rm.regprog);
7190 no_hlsearch = TRUE;
7191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007193 shl->lnum = 0;
7194 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 break;
7196 }
7197 if (nmatched == 0)
7198 {
7199 shl->lnum = 0; /* no match found */
7200 break;
7201 }
7202 if (shl->rm.startpos[0].lnum > 0
7203 || shl->rm.startpos[0].col >= mincol
7204 || nmatched > 1
7205 || shl->rm.endpos[0].col > mincol)
7206 {
7207 shl->lnum += shl->rm.startpos[0].lnum;
7208 break; /* useful match found */
7209 }
7210 }
7211}
7212#endif
7213
7214 static void
7215screen_start_highlight(attr)
7216 int attr;
7217{
7218 attrentry_T *aep = NULL;
7219
7220 screen_attr = attr;
7221 if (full_screen
7222#ifdef WIN3264
7223 && termcap_active
7224#endif
7225 )
7226 {
7227#ifdef FEAT_GUI
7228 if (gui.in_use)
7229 {
7230 char buf[20];
7231
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007232 /* The GUI handles this internally. */
7233 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 OUT_STR(buf);
7235 }
7236 else
7237#endif
7238 {
7239 if (attr > HL_ALL) /* special HL attr. */
7240 {
7241 if (t_colors > 1)
7242 aep = syn_cterm_attr2entry(attr);
7243 else
7244 aep = syn_term_attr2entry(attr);
7245 if (aep == NULL) /* did ":syntax clear" */
7246 attr = 0;
7247 else
7248 attr = aep->ae_attr;
7249 }
7250 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7251 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007252 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7253 && cterm_normal_fg_bold)
7254 /* If the Normal FG color has BOLD attribute and the new HL
7255 * has a FG color defined, clear BOLD. */
7256 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7258 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007259 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7260 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 out_str(T_US);
7262 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7263 out_str(T_CZH);
7264 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7265 out_str(T_MR);
7266
7267 /*
7268 * Output the color or start string after bold etc., in case the
7269 * bold etc. override the color setting.
7270 */
7271 if (aep != NULL)
7272 {
7273 if (t_colors > 1)
7274 {
7275 if (aep->ae_u.cterm.fg_color)
7276 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7277 if (aep->ae_u.cterm.bg_color)
7278 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7279 }
7280 else
7281 {
7282 if (aep->ae_u.term.start != NULL)
7283 out_str(aep->ae_u.term.start);
7284 }
7285 }
7286 }
7287 }
7288}
7289
7290 void
7291screen_stop_highlight()
7292{
7293 int do_ME = FALSE; /* output T_ME code */
7294
7295 if (screen_attr != 0
7296#ifdef WIN3264
7297 && termcap_active
7298#endif
7299 )
7300 {
7301#ifdef FEAT_GUI
7302 if (gui.in_use)
7303 {
7304 char buf[20];
7305
7306 /* use internal GUI code */
7307 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7308 OUT_STR(buf);
7309 }
7310 else
7311#endif
7312 {
7313 if (screen_attr > HL_ALL) /* special HL attr. */
7314 {
7315 attrentry_T *aep;
7316
7317 if (t_colors > 1)
7318 {
7319 /*
7320 * Assume that t_me restores the original colors!
7321 */
7322 aep = syn_cterm_attr2entry(screen_attr);
7323 if (aep != NULL && (aep->ae_u.cterm.fg_color
7324 || aep->ae_u.cterm.bg_color))
7325 do_ME = TRUE;
7326 }
7327 else
7328 {
7329 aep = syn_term_attr2entry(screen_attr);
7330 if (aep != NULL && aep->ae_u.term.stop != NULL)
7331 {
7332 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7333 do_ME = TRUE;
7334 else
7335 out_str(aep->ae_u.term.stop);
7336 }
7337 }
7338 if (aep == NULL) /* did ":syntax clear" */
7339 screen_attr = 0;
7340 else
7341 screen_attr = aep->ae_attr;
7342 }
7343
7344 /*
7345 * Often all ending-codes are equal to T_ME. Avoid outputting the
7346 * same sequence several times.
7347 */
7348 if (screen_attr & HL_STANDOUT)
7349 {
7350 if (STRCMP(T_SE, T_ME) == 0)
7351 do_ME = TRUE;
7352 else
7353 out_str(T_SE);
7354 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007355 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 {
7357 if (STRCMP(T_UE, T_ME) == 0)
7358 do_ME = TRUE;
7359 else
7360 out_str(T_UE);
7361 }
7362 if (screen_attr & HL_ITALIC)
7363 {
7364 if (STRCMP(T_CZR, T_ME) == 0)
7365 do_ME = TRUE;
7366 else
7367 out_str(T_CZR);
7368 }
7369 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7370 out_str(T_ME);
7371
7372 if (t_colors > 1)
7373 {
7374 /* set Normal cterm colors */
7375 if (cterm_normal_fg_color != 0)
7376 term_fg_color(cterm_normal_fg_color - 1);
7377 if (cterm_normal_bg_color != 0)
7378 term_bg_color(cterm_normal_bg_color - 1);
7379 if (cterm_normal_fg_bold)
7380 out_str(T_MD);
7381 }
7382 }
7383 }
7384 screen_attr = 0;
7385}
7386
7387/*
7388 * Reset the colors for a cterm. Used when leaving Vim.
7389 * The machine specific code may override this again.
7390 */
7391 void
7392reset_cterm_colors()
7393{
7394 if (t_colors > 1)
7395 {
7396 /* set Normal cterm colors */
7397 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7398 {
7399 out_str(T_OP);
7400 screen_attr = -1;
7401 }
7402 if (cterm_normal_fg_bold)
7403 {
7404 out_str(T_ME);
7405 screen_attr = -1;
7406 }
7407 }
7408}
7409
7410/*
7411 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7412 * using the attributes from ScreenAttrs["off"].
7413 */
7414 static void
7415screen_char(off, row, col)
7416 unsigned off;
7417 int row;
7418 int col;
7419{
7420 int attr;
7421
7422 /* Check for illegal values, just in case (could happen just after
7423 * resizing). */
7424 if (row >= screen_Rows || col >= screen_Columns)
7425 return;
7426
7427 /* Outputting the last character on the screen may scrollup the screen.
7428 * Don't to it! Mark the character invalid (update it when scrolled up) */
7429 if (row == screen_Rows - 1 && col == screen_Columns - 1
7430#ifdef FEAT_RIGHTLEFT
7431 /* account for first command-line character in rightleft mode */
7432 && !cmdmsg_rl
7433#endif
7434 )
7435 {
7436 ScreenAttrs[off] = (sattr_T)-1;
7437 return;
7438 }
7439
7440 /*
7441 * Stop highlighting first, so it's easier to move the cursor.
7442 */
7443#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7444 if (screen_char_attr != 0)
7445 attr = screen_char_attr;
7446 else
7447#endif
7448 attr = ScreenAttrs[off];
7449 if (screen_attr != attr)
7450 screen_stop_highlight();
7451
7452 windgoto(row, col);
7453
7454 if (screen_attr != attr)
7455 screen_start_highlight(attr);
7456
7457#ifdef FEAT_MBYTE
7458 if (enc_utf8 && ScreenLinesUC[off] != 0)
7459 {
7460 char_u buf[MB_MAXBYTES + 1];
7461
7462 /* Convert UTF-8 character to bytes and write it. */
7463
7464 buf[utfc_char2bytes(off, buf)] = NUL;
7465
7466 out_str(buf);
7467 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7468 ++screen_cur_col;
7469 }
7470 else
7471#endif
7472 {
7473#ifdef FEAT_MBYTE
7474 out_flush_check();
7475#endif
7476 out_char(ScreenLines[off]);
7477#ifdef FEAT_MBYTE
7478 /* double-byte character in single-width cell */
7479 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7480 out_char(ScreenLines2[off]);
7481#endif
7482 }
7483
7484 screen_cur_col++;
7485}
7486
7487#ifdef FEAT_MBYTE
7488
7489/*
7490 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7491 * on the screen at position 'row' and 'col'.
7492 * The attributes of the first byte is used for all. This is required to
7493 * output the two bytes of a double-byte character with nothing in between.
7494 */
7495 static void
7496screen_char_2(off, row, col)
7497 unsigned off;
7498 int row;
7499 int col;
7500{
7501 /* Check for illegal values (could be wrong when screen was resized). */
7502 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7503 return;
7504
7505 /* Outputting the last character on the screen may scrollup the screen.
7506 * Don't to it! Mark the character invalid (update it when scrolled up) */
7507 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7508 {
7509 ScreenAttrs[off] = (sattr_T)-1;
7510 return;
7511 }
7512
7513 /* Output the first byte normally (positions the cursor), then write the
7514 * second byte directly. */
7515 screen_char(off, row, col);
7516 out_char(ScreenLines[off + 1]);
7517 ++screen_cur_col;
7518}
7519#endif
7520
7521#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7522/*
7523 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7524 * This uses the contents of ScreenLines[] and doesn't change it.
7525 */
7526 void
7527screen_draw_rectangle(row, col, height, width, invert)
7528 int row;
7529 int col;
7530 int height;
7531 int width;
7532 int invert;
7533{
7534 int r, c;
7535 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007536#ifdef FEAT_MBYTE
7537 int max_off;
7538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007540 /* Can't use ScreenLines unless initialized */
7541 if (ScreenLines == NULL)
7542 return;
7543
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (invert)
7545 screen_char_attr = HL_INVERSE;
7546 for (r = row; r < row + height; ++r)
7547 {
7548 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007549#ifdef FEAT_MBYTE
7550 max_off = off + screen_Columns;
7551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 for (c = col; c < col + width; ++c)
7553 {
7554#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007555 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {
7557 screen_char_2(off + c, r, c);
7558 ++c;
7559 }
7560 else
7561#endif
7562 {
7563 screen_char(off + c, r, c);
7564#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007565 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 ++c;
7567#endif
7568 }
7569 }
7570 }
7571 screen_char_attr = 0;
7572}
7573#endif
7574
7575#ifdef FEAT_VERTSPLIT
7576/*
7577 * Redraw the characters for a vertically split window.
7578 */
7579 static void
7580redraw_block(row, end, wp)
7581 int row;
7582 int end;
7583 win_T *wp;
7584{
7585 int col;
7586 int width;
7587
7588# ifdef FEAT_CLIPBOARD
7589 clip_may_clear_selection(row, end - 1);
7590# endif
7591
7592 if (wp == NULL)
7593 {
7594 col = 0;
7595 width = Columns;
7596 }
7597 else
7598 {
7599 col = wp->w_wincol;
7600 width = wp->w_width;
7601 }
7602 screen_draw_rectangle(row, col, end - row, width, FALSE);
7603}
7604#endif
7605
7606/*
7607 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7608 * with character 'c1' in first column followed by 'c2' in the other columns.
7609 * Use attributes 'attr'.
7610 */
7611 void
7612screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7613 int start_row, end_row;
7614 int start_col, end_col;
7615 int c1, c2;
7616 int attr;
7617{
7618 int row;
7619 int col;
7620 int off;
7621 int end_off;
7622 int did_delete;
7623 int c;
7624 int norm_term;
7625#if defined(FEAT_GUI) || defined(UNIX)
7626 int force_next = FALSE;
7627#endif
7628
7629 if (end_row > screen_Rows) /* safety check */
7630 end_row = screen_Rows;
7631 if (end_col > screen_Columns) /* safety check */
7632 end_col = screen_Columns;
7633 if (ScreenLines == NULL
7634 || start_row >= end_row
7635 || start_col >= end_col) /* nothing to do */
7636 return;
7637
7638 /* it's a "normal" terminal when not in a GUI or cterm */
7639 norm_term = (
7640#ifdef FEAT_GUI
7641 !gui.in_use &&
7642#endif
7643 t_colors <= 1);
7644 for (row = start_row; row < end_row; ++row)
7645 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007646#ifdef FEAT_MBYTE
7647 if (has_mbyte
7648# ifdef FEAT_GUI
7649 && !gui.in_use
7650# endif
7651 )
7652 {
7653 /* When drawing over the right halve of a double-wide char clear
7654 * out the left halve. When drawing over the left halve of a
7655 * double wide-char clear out the right halve. Only needed in a
7656 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007657 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007658 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007659 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007660 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007661 }
7662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 /*
7664 * Try to use delete-line termcap code, when no attributes or in a
7665 * "normal" terminal, where a bold/italic space is just a
7666 * space.
7667 */
7668 did_delete = FALSE;
7669 if (c2 == ' '
7670 && end_col == Columns
7671 && can_clear(T_CE)
7672 && (attr == 0
7673 || (norm_term
7674 && attr <= HL_ALL
7675 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7676 {
7677 /*
7678 * check if we really need to clear something
7679 */
7680 col = start_col;
7681 if (c1 != ' ') /* don't clear first char */
7682 ++col;
7683
7684 off = LineOffset[row] + col;
7685 end_off = LineOffset[row] + end_col;
7686
7687 /* skip blanks (used often, keep it fast!) */
7688#ifdef FEAT_MBYTE
7689 if (enc_utf8)
7690 while (off < end_off && ScreenLines[off] == ' '
7691 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7692 ++off;
7693 else
7694#endif
7695 while (off < end_off && ScreenLines[off] == ' '
7696 && ScreenAttrs[off] == 0)
7697 ++off;
7698 if (off < end_off) /* something to be cleared */
7699 {
7700 col = off - LineOffset[row];
7701 screen_stop_highlight();
7702 term_windgoto(row, col);/* clear rest of this screen line */
7703 out_str(T_CE);
7704 screen_start(); /* don't know where cursor is now */
7705 col = end_col - col;
7706 while (col--) /* clear chars in ScreenLines */
7707 {
7708 ScreenLines[off] = ' ';
7709#ifdef FEAT_MBYTE
7710 if (enc_utf8)
7711 ScreenLinesUC[off] = 0;
7712#endif
7713 ScreenAttrs[off] = 0;
7714 ++off;
7715 }
7716 }
7717 did_delete = TRUE; /* the chars are cleared now */
7718 }
7719
7720 off = LineOffset[row] + start_col;
7721 c = c1;
7722 for (col = start_col; col < end_col; ++col)
7723 {
7724 if (ScreenLines[off] != c
7725#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007726 || (enc_utf8 && (int)ScreenLinesUC[off]
7727 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728#endif
7729 || ScreenAttrs[off] != attr
7730#if defined(FEAT_GUI) || defined(UNIX)
7731 || force_next
7732#endif
7733 )
7734 {
7735#if defined(FEAT_GUI) || defined(UNIX)
7736 /* The bold trick may make a single row of pixels appear in
7737 * the next character. When a bold character is removed, the
7738 * next character should be redrawn too. This happens for our
7739 * own GUI and for some xterms. */
7740 if (
7741# ifdef FEAT_GUI
7742 gui.in_use
7743# endif
7744# if defined(FEAT_GUI) && defined(UNIX)
7745 ||
7746# endif
7747# ifdef UNIX
7748 term_is_xterm
7749# endif
7750 )
7751 {
7752 if (ScreenLines[off] != ' '
7753 && (ScreenAttrs[off] > HL_ALL
7754 || ScreenAttrs[off] & HL_BOLD))
7755 force_next = TRUE;
7756 else
7757 force_next = FALSE;
7758 }
7759#endif
7760 ScreenLines[off] = c;
7761#ifdef FEAT_MBYTE
7762 if (enc_utf8)
7763 {
7764 if (c >= 0x80)
7765 {
7766 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007767 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
7769 else
7770 ScreenLinesUC[off] = 0;
7771 }
7772#endif
7773 ScreenAttrs[off] = attr;
7774 if (!did_delete || c != ' ')
7775 screen_char(off, row, col);
7776 }
7777 ++off;
7778 if (col == start_col)
7779 {
7780 if (did_delete)
7781 break;
7782 c = c2;
7783 }
7784 }
7785 if (end_col == Columns)
7786 LineWraps[row] = FALSE;
7787 if (row == Rows - 1) /* overwritten the command line */
7788 {
7789 redraw_cmdline = TRUE;
7790 if (c1 == ' ' && c2 == ' ')
7791 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007792 if (start_col == 0)
7793 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 }
7795 }
7796}
7797
7798/*
7799 * Check if there should be a delay. Used before clearing or redrawing the
7800 * screen or the command line.
7801 */
7802 void
7803check_for_delay(check_msg_scroll)
7804 int check_msg_scroll;
7805{
7806 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7807 && !did_wait_return
7808 && emsg_silent == 0)
7809 {
7810 out_flush();
7811 ui_delay(1000L, TRUE);
7812 emsg_on_display = FALSE;
7813 if (check_msg_scroll)
7814 msg_scroll = FALSE;
7815 }
7816}
7817
7818/*
7819 * screen_valid - allocate screen buffers if size changed
7820 * If "clear" is TRUE: clear screen if it has been resized.
7821 * Returns TRUE if there is a valid screen to write to.
7822 * Returns FALSE when starting up and screen not initialized yet.
7823 */
7824 int
7825screen_valid(clear)
7826 int clear;
7827{
7828 screenalloc(clear); /* allocate screen buffers if size changed */
7829 return (ScreenLines != NULL);
7830}
7831
7832/*
7833 * Resize the shell to Rows and Columns.
7834 * Allocate ScreenLines[] and associated items.
7835 *
7836 * There may be some time between setting Rows and Columns and (re)allocating
7837 * ScreenLines[]. This happens when starting up and when (manually) changing
7838 * the shell size. Always use screen_Rows and screen_Columns to access items
7839 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7840 * final size of the shell is needed.
7841 */
7842 void
7843screenalloc(clear)
7844 int clear;
7845{
7846 int new_row, old_row;
7847#ifdef FEAT_GUI
7848 int old_Rows;
7849#endif
7850 win_T *wp;
7851 int outofmem = FALSE;
7852 int len;
7853 schar_T *new_ScreenLines;
7854#ifdef FEAT_MBYTE
7855 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007856 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007858 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859#endif
7860 sattr_T *new_ScreenAttrs;
7861 unsigned *new_LineOffset;
7862 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007863#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007864 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007865 tabpage_T *tp;
7866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007868 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007869#ifdef FEAT_AUTOCMD
7870 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007872retry:
7873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 /*
7875 * Allocation of the screen buffers is done only when the size changes and
7876 * when Rows and Columns have been set and we have started doing full
7877 * screen stuff.
7878 */
7879 if ((ScreenLines != NULL
7880 && Rows == screen_Rows
7881 && Columns == screen_Columns
7882#ifdef FEAT_MBYTE
7883 && enc_utf8 == (ScreenLinesUC != NULL)
7884 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007885 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886#endif
7887 )
7888 || Rows == 0
7889 || Columns == 0
7890 || (!full_screen && ScreenLines == NULL))
7891 return;
7892
7893 /*
7894 * It's possible that we produce an out-of-memory message below, which
7895 * will cause this function to be called again. To break the loop, just
7896 * return here.
7897 */
7898 if (entered)
7899 return;
7900 entered = TRUE;
7901
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007902 /*
7903 * Note that the window sizes are updated before reallocating the arrays,
7904 * thus we must not redraw here!
7905 */
7906 ++RedrawingDisabled;
7907
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 win_new_shellsize(); /* fit the windows in the new sized shell */
7909
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 comp_col(); /* recompute columns for shown command and ruler */
7911
7912 /*
7913 * We're changing the size of the screen.
7914 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7915 * - Move lines from the old arrays into the new arrays, clear extra
7916 * lines (unless the screen is going to be cleared).
7917 * - Free the old arrays.
7918 *
7919 * If anything fails, make ScreenLines NULL, so we don't do anything!
7920 * Continuing with the old ScreenLines may result in a crash, because the
7921 * size is wrong.
7922 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007923 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007925#ifdef FEAT_AUTOCMD
7926 if (aucmd_win != NULL)
7927 win_free_lsize(aucmd_win);
7928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
7930 new_ScreenLines = (schar_T *)lalloc((long_u)(
7931 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7932#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007933 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 if (enc_utf8)
7935 {
7936 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7937 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007938 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007939 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7941 }
7942 if (enc_dbcs == DBCS_JPNU)
7943 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7944 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7945#endif
7946 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7947 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7948 new_LineOffset = (unsigned *)lalloc((long_u)(
7949 Rows * sizeof(unsigned)), FALSE);
7950 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007951#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007952 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007955 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {
7957 if (win_alloc_lines(wp) == FAIL)
7958 {
7959 outofmem = TRUE;
7960#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007961 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962#endif
7963 }
7964 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007965#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007966 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7967 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007968 outofmem = TRUE;
7969#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007970#ifdef FEAT_WINDOWS
7971give_up:
7972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007974#ifdef FEAT_MBYTE
7975 for (i = 0; i < p_mco; ++i)
7976 if (new_ScreenLinesC[i] == NULL)
7977 break;
7978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 if (new_ScreenLines == NULL
7980#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007981 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7983#endif
7984 || new_ScreenAttrs == NULL
7985 || new_LineOffset == NULL
7986 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007987#ifdef FEAT_WINDOWS
7988 || new_TabPageIdxs == NULL
7989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 || outofmem)
7991 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007992 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007993 {
7994 /* guess the size */
7995 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7996
7997 /* Remember we did this to avoid getting outofmem messages over
7998 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007999 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 vim_free(new_ScreenLines);
8002 new_ScreenLines = NULL;
8003#ifdef FEAT_MBYTE
8004 vim_free(new_ScreenLinesUC);
8005 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008006 for (i = 0; i < p_mco; ++i)
8007 {
8008 vim_free(new_ScreenLinesC[i]);
8009 new_ScreenLinesC[i] = NULL;
8010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 vim_free(new_ScreenLines2);
8012 new_ScreenLines2 = NULL;
8013#endif
8014 vim_free(new_ScreenAttrs);
8015 new_ScreenAttrs = NULL;
8016 vim_free(new_LineOffset);
8017 new_LineOffset = NULL;
8018 vim_free(new_LineWraps);
8019 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008020#ifdef FEAT_WINDOWS
8021 vim_free(new_TabPageIdxs);
8022 new_TabPageIdxs = NULL;
8023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025 else
8026 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008027 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008028
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 for (new_row = 0; new_row < Rows; ++new_row)
8030 {
8031 new_LineOffset[new_row] = new_row * Columns;
8032 new_LineWraps[new_row] = FALSE;
8033
8034 /*
8035 * If the screen is not going to be cleared, copy as much as
8036 * possible from the old screen to the new one and clear the rest
8037 * (used when resizing the window at the "--more--" prompt or when
8038 * executing an external command, for the GUI).
8039 */
8040 if (!clear)
8041 {
8042 (void)vim_memset(new_ScreenLines + new_row * Columns,
8043 ' ', (size_t)Columns * sizeof(schar_T));
8044#ifdef FEAT_MBYTE
8045 if (enc_utf8)
8046 {
8047 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8048 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008049 for (i = 0; i < p_mco; ++i)
8050 (void)vim_memset(new_ScreenLinesC[i]
8051 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 0, (size_t)Columns * sizeof(u8char_T));
8053 }
8054 if (enc_dbcs == DBCS_JPNU)
8055 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8056 0, (size_t)Columns * sizeof(schar_T));
8057#endif
8058 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8059 0, (size_t)Columns * sizeof(sattr_T));
8060 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008061 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 {
8063 if (screen_Columns < Columns)
8064 len = screen_Columns;
8065 else
8066 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008067#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008068 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008069 * may be invalid now. Also when p_mco changes. */
8070 if (!(enc_utf8 && ScreenLinesUC == NULL)
8071 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008072#endif
8073 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8074 ScreenLines + LineOffset[old_row],
8075 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008077 if (enc_utf8 && ScreenLinesUC != NULL
8078 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
8080 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8081 ScreenLinesUC + LineOffset[old_row],
8082 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008083 for (i = 0; i < p_mco; ++i)
8084 mch_memmove(new_ScreenLinesC[i]
8085 + new_LineOffset[new_row],
8086 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 (size_t)len * sizeof(u8char_T));
8088 }
8089 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8090 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8091 ScreenLines2 + LineOffset[old_row],
8092 (size_t)len * sizeof(schar_T));
8093#endif
8094 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8095 ScreenAttrs + LineOffset[old_row],
8096 (size_t)len * sizeof(sattr_T));
8097 }
8098 }
8099 }
8100 /* Use the last line of the screen for the current line. */
8101 current_ScreenLine = new_ScreenLines + Rows * Columns;
8102 }
8103
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008104 free_screenlines();
8105
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 ScreenLines = new_ScreenLines;
8107#ifdef FEAT_MBYTE
8108 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008109 for (i = 0; i < p_mco; ++i)
8110 ScreenLinesC[i] = new_ScreenLinesC[i];
8111 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 ScreenLines2 = new_ScreenLines2;
8113#endif
8114 ScreenAttrs = new_ScreenAttrs;
8115 LineOffset = new_LineOffset;
8116 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008117#ifdef FEAT_WINDOWS
8118 TabPageIdxs = new_TabPageIdxs;
8119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
8121 /* It's important that screen_Rows and screen_Columns reflect the actual
8122 * size of ScreenLines[]. Set them before calling anything. */
8123#ifdef FEAT_GUI
8124 old_Rows = screen_Rows;
8125#endif
8126 screen_Rows = Rows;
8127 screen_Columns = Columns;
8128
8129 must_redraw = CLEAR; /* need to clear the screen later */
8130 if (clear)
8131 screenclear2();
8132
8133#ifdef FEAT_GUI
8134 else if (gui.in_use
8135 && !gui.starting
8136 && ScreenLines != NULL
8137 && old_Rows != Rows)
8138 {
8139 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8140 /*
8141 * Adjust the position of the cursor, for when executing an external
8142 * command.
8143 */
8144 if (msg_row >= Rows) /* Rows got smaller */
8145 msg_row = Rows - 1; /* put cursor at last row */
8146 else if (Rows > old_Rows) /* Rows got bigger */
8147 msg_row += Rows - old_Rows; /* put cursor in same place */
8148 if (msg_col >= Columns) /* Columns got smaller */
8149 msg_col = Columns - 1; /* put cursor at last column */
8150 }
8151#endif
8152
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008154 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008155
8156#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008157 /*
8158 * Do not apply autocommands more than 3 times to avoid an endless loop
8159 * in case applying autocommands always changes Rows or Columns.
8160 */
8161 if (starting == 0 && ++retry_count <= 3)
8162 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008163 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008164 /* In rare cases, autocommands may have altered Rows or Columns,
8165 * jump back to check if we need to allocate the screen again. */
8166 goto retry;
8167 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169}
8170
8171 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008172free_screenlines()
8173{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008174#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008175 int i;
8176
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008177 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008178 for (i = 0; i < Screen_mco; ++i)
8179 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008180 vim_free(ScreenLines2);
8181#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008182 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008183 vim_free(ScreenAttrs);
8184 vim_free(LineOffset);
8185 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008186#ifdef FEAT_WINDOWS
8187 vim_free(TabPageIdxs);
8188#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008189}
8190
8191 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192screenclear()
8193{
8194 check_for_delay(FALSE);
8195 screenalloc(FALSE); /* allocate screen buffers if size changed */
8196 screenclear2(); /* clear the screen */
8197}
8198
8199 static void
8200screenclear2()
8201{
8202 int i;
8203
8204 if (starting == NO_SCREEN || ScreenLines == NULL
8205#ifdef FEAT_GUI
8206 || (gui.in_use && gui.starting)
8207#endif
8208 )
8209 return;
8210
8211#ifdef FEAT_GUI
8212 if (!gui.in_use)
8213#endif
8214 screen_attr = -1; /* force setting the Normal colors */
8215 screen_stop_highlight(); /* don't want highlighting here */
8216
8217#ifdef FEAT_CLIPBOARD
8218 /* disable selection without redrawing it */
8219 clip_scroll_selection(9999);
8220#endif
8221
8222 /* blank out ScreenLines */
8223 for (i = 0; i < Rows; ++i)
8224 {
8225 lineclear(LineOffset[i], (int)Columns);
8226 LineWraps[i] = FALSE;
8227 }
8228
8229 if (can_clear(T_CL))
8230 {
8231 out_str(T_CL); /* clear the display */
8232 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008233 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 }
8235 else
8236 {
8237 /* can't clear the screen, mark all chars with invalid attributes */
8238 for (i = 0; i < Rows; ++i)
8239 lineinvalid(LineOffset[i], (int)Columns);
8240 clear_cmdline = TRUE;
8241 }
8242
8243 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8244
8245 win_rest_invalid(firstwin);
8246 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008247#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008248 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 if (must_redraw == CLEAR) /* no need to clear again */
8251 must_redraw = NOT_VALID;
8252 compute_cmdrow();
8253 msg_row = cmdline_row; /* put cursor on last line for messages */
8254 msg_col = 0;
8255 screen_start(); /* don't know where cursor is now */
8256 msg_scrolled = 0; /* can't scroll back */
8257 msg_didany = FALSE;
8258 msg_didout = FALSE;
8259}
8260
8261/*
8262 * Clear one line in ScreenLines.
8263 */
8264 static void
8265lineclear(off, width)
8266 unsigned off;
8267 int width;
8268{
8269 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8270#ifdef FEAT_MBYTE
8271 if (enc_utf8)
8272 (void)vim_memset(ScreenLinesUC + off, 0,
8273 (size_t)width * sizeof(u8char_T));
8274#endif
8275 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8276}
8277
8278/*
8279 * Mark one line in ScreenLines invalid by setting the attributes to an
8280 * invalid value.
8281 */
8282 static void
8283lineinvalid(off, width)
8284 unsigned off;
8285 int width;
8286{
8287 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8288}
8289
8290#ifdef FEAT_VERTSPLIT
8291/*
8292 * Copy part of a Screenline for vertically split window "wp".
8293 */
8294 static void
8295linecopy(to, from, wp)
8296 int to;
8297 int from;
8298 win_T *wp;
8299{
8300 unsigned off_to = LineOffset[to] + wp->w_wincol;
8301 unsigned off_from = LineOffset[from] + wp->w_wincol;
8302
8303 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8304 wp->w_width * sizeof(schar_T));
8305# ifdef FEAT_MBYTE
8306 if (enc_utf8)
8307 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008308 int i;
8309
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8311 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008312 for (i = 0; i < p_mco; ++i)
8313 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8314 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 }
8316 if (enc_dbcs == DBCS_JPNU)
8317 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8318 wp->w_width * sizeof(schar_T));
8319# endif
8320 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8321 wp->w_width * sizeof(sattr_T));
8322}
8323#endif
8324
8325/*
8326 * Return TRUE if clearing with term string "p" would work.
8327 * It can't work when the string is empty or it won't set the right background.
8328 */
8329 int
8330can_clear(p)
8331 char_u *p;
8332{
8333 return (*p != NUL && (t_colors <= 1
8334#ifdef FEAT_GUI
8335 || gui.in_use
8336#endif
8337 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8338}
8339
8340/*
8341 * Reset cursor position. Use whenever cursor was moved because of outputting
8342 * something directly to the screen (shell commands) or a terminal control
8343 * code.
8344 */
8345 void
8346screen_start()
8347{
8348 screen_cur_row = screen_cur_col = 9999;
8349}
8350
8351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 * Move the cursor to position "row","col" in the screen.
8353 * This tries to find the most efficient way to move, minimizing the number of
8354 * characters sent to the terminal.
8355 */
8356 void
8357windgoto(row, col)
8358 int row;
8359 int col;
8360{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008361 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 int i;
8363 int plan;
8364 int cost;
8365 int wouldbe_col;
8366 int noinvcurs;
8367 char_u *bs;
8368 int goto_cost;
8369 int attr;
8370
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008371#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8373
8374#define PLAN_LE 1
8375#define PLAN_CR 2
8376#define PLAN_NL 3
8377#define PLAN_WRITE 4
8378 /* Can't use ScreenLines unless initialized */
8379 if (ScreenLines == NULL)
8380 return;
8381
8382 if (col != screen_cur_col || row != screen_cur_row)
8383 {
8384 /* Check for valid position. */
8385 if (row < 0) /* window without text lines? */
8386 row = 0;
8387 if (row >= screen_Rows)
8388 row = screen_Rows - 1;
8389 if (col >= screen_Columns)
8390 col = screen_Columns - 1;
8391
8392 /* check if no cursor movement is allowed in highlight mode */
8393 if (screen_attr && *T_MS == NUL)
8394 noinvcurs = HIGHL_COST;
8395 else
8396 noinvcurs = 0;
8397 goto_cost = GOTO_COST + noinvcurs;
8398
8399 /*
8400 * Plan how to do the positioning:
8401 * 1. Use CR to move it to column 0, same row.
8402 * 2. Use T_LE to move it a few columns to the left.
8403 * 3. Use NL to move a few lines down, column 0.
8404 * 4. Move a few columns to the right with T_ND or by writing chars.
8405 *
8406 * Don't do this if the cursor went beyond the last column, the cursor
8407 * position is unknown then (some terminals wrap, some don't )
8408 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008409 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 * characters to move the cursor to the right.
8411 */
8412 if (row >= screen_cur_row && screen_cur_col < Columns)
8413 {
8414 /*
8415 * If the cursor is in the same row, bigger col, we can use CR
8416 * or T_LE.
8417 */
8418 bs = NULL; /* init for GCC */
8419 attr = screen_attr;
8420 if (row == screen_cur_row && col < screen_cur_col)
8421 {
8422 /* "le" is preferred over "bc", because "bc" is obsolete */
8423 if (*T_LE)
8424 bs = T_LE; /* "cursor left" */
8425 else
8426 bs = T_BC; /* "backspace character (old) */
8427 if (*bs)
8428 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8429 else
8430 cost = 999;
8431 if (col + 1 < cost) /* using CR is less characters */
8432 {
8433 plan = PLAN_CR;
8434 wouldbe_col = 0;
8435 cost = 1; /* CR is just one character */
8436 }
8437 else
8438 {
8439 plan = PLAN_LE;
8440 wouldbe_col = col;
8441 }
8442 if (noinvcurs) /* will stop highlighting */
8443 {
8444 cost += noinvcurs;
8445 attr = 0;
8446 }
8447 }
8448
8449 /*
8450 * If the cursor is above where we want to be, we can use CR LF.
8451 */
8452 else if (row > screen_cur_row)
8453 {
8454 plan = PLAN_NL;
8455 wouldbe_col = 0;
8456 cost = (row - screen_cur_row) * 2; /* CR LF */
8457 if (noinvcurs) /* will stop highlighting */
8458 {
8459 cost += noinvcurs;
8460 attr = 0;
8461 }
8462 }
8463
8464 /*
8465 * If the cursor is in the same row, smaller col, just use write.
8466 */
8467 else
8468 {
8469 plan = PLAN_WRITE;
8470 wouldbe_col = screen_cur_col;
8471 cost = 0;
8472 }
8473
8474 /*
8475 * Check if any characters that need to be written have the
8476 * correct attributes. Also avoid UTF-8 characters.
8477 */
8478 i = col - wouldbe_col;
8479 if (i > 0)
8480 cost += i;
8481 if (cost < goto_cost && i > 0)
8482 {
8483 /*
8484 * Check if the attributes are correct without additionally
8485 * stopping highlighting.
8486 */
8487 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8488 while (i && *p++ == attr)
8489 --i;
8490 if (i != 0)
8491 {
8492 /*
8493 * Try if it works when highlighting is stopped here.
8494 */
8495 if (*--p == 0)
8496 {
8497 cost += noinvcurs;
8498 while (i && *p++ == 0)
8499 --i;
8500 }
8501 if (i != 0)
8502 cost = 999; /* different attributes, don't do it */
8503 }
8504#ifdef FEAT_MBYTE
8505 if (enc_utf8)
8506 {
8507 /* Don't use an UTF-8 char for positioning, it's slow. */
8508 for (i = wouldbe_col; i < col; ++i)
8509 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8510 {
8511 cost = 999;
8512 break;
8513 }
8514 }
8515#endif
8516 }
8517
8518 /*
8519 * We can do it without term_windgoto()!
8520 */
8521 if (cost < goto_cost)
8522 {
8523 if (plan == PLAN_LE)
8524 {
8525 if (noinvcurs)
8526 screen_stop_highlight();
8527 while (screen_cur_col > col)
8528 {
8529 out_str(bs);
8530 --screen_cur_col;
8531 }
8532 }
8533 else if (plan == PLAN_CR)
8534 {
8535 if (noinvcurs)
8536 screen_stop_highlight();
8537 out_char('\r');
8538 screen_cur_col = 0;
8539 }
8540 else if (plan == PLAN_NL)
8541 {
8542 if (noinvcurs)
8543 screen_stop_highlight();
8544 while (screen_cur_row < row)
8545 {
8546 out_char('\n');
8547 ++screen_cur_row;
8548 }
8549 screen_cur_col = 0;
8550 }
8551
8552 i = col - screen_cur_col;
8553 if (i > 0)
8554 {
8555 /*
8556 * Use cursor-right if it's one character only. Avoids
8557 * removing a line of pixels from the last bold char, when
8558 * using the bold trick in the GUI.
8559 */
8560 if (T_ND[0] != NUL && T_ND[1] == NUL)
8561 {
8562 while (i-- > 0)
8563 out_char(*T_ND);
8564 }
8565 else
8566 {
8567 int off;
8568
8569 off = LineOffset[row] + screen_cur_col;
8570 while (i-- > 0)
8571 {
8572 if (ScreenAttrs[off] != screen_attr)
8573 screen_stop_highlight();
8574#ifdef FEAT_MBYTE
8575 out_flush_check();
8576#endif
8577 out_char(ScreenLines[off]);
8578#ifdef FEAT_MBYTE
8579 if (enc_dbcs == DBCS_JPNU
8580 && ScreenLines[off] == 0x8e)
8581 out_char(ScreenLines2[off]);
8582#endif
8583 ++off;
8584 }
8585 }
8586 }
8587 }
8588 }
8589 else
8590 cost = 999;
8591
8592 if (cost >= goto_cost)
8593 {
8594 if (noinvcurs)
8595 screen_stop_highlight();
8596 if (row == screen_cur_row && (col > screen_cur_col) &&
8597 *T_CRI != NUL)
8598 term_cursor_right(col - screen_cur_col);
8599 else
8600 term_windgoto(row, col);
8601 }
8602 screen_cur_row = row;
8603 screen_cur_col = col;
8604 }
8605}
8606
8607/*
8608 * Set cursor to its position in the current window.
8609 */
8610 void
8611setcursor()
8612{
8613 if (redrawing())
8614 {
8615 validate_cursor();
8616 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8617 W_WINCOL(curwin) + (
8618#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008619 /* With 'rightleft' set and the cursor on a double-wide
8620 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8622# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008623 (has_mbyte
8624 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8625 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626# endif
8627 1)) :
8628#endif
8629 curwin->w_wcol));
8630 }
8631}
8632
8633
8634/*
8635 * insert 'line_count' lines at 'row' in window 'wp'
8636 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8637 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8638 * scrolling.
8639 * Returns FAIL if the lines are not inserted, OK for success.
8640 */
8641 int
8642win_ins_lines(wp, row, line_count, invalid, mayclear)
8643 win_T *wp;
8644 int row;
8645 int line_count;
8646 int invalid;
8647 int mayclear;
8648{
8649 int did_delete;
8650 int nextrow;
8651 int lastrow;
8652 int retval;
8653
8654 if (invalid)
8655 wp->w_lines_valid = 0;
8656
8657 if (wp->w_height < 5)
8658 return FAIL;
8659
8660 if (line_count > wp->w_height - row)
8661 line_count = wp->w_height - row;
8662
8663 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8664 if (retval != MAYBE)
8665 return retval;
8666
8667 /*
8668 * If there is a next window or a status line, we first try to delete the
8669 * lines at the bottom to avoid messing what is after the window.
8670 * If this fails and there are following windows, don't do anything to avoid
8671 * messing up those windows, better just redraw.
8672 */
8673 did_delete = FALSE;
8674#ifdef FEAT_WINDOWS
8675 if (wp->w_next != NULL || wp->w_status_height)
8676 {
8677 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8678 line_count, (int)Rows, FALSE, NULL) == OK)
8679 did_delete = TRUE;
8680 else if (wp->w_next)
8681 return FAIL;
8682 }
8683#endif
8684 /*
8685 * if no lines deleted, blank the lines that will end up below the window
8686 */
8687 if (!did_delete)
8688 {
8689#ifdef FEAT_WINDOWS
8690 wp->w_redr_status = TRUE;
8691#endif
8692 redraw_cmdline = TRUE;
8693 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8694 lastrow = nextrow + line_count;
8695 if (lastrow > Rows)
8696 lastrow = Rows;
8697 screen_fill(nextrow - line_count, lastrow - line_count,
8698 W_WINCOL(wp), (int)W_ENDCOL(wp),
8699 ' ', ' ', 0);
8700 }
8701
8702 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8703 == FAIL)
8704 {
8705 /* deletion will have messed up other windows */
8706 if (did_delete)
8707 {
8708#ifdef FEAT_WINDOWS
8709 wp->w_redr_status = TRUE;
8710#endif
8711 win_rest_invalid(W_NEXT(wp));
8712 }
8713 return FAIL;
8714 }
8715
8716 return OK;
8717}
8718
8719/*
8720 * delete "line_count" window lines at "row" in window "wp"
8721 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8722 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8723 * scrolling
8724 * Return OK for success, FAIL if the lines are not deleted.
8725 */
8726 int
8727win_del_lines(wp, row, line_count, invalid, mayclear)
8728 win_T *wp;
8729 int row;
8730 int line_count;
8731 int invalid;
8732 int mayclear;
8733{
8734 int retval;
8735
8736 if (invalid)
8737 wp->w_lines_valid = 0;
8738
8739 if (line_count > wp->w_height - row)
8740 line_count = wp->w_height - row;
8741
8742 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8743 if (retval != MAYBE)
8744 return retval;
8745
8746 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8747 (int)Rows, FALSE, NULL) == FAIL)
8748 return FAIL;
8749
8750#ifdef FEAT_WINDOWS
8751 /*
8752 * If there are windows or status lines below, try to put them at the
8753 * correct place. If we can't do that, they have to be redrawn.
8754 */
8755 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8756 {
8757 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8758 line_count, (int)Rows, NULL) == FAIL)
8759 {
8760 wp->w_redr_status = TRUE;
8761 win_rest_invalid(wp->w_next);
8762 }
8763 }
8764 /*
8765 * If this is the last window and there is no status line, redraw the
8766 * command line later.
8767 */
8768 else
8769#endif
8770 redraw_cmdline = TRUE;
8771 return OK;
8772}
8773
8774/*
8775 * Common code for win_ins_lines() and win_del_lines().
8776 * Returns OK or FAIL when the work has been done.
8777 * Returns MAYBE when not finished yet.
8778 */
8779 static int
8780win_do_lines(wp, row, line_count, mayclear, del)
8781 win_T *wp;
8782 int row;
8783 int line_count;
8784 int mayclear;
8785 int del;
8786{
8787 int retval;
8788
8789 if (!redrawing() || line_count <= 0)
8790 return FAIL;
8791
8792 /* only a few lines left: redraw is faster */
8793 if (mayclear && Rows - line_count < 5
8794#ifdef FEAT_VERTSPLIT
8795 && wp->w_width == Columns
8796#endif
8797 )
8798 {
8799 screenclear(); /* will set wp->w_lines_valid to 0 */
8800 return FAIL;
8801 }
8802
8803 /*
8804 * Delete all remaining lines
8805 */
8806 if (row + line_count >= wp->w_height)
8807 {
8808 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8809 W_WINCOL(wp), (int)W_ENDCOL(wp),
8810 ' ', ' ', 0);
8811 return OK;
8812 }
8813
8814 /*
8815 * when scrolling, the message on the command line should be cleared,
8816 * otherwise it will stay there forever.
8817 */
8818 clear_cmdline = TRUE;
8819
8820 /*
8821 * If the terminal can set a scroll region, use that.
8822 * Always do this in a vertically split window. This will redraw from
8823 * ScreenLines[] when t_CV isn't defined. That's faster than using
8824 * win_line().
8825 * Don't use a scroll region when we are going to redraw the text, writing
8826 * a character in the lower right corner of the scroll region causes a
8827 * scroll-up in the DJGPP version.
8828 */
8829 if (scroll_region
8830#ifdef FEAT_VERTSPLIT
8831 || W_WIDTH(wp) != Columns
8832#endif
8833 )
8834 {
8835#ifdef FEAT_VERTSPLIT
8836 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8837#endif
8838 scroll_region_set(wp, row);
8839 if (del)
8840 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8841 wp->w_height - row, FALSE, wp);
8842 else
8843 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8844 wp->w_height - row, wp);
8845#ifdef FEAT_VERTSPLIT
8846 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8847#endif
8848 scroll_region_reset();
8849 return retval;
8850 }
8851
8852#ifdef FEAT_WINDOWS
8853 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8854 return FAIL;
8855#endif
8856
8857 return MAYBE;
8858}
8859
8860/*
8861 * window 'wp' and everything after it is messed up, mark it for redraw
8862 */
8863 static void
8864win_rest_invalid(wp)
8865 win_T *wp;
8866{
8867#ifdef FEAT_WINDOWS
8868 while (wp != NULL)
8869#else
8870 if (wp != NULL)
8871#endif
8872 {
8873 redraw_win_later(wp, NOT_VALID);
8874#ifdef FEAT_WINDOWS
8875 wp->w_redr_status = TRUE;
8876 wp = wp->w_next;
8877#endif
8878 }
8879 redraw_cmdline = TRUE;
8880}
8881
8882/*
8883 * The rest of the routines in this file perform screen manipulations. The
8884 * given operation is performed physically on the screen. The corresponding
8885 * change is also made to the internal screen image. In this way, the editor
8886 * anticipates the effect of editing changes on the appearance of the screen.
8887 * That way, when we call screenupdate a complete redraw isn't usually
8888 * necessary. Another advantage is that we can keep adding code to anticipate
8889 * screen changes, and in the meantime, everything still works.
8890 */
8891
8892/*
8893 * types for inserting or deleting lines
8894 */
8895#define USE_T_CAL 1
8896#define USE_T_CDL 2
8897#define USE_T_AL 3
8898#define USE_T_CE 4
8899#define USE_T_DL 5
8900#define USE_T_SR 6
8901#define USE_NL 7
8902#define USE_T_CD 8
8903#define USE_REDRAW 9
8904
8905/*
8906 * insert lines on the screen and update ScreenLines[]
8907 * 'end' is the line after the scrolled part. Normally it is Rows.
8908 * When scrolling region used 'off' is the offset from the top for the region.
8909 * 'row' and 'end' are relative to the start of the region.
8910 *
8911 * return FAIL for failure, OK for success.
8912 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008913 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914screen_ins_lines(off, row, line_count, end, wp)
8915 int off;
8916 int row;
8917 int line_count;
8918 int end;
8919 win_T *wp; /* NULL or window to use width from */
8920{
8921 int i;
8922 int j;
8923 unsigned temp;
8924 int cursor_row;
8925 int type;
8926 int result_empty;
8927 int can_ce = can_clear(T_CE);
8928
8929 /*
8930 * FAIL if
8931 * - there is no valid screen
8932 * - the screen has to be redrawn completely
8933 * - the line count is less than one
8934 * - the line count is more than 'ttyscroll'
8935 */
8936 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8937 return FAIL;
8938
8939 /*
8940 * There are seven ways to insert lines:
8941 * 0. When in a vertically split window and t_CV isn't set, redraw the
8942 * characters from ScreenLines[].
8943 * 1. Use T_CD (clear to end of display) if it exists and the result of
8944 * the insert is just empty lines
8945 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8946 * present or line_count > 1. It looks better if we do all the inserts
8947 * at once.
8948 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8949 * insert is just empty lines and T_CE is not present or line_count >
8950 * 1.
8951 * 4. Use T_AL (insert line) if it exists.
8952 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8953 * just empty lines.
8954 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8955 * just empty lines.
8956 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8957 * the 'da' flag is not set or we have clear line capability.
8958 * 8. redraw the characters from ScreenLines[].
8959 *
8960 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8961 * the scrollbar for the window. It does have insert line, use that if it
8962 * exists.
8963 */
8964 result_empty = (row + line_count >= end);
8965#ifdef FEAT_VERTSPLIT
8966 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8967 type = USE_REDRAW;
8968 else
8969#endif
8970 if (can_clear(T_CD) && result_empty)
8971 type = USE_T_CD;
8972 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8973 type = USE_T_CAL;
8974 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8975 type = USE_T_CDL;
8976 else if (*T_AL != NUL)
8977 type = USE_T_AL;
8978 else if (can_ce && result_empty)
8979 type = USE_T_CE;
8980 else if (*T_DL != NUL && result_empty)
8981 type = USE_T_DL;
8982 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8983 type = USE_T_SR;
8984 else
8985 return FAIL;
8986
8987 /*
8988 * For clearing the lines screen_del_lines() is used. This will also take
8989 * care of t_db if necessary.
8990 */
8991 if (type == USE_T_CD || type == USE_T_CDL ||
8992 type == USE_T_CE || type == USE_T_DL)
8993 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8994
8995 /*
8996 * If text is retained below the screen, first clear or delete as many
8997 * lines at the bottom of the window as are about to be inserted so that
8998 * the deleted lines won't later surface during a screen_del_lines.
8999 */
9000 if (*T_DB)
9001 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9002
9003#ifdef FEAT_CLIPBOARD
9004 /* Remove a modeless selection when inserting lines halfway the screen
9005 * or not the full width of the screen. */
9006 if (off + row > 0
9007# ifdef FEAT_VERTSPLIT
9008 || (wp != NULL && wp->w_width != Columns)
9009# endif
9010 )
9011 clip_clear_selection();
9012 else
9013 clip_scroll_selection(-line_count);
9014#endif
9015
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016#ifdef FEAT_GUI
9017 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9018 * scrolling is actually carried out. */
9019 gui_dont_update_cursor();
9020#endif
9021
9022 if (*T_CCS != NUL) /* cursor relative to region */
9023 cursor_row = row;
9024 else
9025 cursor_row = row + off;
9026
9027 /*
9028 * Shift LineOffset[] line_count down to reflect the inserted lines.
9029 * Clear the inserted lines in ScreenLines[].
9030 */
9031 row += off;
9032 end += off;
9033 for (i = 0; i < line_count; ++i)
9034 {
9035#ifdef FEAT_VERTSPLIT
9036 if (wp != NULL && wp->w_width != Columns)
9037 {
9038 /* need to copy part of a line */
9039 j = end - 1 - i;
9040 while ((j -= line_count) >= row)
9041 linecopy(j + line_count, j, wp);
9042 j += line_count;
9043 if (can_clear((char_u *)" "))
9044 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9045 else
9046 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9047 LineWraps[j] = FALSE;
9048 }
9049 else
9050#endif
9051 {
9052 j = end - 1 - i;
9053 temp = LineOffset[j];
9054 while ((j -= line_count) >= row)
9055 {
9056 LineOffset[j + line_count] = LineOffset[j];
9057 LineWraps[j + line_count] = LineWraps[j];
9058 }
9059 LineOffset[j + line_count] = temp;
9060 LineWraps[j + line_count] = FALSE;
9061 if (can_clear((char_u *)" "))
9062 lineclear(temp, (int)Columns);
9063 else
9064 lineinvalid(temp, (int)Columns);
9065 }
9066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
9068 screen_stop_highlight();
9069 windgoto(cursor_row, 0);
9070
9071#ifdef FEAT_VERTSPLIT
9072 /* redraw the characters */
9073 if (type == USE_REDRAW)
9074 redraw_block(row, end, wp);
9075 else
9076#endif
9077 if (type == USE_T_CAL)
9078 {
9079 term_append_lines(line_count);
9080 screen_start(); /* don't know where cursor is now */
9081 }
9082 else
9083 {
9084 for (i = 0; i < line_count; i++)
9085 {
9086 if (type == USE_T_AL)
9087 {
9088 if (i && cursor_row != 0)
9089 windgoto(cursor_row, 0);
9090 out_str(T_AL);
9091 }
9092 else /* type == USE_T_SR */
9093 out_str(T_SR);
9094 screen_start(); /* don't know where cursor is now */
9095 }
9096 }
9097
9098 /*
9099 * With scroll-reverse and 'da' flag set we need to clear the lines that
9100 * have been scrolled down into the region.
9101 */
9102 if (type == USE_T_SR && *T_DA)
9103 {
9104 for (i = 0; i < line_count; ++i)
9105 {
9106 windgoto(off + i, 0);
9107 out_str(T_CE);
9108 screen_start(); /* don't know where cursor is now */
9109 }
9110 }
9111
9112#ifdef FEAT_GUI
9113 gui_can_update_cursor();
9114 if (gui.in_use)
9115 out_flush(); /* always flush after a scroll */
9116#endif
9117 return OK;
9118}
9119
9120/*
9121 * delete lines on the screen and update ScreenLines[]
9122 * 'end' is the line after the scrolled part. Normally it is Rows.
9123 * When scrolling region used 'off' is the offset from the top for the region.
9124 * 'row' and 'end' are relative to the start of the region.
9125 *
9126 * Return OK for success, FAIL if the lines are not deleted.
9127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 int
9129screen_del_lines(off, row, line_count, end, force, wp)
9130 int off;
9131 int row;
9132 int line_count;
9133 int end;
9134 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009135 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 int j;
9138 int i;
9139 unsigned temp;
9140 int cursor_row;
9141 int cursor_end;
9142 int result_empty; /* result is empty until end of region */
9143 int can_delete; /* deleting line codes can be used */
9144 int type;
9145
9146 /*
9147 * FAIL if
9148 * - there is no valid screen
9149 * - the screen has to be redrawn completely
9150 * - the line count is less than one
9151 * - the line count is more than 'ttyscroll'
9152 */
9153 if (!screen_valid(TRUE) || line_count <= 0 ||
9154 (!force && line_count > p_ttyscroll))
9155 return FAIL;
9156
9157 /*
9158 * Check if the rest of the current region will become empty.
9159 */
9160 result_empty = row + line_count >= end;
9161
9162 /*
9163 * We can delete lines only when 'db' flag not set or when 'ce' option
9164 * available.
9165 */
9166 can_delete = (*T_DB == NUL || can_clear(T_CE));
9167
9168 /*
9169 * There are six ways to delete lines:
9170 * 0. When in a vertically split window and t_CV isn't set, redraw the
9171 * characters from ScreenLines[].
9172 * 1. Use T_CD if it exists and the result is empty.
9173 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9174 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9175 * none of the other ways work.
9176 * 4. Use T_CE (erase line) if the result is empty.
9177 * 5. Use T_DL (delete line) if it exists.
9178 * 6. redraw the characters from ScreenLines[].
9179 */
9180#ifdef FEAT_VERTSPLIT
9181 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9182 type = USE_REDRAW;
9183 else
9184#endif
9185 if (can_clear(T_CD) && result_empty)
9186 type = USE_T_CD;
9187#if defined(__BEOS__) && defined(BEOS_DR8)
9188 /*
9189 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9190 * its internal termcap... this works okay for tests which test *T_DB !=
9191 * NUL. It has the disadvantage that the user cannot use any :set t_*
9192 * command to get T_DB (back) to empty_option, only :set term=... will do
9193 * the trick...
9194 * Anyway, this hack will hopefully go away with the next OS release.
9195 * (Olaf Seibert)
9196 */
9197 else if (row == 0 && T_DB == empty_option
9198 && (line_count == 1 || *T_CDL == NUL))
9199#else
9200 else if (row == 0 && (
9201#ifndef AMIGA
9202 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9203 * up, so use delete-line command */
9204 line_count == 1 ||
9205#endif
9206 *T_CDL == NUL))
9207#endif
9208 type = USE_NL;
9209 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9210 type = USE_T_CDL;
9211 else if (can_clear(T_CE) && result_empty
9212#ifdef FEAT_VERTSPLIT
9213 && (wp == NULL || wp->w_width == Columns)
9214#endif
9215 )
9216 type = USE_T_CE;
9217 else if (*T_DL != NUL && can_delete)
9218 type = USE_T_DL;
9219 else if (*T_CDL != NUL && can_delete)
9220 type = USE_T_CDL;
9221 else
9222 return FAIL;
9223
9224#ifdef FEAT_CLIPBOARD
9225 /* Remove a modeless selection when deleting lines halfway the screen or
9226 * not the full width of the screen. */
9227 if (off + row > 0
9228# ifdef FEAT_VERTSPLIT
9229 || (wp != NULL && wp->w_width != Columns)
9230# endif
9231 )
9232 clip_clear_selection();
9233 else
9234 clip_scroll_selection(line_count);
9235#endif
9236
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237#ifdef FEAT_GUI
9238 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9239 * scrolling is actually carried out. */
9240 gui_dont_update_cursor();
9241#endif
9242
9243 if (*T_CCS != NUL) /* cursor relative to region */
9244 {
9245 cursor_row = row;
9246 cursor_end = end;
9247 }
9248 else
9249 {
9250 cursor_row = row + off;
9251 cursor_end = end + off;
9252 }
9253
9254 /*
9255 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9256 * Clear the inserted lines in ScreenLines[].
9257 */
9258 row += off;
9259 end += off;
9260 for (i = 0; i < line_count; ++i)
9261 {
9262#ifdef FEAT_VERTSPLIT
9263 if (wp != NULL && wp->w_width != Columns)
9264 {
9265 /* need to copy part of a line */
9266 j = row + i;
9267 while ((j += line_count) <= end - 1)
9268 linecopy(j - line_count, j, wp);
9269 j -= line_count;
9270 if (can_clear((char_u *)" "))
9271 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9272 else
9273 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9274 LineWraps[j] = FALSE;
9275 }
9276 else
9277#endif
9278 {
9279 /* whole width, moving the line pointers is faster */
9280 j = row + i;
9281 temp = LineOffset[j];
9282 while ((j += line_count) <= end - 1)
9283 {
9284 LineOffset[j - line_count] = LineOffset[j];
9285 LineWraps[j - line_count] = LineWraps[j];
9286 }
9287 LineOffset[j - line_count] = temp;
9288 LineWraps[j - line_count] = FALSE;
9289 if (can_clear((char_u *)" "))
9290 lineclear(temp, (int)Columns);
9291 else
9292 lineinvalid(temp, (int)Columns);
9293 }
9294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295
9296 screen_stop_highlight();
9297
9298#ifdef FEAT_VERTSPLIT
9299 /* redraw the characters */
9300 if (type == USE_REDRAW)
9301 redraw_block(row, end, wp);
9302 else
9303#endif
9304 if (type == USE_T_CD) /* delete the lines */
9305 {
9306 windgoto(cursor_row, 0);
9307 out_str(T_CD);
9308 screen_start(); /* don't know where cursor is now */
9309 }
9310 else if (type == USE_T_CDL)
9311 {
9312 windgoto(cursor_row, 0);
9313 term_delete_lines(line_count);
9314 screen_start(); /* don't know where cursor is now */
9315 }
9316 /*
9317 * Deleting lines at top of the screen or scroll region: Just scroll
9318 * the whole screen (scroll region) up by outputting newlines on the
9319 * last line.
9320 */
9321 else if (type == USE_NL)
9322 {
9323 windgoto(cursor_end - 1, 0);
9324 for (i = line_count; --i >= 0; )
9325 out_char('\n'); /* cursor will remain on same line */
9326 }
9327 else
9328 {
9329 for (i = line_count; --i >= 0; )
9330 {
9331 if (type == USE_T_DL)
9332 {
9333 windgoto(cursor_row, 0);
9334 out_str(T_DL); /* delete a line */
9335 }
9336 else /* type == USE_T_CE */
9337 {
9338 windgoto(cursor_row + i, 0);
9339 out_str(T_CE); /* erase a line */
9340 }
9341 screen_start(); /* don't know where cursor is now */
9342 }
9343 }
9344
9345 /*
9346 * If the 'db' flag is set, we need to clear the lines that have been
9347 * scrolled up at the bottom of the region.
9348 */
9349 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9350 {
9351 for (i = line_count; i > 0; --i)
9352 {
9353 windgoto(cursor_end - i, 0);
9354 out_str(T_CE); /* erase a line */
9355 screen_start(); /* don't know where cursor is now */
9356 }
9357 }
9358
9359#ifdef FEAT_GUI
9360 gui_can_update_cursor();
9361 if (gui.in_use)
9362 out_flush(); /* always flush after a scroll */
9363#endif
9364
9365 return OK;
9366}
9367
9368/*
9369 * show the current mode and ruler
9370 *
9371 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9372 * If clear_cmdline is FALSE there may be a message there that needs to be
9373 * cleared only if a mode is shown.
9374 * Return the length of the message (0 if no message).
9375 */
9376 int
9377showmode()
9378{
9379 int need_clear;
9380 int length = 0;
9381 int do_mode;
9382 int attr;
9383 int nwr_save;
9384#ifdef FEAT_INS_EXPAND
9385 int sub_attr;
9386#endif
9387
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009388 do_mode = ((p_smd && msg_silent == 0)
9389 && ((State & INSERT)
9390 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391#ifdef FEAT_VISUAL
9392 || VIsual_active
9393#endif
9394 ));
9395 if (do_mode || Recording)
9396 {
9397 /*
9398 * Don't show mode right now, when not redrawing or inside a mapping.
9399 * Call char_avail() only when we are going to show something, because
9400 * it takes a bit of time.
9401 */
9402 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9403 {
9404 redraw_cmdline = TRUE; /* show mode later */
9405 return 0;
9406 }
9407
9408 nwr_save = need_wait_return;
9409
9410 /* wait a bit before overwriting an important message */
9411 check_for_delay(FALSE);
9412
9413 /* if the cmdline is more than one line high, erase top lines */
9414 need_clear = clear_cmdline;
9415 if (clear_cmdline && cmdline_row < Rows - 1)
9416 msg_clr_cmdline(); /* will reset clear_cmdline */
9417
9418 /* Position on the last line in the window, column 0 */
9419 msg_pos_mode();
9420 cursor_off();
9421 attr = hl_attr(HLF_CM); /* Highlight mode */
9422 if (do_mode)
9423 {
9424 MSG_PUTS_ATTR("--", attr);
9425#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009426# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 if (xic != NULL && im_get_status() && !p_imdisable
9428 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009429# else
9430 if (
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009431# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009432 preedit_get_status()
9433# else
9434 im_get_status()
9435# endif
9436 )
9437# endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009438# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 MSG_PUTS_ATTR(" IM", attr);
9440# else
9441 MSG_PUTS_ATTR(" XIM", attr);
9442# endif
9443#endif
9444#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9445 if (gui.in_use)
9446 {
9447 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009448 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 }
9450#endif
9451#ifdef FEAT_INS_EXPAND
9452 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9453 {
9454 /* These messages can get long, avoid a wrap in a narrow
9455 * window. Prefer showing edit_submode_extra. */
9456 length = (Rows - msg_row) * Columns - 3;
9457 if (edit_submode_extra != NULL)
9458 length -= vim_strsize(edit_submode_extra);
9459 if (length > 0)
9460 {
9461 if (edit_submode_pre != NULL)
9462 length -= vim_strsize(edit_submode_pre);
9463 if (length - vim_strsize(edit_submode) > 0)
9464 {
9465 if (edit_submode_pre != NULL)
9466 msg_puts_attr(edit_submode_pre, attr);
9467 msg_puts_attr(edit_submode, attr);
9468 }
9469 if (edit_submode_extra != NULL)
9470 {
9471 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9472 if ((int)edit_submode_highl < (int)HLF_COUNT)
9473 sub_attr = hl_attr(edit_submode_highl);
9474 else
9475 sub_attr = attr;
9476 msg_puts_attr(edit_submode_extra, sub_attr);
9477 }
9478 }
9479 length = 0;
9480 }
9481 else
9482#endif
9483 {
9484#ifdef FEAT_VREPLACE
9485 if (State & VREPLACE_FLAG)
9486 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9487 else
9488#endif
9489 if (State & REPLACE_FLAG)
9490 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9491 else if (State & INSERT)
9492 {
9493#ifdef FEAT_RIGHTLEFT
9494 if (p_ri)
9495 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9496#endif
9497 MSG_PUTS_ATTR(_(" INSERT"), attr);
9498 }
9499 else if (restart_edit == 'I')
9500 MSG_PUTS_ATTR(_(" (insert)"), attr);
9501 else if (restart_edit == 'R')
9502 MSG_PUTS_ATTR(_(" (replace)"), attr);
9503 else if (restart_edit == 'V')
9504 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9505#ifdef FEAT_RIGHTLEFT
9506 if (p_hkmap)
9507 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9508# ifdef FEAT_FKMAP
9509 if (p_fkmap)
9510 MSG_PUTS_ATTR(farsi_text_5, attr);
9511# endif
9512#endif
9513#ifdef FEAT_KEYMAP
9514 if (State & LANGMAP)
9515 {
9516# ifdef FEAT_ARABIC
9517 if (curwin->w_p_arab)
9518 MSG_PUTS_ATTR(_(" Arabic"), attr);
9519 else
9520# endif
9521 MSG_PUTS_ATTR(_(" (lang)"), attr);
9522 }
9523#endif
9524 if ((State & INSERT) && p_paste)
9525 MSG_PUTS_ATTR(_(" (paste)"), attr);
9526
9527#ifdef FEAT_VISUAL
9528 if (VIsual_active)
9529 {
9530 char *p;
9531
9532 /* Don't concatenate separate words to avoid translation
9533 * problems. */
9534 switch ((VIsual_select ? 4 : 0)
9535 + (VIsual_mode == Ctrl_V) * 2
9536 + (VIsual_mode == 'V'))
9537 {
9538 case 0: p = N_(" VISUAL"); break;
9539 case 1: p = N_(" VISUAL LINE"); break;
9540 case 2: p = N_(" VISUAL BLOCK"); break;
9541 case 4: p = N_(" SELECT"); break;
9542 case 5: p = N_(" SELECT LINE"); break;
9543 default: p = N_(" SELECT BLOCK"); break;
9544 }
9545 MSG_PUTS_ATTR(_(p), attr);
9546 }
9547#endif
9548 MSG_PUTS_ATTR(" --", attr);
9549 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009550
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 need_clear = TRUE;
9552 }
9553 if (Recording
9554#ifdef FEAT_INS_EXPAND
9555 && edit_submode == NULL /* otherwise it gets too long */
9556#endif
9557 )
9558 {
9559 MSG_PUTS_ATTR(_("recording"), attr);
9560 need_clear = TRUE;
9561 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009562
9563 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 if (need_clear || clear_cmdline)
9565 msg_clr_eos();
9566 msg_didout = FALSE; /* overwrite this message */
9567 length = msg_col;
9568 msg_col = 0;
9569 need_wait_return = nwr_save; /* never ask for hit-return for this */
9570 }
9571 else if (clear_cmdline && msg_silent == 0)
9572 /* Clear the whole command line. Will reset "clear_cmdline". */
9573 msg_clr_cmdline();
9574
9575#ifdef FEAT_CMDL_INFO
9576# ifdef FEAT_VISUAL
9577 /* In Visual mode the size of the selected area must be redrawn. */
9578 if (VIsual_active)
9579 clear_showcmd();
9580# endif
9581
9582 /* If the last window has no status line, the ruler is after the mode
9583 * message and must be redrawn */
9584 if (redrawing()
9585# ifdef FEAT_WINDOWS
9586 && lastwin->w_status_height == 0
9587# endif
9588 )
9589 win_redr_ruler(lastwin, TRUE);
9590#endif
9591 redraw_cmdline = FALSE;
9592 clear_cmdline = FALSE;
9593
9594 return length;
9595}
9596
9597/*
9598 * Position for a mode message.
9599 */
9600 static void
9601msg_pos_mode()
9602{
9603 msg_col = 0;
9604 msg_row = Rows - 1;
9605}
9606
9607/*
9608 * Delete mode message. Used when ESC is typed which is expected to end
9609 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009610 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 */
9612 void
9613unshowmode(force)
9614 int force;
9615{
9616 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009617 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 */
9619 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9620 redraw_cmdline = TRUE; /* delete mode later */
9621 else
9622 {
9623 msg_pos_mode();
9624 if (Recording)
9625 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9626 msg_clr_eos();
9627 }
9628}
9629
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009630#if defined(FEAT_WINDOWS)
9631/*
9632 * Draw the tab pages line at the top of the Vim window.
9633 */
9634 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009635draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009636{
9637 int tabcount = 0;
9638 tabpage_T *tp;
9639 int tabwidth;
9640 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009641 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009642 int attr;
9643 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009644 win_T *cwp;
9645 int wincount;
9646 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009647 int c;
9648 int len;
9649 int attr_sel = hl_attr(HLF_TPS);
9650 int attr_nosel = hl_attr(HLF_TP);
9651 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009652 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009653 int room;
9654 int use_sep_chars = (t_colors < 8
9655#ifdef FEAT_GUI
9656 && !gui.in_use
9657#endif
9658 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009659
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009660 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009661
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009662#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009663 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009664 if (gui_use_tabline())
9665 {
9666 gui_update_tabline();
9667 return;
9668 }
9669#endif
9670
9671 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009672 return;
9673
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009674#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009675
9676 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9677 for (scol = 0; scol < Columns; ++scol)
9678 TabPageIdxs[scol] = 0;
9679
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009680 /* Use the 'tabline' option if it's set. */
9681 if (*p_tal != NUL)
9682 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009683 int save_called_emsg = called_emsg;
9684
9685 /* Check for an error. If there is one we would loop in redrawing the
9686 * screen. Avoid that by making 'tabline' empty. */
9687 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009688 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009689 if (called_emsg)
9690 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009691 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009692 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009693 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009694 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009695#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009696 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009697 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9698 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009699
Bram Moolenaar238a5642006-02-21 22:12:05 +00009700 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9701 if (tabwidth < 6)
9702 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009703
Bram Moolenaar238a5642006-02-21 22:12:05 +00009704 attr = attr_nosel;
9705 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009706 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009707 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9708 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009709 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009710 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009711
Bram Moolenaar238a5642006-02-21 22:12:05 +00009712 if (tp->tp_topframe == topframe)
9713 attr = attr_sel;
9714 if (use_sep_chars && col > 0)
9715 screen_putchar('|', 0, col++, attr);
9716
9717 if (tp->tp_topframe != topframe)
9718 attr = attr_nosel;
9719
9720 screen_putchar(' ', 0, col++, attr);
9721
9722 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009723 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009724 cwp = curwin;
9725 wp = firstwin;
9726 }
9727 else
9728 {
9729 cwp = tp->tp_curwin;
9730 wp = tp->tp_firstwin;
9731 }
9732
9733 modified = FALSE;
9734 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9735 if (bufIsChanged(wp->w_buffer))
9736 modified = TRUE;
9737 if (modified || wincount > 1)
9738 {
9739 if (wincount > 1)
9740 {
9741 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009742 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009743 if (col + len >= Columns - 3)
9744 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009745 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009746#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009747 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009748#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009749 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009750#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009751 );
9752 col += len;
9753 }
9754 if (modified)
9755 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9756 screen_putchar(' ', 0, col++, attr);
9757 }
9758
9759 room = scol - col + tabwidth - 1;
9760 if (room > 0)
9761 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009762 /* Get buffer name in NameBuff[] */
9763 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009764 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009765 len = vim_strsize(NameBuff);
9766 p = NameBuff;
9767#ifdef FEAT_MBYTE
9768 if (has_mbyte)
9769 while (len > room)
9770 {
9771 len -= ptr2cells(p);
9772 mb_ptr_adv(p);
9773 }
9774 else
9775#endif
9776 if (len > room)
9777 {
9778 p += len - room;
9779 len = room;
9780 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009781 if (len > Columns - col - 1)
9782 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009783
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009784 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009785 col += len;
9786 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009787 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009788
9789 /* Store the tab page number in TabPageIdxs[], so that
9790 * jump_to_mouse() knows where each one is. */
9791 ++tabcount;
9792 while (scol < col)
9793 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009794 }
9795
Bram Moolenaar238a5642006-02-21 22:12:05 +00009796 if (use_sep_chars)
9797 c = '_';
9798 else
9799 c = ' ';
9800 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009801
9802 /* Put an "X" for closing the current tab if there are several. */
9803 if (first_tabpage->tp_next != NULL)
9804 {
9805 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9806 TabPageIdxs[Columns - 1] = -999;
9807 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009808 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009809
9810 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9811 * set. */
9812 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009813}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009814
9815/*
9816 * Get buffer name for "buf" into NameBuff[].
9817 * Takes care of special buffer names and translates special characters.
9818 */
9819 void
9820get_trans_bufname(buf)
9821 buf_T *buf;
9822{
9823 if (buf_spname(buf) != NULL)
9824 STRCPY(NameBuff, buf_spname(buf));
9825 else
9826 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9827 trans_characters(NameBuff, MAXPATHL);
9828}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009829#endif
9830
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9832/*
9833 * Get the character to use in a status line. Get its attributes in "*attr".
9834 */
9835 static int
9836fillchar_status(attr, is_curwin)
9837 int *attr;
9838 int is_curwin;
9839{
9840 int fill;
9841 if (is_curwin)
9842 {
9843 *attr = hl_attr(HLF_S);
9844 fill = fill_stl;
9845 }
9846 else
9847 {
9848 *attr = hl_attr(HLF_SNC);
9849 fill = fill_stlnc;
9850 }
9851 /* Use fill when there is highlighting, and highlighting of current
9852 * window differs, or the fillchars differ, or this is not the
9853 * current window */
9854 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9855 || !is_curwin || firstwin == lastwin)
9856 || (fill_stl != fill_stlnc)))
9857 return fill;
9858 if (is_curwin)
9859 return '^';
9860 return '=';
9861}
9862#endif
9863
9864#ifdef FEAT_VERTSPLIT
9865/*
9866 * Get the character to use in a separator between vertically split windows.
9867 * Get its attributes in "*attr".
9868 */
9869 static int
9870fillchar_vsep(attr)
9871 int *attr;
9872{
9873 *attr = hl_attr(HLF_C);
9874 if (*attr == 0 && fill_vert == ' ')
9875 return '|';
9876 else
9877 return fill_vert;
9878}
9879#endif
9880
9881/*
9882 * Return TRUE if redrawing should currently be done.
9883 */
9884 int
9885redrawing()
9886{
9887 return (!RedrawingDisabled
9888 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9889}
9890
9891/*
9892 * Return TRUE if printing messages should currently be done.
9893 */
9894 int
9895messaging()
9896{
9897 return (!(p_lz && char_avail() && !KeyTyped));
9898}
9899
9900/*
9901 * Show current status info in ruler and various other places
9902 * If always is FALSE, only show ruler if position has changed.
9903 */
9904 void
9905showruler(always)
9906 int always;
9907{
9908 if (!always && !redrawing())
9909 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009910#ifdef FEAT_INS_EXPAND
9911 if (pum_visible())
9912 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009913# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009914 /* Don't redraw right now, do it later. */
9915 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009916# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009917 return;
9918 }
9919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009921 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009922 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009923 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 else
9926#endif
9927#ifdef FEAT_CMDL_INFO
9928 win_redr_ruler(curwin, always);
9929#endif
9930
9931#ifdef FEAT_TITLE
9932 if (need_maketitle
9933# ifdef FEAT_STL_OPT
9934 || (p_icon && (stl_syntax & STL_IN_ICON))
9935 || (p_title && (stl_syntax & STL_IN_TITLE))
9936# endif
9937 )
9938 maketitle();
9939#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009940#ifdef FEAT_WINDOWS
9941 /* Redraw the tab pages line if needed. */
9942 if (redraw_tabline)
9943 draw_tabline();
9944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945}
9946
9947#ifdef FEAT_CMDL_INFO
9948 static void
9949win_redr_ruler(wp, always)
9950 win_T *wp;
9951 int always;
9952{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009953#define RULER_BUF_LEN 70
9954 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 int row;
9956 int fillchar;
9957 int attr;
9958 int empty_line = FALSE;
9959 colnr_T virtcol;
9960 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009961 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 int o;
9963#ifdef FEAT_VERTSPLIT
9964 int this_ru_col;
9965 int off = 0;
9966 int width = Columns;
9967# define WITH_OFF(x) x
9968# define WITH_WIDTH(x) x
9969#else
9970# define WITH_OFF(x) 0
9971# define WITH_WIDTH(x) Columns
9972# define this_ru_col ru_col
9973#endif
9974
9975 /* If 'ruler' off or redrawing disabled, don't do anything */
9976 if (!p_ru)
9977 return;
9978
9979 /*
9980 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9981 * after deleting lines, before cursor.lnum is corrected.
9982 */
9983 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9984 return;
9985
9986#ifdef FEAT_INS_EXPAND
9987 /* Don't draw the ruler while doing insert-completion, it might overwrite
9988 * the (long) mode message. */
9989# ifdef FEAT_WINDOWS
9990 if (wp == lastwin && lastwin->w_status_height == 0)
9991# endif
9992 if (edit_submode != NULL)
9993 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009994 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9995 if (pum_visible())
9996 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997#endif
9998
9999#ifdef FEAT_STL_OPT
10000 if (*p_ruf)
10001 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010002 int save_called_emsg = called_emsg;
10003
10004 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010006 if (called_emsg)
10007 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010008 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010009 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 return;
10011 }
10012#endif
10013
10014 /*
10015 * Check if not in Insert mode and the line is empty (will show "0-1").
10016 */
10017 if (!(State & INSERT)
10018 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10019 empty_line = TRUE;
10020
10021 /*
10022 * Only draw the ruler when something changed.
10023 */
10024 validate_virtcol_win(wp);
10025 if ( redraw_cmdline
10026 || always
10027 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10028 || wp->w_cursor.col != wp->w_ru_cursor.col
10029 || wp->w_virtcol != wp->w_ru_virtcol
10030#ifdef FEAT_VIRTUALEDIT
10031 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10032#endif
10033 || wp->w_topline != wp->w_ru_topline
10034 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10035#ifdef FEAT_DIFF
10036 || wp->w_topfill != wp->w_ru_topfill
10037#endif
10038 || empty_line != wp->w_ru_empty)
10039 {
10040 cursor_off();
10041#ifdef FEAT_WINDOWS
10042 if (wp->w_status_height)
10043 {
10044 row = W_WINROW(wp) + wp->w_height;
10045 fillchar = fillchar_status(&attr, wp == curwin);
10046# ifdef FEAT_VERTSPLIT
10047 off = W_WINCOL(wp);
10048 width = W_WIDTH(wp);
10049# endif
10050 }
10051 else
10052#endif
10053 {
10054 row = Rows - 1;
10055 fillchar = ' ';
10056 attr = 0;
10057#ifdef FEAT_VERTSPLIT
10058 width = Columns;
10059 off = 0;
10060#endif
10061 }
10062
10063 /* In list mode virtcol needs to be recomputed */
10064 virtcol = wp->w_virtcol;
10065 if (wp->w_p_list && lcs_tab1 == NUL)
10066 {
10067 wp->w_p_list = FALSE;
10068 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10069 wp->w_p_list = TRUE;
10070 }
10071
10072 /*
10073 * Some sprintfs return the length, some return a pointer.
10074 * To avoid portability problems we use strlen() here.
10075 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010076 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10078 ? 0L
10079 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010080 len = STRLEN(buffer);
10081 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10083 (int)virtcol + 1);
10084
10085 /*
10086 * Add a "50%" if there is room for it.
10087 * On the last line, don't print in the last column (scrolls the
10088 * screen up on some terminals).
10089 */
10090 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010091 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 o = i + vim_strsize(buffer + i + 1);
10093#ifdef FEAT_WINDOWS
10094 if (wp->w_status_height == 0) /* can't use last char of screen */
10095#endif
10096 ++o;
10097#ifdef FEAT_VERTSPLIT
10098 this_ru_col = ru_col - (Columns - width);
10099 if (this_ru_col < 0)
10100 this_ru_col = 0;
10101#endif
10102 /* Never use more than half the window/screen width, leave the other
10103 * half for the filename. */
10104 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10105 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10106 if (this_ru_col + o < WITH_WIDTH(width))
10107 {
10108 while (this_ru_col + o < WITH_WIDTH(width))
10109 {
10110#ifdef FEAT_MBYTE
10111 if (has_mbyte)
10112 i += (*mb_char2bytes)(fillchar, buffer + i);
10113 else
10114#endif
10115 buffer[i++] = fillchar;
10116 ++o;
10117 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010118 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 }
10120 /* Truncate at window boundary. */
10121#ifdef FEAT_MBYTE
10122 if (has_mbyte)
10123 {
10124 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010125 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 {
10127 o += (*mb_ptr2cells)(buffer + i);
10128 if (this_ru_col + o > WITH_WIDTH(width))
10129 {
10130 buffer[i] = NUL;
10131 break;
10132 }
10133 }
10134 }
10135 else
10136#endif
10137 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10138 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10139
10140 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10141 i = redraw_cmdline;
10142 screen_fill(row, row + 1,
10143 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10144 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10145 fillchar, fillchar, attr);
10146 /* don't redraw the cmdline because of showing the ruler */
10147 redraw_cmdline = i;
10148 wp->w_ru_cursor = wp->w_cursor;
10149 wp->w_ru_virtcol = wp->w_virtcol;
10150 wp->w_ru_empty = empty_line;
10151 wp->w_ru_topline = wp->w_topline;
10152 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10153#ifdef FEAT_DIFF
10154 wp->w_ru_topfill = wp->w_topfill;
10155#endif
10156 }
10157}
10158#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010159
10160#if defined(FEAT_LINEBREAK) || defined(PROTO)
10161/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010162 * Return the width of the 'number' and 'relativenumber' column.
10163 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010164 * Otherwise it depends on 'numberwidth' and the line count.
10165 */
10166 int
10167number_width(wp)
10168 win_T *wp;
10169{
10170 int n;
10171 linenr_T lnum;
10172
Bram Moolenaar64486672010-05-16 15:46:46 +020010173 if (wp->w_p_nu)
10174 /* 'number' */
10175 lnum = wp->w_buffer->b_ml.ml_line_count;
10176 else
10177 /* 'relativenumber' */
10178 lnum = wp->w_height;
10179
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010180 if (lnum == wp->w_nrwidth_line_count)
10181 return wp->w_nrwidth_width;
10182 wp->w_nrwidth_line_count = lnum;
10183
10184 n = 0;
10185 do
10186 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010187 lnum /= 10;
10188 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010189 } while (lnum > 0);
10190
10191 /* 'numberwidth' gives the minimal width plus one */
10192 if (n < wp->w_p_nuw - 1)
10193 n = wp->w_p_nuw - 1;
10194
10195 wp->w_nrwidth_width = n;
10196 return n;
10197}
10198#endif