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