blob: f395bd0fb71248b5eb2a474255fcdac8802123e2 [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
135static void redraw_custum_statusline __ARGS((win_T *wp));
136#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 */
273/*ARGSUSED*/
274 void
275redrawWinline(lnum, invalid)
276 linenr_T lnum;
277 int invalid; /* window line height is invalid now */
278{
279#ifdef FEAT_FOLDING
280 int i;
281#endif
282
283 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
284 curwin->w_redraw_top = lnum;
285 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
286 curwin->w_redraw_bot = lnum;
287 redraw_later(VALID);
288
289#ifdef FEAT_FOLDING
290 if (invalid)
291 {
292 /* A w_lines[] entry for this lnum has become invalid. */
293 i = find_wl_entry(curwin, lnum);
294 if (i >= 0)
295 curwin->w_lines[i].wl_valid = FALSE;
296 }
297#endif
298}
299
300/*
301 * update all windows that are editing the current buffer
302 */
303 void
304update_curbuf(type)
305 int type;
306{
307 redraw_curbuf_later(type);
308 update_screen(type);
309}
310
311/*
312 * update_screen()
313 *
314 * Based on the current value of curwin->w_topline, transfer a screenfull
315 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
316 */
317 void
318update_screen(type)
319 int type;
320{
321 win_T *wp;
322 static int did_intro = FALSE;
323#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
324 int did_one;
325#endif
326
327 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
346 if (!redrawing())
347 {
348 redraw_later(type); /* remember type for next time */
349 must_redraw = type;
350 if (type > INVERTED_ALL)
351 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
352 return;
353 }
354
355 updating_screen = TRUE;
356#ifdef FEAT_SYN_HL
357 ++display_tick; /* let syntax code know we're in a next round of
358 * display updating */
359#endif
360
361 /*
362 * if the screen was scrolled up when displaying a message, scroll it down
363 */
364 if (msg_scrolled)
365 {
366 clear_cmdline = TRUE;
367 if (msg_scrolled > Rows - 5) /* clearing is faster */
368 type = CLEAR;
369 else if (type != CLEAR)
370 {
371 check_for_delay(FALSE);
372 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
373 type = CLEAR;
374 FOR_ALL_WINDOWS(wp)
375 {
376 if (W_WINROW(wp) < msg_scrolled)
377 {
378 if (W_WINROW(wp) + wp->w_height > msg_scrolled
379 && wp->w_redr_type < REDRAW_TOP
380 && wp->w_lines_valid > 0
381 && wp->w_topline == wp->w_lines[0].wl_lnum)
382 {
383 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
384 wp->w_redr_type = REDRAW_TOP;
385 }
386 else
387 {
388 wp->w_redr_type = NOT_VALID;
389#ifdef FEAT_WINDOWS
390 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
391 <= msg_scrolled)
392 wp->w_redr_status = TRUE;
393#endif
394 }
395 }
396 }
397 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000398#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000399 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 }
402 msg_scrolled = 0;
403 need_wait_return = FALSE;
404 }
405
406 /* reset cmdline_row now (may have been changed temporarily) */
407 compute_cmdrow();
408
409 /* Check for changed highlighting */
410 if (need_highlight_changed)
411 highlight_changed();
412
413 if (type == CLEAR) /* first clear screen */
414 {
415 screenclear(); /* will reset clear_cmdline */
416 type = NOT_VALID;
417 }
418
419 if (clear_cmdline) /* going to clear cmdline (done below) */
420 check_for_delay(FALSE);
421
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000422#ifdef FEAT_LINEBREAK
423 /* Force redraw when width of 'number' column changes. */
424 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000425 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000426 curwin->w_redr_type = NOT_VALID;
427#endif
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 /*
430 * Only start redrawing if there is really something to do.
431 */
432 if (type == INVERTED)
433 update_curswant();
434 if (curwin->w_redr_type < type
435 && !((type == VALID
436 && curwin->w_lines[0].wl_valid
437#ifdef FEAT_DIFF
438 && curwin->w_topfill == curwin->w_old_topfill
439 && curwin->w_botfill == curwin->w_old_botfill
440#endif
441 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
442#ifdef FEAT_VISUAL
443 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000444 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
446 && curwin->w_old_visual_mode == VIsual_mode
447 && (curwin->w_valid & VALID_VIRTCOL)
448 && curwin->w_old_curswant == curwin->w_curswant)
449#endif
450 ))
451 curwin->w_redr_type = type;
452
Bram Moolenaar5a305422006-04-28 22:38:25 +0000453#ifdef FEAT_WINDOWS
454 /* Redraw the tab pages line if needed. */
455 if (redraw_tabline || type >= NOT_VALID)
456 draw_tabline();
457#endif
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459#ifdef FEAT_SYN_HL
460 /*
461 * Correct stored syntax highlighting info for changes in each displayed
462 * buffer. Each buffer must only be done once.
463 */
464 FOR_ALL_WINDOWS(wp)
465 {
466 if (wp->w_buffer->b_mod_set)
467 {
468# ifdef FEAT_WINDOWS
469 win_T *wwp;
470
471 /* Check if we already did this buffer. */
472 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
473 if (wwp->w_buffer == wp->w_buffer)
474 break;
475# endif
476 if (
477# ifdef FEAT_WINDOWS
478 wwp == wp &&
479# endif
480 syntax_present(wp->w_buffer))
481 syn_stack_apply_changes(wp->w_buffer);
482 }
483 }
484#endif
485
486 /*
487 * Go from top to bottom through the windows, redrawing the ones that need
488 * it.
489 */
490#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
491 did_one = FALSE;
492#endif
493#ifdef FEAT_SEARCH_EXTRA
494 search_hl.rm.regprog = NULL;
495#endif
496 FOR_ALL_WINDOWS(wp)
497 {
498 if (wp->w_redr_type != 0)
499 {
500 cursor_off();
501#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
502 if (!did_one)
503 {
504 did_one = TRUE;
505# ifdef FEAT_SEARCH_EXTRA
506 start_search_hl();
507# endif
508# ifdef FEAT_CLIPBOARD
509 /* When Visual area changed, may have to update selection. */
510 if (clip_star.available && clip_isautosel())
511 clip_update_selection();
512# endif
513#ifdef FEAT_GUI
514 /* Remove the cursor before starting to do anything, because
515 * scrolling may make it difficult to redraw the text under
516 * it. */
517 if (gui.in_use)
518 gui_undraw_cursor();
519#endif
520 }
521#endif
522 win_update(wp);
523 }
524
525#ifdef FEAT_WINDOWS
526 /* redraw status line after the window to minimize cursor movement */
527 if (wp->w_redr_status)
528 {
529 cursor_off();
530 win_redr_status(wp);
531 }
532#endif
533 }
534#if defined(FEAT_SEARCH_EXTRA)
535 end_search_hl();
536#endif
537
538#ifdef FEAT_WINDOWS
539 /* Reset b_mod_set flags. Going through all windows is probably faster
540 * than going through all buffers (there could be many buffers). */
541 for (wp = firstwin; wp != NULL; wp = wp->w_next)
542 wp->w_buffer->b_mod_set = FALSE;
543#else
544 curbuf->b_mod_set = FALSE;
545#endif
546
547 updating_screen = FALSE;
548#ifdef FEAT_GUI
549 gui_may_resize_shell();
550#endif
551
552 /* Clear or redraw the command line. Done last, because scrolling may
553 * mess up the command line. */
554 if (clear_cmdline || redraw_cmdline)
555 showmode();
556
557 /* May put up an introductory message when not editing a file */
558 if (!did_intro && bufempty()
559 && curbuf->b_fname == NULL
560#ifdef FEAT_WINDOWS
561 && firstwin->w_next == NULL
562#endif
563 && vim_strchr(p_shm, SHM_INTRO) == NULL)
564 intro_message(FALSE);
565 did_intro = TRUE;
566
567#ifdef FEAT_GUI
568 /* Redraw the cursor and update the scrollbars when all screen updating is
569 * done. */
570 if (gui.in_use)
571 {
572 out_flush(); /* required before updating the cursor */
573 if (did_one)
574 gui_update_cursor(FALSE, FALSE);
575 gui_update_scrollbars(FALSE);
576 }
577#endif
578}
579
580#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
581static void update_prepare __ARGS((void));
582static void update_finish __ARGS((void));
583
584/*
585 * Prepare for updating one or more windows.
586 */
587 static void
588update_prepare()
589{
590 cursor_off();
591 updating_screen = TRUE;
592#ifdef FEAT_GUI
593 /* Remove the cursor before starting to do anything, because scrolling may
594 * make it difficult to redraw the text under it. */
595 if (gui.in_use)
596 gui_undraw_cursor();
597#endif
598#ifdef FEAT_SEARCH_EXTRA
599 start_search_hl();
600#endif
601}
602
603/*
604 * Finish updating one or more windows.
605 */
606 static void
607update_finish()
608{
609 if (redraw_cmdline)
610 showmode();
611
612# ifdef FEAT_SEARCH_EXTRA
613 end_search_hl();
614# endif
615
616 updating_screen = FALSE;
617
618# ifdef FEAT_GUI
619 gui_may_resize_shell();
620
621 /* Redraw the cursor and update the scrollbars when all screen updating is
622 * done. */
623 if (gui.in_use)
624 {
625 out_flush(); /* required before updating the cursor */
626 gui_update_cursor(FALSE, FALSE);
627 gui_update_scrollbars(FALSE);
628 }
629# endif
630}
631#endif
632
633#if defined(FEAT_SIGNS) || defined(PROTO)
634 void
635update_debug_sign(buf, lnum)
636 buf_T *buf;
637 linenr_T lnum;
638{
639 win_T *wp;
640 int doit = FALSE;
641
642# ifdef FEAT_FOLDING
643 win_foldinfo.fi_level = 0;
644# endif
645
646 /* update/delete a specific mark */
647 FOR_ALL_WINDOWS(wp)
648 {
649 if (buf != NULL && lnum > 0)
650 {
651 if (wp->w_buffer == buf && lnum >= wp->w_topline
652 && lnum < wp->w_botline)
653 {
654 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
655 wp->w_redraw_top = lnum;
656 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
657 wp->w_redraw_bot = lnum;
658 redraw_win_later(wp, VALID);
659 }
660 }
661 else
662 redraw_win_later(wp, VALID);
663 if (wp->w_redr_type != 0)
664 doit = TRUE;
665 }
666
667 if (!doit)
668 return;
669
670 /* update all windows that need updating */
671 update_prepare();
672
673# ifdef FEAT_WINDOWS
674 for (wp = firstwin; wp; wp = wp->w_next)
675 {
676 if (wp->w_redr_type != 0)
677 win_update(wp);
678 if (wp->w_redr_status)
679 win_redr_status(wp);
680 }
681# else
682 if (curwin->w_redr_type != 0)
683 win_update(curwin);
684# endif
685
686 update_finish();
687}
688#endif
689
690
691#if defined(FEAT_GUI) || defined(PROTO)
692/*
693 * Update a single window, its status line and maybe the command line msg.
694 * Used for the GUI scrollbar.
695 */
696 void
697updateWindow(wp)
698 win_T *wp;
699{
700 update_prepare();
701
702#ifdef FEAT_CLIPBOARD
703 /* When Visual area changed, may have to update selection. */
704 if (clip_star.available && clip_isautosel())
705 clip_update_selection();
706#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000711 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000712 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000713 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (wp->w_redr_status
716# ifdef FEAT_CMDL_INFO
717 || p_ru
718# endif
719# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000720 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721# endif
722 )
723 win_redr_status(wp);
724#endif
725
726 update_finish();
727}
728#endif
729
730/*
731 * Update a single window.
732 *
733 * This may cause the windows below it also to be redrawn (when clearing the
734 * screen or scrolling lines).
735 *
736 * How the window is redrawn depends on wp->w_redr_type. Each type also
737 * implies the one below it.
738 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000739 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
741 * INVERTED redraw the changed part of the Visual area
742 * INVERTED_ALL redraw the whole Visual area
743 * VALID 1. scroll up/down to adjust for a changed w_topline
744 * 2. update lines at the top when scrolled down
745 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000746 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * b_mod_top and b_mod_bot.
748 * - if wp->w_redraw_top non-zero, redraw lines between
749 * wp->w_redraw_top and wp->w_redr_bot.
750 * - continue redrawing when syntax status is invalid.
751 * 4. if scrolled up, update lines at the bottom.
752 * This results in three areas that may need updating:
753 * top: from first row to top_end (when scrolled down)
754 * mid: from mid_start to mid_end (update inversion or changed text)
755 * bot: from bot_start to last row (when scrolled up)
756 */
757 static void
758win_update(wp)
759 win_T *wp;
760{
761 buf_T *buf = wp->w_buffer;
762 int type;
763 int top_end = 0; /* Below last row of the top area that needs
764 updating. 0 when no top area updating. */
765 int mid_start = 999;/* first row of the mid area that needs
766 updating. 999 when no mid area updating. */
767 int mid_end = 0; /* Below last row of the mid area that needs
768 updating. 0 when no mid area updating. */
769 int bot_start = 999;/* first row of the bot area that needs
770 updating. 999 when no bot area updating */
771#ifdef FEAT_VISUAL
772 int scrolled_down = FALSE; /* TRUE when scrolled down when
773 w_topline got smaller a bit */
774#endif
775#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000776 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 int top_to_mod = FALSE; /* redraw above mod_top */
778#endif
779
780 int row; /* current window row to display */
781 linenr_T lnum; /* current buffer lnum to display */
782 int idx; /* current index in w_lines[] */
783 int srow; /* starting row of the current line */
784
785 int eof = FALSE; /* if TRUE, we hit the end of the file */
786 int didline = FALSE; /* if TRUE, we finished the last line */
787 int i;
788 long j;
789 static int recursive = FALSE; /* being called recursively */
790 int old_botline = wp->w_botline;
791#ifdef FEAT_FOLDING
792 long fold_count;
793#endif
794#ifdef FEAT_SYN_HL
795 /* remember what happened to the previous line, to know if
796 * check_visual_highlight() can be used */
797#define DID_NONE 1 /* didn't update a line */
798#define DID_LINE 2 /* updated a normal line */
799#define DID_FOLD 3 /* updated a folded line */
800 int did_update = DID_NONE;
801 linenr_T syntax_last_parsed = 0; /* last parsed text line */
802#endif
803 linenr_T mod_top = 0;
804 linenr_T mod_bot = 0;
805#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
806 int save_got_int;
807#endif
808
809 type = wp->w_redr_type;
810
811 if (type == NOT_VALID)
812 {
813#ifdef FEAT_WINDOWS
814 wp->w_redr_status = TRUE;
815#endif
816 wp->w_lines_valid = 0;
817 }
818
819 /* Window is zero-height: nothing to draw. */
820 if (wp->w_height == 0)
821 {
822 wp->w_redr_type = 0;
823 return;
824 }
825
826#ifdef FEAT_VERTSPLIT
827 /* Window is zero-width: Only need to draw the separator. */
828 if (wp->w_width == 0)
829 {
830 /* draw the vertical separator right of this window */
831 draw_vsep_win(wp, 0);
832 wp->w_redr_type = 0;
833 return;
834 }
835#endif
836
837#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000838 /* Setup for match and 'hlsearch' highlighting. Disable any previous
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000839 * match */
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000840 cur = wp->w_match_head;
841 while (cur != NULL)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000842 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000843 cur->hl.rm = cur->match;
844 if (cur->hlg_id == 0)
845 cur->hl.attr = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000846 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000847 cur->hl.attr = syn_id2attr(cur->hlg_id);
848 cur->hl.buf = buf;
849 cur->hl.lnum = 0;
850 cur->hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000851# ifdef FEAT_RELTIME
852 /* Set the time limit to 'redrawtime'. */
853 profile_setlimit(p_rdt, &(cur->hl.tm));
854# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000855 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 search_hl.buf = buf;
858 search_hl.lnum = 0;
859 search_hl.first_lnum = 0;
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000860 /* time limit is set at the toplevel, for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861#endif
862
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000863#ifdef FEAT_LINEBREAK
864 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000865 i = wp->w_p_nu ? number_width(wp) : 0;
866 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000867 {
868 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000869 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000870 }
871 else
872#endif
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
875 {
876 /*
877 * When there are both inserted/deleted lines and specific lines to be
878 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
879 * everything (only happens when redrawing is off for while).
880 */
881 type = NOT_VALID;
882 }
883 else
884 {
885 /*
886 * Set mod_top to the first line that needs displaying because of
887 * changes. Set mod_bot to the first line after the changes.
888 */
889 mod_top = wp->w_redraw_top;
890 if (wp->w_redraw_bot != 0)
891 mod_bot = wp->w_redraw_bot + 1;
892 else
893 mod_bot = 0;
894 wp->w_redraw_top = 0; /* reset for next time */
895 wp->w_redraw_bot = 0;
896 if (buf->b_mod_set)
897 {
898 if (mod_top == 0 || mod_top > buf->b_mod_top)
899 {
900 mod_top = buf->b_mod_top;
901#ifdef FEAT_SYN_HL
902 /* Need to redraw lines above the change that may be included
903 * in a pattern match. */
904 if (syntax_present(buf))
905 {
906 mod_top -= buf->b_syn_sync_linebreaks;
907 if (mod_top < 1)
908 mod_top = 1;
909 }
910#endif
911 }
912 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
913 mod_bot = buf->b_mod_bot;
914
915#ifdef FEAT_SEARCH_EXTRA
916 /* When 'hlsearch' is on and using a multi-line search pattern, a
917 * change in one line may make the Search highlighting in a
918 * previous line invalid. Simple solution: redraw all visible
919 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000920 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000922 if (search_hl.rm.regprog != NULL
923 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000925 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000926 {
927 cur = wp->w_match_head;
928 while (cur != NULL)
929 {
930 if (cur->match.regprog != NULL
931 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000932 {
933 top_to_mod = TRUE;
934 break;
935 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000936 cur = cur->next;
937 }
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#endif
940 }
941#ifdef FEAT_FOLDING
942 if (mod_top != 0 && hasAnyFolding(wp))
943 {
944 linenr_T lnumt, lnumb;
945
946 /*
947 * A change in a line can cause lines above it to become folded or
948 * unfolded. Find the top most buffer line that may be affected.
949 * If the line was previously folded and displayed, get the first
950 * line of that fold. If the line is folded now, get the first
951 * folded line. Use the minimum of these two.
952 */
953
954 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
955 * the line below it. If there is no valid entry, use w_topline.
956 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
957 * to this line. If there is no valid entry, use MAXLNUM. */
958 lnumt = wp->w_topline;
959 lnumb = MAXLNUM;
960 for (i = 0; i < wp->w_lines_valid; ++i)
961 if (wp->w_lines[i].wl_valid)
962 {
963 if (wp->w_lines[i].wl_lastlnum < mod_top)
964 lnumt = wp->w_lines[i].wl_lastlnum + 1;
965 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
966 {
967 lnumb = wp->w_lines[i].wl_lnum;
968 /* When there is a fold column it might need updating
969 * in the next line ("J" just above an open fold). */
970 if (wp->w_p_fdc > 0)
971 ++lnumb;
972 }
973 }
974
975 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
976 if (mod_top > lnumt)
977 mod_top = lnumt;
978
979 /* Now do the same for the bottom line (one above mod_bot). */
980 --mod_bot;
981 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
982 ++mod_bot;
983 if (mod_bot < lnumb)
984 mod_bot = lnumb;
985 }
986#endif
987
988 /* When a change starts above w_topline and the end is below
989 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000990 * If the end of the change is above w_topline: do like no change was
991 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (mod_top != 0 && mod_top < wp->w_topline)
993 {
994 if (mod_bot > wp->w_topline)
995 mod_top = wp->w_topline;
996#ifdef FEAT_SYN_HL
997 else if (syntax_present(buf))
998 top_end = 1;
999#endif
1000 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001001
1002 /* When line numbers are displayed need to redraw all lines below
1003 * inserted/deleted lines. */
1004 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1005 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007
1008 /*
1009 * When only displaying the lines at the top, set top_end. Used when
1010 * window has scrolled down for msg_scrolled.
1011 */
1012 if (type == REDRAW_TOP)
1013 {
1014 j = 0;
1015 for (i = 0; i < wp->w_lines_valid; ++i)
1016 {
1017 j += wp->w_lines[i].wl_size;
1018 if (j >= wp->w_upd_rows)
1019 {
1020 top_end = j;
1021 break;
1022 }
1023 }
1024 if (top_end == 0)
1025 /* not found (cannot happen?): redraw everything */
1026 type = NOT_VALID;
1027 else
1028 /* top area defined, the rest is VALID */
1029 type = VALID;
1030 }
1031
Bram Moolenaar367329b2007-08-30 11:53:22 +00001032 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001033 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1034 * non-zero and thus not FALSE) will indicate that screenclear() was not
1035 * called. */
1036 if (screen_cleared)
1037 screen_cleared = MAYBE;
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 /*
1040 * If there are no changes on the screen that require a complete redraw,
1041 * handle three cases:
1042 * 1: we are off the top of the screen by a few lines: scroll down
1043 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1044 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1045 * w_lines[] that needs updating.
1046 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001047 if ((type == VALID || type == SOME_VALID
1048 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#ifdef FEAT_DIFF
1050 && !wp->w_botfill && !wp->w_old_botfill
1051#endif
1052 )
1053 {
1054 if (mod_top != 0 && wp->w_topline == mod_top)
1055 {
1056 /*
1057 * w_topline is the first changed line, the scrolling will be done
1058 * further down.
1059 */
1060 }
1061 else if (wp->w_lines[0].wl_valid
1062 && (wp->w_topline < wp->w_lines[0].wl_lnum
1063#ifdef FEAT_DIFF
1064 || (wp->w_topline == wp->w_lines[0].wl_lnum
1065 && wp->w_topfill > wp->w_old_topfill)
1066#endif
1067 ))
1068 {
1069 /*
1070 * New topline is above old topline: May scroll down.
1071 */
1072#ifdef FEAT_FOLDING
1073 if (hasAnyFolding(wp))
1074 {
1075 linenr_T ln;
1076
1077 /* count the number of lines we are off, counting a sequence
1078 * of folded lines as one */
1079 j = 0;
1080 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1081 {
1082 ++j;
1083 if (j >= wp->w_height - 2)
1084 break;
1085 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1086 }
1087 }
1088 else
1089#endif
1090 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1091 if (j < wp->w_height - 2) /* not too far off */
1092 {
1093 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1094#ifdef FEAT_DIFF
1095 /* insert extra lines for previously invisible filler lines */
1096 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1097 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1098 - wp->w_old_topfill;
1099#endif
1100 if (i < wp->w_height - 2) /* less than a screen off */
1101 {
1102 /*
1103 * Try to insert the correct number of lines.
1104 * If not the last window, delete the lines at the bottom.
1105 * win_ins_lines may fail when the terminal can't do it.
1106 */
1107 if (i > 0)
1108 check_for_delay(FALSE);
1109 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1110 {
1111 if (wp->w_lines_valid != 0)
1112 {
1113 /* Need to update rows that are new, stop at the
1114 * first one that scrolled down. */
1115 top_end = i;
1116#ifdef FEAT_VISUAL
1117 scrolled_down = TRUE;
1118#endif
1119
1120 /* Move the entries that were scrolled, disable
1121 * the entries for the lines to be redrawn. */
1122 if ((wp->w_lines_valid += j) > wp->w_height)
1123 wp->w_lines_valid = wp->w_height;
1124 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1125 wp->w_lines[idx] = wp->w_lines[idx - j];
1126 while (idx >= 0)
1127 wp->w_lines[idx--].wl_valid = FALSE;
1128 }
1129 }
1130 else
1131 mid_start = 0; /* redraw all lines */
1132 }
1133 else
1134 mid_start = 0; /* redraw all lines */
1135 }
1136 else
1137 mid_start = 0; /* redraw all lines */
1138 }
1139 else
1140 {
1141 /*
1142 * New topline is at or below old topline: May scroll up.
1143 * When topline didn't change, find first entry in w_lines[] that
1144 * needs updating.
1145 */
1146
1147 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1148 j = -1;
1149 row = 0;
1150 for (i = 0; i < wp->w_lines_valid; i++)
1151 {
1152 if (wp->w_lines[i].wl_valid
1153 && wp->w_lines[i].wl_lnum == wp->w_topline)
1154 {
1155 j = i;
1156 break;
1157 }
1158 row += wp->w_lines[i].wl_size;
1159 }
1160 if (j == -1)
1161 {
1162 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1163 * lines */
1164 mid_start = 0;
1165 }
1166 else
1167 {
1168 /*
1169 * Try to delete the correct number of lines.
1170 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1171 */
1172#ifdef FEAT_DIFF
1173 /* If the topline didn't change, delete old filler lines,
1174 * otherwise delete filler lines of the new topline... */
1175 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1176 row += wp->w_old_topfill;
1177 else
1178 row += diff_check_fill(wp, wp->w_topline);
1179 /* ... but don't delete new filler lines. */
1180 row -= wp->w_topfill;
1181#endif
1182 if (row > 0)
1183 {
1184 check_for_delay(FALSE);
1185 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1186 bot_start = wp->w_height - row;
1187 else
1188 mid_start = 0; /* redraw all lines */
1189 }
1190 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1191 {
1192 /*
1193 * Skip the lines (below the deleted lines) that are still
1194 * valid and don't need redrawing. Copy their info
1195 * upwards, to compensate for the deleted lines. Set
1196 * bot_start to the first row that needs redrawing.
1197 */
1198 bot_start = 0;
1199 idx = 0;
1200 for (;;)
1201 {
1202 wp->w_lines[idx] = wp->w_lines[j];
1203 /* stop at line that didn't fit, unless it is still
1204 * valid (no lines deleted) */
1205 if (row > 0 && bot_start + row
1206 + (int)wp->w_lines[j].wl_size > wp->w_height)
1207 {
1208 wp->w_lines_valid = idx + 1;
1209 break;
1210 }
1211 bot_start += wp->w_lines[idx++].wl_size;
1212
1213 /* stop at the last valid entry in w_lines[].wl_size */
1214 if (++j >= wp->w_lines_valid)
1215 {
1216 wp->w_lines_valid = idx;
1217 break;
1218 }
1219 }
1220#ifdef FEAT_DIFF
1221 /* Correct the first entry for filler lines at the top
1222 * when it won't get updated below. */
1223 if (wp->w_p_diff && bot_start > 0)
1224 wp->w_lines[0].wl_size =
1225 plines_win_nofill(wp, wp->w_topline, TRUE)
1226 + wp->w_topfill;
1227#endif
1228 }
1229 }
1230 }
1231
1232 /* When starting redraw in the first line, redraw all lines. When
1233 * there is only one window it's probably faster to clear the screen
1234 * first. */
1235 if (mid_start == 0)
1236 {
1237 mid_end = wp->w_height;
1238 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001239 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001240 /* Clear the screen when it was not done by win_del_lines() or
1241 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1242 * then. */
1243 if (screen_cleared != TRUE)
1244 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001245#ifdef FEAT_WINDOWS
1246 /* The screen was cleared, redraw the tab pages line. */
1247 if (redraw_tabline)
1248 draw_tabline();
1249#endif
1250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001252
1253 /* When win_del_lines() or win_ins_lines() caused the screen to be
1254 * cleared (only happens for the first window) or when screenclear()
1255 * was called directly above, "must_redraw" will have been set to
1256 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1257 if (screen_cleared == TRUE)
1258 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
1260 else
1261 {
1262 /* Not VALID or INVERTED: redraw all lines. */
1263 mid_start = 0;
1264 mid_end = wp->w_height;
1265 }
1266
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001267 if (type == SOME_VALID)
1268 {
1269 /* SOME_VALID: redraw all lines. */
1270 mid_start = 0;
1271 mid_end = wp->w_height;
1272 type = NOT_VALID;
1273 }
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#ifdef FEAT_VISUAL
1276 /* check if we are updating or removing the inverted part */
1277 if ((VIsual_active && buf == curwin->w_buffer)
1278 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1279 {
1280 linenr_T from, to;
1281
1282 if (VIsual_active)
1283 {
1284 if (VIsual_active
1285 && (VIsual_mode != wp->w_old_visual_mode
1286 || type == INVERTED_ALL))
1287 {
1288 /*
1289 * If the type of Visual selection changed, redraw the whole
1290 * selection. Also when the ownership of the X selection is
1291 * gained or lost.
1292 */
1293 if (curwin->w_cursor.lnum < VIsual.lnum)
1294 {
1295 from = curwin->w_cursor.lnum;
1296 to = VIsual.lnum;
1297 }
1298 else
1299 {
1300 from = VIsual.lnum;
1301 to = curwin->w_cursor.lnum;
1302 }
1303 /* redraw more when the cursor moved as well */
1304 if (wp->w_old_cursor_lnum < from)
1305 from = wp->w_old_cursor_lnum;
1306 if (wp->w_old_cursor_lnum > to)
1307 to = wp->w_old_cursor_lnum;
1308 if (wp->w_old_visual_lnum < from)
1309 from = wp->w_old_visual_lnum;
1310 if (wp->w_old_visual_lnum > to)
1311 to = wp->w_old_visual_lnum;
1312 }
1313 else
1314 {
1315 /*
1316 * Find the line numbers that need to be updated: The lines
1317 * between the old cursor position and the current cursor
1318 * position. Also check if the Visual position changed.
1319 */
1320 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1321 {
1322 from = curwin->w_cursor.lnum;
1323 to = wp->w_old_cursor_lnum;
1324 }
1325 else
1326 {
1327 from = wp->w_old_cursor_lnum;
1328 to = curwin->w_cursor.lnum;
1329 if (from == 0) /* Visual mode just started */
1330 from = to;
1331 }
1332
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001333 if (VIsual.lnum != wp->w_old_visual_lnum
1334 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 if (wp->w_old_visual_lnum < from
1337 && wp->w_old_visual_lnum != 0)
1338 from = wp->w_old_visual_lnum;
1339 if (wp->w_old_visual_lnum > to)
1340 to = wp->w_old_visual_lnum;
1341 if (VIsual.lnum < from)
1342 from = VIsual.lnum;
1343 if (VIsual.lnum > to)
1344 to = VIsual.lnum;
1345 }
1346 }
1347
1348 /*
1349 * If in block mode and changed column or curwin->w_curswant:
1350 * update all lines.
1351 * First compute the actual start and end column.
1352 */
1353 if (VIsual_mode == Ctrl_V)
1354 {
1355 colnr_T fromc, toc;
1356
1357 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1358 ++toc;
1359 if (curwin->w_curswant == MAXCOL)
1360 toc = MAXCOL;
1361
1362 if (fromc != wp->w_old_cursor_fcol
1363 || toc != wp->w_old_cursor_lcol)
1364 {
1365 if (from > VIsual.lnum)
1366 from = VIsual.lnum;
1367 if (to < VIsual.lnum)
1368 to = VIsual.lnum;
1369 }
1370 wp->w_old_cursor_fcol = fromc;
1371 wp->w_old_cursor_lcol = toc;
1372 }
1373 }
1374 else
1375 {
1376 /* Use the line numbers of the old Visual area. */
1377 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1378 {
1379 from = wp->w_old_cursor_lnum;
1380 to = wp->w_old_visual_lnum;
1381 }
1382 else
1383 {
1384 from = wp->w_old_visual_lnum;
1385 to = wp->w_old_cursor_lnum;
1386 }
1387 }
1388
1389 /*
1390 * There is no need to update lines above the top of the window.
1391 */
1392 if (from < wp->w_topline)
1393 from = wp->w_topline;
1394
1395 /*
1396 * If we know the value of w_botline, use it to restrict the update to
1397 * the lines that are visible in the window.
1398 */
1399 if (wp->w_valid & VALID_BOTLINE)
1400 {
1401 if (from >= wp->w_botline)
1402 from = wp->w_botline - 1;
1403 if (to >= wp->w_botline)
1404 to = wp->w_botline - 1;
1405 }
1406
1407 /*
1408 * Find the minimal part to be updated.
1409 * Watch out for scrolling that made entries in w_lines[] invalid.
1410 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1411 * top_end; need to redraw from top_end to the "to" line.
1412 * A middle mouse click with a Visual selection may change the text
1413 * above the Visual area and reset wl_valid, do count these for
1414 * mid_end (in srow).
1415 */
1416 if (mid_start > 0)
1417 {
1418 lnum = wp->w_topline;
1419 idx = 0;
1420 srow = 0;
1421 if (scrolled_down)
1422 mid_start = top_end;
1423 else
1424 mid_start = 0;
1425 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1426 {
1427 if (wp->w_lines[idx].wl_valid)
1428 mid_start += wp->w_lines[idx].wl_size;
1429 else if (!scrolled_down)
1430 srow += wp->w_lines[idx].wl_size;
1431 ++idx;
1432# ifdef FEAT_FOLDING
1433 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1434 lnum = wp->w_lines[idx].wl_lnum;
1435 else
1436# endif
1437 ++lnum;
1438 }
1439 srow += mid_start;
1440 mid_end = wp->w_height;
1441 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1442 {
1443 if (wp->w_lines[idx].wl_valid
1444 && wp->w_lines[idx].wl_lnum >= to + 1)
1445 {
1446 /* Only update until first row of this line */
1447 mid_end = srow;
1448 break;
1449 }
1450 srow += wp->w_lines[idx].wl_size;
1451 }
1452 }
1453 }
1454
1455 if (VIsual_active && buf == curwin->w_buffer)
1456 {
1457 wp->w_old_visual_mode = VIsual_mode;
1458 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1459 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001460 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 wp->w_old_curswant = curwin->w_curswant;
1462 }
1463 else
1464 {
1465 wp->w_old_visual_mode = 0;
1466 wp->w_old_cursor_lnum = 0;
1467 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001468 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470#endif /* FEAT_VISUAL */
1471
1472#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1473 /* reset got_int, otherwise regexp won't work */
1474 save_got_int = got_int;
1475 got_int = 0;
1476#endif
1477#ifdef FEAT_FOLDING
1478 win_foldinfo.fi_level = 0;
1479#endif
1480
1481 /*
1482 * Update all the window rows.
1483 */
1484 idx = 0; /* first entry in w_lines[].wl_size */
1485 row = 0;
1486 srow = 0;
1487 lnum = wp->w_topline; /* first line shown in window */
1488 for (;;)
1489 {
1490 /* stop updating when reached the end of the window (check for _past_
1491 * the end of the window is at the end of the loop) */
1492 if (row == wp->w_height)
1493 {
1494 didline = TRUE;
1495 break;
1496 }
1497
1498 /* stop updating when hit the end of the file */
1499 if (lnum > buf->b_ml.ml_line_count)
1500 {
1501 eof = TRUE;
1502 break;
1503 }
1504
1505 /* Remember the starting row of the line that is going to be dealt
1506 * with. It is used further down when the line doesn't fit. */
1507 srow = row;
1508
1509 /*
1510 * Update a line when it is in an area that needs updating, when it
1511 * has changes or w_lines[idx] is invalid.
1512 * bot_start may be halfway a wrapped line after using
1513 * win_del_lines(), check if the current line includes it.
1514 * When syntax folding is being used, the saved syntax states will
1515 * already have been updated, we can't see where the syntax state is
1516 * the same again, just update until the end of the window.
1517 */
1518 if (row < top_end
1519 || (row >= mid_start && row < mid_end)
1520#ifdef FEAT_SEARCH_EXTRA
1521 || top_to_mod
1522#endif
1523 || idx >= wp->w_lines_valid
1524 || (row + wp->w_lines[idx].wl_size > bot_start)
1525 || (mod_top != 0
1526 && (lnum == mod_top
1527 || (lnum >= mod_top
1528 && (lnum < mod_bot
1529#ifdef FEAT_SYN_HL
1530 || did_update == DID_FOLD
1531 || (did_update == DID_LINE
1532 && syntax_present(buf)
1533 && (
1534# ifdef FEAT_FOLDING
1535 (foldmethodIsSyntax(wp)
1536 && hasAnyFolding(wp)) ||
1537# endif
1538 syntax_check_changed(lnum)))
1539#endif
1540 )))))
1541 {
1542#ifdef FEAT_SEARCH_EXTRA
1543 if (lnum == mod_top)
1544 top_to_mod = FALSE;
1545#endif
1546
1547 /*
1548 * When at start of changed lines: May scroll following lines
1549 * up or down to minimize redrawing.
1550 * Don't do this when the change continues until the end.
1551 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1552 */
1553 if (lnum == mod_top
1554 && mod_bot != MAXLNUM
1555 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1556 {
1557 int old_rows = 0;
1558 int new_rows = 0;
1559 int xtra_rows;
1560 linenr_T l;
1561
1562 /* Count the old number of window rows, using w_lines[], which
1563 * should still contain the sizes for the lines as they are
1564 * currently displayed. */
1565 for (i = idx; i < wp->w_lines_valid; ++i)
1566 {
1567 /* Only valid lines have a meaningful wl_lnum. Invalid
1568 * lines are part of the changed area. */
1569 if (wp->w_lines[i].wl_valid
1570 && wp->w_lines[i].wl_lnum == mod_bot)
1571 break;
1572 old_rows += wp->w_lines[i].wl_size;
1573#ifdef FEAT_FOLDING
1574 if (wp->w_lines[i].wl_valid
1575 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1576 {
1577 /* Must have found the last valid entry above mod_bot.
1578 * Add following invalid entries. */
1579 ++i;
1580 while (i < wp->w_lines_valid
1581 && !wp->w_lines[i].wl_valid)
1582 old_rows += wp->w_lines[i++].wl_size;
1583 break;
1584 }
1585#endif
1586 }
1587
1588 if (i >= wp->w_lines_valid)
1589 {
1590 /* We can't find a valid line below the changed lines,
1591 * need to redraw until the end of the window.
1592 * Inserting/deleting lines has no use. */
1593 bot_start = 0;
1594 }
1595 else
1596 {
1597 /* Able to count old number of rows: Count new window
1598 * rows, and may insert/delete lines */
1599 j = idx;
1600 for (l = lnum; l < mod_bot; ++l)
1601 {
1602#ifdef FEAT_FOLDING
1603 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1604 ++new_rows;
1605 else
1606#endif
1607#ifdef FEAT_DIFF
1608 if (l == wp->w_topline)
1609 new_rows += plines_win_nofill(wp, l, TRUE)
1610 + wp->w_topfill;
1611 else
1612#endif
1613 new_rows += plines_win(wp, l, TRUE);
1614 ++j;
1615 if (new_rows > wp->w_height - row - 2)
1616 {
1617 /* it's getting too much, must redraw the rest */
1618 new_rows = 9999;
1619 break;
1620 }
1621 }
1622 xtra_rows = new_rows - old_rows;
1623 if (xtra_rows < 0)
1624 {
1625 /* May scroll text up. If there is not enough
1626 * remaining text or scrolling fails, must redraw the
1627 * rest. If scrolling works, must redraw the text
1628 * below the scrolled text. */
1629 if (row - xtra_rows >= wp->w_height - 2)
1630 mod_bot = MAXLNUM;
1631 else
1632 {
1633 check_for_delay(FALSE);
1634 if (win_del_lines(wp, row,
1635 -xtra_rows, FALSE, FALSE) == FAIL)
1636 mod_bot = MAXLNUM;
1637 else
1638 bot_start = wp->w_height + xtra_rows;
1639 }
1640 }
1641 else if (xtra_rows > 0)
1642 {
1643 /* May scroll text down. If there is not enough
1644 * remaining text of scrolling fails, must redraw the
1645 * rest. */
1646 if (row + xtra_rows >= wp->w_height - 2)
1647 mod_bot = MAXLNUM;
1648 else
1649 {
1650 check_for_delay(FALSE);
1651 if (win_ins_lines(wp, row + old_rows,
1652 xtra_rows, FALSE, FALSE) == FAIL)
1653 mod_bot = MAXLNUM;
1654 else if (top_end > row + old_rows)
1655 /* Scrolled the part at the top that requires
1656 * updating down. */
1657 top_end += xtra_rows;
1658 }
1659 }
1660
1661 /* When not updating the rest, may need to move w_lines[]
1662 * entries. */
1663 if (mod_bot != MAXLNUM && i != j)
1664 {
1665 if (j < i)
1666 {
1667 int x = row + new_rows;
1668
1669 /* move entries in w_lines[] upwards */
1670 for (;;)
1671 {
1672 /* stop at last valid entry in w_lines[] */
1673 if (i >= wp->w_lines_valid)
1674 {
1675 wp->w_lines_valid = j;
1676 break;
1677 }
1678 wp->w_lines[j] = wp->w_lines[i];
1679 /* stop at a line that won't fit */
1680 if (x + (int)wp->w_lines[j].wl_size
1681 > wp->w_height)
1682 {
1683 wp->w_lines_valid = j + 1;
1684 break;
1685 }
1686 x += wp->w_lines[j++].wl_size;
1687 ++i;
1688 }
1689 if (bot_start > x)
1690 bot_start = x;
1691 }
1692 else /* j > i */
1693 {
1694 /* move entries in w_lines[] downwards */
1695 j -= i;
1696 wp->w_lines_valid += j;
1697 if (wp->w_lines_valid > wp->w_height)
1698 wp->w_lines_valid = wp->w_height;
1699 for (i = wp->w_lines_valid; i - j >= idx; --i)
1700 wp->w_lines[i] = wp->w_lines[i - j];
1701
1702 /* The w_lines[] entries for inserted lines are
1703 * now invalid, but wl_size may be used above.
1704 * Reset to zero. */
1705 while (i >= idx)
1706 {
1707 wp->w_lines[i].wl_size = 0;
1708 wp->w_lines[i--].wl_valid = FALSE;
1709 }
1710 }
1711 }
1712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714
1715#ifdef FEAT_FOLDING
1716 /*
1717 * When lines are folded, display one line for all of them.
1718 * Otherwise, display normally (can be several display lines when
1719 * 'wrap' is on).
1720 */
1721 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1722 if (fold_count != 0)
1723 {
1724 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1725 ++row;
1726 --fold_count;
1727 wp->w_lines[idx].wl_folded = TRUE;
1728 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1729# ifdef FEAT_SYN_HL
1730 did_update = DID_FOLD;
1731# endif
1732 }
1733 else
1734#endif
1735 if (idx < wp->w_lines_valid
1736 && wp->w_lines[idx].wl_valid
1737 && wp->w_lines[idx].wl_lnum == lnum
1738 && lnum > wp->w_topline
1739 && !(dy_flags & DY_LASTLINE)
1740 && srow + wp->w_lines[idx].wl_size > wp->w_height
1741#ifdef FEAT_DIFF
1742 && diff_check_fill(wp, lnum) == 0
1743#endif
1744 )
1745 {
1746 /* This line is not going to fit. Don't draw anything here,
1747 * will draw "@ " lines below. */
1748 row = wp->w_height + 1;
1749 }
1750 else
1751 {
1752#ifdef FEAT_SEARCH_EXTRA
1753 prepare_search_hl(wp, lnum);
1754#endif
1755#ifdef FEAT_SYN_HL
1756 /* Let the syntax stuff know we skipped a few lines. */
1757 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1758 && syntax_present(buf))
1759 syntax_end_parsing(syntax_last_parsed + 1);
1760#endif
1761
1762 /*
1763 * Display one line.
1764 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001765 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
1767#ifdef FEAT_FOLDING
1768 wp->w_lines[idx].wl_folded = FALSE;
1769 wp->w_lines[idx].wl_lastlnum = lnum;
1770#endif
1771#ifdef FEAT_SYN_HL
1772 did_update = DID_LINE;
1773 syntax_last_parsed = lnum;
1774#endif
1775 }
1776
1777 wp->w_lines[idx].wl_lnum = lnum;
1778 wp->w_lines[idx].wl_valid = TRUE;
1779 if (row > wp->w_height) /* past end of screen */
1780 {
1781 /* we may need the size of that too long line later on */
1782 if (dollar_vcol == 0)
1783 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1784 ++idx;
1785 break;
1786 }
1787 if (dollar_vcol == 0)
1788 wp->w_lines[idx].wl_size = row - srow;
1789 ++idx;
1790#ifdef FEAT_FOLDING
1791 lnum += fold_count + 1;
1792#else
1793 ++lnum;
1794#endif
1795 }
1796 else
1797 {
1798 /* This line does not need updating, advance to the next one */
1799 row += wp->w_lines[idx++].wl_size;
1800 if (row > wp->w_height) /* past end of screen */
1801 break;
1802#ifdef FEAT_FOLDING
1803 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1804#else
1805 ++lnum;
1806#endif
1807#ifdef FEAT_SYN_HL
1808 did_update = DID_NONE;
1809#endif
1810 }
1811
1812 if (lnum > buf->b_ml.ml_line_count)
1813 {
1814 eof = TRUE;
1815 break;
1816 }
1817 }
1818 /*
1819 * End of loop over all window lines.
1820 */
1821
1822
1823 if (idx > wp->w_lines_valid)
1824 wp->w_lines_valid = idx;
1825
1826#ifdef FEAT_SYN_HL
1827 /*
1828 * Let the syntax stuff know we stop parsing here.
1829 */
1830 if (syntax_last_parsed != 0 && syntax_present(buf))
1831 syntax_end_parsing(syntax_last_parsed + 1);
1832#endif
1833
1834 /*
1835 * If we didn't hit the end of the file, and we didn't finish the last
1836 * line we were working on, then the line didn't fit.
1837 */
1838 wp->w_empty_rows = 0;
1839#ifdef FEAT_DIFF
1840 wp->w_filler_rows = 0;
1841#endif
1842 if (!eof && !didline)
1843 {
1844 if (lnum == wp->w_topline)
1845 {
1846 /*
1847 * Single line that does not fit!
1848 * Don't overwrite it, it can be edited.
1849 */
1850 wp->w_botline = lnum + 1;
1851 }
1852#ifdef FEAT_DIFF
1853 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1854 {
1855 /* Window ends in filler lines. */
1856 wp->w_botline = lnum;
1857 wp->w_filler_rows = wp->w_height - srow;
1858 }
1859#endif
1860 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1861 {
1862 /*
1863 * Last line isn't finished: Display "@@@" at the end.
1864 */
1865 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1866 W_WINROW(wp) + wp->w_height,
1867 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1868 '@', '@', hl_attr(HLF_AT));
1869 set_empty_rows(wp, srow);
1870 wp->w_botline = lnum;
1871 }
1872 else
1873 {
1874 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1875 wp->w_botline = lnum;
1876 }
1877 }
1878 else
1879 {
1880#ifdef FEAT_VERTSPLIT
1881 draw_vsep_win(wp, row);
1882#endif
1883 if (eof) /* we hit the end of the file */
1884 {
1885 wp->w_botline = buf->b_ml.ml_line_count + 1;
1886#ifdef FEAT_DIFF
1887 j = diff_check_fill(wp, wp->w_botline);
1888 if (j > 0 && !wp->w_botfill)
1889 {
1890 /*
1891 * Display filler lines at the end of the file
1892 */
1893 if (char2cells(fill_diff) > 1)
1894 i = '-';
1895 else
1896 i = fill_diff;
1897 if (row + j > wp->w_height)
1898 j = wp->w_height - row;
1899 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1900 row += j;
1901 }
1902#endif
1903 }
1904 else if (dollar_vcol == 0)
1905 wp->w_botline = lnum;
1906
1907 /* make sure the rest of the screen is blank */
1908 /* put '~'s on rows that aren't part of the file. */
1909 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1910 }
1911
1912 /* Reset the type of redrawing required, the window has been updated. */
1913 wp->w_redr_type = 0;
1914#ifdef FEAT_DIFF
1915 wp->w_old_topfill = wp->w_topfill;
1916 wp->w_old_botfill = wp->w_botfill;
1917#endif
1918
1919 if (dollar_vcol == 0)
1920 {
1921 /*
1922 * There is a trick with w_botline. If we invalidate it on each
1923 * change that might modify it, this will cause a lot of expensive
1924 * calls to plines() in update_topline() each time. Therefore the
1925 * value of w_botline is often approximated, and this value is used to
1926 * compute the value of w_topline. If the value of w_botline was
1927 * wrong, check that the value of w_topline is correct (cursor is on
1928 * the visible part of the text). If it's not, we need to redraw
1929 * again. Mostly this just means scrolling up a few lines, so it
1930 * doesn't look too bad. Only do this for the current window (where
1931 * changes are relevant).
1932 */
1933 wp->w_valid |= VALID_BOTLINE;
1934 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1935 {
1936 recursive = TRUE;
1937 curwin->w_valid &= ~VALID_TOPLINE;
1938 update_topline(); /* may invalidate w_botline again */
1939 if (must_redraw != 0)
1940 {
1941 /* Don't update for changes in buffer again. */
1942 i = curbuf->b_mod_set;
1943 curbuf->b_mod_set = FALSE;
1944 win_update(curwin);
1945 must_redraw = 0;
1946 curbuf->b_mod_set = i;
1947 }
1948 recursive = FALSE;
1949 }
1950 }
1951
1952#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1953 /* restore got_int, unless CTRL-C was hit while redrawing */
1954 if (!got_int)
1955 got_int = save_got_int;
1956#endif
1957}
1958
1959#ifdef FEAT_SIGNS
1960static int draw_signcolumn __ARGS((win_T *wp));
1961
1962/*
1963 * Return TRUE when window "wp" has a column to draw signs in.
1964 */
1965 static int
1966draw_signcolumn(wp)
1967 win_T *wp;
1968{
1969 return (wp->w_buffer->b_signlist != NULL
1970# ifdef FEAT_NETBEANS_INTG
1971 || usingNetbeans
1972# endif
1973 );
1974}
1975#endif
1976
1977/*
1978 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1979 * as the filler character.
1980 */
1981 static void
1982win_draw_end(wp, c1, c2, row, endrow, hl)
1983 win_T *wp;
1984 int c1;
1985 int c2;
1986 int row;
1987 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001988 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1991 int n = 0;
1992# define FDC_OFF n
1993#else
1994# define FDC_OFF 0
1995#endif
1996
1997#ifdef FEAT_RIGHTLEFT
1998 if (wp->w_p_rl)
1999 {
2000 /* No check for cmdline window: should never be right-left. */
2001# ifdef FEAT_FOLDING
2002 n = wp->w_p_fdc;
2003
2004 if (n > 0)
2005 {
2006 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002007 if (n > W_WIDTH(wp))
2008 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2010 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2011 ' ', ' ', hl_attr(HLF_FC));
2012 }
2013# endif
2014# ifdef FEAT_SIGNS
2015 if (draw_signcolumn(wp))
2016 {
2017 int nn = n + 2;
2018
2019 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002020 if (nn > W_WIDTH(wp))
2021 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2023 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2024 ' ', ' ', hl_attr(HLF_SC));
2025 n = nn;
2026 }
2027# endif
2028 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2029 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2030 c2, c2, hl_attr(hl));
2031 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2032 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2033 c1, c2, hl_attr(hl));
2034 }
2035 else
2036#endif
2037 {
2038#ifdef FEAT_CMDWIN
2039 if (cmdwin_type != 0 && wp == curwin)
2040 {
2041 /* draw the cmdline character in the leftmost column */
2042 n = 1;
2043 if (n > wp->w_width)
2044 n = wp->w_width;
2045 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2046 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2047 cmdwin_type, ' ', hl_attr(HLF_AT));
2048 }
2049#endif
2050#ifdef FEAT_FOLDING
2051 if (wp->w_p_fdc > 0)
2052 {
2053 int nn = n + wp->w_p_fdc;
2054
2055 /* draw the fold column at the left */
2056 if (nn > W_WIDTH(wp))
2057 nn = W_WIDTH(wp);
2058 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2059 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2060 ' ', ' ', hl_attr(HLF_FC));
2061 n = nn;
2062 }
2063#endif
2064#ifdef FEAT_SIGNS
2065 if (draw_signcolumn(wp))
2066 {
2067 int nn = n + 2;
2068
2069 /* draw the sign column after the fold column */
2070 if (nn > W_WIDTH(wp))
2071 nn = W_WIDTH(wp);
2072 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2073 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2074 ' ', ' ', hl_attr(HLF_SC));
2075 n = nn;
2076 }
2077#endif
2078 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2079 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2080 c1, c2, hl_attr(hl));
2081 }
2082 set_empty_rows(wp, row);
2083}
2084
2085#ifdef FEAT_FOLDING
2086/*
2087 * Display one folded line.
2088 */
2089 static void
2090fold_line(wp, fold_count, foldinfo, lnum, row)
2091 win_T *wp;
2092 long fold_count;
2093 foldinfo_T *foldinfo;
2094 linenr_T lnum;
2095 int row;
2096{
2097 char_u buf[51];
2098 pos_T *top, *bot;
2099 linenr_T lnume = lnum + fold_count - 1;
2100 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002101 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 int col;
2104 int txtcol;
2105 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002106 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108 /* Build the fold line:
2109 * 1. Add the cmdwin_type for the command-line window
2110 * 2. Add the 'foldcolumn'
2111 * 3. Add the 'number' column
2112 * 4. Compose the text
2113 * 5. Add the text
2114 * 6. set highlighting for the Visual area an other text
2115 */
2116 col = 0;
2117
2118 /*
2119 * 1. Add the cmdwin_type for the command-line window
2120 * Ignores 'rightleft', this window is never right-left.
2121 */
2122#ifdef FEAT_CMDWIN
2123 if (cmdwin_type != 0 && wp == curwin)
2124 {
2125 ScreenLines[off] = cmdwin_type;
2126 ScreenAttrs[off] = hl_attr(HLF_AT);
2127#ifdef FEAT_MBYTE
2128 if (enc_utf8)
2129 ScreenLinesUC[off] = 0;
2130#endif
2131 ++col;
2132 }
2133#endif
2134
2135 /*
2136 * 2. Add the 'foldcolumn'
2137 */
2138 fdc = wp->w_p_fdc;
2139 if (fdc > W_WIDTH(wp) - col)
2140 fdc = W_WIDTH(wp) - col;
2141 if (fdc > 0)
2142 {
2143 fill_foldcolumn(buf, wp, TRUE, lnum);
2144#ifdef FEAT_RIGHTLEFT
2145 if (wp->w_p_rl)
2146 {
2147 int i;
2148
2149 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2150 hl_attr(HLF_FC));
2151 /* reverse the fold column */
2152 for (i = 0; i < fdc; ++i)
2153 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2154 }
2155 else
2156#endif
2157 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2158 col += fdc;
2159 }
2160
2161#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002162# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2163 for (ri = 0; ri < l; ++ri) \
2164 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2165 else \
2166 for (ri = 0; ri < l; ++ri) \
2167 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002169# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2170 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#endif
2172
2173 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002174 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
2176#ifdef FEAT_SIGNS
2177 /* If signs are being displayed, add two spaces. */
2178 if (draw_signcolumn(wp))
2179 {
2180 len = W_WIDTH(wp) - col;
2181 if (len > 0)
2182 {
2183 if (len > 2)
2184 len = 2;
2185# ifdef FEAT_RIGHTLEFT
2186 if (wp->w_p_rl)
2187 /* the line number isn't reversed */
2188 copy_text_attr(off + W_WIDTH(wp) - len - col,
2189 (char_u *)" ", len, hl_attr(HLF_FL));
2190 else
2191# endif
2192 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2193 col += len;
2194 }
2195 }
2196#endif
2197
2198 /*
2199 * 3. Add the 'number' column
2200 */
2201 if (wp->w_p_nu)
2202 {
2203 len = W_WIDTH(wp) - col;
2204 if (len > 0)
2205 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002206 int w = number_width(wp);
2207
2208 if (len > w + 1)
2209 len = w + 1;
2210 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef FEAT_RIGHTLEFT
2212 if (wp->w_p_rl)
2213 /* the line number isn't reversed */
2214 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2215 hl_attr(HLF_FL));
2216 else
2217#endif
2218 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2219 col += len;
2220 }
2221 }
2222
2223 /*
2224 * 4. Compose the folded-line string with 'foldtext', if set.
2225 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002226 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228 txtcol = col; /* remember where text starts */
2229
2230 /*
2231 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2232 * Right-left text is put in columns 0 - number-col, normal text is put
2233 * in columns number-col - window-width.
2234 */
2235#ifdef FEAT_MBYTE
2236 if (has_mbyte)
2237 {
2238 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002239 int u8c, u8cc[MAX_MCO];
2240 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 int idx;
2242 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002243 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244# ifdef FEAT_ARABIC
2245 int prev_c = 0; /* previous Arabic character */
2246 int prev_c1 = 0; /* first composing char for prev_c */
2247# endif
2248
2249# ifdef FEAT_RIGHTLEFT
2250 if (wp->w_p_rl)
2251 idx = off;
2252 else
2253# endif
2254 idx = off + col;
2255
2256 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2257 for (p = text; *p != NUL; )
2258 {
2259 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002260 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (col + cells > W_WIDTH(wp)
2262# ifdef FEAT_RIGHTLEFT
2263 - (wp->w_p_rl ? col : 0)
2264# endif
2265 )
2266 break;
2267 ScreenLines[idx] = *p;
2268 if (enc_utf8)
2269 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002270 u8c = utfc_ptr2char(p, u8cc);
2271 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 ScreenLinesUC[idx] = 0;
2274#ifdef FEAT_ARABIC
2275 prev_c = u8c;
2276#endif
2277 }
2278 else
2279 {
2280#ifdef FEAT_ARABIC
2281 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2282 {
2283 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002284 int pc, pc1, nc;
2285 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 int firstbyte = *p;
2287
2288 /* The idea of what is the previous and next
2289 * character depends on 'rightleft'. */
2290 if (wp->w_p_rl)
2291 {
2292 pc = prev_c;
2293 pc1 = prev_c1;
2294 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002295 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 else
2298 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002299 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002301 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 prev_c = u8c;
2304
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002305 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 pc, pc1, nc);
2307 ScreenLines[idx] = firstbyte;
2308 }
2309 else
2310 prev_c = u8c;
2311#endif
2312 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002313#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (u8c >= 0x10000)
2315 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2316 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002319 for (i = 0; i < Screen_mco; ++i)
2320 {
2321 ScreenLinesC[i][idx] = u8cc[i];
2322 if (u8cc[i] == 0)
2323 break;
2324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326 if (cells > 1)
2327 ScreenLines[idx + 1] = 0;
2328 }
2329 else if (cells > 1) /* double-byte character */
2330 {
2331 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2332 ScreenLines2[idx] = p[1];
2333 else
2334 ScreenLines[idx + 1] = p[1];
2335 }
2336 col += cells;
2337 idx += cells;
2338 p += c_len;
2339 }
2340 }
2341 else
2342#endif
2343 {
2344 len = (int)STRLEN(text);
2345 if (len > W_WIDTH(wp) - col)
2346 len = W_WIDTH(wp) - col;
2347 if (len > 0)
2348 {
2349#ifdef FEAT_RIGHTLEFT
2350 if (wp->w_p_rl)
2351 STRNCPY(current_ScreenLine, text, len);
2352 else
2353#endif
2354 STRNCPY(current_ScreenLine + col, text, len);
2355 col += len;
2356 }
2357 }
2358
2359 /* Fill the rest of the line with the fold filler */
2360#ifdef FEAT_RIGHTLEFT
2361 if (wp->w_p_rl)
2362 col -= txtcol;
2363#endif
2364 while (col < W_WIDTH(wp)
2365#ifdef FEAT_RIGHTLEFT
2366 - (wp->w_p_rl ? txtcol : 0)
2367#endif
2368 )
2369 {
2370#ifdef FEAT_MBYTE
2371 if (enc_utf8)
2372 {
2373 if (fill_fold >= 0x80)
2374 {
2375 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002376 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378 else
2379 ScreenLinesUC[off + col] = 0;
2380 }
2381#endif
2382 ScreenLines[off + col++] = fill_fold;
2383 }
2384
2385 if (text != buf)
2386 vim_free(text);
2387
2388 /*
2389 * 6. set highlighting for the Visual area an other text.
2390 * If all folded lines are in the Visual area, highlight the line.
2391 */
2392#ifdef FEAT_VISUAL
2393 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2394 {
2395 if (ltoreq(curwin->w_cursor, VIsual))
2396 {
2397 /* Visual is after curwin->w_cursor */
2398 top = &curwin->w_cursor;
2399 bot = &VIsual;
2400 }
2401 else
2402 {
2403 /* Visual is before curwin->w_cursor */
2404 top = &VIsual;
2405 bot = &curwin->w_cursor;
2406 }
2407 if (lnum >= top->lnum
2408 && lnume <= bot->lnum
2409 && (VIsual_mode != 'v'
2410 || ((lnum > top->lnum
2411 || (lnum == top->lnum
2412 && top->col == 0))
2413 && (lnume < bot->lnum
2414 || (lnume == bot->lnum
2415 && (bot->col - (*p_sel == 'e'))
2416 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2417 {
2418 if (VIsual_mode == Ctrl_V)
2419 {
2420 /* Visual block mode: highlight the chars part of the block */
2421 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2422 {
2423 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2424 len = wp->w_old_cursor_lcol;
2425 else
2426 len = W_WIDTH(wp) - txtcol;
2427 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002428 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430 }
2431 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002434 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 }
2438#endif
2439
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002440#ifdef FEAT_SYN_HL
2441 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002442 if (wp->w_p_cuc)
2443 {
2444 txtcol += wp->w_virtcol;
2445 if (wp->w_p_wrap)
2446 txtcol -= wp->w_skipcol;
2447 else
2448 txtcol -= wp->w_leftcol;
2449 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2450 ScreenAttrs[off + txtcol] = hl_combine_attr(
2451 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2452 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2456 (int)W_WIDTH(wp), FALSE);
2457
2458 /*
2459 * Update w_cline_height and w_cline_folded if the cursor line was
2460 * updated (saves a call to plines() later).
2461 */
2462 if (wp == curwin
2463 && lnum <= curwin->w_cursor.lnum
2464 && lnume >= curwin->w_cursor.lnum)
2465 {
2466 curwin->w_cline_row = row;
2467 curwin->w_cline_height = 1;
2468 curwin->w_cline_folded = TRUE;
2469 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2470 }
2471}
2472
2473/*
2474 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2475 */
2476 static void
2477copy_text_attr(off, buf, len, attr)
2478 int off;
2479 char_u *buf;
2480 int len;
2481 int attr;
2482{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002483 int i;
2484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 mch_memmove(ScreenLines + off, buf, (size_t)len);
2486# ifdef FEAT_MBYTE
2487 if (enc_utf8)
2488 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2489# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002490 for (i = 0; i < len; ++i)
2491 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492}
2493
2494/*
2495 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002496 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 */
2498 static void
2499fill_foldcolumn(p, wp, closed, lnum)
2500 char_u *p;
2501 win_T *wp;
2502 int closed; /* TRUE of FALSE */
2503 linenr_T lnum; /* current line number */
2504{
2505 int i = 0;
2506 int level;
2507 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002508 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 /* Init to all spaces. */
2511 copy_spaces(p, (size_t)wp->w_p_fdc);
2512
2513 level = win_foldinfo.fi_level;
2514 if (level > 0)
2515 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002516 /* If there is only one column put more info in it. */
2517 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2518
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 /* If the column is too narrow, we start at the lowest level that
2520 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002521 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (first_level < 1)
2523 first_level = 1;
2524
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002525 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 if (win_foldinfo.fi_lnum == lnum
2528 && first_level + i >= win_foldinfo.fi_low_level)
2529 p[i] = '-';
2530 else if (first_level == 1)
2531 p[i] = '|';
2532 else if (first_level + i <= 9)
2533 p[i] = '0' + first_level + i;
2534 else
2535 p[i] = '>';
2536 if (first_level + i == level)
2537 break;
2538 }
2539 }
2540 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002541 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542}
2543#endif /* FEAT_FOLDING */
2544
2545/*
2546 * Display line "lnum" of window 'wp' on the screen.
2547 * Start at row "startrow", stop when "endrow" is reached.
2548 * wp->w_virtcol needs to be valid.
2549 *
2550 * Return the number of last row the line occupies.
2551 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002552/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002554win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 win_T *wp;
2556 linenr_T lnum;
2557 int startrow;
2558 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002559 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 int col; /* visual column on screen */
2562 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2563 int c = 0; /* init for GCC */
2564 long vcol = 0; /* virtual column (for tabs) */
2565 long vcol_prev = -1; /* "vcol" of previous character */
2566 char_u *line; /* current line */
2567 char_u *ptr; /* current position in "line" */
2568 int row; /* row in the window, excl w_winrow */
2569 int screen_row; /* row on the screen, incl w_winrow */
2570
2571 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2572 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002573 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 int c_extra = NUL; /* extra chars, all the same */
2575 int extra_attr = 0; /* attributes when n_extra != 0 */
2576 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2577 displaying lcs_eol at end-of-line */
2578 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2579 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2580
2581 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2582 int saved_n_extra = 0;
2583 char_u *saved_p_extra = NULL;
2584 int saved_c_extra = 0;
2585 int saved_char_attr = 0;
2586
2587 int n_attr = 0; /* chars with special attr */
2588 int saved_attr2 = 0; /* char_attr saved for n_attr */
2589 int n_attr3 = 0; /* chars with overruling special attr */
2590 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2591
2592 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2593
2594 int fromcol, tocol; /* start/end of inverting */
2595 int fromcol_prev = -2; /* start of inverting after cursor */
2596 int noinvcur = FALSE; /* don't invert the cursor */
2597#ifdef FEAT_VISUAL
2598 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002599 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600#endif
2601 pos_T pos;
2602 long v;
2603
2604 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002605 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2607 in this line */
2608 int attr = 0; /* attributes for area highlighting */
2609 int area_attr = 0; /* attributes desired by highlighting */
2610 int search_attr = 0; /* attributes desired by 'hlsearch' */
2611#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002612 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 int syntax_attr = 0; /* attributes desired by syntax */
2614 int has_syntax = FALSE; /* this buffer has syntax highl. */
2615 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002616 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002617#endif
2618#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002619 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002620# define SPWORDLEN 150
2621 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002622 int nextlinecol = 0; /* column where nextline[] starts */
2623 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002624 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002625 int spell_attr = 0; /* attributes desired by spelling */
2626 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002627 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2628 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002629 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002630 static int cap_col = -1; /* column to check for Cap word */
2631 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002632 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#endif
2634 int extra_check; /* has syntax or linebreak */
2635#ifdef FEAT_MBYTE
2636 int multi_attr = 0; /* attributes desired by multibyte */
2637 int mb_l = 1; /* multi-byte byte length */
2638 int mb_c = 0; /* decoded multi-byte character */
2639 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002640 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641#endif
2642#ifdef FEAT_DIFF
2643 int filler_lines; /* nr of filler lines to be drawn */
2644 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002645 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 int change_start = MAXCOL; /* first col of changed area */
2647 int change_end = -1; /* last col of changed area */
2648#endif
2649 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2650#ifdef FEAT_LINEBREAK
2651 int need_showbreak = FALSE;
2652#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002653#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2654 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002656 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002659 matchitem_T *cur; /* points to the match list */
2660 match_T *shl; /* points to search_hl or a match */
2661 int shl_flag; /* flag to indicate whether search_hl
2662 has been processed or not */
2663 int prevcol_hl_flag; /* flag to indicate whether prevcol
2664 equals startcol of search_hl or one
2665 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666#endif
2667#ifdef FEAT_ARABIC
2668 int prev_c = 0; /* previous Arabic character */
2669 int prev_c1 = 0; /* first composing char for prev_c */
2670#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002671#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002672 int did_line_attr = 0;
2673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 /* draw_state: items that are drawn in sequence: */
2676#define WL_START 0 /* nothing done yet */
2677#ifdef FEAT_CMDWIN
2678# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2679#else
2680# define WL_CMDLINE WL_START
2681#endif
2682#ifdef FEAT_FOLDING
2683# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2684#else
2685# define WL_FOLD WL_CMDLINE
2686#endif
2687#ifdef FEAT_SIGNS
2688# define WL_SIGN WL_FOLD + 1 /* column for signs */
2689#else
2690# define WL_SIGN WL_FOLD /* column for signs */
2691#endif
2692#define WL_NR WL_SIGN + 1 /* line number */
2693#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2694# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2695#else
2696# define WL_SBR WL_NR
2697#endif
2698#define WL_LINE WL_SBR + 1 /* text in the line */
2699 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002700#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 int feedback_col = 0;
2702 int feedback_old_attr = -1;
2703#endif
2704
2705
2706 if (startrow > endrow) /* past the end already! */
2707 return startrow;
2708
2709 row = startrow;
2710 screen_row = row + W_WINROW(wp);
2711
2712 /*
2713 * To speed up the loop below, set extra_check when there is linebreak,
2714 * trailing white space and/or syntax processing to be done.
2715 */
2716#ifdef FEAT_LINEBREAK
2717 extra_check = wp->w_p_lbr;
2718#else
2719 extra_check = 0;
2720#endif
2721#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002722 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
2724 /* Prepare for syntax highlighting in this line. When there is an
2725 * error, stop syntax highlighting. */
2726 save_did_emsg = did_emsg;
2727 did_emsg = FALSE;
2728 syntax_start(wp, lnum);
2729 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002730 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 else
2732 {
2733 did_emsg = save_did_emsg;
2734 has_syntax = TRUE;
2735 extra_check = TRUE;
2736 }
2737 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002738#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002739
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002740#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002741 if (wp->w_p_spell
2742 && *wp->w_buffer->b_p_spl != NUL
2743 && wp->w_buffer->b_langp.ga_len > 0
2744 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002745 {
2746 /* Prepare for spell checking. */
2747 has_spell = TRUE;
2748 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002749
2750 /* Get the start of the next line, so that words that wrap to the next
2751 * line are found too: "et<line-break>al.".
2752 * Trick: skip a few chars for C/shell/Vim comments */
2753 nextline[SPWORDLEN] = NUL;
2754 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2755 {
2756 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2757 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2758 }
2759
2760 /* When a word wrapped from the previous line the start of the current
2761 * line is valid. */
2762 if (lnum == checked_lnum)
2763 cur_checked_col = checked_col;
2764 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002765
2766 /* When there was a sentence end in the previous line may require a
2767 * word starting with capital in this line. In line 1 always check
2768 * the first word. */
2769 if (lnum != capcol_lnum)
2770 cap_col = -1;
2771 if (lnum == 1)
2772 cap_col = 0;
2773 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775#endif
2776
2777 /*
2778 * handle visual active in this window
2779 */
2780 fromcol = -10;
2781 tocol = MAXCOL;
2782#ifdef FEAT_VISUAL
2783 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2784 {
2785 /* Visual is after curwin->w_cursor */
2786 if (ltoreq(curwin->w_cursor, VIsual))
2787 {
2788 top = &curwin->w_cursor;
2789 bot = &VIsual;
2790 }
2791 else /* Visual is before curwin->w_cursor */
2792 {
2793 top = &VIsual;
2794 bot = &curwin->w_cursor;
2795 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002796 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (VIsual_mode == Ctrl_V) /* block mode */
2798 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002799 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
2801 fromcol = wp->w_old_cursor_fcol;
2802 tocol = wp->w_old_cursor_lcol;
2803 }
2804 }
2805 else /* non-block mode */
2806 {
2807 if (lnum > top->lnum && lnum <= bot->lnum)
2808 fromcol = 0;
2809 else if (lnum == top->lnum)
2810 {
2811 if (VIsual_mode == 'V') /* linewise */
2812 fromcol = 0;
2813 else
2814 {
2815 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2816 if (gchar_pos(top) == NUL)
2817 tocol = fromcol + 1;
2818 }
2819 }
2820 if (VIsual_mode != 'V' && lnum == bot->lnum)
2821 {
2822 if (*p_sel == 'e' && bot->col == 0
2823#ifdef FEAT_VIRTUALEDIT
2824 && bot->coladd == 0
2825#endif
2826 )
2827 {
2828 fromcol = -10;
2829 tocol = MAXCOL;
2830 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002831 else if (bot->col == MAXCOL)
2832 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 else
2834 {
2835 pos = *bot;
2836 if (*p_sel == 'e')
2837 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2838 else
2839 {
2840 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2841 ++tocol;
2842 }
2843 }
2844 }
2845 }
2846
2847#ifndef MSDOS
2848 /* Check if the character under the cursor should not be inverted */
2849 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2850# ifdef FEAT_GUI
2851 && !gui.in_use
2852# endif
2853 )
2854 noinvcur = TRUE;
2855#endif
2856
2857 /* if inverting in this line set area_highlighting */
2858 if (fromcol >= 0)
2859 {
2860 area_highlighting = TRUE;
2861 attr = hl_attr(HLF_V);
2862#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2863 if (clip_star.available && !clip_star.owned && clip_isautosel())
2864 attr = hl_attr(HLF_VNC);
2865#endif
2866 }
2867 }
2868
2869 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002870 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 */
2872 else
2873#endif /* FEAT_VISUAL */
2874 if (highlight_match
2875 && wp == curwin
2876 && lnum >= curwin->w_cursor.lnum
2877 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2878 {
2879 if (lnum == curwin->w_cursor.lnum)
2880 getvcol(curwin, &(curwin->w_cursor),
2881 (colnr_T *)&fromcol, NULL, NULL);
2882 else
2883 fromcol = 0;
2884 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2885 {
2886 pos.lnum = lnum;
2887 pos.col = search_match_endcol;
2888 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2889 }
2890 else
2891 tocol = MAXCOL;
2892 if (fromcol == tocol) /* do at least one character */
2893 tocol = fromcol + 1; /* happens when past end of line */
2894 area_highlighting = TRUE;
2895 attr = hl_attr(HLF_I);
2896 }
2897
2898#ifdef FEAT_DIFF
2899 filler_lines = diff_check(wp, lnum);
2900 if (filler_lines < 0)
2901 {
2902 if (filler_lines == -1)
2903 {
2904 if (diff_find_change(wp, lnum, &change_start, &change_end))
2905 diff_hlf = HLF_ADD; /* added line */
2906 else if (change_start == 0)
2907 diff_hlf = HLF_TXD; /* changed text */
2908 else
2909 diff_hlf = HLF_CHD; /* changed line */
2910 }
2911 else
2912 diff_hlf = HLF_ADD; /* added line */
2913 filler_lines = 0;
2914 area_highlighting = TRUE;
2915 }
2916 if (lnum == wp->w_topline)
2917 filler_lines = wp->w_topfill;
2918 filler_todo = filler_lines;
2919#endif
2920
2921#ifdef LINE_ATTR
2922# ifdef FEAT_SIGNS
2923 /* If this line has a sign with line highlighting set line_attr. */
2924 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2925 if (v != 0)
2926 line_attr = sign_get_attr((int)v, TRUE);
2927# endif
2928# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2929 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002930 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 line_attr = hl_attr(HLF_L);
2932# endif
2933 if (line_attr != 0)
2934 area_highlighting = TRUE;
2935#endif
2936
2937 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2938 ptr = line;
2939
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002940#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002941 if (has_spell)
2942 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002943 /* For checking first word with a capital skip white space. */
2944 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002945 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002946
Bram Moolenaar30abd282005-06-22 22:35:10 +00002947 /* To be able to spell-check over line boundaries copy the end of the
2948 * current line into nextline[]. Above the start of the next line was
2949 * copied to nextline[SPWORDLEN]. */
2950 if (nextline[SPWORDLEN] == NUL)
2951 {
2952 /* No next line or it is empty. */
2953 nextlinecol = MAXCOL;
2954 nextline_idx = 0;
2955 }
2956 else
2957 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002958 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002959 if (v < SPWORDLEN)
2960 {
2961 /* Short line, use it completely and append the start of the
2962 * next line. */
2963 nextlinecol = 0;
2964 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00002965 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002966 nextline_idx = v + 1;
2967 }
2968 else
2969 {
2970 /* Long line, use only the last SPWORDLEN bytes. */
2971 nextlinecol = v - SPWORDLEN;
2972 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2973 nextline_idx = SPWORDLEN + 1;
2974 }
2975 }
2976 }
2977#endif
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 /* find start of trailing whitespace */
2980 if (wp->w_p_list && lcs_trail)
2981 {
2982 trailcol = (colnr_T)STRLEN(ptr);
2983 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2984 --trailcol;
2985 trailcol += (colnr_T) (ptr - line);
2986 extra_check = TRUE;
2987 }
2988
2989 /*
2990 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2991 * first character to be displayed.
2992 */
2993 if (wp->w_p_wrap)
2994 v = wp->w_skipcol;
2995 else
2996 v = wp->w_leftcol;
2997 if (v > 0)
2998 {
2999#ifdef FEAT_MBYTE
3000 char_u *prev_ptr = ptr;
3001#endif
3002 while (vcol < v && *ptr != NUL)
3003 {
3004 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3005 vcol += c;
3006#ifdef FEAT_MBYTE
3007 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003009 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
3011
3012#ifdef FEAT_VIRTUALEDIT
3013 /* When 'virtualedit' is set the end of the line may be before the
3014 * start of the displayed part. */
3015 if (vcol < v && *ptr == NUL && virtual_active())
3016 vcol = v;
3017#endif
3018
3019 /* Handle a character that's not completely on the screen: Put ptr at
3020 * that character but skip the first few screen characters. */
3021 if (vcol > v)
3022 {
3023 vcol -= c;
3024#ifdef FEAT_MBYTE
3025 ptr = prev_ptr;
3026#else
3027 --ptr;
3028#endif
3029 n_skip = v - vcol;
3030 }
3031
3032 /*
3033 * Adjust for when the inverted text is before the screen,
3034 * and when the start of the inverted text is before the screen.
3035 */
3036 if (tocol <= vcol)
3037 fromcol = 0;
3038 else if (fromcol >= 0 && fromcol < vcol)
3039 fromcol = vcol;
3040
3041#ifdef FEAT_LINEBREAK
3042 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3043 if (wp->w_p_wrap)
3044 need_showbreak = TRUE;
3045#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003046#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003047 /* When spell checking a word we need to figure out the start of the
3048 * word and if it's badly spelled or not. */
3049 if (has_spell)
3050 {
3051 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003052 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003053 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003054
3055 pos = wp->w_cursor;
3056 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003057 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003058 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003059
3060 /* spell_move_to() may call ml_get() and make "line" invalid */
3061 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3062 ptr = line + linecol;
3063
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003064 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003065 {
3066 /* no bad word found at line start, don't check until end of a
3067 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003068 spell_hlf = HLF_COUNT;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003069 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer)
3070 - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003071 }
3072 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003073 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003074 /* bad word found, use attributes until end of word */
3075 word_end = wp->w_cursor.col + len + 1;
3076
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003077 /* Turn index into actual attributes. */
3078 if (spell_hlf != HLF_COUNT)
3079 spell_attr = highlight_attr[spell_hlf];
3080 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003081 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003082
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003083# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003084 /* Need to restart syntax highlighting for this line. */
3085 if (has_syntax)
3086 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003087# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003088 }
3089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
3091
3092 /*
3093 * Correct highlighting for cursor that can't be disabled.
3094 * Avoids having to check this for each character.
3095 */
3096 if (fromcol >= 0)
3097 {
3098 if (noinvcur)
3099 {
3100 if ((colnr_T)fromcol == wp->w_virtcol)
3101 {
3102 /* highlighting starts at cursor, let it start just after the
3103 * cursor */
3104 fromcol_prev = fromcol;
3105 fromcol = -1;
3106 }
3107 else if ((colnr_T)fromcol < wp->w_virtcol)
3108 /* restart highlighting after the cursor */
3109 fromcol_prev = wp->w_virtcol;
3110 }
3111 if (fromcol >= tocol)
3112 fromcol = -1;
3113 }
3114
3115#ifdef FEAT_SEARCH_EXTRA
3116 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003117 * Handle highlighting the last used search pattern and matches.
3118 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003120 cur = wp->w_match_head;
3121 shl_flag = FALSE;
3122 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003124 if (shl_flag == FALSE)
3125 {
3126 shl = &search_hl;
3127 shl_flag = TRUE;
3128 }
3129 else
3130 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003131 shl->startcol = MAXCOL;
3132 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 shl->attr_cur = 0;
3134 if (shl->rm.regprog != NULL)
3135 {
3136 v = (long)(ptr - line);
3137 next_search_hl(wp, shl, lnum, (colnr_T)v);
3138
3139 /* Need to get the line again, a multi-line regexp may have made it
3140 * invalid. */
3141 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3142 ptr = line + v;
3143
3144 if (shl->lnum != 0 && shl->lnum <= lnum)
3145 {
3146 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003147 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003149 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3151 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003152 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003154 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003156 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 {
3158#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003159 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003160 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 else
3162#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003163 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003165 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
3167 shl->attr_cur = shl->attr;
3168 search_attr = shl->attr;
3169 }
3170 area_highlighting = TRUE;
3171 }
3172 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003173 if (shl != &search_hl && cur != NULL)
3174 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
3176#endif
3177
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003178#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003179 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3180 * active, because it's not clear what is selected then. */
3181 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003182 {
3183 line_attr = hl_attr(HLF_CUL);
3184 area_highlighting = TRUE;
3185 }
3186#endif
3187
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003188 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 col = 0;
3190#ifdef FEAT_RIGHTLEFT
3191 if (wp->w_p_rl)
3192 {
3193 /* Rightleft window: process the text in the normal direction, but put
3194 * it in current_ScreenLine[] from right to left. Start at the
3195 * rightmost column of the window. */
3196 col = W_WIDTH(wp) - 1;
3197 off += col;
3198 }
3199#endif
3200
3201 /*
3202 * Repeat for the whole displayed line.
3203 */
3204 for (;;)
3205 {
3206 /* Skip this quickly when working on the text. */
3207 if (draw_state != WL_LINE)
3208 {
3209#ifdef FEAT_CMDWIN
3210 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3211 {
3212 draw_state = WL_CMDLINE;
3213 if (cmdwin_type != 0 && wp == curwin)
3214 {
3215 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003217 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 char_attr = hl_attr(HLF_AT);
3219 }
3220 }
3221#endif
3222
3223#ifdef FEAT_FOLDING
3224 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3225 {
3226 draw_state = WL_FOLD;
3227 if (wp->w_p_fdc > 0)
3228 {
3229 /* Draw the 'foldcolumn'. */
3230 fill_foldcolumn(extra, wp, FALSE, lnum);
3231 n_extra = wp->w_p_fdc;
3232 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003233 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 c_extra = NUL;
3235 char_attr = hl_attr(HLF_FC);
3236 }
3237 }
3238#endif
3239
3240#ifdef FEAT_SIGNS
3241 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3242 {
3243 draw_state = WL_SIGN;
3244 /* Show the sign column when there are any signs in this
3245 * buffer or when using Netbeans. */
3246 if (draw_signcolumn(wp)
3247# ifdef FEAT_DIFF
3248 && filler_todo <= 0
3249# endif
3250 )
3251 {
3252 int_u text_sign;
3253# ifdef FEAT_SIGN_ICONS
3254 int_u icon_sign;
3255# endif
3256
3257 /* Draw two cells with the sign value or blank. */
3258 c_extra = ' ';
3259 char_attr = hl_attr(HLF_SC);
3260 n_extra = 2;
3261
3262 if (row == startrow)
3263 {
3264 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3265 SIGN_TEXT);
3266# ifdef FEAT_SIGN_ICONS
3267 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3268 SIGN_ICON);
3269 if (gui.in_use && icon_sign != 0)
3270 {
3271 /* Use the image in this position. */
3272 c_extra = SIGN_BYTE;
3273# ifdef FEAT_NETBEANS_INTG
3274 if (buf_signcount(wp->w_buffer, lnum) > 1)
3275 c_extra = MULTISIGN_BYTE;
3276# endif
3277 char_attr = icon_sign;
3278 }
3279 else
3280# endif
3281 if (text_sign != 0)
3282 {
3283 p_extra = sign_get_text(text_sign);
3284 if (p_extra != NULL)
3285 {
3286 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003287 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
3289 char_attr = sign_get_attr(text_sign, FALSE);
3290 }
3291 }
3292 }
3293 }
3294#endif
3295
3296 if (draw_state == WL_NR - 1 && n_extra == 0)
3297 {
3298 draw_state = WL_NR;
3299 /* Display the line number. After the first fill with blanks
3300 * when the 'n' flag isn't in 'cpo' */
3301 if (wp->w_p_nu
3302 && (row == startrow
3303#ifdef FEAT_DIFF
3304 + filler_lines
3305#endif
3306 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3307 {
3308 /* Draw the line number (empty space after wrapping). */
3309 if (row == startrow
3310#ifdef FEAT_DIFF
3311 + filler_lines
3312#endif
3313 )
3314 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003315 sprintf((char *)extra, "%*ld ",
3316 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (wp->w_skipcol > 0)
3318 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3319 *p_extra = '-';
3320#ifdef FEAT_RIGHTLEFT
3321 if (wp->w_p_rl) /* reverse line numbers */
3322 rl_mirror(extra);
3323#endif
3324 p_extra = extra;
3325 c_extra = NUL;
3326 }
3327 else
3328 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003329 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003331#ifdef FEAT_SYN_HL
3332 /* When 'cursorline' is set highlight the line number of
3333 * the current line differently. */
3334 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3335 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338 }
3339
3340#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3341 if (draw_state == WL_SBR - 1 && n_extra == 0)
3342 {
3343 draw_state = WL_SBR;
3344# ifdef FEAT_DIFF
3345 if (filler_todo > 0)
3346 {
3347 /* Draw "deleted" diff line(s). */
3348 if (char2cells(fill_diff) > 1)
3349 c_extra = '-';
3350 else
3351 c_extra = fill_diff;
3352# ifdef FEAT_RIGHTLEFT
3353 if (wp->w_p_rl)
3354 n_extra = col + 1;
3355 else
3356# endif
3357 n_extra = W_WIDTH(wp) - col;
3358 char_attr = hl_attr(HLF_DED);
3359 }
3360# endif
3361# ifdef FEAT_LINEBREAK
3362 if (*p_sbr != NUL && need_showbreak)
3363 {
3364 /* Draw 'showbreak' at the start of each broken line. */
3365 p_extra = p_sbr;
3366 c_extra = NUL;
3367 n_extra = (int)STRLEN(p_sbr);
3368 char_attr = hl_attr(HLF_AT);
3369 need_showbreak = FALSE;
3370 /* Correct end of highlighted area for 'showbreak',
3371 * required when 'linebreak' is also set. */
3372 if (tocol == vcol)
3373 tocol += n_extra;
3374 }
3375# endif
3376 }
3377#endif
3378
3379 if (draw_state == WL_LINE - 1 && n_extra == 0)
3380 {
3381 draw_state = WL_LINE;
3382 if (saved_n_extra)
3383 {
3384 /* Continue item from end of wrapped line. */
3385 n_extra = saved_n_extra;
3386 c_extra = saved_c_extra;
3387 p_extra = saved_p_extra;
3388 char_attr = saved_char_attr;
3389 }
3390 else
3391 char_attr = 0;
3392 }
3393 }
3394
3395 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003396 if (dollar_vcol != 0 && wp == curwin
3397 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398#ifdef FEAT_DIFF
3399 && filler_todo <= 0
3400#endif
3401 )
3402 {
3403 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3404 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003405 /* Pretend we have finished updating the window. Except when
3406 * 'cursorcolumn' is set. */
3407#ifdef FEAT_SYN_HL
3408 if (wp->w_p_cuc)
3409 row = wp->w_cline_row + wp->w_cline_height;
3410 else
3411#endif
3412 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 break;
3414 }
3415
3416 if (draw_state == WL_LINE && area_highlighting)
3417 {
3418 /* handle Visual or match highlighting in this line */
3419 if (vcol == fromcol
3420#ifdef FEAT_MBYTE
3421 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3422 && (*mb_ptr2cells)(ptr) > 1)
3423#endif
3424 || ((int)vcol_prev == fromcol_prev
3425 && vcol < tocol))
3426 area_attr = attr; /* start highlighting */
3427 else if (area_attr != 0
3428 && (vcol == tocol
3429 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432#ifdef FEAT_SEARCH_EXTRA
3433 if (!n_extra)
3434 {
3435 /*
3436 * Check for start/end of search pattern match.
3437 * After end, check for start/end of next match.
3438 * When another match, have to check for start again.
3439 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003440 * Do this for 'search_hl' and the match list (ordered by
3441 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003443 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003444 cur = wp->w_match_head;
3445 shl_flag = FALSE;
3446 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003448 if (shl_flag == FALSE
3449 && ((cur != NULL
3450 && cur->priority > SEARCH_HL_PRIORITY)
3451 || cur == NULL))
3452 {
3453 shl = &search_hl;
3454 shl_flag = TRUE;
3455 }
3456 else
3457 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 while (shl->rm.regprog != NULL)
3459 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003460 if (shl->startcol != MAXCOL
3461 && v >= (long)shl->startcol
3462 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 shl->attr_cur = shl->attr;
3465 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003466 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
3468 shl->attr_cur = 0;
3469
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 next_search_hl(wp, shl, lnum, (colnr_T)v);
3471
3472 /* Need to get the line again, a multi-line regexp
3473 * may have made it invalid. */
3474 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3475 ptr = line + v;
3476
3477 if (shl->lnum == lnum)
3478 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003479 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003481 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003483 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003485 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
3487 /* highlight empty match, try again after
3488 * it */
3489#ifdef FEAT_MBYTE
3490 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003491 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003492 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 else
3494#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003495 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497
3498 /* Loop to check if the match starts at the
3499 * current position */
3500 continue;
3501 }
3502 }
3503 break;
3504 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003505 if (shl != &search_hl && cur != NULL)
3506 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003508
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003509 /* Use attributes from match with highest priority among
3510 * 'search_hl' and the match list. */
3511 search_attr = search_hl.attr_cur;
3512 cur = wp->w_match_head;
3513 shl_flag = FALSE;
3514 while (cur != NULL || shl_flag == FALSE)
3515 {
3516 if (shl_flag == FALSE
3517 && ((cur != NULL
3518 && cur->priority > SEARCH_HL_PRIORITY)
3519 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003520 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003521 shl = &search_hl;
3522 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003523 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003524 else
3525 shl = &cur->hl;
3526 if (shl->attr_cur != 0)
3527 search_attr = shl->attr_cur;
3528 if (shl != &search_hl && cur != NULL)
3529 cur = cur->next;
3530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532#endif
3533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003535 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003537 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3538 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003540 if (diff_hlf == HLF_TXD && ptr - line > change_end
3541 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003543 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003546
3547 /* Decide which of the highlight attributes to use. */
3548 attr_pri = TRUE;
3549 if (area_attr != 0)
3550 char_attr = area_attr;
3551 else if (search_attr != 0)
3552 char_attr = search_attr;
3553#ifdef LINE_ATTR
3554 /* Use line_attr when not in the Visual or 'incsearch' area
3555 * (area_attr may be 0 when "noinvcur" is set). */
3556 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3557 || (vcol < fromcol || vcol >= tocol)))
3558 char_attr = line_attr;
3559#endif
3560 else
3561 {
3562 attr_pri = FALSE;
3563#ifdef FEAT_SYN_HL
3564 if (has_syntax)
3565 char_attr = syntax_attr;
3566 else
3567#endif
3568 char_attr = 0;
3569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571
3572 /*
3573 * Get the next character to put on the screen.
3574 */
3575 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003576 * The "p_extra" points to the extra stuff that is inserted to
3577 * represent special characters (non-printable stuff) and other
3578 * things. When all characters are the same, c_extra is used.
3579 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3580 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3582 */
3583 if (n_extra > 0)
3584 {
3585 if (c_extra != NUL)
3586 {
3587 c = c_extra;
3588#ifdef FEAT_MBYTE
3589 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3590 if (enc_utf8 && (*mb_char2len)(c) > 1)
3591 {
3592 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003593 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003594 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
3596 else
3597 mb_utf8 = FALSE;
3598#endif
3599 }
3600 else
3601 {
3602 c = *p_extra;
3603#ifdef FEAT_MBYTE
3604 if (has_mbyte)
3605 {
3606 mb_c = c;
3607 if (enc_utf8)
3608 {
3609 /* If the UTF-8 character is more than one byte:
3610 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003611 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 mb_utf8 = FALSE;
3613 if (mb_l > n_extra)
3614 mb_l = 1;
3615 else if (mb_l > 1)
3616 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003617 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003619 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
3621 }
3622 else
3623 {
3624 /* if this is a DBCS character, put it in "mb_c" */
3625 mb_l = MB_BYTE2LEN(c);
3626 if (mb_l >= n_extra)
3627 mb_l = 1;
3628 else if (mb_l > 1)
3629 mb_c = (c << 8) + p_extra[1];
3630 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003631 if (mb_l == 0) /* at the NUL at end-of-line */
3632 mb_l = 1;
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 /* If a double-width char doesn't fit display a '>' in the
3635 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003636 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637# ifdef FEAT_RIGHTLEFT
3638 wp->w_p_rl ? (col <= 0) :
3639# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003640 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 && (*mb_char2cells)(mb_c) == 2)
3642 {
3643 c = '>';
3644 mb_c = c;
3645 mb_l = 1;
3646 mb_utf8 = FALSE;
3647 multi_attr = hl_attr(HLF_AT);
3648 /* put the pointer back to output the double-width
3649 * character at the start of the next line. */
3650 ++n_extra;
3651 --p_extra;
3652 }
3653 else
3654 {
3655 n_extra -= mb_l - 1;
3656 p_extra += mb_l - 1;
3657 }
3658 }
3659#endif
3660 ++p_extra;
3661 }
3662 --n_extra;
3663 }
3664 else
3665 {
3666 /*
3667 * Get a character from the line itself.
3668 */
3669 c = *ptr;
3670#ifdef FEAT_MBYTE
3671 if (has_mbyte)
3672 {
3673 mb_c = c;
3674 if (enc_utf8)
3675 {
3676 /* If the UTF-8 character is more than one byte: Decode it
3677 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003678 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 mb_utf8 = FALSE;
3680 if (mb_l > 1)
3681 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003682 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 /* Overlong encoded ASCII or ASCII with composing char
3684 * is displayed normally, except a NUL. */
3685 if (mb_c < 0x80)
3686 c = mb_c;
3687 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003688
3689 /* At start of the line we can have a composing char.
3690 * Draw it as a space with a composing char. */
3691 if (utf_iscomposing(mb_c))
3692 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003693 int i;
3694
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003695 for (i = Screen_mco - 1; i > 0; --i)
3696 u8cc[i] = u8cc[i - 1];
3697 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003698 mb_c = ' ';
3699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701
3702 if ((mb_l == 1 && c >= 0x80)
3703 || (mb_l >= 1 && mb_c == 0)
3704 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00003705# ifdef UNICODE16
3706 || mb_c >= 0x10000
3707# endif
3708 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 {
3710 /*
3711 * Illegal UTF-8 byte: display as <xx>.
3712 * Non-BMP character : display as ? or fullwidth ?.
3713 */
Bram Moolenaar11936362007-09-17 20:39:42 +00003714# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00003716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
3718 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003719# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (wp->w_p_rl) /* reverse */
3721 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
Bram Moolenaar11936362007-09-17 20:39:42 +00003724# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 else if (utf_char2cells(mb_c) != 2)
3726 STRCPY(extra, "?");
3727 else
3728 /* 0xff1f in UTF-8: full-width '?' */
3729 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00003730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
3732 p_extra = extra;
3733 c = *p_extra;
3734 mb_c = mb_ptr2char_adv(&p_extra);
3735 mb_utf8 = (c >= 0x80);
3736 n_extra = (int)STRLEN(p_extra);
3737 c_extra = NUL;
3738 if (area_attr == 0 && search_attr == 0)
3739 {
3740 n_attr = n_extra + 1;
3741 extra_attr = hl_attr(HLF_8);
3742 saved_attr2 = char_attr; /* save current attr */
3743 }
3744 }
3745 else if (mb_l == 0) /* at the NUL at end-of-line */
3746 mb_l = 1;
3747#ifdef FEAT_ARABIC
3748 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3749 {
3750 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003751 int pc, pc1, nc;
3752 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
3754 /* The idea of what is the previous and next
3755 * character depends on 'rightleft'. */
3756 if (wp->w_p_rl)
3757 {
3758 pc = prev_c;
3759 pc1 = prev_c1;
3760 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003761 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763 else
3764 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003765 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003767 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 prev_c = mb_c;
3770
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003771 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 else
3774 prev_c = mb_c;
3775#endif
3776 }
3777 else /* enc_dbcs */
3778 {
3779 mb_l = MB_BYTE2LEN(c);
3780 if (mb_l == 0) /* at the NUL at end-of-line */
3781 mb_l = 1;
3782 else if (mb_l > 1)
3783 {
3784 /* We assume a second byte below 32 is illegal.
3785 * Hopefully this is OK for all double-byte encodings!
3786 */
3787 if (ptr[1] >= 32)
3788 mb_c = (c << 8) + ptr[1];
3789 else
3790 {
3791 if (ptr[1] == NUL)
3792 {
3793 /* head byte at end of line */
3794 mb_l = 1;
3795 transchar_nonprint(extra, c);
3796 }
3797 else
3798 {
3799 /* illegal tail byte */
3800 mb_l = 2;
3801 STRCPY(extra, "XX");
3802 }
3803 p_extra = extra;
3804 n_extra = (int)STRLEN(extra) - 1;
3805 c_extra = NUL;
3806 c = *p_extra++;
3807 if (area_attr == 0 && search_attr == 0)
3808 {
3809 n_attr = n_extra + 1;
3810 extra_attr = hl_attr(HLF_8);
3811 saved_attr2 = char_attr; /* save current attr */
3812 }
3813 mb_c = c;
3814 }
3815 }
3816 }
3817 /* If a double-width char doesn't fit display a '>' in the
3818 * last column; the character is displayed at the start of the
3819 * next line. */
3820 if ((
3821# ifdef FEAT_RIGHTLEFT
3822 wp->w_p_rl ? (col <= 0) :
3823# endif
3824 (col >= W_WIDTH(wp) - 1))
3825 && (*mb_char2cells)(mb_c) == 2)
3826 {
3827 c = '>';
3828 mb_c = c;
3829 mb_utf8 = FALSE;
3830 mb_l = 1;
3831 multi_attr = hl_attr(HLF_AT);
3832 /* Put pointer back so that the character will be
3833 * displayed at the start of the next line. */
3834 --ptr;
3835 }
3836 else if (*ptr != NUL)
3837 ptr += mb_l - 1;
3838
3839 /* If a double-width char doesn't fit at the left side display
3840 * a '<' in the first column. */
3841 if (n_skip > 0 && mb_l > 1)
3842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003844 c_extra = '<';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 c = ' ';
3846 if (area_attr == 0 && search_attr == 0)
3847 {
3848 n_attr = n_extra + 1;
3849 extra_attr = hl_attr(HLF_AT);
3850 saved_attr2 = char_attr; /* save current attr */
3851 }
3852 mb_c = c;
3853 mb_utf8 = FALSE;
3854 mb_l = 1;
3855 }
3856
3857 }
3858#endif
3859 ++ptr;
3860
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003861 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003862 if (wp->w_p_list && (c == 160
3863#ifdef FEAT_MBYTE
3864 || (mb_utf8 && mb_c == 160)
3865#endif
3866 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003867 {
3868 c = lcs_nbsp;
3869 if (area_attr == 0 && search_attr == 0)
3870 {
3871 n_attr = 1;
3872 extra_attr = hl_attr(HLF_8);
3873 saved_attr2 = char_attr; /* save current attr */
3874 }
3875#ifdef FEAT_MBYTE
3876 mb_c = c;
3877 if (enc_utf8 && (*mb_char2len)(c) > 1)
3878 {
3879 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003880 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003881 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003882 }
3883 else
3884 mb_utf8 = FALSE;
3885#endif
3886 }
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (extra_check)
3889 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003890#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003891 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003892#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003893
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003894#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 /* Get syntax attribute, unless still at the start of the line
3896 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003897 v = (long)(ptr - line);
3898 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 {
3900 /* Get the syntax attribute for the character. If there
3901 * is an error, disable syntax highlighting. */
3902 save_did_emsg = did_emsg;
3903 did_emsg = FALSE;
3904
Bram Moolenaar217ad922005-03-20 22:37:15 +00003905 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003906# ifdef FEAT_SPELL
3907 has_spell ? &can_spell :
3908# endif
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00003909 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003912 {
3913 wp->w_buffer->b_syn_error = TRUE;
3914 has_syntax = FALSE;
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 else
3917 did_emsg = save_did_emsg;
3918
3919 /* Need to get the line again, a multi-line regexp may
3920 * have made it invalid. */
3921 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3922 ptr = line + v;
3923
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003924 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003926 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003927 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003929#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003930
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003931#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003932 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003933 * Only do this when there is no syntax highlighting, the
3934 * @Spell cluster is not used or the current syntax item
3935 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003936 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003937 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003938 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003939# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003940 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003941 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003942# endif
3943 if (c != 0 && (
3944# ifdef FEAT_SYN_HL
3945 !has_syntax ||
3946# endif
3947 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003948 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003949 char_u *prev_ptr, *p;
3950 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003951 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003952# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003953 if (has_mbyte)
3954 {
3955 prev_ptr = ptr - mb_l;
3956 v -= mb_l - 1;
3957 }
3958 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003959# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003960 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003961
3962 /* Use nextline[] if possible, it has the start of the
3963 * next line concatenated. */
3964 if ((prev_ptr - line) - nextlinecol >= 0)
3965 p = nextline + (prev_ptr - line) - nextlinecol;
3966 else
3967 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003968 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003969 len = spell_check(wp, p, &spell_hlf, &cap_col,
3970 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003971 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003972
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003973 /* In Insert mode only highlight a word that
3974 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003975 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003976 && (State & INSERT) != 0
3977 && wp->w_cursor.lnum == lnum
3978 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003979 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003980 && wp->w_cursor.col < (colnr_T)word_end)
3981 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003982 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003983 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003984 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003985
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003986 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003987 && (p - nextline) + len > nextline_idx)
3988 {
3989 /* Remember that the good word continues at the
3990 * start of the next line. */
3991 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003992 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003993 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003994
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003995 /* Turn index into actual attributes. */
3996 if (spell_hlf != HLF_COUNT)
3997 spell_attr = highlight_attr[spell_hlf];
3998
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003999 if (cap_col > 0)
4000 {
4001 if (p != prev_ptr
4002 && (p - nextline) + cap_col >= nextline_idx)
4003 {
4004 /* Remember that the word in the next line
4005 * must start with a capital. */
4006 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004007 cap_col = (int)((p - nextline) + cap_col
4008 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004009 }
4010 else
4011 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004012 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004013 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004014 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004015 }
4016 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004017 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004018 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004019 char_attr = hl_combine_attr(char_attr, spell_attr);
4020 else
4021 char_attr = hl_combine_attr(spell_attr, char_attr);
4022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#endif
4024#ifdef FEAT_LINEBREAK
4025 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004026 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 */
4028 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
4031 n_extra = win_lbr_chartabsize(wp, ptr - (
4032# ifdef FEAT_MBYTE
4033 has_mbyte ? mb_l :
4034# endif
4035 1), (colnr_T)vcol, NULL) - 1;
4036 c_extra = ' ';
4037 if (vim_iswhite(c))
4038 c = ' ';
4039 }
4040#endif
4041
4042 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4043 {
4044 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004045 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
4047 n_attr = 1;
4048 extra_attr = hl_attr(HLF_8);
4049 saved_attr2 = char_attr; /* save current attr */
4050 }
4051#ifdef FEAT_MBYTE
4052 mb_c = c;
4053 if (enc_utf8 && (*mb_char2len)(c) > 1)
4054 {
4055 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004056 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004057 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059 else
4060 mb_utf8 = FALSE;
4061#endif
4062 }
4063 }
4064
4065 /*
4066 * Handling of non-printable characters.
4067 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004068 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
4070 /*
4071 * when getting a character from the file, we may have to
4072 * turn it into something else on the way to putting it
4073 * into "ScreenLines".
4074 */
4075 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4076 {
4077 /* tab amount depends on current column */
4078 n_extra = (int)wp->w_buffer->b_p_ts
4079 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4080#ifdef FEAT_MBYTE
4081 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4082#endif
4083 if (wp->w_p_list)
4084 {
4085 c = lcs_tab1;
4086 c_extra = lcs_tab2;
4087 n_attr = n_extra + 1;
4088 extra_attr = hl_attr(HLF_8);
4089 saved_attr2 = char_attr; /* save current attr */
4090#ifdef FEAT_MBYTE
4091 mb_c = c;
4092 if (enc_utf8 && (*mb_char2len)(c) > 1)
4093 {
4094 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004095 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004096 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098#endif
4099 }
4100 else
4101 {
4102 c_extra = ' ';
4103 c = ' ';
4104 }
4105 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004106 else if (c == NUL
4107 && ((wp->w_p_list && lcs_eol > 0)
4108 || ((fromcol >= 0 || fromcol_prev >= 0)
4109 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004110#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004111 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004112#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004113 && (
4114# ifdef FEAT_RIGHTLEFT
4115 wp->w_p_rl ? (col >= 0) :
4116# endif
4117 (col < W_WIDTH(wp)))
4118 && !(noinvcur
4119 && (colnr_T)vcol == wp->w_virtcol)))
4120 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004122 /* Display a '$' after the line or highlight an extra
4123 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4125 /* For a diff line the highlighting continues after the
4126 * "$". */
4127 if (
4128# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004129 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130# ifdef LINE_ATTR
4131 &&
4132# endif
4133# endif
4134# ifdef LINE_ATTR
4135 line_attr == 0
4136# endif
4137 )
4138#endif
4139 {
4140#ifdef FEAT_VIRTUALEDIT
4141 /* In virtualedit, visual selections may extend
4142 * beyond end of line. */
4143 if (area_highlighting && virtual_active()
4144 && tocol != MAXCOL && vcol < tocol)
4145 n_extra = 0;
4146 else
4147#endif
4148 {
4149 p_extra = at_end_str;
4150 n_extra = 1;
4151 c_extra = NUL;
4152 }
4153 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004154 if (wp->w_p_list)
4155 c = lcs_eol;
4156 else
4157 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 lcs_eol_one = -1;
4159 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004160 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 {
4162 extra_attr = hl_attr(HLF_AT);
4163 n_attr = 1;
4164 }
4165#ifdef FEAT_MBYTE
4166 mb_c = c;
4167 if (enc_utf8 && (*mb_char2len)(c) > 1)
4168 {
4169 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004170 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004171 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 else
4174 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4175#endif
4176 }
4177 else if (c != NUL)
4178 {
4179 p_extra = transchar(c);
4180#ifdef FEAT_RIGHTLEFT
4181 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4182 rl_mirror(p_extra); /* reverse "<12>" */
4183#endif
4184 n_extra = byte2cells(c) - 1;
4185 c_extra = NUL;
4186 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004187 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
4189 n_attr = n_extra + 1;
4190 extra_attr = hl_attr(HLF_8);
4191 saved_attr2 = char_attr; /* save current attr */
4192 }
4193#ifdef FEAT_MBYTE
4194 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4195#endif
4196 }
4197#ifdef FEAT_VIRTUALEDIT
4198 else if (VIsual_active
4199 && (VIsual_mode == Ctrl_V
4200 || VIsual_mode == 'v')
4201 && virtual_active()
4202 && tocol != MAXCOL
4203 && vcol < tocol
4204 && (
4205# ifdef FEAT_RIGHTLEFT
4206 wp->w_p_rl ? (col >= 0) :
4207# endif
4208 (col < W_WIDTH(wp))))
4209 {
4210 c = ' ';
4211 --ptr; /* put it back at the NUL */
4212 }
4213#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004214#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 else if ((
4216# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004217 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 ) && (
4221# ifdef FEAT_RIGHTLEFT
4222 wp->w_p_rl ? (col >= 0) :
4223# endif
4224 (col < W_WIDTH(wp))))
4225 {
4226 /* Highlight until the right side of the window */
4227 c = ' ';
4228 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004229
4230 /* Remember we do the char for line highlighting. */
4231 ++did_line_attr;
4232
4233 /* don't do search HL for the rest of the line */
4234 if (line_attr != 0 && char_attr == search_attr && col > 0)
4235 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236# ifdef FEAT_DIFF
4237 if (diff_hlf == HLF_TXD)
4238 {
4239 diff_hlf = HLF_CHD;
4240 if (attr == 0 || char_attr != attr)
4241 char_attr = hl_attr(diff_hlf);
4242 }
4243# endif
4244 }
4245#endif
4246 }
4247 }
4248
4249 /* Don't override visual selection highlighting. */
4250 if (n_attr > 0
4251 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004252 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 char_attr = extra_attr;
4254
Bram Moolenaar81695252004-12-29 20:58:21 +00004255#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 /* XIM don't send preedit_start and preedit_end, but they send
4257 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4258 * im_is_preediting() here. */
4259 if (xic != NULL
4260 && lnum == curwin->w_cursor.lnum
4261 && (State & INSERT)
4262 && !p_imdisable
4263 && im_is_preediting()
4264 && draw_state == WL_LINE)
4265 {
4266 colnr_T tcol;
4267
4268 if (preedit_end_col == MAXCOL)
4269 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4270 else
4271 tcol = preedit_end_col;
4272 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4273 {
4274 if (feedback_old_attr < 0)
4275 {
4276 feedback_col = 0;
4277 feedback_old_attr = char_attr;
4278 }
4279 char_attr = im_get_feedback_attr(feedback_col);
4280 if (char_attr < 0)
4281 char_attr = feedback_old_attr;
4282 feedback_col++;
4283 }
4284 else if (feedback_old_attr >= 0)
4285 {
4286 char_attr = feedback_old_attr;
4287 feedback_old_attr = -1;
4288 feedback_col = 0;
4289 }
4290 }
4291#endif
4292 /*
4293 * Handle the case where we are in column 0 but not on the first
4294 * character of the line and the user wants us to show us a
4295 * special character (via 'listchars' option "precedes:<char>".
4296 */
4297 if (lcs_prec_todo != NUL
4298 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4299#ifdef FEAT_DIFF
4300 && filler_todo <= 0
4301#endif
4302 && draw_state > WL_NR
4303 && c != NUL)
4304 {
4305 c = lcs_prec;
4306 lcs_prec_todo = NUL;
4307#ifdef FEAT_MBYTE
4308 mb_c = c;
4309 if (enc_utf8 && (*mb_char2len)(c) > 1)
4310 {
4311 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004312 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004313 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 else
4316 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4317#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004318 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 saved_attr3 = char_attr; /* save current attr */
4321 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4322 n_attr3 = 1;
4323 }
4324 }
4325
4326 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004327 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004329 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004330#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004331 || did_line_attr == 1
4332#endif
4333 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004335#ifdef FEAT_SEARCH_EXTRA
4336 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004337
4338 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004339 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004340 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004341#endif
4342
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 /* invert at least one char, used for Visual and empty line or
4344 * highlight match at end of line. If it's beyond the last
4345 * char on the screen, just overwrite that one (tricky!) Not
4346 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004347#ifdef FEAT_SEARCH_EXTRA
4348 prevcol_hl_flag = FALSE;
4349 if (prevcol == (long)search_hl.startcol)
4350 prevcol_hl_flag = TRUE;
4351 else
4352 {
4353 cur = wp->w_match_head;
4354 while (cur != NULL)
4355 {
4356 if (prevcol == (long)cur->hl.startcol)
4357 {
4358 prevcol_hl_flag = TRUE;
4359 break;
4360 }
4361 cur = cur->next;
4362 }
4363 }
4364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 if (lcs_eol == lcs_eol_one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004366 && ((area_attr != 0 && vcol == fromcol && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367#ifdef FEAT_SEARCH_EXTRA
4368 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004369 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004370# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004371 && did_line_attr <= 1
4372# endif
4373 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374#endif
4375 ))
4376 {
4377 int n = 0;
4378
4379#ifdef FEAT_RIGHTLEFT
4380 if (wp->w_p_rl)
4381 {
4382 if (col < 0)
4383 n = 1;
4384 }
4385 else
4386#endif
4387 {
4388 if (col >= W_WIDTH(wp))
4389 n = -1;
4390 }
4391 if (n != 0)
4392 {
4393 /* At the window boundary, highlight the last character
4394 * instead (better than nothing). */
4395 off += n;
4396 col += n;
4397 }
4398 else
4399 {
4400 /* Add a blank character to highlight. */
4401 ScreenLines[off] = ' ';
4402#ifdef FEAT_MBYTE
4403 if (enc_utf8)
4404 ScreenLinesUC[off] = 0;
4405#endif
4406 }
4407#ifdef FEAT_SEARCH_EXTRA
4408 if (area_attr == 0)
4409 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004410 /* Use attributes from match with highest priority among
4411 * 'search_hl' and the match list. */
4412 char_attr = search_hl.attr;
4413 cur = wp->w_match_head;
4414 shl_flag = FALSE;
4415 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004416 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004417 if (shl_flag == FALSE
4418 && ((cur != NULL
4419 && cur->priority > SEARCH_HL_PRIORITY)
4420 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004421 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004422 shl = &search_hl;
4423 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004424 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004425 else
4426 shl = &cur->hl;
4427 if ((ptr - line) - 1 == (long)shl->startcol)
4428 char_attr = shl->attr;
4429 if (shl != &search_hl && cur != NULL)
4430 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433#endif
4434 ScreenAttrs[off] = char_attr;
4435#ifdef FEAT_RIGHTLEFT
4436 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004439 --off;
4440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 else
4442#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004445 ++off;
4446 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004447 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004448#ifdef FEAT_SYN_HL
4449 eol_hl_off = 1;
4450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaar91170f82006-05-05 21:15:17 +00004454 /*
4455 * At end of the text line.
4456 */
4457 if (c == NUL)
4458 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004459#ifdef FEAT_SYN_HL
Bram Moolenaara443af82007-11-08 13:51:42 +00004460 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol)
4461 {
4462 /* highlight last char after line */
4463 --col;
4464 --off;
4465 --vcol;
4466 }
4467
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004468 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004469 if (wp->w_p_wrap)
4470 v = wp->w_skipcol;
4471 else
4472 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004473 /* check if line ends before left margin */
4474 if (vcol < v + col - win_col_off(wp))
4475
4476 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004477 if (wp->w_p_cuc
Bram Moolenaara443af82007-11-08 13:51:42 +00004478 && (int)wp->w_virtcol >= vcol - eol_hl_off
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004479 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4480 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004481 && lnum != wp->w_cursor.lnum
4482# ifdef FEAT_RIGHTLEFT
4483 && !wp->w_p_rl
4484# endif
4485 )
4486 {
4487 while (col < W_WIDTH(wp))
4488 {
4489 ScreenLines[off] = ' ';
4490#ifdef FEAT_MBYTE
4491 if (enc_utf8)
4492 ScreenLinesUC[off] = 0;
4493#endif
4494 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004495 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004496 {
4497 ScreenAttrs[off] = hl_attr(HLF_CUC);
4498 break;
4499 }
4500 ScreenAttrs[off++] = 0;
4501 ++vcol;
4502 }
4503 }
4504#endif
4505
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4507 wp->w_p_rl);
4508 row++;
4509
4510 /*
4511 * Update w_cline_height and w_cline_folded if the cursor line was
4512 * updated (saves a call to plines() later).
4513 */
4514 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4515 {
4516 curwin->w_cline_row = startrow;
4517 curwin->w_cline_height = row - startrow;
4518#ifdef FEAT_FOLDING
4519 curwin->w_cline_folded = FALSE;
4520#endif
4521 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4522 }
4523
4524 break;
4525 }
4526
4527 /* line continues beyond line end */
4528 if (lcs_ext
4529 && !wp->w_p_wrap
4530#ifdef FEAT_DIFF
4531 && filler_todo <= 0
4532#endif
4533 && (
4534#ifdef FEAT_RIGHTLEFT
4535 wp->w_p_rl ? col == 0 :
4536#endif
4537 col == W_WIDTH(wp) - 1)
4538 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00004539 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4541 {
4542 c = lcs_ext;
4543 char_attr = hl_attr(HLF_AT);
4544#ifdef FEAT_MBYTE
4545 mb_c = c;
4546 if (enc_utf8 && (*mb_char2len)(c) > 1)
4547 {
4548 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004549 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004550 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 }
4552 else
4553 mb_utf8 = FALSE;
4554#endif
4555 }
4556
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004557#ifdef FEAT_SYN_HL
4558 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4559 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004560 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004561 && lnum != wp->w_cursor.lnum
Bram Moolenaar54ef7112009-02-21 20:11:41 +00004562 && draw_state == WL_LINE
4563 && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004564 {
4565 vcol_save_attr = char_attr;
4566 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4567 }
4568 else
4569 vcol_save_attr = -1;
4570#endif
4571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 /*
4573 * Store character to be displayed.
4574 * Skip characters that are left of the screen for 'nowrap'.
4575 */
4576 vcol_prev = vcol;
4577 if (draw_state < WL_LINE || n_skip <= 0)
4578 {
4579 /*
4580 * Store the character.
4581 */
4582#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4583 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4584 {
4585 /* A double-wide character is: put first halve in left cell. */
4586 --off;
4587 --col;
4588 }
4589#endif
4590 ScreenLines[off] = c;
4591#ifdef FEAT_MBYTE
4592 if (enc_dbcs == DBCS_JPNU)
4593 ScreenLines2[off] = mb_c & 0xff;
4594 else if (enc_utf8)
4595 {
4596 if (mb_utf8)
4597 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004598 int i;
4599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004601 if ((c & 0xff) == 0)
4602 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004603 for (i = 0; i < Screen_mco; ++i)
4604 {
4605 ScreenLinesC[i][off] = u8cc[i];
4606 if (u8cc[i] == 0)
4607 break;
4608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610 else
4611 ScreenLinesUC[off] = 0;
4612 }
4613 if (multi_attr)
4614 {
4615 ScreenAttrs[off] = multi_attr;
4616 multi_attr = 0;
4617 }
4618 else
4619#endif
4620 ScreenAttrs[off] = char_attr;
4621
4622#ifdef FEAT_MBYTE
4623 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4624 {
4625 /* Need to fill two screen columns. */
4626 ++off;
4627 ++col;
4628 if (enc_utf8)
4629 /* UTF-8: Put a 0 in the second screen char. */
4630 ScreenLines[off] = 0;
4631 else
4632 /* DBCS: Put second byte in the second screen char. */
4633 ScreenLines[off] = mb_c & 0xff;
4634 ++vcol;
4635 /* When "tocol" is halfway a character, set it to the end of
4636 * the character, otherwise highlighting won't stop. */
4637 if (tocol == vcol)
4638 ++tocol;
4639#ifdef FEAT_RIGHTLEFT
4640 if (wp->w_p_rl)
4641 {
4642 /* now it's time to backup one cell */
4643 --off;
4644 --col;
4645 }
4646#endif
4647 }
4648#endif
4649#ifdef FEAT_RIGHTLEFT
4650 if (wp->w_p_rl)
4651 {
4652 --off;
4653 --col;
4654 }
4655 else
4656#endif
4657 {
4658 ++off;
4659 ++col;
4660 }
4661 }
4662 else
4663 --n_skip;
4664
4665 /* Only advance the "vcol" when after the 'number' column. */
4666 if (draw_state >= WL_SBR
4667#ifdef FEAT_DIFF
4668 && filler_todo <= 0
4669#endif
4670 )
4671 ++vcol;
4672
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004673#ifdef FEAT_SYN_HL
4674 if (vcol_save_attr >= 0)
4675 char_attr = vcol_save_attr;
4676#endif
4677
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 /* restore attributes after "predeces" in 'listchars' */
4679 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4680 char_attr = saved_attr3;
4681
4682 /* restore attributes after last 'listchars' or 'number' char */
4683 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4684 char_attr = saved_attr2;
4685
4686 /*
4687 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00004688 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 */
4690 if ((
4691#ifdef FEAT_RIGHTLEFT
4692 wp->w_p_rl ? (col < 0) :
4693#endif
4694 (col >= W_WIDTH(wp)))
4695 && (*ptr != NUL
4696#ifdef FEAT_DIFF
4697 || filler_todo > 0
4698#endif
4699 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4700 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4701 )
4702 {
4703 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4704 wp->w_p_rl);
4705 ++row;
4706 ++screen_row;
4707
4708 /* When not wrapping and finished diff lines, or when displayed
4709 * '$' and highlighting until last column, break here. */
4710 if ((!wp->w_p_wrap
4711#ifdef FEAT_DIFF
4712 && filler_todo <= 0
4713#endif
4714 ) || lcs_eol_one == -1)
4715 break;
4716
4717 /* When the window is too narrow draw all "@" lines. */
4718 if (draw_state != WL_LINE
4719#ifdef FEAT_DIFF
4720 && filler_todo <= 0
4721#endif
4722 )
4723 {
4724 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4725#ifdef FEAT_VERTSPLIT
4726 draw_vsep_win(wp, row);
4727#endif
4728 row = endrow;
4729 }
4730
4731 /* When line got too long for screen break here. */
4732 if (row == endrow)
4733 {
4734 ++row;
4735 break;
4736 }
4737
4738 if (screen_cur_row == screen_row - 1
4739#ifdef FEAT_DIFF
4740 && filler_todo <= 0
4741#endif
4742 && W_WIDTH(wp) == Columns)
4743 {
4744 /* Remember that the line wraps, used for modeless copy. */
4745 LineWraps[screen_row - 1] = TRUE;
4746
4747 /*
4748 * Special trick to make copy/paste of wrapped lines work with
4749 * xterm/screen: write an extra character beyond the end of
4750 * the line. This will work with all terminal types
4751 * (regardless of the xn,am settings).
4752 * Only do this on a fast tty.
4753 * Only do this if the cursor is on the current line
4754 * (something has been written in it).
4755 * Don't do this for the GUI.
4756 * Don't do this for double-width characters.
4757 * Don't do this for a window not at the right screen border.
4758 */
4759 if (p_tf
4760#ifdef FEAT_GUI
4761 && !gui.in_use
4762#endif
4763#ifdef FEAT_MBYTE
4764 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00004765 && ((*mb_off2cells)(LineOffset[screen_row],
4766 LineOffset[screen_row] + screen_Columns)
4767 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00004769 + (int)Columns - 2,
4770 LineOffset[screen_row] + screen_Columns)
4771 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772#endif
4773 )
4774 {
4775 /* First make sure we are at the end of the screen line,
4776 * then output the same character again to let the
4777 * terminal know about the wrap. If the terminal doesn't
4778 * auto-wrap, we overwrite the character. */
4779 if (screen_cur_col != W_WIDTH(wp))
4780 screen_char(LineOffset[screen_row - 1]
4781 + (unsigned)Columns - 1,
4782 screen_row - 1, (int)(Columns - 1));
4783
4784#ifdef FEAT_MBYTE
4785 /* When there is a multi-byte character, just output a
4786 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004787 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4788 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 out_char(' ');
4790 else
4791#endif
4792 out_char(ScreenLines[LineOffset[screen_row - 1]
4793 + (Columns - 1)]);
4794 /* force a redraw of the first char on the next line */
4795 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4796 screen_start(); /* don't know where cursor is now */
4797 }
4798 }
4799
4800 col = 0;
4801 off = (unsigned)(current_ScreenLine - ScreenLines);
4802#ifdef FEAT_RIGHTLEFT
4803 if (wp->w_p_rl)
4804 {
4805 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4806 off += col;
4807 }
4808#endif
4809
4810 /* reset the drawing state for the start of a wrapped line */
4811 draw_state = WL_START;
4812 saved_n_extra = n_extra;
4813 saved_p_extra = p_extra;
4814 saved_c_extra = c_extra;
4815 saved_char_attr = char_attr;
4816 n_extra = 0;
4817 lcs_prec_todo = lcs_prec;
4818#ifdef FEAT_LINEBREAK
4819# ifdef FEAT_DIFF
4820 if (filler_todo <= 0)
4821# endif
4822 need_showbreak = TRUE;
4823#endif
4824#ifdef FEAT_DIFF
4825 --filler_todo;
4826 /* When the filler lines are actually below the last line of the
4827 * file, don't draw the line itself, break here. */
4828 if (filler_todo == 0 && wp->w_botfill)
4829 break;
4830#endif
4831 }
4832
4833 } /* for every character in the line */
4834
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004835#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004836 /* After an empty line check first word for capital. */
4837 if (*skipwhite(line) == NUL)
4838 {
4839 capcol_lnum = lnum + 1;
4840 cap_col = 0;
4841 }
4842#endif
4843
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 return row;
4845}
4846
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004847#ifdef FEAT_MBYTE
4848static int comp_char_differs __ARGS((int, int));
4849
4850/*
4851 * Return if the composing characters at "off_from" and "off_to" differ.
4852 */
4853 static int
4854comp_char_differs(off_from, off_to)
4855 int off_from;
4856 int off_to;
4857{
4858 int i;
4859
4860 for (i = 0; i < Screen_mco; ++i)
4861 {
4862 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4863 return TRUE;
4864 if (ScreenLinesC[i][off_from] == 0)
4865 break;
4866 }
4867 return FALSE;
4868}
4869#endif
4870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871/*
4872 * Check whether the given character needs redrawing:
4873 * - the (first byte of the) character is different
4874 * - the attributes are different
4875 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004876 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 */
4878 static int
4879char_needs_redraw(off_from, off_to, cols)
4880 int off_from;
4881 int off_to;
4882 int cols;
4883{
4884 if (cols > 0
4885 && ((ScreenLines[off_from] != ScreenLines[off_to]
4886 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4887
4888#ifdef FEAT_MBYTE
4889 || (enc_dbcs != 0
4890 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4891 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4892 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4893 : (cols > 1 && ScreenLines[off_from + 1]
4894 != ScreenLines[off_to + 1])))
4895 || (enc_utf8
4896 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4897 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00004898 && comp_char_differs(off_from, off_to))
4899 || (cols > 1 && ScreenLines[off_from + 1]
4900 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#endif
4902 ))
4903 return TRUE;
4904 return FALSE;
4905}
4906
4907/*
4908 * Move one "cooked" screen line to the screen, but only the characters that
4909 * have actually changed. Handle insert/delete character.
4910 * "coloff" gives the first column on the screen for this line.
4911 * "endcol" gives the columns where valid characters are.
4912 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4913 * needs to be cleared, negative otherwise.
4914 * "rlflag" is TRUE in a rightleft window:
4915 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4916 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4917 */
4918 static void
4919screen_line(row, coloff, endcol, clear_width
4920#ifdef FEAT_RIGHTLEFT
4921 , rlflag
4922#endif
4923 )
4924 int row;
4925 int coloff;
4926 int endcol;
4927 int clear_width;
4928#ifdef FEAT_RIGHTLEFT
4929 int rlflag;
4930#endif
4931{
4932 unsigned off_from;
4933 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004934#ifdef FEAT_MBYTE
4935 unsigned max_off_from;
4936 unsigned max_off_to;
4937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 int col = 0;
4939#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4940 int hl;
4941#endif
4942 int force = FALSE; /* force update rest of the line */
4943 int redraw_this /* bool: does character need redraw? */
4944#ifdef FEAT_GUI
4945 = TRUE /* For GUI when while-loop empty */
4946#endif
4947 ;
4948 int redraw_next; /* redraw_this for next character */
4949#ifdef FEAT_MBYTE
4950 int clear_next = FALSE;
4951 int char_cells; /* 1: normal char */
4952 /* 2: occupies two display cells */
4953# define CHAR_CELLS char_cells
4954#else
4955# define CHAR_CELLS 1
4956#endif
4957
4958# ifdef FEAT_CLIPBOARD
4959 clip_may_clear_selection(row, row);
4960# endif
4961
4962 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4963 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00004964#ifdef FEAT_MBYTE
4965 max_off_from = off_from + screen_Columns;
4966 max_off_to = LineOffset[row] + screen_Columns;
4967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969#ifdef FEAT_RIGHTLEFT
4970 if (rlflag)
4971 {
4972 /* Clear rest first, because it's left of the text. */
4973 if (clear_width > 0)
4974 {
4975 while (col <= endcol && ScreenLines[off_to] == ' '
4976 && ScreenAttrs[off_to] == 0
4977# ifdef FEAT_MBYTE
4978 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4979# endif
4980 )
4981 {
4982 ++off_to;
4983 ++col;
4984 }
4985 if (col <= endcol)
4986 screen_fill(row, row + 1, col + coloff,
4987 endcol + coloff + 1, ' ', ' ', 0);
4988 }
4989 col = endcol + 1;
4990 off_to = LineOffset[row] + col + coloff;
4991 off_from += col;
4992 endcol = (clear_width > 0 ? clear_width : -clear_width);
4993 }
4994#endif /* FEAT_RIGHTLEFT */
4995
4996 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4997
4998 while (col < endcol)
4999 {
5000#ifdef FEAT_MBYTE
5001 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005002 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 else
5004 char_cells = 1;
5005#endif
5006
5007 redraw_this = redraw_next;
5008 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5009 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5010
5011#ifdef FEAT_GUI
5012 /* If the next character was bold, then redraw the current character to
5013 * remove any pixels that might have spilt over into us. This only
5014 * happens in the GUI.
5015 */
5016 if (redraw_next && gui.in_use)
5017 {
5018 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005019 if (hl > HL_ALL)
5020 hl = syn_attr2attr(hl);
5021 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 redraw_this = TRUE;
5023 }
5024#endif
5025
5026 if (redraw_this)
5027 {
5028 /*
5029 * Special handling when 'xs' termcap flag set (hpterm):
5030 * Attributes for characters are stored at the position where the
5031 * cursor is when writing the highlighting code. The
5032 * start-highlighting code must be written with the cursor on the
5033 * first highlighted character. The stop-highlighting code must
5034 * be written with the cursor just after the last highlighted
5035 * character.
5036 * Overwriting a character doesn't remove it's highlighting. Need
5037 * to clear the rest of the line, and force redrawing it
5038 * completely.
5039 */
5040 if ( p_wiv
5041 && !force
5042#ifdef FEAT_GUI
5043 && !gui.in_use
5044#endif
5045 && ScreenAttrs[off_to] != 0
5046 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5047 {
5048 /*
5049 * Need to remove highlighting attributes here.
5050 */
5051 windgoto(row, col + coloff);
5052 out_str(T_CE); /* clear rest of this screen line */
5053 screen_start(); /* don't know where cursor is now */
5054 force = TRUE; /* force redraw of rest of the line */
5055 redraw_next = TRUE; /* or else next char would miss out */
5056
5057 /*
5058 * If the previous character was highlighted, need to stop
5059 * highlighting at this character.
5060 */
5061 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5062 {
5063 screen_attr = ScreenAttrs[off_to - 1];
5064 term_windgoto(row, col + coloff);
5065 screen_stop_highlight();
5066 }
5067 else
5068 screen_attr = 0; /* highlighting has stopped */
5069 }
5070#ifdef FEAT_MBYTE
5071 if (enc_dbcs != 0)
5072 {
5073 /* Check if overwriting a double-byte with a single-byte or
5074 * the other way around requires another character to be
5075 * redrawn. For UTF-8 this isn't needed, because comparing
5076 * ScreenLinesUC[] is sufficient. */
5077 if (char_cells == 1
5078 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005079 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 /* Writing a single-cell character over a double-cell
5082 * character: need to redraw the next cell. */
5083 ScreenLines[off_to + 1] = 0;
5084 redraw_next = TRUE;
5085 }
5086 else if (char_cells == 2
5087 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005088 && (*mb_off2cells)(off_to, max_off_to) == 1
5089 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 /* Writing the second half of a double-cell character over
5092 * a double-cell character: need to redraw the second
5093 * cell. */
5094 ScreenLines[off_to + 2] = 0;
5095 redraw_next = TRUE;
5096 }
5097
5098 if (enc_dbcs == DBCS_JPNU)
5099 ScreenLines2[off_to] = ScreenLines2[off_from];
5100 }
5101 /* When writing a single-width character over a double-width
5102 * character and at the end of the redrawn text, need to clear out
5103 * the right halve of the old character.
5104 * Also required when writing the right halve of a double-width
5105 * char over the left halve of an existing one. */
5106 if (has_mbyte && col + char_cells == endcol
5107 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005108 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005110 && (*mb_off2cells)(off_to, max_off_to) == 1
5111 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 clear_next = TRUE;
5113#endif
5114
5115 ScreenLines[off_to] = ScreenLines[off_from];
5116#ifdef FEAT_MBYTE
5117 if (enc_utf8)
5118 {
5119 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5120 if (ScreenLinesUC[off_from] != 0)
5121 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005122 int i;
5123
5124 for (i = 0; i < Screen_mco; ++i)
5125 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 }
5128 if (char_cells == 2)
5129 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5130#endif
5131
5132#if defined(FEAT_GUI) || defined(UNIX)
5133 /* The bold trick makes a single row of pixels appear in the next
5134 * character. When a bold character is removed, the next
5135 * character should be redrawn too. This happens for our own GUI
5136 * and for some xterms. */
5137 if (
5138# ifdef FEAT_GUI
5139 gui.in_use
5140# endif
5141# if defined(FEAT_GUI) && defined(UNIX)
5142 ||
5143# endif
5144# ifdef UNIX
5145 term_is_xterm
5146# endif
5147 )
5148 {
5149 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005150 if (hl > HL_ALL)
5151 hl = syn_attr2attr(hl);
5152 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 redraw_next = TRUE;
5154 }
5155#endif
5156 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5157#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005158 /* For simplicity set the attributes of second half of a
5159 * double-wide character equal to the first half. */
5160 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005162
5163 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 else
5166#endif
5167 screen_char(off_to, row, col + coloff);
5168 }
5169 else if ( p_wiv
5170#ifdef FEAT_GUI
5171 && !gui.in_use
5172#endif
5173 && col + coloff > 0)
5174 {
5175 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5176 {
5177 /*
5178 * Don't output stop-highlight when moving the cursor, it will
5179 * stop the highlighting when it should continue.
5180 */
5181 screen_attr = 0;
5182 }
5183 else if (screen_attr != 0)
5184 screen_stop_highlight();
5185 }
5186
5187 off_to += CHAR_CELLS;
5188 off_from += CHAR_CELLS;
5189 col += CHAR_CELLS;
5190 }
5191
5192#ifdef FEAT_MBYTE
5193 if (clear_next)
5194 {
5195 /* Clear the second half of a double-wide character of which the left
5196 * half was overwritten with a single-wide character. */
5197 ScreenLines[off_to] = ' ';
5198 if (enc_utf8)
5199 ScreenLinesUC[off_to] = 0;
5200 screen_char(off_to, row, col + coloff);
5201 }
5202#endif
5203
5204 if (clear_width > 0
5205#ifdef FEAT_RIGHTLEFT
5206 && !rlflag
5207#endif
5208 )
5209 {
5210#ifdef FEAT_GUI
5211 int startCol = col;
5212#endif
5213
5214 /* blank out the rest of the line */
5215 while (col < clear_width && ScreenLines[off_to] == ' '
5216 && ScreenAttrs[off_to] == 0
5217#ifdef FEAT_MBYTE
5218 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5219#endif
5220 )
5221 {
5222 ++off_to;
5223 ++col;
5224 }
5225 if (col < clear_width)
5226 {
5227#ifdef FEAT_GUI
5228 /*
5229 * In the GUI, clearing the rest of the line may leave pixels
5230 * behind if the first character cleared was bold. Some bold
5231 * fonts spill over the left. In this case we redraw the previous
5232 * character too. If we didn't skip any blanks above, then we
5233 * only redraw if the character wasn't already redrawn anyway.
5234 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005235 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
5237 hl = ScreenAttrs[off_to];
5238 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005239 {
5240 int prev_cells = 1;
5241# ifdef FEAT_MBYTE
5242 if (enc_utf8)
5243 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5244 * that its width is 2. */
5245 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5246 else if (enc_dbcs != 0)
5247 {
5248 /* find previous character by counting from first
5249 * column and get its width. */
5250 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005251 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005252
5253 while (off < off_to)
5254 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005255 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005256 off += prev_cells;
5257 }
5258 }
5259
5260 if (enc_dbcs != 0 && prev_cells > 1)
5261 screen_char_2(off_to - prev_cells, row,
5262 col + coloff - prev_cells);
5263 else
5264# endif
5265 screen_char(off_to - prev_cells, row,
5266 col + coloff - prev_cells);
5267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
5269#endif
5270 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5271 ' ', ' ', 0);
5272#ifdef FEAT_VERTSPLIT
5273 off_to += clear_width - col;
5274 col = clear_width;
5275#endif
5276 }
5277 }
5278
5279 if (clear_width > 0)
5280 {
5281#ifdef FEAT_VERTSPLIT
5282 /* For a window that's left of another, draw the separator char. */
5283 if (col + coloff < Columns)
5284 {
5285 int c;
5286
5287 c = fillchar_vsep(&hl);
5288 if (ScreenLines[off_to] != c
5289# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005290 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5291 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292# endif
5293 || ScreenAttrs[off_to] != hl)
5294 {
5295 ScreenLines[off_to] = c;
5296 ScreenAttrs[off_to] = hl;
5297# ifdef FEAT_MBYTE
5298 if (enc_utf8)
5299 {
5300 if (c >= 0x80)
5301 {
5302 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005303 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 else
5306 ScreenLinesUC[off_to] = 0;
5307 }
5308# endif
5309 screen_char(off_to, row, col + coloff);
5310 }
5311 }
5312 else
5313#endif
5314 LineWraps[row] = FALSE;
5315 }
5316}
5317
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005318#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005320 * Mirror text "str" for right-left displaying.
5321 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005323 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324rl_mirror(str)
5325 char_u *str;
5326{
5327 char_u *p1, *p2;
5328 int t;
5329
5330 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5331 {
5332 t = *p1;
5333 *p1 = *p2;
5334 *p2 = t;
5335 }
5336}
5337#endif
5338
5339#if defined(FEAT_WINDOWS) || defined(PROTO)
5340/*
5341 * mark all status lines for redraw; used after first :cd
5342 */
5343 void
5344status_redraw_all()
5345{
5346 win_T *wp;
5347
5348 for (wp = firstwin; wp; wp = wp->w_next)
5349 if (wp->w_status_height)
5350 {
5351 wp->w_redr_status = TRUE;
5352 redraw_later(VALID);
5353 }
5354}
5355
5356/*
5357 * mark all status lines of the current buffer for redraw
5358 */
5359 void
5360status_redraw_curbuf()
5361{
5362 win_T *wp;
5363
5364 for (wp = firstwin; wp; wp = wp->w_next)
5365 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5366 {
5367 wp->w_redr_status = TRUE;
5368 redraw_later(VALID);
5369 }
5370}
5371
5372/*
5373 * Redraw all status lines that need to be redrawn.
5374 */
5375 void
5376redraw_statuslines()
5377{
5378 win_T *wp;
5379
5380 for (wp = firstwin; wp; wp = wp->w_next)
5381 if (wp->w_redr_status)
5382 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005383 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005384 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385}
5386#endif
5387
5388#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5389/*
5390 * Redraw all status lines at the bottom of frame "frp".
5391 */
5392 void
5393win_redraw_last_status(frp)
5394 frame_T *frp;
5395{
5396 if (frp->fr_layout == FR_LEAF)
5397 frp->fr_win->w_redr_status = TRUE;
5398 else if (frp->fr_layout == FR_ROW)
5399 {
5400 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5401 win_redraw_last_status(frp);
5402 }
5403 else /* frp->fr_layout == FR_COL */
5404 {
5405 frp = frp->fr_child;
5406 while (frp->fr_next != NULL)
5407 frp = frp->fr_next;
5408 win_redraw_last_status(frp);
5409 }
5410}
5411#endif
5412
5413#ifdef FEAT_VERTSPLIT
5414/*
5415 * Draw the verticap separator right of window "wp" starting with line "row".
5416 */
5417 static void
5418draw_vsep_win(wp, row)
5419 win_T *wp;
5420 int row;
5421{
5422 int hl;
5423 int c;
5424
5425 if (wp->w_vsep_width)
5426 {
5427 /* draw the vertical separator right of this window */
5428 c = fillchar_vsep(&hl);
5429 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5430 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5431 c, ' ', hl);
5432 }
5433}
5434#endif
5435
5436#ifdef FEAT_WILDMENU
5437static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005438static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
5440/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00005441 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 */
5443 static int
5444status_match_len(xp, s)
5445 expand_T *xp;
5446 char_u *s;
5447{
5448 int len = 0;
5449
5450#ifdef FEAT_MENU
5451 int emenu = (xp->xp_context == EXPAND_MENUS
5452 || xp->xp_context == EXPAND_MENUNAMES);
5453
5454 /* Check for menu separators - replace with '|'. */
5455 if (emenu && menu_is_separator(s))
5456 return 1;
5457#endif
5458
5459 while (*s != NUL)
5460 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005461 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00005462 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005463 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465
5466 return len;
5467}
5468
5469/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005470 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005471 * These are backslashes used for escaping. Do show backslashes in help tags.
5472 */
5473 static int
5474skip_status_match_char(xp, s)
5475 expand_T *xp;
5476 char_u *s;
5477{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005478 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005479#ifdef FEAT_MENU
5480 || ((xp->xp_context == EXPAND_MENUS
5481 || xp->xp_context == EXPAND_MENUNAMES)
5482 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5483#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005484 )
5485 {
5486#ifndef BACKSLASH_IN_FILENAME
5487 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
5488 return 2;
5489#endif
5490 return 1;
5491 }
5492 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005493}
5494
5495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 * Show wildchar matches in the status line.
5497 * Show at least the "match" item.
5498 * We start at item 'first_match' in the list and show all matches that fit.
5499 *
5500 * If inversion is possible we use it. Else '=' characters are used.
5501 */
5502 void
5503win_redr_status_matches(xp, num_matches, matches, match, showtail)
5504 expand_T *xp;
5505 int num_matches;
5506 char_u **matches; /* list of matches */
5507 int match;
5508 int showtail;
5509{
5510#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5511 int row;
5512 char_u *buf;
5513 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005514 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 int fillchar;
5516 int attr;
5517 int i;
5518 int highlight = TRUE;
5519 char_u *selstart = NULL;
5520 int selstart_col = 0;
5521 char_u *selend = NULL;
5522 static int first_match = 0;
5523 int add_left = FALSE;
5524 char_u *s;
5525#ifdef FEAT_MENU
5526 int emenu;
5527#endif
5528#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5529 int l;
5530#endif
5531
5532 if (matches == NULL) /* interrupted completion? */
5533 return;
5534
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005535#ifdef FEAT_MBYTE
5536 if (has_mbyte)
5537 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5538 else
5539#endif
5540 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 if (buf == NULL)
5542 return;
5543
5544 if (match == -1) /* don't show match but original text */
5545 {
5546 match = 0;
5547 highlight = FALSE;
5548 }
5549 /* count 1 for the ending ">" */
5550 clen = status_match_len(xp, L_MATCH(match)) + 3;
5551 if (match == 0)
5552 first_match = 0;
5553 else if (match < first_match)
5554 {
5555 /* jumping left, as far as we can go */
5556 first_match = match;
5557 add_left = TRUE;
5558 }
5559 else
5560 {
5561 /* check if match fits on the screen */
5562 for (i = first_match; i < match; ++i)
5563 clen += status_match_len(xp, L_MATCH(i)) + 2;
5564 if (first_match > 0)
5565 clen += 2;
5566 /* jumping right, put match at the left */
5567 if ((long)clen > Columns)
5568 {
5569 first_match = match;
5570 /* if showing the last match, we can add some on the left */
5571 clen = 2;
5572 for (i = match; i < num_matches; ++i)
5573 {
5574 clen += status_match_len(xp, L_MATCH(i)) + 2;
5575 if ((long)clen >= Columns)
5576 break;
5577 }
5578 if (i == num_matches)
5579 add_left = TRUE;
5580 }
5581 }
5582 if (add_left)
5583 while (first_match > 0)
5584 {
5585 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5586 if ((long)clen >= Columns)
5587 break;
5588 --first_match;
5589 }
5590
5591 fillchar = fillchar_status(&attr, TRUE);
5592
5593 if (first_match == 0)
5594 {
5595 *buf = NUL;
5596 len = 0;
5597 }
5598 else
5599 {
5600 STRCPY(buf, "< ");
5601 len = 2;
5602 }
5603 clen = len;
5604
5605 i = first_match;
5606 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5607 {
5608 if (i == match)
5609 {
5610 selstart = buf + len;
5611 selstart_col = clen;
5612 }
5613
5614 s = L_MATCH(i);
5615 /* Check for menu separators - replace with '|' */
5616#ifdef FEAT_MENU
5617 emenu = (xp->xp_context == EXPAND_MENUS
5618 || xp->xp_context == EXPAND_MENUNAMES);
5619 if (emenu && menu_is_separator(s))
5620 {
5621 STRCPY(buf + len, transchar('|'));
5622 l = (int)STRLEN(buf + len);
5623 len += l;
5624 clen += l;
5625 }
5626 else
5627#endif
5628 for ( ; *s != NUL; ++s)
5629 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00005630 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 clen += ptr2cells(s);
5632#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005633 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
5635 STRNCPY(buf + len, s, l);
5636 s += l - 1;
5637 len += l;
5638 }
5639 else
5640#endif
5641 {
5642 STRCPY(buf + len, transchar_byte(*s));
5643 len += (int)STRLEN(buf + len);
5644 }
5645 }
5646 if (i == match)
5647 selend = buf + len;
5648
5649 *(buf + len++) = ' ';
5650 *(buf + len++) = ' ';
5651 clen += 2;
5652 if (++i == num_matches)
5653 break;
5654 }
5655
5656 if (i != num_matches)
5657 {
5658 *(buf + len++) = '>';
5659 ++clen;
5660 }
5661
5662 buf[len] = NUL;
5663
5664 row = cmdline_row - 1;
5665 if (row >= 0)
5666 {
5667 if (wild_menu_showing == 0)
5668 {
5669 if (msg_scrolled > 0)
5670 {
5671 /* Put the wildmenu just above the command line. If there is
5672 * no room, scroll the screen one line up. */
5673 if (cmdline_row == Rows - 1)
5674 {
5675 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5676 ++msg_scrolled;
5677 }
5678 else
5679 {
5680 ++cmdline_row;
5681 ++row;
5682 }
5683 wild_menu_showing = WM_SCROLLED;
5684 }
5685 else
5686 {
5687 /* Create status line if needed by setting 'laststatus' to 2.
5688 * Set 'winminheight' to zero to avoid that the window is
5689 * resized. */
5690 if (lastwin->w_status_height == 0)
5691 {
5692 save_p_ls = p_ls;
5693 save_p_wmh = p_wmh;
5694 p_ls = 2;
5695 p_wmh = 0;
5696 last_status(FALSE);
5697 }
5698 wild_menu_showing = WM_SHOWN;
5699 }
5700 }
5701
5702 screen_puts(buf, row, 0, attr);
5703 if (selstart != NULL && highlight)
5704 {
5705 *selend = NUL;
5706 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5707 }
5708
5709 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5710 }
5711
5712#ifdef FEAT_VERTSPLIT
5713 win_redraw_last_status(topframe);
5714#else
5715 lastwin->w_redr_status = TRUE;
5716#endif
5717 vim_free(buf);
5718}
5719#endif
5720
5721#if defined(FEAT_WINDOWS) || defined(PROTO)
5722/*
5723 * Redraw the status line of window wp.
5724 *
5725 * If inversion is possible we use it. Else '=' characters are used.
5726 */
5727 void
5728win_redr_status(wp)
5729 win_T *wp;
5730{
5731 int row;
5732 char_u *p;
5733 int len;
5734 int fillchar;
5735 int attr;
5736 int this_ru_col;
5737
5738 wp->w_redr_status = FALSE;
5739 if (wp->w_status_height == 0)
5740 {
5741 /* no status line, can only be last window */
5742 redraw_cmdline = TRUE;
5743 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005744 else if (!redrawing()
5745#ifdef FEAT_INS_EXPAND
5746 /* don't update status line when popup menu is visible and may be
5747 * drawn over it */
5748 || pum_visible()
5749#endif
5750 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 /* Don't redraw right now, do it later. */
5753 wp->w_redr_status = TRUE;
5754 }
5755#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005756 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005759 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761#endif
5762 else
5763 {
5764 fillchar = fillchar_status(&attr, wp == curwin);
5765
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005766 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 p = NameBuff;
5768 len = (int)STRLEN(p);
5769
5770 if (wp->w_buffer->b_help
5771#ifdef FEAT_QUICKFIX
5772 || wp->w_p_pvw
5773#endif
5774 || bufIsChanged(wp->w_buffer)
5775 || wp->w_buffer->b_p_ro)
5776 *(p + len++) = ' ';
5777 if (wp->w_buffer->b_help)
5778 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005779 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 len += (int)STRLEN(p + len);
5781 }
5782#ifdef FEAT_QUICKFIX
5783 if (wp->w_p_pvw)
5784 {
5785 STRCPY(p + len, _("[Preview]"));
5786 len += (int)STRLEN(p + len);
5787 }
5788#endif
5789 if (bufIsChanged(wp->w_buffer))
5790 {
5791 STRCPY(p + len, "[+]");
5792 len += 3;
5793 }
5794 if (wp->w_buffer->b_p_ro)
5795 {
5796 STRCPY(p + len, "[RO]");
5797 len += 4;
5798 }
5799
5800#ifndef FEAT_VERTSPLIT
5801 this_ru_col = ru_col;
5802 if (this_ru_col < (Columns + 1) / 2)
5803 this_ru_col = (Columns + 1) / 2;
5804#else
5805 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5806 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5807 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5808 if (this_ru_col <= 1)
5809 {
5810 p = (char_u *)"<"; /* No room for file name! */
5811 len = 1;
5812 }
5813 else
5814#endif
5815#ifdef FEAT_MBYTE
5816 if (has_mbyte)
5817 {
5818 int clen = 0, i;
5819
5820 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005821 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 clen += (*mb_ptr2cells)(p + i);
5823 /* Find first character that will fit.
5824 * Going from start to end is much faster for DBCS. */
5825 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005826 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 clen -= (*mb_ptr2cells)(p + i);
5828 len = clen;
5829 if (i > 0)
5830 {
5831 p = p + i - 1;
5832 *p = '<';
5833 ++len;
5834 }
5835
5836 }
5837 else
5838#endif
5839 if (len > this_ru_col - 1)
5840 {
5841 p += len - (this_ru_col - 1);
5842 *p = '<';
5843 len = this_ru_col - 1;
5844 }
5845
5846 row = W_WINROW(wp) + wp->w_height;
5847 screen_puts(p, row, W_WINCOL(wp), attr);
5848 screen_fill(row, row + 1, len + W_WINCOL(wp),
5849 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5850
5851 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5852 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5853 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5854 - 1 + W_WINCOL(wp)), attr);
5855
5856#ifdef FEAT_CMDL_INFO
5857 win_redr_ruler(wp, TRUE);
5858#endif
5859 }
5860
5861#ifdef FEAT_VERTSPLIT
5862 /*
5863 * May need to draw the character below the vertical separator.
5864 */
5865 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5866 {
5867 if (stl_connected(wp))
5868 fillchar = fillchar_status(&attr, wp == curwin);
5869 else
5870 fillchar = fillchar_vsep(&attr);
5871 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5872 attr);
5873 }
5874#endif
5875}
5876
Bram Moolenaar238a5642006-02-21 22:12:05 +00005877#ifdef FEAT_STL_OPT
5878/*
5879 * Redraw the status line according to 'statusline' and take care of any
5880 * errors encountered.
5881 */
5882 static void
5883redraw_custum_statusline(wp)
5884 win_T *wp;
5885{
5886 int save_called_emsg = called_emsg;
5887
5888 called_emsg = FALSE;
5889 win_redr_custom(wp, FALSE);
5890 if (called_emsg)
5891 set_string_option_direct((char_u *)"statusline", -1,
5892 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005893 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005894 called_emsg |= save_called_emsg;
5895}
5896#endif
5897
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898# ifdef FEAT_VERTSPLIT
5899/*
5900 * Return TRUE if the status line of window "wp" is connected to the status
5901 * line of the window right of it. If not, then it's a vertical separator.
5902 * Only call if (wp->w_vsep_width != 0).
5903 */
5904 int
5905stl_connected(wp)
5906 win_T *wp;
5907{
5908 frame_T *fr;
5909
5910 fr = wp->w_frame;
5911 while (fr->fr_parent != NULL)
5912 {
5913 if (fr->fr_parent->fr_layout == FR_COL)
5914 {
5915 if (fr->fr_next != NULL)
5916 break;
5917 }
5918 else
5919 {
5920 if (fr->fr_next != NULL)
5921 return TRUE;
5922 }
5923 fr = fr->fr_parent;
5924 }
5925 return FALSE;
5926}
5927# endif
5928
5929#endif /* FEAT_WINDOWS */
5930
5931#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5932/*
5933 * Get the value to show for the language mappings, active 'keymap'.
5934 */
5935 int
5936get_keymap_str(wp, buf, len)
5937 win_T *wp;
5938 char_u *buf; /* buffer for the result */
5939 int len; /* length of buffer */
5940{
5941 char_u *p;
5942
5943 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5944 return FALSE;
5945
5946 {
5947#ifdef FEAT_EVAL
5948 buf_T *old_curbuf = curbuf;
5949 win_T *old_curwin = curwin;
5950 char_u *s;
5951
5952 curbuf = wp->w_buffer;
5953 curwin = wp;
5954 STRCPY(buf, "b:keymap_name"); /* must be writable */
5955 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005956 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 --emsg_skip;
5958 curbuf = old_curbuf;
5959 curwin = old_curwin;
5960 if (p == NULL || *p == NUL)
5961#endif
5962 {
5963#ifdef FEAT_KEYMAP
5964 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5965 p = wp->w_buffer->b_p_keymap;
5966 else
5967#endif
5968 p = (char_u *)"lang";
5969 }
5970 if ((int)(STRLEN(p) + 3) < len)
5971 sprintf((char *)buf, "<%s>", p);
5972 else
5973 buf[0] = NUL;
5974#ifdef FEAT_EVAL
5975 vim_free(s);
5976#endif
5977 }
5978 return buf[0] != NUL;
5979}
5980#endif
5981
5982#if defined(FEAT_STL_OPT) || defined(PROTO)
5983/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005984 * Redraw the status line or ruler of window "wp".
5985 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 */
5987 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005988win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005990 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 int attr;
5993 int curattr;
5994 int row;
5995 int col = 0;
5996 int maxwidth;
5997 int width;
5998 int n;
5999 int len;
6000 int fillchar;
6001 char_u buf[MAXPATHL];
6002 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006003 struct stl_hlrec hltab[STL_MAX_ITEM];
6004 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006005 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
6007 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006008 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006010 /* Use 'tabline'. Always at the first line of the screen. */
6011 p = p_tal;
6012 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006013 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006014 attr = hl_attr(HLF_TPF);
6015 maxwidth = Columns;
6016# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006017 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006018# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006020 else
6021 {
6022 row = W_WINROW(wp) + wp->w_height;
6023 fillchar = fillchar_status(&attr, wp == curwin);
6024 maxwidth = W_WIDTH(wp);
6025
6026 if (draw_ruler)
6027 {
6028 p = p_ruf;
6029 /* advance past any leading group spec - implicit in ru_col */
6030 if (*p == '%')
6031 {
6032 if (*++p == '-')
6033 p++;
6034 if (atoi((char *) p))
6035 while (VIM_ISDIGIT(*p))
6036 p++;
6037 if (*p++ != '(')
6038 p = p_ruf;
6039 }
6040#ifdef FEAT_VERTSPLIT
6041 col = ru_col - (Columns - W_WIDTH(wp));
6042 if (col < (W_WIDTH(wp) + 1) / 2)
6043 col = (W_WIDTH(wp) + 1) / 2;
6044#else
6045 col = ru_col;
6046 if (col > (Columns + 1) / 2)
6047 col = (Columns + 1) / 2;
6048#endif
6049 maxwidth = W_WIDTH(wp) - col;
6050#ifdef FEAT_WINDOWS
6051 if (!wp->w_status_height)
6052#endif
6053 {
6054 row = Rows - 1;
6055 --maxwidth; /* writing in last column may cause scrolling */
6056 fillchar = ' ';
6057 attr = 0;
6058 }
6059
6060# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006061 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006062# endif
6063 }
6064 else
6065 {
6066 if (*wp->w_p_stl != NUL)
6067 p = wp->w_p_stl;
6068 else
6069 p = p_stl;
6070# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006071 use_sandbox = was_set_insecurely((char_u *)"statusline",
6072 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006073# endif
6074 }
6075
6076#ifdef FEAT_VERTSPLIT
6077 col += W_WINCOL(wp);
6078#endif
6079 }
6080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (maxwidth <= 0)
6082 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006084 width = build_stl_str_hl(wp == NULL ? curwin : wp,
6085 buf, sizeof(buf),
6086 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006087 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006088 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089
6090 while (width < maxwidth && len < sizeof(buf) - 1)
6091 {
6092#ifdef FEAT_MBYTE
6093 len += (*mb_char2bytes)(fillchar, buf + len);
6094#else
6095 buf[len++] = fillchar;
6096#endif
6097 ++width;
6098 }
6099 buf[len] = NUL;
6100
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006101 /*
6102 * Draw each snippet with the specified highlighting.
6103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 curattr = attr;
6105 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006106 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006108 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 screen_puts_len(p, len, row, col, curattr);
6110 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006111 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006113 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006115 else if (hltab[n].userhl < 0)
6116 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006118 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006119 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120#endif
6121 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006122 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 }
6124 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006125
6126 if (wp == NULL)
6127 {
6128 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6129 col = 0;
6130 len = 0;
6131 p = buf;
6132 fillchar = 0;
6133 for (n = 0; tabtab[n].start != NULL; n++)
6134 {
6135 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6136 while (col < len)
6137 TabPageIdxs[col++] = fillchar;
6138 p = tabtab[n].start;
6139 fillchar = tabtab[n].userhl;
6140 }
6141 while (col < Columns)
6142 TabPageIdxs[col++] = fillchar;
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144}
6145
6146#endif /* FEAT_STL_OPT */
6147
6148/*
6149 * Output a single character directly to the screen and update ScreenLines.
6150 */
6151 void
6152screen_putchar(c, row, col, attr)
6153 int c;
6154 int row, col;
6155 int attr;
6156{
6157#ifdef FEAT_MBYTE
6158 char_u buf[MB_MAXBYTES + 1];
6159
6160 buf[(*mb_char2bytes)(c, buf)] = NUL;
6161#else
6162 char_u buf[2];
6163
6164 buf[0] = c;
6165 buf[1] = NUL;
6166#endif
6167 screen_puts(buf, row, col, attr);
6168}
6169
6170/*
6171 * Get a single character directly from ScreenLines into "bytes[]".
6172 * Also return its attribute in *attrp;
6173 */
6174 void
6175screen_getbytes(row, col, bytes, attrp)
6176 int row, col;
6177 char_u *bytes;
6178 int *attrp;
6179{
6180 unsigned off;
6181
6182 /* safety check */
6183 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6184 {
6185 off = LineOffset[row] + col;
6186 *attrp = ScreenAttrs[off];
6187 bytes[0] = ScreenLines[off];
6188 bytes[1] = NUL;
6189
6190#ifdef FEAT_MBYTE
6191 if (enc_utf8 && ScreenLinesUC[off] != 0)
6192 bytes[utfc_char2bytes(off, bytes)] = NUL;
6193 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6194 {
6195 bytes[0] = ScreenLines[off];
6196 bytes[1] = ScreenLines2[off];
6197 bytes[2] = NUL;
6198 }
6199 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6200 {
6201 bytes[1] = ScreenLines[off + 1];
6202 bytes[2] = NUL;
6203 }
6204#endif
6205 }
6206}
6207
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006208#ifdef FEAT_MBYTE
6209static int screen_comp_differs __ARGS((int, int*));
6210
6211/*
6212 * Return TRUE if composing characters for screen posn "off" differs from
6213 * composing characters in "u8cc".
6214 */
6215 static int
6216screen_comp_differs(off, u8cc)
6217 int off;
6218 int *u8cc;
6219{
6220 int i;
6221
6222 for (i = 0; i < Screen_mco; ++i)
6223 {
6224 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6225 return TRUE;
6226 if (u8cc[i] == 0)
6227 break;
6228 }
6229 return FALSE;
6230}
6231#endif
6232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233/*
6234 * Put string '*text' on the screen at position 'row' and 'col', with
6235 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6236 * Note: only outputs within one row, message is truncated at screen boundary!
6237 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6238 */
6239 void
6240screen_puts(text, row, col, attr)
6241 char_u *text;
6242 int row;
6243 int col;
6244 int attr;
6245{
6246 screen_puts_len(text, -1, row, col, attr);
6247}
6248
6249/*
6250 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6251 * a NUL.
6252 */
6253 void
6254screen_puts_len(text, len, row, col, attr)
6255 char_u *text;
6256 int len;
6257 int row;
6258 int col;
6259 int attr;
6260{
6261 unsigned off;
6262 char_u *ptr = text;
6263 int c;
6264#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006265 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 int mbyte_blen = 1;
6267 int mbyte_cells = 1;
6268 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006269 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 int clear_next_cell = FALSE;
6271# ifdef FEAT_ARABIC
6272 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006273 int pc, nc, nc1;
6274 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275# endif
6276#endif
6277
6278 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6279 return;
6280
Bram Moolenaarc236c162008-07-13 17:41:49 +00006281#ifdef FEAT_MBYTE
6282 /* When drawing over the right halve of a double-wide char clear out the
6283 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006284 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006285# ifdef FEAT_GUI
6286 && !gui.in_use
6287# endif
6288 && mb_fix_col(col, row) != col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00006289 screen_puts_len((char_u *)" ", 1, row, col - 1, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00006290#endif
6291
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 off = LineOffset[row] + col;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006293#ifdef FEAT_MBYTE
6294 max_off = LineOffset[row] + screen_Columns;
6295#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006296 while (col < screen_Columns
6297 && (len < 0 || (int)(ptr - text) < len)
6298 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
6300 c = *ptr;
6301#ifdef FEAT_MBYTE
6302 /* check if this is the first byte of a multibyte */
6303 if (has_mbyte)
6304 {
6305 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006306 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006308 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6310 mbyte_cells = 1;
6311 else if (enc_dbcs != 0)
6312 mbyte_cells = mbyte_blen;
6313 else /* enc_utf8 */
6314 {
6315 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006316 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 (int)((text + len) - ptr));
6318 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006319 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00006321# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 /* Non-BMP character: display as ? or fullwidth ?. */
6323 if (u8c >= 0x10000)
6324 {
6325 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6326 if (attr == 0)
6327 attr = hl_attr(HLF_8);
6328 }
Bram Moolenaar11936362007-09-17 20:39:42 +00006329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330# ifdef FEAT_ARABIC
6331 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6332 {
6333 /* Do Arabic shaping. */
6334 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6335 {
6336 /* Past end of string to be displayed. */
6337 nc = NUL;
6338 nc1 = NUL;
6339 }
6340 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006341 {
6342 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6343 nc1 = pcc[0];
6344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 pc = prev_c;
6346 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006347 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 }
6349 else
6350 prev_c = u8c;
6351# endif
6352 }
6353 }
6354#endif
6355
6356 if (ScreenLines[off] != c
6357#ifdef FEAT_MBYTE
6358 || (mbyte_cells == 2
6359 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6360 || (enc_dbcs == DBCS_JPNU
6361 && c == 0x8e
6362 && ScreenLines2[off] != ptr[1])
6363 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006364 && (ScreenLinesUC[off] != (u8char_T)u8c
6365 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366#endif
6367 || ScreenAttrs[off] != attr
6368 || exmode_active
6369 )
6370 {
6371#if defined(FEAT_GUI) || defined(UNIX)
6372 /* The bold trick makes a single row of pixels appear in the next
6373 * character. When a bold character is removed, the next
6374 * character should be redrawn too. This happens for our own GUI
6375 * and for some xterms.
6376 * Force the redraw by setting the attribute to a different value
6377 * than "attr", the contents of ScreenLines[] may be needed by
6378 * mb_off2cells() further on.
6379 * Don't do this for the last drawn character, because the next
6380 * character may not be redrawn. */
6381 if (
6382# ifdef FEAT_GUI
6383 gui.in_use
6384# endif
6385# if defined(FEAT_GUI) && defined(UNIX)
6386 ||
6387# endif
6388# ifdef UNIX
6389 term_is_xterm
6390# endif
6391 )
6392 {
6393 int n;
6394
6395 n = ScreenAttrs[off];
6396# ifdef FEAT_MBYTE
6397 if (col + mbyte_cells < screen_Columns
6398 && (n > HL_ALL || (n & HL_BOLD))
6399 && (len < 0 ? ptr[mbyte_blen] != NUL
6400 : ptr + mbyte_blen < text + len))
6401 ScreenAttrs[off + mbyte_cells] = attr + 1;
6402# else
6403 if (col + 1 < screen_Columns
6404 && (n > HL_ALL || (n & HL_BOLD))
6405 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6406 ScreenLines[off + 1] = 0;
6407# endif
6408 }
6409#endif
6410#ifdef FEAT_MBYTE
6411 /* When at the end of the text and overwriting a two-cell
6412 * character with a one-cell character, need to clear the next
6413 * cell. Also when overwriting the left halve of a two-cell char
6414 * with the right halve of a two-cell char. Do this only once
6415 * (mb_off2cells() may return 2 on the right halve). */
6416 if (clear_next_cell)
6417 clear_next_cell = FALSE;
6418 else if (has_mbyte
6419 && (len < 0 ? ptr[mbyte_blen] == NUL
6420 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00006421 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006423 && (*mb_off2cells)(off, max_off) == 1
6424 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 clear_next_cell = TRUE;
6426
6427 /* Make sure we never leave a second byte of a double-byte behind,
6428 * it confuses mb_off2cells(). */
6429 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00006430 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006432 && (*mb_off2cells)(off, max_off) == 1
6433 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 ScreenLines[off + mbyte_blen] = 0;
6435#endif
6436 ScreenLines[off] = c;
6437 ScreenAttrs[off] = attr;
6438#ifdef FEAT_MBYTE
6439 if (enc_utf8)
6440 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006441 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 ScreenLinesUC[off] = 0;
6443 else
6444 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006445 int i;
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006448 for (i = 0; i < Screen_mco; ++i)
6449 {
6450 ScreenLinesC[i][off] = u8cc[i];
6451 if (u8cc[i] == 0)
6452 break;
6453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
6455 if (mbyte_cells == 2)
6456 {
6457 ScreenLines[off + 1] = 0;
6458 ScreenAttrs[off + 1] = attr;
6459 }
6460 screen_char(off, row, col);
6461 }
6462 else if (mbyte_cells == 2)
6463 {
6464 ScreenLines[off + 1] = ptr[1];
6465 ScreenAttrs[off + 1] = attr;
6466 screen_char_2(off, row, col);
6467 }
6468 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6469 {
6470 ScreenLines2[off] = ptr[1];
6471 screen_char(off, row, col);
6472 }
6473 else
6474#endif
6475 screen_char(off, row, col);
6476 }
6477#ifdef FEAT_MBYTE
6478 if (has_mbyte)
6479 {
6480 off += mbyte_cells;
6481 col += mbyte_cells;
6482 ptr += mbyte_blen;
6483 if (clear_next_cell)
6484 ptr = (char_u *)" ";
6485 }
6486 else
6487#endif
6488 {
6489 ++off;
6490 ++col;
6491 ++ptr;
6492 }
6493 }
6494}
6495
6496#ifdef FEAT_SEARCH_EXTRA
6497/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006498 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 */
6500 static void
6501start_search_hl()
6502{
6503 if (p_hls && !no_hlsearch)
6504 {
6505 last_pat_prog(&search_hl.rm);
6506 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006507# ifdef FEAT_RELTIME
6508 /* Set the time limit to 'redrawtime'. */
6509 profile_setlimit(p_rdt, &search_hl.tm);
6510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 }
6512}
6513
6514/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006515 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 */
6517 static void
6518end_search_hl()
6519{
6520 if (search_hl.rm.regprog != NULL)
6521 {
6522 vim_free(search_hl.rm.regprog);
6523 search_hl.rm.regprog = NULL;
6524 }
6525}
6526
6527/*
6528 * Advance to the match in window "wp" line "lnum" or past it.
6529 */
6530 static void
6531prepare_search_hl(wp, lnum)
6532 win_T *wp;
6533 linenr_T lnum;
6534{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006535 matchitem_T *cur; /* points to the match list */
6536 match_T *shl; /* points to search_hl or a match */
6537 int shl_flag; /* flag to indicate whether search_hl
6538 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 int n;
6540
6541 /*
6542 * When using a multi-line pattern, start searching at the top
6543 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006544 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006546 cur = wp->w_match_head;
6547 shl_flag = FALSE;
6548 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006550 if (shl_flag == FALSE)
6551 {
6552 shl = &search_hl;
6553 shl_flag = TRUE;
6554 }
6555 else
6556 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 if (shl->rm.regprog != NULL
6558 && shl->lnum == 0
6559 && re_multiline(shl->rm.regprog))
6560 {
6561 if (shl->first_lnum == 0)
6562 {
6563# ifdef FEAT_FOLDING
6564 for (shl->first_lnum = lnum;
6565 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6566 if (hasFoldingWin(wp, shl->first_lnum - 1,
6567 NULL, NULL, TRUE, NULL))
6568 break;
6569# else
6570 shl->first_lnum = wp->w_topline;
6571# endif
6572 }
6573 n = 0;
6574 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6575 {
6576 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6577 if (shl->lnum != 0)
6578 {
6579 shl->first_lnum = shl->lnum
6580 + shl->rm.endpos[0].lnum
6581 - shl->rm.startpos[0].lnum;
6582 n = shl->rm.endpos[0].col;
6583 }
6584 else
6585 {
6586 ++shl->first_lnum;
6587 n = 0;
6588 }
6589 }
6590 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006591 if (shl != &search_hl && cur != NULL)
6592 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594}
6595
6596/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006597 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 * Uses shl->buf.
6599 * Sets shl->lnum and shl->rm contents.
6600 * Note: Assumes a previous match is always before "lnum", unless
6601 * shl->lnum is zero.
6602 * Careful: Any pointers for buffer lines will become invalid.
6603 */
6604 static void
6605next_search_hl(win, shl, lnum, mincol)
6606 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006607 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 linenr_T lnum;
6609 colnr_T mincol; /* minimal column for a match */
6610{
6611 linenr_T l;
6612 colnr_T matchcol;
6613 long nmatched;
6614
6615 if (shl->lnum != 0)
6616 {
6617 /* Check for three situations:
6618 * 1. If the "lnum" is below a previous match, start a new search.
6619 * 2. If the previous match includes "mincol", use it.
6620 * 3. Continue after the previous match.
6621 */
6622 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6623 if (lnum > l)
6624 shl->lnum = 0;
6625 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6626 return;
6627 }
6628
6629 /*
6630 * Repeat searching for a match until one is found that includes "mincol"
6631 * or none is found in this line.
6632 */
6633 called_emsg = FALSE;
6634 for (;;)
6635 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006636#ifdef FEAT_RELTIME
6637 /* Stop searching after passing the time limit. */
6638 if (profile_passed_limit(&(shl->tm)))
6639 {
6640 shl->lnum = 0; /* no match found in time */
6641 break;
6642 }
6643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 /* Three situations:
6645 * 1. No useful previous match: search from start of line.
6646 * 2. Not Vi compatible or empty match: continue at next character.
6647 * Break the loop if this is beyond the end of the line.
6648 * 3. Vi compatible searching: continue at end of previous match.
6649 */
6650 if (shl->lnum == 0)
6651 matchcol = 0;
6652 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6653 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006654 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006656 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006657
6658 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006659 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006660 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006662 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 shl->lnum = 0;
6664 break;
6665 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006666#ifdef FEAT_MBYTE
6667 if (has_mbyte)
6668 matchcol += mb_ptr2len(ml);
6669 else
6670#endif
6671 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
6673 else
6674 matchcol = shl->rm.endpos[0].col;
6675
6676 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006677 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
6678#ifdef FEAT_RELTIME
6679 &(shl->tm)
6680#else
6681 NULL
6682#endif
6683 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 if (called_emsg)
6685 {
6686 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006687 if (shl == &search_hl)
6688 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006689 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006690 vim_free(shl->rm.regprog);
6691 no_hlsearch = TRUE;
6692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00006694 shl->lnum = 0;
6695 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 break;
6697 }
6698 if (nmatched == 0)
6699 {
6700 shl->lnum = 0; /* no match found */
6701 break;
6702 }
6703 if (shl->rm.startpos[0].lnum > 0
6704 || shl->rm.startpos[0].col >= mincol
6705 || nmatched > 1
6706 || shl->rm.endpos[0].col > mincol)
6707 {
6708 shl->lnum += shl->rm.startpos[0].lnum;
6709 break; /* useful match found */
6710 }
6711 }
6712}
6713#endif
6714
6715 static void
6716screen_start_highlight(attr)
6717 int attr;
6718{
6719 attrentry_T *aep = NULL;
6720
6721 screen_attr = attr;
6722 if (full_screen
6723#ifdef WIN3264
6724 && termcap_active
6725#endif
6726 )
6727 {
6728#ifdef FEAT_GUI
6729 if (gui.in_use)
6730 {
6731 char buf[20];
6732
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006733 /* The GUI handles this internally. */
6734 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 OUT_STR(buf);
6736 }
6737 else
6738#endif
6739 {
6740 if (attr > HL_ALL) /* special HL attr. */
6741 {
6742 if (t_colors > 1)
6743 aep = syn_cterm_attr2entry(attr);
6744 else
6745 aep = syn_term_attr2entry(attr);
6746 if (aep == NULL) /* did ":syntax clear" */
6747 attr = 0;
6748 else
6749 attr = aep->ae_attr;
6750 }
6751 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6752 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006753 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6754 && cterm_normal_fg_bold)
6755 /* If the Normal FG color has BOLD attribute and the new HL
6756 * has a FG color defined, clear BOLD. */
6757 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6759 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006760 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6761 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 out_str(T_US);
6763 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6764 out_str(T_CZH);
6765 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6766 out_str(T_MR);
6767
6768 /*
6769 * Output the color or start string after bold etc., in case the
6770 * bold etc. override the color setting.
6771 */
6772 if (aep != NULL)
6773 {
6774 if (t_colors > 1)
6775 {
6776 if (aep->ae_u.cterm.fg_color)
6777 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6778 if (aep->ae_u.cterm.bg_color)
6779 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6780 }
6781 else
6782 {
6783 if (aep->ae_u.term.start != NULL)
6784 out_str(aep->ae_u.term.start);
6785 }
6786 }
6787 }
6788 }
6789}
6790
6791 void
6792screen_stop_highlight()
6793{
6794 int do_ME = FALSE; /* output T_ME code */
6795
6796 if (screen_attr != 0
6797#ifdef WIN3264
6798 && termcap_active
6799#endif
6800 )
6801 {
6802#ifdef FEAT_GUI
6803 if (gui.in_use)
6804 {
6805 char buf[20];
6806
6807 /* use internal GUI code */
6808 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6809 OUT_STR(buf);
6810 }
6811 else
6812#endif
6813 {
6814 if (screen_attr > HL_ALL) /* special HL attr. */
6815 {
6816 attrentry_T *aep;
6817
6818 if (t_colors > 1)
6819 {
6820 /*
6821 * Assume that t_me restores the original colors!
6822 */
6823 aep = syn_cterm_attr2entry(screen_attr);
6824 if (aep != NULL && (aep->ae_u.cterm.fg_color
6825 || aep->ae_u.cterm.bg_color))
6826 do_ME = TRUE;
6827 }
6828 else
6829 {
6830 aep = syn_term_attr2entry(screen_attr);
6831 if (aep != NULL && aep->ae_u.term.stop != NULL)
6832 {
6833 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6834 do_ME = TRUE;
6835 else
6836 out_str(aep->ae_u.term.stop);
6837 }
6838 }
6839 if (aep == NULL) /* did ":syntax clear" */
6840 screen_attr = 0;
6841 else
6842 screen_attr = aep->ae_attr;
6843 }
6844
6845 /*
6846 * Often all ending-codes are equal to T_ME. Avoid outputting the
6847 * same sequence several times.
6848 */
6849 if (screen_attr & HL_STANDOUT)
6850 {
6851 if (STRCMP(T_SE, T_ME) == 0)
6852 do_ME = TRUE;
6853 else
6854 out_str(T_SE);
6855 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006856 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 {
6858 if (STRCMP(T_UE, T_ME) == 0)
6859 do_ME = TRUE;
6860 else
6861 out_str(T_UE);
6862 }
6863 if (screen_attr & HL_ITALIC)
6864 {
6865 if (STRCMP(T_CZR, T_ME) == 0)
6866 do_ME = TRUE;
6867 else
6868 out_str(T_CZR);
6869 }
6870 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6871 out_str(T_ME);
6872
6873 if (t_colors > 1)
6874 {
6875 /* set Normal cterm colors */
6876 if (cterm_normal_fg_color != 0)
6877 term_fg_color(cterm_normal_fg_color - 1);
6878 if (cterm_normal_bg_color != 0)
6879 term_bg_color(cterm_normal_bg_color - 1);
6880 if (cterm_normal_fg_bold)
6881 out_str(T_MD);
6882 }
6883 }
6884 }
6885 screen_attr = 0;
6886}
6887
6888/*
6889 * Reset the colors for a cterm. Used when leaving Vim.
6890 * The machine specific code may override this again.
6891 */
6892 void
6893reset_cterm_colors()
6894{
6895 if (t_colors > 1)
6896 {
6897 /* set Normal cterm colors */
6898 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6899 {
6900 out_str(T_OP);
6901 screen_attr = -1;
6902 }
6903 if (cterm_normal_fg_bold)
6904 {
6905 out_str(T_ME);
6906 screen_attr = -1;
6907 }
6908 }
6909}
6910
6911/*
6912 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6913 * using the attributes from ScreenAttrs["off"].
6914 */
6915 static void
6916screen_char(off, row, col)
6917 unsigned off;
6918 int row;
6919 int col;
6920{
6921 int attr;
6922
6923 /* Check for illegal values, just in case (could happen just after
6924 * resizing). */
6925 if (row >= screen_Rows || col >= screen_Columns)
6926 return;
6927
6928 /* Outputting the last character on the screen may scrollup the screen.
6929 * Don't to it! Mark the character invalid (update it when scrolled up) */
6930 if (row == screen_Rows - 1 && col == screen_Columns - 1
6931#ifdef FEAT_RIGHTLEFT
6932 /* account for first command-line character in rightleft mode */
6933 && !cmdmsg_rl
6934#endif
6935 )
6936 {
6937 ScreenAttrs[off] = (sattr_T)-1;
6938 return;
6939 }
6940
6941 /*
6942 * Stop highlighting first, so it's easier to move the cursor.
6943 */
6944#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6945 if (screen_char_attr != 0)
6946 attr = screen_char_attr;
6947 else
6948#endif
6949 attr = ScreenAttrs[off];
6950 if (screen_attr != attr)
6951 screen_stop_highlight();
6952
6953 windgoto(row, col);
6954
6955 if (screen_attr != attr)
6956 screen_start_highlight(attr);
6957
6958#ifdef FEAT_MBYTE
6959 if (enc_utf8 && ScreenLinesUC[off] != 0)
6960 {
6961 char_u buf[MB_MAXBYTES + 1];
6962
6963 /* Convert UTF-8 character to bytes and write it. */
6964
6965 buf[utfc_char2bytes(off, buf)] = NUL;
6966
6967 out_str(buf);
6968 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6969 ++screen_cur_col;
6970 }
6971 else
6972#endif
6973 {
6974#ifdef FEAT_MBYTE
6975 out_flush_check();
6976#endif
6977 out_char(ScreenLines[off]);
6978#ifdef FEAT_MBYTE
6979 /* double-byte character in single-width cell */
6980 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6981 out_char(ScreenLines2[off]);
6982#endif
6983 }
6984
6985 screen_cur_col++;
6986}
6987
6988#ifdef FEAT_MBYTE
6989
6990/*
6991 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6992 * on the screen at position 'row' and 'col'.
6993 * The attributes of the first byte is used for all. This is required to
6994 * output the two bytes of a double-byte character with nothing in between.
6995 */
6996 static void
6997screen_char_2(off, row, col)
6998 unsigned off;
6999 int row;
7000 int col;
7001{
7002 /* Check for illegal values (could be wrong when screen was resized). */
7003 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7004 return;
7005
7006 /* Outputting the last character on the screen may scrollup the screen.
7007 * Don't to it! Mark the character invalid (update it when scrolled up) */
7008 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7009 {
7010 ScreenAttrs[off] = (sattr_T)-1;
7011 return;
7012 }
7013
7014 /* Output the first byte normally (positions the cursor), then write the
7015 * second byte directly. */
7016 screen_char(off, row, col);
7017 out_char(ScreenLines[off + 1]);
7018 ++screen_cur_col;
7019}
7020#endif
7021
7022#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7023/*
7024 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7025 * This uses the contents of ScreenLines[] and doesn't change it.
7026 */
7027 void
7028screen_draw_rectangle(row, col, height, width, invert)
7029 int row;
7030 int col;
7031 int height;
7032 int width;
7033 int invert;
7034{
7035 int r, c;
7036 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007037#ifdef FEAT_MBYTE
7038 int max_off;
7039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007041 /* Can't use ScreenLines unless initialized */
7042 if (ScreenLines == NULL)
7043 return;
7044
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 if (invert)
7046 screen_char_attr = HL_INVERSE;
7047 for (r = row; r < row + height; ++r)
7048 {
7049 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007050#ifdef FEAT_MBYTE
7051 max_off = off + screen_Columns;
7052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 for (c = col; c < col + width; ++c)
7054 {
7055#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007056 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
7058 screen_char_2(off + c, r, c);
7059 ++c;
7060 }
7061 else
7062#endif
7063 {
7064 screen_char(off + c, r, c);
7065#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007066 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 ++c;
7068#endif
7069 }
7070 }
7071 }
7072 screen_char_attr = 0;
7073}
7074#endif
7075
7076#ifdef FEAT_VERTSPLIT
7077/*
7078 * Redraw the characters for a vertically split window.
7079 */
7080 static void
7081redraw_block(row, end, wp)
7082 int row;
7083 int end;
7084 win_T *wp;
7085{
7086 int col;
7087 int width;
7088
7089# ifdef FEAT_CLIPBOARD
7090 clip_may_clear_selection(row, end - 1);
7091# endif
7092
7093 if (wp == NULL)
7094 {
7095 col = 0;
7096 width = Columns;
7097 }
7098 else
7099 {
7100 col = wp->w_wincol;
7101 width = wp->w_width;
7102 }
7103 screen_draw_rectangle(row, col, end - row, width, FALSE);
7104}
7105#endif
7106
7107/*
7108 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7109 * with character 'c1' in first column followed by 'c2' in the other columns.
7110 * Use attributes 'attr'.
7111 */
7112 void
7113screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7114 int start_row, end_row;
7115 int start_col, end_col;
7116 int c1, c2;
7117 int attr;
7118{
7119 int row;
7120 int col;
7121 int off;
7122 int end_off;
7123 int did_delete;
7124 int c;
7125 int norm_term;
7126#if defined(FEAT_GUI) || defined(UNIX)
7127 int force_next = FALSE;
7128#endif
7129
7130 if (end_row > screen_Rows) /* safety check */
7131 end_row = screen_Rows;
7132 if (end_col > screen_Columns) /* safety check */
7133 end_col = screen_Columns;
7134 if (ScreenLines == NULL
7135 || start_row >= end_row
7136 || start_col >= end_col) /* nothing to do */
7137 return;
7138
7139 /* it's a "normal" terminal when not in a GUI or cterm */
7140 norm_term = (
7141#ifdef FEAT_GUI
7142 !gui.in_use &&
7143#endif
7144 t_colors <= 1);
7145 for (row = start_row; row < end_row; ++row)
7146 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007147#ifdef FEAT_MBYTE
7148 if (has_mbyte
7149# ifdef FEAT_GUI
7150 && !gui.in_use
7151# endif
7152 )
7153 {
7154 /* When drawing over the right halve of a double-wide char clear
7155 * out the left halve. When drawing over the left halve of a
7156 * double wide-char clear out the right halve. Only needed in a
7157 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007158 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007159 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007160 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007161 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007162 }
7163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 /*
7165 * Try to use delete-line termcap code, when no attributes or in a
7166 * "normal" terminal, where a bold/italic space is just a
7167 * space.
7168 */
7169 did_delete = FALSE;
7170 if (c2 == ' '
7171 && end_col == Columns
7172 && can_clear(T_CE)
7173 && (attr == 0
7174 || (norm_term
7175 && attr <= HL_ALL
7176 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7177 {
7178 /*
7179 * check if we really need to clear something
7180 */
7181 col = start_col;
7182 if (c1 != ' ') /* don't clear first char */
7183 ++col;
7184
7185 off = LineOffset[row] + col;
7186 end_off = LineOffset[row] + end_col;
7187
7188 /* skip blanks (used often, keep it fast!) */
7189#ifdef FEAT_MBYTE
7190 if (enc_utf8)
7191 while (off < end_off && ScreenLines[off] == ' '
7192 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7193 ++off;
7194 else
7195#endif
7196 while (off < end_off && ScreenLines[off] == ' '
7197 && ScreenAttrs[off] == 0)
7198 ++off;
7199 if (off < end_off) /* something to be cleared */
7200 {
7201 col = off - LineOffset[row];
7202 screen_stop_highlight();
7203 term_windgoto(row, col);/* clear rest of this screen line */
7204 out_str(T_CE);
7205 screen_start(); /* don't know where cursor is now */
7206 col = end_col - col;
7207 while (col--) /* clear chars in ScreenLines */
7208 {
7209 ScreenLines[off] = ' ';
7210#ifdef FEAT_MBYTE
7211 if (enc_utf8)
7212 ScreenLinesUC[off] = 0;
7213#endif
7214 ScreenAttrs[off] = 0;
7215 ++off;
7216 }
7217 }
7218 did_delete = TRUE; /* the chars are cleared now */
7219 }
7220
7221 off = LineOffset[row] + start_col;
7222 c = c1;
7223 for (col = start_col; col < end_col; ++col)
7224 {
7225 if (ScreenLines[off] != c
7226#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007227 || (enc_utf8 && (int)ScreenLinesUC[off]
7228 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229#endif
7230 || ScreenAttrs[off] != attr
7231#if defined(FEAT_GUI) || defined(UNIX)
7232 || force_next
7233#endif
7234 )
7235 {
7236#if defined(FEAT_GUI) || defined(UNIX)
7237 /* The bold trick may make a single row of pixels appear in
7238 * the next character. When a bold character is removed, the
7239 * next character should be redrawn too. This happens for our
7240 * own GUI and for some xterms. */
7241 if (
7242# ifdef FEAT_GUI
7243 gui.in_use
7244# endif
7245# if defined(FEAT_GUI) && defined(UNIX)
7246 ||
7247# endif
7248# ifdef UNIX
7249 term_is_xterm
7250# endif
7251 )
7252 {
7253 if (ScreenLines[off] != ' '
7254 && (ScreenAttrs[off] > HL_ALL
7255 || ScreenAttrs[off] & HL_BOLD))
7256 force_next = TRUE;
7257 else
7258 force_next = FALSE;
7259 }
7260#endif
7261 ScreenLines[off] = c;
7262#ifdef FEAT_MBYTE
7263 if (enc_utf8)
7264 {
7265 if (c >= 0x80)
7266 {
7267 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007268 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270 else
7271 ScreenLinesUC[off] = 0;
7272 }
7273#endif
7274 ScreenAttrs[off] = attr;
7275 if (!did_delete || c != ' ')
7276 screen_char(off, row, col);
7277 }
7278 ++off;
7279 if (col == start_col)
7280 {
7281 if (did_delete)
7282 break;
7283 c = c2;
7284 }
7285 }
7286 if (end_col == Columns)
7287 LineWraps[row] = FALSE;
7288 if (row == Rows - 1) /* overwritten the command line */
7289 {
7290 redraw_cmdline = TRUE;
7291 if (c1 == ' ' && c2 == ' ')
7292 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007293 if (start_col == 0)
7294 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 }
7296 }
7297}
7298
7299/*
7300 * Check if there should be a delay. Used before clearing or redrawing the
7301 * screen or the command line.
7302 */
7303 void
7304check_for_delay(check_msg_scroll)
7305 int check_msg_scroll;
7306{
7307 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7308 && !did_wait_return
7309 && emsg_silent == 0)
7310 {
7311 out_flush();
7312 ui_delay(1000L, TRUE);
7313 emsg_on_display = FALSE;
7314 if (check_msg_scroll)
7315 msg_scroll = FALSE;
7316 }
7317}
7318
7319/*
7320 * screen_valid - allocate screen buffers if size changed
7321 * If "clear" is TRUE: clear screen if it has been resized.
7322 * Returns TRUE if there is a valid screen to write to.
7323 * Returns FALSE when starting up and screen not initialized yet.
7324 */
7325 int
7326screen_valid(clear)
7327 int clear;
7328{
7329 screenalloc(clear); /* allocate screen buffers if size changed */
7330 return (ScreenLines != NULL);
7331}
7332
7333/*
7334 * Resize the shell to Rows and Columns.
7335 * Allocate ScreenLines[] and associated items.
7336 *
7337 * There may be some time between setting Rows and Columns and (re)allocating
7338 * ScreenLines[]. This happens when starting up and when (manually) changing
7339 * the shell size. Always use screen_Rows and screen_Columns to access items
7340 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7341 * final size of the shell is needed.
7342 */
7343 void
7344screenalloc(clear)
7345 int clear;
7346{
7347 int new_row, old_row;
7348#ifdef FEAT_GUI
7349 int old_Rows;
7350#endif
7351 win_T *wp;
7352 int outofmem = FALSE;
7353 int len;
7354 schar_T *new_ScreenLines;
7355#ifdef FEAT_MBYTE
7356 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007357 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007359 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360#endif
7361 sattr_T *new_ScreenAttrs;
7362 unsigned *new_LineOffset;
7363 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007364#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007365 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007366 tabpage_T *tp;
7367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007369 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
7371 /*
7372 * Allocation of the screen buffers is done only when the size changes and
7373 * when Rows and Columns have been set and we have started doing full
7374 * screen stuff.
7375 */
7376 if ((ScreenLines != NULL
7377 && Rows == screen_Rows
7378 && Columns == screen_Columns
7379#ifdef FEAT_MBYTE
7380 && enc_utf8 == (ScreenLinesUC != NULL)
7381 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007382 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383#endif
7384 )
7385 || Rows == 0
7386 || Columns == 0
7387 || (!full_screen && ScreenLines == NULL))
7388 return;
7389
7390 /*
7391 * It's possible that we produce an out-of-memory message below, which
7392 * will cause this function to be called again. To break the loop, just
7393 * return here.
7394 */
7395 if (entered)
7396 return;
7397 entered = TRUE;
7398
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007399 /*
7400 * Note that the window sizes are updated before reallocating the arrays,
7401 * thus we must not redraw here!
7402 */
7403 ++RedrawingDisabled;
7404
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 win_new_shellsize(); /* fit the windows in the new sized shell */
7406
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 comp_col(); /* recompute columns for shown command and ruler */
7408
7409 /*
7410 * We're changing the size of the screen.
7411 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7412 * - Move lines from the old arrays into the new arrays, clear extra
7413 * lines (unless the screen is going to be cleared).
7414 * - Free the old arrays.
7415 *
7416 * If anything fails, make ScreenLines NULL, so we don't do anything!
7417 * Continuing with the old ScreenLines may result in a crash, because the
7418 * size is wrong.
7419 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007420 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
7423 new_ScreenLines = (schar_T *)lalloc((long_u)(
7424 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7425#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007426 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 if (enc_utf8)
7428 {
7429 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7430 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007431 for (i = 0; i < p_mco; ++i)
7432 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7434 }
7435 if (enc_dbcs == DBCS_JPNU)
7436 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7437 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7438#endif
7439 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7440 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7441 new_LineOffset = (unsigned *)lalloc((long_u)(
7442 Rows * sizeof(unsigned)), FALSE);
7443 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007444#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007445 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007448 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
7450 if (win_alloc_lines(wp) == FAIL)
7451 {
7452 outofmem = TRUE;
7453#ifdef FEAT_WINDOWS
7454 break;
7455#endif
7456 }
7457 }
7458
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007459#ifdef FEAT_MBYTE
7460 for (i = 0; i < p_mco; ++i)
7461 if (new_ScreenLinesC[i] == NULL)
7462 break;
7463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 if (new_ScreenLines == NULL
7465#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007466 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7468#endif
7469 || new_ScreenAttrs == NULL
7470 || new_LineOffset == NULL
7471 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007472#ifdef FEAT_WINDOWS
7473 || new_TabPageIdxs == NULL
7474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 || outofmem)
7476 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007477 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007478 {
7479 /* guess the size */
7480 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7481
7482 /* Remember we did this to avoid getting outofmem messages over
7483 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007484 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 vim_free(new_ScreenLines);
7487 new_ScreenLines = NULL;
7488#ifdef FEAT_MBYTE
7489 vim_free(new_ScreenLinesUC);
7490 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007491 for (i = 0; i < p_mco; ++i)
7492 {
7493 vim_free(new_ScreenLinesC[i]);
7494 new_ScreenLinesC[i] = NULL;
7495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 vim_free(new_ScreenLines2);
7497 new_ScreenLines2 = NULL;
7498#endif
7499 vim_free(new_ScreenAttrs);
7500 new_ScreenAttrs = NULL;
7501 vim_free(new_LineOffset);
7502 new_LineOffset = NULL;
7503 vim_free(new_LineWraps);
7504 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007505#ifdef FEAT_WINDOWS
7506 vim_free(new_TabPageIdxs);
7507 new_TabPageIdxs = NULL;
7508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 else
7511 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007512 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007513
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 for (new_row = 0; new_row < Rows; ++new_row)
7515 {
7516 new_LineOffset[new_row] = new_row * Columns;
7517 new_LineWraps[new_row] = FALSE;
7518
7519 /*
7520 * If the screen is not going to be cleared, copy as much as
7521 * possible from the old screen to the new one and clear the rest
7522 * (used when resizing the window at the "--more--" prompt or when
7523 * executing an external command, for the GUI).
7524 */
7525 if (!clear)
7526 {
7527 (void)vim_memset(new_ScreenLines + new_row * Columns,
7528 ' ', (size_t)Columns * sizeof(schar_T));
7529#ifdef FEAT_MBYTE
7530 if (enc_utf8)
7531 {
7532 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7533 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007534 for (i = 0; i < p_mco; ++i)
7535 (void)vim_memset(new_ScreenLinesC[i]
7536 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 0, (size_t)Columns * sizeof(u8char_T));
7538 }
7539 if (enc_dbcs == DBCS_JPNU)
7540 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7541 0, (size_t)Columns * sizeof(schar_T));
7542#endif
7543 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7544 0, (size_t)Columns * sizeof(sattr_T));
7545 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007546 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 {
7548 if (screen_Columns < Columns)
7549 len = screen_Columns;
7550 else
7551 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007552#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007553 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007554 * may be invalid now. Also when p_mco changes. */
7555 if (!(enc_utf8 && ScreenLinesUC == NULL)
7556 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007557#endif
7558 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7559 ScreenLines + LineOffset[old_row],
7560 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007562 if (enc_utf8 && ScreenLinesUC != NULL
7563 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 {
7565 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7566 ScreenLinesUC + LineOffset[old_row],
7567 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007568 for (i = 0; i < p_mco; ++i)
7569 mch_memmove(new_ScreenLinesC[i]
7570 + new_LineOffset[new_row],
7571 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 (size_t)len * sizeof(u8char_T));
7573 }
7574 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7575 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7576 ScreenLines2 + LineOffset[old_row],
7577 (size_t)len * sizeof(schar_T));
7578#endif
7579 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7580 ScreenAttrs + LineOffset[old_row],
7581 (size_t)len * sizeof(sattr_T));
7582 }
7583 }
7584 }
7585 /* Use the last line of the screen for the current line. */
7586 current_ScreenLine = new_ScreenLines + Rows * Columns;
7587 }
7588
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007589 free_screenlines();
7590
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 ScreenLines = new_ScreenLines;
7592#ifdef FEAT_MBYTE
7593 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007594 for (i = 0; i < p_mco; ++i)
7595 ScreenLinesC[i] = new_ScreenLinesC[i];
7596 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 ScreenLines2 = new_ScreenLines2;
7598#endif
7599 ScreenAttrs = new_ScreenAttrs;
7600 LineOffset = new_LineOffset;
7601 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007602#ifdef FEAT_WINDOWS
7603 TabPageIdxs = new_TabPageIdxs;
7604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605
7606 /* It's important that screen_Rows and screen_Columns reflect the actual
7607 * size of ScreenLines[]. Set them before calling anything. */
7608#ifdef FEAT_GUI
7609 old_Rows = screen_Rows;
7610#endif
7611 screen_Rows = Rows;
7612 screen_Columns = Columns;
7613
7614 must_redraw = CLEAR; /* need to clear the screen later */
7615 if (clear)
7616 screenclear2();
7617
7618#ifdef FEAT_GUI
7619 else if (gui.in_use
7620 && !gui.starting
7621 && ScreenLines != NULL
7622 && old_Rows != Rows)
7623 {
7624 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7625 /*
7626 * Adjust the position of the cursor, for when executing an external
7627 * command.
7628 */
7629 if (msg_row >= Rows) /* Rows got smaller */
7630 msg_row = Rows - 1; /* put cursor at last row */
7631 else if (Rows > old_Rows) /* Rows got bigger */
7632 msg_row += Rows - old_Rows; /* put cursor in same place */
7633 if (msg_col >= Columns) /* Columns got smaller */
7634 msg_col = Columns - 1; /* put cursor at last column */
7635 }
7636#endif
7637
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007639 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007640
7641#ifdef FEAT_AUTOCMD
7642 if (starting == 0)
7643 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645}
7646
7647 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007648free_screenlines()
7649{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007650#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007651 int i;
7652
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007653 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007654 for (i = 0; i < Screen_mco; ++i)
7655 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007656 vim_free(ScreenLines2);
7657#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007658 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007659 vim_free(ScreenAttrs);
7660 vim_free(LineOffset);
7661 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007662#ifdef FEAT_WINDOWS
7663 vim_free(TabPageIdxs);
7664#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007665}
7666
7667 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668screenclear()
7669{
7670 check_for_delay(FALSE);
7671 screenalloc(FALSE); /* allocate screen buffers if size changed */
7672 screenclear2(); /* clear the screen */
7673}
7674
7675 static void
7676screenclear2()
7677{
7678 int i;
7679
7680 if (starting == NO_SCREEN || ScreenLines == NULL
7681#ifdef FEAT_GUI
7682 || (gui.in_use && gui.starting)
7683#endif
7684 )
7685 return;
7686
7687#ifdef FEAT_GUI
7688 if (!gui.in_use)
7689#endif
7690 screen_attr = -1; /* force setting the Normal colors */
7691 screen_stop_highlight(); /* don't want highlighting here */
7692
7693#ifdef FEAT_CLIPBOARD
7694 /* disable selection without redrawing it */
7695 clip_scroll_selection(9999);
7696#endif
7697
7698 /* blank out ScreenLines */
7699 for (i = 0; i < Rows; ++i)
7700 {
7701 lineclear(LineOffset[i], (int)Columns);
7702 LineWraps[i] = FALSE;
7703 }
7704
7705 if (can_clear(T_CL))
7706 {
7707 out_str(T_CL); /* clear the display */
7708 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007709 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 }
7711 else
7712 {
7713 /* can't clear the screen, mark all chars with invalid attributes */
7714 for (i = 0; i < Rows; ++i)
7715 lineinvalid(LineOffset[i], (int)Columns);
7716 clear_cmdline = TRUE;
7717 }
7718
7719 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7720
7721 win_rest_invalid(firstwin);
7722 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007723#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007724 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 if (must_redraw == CLEAR) /* no need to clear again */
7727 must_redraw = NOT_VALID;
7728 compute_cmdrow();
7729 msg_row = cmdline_row; /* put cursor on last line for messages */
7730 msg_col = 0;
7731 screen_start(); /* don't know where cursor is now */
7732 msg_scrolled = 0; /* can't scroll back */
7733 msg_didany = FALSE;
7734 msg_didout = FALSE;
7735}
7736
7737/*
7738 * Clear one line in ScreenLines.
7739 */
7740 static void
7741lineclear(off, width)
7742 unsigned off;
7743 int width;
7744{
7745 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7746#ifdef FEAT_MBYTE
7747 if (enc_utf8)
7748 (void)vim_memset(ScreenLinesUC + off, 0,
7749 (size_t)width * sizeof(u8char_T));
7750#endif
7751 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7752}
7753
7754/*
7755 * Mark one line in ScreenLines invalid by setting the attributes to an
7756 * invalid value.
7757 */
7758 static void
7759lineinvalid(off, width)
7760 unsigned off;
7761 int width;
7762{
7763 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7764}
7765
7766#ifdef FEAT_VERTSPLIT
7767/*
7768 * Copy part of a Screenline for vertically split window "wp".
7769 */
7770 static void
7771linecopy(to, from, wp)
7772 int to;
7773 int from;
7774 win_T *wp;
7775{
7776 unsigned off_to = LineOffset[to] + wp->w_wincol;
7777 unsigned off_from = LineOffset[from] + wp->w_wincol;
7778
7779 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7780 wp->w_width * sizeof(schar_T));
7781# ifdef FEAT_MBYTE
7782 if (enc_utf8)
7783 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007784 int i;
7785
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7787 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007788 for (i = 0; i < p_mco; ++i)
7789 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7790 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 }
7792 if (enc_dbcs == DBCS_JPNU)
7793 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7794 wp->w_width * sizeof(schar_T));
7795# endif
7796 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7797 wp->w_width * sizeof(sattr_T));
7798}
7799#endif
7800
7801/*
7802 * Return TRUE if clearing with term string "p" would work.
7803 * It can't work when the string is empty or it won't set the right background.
7804 */
7805 int
7806can_clear(p)
7807 char_u *p;
7808{
7809 return (*p != NUL && (t_colors <= 1
7810#ifdef FEAT_GUI
7811 || gui.in_use
7812#endif
7813 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7814}
7815
7816/*
7817 * Reset cursor position. Use whenever cursor was moved because of outputting
7818 * something directly to the screen (shell commands) or a terminal control
7819 * code.
7820 */
7821 void
7822screen_start()
7823{
7824 screen_cur_row = screen_cur_col = 9999;
7825}
7826
7827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 * Move the cursor to position "row","col" in the screen.
7829 * This tries to find the most efficient way to move, minimizing the number of
7830 * characters sent to the terminal.
7831 */
7832 void
7833windgoto(row, col)
7834 int row;
7835 int col;
7836{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007837 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 int i;
7839 int plan;
7840 int cost;
7841 int wouldbe_col;
7842 int noinvcurs;
7843 char_u *bs;
7844 int goto_cost;
7845 int attr;
7846
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007847#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7849
7850#define PLAN_LE 1
7851#define PLAN_CR 2
7852#define PLAN_NL 3
7853#define PLAN_WRITE 4
7854 /* Can't use ScreenLines unless initialized */
7855 if (ScreenLines == NULL)
7856 return;
7857
7858 if (col != screen_cur_col || row != screen_cur_row)
7859 {
7860 /* Check for valid position. */
7861 if (row < 0) /* window without text lines? */
7862 row = 0;
7863 if (row >= screen_Rows)
7864 row = screen_Rows - 1;
7865 if (col >= screen_Columns)
7866 col = screen_Columns - 1;
7867
7868 /* check if no cursor movement is allowed in highlight mode */
7869 if (screen_attr && *T_MS == NUL)
7870 noinvcurs = HIGHL_COST;
7871 else
7872 noinvcurs = 0;
7873 goto_cost = GOTO_COST + noinvcurs;
7874
7875 /*
7876 * Plan how to do the positioning:
7877 * 1. Use CR to move it to column 0, same row.
7878 * 2. Use T_LE to move it a few columns to the left.
7879 * 3. Use NL to move a few lines down, column 0.
7880 * 4. Move a few columns to the right with T_ND or by writing chars.
7881 *
7882 * Don't do this if the cursor went beyond the last column, the cursor
7883 * position is unknown then (some terminals wrap, some don't )
7884 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007885 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 * characters to move the cursor to the right.
7887 */
7888 if (row >= screen_cur_row && screen_cur_col < Columns)
7889 {
7890 /*
7891 * If the cursor is in the same row, bigger col, we can use CR
7892 * or T_LE.
7893 */
7894 bs = NULL; /* init for GCC */
7895 attr = screen_attr;
7896 if (row == screen_cur_row && col < screen_cur_col)
7897 {
7898 /* "le" is preferred over "bc", because "bc" is obsolete */
7899 if (*T_LE)
7900 bs = T_LE; /* "cursor left" */
7901 else
7902 bs = T_BC; /* "backspace character (old) */
7903 if (*bs)
7904 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7905 else
7906 cost = 999;
7907 if (col + 1 < cost) /* using CR is less characters */
7908 {
7909 plan = PLAN_CR;
7910 wouldbe_col = 0;
7911 cost = 1; /* CR is just one character */
7912 }
7913 else
7914 {
7915 plan = PLAN_LE;
7916 wouldbe_col = col;
7917 }
7918 if (noinvcurs) /* will stop highlighting */
7919 {
7920 cost += noinvcurs;
7921 attr = 0;
7922 }
7923 }
7924
7925 /*
7926 * If the cursor is above where we want to be, we can use CR LF.
7927 */
7928 else if (row > screen_cur_row)
7929 {
7930 plan = PLAN_NL;
7931 wouldbe_col = 0;
7932 cost = (row - screen_cur_row) * 2; /* CR LF */
7933 if (noinvcurs) /* will stop highlighting */
7934 {
7935 cost += noinvcurs;
7936 attr = 0;
7937 }
7938 }
7939
7940 /*
7941 * If the cursor is in the same row, smaller col, just use write.
7942 */
7943 else
7944 {
7945 plan = PLAN_WRITE;
7946 wouldbe_col = screen_cur_col;
7947 cost = 0;
7948 }
7949
7950 /*
7951 * Check if any characters that need to be written have the
7952 * correct attributes. Also avoid UTF-8 characters.
7953 */
7954 i = col - wouldbe_col;
7955 if (i > 0)
7956 cost += i;
7957 if (cost < goto_cost && i > 0)
7958 {
7959 /*
7960 * Check if the attributes are correct without additionally
7961 * stopping highlighting.
7962 */
7963 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7964 while (i && *p++ == attr)
7965 --i;
7966 if (i != 0)
7967 {
7968 /*
7969 * Try if it works when highlighting is stopped here.
7970 */
7971 if (*--p == 0)
7972 {
7973 cost += noinvcurs;
7974 while (i && *p++ == 0)
7975 --i;
7976 }
7977 if (i != 0)
7978 cost = 999; /* different attributes, don't do it */
7979 }
7980#ifdef FEAT_MBYTE
7981 if (enc_utf8)
7982 {
7983 /* Don't use an UTF-8 char for positioning, it's slow. */
7984 for (i = wouldbe_col; i < col; ++i)
7985 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7986 {
7987 cost = 999;
7988 break;
7989 }
7990 }
7991#endif
7992 }
7993
7994 /*
7995 * We can do it without term_windgoto()!
7996 */
7997 if (cost < goto_cost)
7998 {
7999 if (plan == PLAN_LE)
8000 {
8001 if (noinvcurs)
8002 screen_stop_highlight();
8003 while (screen_cur_col > col)
8004 {
8005 out_str(bs);
8006 --screen_cur_col;
8007 }
8008 }
8009 else if (plan == PLAN_CR)
8010 {
8011 if (noinvcurs)
8012 screen_stop_highlight();
8013 out_char('\r');
8014 screen_cur_col = 0;
8015 }
8016 else if (plan == PLAN_NL)
8017 {
8018 if (noinvcurs)
8019 screen_stop_highlight();
8020 while (screen_cur_row < row)
8021 {
8022 out_char('\n');
8023 ++screen_cur_row;
8024 }
8025 screen_cur_col = 0;
8026 }
8027
8028 i = col - screen_cur_col;
8029 if (i > 0)
8030 {
8031 /*
8032 * Use cursor-right if it's one character only. Avoids
8033 * removing a line of pixels from the last bold char, when
8034 * using the bold trick in the GUI.
8035 */
8036 if (T_ND[0] != NUL && T_ND[1] == NUL)
8037 {
8038 while (i-- > 0)
8039 out_char(*T_ND);
8040 }
8041 else
8042 {
8043 int off;
8044
8045 off = LineOffset[row] + screen_cur_col;
8046 while (i-- > 0)
8047 {
8048 if (ScreenAttrs[off] != screen_attr)
8049 screen_stop_highlight();
8050#ifdef FEAT_MBYTE
8051 out_flush_check();
8052#endif
8053 out_char(ScreenLines[off]);
8054#ifdef FEAT_MBYTE
8055 if (enc_dbcs == DBCS_JPNU
8056 && ScreenLines[off] == 0x8e)
8057 out_char(ScreenLines2[off]);
8058#endif
8059 ++off;
8060 }
8061 }
8062 }
8063 }
8064 }
8065 else
8066 cost = 999;
8067
8068 if (cost >= goto_cost)
8069 {
8070 if (noinvcurs)
8071 screen_stop_highlight();
8072 if (row == screen_cur_row && (col > screen_cur_col) &&
8073 *T_CRI != NUL)
8074 term_cursor_right(col - screen_cur_col);
8075 else
8076 term_windgoto(row, col);
8077 }
8078 screen_cur_row = row;
8079 screen_cur_col = col;
8080 }
8081}
8082
8083/*
8084 * Set cursor to its position in the current window.
8085 */
8086 void
8087setcursor()
8088{
8089 if (redrawing())
8090 {
8091 validate_cursor();
8092 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8093 W_WINCOL(curwin) + (
8094#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008095 /* With 'rightleft' set and the cursor on a double-wide
8096 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8098# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008099 (has_mbyte
8100 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8101 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102# endif
8103 1)) :
8104#endif
8105 curwin->w_wcol));
8106 }
8107}
8108
8109
8110/*
8111 * insert 'line_count' lines at 'row' in window 'wp'
8112 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8113 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8114 * scrolling.
8115 * Returns FAIL if the lines are not inserted, OK for success.
8116 */
8117 int
8118win_ins_lines(wp, row, line_count, invalid, mayclear)
8119 win_T *wp;
8120 int row;
8121 int line_count;
8122 int invalid;
8123 int mayclear;
8124{
8125 int did_delete;
8126 int nextrow;
8127 int lastrow;
8128 int retval;
8129
8130 if (invalid)
8131 wp->w_lines_valid = 0;
8132
8133 if (wp->w_height < 5)
8134 return FAIL;
8135
8136 if (line_count > wp->w_height - row)
8137 line_count = wp->w_height - row;
8138
8139 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8140 if (retval != MAYBE)
8141 return retval;
8142
8143 /*
8144 * If there is a next window or a status line, we first try to delete the
8145 * lines at the bottom to avoid messing what is after the window.
8146 * If this fails and there are following windows, don't do anything to avoid
8147 * messing up those windows, better just redraw.
8148 */
8149 did_delete = FALSE;
8150#ifdef FEAT_WINDOWS
8151 if (wp->w_next != NULL || wp->w_status_height)
8152 {
8153 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8154 line_count, (int)Rows, FALSE, NULL) == OK)
8155 did_delete = TRUE;
8156 else if (wp->w_next)
8157 return FAIL;
8158 }
8159#endif
8160 /*
8161 * if no lines deleted, blank the lines that will end up below the window
8162 */
8163 if (!did_delete)
8164 {
8165#ifdef FEAT_WINDOWS
8166 wp->w_redr_status = TRUE;
8167#endif
8168 redraw_cmdline = TRUE;
8169 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8170 lastrow = nextrow + line_count;
8171 if (lastrow > Rows)
8172 lastrow = Rows;
8173 screen_fill(nextrow - line_count, lastrow - line_count,
8174 W_WINCOL(wp), (int)W_ENDCOL(wp),
8175 ' ', ' ', 0);
8176 }
8177
8178 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8179 == FAIL)
8180 {
8181 /* deletion will have messed up other windows */
8182 if (did_delete)
8183 {
8184#ifdef FEAT_WINDOWS
8185 wp->w_redr_status = TRUE;
8186#endif
8187 win_rest_invalid(W_NEXT(wp));
8188 }
8189 return FAIL;
8190 }
8191
8192 return OK;
8193}
8194
8195/*
8196 * delete "line_count" window lines at "row" in window "wp"
8197 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8198 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8199 * scrolling
8200 * Return OK for success, FAIL if the lines are not deleted.
8201 */
8202 int
8203win_del_lines(wp, row, line_count, invalid, mayclear)
8204 win_T *wp;
8205 int row;
8206 int line_count;
8207 int invalid;
8208 int mayclear;
8209{
8210 int retval;
8211
8212 if (invalid)
8213 wp->w_lines_valid = 0;
8214
8215 if (line_count > wp->w_height - row)
8216 line_count = wp->w_height - row;
8217
8218 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8219 if (retval != MAYBE)
8220 return retval;
8221
8222 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8223 (int)Rows, FALSE, NULL) == FAIL)
8224 return FAIL;
8225
8226#ifdef FEAT_WINDOWS
8227 /*
8228 * If there are windows or status lines below, try to put them at the
8229 * correct place. If we can't do that, they have to be redrawn.
8230 */
8231 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8232 {
8233 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8234 line_count, (int)Rows, NULL) == FAIL)
8235 {
8236 wp->w_redr_status = TRUE;
8237 win_rest_invalid(wp->w_next);
8238 }
8239 }
8240 /*
8241 * If this is the last window and there is no status line, redraw the
8242 * command line later.
8243 */
8244 else
8245#endif
8246 redraw_cmdline = TRUE;
8247 return OK;
8248}
8249
8250/*
8251 * Common code for win_ins_lines() and win_del_lines().
8252 * Returns OK or FAIL when the work has been done.
8253 * Returns MAYBE when not finished yet.
8254 */
8255 static int
8256win_do_lines(wp, row, line_count, mayclear, del)
8257 win_T *wp;
8258 int row;
8259 int line_count;
8260 int mayclear;
8261 int del;
8262{
8263 int retval;
8264
8265 if (!redrawing() || line_count <= 0)
8266 return FAIL;
8267
8268 /* only a few lines left: redraw is faster */
8269 if (mayclear && Rows - line_count < 5
8270#ifdef FEAT_VERTSPLIT
8271 && wp->w_width == Columns
8272#endif
8273 )
8274 {
8275 screenclear(); /* will set wp->w_lines_valid to 0 */
8276 return FAIL;
8277 }
8278
8279 /*
8280 * Delete all remaining lines
8281 */
8282 if (row + line_count >= wp->w_height)
8283 {
8284 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8285 W_WINCOL(wp), (int)W_ENDCOL(wp),
8286 ' ', ' ', 0);
8287 return OK;
8288 }
8289
8290 /*
8291 * when scrolling, the message on the command line should be cleared,
8292 * otherwise it will stay there forever.
8293 */
8294 clear_cmdline = TRUE;
8295
8296 /*
8297 * If the terminal can set a scroll region, use that.
8298 * Always do this in a vertically split window. This will redraw from
8299 * ScreenLines[] when t_CV isn't defined. That's faster than using
8300 * win_line().
8301 * Don't use a scroll region when we are going to redraw the text, writing
8302 * a character in the lower right corner of the scroll region causes a
8303 * scroll-up in the DJGPP version.
8304 */
8305 if (scroll_region
8306#ifdef FEAT_VERTSPLIT
8307 || W_WIDTH(wp) != Columns
8308#endif
8309 )
8310 {
8311#ifdef FEAT_VERTSPLIT
8312 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8313#endif
8314 scroll_region_set(wp, row);
8315 if (del)
8316 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8317 wp->w_height - row, FALSE, wp);
8318 else
8319 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8320 wp->w_height - row, wp);
8321#ifdef FEAT_VERTSPLIT
8322 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8323#endif
8324 scroll_region_reset();
8325 return retval;
8326 }
8327
8328#ifdef FEAT_WINDOWS
8329 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8330 return FAIL;
8331#endif
8332
8333 return MAYBE;
8334}
8335
8336/*
8337 * window 'wp' and everything after it is messed up, mark it for redraw
8338 */
8339 static void
8340win_rest_invalid(wp)
8341 win_T *wp;
8342{
8343#ifdef FEAT_WINDOWS
8344 while (wp != NULL)
8345#else
8346 if (wp != NULL)
8347#endif
8348 {
8349 redraw_win_later(wp, NOT_VALID);
8350#ifdef FEAT_WINDOWS
8351 wp->w_redr_status = TRUE;
8352 wp = wp->w_next;
8353#endif
8354 }
8355 redraw_cmdline = TRUE;
8356}
8357
8358/*
8359 * The rest of the routines in this file perform screen manipulations. The
8360 * given operation is performed physically on the screen. The corresponding
8361 * change is also made to the internal screen image. In this way, the editor
8362 * anticipates the effect of editing changes on the appearance of the screen.
8363 * That way, when we call screenupdate a complete redraw isn't usually
8364 * necessary. Another advantage is that we can keep adding code to anticipate
8365 * screen changes, and in the meantime, everything still works.
8366 */
8367
8368/*
8369 * types for inserting or deleting lines
8370 */
8371#define USE_T_CAL 1
8372#define USE_T_CDL 2
8373#define USE_T_AL 3
8374#define USE_T_CE 4
8375#define USE_T_DL 5
8376#define USE_T_SR 6
8377#define USE_NL 7
8378#define USE_T_CD 8
8379#define USE_REDRAW 9
8380
8381/*
8382 * insert lines on the screen and update ScreenLines[]
8383 * 'end' is the line after the scrolled part. Normally it is Rows.
8384 * When scrolling region used 'off' is the offset from the top for the region.
8385 * 'row' and 'end' are relative to the start of the region.
8386 *
8387 * return FAIL for failure, OK for success.
8388 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008389 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390screen_ins_lines(off, row, line_count, end, wp)
8391 int off;
8392 int row;
8393 int line_count;
8394 int end;
8395 win_T *wp; /* NULL or window to use width from */
8396{
8397 int i;
8398 int j;
8399 unsigned temp;
8400 int cursor_row;
8401 int type;
8402 int result_empty;
8403 int can_ce = can_clear(T_CE);
8404
8405 /*
8406 * FAIL if
8407 * - there is no valid screen
8408 * - the screen has to be redrawn completely
8409 * - the line count is less than one
8410 * - the line count is more than 'ttyscroll'
8411 */
8412 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8413 return FAIL;
8414
8415 /*
8416 * There are seven ways to insert lines:
8417 * 0. When in a vertically split window and t_CV isn't set, redraw the
8418 * characters from ScreenLines[].
8419 * 1. Use T_CD (clear to end of display) if it exists and the result of
8420 * the insert is just empty lines
8421 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8422 * present or line_count > 1. It looks better if we do all the inserts
8423 * at once.
8424 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8425 * insert is just empty lines and T_CE is not present or line_count >
8426 * 1.
8427 * 4. Use T_AL (insert line) if it exists.
8428 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8429 * just empty lines.
8430 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8431 * just empty lines.
8432 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8433 * the 'da' flag is not set or we have clear line capability.
8434 * 8. redraw the characters from ScreenLines[].
8435 *
8436 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8437 * the scrollbar for the window. It does have insert line, use that if it
8438 * exists.
8439 */
8440 result_empty = (row + line_count >= end);
8441#ifdef FEAT_VERTSPLIT
8442 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8443 type = USE_REDRAW;
8444 else
8445#endif
8446 if (can_clear(T_CD) && result_empty)
8447 type = USE_T_CD;
8448 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8449 type = USE_T_CAL;
8450 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8451 type = USE_T_CDL;
8452 else if (*T_AL != NUL)
8453 type = USE_T_AL;
8454 else if (can_ce && result_empty)
8455 type = USE_T_CE;
8456 else if (*T_DL != NUL && result_empty)
8457 type = USE_T_DL;
8458 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8459 type = USE_T_SR;
8460 else
8461 return FAIL;
8462
8463 /*
8464 * For clearing the lines screen_del_lines() is used. This will also take
8465 * care of t_db if necessary.
8466 */
8467 if (type == USE_T_CD || type == USE_T_CDL ||
8468 type == USE_T_CE || type == USE_T_DL)
8469 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8470
8471 /*
8472 * If text is retained below the screen, first clear or delete as many
8473 * lines at the bottom of the window as are about to be inserted so that
8474 * the deleted lines won't later surface during a screen_del_lines.
8475 */
8476 if (*T_DB)
8477 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8478
8479#ifdef FEAT_CLIPBOARD
8480 /* Remove a modeless selection when inserting lines halfway the screen
8481 * or not the full width of the screen. */
8482 if (off + row > 0
8483# ifdef FEAT_VERTSPLIT
8484 || (wp != NULL && wp->w_width != Columns)
8485# endif
8486 )
8487 clip_clear_selection();
8488 else
8489 clip_scroll_selection(-line_count);
8490#endif
8491
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492#ifdef FEAT_GUI
8493 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8494 * scrolling is actually carried out. */
8495 gui_dont_update_cursor();
8496#endif
8497
8498 if (*T_CCS != NUL) /* cursor relative to region */
8499 cursor_row = row;
8500 else
8501 cursor_row = row + off;
8502
8503 /*
8504 * Shift LineOffset[] line_count down to reflect the inserted lines.
8505 * Clear the inserted lines in ScreenLines[].
8506 */
8507 row += off;
8508 end += off;
8509 for (i = 0; i < line_count; ++i)
8510 {
8511#ifdef FEAT_VERTSPLIT
8512 if (wp != NULL && wp->w_width != Columns)
8513 {
8514 /* need to copy part of a line */
8515 j = end - 1 - i;
8516 while ((j -= line_count) >= row)
8517 linecopy(j + line_count, j, wp);
8518 j += line_count;
8519 if (can_clear((char_u *)" "))
8520 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8521 else
8522 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8523 LineWraps[j] = FALSE;
8524 }
8525 else
8526#endif
8527 {
8528 j = end - 1 - i;
8529 temp = LineOffset[j];
8530 while ((j -= line_count) >= row)
8531 {
8532 LineOffset[j + line_count] = LineOffset[j];
8533 LineWraps[j + line_count] = LineWraps[j];
8534 }
8535 LineOffset[j + line_count] = temp;
8536 LineWraps[j + line_count] = FALSE;
8537 if (can_clear((char_u *)" "))
8538 lineclear(temp, (int)Columns);
8539 else
8540 lineinvalid(temp, (int)Columns);
8541 }
8542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543
8544 screen_stop_highlight();
8545 windgoto(cursor_row, 0);
8546
8547#ifdef FEAT_VERTSPLIT
8548 /* redraw the characters */
8549 if (type == USE_REDRAW)
8550 redraw_block(row, end, wp);
8551 else
8552#endif
8553 if (type == USE_T_CAL)
8554 {
8555 term_append_lines(line_count);
8556 screen_start(); /* don't know where cursor is now */
8557 }
8558 else
8559 {
8560 for (i = 0; i < line_count; i++)
8561 {
8562 if (type == USE_T_AL)
8563 {
8564 if (i && cursor_row != 0)
8565 windgoto(cursor_row, 0);
8566 out_str(T_AL);
8567 }
8568 else /* type == USE_T_SR */
8569 out_str(T_SR);
8570 screen_start(); /* don't know where cursor is now */
8571 }
8572 }
8573
8574 /*
8575 * With scroll-reverse and 'da' flag set we need to clear the lines that
8576 * have been scrolled down into the region.
8577 */
8578 if (type == USE_T_SR && *T_DA)
8579 {
8580 for (i = 0; i < line_count; ++i)
8581 {
8582 windgoto(off + i, 0);
8583 out_str(T_CE);
8584 screen_start(); /* don't know where cursor is now */
8585 }
8586 }
8587
8588#ifdef FEAT_GUI
8589 gui_can_update_cursor();
8590 if (gui.in_use)
8591 out_flush(); /* always flush after a scroll */
8592#endif
8593 return OK;
8594}
8595
8596/*
8597 * delete lines on the screen and update ScreenLines[]
8598 * 'end' is the line after the scrolled part. Normally it is Rows.
8599 * When scrolling region used 'off' is the offset from the top for the region.
8600 * 'row' and 'end' are relative to the start of the region.
8601 *
8602 * Return OK for success, FAIL if the lines are not deleted.
8603 */
8604/*ARGSUSED*/
8605 int
8606screen_del_lines(off, row, line_count, end, force, wp)
8607 int off;
8608 int row;
8609 int line_count;
8610 int end;
8611 int force; /* even when line_count > p_ttyscroll */
8612 win_T *wp; /* NULL or window to use width from */
8613{
8614 int j;
8615 int i;
8616 unsigned temp;
8617 int cursor_row;
8618 int cursor_end;
8619 int result_empty; /* result is empty until end of region */
8620 int can_delete; /* deleting line codes can be used */
8621 int type;
8622
8623 /*
8624 * FAIL if
8625 * - there is no valid screen
8626 * - the screen has to be redrawn completely
8627 * - the line count is less than one
8628 * - the line count is more than 'ttyscroll'
8629 */
8630 if (!screen_valid(TRUE) || line_count <= 0 ||
8631 (!force && line_count > p_ttyscroll))
8632 return FAIL;
8633
8634 /*
8635 * Check if the rest of the current region will become empty.
8636 */
8637 result_empty = row + line_count >= end;
8638
8639 /*
8640 * We can delete lines only when 'db' flag not set or when 'ce' option
8641 * available.
8642 */
8643 can_delete = (*T_DB == NUL || can_clear(T_CE));
8644
8645 /*
8646 * There are six ways to delete lines:
8647 * 0. When in a vertically split window and t_CV isn't set, redraw the
8648 * characters from ScreenLines[].
8649 * 1. Use T_CD if it exists and the result is empty.
8650 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8651 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8652 * none of the other ways work.
8653 * 4. Use T_CE (erase line) if the result is empty.
8654 * 5. Use T_DL (delete line) if it exists.
8655 * 6. redraw the characters from ScreenLines[].
8656 */
8657#ifdef FEAT_VERTSPLIT
8658 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8659 type = USE_REDRAW;
8660 else
8661#endif
8662 if (can_clear(T_CD) && result_empty)
8663 type = USE_T_CD;
8664#if defined(__BEOS__) && defined(BEOS_DR8)
8665 /*
8666 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8667 * its internal termcap... this works okay for tests which test *T_DB !=
8668 * NUL. It has the disadvantage that the user cannot use any :set t_*
8669 * command to get T_DB (back) to empty_option, only :set term=... will do
8670 * the trick...
8671 * Anyway, this hack will hopefully go away with the next OS release.
8672 * (Olaf Seibert)
8673 */
8674 else if (row == 0 && T_DB == empty_option
8675 && (line_count == 1 || *T_CDL == NUL))
8676#else
8677 else if (row == 0 && (
8678#ifndef AMIGA
8679 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8680 * up, so use delete-line command */
8681 line_count == 1 ||
8682#endif
8683 *T_CDL == NUL))
8684#endif
8685 type = USE_NL;
8686 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8687 type = USE_T_CDL;
8688 else if (can_clear(T_CE) && result_empty
8689#ifdef FEAT_VERTSPLIT
8690 && (wp == NULL || wp->w_width == Columns)
8691#endif
8692 )
8693 type = USE_T_CE;
8694 else if (*T_DL != NUL && can_delete)
8695 type = USE_T_DL;
8696 else if (*T_CDL != NUL && can_delete)
8697 type = USE_T_CDL;
8698 else
8699 return FAIL;
8700
8701#ifdef FEAT_CLIPBOARD
8702 /* Remove a modeless selection when deleting lines halfway the screen or
8703 * not the full width of the screen. */
8704 if (off + row > 0
8705# ifdef FEAT_VERTSPLIT
8706 || (wp != NULL && wp->w_width != Columns)
8707# endif
8708 )
8709 clip_clear_selection();
8710 else
8711 clip_scroll_selection(line_count);
8712#endif
8713
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714#ifdef FEAT_GUI
8715 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8716 * scrolling is actually carried out. */
8717 gui_dont_update_cursor();
8718#endif
8719
8720 if (*T_CCS != NUL) /* cursor relative to region */
8721 {
8722 cursor_row = row;
8723 cursor_end = end;
8724 }
8725 else
8726 {
8727 cursor_row = row + off;
8728 cursor_end = end + off;
8729 }
8730
8731 /*
8732 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8733 * Clear the inserted lines in ScreenLines[].
8734 */
8735 row += off;
8736 end += off;
8737 for (i = 0; i < line_count; ++i)
8738 {
8739#ifdef FEAT_VERTSPLIT
8740 if (wp != NULL && wp->w_width != Columns)
8741 {
8742 /* need to copy part of a line */
8743 j = row + i;
8744 while ((j += line_count) <= end - 1)
8745 linecopy(j - line_count, j, wp);
8746 j -= line_count;
8747 if (can_clear((char_u *)" "))
8748 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8749 else
8750 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8751 LineWraps[j] = FALSE;
8752 }
8753 else
8754#endif
8755 {
8756 /* whole width, moving the line pointers is faster */
8757 j = row + i;
8758 temp = LineOffset[j];
8759 while ((j += line_count) <= end - 1)
8760 {
8761 LineOffset[j - line_count] = LineOffset[j];
8762 LineWraps[j - line_count] = LineWraps[j];
8763 }
8764 LineOffset[j - line_count] = temp;
8765 LineWraps[j - line_count] = FALSE;
8766 if (can_clear((char_u *)" "))
8767 lineclear(temp, (int)Columns);
8768 else
8769 lineinvalid(temp, (int)Columns);
8770 }
8771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
8773 screen_stop_highlight();
8774
8775#ifdef FEAT_VERTSPLIT
8776 /* redraw the characters */
8777 if (type == USE_REDRAW)
8778 redraw_block(row, end, wp);
8779 else
8780#endif
8781 if (type == USE_T_CD) /* delete the lines */
8782 {
8783 windgoto(cursor_row, 0);
8784 out_str(T_CD);
8785 screen_start(); /* don't know where cursor is now */
8786 }
8787 else if (type == USE_T_CDL)
8788 {
8789 windgoto(cursor_row, 0);
8790 term_delete_lines(line_count);
8791 screen_start(); /* don't know where cursor is now */
8792 }
8793 /*
8794 * Deleting lines at top of the screen or scroll region: Just scroll
8795 * the whole screen (scroll region) up by outputting newlines on the
8796 * last line.
8797 */
8798 else if (type == USE_NL)
8799 {
8800 windgoto(cursor_end - 1, 0);
8801 for (i = line_count; --i >= 0; )
8802 out_char('\n'); /* cursor will remain on same line */
8803 }
8804 else
8805 {
8806 for (i = line_count; --i >= 0; )
8807 {
8808 if (type == USE_T_DL)
8809 {
8810 windgoto(cursor_row, 0);
8811 out_str(T_DL); /* delete a line */
8812 }
8813 else /* type == USE_T_CE */
8814 {
8815 windgoto(cursor_row + i, 0);
8816 out_str(T_CE); /* erase a line */
8817 }
8818 screen_start(); /* don't know where cursor is now */
8819 }
8820 }
8821
8822 /*
8823 * If the 'db' flag is set, we need to clear the lines that have been
8824 * scrolled up at the bottom of the region.
8825 */
8826 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8827 {
8828 for (i = line_count; i > 0; --i)
8829 {
8830 windgoto(cursor_end - i, 0);
8831 out_str(T_CE); /* erase a line */
8832 screen_start(); /* don't know where cursor is now */
8833 }
8834 }
8835
8836#ifdef FEAT_GUI
8837 gui_can_update_cursor();
8838 if (gui.in_use)
8839 out_flush(); /* always flush after a scroll */
8840#endif
8841
8842 return OK;
8843}
8844
8845/*
8846 * show the current mode and ruler
8847 *
8848 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8849 * If clear_cmdline is FALSE there may be a message there that needs to be
8850 * cleared only if a mode is shown.
8851 * Return the length of the message (0 if no message).
8852 */
8853 int
8854showmode()
8855{
8856 int need_clear;
8857 int length = 0;
8858 int do_mode;
8859 int attr;
8860 int nwr_save;
8861#ifdef FEAT_INS_EXPAND
8862 int sub_attr;
8863#endif
8864
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008865 do_mode = ((p_smd && msg_silent == 0)
8866 && ((State & INSERT)
8867 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868#ifdef FEAT_VISUAL
8869 || VIsual_active
8870#endif
8871 ));
8872 if (do_mode || Recording)
8873 {
8874 /*
8875 * Don't show mode right now, when not redrawing or inside a mapping.
8876 * Call char_avail() only when we are going to show something, because
8877 * it takes a bit of time.
8878 */
8879 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8880 {
8881 redraw_cmdline = TRUE; /* show mode later */
8882 return 0;
8883 }
8884
8885 nwr_save = need_wait_return;
8886
8887 /* wait a bit before overwriting an important message */
8888 check_for_delay(FALSE);
8889
8890 /* if the cmdline is more than one line high, erase top lines */
8891 need_clear = clear_cmdline;
8892 if (clear_cmdline && cmdline_row < Rows - 1)
8893 msg_clr_cmdline(); /* will reset clear_cmdline */
8894
8895 /* Position on the last line in the window, column 0 */
8896 msg_pos_mode();
8897 cursor_off();
8898 attr = hl_attr(HLF_CM); /* Highlight mode */
8899 if (do_mode)
8900 {
8901 MSG_PUTS_ATTR("--", attr);
8902#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00008903# if 0 /* old version, changed by SungHyun Nam July 2008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 if (xic != NULL && im_get_status() && !p_imdisable
8905 && curbuf->b_p_iminsert == B_IMODE_IM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00008906# else
8907 if (
8908# ifdef HAVE_GTK2
8909 preedit_get_status()
8910# else
8911 im_get_status()
8912# endif
8913 )
8914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8916 MSG_PUTS_ATTR(" IM", attr);
8917# else
8918 MSG_PUTS_ATTR(" XIM", attr);
8919# endif
8920#endif
8921#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8922 if (gui.in_use)
8923 {
8924 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008925 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 }
8927#endif
8928#ifdef FEAT_INS_EXPAND
8929 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8930 {
8931 /* These messages can get long, avoid a wrap in a narrow
8932 * window. Prefer showing edit_submode_extra. */
8933 length = (Rows - msg_row) * Columns - 3;
8934 if (edit_submode_extra != NULL)
8935 length -= vim_strsize(edit_submode_extra);
8936 if (length > 0)
8937 {
8938 if (edit_submode_pre != NULL)
8939 length -= vim_strsize(edit_submode_pre);
8940 if (length - vim_strsize(edit_submode) > 0)
8941 {
8942 if (edit_submode_pre != NULL)
8943 msg_puts_attr(edit_submode_pre, attr);
8944 msg_puts_attr(edit_submode, attr);
8945 }
8946 if (edit_submode_extra != NULL)
8947 {
8948 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8949 if ((int)edit_submode_highl < (int)HLF_COUNT)
8950 sub_attr = hl_attr(edit_submode_highl);
8951 else
8952 sub_attr = attr;
8953 msg_puts_attr(edit_submode_extra, sub_attr);
8954 }
8955 }
8956 length = 0;
8957 }
8958 else
8959#endif
8960 {
8961#ifdef FEAT_VREPLACE
8962 if (State & VREPLACE_FLAG)
8963 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8964 else
8965#endif
8966 if (State & REPLACE_FLAG)
8967 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8968 else if (State & INSERT)
8969 {
8970#ifdef FEAT_RIGHTLEFT
8971 if (p_ri)
8972 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8973#endif
8974 MSG_PUTS_ATTR(_(" INSERT"), attr);
8975 }
8976 else if (restart_edit == 'I')
8977 MSG_PUTS_ATTR(_(" (insert)"), attr);
8978 else if (restart_edit == 'R')
8979 MSG_PUTS_ATTR(_(" (replace)"), attr);
8980 else if (restart_edit == 'V')
8981 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8982#ifdef FEAT_RIGHTLEFT
8983 if (p_hkmap)
8984 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8985# ifdef FEAT_FKMAP
8986 if (p_fkmap)
8987 MSG_PUTS_ATTR(farsi_text_5, attr);
8988# endif
8989#endif
8990#ifdef FEAT_KEYMAP
8991 if (State & LANGMAP)
8992 {
8993# ifdef FEAT_ARABIC
8994 if (curwin->w_p_arab)
8995 MSG_PUTS_ATTR(_(" Arabic"), attr);
8996 else
8997# endif
8998 MSG_PUTS_ATTR(_(" (lang)"), attr);
8999 }
9000#endif
9001 if ((State & INSERT) && p_paste)
9002 MSG_PUTS_ATTR(_(" (paste)"), attr);
9003
9004#ifdef FEAT_VISUAL
9005 if (VIsual_active)
9006 {
9007 char *p;
9008
9009 /* Don't concatenate separate words to avoid translation
9010 * problems. */
9011 switch ((VIsual_select ? 4 : 0)
9012 + (VIsual_mode == Ctrl_V) * 2
9013 + (VIsual_mode == 'V'))
9014 {
9015 case 0: p = N_(" VISUAL"); break;
9016 case 1: p = N_(" VISUAL LINE"); break;
9017 case 2: p = N_(" VISUAL BLOCK"); break;
9018 case 4: p = N_(" SELECT"); break;
9019 case 5: p = N_(" SELECT LINE"); break;
9020 default: p = N_(" SELECT BLOCK"); break;
9021 }
9022 MSG_PUTS_ATTR(_(p), attr);
9023 }
9024#endif
9025 MSG_PUTS_ATTR(" --", attr);
9026 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009027
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 need_clear = TRUE;
9029 }
9030 if (Recording
9031#ifdef FEAT_INS_EXPAND
9032 && edit_submode == NULL /* otherwise it gets too long */
9033#endif
9034 )
9035 {
9036 MSG_PUTS_ATTR(_("recording"), attr);
9037 need_clear = TRUE;
9038 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009039
9040 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 if (need_clear || clear_cmdline)
9042 msg_clr_eos();
9043 msg_didout = FALSE; /* overwrite this message */
9044 length = msg_col;
9045 msg_col = 0;
9046 need_wait_return = nwr_save; /* never ask for hit-return for this */
9047 }
9048 else if (clear_cmdline && msg_silent == 0)
9049 /* Clear the whole command line. Will reset "clear_cmdline". */
9050 msg_clr_cmdline();
9051
9052#ifdef FEAT_CMDL_INFO
9053# ifdef FEAT_VISUAL
9054 /* In Visual mode the size of the selected area must be redrawn. */
9055 if (VIsual_active)
9056 clear_showcmd();
9057# endif
9058
9059 /* If the last window has no status line, the ruler is after the mode
9060 * message and must be redrawn */
9061 if (redrawing()
9062# ifdef FEAT_WINDOWS
9063 && lastwin->w_status_height == 0
9064# endif
9065 )
9066 win_redr_ruler(lastwin, TRUE);
9067#endif
9068 redraw_cmdline = FALSE;
9069 clear_cmdline = FALSE;
9070
9071 return length;
9072}
9073
9074/*
9075 * Position for a mode message.
9076 */
9077 static void
9078msg_pos_mode()
9079{
9080 msg_col = 0;
9081 msg_row = Rows - 1;
9082}
9083
9084/*
9085 * Delete mode message. Used when ESC is typed which is expected to end
9086 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009087 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 */
9089 void
9090unshowmode(force)
9091 int force;
9092{
9093 /*
9094 * Don't delete it right now, when not redrawing or insided a mapping.
9095 */
9096 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9097 redraw_cmdline = TRUE; /* delete mode later */
9098 else
9099 {
9100 msg_pos_mode();
9101 if (Recording)
9102 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9103 msg_clr_eos();
9104 }
9105}
9106
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009107#if defined(FEAT_WINDOWS)
9108/*
9109 * Draw the tab pages line at the top of the Vim window.
9110 */
9111 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009112draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009113{
9114 int tabcount = 0;
9115 tabpage_T *tp;
9116 int tabwidth;
9117 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009118 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009119 int attr;
9120 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009121 win_T *cwp;
9122 int wincount;
9123 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009124 int c;
9125 int len;
9126 int attr_sel = hl_attr(HLF_TPS);
9127 int attr_nosel = hl_attr(HLF_TP);
9128 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009129 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009130 int room;
9131 int use_sep_chars = (t_colors < 8
9132#ifdef FEAT_GUI
9133 && !gui.in_use
9134#endif
9135 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009136
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009137 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009138
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009139#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009140 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009141 if (gui_use_tabline())
9142 {
9143 gui_update_tabline();
9144 return;
9145 }
9146#endif
9147
9148 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009149 return;
9150
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009151#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009152
9153 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9154 for (scol = 0; scol < Columns; ++scol)
9155 TabPageIdxs[scol] = 0;
9156
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009157 /* Use the 'tabline' option if it's set. */
9158 if (*p_tal != NUL)
9159 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009160 int save_called_emsg = called_emsg;
9161
9162 /* Check for an error. If there is one we would loop in redrawing the
9163 * screen. Avoid that by making 'tabline' empty. */
9164 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009165 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009166 if (called_emsg)
9167 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009168 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009169 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009170 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009171 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009172#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009173 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009174 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9175 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009176
Bram Moolenaar238a5642006-02-21 22:12:05 +00009177 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9178 if (tabwidth < 6)
9179 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009180
Bram Moolenaar238a5642006-02-21 22:12:05 +00009181 attr = attr_nosel;
9182 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009183 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009184 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9185 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009186 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009187 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009188
Bram Moolenaar238a5642006-02-21 22:12:05 +00009189 if (tp->tp_topframe == topframe)
9190 attr = attr_sel;
9191 if (use_sep_chars && col > 0)
9192 screen_putchar('|', 0, col++, attr);
9193
9194 if (tp->tp_topframe != topframe)
9195 attr = attr_nosel;
9196
9197 screen_putchar(' ', 0, col++, attr);
9198
9199 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009200 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009201 cwp = curwin;
9202 wp = firstwin;
9203 }
9204 else
9205 {
9206 cwp = tp->tp_curwin;
9207 wp = tp->tp_firstwin;
9208 }
9209
9210 modified = FALSE;
9211 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9212 if (bufIsChanged(wp->w_buffer))
9213 modified = TRUE;
9214 if (modified || wincount > 1)
9215 {
9216 if (wincount > 1)
9217 {
9218 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009219 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009220 if (col + len >= Columns - 3)
9221 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009222 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009223#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009224 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009225#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009226 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009227#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009228 );
9229 col += len;
9230 }
9231 if (modified)
9232 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9233 screen_putchar(' ', 0, col++, attr);
9234 }
9235
9236 room = scol - col + tabwidth - 1;
9237 if (room > 0)
9238 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009239 /* Get buffer name in NameBuff[] */
9240 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009241 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009242 len = vim_strsize(NameBuff);
9243 p = NameBuff;
9244#ifdef FEAT_MBYTE
9245 if (has_mbyte)
9246 while (len > room)
9247 {
9248 len -= ptr2cells(p);
9249 mb_ptr_adv(p);
9250 }
9251 else
9252#endif
9253 if (len > room)
9254 {
9255 p += len - room;
9256 len = room;
9257 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009258 if (len > Columns - col - 1)
9259 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009260
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009261 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009262 col += len;
9263 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00009264 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009265
9266 /* Store the tab page number in TabPageIdxs[], so that
9267 * jump_to_mouse() knows where each one is. */
9268 ++tabcount;
9269 while (scol < col)
9270 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009271 }
9272
Bram Moolenaar238a5642006-02-21 22:12:05 +00009273 if (use_sep_chars)
9274 c = '_';
9275 else
9276 c = ' ';
9277 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009278
9279 /* Put an "X" for closing the current tab if there are several. */
9280 if (first_tabpage->tp_next != NULL)
9281 {
9282 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9283 TabPageIdxs[Columns - 1] = -999;
9284 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009285 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009286
9287 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9288 * set. */
9289 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009290}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009291
9292/*
9293 * Get buffer name for "buf" into NameBuff[].
9294 * Takes care of special buffer names and translates special characters.
9295 */
9296 void
9297get_trans_bufname(buf)
9298 buf_T *buf;
9299{
9300 if (buf_spname(buf) != NULL)
9301 STRCPY(NameBuff, buf_spname(buf));
9302 else
9303 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9304 trans_characters(NameBuff, MAXPATHL);
9305}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009306#endif
9307
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9309/*
9310 * Get the character to use in a status line. Get its attributes in "*attr".
9311 */
9312 static int
9313fillchar_status(attr, is_curwin)
9314 int *attr;
9315 int is_curwin;
9316{
9317 int fill;
9318 if (is_curwin)
9319 {
9320 *attr = hl_attr(HLF_S);
9321 fill = fill_stl;
9322 }
9323 else
9324 {
9325 *attr = hl_attr(HLF_SNC);
9326 fill = fill_stlnc;
9327 }
9328 /* Use fill when there is highlighting, and highlighting of current
9329 * window differs, or the fillchars differ, or this is not the
9330 * current window */
9331 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9332 || !is_curwin || firstwin == lastwin)
9333 || (fill_stl != fill_stlnc)))
9334 return fill;
9335 if (is_curwin)
9336 return '^';
9337 return '=';
9338}
9339#endif
9340
9341#ifdef FEAT_VERTSPLIT
9342/*
9343 * Get the character to use in a separator between vertically split windows.
9344 * Get its attributes in "*attr".
9345 */
9346 static int
9347fillchar_vsep(attr)
9348 int *attr;
9349{
9350 *attr = hl_attr(HLF_C);
9351 if (*attr == 0 && fill_vert == ' ')
9352 return '|';
9353 else
9354 return fill_vert;
9355}
9356#endif
9357
9358/*
9359 * Return TRUE if redrawing should currently be done.
9360 */
9361 int
9362redrawing()
9363{
9364 return (!RedrawingDisabled
9365 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9366}
9367
9368/*
9369 * Return TRUE if printing messages should currently be done.
9370 */
9371 int
9372messaging()
9373{
9374 return (!(p_lz && char_avail() && !KeyTyped));
9375}
9376
9377/*
9378 * Show current status info in ruler and various other places
9379 * If always is FALSE, only show ruler if position has changed.
9380 */
9381 void
9382showruler(always)
9383 int always;
9384{
9385 if (!always && !redrawing())
9386 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009387#ifdef FEAT_INS_EXPAND
9388 if (pum_visible())
9389 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009390# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009391 /* Don't redraw right now, do it later. */
9392 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009393# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009394 return;
9395 }
9396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009398 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009399 {
9400 redraw_custum_statusline(curwin);
9401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 else
9403#endif
9404#ifdef FEAT_CMDL_INFO
9405 win_redr_ruler(curwin, always);
9406#endif
9407
9408#ifdef FEAT_TITLE
9409 if (need_maketitle
9410# ifdef FEAT_STL_OPT
9411 || (p_icon && (stl_syntax & STL_IN_ICON))
9412 || (p_title && (stl_syntax & STL_IN_TITLE))
9413# endif
9414 )
9415 maketitle();
9416#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +00009417#ifdef FEAT_WINDOWS
9418 /* Redraw the tab pages line if needed. */
9419 if (redraw_tabline)
9420 draw_tabline();
9421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422}
9423
9424#ifdef FEAT_CMDL_INFO
9425 static void
9426win_redr_ruler(wp, always)
9427 win_T *wp;
9428 int always;
9429{
9430 char_u buffer[70];
9431 int row;
9432 int fillchar;
9433 int attr;
9434 int empty_line = FALSE;
9435 colnr_T virtcol;
9436 int i;
9437 int o;
9438#ifdef FEAT_VERTSPLIT
9439 int this_ru_col;
9440 int off = 0;
9441 int width = Columns;
9442# define WITH_OFF(x) x
9443# define WITH_WIDTH(x) x
9444#else
9445# define WITH_OFF(x) 0
9446# define WITH_WIDTH(x) Columns
9447# define this_ru_col ru_col
9448#endif
9449
9450 /* If 'ruler' off or redrawing disabled, don't do anything */
9451 if (!p_ru)
9452 return;
9453
9454 /*
9455 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9456 * after deleting lines, before cursor.lnum is corrected.
9457 */
9458 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9459 return;
9460
9461#ifdef FEAT_INS_EXPAND
9462 /* Don't draw the ruler while doing insert-completion, it might overwrite
9463 * the (long) mode message. */
9464# ifdef FEAT_WINDOWS
9465 if (wp == lastwin && lastwin->w_status_height == 0)
9466# endif
9467 if (edit_submode != NULL)
9468 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009469 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9470 if (pum_visible())
9471 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472#endif
9473
9474#ifdef FEAT_STL_OPT
9475 if (*p_ruf)
9476 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009477 int save_called_emsg = called_emsg;
9478
9479 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009481 if (called_emsg)
9482 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009483 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009484 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 return;
9486 }
9487#endif
9488
9489 /*
9490 * Check if not in Insert mode and the line is empty (will show "0-1").
9491 */
9492 if (!(State & INSERT)
9493 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9494 empty_line = TRUE;
9495
9496 /*
9497 * Only draw the ruler when something changed.
9498 */
9499 validate_virtcol_win(wp);
9500 if ( redraw_cmdline
9501 || always
9502 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9503 || wp->w_cursor.col != wp->w_ru_cursor.col
9504 || wp->w_virtcol != wp->w_ru_virtcol
9505#ifdef FEAT_VIRTUALEDIT
9506 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9507#endif
9508 || wp->w_topline != wp->w_ru_topline
9509 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9510#ifdef FEAT_DIFF
9511 || wp->w_topfill != wp->w_ru_topfill
9512#endif
9513 || empty_line != wp->w_ru_empty)
9514 {
9515 cursor_off();
9516#ifdef FEAT_WINDOWS
9517 if (wp->w_status_height)
9518 {
9519 row = W_WINROW(wp) + wp->w_height;
9520 fillchar = fillchar_status(&attr, wp == curwin);
9521# ifdef FEAT_VERTSPLIT
9522 off = W_WINCOL(wp);
9523 width = W_WIDTH(wp);
9524# endif
9525 }
9526 else
9527#endif
9528 {
9529 row = Rows - 1;
9530 fillchar = ' ';
9531 attr = 0;
9532#ifdef FEAT_VERTSPLIT
9533 width = Columns;
9534 off = 0;
9535#endif
9536 }
9537
9538 /* In list mode virtcol needs to be recomputed */
9539 virtcol = wp->w_virtcol;
9540 if (wp->w_p_list && lcs_tab1 == NUL)
9541 {
9542 wp->w_p_list = FALSE;
9543 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9544 wp->w_p_list = TRUE;
9545 }
9546
9547 /*
9548 * Some sprintfs return the length, some return a pointer.
9549 * To avoid portability problems we use strlen() here.
9550 */
9551 sprintf((char *)buffer, "%ld,",
9552 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9553 ? 0L
9554 : (long)(wp->w_cursor.lnum));
9555 col_print(buffer + STRLEN(buffer),
9556 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9557 (int)virtcol + 1);
9558
9559 /*
9560 * Add a "50%" if there is room for it.
9561 * On the last line, don't print in the last column (scrolls the
9562 * screen up on some terminals).
9563 */
9564 i = (int)STRLEN(buffer);
9565 get_rel_pos(wp, buffer + i + 1);
9566 o = i + vim_strsize(buffer + i + 1);
9567#ifdef FEAT_WINDOWS
9568 if (wp->w_status_height == 0) /* can't use last char of screen */
9569#endif
9570 ++o;
9571#ifdef FEAT_VERTSPLIT
9572 this_ru_col = ru_col - (Columns - width);
9573 if (this_ru_col < 0)
9574 this_ru_col = 0;
9575#endif
9576 /* Never use more than half the window/screen width, leave the other
9577 * half for the filename. */
9578 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9579 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9580 if (this_ru_col + o < WITH_WIDTH(width))
9581 {
9582 while (this_ru_col + o < WITH_WIDTH(width))
9583 {
9584#ifdef FEAT_MBYTE
9585 if (has_mbyte)
9586 i += (*mb_char2bytes)(fillchar, buffer + i);
9587 else
9588#endif
9589 buffer[i++] = fillchar;
9590 ++o;
9591 }
9592 get_rel_pos(wp, buffer + i);
9593 }
9594 /* Truncate at window boundary. */
9595#ifdef FEAT_MBYTE
9596 if (has_mbyte)
9597 {
9598 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009599 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
9601 o += (*mb_ptr2cells)(buffer + i);
9602 if (this_ru_col + o > WITH_WIDTH(width))
9603 {
9604 buffer[i] = NUL;
9605 break;
9606 }
9607 }
9608 }
9609 else
9610#endif
9611 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9612 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9613
9614 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9615 i = redraw_cmdline;
9616 screen_fill(row, row + 1,
9617 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9618 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9619 fillchar, fillchar, attr);
9620 /* don't redraw the cmdline because of showing the ruler */
9621 redraw_cmdline = i;
9622 wp->w_ru_cursor = wp->w_cursor;
9623 wp->w_ru_virtcol = wp->w_virtcol;
9624 wp->w_ru_empty = empty_line;
9625 wp->w_ru_topline = wp->w_topline;
9626 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9627#ifdef FEAT_DIFF
9628 wp->w_ru_topfill = wp->w_topfill;
9629#endif
9630 }
9631}
9632#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009633
9634#if defined(FEAT_LINEBREAK) || defined(PROTO)
9635/*
9636 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009637 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009638 * Otherwise it depends on 'numberwidth' and the line count.
9639 */
9640 int
9641number_width(wp)
9642 win_T *wp;
9643{
9644 int n;
9645 linenr_T lnum;
9646
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009647 lnum = wp->w_buffer->b_ml.ml_line_count;
9648 if (lnum == wp->w_nrwidth_line_count)
9649 return wp->w_nrwidth_width;
9650 wp->w_nrwidth_line_count = lnum;
9651
9652 n = 0;
9653 do
9654 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009655 lnum /= 10;
9656 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009657 } while (lnum > 0);
9658
9659 /* 'numberwidth' gives the minimal width plus one */
9660 if (n < wp->w_p_nuw - 1)
9661 n = wp->w_p_nuw - 1;
9662
9663 wp->w_nrwidth_width = n;
9664 return n;
9665}
9666#endif