blob: 79980e90fdb8a4ddd436e60da56acace8a2f0619 [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
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000030 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
34 *
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
37 *
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000040 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 *
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
47 *
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
51 *
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
56 *
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
63 *
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
68 *
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
72 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000073 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
77 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
80 *
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
83 *
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
87 */
88
89#include "vim.h"
90
91/*
92 * The attributes that are actually active for writing to the screen.
93 */
94static int screen_attr = 0;
95
96/*
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
99 */
100static int screen_cur_row, screen_cur_col; /* last known cursor position */
101
102#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
106#ifdef FEAT_FOLDING
107static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
108#endif
109
110/*
111 * Buffer for one screen line (characters and attributes).
112 */
113static schar_T *current_ScreenLine;
114
115static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000116static 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 +0000117#ifdef FEAT_FOLDING
118static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
119static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
120static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
121#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000122static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
124#ifdef FEAT_RIGHTLEFT
125static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
126# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
127#else
128static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
129# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#ifdef FEAT_VERTSPLIT
132static void draw_vsep_win __ARGS((win_T *wp, int row));
133#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000135static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000138#define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static void start_search_hl __ARGS((void));
140static void end_search_hl __ARGS((void));
141static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
142static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
143#endif
144static void screen_start_highlight __ARGS((int attr));
145static void screen_char __ARGS((unsigned off, int row, int col));
146#ifdef FEAT_MBYTE
147static void screen_char_2 __ARGS((unsigned off, int row, int col));
148#endif
149static void screenclear2 __ARGS((void));
150static void lineclear __ARGS((unsigned off, int width));
151static void lineinvalid __ARGS((unsigned off, int width));
152#ifdef FEAT_VERTSPLIT
153static void linecopy __ARGS((int to, int from, win_T *wp));
154static void redraw_block __ARGS((int row, int end, win_T *wp));
155#endif
156static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
157static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000159#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000160static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
163static int fillchar_status __ARGS((int *attr, int is_curwin));
164#endif
165#ifdef FEAT_VERTSPLIT
166static int fillchar_vsep __ARGS((int *attr));
167#endif
168#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000169static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#endif
171#ifdef FEAT_CMDL_INFO
172static void win_redr_ruler __ARGS((win_T *wp, int always));
173#endif
174
175#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
176/* Ugly global: overrule attribute used by screen_char() */
177static int screen_char_attr = 0;
178#endif
179
180/*
181 * Redraw the current window later, with update_screen(type).
182 * Set must_redraw only if not already set to a higher value.
183 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
184 */
185 void
186redraw_later(type)
187 int type;
188{
189 redraw_win_later(curwin, type);
190}
191
192 void
193redraw_win_later(wp, type)
194 win_T *wp;
195 int type;
196{
197 if (wp->w_redr_type < type)
198 {
199 wp->w_redr_type = type;
200 if (type >= NOT_VALID)
201 wp->w_lines_valid = 0;
202 if (must_redraw < type) /* must_redraw is the maximum of all windows */
203 must_redraw = type;
204 }
205}
206
207/*
208 * Force a complete redraw later. Also resets the highlighting. To be used
209 * after executing a shell command that messes up the screen.
210 */
211 void
212redraw_later_clear()
213{
214 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000215#ifdef FEAT_GUI
216 if (gui.in_use)
217 /* Use a code that will reset gui.highlight_mask in
218 * gui_stop_highlight(). */
219 screen_attr = HL_ALL + 1;
220 else
221#endif
222 /* Use attributes that is very unlikely to appear in text. */
223 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224}
225
226/*
227 * Mark all windows to be redrawn later.
228 */
229 void
230redraw_all_later(type)
231 int type;
232{
233 win_T *wp;
234
235 FOR_ALL_WINDOWS(wp)
236 {
237 redraw_win_later(wp, type);
238 }
239}
240
241/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000242 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 */
244 void
245redraw_curbuf_later(type)
246 int type;
247{
248 redraw_buf_later(curbuf, type);
249}
250
251 void
252redraw_buf_later(buf, type)
253 buf_T *buf;
254 int type;
255{
256 win_T *wp;
257
258 FOR_ALL_WINDOWS(wp)
259 {
260 if (wp->w_buffer == buf)
261 redraw_win_later(wp, type);
262 }
263}
264
265/*
266 * Changed something in the current window, at buffer line "lnum", that
267 * requires that line and possibly other lines to be redrawn.
268 * Used when entering/leaving Insert mode with the cursor on a folded line.
269 * Used to remove the "$" from a change command.
270 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
271 * may become invalid and the whole window will have to be redrawn.
272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 void
274redrawWinline(lnum, invalid)
275 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000276 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277{
278#ifdef FEAT_FOLDING
279 int i;
280#endif
281
282 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
283 curwin->w_redraw_top = lnum;
284 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
285 curwin->w_redraw_bot = lnum;
286 redraw_later(VALID);
287
288#ifdef FEAT_FOLDING
289 if (invalid)
290 {
291 /* A w_lines[] entry for this lnum has become invalid. */
292 i = find_wl_entry(curwin, lnum);
293 if (i >= 0)
294 curwin->w_lines[i].wl_valid = FALSE;
295 }
296#endif
297}
298
299/*
300 * update all windows that are editing the current buffer
301 */
302 void
303update_curbuf(type)
304 int type;
305{
306 redraw_curbuf_later(type);
307 update_screen(type);
308}
309
310/*
311 * update_screen()
312 *
313 * Based on the current value of curwin->w_topline, transfer a screenfull
314 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
315 */
316 void
317update_screen(type)
318 int type;
319{
320 win_T *wp;
321 static int did_intro = FALSE;
322#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
323 int did_one;
324#endif
325
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000326 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 if (!screen_valid(TRUE))
328 return;
329
330 if (must_redraw)
331 {
332 if (type < must_redraw) /* use maximal type */
333 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000334
335 /* must_redraw is reset here, so that when we run into some weird
336 * reason to redraw while busy redrawing (e.g., asynchronous
337 * scrolling), or update_topline() in win_update() will cause a
338 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 must_redraw = 0;
340 }
341
342 /* Need to update w_lines[]. */
343 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
344 type = NOT_VALID;
345
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000346 /* Postpone the redrawing when it's not needed and when being called
347 * recursively. */
348 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {
350 redraw_later(type); /* remember type for next time */
351 must_redraw = type;
352 if (type > INVERTED_ALL)
353 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
354 return;
355 }
356
357 updating_screen = TRUE;
358#ifdef FEAT_SYN_HL
359 ++display_tick; /* let syntax code know we're in a next round of
360 * display updating */
361#endif
362
363 /*
364 * if the screen was scrolled up when displaying a message, scroll it down
365 */
366 if (msg_scrolled)
367 {
368 clear_cmdline = TRUE;
369 if (msg_scrolled > Rows - 5) /* clearing is faster */
370 type = CLEAR;
371 else if (type != CLEAR)
372 {
373 check_for_delay(FALSE);
374 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
375 type = CLEAR;
376 FOR_ALL_WINDOWS(wp)
377 {
378 if (W_WINROW(wp) < msg_scrolled)
379 {
380 if (W_WINROW(wp) + wp->w_height > msg_scrolled
381 && wp->w_redr_type < REDRAW_TOP
382 && wp->w_lines_valid > 0
383 && wp->w_topline == wp->w_lines[0].wl_lnum)
384 {
385 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
386 wp->w_redr_type = REDRAW_TOP;
387 }
388 else
389 {
390 wp->w_redr_type = NOT_VALID;
391#ifdef FEAT_WINDOWS
392 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
393 <= msg_scrolled)
394 wp->w_redr_status = TRUE;
395#endif
396 }
397 }
398 }
399 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000401 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 }
404 msg_scrolled = 0;
405 need_wait_return = FALSE;
406 }
407
408 /* reset cmdline_row now (may have been changed temporarily) */
409 compute_cmdrow();
410
411 /* Check for changed highlighting */
412 if (need_highlight_changed)
413 highlight_changed();
414
415 if (type == CLEAR) /* first clear screen */
416 {
417 screenclear(); /* will reset clear_cmdline */
418 type = NOT_VALID;
419 }
420
421 if (clear_cmdline) /* going to clear cmdline (done below) */
422 check_for_delay(FALSE);
423
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000424#ifdef FEAT_LINEBREAK
425 /* Force redraw when width of 'number' column changes. */
426 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000427 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000428 curwin->w_redr_type = NOT_VALID;
429#endif
430
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 /*
432 * Only start redrawing if there is really something to do.
433 */
434 if (type == INVERTED)
435 update_curswant();
436 if (curwin->w_redr_type < type
437 && !((type == VALID
438 && curwin->w_lines[0].wl_valid
439#ifdef FEAT_DIFF
440 && curwin->w_topfill == curwin->w_old_topfill
441 && curwin->w_botfill == curwin->w_old_botfill
442#endif
443 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
444#ifdef FEAT_VISUAL
445 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000446 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
448 && curwin->w_old_visual_mode == VIsual_mode
449 && (curwin->w_valid & VALID_VIRTCOL)
450 && curwin->w_old_curswant == curwin->w_curswant)
451#endif
452 ))
453 curwin->w_redr_type = type;
454
Bram Moolenaar5a305422006-04-28 22:38:25 +0000455#ifdef FEAT_WINDOWS
456 /* Redraw the tab pages line if needed. */
457 if (redraw_tabline || type >= NOT_VALID)
458 draw_tabline();
459#endif
460
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461#ifdef FEAT_SYN_HL
462 /*
463 * Correct stored syntax highlighting info for changes in each displayed
464 * buffer. Each buffer must only be done once.
465 */
466 FOR_ALL_WINDOWS(wp)
467 {
468 if (wp->w_buffer->b_mod_set)
469 {
470# ifdef FEAT_WINDOWS
471 win_T *wwp;
472
473 /* Check if we already did this buffer. */
474 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
475 if (wwp->w_buffer == wp->w_buffer)
476 break;
477# endif
478 if (
479# ifdef FEAT_WINDOWS
480 wwp == wp &&
481# endif
482 syntax_present(wp->w_buffer))
483 syn_stack_apply_changes(wp->w_buffer);
484 }
485 }
486#endif
487
488 /*
489 * Go from top to bottom through the windows, redrawing the ones that need
490 * it.
491 */
492#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
493 did_one = FALSE;
494#endif
495#ifdef FEAT_SEARCH_EXTRA
496 search_hl.rm.regprog = NULL;
497#endif
498 FOR_ALL_WINDOWS(wp)
499 {
500 if (wp->w_redr_type != 0)
501 {
502 cursor_off();
503#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
504 if (!did_one)
505 {
506 did_one = TRUE;
507# ifdef FEAT_SEARCH_EXTRA
508 start_search_hl();
509# endif
510# ifdef FEAT_CLIPBOARD
511 /* When Visual area changed, may have to update selection. */
512 if (clip_star.available && clip_isautosel())
513 clip_update_selection();
514# endif
515#ifdef FEAT_GUI
516 /* Remove the cursor before starting to do anything, because
517 * scrolling may make it difficult to redraw the text under
518 * it. */
519 if (gui.in_use)
520 gui_undraw_cursor();
521#endif
522 }
523#endif
524 win_update(wp);
525 }
526
527#ifdef FEAT_WINDOWS
528 /* redraw status line after the window to minimize cursor movement */
529 if (wp->w_redr_status)
530 {
531 cursor_off();
532 win_redr_status(wp);
533 }
534#endif
535 }
536#if defined(FEAT_SEARCH_EXTRA)
537 end_search_hl();
538#endif
539
540#ifdef FEAT_WINDOWS
541 /* Reset b_mod_set flags. Going through all windows is probably faster
542 * than going through all buffers (there could be many buffers). */
543 for (wp = firstwin; wp != NULL; wp = wp->w_next)
544 wp->w_buffer->b_mod_set = FALSE;
545#else
546 curbuf->b_mod_set = FALSE;
547#endif
548
549 updating_screen = FALSE;
550#ifdef FEAT_GUI
551 gui_may_resize_shell();
552#endif
553
554 /* Clear or redraw the command line. Done last, because scrolling may
555 * mess up the command line. */
556 if (clear_cmdline || redraw_cmdline)
557 showmode();
558
559 /* May put up an introductory message when not editing a file */
560 if (!did_intro && bufempty()
561 && curbuf->b_fname == NULL
562#ifdef FEAT_WINDOWS
563 && firstwin->w_next == NULL
564#endif
565 && vim_strchr(p_shm, SHM_INTRO) == NULL)
566 intro_message(FALSE);
567 did_intro = TRUE;
568
569#ifdef FEAT_GUI
570 /* Redraw the cursor and update the scrollbars when all screen updating is
571 * done. */
572 if (gui.in_use)
573 {
574 out_flush(); /* required before updating the cursor */
575 if (did_one)
576 gui_update_cursor(FALSE, FALSE);
577 gui_update_scrollbars(FALSE);
578 }
579#endif
580}
581
582#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
583static void update_prepare __ARGS((void));
584static void update_finish __ARGS((void));
585
586/*
587 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000588 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 */
590 static void
591update_prepare()
592{
593 cursor_off();
594 updating_screen = TRUE;
595#ifdef FEAT_GUI
596 /* Remove the cursor before starting to do anything, because scrolling may
597 * make it difficult to redraw the text under it. */
598 if (gui.in_use)
599 gui_undraw_cursor();
600#endif
601#ifdef FEAT_SEARCH_EXTRA
602 start_search_hl();
603#endif
604}
605
606/*
607 * Finish updating one or more windows.
608 */
609 static void
610update_finish()
611{
612 if (redraw_cmdline)
613 showmode();
614
615# ifdef FEAT_SEARCH_EXTRA
616 end_search_hl();
617# endif
618
619 updating_screen = FALSE;
620
621# ifdef FEAT_GUI
622 gui_may_resize_shell();
623
624 /* Redraw the cursor and update the scrollbars when all screen updating is
625 * done. */
626 if (gui.in_use)
627 {
628 out_flush(); /* required before updating the cursor */
629 gui_update_cursor(FALSE, FALSE);
630 gui_update_scrollbars(FALSE);
631 }
632# endif
633}
634#endif
635
636#if defined(FEAT_SIGNS) || defined(PROTO)
637 void
638update_debug_sign(buf, lnum)
639 buf_T *buf;
640 linenr_T lnum;
641{
642 win_T *wp;
643 int doit = FALSE;
644
645# ifdef FEAT_FOLDING
646 win_foldinfo.fi_level = 0;
647# endif
648
649 /* update/delete a specific mark */
650 FOR_ALL_WINDOWS(wp)
651 {
652 if (buf != NULL && lnum > 0)
653 {
654 if (wp->w_buffer == buf && lnum >= wp->w_topline
655 && lnum < wp->w_botline)
656 {
657 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
658 wp->w_redraw_top = lnum;
659 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
660 wp->w_redraw_bot = lnum;
661 redraw_win_later(wp, VALID);
662 }
663 }
664 else
665 redraw_win_later(wp, VALID);
666 if (wp->w_redr_type != 0)
667 doit = TRUE;
668 }
669
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000670 /* Return when there is nothing to do or screen updating already
671 * happening. */
672 if (!doit || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 return;
674
675 /* update all windows that need updating */
676 update_prepare();
677
678# ifdef FEAT_WINDOWS
679 for (wp = firstwin; wp; wp = wp->w_next)
680 {
681 if (wp->w_redr_type != 0)
682 win_update(wp);
683 if (wp->w_redr_status)
684 win_redr_status(wp);
685 }
686# else
687 if (curwin->w_redr_type != 0)
688 win_update(curwin);
689# endif
690
691 update_finish();
692}
693#endif
694
695
696#if defined(FEAT_GUI) || defined(PROTO)
697/*
698 * Update a single window, its status line and maybe the command line msg.
699 * Used for the GUI scrollbar.
700 */
701 void
702updateWindow(wp)
703 win_T *wp;
704{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000705 /* return if already busy updating */
706 if (updating_screen)
707 return;
708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 update_prepare();
710
711#ifdef FEAT_CLIPBOARD
712 /* When Visual area changed, may have to update selection. */
713 if (clip_star.available && clip_isautosel())
714 clip_update_selection();
715#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000720 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000721 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000722 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000723
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 if (wp->w_redr_status
725# ifdef FEAT_CMDL_INFO
726 || p_ru
727# endif
728# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000729 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730# endif
731 )
732 win_redr_status(wp);
733#endif
734
735 update_finish();
736}
737#endif
738
739/*
740 * Update a single window.
741 *
742 * This may cause the windows below it also to be redrawn (when clearing the
743 * screen or scrolling lines).
744 *
745 * How the window is redrawn depends on wp->w_redr_type. Each type also
746 * implies the one below it.
747 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000748 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
750 * INVERTED redraw the changed part of the Visual area
751 * INVERTED_ALL redraw the whole Visual area
752 * VALID 1. scroll up/down to adjust for a changed w_topline
753 * 2. update lines at the top when scrolled down
754 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000755 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 * b_mod_top and b_mod_bot.
757 * - if wp->w_redraw_top non-zero, redraw lines between
758 * wp->w_redraw_top and wp->w_redr_bot.
759 * - continue redrawing when syntax status is invalid.
760 * 4. if scrolled up, update lines at the bottom.
761 * This results in three areas that may need updating:
762 * top: from first row to top_end (when scrolled down)
763 * mid: from mid_start to mid_end (update inversion or changed text)
764 * bot: from bot_start to last row (when scrolled up)
765 */
766 static void
767win_update(wp)
768 win_T *wp;
769{
770 buf_T *buf = wp->w_buffer;
771 int type;
772 int top_end = 0; /* Below last row of the top area that needs
773 updating. 0 when no top area updating. */
774 int mid_start = 999;/* first row of the mid area that needs
775 updating. 999 when no mid area updating. */
776 int mid_end = 0; /* Below last row of the mid area that needs
777 updating. 0 when no mid area updating. */
778 int bot_start = 999;/* first row of the bot area that needs
779 updating. 999 when no bot area updating */
780#ifdef FEAT_VISUAL
781 int scrolled_down = FALSE; /* TRUE when scrolled down when
782 w_topline got smaller a bit */
783#endif
784#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000785 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 int top_to_mod = FALSE; /* redraw above mod_top */
787#endif
788
789 int row; /* current window row to display */
790 linenr_T lnum; /* current buffer lnum to display */
791 int idx; /* current index in w_lines[] */
792 int srow; /* starting row of the current line */
793
794 int eof = FALSE; /* if TRUE, we hit the end of the file */
795 int didline = FALSE; /* if TRUE, we finished the last line */
796 int i;
797 long j;
798 static int recursive = FALSE; /* being called recursively */
799 int old_botline = wp->w_botline;
800#ifdef FEAT_FOLDING
801 long fold_count;
802#endif
803#ifdef FEAT_SYN_HL
804 /* remember what happened to the previous line, to know if
805 * check_visual_highlight() can be used */
806#define DID_NONE 1 /* didn't update a line */
807#define DID_LINE 2 /* updated a normal line */
808#define DID_FOLD 3 /* updated a folded line */
809 int did_update = DID_NONE;
810 linenr_T syntax_last_parsed = 0; /* last parsed text line */
811#endif
812 linenr_T mod_top = 0;
813 linenr_T mod_bot = 0;
814#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
815 int save_got_int;
816#endif
817
818 type = wp->w_redr_type;
819
820 if (type == NOT_VALID)
821 {
822#ifdef FEAT_WINDOWS
823 wp->w_redr_status = TRUE;
824#endif
825 wp->w_lines_valid = 0;
826 }
827
828 /* Window is zero-height: nothing to draw. */
829 if (wp->w_height == 0)
830 {
831 wp->w_redr_type = 0;
832 return;
833 }
834
835#ifdef FEAT_VERTSPLIT
836 /* Window is zero-width: Only need to draw the separator. */
837 if (wp->w_width == 0)
838 {
839 /* draw the vertical separator right of this window */
840 draw_vsep_win(wp, 0);
841 wp->w_redr_type = 0;
842 return;
843 }
844#endif
845
846#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000847 /* Setup for match and 'hlsearch' highlighting. Disable any previous
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000848 * match */
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000849 cur = wp->w_match_head;
850 while (cur != NULL)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000851 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000852 cur->hl.rm = cur->match;
853 if (cur->hlg_id == 0)
854 cur->hl.attr = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000855 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000856 cur->hl.attr = syn_id2attr(cur->hlg_id);
857 cur->hl.buf = buf;
858 cur->hl.lnum = 0;
859 cur->hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000860# ifdef FEAT_RELTIME
861 /* Set the time limit to 'redrawtime'. */
862 profile_setlimit(p_rdt, &(cur->hl.tm));
863# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000864 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 search_hl.buf = buf;
867 search_hl.lnum = 0;
868 search_hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000869 /* time limit is set at the toplevel, for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#endif
871
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000872#ifdef FEAT_LINEBREAK
873 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000874 i = wp->w_p_nu ? number_width(wp) : 0;
875 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000876 {
877 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000878 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000879 }
880 else
881#endif
882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
884 {
885 /*
886 * When there are both inserted/deleted lines and specific lines to be
887 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
888 * everything (only happens when redrawing is off for while).
889 */
890 type = NOT_VALID;
891 }
892 else
893 {
894 /*
895 * Set mod_top to the first line that needs displaying because of
896 * changes. Set mod_bot to the first line after the changes.
897 */
898 mod_top = wp->w_redraw_top;
899 if (wp->w_redraw_bot != 0)
900 mod_bot = wp->w_redraw_bot + 1;
901 else
902 mod_bot = 0;
903 wp->w_redraw_top = 0; /* reset for next time */
904 wp->w_redraw_bot = 0;
905 if (buf->b_mod_set)
906 {
907 if (mod_top == 0 || mod_top > buf->b_mod_top)
908 {
909 mod_top = buf->b_mod_top;
910#ifdef FEAT_SYN_HL
911 /* Need to redraw lines above the change that may be included
912 * in a pattern match. */
913 if (syntax_present(buf))
914 {
915 mod_top -= buf->b_syn_sync_linebreaks;
916 if (mod_top < 1)
917 mod_top = 1;
918 }
919#endif
920 }
921 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
922 mod_bot = buf->b_mod_bot;
923
924#ifdef FEAT_SEARCH_EXTRA
925 /* When 'hlsearch' is on and using a multi-line search pattern, a
926 * change in one line may make the Search highlighting in a
927 * previous line invalid. Simple solution: redraw all visible
928 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000929 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000931 if (search_hl.rm.regprog != NULL
932 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000934 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000935 {
936 cur = wp->w_match_head;
937 while (cur != NULL)
938 {
939 if (cur->match.regprog != NULL
940 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000941 {
942 top_to_mod = TRUE;
943 break;
944 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000945 cur = cur->next;
946 }
947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#endif
949 }
950#ifdef FEAT_FOLDING
951 if (mod_top != 0 && hasAnyFolding(wp))
952 {
953 linenr_T lnumt, lnumb;
954
955 /*
956 * A change in a line can cause lines above it to become folded or
957 * unfolded. Find the top most buffer line that may be affected.
958 * If the line was previously folded and displayed, get the first
959 * line of that fold. If the line is folded now, get the first
960 * folded line. Use the minimum of these two.
961 */
962
963 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
964 * the line below it. If there is no valid entry, use w_topline.
965 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
966 * to this line. If there is no valid entry, use MAXLNUM. */
967 lnumt = wp->w_topline;
968 lnumb = MAXLNUM;
969 for (i = 0; i < wp->w_lines_valid; ++i)
970 if (wp->w_lines[i].wl_valid)
971 {
972 if (wp->w_lines[i].wl_lastlnum < mod_top)
973 lnumt = wp->w_lines[i].wl_lastlnum + 1;
974 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
975 {
976 lnumb = wp->w_lines[i].wl_lnum;
977 /* When there is a fold column it might need updating
978 * in the next line ("J" just above an open fold). */
979 if (wp->w_p_fdc > 0)
980 ++lnumb;
981 }
982 }
983
984 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
985 if (mod_top > lnumt)
986 mod_top = lnumt;
987
988 /* Now do the same for the bottom line (one above mod_bot). */
989 --mod_bot;
990 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
991 ++mod_bot;
992 if (mod_bot < lnumb)
993 mod_bot = lnumb;
994 }
995#endif
996
997 /* When a change starts above w_topline and the end is below
998 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000999 * If the end of the change is above w_topline: do like no change was
1000 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 if (mod_top != 0 && mod_top < wp->w_topline)
1002 {
1003 if (mod_bot > wp->w_topline)
1004 mod_top = wp->w_topline;
1005#ifdef FEAT_SYN_HL
1006 else if (syntax_present(buf))
1007 top_end = 1;
1008#endif
1009 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001010
1011 /* When line numbers are displayed need to redraw all lines below
1012 * inserted/deleted lines. */
1013 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1014 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016
1017 /*
1018 * When only displaying the lines at the top, set top_end. Used when
1019 * window has scrolled down for msg_scrolled.
1020 */
1021 if (type == REDRAW_TOP)
1022 {
1023 j = 0;
1024 for (i = 0; i < wp->w_lines_valid; ++i)
1025 {
1026 j += wp->w_lines[i].wl_size;
1027 if (j >= wp->w_upd_rows)
1028 {
1029 top_end = j;
1030 break;
1031 }
1032 }
1033 if (top_end == 0)
1034 /* not found (cannot happen?): redraw everything */
1035 type = NOT_VALID;
1036 else
1037 /* top area defined, the rest is VALID */
1038 type = VALID;
1039 }
1040
Bram Moolenaar367329b2007-08-30 11:53:22 +00001041 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001042 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1043 * non-zero and thus not FALSE) will indicate that screenclear() was not
1044 * called. */
1045 if (screen_cleared)
1046 screen_cleared = MAYBE;
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 /*
1049 * If there are no changes on the screen that require a complete redraw,
1050 * handle three cases:
1051 * 1: we are off the top of the screen by a few lines: scroll down
1052 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1053 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1054 * w_lines[] that needs updating.
1055 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001056 if ((type == VALID || type == SOME_VALID
1057 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058#ifdef FEAT_DIFF
1059 && !wp->w_botfill && !wp->w_old_botfill
1060#endif
1061 )
1062 {
1063 if (mod_top != 0 && wp->w_topline == mod_top)
1064 {
1065 /*
1066 * w_topline is the first changed line, the scrolling will be done
1067 * further down.
1068 */
1069 }
1070 else if (wp->w_lines[0].wl_valid
1071 && (wp->w_topline < wp->w_lines[0].wl_lnum
1072#ifdef FEAT_DIFF
1073 || (wp->w_topline == wp->w_lines[0].wl_lnum
1074 && wp->w_topfill > wp->w_old_topfill)
1075#endif
1076 ))
1077 {
1078 /*
1079 * New topline is above old topline: May scroll down.
1080 */
1081#ifdef FEAT_FOLDING
1082 if (hasAnyFolding(wp))
1083 {
1084 linenr_T ln;
1085
1086 /* count the number of lines we are off, counting a sequence
1087 * of folded lines as one */
1088 j = 0;
1089 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1090 {
1091 ++j;
1092 if (j >= wp->w_height - 2)
1093 break;
1094 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1095 }
1096 }
1097 else
1098#endif
1099 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1100 if (j < wp->w_height - 2) /* not too far off */
1101 {
1102 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1103#ifdef FEAT_DIFF
1104 /* insert extra lines for previously invisible filler lines */
1105 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1106 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1107 - wp->w_old_topfill;
1108#endif
1109 if (i < wp->w_height - 2) /* less than a screen off */
1110 {
1111 /*
1112 * Try to insert the correct number of lines.
1113 * If not the last window, delete the lines at the bottom.
1114 * win_ins_lines may fail when the terminal can't do it.
1115 */
1116 if (i > 0)
1117 check_for_delay(FALSE);
1118 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1119 {
1120 if (wp->w_lines_valid != 0)
1121 {
1122 /* Need to update rows that are new, stop at the
1123 * first one that scrolled down. */
1124 top_end = i;
1125#ifdef FEAT_VISUAL
1126 scrolled_down = TRUE;
1127#endif
1128
1129 /* Move the entries that were scrolled, disable
1130 * the entries for the lines to be redrawn. */
1131 if ((wp->w_lines_valid += j) > wp->w_height)
1132 wp->w_lines_valid = wp->w_height;
1133 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1134 wp->w_lines[idx] = wp->w_lines[idx - j];
1135 while (idx >= 0)
1136 wp->w_lines[idx--].wl_valid = FALSE;
1137 }
1138 }
1139 else
1140 mid_start = 0; /* redraw all lines */
1141 }
1142 else
1143 mid_start = 0; /* redraw all lines */
1144 }
1145 else
1146 mid_start = 0; /* redraw all lines */
1147 }
1148 else
1149 {
1150 /*
1151 * New topline is at or below old topline: May scroll up.
1152 * When topline didn't change, find first entry in w_lines[] that
1153 * needs updating.
1154 */
1155
1156 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1157 j = -1;
1158 row = 0;
1159 for (i = 0; i < wp->w_lines_valid; i++)
1160 {
1161 if (wp->w_lines[i].wl_valid
1162 && wp->w_lines[i].wl_lnum == wp->w_topline)
1163 {
1164 j = i;
1165 break;
1166 }
1167 row += wp->w_lines[i].wl_size;
1168 }
1169 if (j == -1)
1170 {
1171 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1172 * lines */
1173 mid_start = 0;
1174 }
1175 else
1176 {
1177 /*
1178 * Try to delete the correct number of lines.
1179 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1180 */
1181#ifdef FEAT_DIFF
1182 /* If the topline didn't change, delete old filler lines,
1183 * otherwise delete filler lines of the new topline... */
1184 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1185 row += wp->w_old_topfill;
1186 else
1187 row += diff_check_fill(wp, wp->w_topline);
1188 /* ... but don't delete new filler lines. */
1189 row -= wp->w_topfill;
1190#endif
1191 if (row > 0)
1192 {
1193 check_for_delay(FALSE);
1194 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1195 bot_start = wp->w_height - row;
1196 else
1197 mid_start = 0; /* redraw all lines */
1198 }
1199 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1200 {
1201 /*
1202 * Skip the lines (below the deleted lines) that are still
1203 * valid and don't need redrawing. Copy their info
1204 * upwards, to compensate for the deleted lines. Set
1205 * bot_start to the first row that needs redrawing.
1206 */
1207 bot_start = 0;
1208 idx = 0;
1209 for (;;)
1210 {
1211 wp->w_lines[idx] = wp->w_lines[j];
1212 /* stop at line that didn't fit, unless it is still
1213 * valid (no lines deleted) */
1214 if (row > 0 && bot_start + row
1215 + (int)wp->w_lines[j].wl_size > wp->w_height)
1216 {
1217 wp->w_lines_valid = idx + 1;
1218 break;
1219 }
1220 bot_start += wp->w_lines[idx++].wl_size;
1221
1222 /* stop at the last valid entry in w_lines[].wl_size */
1223 if (++j >= wp->w_lines_valid)
1224 {
1225 wp->w_lines_valid = idx;
1226 break;
1227 }
1228 }
1229#ifdef FEAT_DIFF
1230 /* Correct the first entry for filler lines at the top
1231 * when it won't get updated below. */
1232 if (wp->w_p_diff && bot_start > 0)
1233 wp->w_lines[0].wl_size =
1234 plines_win_nofill(wp, wp->w_topline, TRUE)
1235 + wp->w_topfill;
1236#endif
1237 }
1238 }
1239 }
1240
1241 /* When starting redraw in the first line, redraw all lines. When
1242 * there is only one window it's probably faster to clear the screen
1243 * first. */
1244 if (mid_start == 0)
1245 {
1246 mid_end = wp->w_height;
1247 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001248 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001249 /* Clear the screen when it was not done by win_del_lines() or
1250 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1251 * then. */
1252 if (screen_cleared != TRUE)
1253 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001254#ifdef FEAT_WINDOWS
1255 /* The screen was cleared, redraw the tab pages line. */
1256 if (redraw_tabline)
1257 draw_tabline();
1258#endif
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001261
1262 /* When win_del_lines() or win_ins_lines() caused the screen to be
1263 * cleared (only happens for the first window) or when screenclear()
1264 * was called directly above, "must_redraw" will have been set to
1265 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1266 if (screen_cleared == TRUE)
1267 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 else
1270 {
1271 /* Not VALID or INVERTED: redraw all lines. */
1272 mid_start = 0;
1273 mid_end = wp->w_height;
1274 }
1275
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001276 if (type == SOME_VALID)
1277 {
1278 /* SOME_VALID: redraw all lines. */
1279 mid_start = 0;
1280 mid_end = wp->w_height;
1281 type = NOT_VALID;
1282 }
1283
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284#ifdef FEAT_VISUAL
1285 /* check if we are updating or removing the inverted part */
1286 if ((VIsual_active && buf == curwin->w_buffer)
1287 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1288 {
1289 linenr_T from, to;
1290
1291 if (VIsual_active)
1292 {
1293 if (VIsual_active
1294 && (VIsual_mode != wp->w_old_visual_mode
1295 || type == INVERTED_ALL))
1296 {
1297 /*
1298 * If the type of Visual selection changed, redraw the whole
1299 * selection. Also when the ownership of the X selection is
1300 * gained or lost.
1301 */
1302 if (curwin->w_cursor.lnum < VIsual.lnum)
1303 {
1304 from = curwin->w_cursor.lnum;
1305 to = VIsual.lnum;
1306 }
1307 else
1308 {
1309 from = VIsual.lnum;
1310 to = curwin->w_cursor.lnum;
1311 }
1312 /* redraw more when the cursor moved as well */
1313 if (wp->w_old_cursor_lnum < from)
1314 from = wp->w_old_cursor_lnum;
1315 if (wp->w_old_cursor_lnum > to)
1316 to = wp->w_old_cursor_lnum;
1317 if (wp->w_old_visual_lnum < from)
1318 from = wp->w_old_visual_lnum;
1319 if (wp->w_old_visual_lnum > to)
1320 to = wp->w_old_visual_lnum;
1321 }
1322 else
1323 {
1324 /*
1325 * Find the line numbers that need to be updated: The lines
1326 * between the old cursor position and the current cursor
1327 * position. Also check if the Visual position changed.
1328 */
1329 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1330 {
1331 from = curwin->w_cursor.lnum;
1332 to = wp->w_old_cursor_lnum;
1333 }
1334 else
1335 {
1336 from = wp->w_old_cursor_lnum;
1337 to = curwin->w_cursor.lnum;
1338 if (from == 0) /* Visual mode just started */
1339 from = to;
1340 }
1341
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001342 if (VIsual.lnum != wp->w_old_visual_lnum
1343 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 {
1345 if (wp->w_old_visual_lnum < from
1346 && wp->w_old_visual_lnum != 0)
1347 from = wp->w_old_visual_lnum;
1348 if (wp->w_old_visual_lnum > to)
1349 to = wp->w_old_visual_lnum;
1350 if (VIsual.lnum < from)
1351 from = VIsual.lnum;
1352 if (VIsual.lnum > to)
1353 to = VIsual.lnum;
1354 }
1355 }
1356
1357 /*
1358 * If in block mode and changed column or curwin->w_curswant:
1359 * update all lines.
1360 * First compute the actual start and end column.
1361 */
1362 if (VIsual_mode == Ctrl_V)
1363 {
1364 colnr_T fromc, toc;
1365
1366 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1367 ++toc;
1368 if (curwin->w_curswant == MAXCOL)
1369 toc = MAXCOL;
1370
1371 if (fromc != wp->w_old_cursor_fcol
1372 || toc != wp->w_old_cursor_lcol)
1373 {
1374 if (from > VIsual.lnum)
1375 from = VIsual.lnum;
1376 if (to < VIsual.lnum)
1377 to = VIsual.lnum;
1378 }
1379 wp->w_old_cursor_fcol = fromc;
1380 wp->w_old_cursor_lcol = toc;
1381 }
1382 }
1383 else
1384 {
1385 /* Use the line numbers of the old Visual area. */
1386 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1387 {
1388 from = wp->w_old_cursor_lnum;
1389 to = wp->w_old_visual_lnum;
1390 }
1391 else
1392 {
1393 from = wp->w_old_visual_lnum;
1394 to = wp->w_old_cursor_lnum;
1395 }
1396 }
1397
1398 /*
1399 * There is no need to update lines above the top of the window.
1400 */
1401 if (from < wp->w_topline)
1402 from = wp->w_topline;
1403
1404 /*
1405 * If we know the value of w_botline, use it to restrict the update to
1406 * the lines that are visible in the window.
1407 */
1408 if (wp->w_valid & VALID_BOTLINE)
1409 {
1410 if (from >= wp->w_botline)
1411 from = wp->w_botline - 1;
1412 if (to >= wp->w_botline)
1413 to = wp->w_botline - 1;
1414 }
1415
1416 /*
1417 * Find the minimal part to be updated.
1418 * Watch out for scrolling that made entries in w_lines[] invalid.
1419 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1420 * top_end; need to redraw from top_end to the "to" line.
1421 * A middle mouse click with a Visual selection may change the text
1422 * above the Visual area and reset wl_valid, do count these for
1423 * mid_end (in srow).
1424 */
1425 if (mid_start > 0)
1426 {
1427 lnum = wp->w_topline;
1428 idx = 0;
1429 srow = 0;
1430 if (scrolled_down)
1431 mid_start = top_end;
1432 else
1433 mid_start = 0;
1434 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1435 {
1436 if (wp->w_lines[idx].wl_valid)
1437 mid_start += wp->w_lines[idx].wl_size;
1438 else if (!scrolled_down)
1439 srow += wp->w_lines[idx].wl_size;
1440 ++idx;
1441# ifdef FEAT_FOLDING
1442 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1443 lnum = wp->w_lines[idx].wl_lnum;
1444 else
1445# endif
1446 ++lnum;
1447 }
1448 srow += mid_start;
1449 mid_end = wp->w_height;
1450 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1451 {
1452 if (wp->w_lines[idx].wl_valid
1453 && wp->w_lines[idx].wl_lnum >= to + 1)
1454 {
1455 /* Only update until first row of this line */
1456 mid_end = srow;
1457 break;
1458 }
1459 srow += wp->w_lines[idx].wl_size;
1460 }
1461 }
1462 }
1463
1464 if (VIsual_active && buf == curwin->w_buffer)
1465 {
1466 wp->w_old_visual_mode = VIsual_mode;
1467 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1468 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001469 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 wp->w_old_curswant = curwin->w_curswant;
1471 }
1472 else
1473 {
1474 wp->w_old_visual_mode = 0;
1475 wp->w_old_cursor_lnum = 0;
1476 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001477 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 }
1479#endif /* FEAT_VISUAL */
1480
1481#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1482 /* reset got_int, otherwise regexp won't work */
1483 save_got_int = got_int;
1484 got_int = 0;
1485#endif
1486#ifdef FEAT_FOLDING
1487 win_foldinfo.fi_level = 0;
1488#endif
1489
1490 /*
1491 * Update all the window rows.
1492 */
1493 idx = 0; /* first entry in w_lines[].wl_size */
1494 row = 0;
1495 srow = 0;
1496 lnum = wp->w_topline; /* first line shown in window */
1497 for (;;)
1498 {
1499 /* stop updating when reached the end of the window (check for _past_
1500 * the end of the window is at the end of the loop) */
1501 if (row == wp->w_height)
1502 {
1503 didline = TRUE;
1504 break;
1505 }
1506
1507 /* stop updating when hit the end of the file */
1508 if (lnum > buf->b_ml.ml_line_count)
1509 {
1510 eof = TRUE;
1511 break;
1512 }
1513
1514 /* Remember the starting row of the line that is going to be dealt
1515 * with. It is used further down when the line doesn't fit. */
1516 srow = row;
1517
1518 /*
1519 * Update a line when it is in an area that needs updating, when it
1520 * has changes or w_lines[idx] is invalid.
1521 * bot_start may be halfway a wrapped line after using
1522 * win_del_lines(), check if the current line includes it.
1523 * When syntax folding is being used, the saved syntax states will
1524 * already have been updated, we can't see where the syntax state is
1525 * the same again, just update until the end of the window.
1526 */
1527 if (row < top_end
1528 || (row >= mid_start && row < mid_end)
1529#ifdef FEAT_SEARCH_EXTRA
1530 || top_to_mod
1531#endif
1532 || idx >= wp->w_lines_valid
1533 || (row + wp->w_lines[idx].wl_size > bot_start)
1534 || (mod_top != 0
1535 && (lnum == mod_top
1536 || (lnum >= mod_top
1537 && (lnum < mod_bot
1538#ifdef FEAT_SYN_HL
1539 || did_update == DID_FOLD
1540 || (did_update == DID_LINE
1541 && syntax_present(buf)
1542 && (
1543# ifdef FEAT_FOLDING
1544 (foldmethodIsSyntax(wp)
1545 && hasAnyFolding(wp)) ||
1546# endif
1547 syntax_check_changed(lnum)))
1548#endif
1549 )))))
1550 {
1551#ifdef FEAT_SEARCH_EXTRA
1552 if (lnum == mod_top)
1553 top_to_mod = FALSE;
1554#endif
1555
1556 /*
1557 * When at start of changed lines: May scroll following lines
1558 * up or down to minimize redrawing.
1559 * Don't do this when the change continues until the end.
1560 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1561 */
1562 if (lnum == mod_top
1563 && mod_bot != MAXLNUM
1564 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1565 {
1566 int old_rows = 0;
1567 int new_rows = 0;
1568 int xtra_rows;
1569 linenr_T l;
1570
1571 /* Count the old number of window rows, using w_lines[], which
1572 * should still contain the sizes for the lines as they are
1573 * currently displayed. */
1574 for (i = idx; i < wp->w_lines_valid; ++i)
1575 {
1576 /* Only valid lines have a meaningful wl_lnum. Invalid
1577 * lines are part of the changed area. */
1578 if (wp->w_lines[i].wl_valid
1579 && wp->w_lines[i].wl_lnum == mod_bot)
1580 break;
1581 old_rows += wp->w_lines[i].wl_size;
1582#ifdef FEAT_FOLDING
1583 if (wp->w_lines[i].wl_valid
1584 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1585 {
1586 /* Must have found the last valid entry above mod_bot.
1587 * Add following invalid entries. */
1588 ++i;
1589 while (i < wp->w_lines_valid
1590 && !wp->w_lines[i].wl_valid)
1591 old_rows += wp->w_lines[i++].wl_size;
1592 break;
1593 }
1594#endif
1595 }
1596
1597 if (i >= wp->w_lines_valid)
1598 {
1599 /* We can't find a valid line below the changed lines,
1600 * need to redraw until the end of the window.
1601 * Inserting/deleting lines has no use. */
1602 bot_start = 0;
1603 }
1604 else
1605 {
1606 /* Able to count old number of rows: Count new window
1607 * rows, and may insert/delete lines */
1608 j = idx;
1609 for (l = lnum; l < mod_bot; ++l)
1610 {
1611#ifdef FEAT_FOLDING
1612 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1613 ++new_rows;
1614 else
1615#endif
1616#ifdef FEAT_DIFF
1617 if (l == wp->w_topline)
1618 new_rows += plines_win_nofill(wp, l, TRUE)
1619 + wp->w_topfill;
1620 else
1621#endif
1622 new_rows += plines_win(wp, l, TRUE);
1623 ++j;
1624 if (new_rows > wp->w_height - row - 2)
1625 {
1626 /* it's getting too much, must redraw the rest */
1627 new_rows = 9999;
1628 break;
1629 }
1630 }
1631 xtra_rows = new_rows - old_rows;
1632 if (xtra_rows < 0)
1633 {
1634 /* May scroll text up. If there is not enough
1635 * remaining text or scrolling fails, must redraw the
1636 * rest. If scrolling works, must redraw the text
1637 * below the scrolled text. */
1638 if (row - xtra_rows >= wp->w_height - 2)
1639 mod_bot = MAXLNUM;
1640 else
1641 {
1642 check_for_delay(FALSE);
1643 if (win_del_lines(wp, row,
1644 -xtra_rows, FALSE, FALSE) == FAIL)
1645 mod_bot = MAXLNUM;
1646 else
1647 bot_start = wp->w_height + xtra_rows;
1648 }
1649 }
1650 else if (xtra_rows > 0)
1651 {
1652 /* May scroll text down. If there is not enough
1653 * remaining text of scrolling fails, must redraw the
1654 * rest. */
1655 if (row + xtra_rows >= wp->w_height - 2)
1656 mod_bot = MAXLNUM;
1657 else
1658 {
1659 check_for_delay(FALSE);
1660 if (win_ins_lines(wp, row + old_rows,
1661 xtra_rows, FALSE, FALSE) == FAIL)
1662 mod_bot = MAXLNUM;
1663 else if (top_end > row + old_rows)
1664 /* Scrolled the part at the top that requires
1665 * updating down. */
1666 top_end += xtra_rows;
1667 }
1668 }
1669
1670 /* When not updating the rest, may need to move w_lines[]
1671 * entries. */
1672 if (mod_bot != MAXLNUM && i != j)
1673 {
1674 if (j < i)
1675 {
1676 int x = row + new_rows;
1677
1678 /* move entries in w_lines[] upwards */
1679 for (;;)
1680 {
1681 /* stop at last valid entry in w_lines[] */
1682 if (i >= wp->w_lines_valid)
1683 {
1684 wp->w_lines_valid = j;
1685 break;
1686 }
1687 wp->w_lines[j] = wp->w_lines[i];
1688 /* stop at a line that won't fit */
1689 if (x + (int)wp->w_lines[j].wl_size
1690 > wp->w_height)
1691 {
1692 wp->w_lines_valid = j + 1;
1693 break;
1694 }
1695 x += wp->w_lines[j++].wl_size;
1696 ++i;
1697 }
1698 if (bot_start > x)
1699 bot_start = x;
1700 }
1701 else /* j > i */
1702 {
1703 /* move entries in w_lines[] downwards */
1704 j -= i;
1705 wp->w_lines_valid += j;
1706 if (wp->w_lines_valid > wp->w_height)
1707 wp->w_lines_valid = wp->w_height;
1708 for (i = wp->w_lines_valid; i - j >= idx; --i)
1709 wp->w_lines[i] = wp->w_lines[i - j];
1710
1711 /* The w_lines[] entries for inserted lines are
1712 * now invalid, but wl_size may be used above.
1713 * Reset to zero. */
1714 while (i >= idx)
1715 {
1716 wp->w_lines[i].wl_size = 0;
1717 wp->w_lines[i--].wl_valid = FALSE;
1718 }
1719 }
1720 }
1721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
1723
1724#ifdef FEAT_FOLDING
1725 /*
1726 * When lines are folded, display one line for all of them.
1727 * Otherwise, display normally (can be several display lines when
1728 * 'wrap' is on).
1729 */
1730 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1731 if (fold_count != 0)
1732 {
1733 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1734 ++row;
1735 --fold_count;
1736 wp->w_lines[idx].wl_folded = TRUE;
1737 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1738# ifdef FEAT_SYN_HL
1739 did_update = DID_FOLD;
1740# endif
1741 }
1742 else
1743#endif
1744 if (idx < wp->w_lines_valid
1745 && wp->w_lines[idx].wl_valid
1746 && wp->w_lines[idx].wl_lnum == lnum
1747 && lnum > wp->w_topline
1748 && !(dy_flags & DY_LASTLINE)
1749 && srow + wp->w_lines[idx].wl_size > wp->w_height
1750#ifdef FEAT_DIFF
1751 && diff_check_fill(wp, lnum) == 0
1752#endif
1753 )
1754 {
1755 /* This line is not going to fit. Don't draw anything here,
1756 * will draw "@ " lines below. */
1757 row = wp->w_height + 1;
1758 }
1759 else
1760 {
1761#ifdef FEAT_SEARCH_EXTRA
1762 prepare_search_hl(wp, lnum);
1763#endif
1764#ifdef FEAT_SYN_HL
1765 /* Let the syntax stuff know we skipped a few lines. */
1766 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1767 && syntax_present(buf))
1768 syntax_end_parsing(syntax_last_parsed + 1);
1769#endif
1770
1771 /*
1772 * Display one line.
1773 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001774 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
1776#ifdef FEAT_FOLDING
1777 wp->w_lines[idx].wl_folded = FALSE;
1778 wp->w_lines[idx].wl_lastlnum = lnum;
1779#endif
1780#ifdef FEAT_SYN_HL
1781 did_update = DID_LINE;
1782 syntax_last_parsed = lnum;
1783#endif
1784 }
1785
1786 wp->w_lines[idx].wl_lnum = lnum;
1787 wp->w_lines[idx].wl_valid = TRUE;
1788 if (row > wp->w_height) /* past end of screen */
1789 {
1790 /* we may need the size of that too long line later on */
1791 if (dollar_vcol == 0)
1792 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1793 ++idx;
1794 break;
1795 }
1796 if (dollar_vcol == 0)
1797 wp->w_lines[idx].wl_size = row - srow;
1798 ++idx;
1799#ifdef FEAT_FOLDING
1800 lnum += fold_count + 1;
1801#else
1802 ++lnum;
1803#endif
1804 }
1805 else
1806 {
1807 /* This line does not need updating, advance to the next one */
1808 row += wp->w_lines[idx++].wl_size;
1809 if (row > wp->w_height) /* past end of screen */
1810 break;
1811#ifdef FEAT_FOLDING
1812 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1813#else
1814 ++lnum;
1815#endif
1816#ifdef FEAT_SYN_HL
1817 did_update = DID_NONE;
1818#endif
1819 }
1820
1821 if (lnum > buf->b_ml.ml_line_count)
1822 {
1823 eof = TRUE;
1824 break;
1825 }
1826 }
1827 /*
1828 * End of loop over all window lines.
1829 */
1830
1831
1832 if (idx > wp->w_lines_valid)
1833 wp->w_lines_valid = idx;
1834
1835#ifdef FEAT_SYN_HL
1836 /*
1837 * Let the syntax stuff know we stop parsing here.
1838 */
1839 if (syntax_last_parsed != 0 && syntax_present(buf))
1840 syntax_end_parsing(syntax_last_parsed + 1);
1841#endif
1842
1843 /*
1844 * If we didn't hit the end of the file, and we didn't finish the last
1845 * line we were working on, then the line didn't fit.
1846 */
1847 wp->w_empty_rows = 0;
1848#ifdef FEAT_DIFF
1849 wp->w_filler_rows = 0;
1850#endif
1851 if (!eof && !didline)
1852 {
1853 if (lnum == wp->w_topline)
1854 {
1855 /*
1856 * Single line that does not fit!
1857 * Don't overwrite it, it can be edited.
1858 */
1859 wp->w_botline = lnum + 1;
1860 }
1861#ifdef FEAT_DIFF
1862 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1863 {
1864 /* Window ends in filler lines. */
1865 wp->w_botline = lnum;
1866 wp->w_filler_rows = wp->w_height - srow;
1867 }
1868#endif
1869 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1870 {
1871 /*
1872 * Last line isn't finished: Display "@@@" at the end.
1873 */
1874 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1875 W_WINROW(wp) + wp->w_height,
1876 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1877 '@', '@', hl_attr(HLF_AT));
1878 set_empty_rows(wp, srow);
1879 wp->w_botline = lnum;
1880 }
1881 else
1882 {
1883 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1884 wp->w_botline = lnum;
1885 }
1886 }
1887 else
1888 {
1889#ifdef FEAT_VERTSPLIT
1890 draw_vsep_win(wp, row);
1891#endif
1892 if (eof) /* we hit the end of the file */
1893 {
1894 wp->w_botline = buf->b_ml.ml_line_count + 1;
1895#ifdef FEAT_DIFF
1896 j = diff_check_fill(wp, wp->w_botline);
1897 if (j > 0 && !wp->w_botfill)
1898 {
1899 /*
1900 * Display filler lines at the end of the file
1901 */
1902 if (char2cells(fill_diff) > 1)
1903 i = '-';
1904 else
1905 i = fill_diff;
1906 if (row + j > wp->w_height)
1907 j = wp->w_height - row;
1908 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1909 row += j;
1910 }
1911#endif
1912 }
1913 else if (dollar_vcol == 0)
1914 wp->w_botline = lnum;
1915
1916 /* make sure the rest of the screen is blank */
1917 /* put '~'s on rows that aren't part of the file. */
1918 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1919 }
1920
1921 /* Reset the type of redrawing required, the window has been updated. */
1922 wp->w_redr_type = 0;
1923#ifdef FEAT_DIFF
1924 wp->w_old_topfill = wp->w_topfill;
1925 wp->w_old_botfill = wp->w_botfill;
1926#endif
1927
1928 if (dollar_vcol == 0)
1929 {
1930 /*
1931 * There is a trick with w_botline. If we invalidate it on each
1932 * change that might modify it, this will cause a lot of expensive
1933 * calls to plines() in update_topline() each time. Therefore the
1934 * value of w_botline is often approximated, and this value is used to
1935 * compute the value of w_topline. If the value of w_botline was
1936 * wrong, check that the value of w_topline is correct (cursor is on
1937 * the visible part of the text). If it's not, we need to redraw
1938 * again. Mostly this just means scrolling up a few lines, so it
1939 * doesn't look too bad. Only do this for the current window (where
1940 * changes are relevant).
1941 */
1942 wp->w_valid |= VALID_BOTLINE;
1943 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1944 {
1945 recursive = TRUE;
1946 curwin->w_valid &= ~VALID_TOPLINE;
1947 update_topline(); /* may invalidate w_botline again */
1948 if (must_redraw != 0)
1949 {
1950 /* Don't update for changes in buffer again. */
1951 i = curbuf->b_mod_set;
1952 curbuf->b_mod_set = FALSE;
1953 win_update(curwin);
1954 must_redraw = 0;
1955 curbuf->b_mod_set = i;
1956 }
1957 recursive = FALSE;
1958 }
1959 }
1960
1961#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1962 /* restore got_int, unless CTRL-C was hit while redrawing */
1963 if (!got_int)
1964 got_int = save_got_int;
1965#endif
1966}
1967
1968#ifdef FEAT_SIGNS
1969static int draw_signcolumn __ARGS((win_T *wp));
1970
1971/*
1972 * Return TRUE when window "wp" has a column to draw signs in.
1973 */
1974 static int
1975draw_signcolumn(wp)
1976 win_T *wp;
1977{
1978 return (wp->w_buffer->b_signlist != NULL
1979# ifdef FEAT_NETBEANS_INTG
1980 || usingNetbeans
1981# endif
1982 );
1983}
1984#endif
1985
1986/*
1987 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1988 * as the filler character.
1989 */
1990 static void
1991win_draw_end(wp, c1, c2, row, endrow, hl)
1992 win_T *wp;
1993 int c1;
1994 int c2;
1995 int row;
1996 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001997 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
1999#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2000 int n = 0;
2001# define FDC_OFF n
2002#else
2003# define FDC_OFF 0
2004#endif
2005
2006#ifdef FEAT_RIGHTLEFT
2007 if (wp->w_p_rl)
2008 {
2009 /* No check for cmdline window: should never be right-left. */
2010# ifdef FEAT_FOLDING
2011 n = wp->w_p_fdc;
2012
2013 if (n > 0)
2014 {
2015 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002016 if (n > W_WIDTH(wp))
2017 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2019 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2020 ' ', ' ', hl_attr(HLF_FC));
2021 }
2022# endif
2023# ifdef FEAT_SIGNS
2024 if (draw_signcolumn(wp))
2025 {
2026 int nn = n + 2;
2027
2028 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002029 if (nn > W_WIDTH(wp))
2030 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2032 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2033 ' ', ' ', hl_attr(HLF_SC));
2034 n = nn;
2035 }
2036# endif
2037 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2038 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2039 c2, c2, hl_attr(hl));
2040 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2041 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2042 c1, c2, hl_attr(hl));
2043 }
2044 else
2045#endif
2046 {
2047#ifdef FEAT_CMDWIN
2048 if (cmdwin_type != 0 && wp == curwin)
2049 {
2050 /* draw the cmdline character in the leftmost column */
2051 n = 1;
2052 if (n > wp->w_width)
2053 n = wp->w_width;
2054 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2055 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2056 cmdwin_type, ' ', hl_attr(HLF_AT));
2057 }
2058#endif
2059#ifdef FEAT_FOLDING
2060 if (wp->w_p_fdc > 0)
2061 {
2062 int nn = n + wp->w_p_fdc;
2063
2064 /* draw the fold column at the left */
2065 if (nn > W_WIDTH(wp))
2066 nn = W_WIDTH(wp);
2067 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2068 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2069 ' ', ' ', hl_attr(HLF_FC));
2070 n = nn;
2071 }
2072#endif
2073#ifdef FEAT_SIGNS
2074 if (draw_signcolumn(wp))
2075 {
2076 int nn = n + 2;
2077
2078 /* draw the sign column after the fold column */
2079 if (nn > W_WIDTH(wp))
2080 nn = W_WIDTH(wp);
2081 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2082 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2083 ' ', ' ', hl_attr(HLF_SC));
2084 n = nn;
2085 }
2086#endif
2087 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2088 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2089 c1, c2, hl_attr(hl));
2090 }
2091 set_empty_rows(wp, row);
2092}
2093
2094#ifdef FEAT_FOLDING
2095/*
2096 * Display one folded line.
2097 */
2098 static void
2099fold_line(wp, fold_count, foldinfo, lnum, row)
2100 win_T *wp;
2101 long fold_count;
2102 foldinfo_T *foldinfo;
2103 linenr_T lnum;
2104 int row;
2105{
2106 char_u buf[51];
2107 pos_T *top, *bot;
2108 linenr_T lnume = lnum + fold_count - 1;
2109 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002110 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 int col;
2113 int txtcol;
2114 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002115 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 /* Build the fold line:
2118 * 1. Add the cmdwin_type for the command-line window
2119 * 2. Add the 'foldcolumn'
2120 * 3. Add the 'number' column
2121 * 4. Compose the text
2122 * 5. Add the text
2123 * 6. set highlighting for the Visual area an other text
2124 */
2125 col = 0;
2126
2127 /*
2128 * 1. Add the cmdwin_type for the command-line window
2129 * Ignores 'rightleft', this window is never right-left.
2130 */
2131#ifdef FEAT_CMDWIN
2132 if (cmdwin_type != 0 && wp == curwin)
2133 {
2134 ScreenLines[off] = cmdwin_type;
2135 ScreenAttrs[off] = hl_attr(HLF_AT);
2136#ifdef FEAT_MBYTE
2137 if (enc_utf8)
2138 ScreenLinesUC[off] = 0;
2139#endif
2140 ++col;
2141 }
2142#endif
2143
2144 /*
2145 * 2. Add the 'foldcolumn'
2146 */
2147 fdc = wp->w_p_fdc;
2148 if (fdc > W_WIDTH(wp) - col)
2149 fdc = W_WIDTH(wp) - col;
2150 if (fdc > 0)
2151 {
2152 fill_foldcolumn(buf, wp, TRUE, lnum);
2153#ifdef FEAT_RIGHTLEFT
2154 if (wp->w_p_rl)
2155 {
2156 int i;
2157
2158 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2159 hl_attr(HLF_FC));
2160 /* reverse the fold column */
2161 for (i = 0; i < fdc; ++i)
2162 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2163 }
2164 else
2165#endif
2166 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2167 col += fdc;
2168 }
2169
2170#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002171# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2172 for (ri = 0; ri < l; ++ri) \
2173 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2174 else \
2175 for (ri = 0; ri < l; ++ri) \
2176 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002178# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2179 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180#endif
2181
2182 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002183 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185#ifdef FEAT_SIGNS
2186 /* If signs are being displayed, add two spaces. */
2187 if (draw_signcolumn(wp))
2188 {
2189 len = W_WIDTH(wp) - col;
2190 if (len > 0)
2191 {
2192 if (len > 2)
2193 len = 2;
2194# ifdef FEAT_RIGHTLEFT
2195 if (wp->w_p_rl)
2196 /* the line number isn't reversed */
2197 copy_text_attr(off + W_WIDTH(wp) - len - col,
2198 (char_u *)" ", len, hl_attr(HLF_FL));
2199 else
2200# endif
2201 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2202 col += len;
2203 }
2204 }
2205#endif
2206
2207 /*
2208 * 3. Add the 'number' column
2209 */
2210 if (wp->w_p_nu)
2211 {
2212 len = W_WIDTH(wp) - col;
2213 if (len > 0)
2214 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002215 int w = number_width(wp);
2216
2217 if (len > w + 1)
2218 len = w + 1;
2219 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#ifdef FEAT_RIGHTLEFT
2221 if (wp->w_p_rl)
2222 /* the line number isn't reversed */
2223 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2224 hl_attr(HLF_FL));
2225 else
2226#endif
2227 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2228 col += len;
2229 }
2230 }
2231
2232 /*
2233 * 4. Compose the folded-line string with 'foldtext', if set.
2234 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002235 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 txtcol = col; /* remember where text starts */
2238
2239 /*
2240 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2241 * Right-left text is put in columns 0 - number-col, normal text is put
2242 * in columns number-col - window-width.
2243 */
2244#ifdef FEAT_MBYTE
2245 if (has_mbyte)
2246 {
2247 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002248 int u8c, u8cc[MAX_MCO];
2249 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 int idx;
2251 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002252 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253# ifdef FEAT_ARABIC
2254 int prev_c = 0; /* previous Arabic character */
2255 int prev_c1 = 0; /* first composing char for prev_c */
2256# endif
2257
2258# ifdef FEAT_RIGHTLEFT
2259 if (wp->w_p_rl)
2260 idx = off;
2261 else
2262# endif
2263 idx = off + col;
2264
2265 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2266 for (p = text; *p != NUL; )
2267 {
2268 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002269 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (col + cells > W_WIDTH(wp)
2271# ifdef FEAT_RIGHTLEFT
2272 - (wp->w_p_rl ? col : 0)
2273# endif
2274 )
2275 break;
2276 ScreenLines[idx] = *p;
2277 if (enc_utf8)
2278 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002279 u8c = utfc_ptr2char(p, u8cc);
2280 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
2282 ScreenLinesUC[idx] = 0;
2283#ifdef FEAT_ARABIC
2284 prev_c = u8c;
2285#endif
2286 }
2287 else
2288 {
2289#ifdef FEAT_ARABIC
2290 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2291 {
2292 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002293 int pc, pc1, nc;
2294 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 int firstbyte = *p;
2296
2297 /* The idea of what is the previous and next
2298 * character depends on 'rightleft'. */
2299 if (wp->w_p_rl)
2300 {
2301 pc = prev_c;
2302 pc1 = prev_c1;
2303 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002304 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
2306 else
2307 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002308 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002310 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312 prev_c = u8c;
2313
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002314 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 pc, pc1, nc);
2316 ScreenLines[idx] = firstbyte;
2317 }
2318 else
2319 prev_c = u8c;
2320#endif
2321 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002322#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (u8c >= 0x10000)
2324 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2325 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002328 for (i = 0; i < Screen_mco; ++i)
2329 {
2330 ScreenLinesC[i][idx] = u8cc[i];
2331 if (u8cc[i] == 0)
2332 break;
2333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335 if (cells > 1)
2336 ScreenLines[idx + 1] = 0;
2337 }
2338 else if (cells > 1) /* double-byte character */
2339 {
2340 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2341 ScreenLines2[idx] = p[1];
2342 else
2343 ScreenLines[idx + 1] = p[1];
2344 }
2345 col += cells;
2346 idx += cells;
2347 p += c_len;
2348 }
2349 }
2350 else
2351#endif
2352 {
2353 len = (int)STRLEN(text);
2354 if (len > W_WIDTH(wp) - col)
2355 len = W_WIDTH(wp) - col;
2356 if (len > 0)
2357 {
2358#ifdef FEAT_RIGHTLEFT
2359 if (wp->w_p_rl)
2360 STRNCPY(current_ScreenLine, text, len);
2361 else
2362#endif
2363 STRNCPY(current_ScreenLine + col, text, len);
2364 col += len;
2365 }
2366 }
2367
2368 /* Fill the rest of the line with the fold filler */
2369#ifdef FEAT_RIGHTLEFT
2370 if (wp->w_p_rl)
2371 col -= txtcol;
2372#endif
2373 while (col < W_WIDTH(wp)
2374#ifdef FEAT_RIGHTLEFT
2375 - (wp->w_p_rl ? txtcol : 0)
2376#endif
2377 )
2378 {
2379#ifdef FEAT_MBYTE
2380 if (enc_utf8)
2381 {
2382 if (fill_fold >= 0x80)
2383 {
2384 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002385 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
2387 else
2388 ScreenLinesUC[off + col] = 0;
2389 }
2390#endif
2391 ScreenLines[off + col++] = fill_fold;
2392 }
2393
2394 if (text != buf)
2395 vim_free(text);
2396
2397 /*
2398 * 6. set highlighting for the Visual area an other text.
2399 * If all folded lines are in the Visual area, highlight the line.
2400 */
2401#ifdef FEAT_VISUAL
2402 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2403 {
2404 if (ltoreq(curwin->w_cursor, VIsual))
2405 {
2406 /* Visual is after curwin->w_cursor */
2407 top = &curwin->w_cursor;
2408 bot = &VIsual;
2409 }
2410 else
2411 {
2412 /* Visual is before curwin->w_cursor */
2413 top = &VIsual;
2414 bot = &curwin->w_cursor;
2415 }
2416 if (lnum >= top->lnum
2417 && lnume <= bot->lnum
2418 && (VIsual_mode != 'v'
2419 || ((lnum > top->lnum
2420 || (lnum == top->lnum
2421 && top->col == 0))
2422 && (lnume < bot->lnum
2423 || (lnume == bot->lnum
2424 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002425 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 if (VIsual_mode == Ctrl_V)
2428 {
2429 /* Visual block mode: highlight the chars part of the block */
2430 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2431 {
2432 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2433 len = wp->w_old_cursor_lcol;
2434 else
2435 len = W_WIDTH(wp) - txtcol;
2436 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002437 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 }
2440 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002443 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446 }
2447#endif
2448
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002449#ifdef FEAT_SYN_HL
2450 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002451 if (wp->w_p_cuc)
2452 {
2453 txtcol += wp->w_virtcol;
2454 if (wp->w_p_wrap)
2455 txtcol -= wp->w_skipcol;
2456 else
2457 txtcol -= wp->w_leftcol;
2458 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2459 ScreenAttrs[off + txtcol] = hl_combine_attr(
2460 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2461 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
2464 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2465 (int)W_WIDTH(wp), FALSE);
2466
2467 /*
2468 * Update w_cline_height and w_cline_folded if the cursor line was
2469 * updated (saves a call to plines() later).
2470 */
2471 if (wp == curwin
2472 && lnum <= curwin->w_cursor.lnum
2473 && lnume >= curwin->w_cursor.lnum)
2474 {
2475 curwin->w_cline_row = row;
2476 curwin->w_cline_height = 1;
2477 curwin->w_cline_folded = TRUE;
2478 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2479 }
2480}
2481
2482/*
2483 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2484 */
2485 static void
2486copy_text_attr(off, buf, len, attr)
2487 int off;
2488 char_u *buf;
2489 int len;
2490 int attr;
2491{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002492 int i;
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 mch_memmove(ScreenLines + off, buf, (size_t)len);
2495# ifdef FEAT_MBYTE
2496 if (enc_utf8)
2497 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2498# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002499 for (i = 0; i < len; ++i)
2500 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501}
2502
2503/*
2504 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002505 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 */
2507 static void
2508fill_foldcolumn(p, wp, closed, lnum)
2509 char_u *p;
2510 win_T *wp;
2511 int closed; /* TRUE of FALSE */
2512 linenr_T lnum; /* current line number */
2513{
2514 int i = 0;
2515 int level;
2516 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002517 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 /* Init to all spaces. */
2520 copy_spaces(p, (size_t)wp->w_p_fdc);
2521
2522 level = win_foldinfo.fi_level;
2523 if (level > 0)
2524 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002525 /* If there is only one column put more info in it. */
2526 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2527
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 /* If the column is too narrow, we start at the lowest level that
2529 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002530 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (first_level < 1)
2532 first_level = 1;
2533
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002534 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 if (win_foldinfo.fi_lnum == lnum
2537 && first_level + i >= win_foldinfo.fi_low_level)
2538 p[i] = '-';
2539 else if (first_level == 1)
2540 p[i] = '|';
2541 else if (first_level + i <= 9)
2542 p[i] = '0' + first_level + i;
2543 else
2544 p[i] = '>';
2545 if (first_level + i == level)
2546 break;
2547 }
2548 }
2549 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002550 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551}
2552#endif /* FEAT_FOLDING */
2553
2554/*
2555 * Display line "lnum" of window 'wp' on the screen.
2556 * Start at row "startrow", stop when "endrow" is reached.
2557 * wp->w_virtcol needs to be valid.
2558 *
2559 * Return the number of last row the line occupies.
2560 */
2561 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002562win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 win_T *wp;
2564 linenr_T lnum;
2565 int startrow;
2566 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002567 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
2569 int col; /* visual column on screen */
2570 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2571 int c = 0; /* init for GCC */
2572 long vcol = 0; /* virtual column (for tabs) */
2573 long vcol_prev = -1; /* "vcol" of previous character */
2574 char_u *line; /* current line */
2575 char_u *ptr; /* current position in "line" */
2576 int row; /* row in the window, excl w_winrow */
2577 int screen_row; /* row on the screen, incl w_winrow */
2578
2579 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2580 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002581 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 int c_extra = NUL; /* extra chars, all the same */
2583 int extra_attr = 0; /* attributes when n_extra != 0 */
2584 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2585 displaying lcs_eol at end-of-line */
2586 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2587 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2588
2589 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2590 int saved_n_extra = 0;
2591 char_u *saved_p_extra = NULL;
2592 int saved_c_extra = 0;
2593 int saved_char_attr = 0;
2594
2595 int n_attr = 0; /* chars with special attr */
2596 int saved_attr2 = 0; /* char_attr saved for n_attr */
2597 int n_attr3 = 0; /* chars with overruling special attr */
2598 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2599
2600 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2601
2602 int fromcol, tocol; /* start/end of inverting */
2603 int fromcol_prev = -2; /* start of inverting after cursor */
2604 int noinvcur = FALSE; /* don't invert the cursor */
2605#ifdef FEAT_VISUAL
2606 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002607 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608#endif
2609 pos_T pos;
2610 long v;
2611
2612 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002613 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2615 in this line */
2616 int attr = 0; /* attributes for area highlighting */
2617 int area_attr = 0; /* attributes desired by highlighting */
2618 int search_attr = 0; /* attributes desired by 'hlsearch' */
2619#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002620 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 int syntax_attr = 0; /* attributes desired by syntax */
2622 int has_syntax = FALSE; /* this buffer has syntax highl. */
2623 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002624 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002625#endif
2626#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002627 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002628# define SPWORDLEN 150
2629 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002630 int nextlinecol = 0; /* column where nextline[] starts */
2631 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002632 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002633 int spell_attr = 0; /* attributes desired by spelling */
2634 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002635 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2636 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002637 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002638 static int cap_col = -1; /* column to check for Cap word */
2639 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002640 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641#endif
2642 int extra_check; /* has syntax or linebreak */
2643#ifdef FEAT_MBYTE
2644 int multi_attr = 0; /* attributes desired by multibyte */
2645 int mb_l = 1; /* multi-byte byte length */
2646 int mb_c = 0; /* decoded multi-byte character */
2647 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002648 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#endif
2650#ifdef FEAT_DIFF
2651 int filler_lines; /* nr of filler lines to be drawn */
2652 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002653 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 int change_start = MAXCOL; /* first col of changed area */
2655 int change_end = -1; /* last col of changed area */
2656#endif
2657 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2658#ifdef FEAT_LINEBREAK
2659 int need_showbreak = FALSE;
2660#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002661#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2662 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002664 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#endif
2666#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002667 matchitem_T *cur; /* points to the match list */
2668 match_T *shl; /* points to search_hl or a match */
2669 int shl_flag; /* flag to indicate whether search_hl
2670 has been processed or not */
2671 int prevcol_hl_flag; /* flag to indicate whether prevcol
2672 equals startcol of search_hl or one
2673 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674#endif
2675#ifdef FEAT_ARABIC
2676 int prev_c = 0; /* previous Arabic character */
2677 int prev_c1 = 0; /* first composing char for prev_c */
2678#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002679#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002680 int did_line_attr = 0;
2681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683 /* draw_state: items that are drawn in sequence: */
2684#define WL_START 0 /* nothing done yet */
2685#ifdef FEAT_CMDWIN
2686# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2687#else
2688# define WL_CMDLINE WL_START
2689#endif
2690#ifdef FEAT_FOLDING
2691# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2692#else
2693# define WL_FOLD WL_CMDLINE
2694#endif
2695#ifdef FEAT_SIGNS
2696# define WL_SIGN WL_FOLD + 1 /* column for signs */
2697#else
2698# define WL_SIGN WL_FOLD /* column for signs */
2699#endif
2700#define WL_NR WL_SIGN + 1 /* line number */
2701#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2702# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2703#else
2704# define WL_SBR WL_NR
2705#endif
2706#define WL_LINE WL_SBR + 1 /* text in the line */
2707 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002708#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 int feedback_col = 0;
2710 int feedback_old_attr = -1;
2711#endif
2712
2713
2714 if (startrow > endrow) /* past the end already! */
2715 return startrow;
2716
2717 row = startrow;
2718 screen_row = row + W_WINROW(wp);
2719
2720 /*
2721 * To speed up the loop below, set extra_check when there is linebreak,
2722 * trailing white space and/or syntax processing to be done.
2723 */
2724#ifdef FEAT_LINEBREAK
2725 extra_check = wp->w_p_lbr;
2726#else
2727 extra_check = 0;
2728#endif
2729#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002730 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
2732 /* Prepare for syntax highlighting in this line. When there is an
2733 * error, stop syntax highlighting. */
2734 save_did_emsg = did_emsg;
2735 did_emsg = FALSE;
2736 syntax_start(wp, lnum);
2737 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002738 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 else
2740 {
2741 did_emsg = save_did_emsg;
2742 has_syntax = TRUE;
2743 extra_check = TRUE;
2744 }
2745 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002746#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002747
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002748#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002749 if (wp->w_p_spell
2750 && *wp->w_buffer->b_p_spl != NUL
2751 && wp->w_buffer->b_langp.ga_len > 0
2752 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002753 {
2754 /* Prepare for spell checking. */
2755 has_spell = TRUE;
2756 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002757
2758 /* Get the start of the next line, so that words that wrap to the next
2759 * line are found too: "et<line-break>al.".
2760 * Trick: skip a few chars for C/shell/Vim comments */
2761 nextline[SPWORDLEN] = NUL;
2762 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2763 {
2764 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2765 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2766 }
2767
2768 /* When a word wrapped from the previous line the start of the current
2769 * line is valid. */
2770 if (lnum == checked_lnum)
2771 cur_checked_col = checked_col;
2772 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002773
2774 /* When there was a sentence end in the previous line may require a
2775 * word starting with capital in this line. In line 1 always check
2776 * the first word. */
2777 if (lnum != capcol_lnum)
2778 cap_col = -1;
2779 if (lnum == 1)
2780 cap_col = 0;
2781 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783#endif
2784
2785 /*
2786 * handle visual active in this window
2787 */
2788 fromcol = -10;
2789 tocol = MAXCOL;
2790#ifdef FEAT_VISUAL
2791 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2792 {
2793 /* Visual is after curwin->w_cursor */
2794 if (ltoreq(curwin->w_cursor, VIsual))
2795 {
2796 top = &curwin->w_cursor;
2797 bot = &VIsual;
2798 }
2799 else /* Visual is before curwin->w_cursor */
2800 {
2801 top = &VIsual;
2802 bot = &curwin->w_cursor;
2803 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002804 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 if (VIsual_mode == Ctrl_V) /* block mode */
2806 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002807 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
2809 fromcol = wp->w_old_cursor_fcol;
2810 tocol = wp->w_old_cursor_lcol;
2811 }
2812 }
2813 else /* non-block mode */
2814 {
2815 if (lnum > top->lnum && lnum <= bot->lnum)
2816 fromcol = 0;
2817 else if (lnum == top->lnum)
2818 {
2819 if (VIsual_mode == 'V') /* linewise */
2820 fromcol = 0;
2821 else
2822 {
2823 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2824 if (gchar_pos(top) == NUL)
2825 tocol = fromcol + 1;
2826 }
2827 }
2828 if (VIsual_mode != 'V' && lnum == bot->lnum)
2829 {
2830 if (*p_sel == 'e' && bot->col == 0
2831#ifdef FEAT_VIRTUALEDIT
2832 && bot->coladd == 0
2833#endif
2834 )
2835 {
2836 fromcol = -10;
2837 tocol = MAXCOL;
2838 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002839 else if (bot->col == MAXCOL)
2840 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 else
2842 {
2843 pos = *bot;
2844 if (*p_sel == 'e')
2845 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2846 else
2847 {
2848 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2849 ++tocol;
2850 }
2851 }
2852 }
2853 }
2854
2855#ifndef MSDOS
2856 /* Check if the character under the cursor should not be inverted */
2857 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2858# ifdef FEAT_GUI
2859 && !gui.in_use
2860# endif
2861 )
2862 noinvcur = TRUE;
2863#endif
2864
2865 /* if inverting in this line set area_highlighting */
2866 if (fromcol >= 0)
2867 {
2868 area_highlighting = TRUE;
2869 attr = hl_attr(HLF_V);
2870#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2871 if (clip_star.available && !clip_star.owned && clip_isautosel())
2872 attr = hl_attr(HLF_VNC);
2873#endif
2874 }
2875 }
2876
2877 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002878 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 */
2880 else
2881#endif /* FEAT_VISUAL */
2882 if (highlight_match
2883 && wp == curwin
2884 && lnum >= curwin->w_cursor.lnum
2885 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2886 {
2887 if (lnum == curwin->w_cursor.lnum)
2888 getvcol(curwin, &(curwin->w_cursor),
2889 (colnr_T *)&fromcol, NULL, NULL);
2890 else
2891 fromcol = 0;
2892 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2893 {
2894 pos.lnum = lnum;
2895 pos.col = search_match_endcol;
2896 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2897 }
2898 else
2899 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00002900 /* do at least one character; happens when past end of line */
2901 if (fromcol == tocol)
2902 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 area_highlighting = TRUE;
2904 attr = hl_attr(HLF_I);
2905 }
2906
2907#ifdef FEAT_DIFF
2908 filler_lines = diff_check(wp, lnum);
2909 if (filler_lines < 0)
2910 {
2911 if (filler_lines == -1)
2912 {
2913 if (diff_find_change(wp, lnum, &change_start, &change_end))
2914 diff_hlf = HLF_ADD; /* added line */
2915 else if (change_start == 0)
2916 diff_hlf = HLF_TXD; /* changed text */
2917 else
2918 diff_hlf = HLF_CHD; /* changed line */
2919 }
2920 else
2921 diff_hlf = HLF_ADD; /* added line */
2922 filler_lines = 0;
2923 area_highlighting = TRUE;
2924 }
2925 if (lnum == wp->w_topline)
2926 filler_lines = wp->w_topfill;
2927 filler_todo = filler_lines;
2928#endif
2929
2930#ifdef LINE_ATTR
2931# ifdef FEAT_SIGNS
2932 /* If this line has a sign with line highlighting set line_attr. */
2933 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2934 if (v != 0)
2935 line_attr = sign_get_attr((int)v, TRUE);
2936# endif
2937# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2938 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002939 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 line_attr = hl_attr(HLF_L);
2941# endif
2942 if (line_attr != 0)
2943 area_highlighting = TRUE;
2944#endif
2945
2946 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2947 ptr = line;
2948
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002949#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002950 if (has_spell)
2951 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002952 /* For checking first word with a capital skip white space. */
2953 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002954 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002955
Bram Moolenaar30abd282005-06-22 22:35:10 +00002956 /* To be able to spell-check over line boundaries copy the end of the
2957 * current line into nextline[]. Above the start of the next line was
2958 * copied to nextline[SPWORDLEN]. */
2959 if (nextline[SPWORDLEN] == NUL)
2960 {
2961 /* No next line or it is empty. */
2962 nextlinecol = MAXCOL;
2963 nextline_idx = 0;
2964 }
2965 else
2966 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002967 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002968 if (v < SPWORDLEN)
2969 {
2970 /* Short line, use it completely and append the start of the
2971 * next line. */
2972 nextlinecol = 0;
2973 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00002974 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002975 nextline_idx = v + 1;
2976 }
2977 else
2978 {
2979 /* Long line, use only the last SPWORDLEN bytes. */
2980 nextlinecol = v - SPWORDLEN;
2981 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2982 nextline_idx = SPWORDLEN + 1;
2983 }
2984 }
2985 }
2986#endif
2987
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 /* find start of trailing whitespace */
2989 if (wp->w_p_list && lcs_trail)
2990 {
2991 trailcol = (colnr_T)STRLEN(ptr);
2992 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2993 --trailcol;
2994 trailcol += (colnr_T) (ptr - line);
2995 extra_check = TRUE;
2996 }
2997
2998 /*
2999 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3000 * first character to be displayed.
3001 */
3002 if (wp->w_p_wrap)
3003 v = wp->w_skipcol;
3004 else
3005 v = wp->w_leftcol;
3006 if (v > 0)
3007 {
3008#ifdef FEAT_MBYTE
3009 char_u *prev_ptr = ptr;
3010#endif
3011 while (vcol < v && *ptr != NUL)
3012 {
3013 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3014 vcol += c;
3015#ifdef FEAT_MBYTE
3016 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003018 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 }
3020
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003021#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3022 /* When:
3023 * - 'cuc' is set, or
3024 * - 'virtualedit' is set, or
3025 * - the visual mode is active,
3026 * the end of the line may be before the start of the displayed part.
3027 */
3028 if (vcol < v && (
3029# ifdef FEAT_SYN_HL
3030 wp->w_p_cuc
3031# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3032 ||
3033# endif
3034# endif
3035# ifdef FEAT_VIRTUALEDIT
3036 virtual_active()
3037# ifdef FEAT_VISUAL
3038 ||
3039# endif
3040# endif
3041# ifdef FEAT_VISUAL
3042 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3043# endif
3044 ))
3045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048#endif
3049
3050 /* Handle a character that's not completely on the screen: Put ptr at
3051 * that character but skip the first few screen characters. */
3052 if (vcol > v)
3053 {
3054 vcol -= c;
3055#ifdef FEAT_MBYTE
3056 ptr = prev_ptr;
3057#else
3058 --ptr;
3059#endif
3060 n_skip = v - vcol;
3061 }
3062
3063 /*
3064 * Adjust for when the inverted text is before the screen,
3065 * and when the start of the inverted text is before the screen.
3066 */
3067 if (tocol <= vcol)
3068 fromcol = 0;
3069 else if (fromcol >= 0 && fromcol < vcol)
3070 fromcol = vcol;
3071
3072#ifdef FEAT_LINEBREAK
3073 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3074 if (wp->w_p_wrap)
3075 need_showbreak = TRUE;
3076#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003077#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003078 /* When spell checking a word we need to figure out the start of the
3079 * word and if it's badly spelled or not. */
3080 if (has_spell)
3081 {
3082 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003083 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003084 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003085
3086 pos = wp->w_cursor;
3087 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003088 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003089 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003090
3091 /* spell_move_to() may call ml_get() and make "line" invalid */
3092 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3093 ptr = line + linecol;
3094
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003095 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003096 {
3097 /* no bad word found at line start, don't check until end of a
3098 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003099 spell_hlf = HLF_COUNT;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003100 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3101 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003102 }
3103 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003104 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003105 /* bad word found, use attributes until end of word */
3106 word_end = wp->w_cursor.col + len + 1;
3107
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003108 /* Turn index into actual attributes. */
3109 if (spell_hlf != HLF_COUNT)
3110 spell_attr = highlight_attr[spell_hlf];
3111 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003112 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003113
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003114# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003115 /* Need to restart syntax highlighting for this line. */
3116 if (has_syntax)
3117 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003118# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003119 }
3120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
3122
3123 /*
3124 * Correct highlighting for cursor that can't be disabled.
3125 * Avoids having to check this for each character.
3126 */
3127 if (fromcol >= 0)
3128 {
3129 if (noinvcur)
3130 {
3131 if ((colnr_T)fromcol == wp->w_virtcol)
3132 {
3133 /* highlighting starts at cursor, let it start just after the
3134 * cursor */
3135 fromcol_prev = fromcol;
3136 fromcol = -1;
3137 }
3138 else if ((colnr_T)fromcol < wp->w_virtcol)
3139 /* restart highlighting after the cursor */
3140 fromcol_prev = wp->w_virtcol;
3141 }
3142 if (fromcol >= tocol)
3143 fromcol = -1;
3144 }
3145
3146#ifdef FEAT_SEARCH_EXTRA
3147 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003148 * Handle highlighting the last used search pattern and matches.
3149 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003151 cur = wp->w_match_head;
3152 shl_flag = FALSE;
3153 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003155 if (shl_flag == FALSE)
3156 {
3157 shl = &search_hl;
3158 shl_flag = TRUE;
3159 }
3160 else
3161 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003162 shl->startcol = MAXCOL;
3163 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 shl->attr_cur = 0;
3165 if (shl->rm.regprog != NULL)
3166 {
3167 v = (long)(ptr - line);
3168 next_search_hl(wp, shl, lnum, (colnr_T)v);
3169
3170 /* Need to get the line again, a multi-line regexp may have made it
3171 * invalid. */
3172 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3173 ptr = line + v;
3174
3175 if (shl->lnum != 0 && shl->lnum <= lnum)
3176 {
3177 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003178 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003180 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3182 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003183 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003185 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003187 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 {
3189#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003190 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003191 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 else
3193#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003194 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003196 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
3198 shl->attr_cur = shl->attr;
3199 search_attr = shl->attr;
3200 }
3201 area_highlighting = TRUE;
3202 }
3203 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003204 if (shl != &search_hl && cur != NULL)
3205 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 }
3207#endif
3208
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003209#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003210 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3211 * active, because it's not clear what is selected then. */
3212 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003213 {
3214 line_attr = hl_attr(HLF_CUL);
3215 area_highlighting = TRUE;
3216 }
3217#endif
3218
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003219 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 col = 0;
3221#ifdef FEAT_RIGHTLEFT
3222 if (wp->w_p_rl)
3223 {
3224 /* Rightleft window: process the text in the normal direction, but put
3225 * it in current_ScreenLine[] from right to left. Start at the
3226 * rightmost column of the window. */
3227 col = W_WIDTH(wp) - 1;
3228 off += col;
3229 }
3230#endif
3231
3232 /*
3233 * Repeat for the whole displayed line.
3234 */
3235 for (;;)
3236 {
3237 /* Skip this quickly when working on the text. */
3238 if (draw_state != WL_LINE)
3239 {
3240#ifdef FEAT_CMDWIN
3241 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3242 {
3243 draw_state = WL_CMDLINE;
3244 if (cmdwin_type != 0 && wp == curwin)
3245 {
3246 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003248 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 char_attr = hl_attr(HLF_AT);
3250 }
3251 }
3252#endif
3253
3254#ifdef FEAT_FOLDING
3255 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3256 {
3257 draw_state = WL_FOLD;
3258 if (wp->w_p_fdc > 0)
3259 {
3260 /* Draw the 'foldcolumn'. */
3261 fill_foldcolumn(extra, wp, FALSE, lnum);
3262 n_extra = wp->w_p_fdc;
3263 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003264 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 c_extra = NUL;
3266 char_attr = hl_attr(HLF_FC);
3267 }
3268 }
3269#endif
3270
3271#ifdef FEAT_SIGNS
3272 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3273 {
3274 draw_state = WL_SIGN;
3275 /* Show the sign column when there are any signs in this
3276 * buffer or when using Netbeans. */
3277 if (draw_signcolumn(wp)
3278# ifdef FEAT_DIFF
3279 && filler_todo <= 0
3280# endif
3281 )
3282 {
3283 int_u text_sign;
3284# ifdef FEAT_SIGN_ICONS
3285 int_u icon_sign;
3286# endif
3287
3288 /* Draw two cells with the sign value or blank. */
3289 c_extra = ' ';
3290 char_attr = hl_attr(HLF_SC);
3291 n_extra = 2;
3292
3293 if (row == startrow)
3294 {
3295 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3296 SIGN_TEXT);
3297# ifdef FEAT_SIGN_ICONS
3298 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3299 SIGN_ICON);
3300 if (gui.in_use && icon_sign != 0)
3301 {
3302 /* Use the image in this position. */
3303 c_extra = SIGN_BYTE;
3304# ifdef FEAT_NETBEANS_INTG
3305 if (buf_signcount(wp->w_buffer, lnum) > 1)
3306 c_extra = MULTISIGN_BYTE;
3307# endif
3308 char_attr = icon_sign;
3309 }
3310 else
3311# endif
3312 if (text_sign != 0)
3313 {
3314 p_extra = sign_get_text(text_sign);
3315 if (p_extra != NULL)
3316 {
3317 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003318 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
3320 char_attr = sign_get_attr(text_sign, FALSE);
3321 }
3322 }
3323 }
3324 }
3325#endif
3326
3327 if (draw_state == WL_NR - 1 && n_extra == 0)
3328 {
3329 draw_state = WL_NR;
3330 /* Display the line number. After the first fill with blanks
3331 * when the 'n' flag isn't in 'cpo' */
3332 if (wp->w_p_nu
3333 && (row == startrow
3334#ifdef FEAT_DIFF
3335 + filler_lines
3336#endif
3337 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3338 {
3339 /* Draw the line number (empty space after wrapping). */
3340 if (row == startrow
3341#ifdef FEAT_DIFF
3342 + filler_lines
3343#endif
3344 )
3345 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003346 sprintf((char *)extra, "%*ld ",
3347 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 if (wp->w_skipcol > 0)
3349 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3350 *p_extra = '-';
3351#ifdef FEAT_RIGHTLEFT
3352 if (wp->w_p_rl) /* reverse line numbers */
3353 rl_mirror(extra);
3354#endif
3355 p_extra = extra;
3356 c_extra = NUL;
3357 }
3358 else
3359 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003360 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003362#ifdef FEAT_SYN_HL
3363 /* When 'cursorline' is set highlight the line number of
3364 * the current line differently. */
3365 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3366 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 }
3370
3371#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3372 if (draw_state == WL_SBR - 1 && n_extra == 0)
3373 {
3374 draw_state = WL_SBR;
3375# ifdef FEAT_DIFF
3376 if (filler_todo > 0)
3377 {
3378 /* Draw "deleted" diff line(s). */
3379 if (char2cells(fill_diff) > 1)
3380 c_extra = '-';
3381 else
3382 c_extra = fill_diff;
3383# ifdef FEAT_RIGHTLEFT
3384 if (wp->w_p_rl)
3385 n_extra = col + 1;
3386 else
3387# endif
3388 n_extra = W_WIDTH(wp) - col;
3389 char_attr = hl_attr(HLF_DED);
3390 }
3391# endif
3392# ifdef FEAT_LINEBREAK
3393 if (*p_sbr != NUL && need_showbreak)
3394 {
3395 /* Draw 'showbreak' at the start of each broken line. */
3396 p_extra = p_sbr;
3397 c_extra = NUL;
3398 n_extra = (int)STRLEN(p_sbr);
3399 char_attr = hl_attr(HLF_AT);
3400 need_showbreak = FALSE;
3401 /* Correct end of highlighted area for 'showbreak',
3402 * required when 'linebreak' is also set. */
3403 if (tocol == vcol)
3404 tocol += n_extra;
3405 }
3406# endif
3407 }
3408#endif
3409
3410 if (draw_state == WL_LINE - 1 && n_extra == 0)
3411 {
3412 draw_state = WL_LINE;
3413 if (saved_n_extra)
3414 {
3415 /* Continue item from end of wrapped line. */
3416 n_extra = saved_n_extra;
3417 c_extra = saved_c_extra;
3418 p_extra = saved_p_extra;
3419 char_attr = saved_char_attr;
3420 }
3421 else
3422 char_attr = 0;
3423 }
3424 }
3425
3426 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003427 if (dollar_vcol != 0 && wp == curwin
3428 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#ifdef FEAT_DIFF
3430 && filler_todo <= 0
3431#endif
3432 )
3433 {
3434 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3435 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003436 /* Pretend we have finished updating the window. Except when
3437 * 'cursorcolumn' is set. */
3438#ifdef FEAT_SYN_HL
3439 if (wp->w_p_cuc)
3440 row = wp->w_cline_row + wp->w_cline_height;
3441 else
3442#endif
3443 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 break;
3445 }
3446
3447 if (draw_state == WL_LINE && area_highlighting)
3448 {
3449 /* handle Visual or match highlighting in this line */
3450 if (vcol == fromcol
3451#ifdef FEAT_MBYTE
3452 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3453 && (*mb_ptr2cells)(ptr) > 1)
3454#endif
3455 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003456 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 && vcol < tocol))
3458 area_attr = attr; /* start highlighting */
3459 else if (area_attr != 0
3460 && (vcol == tocol
3461 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464#ifdef FEAT_SEARCH_EXTRA
3465 if (!n_extra)
3466 {
3467 /*
3468 * Check for start/end of search pattern match.
3469 * After end, check for start/end of next match.
3470 * When another match, have to check for start again.
3471 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003472 * Do this for 'search_hl' and the match list (ordered by
3473 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003475 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003476 cur = wp->w_match_head;
3477 shl_flag = FALSE;
3478 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003480 if (shl_flag == FALSE
3481 && ((cur != NULL
3482 && cur->priority > SEARCH_HL_PRIORITY)
3483 || cur == NULL))
3484 {
3485 shl = &search_hl;
3486 shl_flag = TRUE;
3487 }
3488 else
3489 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 while (shl->rm.regprog != NULL)
3491 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003492 if (shl->startcol != MAXCOL
3493 && v >= (long)shl->startcol
3494 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 shl->attr_cur = shl->attr;
3497 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003498 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
3500 shl->attr_cur = 0;
3501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 next_search_hl(wp, shl, lnum, (colnr_T)v);
3503
3504 /* Need to get the line again, a multi-line regexp
3505 * may have made it invalid. */
3506 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3507 ptr = line + v;
3508
3509 if (shl->lnum == lnum)
3510 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003511 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003513 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003515 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003517 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
3519 /* highlight empty match, try again after
3520 * it */
3521#ifdef FEAT_MBYTE
3522 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003523 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003524 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 else
3526#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003527 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529
3530 /* Loop to check if the match starts at the
3531 * current position */
3532 continue;
3533 }
3534 }
3535 break;
3536 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003537 if (shl != &search_hl && cur != NULL)
3538 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003540
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003541 /* Use attributes from match with highest priority among
3542 * 'search_hl' and the match list. */
3543 search_attr = search_hl.attr_cur;
3544 cur = wp->w_match_head;
3545 shl_flag = FALSE;
3546 while (cur != NULL || shl_flag == FALSE)
3547 {
3548 if (shl_flag == FALSE
3549 && ((cur != NULL
3550 && cur->priority > SEARCH_HL_PRIORITY)
3551 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003552 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003553 shl = &search_hl;
3554 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003555 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003556 else
3557 shl = &cur->hl;
3558 if (shl->attr_cur != 0)
3559 search_attr = shl->attr_cur;
3560 if (shl != &search_hl && cur != NULL)
3561 cur = cur->next;
3562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564#endif
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003567 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003569 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3570 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003572 if (diff_hlf == HLF_TXD && ptr - line > change_end
3573 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003575 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003578
3579 /* Decide which of the highlight attributes to use. */
3580 attr_pri = TRUE;
3581 if (area_attr != 0)
3582 char_attr = area_attr;
3583 else if (search_attr != 0)
3584 char_attr = search_attr;
3585#ifdef LINE_ATTR
3586 /* Use line_attr when not in the Visual or 'incsearch' area
3587 * (area_attr may be 0 when "noinvcur" is set). */
3588 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003589 || vcol < fromcol || vcol_prev < fromcol_prev
3590 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003591 char_attr = line_attr;
3592#endif
3593 else
3594 {
3595 attr_pri = FALSE;
3596#ifdef FEAT_SYN_HL
3597 if (has_syntax)
3598 char_attr = syntax_attr;
3599 else
3600#endif
3601 char_attr = 0;
3602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
3604
3605 /*
3606 * Get the next character to put on the screen.
3607 */
3608 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003609 * The "p_extra" points to the extra stuff that is inserted to
3610 * represent special characters (non-printable stuff) and other
3611 * things. When all characters are the same, c_extra is used.
3612 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3613 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3615 */
3616 if (n_extra > 0)
3617 {
3618 if (c_extra != NUL)
3619 {
3620 c = c_extra;
3621#ifdef FEAT_MBYTE
3622 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3623 if (enc_utf8 && (*mb_char2len)(c) > 1)
3624 {
3625 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003626 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003627 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629 else
3630 mb_utf8 = FALSE;
3631#endif
3632 }
3633 else
3634 {
3635 c = *p_extra;
3636#ifdef FEAT_MBYTE
3637 if (has_mbyte)
3638 {
3639 mb_c = c;
3640 if (enc_utf8)
3641 {
3642 /* If the UTF-8 character is more than one byte:
3643 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003644 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 mb_utf8 = FALSE;
3646 if (mb_l > n_extra)
3647 mb_l = 1;
3648 else if (mb_l > 1)
3649 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003650 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003652 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
3654 }
3655 else
3656 {
3657 /* if this is a DBCS character, put it in "mb_c" */
3658 mb_l = MB_BYTE2LEN(c);
3659 if (mb_l >= n_extra)
3660 mb_l = 1;
3661 else if (mb_l > 1)
3662 mb_c = (c << 8) + p_extra[1];
3663 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003664 if (mb_l == 0) /* at the NUL at end-of-line */
3665 mb_l = 1;
3666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 /* If a double-width char doesn't fit display a '>' in the
3668 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003669 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670# ifdef FEAT_RIGHTLEFT
3671 wp->w_p_rl ? (col <= 0) :
3672# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003673 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 && (*mb_char2cells)(mb_c) == 2)
3675 {
3676 c = '>';
3677 mb_c = c;
3678 mb_l = 1;
3679 mb_utf8 = FALSE;
3680 multi_attr = hl_attr(HLF_AT);
3681 /* put the pointer back to output the double-width
3682 * character at the start of the next line. */
3683 ++n_extra;
3684 --p_extra;
3685 }
3686 else
3687 {
3688 n_extra -= mb_l - 1;
3689 p_extra += mb_l - 1;
3690 }
3691 }
3692#endif
3693 ++p_extra;
3694 }
3695 --n_extra;
3696 }
3697 else
3698 {
3699 /*
3700 * Get a character from the line itself.
3701 */
3702 c = *ptr;
3703#ifdef FEAT_MBYTE
3704 if (has_mbyte)
3705 {
3706 mb_c = c;
3707 if (enc_utf8)
3708 {
3709 /* If the UTF-8 character is more than one byte: Decode it
3710 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003711 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 mb_utf8 = FALSE;
3713 if (mb_l > 1)
3714 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003715 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 /* Overlong encoded ASCII or ASCII with composing char
3717 * is displayed normally, except a NUL. */
3718 if (mb_c < 0x80)
3719 c = mb_c;
3720 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003721
3722 /* At start of the line we can have a composing char.
3723 * Draw it as a space with a composing char. */
3724 if (utf_iscomposing(mb_c))
3725 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003726 int i;
3727
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003728 for (i = Screen_mco - 1; i > 0; --i)
3729 u8cc[i] = u8cc[i - 1];
3730 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003731 mb_c = ' ';
3732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
3734
3735 if ((mb_l == 1 && c >= 0x80)
3736 || (mb_l >= 1 && mb_c == 0)
3737 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003738# ifdef UNICODE16
3739 || mb_c >= 0x10000
3740# endif
3741 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
3743 /*
3744 * Illegal UTF-8 byte: display as <xx>.
3745 * Non-BMP character : display as ? or fullwidth ?.
3746 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003747# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
3751 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003752# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (wp->w_p_rl) /* reverse */
3754 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003755# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003757# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 else if (utf_char2cells(mb_c) != 2)
3759 STRCPY(extra, "?");
3760 else
3761 /* 0xff1f in UTF-8: full-width '?' */
3762 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 p_extra = extra;
3766 c = *p_extra;
3767 mb_c = mb_ptr2char_adv(&p_extra);
3768 mb_utf8 = (c >= 0x80);
3769 n_extra = (int)STRLEN(p_extra);
3770 c_extra = NUL;
3771 if (area_attr == 0 && search_attr == 0)
3772 {
3773 n_attr = n_extra + 1;
3774 extra_attr = hl_attr(HLF_8);
3775 saved_attr2 = char_attr; /* save current attr */
3776 }
3777 }
3778 else if (mb_l == 0) /* at the NUL at end-of-line */
3779 mb_l = 1;
3780#ifdef FEAT_ARABIC
3781 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3782 {
3783 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003784 int pc, pc1, nc;
3785 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
3787 /* The idea of what is the previous and next
3788 * character depends on 'rightleft'. */
3789 if (wp->w_p_rl)
3790 {
3791 pc = prev_c;
3792 pc1 = prev_c1;
3793 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003794 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796 else
3797 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003798 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003800 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 prev_c = mb_c;
3803
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003804 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806 else
3807 prev_c = mb_c;
3808#endif
3809 }
3810 else /* enc_dbcs */
3811 {
3812 mb_l = MB_BYTE2LEN(c);
3813 if (mb_l == 0) /* at the NUL at end-of-line */
3814 mb_l = 1;
3815 else if (mb_l > 1)
3816 {
3817 /* We assume a second byte below 32 is illegal.
3818 * Hopefully this is OK for all double-byte encodings!
3819 */
3820 if (ptr[1] >= 32)
3821 mb_c = (c << 8) + ptr[1];
3822 else
3823 {
3824 if (ptr[1] == NUL)
3825 {
3826 /* head byte at end of line */
3827 mb_l = 1;
3828 transchar_nonprint(extra, c);
3829 }
3830 else
3831 {
3832 /* illegal tail byte */
3833 mb_l = 2;
3834 STRCPY(extra, "XX");
3835 }
3836 p_extra = extra;
3837 n_extra = (int)STRLEN(extra) - 1;
3838 c_extra = NUL;
3839 c = *p_extra++;
3840 if (area_attr == 0 && search_attr == 0)
3841 {
3842 n_attr = n_extra + 1;
3843 extra_attr = hl_attr(HLF_8);
3844 saved_attr2 = char_attr; /* save current attr */
3845 }
3846 mb_c = c;
3847 }
3848 }
3849 }
3850 /* If a double-width char doesn't fit display a '>' in the
3851 * last column; the character is displayed at the start of the
3852 * next line. */
3853 if ((
3854# ifdef FEAT_RIGHTLEFT
3855 wp->w_p_rl ? (col <= 0) :
3856# endif
3857 (col >= W_WIDTH(wp) - 1))
3858 && (*mb_char2cells)(mb_c) == 2)
3859 {
3860 c = '>';
3861 mb_c = c;
3862 mb_utf8 = FALSE;
3863 mb_l = 1;
3864 multi_attr = hl_attr(HLF_AT);
3865 /* Put pointer back so that the character will be
3866 * displayed at the start of the next line. */
3867 --ptr;
3868 }
3869 else if (*ptr != NUL)
3870 ptr += mb_l - 1;
3871
3872 /* If a double-width char doesn't fit at the left side display
3873 * a '<' in the first column. */
3874 if (n_skip > 0 && mb_l > 1)
3875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003877 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 c = ' ';
3879 if (area_attr == 0 && search_attr == 0)
3880 {
3881 n_attr = n_extra + 1;
3882 extra_attr = hl_attr(HLF_AT);
3883 saved_attr2 = char_attr; /* save current attr */
3884 }
3885 mb_c = c;
3886 mb_utf8 = FALSE;
3887 mb_l = 1;
3888 }
3889
3890 }
3891#endif
3892 ++ptr;
3893
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003894 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003895 if (wp->w_p_list && (c == 160
3896#ifdef FEAT_MBYTE
3897 || (mb_utf8 && mb_c == 160)
3898#endif
3899 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003900 {
3901 c = lcs_nbsp;
3902 if (area_attr == 0 && search_attr == 0)
3903 {
3904 n_attr = 1;
3905 extra_attr = hl_attr(HLF_8);
3906 saved_attr2 = char_attr; /* save current attr */
3907 }
3908#ifdef FEAT_MBYTE
3909 mb_c = c;
3910 if (enc_utf8 && (*mb_char2len)(c) > 1)
3911 {
3912 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003913 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003914 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003915 }
3916 else
3917 mb_utf8 = FALSE;
3918#endif
3919 }
3920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 if (extra_check)
3922 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003923#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003924 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003925#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003926
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003927#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 /* Get syntax attribute, unless still at the start of the line
3929 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003930 v = (long)(ptr - line);
3931 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
3933 /* Get the syntax attribute for the character. If there
3934 * is an error, disable syntax highlighting. */
3935 save_did_emsg = did_emsg;
3936 did_emsg = FALSE;
3937
Bram Moolenaar217ad922005-03-20 22:37:15 +00003938 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003939# ifdef FEAT_SPELL
3940 has_spell ? &can_spell :
3941# endif
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00003942 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003945 {
3946 wp->w_buffer->b_syn_error = TRUE;
3947 has_syntax = FALSE;
3948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 else
3950 did_emsg = save_did_emsg;
3951
3952 /* Need to get the line again, a multi-line regexp may
3953 * have made it invalid. */
3954 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3955 ptr = line + v;
3956
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003957 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003959 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003960 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003962#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003963
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003964#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003965 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003966 * Only do this when there is no syntax highlighting, the
3967 * @Spell cluster is not used or the current syntax item
3968 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003969 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003970 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003971 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003972# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003973 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003974 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003975# endif
3976 if (c != 0 && (
3977# ifdef FEAT_SYN_HL
3978 !has_syntax ||
3979# endif
3980 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003981 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003982 char_u *prev_ptr, *p;
3983 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003984 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003985# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003986 if (has_mbyte)
3987 {
3988 prev_ptr = ptr - mb_l;
3989 v -= mb_l - 1;
3990 }
3991 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003992# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003993 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003994
3995 /* Use nextline[] if possible, it has the start of the
3996 * next line concatenated. */
3997 if ((prev_ptr - line) - nextlinecol >= 0)
3998 p = nextline + (prev_ptr - line) - nextlinecol;
3999 else
4000 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004001 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004002 len = spell_check(wp, p, &spell_hlf, &cap_col,
4003 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004004 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004005
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004006 /* In Insert mode only highlight a word that
4007 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004008 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004009 && (State & INSERT) != 0
4010 && wp->w_cursor.lnum == lnum
4011 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004012 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004013 && wp->w_cursor.col < (colnr_T)word_end)
4014 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004015 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004016 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004017 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004018
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004019 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004020 && (p - nextline) + len > nextline_idx)
4021 {
4022 /* Remember that the good word continues at the
4023 * start of the next line. */
4024 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004025 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004026 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004027
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004028 /* Turn index into actual attributes. */
4029 if (spell_hlf != HLF_COUNT)
4030 spell_attr = highlight_attr[spell_hlf];
4031
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004032 if (cap_col > 0)
4033 {
4034 if (p != prev_ptr
4035 && (p - nextline) + cap_col >= nextline_idx)
4036 {
4037 /* Remember that the word in the next line
4038 * must start with a capital. */
4039 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004040 cap_col = (int)((p - nextline) + cap_col
4041 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004042 }
4043 else
4044 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004045 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004046 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004047 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004048 }
4049 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004050 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004051 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004052 char_attr = hl_combine_attr(char_attr, spell_attr);
4053 else
4054 char_attr = hl_combine_attr(spell_attr, char_attr);
4055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#endif
4057#ifdef FEAT_LINEBREAK
4058 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004059 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 */
4061 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004062 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
4064 n_extra = win_lbr_chartabsize(wp, ptr - (
4065# ifdef FEAT_MBYTE
4066 has_mbyte ? mb_l :
4067# endif
4068 1), (colnr_T)vcol, NULL) - 1;
4069 c_extra = ' ';
4070 if (vim_iswhite(c))
4071 c = ' ';
4072 }
4073#endif
4074
4075 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4076 {
4077 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004078 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
4080 n_attr = 1;
4081 extra_attr = hl_attr(HLF_8);
4082 saved_attr2 = char_attr; /* save current attr */
4083 }
4084#ifdef FEAT_MBYTE
4085 mb_c = c;
4086 if (enc_utf8 && (*mb_char2len)(c) > 1)
4087 {
4088 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004089 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004090 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
4092 else
4093 mb_utf8 = FALSE;
4094#endif
4095 }
4096 }
4097
4098 /*
4099 * Handling of non-printable characters.
4100 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004101 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
4103 /*
4104 * when getting a character from the file, we may have to
4105 * turn it into something else on the way to putting it
4106 * into "ScreenLines".
4107 */
4108 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4109 {
4110 /* tab amount depends on current column */
4111 n_extra = (int)wp->w_buffer->b_p_ts
4112 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4113#ifdef FEAT_MBYTE
4114 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4115#endif
4116 if (wp->w_p_list)
4117 {
4118 c = lcs_tab1;
4119 c_extra = lcs_tab2;
4120 n_attr = n_extra + 1;
4121 extra_attr = hl_attr(HLF_8);
4122 saved_attr2 = char_attr; /* save current attr */
4123#ifdef FEAT_MBYTE
4124 mb_c = c;
4125 if (enc_utf8 && (*mb_char2len)(c) > 1)
4126 {
4127 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004128 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004129 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131#endif
4132 }
4133 else
4134 {
4135 c_extra = ' ';
4136 c = ' ';
4137 }
4138 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004139 else if (c == NUL
4140 && ((wp->w_p_list && lcs_eol > 0)
4141 || ((fromcol >= 0 || fromcol_prev >= 0)
4142 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004143#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004144 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004145#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004146 && (
4147# ifdef FEAT_RIGHTLEFT
4148 wp->w_p_rl ? (col >= 0) :
4149# endif
4150 (col < W_WIDTH(wp)))
4151 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004152 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004153 && (colnr_T)vcol == wp->w_virtcol)))
4154 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004156 /* Display a '$' after the line or highlight an extra
4157 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4159 /* For a diff line the highlighting continues after the
4160 * "$". */
4161 if (
4162# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004163 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164# ifdef LINE_ATTR
4165 &&
4166# endif
4167# endif
4168# ifdef LINE_ATTR
4169 line_attr == 0
4170# endif
4171 )
4172#endif
4173 {
4174#ifdef FEAT_VIRTUALEDIT
4175 /* In virtualedit, visual selections may extend
4176 * beyond end of line. */
4177 if (area_highlighting && virtual_active()
4178 && tocol != MAXCOL && vcol < tocol)
4179 n_extra = 0;
4180 else
4181#endif
4182 {
4183 p_extra = at_end_str;
4184 n_extra = 1;
4185 c_extra = NUL;
4186 }
4187 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004188 if (wp->w_p_list)
4189 c = lcs_eol;
4190 else
4191 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 lcs_eol_one = -1;
4193 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004194 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
4196 extra_attr = hl_attr(HLF_AT);
4197 n_attr = 1;
4198 }
4199#ifdef FEAT_MBYTE
4200 mb_c = c;
4201 if (enc_utf8 && (*mb_char2len)(c) > 1)
4202 {
4203 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004204 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004205 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207 else
4208 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4209#endif
4210 }
4211 else if (c != NUL)
4212 {
4213 p_extra = transchar(c);
4214#ifdef FEAT_RIGHTLEFT
4215 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4216 rl_mirror(p_extra); /* reverse "<12>" */
4217#endif
4218 n_extra = byte2cells(c) - 1;
4219 c_extra = NUL;
4220 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004221 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
4223 n_attr = n_extra + 1;
4224 extra_attr = hl_attr(HLF_8);
4225 saved_attr2 = char_attr; /* save current attr */
4226 }
4227#ifdef FEAT_MBYTE
4228 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4229#endif
4230 }
4231#ifdef FEAT_VIRTUALEDIT
4232 else if (VIsual_active
4233 && (VIsual_mode == Ctrl_V
4234 || VIsual_mode == 'v')
4235 && virtual_active()
4236 && tocol != MAXCOL
4237 && vcol < tocol
4238 && (
4239# ifdef FEAT_RIGHTLEFT
4240 wp->w_p_rl ? (col >= 0) :
4241# endif
4242 (col < W_WIDTH(wp))))
4243 {
4244 c = ' ';
4245 --ptr; /* put it back at the NUL */
4246 }
4247#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004248#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 else if ((
4250# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004251 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 ) && (
4255# ifdef FEAT_RIGHTLEFT
4256 wp->w_p_rl ? (col >= 0) :
4257# endif
4258 (col < W_WIDTH(wp))))
4259 {
4260 /* Highlight until the right side of the window */
4261 c = ' ';
4262 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004263
4264 /* Remember we do the char for line highlighting. */
4265 ++did_line_attr;
4266
4267 /* don't do search HL for the rest of the line */
4268 if (line_attr != 0 && char_attr == search_attr && col > 0)
4269 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270# ifdef FEAT_DIFF
4271 if (diff_hlf == HLF_TXD)
4272 {
4273 diff_hlf = HLF_CHD;
4274 if (attr == 0 || char_attr != attr)
4275 char_attr = hl_attr(diff_hlf);
4276 }
4277# endif
4278 }
4279#endif
4280 }
4281 }
4282
4283 /* Don't override visual selection highlighting. */
4284 if (n_attr > 0
4285 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004286 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 char_attr = extra_attr;
4288
Bram Moolenaar81695252004-12-29 20:58:21 +00004289#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 /* XIM don't send preedit_start and preedit_end, but they send
4291 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4292 * im_is_preediting() here. */
4293 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004294 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 && (State & INSERT)
4296 && !p_imdisable
4297 && im_is_preediting()
4298 && draw_state == WL_LINE)
4299 {
4300 colnr_T tcol;
4301
4302 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004303 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 else
4305 tcol = preedit_end_col;
4306 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4307 {
4308 if (feedback_old_attr < 0)
4309 {
4310 feedback_col = 0;
4311 feedback_old_attr = char_attr;
4312 }
4313 char_attr = im_get_feedback_attr(feedback_col);
4314 if (char_attr < 0)
4315 char_attr = feedback_old_attr;
4316 feedback_col++;
4317 }
4318 else if (feedback_old_attr >= 0)
4319 {
4320 char_attr = feedback_old_attr;
4321 feedback_old_attr = -1;
4322 feedback_col = 0;
4323 }
4324 }
4325#endif
4326 /*
4327 * Handle the case where we are in column 0 but not on the first
4328 * character of the line and the user wants us to show us a
4329 * special character (via 'listchars' option "precedes:<char>".
4330 */
4331 if (lcs_prec_todo != NUL
4332 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4333#ifdef FEAT_DIFF
4334 && filler_todo <= 0
4335#endif
4336 && draw_state > WL_NR
4337 && c != NUL)
4338 {
4339 c = lcs_prec;
4340 lcs_prec_todo = NUL;
4341#ifdef FEAT_MBYTE
4342 mb_c = c;
4343 if (enc_utf8 && (*mb_char2len)(c) > 1)
4344 {
4345 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004346 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004347 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 else
4350 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4351#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004352 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
4354 saved_attr3 = char_attr; /* save current attr */
4355 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4356 n_attr3 = 1;
4357 }
4358 }
4359
4360 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004361 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004363 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004364#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004365 || did_line_attr == 1
4366#endif
4367 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004369#ifdef FEAT_SEARCH_EXTRA
4370 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004371
4372 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004373 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004374 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004375#endif
4376
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 /* invert at least one char, used for Visual and empty line or
4378 * highlight match at end of line. If it's beyond the last
4379 * char on the screen, just overwrite that one (tricky!) Not
4380 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004381#ifdef FEAT_SEARCH_EXTRA
4382 prevcol_hl_flag = FALSE;
4383 if (prevcol == (long)search_hl.startcol)
4384 prevcol_hl_flag = TRUE;
4385 else
4386 {
4387 cur = wp->w_match_head;
4388 while (cur != NULL)
4389 {
4390 if (prevcol == (long)cur->hl.startcol)
4391 {
4392 prevcol_hl_flag = TRUE;
4393 break;
4394 }
4395 cur = cur->next;
4396 }
4397 }
4398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004400 && ((area_attr != 0 && vcol == fromcol
4401#ifdef FEAT_VISUAL
4402 && (VIsual_mode != Ctrl_V
4403 || lnum == VIsual.lnum
4404 || lnum == curwin->w_cursor.lnum)
4405#endif
4406 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407#ifdef FEAT_SEARCH_EXTRA
4408 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004409 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004410# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004411 && did_line_attr <= 1
4412# endif
4413 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414#endif
4415 ))
4416 {
4417 int n = 0;
4418
4419#ifdef FEAT_RIGHTLEFT
4420 if (wp->w_p_rl)
4421 {
4422 if (col < 0)
4423 n = 1;
4424 }
4425 else
4426#endif
4427 {
4428 if (col >= W_WIDTH(wp))
4429 n = -1;
4430 }
4431 if (n != 0)
4432 {
4433 /* At the window boundary, highlight the last character
4434 * instead (better than nothing). */
4435 off += n;
4436 col += n;
4437 }
4438 else
4439 {
4440 /* Add a blank character to highlight. */
4441 ScreenLines[off] = ' ';
4442#ifdef FEAT_MBYTE
4443 if (enc_utf8)
4444 ScreenLinesUC[off] = 0;
4445#endif
4446 }
4447#ifdef FEAT_SEARCH_EXTRA
4448 if (area_attr == 0)
4449 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004450 /* Use attributes from match with highest priority among
4451 * 'search_hl' and the match list. */
4452 char_attr = search_hl.attr;
4453 cur = wp->w_match_head;
4454 shl_flag = FALSE;
4455 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004456 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004457 if (shl_flag == FALSE
4458 && ((cur != NULL
4459 && cur->priority > SEARCH_HL_PRIORITY)
4460 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004461 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004462 shl = &search_hl;
4463 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004464 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004465 else
4466 shl = &cur->hl;
4467 if ((ptr - line) - 1 == (long)shl->startcol)
4468 char_attr = shl->attr;
4469 if (shl != &search_hl && cur != NULL)
4470 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473#endif
4474 ScreenAttrs[off] = char_attr;
4475#ifdef FEAT_RIGHTLEFT
4476 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004479 --off;
4480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 else
4482#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004485 ++off;
4486 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004487 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004488#ifdef FEAT_SYN_HL
4489 eol_hl_off = 1;
4490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar91170f82006-05-05 21:15:17 +00004494 /*
4495 * At end of the text line.
4496 */
4497 if (c == NUL)
4498 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004499#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004500 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4501 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004502 {
4503 /* highlight last char after line */
4504 --col;
4505 --off;
4506 --vcol;
4507 }
4508
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004509 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004510 if (wp->w_p_wrap)
4511 v = wp->w_skipcol;
4512 else
4513 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004514 /* check if line ends before left margin */
4515 if (vcol < v + col - win_col_off(wp))
4516
4517 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004518 if (wp->w_p_cuc
Bram Moolenaara443af82007-11-08 13:51:42 +00004519 && (int)wp->w_virtcol >= vcol - eol_hl_off
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004520 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4521 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004522 && lnum != wp->w_cursor.lnum
4523# ifdef FEAT_RIGHTLEFT
4524 && !wp->w_p_rl
4525# endif
4526 )
4527 {
4528 while (col < W_WIDTH(wp))
4529 {
4530 ScreenLines[off] = ' ';
4531#ifdef FEAT_MBYTE
4532 if (enc_utf8)
4533 ScreenLinesUC[off] = 0;
4534#endif
4535 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004536 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004537 {
4538 ScreenAttrs[off] = hl_attr(HLF_CUC);
4539 break;
4540 }
4541 ScreenAttrs[off++] = 0;
4542 ++vcol;
4543 }
4544 }
4545#endif
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4548 wp->w_p_rl);
4549 row++;
4550
4551 /*
4552 * Update w_cline_height and w_cline_folded if the cursor line was
4553 * updated (saves a call to plines() later).
4554 */
4555 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4556 {
4557 curwin->w_cline_row = startrow;
4558 curwin->w_cline_height = row - startrow;
4559#ifdef FEAT_FOLDING
4560 curwin->w_cline_folded = FALSE;
4561#endif
4562 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4563 }
4564
4565 break;
4566 }
4567
4568 /* line continues beyond line end */
4569 if (lcs_ext
4570 && !wp->w_p_wrap
4571#ifdef FEAT_DIFF
4572 && filler_todo <= 0
4573#endif
4574 && (
4575#ifdef FEAT_RIGHTLEFT
4576 wp->w_p_rl ? col == 0 :
4577#endif
4578 col == W_WIDTH(wp) - 1)
4579 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004580 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4582 {
4583 c = lcs_ext;
4584 char_attr = hl_attr(HLF_AT);
4585#ifdef FEAT_MBYTE
4586 mb_c = c;
4587 if (enc_utf8 && (*mb_char2len)(c) > 1)
4588 {
4589 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004590 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004591 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 else
4594 mb_utf8 = FALSE;
4595#endif
4596 }
4597
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004598#ifdef FEAT_SYN_HL
4599 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4600 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004601 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004602 && lnum != wp->w_cursor.lnum
Bram Moolenaar54ef7112009-02-21 20:11:41 +00004603 && draw_state == WL_LINE
4604 && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004605 {
4606 vcol_save_attr = char_attr;
4607 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4608 }
4609 else
4610 vcol_save_attr = -1;
4611#endif
4612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /*
4614 * Store character to be displayed.
4615 * Skip characters that are left of the screen for 'nowrap'.
4616 */
4617 vcol_prev = vcol;
4618 if (draw_state < WL_LINE || n_skip <= 0)
4619 {
4620 /*
4621 * Store the character.
4622 */
4623#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4624 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4625 {
4626 /* A double-wide character is: put first halve in left cell. */
4627 --off;
4628 --col;
4629 }
4630#endif
4631 ScreenLines[off] = c;
4632#ifdef FEAT_MBYTE
4633 if (enc_dbcs == DBCS_JPNU)
4634 ScreenLines2[off] = mb_c & 0xff;
4635 else if (enc_utf8)
4636 {
4637 if (mb_utf8)
4638 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004639 int i;
4640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004642 if ((c & 0xff) == 0)
4643 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004644 for (i = 0; i < Screen_mco; ++i)
4645 {
4646 ScreenLinesC[i][off] = u8cc[i];
4647 if (u8cc[i] == 0)
4648 break;
4649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651 else
4652 ScreenLinesUC[off] = 0;
4653 }
4654 if (multi_attr)
4655 {
4656 ScreenAttrs[off] = multi_attr;
4657 multi_attr = 0;
4658 }
4659 else
4660#endif
4661 ScreenAttrs[off] = char_attr;
4662
4663#ifdef FEAT_MBYTE
4664 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4665 {
4666 /* Need to fill two screen columns. */
4667 ++off;
4668 ++col;
4669 if (enc_utf8)
4670 /* UTF-8: Put a 0 in the second screen char. */
4671 ScreenLines[off] = 0;
4672 else
4673 /* DBCS: Put second byte in the second screen char. */
4674 ScreenLines[off] = mb_c & 0xff;
4675 ++vcol;
4676 /* When "tocol" is halfway a character, set it to the end of
4677 * the character, otherwise highlighting won't stop. */
4678 if (tocol == vcol)
4679 ++tocol;
4680#ifdef FEAT_RIGHTLEFT
4681 if (wp->w_p_rl)
4682 {
4683 /* now it's time to backup one cell */
4684 --off;
4685 --col;
4686 }
4687#endif
4688 }
4689#endif
4690#ifdef FEAT_RIGHTLEFT
4691 if (wp->w_p_rl)
4692 {
4693 --off;
4694 --col;
4695 }
4696 else
4697#endif
4698 {
4699 ++off;
4700 ++col;
4701 }
4702 }
4703 else
4704 --n_skip;
4705
4706 /* Only advance the "vcol" when after the 'number' column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00004707 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708#ifdef FEAT_DIFF
4709 && filler_todo <= 0
4710#endif
4711 )
4712 ++vcol;
4713
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004714#ifdef FEAT_SYN_HL
4715 if (vcol_save_attr >= 0)
4716 char_attr = vcol_save_attr;
4717#endif
4718
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 /* restore attributes after "predeces" in 'listchars' */
4720 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4721 char_attr = saved_attr3;
4722
4723 /* restore attributes after last 'listchars' or 'number' char */
4724 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4725 char_attr = saved_attr2;
4726
4727 /*
4728 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004729 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 */
4731 if ((
4732#ifdef FEAT_RIGHTLEFT
4733 wp->w_p_rl ? (col < 0) :
4734#endif
4735 (col >= W_WIDTH(wp)))
4736 && (*ptr != NUL
4737#ifdef FEAT_DIFF
4738 || filler_todo > 0
4739#endif
4740 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4741 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4742 )
4743 {
4744 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4745 wp->w_p_rl);
4746 ++row;
4747 ++screen_row;
4748
4749 /* When not wrapping and finished diff lines, or when displayed
4750 * '$' and highlighting until last column, break here. */
4751 if ((!wp->w_p_wrap
4752#ifdef FEAT_DIFF
4753 && filler_todo <= 0
4754#endif
4755 ) || lcs_eol_one == -1)
4756 break;
4757
4758 /* When the window is too narrow draw all "@" lines. */
4759 if (draw_state != WL_LINE
4760#ifdef FEAT_DIFF
4761 && filler_todo <= 0
4762#endif
4763 )
4764 {
4765 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4766#ifdef FEAT_VERTSPLIT
4767 draw_vsep_win(wp, row);
4768#endif
4769 row = endrow;
4770 }
4771
4772 /* When line got too long for screen break here. */
4773 if (row == endrow)
4774 {
4775 ++row;
4776 break;
4777 }
4778
4779 if (screen_cur_row == screen_row - 1
4780#ifdef FEAT_DIFF
4781 && filler_todo <= 0
4782#endif
4783 && W_WIDTH(wp) == Columns)
4784 {
4785 /* Remember that the line wraps, used for modeless copy. */
4786 LineWraps[screen_row - 1] = TRUE;
4787
4788 /*
4789 * Special trick to make copy/paste of wrapped lines work with
4790 * xterm/screen: write an extra character beyond the end of
4791 * the line. This will work with all terminal types
4792 * (regardless of the xn,am settings).
4793 * Only do this on a fast tty.
4794 * Only do this if the cursor is on the current line
4795 * (something has been written in it).
4796 * Don't do this for the GUI.
4797 * Don't do this for double-width characters.
4798 * Don't do this for a window not at the right screen border.
4799 */
4800 if (p_tf
4801#ifdef FEAT_GUI
4802 && !gui.in_use
4803#endif
4804#ifdef FEAT_MBYTE
4805 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004806 && ((*mb_off2cells)(LineOffset[screen_row],
4807 LineOffset[screen_row] + screen_Columns)
4808 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004810 + (int)Columns - 2,
4811 LineOffset[screen_row] + screen_Columns)
4812 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813#endif
4814 )
4815 {
4816 /* First make sure we are at the end of the screen line,
4817 * then output the same character again to let the
4818 * terminal know about the wrap. If the terminal doesn't
4819 * auto-wrap, we overwrite the character. */
4820 if (screen_cur_col != W_WIDTH(wp))
4821 screen_char(LineOffset[screen_row - 1]
4822 + (unsigned)Columns - 1,
4823 screen_row - 1, (int)(Columns - 1));
4824
4825#ifdef FEAT_MBYTE
4826 /* When there is a multi-byte character, just output a
4827 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004828 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4829 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 out_char(' ');
4831 else
4832#endif
4833 out_char(ScreenLines[LineOffset[screen_row - 1]
4834 + (Columns - 1)]);
4835 /* force a redraw of the first char on the next line */
4836 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4837 screen_start(); /* don't know where cursor is now */
4838 }
4839 }
4840
4841 col = 0;
4842 off = (unsigned)(current_ScreenLine - ScreenLines);
4843#ifdef FEAT_RIGHTLEFT
4844 if (wp->w_p_rl)
4845 {
4846 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4847 off += col;
4848 }
4849#endif
4850
4851 /* reset the drawing state for the start of a wrapped line */
4852 draw_state = WL_START;
4853 saved_n_extra = n_extra;
4854 saved_p_extra = p_extra;
4855 saved_c_extra = c_extra;
4856 saved_char_attr = char_attr;
4857 n_extra = 0;
4858 lcs_prec_todo = lcs_prec;
4859#ifdef FEAT_LINEBREAK
4860# ifdef FEAT_DIFF
4861 if (filler_todo <= 0)
4862# endif
4863 need_showbreak = TRUE;
4864#endif
4865#ifdef FEAT_DIFF
4866 --filler_todo;
4867 /* When the filler lines are actually below the last line of the
4868 * file, don't draw the line itself, break here. */
4869 if (filler_todo == 0 && wp->w_botfill)
4870 break;
4871#endif
4872 }
4873
4874 } /* for every character in the line */
4875
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004876#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004877 /* After an empty line check first word for capital. */
4878 if (*skipwhite(line) == NUL)
4879 {
4880 capcol_lnum = lnum + 1;
4881 cap_col = 0;
4882 }
4883#endif
4884
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 return row;
4886}
4887
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004888#ifdef FEAT_MBYTE
4889static int comp_char_differs __ARGS((int, int));
4890
4891/*
4892 * Return if the composing characters at "off_from" and "off_to" differ.
4893 */
4894 static int
4895comp_char_differs(off_from, off_to)
4896 int off_from;
4897 int off_to;
4898{
4899 int i;
4900
4901 for (i = 0; i < Screen_mco; ++i)
4902 {
4903 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4904 return TRUE;
4905 if (ScreenLinesC[i][off_from] == 0)
4906 break;
4907 }
4908 return FALSE;
4909}
4910#endif
4911
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912/*
4913 * Check whether the given character needs redrawing:
4914 * - the (first byte of the) character is different
4915 * - the attributes are different
4916 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004917 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 */
4919 static int
4920char_needs_redraw(off_from, off_to, cols)
4921 int off_from;
4922 int off_to;
4923 int cols;
4924{
4925 if (cols > 0
4926 && ((ScreenLines[off_from] != ScreenLines[off_to]
4927 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4928
4929#ifdef FEAT_MBYTE
4930 || (enc_dbcs != 0
4931 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4932 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4933 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4934 : (cols > 1 && ScreenLines[off_from + 1]
4935 != ScreenLines[off_to + 1])))
4936 || (enc_utf8
4937 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4938 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004939 && comp_char_differs(off_from, off_to))
4940 || (cols > 1 && ScreenLines[off_from + 1]
4941 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#endif
4943 ))
4944 return TRUE;
4945 return FALSE;
4946}
4947
4948/*
4949 * Move one "cooked" screen line to the screen, but only the characters that
4950 * have actually changed. Handle insert/delete character.
4951 * "coloff" gives the first column on the screen for this line.
4952 * "endcol" gives the columns where valid characters are.
4953 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4954 * needs to be cleared, negative otherwise.
4955 * "rlflag" is TRUE in a rightleft window:
4956 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4957 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4958 */
4959 static void
4960screen_line(row, coloff, endcol, clear_width
4961#ifdef FEAT_RIGHTLEFT
4962 , rlflag
4963#endif
4964 )
4965 int row;
4966 int coloff;
4967 int endcol;
4968 int clear_width;
4969#ifdef FEAT_RIGHTLEFT
4970 int rlflag;
4971#endif
4972{
4973 unsigned off_from;
4974 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004975#ifdef FEAT_MBYTE
4976 unsigned max_off_from;
4977 unsigned max_off_to;
4978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 int col = 0;
4980#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4981 int hl;
4982#endif
4983 int force = FALSE; /* force update rest of the line */
4984 int redraw_this /* bool: does character need redraw? */
4985#ifdef FEAT_GUI
4986 = TRUE /* For GUI when while-loop empty */
4987#endif
4988 ;
4989 int redraw_next; /* redraw_this for next character */
4990#ifdef FEAT_MBYTE
4991 int clear_next = FALSE;
4992 int char_cells; /* 1: normal char */
4993 /* 2: occupies two display cells */
4994# define CHAR_CELLS char_cells
4995#else
4996# define CHAR_CELLS 1
4997#endif
4998
4999# ifdef FEAT_CLIPBOARD
5000 clip_may_clear_selection(row, row);
5001# endif
5002
5003 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5004 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005005#ifdef FEAT_MBYTE
5006 max_off_from = off_from + screen_Columns;
5007 max_off_to = LineOffset[row] + screen_Columns;
5008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
5010#ifdef FEAT_RIGHTLEFT
5011 if (rlflag)
5012 {
5013 /* Clear rest first, because it's left of the text. */
5014 if (clear_width > 0)
5015 {
5016 while (col <= endcol && ScreenLines[off_to] == ' '
5017 && ScreenAttrs[off_to] == 0
5018# ifdef FEAT_MBYTE
5019 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5020# endif
5021 )
5022 {
5023 ++off_to;
5024 ++col;
5025 }
5026 if (col <= endcol)
5027 screen_fill(row, row + 1, col + coloff,
5028 endcol + coloff + 1, ' ', ' ', 0);
5029 }
5030 col = endcol + 1;
5031 off_to = LineOffset[row] + col + coloff;
5032 off_from += col;
5033 endcol = (clear_width > 0 ? clear_width : -clear_width);
5034 }
5035#endif /* FEAT_RIGHTLEFT */
5036
5037 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5038
5039 while (col < endcol)
5040 {
5041#ifdef FEAT_MBYTE
5042 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005043 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 else
5045 char_cells = 1;
5046#endif
5047
5048 redraw_this = redraw_next;
5049 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5050 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5051
5052#ifdef FEAT_GUI
5053 /* If the next character was bold, then redraw the current character to
5054 * remove any pixels that might have spilt over into us. This only
5055 * happens in the GUI.
5056 */
5057 if (redraw_next && gui.in_use)
5058 {
5059 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005060 if (hl > HL_ALL)
5061 hl = syn_attr2attr(hl);
5062 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 redraw_this = TRUE;
5064 }
5065#endif
5066
5067 if (redraw_this)
5068 {
5069 /*
5070 * Special handling when 'xs' termcap flag set (hpterm):
5071 * Attributes for characters are stored at the position where the
5072 * cursor is when writing the highlighting code. The
5073 * start-highlighting code must be written with the cursor on the
5074 * first highlighted character. The stop-highlighting code must
5075 * be written with the cursor just after the last highlighted
5076 * character.
5077 * Overwriting a character doesn't remove it's highlighting. Need
5078 * to clear the rest of the line, and force redrawing it
5079 * completely.
5080 */
5081 if ( p_wiv
5082 && !force
5083#ifdef FEAT_GUI
5084 && !gui.in_use
5085#endif
5086 && ScreenAttrs[off_to] != 0
5087 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5088 {
5089 /*
5090 * Need to remove highlighting attributes here.
5091 */
5092 windgoto(row, col + coloff);
5093 out_str(T_CE); /* clear rest of this screen line */
5094 screen_start(); /* don't know where cursor is now */
5095 force = TRUE; /* force redraw of rest of the line */
5096 redraw_next = TRUE; /* or else next char would miss out */
5097
5098 /*
5099 * If the previous character was highlighted, need to stop
5100 * highlighting at this character.
5101 */
5102 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5103 {
5104 screen_attr = ScreenAttrs[off_to - 1];
5105 term_windgoto(row, col + coloff);
5106 screen_stop_highlight();
5107 }
5108 else
5109 screen_attr = 0; /* highlighting has stopped */
5110 }
5111#ifdef FEAT_MBYTE
5112 if (enc_dbcs != 0)
5113 {
5114 /* Check if overwriting a double-byte with a single-byte or
5115 * the other way around requires another character to be
5116 * redrawn. For UTF-8 this isn't needed, because comparing
5117 * ScreenLinesUC[] is sufficient. */
5118 if (char_cells == 1
5119 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005120 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 /* Writing a single-cell character over a double-cell
5123 * character: need to redraw the next cell. */
5124 ScreenLines[off_to + 1] = 0;
5125 redraw_next = TRUE;
5126 }
5127 else if (char_cells == 2
5128 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005129 && (*mb_off2cells)(off_to, max_off_to) == 1
5130 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
5132 /* Writing the second half of a double-cell character over
5133 * a double-cell character: need to redraw the second
5134 * cell. */
5135 ScreenLines[off_to + 2] = 0;
5136 redraw_next = TRUE;
5137 }
5138
5139 if (enc_dbcs == DBCS_JPNU)
5140 ScreenLines2[off_to] = ScreenLines2[off_from];
5141 }
5142 /* When writing a single-width character over a double-width
5143 * character and at the end of the redrawn text, need to clear out
5144 * the right halve of the old character.
5145 * Also required when writing the right halve of a double-width
5146 * char over the left halve of an existing one. */
5147 if (has_mbyte && col + char_cells == endcol
5148 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005149 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005151 && (*mb_off2cells)(off_to, max_off_to) == 1
5152 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 clear_next = TRUE;
5154#endif
5155
5156 ScreenLines[off_to] = ScreenLines[off_from];
5157#ifdef FEAT_MBYTE
5158 if (enc_utf8)
5159 {
5160 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5161 if (ScreenLinesUC[off_from] != 0)
5162 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005163 int i;
5164
5165 for (i = 0; i < Screen_mco; ++i)
5166 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168 }
5169 if (char_cells == 2)
5170 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5171#endif
5172
5173#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005174 /* The bold trick makes a single column of pixels appear in the
5175 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 * character should be redrawn too. This happens for our own GUI
5177 * and for some xterms. */
5178 if (
5179# ifdef FEAT_GUI
5180 gui.in_use
5181# endif
5182# if defined(FEAT_GUI) && defined(UNIX)
5183 ||
5184# endif
5185# ifdef UNIX
5186 term_is_xterm
5187# endif
5188 )
5189 {
5190 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005191 if (hl > HL_ALL)
5192 hl = syn_attr2attr(hl);
5193 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 redraw_next = TRUE;
5195 }
5196#endif
5197 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5198#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005199 /* For simplicity set the attributes of second half of a
5200 * double-wide character equal to the first half. */
5201 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005203
5204 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 else
5207#endif
5208 screen_char(off_to, row, col + coloff);
5209 }
5210 else if ( p_wiv
5211#ifdef FEAT_GUI
5212 && !gui.in_use
5213#endif
5214 && col + coloff > 0)
5215 {
5216 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5217 {
5218 /*
5219 * Don't output stop-highlight when moving the cursor, it will
5220 * stop the highlighting when it should continue.
5221 */
5222 screen_attr = 0;
5223 }
5224 else if (screen_attr != 0)
5225 screen_stop_highlight();
5226 }
5227
5228 off_to += CHAR_CELLS;
5229 off_from += CHAR_CELLS;
5230 col += CHAR_CELLS;
5231 }
5232
5233#ifdef FEAT_MBYTE
5234 if (clear_next)
5235 {
5236 /* Clear the second half of a double-wide character of which the left
5237 * half was overwritten with a single-wide character. */
5238 ScreenLines[off_to] = ' ';
5239 if (enc_utf8)
5240 ScreenLinesUC[off_to] = 0;
5241 screen_char(off_to, row, col + coloff);
5242 }
5243#endif
5244
5245 if (clear_width > 0
5246#ifdef FEAT_RIGHTLEFT
5247 && !rlflag
5248#endif
5249 )
5250 {
5251#ifdef FEAT_GUI
5252 int startCol = col;
5253#endif
5254
5255 /* blank out the rest of the line */
5256 while (col < clear_width && ScreenLines[off_to] == ' '
5257 && ScreenAttrs[off_to] == 0
5258#ifdef FEAT_MBYTE
5259 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5260#endif
5261 )
5262 {
5263 ++off_to;
5264 ++col;
5265 }
5266 if (col < clear_width)
5267 {
5268#ifdef FEAT_GUI
5269 /*
5270 * In the GUI, clearing the rest of the line may leave pixels
5271 * behind if the first character cleared was bold. Some bold
5272 * fonts spill over the left. In this case we redraw the previous
5273 * character too. If we didn't skip any blanks above, then we
5274 * only redraw if the character wasn't already redrawn anyway.
5275 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005276 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 hl = ScreenAttrs[off_to];
5279 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005280 {
5281 int prev_cells = 1;
5282# ifdef FEAT_MBYTE
5283 if (enc_utf8)
5284 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5285 * that its width is 2. */
5286 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5287 else if (enc_dbcs != 0)
5288 {
5289 /* find previous character by counting from first
5290 * column and get its width. */
5291 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005292 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005293
5294 while (off < off_to)
5295 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005296 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005297 off += prev_cells;
5298 }
5299 }
5300
5301 if (enc_dbcs != 0 && prev_cells > 1)
5302 screen_char_2(off_to - prev_cells, row,
5303 col + coloff - prev_cells);
5304 else
5305# endif
5306 screen_char(off_to - prev_cells, row,
5307 col + coloff - prev_cells);
5308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310#endif
5311 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5312 ' ', ' ', 0);
5313#ifdef FEAT_VERTSPLIT
5314 off_to += clear_width - col;
5315 col = clear_width;
5316#endif
5317 }
5318 }
5319
5320 if (clear_width > 0)
5321 {
5322#ifdef FEAT_VERTSPLIT
5323 /* For a window that's left of another, draw the separator char. */
5324 if (col + coloff < Columns)
5325 {
5326 int c;
5327
5328 c = fillchar_vsep(&hl);
5329 if (ScreenLines[off_to] != c
5330# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005331 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5332 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333# endif
5334 || ScreenAttrs[off_to] != hl)
5335 {
5336 ScreenLines[off_to] = c;
5337 ScreenAttrs[off_to] = hl;
5338# ifdef FEAT_MBYTE
5339 if (enc_utf8)
5340 {
5341 if (c >= 0x80)
5342 {
5343 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005344 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346 else
5347 ScreenLinesUC[off_to] = 0;
5348 }
5349# endif
5350 screen_char(off_to, row, col + coloff);
5351 }
5352 }
5353 else
5354#endif
5355 LineWraps[row] = FALSE;
5356 }
5357}
5358
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005359#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005361 * Mirror text "str" for right-left displaying.
5362 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005364 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365rl_mirror(str)
5366 char_u *str;
5367{
5368 char_u *p1, *p2;
5369 int t;
5370
5371 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5372 {
5373 t = *p1;
5374 *p1 = *p2;
5375 *p2 = t;
5376 }
5377}
5378#endif
5379
5380#if defined(FEAT_WINDOWS) || defined(PROTO)
5381/*
5382 * mark all status lines for redraw; used after first :cd
5383 */
5384 void
5385status_redraw_all()
5386{
5387 win_T *wp;
5388
5389 for (wp = firstwin; wp; wp = wp->w_next)
5390 if (wp->w_status_height)
5391 {
5392 wp->w_redr_status = TRUE;
5393 redraw_later(VALID);
5394 }
5395}
5396
5397/*
5398 * mark all status lines of the current buffer for redraw
5399 */
5400 void
5401status_redraw_curbuf()
5402{
5403 win_T *wp;
5404
5405 for (wp = firstwin; wp; wp = wp->w_next)
5406 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5407 {
5408 wp->w_redr_status = TRUE;
5409 redraw_later(VALID);
5410 }
5411}
5412
5413/*
5414 * Redraw all status lines that need to be redrawn.
5415 */
5416 void
5417redraw_statuslines()
5418{
5419 win_T *wp;
5420
5421 for (wp = firstwin; wp; wp = wp->w_next)
5422 if (wp->w_redr_status)
5423 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005424 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005425 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426}
5427#endif
5428
5429#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5430/*
5431 * Redraw all status lines at the bottom of frame "frp".
5432 */
5433 void
5434win_redraw_last_status(frp)
5435 frame_T *frp;
5436{
5437 if (frp->fr_layout == FR_LEAF)
5438 frp->fr_win->w_redr_status = TRUE;
5439 else if (frp->fr_layout == FR_ROW)
5440 {
5441 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5442 win_redraw_last_status(frp);
5443 }
5444 else /* frp->fr_layout == FR_COL */
5445 {
5446 frp = frp->fr_child;
5447 while (frp->fr_next != NULL)
5448 frp = frp->fr_next;
5449 win_redraw_last_status(frp);
5450 }
5451}
5452#endif
5453
5454#ifdef FEAT_VERTSPLIT
5455/*
5456 * Draw the verticap separator right of window "wp" starting with line "row".
5457 */
5458 static void
5459draw_vsep_win(wp, row)
5460 win_T *wp;
5461 int row;
5462{
5463 int hl;
5464 int c;
5465
5466 if (wp->w_vsep_width)
5467 {
5468 /* draw the vertical separator right of this window */
5469 c = fillchar_vsep(&hl);
5470 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5471 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5472 c, ' ', hl);
5473 }
5474}
5475#endif
5476
5477#ifdef FEAT_WILDMENU
5478static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005479static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
5481/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005482 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 */
5484 static int
5485status_match_len(xp, s)
5486 expand_T *xp;
5487 char_u *s;
5488{
5489 int len = 0;
5490
5491#ifdef FEAT_MENU
5492 int emenu = (xp->xp_context == EXPAND_MENUS
5493 || xp->xp_context == EXPAND_MENUNAMES);
5494
5495 /* Check for menu separators - replace with '|'. */
5496 if (emenu && menu_is_separator(s))
5497 return 1;
5498#endif
5499
5500 while (*s != NUL)
5501 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005502 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005503 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005504 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
5506
5507 return len;
5508}
5509
5510/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005511 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005512 * These are backslashes used for escaping. Do show backslashes in help tags.
5513 */
5514 static int
5515skip_status_match_char(xp, s)
5516 expand_T *xp;
5517 char_u *s;
5518{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005519 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005520#ifdef FEAT_MENU
5521 || ((xp->xp_context == EXPAND_MENUS
5522 || xp->xp_context == EXPAND_MENUNAMES)
5523 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5524#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005525 )
5526 {
5527#ifndef BACKSLASH_IN_FILENAME
5528 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5529 return 2;
5530#endif
5531 return 1;
5532 }
5533 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005534}
5535
5536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 * Show wildchar matches in the status line.
5538 * Show at least the "match" item.
5539 * We start at item 'first_match' in the list and show all matches that fit.
5540 *
5541 * If inversion is possible we use it. Else '=' characters are used.
5542 */
5543 void
5544win_redr_status_matches(xp, num_matches, matches, match, showtail)
5545 expand_T *xp;
5546 int num_matches;
5547 char_u **matches; /* list of matches */
5548 int match;
5549 int showtail;
5550{
5551#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5552 int row;
5553 char_u *buf;
5554 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005555 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 int fillchar;
5557 int attr;
5558 int i;
5559 int highlight = TRUE;
5560 char_u *selstart = NULL;
5561 int selstart_col = 0;
5562 char_u *selend = NULL;
5563 static int first_match = 0;
5564 int add_left = FALSE;
5565 char_u *s;
5566#ifdef FEAT_MENU
5567 int emenu;
5568#endif
5569#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5570 int l;
5571#endif
5572
5573 if (matches == NULL) /* interrupted completion? */
5574 return;
5575
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005576#ifdef FEAT_MBYTE
5577 if (has_mbyte)
5578 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5579 else
5580#endif
5581 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 if (buf == NULL)
5583 return;
5584
5585 if (match == -1) /* don't show match but original text */
5586 {
5587 match = 0;
5588 highlight = FALSE;
5589 }
5590 /* count 1 for the ending ">" */
5591 clen = status_match_len(xp, L_MATCH(match)) + 3;
5592 if (match == 0)
5593 first_match = 0;
5594 else if (match < first_match)
5595 {
5596 /* jumping left, as far as we can go */
5597 first_match = match;
5598 add_left = TRUE;
5599 }
5600 else
5601 {
5602 /* check if match fits on the screen */
5603 for (i = first_match; i < match; ++i)
5604 clen += status_match_len(xp, L_MATCH(i)) + 2;
5605 if (first_match > 0)
5606 clen += 2;
5607 /* jumping right, put match at the left */
5608 if ((long)clen > Columns)
5609 {
5610 first_match = match;
5611 /* if showing the last match, we can add some on the left */
5612 clen = 2;
5613 for (i = match; i < num_matches; ++i)
5614 {
5615 clen += status_match_len(xp, L_MATCH(i)) + 2;
5616 if ((long)clen >= Columns)
5617 break;
5618 }
5619 if (i == num_matches)
5620 add_left = TRUE;
5621 }
5622 }
5623 if (add_left)
5624 while (first_match > 0)
5625 {
5626 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5627 if ((long)clen >= Columns)
5628 break;
5629 --first_match;
5630 }
5631
5632 fillchar = fillchar_status(&attr, TRUE);
5633
5634 if (first_match == 0)
5635 {
5636 *buf = NUL;
5637 len = 0;
5638 }
5639 else
5640 {
5641 STRCPY(buf, "< ");
5642 len = 2;
5643 }
5644 clen = len;
5645
5646 i = first_match;
5647 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5648 {
5649 if (i == match)
5650 {
5651 selstart = buf + len;
5652 selstart_col = clen;
5653 }
5654
5655 s = L_MATCH(i);
5656 /* Check for menu separators - replace with '|' */
5657#ifdef FEAT_MENU
5658 emenu = (xp->xp_context == EXPAND_MENUS
5659 || xp->xp_context == EXPAND_MENUNAMES);
5660 if (emenu && menu_is_separator(s))
5661 {
5662 STRCPY(buf + len, transchar('|'));
5663 l = (int)STRLEN(buf + len);
5664 len += l;
5665 clen += l;
5666 }
5667 else
5668#endif
5669 for ( ; *s != NUL; ++s)
5670 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005671 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 clen += ptr2cells(s);
5673#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005674 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
5676 STRNCPY(buf + len, s, l);
5677 s += l - 1;
5678 len += l;
5679 }
5680 else
5681#endif
5682 {
5683 STRCPY(buf + len, transchar_byte(*s));
5684 len += (int)STRLEN(buf + len);
5685 }
5686 }
5687 if (i == match)
5688 selend = buf + len;
5689
5690 *(buf + len++) = ' ';
5691 *(buf + len++) = ' ';
5692 clen += 2;
5693 if (++i == num_matches)
5694 break;
5695 }
5696
5697 if (i != num_matches)
5698 {
5699 *(buf + len++) = '>';
5700 ++clen;
5701 }
5702
5703 buf[len] = NUL;
5704
5705 row = cmdline_row - 1;
5706 if (row >= 0)
5707 {
5708 if (wild_menu_showing == 0)
5709 {
5710 if (msg_scrolled > 0)
5711 {
5712 /* Put the wildmenu just above the command line. If there is
5713 * no room, scroll the screen one line up. */
5714 if (cmdline_row == Rows - 1)
5715 {
5716 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5717 ++msg_scrolled;
5718 }
5719 else
5720 {
5721 ++cmdline_row;
5722 ++row;
5723 }
5724 wild_menu_showing = WM_SCROLLED;
5725 }
5726 else
5727 {
5728 /* Create status line if needed by setting 'laststatus' to 2.
5729 * Set 'winminheight' to zero to avoid that the window is
5730 * resized. */
5731 if (lastwin->w_status_height == 0)
5732 {
5733 save_p_ls = p_ls;
5734 save_p_wmh = p_wmh;
5735 p_ls = 2;
5736 p_wmh = 0;
5737 last_status(FALSE);
5738 }
5739 wild_menu_showing = WM_SHOWN;
5740 }
5741 }
5742
5743 screen_puts(buf, row, 0, attr);
5744 if (selstart != NULL && highlight)
5745 {
5746 *selend = NUL;
5747 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5748 }
5749
5750 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5751 }
5752
5753#ifdef FEAT_VERTSPLIT
5754 win_redraw_last_status(topframe);
5755#else
5756 lastwin->w_redr_status = TRUE;
5757#endif
5758 vim_free(buf);
5759}
5760#endif
5761
5762#if defined(FEAT_WINDOWS) || defined(PROTO)
5763/*
5764 * Redraw the status line of window wp.
5765 *
5766 * If inversion is possible we use it. Else '=' characters are used.
5767 */
5768 void
5769win_redr_status(wp)
5770 win_T *wp;
5771{
5772 int row;
5773 char_u *p;
5774 int len;
5775 int fillchar;
5776 int attr;
5777 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00005778 static int busy = FALSE;
5779
5780 /* It's possible to get here recursively when 'statusline' (indirectly)
5781 * invokes ":redrawstatus". Simply ignore the call then. */
5782 if (busy)
5783 return;
5784 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
5786 wp->w_redr_status = FALSE;
5787 if (wp->w_status_height == 0)
5788 {
5789 /* no status line, can only be last window */
5790 redraw_cmdline = TRUE;
5791 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005792 else if (!redrawing()
5793#ifdef FEAT_INS_EXPAND
5794 /* don't update status line when popup menu is visible and may be
5795 * drawn over it */
5796 || pum_visible()
5797#endif
5798 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
5800 /* Don't redraw right now, do it later. */
5801 wp->w_redr_status = TRUE;
5802 }
5803#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005804 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 {
5806 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00005807 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
5809#endif
5810 else
5811 {
5812 fillchar = fillchar_status(&attr, wp == curwin);
5813
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005814 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 p = NameBuff;
5816 len = (int)STRLEN(p);
5817
5818 if (wp->w_buffer->b_help
5819#ifdef FEAT_QUICKFIX
5820 || wp->w_p_pvw
5821#endif
5822 || bufIsChanged(wp->w_buffer)
5823 || wp->w_buffer->b_p_ro)
5824 *(p + len++) = ' ';
5825 if (wp->w_buffer->b_help)
5826 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005827 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 len += (int)STRLEN(p + len);
5829 }
5830#ifdef FEAT_QUICKFIX
5831 if (wp->w_p_pvw)
5832 {
5833 STRCPY(p + len, _("[Preview]"));
5834 len += (int)STRLEN(p + len);
5835 }
5836#endif
5837 if (bufIsChanged(wp->w_buffer))
5838 {
5839 STRCPY(p + len, "[+]");
5840 len += 3;
5841 }
5842 if (wp->w_buffer->b_p_ro)
5843 {
5844 STRCPY(p + len, "[RO]");
5845 len += 4;
5846 }
5847
5848#ifndef FEAT_VERTSPLIT
5849 this_ru_col = ru_col;
5850 if (this_ru_col < (Columns + 1) / 2)
5851 this_ru_col = (Columns + 1) / 2;
5852#else
5853 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5854 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5855 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5856 if (this_ru_col <= 1)
5857 {
5858 p = (char_u *)"<"; /* No room for file name! */
5859 len = 1;
5860 }
5861 else
5862#endif
5863#ifdef FEAT_MBYTE
5864 if (has_mbyte)
5865 {
5866 int clen = 0, i;
5867
5868 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005869 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 clen += (*mb_ptr2cells)(p + i);
5871 /* Find first character that will fit.
5872 * Going from start to end is much faster for DBCS. */
5873 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005874 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 clen -= (*mb_ptr2cells)(p + i);
5876 len = clen;
5877 if (i > 0)
5878 {
5879 p = p + i - 1;
5880 *p = '<';
5881 ++len;
5882 }
5883
5884 }
5885 else
5886#endif
5887 if (len > this_ru_col - 1)
5888 {
5889 p += len - (this_ru_col - 1);
5890 *p = '<';
5891 len = this_ru_col - 1;
5892 }
5893
5894 row = W_WINROW(wp) + wp->w_height;
5895 screen_puts(p, row, W_WINCOL(wp), attr);
5896 screen_fill(row, row + 1, len + W_WINCOL(wp),
5897 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5898
5899 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5900 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5901 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5902 - 1 + W_WINCOL(wp)), attr);
5903
5904#ifdef FEAT_CMDL_INFO
5905 win_redr_ruler(wp, TRUE);
5906#endif
5907 }
5908
5909#ifdef FEAT_VERTSPLIT
5910 /*
5911 * May need to draw the character below the vertical separator.
5912 */
5913 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5914 {
5915 if (stl_connected(wp))
5916 fillchar = fillchar_status(&attr, wp == curwin);
5917 else
5918 fillchar = fillchar_vsep(&attr);
5919 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5920 attr);
5921 }
5922#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00005923 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924}
5925
Bram Moolenaar238a5642006-02-21 22:12:05 +00005926#ifdef FEAT_STL_OPT
5927/*
5928 * Redraw the status line according to 'statusline' and take care of any
5929 * errors encountered.
5930 */
5931 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00005932redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00005933 win_T *wp;
5934{
Bram Moolenaar362f3562009-11-03 16:20:34 +00005935 static int entered = FALSE;
5936 int save_called_emsg = called_emsg;
5937
5938 /* When called recursively return. This can happen when the statusline
5939 * contains an expression that triggers a redraw. */
5940 if (entered)
5941 return;
5942 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00005943
5944 called_emsg = FALSE;
5945 win_redr_custom(wp, FALSE);
5946 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00005947 {
5948 /* When there is an error disable the statusline, otherwise the
5949 * display is messed up with errors and a redraw triggers the problem
5950 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005951 set_string_option_direct((char_u *)"statusline", -1,
5952 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005953 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00005954 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00005955 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00005956 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00005957}
5958#endif
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960# ifdef FEAT_VERTSPLIT
5961/*
5962 * Return TRUE if the status line of window "wp" is connected to the status
5963 * line of the window right of it. If not, then it's a vertical separator.
5964 * Only call if (wp->w_vsep_width != 0).
5965 */
5966 int
5967stl_connected(wp)
5968 win_T *wp;
5969{
5970 frame_T *fr;
5971
5972 fr = wp->w_frame;
5973 while (fr->fr_parent != NULL)
5974 {
5975 if (fr->fr_parent->fr_layout == FR_COL)
5976 {
5977 if (fr->fr_next != NULL)
5978 break;
5979 }
5980 else
5981 {
5982 if (fr->fr_next != NULL)
5983 return TRUE;
5984 }
5985 fr = fr->fr_parent;
5986 }
5987 return FALSE;
5988}
5989# endif
5990
5991#endif /* FEAT_WINDOWS */
5992
5993#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5994/*
5995 * Get the value to show for the language mappings, active 'keymap'.
5996 */
5997 int
5998get_keymap_str(wp, buf, len)
5999 win_T *wp;
6000 char_u *buf; /* buffer for the result */
6001 int len; /* length of buffer */
6002{
6003 char_u *p;
6004
6005 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6006 return FALSE;
6007
6008 {
6009#ifdef FEAT_EVAL
6010 buf_T *old_curbuf = curbuf;
6011 win_T *old_curwin = curwin;
6012 char_u *s;
6013
6014 curbuf = wp->w_buffer;
6015 curwin = wp;
6016 STRCPY(buf, "b:keymap_name"); /* must be writable */
6017 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006018 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 --emsg_skip;
6020 curbuf = old_curbuf;
6021 curwin = old_curwin;
6022 if (p == NULL || *p == NUL)
6023#endif
6024 {
6025#ifdef FEAT_KEYMAP
6026 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6027 p = wp->w_buffer->b_p_keymap;
6028 else
6029#endif
6030 p = (char_u *)"lang";
6031 }
6032 if ((int)(STRLEN(p) + 3) < len)
6033 sprintf((char *)buf, "<%s>", p);
6034 else
6035 buf[0] = NUL;
6036#ifdef FEAT_EVAL
6037 vim_free(s);
6038#endif
6039 }
6040 return buf[0] != NUL;
6041}
6042#endif
6043
6044#if defined(FEAT_STL_OPT) || defined(PROTO)
6045/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006046 * Redraw the status line or ruler of window "wp".
6047 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 */
6049 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006050win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006052 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 int attr;
6055 int curattr;
6056 int row;
6057 int col = 0;
6058 int maxwidth;
6059 int width;
6060 int n;
6061 int len;
6062 int fillchar;
6063 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006064 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006066 struct stl_hlrec hltab[STL_MAX_ITEM];
6067 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006068 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
6070 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006071 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006073 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006074 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006075 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006076 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006077 attr = hl_attr(HLF_TPF);
6078 maxwidth = Columns;
6079# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006080 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006083 else
6084 {
6085 row = W_WINROW(wp) + wp->w_height;
6086 fillchar = fillchar_status(&attr, wp == curwin);
6087 maxwidth = W_WIDTH(wp);
6088
6089 if (draw_ruler)
6090 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006091 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006092 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006093 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006094 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006095 if (*++stl == '-')
6096 stl++;
6097 if (atoi((char *)stl))
6098 while (VIM_ISDIGIT(*stl))
6099 stl++;
6100 if (*stl++ != '(')
6101 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006102 }
6103#ifdef FEAT_VERTSPLIT
6104 col = ru_col - (Columns - W_WIDTH(wp));
6105 if (col < (W_WIDTH(wp) + 1) / 2)
6106 col = (W_WIDTH(wp) + 1) / 2;
6107#else
6108 col = ru_col;
6109 if (col > (Columns + 1) / 2)
6110 col = (Columns + 1) / 2;
6111#endif
6112 maxwidth = W_WIDTH(wp) - col;
6113#ifdef FEAT_WINDOWS
6114 if (!wp->w_status_height)
6115#endif
6116 {
6117 row = Rows - 1;
6118 --maxwidth; /* writing in last column may cause scrolling */
6119 fillchar = ' ';
6120 attr = 0;
6121 }
6122
6123# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006124 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006125# endif
6126 }
6127 else
6128 {
6129 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006130 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006131 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006132 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006133# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006134 use_sandbox = was_set_insecurely((char_u *)"statusline",
6135 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006136# endif
6137 }
6138
6139#ifdef FEAT_VERTSPLIT
6140 col += W_WINCOL(wp);
6141#endif
6142 }
6143
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (maxwidth <= 0)
6145 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
Bram Moolenaar362f3562009-11-03 16:20:34 +00006147 /* Make a copy, because the statusline may include a function call that
6148 * might change the option value and free the memory. */
6149 stl = vim_strsave(stl);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006150 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6151 buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006152 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006153 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006154 vim_free(stl);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006155 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006157 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {
6159#ifdef FEAT_MBYTE
6160 len += (*mb_char2bytes)(fillchar, buf + len);
6161#else
6162 buf[len++] = fillchar;
6163#endif
6164 ++width;
6165 }
6166 buf[len] = NUL;
6167
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006168 /*
6169 * Draw each snippet with the specified highlighting.
6170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 curattr = attr;
6172 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006173 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006175 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 screen_puts_len(p, len, row, col, curattr);
6177 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006178 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006180 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006182 else if (hltab[n].userhl < 0)
6183 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006185 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006186 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187#endif
6188 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006189 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 }
6191 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006192
6193 if (wp == NULL)
6194 {
6195 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6196 col = 0;
6197 len = 0;
6198 p = buf;
6199 fillchar = 0;
6200 for (n = 0; tabtab[n].start != NULL; n++)
6201 {
6202 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6203 while (col < len)
6204 TabPageIdxs[col++] = fillchar;
6205 p = tabtab[n].start;
6206 fillchar = tabtab[n].userhl;
6207 }
6208 while (col < Columns)
6209 TabPageIdxs[col++] = fillchar;
6210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211}
6212
6213#endif /* FEAT_STL_OPT */
6214
6215/*
6216 * Output a single character directly to the screen and update ScreenLines.
6217 */
6218 void
6219screen_putchar(c, row, col, attr)
6220 int c;
6221 int row, col;
6222 int attr;
6223{
6224#ifdef FEAT_MBYTE
6225 char_u buf[MB_MAXBYTES + 1];
6226
6227 buf[(*mb_char2bytes)(c, buf)] = NUL;
6228#else
6229 char_u buf[2];
6230
6231 buf[0] = c;
6232 buf[1] = NUL;
6233#endif
6234 screen_puts(buf, row, col, attr);
6235}
6236
6237/*
6238 * Get a single character directly from ScreenLines into "bytes[]".
6239 * Also return its attribute in *attrp;
6240 */
6241 void
6242screen_getbytes(row, col, bytes, attrp)
6243 int row, col;
6244 char_u *bytes;
6245 int *attrp;
6246{
6247 unsigned off;
6248
6249 /* safety check */
6250 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6251 {
6252 off = LineOffset[row] + col;
6253 *attrp = ScreenAttrs[off];
6254 bytes[0] = ScreenLines[off];
6255 bytes[1] = NUL;
6256
6257#ifdef FEAT_MBYTE
6258 if (enc_utf8 && ScreenLinesUC[off] != 0)
6259 bytes[utfc_char2bytes(off, bytes)] = NUL;
6260 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6261 {
6262 bytes[0] = ScreenLines[off];
6263 bytes[1] = ScreenLines2[off];
6264 bytes[2] = NUL;
6265 }
6266 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6267 {
6268 bytes[1] = ScreenLines[off + 1];
6269 bytes[2] = NUL;
6270 }
6271#endif
6272 }
6273}
6274
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006275#ifdef FEAT_MBYTE
6276static int screen_comp_differs __ARGS((int, int*));
6277
6278/*
6279 * Return TRUE if composing characters for screen posn "off" differs from
6280 * composing characters in "u8cc".
6281 */
6282 static int
6283screen_comp_differs(off, u8cc)
6284 int off;
6285 int *u8cc;
6286{
6287 int i;
6288
6289 for (i = 0; i < Screen_mco; ++i)
6290 {
6291 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6292 return TRUE;
6293 if (u8cc[i] == 0)
6294 break;
6295 }
6296 return FALSE;
6297}
6298#endif
6299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300/*
6301 * Put string '*text' on the screen at position 'row' and 'col', with
6302 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6303 * Note: only outputs within one row, message is truncated at screen boundary!
6304 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6305 */
6306 void
6307screen_puts(text, row, col, attr)
6308 char_u *text;
6309 int row;
6310 int col;
6311 int attr;
6312{
6313 screen_puts_len(text, -1, row, col, attr);
6314}
6315
6316/*
6317 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6318 * a NUL.
6319 */
6320 void
6321screen_puts_len(text, len, row, col, attr)
6322 char_u *text;
6323 int len;
6324 int row;
6325 int col;
6326 int attr;
6327{
6328 unsigned off;
6329 char_u *ptr = text;
6330 int c;
6331#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006332 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 int mbyte_blen = 1;
6334 int mbyte_cells = 1;
6335 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006336 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 int clear_next_cell = FALSE;
6338# ifdef FEAT_ARABIC
6339 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006340 int pc, nc, nc1;
6341 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342# endif
6343#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006344#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6345 int force_redraw_this;
6346 int force_redraw_next = FALSE;
6347#endif
6348 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
6350 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6351 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006352 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
Bram Moolenaarc236c162008-07-13 17:41:49 +00006354#ifdef FEAT_MBYTE
6355 /* When drawing over the right halve of a double-wide char clear out the
6356 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006357 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006358# ifdef FEAT_GUI
6359 && !gui.in_use
6360# endif
6361 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006362 {
6363 ScreenLines[off - 1] = ' ';
6364 ScreenAttrs[off - 1] = 0;
6365 if (enc_utf8)
6366 {
6367 ScreenLinesUC[off - 1] = 0;
6368 ScreenLinesC[0][off - 1] = 0;
6369 }
6370 /* redraw the previous cell, make it empty */
6371 screen_char(off - 1, row, col - 1);
6372 /* force the cell at "col" to be redrawn */
6373 force_redraw_next = TRUE;
6374 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006375#endif
6376
Bram Moolenaar367329b2007-08-30 11:53:22 +00006377#ifdef FEAT_MBYTE
6378 max_off = LineOffset[row] + screen_Columns;
6379#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006380 while (col < screen_Columns
6381 && (len < 0 || (int)(ptr - text) < len)
6382 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
6384 c = *ptr;
6385#ifdef FEAT_MBYTE
6386 /* check if this is the first byte of a multibyte */
6387 if (has_mbyte)
6388 {
6389 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006390 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006392 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6394 mbyte_cells = 1;
6395 else if (enc_dbcs != 0)
6396 mbyte_cells = mbyte_blen;
6397 else /* enc_utf8 */
6398 {
6399 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006400 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 (int)((text + len) - ptr));
6402 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006403 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006405# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 /* Non-BMP character: display as ? or fullwidth ?. */
6407 if (u8c >= 0x10000)
6408 {
6409 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6410 if (attr == 0)
6411 attr = hl_attr(HLF_8);
6412 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414# ifdef FEAT_ARABIC
6415 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6416 {
6417 /* Do Arabic shaping. */
6418 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6419 {
6420 /* Past end of string to be displayed. */
6421 nc = NUL;
6422 nc1 = NUL;
6423 }
6424 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006425 {
Bram Moolenaar54620182009-11-11 16:07:20 +00006426 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
6427 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006428 nc1 = pcc[0];
6429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 pc = prev_c;
6431 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006432 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 }
6434 else
6435 prev_c = u8c;
6436# endif
6437 }
6438 }
6439#endif
6440
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006441#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6442 force_redraw_this = force_redraw_next;
6443 force_redraw_next = FALSE;
6444#endif
6445
6446 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447#ifdef FEAT_MBYTE
6448 || (mbyte_cells == 2
6449 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6450 || (enc_dbcs == DBCS_JPNU
6451 && c == 0x8e
6452 && ScreenLines2[off] != ptr[1])
6453 || (enc_utf8
Bram Moolenaarffcce302009-02-22 00:14:58 +00006454 && (ScreenLinesUC[off] != (u8char_T)(c >= 0x80 ? u8c : 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006455 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456#endif
6457 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006458 || exmode_active;
6459
6460 if (need_redraw
6461#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6462 || force_redraw_this
6463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 )
6465 {
6466#if defined(FEAT_GUI) || defined(UNIX)
6467 /* The bold trick makes a single row of pixels appear in the next
6468 * character. When a bold character is removed, the next
6469 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006470 * and for some xterms. */
6471 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472# ifdef FEAT_GUI
6473 gui.in_use
6474# endif
6475# if defined(FEAT_GUI) && defined(UNIX)
6476 ||
6477# endif
6478# ifdef UNIX
6479 term_is_xterm
6480# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006481 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006483 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006485 if (n > HL_ALL)
6486 n = syn_attr2attr(n);
6487 if (n & HL_BOLD)
6488 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 }
6490#endif
6491#ifdef FEAT_MBYTE
6492 /* When at the end of the text and overwriting a two-cell
6493 * character with a one-cell character, need to clear the next
6494 * cell. Also when overwriting the left halve of a two-cell char
6495 * with the right halve of a two-cell char. Do this only once
6496 * (mb_off2cells() may return 2 on the right halve). */
6497 if (clear_next_cell)
6498 clear_next_cell = FALSE;
6499 else if (has_mbyte
6500 && (len < 0 ? ptr[mbyte_blen] == NUL
6501 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006502 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006504 && (*mb_off2cells)(off, max_off) == 1
6505 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 clear_next_cell = TRUE;
6507
6508 /* Make sure we never leave a second byte of a double-byte behind,
6509 * it confuses mb_off2cells(). */
6510 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006511 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006513 && (*mb_off2cells)(off, max_off) == 1
6514 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 ScreenLines[off + mbyte_blen] = 0;
6516#endif
6517 ScreenLines[off] = c;
6518 ScreenAttrs[off] = attr;
6519#ifdef FEAT_MBYTE
6520 if (enc_utf8)
6521 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006522 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 ScreenLinesUC[off] = 0;
6524 else
6525 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006526 int i;
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006529 for (i = 0; i < Screen_mco; ++i)
6530 {
6531 ScreenLinesC[i][off] = u8cc[i];
6532 if (u8cc[i] == 0)
6533 break;
6534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 }
6536 if (mbyte_cells == 2)
6537 {
6538 ScreenLines[off + 1] = 0;
6539 ScreenAttrs[off + 1] = attr;
6540 }
6541 screen_char(off, row, col);
6542 }
6543 else if (mbyte_cells == 2)
6544 {
6545 ScreenLines[off + 1] = ptr[1];
6546 ScreenAttrs[off + 1] = attr;
6547 screen_char_2(off, row, col);
6548 }
6549 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6550 {
6551 ScreenLines2[off] = ptr[1];
6552 screen_char(off, row, col);
6553 }
6554 else
6555#endif
6556 screen_char(off, row, col);
6557 }
6558#ifdef FEAT_MBYTE
6559 if (has_mbyte)
6560 {
6561 off += mbyte_cells;
6562 col += mbyte_cells;
6563 ptr += mbyte_blen;
6564 if (clear_next_cell)
6565 ptr = (char_u *)" ";
6566 }
6567 else
6568#endif
6569 {
6570 ++off;
6571 ++col;
6572 ++ptr;
6573 }
6574 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006575
6576#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6577 /* If we detected the next character needs to be redrawn, but the text
6578 * doesn't extend up to there, update the character here. */
6579 if (force_redraw_next && col < screen_Columns)
6580 {
6581# ifdef FEAT_MBYTE
6582 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
6583 screen_char_2(off, row, col);
6584 else
6585# endif
6586 screen_char(off, row, col);
6587 }
6588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589}
6590
6591#ifdef FEAT_SEARCH_EXTRA
6592/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006593 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 */
6595 static void
6596start_search_hl()
6597{
6598 if (p_hls && !no_hlsearch)
6599 {
6600 last_pat_prog(&search_hl.rm);
6601 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006602# ifdef FEAT_RELTIME
6603 /* Set the time limit to 'redrawtime'. */
6604 profile_setlimit(p_rdt, &search_hl.tm);
6605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
6607}
6608
6609/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006610 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 */
6612 static void
6613end_search_hl()
6614{
6615 if (search_hl.rm.regprog != NULL)
6616 {
6617 vim_free(search_hl.rm.regprog);
6618 search_hl.rm.regprog = NULL;
6619 }
6620}
6621
6622/*
6623 * Advance to the match in window "wp" line "lnum" or past it.
6624 */
6625 static void
6626prepare_search_hl(wp, lnum)
6627 win_T *wp;
6628 linenr_T lnum;
6629{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006630 matchitem_T *cur; /* points to the match list */
6631 match_T *shl; /* points to search_hl or a match */
6632 int shl_flag; /* flag to indicate whether search_hl
6633 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 int n;
6635
6636 /*
6637 * When using a multi-line pattern, start searching at the top
6638 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006639 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006641 cur = wp->w_match_head;
6642 shl_flag = FALSE;
6643 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006645 if (shl_flag == FALSE)
6646 {
6647 shl = &search_hl;
6648 shl_flag = TRUE;
6649 }
6650 else
6651 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (shl->rm.regprog != NULL
6653 && shl->lnum == 0
6654 && re_multiline(shl->rm.regprog))
6655 {
6656 if (shl->first_lnum == 0)
6657 {
6658# ifdef FEAT_FOLDING
6659 for (shl->first_lnum = lnum;
6660 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6661 if (hasFoldingWin(wp, shl->first_lnum - 1,
6662 NULL, NULL, TRUE, NULL))
6663 break;
6664# else
6665 shl->first_lnum = wp->w_topline;
6666# endif
6667 }
6668 n = 0;
6669 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6670 {
6671 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6672 if (shl->lnum != 0)
6673 {
6674 shl->first_lnum = shl->lnum
6675 + shl->rm.endpos[0].lnum
6676 - shl->rm.startpos[0].lnum;
6677 n = shl->rm.endpos[0].col;
6678 }
6679 else
6680 {
6681 ++shl->first_lnum;
6682 n = 0;
6683 }
6684 }
6685 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006686 if (shl != &search_hl && cur != NULL)
6687 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 }
6689}
6690
6691/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006692 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 * Uses shl->buf.
6694 * Sets shl->lnum and shl->rm contents.
6695 * Note: Assumes a previous match is always before "lnum", unless
6696 * shl->lnum is zero.
6697 * Careful: Any pointers for buffer lines will become invalid.
6698 */
6699 static void
6700next_search_hl(win, shl, lnum, mincol)
6701 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006702 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 linenr_T lnum;
6704 colnr_T mincol; /* minimal column for a match */
6705{
6706 linenr_T l;
6707 colnr_T matchcol;
6708 long nmatched;
6709
6710 if (shl->lnum != 0)
6711 {
6712 /* Check for three situations:
6713 * 1. If the "lnum" is below a previous match, start a new search.
6714 * 2. If the previous match includes "mincol", use it.
6715 * 3. Continue after the previous match.
6716 */
6717 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6718 if (lnum > l)
6719 shl->lnum = 0;
6720 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6721 return;
6722 }
6723
6724 /*
6725 * Repeat searching for a match until one is found that includes "mincol"
6726 * or none is found in this line.
6727 */
6728 called_emsg = FALSE;
6729 for (;;)
6730 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006731#ifdef FEAT_RELTIME
6732 /* Stop searching after passing the time limit. */
6733 if (profile_passed_limit(&(shl->tm)))
6734 {
6735 shl->lnum = 0; /* no match found in time */
6736 break;
6737 }
6738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 /* Three situations:
6740 * 1. No useful previous match: search from start of line.
6741 * 2. Not Vi compatible or empty match: continue at next character.
6742 * Break the loop if this is beyond the end of the line.
6743 * 3. Vi compatible searching: continue at end of previous match.
6744 */
6745 if (shl->lnum == 0)
6746 matchcol = 0;
6747 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6748 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006749 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006751 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006752
6753 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006754 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006755 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006757 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 shl->lnum = 0;
6759 break;
6760 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006761#ifdef FEAT_MBYTE
6762 if (has_mbyte)
6763 matchcol += mb_ptr2len(ml);
6764 else
6765#endif
6766 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 }
6768 else
6769 matchcol = shl->rm.endpos[0].col;
6770
6771 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006772 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6773#ifdef FEAT_RELTIME
6774 &(shl->tm)
6775#else
6776 NULL
6777#endif
6778 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (called_emsg)
6780 {
6781 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006782 if (shl == &search_hl)
6783 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006784 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006785 vim_free(shl->rm.regprog);
6786 no_hlsearch = TRUE;
6787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006789 shl->lnum = 0;
6790 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 break;
6792 }
6793 if (nmatched == 0)
6794 {
6795 shl->lnum = 0; /* no match found */
6796 break;
6797 }
6798 if (shl->rm.startpos[0].lnum > 0
6799 || shl->rm.startpos[0].col >= mincol
6800 || nmatched > 1
6801 || shl->rm.endpos[0].col > mincol)
6802 {
6803 shl->lnum += shl->rm.startpos[0].lnum;
6804 break; /* useful match found */
6805 }
6806 }
6807}
6808#endif
6809
6810 static void
6811screen_start_highlight(attr)
6812 int attr;
6813{
6814 attrentry_T *aep = NULL;
6815
6816 screen_attr = attr;
6817 if (full_screen
6818#ifdef WIN3264
6819 && termcap_active
6820#endif
6821 )
6822 {
6823#ifdef FEAT_GUI
6824 if (gui.in_use)
6825 {
6826 char buf[20];
6827
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006828 /* The GUI handles this internally. */
6829 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 OUT_STR(buf);
6831 }
6832 else
6833#endif
6834 {
6835 if (attr > HL_ALL) /* special HL attr. */
6836 {
6837 if (t_colors > 1)
6838 aep = syn_cterm_attr2entry(attr);
6839 else
6840 aep = syn_term_attr2entry(attr);
6841 if (aep == NULL) /* did ":syntax clear" */
6842 attr = 0;
6843 else
6844 attr = aep->ae_attr;
6845 }
6846 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6847 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006848 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6849 && cterm_normal_fg_bold)
6850 /* If the Normal FG color has BOLD attribute and the new HL
6851 * has a FG color defined, clear BOLD. */
6852 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6854 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006855 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6856 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 out_str(T_US);
6858 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6859 out_str(T_CZH);
6860 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6861 out_str(T_MR);
6862
6863 /*
6864 * Output the color or start string after bold etc., in case the
6865 * bold etc. override the color setting.
6866 */
6867 if (aep != NULL)
6868 {
6869 if (t_colors > 1)
6870 {
6871 if (aep->ae_u.cterm.fg_color)
6872 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6873 if (aep->ae_u.cterm.bg_color)
6874 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6875 }
6876 else
6877 {
6878 if (aep->ae_u.term.start != NULL)
6879 out_str(aep->ae_u.term.start);
6880 }
6881 }
6882 }
6883 }
6884}
6885
6886 void
6887screen_stop_highlight()
6888{
6889 int do_ME = FALSE; /* output T_ME code */
6890
6891 if (screen_attr != 0
6892#ifdef WIN3264
6893 && termcap_active
6894#endif
6895 )
6896 {
6897#ifdef FEAT_GUI
6898 if (gui.in_use)
6899 {
6900 char buf[20];
6901
6902 /* use internal GUI code */
6903 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6904 OUT_STR(buf);
6905 }
6906 else
6907#endif
6908 {
6909 if (screen_attr > HL_ALL) /* special HL attr. */
6910 {
6911 attrentry_T *aep;
6912
6913 if (t_colors > 1)
6914 {
6915 /*
6916 * Assume that t_me restores the original colors!
6917 */
6918 aep = syn_cterm_attr2entry(screen_attr);
6919 if (aep != NULL && (aep->ae_u.cterm.fg_color
6920 || aep->ae_u.cterm.bg_color))
6921 do_ME = TRUE;
6922 }
6923 else
6924 {
6925 aep = syn_term_attr2entry(screen_attr);
6926 if (aep != NULL && aep->ae_u.term.stop != NULL)
6927 {
6928 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6929 do_ME = TRUE;
6930 else
6931 out_str(aep->ae_u.term.stop);
6932 }
6933 }
6934 if (aep == NULL) /* did ":syntax clear" */
6935 screen_attr = 0;
6936 else
6937 screen_attr = aep->ae_attr;
6938 }
6939
6940 /*
6941 * Often all ending-codes are equal to T_ME. Avoid outputting the
6942 * same sequence several times.
6943 */
6944 if (screen_attr & HL_STANDOUT)
6945 {
6946 if (STRCMP(T_SE, T_ME) == 0)
6947 do_ME = TRUE;
6948 else
6949 out_str(T_SE);
6950 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006951 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {
6953 if (STRCMP(T_UE, T_ME) == 0)
6954 do_ME = TRUE;
6955 else
6956 out_str(T_UE);
6957 }
6958 if (screen_attr & HL_ITALIC)
6959 {
6960 if (STRCMP(T_CZR, T_ME) == 0)
6961 do_ME = TRUE;
6962 else
6963 out_str(T_CZR);
6964 }
6965 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6966 out_str(T_ME);
6967
6968 if (t_colors > 1)
6969 {
6970 /* set Normal cterm colors */
6971 if (cterm_normal_fg_color != 0)
6972 term_fg_color(cterm_normal_fg_color - 1);
6973 if (cterm_normal_bg_color != 0)
6974 term_bg_color(cterm_normal_bg_color - 1);
6975 if (cterm_normal_fg_bold)
6976 out_str(T_MD);
6977 }
6978 }
6979 }
6980 screen_attr = 0;
6981}
6982
6983/*
6984 * Reset the colors for a cterm. Used when leaving Vim.
6985 * The machine specific code may override this again.
6986 */
6987 void
6988reset_cterm_colors()
6989{
6990 if (t_colors > 1)
6991 {
6992 /* set Normal cterm colors */
6993 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6994 {
6995 out_str(T_OP);
6996 screen_attr = -1;
6997 }
6998 if (cterm_normal_fg_bold)
6999 {
7000 out_str(T_ME);
7001 screen_attr = -1;
7002 }
7003 }
7004}
7005
7006/*
7007 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7008 * using the attributes from ScreenAttrs["off"].
7009 */
7010 static void
7011screen_char(off, row, col)
7012 unsigned off;
7013 int row;
7014 int col;
7015{
7016 int attr;
7017
7018 /* Check for illegal values, just in case (could happen just after
7019 * resizing). */
7020 if (row >= screen_Rows || col >= screen_Columns)
7021 return;
7022
7023 /* Outputting the last character on the screen may scrollup the screen.
7024 * Don't to it! Mark the character invalid (update it when scrolled up) */
7025 if (row == screen_Rows - 1 && col == screen_Columns - 1
7026#ifdef FEAT_RIGHTLEFT
7027 /* account for first command-line character in rightleft mode */
7028 && !cmdmsg_rl
7029#endif
7030 )
7031 {
7032 ScreenAttrs[off] = (sattr_T)-1;
7033 return;
7034 }
7035
7036 /*
7037 * Stop highlighting first, so it's easier to move the cursor.
7038 */
7039#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7040 if (screen_char_attr != 0)
7041 attr = screen_char_attr;
7042 else
7043#endif
7044 attr = ScreenAttrs[off];
7045 if (screen_attr != attr)
7046 screen_stop_highlight();
7047
7048 windgoto(row, col);
7049
7050 if (screen_attr != attr)
7051 screen_start_highlight(attr);
7052
7053#ifdef FEAT_MBYTE
7054 if (enc_utf8 && ScreenLinesUC[off] != 0)
7055 {
7056 char_u buf[MB_MAXBYTES + 1];
7057
7058 /* Convert UTF-8 character to bytes and write it. */
7059
7060 buf[utfc_char2bytes(off, buf)] = NUL;
7061
7062 out_str(buf);
7063 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7064 ++screen_cur_col;
7065 }
7066 else
7067#endif
7068 {
7069#ifdef FEAT_MBYTE
7070 out_flush_check();
7071#endif
7072 out_char(ScreenLines[off]);
7073#ifdef FEAT_MBYTE
7074 /* double-byte character in single-width cell */
7075 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7076 out_char(ScreenLines2[off]);
7077#endif
7078 }
7079
7080 screen_cur_col++;
7081}
7082
7083#ifdef FEAT_MBYTE
7084
7085/*
7086 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7087 * on the screen at position 'row' and 'col'.
7088 * The attributes of the first byte is used for all. This is required to
7089 * output the two bytes of a double-byte character with nothing in between.
7090 */
7091 static void
7092screen_char_2(off, row, col)
7093 unsigned off;
7094 int row;
7095 int col;
7096{
7097 /* Check for illegal values (could be wrong when screen was resized). */
7098 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7099 return;
7100
7101 /* Outputting the last character on the screen may scrollup the screen.
7102 * Don't to it! Mark the character invalid (update it when scrolled up) */
7103 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7104 {
7105 ScreenAttrs[off] = (sattr_T)-1;
7106 return;
7107 }
7108
7109 /* Output the first byte normally (positions the cursor), then write the
7110 * second byte directly. */
7111 screen_char(off, row, col);
7112 out_char(ScreenLines[off + 1]);
7113 ++screen_cur_col;
7114}
7115#endif
7116
7117#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7118/*
7119 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7120 * This uses the contents of ScreenLines[] and doesn't change it.
7121 */
7122 void
7123screen_draw_rectangle(row, col, height, width, invert)
7124 int row;
7125 int col;
7126 int height;
7127 int width;
7128 int invert;
7129{
7130 int r, c;
7131 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007132#ifdef FEAT_MBYTE
7133 int max_off;
7134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007136 /* Can't use ScreenLines unless initialized */
7137 if (ScreenLines == NULL)
7138 return;
7139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 if (invert)
7141 screen_char_attr = HL_INVERSE;
7142 for (r = row; r < row + height; ++r)
7143 {
7144 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007145#ifdef FEAT_MBYTE
7146 max_off = off + screen_Columns;
7147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 for (c = col; c < col + width; ++c)
7149 {
7150#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007151 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {
7153 screen_char_2(off + c, r, c);
7154 ++c;
7155 }
7156 else
7157#endif
7158 {
7159 screen_char(off + c, r, c);
7160#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007161 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 ++c;
7163#endif
7164 }
7165 }
7166 }
7167 screen_char_attr = 0;
7168}
7169#endif
7170
7171#ifdef FEAT_VERTSPLIT
7172/*
7173 * Redraw the characters for a vertically split window.
7174 */
7175 static void
7176redraw_block(row, end, wp)
7177 int row;
7178 int end;
7179 win_T *wp;
7180{
7181 int col;
7182 int width;
7183
7184# ifdef FEAT_CLIPBOARD
7185 clip_may_clear_selection(row, end - 1);
7186# endif
7187
7188 if (wp == NULL)
7189 {
7190 col = 0;
7191 width = Columns;
7192 }
7193 else
7194 {
7195 col = wp->w_wincol;
7196 width = wp->w_width;
7197 }
7198 screen_draw_rectangle(row, col, end - row, width, FALSE);
7199}
7200#endif
7201
7202/*
7203 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7204 * with character 'c1' in first column followed by 'c2' in the other columns.
7205 * Use attributes 'attr'.
7206 */
7207 void
7208screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7209 int start_row, end_row;
7210 int start_col, end_col;
7211 int c1, c2;
7212 int attr;
7213{
7214 int row;
7215 int col;
7216 int off;
7217 int end_off;
7218 int did_delete;
7219 int c;
7220 int norm_term;
7221#if defined(FEAT_GUI) || defined(UNIX)
7222 int force_next = FALSE;
7223#endif
7224
7225 if (end_row > screen_Rows) /* safety check */
7226 end_row = screen_Rows;
7227 if (end_col > screen_Columns) /* safety check */
7228 end_col = screen_Columns;
7229 if (ScreenLines == NULL
7230 || start_row >= end_row
7231 || start_col >= end_col) /* nothing to do */
7232 return;
7233
7234 /* it's a "normal" terminal when not in a GUI or cterm */
7235 norm_term = (
7236#ifdef FEAT_GUI
7237 !gui.in_use &&
7238#endif
7239 t_colors <= 1);
7240 for (row = start_row; row < end_row; ++row)
7241 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007242#ifdef FEAT_MBYTE
7243 if (has_mbyte
7244# ifdef FEAT_GUI
7245 && !gui.in_use
7246# endif
7247 )
7248 {
7249 /* When drawing over the right halve of a double-wide char clear
7250 * out the left halve. When drawing over the left halve of a
7251 * double wide-char clear out the right halve. Only needed in a
7252 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007253 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007254 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007255 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007256 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007257 }
7258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 /*
7260 * Try to use delete-line termcap code, when no attributes or in a
7261 * "normal" terminal, where a bold/italic space is just a
7262 * space.
7263 */
7264 did_delete = FALSE;
7265 if (c2 == ' '
7266 && end_col == Columns
7267 && can_clear(T_CE)
7268 && (attr == 0
7269 || (norm_term
7270 && attr <= HL_ALL
7271 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7272 {
7273 /*
7274 * check if we really need to clear something
7275 */
7276 col = start_col;
7277 if (c1 != ' ') /* don't clear first char */
7278 ++col;
7279
7280 off = LineOffset[row] + col;
7281 end_off = LineOffset[row] + end_col;
7282
7283 /* skip blanks (used often, keep it fast!) */
7284#ifdef FEAT_MBYTE
7285 if (enc_utf8)
7286 while (off < end_off && ScreenLines[off] == ' '
7287 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7288 ++off;
7289 else
7290#endif
7291 while (off < end_off && ScreenLines[off] == ' '
7292 && ScreenAttrs[off] == 0)
7293 ++off;
7294 if (off < end_off) /* something to be cleared */
7295 {
7296 col = off - LineOffset[row];
7297 screen_stop_highlight();
7298 term_windgoto(row, col);/* clear rest of this screen line */
7299 out_str(T_CE);
7300 screen_start(); /* don't know where cursor is now */
7301 col = end_col - col;
7302 while (col--) /* clear chars in ScreenLines */
7303 {
7304 ScreenLines[off] = ' ';
7305#ifdef FEAT_MBYTE
7306 if (enc_utf8)
7307 ScreenLinesUC[off] = 0;
7308#endif
7309 ScreenAttrs[off] = 0;
7310 ++off;
7311 }
7312 }
7313 did_delete = TRUE; /* the chars are cleared now */
7314 }
7315
7316 off = LineOffset[row] + start_col;
7317 c = c1;
7318 for (col = start_col; col < end_col; ++col)
7319 {
7320 if (ScreenLines[off] != c
7321#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007322 || (enc_utf8 && (int)ScreenLinesUC[off]
7323 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324#endif
7325 || ScreenAttrs[off] != attr
7326#if defined(FEAT_GUI) || defined(UNIX)
7327 || force_next
7328#endif
7329 )
7330 {
7331#if defined(FEAT_GUI) || defined(UNIX)
7332 /* The bold trick may make a single row of pixels appear in
7333 * the next character. When a bold character is removed, the
7334 * next character should be redrawn too. This happens for our
7335 * own GUI and for some xterms. */
7336 if (
7337# ifdef FEAT_GUI
7338 gui.in_use
7339# endif
7340# if defined(FEAT_GUI) && defined(UNIX)
7341 ||
7342# endif
7343# ifdef UNIX
7344 term_is_xterm
7345# endif
7346 )
7347 {
7348 if (ScreenLines[off] != ' '
7349 && (ScreenAttrs[off] > HL_ALL
7350 || ScreenAttrs[off] & HL_BOLD))
7351 force_next = TRUE;
7352 else
7353 force_next = FALSE;
7354 }
7355#endif
7356 ScreenLines[off] = c;
7357#ifdef FEAT_MBYTE
7358 if (enc_utf8)
7359 {
7360 if (c >= 0x80)
7361 {
7362 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007363 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
7365 else
7366 ScreenLinesUC[off] = 0;
7367 }
7368#endif
7369 ScreenAttrs[off] = attr;
7370 if (!did_delete || c != ' ')
7371 screen_char(off, row, col);
7372 }
7373 ++off;
7374 if (col == start_col)
7375 {
7376 if (did_delete)
7377 break;
7378 c = c2;
7379 }
7380 }
7381 if (end_col == Columns)
7382 LineWraps[row] = FALSE;
7383 if (row == Rows - 1) /* overwritten the command line */
7384 {
7385 redraw_cmdline = TRUE;
7386 if (c1 == ' ' && c2 == ' ')
7387 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007388 if (start_col == 0)
7389 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 }
7391 }
7392}
7393
7394/*
7395 * Check if there should be a delay. Used before clearing or redrawing the
7396 * screen or the command line.
7397 */
7398 void
7399check_for_delay(check_msg_scroll)
7400 int check_msg_scroll;
7401{
7402 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7403 && !did_wait_return
7404 && emsg_silent == 0)
7405 {
7406 out_flush();
7407 ui_delay(1000L, TRUE);
7408 emsg_on_display = FALSE;
7409 if (check_msg_scroll)
7410 msg_scroll = FALSE;
7411 }
7412}
7413
7414/*
7415 * screen_valid - allocate screen buffers if size changed
7416 * If "clear" is TRUE: clear screen if it has been resized.
7417 * Returns TRUE if there is a valid screen to write to.
7418 * Returns FALSE when starting up and screen not initialized yet.
7419 */
7420 int
7421screen_valid(clear)
7422 int clear;
7423{
7424 screenalloc(clear); /* allocate screen buffers if size changed */
7425 return (ScreenLines != NULL);
7426}
7427
7428/*
7429 * Resize the shell to Rows and Columns.
7430 * Allocate ScreenLines[] and associated items.
7431 *
7432 * There may be some time between setting Rows and Columns and (re)allocating
7433 * ScreenLines[]. This happens when starting up and when (manually) changing
7434 * the shell size. Always use screen_Rows and screen_Columns to access items
7435 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7436 * final size of the shell is needed.
7437 */
7438 void
7439screenalloc(clear)
7440 int clear;
7441{
7442 int new_row, old_row;
7443#ifdef FEAT_GUI
7444 int old_Rows;
7445#endif
7446 win_T *wp;
7447 int outofmem = FALSE;
7448 int len;
7449 schar_T *new_ScreenLines;
7450#ifdef FEAT_MBYTE
7451 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007452 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007454 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455#endif
7456 sattr_T *new_ScreenAttrs;
7457 unsigned *new_LineOffset;
7458 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007459#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007460 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007461 tabpage_T *tp;
7462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007464 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007465#ifdef FEAT_AUTOCMD
7466 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007468retry:
7469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 /*
7471 * Allocation of the screen buffers is done only when the size changes and
7472 * when Rows and Columns have been set and we have started doing full
7473 * screen stuff.
7474 */
7475 if ((ScreenLines != NULL
7476 && Rows == screen_Rows
7477 && Columns == screen_Columns
7478#ifdef FEAT_MBYTE
7479 && enc_utf8 == (ScreenLinesUC != NULL)
7480 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007481 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482#endif
7483 )
7484 || Rows == 0
7485 || Columns == 0
7486 || (!full_screen && ScreenLines == NULL))
7487 return;
7488
7489 /*
7490 * It's possible that we produce an out-of-memory message below, which
7491 * will cause this function to be called again. To break the loop, just
7492 * return here.
7493 */
7494 if (entered)
7495 return;
7496 entered = TRUE;
7497
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007498 /*
7499 * Note that the window sizes are updated before reallocating the arrays,
7500 * thus we must not redraw here!
7501 */
7502 ++RedrawingDisabled;
7503
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 win_new_shellsize(); /* fit the windows in the new sized shell */
7505
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 comp_col(); /* recompute columns for shown command and ruler */
7507
7508 /*
7509 * We're changing the size of the screen.
7510 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7511 * - Move lines from the old arrays into the new arrays, clear extra
7512 * lines (unless the screen is going to be cleared).
7513 * - Free the old arrays.
7514 *
7515 * If anything fails, make ScreenLines NULL, so we don't do anything!
7516 * Continuing with the old ScreenLines may result in a crash, because the
7517 * size is wrong.
7518 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007519 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007521#ifdef FEAT_AUTOCMD
7522 if (aucmd_win != NULL)
7523 win_free_lsize(aucmd_win);
7524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
7526 new_ScreenLines = (schar_T *)lalloc((long_u)(
7527 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7528#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007529 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 if (enc_utf8)
7531 {
7532 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7533 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007534 for (i = 0; i < p_mco; ++i)
7535 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7537 }
7538 if (enc_dbcs == DBCS_JPNU)
7539 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7540 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7541#endif
7542 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7543 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7544 new_LineOffset = (unsigned *)lalloc((long_u)(
7545 Rows * sizeof(unsigned)), FALSE);
7546 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007547#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007548 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007551 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 {
7553 if (win_alloc_lines(wp) == FAIL)
7554 {
7555 outofmem = TRUE;
7556#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007557 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558#endif
7559 }
7560 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007561#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00007562 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
7563 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00007564 outofmem = TRUE;
7565#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00007566#ifdef FEAT_WINDOWS
7567give_up:
7568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007570#ifdef FEAT_MBYTE
7571 for (i = 0; i < p_mco; ++i)
7572 if (new_ScreenLinesC[i] == NULL)
7573 break;
7574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 if (new_ScreenLines == NULL
7576#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007577 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7579#endif
7580 || new_ScreenAttrs == NULL
7581 || new_LineOffset == NULL
7582 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007583#ifdef FEAT_WINDOWS
7584 || new_TabPageIdxs == NULL
7585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 || outofmem)
7587 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007588 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007589 {
7590 /* guess the size */
7591 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7592
7593 /* Remember we did this to avoid getting outofmem messages over
7594 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007595 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 vim_free(new_ScreenLines);
7598 new_ScreenLines = NULL;
7599#ifdef FEAT_MBYTE
7600 vim_free(new_ScreenLinesUC);
7601 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007602 for (i = 0; i < p_mco; ++i)
7603 {
7604 vim_free(new_ScreenLinesC[i]);
7605 new_ScreenLinesC[i] = NULL;
7606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 vim_free(new_ScreenLines2);
7608 new_ScreenLines2 = NULL;
7609#endif
7610 vim_free(new_ScreenAttrs);
7611 new_ScreenAttrs = NULL;
7612 vim_free(new_LineOffset);
7613 new_LineOffset = NULL;
7614 vim_free(new_LineWraps);
7615 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007616#ifdef FEAT_WINDOWS
7617 vim_free(new_TabPageIdxs);
7618 new_TabPageIdxs = NULL;
7619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 }
7621 else
7622 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007623 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007624
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 for (new_row = 0; new_row < Rows; ++new_row)
7626 {
7627 new_LineOffset[new_row] = new_row * Columns;
7628 new_LineWraps[new_row] = FALSE;
7629
7630 /*
7631 * If the screen is not going to be cleared, copy as much as
7632 * possible from the old screen to the new one and clear the rest
7633 * (used when resizing the window at the "--more--" prompt or when
7634 * executing an external command, for the GUI).
7635 */
7636 if (!clear)
7637 {
7638 (void)vim_memset(new_ScreenLines + new_row * Columns,
7639 ' ', (size_t)Columns * sizeof(schar_T));
7640#ifdef FEAT_MBYTE
7641 if (enc_utf8)
7642 {
7643 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7644 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007645 for (i = 0; i < p_mco; ++i)
7646 (void)vim_memset(new_ScreenLinesC[i]
7647 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 0, (size_t)Columns * sizeof(u8char_T));
7649 }
7650 if (enc_dbcs == DBCS_JPNU)
7651 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7652 0, (size_t)Columns * sizeof(schar_T));
7653#endif
7654 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7655 0, (size_t)Columns * sizeof(sattr_T));
7656 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007657 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
7659 if (screen_Columns < Columns)
7660 len = screen_Columns;
7661 else
7662 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007663#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007664 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007665 * may be invalid now. Also when p_mco changes. */
7666 if (!(enc_utf8 && ScreenLinesUC == NULL)
7667 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007668#endif
7669 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7670 ScreenLines + LineOffset[old_row],
7671 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007673 if (enc_utf8 && ScreenLinesUC != NULL
7674 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {
7676 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7677 ScreenLinesUC + LineOffset[old_row],
7678 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007679 for (i = 0; i < p_mco; ++i)
7680 mch_memmove(new_ScreenLinesC[i]
7681 + new_LineOffset[new_row],
7682 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 (size_t)len * sizeof(u8char_T));
7684 }
7685 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7686 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7687 ScreenLines2 + LineOffset[old_row],
7688 (size_t)len * sizeof(schar_T));
7689#endif
7690 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7691 ScreenAttrs + LineOffset[old_row],
7692 (size_t)len * sizeof(sattr_T));
7693 }
7694 }
7695 }
7696 /* Use the last line of the screen for the current line. */
7697 current_ScreenLine = new_ScreenLines + Rows * Columns;
7698 }
7699
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007700 free_screenlines();
7701
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 ScreenLines = new_ScreenLines;
7703#ifdef FEAT_MBYTE
7704 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007705 for (i = 0; i < p_mco; ++i)
7706 ScreenLinesC[i] = new_ScreenLinesC[i];
7707 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 ScreenLines2 = new_ScreenLines2;
7709#endif
7710 ScreenAttrs = new_ScreenAttrs;
7711 LineOffset = new_LineOffset;
7712 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007713#ifdef FEAT_WINDOWS
7714 TabPageIdxs = new_TabPageIdxs;
7715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716
7717 /* It's important that screen_Rows and screen_Columns reflect the actual
7718 * size of ScreenLines[]. Set them before calling anything. */
7719#ifdef FEAT_GUI
7720 old_Rows = screen_Rows;
7721#endif
7722 screen_Rows = Rows;
7723 screen_Columns = Columns;
7724
7725 must_redraw = CLEAR; /* need to clear the screen later */
7726 if (clear)
7727 screenclear2();
7728
7729#ifdef FEAT_GUI
7730 else if (gui.in_use
7731 && !gui.starting
7732 && ScreenLines != NULL
7733 && old_Rows != Rows)
7734 {
7735 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7736 /*
7737 * Adjust the position of the cursor, for when executing an external
7738 * command.
7739 */
7740 if (msg_row >= Rows) /* Rows got smaller */
7741 msg_row = Rows - 1; /* put cursor at last row */
7742 else if (Rows > old_Rows) /* Rows got bigger */
7743 msg_row += Rows - old_Rows; /* put cursor in same place */
7744 if (msg_col >= Columns) /* Columns got smaller */
7745 msg_col = Columns - 1; /* put cursor at last column */
7746 }
7747#endif
7748
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007750 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007751
7752#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007753 /*
7754 * Do not apply autocommands more than 3 times to avoid an endless loop
7755 * in case applying autocommands always changes Rows or Columns.
7756 */
7757 if (starting == 0 && ++retry_count <= 3)
7758 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007759 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00007760 /* In rare cases, autocommands may have altered Rows or Columns,
7761 * jump back to check if we need to allocate the screen again. */
7762 goto retry;
7763 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765}
7766
7767 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007768free_screenlines()
7769{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007770#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007771 int i;
7772
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007773 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007774 for (i = 0; i < Screen_mco; ++i)
7775 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007776 vim_free(ScreenLines2);
7777#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007778 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007779 vim_free(ScreenAttrs);
7780 vim_free(LineOffset);
7781 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007782#ifdef FEAT_WINDOWS
7783 vim_free(TabPageIdxs);
7784#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007785}
7786
7787 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788screenclear()
7789{
7790 check_for_delay(FALSE);
7791 screenalloc(FALSE); /* allocate screen buffers if size changed */
7792 screenclear2(); /* clear the screen */
7793}
7794
7795 static void
7796screenclear2()
7797{
7798 int i;
7799
7800 if (starting == NO_SCREEN || ScreenLines == NULL
7801#ifdef FEAT_GUI
7802 || (gui.in_use && gui.starting)
7803#endif
7804 )
7805 return;
7806
7807#ifdef FEAT_GUI
7808 if (!gui.in_use)
7809#endif
7810 screen_attr = -1; /* force setting the Normal colors */
7811 screen_stop_highlight(); /* don't want highlighting here */
7812
7813#ifdef FEAT_CLIPBOARD
7814 /* disable selection without redrawing it */
7815 clip_scroll_selection(9999);
7816#endif
7817
7818 /* blank out ScreenLines */
7819 for (i = 0; i < Rows; ++i)
7820 {
7821 lineclear(LineOffset[i], (int)Columns);
7822 LineWraps[i] = FALSE;
7823 }
7824
7825 if (can_clear(T_CL))
7826 {
7827 out_str(T_CL); /* clear the display */
7828 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007829 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 }
7831 else
7832 {
7833 /* can't clear the screen, mark all chars with invalid attributes */
7834 for (i = 0; i < Rows; ++i)
7835 lineinvalid(LineOffset[i], (int)Columns);
7836 clear_cmdline = TRUE;
7837 }
7838
7839 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7840
7841 win_rest_invalid(firstwin);
7842 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007843#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007844 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 if (must_redraw == CLEAR) /* no need to clear again */
7847 must_redraw = NOT_VALID;
7848 compute_cmdrow();
7849 msg_row = cmdline_row; /* put cursor on last line for messages */
7850 msg_col = 0;
7851 screen_start(); /* don't know where cursor is now */
7852 msg_scrolled = 0; /* can't scroll back */
7853 msg_didany = FALSE;
7854 msg_didout = FALSE;
7855}
7856
7857/*
7858 * Clear one line in ScreenLines.
7859 */
7860 static void
7861lineclear(off, width)
7862 unsigned off;
7863 int width;
7864{
7865 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7866#ifdef FEAT_MBYTE
7867 if (enc_utf8)
7868 (void)vim_memset(ScreenLinesUC + off, 0,
7869 (size_t)width * sizeof(u8char_T));
7870#endif
7871 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7872}
7873
7874/*
7875 * Mark one line in ScreenLines invalid by setting the attributes to an
7876 * invalid value.
7877 */
7878 static void
7879lineinvalid(off, width)
7880 unsigned off;
7881 int width;
7882{
7883 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7884}
7885
7886#ifdef FEAT_VERTSPLIT
7887/*
7888 * Copy part of a Screenline for vertically split window "wp".
7889 */
7890 static void
7891linecopy(to, from, wp)
7892 int to;
7893 int from;
7894 win_T *wp;
7895{
7896 unsigned off_to = LineOffset[to] + wp->w_wincol;
7897 unsigned off_from = LineOffset[from] + wp->w_wincol;
7898
7899 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7900 wp->w_width * sizeof(schar_T));
7901# ifdef FEAT_MBYTE
7902 if (enc_utf8)
7903 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007904 int i;
7905
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7907 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007908 for (i = 0; i < p_mco; ++i)
7909 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7910 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 }
7912 if (enc_dbcs == DBCS_JPNU)
7913 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7914 wp->w_width * sizeof(schar_T));
7915# endif
7916 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7917 wp->w_width * sizeof(sattr_T));
7918}
7919#endif
7920
7921/*
7922 * Return TRUE if clearing with term string "p" would work.
7923 * It can't work when the string is empty or it won't set the right background.
7924 */
7925 int
7926can_clear(p)
7927 char_u *p;
7928{
7929 return (*p != NUL && (t_colors <= 1
7930#ifdef FEAT_GUI
7931 || gui.in_use
7932#endif
7933 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7934}
7935
7936/*
7937 * Reset cursor position. Use whenever cursor was moved because of outputting
7938 * something directly to the screen (shell commands) or a terminal control
7939 * code.
7940 */
7941 void
7942screen_start()
7943{
7944 screen_cur_row = screen_cur_col = 9999;
7945}
7946
7947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 * Move the cursor to position "row","col" in the screen.
7949 * This tries to find the most efficient way to move, minimizing the number of
7950 * characters sent to the terminal.
7951 */
7952 void
7953windgoto(row, col)
7954 int row;
7955 int col;
7956{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007957 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 int i;
7959 int plan;
7960 int cost;
7961 int wouldbe_col;
7962 int noinvcurs;
7963 char_u *bs;
7964 int goto_cost;
7965 int attr;
7966
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007967#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7969
7970#define PLAN_LE 1
7971#define PLAN_CR 2
7972#define PLAN_NL 3
7973#define PLAN_WRITE 4
7974 /* Can't use ScreenLines unless initialized */
7975 if (ScreenLines == NULL)
7976 return;
7977
7978 if (col != screen_cur_col || row != screen_cur_row)
7979 {
7980 /* Check for valid position. */
7981 if (row < 0) /* window without text lines? */
7982 row = 0;
7983 if (row >= screen_Rows)
7984 row = screen_Rows - 1;
7985 if (col >= screen_Columns)
7986 col = screen_Columns - 1;
7987
7988 /* check if no cursor movement is allowed in highlight mode */
7989 if (screen_attr && *T_MS == NUL)
7990 noinvcurs = HIGHL_COST;
7991 else
7992 noinvcurs = 0;
7993 goto_cost = GOTO_COST + noinvcurs;
7994
7995 /*
7996 * Plan how to do the positioning:
7997 * 1. Use CR to move it to column 0, same row.
7998 * 2. Use T_LE to move it a few columns to the left.
7999 * 3. Use NL to move a few lines down, column 0.
8000 * 4. Move a few columns to the right with T_ND or by writing chars.
8001 *
8002 * Don't do this if the cursor went beyond the last column, the cursor
8003 * position is unknown then (some terminals wrap, some don't )
8004 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008005 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 * characters to move the cursor to the right.
8007 */
8008 if (row >= screen_cur_row && screen_cur_col < Columns)
8009 {
8010 /*
8011 * If the cursor is in the same row, bigger col, we can use CR
8012 * or T_LE.
8013 */
8014 bs = NULL; /* init for GCC */
8015 attr = screen_attr;
8016 if (row == screen_cur_row && col < screen_cur_col)
8017 {
8018 /* "le" is preferred over "bc", because "bc" is obsolete */
8019 if (*T_LE)
8020 bs = T_LE; /* "cursor left" */
8021 else
8022 bs = T_BC; /* "backspace character (old) */
8023 if (*bs)
8024 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8025 else
8026 cost = 999;
8027 if (col + 1 < cost) /* using CR is less characters */
8028 {
8029 plan = PLAN_CR;
8030 wouldbe_col = 0;
8031 cost = 1; /* CR is just one character */
8032 }
8033 else
8034 {
8035 plan = PLAN_LE;
8036 wouldbe_col = col;
8037 }
8038 if (noinvcurs) /* will stop highlighting */
8039 {
8040 cost += noinvcurs;
8041 attr = 0;
8042 }
8043 }
8044
8045 /*
8046 * If the cursor is above where we want to be, we can use CR LF.
8047 */
8048 else if (row > screen_cur_row)
8049 {
8050 plan = PLAN_NL;
8051 wouldbe_col = 0;
8052 cost = (row - screen_cur_row) * 2; /* CR LF */
8053 if (noinvcurs) /* will stop highlighting */
8054 {
8055 cost += noinvcurs;
8056 attr = 0;
8057 }
8058 }
8059
8060 /*
8061 * If the cursor is in the same row, smaller col, just use write.
8062 */
8063 else
8064 {
8065 plan = PLAN_WRITE;
8066 wouldbe_col = screen_cur_col;
8067 cost = 0;
8068 }
8069
8070 /*
8071 * Check if any characters that need to be written have the
8072 * correct attributes. Also avoid UTF-8 characters.
8073 */
8074 i = col - wouldbe_col;
8075 if (i > 0)
8076 cost += i;
8077 if (cost < goto_cost && i > 0)
8078 {
8079 /*
8080 * Check if the attributes are correct without additionally
8081 * stopping highlighting.
8082 */
8083 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8084 while (i && *p++ == attr)
8085 --i;
8086 if (i != 0)
8087 {
8088 /*
8089 * Try if it works when highlighting is stopped here.
8090 */
8091 if (*--p == 0)
8092 {
8093 cost += noinvcurs;
8094 while (i && *p++ == 0)
8095 --i;
8096 }
8097 if (i != 0)
8098 cost = 999; /* different attributes, don't do it */
8099 }
8100#ifdef FEAT_MBYTE
8101 if (enc_utf8)
8102 {
8103 /* Don't use an UTF-8 char for positioning, it's slow. */
8104 for (i = wouldbe_col; i < col; ++i)
8105 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8106 {
8107 cost = 999;
8108 break;
8109 }
8110 }
8111#endif
8112 }
8113
8114 /*
8115 * We can do it without term_windgoto()!
8116 */
8117 if (cost < goto_cost)
8118 {
8119 if (plan == PLAN_LE)
8120 {
8121 if (noinvcurs)
8122 screen_stop_highlight();
8123 while (screen_cur_col > col)
8124 {
8125 out_str(bs);
8126 --screen_cur_col;
8127 }
8128 }
8129 else if (plan == PLAN_CR)
8130 {
8131 if (noinvcurs)
8132 screen_stop_highlight();
8133 out_char('\r');
8134 screen_cur_col = 0;
8135 }
8136 else if (plan == PLAN_NL)
8137 {
8138 if (noinvcurs)
8139 screen_stop_highlight();
8140 while (screen_cur_row < row)
8141 {
8142 out_char('\n');
8143 ++screen_cur_row;
8144 }
8145 screen_cur_col = 0;
8146 }
8147
8148 i = col - screen_cur_col;
8149 if (i > 0)
8150 {
8151 /*
8152 * Use cursor-right if it's one character only. Avoids
8153 * removing a line of pixels from the last bold char, when
8154 * using the bold trick in the GUI.
8155 */
8156 if (T_ND[0] != NUL && T_ND[1] == NUL)
8157 {
8158 while (i-- > 0)
8159 out_char(*T_ND);
8160 }
8161 else
8162 {
8163 int off;
8164
8165 off = LineOffset[row] + screen_cur_col;
8166 while (i-- > 0)
8167 {
8168 if (ScreenAttrs[off] != screen_attr)
8169 screen_stop_highlight();
8170#ifdef FEAT_MBYTE
8171 out_flush_check();
8172#endif
8173 out_char(ScreenLines[off]);
8174#ifdef FEAT_MBYTE
8175 if (enc_dbcs == DBCS_JPNU
8176 && ScreenLines[off] == 0x8e)
8177 out_char(ScreenLines2[off]);
8178#endif
8179 ++off;
8180 }
8181 }
8182 }
8183 }
8184 }
8185 else
8186 cost = 999;
8187
8188 if (cost >= goto_cost)
8189 {
8190 if (noinvcurs)
8191 screen_stop_highlight();
8192 if (row == screen_cur_row && (col > screen_cur_col) &&
8193 *T_CRI != NUL)
8194 term_cursor_right(col - screen_cur_col);
8195 else
8196 term_windgoto(row, col);
8197 }
8198 screen_cur_row = row;
8199 screen_cur_col = col;
8200 }
8201}
8202
8203/*
8204 * Set cursor to its position in the current window.
8205 */
8206 void
8207setcursor()
8208{
8209 if (redrawing())
8210 {
8211 validate_cursor();
8212 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8213 W_WINCOL(curwin) + (
8214#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008215 /* With 'rightleft' set and the cursor on a double-wide
8216 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8218# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008219 (has_mbyte
8220 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8221 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222# endif
8223 1)) :
8224#endif
8225 curwin->w_wcol));
8226 }
8227}
8228
8229
8230/*
8231 * insert 'line_count' lines at 'row' in window 'wp'
8232 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8233 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8234 * scrolling.
8235 * Returns FAIL if the lines are not inserted, OK for success.
8236 */
8237 int
8238win_ins_lines(wp, row, line_count, invalid, mayclear)
8239 win_T *wp;
8240 int row;
8241 int line_count;
8242 int invalid;
8243 int mayclear;
8244{
8245 int did_delete;
8246 int nextrow;
8247 int lastrow;
8248 int retval;
8249
8250 if (invalid)
8251 wp->w_lines_valid = 0;
8252
8253 if (wp->w_height < 5)
8254 return FAIL;
8255
8256 if (line_count > wp->w_height - row)
8257 line_count = wp->w_height - row;
8258
8259 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8260 if (retval != MAYBE)
8261 return retval;
8262
8263 /*
8264 * If there is a next window or a status line, we first try to delete the
8265 * lines at the bottom to avoid messing what is after the window.
8266 * If this fails and there are following windows, don't do anything to avoid
8267 * messing up those windows, better just redraw.
8268 */
8269 did_delete = FALSE;
8270#ifdef FEAT_WINDOWS
8271 if (wp->w_next != NULL || wp->w_status_height)
8272 {
8273 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8274 line_count, (int)Rows, FALSE, NULL) == OK)
8275 did_delete = TRUE;
8276 else if (wp->w_next)
8277 return FAIL;
8278 }
8279#endif
8280 /*
8281 * if no lines deleted, blank the lines that will end up below the window
8282 */
8283 if (!did_delete)
8284 {
8285#ifdef FEAT_WINDOWS
8286 wp->w_redr_status = TRUE;
8287#endif
8288 redraw_cmdline = TRUE;
8289 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8290 lastrow = nextrow + line_count;
8291 if (lastrow > Rows)
8292 lastrow = Rows;
8293 screen_fill(nextrow - line_count, lastrow - line_count,
8294 W_WINCOL(wp), (int)W_ENDCOL(wp),
8295 ' ', ' ', 0);
8296 }
8297
8298 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8299 == FAIL)
8300 {
8301 /* deletion will have messed up other windows */
8302 if (did_delete)
8303 {
8304#ifdef FEAT_WINDOWS
8305 wp->w_redr_status = TRUE;
8306#endif
8307 win_rest_invalid(W_NEXT(wp));
8308 }
8309 return FAIL;
8310 }
8311
8312 return OK;
8313}
8314
8315/*
8316 * delete "line_count" window lines at "row" in window "wp"
8317 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8318 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8319 * scrolling
8320 * Return OK for success, FAIL if the lines are not deleted.
8321 */
8322 int
8323win_del_lines(wp, row, line_count, invalid, mayclear)
8324 win_T *wp;
8325 int row;
8326 int line_count;
8327 int invalid;
8328 int mayclear;
8329{
8330 int retval;
8331
8332 if (invalid)
8333 wp->w_lines_valid = 0;
8334
8335 if (line_count > wp->w_height - row)
8336 line_count = wp->w_height - row;
8337
8338 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8339 if (retval != MAYBE)
8340 return retval;
8341
8342 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8343 (int)Rows, FALSE, NULL) == FAIL)
8344 return FAIL;
8345
8346#ifdef FEAT_WINDOWS
8347 /*
8348 * If there are windows or status lines below, try to put them at the
8349 * correct place. If we can't do that, they have to be redrawn.
8350 */
8351 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8352 {
8353 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8354 line_count, (int)Rows, NULL) == FAIL)
8355 {
8356 wp->w_redr_status = TRUE;
8357 win_rest_invalid(wp->w_next);
8358 }
8359 }
8360 /*
8361 * If this is the last window and there is no status line, redraw the
8362 * command line later.
8363 */
8364 else
8365#endif
8366 redraw_cmdline = TRUE;
8367 return OK;
8368}
8369
8370/*
8371 * Common code for win_ins_lines() and win_del_lines().
8372 * Returns OK or FAIL when the work has been done.
8373 * Returns MAYBE when not finished yet.
8374 */
8375 static int
8376win_do_lines(wp, row, line_count, mayclear, del)
8377 win_T *wp;
8378 int row;
8379 int line_count;
8380 int mayclear;
8381 int del;
8382{
8383 int retval;
8384
8385 if (!redrawing() || line_count <= 0)
8386 return FAIL;
8387
8388 /* only a few lines left: redraw is faster */
8389 if (mayclear && Rows - line_count < 5
8390#ifdef FEAT_VERTSPLIT
8391 && wp->w_width == Columns
8392#endif
8393 )
8394 {
8395 screenclear(); /* will set wp->w_lines_valid to 0 */
8396 return FAIL;
8397 }
8398
8399 /*
8400 * Delete all remaining lines
8401 */
8402 if (row + line_count >= wp->w_height)
8403 {
8404 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8405 W_WINCOL(wp), (int)W_ENDCOL(wp),
8406 ' ', ' ', 0);
8407 return OK;
8408 }
8409
8410 /*
8411 * when scrolling, the message on the command line should be cleared,
8412 * otherwise it will stay there forever.
8413 */
8414 clear_cmdline = TRUE;
8415
8416 /*
8417 * If the terminal can set a scroll region, use that.
8418 * Always do this in a vertically split window. This will redraw from
8419 * ScreenLines[] when t_CV isn't defined. That's faster than using
8420 * win_line().
8421 * Don't use a scroll region when we are going to redraw the text, writing
8422 * a character in the lower right corner of the scroll region causes a
8423 * scroll-up in the DJGPP version.
8424 */
8425 if (scroll_region
8426#ifdef FEAT_VERTSPLIT
8427 || W_WIDTH(wp) != Columns
8428#endif
8429 )
8430 {
8431#ifdef FEAT_VERTSPLIT
8432 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8433#endif
8434 scroll_region_set(wp, row);
8435 if (del)
8436 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8437 wp->w_height - row, FALSE, wp);
8438 else
8439 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8440 wp->w_height - row, wp);
8441#ifdef FEAT_VERTSPLIT
8442 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8443#endif
8444 scroll_region_reset();
8445 return retval;
8446 }
8447
8448#ifdef FEAT_WINDOWS
8449 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8450 return FAIL;
8451#endif
8452
8453 return MAYBE;
8454}
8455
8456/*
8457 * window 'wp' and everything after it is messed up, mark it for redraw
8458 */
8459 static void
8460win_rest_invalid(wp)
8461 win_T *wp;
8462{
8463#ifdef FEAT_WINDOWS
8464 while (wp != NULL)
8465#else
8466 if (wp != NULL)
8467#endif
8468 {
8469 redraw_win_later(wp, NOT_VALID);
8470#ifdef FEAT_WINDOWS
8471 wp->w_redr_status = TRUE;
8472 wp = wp->w_next;
8473#endif
8474 }
8475 redraw_cmdline = TRUE;
8476}
8477
8478/*
8479 * The rest of the routines in this file perform screen manipulations. The
8480 * given operation is performed physically on the screen. The corresponding
8481 * change is also made to the internal screen image. In this way, the editor
8482 * anticipates the effect of editing changes on the appearance of the screen.
8483 * That way, when we call screenupdate a complete redraw isn't usually
8484 * necessary. Another advantage is that we can keep adding code to anticipate
8485 * screen changes, and in the meantime, everything still works.
8486 */
8487
8488/*
8489 * types for inserting or deleting lines
8490 */
8491#define USE_T_CAL 1
8492#define USE_T_CDL 2
8493#define USE_T_AL 3
8494#define USE_T_CE 4
8495#define USE_T_DL 5
8496#define USE_T_SR 6
8497#define USE_NL 7
8498#define USE_T_CD 8
8499#define USE_REDRAW 9
8500
8501/*
8502 * insert lines on the screen and update ScreenLines[]
8503 * 'end' is the line after the scrolled part. Normally it is Rows.
8504 * When scrolling region used 'off' is the offset from the top for the region.
8505 * 'row' and 'end' are relative to the start of the region.
8506 *
8507 * return FAIL for failure, OK for success.
8508 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008509 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510screen_ins_lines(off, row, line_count, end, wp)
8511 int off;
8512 int row;
8513 int line_count;
8514 int end;
8515 win_T *wp; /* NULL or window to use width from */
8516{
8517 int i;
8518 int j;
8519 unsigned temp;
8520 int cursor_row;
8521 int type;
8522 int result_empty;
8523 int can_ce = can_clear(T_CE);
8524
8525 /*
8526 * FAIL if
8527 * - there is no valid screen
8528 * - the screen has to be redrawn completely
8529 * - the line count is less than one
8530 * - the line count is more than 'ttyscroll'
8531 */
8532 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8533 return FAIL;
8534
8535 /*
8536 * There are seven ways to insert lines:
8537 * 0. When in a vertically split window and t_CV isn't set, redraw the
8538 * characters from ScreenLines[].
8539 * 1. Use T_CD (clear to end of display) if it exists and the result of
8540 * the insert is just empty lines
8541 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8542 * present or line_count > 1. It looks better if we do all the inserts
8543 * at once.
8544 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8545 * insert is just empty lines and T_CE is not present or line_count >
8546 * 1.
8547 * 4. Use T_AL (insert line) if it exists.
8548 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8549 * just empty lines.
8550 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8551 * just empty lines.
8552 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8553 * the 'da' flag is not set or we have clear line capability.
8554 * 8. redraw the characters from ScreenLines[].
8555 *
8556 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8557 * the scrollbar for the window. It does have insert line, use that if it
8558 * exists.
8559 */
8560 result_empty = (row + line_count >= end);
8561#ifdef FEAT_VERTSPLIT
8562 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8563 type = USE_REDRAW;
8564 else
8565#endif
8566 if (can_clear(T_CD) && result_empty)
8567 type = USE_T_CD;
8568 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8569 type = USE_T_CAL;
8570 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8571 type = USE_T_CDL;
8572 else if (*T_AL != NUL)
8573 type = USE_T_AL;
8574 else if (can_ce && result_empty)
8575 type = USE_T_CE;
8576 else if (*T_DL != NUL && result_empty)
8577 type = USE_T_DL;
8578 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8579 type = USE_T_SR;
8580 else
8581 return FAIL;
8582
8583 /*
8584 * For clearing the lines screen_del_lines() is used. This will also take
8585 * care of t_db if necessary.
8586 */
8587 if (type == USE_T_CD || type == USE_T_CDL ||
8588 type == USE_T_CE || type == USE_T_DL)
8589 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8590
8591 /*
8592 * If text is retained below the screen, first clear or delete as many
8593 * lines at the bottom of the window as are about to be inserted so that
8594 * the deleted lines won't later surface during a screen_del_lines.
8595 */
8596 if (*T_DB)
8597 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8598
8599#ifdef FEAT_CLIPBOARD
8600 /* Remove a modeless selection when inserting lines halfway the screen
8601 * or not the full width of the screen. */
8602 if (off + row > 0
8603# ifdef FEAT_VERTSPLIT
8604 || (wp != NULL && wp->w_width != Columns)
8605# endif
8606 )
8607 clip_clear_selection();
8608 else
8609 clip_scroll_selection(-line_count);
8610#endif
8611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612#ifdef FEAT_GUI
8613 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8614 * scrolling is actually carried out. */
8615 gui_dont_update_cursor();
8616#endif
8617
8618 if (*T_CCS != NUL) /* cursor relative to region */
8619 cursor_row = row;
8620 else
8621 cursor_row = row + off;
8622
8623 /*
8624 * Shift LineOffset[] line_count down to reflect the inserted lines.
8625 * Clear the inserted lines in ScreenLines[].
8626 */
8627 row += off;
8628 end += off;
8629 for (i = 0; i < line_count; ++i)
8630 {
8631#ifdef FEAT_VERTSPLIT
8632 if (wp != NULL && wp->w_width != Columns)
8633 {
8634 /* need to copy part of a line */
8635 j = end - 1 - i;
8636 while ((j -= line_count) >= row)
8637 linecopy(j + line_count, j, wp);
8638 j += line_count;
8639 if (can_clear((char_u *)" "))
8640 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8641 else
8642 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8643 LineWraps[j] = FALSE;
8644 }
8645 else
8646#endif
8647 {
8648 j = end - 1 - i;
8649 temp = LineOffset[j];
8650 while ((j -= line_count) >= row)
8651 {
8652 LineOffset[j + line_count] = LineOffset[j];
8653 LineWraps[j + line_count] = LineWraps[j];
8654 }
8655 LineOffset[j + line_count] = temp;
8656 LineWraps[j + line_count] = FALSE;
8657 if (can_clear((char_u *)" "))
8658 lineclear(temp, (int)Columns);
8659 else
8660 lineinvalid(temp, (int)Columns);
8661 }
8662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663
8664 screen_stop_highlight();
8665 windgoto(cursor_row, 0);
8666
8667#ifdef FEAT_VERTSPLIT
8668 /* redraw the characters */
8669 if (type == USE_REDRAW)
8670 redraw_block(row, end, wp);
8671 else
8672#endif
8673 if (type == USE_T_CAL)
8674 {
8675 term_append_lines(line_count);
8676 screen_start(); /* don't know where cursor is now */
8677 }
8678 else
8679 {
8680 for (i = 0; i < line_count; i++)
8681 {
8682 if (type == USE_T_AL)
8683 {
8684 if (i && cursor_row != 0)
8685 windgoto(cursor_row, 0);
8686 out_str(T_AL);
8687 }
8688 else /* type == USE_T_SR */
8689 out_str(T_SR);
8690 screen_start(); /* don't know where cursor is now */
8691 }
8692 }
8693
8694 /*
8695 * With scroll-reverse and 'da' flag set we need to clear the lines that
8696 * have been scrolled down into the region.
8697 */
8698 if (type == USE_T_SR && *T_DA)
8699 {
8700 for (i = 0; i < line_count; ++i)
8701 {
8702 windgoto(off + i, 0);
8703 out_str(T_CE);
8704 screen_start(); /* don't know where cursor is now */
8705 }
8706 }
8707
8708#ifdef FEAT_GUI
8709 gui_can_update_cursor();
8710 if (gui.in_use)
8711 out_flush(); /* always flush after a scroll */
8712#endif
8713 return OK;
8714}
8715
8716/*
8717 * delete lines on the screen and update ScreenLines[]
8718 * 'end' is the line after the scrolled part. Normally it is Rows.
8719 * When scrolling region used 'off' is the offset from the top for the region.
8720 * 'row' and 'end' are relative to the start of the region.
8721 *
8722 * Return OK for success, FAIL if the lines are not deleted.
8723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 int
8725screen_del_lines(off, row, line_count, end, force, wp)
8726 int off;
8727 int row;
8728 int line_count;
8729 int end;
8730 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00008731 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
8733 int j;
8734 int i;
8735 unsigned temp;
8736 int cursor_row;
8737 int cursor_end;
8738 int result_empty; /* result is empty until end of region */
8739 int can_delete; /* deleting line codes can be used */
8740 int type;
8741
8742 /*
8743 * FAIL if
8744 * - there is no valid screen
8745 * - the screen has to be redrawn completely
8746 * - the line count is less than one
8747 * - the line count is more than 'ttyscroll'
8748 */
8749 if (!screen_valid(TRUE) || line_count <= 0 ||
8750 (!force && line_count > p_ttyscroll))
8751 return FAIL;
8752
8753 /*
8754 * Check if the rest of the current region will become empty.
8755 */
8756 result_empty = row + line_count >= end;
8757
8758 /*
8759 * We can delete lines only when 'db' flag not set or when 'ce' option
8760 * available.
8761 */
8762 can_delete = (*T_DB == NUL || can_clear(T_CE));
8763
8764 /*
8765 * There are six ways to delete lines:
8766 * 0. When in a vertically split window and t_CV isn't set, redraw the
8767 * characters from ScreenLines[].
8768 * 1. Use T_CD if it exists and the result is empty.
8769 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8770 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8771 * none of the other ways work.
8772 * 4. Use T_CE (erase line) if the result is empty.
8773 * 5. Use T_DL (delete line) if it exists.
8774 * 6. redraw the characters from ScreenLines[].
8775 */
8776#ifdef FEAT_VERTSPLIT
8777 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8778 type = USE_REDRAW;
8779 else
8780#endif
8781 if (can_clear(T_CD) && result_empty)
8782 type = USE_T_CD;
8783#if defined(__BEOS__) && defined(BEOS_DR8)
8784 /*
8785 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8786 * its internal termcap... this works okay for tests which test *T_DB !=
8787 * NUL. It has the disadvantage that the user cannot use any :set t_*
8788 * command to get T_DB (back) to empty_option, only :set term=... will do
8789 * the trick...
8790 * Anyway, this hack will hopefully go away with the next OS release.
8791 * (Olaf Seibert)
8792 */
8793 else if (row == 0 && T_DB == empty_option
8794 && (line_count == 1 || *T_CDL == NUL))
8795#else
8796 else if (row == 0 && (
8797#ifndef AMIGA
8798 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8799 * up, so use delete-line command */
8800 line_count == 1 ||
8801#endif
8802 *T_CDL == NUL))
8803#endif
8804 type = USE_NL;
8805 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8806 type = USE_T_CDL;
8807 else if (can_clear(T_CE) && result_empty
8808#ifdef FEAT_VERTSPLIT
8809 && (wp == NULL || wp->w_width == Columns)
8810#endif
8811 )
8812 type = USE_T_CE;
8813 else if (*T_DL != NUL && can_delete)
8814 type = USE_T_DL;
8815 else if (*T_CDL != NUL && can_delete)
8816 type = USE_T_CDL;
8817 else
8818 return FAIL;
8819
8820#ifdef FEAT_CLIPBOARD
8821 /* Remove a modeless selection when deleting lines halfway the screen or
8822 * not the full width of the screen. */
8823 if (off + row > 0
8824# ifdef FEAT_VERTSPLIT
8825 || (wp != NULL && wp->w_width != Columns)
8826# endif
8827 )
8828 clip_clear_selection();
8829 else
8830 clip_scroll_selection(line_count);
8831#endif
8832
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833#ifdef FEAT_GUI
8834 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8835 * scrolling is actually carried out. */
8836 gui_dont_update_cursor();
8837#endif
8838
8839 if (*T_CCS != NUL) /* cursor relative to region */
8840 {
8841 cursor_row = row;
8842 cursor_end = end;
8843 }
8844 else
8845 {
8846 cursor_row = row + off;
8847 cursor_end = end + off;
8848 }
8849
8850 /*
8851 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8852 * Clear the inserted lines in ScreenLines[].
8853 */
8854 row += off;
8855 end += off;
8856 for (i = 0; i < line_count; ++i)
8857 {
8858#ifdef FEAT_VERTSPLIT
8859 if (wp != NULL && wp->w_width != Columns)
8860 {
8861 /* need to copy part of a line */
8862 j = row + i;
8863 while ((j += line_count) <= end - 1)
8864 linecopy(j - line_count, j, wp);
8865 j -= line_count;
8866 if (can_clear((char_u *)" "))
8867 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8868 else
8869 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8870 LineWraps[j] = FALSE;
8871 }
8872 else
8873#endif
8874 {
8875 /* whole width, moving the line pointers is faster */
8876 j = row + i;
8877 temp = LineOffset[j];
8878 while ((j += line_count) <= end - 1)
8879 {
8880 LineOffset[j - line_count] = LineOffset[j];
8881 LineWraps[j - line_count] = LineWraps[j];
8882 }
8883 LineOffset[j - line_count] = temp;
8884 LineWraps[j - line_count] = FALSE;
8885 if (can_clear((char_u *)" "))
8886 lineclear(temp, (int)Columns);
8887 else
8888 lineinvalid(temp, (int)Columns);
8889 }
8890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
8892 screen_stop_highlight();
8893
8894#ifdef FEAT_VERTSPLIT
8895 /* redraw the characters */
8896 if (type == USE_REDRAW)
8897 redraw_block(row, end, wp);
8898 else
8899#endif
8900 if (type == USE_T_CD) /* delete the lines */
8901 {
8902 windgoto(cursor_row, 0);
8903 out_str(T_CD);
8904 screen_start(); /* don't know where cursor is now */
8905 }
8906 else if (type == USE_T_CDL)
8907 {
8908 windgoto(cursor_row, 0);
8909 term_delete_lines(line_count);
8910 screen_start(); /* don't know where cursor is now */
8911 }
8912 /*
8913 * Deleting lines at top of the screen or scroll region: Just scroll
8914 * the whole screen (scroll region) up by outputting newlines on the
8915 * last line.
8916 */
8917 else if (type == USE_NL)
8918 {
8919 windgoto(cursor_end - 1, 0);
8920 for (i = line_count; --i >= 0; )
8921 out_char('\n'); /* cursor will remain on same line */
8922 }
8923 else
8924 {
8925 for (i = line_count; --i >= 0; )
8926 {
8927 if (type == USE_T_DL)
8928 {
8929 windgoto(cursor_row, 0);
8930 out_str(T_DL); /* delete a line */
8931 }
8932 else /* type == USE_T_CE */
8933 {
8934 windgoto(cursor_row + i, 0);
8935 out_str(T_CE); /* erase a line */
8936 }
8937 screen_start(); /* don't know where cursor is now */
8938 }
8939 }
8940
8941 /*
8942 * If the 'db' flag is set, we need to clear the lines that have been
8943 * scrolled up at the bottom of the region.
8944 */
8945 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8946 {
8947 for (i = line_count; i > 0; --i)
8948 {
8949 windgoto(cursor_end - i, 0);
8950 out_str(T_CE); /* erase a line */
8951 screen_start(); /* don't know where cursor is now */
8952 }
8953 }
8954
8955#ifdef FEAT_GUI
8956 gui_can_update_cursor();
8957 if (gui.in_use)
8958 out_flush(); /* always flush after a scroll */
8959#endif
8960
8961 return OK;
8962}
8963
8964/*
8965 * show the current mode and ruler
8966 *
8967 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8968 * If clear_cmdline is FALSE there may be a message there that needs to be
8969 * cleared only if a mode is shown.
8970 * Return the length of the message (0 if no message).
8971 */
8972 int
8973showmode()
8974{
8975 int need_clear;
8976 int length = 0;
8977 int do_mode;
8978 int attr;
8979 int nwr_save;
8980#ifdef FEAT_INS_EXPAND
8981 int sub_attr;
8982#endif
8983
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008984 do_mode = ((p_smd && msg_silent == 0)
8985 && ((State & INSERT)
8986 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987#ifdef FEAT_VISUAL
8988 || VIsual_active
8989#endif
8990 ));
8991 if (do_mode || Recording)
8992 {
8993 /*
8994 * Don't show mode right now, when not redrawing or inside a mapping.
8995 * Call char_avail() only when we are going to show something, because
8996 * it takes a bit of time.
8997 */
8998 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8999 {
9000 redraw_cmdline = TRUE; /* show mode later */
9001 return 0;
9002 }
9003
9004 nwr_save = need_wait_return;
9005
9006 /* wait a bit before overwriting an important message */
9007 check_for_delay(FALSE);
9008
9009 /* if the cmdline is more than one line high, erase top lines */
9010 need_clear = clear_cmdline;
9011 if (clear_cmdline && cmdline_row < Rows - 1)
9012 msg_clr_cmdline(); /* will reset clear_cmdline */
9013
9014 /* Position on the last line in the window, column 0 */
9015 msg_pos_mode();
9016 cursor_off();
9017 attr = hl_attr(HLF_CM); /* Highlight mode */
9018 if (do_mode)
9019 {
9020 MSG_PUTS_ATTR("--", attr);
9021#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009022# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 if (xic != NULL && im_get_status() && !p_imdisable
9024 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009025# else
9026 if (
9027# ifdef HAVE_GTK2
9028 preedit_get_status()
9029# else
9030 im_get_status()
9031# endif
9032 )
9033# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
9035 MSG_PUTS_ATTR(" IM", attr);
9036# else
9037 MSG_PUTS_ATTR(" XIM", attr);
9038# endif
9039#endif
9040#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9041 if (gui.in_use)
9042 {
9043 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009044 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 }
9046#endif
9047#ifdef FEAT_INS_EXPAND
9048 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9049 {
9050 /* These messages can get long, avoid a wrap in a narrow
9051 * window. Prefer showing edit_submode_extra. */
9052 length = (Rows - msg_row) * Columns - 3;
9053 if (edit_submode_extra != NULL)
9054 length -= vim_strsize(edit_submode_extra);
9055 if (length > 0)
9056 {
9057 if (edit_submode_pre != NULL)
9058 length -= vim_strsize(edit_submode_pre);
9059 if (length - vim_strsize(edit_submode) > 0)
9060 {
9061 if (edit_submode_pre != NULL)
9062 msg_puts_attr(edit_submode_pre, attr);
9063 msg_puts_attr(edit_submode, attr);
9064 }
9065 if (edit_submode_extra != NULL)
9066 {
9067 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9068 if ((int)edit_submode_highl < (int)HLF_COUNT)
9069 sub_attr = hl_attr(edit_submode_highl);
9070 else
9071 sub_attr = attr;
9072 msg_puts_attr(edit_submode_extra, sub_attr);
9073 }
9074 }
9075 length = 0;
9076 }
9077 else
9078#endif
9079 {
9080#ifdef FEAT_VREPLACE
9081 if (State & VREPLACE_FLAG)
9082 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9083 else
9084#endif
9085 if (State & REPLACE_FLAG)
9086 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9087 else if (State & INSERT)
9088 {
9089#ifdef FEAT_RIGHTLEFT
9090 if (p_ri)
9091 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9092#endif
9093 MSG_PUTS_ATTR(_(" INSERT"), attr);
9094 }
9095 else if (restart_edit == 'I')
9096 MSG_PUTS_ATTR(_(" (insert)"), attr);
9097 else if (restart_edit == 'R')
9098 MSG_PUTS_ATTR(_(" (replace)"), attr);
9099 else if (restart_edit == 'V')
9100 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9101#ifdef FEAT_RIGHTLEFT
9102 if (p_hkmap)
9103 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9104# ifdef FEAT_FKMAP
9105 if (p_fkmap)
9106 MSG_PUTS_ATTR(farsi_text_5, attr);
9107# endif
9108#endif
9109#ifdef FEAT_KEYMAP
9110 if (State & LANGMAP)
9111 {
9112# ifdef FEAT_ARABIC
9113 if (curwin->w_p_arab)
9114 MSG_PUTS_ATTR(_(" Arabic"), attr);
9115 else
9116# endif
9117 MSG_PUTS_ATTR(_(" (lang)"), attr);
9118 }
9119#endif
9120 if ((State & INSERT) && p_paste)
9121 MSG_PUTS_ATTR(_(" (paste)"), attr);
9122
9123#ifdef FEAT_VISUAL
9124 if (VIsual_active)
9125 {
9126 char *p;
9127
9128 /* Don't concatenate separate words to avoid translation
9129 * problems. */
9130 switch ((VIsual_select ? 4 : 0)
9131 + (VIsual_mode == Ctrl_V) * 2
9132 + (VIsual_mode == 'V'))
9133 {
9134 case 0: p = N_(" VISUAL"); break;
9135 case 1: p = N_(" VISUAL LINE"); break;
9136 case 2: p = N_(" VISUAL BLOCK"); break;
9137 case 4: p = N_(" SELECT"); break;
9138 case 5: p = N_(" SELECT LINE"); break;
9139 default: p = N_(" SELECT BLOCK"); break;
9140 }
9141 MSG_PUTS_ATTR(_(p), attr);
9142 }
9143#endif
9144 MSG_PUTS_ATTR(" --", attr);
9145 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009146
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 need_clear = TRUE;
9148 }
9149 if (Recording
9150#ifdef FEAT_INS_EXPAND
9151 && edit_submode == NULL /* otherwise it gets too long */
9152#endif
9153 )
9154 {
9155 MSG_PUTS_ATTR(_("recording"), attr);
9156 need_clear = TRUE;
9157 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009158
9159 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 if (need_clear || clear_cmdline)
9161 msg_clr_eos();
9162 msg_didout = FALSE; /* overwrite this message */
9163 length = msg_col;
9164 msg_col = 0;
9165 need_wait_return = nwr_save; /* never ask for hit-return for this */
9166 }
9167 else if (clear_cmdline && msg_silent == 0)
9168 /* Clear the whole command line. Will reset "clear_cmdline". */
9169 msg_clr_cmdline();
9170
9171#ifdef FEAT_CMDL_INFO
9172# ifdef FEAT_VISUAL
9173 /* In Visual mode the size of the selected area must be redrawn. */
9174 if (VIsual_active)
9175 clear_showcmd();
9176# endif
9177
9178 /* If the last window has no status line, the ruler is after the mode
9179 * message and must be redrawn */
9180 if (redrawing()
9181# ifdef FEAT_WINDOWS
9182 && lastwin->w_status_height == 0
9183# endif
9184 )
9185 win_redr_ruler(lastwin, TRUE);
9186#endif
9187 redraw_cmdline = FALSE;
9188 clear_cmdline = FALSE;
9189
9190 return length;
9191}
9192
9193/*
9194 * Position for a mode message.
9195 */
9196 static void
9197msg_pos_mode()
9198{
9199 msg_col = 0;
9200 msg_row = Rows - 1;
9201}
9202
9203/*
9204 * Delete mode message. Used when ESC is typed which is expected to end
9205 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009206 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 */
9208 void
9209unshowmode(force)
9210 int force;
9211{
9212 /*
9213 * Don't delete it right now, when not redrawing or insided a mapping.
9214 */
9215 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9216 redraw_cmdline = TRUE; /* delete mode later */
9217 else
9218 {
9219 msg_pos_mode();
9220 if (Recording)
9221 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9222 msg_clr_eos();
9223 }
9224}
9225
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009226#if defined(FEAT_WINDOWS)
9227/*
9228 * Draw the tab pages line at the top of the Vim window.
9229 */
9230 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009231draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009232{
9233 int tabcount = 0;
9234 tabpage_T *tp;
9235 int tabwidth;
9236 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009237 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009238 int attr;
9239 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009240 win_T *cwp;
9241 int wincount;
9242 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009243 int c;
9244 int len;
9245 int attr_sel = hl_attr(HLF_TPS);
9246 int attr_nosel = hl_attr(HLF_TP);
9247 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009248 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009249 int room;
9250 int use_sep_chars = (t_colors < 8
9251#ifdef FEAT_GUI
9252 && !gui.in_use
9253#endif
9254 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009255
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009256 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009257
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009258#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009259 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009260 if (gui_use_tabline())
9261 {
9262 gui_update_tabline();
9263 return;
9264 }
9265#endif
9266
9267 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009268 return;
9269
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009270#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009271
9272 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9273 for (scol = 0; scol < Columns; ++scol)
9274 TabPageIdxs[scol] = 0;
9275
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009276 /* Use the 'tabline' option if it's set. */
9277 if (*p_tal != NUL)
9278 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009279 int save_called_emsg = called_emsg;
9280
9281 /* Check for an error. If there is one we would loop in redrawing the
9282 * screen. Avoid that by making 'tabline' empty. */
9283 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009284 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009285 if (called_emsg)
9286 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009287 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009288 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009289 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009290 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009291#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009292 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009293 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9294 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009295
Bram Moolenaar238a5642006-02-21 22:12:05 +00009296 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9297 if (tabwidth < 6)
9298 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009299
Bram Moolenaar238a5642006-02-21 22:12:05 +00009300 attr = attr_nosel;
9301 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009302 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009303 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9304 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009305 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009306 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009307
Bram Moolenaar238a5642006-02-21 22:12:05 +00009308 if (tp->tp_topframe == topframe)
9309 attr = attr_sel;
9310 if (use_sep_chars && col > 0)
9311 screen_putchar('|', 0, col++, attr);
9312
9313 if (tp->tp_topframe != topframe)
9314 attr = attr_nosel;
9315
9316 screen_putchar(' ', 0, col++, attr);
9317
9318 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009319 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009320 cwp = curwin;
9321 wp = firstwin;
9322 }
9323 else
9324 {
9325 cwp = tp->tp_curwin;
9326 wp = tp->tp_firstwin;
9327 }
9328
9329 modified = FALSE;
9330 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9331 if (bufIsChanged(wp->w_buffer))
9332 modified = TRUE;
9333 if (modified || wincount > 1)
9334 {
9335 if (wincount > 1)
9336 {
9337 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009338 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009339 if (col + len >= Columns - 3)
9340 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009341 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009342#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009343 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009344#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009345 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009346#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009347 );
9348 col += len;
9349 }
9350 if (modified)
9351 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9352 screen_putchar(' ', 0, col++, attr);
9353 }
9354
9355 room = scol - col + tabwidth - 1;
9356 if (room > 0)
9357 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009358 /* Get buffer name in NameBuff[] */
9359 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009360 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009361 len = vim_strsize(NameBuff);
9362 p = NameBuff;
9363#ifdef FEAT_MBYTE
9364 if (has_mbyte)
9365 while (len > room)
9366 {
9367 len -= ptr2cells(p);
9368 mb_ptr_adv(p);
9369 }
9370 else
9371#endif
9372 if (len > room)
9373 {
9374 p += len - room;
9375 len = room;
9376 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009377 if (len > Columns - col - 1)
9378 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009379
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009380 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009381 col += len;
9382 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009383 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009384
9385 /* Store the tab page number in TabPageIdxs[], so that
9386 * jump_to_mouse() knows where each one is. */
9387 ++tabcount;
9388 while (scol < col)
9389 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009390 }
9391
Bram Moolenaar238a5642006-02-21 22:12:05 +00009392 if (use_sep_chars)
9393 c = '_';
9394 else
9395 c = ' ';
9396 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009397
9398 /* Put an "X" for closing the current tab if there are several. */
9399 if (first_tabpage->tp_next != NULL)
9400 {
9401 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9402 TabPageIdxs[Columns - 1] = -999;
9403 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009404 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009405
9406 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9407 * set. */
9408 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009409}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009410
9411/*
9412 * Get buffer name for "buf" into NameBuff[].
9413 * Takes care of special buffer names and translates special characters.
9414 */
9415 void
9416get_trans_bufname(buf)
9417 buf_T *buf;
9418{
9419 if (buf_spname(buf) != NULL)
9420 STRCPY(NameBuff, buf_spname(buf));
9421 else
9422 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9423 trans_characters(NameBuff, MAXPATHL);
9424}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009425#endif
9426
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9428/*
9429 * Get the character to use in a status line. Get its attributes in "*attr".
9430 */
9431 static int
9432fillchar_status(attr, is_curwin)
9433 int *attr;
9434 int is_curwin;
9435{
9436 int fill;
9437 if (is_curwin)
9438 {
9439 *attr = hl_attr(HLF_S);
9440 fill = fill_stl;
9441 }
9442 else
9443 {
9444 *attr = hl_attr(HLF_SNC);
9445 fill = fill_stlnc;
9446 }
9447 /* Use fill when there is highlighting, and highlighting of current
9448 * window differs, or the fillchars differ, or this is not the
9449 * current window */
9450 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9451 || !is_curwin || firstwin == lastwin)
9452 || (fill_stl != fill_stlnc)))
9453 return fill;
9454 if (is_curwin)
9455 return '^';
9456 return '=';
9457}
9458#endif
9459
9460#ifdef FEAT_VERTSPLIT
9461/*
9462 * Get the character to use in a separator between vertically split windows.
9463 * Get its attributes in "*attr".
9464 */
9465 static int
9466fillchar_vsep(attr)
9467 int *attr;
9468{
9469 *attr = hl_attr(HLF_C);
9470 if (*attr == 0 && fill_vert == ' ')
9471 return '|';
9472 else
9473 return fill_vert;
9474}
9475#endif
9476
9477/*
9478 * Return TRUE if redrawing should currently be done.
9479 */
9480 int
9481redrawing()
9482{
9483 return (!RedrawingDisabled
9484 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9485}
9486
9487/*
9488 * Return TRUE if printing messages should currently be done.
9489 */
9490 int
9491messaging()
9492{
9493 return (!(p_lz && char_avail() && !KeyTyped));
9494}
9495
9496/*
9497 * Show current status info in ruler and various other places
9498 * If always is FALSE, only show ruler if position has changed.
9499 */
9500 void
9501showruler(always)
9502 int always;
9503{
9504 if (!always && !redrawing())
9505 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009506#ifdef FEAT_INS_EXPAND
9507 if (pum_visible())
9508 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009509# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009510 /* Don't redraw right now, do it later. */
9511 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009512# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009513 return;
9514 }
9515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009517 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009518 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00009519 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 else
9522#endif
9523#ifdef FEAT_CMDL_INFO
9524 win_redr_ruler(curwin, always);
9525#endif
9526
9527#ifdef FEAT_TITLE
9528 if (need_maketitle
9529# ifdef FEAT_STL_OPT
9530 || (p_icon && (stl_syntax & STL_IN_ICON))
9531 || (p_title && (stl_syntax & STL_IN_TITLE))
9532# endif
9533 )
9534 maketitle();
9535#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009536#ifdef FEAT_WINDOWS
9537 /* Redraw the tab pages line if needed. */
9538 if (redraw_tabline)
9539 draw_tabline();
9540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541}
9542
9543#ifdef FEAT_CMDL_INFO
9544 static void
9545win_redr_ruler(wp, always)
9546 win_T *wp;
9547 int always;
9548{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009549#define RULER_BUF_LEN 70
9550 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 int row;
9552 int fillchar;
9553 int attr;
9554 int empty_line = FALSE;
9555 colnr_T virtcol;
9556 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009557 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 int o;
9559#ifdef FEAT_VERTSPLIT
9560 int this_ru_col;
9561 int off = 0;
9562 int width = Columns;
9563# define WITH_OFF(x) x
9564# define WITH_WIDTH(x) x
9565#else
9566# define WITH_OFF(x) 0
9567# define WITH_WIDTH(x) Columns
9568# define this_ru_col ru_col
9569#endif
9570
9571 /* If 'ruler' off or redrawing disabled, don't do anything */
9572 if (!p_ru)
9573 return;
9574
9575 /*
9576 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9577 * after deleting lines, before cursor.lnum is corrected.
9578 */
9579 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9580 return;
9581
9582#ifdef FEAT_INS_EXPAND
9583 /* Don't draw the ruler while doing insert-completion, it might overwrite
9584 * the (long) mode message. */
9585# ifdef FEAT_WINDOWS
9586 if (wp == lastwin && lastwin->w_status_height == 0)
9587# endif
9588 if (edit_submode != NULL)
9589 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009590 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9591 if (pum_visible())
9592 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593#endif
9594
9595#ifdef FEAT_STL_OPT
9596 if (*p_ruf)
9597 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009598 int save_called_emsg = called_emsg;
9599
9600 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009602 if (called_emsg)
9603 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009604 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009605 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 return;
9607 }
9608#endif
9609
9610 /*
9611 * Check if not in Insert mode and the line is empty (will show "0-1").
9612 */
9613 if (!(State & INSERT)
9614 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9615 empty_line = TRUE;
9616
9617 /*
9618 * Only draw the ruler when something changed.
9619 */
9620 validate_virtcol_win(wp);
9621 if ( redraw_cmdline
9622 || always
9623 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9624 || wp->w_cursor.col != wp->w_ru_cursor.col
9625 || wp->w_virtcol != wp->w_ru_virtcol
9626#ifdef FEAT_VIRTUALEDIT
9627 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9628#endif
9629 || wp->w_topline != wp->w_ru_topline
9630 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9631#ifdef FEAT_DIFF
9632 || wp->w_topfill != wp->w_ru_topfill
9633#endif
9634 || empty_line != wp->w_ru_empty)
9635 {
9636 cursor_off();
9637#ifdef FEAT_WINDOWS
9638 if (wp->w_status_height)
9639 {
9640 row = W_WINROW(wp) + wp->w_height;
9641 fillchar = fillchar_status(&attr, wp == curwin);
9642# ifdef FEAT_VERTSPLIT
9643 off = W_WINCOL(wp);
9644 width = W_WIDTH(wp);
9645# endif
9646 }
9647 else
9648#endif
9649 {
9650 row = Rows - 1;
9651 fillchar = ' ';
9652 attr = 0;
9653#ifdef FEAT_VERTSPLIT
9654 width = Columns;
9655 off = 0;
9656#endif
9657 }
9658
9659 /* In list mode virtcol needs to be recomputed */
9660 virtcol = wp->w_virtcol;
9661 if (wp->w_p_list && lcs_tab1 == NUL)
9662 {
9663 wp->w_p_list = FALSE;
9664 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9665 wp->w_p_list = TRUE;
9666 }
9667
9668 /*
9669 * Some sprintfs return the length, some return a pointer.
9670 * To avoid portability problems we use strlen() here.
9671 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009672 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9674 ? 0L
9675 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009676 len = STRLEN(buffer);
9677 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9679 (int)virtcol + 1);
9680
9681 /*
9682 * Add a "50%" if there is room for it.
9683 * On the last line, don't print in the last column (scrolls the
9684 * screen up on some terminals).
9685 */
9686 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009687 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 o = i + vim_strsize(buffer + i + 1);
9689#ifdef FEAT_WINDOWS
9690 if (wp->w_status_height == 0) /* can't use last char of screen */
9691#endif
9692 ++o;
9693#ifdef FEAT_VERTSPLIT
9694 this_ru_col = ru_col - (Columns - width);
9695 if (this_ru_col < 0)
9696 this_ru_col = 0;
9697#endif
9698 /* Never use more than half the window/screen width, leave the other
9699 * half for the filename. */
9700 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9701 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9702 if (this_ru_col + o < WITH_WIDTH(width))
9703 {
9704 while (this_ru_col + o < WITH_WIDTH(width))
9705 {
9706#ifdef FEAT_MBYTE
9707 if (has_mbyte)
9708 i += (*mb_char2bytes)(fillchar, buffer + i);
9709 else
9710#endif
9711 buffer[i++] = fillchar;
9712 ++o;
9713 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00009714 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 }
9716 /* Truncate at window boundary. */
9717#ifdef FEAT_MBYTE
9718 if (has_mbyte)
9719 {
9720 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009721 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 {
9723 o += (*mb_ptr2cells)(buffer + i);
9724 if (this_ru_col + o > WITH_WIDTH(width))
9725 {
9726 buffer[i] = NUL;
9727 break;
9728 }
9729 }
9730 }
9731 else
9732#endif
9733 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9734 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9735
9736 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9737 i = redraw_cmdline;
9738 screen_fill(row, row + 1,
9739 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9740 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9741 fillchar, fillchar, attr);
9742 /* don't redraw the cmdline because of showing the ruler */
9743 redraw_cmdline = i;
9744 wp->w_ru_cursor = wp->w_cursor;
9745 wp->w_ru_virtcol = wp->w_virtcol;
9746 wp->w_ru_empty = empty_line;
9747 wp->w_ru_topline = wp->w_topline;
9748 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9749#ifdef FEAT_DIFF
9750 wp->w_ru_topfill = wp->w_topfill;
9751#endif
9752 }
9753}
9754#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009755
9756#if defined(FEAT_LINEBREAK) || defined(PROTO)
9757/*
9758 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009759 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009760 * Otherwise it depends on 'numberwidth' and the line count.
9761 */
9762 int
9763number_width(wp)
9764 win_T *wp;
9765{
9766 int n;
9767 linenr_T lnum;
9768
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009769 lnum = wp->w_buffer->b_ml.ml_line_count;
9770 if (lnum == wp->w_nrwidth_line_count)
9771 return wp->w_nrwidth_width;
9772 wp->w_nrwidth_line_count = lnum;
9773
9774 n = 0;
9775 do
9776 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009777 lnum /= 10;
9778 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009779 } while (lnum > 0);
9780
9781 /* 'numberwidth' gives the minimal width plus one */
9782 if (n < wp->w_p_nuw - 1)
9783 n = wp->w_p_nuw - 1;
9784
9785 wp->w_nrwidth_width = n;
9786 return n;
9787}
9788#endif