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