blob: ab08cf74def5d229c58a9b8f3e718845ca99805d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
45 * - w_topfill (filler line above the first line)
46 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
92/*
93 * The attributes that are actually active for writing to the screen.
94 */
95static int screen_attr = 0;
96
97/*
98 * Positioning the cursor is reduced by remembering the last position.
99 * Mostly used by windgoto() and screen_char().
100 */
101static int screen_cur_row, screen_cur_col; /* last known cursor position */
102
103#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106
107#ifdef FEAT_FOLDING
108static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
109#endif
110
111/*
112 * Buffer for one screen line (characters and attributes).
113 */
114static schar_T *current_ScreenLine;
115
116static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000117static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_FOLDING
119static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
120static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
121static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
122#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000123static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
125#ifdef FEAT_RIGHTLEFT
126static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
127# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
128#else
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_VERTSPLIT
133static void draw_vsep_win __ARGS((win_T *wp, int row));
134#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000135#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000136static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000139#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static void start_search_hl __ARGS((void));
141static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200142static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
144static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
145#endif
146static void screen_start_highlight __ARGS((int attr));
147static void screen_char __ARGS((unsigned off, int row, int col));
148#ifdef FEAT_MBYTE
149static void screen_char_2 __ARGS((unsigned off, int row, int col));
150#endif
151static void screenclear2 __ARGS((void));
152static void lineclear __ARGS((unsigned off, int width));
153static void lineinvalid __ARGS((unsigned off, int width));
154#ifdef FEAT_VERTSPLIT
155static void linecopy __ARGS((int to, int from, win_T *wp));
156static void redraw_block __ARGS((int row, int end, win_T *wp));
157#endif
158static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
159static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000162static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
165static int fillchar_status __ARGS((int *attr, int is_curwin));
166#endif
167#ifdef FEAT_VERTSPLIT
168static int fillchar_vsep __ARGS((int *attr));
169#endif
170#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000171static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_CMDL_INFO
174static void win_redr_ruler __ARGS((win_T *wp, int always));
175#endif
176
177#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
178/* Ugly global: overrule attribute used by screen_char() */
179static int screen_char_attr = 0;
180#endif
181
182/*
183 * Redraw the current window later, with update_screen(type).
184 * Set must_redraw only if not already set to a higher value.
185 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
186 */
187 void
188redraw_later(type)
189 int type;
190{
191 redraw_win_later(curwin, type);
192}
193
194 void
195redraw_win_later(wp, type)
196 win_T *wp;
197 int type;
198{
199 if (wp->w_redr_type < type)
200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
214redraw_later_clear()
215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
232redraw_all_later(type)
233 int type;
234{
235 win_T *wp;
236
237 FOR_ALL_WINDOWS(wp)
238 {
239 redraw_win_later(wp, type);
240 }
241}
242
243/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000244 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
247redraw_curbuf_later(type)
248 int type;
249{
250 redraw_buf_later(curbuf, type);
251}
252
253 void
254redraw_buf_later(buf, type)
255 buf_T *buf;
256 int type;
257{
258 win_T *wp;
259
260 FOR_ALL_WINDOWS(wp)
261 {
262 if (wp->w_buffer == buf)
263 redraw_win_later(wp, type);
264 }
265}
266
267/*
268 * Changed something in the current window, at buffer line "lnum", that
269 * requires that line and possibly other lines to be redrawn.
270 * Used when entering/leaving Insert mode with the cursor on a folded line.
271 * Used to remove the "$" from a change command.
272 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
273 * may become invalid and the whole window will have to be redrawn.
274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 void
276redrawWinline(lnum, invalid)
277 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000278 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
280#ifdef FEAT_FOLDING
281 int i;
282#endif
283
284 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
285 curwin->w_redraw_top = lnum;
286 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
287 curwin->w_redraw_bot = lnum;
288 redraw_later(VALID);
289
290#ifdef FEAT_FOLDING
291 if (invalid)
292 {
293 /* A w_lines[] entry for this lnum has become invalid. */
294 i = find_wl_entry(curwin, lnum);
295 if (i >= 0)
296 curwin->w_lines[i].wl_valid = FALSE;
297 }
298#endif
299}
300
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200301#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200302 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
304 * update all windows that are editing the current buffer
305 */
306 void
307update_curbuf(type)
308 int type;
309{
310 redraw_curbuf_later(type);
311 update_screen(type);
312}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
315/*
316 * update_screen()
317 *
318 * Based on the current value of curwin->w_topline, transfer a screenfull
319 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
320 */
321 void
322update_screen(type)
323 int type;
324{
325 win_T *wp;
326 static int did_intro = FALSE;
327#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
328 int did_one;
329#endif
330
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000331 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 if (!screen_valid(TRUE))
333 return;
334
335 if (must_redraw)
336 {
337 if (type < must_redraw) /* use maximal type */
338 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000339
340 /* must_redraw is reset here, so that when we run into some weird
341 * reason to redraw while busy redrawing (e.g., asynchronous
342 * scrolling), or update_topline() in win_update() will cause a
343 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 must_redraw = 0;
345 }
346
347 /* Need to update w_lines[]. */
348 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
349 type = NOT_VALID;
350
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000351 /* Postpone the redrawing when it's not needed and when being called
352 * recursively. */
353 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 redraw_later(type); /* remember type for next time */
356 must_redraw = type;
357 if (type > INVERTED_ALL)
358 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
359 return;
360 }
361
362 updating_screen = TRUE;
363#ifdef FEAT_SYN_HL
364 ++display_tick; /* let syntax code know we're in a next round of
365 * display updating */
366#endif
367
368 /*
369 * if the screen was scrolled up when displaying a message, scroll it down
370 */
371 if (msg_scrolled)
372 {
373 clear_cmdline = TRUE;
374 if (msg_scrolled > Rows - 5) /* clearing is faster */
375 type = CLEAR;
376 else if (type != CLEAR)
377 {
378 check_for_delay(FALSE);
379 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
380 type = CLEAR;
381 FOR_ALL_WINDOWS(wp)
382 {
383 if (W_WINROW(wp) < msg_scrolled)
384 {
385 if (W_WINROW(wp) + wp->w_height > msg_scrolled
386 && wp->w_redr_type < REDRAW_TOP
387 && wp->w_lines_valid > 0
388 && wp->w_topline == wp->w_lines[0].wl_lnum)
389 {
390 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
391 wp->w_redr_type = REDRAW_TOP;
392 }
393 else
394 {
395 wp->w_redr_type = NOT_VALID;
396#ifdef FEAT_WINDOWS
397 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
398 <= msg_scrolled)
399 wp->w_redr_status = TRUE;
400#endif
401 }
402 }
403 }
404 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000406 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409 msg_scrolled = 0;
410 need_wait_return = FALSE;
411 }
412
413 /* reset cmdline_row now (may have been changed temporarily) */
414 compute_cmdrow();
415
416 /* Check for changed highlighting */
417 if (need_highlight_changed)
418 highlight_changed();
419
420 if (type == CLEAR) /* first clear screen */
421 {
422 screenclear(); /* will reset clear_cmdline */
423 type = NOT_VALID;
424 }
425
426 if (clear_cmdline) /* going to clear cmdline (done below) */
427 check_for_delay(FALSE);
428
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000429#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200430 /* Force redraw when width of 'number' or 'relativenumber' column
431 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200433 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
434 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000435 curwin->w_redr_type = NOT_VALID;
436#endif
437
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 /*
439 * Only start redrawing if there is really something to do.
440 */
441 if (type == INVERTED)
442 update_curswant();
443 if (curwin->w_redr_type < type
444 && !((type == VALID
445 && curwin->w_lines[0].wl_valid
446#ifdef FEAT_DIFF
447 && curwin->w_topfill == curwin->w_old_topfill
448 && curwin->w_botfill == curwin->w_old_botfill
449#endif
450 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
451#ifdef FEAT_VISUAL
452 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000453 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
455 && curwin->w_old_visual_mode == VIsual_mode
456 && (curwin->w_valid & VALID_VIRTCOL)
457 && curwin->w_old_curswant == curwin->w_curswant)
458#endif
459 ))
460 curwin->w_redr_type = type;
461
Bram Moolenaar5a305422006-04-28 22:38:25 +0000462#ifdef FEAT_WINDOWS
463 /* Redraw the tab pages line if needed. */
464 if (redraw_tabline || type >= NOT_VALID)
465 draw_tabline();
466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#ifdef FEAT_SYN_HL
469 /*
470 * Correct stored syntax highlighting info for changes in each displayed
471 * buffer. Each buffer must only be done once.
472 */
473 FOR_ALL_WINDOWS(wp)
474 {
475 if (wp->w_buffer->b_mod_set)
476 {
477# ifdef FEAT_WINDOWS
478 win_T *wwp;
479
480 /* Check if we already did this buffer. */
481 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
482 if (wwp->w_buffer == wp->w_buffer)
483 break;
484# endif
485 if (
486# ifdef FEAT_WINDOWS
487 wwp == wp &&
488# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200489 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 syn_stack_apply_changes(wp->w_buffer);
491 }
492 }
493#endif
494
495 /*
496 * Go from top to bottom through the windows, redrawing the ones that need
497 * it.
498 */
499#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
500 did_one = FALSE;
501#endif
502#ifdef FEAT_SEARCH_EXTRA
503 search_hl.rm.regprog = NULL;
504#endif
505 FOR_ALL_WINDOWS(wp)
506 {
507 if (wp->w_redr_type != 0)
508 {
509 cursor_off();
510#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
511 if (!did_one)
512 {
513 did_one = TRUE;
514# ifdef FEAT_SEARCH_EXTRA
515 start_search_hl();
516# endif
517# ifdef FEAT_CLIPBOARD
518 /* When Visual area changed, may have to update selection. */
519 if (clip_star.available && clip_isautosel())
520 clip_update_selection();
521# endif
522#ifdef FEAT_GUI
523 /* Remove the cursor before starting to do anything, because
524 * scrolling may make it difficult to redraw the text under
525 * it. */
526 if (gui.in_use)
527 gui_undraw_cursor();
528#endif
529 }
530#endif
531 win_update(wp);
532 }
533
534#ifdef FEAT_WINDOWS
535 /* redraw status line after the window to minimize cursor movement */
536 if (wp->w_redr_status)
537 {
538 cursor_off();
539 win_redr_status(wp);
540 }
541#endif
542 }
543#if defined(FEAT_SEARCH_EXTRA)
544 end_search_hl();
545#endif
546
547#ifdef FEAT_WINDOWS
548 /* Reset b_mod_set flags. Going through all windows is probably faster
549 * than going through all buffers (there could be many buffers). */
550 for (wp = firstwin; wp != NULL; wp = wp->w_next)
551 wp->w_buffer->b_mod_set = FALSE;
552#else
553 curbuf->b_mod_set = FALSE;
554#endif
555
556 updating_screen = FALSE;
557#ifdef FEAT_GUI
558 gui_may_resize_shell();
559#endif
560
561 /* Clear or redraw the command line. Done last, because scrolling may
562 * mess up the command line. */
563 if (clear_cmdline || redraw_cmdline)
564 showmode();
565
566 /* May put up an introductory message when not editing a file */
567 if (!did_intro && bufempty()
568 && curbuf->b_fname == NULL
569#ifdef FEAT_WINDOWS
570 && firstwin->w_next == NULL
571#endif
572 && vim_strchr(p_shm, SHM_INTRO) == NULL)
573 intro_message(FALSE);
574 did_intro = TRUE;
575
576#ifdef FEAT_GUI
577 /* Redraw the cursor and update the scrollbars when all screen updating is
578 * done. */
579 if (gui.in_use)
580 {
581 out_flush(); /* required before updating the cursor */
582 if (did_one)
583 gui_update_cursor(FALSE, FALSE);
584 gui_update_scrollbars(FALSE);
585 }
586#endif
587}
588
Bram Moolenaar860cae12010-06-05 23:22:07 +0200589#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200590/*
591 * Return TRUE if the cursor line in window "wp" may be concealed, according
592 * to the 'concealcursor' option.
593 */
594 int
595conceal_cursor_line(wp)
596 win_T *wp;
597{
598 int c;
599
600 if (*wp->w_p_cocu == NUL)
601 return FALSE;
602 if (get_real_state() & VISUAL)
603 c = 'v';
604 else if (State & INSERT)
605 c = 'i';
606 else if (State & NORMAL)
607 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200608 else if (State & CMDLINE)
609 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200610 else
611 return FALSE;
612 return vim_strchr(wp->w_p_cocu, c) != NULL;
613}
614
615/*
616 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
617 */
618 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200619conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200620{
621 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
622 {
623 need_cursor_line_redraw = TRUE;
624 /* Need to recompute cursor column, e.g., when starting Visual mode
625 * without concealing. */
626 curs_columns(TRUE);
627 }
628}
629
Bram Moolenaar860cae12010-06-05 23:22:07 +0200630 void
631update_single_line(wp, lnum)
632 win_T *wp;
633 linenr_T lnum;
634{
635 int row;
636 int j;
637
638 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200639 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200640 {
641# ifdef FEAT_GUI
642 /* Remove the cursor before starting to do anything, because scrolling
643 * may make it difficult to redraw the text under it. */
644 if (gui.in_use)
645 gui_undraw_cursor();
646# endif
647 row = 0;
648 for (j = 0; j < wp->w_lines_valid; ++j)
649 {
650 if (lnum == wp->w_lines[j].wl_lnum)
651 {
652 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200653# ifdef FEAT_SEARCH_EXTRA
654 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200655 start_search_hl();
656 prepare_search_hl(wp, lnum);
657# endif
658 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
659# if defined(FEAT_SEARCH_EXTRA)
660 end_search_hl();
661# endif
662 break;
663 }
664 row += wp->w_lines[j].wl_size;
665 }
666# ifdef FEAT_GUI
667 /* Redraw the cursor */
668 if (gui.in_use)
669 {
670 out_flush(); /* required before updating the cursor */
671 gui_update_cursor(FALSE, FALSE);
672 }
673# endif
674 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200675 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200676}
677#endif
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
680static void update_prepare __ARGS((void));
681static void update_finish __ARGS((void));
682
683/*
684 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000685 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 */
687 static void
688update_prepare()
689{
690 cursor_off();
691 updating_screen = TRUE;
692#ifdef FEAT_GUI
693 /* Remove the cursor before starting to do anything, because scrolling may
694 * make it difficult to redraw the text under it. */
695 if (gui.in_use)
696 gui_undraw_cursor();
697#endif
698#ifdef FEAT_SEARCH_EXTRA
699 start_search_hl();
700#endif
701}
702
703/*
704 * Finish updating one or more windows.
705 */
706 static void
707update_finish()
708{
709 if (redraw_cmdline)
710 showmode();
711
712# ifdef FEAT_SEARCH_EXTRA
713 end_search_hl();
714# endif
715
716 updating_screen = FALSE;
717
718# ifdef FEAT_GUI
719 gui_may_resize_shell();
720
721 /* Redraw the cursor and update the scrollbars when all screen updating is
722 * done. */
723 if (gui.in_use)
724 {
725 out_flush(); /* required before updating the cursor */
726 gui_update_cursor(FALSE, FALSE);
727 gui_update_scrollbars(FALSE);
728 }
729# endif
730}
731#endif
732
733#if defined(FEAT_SIGNS) || defined(PROTO)
734 void
735update_debug_sign(buf, lnum)
736 buf_T *buf;
737 linenr_T lnum;
738{
739 win_T *wp;
740 int doit = FALSE;
741
742# ifdef FEAT_FOLDING
743 win_foldinfo.fi_level = 0;
744# endif
745
746 /* update/delete a specific mark */
747 FOR_ALL_WINDOWS(wp)
748 {
749 if (buf != NULL && lnum > 0)
750 {
751 if (wp->w_buffer == buf && lnum >= wp->w_topline
752 && lnum < wp->w_botline)
753 {
754 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
755 wp->w_redraw_top = lnum;
756 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
757 wp->w_redraw_bot = lnum;
758 redraw_win_later(wp, VALID);
759 }
760 }
761 else
762 redraw_win_later(wp, VALID);
763 if (wp->w_redr_type != 0)
764 doit = TRUE;
765 }
766
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000767 /* Return when there is nothing to do or screen updating already
768 * happening. */
769 if (!doit || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 return;
771
772 /* update all windows that need updating */
773 update_prepare();
774
775# ifdef FEAT_WINDOWS
776 for (wp = firstwin; wp; wp = wp->w_next)
777 {
778 if (wp->w_redr_type != 0)
779 win_update(wp);
780 if (wp->w_redr_status)
781 win_redr_status(wp);
782 }
783# else
784 if (curwin->w_redr_type != 0)
785 win_update(curwin);
786# endif
787
788 update_finish();
789}
790#endif
791
792
793#if defined(FEAT_GUI) || defined(PROTO)
794/*
795 * Update a single window, its status line and maybe the command line msg.
796 * Used for the GUI scrollbar.
797 */
798 void
799updateWindow(wp)
800 win_T *wp;
801{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000802 /* return if already busy updating */
803 if (updating_screen)
804 return;
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 update_prepare();
807
808#ifdef FEAT_CLIPBOARD
809 /* When Visual area changed, may have to update selection. */
810 if (clip_star.available && clip_isautosel())
811 clip_update_selection();
812#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000817 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000818 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000819 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (wp->w_redr_status
822# ifdef FEAT_CMDL_INFO
823 || p_ru
824# endif
825# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000826 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827# endif
828 )
829 win_redr_status(wp);
830#endif
831
832 update_finish();
833}
834#endif
835
836/*
837 * Update a single window.
838 *
839 * This may cause the windows below it also to be redrawn (when clearing the
840 * screen or scrolling lines).
841 *
842 * How the window is redrawn depends on wp->w_redr_type. Each type also
843 * implies the one below it.
844 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000845 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
847 * INVERTED redraw the changed part of the Visual area
848 * INVERTED_ALL redraw the whole Visual area
849 * VALID 1. scroll up/down to adjust for a changed w_topline
850 * 2. update lines at the top when scrolled down
851 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000852 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 * b_mod_top and b_mod_bot.
854 * - if wp->w_redraw_top non-zero, redraw lines between
855 * wp->w_redraw_top and wp->w_redr_bot.
856 * - continue redrawing when syntax status is invalid.
857 * 4. if scrolled up, update lines at the bottom.
858 * This results in three areas that may need updating:
859 * top: from first row to top_end (when scrolled down)
860 * mid: from mid_start to mid_end (update inversion or changed text)
861 * bot: from bot_start to last row (when scrolled up)
862 */
863 static void
864win_update(wp)
865 win_T *wp;
866{
867 buf_T *buf = wp->w_buffer;
868 int type;
869 int top_end = 0; /* Below last row of the top area that needs
870 updating. 0 when no top area updating. */
871 int mid_start = 999;/* first row of the mid area that needs
872 updating. 999 when no mid area updating. */
873 int mid_end = 0; /* Below last row of the mid area that needs
874 updating. 0 when no mid area updating. */
875 int bot_start = 999;/* first row of the bot area that needs
876 updating. 999 when no bot area updating */
877#ifdef FEAT_VISUAL
878 int scrolled_down = FALSE; /* TRUE when scrolled down when
879 w_topline got smaller a bit */
880#endif
881#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000882 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 int top_to_mod = FALSE; /* redraw above mod_top */
884#endif
885
886 int row; /* current window row to display */
887 linenr_T lnum; /* current buffer lnum to display */
888 int idx; /* current index in w_lines[] */
889 int srow; /* starting row of the current line */
890
891 int eof = FALSE; /* if TRUE, we hit the end of the file */
892 int didline = FALSE; /* if TRUE, we finished the last line */
893 int i;
894 long j;
895 static int recursive = FALSE; /* being called recursively */
896 int old_botline = wp->w_botline;
897#ifdef FEAT_FOLDING
898 long fold_count;
899#endif
900#ifdef FEAT_SYN_HL
901 /* remember what happened to the previous line, to know if
902 * check_visual_highlight() can be used */
903#define DID_NONE 1 /* didn't update a line */
904#define DID_LINE 2 /* updated a normal line */
905#define DID_FOLD 3 /* updated a folded line */
906 int did_update = DID_NONE;
907 linenr_T syntax_last_parsed = 0; /* last parsed text line */
908#endif
909 linenr_T mod_top = 0;
910 linenr_T mod_bot = 0;
911#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
912 int save_got_int;
913#endif
914
915 type = wp->w_redr_type;
916
917 if (type == NOT_VALID)
918 {
919#ifdef FEAT_WINDOWS
920 wp->w_redr_status = TRUE;
921#endif
922 wp->w_lines_valid = 0;
923 }
924
925 /* Window is zero-height: nothing to draw. */
926 if (wp->w_height == 0)
927 {
928 wp->w_redr_type = 0;
929 return;
930 }
931
932#ifdef FEAT_VERTSPLIT
933 /* Window is zero-width: Only need to draw the separator. */
934 if (wp->w_width == 0)
935 {
936 /* draw the vertical separator right of this window */
937 draw_vsep_win(wp, 0);
938 wp->w_redr_type = 0;
939 return;
940 }
941#endif
942
943#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200944 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#endif
946
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000947#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200948 /* Force redraw when width of 'number' or 'relativenumber' column
949 * changes. */
950 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000951 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000952 {
953 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000954 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000955 }
956 else
957#endif
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
960 {
961 /*
962 * When there are both inserted/deleted lines and specific lines to be
963 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
964 * everything (only happens when redrawing is off for while).
965 */
966 type = NOT_VALID;
967 }
968 else
969 {
970 /*
971 * Set mod_top to the first line that needs displaying because of
972 * changes. Set mod_bot to the first line after the changes.
973 */
974 mod_top = wp->w_redraw_top;
975 if (wp->w_redraw_bot != 0)
976 mod_bot = wp->w_redraw_bot + 1;
977 else
978 mod_bot = 0;
979 wp->w_redraw_top = 0; /* reset for next time */
980 wp->w_redraw_bot = 0;
981 if (buf->b_mod_set)
982 {
983 if (mod_top == 0 || mod_top > buf->b_mod_top)
984 {
985 mod_top = buf->b_mod_top;
986#ifdef FEAT_SYN_HL
987 /* Need to redraw lines above the change that may be included
988 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200989 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200991 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (mod_top < 1)
993 mod_top = 1;
994 }
995#endif
996 }
997 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
998 mod_bot = buf->b_mod_bot;
999
1000#ifdef FEAT_SEARCH_EXTRA
1001 /* When 'hlsearch' is on and using a multi-line search pattern, a
1002 * change in one line may make the Search highlighting in a
1003 * previous line invalid. Simple solution: redraw all visible
1004 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001005 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001007 if (search_hl.rm.regprog != NULL
1008 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001010 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001011 {
1012 cur = wp->w_match_head;
1013 while (cur != NULL)
1014 {
1015 if (cur->match.regprog != NULL
1016 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001017 {
1018 top_to_mod = TRUE;
1019 break;
1020 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001021 cur = cur->next;
1022 }
1023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#endif
1025 }
1026#ifdef FEAT_FOLDING
1027 if (mod_top != 0 && hasAnyFolding(wp))
1028 {
1029 linenr_T lnumt, lnumb;
1030
1031 /*
1032 * A change in a line can cause lines above it to become folded or
1033 * unfolded. Find the top most buffer line that may be affected.
1034 * If the line was previously folded and displayed, get the first
1035 * line of that fold. If the line is folded now, get the first
1036 * folded line. Use the minimum of these two.
1037 */
1038
1039 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1040 * the line below it. If there is no valid entry, use w_topline.
1041 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1042 * to this line. If there is no valid entry, use MAXLNUM. */
1043 lnumt = wp->w_topline;
1044 lnumb = MAXLNUM;
1045 for (i = 0; i < wp->w_lines_valid; ++i)
1046 if (wp->w_lines[i].wl_valid)
1047 {
1048 if (wp->w_lines[i].wl_lastlnum < mod_top)
1049 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1050 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1051 {
1052 lnumb = wp->w_lines[i].wl_lnum;
1053 /* When there is a fold column it might need updating
1054 * in the next line ("J" just above an open fold). */
1055 if (wp->w_p_fdc > 0)
1056 ++lnumb;
1057 }
1058 }
1059
1060 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1061 if (mod_top > lnumt)
1062 mod_top = lnumt;
1063
1064 /* Now do the same for the bottom line (one above mod_bot). */
1065 --mod_bot;
1066 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1067 ++mod_bot;
1068 if (mod_bot < lnumb)
1069 mod_bot = lnumb;
1070 }
1071#endif
1072
1073 /* When a change starts above w_topline and the end is below
1074 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001075 * If the end of the change is above w_topline: do like no change was
1076 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (mod_top != 0 && mod_top < wp->w_topline)
1078 {
1079 if (mod_bot > wp->w_topline)
1080 mod_top = wp->w_topline;
1081#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001082 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 top_end = 1;
1084#endif
1085 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001086
1087 /* When line numbers are displayed need to redraw all lines below
1088 * inserted/deleted lines. */
1089 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1090 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092
1093 /*
1094 * When only displaying the lines at the top, set top_end. Used when
1095 * window has scrolled down for msg_scrolled.
1096 */
1097 if (type == REDRAW_TOP)
1098 {
1099 j = 0;
1100 for (i = 0; i < wp->w_lines_valid; ++i)
1101 {
1102 j += wp->w_lines[i].wl_size;
1103 if (j >= wp->w_upd_rows)
1104 {
1105 top_end = j;
1106 break;
1107 }
1108 }
1109 if (top_end == 0)
1110 /* not found (cannot happen?): redraw everything */
1111 type = NOT_VALID;
1112 else
1113 /* top area defined, the rest is VALID */
1114 type = VALID;
1115 }
1116
Bram Moolenaar367329b2007-08-30 11:53:22 +00001117 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001118 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1119 * non-zero and thus not FALSE) will indicate that screenclear() was not
1120 * called. */
1121 if (screen_cleared)
1122 screen_cleared = MAYBE;
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 /*
1125 * If there are no changes on the screen that require a complete redraw,
1126 * handle three cases:
1127 * 1: we are off the top of the screen by a few lines: scroll down
1128 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1129 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1130 * w_lines[] that needs updating.
1131 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001132 if ((type == VALID || type == SOME_VALID
1133 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#ifdef FEAT_DIFF
1135 && !wp->w_botfill && !wp->w_old_botfill
1136#endif
1137 )
1138 {
1139 if (mod_top != 0 && wp->w_topline == mod_top)
1140 {
1141 /*
1142 * w_topline is the first changed line, the scrolling will be done
1143 * further down.
1144 */
1145 }
1146 else if (wp->w_lines[0].wl_valid
1147 && (wp->w_topline < wp->w_lines[0].wl_lnum
1148#ifdef FEAT_DIFF
1149 || (wp->w_topline == wp->w_lines[0].wl_lnum
1150 && wp->w_topfill > wp->w_old_topfill)
1151#endif
1152 ))
1153 {
1154 /*
1155 * New topline is above old topline: May scroll down.
1156 */
1157#ifdef FEAT_FOLDING
1158 if (hasAnyFolding(wp))
1159 {
1160 linenr_T ln;
1161
1162 /* count the number of lines we are off, counting a sequence
1163 * of folded lines as one */
1164 j = 0;
1165 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1166 {
1167 ++j;
1168 if (j >= wp->w_height - 2)
1169 break;
1170 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1171 }
1172 }
1173 else
1174#endif
1175 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1176 if (j < wp->w_height - 2) /* not too far off */
1177 {
1178 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1179#ifdef FEAT_DIFF
1180 /* insert extra lines for previously invisible filler lines */
1181 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1182 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1183 - wp->w_old_topfill;
1184#endif
1185 if (i < wp->w_height - 2) /* less than a screen off */
1186 {
1187 /*
1188 * Try to insert the correct number of lines.
1189 * If not the last window, delete the lines at the bottom.
1190 * win_ins_lines may fail when the terminal can't do it.
1191 */
1192 if (i > 0)
1193 check_for_delay(FALSE);
1194 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1195 {
1196 if (wp->w_lines_valid != 0)
1197 {
1198 /* Need to update rows that are new, stop at the
1199 * first one that scrolled down. */
1200 top_end = i;
1201#ifdef FEAT_VISUAL
1202 scrolled_down = TRUE;
1203#endif
1204
1205 /* Move the entries that were scrolled, disable
1206 * the entries for the lines to be redrawn. */
1207 if ((wp->w_lines_valid += j) > wp->w_height)
1208 wp->w_lines_valid = wp->w_height;
1209 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1210 wp->w_lines[idx] = wp->w_lines[idx - j];
1211 while (idx >= 0)
1212 wp->w_lines[idx--].wl_valid = FALSE;
1213 }
1214 }
1215 else
1216 mid_start = 0; /* redraw all lines */
1217 }
1218 else
1219 mid_start = 0; /* redraw all lines */
1220 }
1221 else
1222 mid_start = 0; /* redraw all lines */
1223 }
1224 else
1225 {
1226 /*
1227 * New topline is at or below old topline: May scroll up.
1228 * When topline didn't change, find first entry in w_lines[] that
1229 * needs updating.
1230 */
1231
1232 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1233 j = -1;
1234 row = 0;
1235 for (i = 0; i < wp->w_lines_valid; i++)
1236 {
1237 if (wp->w_lines[i].wl_valid
1238 && wp->w_lines[i].wl_lnum == wp->w_topline)
1239 {
1240 j = i;
1241 break;
1242 }
1243 row += wp->w_lines[i].wl_size;
1244 }
1245 if (j == -1)
1246 {
1247 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1248 * lines */
1249 mid_start = 0;
1250 }
1251 else
1252 {
1253 /*
1254 * Try to delete the correct number of lines.
1255 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1256 */
1257#ifdef FEAT_DIFF
1258 /* If the topline didn't change, delete old filler lines,
1259 * otherwise delete filler lines of the new topline... */
1260 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1261 row += wp->w_old_topfill;
1262 else
1263 row += diff_check_fill(wp, wp->w_topline);
1264 /* ... but don't delete new filler lines. */
1265 row -= wp->w_topfill;
1266#endif
1267 if (row > 0)
1268 {
1269 check_for_delay(FALSE);
1270 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1271 bot_start = wp->w_height - row;
1272 else
1273 mid_start = 0; /* redraw all lines */
1274 }
1275 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1276 {
1277 /*
1278 * Skip the lines (below the deleted lines) that are still
1279 * valid and don't need redrawing. Copy their info
1280 * upwards, to compensate for the deleted lines. Set
1281 * bot_start to the first row that needs redrawing.
1282 */
1283 bot_start = 0;
1284 idx = 0;
1285 for (;;)
1286 {
1287 wp->w_lines[idx] = wp->w_lines[j];
1288 /* stop at line that didn't fit, unless it is still
1289 * valid (no lines deleted) */
1290 if (row > 0 && bot_start + row
1291 + (int)wp->w_lines[j].wl_size > wp->w_height)
1292 {
1293 wp->w_lines_valid = idx + 1;
1294 break;
1295 }
1296 bot_start += wp->w_lines[idx++].wl_size;
1297
1298 /* stop at the last valid entry in w_lines[].wl_size */
1299 if (++j >= wp->w_lines_valid)
1300 {
1301 wp->w_lines_valid = idx;
1302 break;
1303 }
1304 }
1305#ifdef FEAT_DIFF
1306 /* Correct the first entry for filler lines at the top
1307 * when it won't get updated below. */
1308 if (wp->w_p_diff && bot_start > 0)
1309 wp->w_lines[0].wl_size =
1310 plines_win_nofill(wp, wp->w_topline, TRUE)
1311 + wp->w_topfill;
1312#endif
1313 }
1314 }
1315 }
1316
1317 /* When starting redraw in the first line, redraw all lines. When
1318 * there is only one window it's probably faster to clear the screen
1319 * first. */
1320 if (mid_start == 0)
1321 {
1322 mid_end = wp->w_height;
1323 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001324 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001325 /* Clear the screen when it was not done by win_del_lines() or
1326 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1327 * then. */
1328 if (screen_cleared != TRUE)
1329 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001330#ifdef FEAT_WINDOWS
1331 /* The screen was cleared, redraw the tab pages line. */
1332 if (redraw_tabline)
1333 draw_tabline();
1334#endif
1335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001337
1338 /* When win_del_lines() or win_ins_lines() caused the screen to be
1339 * cleared (only happens for the first window) or when screenclear()
1340 * was called directly above, "must_redraw" will have been set to
1341 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1342 if (screen_cleared == TRUE)
1343 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345 else
1346 {
1347 /* Not VALID or INVERTED: redraw all lines. */
1348 mid_start = 0;
1349 mid_end = wp->w_height;
1350 }
1351
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001352 if (type == SOME_VALID)
1353 {
1354 /* SOME_VALID: redraw all lines. */
1355 mid_start = 0;
1356 mid_end = wp->w_height;
1357 type = NOT_VALID;
1358 }
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360#ifdef FEAT_VISUAL
1361 /* check if we are updating or removing the inverted part */
1362 if ((VIsual_active && buf == curwin->w_buffer)
1363 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1364 {
1365 linenr_T from, to;
1366
1367 if (VIsual_active)
1368 {
1369 if (VIsual_active
1370 && (VIsual_mode != wp->w_old_visual_mode
1371 || type == INVERTED_ALL))
1372 {
1373 /*
1374 * If the type of Visual selection changed, redraw the whole
1375 * selection. Also when the ownership of the X selection is
1376 * gained or lost.
1377 */
1378 if (curwin->w_cursor.lnum < VIsual.lnum)
1379 {
1380 from = curwin->w_cursor.lnum;
1381 to = VIsual.lnum;
1382 }
1383 else
1384 {
1385 from = VIsual.lnum;
1386 to = curwin->w_cursor.lnum;
1387 }
1388 /* redraw more when the cursor moved as well */
1389 if (wp->w_old_cursor_lnum < from)
1390 from = wp->w_old_cursor_lnum;
1391 if (wp->w_old_cursor_lnum > to)
1392 to = wp->w_old_cursor_lnum;
1393 if (wp->w_old_visual_lnum < from)
1394 from = wp->w_old_visual_lnum;
1395 if (wp->w_old_visual_lnum > to)
1396 to = wp->w_old_visual_lnum;
1397 }
1398 else
1399 {
1400 /*
1401 * Find the line numbers that need to be updated: The lines
1402 * between the old cursor position and the current cursor
1403 * position. Also check if the Visual position changed.
1404 */
1405 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1406 {
1407 from = curwin->w_cursor.lnum;
1408 to = wp->w_old_cursor_lnum;
1409 }
1410 else
1411 {
1412 from = wp->w_old_cursor_lnum;
1413 to = curwin->w_cursor.lnum;
1414 if (from == 0) /* Visual mode just started */
1415 from = to;
1416 }
1417
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001418 if (VIsual.lnum != wp->w_old_visual_lnum
1419 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 {
1421 if (wp->w_old_visual_lnum < from
1422 && wp->w_old_visual_lnum != 0)
1423 from = wp->w_old_visual_lnum;
1424 if (wp->w_old_visual_lnum > to)
1425 to = wp->w_old_visual_lnum;
1426 if (VIsual.lnum < from)
1427 from = VIsual.lnum;
1428 if (VIsual.lnum > to)
1429 to = VIsual.lnum;
1430 }
1431 }
1432
1433 /*
1434 * If in block mode and changed column or curwin->w_curswant:
1435 * update all lines.
1436 * First compute the actual start and end column.
1437 */
1438 if (VIsual_mode == Ctrl_V)
1439 {
1440 colnr_T fromc, toc;
1441
1442 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1443 ++toc;
1444 if (curwin->w_curswant == MAXCOL)
1445 toc = MAXCOL;
1446
1447 if (fromc != wp->w_old_cursor_fcol
1448 || toc != wp->w_old_cursor_lcol)
1449 {
1450 if (from > VIsual.lnum)
1451 from = VIsual.lnum;
1452 if (to < VIsual.lnum)
1453 to = VIsual.lnum;
1454 }
1455 wp->w_old_cursor_fcol = fromc;
1456 wp->w_old_cursor_lcol = toc;
1457 }
1458 }
1459 else
1460 {
1461 /* Use the line numbers of the old Visual area. */
1462 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1463 {
1464 from = wp->w_old_cursor_lnum;
1465 to = wp->w_old_visual_lnum;
1466 }
1467 else
1468 {
1469 from = wp->w_old_visual_lnum;
1470 to = wp->w_old_cursor_lnum;
1471 }
1472 }
1473
1474 /*
1475 * There is no need to update lines above the top of the window.
1476 */
1477 if (from < wp->w_topline)
1478 from = wp->w_topline;
1479
1480 /*
1481 * If we know the value of w_botline, use it to restrict the update to
1482 * the lines that are visible in the window.
1483 */
1484 if (wp->w_valid & VALID_BOTLINE)
1485 {
1486 if (from >= wp->w_botline)
1487 from = wp->w_botline - 1;
1488 if (to >= wp->w_botline)
1489 to = wp->w_botline - 1;
1490 }
1491
1492 /*
1493 * Find the minimal part to be updated.
1494 * Watch out for scrolling that made entries in w_lines[] invalid.
1495 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1496 * top_end; need to redraw from top_end to the "to" line.
1497 * A middle mouse click with a Visual selection may change the text
1498 * above the Visual area and reset wl_valid, do count these for
1499 * mid_end (in srow).
1500 */
1501 if (mid_start > 0)
1502 {
1503 lnum = wp->w_topline;
1504 idx = 0;
1505 srow = 0;
1506 if (scrolled_down)
1507 mid_start = top_end;
1508 else
1509 mid_start = 0;
1510 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1511 {
1512 if (wp->w_lines[idx].wl_valid)
1513 mid_start += wp->w_lines[idx].wl_size;
1514 else if (!scrolled_down)
1515 srow += wp->w_lines[idx].wl_size;
1516 ++idx;
1517# ifdef FEAT_FOLDING
1518 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1519 lnum = wp->w_lines[idx].wl_lnum;
1520 else
1521# endif
1522 ++lnum;
1523 }
1524 srow += mid_start;
1525 mid_end = wp->w_height;
1526 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1527 {
1528 if (wp->w_lines[idx].wl_valid
1529 && wp->w_lines[idx].wl_lnum >= to + 1)
1530 {
1531 /* Only update until first row of this line */
1532 mid_end = srow;
1533 break;
1534 }
1535 srow += wp->w_lines[idx].wl_size;
1536 }
1537 }
1538 }
1539
1540 if (VIsual_active && buf == curwin->w_buffer)
1541 {
1542 wp->w_old_visual_mode = VIsual_mode;
1543 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1544 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001545 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 wp->w_old_curswant = curwin->w_curswant;
1547 }
1548 else
1549 {
1550 wp->w_old_visual_mode = 0;
1551 wp->w_old_cursor_lnum = 0;
1552 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001553 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 }
1555#endif /* FEAT_VISUAL */
1556
1557#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1558 /* reset got_int, otherwise regexp won't work */
1559 save_got_int = got_int;
1560 got_int = 0;
1561#endif
1562#ifdef FEAT_FOLDING
1563 win_foldinfo.fi_level = 0;
1564#endif
1565
1566 /*
1567 * Update all the window rows.
1568 */
1569 idx = 0; /* first entry in w_lines[].wl_size */
1570 row = 0;
1571 srow = 0;
1572 lnum = wp->w_topline; /* first line shown in window */
1573 for (;;)
1574 {
1575 /* stop updating when reached the end of the window (check for _past_
1576 * the end of the window is at the end of the loop) */
1577 if (row == wp->w_height)
1578 {
1579 didline = TRUE;
1580 break;
1581 }
1582
1583 /* stop updating when hit the end of the file */
1584 if (lnum > buf->b_ml.ml_line_count)
1585 {
1586 eof = TRUE;
1587 break;
1588 }
1589
1590 /* Remember the starting row of the line that is going to be dealt
1591 * with. It is used further down when the line doesn't fit. */
1592 srow = row;
1593
1594 /*
1595 * Update a line when it is in an area that needs updating, when it
1596 * has changes or w_lines[idx] is invalid.
1597 * bot_start may be halfway a wrapped line after using
1598 * win_del_lines(), check if the current line includes it.
1599 * When syntax folding is being used, the saved syntax states will
1600 * already have been updated, we can't see where the syntax state is
1601 * the same again, just update until the end of the window.
1602 */
1603 if (row < top_end
1604 || (row >= mid_start && row < mid_end)
1605#ifdef FEAT_SEARCH_EXTRA
1606 || top_to_mod
1607#endif
1608 || idx >= wp->w_lines_valid
1609 || (row + wp->w_lines[idx].wl_size > bot_start)
1610 || (mod_top != 0
1611 && (lnum == mod_top
1612 || (lnum >= mod_top
1613 && (lnum < mod_bot
1614#ifdef FEAT_SYN_HL
1615 || did_update == DID_FOLD
1616 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001617 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 && (
1619# ifdef FEAT_FOLDING
1620 (foldmethodIsSyntax(wp)
1621 && hasAnyFolding(wp)) ||
1622# endif
1623 syntax_check_changed(lnum)))
1624#endif
1625 )))))
1626 {
1627#ifdef FEAT_SEARCH_EXTRA
1628 if (lnum == mod_top)
1629 top_to_mod = FALSE;
1630#endif
1631
1632 /*
1633 * When at start of changed lines: May scroll following lines
1634 * up or down to minimize redrawing.
1635 * Don't do this when the change continues until the end.
1636 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1637 */
1638 if (lnum == mod_top
1639 && mod_bot != MAXLNUM
1640 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1641 {
1642 int old_rows = 0;
1643 int new_rows = 0;
1644 int xtra_rows;
1645 linenr_T l;
1646
1647 /* Count the old number of window rows, using w_lines[], which
1648 * should still contain the sizes for the lines as they are
1649 * currently displayed. */
1650 for (i = idx; i < wp->w_lines_valid; ++i)
1651 {
1652 /* Only valid lines have a meaningful wl_lnum. Invalid
1653 * lines are part of the changed area. */
1654 if (wp->w_lines[i].wl_valid
1655 && wp->w_lines[i].wl_lnum == mod_bot)
1656 break;
1657 old_rows += wp->w_lines[i].wl_size;
1658#ifdef FEAT_FOLDING
1659 if (wp->w_lines[i].wl_valid
1660 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1661 {
1662 /* Must have found the last valid entry above mod_bot.
1663 * Add following invalid entries. */
1664 ++i;
1665 while (i < wp->w_lines_valid
1666 && !wp->w_lines[i].wl_valid)
1667 old_rows += wp->w_lines[i++].wl_size;
1668 break;
1669 }
1670#endif
1671 }
1672
1673 if (i >= wp->w_lines_valid)
1674 {
1675 /* We can't find a valid line below the changed lines,
1676 * need to redraw until the end of the window.
1677 * Inserting/deleting lines has no use. */
1678 bot_start = 0;
1679 }
1680 else
1681 {
1682 /* Able to count old number of rows: Count new window
1683 * rows, and may insert/delete lines */
1684 j = idx;
1685 for (l = lnum; l < mod_bot; ++l)
1686 {
1687#ifdef FEAT_FOLDING
1688 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1689 ++new_rows;
1690 else
1691#endif
1692#ifdef FEAT_DIFF
1693 if (l == wp->w_topline)
1694 new_rows += plines_win_nofill(wp, l, TRUE)
1695 + wp->w_topfill;
1696 else
1697#endif
1698 new_rows += plines_win(wp, l, TRUE);
1699 ++j;
1700 if (new_rows > wp->w_height - row - 2)
1701 {
1702 /* it's getting too much, must redraw the rest */
1703 new_rows = 9999;
1704 break;
1705 }
1706 }
1707 xtra_rows = new_rows - old_rows;
1708 if (xtra_rows < 0)
1709 {
1710 /* May scroll text up. If there is not enough
1711 * remaining text or scrolling fails, must redraw the
1712 * rest. If scrolling works, must redraw the text
1713 * below the scrolled text. */
1714 if (row - xtra_rows >= wp->w_height - 2)
1715 mod_bot = MAXLNUM;
1716 else
1717 {
1718 check_for_delay(FALSE);
1719 if (win_del_lines(wp, row,
1720 -xtra_rows, FALSE, FALSE) == FAIL)
1721 mod_bot = MAXLNUM;
1722 else
1723 bot_start = wp->w_height + xtra_rows;
1724 }
1725 }
1726 else if (xtra_rows > 0)
1727 {
1728 /* May scroll text down. If there is not enough
1729 * remaining text of scrolling fails, must redraw the
1730 * rest. */
1731 if (row + xtra_rows >= wp->w_height - 2)
1732 mod_bot = MAXLNUM;
1733 else
1734 {
1735 check_for_delay(FALSE);
1736 if (win_ins_lines(wp, row + old_rows,
1737 xtra_rows, FALSE, FALSE) == FAIL)
1738 mod_bot = MAXLNUM;
1739 else if (top_end > row + old_rows)
1740 /* Scrolled the part at the top that requires
1741 * updating down. */
1742 top_end += xtra_rows;
1743 }
1744 }
1745
1746 /* When not updating the rest, may need to move w_lines[]
1747 * entries. */
1748 if (mod_bot != MAXLNUM && i != j)
1749 {
1750 if (j < i)
1751 {
1752 int x = row + new_rows;
1753
1754 /* move entries in w_lines[] upwards */
1755 for (;;)
1756 {
1757 /* stop at last valid entry in w_lines[] */
1758 if (i >= wp->w_lines_valid)
1759 {
1760 wp->w_lines_valid = j;
1761 break;
1762 }
1763 wp->w_lines[j] = wp->w_lines[i];
1764 /* stop at a line that won't fit */
1765 if (x + (int)wp->w_lines[j].wl_size
1766 > wp->w_height)
1767 {
1768 wp->w_lines_valid = j + 1;
1769 break;
1770 }
1771 x += wp->w_lines[j++].wl_size;
1772 ++i;
1773 }
1774 if (bot_start > x)
1775 bot_start = x;
1776 }
1777 else /* j > i */
1778 {
1779 /* move entries in w_lines[] downwards */
1780 j -= i;
1781 wp->w_lines_valid += j;
1782 if (wp->w_lines_valid > wp->w_height)
1783 wp->w_lines_valid = wp->w_height;
1784 for (i = wp->w_lines_valid; i - j >= idx; --i)
1785 wp->w_lines[i] = wp->w_lines[i - j];
1786
1787 /* The w_lines[] entries for inserted lines are
1788 * now invalid, but wl_size may be used above.
1789 * Reset to zero. */
1790 while (i >= idx)
1791 {
1792 wp->w_lines[i].wl_size = 0;
1793 wp->w_lines[i--].wl_valid = FALSE;
1794 }
1795 }
1796 }
1797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 }
1799
1800#ifdef FEAT_FOLDING
1801 /*
1802 * When lines are folded, display one line for all of them.
1803 * Otherwise, display normally (can be several display lines when
1804 * 'wrap' is on).
1805 */
1806 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1807 if (fold_count != 0)
1808 {
1809 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1810 ++row;
1811 --fold_count;
1812 wp->w_lines[idx].wl_folded = TRUE;
1813 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1814# ifdef FEAT_SYN_HL
1815 did_update = DID_FOLD;
1816# endif
1817 }
1818 else
1819#endif
1820 if (idx < wp->w_lines_valid
1821 && wp->w_lines[idx].wl_valid
1822 && wp->w_lines[idx].wl_lnum == lnum
1823 && lnum > wp->w_topline
1824 && !(dy_flags & DY_LASTLINE)
1825 && srow + wp->w_lines[idx].wl_size > wp->w_height
1826#ifdef FEAT_DIFF
1827 && diff_check_fill(wp, lnum) == 0
1828#endif
1829 )
1830 {
1831 /* This line is not going to fit. Don't draw anything here,
1832 * will draw "@ " lines below. */
1833 row = wp->w_height + 1;
1834 }
1835 else
1836 {
1837#ifdef FEAT_SEARCH_EXTRA
1838 prepare_search_hl(wp, lnum);
1839#endif
1840#ifdef FEAT_SYN_HL
1841 /* Let the syntax stuff know we skipped a few lines. */
1842 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001843 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 syntax_end_parsing(syntax_last_parsed + 1);
1845#endif
1846
1847 /*
1848 * Display one line.
1849 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001850 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852#ifdef FEAT_FOLDING
1853 wp->w_lines[idx].wl_folded = FALSE;
1854 wp->w_lines[idx].wl_lastlnum = lnum;
1855#endif
1856#ifdef FEAT_SYN_HL
1857 did_update = DID_LINE;
1858 syntax_last_parsed = lnum;
1859#endif
1860 }
1861
1862 wp->w_lines[idx].wl_lnum = lnum;
1863 wp->w_lines[idx].wl_valid = TRUE;
1864 if (row > wp->w_height) /* past end of screen */
1865 {
1866 /* we may need the size of that too long line later on */
1867 if (dollar_vcol == 0)
1868 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1869 ++idx;
1870 break;
1871 }
1872 if (dollar_vcol == 0)
1873 wp->w_lines[idx].wl_size = row - srow;
1874 ++idx;
1875#ifdef FEAT_FOLDING
1876 lnum += fold_count + 1;
1877#else
1878 ++lnum;
1879#endif
1880 }
1881 else
1882 {
1883 /* This line does not need updating, advance to the next one */
1884 row += wp->w_lines[idx++].wl_size;
1885 if (row > wp->w_height) /* past end of screen */
1886 break;
1887#ifdef FEAT_FOLDING
1888 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1889#else
1890 ++lnum;
1891#endif
1892#ifdef FEAT_SYN_HL
1893 did_update = DID_NONE;
1894#endif
1895 }
1896
1897 if (lnum > buf->b_ml.ml_line_count)
1898 {
1899 eof = TRUE;
1900 break;
1901 }
1902 }
1903 /*
1904 * End of loop over all window lines.
1905 */
1906
1907
1908 if (idx > wp->w_lines_valid)
1909 wp->w_lines_valid = idx;
1910
1911#ifdef FEAT_SYN_HL
1912 /*
1913 * Let the syntax stuff know we stop parsing here.
1914 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001915 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 syntax_end_parsing(syntax_last_parsed + 1);
1917#endif
1918
1919 /*
1920 * If we didn't hit the end of the file, and we didn't finish the last
1921 * line we were working on, then the line didn't fit.
1922 */
1923 wp->w_empty_rows = 0;
1924#ifdef FEAT_DIFF
1925 wp->w_filler_rows = 0;
1926#endif
1927 if (!eof && !didline)
1928 {
1929 if (lnum == wp->w_topline)
1930 {
1931 /*
1932 * Single line that does not fit!
1933 * Don't overwrite it, it can be edited.
1934 */
1935 wp->w_botline = lnum + 1;
1936 }
1937#ifdef FEAT_DIFF
1938 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1939 {
1940 /* Window ends in filler lines. */
1941 wp->w_botline = lnum;
1942 wp->w_filler_rows = wp->w_height - srow;
1943 }
1944#endif
1945 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1946 {
1947 /*
1948 * Last line isn't finished: Display "@@@" at the end.
1949 */
1950 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1951 W_WINROW(wp) + wp->w_height,
1952 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1953 '@', '@', hl_attr(HLF_AT));
1954 set_empty_rows(wp, srow);
1955 wp->w_botline = lnum;
1956 }
1957 else
1958 {
1959 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1960 wp->w_botline = lnum;
1961 }
1962 }
1963 else
1964 {
1965#ifdef FEAT_VERTSPLIT
1966 draw_vsep_win(wp, row);
1967#endif
1968 if (eof) /* we hit the end of the file */
1969 {
1970 wp->w_botline = buf->b_ml.ml_line_count + 1;
1971#ifdef FEAT_DIFF
1972 j = diff_check_fill(wp, wp->w_botline);
1973 if (j > 0 && !wp->w_botfill)
1974 {
1975 /*
1976 * Display filler lines at the end of the file
1977 */
1978 if (char2cells(fill_diff) > 1)
1979 i = '-';
1980 else
1981 i = fill_diff;
1982 if (row + j > wp->w_height)
1983 j = wp->w_height - row;
1984 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1985 row += j;
1986 }
1987#endif
1988 }
1989 else if (dollar_vcol == 0)
1990 wp->w_botline = lnum;
1991
1992 /* make sure the rest of the screen is blank */
1993 /* put '~'s on rows that aren't part of the file. */
1994 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1995 }
1996
1997 /* Reset the type of redrawing required, the window has been updated. */
1998 wp->w_redr_type = 0;
1999#ifdef FEAT_DIFF
2000 wp->w_old_topfill = wp->w_topfill;
2001 wp->w_old_botfill = wp->w_botfill;
2002#endif
2003
2004 if (dollar_vcol == 0)
2005 {
2006 /*
2007 * There is a trick with w_botline. If we invalidate it on each
2008 * change that might modify it, this will cause a lot of expensive
2009 * calls to plines() in update_topline() each time. Therefore the
2010 * value of w_botline is often approximated, and this value is used to
2011 * compute the value of w_topline. If the value of w_botline was
2012 * wrong, check that the value of w_topline is correct (cursor is on
2013 * the visible part of the text). If it's not, we need to redraw
2014 * again. Mostly this just means scrolling up a few lines, so it
2015 * doesn't look too bad. Only do this for the current window (where
2016 * changes are relevant).
2017 */
2018 wp->w_valid |= VALID_BOTLINE;
2019 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2020 {
2021 recursive = TRUE;
2022 curwin->w_valid &= ~VALID_TOPLINE;
2023 update_topline(); /* may invalidate w_botline again */
2024 if (must_redraw != 0)
2025 {
2026 /* Don't update for changes in buffer again. */
2027 i = curbuf->b_mod_set;
2028 curbuf->b_mod_set = FALSE;
2029 win_update(curwin);
2030 must_redraw = 0;
2031 curbuf->b_mod_set = i;
2032 }
2033 recursive = FALSE;
2034 }
2035 }
2036
2037#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2038 /* restore got_int, unless CTRL-C was hit while redrawing */
2039 if (!got_int)
2040 got_int = save_got_int;
2041#endif
2042}
2043
2044#ifdef FEAT_SIGNS
2045static int draw_signcolumn __ARGS((win_T *wp));
2046
2047/*
2048 * Return TRUE when window "wp" has a column to draw signs in.
2049 */
2050 static int
2051draw_signcolumn(wp)
2052 win_T *wp;
2053{
2054 return (wp->w_buffer->b_signlist != NULL
2055# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002056 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057# endif
2058 );
2059}
2060#endif
2061
2062/*
2063 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2064 * as the filler character.
2065 */
2066 static void
2067win_draw_end(wp, c1, c2, row, endrow, hl)
2068 win_T *wp;
2069 int c1;
2070 int c2;
2071 int row;
2072 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002073 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2076 int n = 0;
2077# define FDC_OFF n
2078#else
2079# define FDC_OFF 0
2080#endif
2081
2082#ifdef FEAT_RIGHTLEFT
2083 if (wp->w_p_rl)
2084 {
2085 /* No check for cmdline window: should never be right-left. */
2086# ifdef FEAT_FOLDING
2087 n = wp->w_p_fdc;
2088
2089 if (n > 0)
2090 {
2091 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002092 if (n > W_WIDTH(wp))
2093 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2095 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2096 ' ', ' ', hl_attr(HLF_FC));
2097 }
2098# endif
2099# ifdef FEAT_SIGNS
2100 if (draw_signcolumn(wp))
2101 {
2102 int nn = n + 2;
2103
2104 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002105 if (nn > W_WIDTH(wp))
2106 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2108 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2109 ' ', ' ', hl_attr(HLF_SC));
2110 n = nn;
2111 }
2112# endif
2113 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2114 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2115 c2, c2, hl_attr(hl));
2116 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2117 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2118 c1, c2, hl_attr(hl));
2119 }
2120 else
2121#endif
2122 {
2123#ifdef FEAT_CMDWIN
2124 if (cmdwin_type != 0 && wp == curwin)
2125 {
2126 /* draw the cmdline character in the leftmost column */
2127 n = 1;
2128 if (n > wp->w_width)
2129 n = wp->w_width;
2130 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2131 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2132 cmdwin_type, ' ', hl_attr(HLF_AT));
2133 }
2134#endif
2135#ifdef FEAT_FOLDING
2136 if (wp->w_p_fdc > 0)
2137 {
2138 int nn = n + wp->w_p_fdc;
2139
2140 /* draw the fold column at the left */
2141 if (nn > W_WIDTH(wp))
2142 nn = W_WIDTH(wp);
2143 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2144 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2145 ' ', ' ', hl_attr(HLF_FC));
2146 n = nn;
2147 }
2148#endif
2149#ifdef FEAT_SIGNS
2150 if (draw_signcolumn(wp))
2151 {
2152 int nn = n + 2;
2153
2154 /* draw the sign column after the fold column */
2155 if (nn > W_WIDTH(wp))
2156 nn = W_WIDTH(wp);
2157 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2158 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2159 ' ', ' ', hl_attr(HLF_SC));
2160 n = nn;
2161 }
2162#endif
2163 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2164 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2165 c1, c2, hl_attr(hl));
2166 }
2167 set_empty_rows(wp, row);
2168}
2169
Bram Moolenaar1a384422010-07-14 19:53:30 +02002170#ifdef FEAT_SYN_HL
2171static int advance_color_col __ARGS((int vcol, int **color_cols));
2172
2173/*
2174 * Advance **color_cols and return TRUE when there are columns to draw.
2175 */
2176 static int
2177advance_color_col(vcol, color_cols)
2178 int vcol;
2179 int **color_cols;
2180{
2181 while (**color_cols >= 0 && vcol > **color_cols)
2182 ++*color_cols;
2183 return (**color_cols >= 0);
2184}
2185#endif
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#ifdef FEAT_FOLDING
2188/*
2189 * Display one folded line.
2190 */
2191 static void
2192fold_line(wp, fold_count, foldinfo, lnum, row)
2193 win_T *wp;
2194 long fold_count;
2195 foldinfo_T *foldinfo;
2196 linenr_T lnum;
2197 int row;
2198{
2199 char_u buf[51];
2200 pos_T *top, *bot;
2201 linenr_T lnume = lnum + fold_count - 1;
2202 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002203 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 int col;
2206 int txtcol;
2207 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002208 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210 /* Build the fold line:
2211 * 1. Add the cmdwin_type for the command-line window
2212 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002213 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 * 4. Compose the text
2215 * 5. Add the text
2216 * 6. set highlighting for the Visual area an other text
2217 */
2218 col = 0;
2219
2220 /*
2221 * 1. Add the cmdwin_type for the command-line window
2222 * Ignores 'rightleft', this window is never right-left.
2223 */
2224#ifdef FEAT_CMDWIN
2225 if (cmdwin_type != 0 && wp == curwin)
2226 {
2227 ScreenLines[off] = cmdwin_type;
2228 ScreenAttrs[off] = hl_attr(HLF_AT);
2229#ifdef FEAT_MBYTE
2230 if (enc_utf8)
2231 ScreenLinesUC[off] = 0;
2232#endif
2233 ++col;
2234 }
2235#endif
2236
2237 /*
2238 * 2. Add the 'foldcolumn'
2239 */
2240 fdc = wp->w_p_fdc;
2241 if (fdc > W_WIDTH(wp) - col)
2242 fdc = W_WIDTH(wp) - col;
2243 if (fdc > 0)
2244 {
2245 fill_foldcolumn(buf, wp, TRUE, lnum);
2246#ifdef FEAT_RIGHTLEFT
2247 if (wp->w_p_rl)
2248 {
2249 int i;
2250
2251 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2252 hl_attr(HLF_FC));
2253 /* reverse the fold column */
2254 for (i = 0; i < fdc; ++i)
2255 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2256 }
2257 else
2258#endif
2259 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2260 col += fdc;
2261 }
2262
2263#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002264# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2265 for (ri = 0; ri < l; ++ri) \
2266 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2267 else \
2268 for (ri = 0; ri < l; ++ri) \
2269 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002271# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2272 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273#endif
2274
Bram Moolenaar64486672010-05-16 15:46:46 +02002275 /* Set all attributes of the 'number' or 'relativenumber' column and the
2276 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002277 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
2279#ifdef FEAT_SIGNS
2280 /* If signs are being displayed, add two spaces. */
2281 if (draw_signcolumn(wp))
2282 {
2283 len = W_WIDTH(wp) - col;
2284 if (len > 0)
2285 {
2286 if (len > 2)
2287 len = 2;
2288# ifdef FEAT_RIGHTLEFT
2289 if (wp->w_p_rl)
2290 /* the line number isn't reversed */
2291 copy_text_attr(off + W_WIDTH(wp) - len - col,
2292 (char_u *)" ", len, hl_attr(HLF_FL));
2293 else
2294# endif
2295 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2296 col += len;
2297 }
2298 }
2299#endif
2300
2301 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002302 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002304 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 len = W_WIDTH(wp) - col;
2307 if (len > 0)
2308 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002309 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002310 long num;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002311
2312 if (len > w + 1)
2313 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002314
2315 if (wp->w_p_nu)
2316 /* 'number' */
2317 num = (long)lnum;
2318 else
2319 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002320 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02002321
2322 sprintf((char *)buf, "%*ld ", w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#ifdef FEAT_RIGHTLEFT
2324 if (wp->w_p_rl)
2325 /* the line number isn't reversed */
2326 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2327 hl_attr(HLF_FL));
2328 else
2329#endif
2330 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2331 col += len;
2332 }
2333 }
2334
2335 /*
2336 * 4. Compose the folded-line string with 'foldtext', if set.
2337 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002338 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
2340 txtcol = col; /* remember where text starts */
2341
2342 /*
2343 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2344 * Right-left text is put in columns 0 - number-col, normal text is put
2345 * in columns number-col - window-width.
2346 */
2347#ifdef FEAT_MBYTE
2348 if (has_mbyte)
2349 {
2350 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002351 int u8c, u8cc[MAX_MCO];
2352 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 int idx;
2354 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002355 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356# ifdef FEAT_ARABIC
2357 int prev_c = 0; /* previous Arabic character */
2358 int prev_c1 = 0; /* first composing char for prev_c */
2359# endif
2360
2361# ifdef FEAT_RIGHTLEFT
2362 if (wp->w_p_rl)
2363 idx = off;
2364 else
2365# endif
2366 idx = off + col;
2367
2368 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2369 for (p = text; *p != NUL; )
2370 {
2371 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002372 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (col + cells > W_WIDTH(wp)
2374# ifdef FEAT_RIGHTLEFT
2375 - (wp->w_p_rl ? col : 0)
2376# endif
2377 )
2378 break;
2379 ScreenLines[idx] = *p;
2380 if (enc_utf8)
2381 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002382 u8c = utfc_ptr2char(p, u8cc);
2383 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
2385 ScreenLinesUC[idx] = 0;
2386#ifdef FEAT_ARABIC
2387 prev_c = u8c;
2388#endif
2389 }
2390 else
2391 {
2392#ifdef FEAT_ARABIC
2393 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2394 {
2395 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002396 int pc, pc1, nc;
2397 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 int firstbyte = *p;
2399
2400 /* The idea of what is the previous and next
2401 * character depends on 'rightleft'. */
2402 if (wp->w_p_rl)
2403 {
2404 pc = prev_c;
2405 pc1 = prev_c1;
2406 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002407 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 else
2410 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002411 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002413 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415 prev_c = u8c;
2416
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002417 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 pc, pc1, nc);
2419 ScreenLines[idx] = firstbyte;
2420 }
2421 else
2422 prev_c = u8c;
2423#endif
2424 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002425#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (u8c >= 0x10000)
2427 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2428 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002431 for (i = 0; i < Screen_mco; ++i)
2432 {
2433 ScreenLinesC[i][idx] = u8cc[i];
2434 if (u8cc[i] == 0)
2435 break;
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
2438 if (cells > 1)
2439 ScreenLines[idx + 1] = 0;
2440 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002441 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2442 /* double-byte single width character */
2443 ScreenLines2[idx] = p[1];
2444 else if (cells > 1)
2445 /* double-width character */
2446 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 col += cells;
2448 idx += cells;
2449 p += c_len;
2450 }
2451 }
2452 else
2453#endif
2454 {
2455 len = (int)STRLEN(text);
2456 if (len > W_WIDTH(wp) - col)
2457 len = W_WIDTH(wp) - col;
2458 if (len > 0)
2459 {
2460#ifdef FEAT_RIGHTLEFT
2461 if (wp->w_p_rl)
2462 STRNCPY(current_ScreenLine, text, len);
2463 else
2464#endif
2465 STRNCPY(current_ScreenLine + col, text, len);
2466 col += len;
2467 }
2468 }
2469
2470 /* Fill the rest of the line with the fold filler */
2471#ifdef FEAT_RIGHTLEFT
2472 if (wp->w_p_rl)
2473 col -= txtcol;
2474#endif
2475 while (col < W_WIDTH(wp)
2476#ifdef FEAT_RIGHTLEFT
2477 - (wp->w_p_rl ? txtcol : 0)
2478#endif
2479 )
2480 {
2481#ifdef FEAT_MBYTE
2482 if (enc_utf8)
2483 {
2484 if (fill_fold >= 0x80)
2485 {
2486 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002487 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489 else
2490 ScreenLinesUC[off + col] = 0;
2491 }
2492#endif
2493 ScreenLines[off + col++] = fill_fold;
2494 }
2495
2496 if (text != buf)
2497 vim_free(text);
2498
2499 /*
2500 * 6. set highlighting for the Visual area an other text.
2501 * If all folded lines are in the Visual area, highlight the line.
2502 */
2503#ifdef FEAT_VISUAL
2504 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2505 {
2506 if (ltoreq(curwin->w_cursor, VIsual))
2507 {
2508 /* Visual is after curwin->w_cursor */
2509 top = &curwin->w_cursor;
2510 bot = &VIsual;
2511 }
2512 else
2513 {
2514 /* Visual is before curwin->w_cursor */
2515 top = &VIsual;
2516 bot = &curwin->w_cursor;
2517 }
2518 if (lnum >= top->lnum
2519 && lnume <= bot->lnum
2520 && (VIsual_mode != 'v'
2521 || ((lnum > top->lnum
2522 || (lnum == top->lnum
2523 && top->col == 0))
2524 && (lnume < bot->lnum
2525 || (lnume == bot->lnum
2526 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002527 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
2529 if (VIsual_mode == Ctrl_V)
2530 {
2531 /* Visual block mode: highlight the chars part of the block */
2532 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2533 {
2534 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2535 len = wp->w_old_cursor_lcol;
2536 else
2537 len = W_WIDTH(wp) - txtcol;
2538 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002539 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541 }
2542 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002545 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548 }
2549#endif
2550
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002551#ifdef FEAT_SYN_HL
2552 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002553 if (wp->w_p_cuc)
2554 {
2555 txtcol += wp->w_virtcol;
2556 if (wp->w_p_wrap)
2557 txtcol -= wp->w_skipcol;
2558 else
2559 txtcol -= wp->w_leftcol;
2560 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2561 ScreenAttrs[off + txtcol] = hl_combine_attr(
2562 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2563 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2567 (int)W_WIDTH(wp), FALSE);
2568
2569 /*
2570 * Update w_cline_height and w_cline_folded if the cursor line was
2571 * updated (saves a call to plines() later).
2572 */
2573 if (wp == curwin
2574 && lnum <= curwin->w_cursor.lnum
2575 && lnume >= curwin->w_cursor.lnum)
2576 {
2577 curwin->w_cline_row = row;
2578 curwin->w_cline_height = 1;
2579 curwin->w_cline_folded = TRUE;
2580 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2581 }
2582}
2583
2584/*
2585 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2586 */
2587 static void
2588copy_text_attr(off, buf, len, attr)
2589 int off;
2590 char_u *buf;
2591 int len;
2592 int attr;
2593{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002594 int i;
2595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 mch_memmove(ScreenLines + off, buf, (size_t)len);
2597# ifdef FEAT_MBYTE
2598 if (enc_utf8)
2599 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2600# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002601 for (i = 0; i < len; ++i)
2602 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603}
2604
2605/*
2606 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002607 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 */
2609 static void
2610fill_foldcolumn(p, wp, closed, lnum)
2611 char_u *p;
2612 win_T *wp;
2613 int closed; /* TRUE of FALSE */
2614 linenr_T lnum; /* current line number */
2615{
2616 int i = 0;
2617 int level;
2618 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002619 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621 /* Init to all spaces. */
2622 copy_spaces(p, (size_t)wp->w_p_fdc);
2623
2624 level = win_foldinfo.fi_level;
2625 if (level > 0)
2626 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002627 /* If there is only one column put more info in it. */
2628 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 /* If the column is too narrow, we start at the lowest level that
2631 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002632 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (first_level < 1)
2634 first_level = 1;
2635
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002636 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
2638 if (win_foldinfo.fi_lnum == lnum
2639 && first_level + i >= win_foldinfo.fi_low_level)
2640 p[i] = '-';
2641 else if (first_level == 1)
2642 p[i] = '|';
2643 else if (first_level + i <= 9)
2644 p[i] = '0' + first_level + i;
2645 else
2646 p[i] = '>';
2647 if (first_level + i == level)
2648 break;
2649 }
2650 }
2651 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002652 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653}
2654#endif /* FEAT_FOLDING */
2655
2656/*
2657 * Display line "lnum" of window 'wp' on the screen.
2658 * Start at row "startrow", stop when "endrow" is reached.
2659 * wp->w_virtcol needs to be valid.
2660 *
2661 * Return the number of last row the line occupies.
2662 */
2663 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002664win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 win_T *wp;
2666 linenr_T lnum;
2667 int startrow;
2668 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002669 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
2671 int col; /* visual column on screen */
2672 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2673 int c = 0; /* init for GCC */
2674 long vcol = 0; /* virtual column (for tabs) */
2675 long vcol_prev = -1; /* "vcol" of previous character */
2676 char_u *line; /* current line */
2677 char_u *ptr; /* current position in "line" */
2678 int row; /* row in the window, excl w_winrow */
2679 int screen_row; /* row on the screen, incl w_winrow */
2680
2681 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2682 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002683 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int c_extra = NUL; /* extra chars, all the same */
2685 int extra_attr = 0; /* attributes when n_extra != 0 */
2686 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2687 displaying lcs_eol at end-of-line */
2688 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2689 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2690
2691 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2692 int saved_n_extra = 0;
2693 char_u *saved_p_extra = NULL;
2694 int saved_c_extra = 0;
2695 int saved_char_attr = 0;
2696
2697 int n_attr = 0; /* chars with special attr */
2698 int saved_attr2 = 0; /* char_attr saved for n_attr */
2699 int n_attr3 = 0; /* chars with overruling special attr */
2700 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2701
2702 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2703
2704 int fromcol, tocol; /* start/end of inverting */
2705 int fromcol_prev = -2; /* start of inverting after cursor */
2706 int noinvcur = FALSE; /* don't invert the cursor */
2707#ifdef FEAT_VISUAL
2708 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002709 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710#endif
2711 pos_T pos;
2712 long v;
2713
2714 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002715 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2717 in this line */
2718 int attr = 0; /* attributes for area highlighting */
2719 int area_attr = 0; /* attributes desired by highlighting */
2720 int search_attr = 0; /* attributes desired by 'hlsearch' */
2721#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002722 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 int syntax_attr = 0; /* attributes desired by syntax */
2724 int has_syntax = FALSE; /* this buffer has syntax highl. */
2725 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002726 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002727 int draw_color_col = FALSE; /* highlight colorcolumn */
2728 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002729#endif
2730#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002731 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002732# define SPWORDLEN 150
2733 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002734 int nextlinecol = 0; /* column where nextline[] starts */
2735 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002736 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002737 int spell_attr = 0; /* attributes desired by spelling */
2738 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002739 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2740 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002741 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002742 static int cap_col = -1; /* column to check for Cap word */
2743 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002744 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745#endif
2746 int extra_check; /* has syntax or linebreak */
2747#ifdef FEAT_MBYTE
2748 int multi_attr = 0; /* attributes desired by multibyte */
2749 int mb_l = 1; /* multi-byte byte length */
2750 int mb_c = 0; /* decoded multi-byte character */
2751 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002752 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#endif
2754#ifdef FEAT_DIFF
2755 int filler_lines; /* nr of filler lines to be drawn */
2756 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002757 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 int change_start = MAXCOL; /* first col of changed area */
2759 int change_end = -1; /* last col of changed area */
2760#endif
2761 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2762#ifdef FEAT_LINEBREAK
2763 int need_showbreak = FALSE;
2764#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002765#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2766 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002768 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769#endif
2770#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002771 matchitem_T *cur; /* points to the match list */
2772 match_T *shl; /* points to search_hl or a match */
2773 int shl_flag; /* flag to indicate whether search_hl
2774 has been processed or not */
2775 int prevcol_hl_flag; /* flag to indicate whether prevcol
2776 equals startcol of search_hl or one
2777 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778#endif
2779#ifdef FEAT_ARABIC
2780 int prev_c = 0; /* previous Arabic character */
2781 int prev_c1 = 0; /* first composing char for prev_c */
2782#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002783#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002784 int did_line_attr = 0;
2785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 /* draw_state: items that are drawn in sequence: */
2788#define WL_START 0 /* nothing done yet */
2789#ifdef FEAT_CMDWIN
2790# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2791#else
2792# define WL_CMDLINE WL_START
2793#endif
2794#ifdef FEAT_FOLDING
2795# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2796#else
2797# define WL_FOLD WL_CMDLINE
2798#endif
2799#ifdef FEAT_SIGNS
2800# define WL_SIGN WL_FOLD + 1 /* column for signs */
2801#else
2802# define WL_SIGN WL_FOLD /* column for signs */
2803#endif
2804#define WL_NR WL_SIGN + 1 /* line number */
2805#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2806# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2807#else
2808# define WL_SBR WL_NR
2809#endif
2810#define WL_LINE WL_SBR + 1 /* text in the line */
2811 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002812#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 int feedback_col = 0;
2814 int feedback_old_attr = -1;
2815#endif
2816
Bram Moolenaar860cae12010-06-05 23:22:07 +02002817#ifdef FEAT_CONCEAL
2818 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002819 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002820 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002821 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002822 int is_concealing = FALSE;
2823 int boguscols = 0; /* nonexistent columns added to force
2824 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002825 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002826 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002827# define VCOL_HLC (vcol - vcol_off)
2828#else
2829# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
2832 if (startrow > endrow) /* past the end already! */
2833 return startrow;
2834
2835 row = startrow;
2836 screen_row = row + W_WINROW(wp);
2837
2838 /*
2839 * To speed up the loop below, set extra_check when there is linebreak,
2840 * trailing white space and/or syntax processing to be done.
2841 */
2842#ifdef FEAT_LINEBREAK
2843 extra_check = wp->w_p_lbr;
2844#else
2845 extra_check = 0;
2846#endif
2847#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002848 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
2850 /* Prepare for syntax highlighting in this line. When there is an
2851 * error, stop syntax highlighting. */
2852 save_did_emsg = did_emsg;
2853 did_emsg = FALSE;
2854 syntax_start(wp, lnum);
2855 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002856 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 else
2858 {
2859 did_emsg = save_did_emsg;
2860 has_syntax = TRUE;
2861 extra_check = TRUE;
2862 }
2863 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02002864
2865 /* Check for columns to display for 'colorcolumn'. */
2866 color_cols = wp->w_p_cc_cols;
2867 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002868 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002869#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002870
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002871#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002872 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02002873 && *wp->w_s->b_p_spl != NUL
2874 && wp->w_s->b_langp.ga_len > 0
2875 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002876 {
2877 /* Prepare for spell checking. */
2878 has_spell = TRUE;
2879 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002880
2881 /* Get the start of the next line, so that words that wrap to the next
2882 * line are found too: "et<line-break>al.".
2883 * Trick: skip a few chars for C/shell/Vim comments */
2884 nextline[SPWORDLEN] = NUL;
2885 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2886 {
2887 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2888 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2889 }
2890
2891 /* When a word wrapped from the previous line the start of the current
2892 * line is valid. */
2893 if (lnum == checked_lnum)
2894 cur_checked_col = checked_col;
2895 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002896
2897 /* When there was a sentence end in the previous line may require a
2898 * word starting with capital in this line. In line 1 always check
2899 * the first word. */
2900 if (lnum != capcol_lnum)
2901 cap_col = -1;
2902 if (lnum == 1)
2903 cap_col = 0;
2904 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906#endif
2907
2908 /*
2909 * handle visual active in this window
2910 */
2911 fromcol = -10;
2912 tocol = MAXCOL;
2913#ifdef FEAT_VISUAL
2914 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2915 {
2916 /* Visual is after curwin->w_cursor */
2917 if (ltoreq(curwin->w_cursor, VIsual))
2918 {
2919 top = &curwin->w_cursor;
2920 bot = &VIsual;
2921 }
2922 else /* Visual is before curwin->w_cursor */
2923 {
2924 top = &VIsual;
2925 bot = &curwin->w_cursor;
2926 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002927 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 if (VIsual_mode == Ctrl_V) /* block mode */
2929 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002930 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 {
2932 fromcol = wp->w_old_cursor_fcol;
2933 tocol = wp->w_old_cursor_lcol;
2934 }
2935 }
2936 else /* non-block mode */
2937 {
2938 if (lnum > top->lnum && lnum <= bot->lnum)
2939 fromcol = 0;
2940 else if (lnum == top->lnum)
2941 {
2942 if (VIsual_mode == 'V') /* linewise */
2943 fromcol = 0;
2944 else
2945 {
2946 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2947 if (gchar_pos(top) == NUL)
2948 tocol = fromcol + 1;
2949 }
2950 }
2951 if (VIsual_mode != 'V' && lnum == bot->lnum)
2952 {
2953 if (*p_sel == 'e' && bot->col == 0
2954#ifdef FEAT_VIRTUALEDIT
2955 && bot->coladd == 0
2956#endif
2957 )
2958 {
2959 fromcol = -10;
2960 tocol = MAXCOL;
2961 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002962 else if (bot->col == MAXCOL)
2963 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 else
2965 {
2966 pos = *bot;
2967 if (*p_sel == 'e')
2968 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2969 else
2970 {
2971 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2972 ++tocol;
2973 }
2974 }
2975 }
2976 }
2977
2978#ifndef MSDOS
2979 /* Check if the character under the cursor should not be inverted */
2980 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2981# ifdef FEAT_GUI
2982 && !gui.in_use
2983# endif
2984 )
2985 noinvcur = TRUE;
2986#endif
2987
2988 /* if inverting in this line set area_highlighting */
2989 if (fromcol >= 0)
2990 {
2991 area_highlighting = TRUE;
2992 attr = hl_attr(HLF_V);
2993#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2994 if (clip_star.available && !clip_star.owned && clip_isautosel())
2995 attr = hl_attr(HLF_VNC);
2996#endif
2997 }
2998 }
2999
3000 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003001 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 */
3003 else
3004#endif /* FEAT_VISUAL */
3005 if (highlight_match
3006 && wp == curwin
3007 && lnum >= curwin->w_cursor.lnum
3008 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3009 {
3010 if (lnum == curwin->w_cursor.lnum)
3011 getvcol(curwin, &(curwin->w_cursor),
3012 (colnr_T *)&fromcol, NULL, NULL);
3013 else
3014 fromcol = 0;
3015 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3016 {
3017 pos.lnum = lnum;
3018 pos.col = search_match_endcol;
3019 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3020 }
3021 else
3022 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003023 /* do at least one character; happens when past end of line */
3024 if (fromcol == tocol)
3025 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 area_highlighting = TRUE;
3027 attr = hl_attr(HLF_I);
3028 }
3029
3030#ifdef FEAT_DIFF
3031 filler_lines = diff_check(wp, lnum);
3032 if (filler_lines < 0)
3033 {
3034 if (filler_lines == -1)
3035 {
3036 if (diff_find_change(wp, lnum, &change_start, &change_end))
3037 diff_hlf = HLF_ADD; /* added line */
3038 else if (change_start == 0)
3039 diff_hlf = HLF_TXD; /* changed text */
3040 else
3041 diff_hlf = HLF_CHD; /* changed line */
3042 }
3043 else
3044 diff_hlf = HLF_ADD; /* added line */
3045 filler_lines = 0;
3046 area_highlighting = TRUE;
3047 }
3048 if (lnum == wp->w_topline)
3049 filler_lines = wp->w_topfill;
3050 filler_todo = filler_lines;
3051#endif
3052
3053#ifdef LINE_ATTR
3054# ifdef FEAT_SIGNS
3055 /* If this line has a sign with line highlighting set line_attr. */
3056 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3057 if (v != 0)
3058 line_attr = sign_get_attr((int)v, TRUE);
3059# endif
3060# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3061 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003062 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 line_attr = hl_attr(HLF_L);
3064# endif
3065 if (line_attr != 0)
3066 area_highlighting = TRUE;
3067#endif
3068
3069 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3070 ptr = line;
3071
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003072#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003073 if (has_spell)
3074 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003075 /* For checking first word with a capital skip white space. */
3076 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003077 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003078
Bram Moolenaar30abd282005-06-22 22:35:10 +00003079 /* To be able to spell-check over line boundaries copy the end of the
3080 * current line into nextline[]. Above the start of the next line was
3081 * copied to nextline[SPWORDLEN]. */
3082 if (nextline[SPWORDLEN] == NUL)
3083 {
3084 /* No next line or it is empty. */
3085 nextlinecol = MAXCOL;
3086 nextline_idx = 0;
3087 }
3088 else
3089 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003090 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003091 if (v < SPWORDLEN)
3092 {
3093 /* Short line, use it completely and append the start of the
3094 * next line. */
3095 nextlinecol = 0;
3096 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003097 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003098 nextline_idx = v + 1;
3099 }
3100 else
3101 {
3102 /* Long line, use only the last SPWORDLEN bytes. */
3103 nextlinecol = v - SPWORDLEN;
3104 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3105 nextline_idx = SPWORDLEN + 1;
3106 }
3107 }
3108 }
3109#endif
3110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 /* find start of trailing whitespace */
3112 if (wp->w_p_list && lcs_trail)
3113 {
3114 trailcol = (colnr_T)STRLEN(ptr);
3115 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3116 --trailcol;
3117 trailcol += (colnr_T) (ptr - line);
3118 extra_check = TRUE;
3119 }
3120
3121 /*
3122 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3123 * first character to be displayed.
3124 */
3125 if (wp->w_p_wrap)
3126 v = wp->w_skipcol;
3127 else
3128 v = wp->w_leftcol;
3129 if (v > 0)
3130 {
3131#ifdef FEAT_MBYTE
3132 char_u *prev_ptr = ptr;
3133#endif
3134 while (vcol < v && *ptr != NUL)
3135 {
3136 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3137 vcol += c;
3138#ifdef FEAT_MBYTE
3139 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003141 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 }
3143
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003144#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3145 /* When:
3146 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003147 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003148 * - 'virtualedit' is set, or
3149 * - the visual mode is active,
3150 * the end of the line may be before the start of the displayed part.
3151 */
3152 if (vcol < v && (
3153# ifdef FEAT_SYN_HL
3154 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003155 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003156# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3157 ||
3158# endif
3159# endif
3160# ifdef FEAT_VIRTUALEDIT
3161 virtual_active()
3162# ifdef FEAT_VISUAL
3163 ||
3164# endif
3165# endif
3166# ifdef FEAT_VISUAL
3167 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3168# endif
3169 ))
3170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173#endif
3174
3175 /* Handle a character that's not completely on the screen: Put ptr at
3176 * that character but skip the first few screen characters. */
3177 if (vcol > v)
3178 {
3179 vcol -= c;
3180#ifdef FEAT_MBYTE
3181 ptr = prev_ptr;
3182#else
3183 --ptr;
3184#endif
3185 n_skip = v - vcol;
3186 }
3187
3188 /*
3189 * Adjust for when the inverted text is before the screen,
3190 * and when the start of the inverted text is before the screen.
3191 */
3192 if (tocol <= vcol)
3193 fromcol = 0;
3194 else if (fromcol >= 0 && fromcol < vcol)
3195 fromcol = vcol;
3196
3197#ifdef FEAT_LINEBREAK
3198 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3199 if (wp->w_p_wrap)
3200 need_showbreak = TRUE;
3201#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003202#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003203 /* When spell checking a word we need to figure out the start of the
3204 * word and if it's badly spelled or not. */
3205 if (has_spell)
3206 {
3207 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003208 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003209 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003210
3211 pos = wp->w_cursor;
3212 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003213 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003214 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003215
3216 /* spell_move_to() may call ml_get() and make "line" invalid */
3217 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3218 ptr = line + linecol;
3219
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003220 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003221 {
3222 /* no bad word found at line start, don't check until end of a
3223 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003224 spell_hlf = HLF_COUNT;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003225 word_end = (int)(spell_to_word_end(ptr, wp)
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003226 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003227 }
3228 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003229 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003230 /* bad word found, use attributes until end of word */
3231 word_end = wp->w_cursor.col + len + 1;
3232
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003233 /* Turn index into actual attributes. */
3234 if (spell_hlf != HLF_COUNT)
3235 spell_attr = highlight_attr[spell_hlf];
3236 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003237 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003238
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003239# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003240 /* Need to restart syntax highlighting for this line. */
3241 if (has_syntax)
3242 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003243# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003244 }
3245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247
3248 /*
3249 * Correct highlighting for cursor that can't be disabled.
3250 * Avoids having to check this for each character.
3251 */
3252 if (fromcol >= 0)
3253 {
3254 if (noinvcur)
3255 {
3256 if ((colnr_T)fromcol == wp->w_virtcol)
3257 {
3258 /* highlighting starts at cursor, let it start just after the
3259 * cursor */
3260 fromcol_prev = fromcol;
3261 fromcol = -1;
3262 }
3263 else if ((colnr_T)fromcol < wp->w_virtcol)
3264 /* restart highlighting after the cursor */
3265 fromcol_prev = wp->w_virtcol;
3266 }
3267 if (fromcol >= tocol)
3268 fromcol = -1;
3269 }
3270
3271#ifdef FEAT_SEARCH_EXTRA
3272 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003273 * Handle highlighting the last used search pattern and matches.
3274 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003276 cur = wp->w_match_head;
3277 shl_flag = FALSE;
3278 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003280 if (shl_flag == FALSE)
3281 {
3282 shl = &search_hl;
3283 shl_flag = TRUE;
3284 }
3285 else
3286 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003287 shl->startcol = MAXCOL;
3288 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 shl->attr_cur = 0;
3290 if (shl->rm.regprog != NULL)
3291 {
3292 v = (long)(ptr - line);
3293 next_search_hl(wp, shl, lnum, (colnr_T)v);
3294
3295 /* Need to get the line again, a multi-line regexp may have made it
3296 * invalid. */
3297 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3298 ptr = line + v;
3299
3300 if (shl->lnum != 0 && shl->lnum <= lnum)
3301 {
3302 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003303 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003305 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3307 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003308 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003310 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003312 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003315 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003316 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 else
3318#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003319 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003321 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 {
3323 shl->attr_cur = shl->attr;
3324 search_attr = shl->attr;
3325 }
3326 area_highlighting = TRUE;
3327 }
3328 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003329 if (shl != &search_hl && cur != NULL)
3330 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 }
3332#endif
3333
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003334#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003335 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3336 * active, because it's not clear what is selected then. */
3337 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003338 {
3339 line_attr = hl_attr(HLF_CUL);
3340 area_highlighting = TRUE;
3341 }
3342#endif
3343
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003344 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 col = 0;
3346#ifdef FEAT_RIGHTLEFT
3347 if (wp->w_p_rl)
3348 {
3349 /* Rightleft window: process the text in the normal direction, but put
3350 * it in current_ScreenLine[] from right to left. Start at the
3351 * rightmost column of the window. */
3352 col = W_WIDTH(wp) - 1;
3353 off += col;
3354 }
3355#endif
3356
3357 /*
3358 * Repeat for the whole displayed line.
3359 */
3360 for (;;)
3361 {
3362 /* Skip this quickly when working on the text. */
3363 if (draw_state != WL_LINE)
3364 {
3365#ifdef FEAT_CMDWIN
3366 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3367 {
3368 draw_state = WL_CMDLINE;
3369 if (cmdwin_type != 0 && wp == curwin)
3370 {
3371 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003373 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 char_attr = hl_attr(HLF_AT);
3375 }
3376 }
3377#endif
3378
3379#ifdef FEAT_FOLDING
3380 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3381 {
3382 draw_state = WL_FOLD;
3383 if (wp->w_p_fdc > 0)
3384 {
3385 /* Draw the 'foldcolumn'. */
3386 fill_foldcolumn(extra, wp, FALSE, lnum);
3387 n_extra = wp->w_p_fdc;
3388 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003389 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 c_extra = NUL;
3391 char_attr = hl_attr(HLF_FC);
3392 }
3393 }
3394#endif
3395
3396#ifdef FEAT_SIGNS
3397 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3398 {
3399 draw_state = WL_SIGN;
3400 /* Show the sign column when there are any signs in this
3401 * buffer or when using Netbeans. */
3402 if (draw_signcolumn(wp)
3403# ifdef FEAT_DIFF
3404 && filler_todo <= 0
3405# endif
3406 )
3407 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003408 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003410 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411# endif
3412
3413 /* Draw two cells with the sign value or blank. */
3414 c_extra = ' ';
3415 char_attr = hl_attr(HLF_SC);
3416 n_extra = 2;
3417
3418 if (row == startrow)
3419 {
3420 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3421 SIGN_TEXT);
3422# ifdef FEAT_SIGN_ICONS
3423 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3424 SIGN_ICON);
3425 if (gui.in_use && icon_sign != 0)
3426 {
3427 /* Use the image in this position. */
3428 c_extra = SIGN_BYTE;
3429# ifdef FEAT_NETBEANS_INTG
3430 if (buf_signcount(wp->w_buffer, lnum) > 1)
3431 c_extra = MULTISIGN_BYTE;
3432# endif
3433 char_attr = icon_sign;
3434 }
3435 else
3436# endif
3437 if (text_sign != 0)
3438 {
3439 p_extra = sign_get_text(text_sign);
3440 if (p_extra != NULL)
3441 {
3442 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003443 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445 char_attr = sign_get_attr(text_sign, FALSE);
3446 }
3447 }
3448 }
3449 }
3450#endif
3451
3452 if (draw_state == WL_NR - 1 && n_extra == 0)
3453 {
3454 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003455 /* Display the absolute or relative line number. After the
3456 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3457 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 && (row == startrow
3459#ifdef FEAT_DIFF
3460 + filler_lines
3461#endif
3462 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3463 {
3464 /* Draw the line number (empty space after wrapping). */
3465 if (row == startrow
3466#ifdef FEAT_DIFF
3467 + filler_lines
3468#endif
3469 )
3470 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003471 long num;
3472
3473 if (wp->w_p_nu)
3474 /* 'number' */
3475 num = (long)lnum;
3476 else
3477 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003478 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar64486672010-05-16 15:46:46 +02003479
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003480 sprintf((char *)extra, "%*ld ",
Bram Moolenaar64486672010-05-16 15:46:46 +02003481 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (wp->w_skipcol > 0)
3483 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3484 *p_extra = '-';
3485#ifdef FEAT_RIGHTLEFT
3486 if (wp->w_p_rl) /* reverse line numbers */
3487 rl_mirror(extra);
3488#endif
3489 p_extra = extra;
3490 c_extra = NUL;
3491 }
3492 else
3493 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003494 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003496#ifdef FEAT_SYN_HL
3497 /* When 'cursorline' is set highlight the line number of
3498 * the current line differently. */
3499 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3500 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503 }
3504
3505#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3506 if (draw_state == WL_SBR - 1 && n_extra == 0)
3507 {
3508 draw_state = WL_SBR;
3509# ifdef FEAT_DIFF
3510 if (filler_todo > 0)
3511 {
3512 /* Draw "deleted" diff line(s). */
3513 if (char2cells(fill_diff) > 1)
3514 c_extra = '-';
3515 else
3516 c_extra = fill_diff;
3517# ifdef FEAT_RIGHTLEFT
3518 if (wp->w_p_rl)
3519 n_extra = col + 1;
3520 else
3521# endif
3522 n_extra = W_WIDTH(wp) - col;
3523 char_attr = hl_attr(HLF_DED);
3524 }
3525# endif
3526# ifdef FEAT_LINEBREAK
3527 if (*p_sbr != NUL && need_showbreak)
3528 {
3529 /* Draw 'showbreak' at the start of each broken line. */
3530 p_extra = p_sbr;
3531 c_extra = NUL;
3532 n_extra = (int)STRLEN(p_sbr);
3533 char_attr = hl_attr(HLF_AT);
3534 need_showbreak = FALSE;
3535 /* Correct end of highlighted area for 'showbreak',
3536 * required when 'linebreak' is also set. */
3537 if (tocol == vcol)
3538 tocol += n_extra;
3539 }
3540# endif
3541 }
3542#endif
3543
3544 if (draw_state == WL_LINE - 1 && n_extra == 0)
3545 {
3546 draw_state = WL_LINE;
3547 if (saved_n_extra)
3548 {
3549 /* Continue item from end of wrapped line. */
3550 n_extra = saved_n_extra;
3551 c_extra = saved_c_extra;
3552 p_extra = saved_p_extra;
3553 char_attr = saved_char_attr;
3554 }
3555 else
3556 char_attr = 0;
3557 }
3558 }
3559
3560 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003561 if (dollar_vcol != 0 && wp == curwin
3562 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563#ifdef FEAT_DIFF
3564 && filler_todo <= 0
3565#endif
3566 )
3567 {
3568 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3569 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003570 /* Pretend we have finished updating the window. Except when
3571 * 'cursorcolumn' is set. */
3572#ifdef FEAT_SYN_HL
3573 if (wp->w_p_cuc)
3574 row = wp->w_cline_row + wp->w_cline_height;
3575 else
3576#endif
3577 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 break;
3579 }
3580
3581 if (draw_state == WL_LINE && area_highlighting)
3582 {
3583 /* handle Visual or match highlighting in this line */
3584 if (vcol == fromcol
3585#ifdef FEAT_MBYTE
3586 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3587 && (*mb_ptr2cells)(ptr) > 1)
3588#endif
3589 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003590 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 && vcol < tocol))
3592 area_attr = attr; /* start highlighting */
3593 else if (area_attr != 0
3594 && (vcol == tocol
3595 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
3598#ifdef FEAT_SEARCH_EXTRA
3599 if (!n_extra)
3600 {
3601 /*
3602 * Check for start/end of search pattern match.
3603 * After end, check for start/end of next match.
3604 * When another match, have to check for start again.
3605 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003606 * Do this for 'search_hl' and the match list (ordered by
3607 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003609 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003610 cur = wp->w_match_head;
3611 shl_flag = FALSE;
3612 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003614 if (shl_flag == FALSE
3615 && ((cur != NULL
3616 && cur->priority > SEARCH_HL_PRIORITY)
3617 || cur == NULL))
3618 {
3619 shl = &search_hl;
3620 shl_flag = TRUE;
3621 }
3622 else
3623 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 while (shl->rm.regprog != NULL)
3625 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003626 if (shl->startcol != MAXCOL
3627 && v >= (long)shl->startcol
3628 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
3630 shl->attr_cur = shl->attr;
3631 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003632 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 shl->attr_cur = 0;
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 next_search_hl(wp, shl, lnum, (colnr_T)v);
3637
3638 /* Need to get the line again, a multi-line regexp
3639 * may have made it invalid. */
3640 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3641 ptr = line + v;
3642
3643 if (shl->lnum == lnum)
3644 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003645 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003647 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003649 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003651 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
3653 /* highlight empty match, try again after
3654 * it */
3655#ifdef FEAT_MBYTE
3656 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003657 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003658 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 else
3660#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003661 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663
3664 /* Loop to check if the match starts at the
3665 * current position */
3666 continue;
3667 }
3668 }
3669 break;
3670 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003671 if (shl != &search_hl && cur != NULL)
3672 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003674
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003675 /* Use attributes from match with highest priority among
3676 * 'search_hl' and the match list. */
3677 search_attr = search_hl.attr_cur;
3678 cur = wp->w_match_head;
3679 shl_flag = FALSE;
3680 while (cur != NULL || shl_flag == FALSE)
3681 {
3682 if (shl_flag == FALSE
3683 && ((cur != NULL
3684 && cur->priority > SEARCH_HL_PRIORITY)
3685 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003686 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003687 shl = &search_hl;
3688 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003689 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003690 else
3691 shl = &cur->hl;
3692 if (shl->attr_cur != 0)
3693 search_attr = shl->attr_cur;
3694 if (shl != &search_hl && cur != NULL)
3695 cur = cur->next;
3696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698#endif
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003701 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003703 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3704 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003706 if (diff_hlf == HLF_TXD && ptr - line > change_end
3707 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003709 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003712
3713 /* Decide which of the highlight attributes to use. */
3714 attr_pri = TRUE;
3715 if (area_attr != 0)
3716 char_attr = area_attr;
3717 else if (search_attr != 0)
3718 char_attr = search_attr;
3719#ifdef LINE_ATTR
3720 /* Use line_attr when not in the Visual or 'incsearch' area
3721 * (area_attr may be 0 when "noinvcur" is set). */
3722 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003723 || vcol < fromcol || vcol_prev < fromcol_prev
3724 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003725 char_attr = line_attr;
3726#endif
3727 else
3728 {
3729 attr_pri = FALSE;
3730#ifdef FEAT_SYN_HL
3731 if (has_syntax)
3732 char_attr = syntax_attr;
3733 else
3734#endif
3735 char_attr = 0;
3736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738
3739 /*
3740 * Get the next character to put on the screen.
3741 */
3742 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003743 * The "p_extra" points to the extra stuff that is inserted to
3744 * represent special characters (non-printable stuff) and other
3745 * things. When all characters are the same, c_extra is used.
3746 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3747 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3749 */
3750 if (n_extra > 0)
3751 {
3752 if (c_extra != NUL)
3753 {
3754 c = c_extra;
3755#ifdef FEAT_MBYTE
3756 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3757 if (enc_utf8 && (*mb_char2len)(c) > 1)
3758 {
3759 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003760 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003761 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763 else
3764 mb_utf8 = FALSE;
3765#endif
3766 }
3767 else
3768 {
3769 c = *p_extra;
3770#ifdef FEAT_MBYTE
3771 if (has_mbyte)
3772 {
3773 mb_c = c;
3774 if (enc_utf8)
3775 {
3776 /* If the UTF-8 character is more than one byte:
3777 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003778 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 mb_utf8 = FALSE;
3780 if (mb_l > n_extra)
3781 mb_l = 1;
3782 else if (mb_l > 1)
3783 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003784 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003786 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788 }
3789 else
3790 {
3791 /* if this is a DBCS character, put it in "mb_c" */
3792 mb_l = MB_BYTE2LEN(c);
3793 if (mb_l >= n_extra)
3794 mb_l = 1;
3795 else if (mb_l > 1)
3796 mb_c = (c << 8) + p_extra[1];
3797 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003798 if (mb_l == 0) /* at the NUL at end-of-line */
3799 mb_l = 1;
3800
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 /* If a double-width char doesn't fit display a '>' in the
3802 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003803 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804# ifdef FEAT_RIGHTLEFT
3805 wp->w_p_rl ? (col <= 0) :
3806# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003807 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 && (*mb_char2cells)(mb_c) == 2)
3809 {
3810 c = '>';
3811 mb_c = c;
3812 mb_l = 1;
3813 mb_utf8 = FALSE;
3814 multi_attr = hl_attr(HLF_AT);
3815 /* put the pointer back to output the double-width
3816 * character at the start of the next line. */
3817 ++n_extra;
3818 --p_extra;
3819 }
3820 else
3821 {
3822 n_extra -= mb_l - 1;
3823 p_extra += mb_l - 1;
3824 }
3825 }
3826#endif
3827 ++p_extra;
3828 }
3829 --n_extra;
3830 }
3831 else
3832 {
3833 /*
3834 * Get a character from the line itself.
3835 */
3836 c = *ptr;
3837#ifdef FEAT_MBYTE
3838 if (has_mbyte)
3839 {
3840 mb_c = c;
3841 if (enc_utf8)
3842 {
3843 /* If the UTF-8 character is more than one byte: Decode it
3844 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003845 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 mb_utf8 = FALSE;
3847 if (mb_l > 1)
3848 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003849 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 /* Overlong encoded ASCII or ASCII with composing char
3851 * is displayed normally, except a NUL. */
3852 if (mb_c < 0x80)
3853 c = mb_c;
3854 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003855
3856 /* At start of the line we can have a composing char.
3857 * Draw it as a space with a composing char. */
3858 if (utf_iscomposing(mb_c))
3859 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003860 int i;
3861
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003862 for (i = Screen_mco - 1; i > 0; --i)
3863 u8cc[i] = u8cc[i - 1];
3864 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003865 mb_c = ' ';
3866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868
3869 if ((mb_l == 1 && c >= 0x80)
3870 || (mb_l >= 1 && mb_c == 0)
3871 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003872# ifdef UNICODE16
3873 || mb_c >= 0x10000
3874# endif
3875 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
3877 /*
3878 * Illegal UTF-8 byte: display as <xx>.
3879 * Non-BMP character : display as ? or fullwidth ?.
3880 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003881# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
3885 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003886# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (wp->w_p_rl) /* reverse */
3888 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003891# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 else if (utf_char2cells(mb_c) != 2)
3893 STRCPY(extra, "?");
3894 else
3895 /* 0xff1f in UTF-8: full-width '?' */
3896 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003897# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
3899 p_extra = extra;
3900 c = *p_extra;
3901 mb_c = mb_ptr2char_adv(&p_extra);
3902 mb_utf8 = (c >= 0x80);
3903 n_extra = (int)STRLEN(p_extra);
3904 c_extra = NUL;
3905 if (area_attr == 0 && search_attr == 0)
3906 {
3907 n_attr = n_extra + 1;
3908 extra_attr = hl_attr(HLF_8);
3909 saved_attr2 = char_attr; /* save current attr */
3910 }
3911 }
3912 else if (mb_l == 0) /* at the NUL at end-of-line */
3913 mb_l = 1;
3914#ifdef FEAT_ARABIC
3915 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3916 {
3917 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003918 int pc, pc1, nc;
3919 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 /* The idea of what is the previous and next
3922 * character depends on 'rightleft'. */
3923 if (wp->w_p_rl)
3924 {
3925 pc = prev_c;
3926 pc1 = prev_c1;
3927 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003928 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
3930 else
3931 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003932 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003934 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936 prev_c = mb_c;
3937
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003938 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 }
3940 else
3941 prev_c = mb_c;
3942#endif
3943 }
3944 else /* enc_dbcs */
3945 {
3946 mb_l = MB_BYTE2LEN(c);
3947 if (mb_l == 0) /* at the NUL at end-of-line */
3948 mb_l = 1;
3949 else if (mb_l > 1)
3950 {
3951 /* We assume a second byte below 32 is illegal.
3952 * Hopefully this is OK for all double-byte encodings!
3953 */
3954 if (ptr[1] >= 32)
3955 mb_c = (c << 8) + ptr[1];
3956 else
3957 {
3958 if (ptr[1] == NUL)
3959 {
3960 /* head byte at end of line */
3961 mb_l = 1;
3962 transchar_nonprint(extra, c);
3963 }
3964 else
3965 {
3966 /* illegal tail byte */
3967 mb_l = 2;
3968 STRCPY(extra, "XX");
3969 }
3970 p_extra = extra;
3971 n_extra = (int)STRLEN(extra) - 1;
3972 c_extra = NUL;
3973 c = *p_extra++;
3974 if (area_attr == 0 && search_attr == 0)
3975 {
3976 n_attr = n_extra + 1;
3977 extra_attr = hl_attr(HLF_8);
3978 saved_attr2 = char_attr; /* save current attr */
3979 }
3980 mb_c = c;
3981 }
3982 }
3983 }
3984 /* If a double-width char doesn't fit display a '>' in the
3985 * last column; the character is displayed at the start of the
3986 * next line. */
3987 if ((
3988# ifdef FEAT_RIGHTLEFT
3989 wp->w_p_rl ? (col <= 0) :
3990# endif
3991 (col >= W_WIDTH(wp) - 1))
3992 && (*mb_char2cells)(mb_c) == 2)
3993 {
3994 c = '>';
3995 mb_c = c;
3996 mb_utf8 = FALSE;
3997 mb_l = 1;
3998 multi_attr = hl_attr(HLF_AT);
3999 /* Put pointer back so that the character will be
4000 * displayed at the start of the next line. */
4001 --ptr;
4002 }
4003 else if (*ptr != NUL)
4004 ptr += mb_l - 1;
4005
4006 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004007 * a '<' in the first column. Don't do this for unprintable
4008 * charactes. */
4009 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004012 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 c = ' ';
4014 if (area_attr == 0 && search_attr == 0)
4015 {
4016 n_attr = n_extra + 1;
4017 extra_attr = hl_attr(HLF_AT);
4018 saved_attr2 = char_attr; /* save current attr */
4019 }
4020 mb_c = c;
4021 mb_utf8 = FALSE;
4022 mb_l = 1;
4023 }
4024
4025 }
4026#endif
4027 ++ptr;
4028
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004029 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004030 if (wp->w_p_list && (c == 160
4031#ifdef FEAT_MBYTE
4032 || (mb_utf8 && mb_c == 160)
4033#endif
4034 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004035 {
4036 c = lcs_nbsp;
4037 if (area_attr == 0 && search_attr == 0)
4038 {
4039 n_attr = 1;
4040 extra_attr = hl_attr(HLF_8);
4041 saved_attr2 = char_attr; /* save current attr */
4042 }
4043#ifdef FEAT_MBYTE
4044 mb_c = c;
4045 if (enc_utf8 && (*mb_char2len)(c) > 1)
4046 {
4047 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004048 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004049 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004050 }
4051 else
4052 mb_utf8 = FALSE;
4053#endif
4054 }
4055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (extra_check)
4057 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004058#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004059 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004060#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004061
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004062#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 /* Get syntax attribute, unless still at the start of the line
4064 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004065 v = (long)(ptr - line);
4066 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
4068 /* Get the syntax attribute for the character. If there
4069 * is an error, disable syntax highlighting. */
4070 save_did_emsg = did_emsg;
4071 did_emsg = FALSE;
4072
Bram Moolenaar217ad922005-03-20 22:37:15 +00004073 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004074# ifdef FEAT_SPELL
4075 has_spell ? &can_spell :
4076# endif
4077 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004080 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004081 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004082 has_syntax = FALSE;
4083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 else
4085 did_emsg = save_did_emsg;
4086
4087 /* Need to get the line again, a multi-line regexp may
4088 * have made it invalid. */
4089 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4090 ptr = line + v;
4091
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004092 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004094 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004095 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004096# ifdef FEAT_CONCEAL
4097 /* no concealing past the end of the line, it interferes
4098 * with line highlighting */
4099 if (c == NUL)
4100 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004101 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004102 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004105#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004106
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004107#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004108 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004109 * Only do this when there is no syntax highlighting, the
4110 * @Spell cluster is not used or the current syntax item
4111 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004112 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004113 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004114 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004115# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004116 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004117 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004118# endif
4119 if (c != 0 && (
4120# ifdef FEAT_SYN_HL
4121 !has_syntax ||
4122# endif
4123 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004124 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004125 char_u *prev_ptr, *p;
4126 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004127 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004128# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004129 if (has_mbyte)
4130 {
4131 prev_ptr = ptr - mb_l;
4132 v -= mb_l - 1;
4133 }
4134 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004135# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004136 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004137
4138 /* Use nextline[] if possible, it has the start of the
4139 * next line concatenated. */
4140 if ((prev_ptr - line) - nextlinecol >= 0)
4141 p = nextline + (prev_ptr - line) - nextlinecol;
4142 else
4143 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004144 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004145 len = spell_check(wp, p, &spell_hlf, &cap_col,
4146 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004147 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004148
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004149 /* In Insert mode only highlight a word that
4150 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004151 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004152 && (State & INSERT) != 0
4153 && wp->w_cursor.lnum == lnum
4154 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004155 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004156 && wp->w_cursor.col < (colnr_T)word_end)
4157 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004158 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004159 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004160 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004161
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004162 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004163 && (p - nextline) + len > nextline_idx)
4164 {
4165 /* Remember that the good word continues at the
4166 * start of the next line. */
4167 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004168 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004169 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004170
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004171 /* Turn index into actual attributes. */
4172 if (spell_hlf != HLF_COUNT)
4173 spell_attr = highlight_attr[spell_hlf];
4174
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004175 if (cap_col > 0)
4176 {
4177 if (p != prev_ptr
4178 && (p - nextline) + cap_col >= nextline_idx)
4179 {
4180 /* Remember that the word in the next line
4181 * must start with a capital. */
4182 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004183 cap_col = (int)((p - nextline) + cap_col
4184 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004185 }
4186 else
4187 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004188 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004189 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004190 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004191 }
4192 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004193 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004194 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004195 char_attr = hl_combine_attr(char_attr, spell_attr);
4196 else
4197 char_attr = hl_combine_attr(spell_attr, char_attr);
4198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199#endif
4200#ifdef FEAT_LINEBREAK
4201 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004202 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 */
4204 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004205 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
4207 n_extra = win_lbr_chartabsize(wp, ptr - (
4208# ifdef FEAT_MBYTE
4209 has_mbyte ? mb_l :
4210# endif
4211 1), (colnr_T)vcol, NULL) - 1;
4212 c_extra = ' ';
4213 if (vim_iswhite(c))
4214 c = ' ';
4215 }
4216#endif
4217
4218 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4219 {
4220 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004221 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
4223 n_attr = 1;
4224 extra_attr = hl_attr(HLF_8);
4225 saved_attr2 = char_attr; /* save current attr */
4226 }
4227#ifdef FEAT_MBYTE
4228 mb_c = c;
4229 if (enc_utf8 && (*mb_char2len)(c) > 1)
4230 {
4231 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004232 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004233 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235 else
4236 mb_utf8 = FALSE;
4237#endif
4238 }
4239 }
4240
4241 /*
4242 * Handling of non-printable characters.
4243 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004244 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 /*
4247 * when getting a character from the file, we may have to
4248 * turn it into something else on the way to putting it
4249 * into "ScreenLines".
4250 */
4251 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4252 {
4253 /* tab amount depends on current column */
4254 n_extra = (int)wp->w_buffer->b_p_ts
4255 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4256#ifdef FEAT_MBYTE
4257 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4258#endif
4259 if (wp->w_p_list)
4260 {
4261 c = lcs_tab1;
4262 c_extra = lcs_tab2;
4263 n_attr = n_extra + 1;
4264 extra_attr = hl_attr(HLF_8);
4265 saved_attr2 = char_attr; /* save current attr */
4266#ifdef FEAT_MBYTE
4267 mb_c = c;
4268 if (enc_utf8 && (*mb_char2len)(c) > 1)
4269 {
4270 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004271 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004272 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274#endif
4275 }
4276 else
4277 {
4278 c_extra = ' ';
4279 c = ' ';
4280 }
4281 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004282 else if (c == NUL
4283 && ((wp->w_p_list && lcs_eol > 0)
4284 || ((fromcol >= 0 || fromcol_prev >= 0)
4285 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004286#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004287 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004288#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004289 && (
4290# ifdef FEAT_RIGHTLEFT
4291 wp->w_p_rl ? (col >= 0) :
4292# endif
4293 (col < W_WIDTH(wp)))
4294 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004295 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004296 && (colnr_T)vcol == wp->w_virtcol)))
4297 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004299 /* Display a '$' after the line or highlight an extra
4300 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4302 /* For a diff line the highlighting continues after the
4303 * "$". */
4304 if (
4305# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004306 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307# ifdef LINE_ATTR
4308 &&
4309# endif
4310# endif
4311# ifdef LINE_ATTR
4312 line_attr == 0
4313# endif
4314 )
4315#endif
4316 {
4317#ifdef FEAT_VIRTUALEDIT
4318 /* In virtualedit, visual selections may extend
4319 * beyond end of line. */
4320 if (area_highlighting && virtual_active()
4321 && tocol != MAXCOL && vcol < tocol)
4322 n_extra = 0;
4323 else
4324#endif
4325 {
4326 p_extra = at_end_str;
4327 n_extra = 1;
4328 c_extra = NUL;
4329 }
4330 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004331 if (wp->w_p_list)
4332 c = lcs_eol;
4333 else
4334 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 lcs_eol_one = -1;
4336 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004337 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
4339 extra_attr = hl_attr(HLF_AT);
4340 n_attr = 1;
4341 }
4342#ifdef FEAT_MBYTE
4343 mb_c = c;
4344 if (enc_utf8 && (*mb_char2len)(c) > 1)
4345 {
4346 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004347 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004348 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 else
4351 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4352#endif
4353 }
4354 else if (c != NUL)
4355 {
4356 p_extra = transchar(c);
4357#ifdef FEAT_RIGHTLEFT
4358 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4359 rl_mirror(p_extra); /* reverse "<12>" */
4360#endif
4361 n_extra = byte2cells(c) - 1;
4362 c_extra = NUL;
4363 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004364 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 {
4366 n_attr = n_extra + 1;
4367 extra_attr = hl_attr(HLF_8);
4368 saved_attr2 = char_attr; /* save current attr */
4369 }
4370#ifdef FEAT_MBYTE
4371 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4372#endif
4373 }
4374#ifdef FEAT_VIRTUALEDIT
4375 else if (VIsual_active
4376 && (VIsual_mode == Ctrl_V
4377 || VIsual_mode == 'v')
4378 && virtual_active()
4379 && tocol != MAXCOL
4380 && vcol < tocol
4381 && (
4382# ifdef FEAT_RIGHTLEFT
4383 wp->w_p_rl ? (col >= 0) :
4384# endif
4385 (col < W_WIDTH(wp))))
4386 {
4387 c = ' ';
4388 --ptr; /* put it back at the NUL */
4389 }
4390#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004391#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 else if ((
4393# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004394 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 ) && (
4398# ifdef FEAT_RIGHTLEFT
4399 wp->w_p_rl ? (col >= 0) :
4400# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004401 (col
4402# ifdef FEAT_CONCEAL
4403 - boguscols
4404# endif
4405 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 {
4407 /* Highlight until the right side of the window */
4408 c = ' ';
4409 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004410
4411 /* Remember we do the char for line highlighting. */
4412 ++did_line_attr;
4413
4414 /* don't do search HL for the rest of the line */
4415 if (line_attr != 0 && char_attr == search_attr && col > 0)
4416 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417# ifdef FEAT_DIFF
4418 if (diff_hlf == HLF_TXD)
4419 {
4420 diff_hlf = HLF_CHD;
4421 if (attr == 0 || char_attr != attr)
4422 char_attr = hl_attr(diff_hlf);
4423 }
4424# endif
4425 }
4426#endif
4427 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004428
4429#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004430 if ( wp->w_p_cole > 0
4431 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4432 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004433 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004434 && !(lnum_in_visual_area
4435 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004436 {
4437 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004438 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004439 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4440 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004441 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004442 /* First time at this concealed item: display one
4443 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004444 if (syn_get_sub_char() != NUL)
4445 c = syn_get_sub_char();
4446 else if (lcs_conceal != NUL)
4447 c = lcs_conceal;
4448 else
4449 c = ' ';
4450
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004451 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004452
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004453 if (n_extra > 0)
4454 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004455 vcol += n_extra;
4456 if (wp->w_p_wrap && n_extra > 0)
4457 {
4458# ifdef FEAT_RIGHTLEFT
4459 if (wp->w_p_rl)
4460 {
4461 col -= n_extra;
4462 boguscols -= n_extra;
4463 }
4464 else
4465# endif
4466 {
4467 boguscols += n_extra;
4468 col += n_extra;
4469 }
4470 }
4471 n_extra = 0;
4472 n_attr = 0;
4473 }
4474 else if (n_skip == 0)
4475 {
4476 is_concealing = TRUE;
4477 n_skip = 1;
4478 }
4479# ifdef FEAT_MBYTE
4480 mb_c = c;
4481 if (enc_utf8 && (*mb_char2len)(c) > 1)
4482 {
4483 mb_utf8 = TRUE;
4484 u8cc[0] = 0;
4485 c = 0xc0;
4486 }
4487 else
4488 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4489# endif
4490 }
4491 else
4492 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004493 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004494 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004495 }
4496#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004499#ifdef FEAT_CONCEAL
4500 /* In the cursor line and we may be concealing characters: correct
4501 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004502 if (!did_wcol && draw_state == WL_LINE
4503 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004504 && conceal_cursor_line(wp)
4505 && (int)wp->w_virtcol <= vcol + n_skip)
4506 {
4507 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004508 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004509 did_wcol = TRUE;
4510 }
4511#endif
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 /* Don't override visual selection highlighting. */
4514 if (n_attr > 0
4515 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004516 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 char_attr = extra_attr;
4518
Bram Moolenaar81695252004-12-29 20:58:21 +00004519#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 /* XIM don't send preedit_start and preedit_end, but they send
4521 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4522 * im_is_preediting() here. */
4523 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004524 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 && (State & INSERT)
4526 && !p_imdisable
4527 && im_is_preediting()
4528 && draw_state == WL_LINE)
4529 {
4530 colnr_T tcol;
4531
4532 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004533 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 else
4535 tcol = preedit_end_col;
4536 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4537 {
4538 if (feedback_old_attr < 0)
4539 {
4540 feedback_col = 0;
4541 feedback_old_attr = char_attr;
4542 }
4543 char_attr = im_get_feedback_attr(feedback_col);
4544 if (char_attr < 0)
4545 char_attr = feedback_old_attr;
4546 feedback_col++;
4547 }
4548 else if (feedback_old_attr >= 0)
4549 {
4550 char_attr = feedback_old_attr;
4551 feedback_old_attr = -1;
4552 feedback_col = 0;
4553 }
4554 }
4555#endif
4556 /*
4557 * Handle the case where we are in column 0 but not on the first
4558 * character of the line and the user wants us to show us a
4559 * special character (via 'listchars' option "precedes:<char>".
4560 */
4561 if (lcs_prec_todo != NUL
4562 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4563#ifdef FEAT_DIFF
4564 && filler_todo <= 0
4565#endif
4566 && draw_state > WL_NR
4567 && c != NUL)
4568 {
4569 c = lcs_prec;
4570 lcs_prec_todo = NUL;
4571#ifdef FEAT_MBYTE
4572 mb_c = c;
4573 if (enc_utf8 && (*mb_char2len)(c) > 1)
4574 {
4575 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004576 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004577 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
4579 else
4580 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4581#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004582 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
4584 saved_attr3 = char_attr; /* save current attr */
4585 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4586 n_attr3 = 1;
4587 }
4588 }
4589
4590 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004591 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004593 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004594#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004595 || did_line_attr == 1
4596#endif
4597 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004599#ifdef FEAT_SEARCH_EXTRA
4600 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004601
4602 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004603 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004604 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004605#endif
4606
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004607 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 * highlight match at end of line. If it's beyond the last
4609 * char on the screen, just overwrite that one (tricky!) Not
4610 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004611#ifdef FEAT_SEARCH_EXTRA
4612 prevcol_hl_flag = FALSE;
4613 if (prevcol == (long)search_hl.startcol)
4614 prevcol_hl_flag = TRUE;
4615 else
4616 {
4617 cur = wp->w_match_head;
4618 while (cur != NULL)
4619 {
4620 if (prevcol == (long)cur->hl.startcol)
4621 {
4622 prevcol_hl_flag = TRUE;
4623 break;
4624 }
4625 cur = cur->next;
4626 }
4627 }
4628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004630 && ((area_attr != 0 && vcol == fromcol
4631#ifdef FEAT_VISUAL
4632 && (VIsual_mode != Ctrl_V
4633 || lnum == VIsual.lnum
4634 || lnum == curwin->w_cursor.lnum)
4635#endif
4636 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#ifdef FEAT_SEARCH_EXTRA
4638 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004639 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004640# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004641 && did_line_attr <= 1
4642# endif
4643 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644#endif
4645 ))
4646 {
4647 int n = 0;
4648
4649#ifdef FEAT_RIGHTLEFT
4650 if (wp->w_p_rl)
4651 {
4652 if (col < 0)
4653 n = 1;
4654 }
4655 else
4656#endif
4657 {
4658 if (col >= W_WIDTH(wp))
4659 n = -1;
4660 }
4661 if (n != 0)
4662 {
4663 /* At the window boundary, highlight the last character
4664 * instead (better than nothing). */
4665 off += n;
4666 col += n;
4667 }
4668 else
4669 {
4670 /* Add a blank character to highlight. */
4671 ScreenLines[off] = ' ';
4672#ifdef FEAT_MBYTE
4673 if (enc_utf8)
4674 ScreenLinesUC[off] = 0;
4675#endif
4676 }
4677#ifdef FEAT_SEARCH_EXTRA
4678 if (area_attr == 0)
4679 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004680 /* Use attributes from match with highest priority among
4681 * 'search_hl' and the match list. */
4682 char_attr = search_hl.attr;
4683 cur = wp->w_match_head;
4684 shl_flag = FALSE;
4685 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004686 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004687 if (shl_flag == FALSE
4688 && ((cur != NULL
4689 && cur->priority > SEARCH_HL_PRIORITY)
4690 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004691 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004692 shl = &search_hl;
4693 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004694 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004695 else
4696 shl = &cur->hl;
4697 if ((ptr - line) - 1 == (long)shl->startcol)
4698 char_attr = shl->attr;
4699 if (shl != &search_hl && cur != NULL)
4700 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703#endif
4704 ScreenAttrs[off] = char_attr;
4705#ifdef FEAT_RIGHTLEFT
4706 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004707 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004709 --off;
4710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 else
4712#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004715 ++off;
4716 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004717 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004718#ifdef FEAT_SYN_HL
4719 eol_hl_off = 1;
4720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
Bram Moolenaar91170f82006-05-05 21:15:17 +00004724 /*
4725 * At end of the text line.
4726 */
4727 if (c == NUL)
4728 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004729#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004730 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4731 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004732 {
4733 /* highlight last char after line */
4734 --col;
4735 --off;
4736 --vcol;
4737 }
4738
Bram Moolenaar1a384422010-07-14 19:53:30 +02004739 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004740 if (wp->w_p_wrap)
4741 v = wp->w_skipcol;
4742 else
4743 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004744
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004745 /* check if line ends before left margin */
4746 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004747 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004748#ifdef FEAT_CONCEAL
4749 /* Get rid of the boguscols now, we want to draw until the right
4750 * edge for 'cursorcolumn'. */
4751 col -= boguscols;
4752 boguscols = 0;
4753#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004754
4755 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004756 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004757
4758 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004759 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4760 && (int)wp->w_virtcol <
4761 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004762 && lnum != wp->w_cursor.lnum)
4763 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004764# ifdef FEAT_RIGHTLEFT
4765 && !wp->w_p_rl
4766# endif
4767 )
4768 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004769 int rightmost_vcol = 0;
4770 int i;
4771
4772 if (wp->w_p_cuc)
4773 rightmost_vcol = wp->w_virtcol;
4774 if (draw_color_col)
4775 /* determine rightmost colorcolumn to possibly draw */
4776 for (i = 0; color_cols[i] >= 0; ++i)
4777 if (rightmost_vcol < color_cols[i])
4778 rightmost_vcol = color_cols[i];
4779
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004780 while (col < W_WIDTH(wp))
4781 {
4782 ScreenLines[off] = ' ';
4783#ifdef FEAT_MBYTE
4784 if (enc_utf8)
4785 ScreenLinesUC[off] = 0;
4786#endif
4787 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004788 if (draw_color_col)
4789 draw_color_col = advance_color_col(VCOL_HLC,
4790 &color_cols);
4791
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004792 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004793 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004794 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004795 ScreenAttrs[off++] = hl_attr(HLF_MC);
4796 else
4797 ScreenAttrs[off++] = 0;
4798
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004799 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004800 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004801
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004802 ++vcol;
4803 }
4804 }
4805#endif
4806
Bram Moolenaar860cae12010-06-05 23:22:07 +02004807 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
4808 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 row++;
4810
4811 /*
4812 * Update w_cline_height and w_cline_folded if the cursor line was
4813 * updated (saves a call to plines() later).
4814 */
4815 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4816 {
4817 curwin->w_cline_row = startrow;
4818 curwin->w_cline_height = row - startrow;
4819#ifdef FEAT_FOLDING
4820 curwin->w_cline_folded = FALSE;
4821#endif
4822 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4823 }
4824
4825 break;
4826 }
4827
4828 /* line continues beyond line end */
4829 if (lcs_ext
4830 && !wp->w_p_wrap
4831#ifdef FEAT_DIFF
4832 && filler_todo <= 0
4833#endif
4834 && (
4835#ifdef FEAT_RIGHTLEFT
4836 wp->w_p_rl ? col == 0 :
4837#endif
4838 col == W_WIDTH(wp) - 1)
4839 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004840 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4842 {
4843 c = lcs_ext;
4844 char_attr = hl_attr(HLF_AT);
4845#ifdef FEAT_MBYTE
4846 mb_c = c;
4847 if (enc_utf8 && (*mb_char2len)(c) > 1)
4848 {
4849 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004850 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004851 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 else
4854 mb_utf8 = FALSE;
4855#endif
4856 }
4857
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004858#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004859 /* advance to the next 'colorcolumn' */
4860 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004861 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004862
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004863 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02004864 * highlight the cursor position itself.
4865 * Also highlight the 'colorcolumn' if it is different than
4866 * 'cursorcolumn' */
4867 vcol_save_attr = -1;
4868 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004869 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004870 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02004871 && lnum != wp->w_cursor.lnum)
4872 {
4873 vcol_save_attr = char_attr;
4874 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4875 }
Bram Moolenaard160c342010-07-18 23:30:34 +02004876 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004877 {
4878 vcol_save_attr = char_attr;
4879 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
4880 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004881 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004882#endif
4883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 /*
4885 * Store character to be displayed.
4886 * Skip characters that are left of the screen for 'nowrap'.
4887 */
4888 vcol_prev = vcol;
4889 if (draw_state < WL_LINE || n_skip <= 0)
4890 {
4891 /*
4892 * Store the character.
4893 */
4894#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4895 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4896 {
4897 /* A double-wide character is: put first halve in left cell. */
4898 --off;
4899 --col;
4900 }
4901#endif
4902 ScreenLines[off] = c;
4903#ifdef FEAT_MBYTE
4904 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01004905 {
4906 if ((mb_c & 0xff00) == 0x8e00)
4907 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 else if (enc_utf8)
4911 {
4912 if (mb_utf8)
4913 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004914 int i;
4915
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004917 if ((c & 0xff) == 0)
4918 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004919 for (i = 0; i < Screen_mco; ++i)
4920 {
4921 ScreenLinesC[i][off] = u8cc[i];
4922 if (u8cc[i] == 0)
4923 break;
4924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 else
4927 ScreenLinesUC[off] = 0;
4928 }
4929 if (multi_attr)
4930 {
4931 ScreenAttrs[off] = multi_attr;
4932 multi_attr = 0;
4933 }
4934 else
4935#endif
4936 ScreenAttrs[off] = char_attr;
4937
4938#ifdef FEAT_MBYTE
4939 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4940 {
4941 /* Need to fill two screen columns. */
4942 ++off;
4943 ++col;
4944 if (enc_utf8)
4945 /* UTF-8: Put a 0 in the second screen char. */
4946 ScreenLines[off] = 0;
4947 else
4948 /* DBCS: Put second byte in the second screen char. */
4949 ScreenLines[off] = mb_c & 0xff;
4950 ++vcol;
4951 /* When "tocol" is halfway a character, set it to the end of
4952 * the character, otherwise highlighting won't stop. */
4953 if (tocol == vcol)
4954 ++tocol;
4955#ifdef FEAT_RIGHTLEFT
4956 if (wp->w_p_rl)
4957 {
4958 /* now it's time to backup one cell */
4959 --off;
4960 --col;
4961 }
4962#endif
4963 }
4964#endif
4965#ifdef FEAT_RIGHTLEFT
4966 if (wp->w_p_rl)
4967 {
4968 --off;
4969 --col;
4970 }
4971 else
4972#endif
4973 {
4974 ++off;
4975 ++col;
4976 }
4977 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004978#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004979 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004980 {
4981 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004982 ++vcol_off;
4983 if (n_extra > 0)
4984 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004985 if (wp->w_p_wrap)
4986 {
4987 /*
4988 * Special voodoo required if 'wrap' is on.
4989 *
4990 * Advance the column indicator to force the line
4991 * drawing to wrap early. This will make the line
4992 * take up the same screen space when parts are concealed,
4993 * so that cursor line computations aren't messed up.
4994 *
4995 * To avoid the fictitious advance of 'col' causing
4996 * trailing junk to be written out of the screen line
4997 * we are building, 'boguscols' keeps track of the number
4998 * of bad columns we have advanced.
4999 */
5000 if (n_extra > 0)
5001 {
5002 vcol += n_extra;
5003# ifdef FEAT_RIGHTLEFT
5004 if (wp->w_p_rl)
5005 {
5006 col -= n_extra;
5007 boguscols -= n_extra;
5008 }
5009 else
5010# endif
5011 {
5012 col += n_extra;
5013 boguscols += n_extra;
5014 }
5015 n_extra = 0;
5016 n_attr = 0;
5017 }
5018
5019
5020# ifdef FEAT_MBYTE
5021 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5022 {
5023 /* Need to fill two screen columns. */
5024# ifdef FEAT_RIGHTLEFT
5025 if (wp->w_p_rl)
5026 {
5027 --boguscols;
5028 --col;
5029 }
5030 else
5031# endif
5032 {
5033 ++boguscols;
5034 ++col;
5035 }
5036 }
5037# endif
5038
5039# ifdef FEAT_RIGHTLEFT
5040 if (wp->w_p_rl)
5041 {
5042 --boguscols;
5043 --col;
5044 }
5045 else
5046# endif
5047 {
5048 ++boguscols;
5049 ++col;
5050 }
5051 }
5052 else
5053 {
5054 if (n_extra > 0)
5055 {
5056 vcol += n_extra;
5057 n_extra = 0;
5058 n_attr = 0;
5059 }
5060 }
5061
5062 }
5063#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 else
5065 --n_skip;
5066
Bram Moolenaar64486672010-05-16 15:46:46 +02005067 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5068 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005069 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070#ifdef FEAT_DIFF
5071 && filler_todo <= 0
5072#endif
5073 )
5074 ++vcol;
5075
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005076#ifdef FEAT_SYN_HL
5077 if (vcol_save_attr >= 0)
5078 char_attr = vcol_save_attr;
5079#endif
5080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 /* restore attributes after "predeces" in 'listchars' */
5082 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5083 char_attr = saved_attr3;
5084
5085 /* restore attributes after last 'listchars' or 'number' char */
5086 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5087 char_attr = saved_attr2;
5088
5089 /*
5090 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005091 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
5093 if ((
5094#ifdef FEAT_RIGHTLEFT
5095 wp->w_p_rl ? (col < 0) :
5096#endif
5097 (col >= W_WIDTH(wp)))
5098 && (*ptr != NUL
5099#ifdef FEAT_DIFF
5100 || filler_todo > 0
5101#endif
Bram Moolenaar5ec3aea2011-02-09 17:09:30 +01005102 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str
5103#ifdef FEAT_SYN_HL
5104 && !wp->w_p_cul
5105#endif
5106 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5108 )
5109 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005110#ifdef FEAT_CONCEAL
5111 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5112 (int)W_WIDTH(wp), wp->w_p_rl);
5113 boguscols = 0;
5114#else
5115 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5116 (int)W_WIDTH(wp), wp->w_p_rl);
5117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 ++row;
5119 ++screen_row;
5120
5121 /* When not wrapping and finished diff lines, or when displayed
5122 * '$' and highlighting until last column, break here. */
5123 if ((!wp->w_p_wrap
5124#ifdef FEAT_DIFF
5125 && filler_todo <= 0
5126#endif
5127 ) || lcs_eol_one == -1)
5128 break;
5129
5130 /* When the window is too narrow draw all "@" lines. */
5131 if (draw_state != WL_LINE
5132#ifdef FEAT_DIFF
5133 && filler_todo <= 0
5134#endif
5135 )
5136 {
5137 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5138#ifdef FEAT_VERTSPLIT
5139 draw_vsep_win(wp, row);
5140#endif
5141 row = endrow;
5142 }
5143
5144 /* When line got too long for screen break here. */
5145 if (row == endrow)
5146 {
5147 ++row;
5148 break;
5149 }
5150
5151 if (screen_cur_row == screen_row - 1
5152#ifdef FEAT_DIFF
5153 && filler_todo <= 0
5154#endif
5155 && W_WIDTH(wp) == Columns)
5156 {
5157 /* Remember that the line wraps, used for modeless copy. */
5158 LineWraps[screen_row - 1] = TRUE;
5159
5160 /*
5161 * Special trick to make copy/paste of wrapped lines work with
5162 * xterm/screen: write an extra character beyond the end of
5163 * the line. This will work with all terminal types
5164 * (regardless of the xn,am settings).
5165 * Only do this on a fast tty.
5166 * Only do this if the cursor is on the current line
5167 * (something has been written in it).
5168 * Don't do this for the GUI.
5169 * Don't do this for double-width characters.
5170 * Don't do this for a window not at the right screen border.
5171 */
5172 if (p_tf
5173#ifdef FEAT_GUI
5174 && !gui.in_use
5175#endif
5176#ifdef FEAT_MBYTE
5177 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005178 && ((*mb_off2cells)(LineOffset[screen_row],
5179 LineOffset[screen_row] + screen_Columns)
5180 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005182 + (int)Columns - 2,
5183 LineOffset[screen_row] + screen_Columns)
5184 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185#endif
5186 )
5187 {
5188 /* First make sure we are at the end of the screen line,
5189 * then output the same character again to let the
5190 * terminal know about the wrap. If the terminal doesn't
5191 * auto-wrap, we overwrite the character. */
5192 if (screen_cur_col != W_WIDTH(wp))
5193 screen_char(LineOffset[screen_row - 1]
5194 + (unsigned)Columns - 1,
5195 screen_row - 1, (int)(Columns - 1));
5196
5197#ifdef FEAT_MBYTE
5198 /* When there is a multi-byte character, just output a
5199 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005200 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5201 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 out_char(' ');
5203 else
5204#endif
5205 out_char(ScreenLines[LineOffset[screen_row - 1]
5206 + (Columns - 1)]);
5207 /* force a redraw of the first char on the next line */
5208 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5209 screen_start(); /* don't know where cursor is now */
5210 }
5211 }
5212
5213 col = 0;
5214 off = (unsigned)(current_ScreenLine - ScreenLines);
5215#ifdef FEAT_RIGHTLEFT
5216 if (wp->w_p_rl)
5217 {
5218 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5219 off += col;
5220 }
5221#endif
5222
5223 /* reset the drawing state for the start of a wrapped line */
5224 draw_state = WL_START;
5225 saved_n_extra = n_extra;
5226 saved_p_extra = p_extra;
5227 saved_c_extra = c_extra;
5228 saved_char_attr = char_attr;
5229 n_extra = 0;
5230 lcs_prec_todo = lcs_prec;
5231#ifdef FEAT_LINEBREAK
5232# ifdef FEAT_DIFF
5233 if (filler_todo <= 0)
5234# endif
5235 need_showbreak = TRUE;
5236#endif
5237#ifdef FEAT_DIFF
5238 --filler_todo;
5239 /* When the filler lines are actually below the last line of the
5240 * file, don't draw the line itself, break here. */
5241 if (filler_todo == 0 && wp->w_botfill)
5242 break;
5243#endif
5244 }
5245
5246 } /* for every character in the line */
5247
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005248#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005249 /* After an empty line check first word for capital. */
5250 if (*skipwhite(line) == NUL)
5251 {
5252 capcol_lnum = lnum + 1;
5253 cap_col = 0;
5254 }
5255#endif
5256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 return row;
5258}
5259
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005260#ifdef FEAT_MBYTE
5261static int comp_char_differs __ARGS((int, int));
5262
5263/*
5264 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005265 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005266 */
5267 static int
5268comp_char_differs(off_from, off_to)
5269 int off_from;
5270 int off_to;
5271{
5272 int i;
5273
5274 for (i = 0; i < Screen_mco; ++i)
5275 {
5276 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5277 return TRUE;
5278 if (ScreenLinesC[i][off_from] == 0)
5279 break;
5280 }
5281 return FALSE;
5282}
5283#endif
5284
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285/*
5286 * Check whether the given character needs redrawing:
5287 * - the (first byte of the) character is different
5288 * - the attributes are different
5289 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005290 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 */
5292 static int
5293char_needs_redraw(off_from, off_to, cols)
5294 int off_from;
5295 int off_to;
5296 int cols;
5297{
5298 if (cols > 0
5299 && ((ScreenLines[off_from] != ScreenLines[off_to]
5300 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5301
5302#ifdef FEAT_MBYTE
5303 || (enc_dbcs != 0
5304 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5305 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5306 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5307 : (cols > 1 && ScreenLines[off_from + 1]
5308 != ScreenLines[off_to + 1])))
5309 || (enc_utf8
5310 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5311 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005312 && comp_char_differs(off_from, off_to))
5313 || (cols > 1 && ScreenLines[off_from + 1]
5314 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#endif
5316 ))
5317 return TRUE;
5318 return FALSE;
5319}
5320
5321/*
5322 * Move one "cooked" screen line to the screen, but only the characters that
5323 * have actually changed. Handle insert/delete character.
5324 * "coloff" gives the first column on the screen for this line.
5325 * "endcol" gives the columns where valid characters are.
5326 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5327 * needs to be cleared, negative otherwise.
5328 * "rlflag" is TRUE in a rightleft window:
5329 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5330 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5331 */
5332 static void
5333screen_line(row, coloff, endcol, clear_width
5334#ifdef FEAT_RIGHTLEFT
5335 , rlflag
5336#endif
5337 )
5338 int row;
5339 int coloff;
5340 int endcol;
5341 int clear_width;
5342#ifdef FEAT_RIGHTLEFT
5343 int rlflag;
5344#endif
5345{
5346 unsigned off_from;
5347 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005348#ifdef FEAT_MBYTE
5349 unsigned max_off_from;
5350 unsigned max_off_to;
5351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 int col = 0;
5353#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5354 int hl;
5355#endif
5356 int force = FALSE; /* force update rest of the line */
5357 int redraw_this /* bool: does character need redraw? */
5358#ifdef FEAT_GUI
5359 = TRUE /* For GUI when while-loop empty */
5360#endif
5361 ;
5362 int redraw_next; /* redraw_this for next character */
5363#ifdef FEAT_MBYTE
5364 int clear_next = FALSE;
5365 int char_cells; /* 1: normal char */
5366 /* 2: occupies two display cells */
5367# define CHAR_CELLS char_cells
5368#else
5369# define CHAR_CELLS 1
5370#endif
5371
5372# ifdef FEAT_CLIPBOARD
5373 clip_may_clear_selection(row, row);
5374# endif
5375
5376 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5377 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005378#ifdef FEAT_MBYTE
5379 max_off_from = off_from + screen_Columns;
5380 max_off_to = LineOffset[row] + screen_Columns;
5381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383#ifdef FEAT_RIGHTLEFT
5384 if (rlflag)
5385 {
5386 /* Clear rest first, because it's left of the text. */
5387 if (clear_width > 0)
5388 {
5389 while (col <= endcol && ScreenLines[off_to] == ' '
5390 && ScreenAttrs[off_to] == 0
5391# ifdef FEAT_MBYTE
5392 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5393# endif
5394 )
5395 {
5396 ++off_to;
5397 ++col;
5398 }
5399 if (col <= endcol)
5400 screen_fill(row, row + 1, col + coloff,
5401 endcol + coloff + 1, ' ', ' ', 0);
5402 }
5403 col = endcol + 1;
5404 off_to = LineOffset[row] + col + coloff;
5405 off_from += col;
5406 endcol = (clear_width > 0 ? clear_width : -clear_width);
5407 }
5408#endif /* FEAT_RIGHTLEFT */
5409
5410 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5411
5412 while (col < endcol)
5413 {
5414#ifdef FEAT_MBYTE
5415 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005416 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 else
5418 char_cells = 1;
5419#endif
5420
5421 redraw_this = redraw_next;
5422 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5423 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5424
5425#ifdef FEAT_GUI
5426 /* If the next character was bold, then redraw the current character to
5427 * remove any pixels that might have spilt over into us. This only
5428 * happens in the GUI.
5429 */
5430 if (redraw_next && gui.in_use)
5431 {
5432 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005433 if (hl > HL_ALL)
5434 hl = syn_attr2attr(hl);
5435 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 redraw_this = TRUE;
5437 }
5438#endif
5439
5440 if (redraw_this)
5441 {
5442 /*
5443 * Special handling when 'xs' termcap flag set (hpterm):
5444 * Attributes for characters are stored at the position where the
5445 * cursor is when writing the highlighting code. The
5446 * start-highlighting code must be written with the cursor on the
5447 * first highlighted character. The stop-highlighting code must
5448 * be written with the cursor just after the last highlighted
5449 * character.
5450 * Overwriting a character doesn't remove it's highlighting. Need
5451 * to clear the rest of the line, and force redrawing it
5452 * completely.
5453 */
5454 if ( p_wiv
5455 && !force
5456#ifdef FEAT_GUI
5457 && !gui.in_use
5458#endif
5459 && ScreenAttrs[off_to] != 0
5460 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5461 {
5462 /*
5463 * Need to remove highlighting attributes here.
5464 */
5465 windgoto(row, col + coloff);
5466 out_str(T_CE); /* clear rest of this screen line */
5467 screen_start(); /* don't know where cursor is now */
5468 force = TRUE; /* force redraw of rest of the line */
5469 redraw_next = TRUE; /* or else next char would miss out */
5470
5471 /*
5472 * If the previous character was highlighted, need to stop
5473 * highlighting at this character.
5474 */
5475 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5476 {
5477 screen_attr = ScreenAttrs[off_to - 1];
5478 term_windgoto(row, col + coloff);
5479 screen_stop_highlight();
5480 }
5481 else
5482 screen_attr = 0; /* highlighting has stopped */
5483 }
5484#ifdef FEAT_MBYTE
5485 if (enc_dbcs != 0)
5486 {
5487 /* Check if overwriting a double-byte with a single-byte or
5488 * the other way around requires another character to be
5489 * redrawn. For UTF-8 this isn't needed, because comparing
5490 * ScreenLinesUC[] is sufficient. */
5491 if (char_cells == 1
5492 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005493 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
5495 /* Writing a single-cell character over a double-cell
5496 * character: need to redraw the next cell. */
5497 ScreenLines[off_to + 1] = 0;
5498 redraw_next = TRUE;
5499 }
5500 else if (char_cells == 2
5501 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005502 && (*mb_off2cells)(off_to, max_off_to) == 1
5503 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
5505 /* Writing the second half of a double-cell character over
5506 * a double-cell character: need to redraw the second
5507 * cell. */
5508 ScreenLines[off_to + 2] = 0;
5509 redraw_next = TRUE;
5510 }
5511
5512 if (enc_dbcs == DBCS_JPNU)
5513 ScreenLines2[off_to] = ScreenLines2[off_from];
5514 }
5515 /* When writing a single-width character over a double-width
5516 * character and at the end of the redrawn text, need to clear out
5517 * the right halve of the old character.
5518 * Also required when writing the right halve of a double-width
5519 * char over the left halve of an existing one. */
5520 if (has_mbyte && col + char_cells == endcol
5521 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005522 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005524 && (*mb_off2cells)(off_to, max_off_to) == 1
5525 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 clear_next = TRUE;
5527#endif
5528
5529 ScreenLines[off_to] = ScreenLines[off_from];
5530#ifdef FEAT_MBYTE
5531 if (enc_utf8)
5532 {
5533 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5534 if (ScreenLinesUC[off_from] != 0)
5535 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005536 int i;
5537
5538 for (i = 0; i < Screen_mco; ++i)
5539 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541 }
5542 if (char_cells == 2)
5543 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5544#endif
5545
5546#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005547 /* The bold trick makes a single column of pixels appear in the
5548 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 * character should be redrawn too. This happens for our own GUI
5550 * and for some xterms. */
5551 if (
5552# ifdef FEAT_GUI
5553 gui.in_use
5554# endif
5555# if defined(FEAT_GUI) && defined(UNIX)
5556 ||
5557# endif
5558# ifdef UNIX
5559 term_is_xterm
5560# endif
5561 )
5562 {
5563 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005564 if (hl > HL_ALL)
5565 hl = syn_attr2attr(hl);
5566 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 redraw_next = TRUE;
5568 }
5569#endif
5570 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5571#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005572 /* For simplicity set the attributes of second half of a
5573 * double-wide character equal to the first half. */
5574 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005576
5577 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 else
5580#endif
5581 screen_char(off_to, row, col + coloff);
5582 }
5583 else if ( p_wiv
5584#ifdef FEAT_GUI
5585 && !gui.in_use
5586#endif
5587 && col + coloff > 0)
5588 {
5589 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5590 {
5591 /*
5592 * Don't output stop-highlight when moving the cursor, it will
5593 * stop the highlighting when it should continue.
5594 */
5595 screen_attr = 0;
5596 }
5597 else if (screen_attr != 0)
5598 screen_stop_highlight();
5599 }
5600
5601 off_to += CHAR_CELLS;
5602 off_from += CHAR_CELLS;
5603 col += CHAR_CELLS;
5604 }
5605
5606#ifdef FEAT_MBYTE
5607 if (clear_next)
5608 {
5609 /* Clear the second half of a double-wide character of which the left
5610 * half was overwritten with a single-wide character. */
5611 ScreenLines[off_to] = ' ';
5612 if (enc_utf8)
5613 ScreenLinesUC[off_to] = 0;
5614 screen_char(off_to, row, col + coloff);
5615 }
5616#endif
5617
5618 if (clear_width > 0
5619#ifdef FEAT_RIGHTLEFT
5620 && !rlflag
5621#endif
5622 )
5623 {
5624#ifdef FEAT_GUI
5625 int startCol = col;
5626#endif
5627
5628 /* blank out the rest of the line */
5629 while (col < clear_width && ScreenLines[off_to] == ' '
5630 && ScreenAttrs[off_to] == 0
5631#ifdef FEAT_MBYTE
5632 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5633#endif
5634 )
5635 {
5636 ++off_to;
5637 ++col;
5638 }
5639 if (col < clear_width)
5640 {
5641#ifdef FEAT_GUI
5642 /*
5643 * In the GUI, clearing the rest of the line may leave pixels
5644 * behind if the first character cleared was bold. Some bold
5645 * fonts spill over the left. In this case we redraw the previous
5646 * character too. If we didn't skip any blanks above, then we
5647 * only redraw if the character wasn't already redrawn anyway.
5648 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005649 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 {
5651 hl = ScreenAttrs[off_to];
5652 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005653 {
5654 int prev_cells = 1;
5655# ifdef FEAT_MBYTE
5656 if (enc_utf8)
5657 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5658 * that its width is 2. */
5659 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5660 else if (enc_dbcs != 0)
5661 {
5662 /* find previous character by counting from first
5663 * column and get its width. */
5664 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005665 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005666
5667 while (off < off_to)
5668 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005669 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005670 off += prev_cells;
5671 }
5672 }
5673
5674 if (enc_dbcs != 0 && prev_cells > 1)
5675 screen_char_2(off_to - prev_cells, row,
5676 col + coloff - prev_cells);
5677 else
5678# endif
5679 screen_char(off_to - prev_cells, row,
5680 col + coloff - prev_cells);
5681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683#endif
5684 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5685 ' ', ' ', 0);
5686#ifdef FEAT_VERTSPLIT
5687 off_to += clear_width - col;
5688 col = clear_width;
5689#endif
5690 }
5691 }
5692
5693 if (clear_width > 0)
5694 {
5695#ifdef FEAT_VERTSPLIT
5696 /* For a window that's left of another, draw the separator char. */
5697 if (col + coloff < Columns)
5698 {
5699 int c;
5700
5701 c = fillchar_vsep(&hl);
5702 if (ScreenLines[off_to] != c
5703# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005704 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5705 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706# endif
5707 || ScreenAttrs[off_to] != hl)
5708 {
5709 ScreenLines[off_to] = c;
5710 ScreenAttrs[off_to] = hl;
5711# ifdef FEAT_MBYTE
5712 if (enc_utf8)
5713 {
5714 if (c >= 0x80)
5715 {
5716 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005717 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
5719 else
5720 ScreenLinesUC[off_to] = 0;
5721 }
5722# endif
5723 screen_char(off_to, row, col + coloff);
5724 }
5725 }
5726 else
5727#endif
5728 LineWraps[row] = FALSE;
5729 }
5730}
5731
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005732#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005734 * Mirror text "str" for right-left displaying.
5735 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005737 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738rl_mirror(str)
5739 char_u *str;
5740{
5741 char_u *p1, *p2;
5742 int t;
5743
5744 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5745 {
5746 t = *p1;
5747 *p1 = *p2;
5748 *p2 = t;
5749 }
5750}
5751#endif
5752
5753#if defined(FEAT_WINDOWS) || defined(PROTO)
5754/*
5755 * mark all status lines for redraw; used after first :cd
5756 */
5757 void
5758status_redraw_all()
5759{
5760 win_T *wp;
5761
5762 for (wp = firstwin; wp; wp = wp->w_next)
5763 if (wp->w_status_height)
5764 {
5765 wp->w_redr_status = TRUE;
5766 redraw_later(VALID);
5767 }
5768}
5769
5770/*
5771 * mark all status lines of the current buffer for redraw
5772 */
5773 void
5774status_redraw_curbuf()
5775{
5776 win_T *wp;
5777
5778 for (wp = firstwin; wp; wp = wp->w_next)
5779 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5780 {
5781 wp->w_redr_status = TRUE;
5782 redraw_later(VALID);
5783 }
5784}
5785
5786/*
5787 * Redraw all status lines that need to be redrawn.
5788 */
5789 void
5790redraw_statuslines()
5791{
5792 win_T *wp;
5793
5794 for (wp = firstwin; wp; wp = wp->w_next)
5795 if (wp->w_redr_status)
5796 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005797 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005798 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799}
5800#endif
5801
5802#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5803/*
5804 * Redraw all status lines at the bottom of frame "frp".
5805 */
5806 void
5807win_redraw_last_status(frp)
5808 frame_T *frp;
5809{
5810 if (frp->fr_layout == FR_LEAF)
5811 frp->fr_win->w_redr_status = TRUE;
5812 else if (frp->fr_layout == FR_ROW)
5813 {
5814 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5815 win_redraw_last_status(frp);
5816 }
5817 else /* frp->fr_layout == FR_COL */
5818 {
5819 frp = frp->fr_child;
5820 while (frp->fr_next != NULL)
5821 frp = frp->fr_next;
5822 win_redraw_last_status(frp);
5823 }
5824}
5825#endif
5826
5827#ifdef FEAT_VERTSPLIT
5828/*
5829 * Draw the verticap separator right of window "wp" starting with line "row".
5830 */
5831 static void
5832draw_vsep_win(wp, row)
5833 win_T *wp;
5834 int row;
5835{
5836 int hl;
5837 int c;
5838
5839 if (wp->w_vsep_width)
5840 {
5841 /* draw the vertical separator right of this window */
5842 c = fillchar_vsep(&hl);
5843 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5844 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5845 c, ' ', hl);
5846 }
5847}
5848#endif
5849
5850#ifdef FEAT_WILDMENU
5851static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005852static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
5854/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005855 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 */
5857 static int
5858status_match_len(xp, s)
5859 expand_T *xp;
5860 char_u *s;
5861{
5862 int len = 0;
5863
5864#ifdef FEAT_MENU
5865 int emenu = (xp->xp_context == EXPAND_MENUS
5866 || xp->xp_context == EXPAND_MENUNAMES);
5867
5868 /* Check for menu separators - replace with '|'. */
5869 if (emenu && menu_is_separator(s))
5870 return 1;
5871#endif
5872
5873 while (*s != NUL)
5874 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005875 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005876 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005877 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 }
5879
5880 return len;
5881}
5882
5883/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005884 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005885 * These are backslashes used for escaping. Do show backslashes in help tags.
5886 */
5887 static int
5888skip_status_match_char(xp, s)
5889 expand_T *xp;
5890 char_u *s;
5891{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005892 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005893#ifdef FEAT_MENU
5894 || ((xp->xp_context == EXPAND_MENUS
5895 || xp->xp_context == EXPAND_MENUNAMES)
5896 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5897#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005898 )
5899 {
5900#ifndef BACKSLASH_IN_FILENAME
5901 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5902 return 2;
5903#endif
5904 return 1;
5905 }
5906 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005907}
5908
5909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 * Show wildchar matches in the status line.
5911 * Show at least the "match" item.
5912 * We start at item 'first_match' in the list and show all matches that fit.
5913 *
5914 * If inversion is possible we use it. Else '=' characters are used.
5915 */
5916 void
5917win_redr_status_matches(xp, num_matches, matches, match, showtail)
5918 expand_T *xp;
5919 int num_matches;
5920 char_u **matches; /* list of matches */
5921 int match;
5922 int showtail;
5923{
5924#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5925 int row;
5926 char_u *buf;
5927 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005928 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 int fillchar;
5930 int attr;
5931 int i;
5932 int highlight = TRUE;
5933 char_u *selstart = NULL;
5934 int selstart_col = 0;
5935 char_u *selend = NULL;
5936 static int first_match = 0;
5937 int add_left = FALSE;
5938 char_u *s;
5939#ifdef FEAT_MENU
5940 int emenu;
5941#endif
5942#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5943 int l;
5944#endif
5945
5946 if (matches == NULL) /* interrupted completion? */
5947 return;
5948
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005949#ifdef FEAT_MBYTE
5950 if (has_mbyte)
5951 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5952 else
5953#endif
5954 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if (buf == NULL)
5956 return;
5957
5958 if (match == -1) /* don't show match but original text */
5959 {
5960 match = 0;
5961 highlight = FALSE;
5962 }
5963 /* count 1 for the ending ">" */
5964 clen = status_match_len(xp, L_MATCH(match)) + 3;
5965 if (match == 0)
5966 first_match = 0;
5967 else if (match < first_match)
5968 {
5969 /* jumping left, as far as we can go */
5970 first_match = match;
5971 add_left = TRUE;
5972 }
5973 else
5974 {
5975 /* check if match fits on the screen */
5976 for (i = first_match; i < match; ++i)
5977 clen += status_match_len(xp, L_MATCH(i)) + 2;
5978 if (first_match > 0)
5979 clen += 2;
5980 /* jumping right, put match at the left */
5981 if ((long)clen > Columns)
5982 {
5983 first_match = match;
5984 /* if showing the last match, we can add some on the left */
5985 clen = 2;
5986 for (i = match; i < num_matches; ++i)
5987 {
5988 clen += status_match_len(xp, L_MATCH(i)) + 2;
5989 if ((long)clen >= Columns)
5990 break;
5991 }
5992 if (i == num_matches)
5993 add_left = TRUE;
5994 }
5995 }
5996 if (add_left)
5997 while (first_match > 0)
5998 {
5999 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6000 if ((long)clen >= Columns)
6001 break;
6002 --first_match;
6003 }
6004
6005 fillchar = fillchar_status(&attr, TRUE);
6006
6007 if (first_match == 0)
6008 {
6009 *buf = NUL;
6010 len = 0;
6011 }
6012 else
6013 {
6014 STRCPY(buf, "< ");
6015 len = 2;
6016 }
6017 clen = len;
6018
6019 i = first_match;
6020 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6021 {
6022 if (i == match)
6023 {
6024 selstart = buf + len;
6025 selstart_col = clen;
6026 }
6027
6028 s = L_MATCH(i);
6029 /* Check for menu separators - replace with '|' */
6030#ifdef FEAT_MENU
6031 emenu = (xp->xp_context == EXPAND_MENUS
6032 || xp->xp_context == EXPAND_MENUNAMES);
6033 if (emenu && menu_is_separator(s))
6034 {
6035 STRCPY(buf + len, transchar('|'));
6036 l = (int)STRLEN(buf + len);
6037 len += l;
6038 clen += l;
6039 }
6040 else
6041#endif
6042 for ( ; *s != NUL; ++s)
6043 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006044 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 clen += ptr2cells(s);
6046#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006047 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {
6049 STRNCPY(buf + len, s, l);
6050 s += l - 1;
6051 len += l;
6052 }
6053 else
6054#endif
6055 {
6056 STRCPY(buf + len, transchar_byte(*s));
6057 len += (int)STRLEN(buf + len);
6058 }
6059 }
6060 if (i == match)
6061 selend = buf + len;
6062
6063 *(buf + len++) = ' ';
6064 *(buf + len++) = ' ';
6065 clen += 2;
6066 if (++i == num_matches)
6067 break;
6068 }
6069
6070 if (i != num_matches)
6071 {
6072 *(buf + len++) = '>';
6073 ++clen;
6074 }
6075
6076 buf[len] = NUL;
6077
6078 row = cmdline_row - 1;
6079 if (row >= 0)
6080 {
6081 if (wild_menu_showing == 0)
6082 {
6083 if (msg_scrolled > 0)
6084 {
6085 /* Put the wildmenu just above the command line. If there is
6086 * no room, scroll the screen one line up. */
6087 if (cmdline_row == Rows - 1)
6088 {
6089 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6090 ++msg_scrolled;
6091 }
6092 else
6093 {
6094 ++cmdline_row;
6095 ++row;
6096 }
6097 wild_menu_showing = WM_SCROLLED;
6098 }
6099 else
6100 {
6101 /* Create status line if needed by setting 'laststatus' to 2.
6102 * Set 'winminheight' to zero to avoid that the window is
6103 * resized. */
6104 if (lastwin->w_status_height == 0)
6105 {
6106 save_p_ls = p_ls;
6107 save_p_wmh = p_wmh;
6108 p_ls = 2;
6109 p_wmh = 0;
6110 last_status(FALSE);
6111 }
6112 wild_menu_showing = WM_SHOWN;
6113 }
6114 }
6115
6116 screen_puts(buf, row, 0, attr);
6117 if (selstart != NULL && highlight)
6118 {
6119 *selend = NUL;
6120 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6121 }
6122
6123 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6124 }
6125
6126#ifdef FEAT_VERTSPLIT
6127 win_redraw_last_status(topframe);
6128#else
6129 lastwin->w_redr_status = TRUE;
6130#endif
6131 vim_free(buf);
6132}
6133#endif
6134
6135#if defined(FEAT_WINDOWS) || defined(PROTO)
6136/*
6137 * Redraw the status line of window wp.
6138 *
6139 * If inversion is possible we use it. Else '=' characters are used.
6140 */
6141 void
6142win_redr_status(wp)
6143 win_T *wp;
6144{
6145 int row;
6146 char_u *p;
6147 int len;
6148 int fillchar;
6149 int attr;
6150 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006151 static int busy = FALSE;
6152
6153 /* It's possible to get here recursively when 'statusline' (indirectly)
6154 * invokes ":redrawstatus". Simply ignore the call then. */
6155 if (busy)
6156 return;
6157 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159 wp->w_redr_status = FALSE;
6160 if (wp->w_status_height == 0)
6161 {
6162 /* no status line, can only be last window */
6163 redraw_cmdline = TRUE;
6164 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006165 else if (!redrawing()
6166#ifdef FEAT_INS_EXPAND
6167 /* don't update status line when popup menu is visible and may be
6168 * drawn over it */
6169 || pum_visible()
6170#endif
6171 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 {
6173 /* Don't redraw right now, do it later. */
6174 wp->w_redr_status = TRUE;
6175 }
6176#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006177 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
6179 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006180 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
6182#endif
6183 else
6184 {
6185 fillchar = fillchar_status(&attr, wp == curwin);
6186
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006187 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 p = NameBuff;
6189 len = (int)STRLEN(p);
6190
6191 if (wp->w_buffer->b_help
6192#ifdef FEAT_QUICKFIX
6193 || wp->w_p_pvw
6194#endif
6195 || bufIsChanged(wp->w_buffer)
6196 || wp->w_buffer->b_p_ro)
6197 *(p + len++) = ' ';
6198 if (wp->w_buffer->b_help)
6199 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006200 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 len += (int)STRLEN(p + len);
6202 }
6203#ifdef FEAT_QUICKFIX
6204 if (wp->w_p_pvw)
6205 {
6206 STRCPY(p + len, _("[Preview]"));
6207 len += (int)STRLEN(p + len);
6208 }
6209#endif
6210 if (bufIsChanged(wp->w_buffer))
6211 {
6212 STRCPY(p + len, "[+]");
6213 len += 3;
6214 }
6215 if (wp->w_buffer->b_p_ro)
6216 {
6217 STRCPY(p + len, "[RO]");
6218 len += 4;
6219 }
6220
6221#ifndef FEAT_VERTSPLIT
6222 this_ru_col = ru_col;
6223 if (this_ru_col < (Columns + 1) / 2)
6224 this_ru_col = (Columns + 1) / 2;
6225#else
6226 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6227 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6228 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6229 if (this_ru_col <= 1)
6230 {
6231 p = (char_u *)"<"; /* No room for file name! */
6232 len = 1;
6233 }
6234 else
6235#endif
6236#ifdef FEAT_MBYTE
6237 if (has_mbyte)
6238 {
6239 int clen = 0, i;
6240
6241 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006242 clen = mb_string2cells(p, -1);
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 /* Find first character that will fit.
6245 * Going from start to end is much faster for DBCS. */
6246 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006247 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 clen -= (*mb_ptr2cells)(p + i);
6249 len = clen;
6250 if (i > 0)
6251 {
6252 p = p + i - 1;
6253 *p = '<';
6254 ++len;
6255 }
6256
6257 }
6258 else
6259#endif
6260 if (len > this_ru_col - 1)
6261 {
6262 p += len - (this_ru_col - 1);
6263 *p = '<';
6264 len = this_ru_col - 1;
6265 }
6266
6267 row = W_WINROW(wp) + wp->w_height;
6268 screen_puts(p, row, W_WINCOL(wp), attr);
6269 screen_fill(row, row + 1, len + W_WINCOL(wp),
6270 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6271
6272 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6273 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6274 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6275 - 1 + W_WINCOL(wp)), attr);
6276
6277#ifdef FEAT_CMDL_INFO
6278 win_redr_ruler(wp, TRUE);
6279#endif
6280 }
6281
6282#ifdef FEAT_VERTSPLIT
6283 /*
6284 * May need to draw the character below the vertical separator.
6285 */
6286 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6287 {
6288 if (stl_connected(wp))
6289 fillchar = fillchar_status(&attr, wp == curwin);
6290 else
6291 fillchar = fillchar_vsep(&attr);
6292 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6293 attr);
6294 }
6295#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006296 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297}
6298
Bram Moolenaar238a5642006-02-21 22:12:05 +00006299#ifdef FEAT_STL_OPT
6300/*
6301 * Redraw the status line according to 'statusline' and take care of any
6302 * errors encountered.
6303 */
6304 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006305redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006306 win_T *wp;
6307{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006308 static int entered = FALSE;
6309 int save_called_emsg = called_emsg;
6310
6311 /* When called recursively return. This can happen when the statusline
6312 * contains an expression that triggers a redraw. */
6313 if (entered)
6314 return;
6315 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006316
6317 called_emsg = FALSE;
6318 win_redr_custom(wp, FALSE);
6319 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006320 {
6321 /* When there is an error disable the statusline, otherwise the
6322 * display is messed up with errors and a redraw triggers the problem
6323 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006324 set_string_option_direct((char_u *)"statusline", -1,
6325 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006326 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006327 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006328 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006329 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006330}
6331#endif
6332
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333# ifdef FEAT_VERTSPLIT
6334/*
6335 * Return TRUE if the status line of window "wp" is connected to the status
6336 * line of the window right of it. If not, then it's a vertical separator.
6337 * Only call if (wp->w_vsep_width != 0).
6338 */
6339 int
6340stl_connected(wp)
6341 win_T *wp;
6342{
6343 frame_T *fr;
6344
6345 fr = wp->w_frame;
6346 while (fr->fr_parent != NULL)
6347 {
6348 if (fr->fr_parent->fr_layout == FR_COL)
6349 {
6350 if (fr->fr_next != NULL)
6351 break;
6352 }
6353 else
6354 {
6355 if (fr->fr_next != NULL)
6356 return TRUE;
6357 }
6358 fr = fr->fr_parent;
6359 }
6360 return FALSE;
6361}
6362# endif
6363
6364#endif /* FEAT_WINDOWS */
6365
6366#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6367/*
6368 * Get the value to show for the language mappings, active 'keymap'.
6369 */
6370 int
6371get_keymap_str(wp, buf, len)
6372 win_T *wp;
6373 char_u *buf; /* buffer for the result */
6374 int len; /* length of buffer */
6375{
6376 char_u *p;
6377
6378 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6379 return FALSE;
6380
6381 {
6382#ifdef FEAT_EVAL
6383 buf_T *old_curbuf = curbuf;
6384 win_T *old_curwin = curwin;
6385 char_u *s;
6386
6387 curbuf = wp->w_buffer;
6388 curwin = wp;
6389 STRCPY(buf, "b:keymap_name"); /* must be writable */
6390 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006391 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 --emsg_skip;
6393 curbuf = old_curbuf;
6394 curwin = old_curwin;
6395 if (p == NULL || *p == NUL)
6396#endif
6397 {
6398#ifdef FEAT_KEYMAP
6399 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6400 p = wp->w_buffer->b_p_keymap;
6401 else
6402#endif
6403 p = (char_u *)"lang";
6404 }
6405 if ((int)(STRLEN(p) + 3) < len)
6406 sprintf((char *)buf, "<%s>", p);
6407 else
6408 buf[0] = NUL;
6409#ifdef FEAT_EVAL
6410 vim_free(s);
6411#endif
6412 }
6413 return buf[0] != NUL;
6414}
6415#endif
6416
6417#if defined(FEAT_STL_OPT) || defined(PROTO)
6418/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006419 * Redraw the status line or ruler of window "wp".
6420 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 */
6422 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006423win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006425 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
6427 int attr;
6428 int curattr;
6429 int row;
6430 int col = 0;
6431 int maxwidth;
6432 int width;
6433 int n;
6434 int len;
6435 int fillchar;
6436 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006437 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006439 struct stl_hlrec hltab[STL_MAX_ITEM];
6440 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006441 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006442 win_T *ewp;
6443 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
6445 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006446 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006448 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006449 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006450 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006451 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006452 attr = hl_attr(HLF_TPF);
6453 maxwidth = Columns;
6454# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006455 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006458 else
6459 {
6460 row = W_WINROW(wp) + wp->w_height;
6461 fillchar = fillchar_status(&attr, wp == curwin);
6462 maxwidth = W_WIDTH(wp);
6463
6464 if (draw_ruler)
6465 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006466 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006467 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006468 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006469 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006470 if (*++stl == '-')
6471 stl++;
6472 if (atoi((char *)stl))
6473 while (VIM_ISDIGIT(*stl))
6474 stl++;
6475 if (*stl++ != '(')
6476 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006477 }
6478#ifdef FEAT_VERTSPLIT
6479 col = ru_col - (Columns - W_WIDTH(wp));
6480 if (col < (W_WIDTH(wp) + 1) / 2)
6481 col = (W_WIDTH(wp) + 1) / 2;
6482#else
6483 col = ru_col;
6484 if (col > (Columns + 1) / 2)
6485 col = (Columns + 1) / 2;
6486#endif
6487 maxwidth = W_WIDTH(wp) - col;
6488#ifdef FEAT_WINDOWS
6489 if (!wp->w_status_height)
6490#endif
6491 {
6492 row = Rows - 1;
6493 --maxwidth; /* writing in last column may cause scrolling */
6494 fillchar = ' ';
6495 attr = 0;
6496 }
6497
6498# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006499 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006500# endif
6501 }
6502 else
6503 {
6504 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006505 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006506 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006507 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006508# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006509 use_sandbox = was_set_insecurely((char_u *)"statusline",
6510 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006511# endif
6512 }
6513
6514#ifdef FEAT_VERTSPLIT
6515 col += W_WINCOL(wp);
6516#endif
6517 }
6518
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 if (maxwidth <= 0)
6520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaar61452852011-02-01 18:01:11 +01006522 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6523 * the cursor away and back. */
6524 ewp = wp == NULL ? curwin : wp;
6525 p_crb_save = ewp->w_p_crb;
6526 ewp->w_p_crb = FALSE;
6527
Bram Moolenaar362f3562009-11-03 16:20:34 +00006528 /* Make a copy, because the statusline may include a function call that
6529 * might change the option value and free the memory. */
6530 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006531 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006532 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006533 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006534 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006535 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006537 /* Make all characters printable. */
6538 p = transstr(buf);
6539 if (p != NULL)
6540 {
6541 vim_strncpy(buf, p, sizeof(buf) - 1);
6542 vim_free(p);
6543 }
6544
6545 /* fill up with "fillchar" */
6546 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006547 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
6549#ifdef FEAT_MBYTE
6550 len += (*mb_char2bytes)(fillchar, buf + len);
6551#else
6552 buf[len++] = fillchar;
6553#endif
6554 ++width;
6555 }
6556 buf[len] = NUL;
6557
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006558 /*
6559 * Draw each snippet with the specified highlighting.
6560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 curattr = attr;
6562 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006563 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006565 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 screen_puts_len(p, len, row, col, curattr);
6567 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006568 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006570 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006572 else if (hltab[n].userhl < 0)
6573 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006575 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006576 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577#endif
6578 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006579 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 }
6581 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006582
6583 if (wp == NULL)
6584 {
6585 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6586 col = 0;
6587 len = 0;
6588 p = buf;
6589 fillchar = 0;
6590 for (n = 0; tabtab[n].start != NULL; n++)
6591 {
6592 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6593 while (col < len)
6594 TabPageIdxs[col++] = fillchar;
6595 p = tabtab[n].start;
6596 fillchar = tabtab[n].userhl;
6597 }
6598 while (col < Columns)
6599 TabPageIdxs[col++] = fillchar;
6600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601}
6602
6603#endif /* FEAT_STL_OPT */
6604
6605/*
6606 * Output a single character directly to the screen and update ScreenLines.
6607 */
6608 void
6609screen_putchar(c, row, col, attr)
6610 int c;
6611 int row, col;
6612 int attr;
6613{
6614#ifdef FEAT_MBYTE
6615 char_u buf[MB_MAXBYTES + 1];
6616
6617 buf[(*mb_char2bytes)(c, buf)] = NUL;
6618#else
6619 char_u buf[2];
6620
6621 buf[0] = c;
6622 buf[1] = NUL;
6623#endif
6624 screen_puts(buf, row, col, attr);
6625}
6626
6627/*
6628 * Get a single character directly from ScreenLines into "bytes[]".
6629 * Also return its attribute in *attrp;
6630 */
6631 void
6632screen_getbytes(row, col, bytes, attrp)
6633 int row, col;
6634 char_u *bytes;
6635 int *attrp;
6636{
6637 unsigned off;
6638
6639 /* safety check */
6640 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6641 {
6642 off = LineOffset[row] + col;
6643 *attrp = ScreenAttrs[off];
6644 bytes[0] = ScreenLines[off];
6645 bytes[1] = NUL;
6646
6647#ifdef FEAT_MBYTE
6648 if (enc_utf8 && ScreenLinesUC[off] != 0)
6649 bytes[utfc_char2bytes(off, bytes)] = NUL;
6650 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6651 {
6652 bytes[0] = ScreenLines[off];
6653 bytes[1] = ScreenLines2[off];
6654 bytes[2] = NUL;
6655 }
6656 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6657 {
6658 bytes[1] = ScreenLines[off + 1];
6659 bytes[2] = NUL;
6660 }
6661#endif
6662 }
6663}
6664
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006665#ifdef FEAT_MBYTE
6666static int screen_comp_differs __ARGS((int, int*));
6667
6668/*
6669 * Return TRUE if composing characters for screen posn "off" differs from
6670 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006671 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006672 */
6673 static int
6674screen_comp_differs(off, u8cc)
6675 int off;
6676 int *u8cc;
6677{
6678 int i;
6679
6680 for (i = 0; i < Screen_mco; ++i)
6681 {
6682 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6683 return TRUE;
6684 if (u8cc[i] == 0)
6685 break;
6686 }
6687 return FALSE;
6688}
6689#endif
6690
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691/*
6692 * Put string '*text' on the screen at position 'row' and 'col', with
6693 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6694 * Note: only outputs within one row, message is truncated at screen boundary!
6695 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6696 */
6697 void
6698screen_puts(text, row, col, attr)
6699 char_u *text;
6700 int row;
6701 int col;
6702 int attr;
6703{
6704 screen_puts_len(text, -1, row, col, attr);
6705}
6706
6707/*
6708 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6709 * a NUL.
6710 */
6711 void
6712screen_puts_len(text, len, row, col, attr)
6713 char_u *text;
6714 int len;
6715 int row;
6716 int col;
6717 int attr;
6718{
6719 unsigned off;
6720 char_u *ptr = text;
6721 int c;
6722#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006723 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 int mbyte_blen = 1;
6725 int mbyte_cells = 1;
6726 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006727 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 int clear_next_cell = FALSE;
6729# ifdef FEAT_ARABIC
6730 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006731 int pc, nc, nc1;
6732 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733# endif
6734#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006735#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6736 int force_redraw_this;
6737 int force_redraw_next = FALSE;
6738#endif
6739 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740
6741 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6742 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006743 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744
Bram Moolenaarc236c162008-07-13 17:41:49 +00006745#ifdef FEAT_MBYTE
6746 /* When drawing over the right halve of a double-wide char clear out the
6747 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006748 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006749# ifdef FEAT_GUI
6750 && !gui.in_use
6751# endif
6752 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006753 {
6754 ScreenLines[off - 1] = ' ';
6755 ScreenAttrs[off - 1] = 0;
6756 if (enc_utf8)
6757 {
6758 ScreenLinesUC[off - 1] = 0;
6759 ScreenLinesC[0][off - 1] = 0;
6760 }
6761 /* redraw the previous cell, make it empty */
6762 screen_char(off - 1, row, col - 1);
6763 /* force the cell at "col" to be redrawn */
6764 force_redraw_next = TRUE;
6765 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006766#endif
6767
Bram Moolenaar367329b2007-08-30 11:53:22 +00006768#ifdef FEAT_MBYTE
6769 max_off = LineOffset[row] + screen_Columns;
6770#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006771 while (col < screen_Columns
6772 && (len < 0 || (int)(ptr - text) < len)
6773 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
6775 c = *ptr;
6776#ifdef FEAT_MBYTE
6777 /* check if this is the first byte of a multibyte */
6778 if (has_mbyte)
6779 {
6780 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006781 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006783 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6785 mbyte_cells = 1;
6786 else if (enc_dbcs != 0)
6787 mbyte_cells = mbyte_blen;
6788 else /* enc_utf8 */
6789 {
6790 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006791 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 (int)((text + len) - ptr));
6793 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006794 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006796# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /* Non-BMP character: display as ? or fullwidth ?. */
6798 if (u8c >= 0x10000)
6799 {
6800 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6801 if (attr == 0)
6802 attr = hl_attr(HLF_8);
6803 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805# ifdef FEAT_ARABIC
6806 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6807 {
6808 /* Do Arabic shaping. */
6809 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6810 {
6811 /* Past end of string to be displayed. */
6812 nc = NUL;
6813 nc1 = NUL;
6814 }
6815 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006816 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006817 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6818 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006819 nc1 = pcc[0];
6820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 pc = prev_c;
6822 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006823 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 }
6825 else
6826 prev_c = u8c;
6827# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01006828 if (col + mbyte_cells > screen_Columns)
6829 {
6830 /* Only 1 cell left, but character requires 2 cells:
6831 * display a '>' in the last column to avoid wrapping. */
6832 c = '>';
6833 mbyte_cells = 1;
6834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
6836 }
6837#endif
6838
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006839#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6840 force_redraw_this = force_redraw_next;
6841 force_redraw_next = FALSE;
6842#endif
6843
6844 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845#ifdef FEAT_MBYTE
6846 || (mbyte_cells == 2
6847 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6848 || (enc_dbcs == DBCS_JPNU
6849 && c == 0x8e
6850 && ScreenLines2[off] != ptr[1])
6851 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006852 && (ScreenLinesUC[off] !=
6853 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
6854 || (ScreenLinesUC[off] != 0
6855 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856#endif
6857 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006858 || exmode_active;
6859
6860 if (need_redraw
6861#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6862 || force_redraw_this
6863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 )
6865 {
6866#if defined(FEAT_GUI) || defined(UNIX)
6867 /* The bold trick makes a single row of pixels appear in the next
6868 * character. When a bold character is removed, the next
6869 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006870 * and for some xterms. */
6871 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872# ifdef FEAT_GUI
6873 gui.in_use
6874# endif
6875# if defined(FEAT_GUI) && defined(UNIX)
6876 ||
6877# endif
6878# ifdef UNIX
6879 term_is_xterm
6880# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006881 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006883 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006885 if (n > HL_ALL)
6886 n = syn_attr2attr(n);
6887 if (n & HL_BOLD)
6888 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890#endif
6891#ifdef FEAT_MBYTE
6892 /* When at the end of the text and overwriting a two-cell
6893 * character with a one-cell character, need to clear the next
6894 * cell. Also when overwriting the left halve of a two-cell char
6895 * with the right halve of a two-cell char. Do this only once
6896 * (mb_off2cells() may return 2 on the right halve). */
6897 if (clear_next_cell)
6898 clear_next_cell = FALSE;
6899 else if (has_mbyte
6900 && (len < 0 ? ptr[mbyte_blen] == NUL
6901 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006902 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006904 && (*mb_off2cells)(off, max_off) == 1
6905 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 clear_next_cell = TRUE;
6907
6908 /* Make sure we never leave a second byte of a double-byte behind,
6909 * it confuses mb_off2cells(). */
6910 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006911 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006913 && (*mb_off2cells)(off, max_off) == 1
6914 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 ScreenLines[off + mbyte_blen] = 0;
6916#endif
6917 ScreenLines[off] = c;
6918 ScreenAttrs[off] = attr;
6919#ifdef FEAT_MBYTE
6920 if (enc_utf8)
6921 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006922 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 ScreenLinesUC[off] = 0;
6924 else
6925 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006926 int i;
6927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006929 for (i = 0; i < Screen_mco; ++i)
6930 {
6931 ScreenLinesC[i][off] = u8cc[i];
6932 if (u8cc[i] == 0)
6933 break;
6934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
6936 if (mbyte_cells == 2)
6937 {
6938 ScreenLines[off + 1] = 0;
6939 ScreenAttrs[off + 1] = attr;
6940 }
6941 screen_char(off, row, col);
6942 }
6943 else if (mbyte_cells == 2)
6944 {
6945 ScreenLines[off + 1] = ptr[1];
6946 ScreenAttrs[off + 1] = attr;
6947 screen_char_2(off, row, col);
6948 }
6949 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6950 {
6951 ScreenLines2[off] = ptr[1];
6952 screen_char(off, row, col);
6953 }
6954 else
6955#endif
6956 screen_char(off, row, col);
6957 }
6958#ifdef FEAT_MBYTE
6959 if (has_mbyte)
6960 {
6961 off += mbyte_cells;
6962 col += mbyte_cells;
6963 ptr += mbyte_blen;
6964 if (clear_next_cell)
6965 ptr = (char_u *)" ";
6966 }
6967 else
6968#endif
6969 {
6970 ++off;
6971 ++col;
6972 ++ptr;
6973 }
6974 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006975
6976#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6977 /* If we detected the next character needs to be redrawn, but the text
6978 * doesn't extend up to there, update the character here. */
6979 if (force_redraw_next && col < screen_Columns)
6980 {
6981# ifdef FEAT_MBYTE
6982 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6983 screen_char_2(off, row, col);
6984 else
6985# endif
6986 screen_char(off, row, col);
6987 }
6988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989}
6990
6991#ifdef FEAT_SEARCH_EXTRA
6992/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006993 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 */
6995 static void
6996start_search_hl()
6997{
6998 if (p_hls && !no_hlsearch)
6999 {
7000 last_pat_prog(&search_hl.rm);
7001 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007002# ifdef FEAT_RELTIME
7003 /* Set the time limit to 'redrawtime'. */
7004 profile_setlimit(p_rdt, &search_hl.tm);
7005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
7007}
7008
7009/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007010 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 */
7012 static void
7013end_search_hl()
7014{
7015 if (search_hl.rm.regprog != NULL)
7016 {
7017 vim_free(search_hl.rm.regprog);
7018 search_hl.rm.regprog = NULL;
7019 }
7020}
7021
7022/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007023 * Init for calling prepare_search_hl().
7024 */
7025 static void
7026init_search_hl(wp)
7027 win_T *wp;
7028{
7029 matchitem_T *cur;
7030
7031 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7032 * match */
7033 cur = wp->w_match_head;
7034 while (cur != NULL)
7035 {
7036 cur->hl.rm = cur->match;
7037 if (cur->hlg_id == 0)
7038 cur->hl.attr = 0;
7039 else
7040 cur->hl.attr = syn_id2attr(cur->hlg_id);
7041 cur->hl.buf = wp->w_buffer;
7042 cur->hl.lnum = 0;
7043 cur->hl.first_lnum = 0;
7044# ifdef FEAT_RELTIME
7045 /* Set the time limit to 'redrawtime'. */
7046 profile_setlimit(p_rdt, &(cur->hl.tm));
7047# endif
7048 cur = cur->next;
7049 }
7050 search_hl.buf = wp->w_buffer;
7051 search_hl.lnum = 0;
7052 search_hl.first_lnum = 0;
7053 /* time limit is set at the toplevel, for all windows */
7054}
7055
7056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 * Advance to the match in window "wp" line "lnum" or past it.
7058 */
7059 static void
7060prepare_search_hl(wp, lnum)
7061 win_T *wp;
7062 linenr_T lnum;
7063{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007064 matchitem_T *cur; /* points to the match list */
7065 match_T *shl; /* points to search_hl or a match */
7066 int shl_flag; /* flag to indicate whether search_hl
7067 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 int n;
7069
7070 /*
7071 * When using a multi-line pattern, start searching at the top
7072 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007073 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007075 cur = wp->w_match_head;
7076 shl_flag = FALSE;
7077 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007079 if (shl_flag == FALSE)
7080 {
7081 shl = &search_hl;
7082 shl_flag = TRUE;
7083 }
7084 else
7085 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 if (shl->rm.regprog != NULL
7087 && shl->lnum == 0
7088 && re_multiline(shl->rm.regprog))
7089 {
7090 if (shl->first_lnum == 0)
7091 {
7092# ifdef FEAT_FOLDING
7093 for (shl->first_lnum = lnum;
7094 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7095 if (hasFoldingWin(wp, shl->first_lnum - 1,
7096 NULL, NULL, TRUE, NULL))
7097 break;
7098# else
7099 shl->first_lnum = wp->w_topline;
7100# endif
7101 }
7102 n = 0;
7103 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7104 {
7105 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7106 if (shl->lnum != 0)
7107 {
7108 shl->first_lnum = shl->lnum
7109 + shl->rm.endpos[0].lnum
7110 - shl->rm.startpos[0].lnum;
7111 n = shl->rm.endpos[0].col;
7112 }
7113 else
7114 {
7115 ++shl->first_lnum;
7116 n = 0;
7117 }
7118 }
7119 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007120 if (shl != &search_hl && cur != NULL)
7121 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 }
7123}
7124
7125/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007126 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 * Uses shl->buf.
7128 * Sets shl->lnum and shl->rm contents.
7129 * Note: Assumes a previous match is always before "lnum", unless
7130 * shl->lnum is zero.
7131 * Careful: Any pointers for buffer lines will become invalid.
7132 */
7133 static void
7134next_search_hl(win, shl, lnum, mincol)
7135 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007136 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 linenr_T lnum;
7138 colnr_T mincol; /* minimal column for a match */
7139{
7140 linenr_T l;
7141 colnr_T matchcol;
7142 long nmatched;
7143
7144 if (shl->lnum != 0)
7145 {
7146 /* Check for three situations:
7147 * 1. If the "lnum" is below a previous match, start a new search.
7148 * 2. If the previous match includes "mincol", use it.
7149 * 3. Continue after the previous match.
7150 */
7151 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7152 if (lnum > l)
7153 shl->lnum = 0;
7154 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7155 return;
7156 }
7157
7158 /*
7159 * Repeat searching for a match until one is found that includes "mincol"
7160 * or none is found in this line.
7161 */
7162 called_emsg = FALSE;
7163 for (;;)
7164 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007165#ifdef FEAT_RELTIME
7166 /* Stop searching after passing the time limit. */
7167 if (profile_passed_limit(&(shl->tm)))
7168 {
7169 shl->lnum = 0; /* no match found in time */
7170 break;
7171 }
7172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 /* Three situations:
7174 * 1. No useful previous match: search from start of line.
7175 * 2. Not Vi compatible or empty match: continue at next character.
7176 * Break the loop if this is beyond the end of the line.
7177 * 3. Vi compatible searching: continue at end of previous match.
7178 */
7179 if (shl->lnum == 0)
7180 matchcol = 0;
7181 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7182 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007183 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007185 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007186
7187 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007188 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007189 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007191 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 shl->lnum = 0;
7193 break;
7194 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007195#ifdef FEAT_MBYTE
7196 if (has_mbyte)
7197 matchcol += mb_ptr2len(ml);
7198 else
7199#endif
7200 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 }
7202 else
7203 matchcol = shl->rm.endpos[0].col;
7204
7205 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007206 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7207#ifdef FEAT_RELTIME
7208 &(shl->tm)
7209#else
7210 NULL
7211#endif
7212 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007213 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {
7215 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007216 if (shl == &search_hl)
7217 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007218 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007219 vim_free(shl->rm.regprog);
7220 no_hlsearch = TRUE;
7221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007223 shl->lnum = 0;
7224 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 break;
7226 }
7227 if (nmatched == 0)
7228 {
7229 shl->lnum = 0; /* no match found */
7230 break;
7231 }
7232 if (shl->rm.startpos[0].lnum > 0
7233 || shl->rm.startpos[0].col >= mincol
7234 || nmatched > 1
7235 || shl->rm.endpos[0].col > mincol)
7236 {
7237 shl->lnum += shl->rm.startpos[0].lnum;
7238 break; /* useful match found */
7239 }
7240 }
7241}
7242#endif
7243
7244 static void
7245screen_start_highlight(attr)
7246 int attr;
7247{
7248 attrentry_T *aep = NULL;
7249
7250 screen_attr = attr;
7251 if (full_screen
7252#ifdef WIN3264
7253 && termcap_active
7254#endif
7255 )
7256 {
7257#ifdef FEAT_GUI
7258 if (gui.in_use)
7259 {
7260 char buf[20];
7261
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007262 /* The GUI handles this internally. */
7263 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 OUT_STR(buf);
7265 }
7266 else
7267#endif
7268 {
7269 if (attr > HL_ALL) /* special HL attr. */
7270 {
7271 if (t_colors > 1)
7272 aep = syn_cterm_attr2entry(attr);
7273 else
7274 aep = syn_term_attr2entry(attr);
7275 if (aep == NULL) /* did ":syntax clear" */
7276 attr = 0;
7277 else
7278 attr = aep->ae_attr;
7279 }
7280 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7281 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007282 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7283 && cterm_normal_fg_bold)
7284 /* If the Normal FG color has BOLD attribute and the new HL
7285 * has a FG color defined, clear BOLD. */
7286 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7288 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007289 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7290 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 out_str(T_US);
7292 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7293 out_str(T_CZH);
7294 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7295 out_str(T_MR);
7296
7297 /*
7298 * Output the color or start string after bold etc., in case the
7299 * bold etc. override the color setting.
7300 */
7301 if (aep != NULL)
7302 {
7303 if (t_colors > 1)
7304 {
7305 if (aep->ae_u.cterm.fg_color)
7306 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7307 if (aep->ae_u.cterm.bg_color)
7308 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7309 }
7310 else
7311 {
7312 if (aep->ae_u.term.start != NULL)
7313 out_str(aep->ae_u.term.start);
7314 }
7315 }
7316 }
7317 }
7318}
7319
7320 void
7321screen_stop_highlight()
7322{
7323 int do_ME = FALSE; /* output T_ME code */
7324
7325 if (screen_attr != 0
7326#ifdef WIN3264
7327 && termcap_active
7328#endif
7329 )
7330 {
7331#ifdef FEAT_GUI
7332 if (gui.in_use)
7333 {
7334 char buf[20];
7335
7336 /* use internal GUI code */
7337 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7338 OUT_STR(buf);
7339 }
7340 else
7341#endif
7342 {
7343 if (screen_attr > HL_ALL) /* special HL attr. */
7344 {
7345 attrentry_T *aep;
7346
7347 if (t_colors > 1)
7348 {
7349 /*
7350 * Assume that t_me restores the original colors!
7351 */
7352 aep = syn_cterm_attr2entry(screen_attr);
7353 if (aep != NULL && (aep->ae_u.cterm.fg_color
7354 || aep->ae_u.cterm.bg_color))
7355 do_ME = TRUE;
7356 }
7357 else
7358 {
7359 aep = syn_term_attr2entry(screen_attr);
7360 if (aep != NULL && aep->ae_u.term.stop != NULL)
7361 {
7362 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7363 do_ME = TRUE;
7364 else
7365 out_str(aep->ae_u.term.stop);
7366 }
7367 }
7368 if (aep == NULL) /* did ":syntax clear" */
7369 screen_attr = 0;
7370 else
7371 screen_attr = aep->ae_attr;
7372 }
7373
7374 /*
7375 * Often all ending-codes are equal to T_ME. Avoid outputting the
7376 * same sequence several times.
7377 */
7378 if (screen_attr & HL_STANDOUT)
7379 {
7380 if (STRCMP(T_SE, T_ME) == 0)
7381 do_ME = TRUE;
7382 else
7383 out_str(T_SE);
7384 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007385 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 {
7387 if (STRCMP(T_UE, T_ME) == 0)
7388 do_ME = TRUE;
7389 else
7390 out_str(T_UE);
7391 }
7392 if (screen_attr & HL_ITALIC)
7393 {
7394 if (STRCMP(T_CZR, T_ME) == 0)
7395 do_ME = TRUE;
7396 else
7397 out_str(T_CZR);
7398 }
7399 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7400 out_str(T_ME);
7401
7402 if (t_colors > 1)
7403 {
7404 /* set Normal cterm colors */
7405 if (cterm_normal_fg_color != 0)
7406 term_fg_color(cterm_normal_fg_color - 1);
7407 if (cterm_normal_bg_color != 0)
7408 term_bg_color(cterm_normal_bg_color - 1);
7409 if (cterm_normal_fg_bold)
7410 out_str(T_MD);
7411 }
7412 }
7413 }
7414 screen_attr = 0;
7415}
7416
7417/*
7418 * Reset the colors for a cterm. Used when leaving Vim.
7419 * The machine specific code may override this again.
7420 */
7421 void
7422reset_cterm_colors()
7423{
7424 if (t_colors > 1)
7425 {
7426 /* set Normal cterm colors */
7427 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7428 {
7429 out_str(T_OP);
7430 screen_attr = -1;
7431 }
7432 if (cterm_normal_fg_bold)
7433 {
7434 out_str(T_ME);
7435 screen_attr = -1;
7436 }
7437 }
7438}
7439
7440/*
7441 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7442 * using the attributes from ScreenAttrs["off"].
7443 */
7444 static void
7445screen_char(off, row, col)
7446 unsigned off;
7447 int row;
7448 int col;
7449{
7450 int attr;
7451
7452 /* Check for illegal values, just in case (could happen just after
7453 * resizing). */
7454 if (row >= screen_Rows || col >= screen_Columns)
7455 return;
7456
7457 /* Outputting the last character on the screen may scrollup the screen.
7458 * Don't to it! Mark the character invalid (update it when scrolled up) */
7459 if (row == screen_Rows - 1 && col == screen_Columns - 1
7460#ifdef FEAT_RIGHTLEFT
7461 /* account for first command-line character in rightleft mode */
7462 && !cmdmsg_rl
7463#endif
7464 )
7465 {
7466 ScreenAttrs[off] = (sattr_T)-1;
7467 return;
7468 }
7469
7470 /*
7471 * Stop highlighting first, so it's easier to move the cursor.
7472 */
7473#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7474 if (screen_char_attr != 0)
7475 attr = screen_char_attr;
7476 else
7477#endif
7478 attr = ScreenAttrs[off];
7479 if (screen_attr != attr)
7480 screen_stop_highlight();
7481
7482 windgoto(row, col);
7483
7484 if (screen_attr != attr)
7485 screen_start_highlight(attr);
7486
7487#ifdef FEAT_MBYTE
7488 if (enc_utf8 && ScreenLinesUC[off] != 0)
7489 {
7490 char_u buf[MB_MAXBYTES + 1];
7491
7492 /* Convert UTF-8 character to bytes and write it. */
7493
7494 buf[utfc_char2bytes(off, buf)] = NUL;
7495
7496 out_str(buf);
7497 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7498 ++screen_cur_col;
7499 }
7500 else
7501#endif
7502 {
7503#ifdef FEAT_MBYTE
7504 out_flush_check();
7505#endif
7506 out_char(ScreenLines[off]);
7507#ifdef FEAT_MBYTE
7508 /* double-byte character in single-width cell */
7509 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7510 out_char(ScreenLines2[off]);
7511#endif
7512 }
7513
7514 screen_cur_col++;
7515}
7516
7517#ifdef FEAT_MBYTE
7518
7519/*
7520 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7521 * on the screen at position 'row' and 'col'.
7522 * The attributes of the first byte is used for all. This is required to
7523 * output the two bytes of a double-byte character with nothing in between.
7524 */
7525 static void
7526screen_char_2(off, row, col)
7527 unsigned off;
7528 int row;
7529 int col;
7530{
7531 /* Check for illegal values (could be wrong when screen was resized). */
7532 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7533 return;
7534
7535 /* Outputting the last character on the screen may scrollup the screen.
7536 * Don't to it! Mark the character invalid (update it when scrolled up) */
7537 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7538 {
7539 ScreenAttrs[off] = (sattr_T)-1;
7540 return;
7541 }
7542
7543 /* Output the first byte normally (positions the cursor), then write the
7544 * second byte directly. */
7545 screen_char(off, row, col);
7546 out_char(ScreenLines[off + 1]);
7547 ++screen_cur_col;
7548}
7549#endif
7550
7551#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7552/*
7553 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7554 * This uses the contents of ScreenLines[] and doesn't change it.
7555 */
7556 void
7557screen_draw_rectangle(row, col, height, width, invert)
7558 int row;
7559 int col;
7560 int height;
7561 int width;
7562 int invert;
7563{
7564 int r, c;
7565 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007566#ifdef FEAT_MBYTE
7567 int max_off;
7568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007570 /* Can't use ScreenLines unless initialized */
7571 if (ScreenLines == NULL)
7572 return;
7573
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (invert)
7575 screen_char_attr = HL_INVERSE;
7576 for (r = row; r < row + height; ++r)
7577 {
7578 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007579#ifdef FEAT_MBYTE
7580 max_off = off + screen_Columns;
7581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 for (c = col; c < col + width; ++c)
7583 {
7584#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007585 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {
7587 screen_char_2(off + c, r, c);
7588 ++c;
7589 }
7590 else
7591#endif
7592 {
7593 screen_char(off + c, r, c);
7594#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007595 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 ++c;
7597#endif
7598 }
7599 }
7600 }
7601 screen_char_attr = 0;
7602}
7603#endif
7604
7605#ifdef FEAT_VERTSPLIT
7606/*
7607 * Redraw the characters for a vertically split window.
7608 */
7609 static void
7610redraw_block(row, end, wp)
7611 int row;
7612 int end;
7613 win_T *wp;
7614{
7615 int col;
7616 int width;
7617
7618# ifdef FEAT_CLIPBOARD
7619 clip_may_clear_selection(row, end - 1);
7620# endif
7621
7622 if (wp == NULL)
7623 {
7624 col = 0;
7625 width = Columns;
7626 }
7627 else
7628 {
7629 col = wp->w_wincol;
7630 width = wp->w_width;
7631 }
7632 screen_draw_rectangle(row, col, end - row, width, FALSE);
7633}
7634#endif
7635
7636/*
7637 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7638 * with character 'c1' in first column followed by 'c2' in the other columns.
7639 * Use attributes 'attr'.
7640 */
7641 void
7642screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7643 int start_row, end_row;
7644 int start_col, end_col;
7645 int c1, c2;
7646 int attr;
7647{
7648 int row;
7649 int col;
7650 int off;
7651 int end_off;
7652 int did_delete;
7653 int c;
7654 int norm_term;
7655#if defined(FEAT_GUI) || defined(UNIX)
7656 int force_next = FALSE;
7657#endif
7658
7659 if (end_row > screen_Rows) /* safety check */
7660 end_row = screen_Rows;
7661 if (end_col > screen_Columns) /* safety check */
7662 end_col = screen_Columns;
7663 if (ScreenLines == NULL
7664 || start_row >= end_row
7665 || start_col >= end_col) /* nothing to do */
7666 return;
7667
7668 /* it's a "normal" terminal when not in a GUI or cterm */
7669 norm_term = (
7670#ifdef FEAT_GUI
7671 !gui.in_use &&
7672#endif
7673 t_colors <= 1);
7674 for (row = start_row; row < end_row; ++row)
7675 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007676#ifdef FEAT_MBYTE
7677 if (has_mbyte
7678# ifdef FEAT_GUI
7679 && !gui.in_use
7680# endif
7681 )
7682 {
7683 /* When drawing over the right halve of a double-wide char clear
7684 * out the left halve. When drawing over the left halve of a
7685 * double wide-char clear out the right halve. Only needed in a
7686 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007687 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007688 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007689 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007690 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007691 }
7692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 /*
7694 * Try to use delete-line termcap code, when no attributes or in a
7695 * "normal" terminal, where a bold/italic space is just a
7696 * space.
7697 */
7698 did_delete = FALSE;
7699 if (c2 == ' '
7700 && end_col == Columns
7701 && can_clear(T_CE)
7702 && (attr == 0
7703 || (norm_term
7704 && attr <= HL_ALL
7705 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7706 {
7707 /*
7708 * check if we really need to clear something
7709 */
7710 col = start_col;
7711 if (c1 != ' ') /* don't clear first char */
7712 ++col;
7713
7714 off = LineOffset[row] + col;
7715 end_off = LineOffset[row] + end_col;
7716
7717 /* skip blanks (used often, keep it fast!) */
7718#ifdef FEAT_MBYTE
7719 if (enc_utf8)
7720 while (off < end_off && ScreenLines[off] == ' '
7721 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7722 ++off;
7723 else
7724#endif
7725 while (off < end_off && ScreenLines[off] == ' '
7726 && ScreenAttrs[off] == 0)
7727 ++off;
7728 if (off < end_off) /* something to be cleared */
7729 {
7730 col = off - LineOffset[row];
7731 screen_stop_highlight();
7732 term_windgoto(row, col);/* clear rest of this screen line */
7733 out_str(T_CE);
7734 screen_start(); /* don't know where cursor is now */
7735 col = end_col - col;
7736 while (col--) /* clear chars in ScreenLines */
7737 {
7738 ScreenLines[off] = ' ';
7739#ifdef FEAT_MBYTE
7740 if (enc_utf8)
7741 ScreenLinesUC[off] = 0;
7742#endif
7743 ScreenAttrs[off] = 0;
7744 ++off;
7745 }
7746 }
7747 did_delete = TRUE; /* the chars are cleared now */
7748 }
7749
7750 off = LineOffset[row] + start_col;
7751 c = c1;
7752 for (col = start_col; col < end_col; ++col)
7753 {
7754 if (ScreenLines[off] != c
7755#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007756 || (enc_utf8 && (int)ScreenLinesUC[off]
7757 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758#endif
7759 || ScreenAttrs[off] != attr
7760#if defined(FEAT_GUI) || defined(UNIX)
7761 || force_next
7762#endif
7763 )
7764 {
7765#if defined(FEAT_GUI) || defined(UNIX)
7766 /* The bold trick may make a single row of pixels appear in
7767 * the next character. When a bold character is removed, the
7768 * next character should be redrawn too. This happens for our
7769 * own GUI and for some xterms. */
7770 if (
7771# ifdef FEAT_GUI
7772 gui.in_use
7773# endif
7774# if defined(FEAT_GUI) && defined(UNIX)
7775 ||
7776# endif
7777# ifdef UNIX
7778 term_is_xterm
7779# endif
7780 )
7781 {
7782 if (ScreenLines[off] != ' '
7783 && (ScreenAttrs[off] > HL_ALL
7784 || ScreenAttrs[off] & HL_BOLD))
7785 force_next = TRUE;
7786 else
7787 force_next = FALSE;
7788 }
7789#endif
7790 ScreenLines[off] = c;
7791#ifdef FEAT_MBYTE
7792 if (enc_utf8)
7793 {
7794 if (c >= 0x80)
7795 {
7796 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007797 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 }
7799 else
7800 ScreenLinesUC[off] = 0;
7801 }
7802#endif
7803 ScreenAttrs[off] = attr;
7804 if (!did_delete || c != ' ')
7805 screen_char(off, row, col);
7806 }
7807 ++off;
7808 if (col == start_col)
7809 {
7810 if (did_delete)
7811 break;
7812 c = c2;
7813 }
7814 }
7815 if (end_col == Columns)
7816 LineWraps[row] = FALSE;
7817 if (row == Rows - 1) /* overwritten the command line */
7818 {
7819 redraw_cmdline = TRUE;
7820 if (c1 == ' ' && c2 == ' ')
7821 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007822 if (start_col == 0)
7823 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 }
7825 }
7826}
7827
7828/*
7829 * Check if there should be a delay. Used before clearing or redrawing the
7830 * screen or the command line.
7831 */
7832 void
7833check_for_delay(check_msg_scroll)
7834 int check_msg_scroll;
7835{
7836 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7837 && !did_wait_return
7838 && emsg_silent == 0)
7839 {
7840 out_flush();
7841 ui_delay(1000L, TRUE);
7842 emsg_on_display = FALSE;
7843 if (check_msg_scroll)
7844 msg_scroll = FALSE;
7845 }
7846}
7847
7848/*
7849 * screen_valid - allocate screen buffers if size changed
7850 * If "clear" is TRUE: clear screen if it has been resized.
7851 * Returns TRUE if there is a valid screen to write to.
7852 * Returns FALSE when starting up and screen not initialized yet.
7853 */
7854 int
7855screen_valid(clear)
7856 int clear;
7857{
7858 screenalloc(clear); /* allocate screen buffers if size changed */
7859 return (ScreenLines != NULL);
7860}
7861
7862/*
7863 * Resize the shell to Rows and Columns.
7864 * Allocate ScreenLines[] and associated items.
7865 *
7866 * There may be some time between setting Rows and Columns and (re)allocating
7867 * ScreenLines[]. This happens when starting up and when (manually) changing
7868 * the shell size. Always use screen_Rows and screen_Columns to access items
7869 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7870 * final size of the shell is needed.
7871 */
7872 void
7873screenalloc(clear)
7874 int clear;
7875{
7876 int new_row, old_row;
7877#ifdef FEAT_GUI
7878 int old_Rows;
7879#endif
7880 win_T *wp;
7881 int outofmem = FALSE;
7882 int len;
7883 schar_T *new_ScreenLines;
7884#ifdef FEAT_MBYTE
7885 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007886 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007888 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889#endif
7890 sattr_T *new_ScreenAttrs;
7891 unsigned *new_LineOffset;
7892 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007893#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007894 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007895 tabpage_T *tp;
7896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007898 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007899#ifdef FEAT_AUTOCMD
7900 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007902retry:
7903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 /*
7905 * Allocation of the screen buffers is done only when the size changes and
7906 * when Rows and Columns have been set and we have started doing full
7907 * screen stuff.
7908 */
7909 if ((ScreenLines != NULL
7910 && Rows == screen_Rows
7911 && Columns == screen_Columns
7912#ifdef FEAT_MBYTE
7913 && enc_utf8 == (ScreenLinesUC != NULL)
7914 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007915 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916#endif
7917 )
7918 || Rows == 0
7919 || Columns == 0
7920 || (!full_screen && ScreenLines == NULL))
7921 return;
7922
7923 /*
7924 * It's possible that we produce an out-of-memory message below, which
7925 * will cause this function to be called again. To break the loop, just
7926 * return here.
7927 */
7928 if (entered)
7929 return;
7930 entered = TRUE;
7931
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007932 /*
7933 * Note that the window sizes are updated before reallocating the arrays,
7934 * thus we must not redraw here!
7935 */
7936 ++RedrawingDisabled;
7937
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 win_new_shellsize(); /* fit the windows in the new sized shell */
7939
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 comp_col(); /* recompute columns for shown command and ruler */
7941
7942 /*
7943 * We're changing the size of the screen.
7944 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7945 * - Move lines from the old arrays into the new arrays, clear extra
7946 * lines (unless the screen is going to be cleared).
7947 * - Free the old arrays.
7948 *
7949 * If anything fails, make ScreenLines NULL, so we don't do anything!
7950 * Continuing with the old ScreenLines may result in a crash, because the
7951 * size is wrong.
7952 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007953 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007955#ifdef FEAT_AUTOCMD
7956 if (aucmd_win != NULL)
7957 win_free_lsize(aucmd_win);
7958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959
7960 new_ScreenLines = (schar_T *)lalloc((long_u)(
7961 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7962#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01007963 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (enc_utf8)
7965 {
7966 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7967 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007968 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007969 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7971 }
7972 if (enc_dbcs == DBCS_JPNU)
7973 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7974 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7975#endif
7976 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7977 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7978 new_LineOffset = (unsigned *)lalloc((long_u)(
7979 Rows * sizeof(unsigned)), FALSE);
7980 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007981#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007982 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007985 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {
7987 if (win_alloc_lines(wp) == FAIL)
7988 {
7989 outofmem = TRUE;
7990#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007991 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992#endif
7993 }
7994 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007995#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007996 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7997 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007998 outofmem = TRUE;
7999#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008000#ifdef FEAT_WINDOWS
8001give_up:
8002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008004#ifdef FEAT_MBYTE
8005 for (i = 0; i < p_mco; ++i)
8006 if (new_ScreenLinesC[i] == NULL)
8007 break;
8008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (new_ScreenLines == NULL
8010#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008011 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8013#endif
8014 || new_ScreenAttrs == NULL
8015 || new_LineOffset == NULL
8016 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008017#ifdef FEAT_WINDOWS
8018 || new_TabPageIdxs == NULL
8019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 || outofmem)
8021 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008022 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008023 {
8024 /* guess the size */
8025 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8026
8027 /* Remember we did this to avoid getting outofmem messages over
8028 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008029 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 vim_free(new_ScreenLines);
8032 new_ScreenLines = NULL;
8033#ifdef FEAT_MBYTE
8034 vim_free(new_ScreenLinesUC);
8035 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008036 for (i = 0; i < p_mco; ++i)
8037 {
8038 vim_free(new_ScreenLinesC[i]);
8039 new_ScreenLinesC[i] = NULL;
8040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 vim_free(new_ScreenLines2);
8042 new_ScreenLines2 = NULL;
8043#endif
8044 vim_free(new_ScreenAttrs);
8045 new_ScreenAttrs = NULL;
8046 vim_free(new_LineOffset);
8047 new_LineOffset = NULL;
8048 vim_free(new_LineWraps);
8049 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008050#ifdef FEAT_WINDOWS
8051 vim_free(new_TabPageIdxs);
8052 new_TabPageIdxs = NULL;
8053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 }
8055 else
8056 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008057 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008058
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 for (new_row = 0; new_row < Rows; ++new_row)
8060 {
8061 new_LineOffset[new_row] = new_row * Columns;
8062 new_LineWraps[new_row] = FALSE;
8063
8064 /*
8065 * If the screen is not going to be cleared, copy as much as
8066 * possible from the old screen to the new one and clear the rest
8067 * (used when resizing the window at the "--more--" prompt or when
8068 * executing an external command, for the GUI).
8069 */
8070 if (!clear)
8071 {
8072 (void)vim_memset(new_ScreenLines + new_row * Columns,
8073 ' ', (size_t)Columns * sizeof(schar_T));
8074#ifdef FEAT_MBYTE
8075 if (enc_utf8)
8076 {
8077 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8078 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008079 for (i = 0; i < p_mco; ++i)
8080 (void)vim_memset(new_ScreenLinesC[i]
8081 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 0, (size_t)Columns * sizeof(u8char_T));
8083 }
8084 if (enc_dbcs == DBCS_JPNU)
8085 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8086 0, (size_t)Columns * sizeof(schar_T));
8087#endif
8088 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8089 0, (size_t)Columns * sizeof(sattr_T));
8090 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008091 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {
8093 if (screen_Columns < Columns)
8094 len = screen_Columns;
8095 else
8096 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008097#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008098 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008099 * may be invalid now. Also when p_mco changes. */
8100 if (!(enc_utf8 && ScreenLinesUC == NULL)
8101 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008102#endif
8103 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8104 ScreenLines + LineOffset[old_row],
8105 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008107 if (enc_utf8 && ScreenLinesUC != NULL
8108 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {
8110 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8111 ScreenLinesUC + LineOffset[old_row],
8112 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008113 for (i = 0; i < p_mco; ++i)
8114 mch_memmove(new_ScreenLinesC[i]
8115 + new_LineOffset[new_row],
8116 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 (size_t)len * sizeof(u8char_T));
8118 }
8119 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8120 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8121 ScreenLines2 + LineOffset[old_row],
8122 (size_t)len * sizeof(schar_T));
8123#endif
8124 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8125 ScreenAttrs + LineOffset[old_row],
8126 (size_t)len * sizeof(sattr_T));
8127 }
8128 }
8129 }
8130 /* Use the last line of the screen for the current line. */
8131 current_ScreenLine = new_ScreenLines + Rows * Columns;
8132 }
8133
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008134 free_screenlines();
8135
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 ScreenLines = new_ScreenLines;
8137#ifdef FEAT_MBYTE
8138 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008139 for (i = 0; i < p_mco; ++i)
8140 ScreenLinesC[i] = new_ScreenLinesC[i];
8141 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 ScreenLines2 = new_ScreenLines2;
8143#endif
8144 ScreenAttrs = new_ScreenAttrs;
8145 LineOffset = new_LineOffset;
8146 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008147#ifdef FEAT_WINDOWS
8148 TabPageIdxs = new_TabPageIdxs;
8149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150
8151 /* It's important that screen_Rows and screen_Columns reflect the actual
8152 * size of ScreenLines[]. Set them before calling anything. */
8153#ifdef FEAT_GUI
8154 old_Rows = screen_Rows;
8155#endif
8156 screen_Rows = Rows;
8157 screen_Columns = Columns;
8158
8159 must_redraw = CLEAR; /* need to clear the screen later */
8160 if (clear)
8161 screenclear2();
8162
8163#ifdef FEAT_GUI
8164 else if (gui.in_use
8165 && !gui.starting
8166 && ScreenLines != NULL
8167 && old_Rows != Rows)
8168 {
8169 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8170 /*
8171 * Adjust the position of the cursor, for when executing an external
8172 * command.
8173 */
8174 if (msg_row >= Rows) /* Rows got smaller */
8175 msg_row = Rows - 1; /* put cursor at last row */
8176 else if (Rows > old_Rows) /* Rows got bigger */
8177 msg_row += Rows - old_Rows; /* put cursor in same place */
8178 if (msg_col >= Columns) /* Columns got smaller */
8179 msg_col = Columns - 1; /* put cursor at last column */
8180 }
8181#endif
8182
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008184 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008185
8186#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008187 /*
8188 * Do not apply autocommands more than 3 times to avoid an endless loop
8189 * in case applying autocommands always changes Rows or Columns.
8190 */
8191 if (starting == 0 && ++retry_count <= 3)
8192 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008193 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008194 /* In rare cases, autocommands may have altered Rows or Columns,
8195 * jump back to check if we need to allocate the screen again. */
8196 goto retry;
8197 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199}
8200
8201 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008202free_screenlines()
8203{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008204#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008205 int i;
8206
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008207 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008208 for (i = 0; i < Screen_mco; ++i)
8209 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008210 vim_free(ScreenLines2);
8211#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008212 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008213 vim_free(ScreenAttrs);
8214 vim_free(LineOffset);
8215 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008216#ifdef FEAT_WINDOWS
8217 vim_free(TabPageIdxs);
8218#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008219}
8220
8221 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222screenclear()
8223{
8224 check_for_delay(FALSE);
8225 screenalloc(FALSE); /* allocate screen buffers if size changed */
8226 screenclear2(); /* clear the screen */
8227}
8228
8229 static void
8230screenclear2()
8231{
8232 int i;
8233
8234 if (starting == NO_SCREEN || ScreenLines == NULL
8235#ifdef FEAT_GUI
8236 || (gui.in_use && gui.starting)
8237#endif
8238 )
8239 return;
8240
8241#ifdef FEAT_GUI
8242 if (!gui.in_use)
8243#endif
8244 screen_attr = -1; /* force setting the Normal colors */
8245 screen_stop_highlight(); /* don't want highlighting here */
8246
8247#ifdef FEAT_CLIPBOARD
8248 /* disable selection without redrawing it */
8249 clip_scroll_selection(9999);
8250#endif
8251
8252 /* blank out ScreenLines */
8253 for (i = 0; i < Rows; ++i)
8254 {
8255 lineclear(LineOffset[i], (int)Columns);
8256 LineWraps[i] = FALSE;
8257 }
8258
8259 if (can_clear(T_CL))
8260 {
8261 out_str(T_CL); /* clear the display */
8262 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008263 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 }
8265 else
8266 {
8267 /* can't clear the screen, mark all chars with invalid attributes */
8268 for (i = 0; i < Rows; ++i)
8269 lineinvalid(LineOffset[i], (int)Columns);
8270 clear_cmdline = TRUE;
8271 }
8272
8273 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8274
8275 win_rest_invalid(firstwin);
8276 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008277#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008278 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (must_redraw == CLEAR) /* no need to clear again */
8281 must_redraw = NOT_VALID;
8282 compute_cmdrow();
8283 msg_row = cmdline_row; /* put cursor on last line for messages */
8284 msg_col = 0;
8285 screen_start(); /* don't know where cursor is now */
8286 msg_scrolled = 0; /* can't scroll back */
8287 msg_didany = FALSE;
8288 msg_didout = FALSE;
8289}
8290
8291/*
8292 * Clear one line in ScreenLines.
8293 */
8294 static void
8295lineclear(off, width)
8296 unsigned off;
8297 int width;
8298{
8299 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8300#ifdef FEAT_MBYTE
8301 if (enc_utf8)
8302 (void)vim_memset(ScreenLinesUC + off, 0,
8303 (size_t)width * sizeof(u8char_T));
8304#endif
8305 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8306}
8307
8308/*
8309 * Mark one line in ScreenLines invalid by setting the attributes to an
8310 * invalid value.
8311 */
8312 static void
8313lineinvalid(off, width)
8314 unsigned off;
8315 int width;
8316{
8317 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8318}
8319
8320#ifdef FEAT_VERTSPLIT
8321/*
8322 * Copy part of a Screenline for vertically split window "wp".
8323 */
8324 static void
8325linecopy(to, from, wp)
8326 int to;
8327 int from;
8328 win_T *wp;
8329{
8330 unsigned off_to = LineOffset[to] + wp->w_wincol;
8331 unsigned off_from = LineOffset[from] + wp->w_wincol;
8332
8333 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8334 wp->w_width * sizeof(schar_T));
8335# ifdef FEAT_MBYTE
8336 if (enc_utf8)
8337 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008338 int i;
8339
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8341 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008342 for (i = 0; i < p_mco; ++i)
8343 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8344 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 }
8346 if (enc_dbcs == DBCS_JPNU)
8347 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8348 wp->w_width * sizeof(schar_T));
8349# endif
8350 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8351 wp->w_width * sizeof(sattr_T));
8352}
8353#endif
8354
8355/*
8356 * Return TRUE if clearing with term string "p" would work.
8357 * It can't work when the string is empty or it won't set the right background.
8358 */
8359 int
8360can_clear(p)
8361 char_u *p;
8362{
8363 return (*p != NUL && (t_colors <= 1
8364#ifdef FEAT_GUI
8365 || gui.in_use
8366#endif
8367 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8368}
8369
8370/*
8371 * Reset cursor position. Use whenever cursor was moved because of outputting
8372 * something directly to the screen (shell commands) or a terminal control
8373 * code.
8374 */
8375 void
8376screen_start()
8377{
8378 screen_cur_row = screen_cur_col = 9999;
8379}
8380
8381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 * Move the cursor to position "row","col" in the screen.
8383 * This tries to find the most efficient way to move, minimizing the number of
8384 * characters sent to the terminal.
8385 */
8386 void
8387windgoto(row, col)
8388 int row;
8389 int col;
8390{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008391 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 int i;
8393 int plan;
8394 int cost;
8395 int wouldbe_col;
8396 int noinvcurs;
8397 char_u *bs;
8398 int goto_cost;
8399 int attr;
8400
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008401#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8403
8404#define PLAN_LE 1
8405#define PLAN_CR 2
8406#define PLAN_NL 3
8407#define PLAN_WRITE 4
8408 /* Can't use ScreenLines unless initialized */
8409 if (ScreenLines == NULL)
8410 return;
8411
8412 if (col != screen_cur_col || row != screen_cur_row)
8413 {
8414 /* Check for valid position. */
8415 if (row < 0) /* window without text lines? */
8416 row = 0;
8417 if (row >= screen_Rows)
8418 row = screen_Rows - 1;
8419 if (col >= screen_Columns)
8420 col = screen_Columns - 1;
8421
8422 /* check if no cursor movement is allowed in highlight mode */
8423 if (screen_attr && *T_MS == NUL)
8424 noinvcurs = HIGHL_COST;
8425 else
8426 noinvcurs = 0;
8427 goto_cost = GOTO_COST + noinvcurs;
8428
8429 /*
8430 * Plan how to do the positioning:
8431 * 1. Use CR to move it to column 0, same row.
8432 * 2. Use T_LE to move it a few columns to the left.
8433 * 3. Use NL to move a few lines down, column 0.
8434 * 4. Move a few columns to the right with T_ND or by writing chars.
8435 *
8436 * Don't do this if the cursor went beyond the last column, the cursor
8437 * position is unknown then (some terminals wrap, some don't )
8438 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008439 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 * characters to move the cursor to the right.
8441 */
8442 if (row >= screen_cur_row && screen_cur_col < Columns)
8443 {
8444 /*
8445 * If the cursor is in the same row, bigger col, we can use CR
8446 * or T_LE.
8447 */
8448 bs = NULL; /* init for GCC */
8449 attr = screen_attr;
8450 if (row == screen_cur_row && col < screen_cur_col)
8451 {
8452 /* "le" is preferred over "bc", because "bc" is obsolete */
8453 if (*T_LE)
8454 bs = T_LE; /* "cursor left" */
8455 else
8456 bs = T_BC; /* "backspace character (old) */
8457 if (*bs)
8458 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8459 else
8460 cost = 999;
8461 if (col + 1 < cost) /* using CR is less characters */
8462 {
8463 plan = PLAN_CR;
8464 wouldbe_col = 0;
8465 cost = 1; /* CR is just one character */
8466 }
8467 else
8468 {
8469 plan = PLAN_LE;
8470 wouldbe_col = col;
8471 }
8472 if (noinvcurs) /* will stop highlighting */
8473 {
8474 cost += noinvcurs;
8475 attr = 0;
8476 }
8477 }
8478
8479 /*
8480 * If the cursor is above where we want to be, we can use CR LF.
8481 */
8482 else if (row > screen_cur_row)
8483 {
8484 plan = PLAN_NL;
8485 wouldbe_col = 0;
8486 cost = (row - screen_cur_row) * 2; /* CR LF */
8487 if (noinvcurs) /* will stop highlighting */
8488 {
8489 cost += noinvcurs;
8490 attr = 0;
8491 }
8492 }
8493
8494 /*
8495 * If the cursor is in the same row, smaller col, just use write.
8496 */
8497 else
8498 {
8499 plan = PLAN_WRITE;
8500 wouldbe_col = screen_cur_col;
8501 cost = 0;
8502 }
8503
8504 /*
8505 * Check if any characters that need to be written have the
8506 * correct attributes. Also avoid UTF-8 characters.
8507 */
8508 i = col - wouldbe_col;
8509 if (i > 0)
8510 cost += i;
8511 if (cost < goto_cost && i > 0)
8512 {
8513 /*
8514 * Check if the attributes are correct without additionally
8515 * stopping highlighting.
8516 */
8517 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8518 while (i && *p++ == attr)
8519 --i;
8520 if (i != 0)
8521 {
8522 /*
8523 * Try if it works when highlighting is stopped here.
8524 */
8525 if (*--p == 0)
8526 {
8527 cost += noinvcurs;
8528 while (i && *p++ == 0)
8529 --i;
8530 }
8531 if (i != 0)
8532 cost = 999; /* different attributes, don't do it */
8533 }
8534#ifdef FEAT_MBYTE
8535 if (enc_utf8)
8536 {
8537 /* Don't use an UTF-8 char for positioning, it's slow. */
8538 for (i = wouldbe_col; i < col; ++i)
8539 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8540 {
8541 cost = 999;
8542 break;
8543 }
8544 }
8545#endif
8546 }
8547
8548 /*
8549 * We can do it without term_windgoto()!
8550 */
8551 if (cost < goto_cost)
8552 {
8553 if (plan == PLAN_LE)
8554 {
8555 if (noinvcurs)
8556 screen_stop_highlight();
8557 while (screen_cur_col > col)
8558 {
8559 out_str(bs);
8560 --screen_cur_col;
8561 }
8562 }
8563 else if (plan == PLAN_CR)
8564 {
8565 if (noinvcurs)
8566 screen_stop_highlight();
8567 out_char('\r');
8568 screen_cur_col = 0;
8569 }
8570 else if (plan == PLAN_NL)
8571 {
8572 if (noinvcurs)
8573 screen_stop_highlight();
8574 while (screen_cur_row < row)
8575 {
8576 out_char('\n');
8577 ++screen_cur_row;
8578 }
8579 screen_cur_col = 0;
8580 }
8581
8582 i = col - screen_cur_col;
8583 if (i > 0)
8584 {
8585 /*
8586 * Use cursor-right if it's one character only. Avoids
8587 * removing a line of pixels from the last bold char, when
8588 * using the bold trick in the GUI.
8589 */
8590 if (T_ND[0] != NUL && T_ND[1] == NUL)
8591 {
8592 while (i-- > 0)
8593 out_char(*T_ND);
8594 }
8595 else
8596 {
8597 int off;
8598
8599 off = LineOffset[row] + screen_cur_col;
8600 while (i-- > 0)
8601 {
8602 if (ScreenAttrs[off] != screen_attr)
8603 screen_stop_highlight();
8604#ifdef FEAT_MBYTE
8605 out_flush_check();
8606#endif
8607 out_char(ScreenLines[off]);
8608#ifdef FEAT_MBYTE
8609 if (enc_dbcs == DBCS_JPNU
8610 && ScreenLines[off] == 0x8e)
8611 out_char(ScreenLines2[off]);
8612#endif
8613 ++off;
8614 }
8615 }
8616 }
8617 }
8618 }
8619 else
8620 cost = 999;
8621
8622 if (cost >= goto_cost)
8623 {
8624 if (noinvcurs)
8625 screen_stop_highlight();
8626 if (row == screen_cur_row && (col > screen_cur_col) &&
8627 *T_CRI != NUL)
8628 term_cursor_right(col - screen_cur_col);
8629 else
8630 term_windgoto(row, col);
8631 }
8632 screen_cur_row = row;
8633 screen_cur_col = col;
8634 }
8635}
8636
8637/*
8638 * Set cursor to its position in the current window.
8639 */
8640 void
8641setcursor()
8642{
8643 if (redrawing())
8644 {
8645 validate_cursor();
8646 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8647 W_WINCOL(curwin) + (
8648#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008649 /* With 'rightleft' set and the cursor on a double-wide
8650 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8652# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008653 (has_mbyte
8654 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8655 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656# endif
8657 1)) :
8658#endif
8659 curwin->w_wcol));
8660 }
8661}
8662
8663
8664/*
8665 * insert 'line_count' lines at 'row' in window 'wp'
8666 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8667 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8668 * scrolling.
8669 * Returns FAIL if the lines are not inserted, OK for success.
8670 */
8671 int
8672win_ins_lines(wp, row, line_count, invalid, mayclear)
8673 win_T *wp;
8674 int row;
8675 int line_count;
8676 int invalid;
8677 int mayclear;
8678{
8679 int did_delete;
8680 int nextrow;
8681 int lastrow;
8682 int retval;
8683
8684 if (invalid)
8685 wp->w_lines_valid = 0;
8686
8687 if (wp->w_height < 5)
8688 return FAIL;
8689
8690 if (line_count > wp->w_height - row)
8691 line_count = wp->w_height - row;
8692
8693 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8694 if (retval != MAYBE)
8695 return retval;
8696
8697 /*
8698 * If there is a next window or a status line, we first try to delete the
8699 * lines at the bottom to avoid messing what is after the window.
8700 * If this fails and there are following windows, don't do anything to avoid
8701 * messing up those windows, better just redraw.
8702 */
8703 did_delete = FALSE;
8704#ifdef FEAT_WINDOWS
8705 if (wp->w_next != NULL || wp->w_status_height)
8706 {
8707 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8708 line_count, (int)Rows, FALSE, NULL) == OK)
8709 did_delete = TRUE;
8710 else if (wp->w_next)
8711 return FAIL;
8712 }
8713#endif
8714 /*
8715 * if no lines deleted, blank the lines that will end up below the window
8716 */
8717 if (!did_delete)
8718 {
8719#ifdef FEAT_WINDOWS
8720 wp->w_redr_status = TRUE;
8721#endif
8722 redraw_cmdline = TRUE;
8723 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8724 lastrow = nextrow + line_count;
8725 if (lastrow > Rows)
8726 lastrow = Rows;
8727 screen_fill(nextrow - line_count, lastrow - line_count,
8728 W_WINCOL(wp), (int)W_ENDCOL(wp),
8729 ' ', ' ', 0);
8730 }
8731
8732 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8733 == FAIL)
8734 {
8735 /* deletion will have messed up other windows */
8736 if (did_delete)
8737 {
8738#ifdef FEAT_WINDOWS
8739 wp->w_redr_status = TRUE;
8740#endif
8741 win_rest_invalid(W_NEXT(wp));
8742 }
8743 return FAIL;
8744 }
8745
8746 return OK;
8747}
8748
8749/*
8750 * delete "line_count" window lines at "row" in window "wp"
8751 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8752 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8753 * scrolling
8754 * Return OK for success, FAIL if the lines are not deleted.
8755 */
8756 int
8757win_del_lines(wp, row, line_count, invalid, mayclear)
8758 win_T *wp;
8759 int row;
8760 int line_count;
8761 int invalid;
8762 int mayclear;
8763{
8764 int retval;
8765
8766 if (invalid)
8767 wp->w_lines_valid = 0;
8768
8769 if (line_count > wp->w_height - row)
8770 line_count = wp->w_height - row;
8771
8772 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8773 if (retval != MAYBE)
8774 return retval;
8775
8776 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8777 (int)Rows, FALSE, NULL) == FAIL)
8778 return FAIL;
8779
8780#ifdef FEAT_WINDOWS
8781 /*
8782 * If there are windows or status lines below, try to put them at the
8783 * correct place. If we can't do that, they have to be redrawn.
8784 */
8785 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8786 {
8787 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8788 line_count, (int)Rows, NULL) == FAIL)
8789 {
8790 wp->w_redr_status = TRUE;
8791 win_rest_invalid(wp->w_next);
8792 }
8793 }
8794 /*
8795 * If this is the last window and there is no status line, redraw the
8796 * command line later.
8797 */
8798 else
8799#endif
8800 redraw_cmdline = TRUE;
8801 return OK;
8802}
8803
8804/*
8805 * Common code for win_ins_lines() and win_del_lines().
8806 * Returns OK or FAIL when the work has been done.
8807 * Returns MAYBE when not finished yet.
8808 */
8809 static int
8810win_do_lines(wp, row, line_count, mayclear, del)
8811 win_T *wp;
8812 int row;
8813 int line_count;
8814 int mayclear;
8815 int del;
8816{
8817 int retval;
8818
8819 if (!redrawing() || line_count <= 0)
8820 return FAIL;
8821
8822 /* only a few lines left: redraw is faster */
8823 if (mayclear && Rows - line_count < 5
8824#ifdef FEAT_VERTSPLIT
8825 && wp->w_width == Columns
8826#endif
8827 )
8828 {
8829 screenclear(); /* will set wp->w_lines_valid to 0 */
8830 return FAIL;
8831 }
8832
8833 /*
8834 * Delete all remaining lines
8835 */
8836 if (row + line_count >= wp->w_height)
8837 {
8838 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8839 W_WINCOL(wp), (int)W_ENDCOL(wp),
8840 ' ', ' ', 0);
8841 return OK;
8842 }
8843
8844 /*
8845 * when scrolling, the message on the command line should be cleared,
8846 * otherwise it will stay there forever.
8847 */
8848 clear_cmdline = TRUE;
8849
8850 /*
8851 * If the terminal can set a scroll region, use that.
8852 * Always do this in a vertically split window. This will redraw from
8853 * ScreenLines[] when t_CV isn't defined. That's faster than using
8854 * win_line().
8855 * Don't use a scroll region when we are going to redraw the text, writing
8856 * a character in the lower right corner of the scroll region causes a
8857 * scroll-up in the DJGPP version.
8858 */
8859 if (scroll_region
8860#ifdef FEAT_VERTSPLIT
8861 || W_WIDTH(wp) != Columns
8862#endif
8863 )
8864 {
8865#ifdef FEAT_VERTSPLIT
8866 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8867#endif
8868 scroll_region_set(wp, row);
8869 if (del)
8870 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8871 wp->w_height - row, FALSE, wp);
8872 else
8873 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8874 wp->w_height - row, wp);
8875#ifdef FEAT_VERTSPLIT
8876 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8877#endif
8878 scroll_region_reset();
8879 return retval;
8880 }
8881
8882#ifdef FEAT_WINDOWS
8883 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8884 return FAIL;
8885#endif
8886
8887 return MAYBE;
8888}
8889
8890/*
8891 * window 'wp' and everything after it is messed up, mark it for redraw
8892 */
8893 static void
8894win_rest_invalid(wp)
8895 win_T *wp;
8896{
8897#ifdef FEAT_WINDOWS
8898 while (wp != NULL)
8899#else
8900 if (wp != NULL)
8901#endif
8902 {
8903 redraw_win_later(wp, NOT_VALID);
8904#ifdef FEAT_WINDOWS
8905 wp->w_redr_status = TRUE;
8906 wp = wp->w_next;
8907#endif
8908 }
8909 redraw_cmdline = TRUE;
8910}
8911
8912/*
8913 * The rest of the routines in this file perform screen manipulations. The
8914 * given operation is performed physically on the screen. The corresponding
8915 * change is also made to the internal screen image. In this way, the editor
8916 * anticipates the effect of editing changes on the appearance of the screen.
8917 * That way, when we call screenupdate a complete redraw isn't usually
8918 * necessary. Another advantage is that we can keep adding code to anticipate
8919 * screen changes, and in the meantime, everything still works.
8920 */
8921
8922/*
8923 * types for inserting or deleting lines
8924 */
8925#define USE_T_CAL 1
8926#define USE_T_CDL 2
8927#define USE_T_AL 3
8928#define USE_T_CE 4
8929#define USE_T_DL 5
8930#define USE_T_SR 6
8931#define USE_NL 7
8932#define USE_T_CD 8
8933#define USE_REDRAW 9
8934
8935/*
8936 * insert lines on the screen and update ScreenLines[]
8937 * 'end' is the line after the scrolled part. Normally it is Rows.
8938 * When scrolling region used 'off' is the offset from the top for the region.
8939 * 'row' and 'end' are relative to the start of the region.
8940 *
8941 * return FAIL for failure, OK for success.
8942 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008943 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944screen_ins_lines(off, row, line_count, end, wp)
8945 int off;
8946 int row;
8947 int line_count;
8948 int end;
8949 win_T *wp; /* NULL or window to use width from */
8950{
8951 int i;
8952 int j;
8953 unsigned temp;
8954 int cursor_row;
8955 int type;
8956 int result_empty;
8957 int can_ce = can_clear(T_CE);
8958
8959 /*
8960 * FAIL if
8961 * - there is no valid screen
8962 * - the screen has to be redrawn completely
8963 * - the line count is less than one
8964 * - the line count is more than 'ttyscroll'
8965 */
8966 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8967 return FAIL;
8968
8969 /*
8970 * There are seven ways to insert lines:
8971 * 0. When in a vertically split window and t_CV isn't set, redraw the
8972 * characters from ScreenLines[].
8973 * 1. Use T_CD (clear to end of display) if it exists and the result of
8974 * the insert is just empty lines
8975 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8976 * present or line_count > 1. It looks better if we do all the inserts
8977 * at once.
8978 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8979 * insert is just empty lines and T_CE is not present or line_count >
8980 * 1.
8981 * 4. Use T_AL (insert line) if it exists.
8982 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8983 * just empty lines.
8984 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8985 * just empty lines.
8986 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8987 * the 'da' flag is not set or we have clear line capability.
8988 * 8. redraw the characters from ScreenLines[].
8989 *
8990 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8991 * the scrollbar for the window. It does have insert line, use that if it
8992 * exists.
8993 */
8994 result_empty = (row + line_count >= end);
8995#ifdef FEAT_VERTSPLIT
8996 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8997 type = USE_REDRAW;
8998 else
8999#endif
9000 if (can_clear(T_CD) && result_empty)
9001 type = USE_T_CD;
9002 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9003 type = USE_T_CAL;
9004 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9005 type = USE_T_CDL;
9006 else if (*T_AL != NUL)
9007 type = USE_T_AL;
9008 else if (can_ce && result_empty)
9009 type = USE_T_CE;
9010 else if (*T_DL != NUL && result_empty)
9011 type = USE_T_DL;
9012 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9013 type = USE_T_SR;
9014 else
9015 return FAIL;
9016
9017 /*
9018 * For clearing the lines screen_del_lines() is used. This will also take
9019 * care of t_db if necessary.
9020 */
9021 if (type == USE_T_CD || type == USE_T_CDL ||
9022 type == USE_T_CE || type == USE_T_DL)
9023 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9024
9025 /*
9026 * If text is retained below the screen, first clear or delete as many
9027 * lines at the bottom of the window as are about to be inserted so that
9028 * the deleted lines won't later surface during a screen_del_lines.
9029 */
9030 if (*T_DB)
9031 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9032
9033#ifdef FEAT_CLIPBOARD
9034 /* Remove a modeless selection when inserting lines halfway the screen
9035 * or not the full width of the screen. */
9036 if (off + row > 0
9037# ifdef FEAT_VERTSPLIT
9038 || (wp != NULL && wp->w_width != Columns)
9039# endif
9040 )
9041 clip_clear_selection();
9042 else
9043 clip_scroll_selection(-line_count);
9044#endif
9045
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046#ifdef FEAT_GUI
9047 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9048 * scrolling is actually carried out. */
9049 gui_dont_update_cursor();
9050#endif
9051
9052 if (*T_CCS != NUL) /* cursor relative to region */
9053 cursor_row = row;
9054 else
9055 cursor_row = row + off;
9056
9057 /*
9058 * Shift LineOffset[] line_count down to reflect the inserted lines.
9059 * Clear the inserted lines in ScreenLines[].
9060 */
9061 row += off;
9062 end += off;
9063 for (i = 0; i < line_count; ++i)
9064 {
9065#ifdef FEAT_VERTSPLIT
9066 if (wp != NULL && wp->w_width != Columns)
9067 {
9068 /* need to copy part of a line */
9069 j = end - 1 - i;
9070 while ((j -= line_count) >= row)
9071 linecopy(j + line_count, j, wp);
9072 j += line_count;
9073 if (can_clear((char_u *)" "))
9074 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9075 else
9076 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9077 LineWraps[j] = FALSE;
9078 }
9079 else
9080#endif
9081 {
9082 j = end - 1 - i;
9083 temp = LineOffset[j];
9084 while ((j -= line_count) >= row)
9085 {
9086 LineOffset[j + line_count] = LineOffset[j];
9087 LineWraps[j + line_count] = LineWraps[j];
9088 }
9089 LineOffset[j + line_count] = temp;
9090 LineWraps[j + line_count] = FALSE;
9091 if (can_clear((char_u *)" "))
9092 lineclear(temp, (int)Columns);
9093 else
9094 lineinvalid(temp, (int)Columns);
9095 }
9096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
9098 screen_stop_highlight();
9099 windgoto(cursor_row, 0);
9100
9101#ifdef FEAT_VERTSPLIT
9102 /* redraw the characters */
9103 if (type == USE_REDRAW)
9104 redraw_block(row, end, wp);
9105 else
9106#endif
9107 if (type == USE_T_CAL)
9108 {
9109 term_append_lines(line_count);
9110 screen_start(); /* don't know where cursor is now */
9111 }
9112 else
9113 {
9114 for (i = 0; i < line_count; i++)
9115 {
9116 if (type == USE_T_AL)
9117 {
9118 if (i && cursor_row != 0)
9119 windgoto(cursor_row, 0);
9120 out_str(T_AL);
9121 }
9122 else /* type == USE_T_SR */
9123 out_str(T_SR);
9124 screen_start(); /* don't know where cursor is now */
9125 }
9126 }
9127
9128 /*
9129 * With scroll-reverse and 'da' flag set we need to clear the lines that
9130 * have been scrolled down into the region.
9131 */
9132 if (type == USE_T_SR && *T_DA)
9133 {
9134 for (i = 0; i < line_count; ++i)
9135 {
9136 windgoto(off + i, 0);
9137 out_str(T_CE);
9138 screen_start(); /* don't know where cursor is now */
9139 }
9140 }
9141
9142#ifdef FEAT_GUI
9143 gui_can_update_cursor();
9144 if (gui.in_use)
9145 out_flush(); /* always flush after a scroll */
9146#endif
9147 return OK;
9148}
9149
9150/*
9151 * delete lines on the screen and update ScreenLines[]
9152 * 'end' is the line after the scrolled part. Normally it is Rows.
9153 * When scrolling region used 'off' is the offset from the top for the region.
9154 * 'row' and 'end' are relative to the start of the region.
9155 *
9156 * Return OK for success, FAIL if the lines are not deleted.
9157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 int
9159screen_del_lines(off, row, line_count, end, force, wp)
9160 int off;
9161 int row;
9162 int line_count;
9163 int end;
9164 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009165 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167 int j;
9168 int i;
9169 unsigned temp;
9170 int cursor_row;
9171 int cursor_end;
9172 int result_empty; /* result is empty until end of region */
9173 int can_delete; /* deleting line codes can be used */
9174 int type;
9175
9176 /*
9177 * FAIL if
9178 * - there is no valid screen
9179 * - the screen has to be redrawn completely
9180 * - the line count is less than one
9181 * - the line count is more than 'ttyscroll'
9182 */
9183 if (!screen_valid(TRUE) || line_count <= 0 ||
9184 (!force && line_count > p_ttyscroll))
9185 return FAIL;
9186
9187 /*
9188 * Check if the rest of the current region will become empty.
9189 */
9190 result_empty = row + line_count >= end;
9191
9192 /*
9193 * We can delete lines only when 'db' flag not set or when 'ce' option
9194 * available.
9195 */
9196 can_delete = (*T_DB == NUL || can_clear(T_CE));
9197
9198 /*
9199 * There are six ways to delete lines:
9200 * 0. When in a vertically split window and t_CV isn't set, redraw the
9201 * characters from ScreenLines[].
9202 * 1. Use T_CD if it exists and the result is empty.
9203 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9204 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9205 * none of the other ways work.
9206 * 4. Use T_CE (erase line) if the result is empty.
9207 * 5. Use T_DL (delete line) if it exists.
9208 * 6. redraw the characters from ScreenLines[].
9209 */
9210#ifdef FEAT_VERTSPLIT
9211 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9212 type = USE_REDRAW;
9213 else
9214#endif
9215 if (can_clear(T_CD) && result_empty)
9216 type = USE_T_CD;
9217#if defined(__BEOS__) && defined(BEOS_DR8)
9218 /*
9219 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9220 * its internal termcap... this works okay for tests which test *T_DB !=
9221 * NUL. It has the disadvantage that the user cannot use any :set t_*
9222 * command to get T_DB (back) to empty_option, only :set term=... will do
9223 * the trick...
9224 * Anyway, this hack will hopefully go away with the next OS release.
9225 * (Olaf Seibert)
9226 */
9227 else if (row == 0 && T_DB == empty_option
9228 && (line_count == 1 || *T_CDL == NUL))
9229#else
9230 else if (row == 0 && (
9231#ifndef AMIGA
9232 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9233 * up, so use delete-line command */
9234 line_count == 1 ||
9235#endif
9236 *T_CDL == NUL))
9237#endif
9238 type = USE_NL;
9239 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9240 type = USE_T_CDL;
9241 else if (can_clear(T_CE) && result_empty
9242#ifdef FEAT_VERTSPLIT
9243 && (wp == NULL || wp->w_width == Columns)
9244#endif
9245 )
9246 type = USE_T_CE;
9247 else if (*T_DL != NUL && can_delete)
9248 type = USE_T_DL;
9249 else if (*T_CDL != NUL && can_delete)
9250 type = USE_T_CDL;
9251 else
9252 return FAIL;
9253
9254#ifdef FEAT_CLIPBOARD
9255 /* Remove a modeless selection when deleting lines halfway the screen or
9256 * not the full width of the screen. */
9257 if (off + row > 0
9258# ifdef FEAT_VERTSPLIT
9259 || (wp != NULL && wp->w_width != Columns)
9260# endif
9261 )
9262 clip_clear_selection();
9263 else
9264 clip_scroll_selection(line_count);
9265#endif
9266
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267#ifdef FEAT_GUI
9268 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9269 * scrolling is actually carried out. */
9270 gui_dont_update_cursor();
9271#endif
9272
9273 if (*T_CCS != NUL) /* cursor relative to region */
9274 {
9275 cursor_row = row;
9276 cursor_end = end;
9277 }
9278 else
9279 {
9280 cursor_row = row + off;
9281 cursor_end = end + off;
9282 }
9283
9284 /*
9285 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9286 * Clear the inserted lines in ScreenLines[].
9287 */
9288 row += off;
9289 end += off;
9290 for (i = 0; i < line_count; ++i)
9291 {
9292#ifdef FEAT_VERTSPLIT
9293 if (wp != NULL && wp->w_width != Columns)
9294 {
9295 /* need to copy part of a line */
9296 j = row + i;
9297 while ((j += line_count) <= end - 1)
9298 linecopy(j - line_count, j, wp);
9299 j -= line_count;
9300 if (can_clear((char_u *)" "))
9301 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9302 else
9303 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9304 LineWraps[j] = FALSE;
9305 }
9306 else
9307#endif
9308 {
9309 /* whole width, moving the line pointers is faster */
9310 j = row + i;
9311 temp = LineOffset[j];
9312 while ((j += line_count) <= end - 1)
9313 {
9314 LineOffset[j - line_count] = LineOffset[j];
9315 LineWraps[j - line_count] = LineWraps[j];
9316 }
9317 LineOffset[j - line_count] = temp;
9318 LineWraps[j - line_count] = FALSE;
9319 if (can_clear((char_u *)" "))
9320 lineclear(temp, (int)Columns);
9321 else
9322 lineinvalid(temp, (int)Columns);
9323 }
9324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325
9326 screen_stop_highlight();
9327
9328#ifdef FEAT_VERTSPLIT
9329 /* redraw the characters */
9330 if (type == USE_REDRAW)
9331 redraw_block(row, end, wp);
9332 else
9333#endif
9334 if (type == USE_T_CD) /* delete the lines */
9335 {
9336 windgoto(cursor_row, 0);
9337 out_str(T_CD);
9338 screen_start(); /* don't know where cursor is now */
9339 }
9340 else if (type == USE_T_CDL)
9341 {
9342 windgoto(cursor_row, 0);
9343 term_delete_lines(line_count);
9344 screen_start(); /* don't know where cursor is now */
9345 }
9346 /*
9347 * Deleting lines at top of the screen or scroll region: Just scroll
9348 * the whole screen (scroll region) up by outputting newlines on the
9349 * last line.
9350 */
9351 else if (type == USE_NL)
9352 {
9353 windgoto(cursor_end - 1, 0);
9354 for (i = line_count; --i >= 0; )
9355 out_char('\n'); /* cursor will remain on same line */
9356 }
9357 else
9358 {
9359 for (i = line_count; --i >= 0; )
9360 {
9361 if (type == USE_T_DL)
9362 {
9363 windgoto(cursor_row, 0);
9364 out_str(T_DL); /* delete a line */
9365 }
9366 else /* type == USE_T_CE */
9367 {
9368 windgoto(cursor_row + i, 0);
9369 out_str(T_CE); /* erase a line */
9370 }
9371 screen_start(); /* don't know where cursor is now */
9372 }
9373 }
9374
9375 /*
9376 * If the 'db' flag is set, we need to clear the lines that have been
9377 * scrolled up at the bottom of the region.
9378 */
9379 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9380 {
9381 for (i = line_count; i > 0; --i)
9382 {
9383 windgoto(cursor_end - i, 0);
9384 out_str(T_CE); /* erase a line */
9385 screen_start(); /* don't know where cursor is now */
9386 }
9387 }
9388
9389#ifdef FEAT_GUI
9390 gui_can_update_cursor();
9391 if (gui.in_use)
9392 out_flush(); /* always flush after a scroll */
9393#endif
9394
9395 return OK;
9396}
9397
9398/*
9399 * show the current mode and ruler
9400 *
9401 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9402 * If clear_cmdline is FALSE there may be a message there that needs to be
9403 * cleared only if a mode is shown.
9404 * Return the length of the message (0 if no message).
9405 */
9406 int
9407showmode()
9408{
9409 int need_clear;
9410 int length = 0;
9411 int do_mode;
9412 int attr;
9413 int nwr_save;
9414#ifdef FEAT_INS_EXPAND
9415 int sub_attr;
9416#endif
9417
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009418 do_mode = ((p_smd && msg_silent == 0)
9419 && ((State & INSERT)
9420 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421#ifdef FEAT_VISUAL
9422 || VIsual_active
9423#endif
9424 ));
9425 if (do_mode || Recording)
9426 {
9427 /*
9428 * Don't show mode right now, when not redrawing or inside a mapping.
9429 * Call char_avail() only when we are going to show something, because
9430 * it takes a bit of time.
9431 */
9432 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9433 {
9434 redraw_cmdline = TRUE; /* show mode later */
9435 return 0;
9436 }
9437
9438 nwr_save = need_wait_return;
9439
9440 /* wait a bit before overwriting an important message */
9441 check_for_delay(FALSE);
9442
9443 /* if the cmdline is more than one line high, erase top lines */
9444 need_clear = clear_cmdline;
9445 if (clear_cmdline && cmdline_row < Rows - 1)
9446 msg_clr_cmdline(); /* will reset clear_cmdline */
9447
9448 /* Position on the last line in the window, column 0 */
9449 msg_pos_mode();
9450 cursor_off();
9451 attr = hl_attr(HLF_CM); /* Highlight mode */
9452 if (do_mode)
9453 {
9454 MSG_PUTS_ATTR("--", attr);
9455#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009456 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009457# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009458 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009459# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009460 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009461# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009462 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009463# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 MSG_PUTS_ATTR(" IM", attr);
9465# else
9466 MSG_PUTS_ATTR(" XIM", attr);
9467# endif
9468#endif
9469#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9470 if (gui.in_use)
9471 {
9472 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009473 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 }
9475#endif
9476#ifdef FEAT_INS_EXPAND
9477 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9478 {
9479 /* These messages can get long, avoid a wrap in a narrow
9480 * window. Prefer showing edit_submode_extra. */
9481 length = (Rows - msg_row) * Columns - 3;
9482 if (edit_submode_extra != NULL)
9483 length -= vim_strsize(edit_submode_extra);
9484 if (length > 0)
9485 {
9486 if (edit_submode_pre != NULL)
9487 length -= vim_strsize(edit_submode_pre);
9488 if (length - vim_strsize(edit_submode) > 0)
9489 {
9490 if (edit_submode_pre != NULL)
9491 msg_puts_attr(edit_submode_pre, attr);
9492 msg_puts_attr(edit_submode, attr);
9493 }
9494 if (edit_submode_extra != NULL)
9495 {
9496 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9497 if ((int)edit_submode_highl < (int)HLF_COUNT)
9498 sub_attr = hl_attr(edit_submode_highl);
9499 else
9500 sub_attr = attr;
9501 msg_puts_attr(edit_submode_extra, sub_attr);
9502 }
9503 }
9504 length = 0;
9505 }
9506 else
9507#endif
9508 {
9509#ifdef FEAT_VREPLACE
9510 if (State & VREPLACE_FLAG)
9511 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9512 else
9513#endif
9514 if (State & REPLACE_FLAG)
9515 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9516 else if (State & INSERT)
9517 {
9518#ifdef FEAT_RIGHTLEFT
9519 if (p_ri)
9520 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9521#endif
9522 MSG_PUTS_ATTR(_(" INSERT"), attr);
9523 }
9524 else if (restart_edit == 'I')
9525 MSG_PUTS_ATTR(_(" (insert)"), attr);
9526 else if (restart_edit == 'R')
9527 MSG_PUTS_ATTR(_(" (replace)"), attr);
9528 else if (restart_edit == 'V')
9529 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9530#ifdef FEAT_RIGHTLEFT
9531 if (p_hkmap)
9532 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9533# ifdef FEAT_FKMAP
9534 if (p_fkmap)
9535 MSG_PUTS_ATTR(farsi_text_5, attr);
9536# endif
9537#endif
9538#ifdef FEAT_KEYMAP
9539 if (State & LANGMAP)
9540 {
9541# ifdef FEAT_ARABIC
9542 if (curwin->w_p_arab)
9543 MSG_PUTS_ATTR(_(" Arabic"), attr);
9544 else
9545# endif
9546 MSG_PUTS_ATTR(_(" (lang)"), attr);
9547 }
9548#endif
9549 if ((State & INSERT) && p_paste)
9550 MSG_PUTS_ATTR(_(" (paste)"), attr);
9551
9552#ifdef FEAT_VISUAL
9553 if (VIsual_active)
9554 {
9555 char *p;
9556
9557 /* Don't concatenate separate words to avoid translation
9558 * problems. */
9559 switch ((VIsual_select ? 4 : 0)
9560 + (VIsual_mode == Ctrl_V) * 2
9561 + (VIsual_mode == 'V'))
9562 {
9563 case 0: p = N_(" VISUAL"); break;
9564 case 1: p = N_(" VISUAL LINE"); break;
9565 case 2: p = N_(" VISUAL BLOCK"); break;
9566 case 4: p = N_(" SELECT"); break;
9567 case 5: p = N_(" SELECT LINE"); break;
9568 default: p = N_(" SELECT BLOCK"); break;
9569 }
9570 MSG_PUTS_ATTR(_(p), attr);
9571 }
9572#endif
9573 MSG_PUTS_ATTR(" --", attr);
9574 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009575
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 need_clear = TRUE;
9577 }
9578 if (Recording
9579#ifdef FEAT_INS_EXPAND
9580 && edit_submode == NULL /* otherwise it gets too long */
9581#endif
9582 )
9583 {
9584 MSG_PUTS_ATTR(_("recording"), attr);
9585 need_clear = TRUE;
9586 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009587
9588 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 if (need_clear || clear_cmdline)
9590 msg_clr_eos();
9591 msg_didout = FALSE; /* overwrite this message */
9592 length = msg_col;
9593 msg_col = 0;
9594 need_wait_return = nwr_save; /* never ask for hit-return for this */
9595 }
9596 else if (clear_cmdline && msg_silent == 0)
9597 /* Clear the whole command line. Will reset "clear_cmdline". */
9598 msg_clr_cmdline();
9599
9600#ifdef FEAT_CMDL_INFO
9601# ifdef FEAT_VISUAL
9602 /* In Visual mode the size of the selected area must be redrawn. */
9603 if (VIsual_active)
9604 clear_showcmd();
9605# endif
9606
9607 /* If the last window has no status line, the ruler is after the mode
9608 * message and must be redrawn */
9609 if (redrawing()
9610# ifdef FEAT_WINDOWS
9611 && lastwin->w_status_height == 0
9612# endif
9613 )
9614 win_redr_ruler(lastwin, TRUE);
9615#endif
9616 redraw_cmdline = FALSE;
9617 clear_cmdline = FALSE;
9618
9619 return length;
9620}
9621
9622/*
9623 * Position for a mode message.
9624 */
9625 static void
9626msg_pos_mode()
9627{
9628 msg_col = 0;
9629 msg_row = Rows - 1;
9630}
9631
9632/*
9633 * Delete mode message. Used when ESC is typed which is expected to end
9634 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009635 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 */
9637 void
9638unshowmode(force)
9639 int force;
9640{
9641 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009642 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 */
9644 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9645 redraw_cmdline = TRUE; /* delete mode later */
9646 else
9647 {
9648 msg_pos_mode();
9649 if (Recording)
9650 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9651 msg_clr_eos();
9652 }
9653}
9654
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009655#if defined(FEAT_WINDOWS)
9656/*
9657 * Draw the tab pages line at the top of the Vim window.
9658 */
9659 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009660draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009661{
9662 int tabcount = 0;
9663 tabpage_T *tp;
9664 int tabwidth;
9665 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009666 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009667 int attr;
9668 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009669 win_T *cwp;
9670 int wincount;
9671 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009672 int c;
9673 int len;
9674 int attr_sel = hl_attr(HLF_TPS);
9675 int attr_nosel = hl_attr(HLF_TP);
9676 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009677 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009678 int room;
9679 int use_sep_chars = (t_colors < 8
9680#ifdef FEAT_GUI
9681 && !gui.in_use
9682#endif
9683 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009684
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009685 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009686
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009687#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009688 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009689 if (gui_use_tabline())
9690 {
9691 gui_update_tabline();
9692 return;
9693 }
9694#endif
9695
9696 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009697 return;
9698
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009699#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009700
9701 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9702 for (scol = 0; scol < Columns; ++scol)
9703 TabPageIdxs[scol] = 0;
9704
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009705 /* Use the 'tabline' option if it's set. */
9706 if (*p_tal != NUL)
9707 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009708 int save_called_emsg = called_emsg;
9709
9710 /* Check for an error. If there is one we would loop in redrawing the
9711 * screen. Avoid that by making 'tabline' empty. */
9712 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009713 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009714 if (called_emsg)
9715 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009716 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009717 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009718 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009719 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009720#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009721 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009722 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9723 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009724
Bram Moolenaar238a5642006-02-21 22:12:05 +00009725 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9726 if (tabwidth < 6)
9727 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009728
Bram Moolenaar238a5642006-02-21 22:12:05 +00009729 attr = attr_nosel;
9730 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009731 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009732 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9733 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009734 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009735 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009736
Bram Moolenaar238a5642006-02-21 22:12:05 +00009737 if (tp->tp_topframe == topframe)
9738 attr = attr_sel;
9739 if (use_sep_chars && col > 0)
9740 screen_putchar('|', 0, col++, attr);
9741
9742 if (tp->tp_topframe != topframe)
9743 attr = attr_nosel;
9744
9745 screen_putchar(' ', 0, col++, attr);
9746
9747 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009748 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009749 cwp = curwin;
9750 wp = firstwin;
9751 }
9752 else
9753 {
9754 cwp = tp->tp_curwin;
9755 wp = tp->tp_firstwin;
9756 }
9757
9758 modified = FALSE;
9759 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9760 if (bufIsChanged(wp->w_buffer))
9761 modified = TRUE;
9762 if (modified || wincount > 1)
9763 {
9764 if (wincount > 1)
9765 {
9766 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009767 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009768 if (col + len >= Columns - 3)
9769 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009770 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009771#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009772 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009773#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009774 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009775#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009776 );
9777 col += len;
9778 }
9779 if (modified)
9780 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9781 screen_putchar(' ', 0, col++, attr);
9782 }
9783
9784 room = scol - col + tabwidth - 1;
9785 if (room > 0)
9786 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009787 /* Get buffer name in NameBuff[] */
9788 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009789 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009790 len = vim_strsize(NameBuff);
9791 p = NameBuff;
9792#ifdef FEAT_MBYTE
9793 if (has_mbyte)
9794 while (len > room)
9795 {
9796 len -= ptr2cells(p);
9797 mb_ptr_adv(p);
9798 }
9799 else
9800#endif
9801 if (len > room)
9802 {
9803 p += len - room;
9804 len = room;
9805 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009806 if (len > Columns - col - 1)
9807 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009808
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009809 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009810 col += len;
9811 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009812 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009813
9814 /* Store the tab page number in TabPageIdxs[], so that
9815 * jump_to_mouse() knows where each one is. */
9816 ++tabcount;
9817 while (scol < col)
9818 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009819 }
9820
Bram Moolenaar238a5642006-02-21 22:12:05 +00009821 if (use_sep_chars)
9822 c = '_';
9823 else
9824 c = ' ';
9825 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009826
9827 /* Put an "X" for closing the current tab if there are several. */
9828 if (first_tabpage->tp_next != NULL)
9829 {
9830 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9831 TabPageIdxs[Columns - 1] = -999;
9832 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009833 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009834
9835 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9836 * set. */
9837 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009838}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009839
9840/*
9841 * Get buffer name for "buf" into NameBuff[].
9842 * Takes care of special buffer names and translates special characters.
9843 */
9844 void
9845get_trans_bufname(buf)
9846 buf_T *buf;
9847{
9848 if (buf_spname(buf) != NULL)
9849 STRCPY(NameBuff, buf_spname(buf));
9850 else
9851 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9852 trans_characters(NameBuff, MAXPATHL);
9853}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009854#endif
9855
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9857/*
9858 * Get the character to use in a status line. Get its attributes in "*attr".
9859 */
9860 static int
9861fillchar_status(attr, is_curwin)
9862 int *attr;
9863 int is_curwin;
9864{
9865 int fill;
9866 if (is_curwin)
9867 {
9868 *attr = hl_attr(HLF_S);
9869 fill = fill_stl;
9870 }
9871 else
9872 {
9873 *attr = hl_attr(HLF_SNC);
9874 fill = fill_stlnc;
9875 }
9876 /* Use fill when there is highlighting, and highlighting of current
9877 * window differs, or the fillchars differ, or this is not the
9878 * current window */
9879 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9880 || !is_curwin || firstwin == lastwin)
9881 || (fill_stl != fill_stlnc)))
9882 return fill;
9883 if (is_curwin)
9884 return '^';
9885 return '=';
9886}
9887#endif
9888
9889#ifdef FEAT_VERTSPLIT
9890/*
9891 * Get the character to use in a separator between vertically split windows.
9892 * Get its attributes in "*attr".
9893 */
9894 static int
9895fillchar_vsep(attr)
9896 int *attr;
9897{
9898 *attr = hl_attr(HLF_C);
9899 if (*attr == 0 && fill_vert == ' ')
9900 return '|';
9901 else
9902 return fill_vert;
9903}
9904#endif
9905
9906/*
9907 * Return TRUE if redrawing should currently be done.
9908 */
9909 int
9910redrawing()
9911{
9912 return (!RedrawingDisabled
9913 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9914}
9915
9916/*
9917 * Return TRUE if printing messages should currently be done.
9918 */
9919 int
9920messaging()
9921{
9922 return (!(p_lz && char_avail() && !KeyTyped));
9923}
9924
9925/*
9926 * Show current status info in ruler and various other places
9927 * If always is FALSE, only show ruler if position has changed.
9928 */
9929 void
9930showruler(always)
9931 int always;
9932{
9933 if (!always && !redrawing())
9934 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009935#ifdef FEAT_INS_EXPAND
9936 if (pum_visible())
9937 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009938# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009939 /* Don't redraw right now, do it later. */
9940 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009941# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009942 return;
9943 }
9944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009946 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009947 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009948 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 else
9951#endif
9952#ifdef FEAT_CMDL_INFO
9953 win_redr_ruler(curwin, always);
9954#endif
9955
9956#ifdef FEAT_TITLE
9957 if (need_maketitle
9958# ifdef FEAT_STL_OPT
9959 || (p_icon && (stl_syntax & STL_IN_ICON))
9960 || (p_title && (stl_syntax & STL_IN_TITLE))
9961# endif
9962 )
9963 maketitle();
9964#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009965#ifdef FEAT_WINDOWS
9966 /* Redraw the tab pages line if needed. */
9967 if (redraw_tabline)
9968 draw_tabline();
9969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970}
9971
9972#ifdef FEAT_CMDL_INFO
9973 static void
9974win_redr_ruler(wp, always)
9975 win_T *wp;
9976 int always;
9977{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009978#define RULER_BUF_LEN 70
9979 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 int row;
9981 int fillchar;
9982 int attr;
9983 int empty_line = FALSE;
9984 colnr_T virtcol;
9985 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009986 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 int o;
9988#ifdef FEAT_VERTSPLIT
9989 int this_ru_col;
9990 int off = 0;
9991 int width = Columns;
9992# define WITH_OFF(x) x
9993# define WITH_WIDTH(x) x
9994#else
9995# define WITH_OFF(x) 0
9996# define WITH_WIDTH(x) Columns
9997# define this_ru_col ru_col
9998#endif
9999
10000 /* If 'ruler' off or redrawing disabled, don't do anything */
10001 if (!p_ru)
10002 return;
10003
10004 /*
10005 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10006 * after deleting lines, before cursor.lnum is corrected.
10007 */
10008 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10009 return;
10010
10011#ifdef FEAT_INS_EXPAND
10012 /* Don't draw the ruler while doing insert-completion, it might overwrite
10013 * the (long) mode message. */
10014# ifdef FEAT_WINDOWS
10015 if (wp == lastwin && lastwin->w_status_height == 0)
10016# endif
10017 if (edit_submode != NULL)
10018 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010019 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10020 if (pum_visible())
10021 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022#endif
10023
10024#ifdef FEAT_STL_OPT
10025 if (*p_ruf)
10026 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010027 int save_called_emsg = called_emsg;
10028
10029 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010031 if (called_emsg)
10032 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010033 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010034 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 return;
10036 }
10037#endif
10038
10039 /*
10040 * Check if not in Insert mode and the line is empty (will show "0-1").
10041 */
10042 if (!(State & INSERT)
10043 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10044 empty_line = TRUE;
10045
10046 /*
10047 * Only draw the ruler when something changed.
10048 */
10049 validate_virtcol_win(wp);
10050 if ( redraw_cmdline
10051 || always
10052 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10053 || wp->w_cursor.col != wp->w_ru_cursor.col
10054 || wp->w_virtcol != wp->w_ru_virtcol
10055#ifdef FEAT_VIRTUALEDIT
10056 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10057#endif
10058 || wp->w_topline != wp->w_ru_topline
10059 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10060#ifdef FEAT_DIFF
10061 || wp->w_topfill != wp->w_ru_topfill
10062#endif
10063 || empty_line != wp->w_ru_empty)
10064 {
10065 cursor_off();
10066#ifdef FEAT_WINDOWS
10067 if (wp->w_status_height)
10068 {
10069 row = W_WINROW(wp) + wp->w_height;
10070 fillchar = fillchar_status(&attr, wp == curwin);
10071# ifdef FEAT_VERTSPLIT
10072 off = W_WINCOL(wp);
10073 width = W_WIDTH(wp);
10074# endif
10075 }
10076 else
10077#endif
10078 {
10079 row = Rows - 1;
10080 fillchar = ' ';
10081 attr = 0;
10082#ifdef FEAT_VERTSPLIT
10083 width = Columns;
10084 off = 0;
10085#endif
10086 }
10087
10088 /* In list mode virtcol needs to be recomputed */
10089 virtcol = wp->w_virtcol;
10090 if (wp->w_p_list && lcs_tab1 == NUL)
10091 {
10092 wp->w_p_list = FALSE;
10093 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10094 wp->w_p_list = TRUE;
10095 }
10096
10097 /*
10098 * Some sprintfs return the length, some return a pointer.
10099 * To avoid portability problems we use strlen() here.
10100 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010101 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10103 ? 0L
10104 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010105 len = STRLEN(buffer);
10106 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10108 (int)virtcol + 1);
10109
10110 /*
10111 * Add a "50%" if there is room for it.
10112 * On the last line, don't print in the last column (scrolls the
10113 * screen up on some terminals).
10114 */
10115 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010116 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 o = i + vim_strsize(buffer + i + 1);
10118#ifdef FEAT_WINDOWS
10119 if (wp->w_status_height == 0) /* can't use last char of screen */
10120#endif
10121 ++o;
10122#ifdef FEAT_VERTSPLIT
10123 this_ru_col = ru_col - (Columns - width);
10124 if (this_ru_col < 0)
10125 this_ru_col = 0;
10126#endif
10127 /* Never use more than half the window/screen width, leave the other
10128 * half for the filename. */
10129 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10130 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10131 if (this_ru_col + o < WITH_WIDTH(width))
10132 {
10133 while (this_ru_col + o < WITH_WIDTH(width))
10134 {
10135#ifdef FEAT_MBYTE
10136 if (has_mbyte)
10137 i += (*mb_char2bytes)(fillchar, buffer + i);
10138 else
10139#endif
10140 buffer[i++] = fillchar;
10141 ++o;
10142 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010143 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 }
10145 /* Truncate at window boundary. */
10146#ifdef FEAT_MBYTE
10147 if (has_mbyte)
10148 {
10149 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010150 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 {
10152 o += (*mb_ptr2cells)(buffer + i);
10153 if (this_ru_col + o > WITH_WIDTH(width))
10154 {
10155 buffer[i] = NUL;
10156 break;
10157 }
10158 }
10159 }
10160 else
10161#endif
10162 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10163 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10164
10165 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10166 i = redraw_cmdline;
10167 screen_fill(row, row + 1,
10168 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10169 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10170 fillchar, fillchar, attr);
10171 /* don't redraw the cmdline because of showing the ruler */
10172 redraw_cmdline = i;
10173 wp->w_ru_cursor = wp->w_cursor;
10174 wp->w_ru_virtcol = wp->w_virtcol;
10175 wp->w_ru_empty = empty_line;
10176 wp->w_ru_topline = wp->w_topline;
10177 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10178#ifdef FEAT_DIFF
10179 wp->w_ru_topfill = wp->w_topfill;
10180#endif
10181 }
10182}
10183#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010184
10185#if defined(FEAT_LINEBREAK) || defined(PROTO)
10186/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010187 * Return the width of the 'number' and 'relativenumber' column.
10188 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010189 * Otherwise it depends on 'numberwidth' and the line count.
10190 */
10191 int
10192number_width(wp)
10193 win_T *wp;
10194{
10195 int n;
10196 linenr_T lnum;
10197
Bram Moolenaar64486672010-05-16 15:46:46 +020010198 if (wp->w_p_nu)
10199 /* 'number' */
10200 lnum = wp->w_buffer->b_ml.ml_line_count;
10201 else
10202 /* 'relativenumber' */
10203 lnum = wp->w_height;
10204
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010205 if (lnum == wp->w_nrwidth_line_count)
10206 return wp->w_nrwidth_width;
10207 wp->w_nrwidth_line_count = lnum;
10208
10209 n = 0;
10210 do
10211 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010212 lnum /= 10;
10213 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010214 } while (lnum > 0);
10215
10216 /* 'numberwidth' gives the minimal width plus one */
10217 if (n < wp->w_p_nuw - 1)
10218 n = wp->w_p_nuw - 1;
10219
10220 wp->w_nrwidth_width = n;
10221 return n;
10222}
10223#endif