blob: 781c19270a4fce64029cce4995b0d9e8ec61bbfc [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
40 * called from other places when an immediated screen update is needed.
41 *
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 *
73 * Commands that move the window position must call redraw_later(NOT_VALID).
74 * TODO: should minimize redrawing by scrolling when possible.
75 *
76 * Commands that change everything (e.g., resizing the screen) must call
77 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
78 *
79 * Things that are handled indirectly:
80 * - When messages scroll the screen up, msg_scrolled will be set and
81 * update_screen() called to redraw.
82 */
83
84#include "vim.h"
85
86/*
87 * The attributes that are actually active for writing to the screen.
88 */
89static int screen_attr = 0;
90
91/*
92 * Positioning the cursor is reduced by remembering the last position.
93 * Mostly used by windgoto() and screen_char().
94 */
95static int screen_cur_row, screen_cur_col; /* last known cursor position */
96
97#ifdef FEAT_SEARCH_EXTRA
98/*
99 * Struct used for highlighting 'hlsearch' matches for the last use search
100 * pattern or a ":match" item.
101 * For 'hlsearch' there is one pattern for all windows. For ":match" there is
102 * a different pattern for each window.
103 */
104typedef struct
105{
106 regmmatch_T rm; /* points to the regexp program; contains last found
107 match (may continue in next line) */
108 buf_T *buf; /* the buffer to search for a match */
109 linenr_T lnum; /* the line to search for a match */
110 int attr; /* attributes to be used for a match */
111 int attr_cur; /* attributes currently active in win_line() */
112 linenr_T first_lnum; /* first lnum to search for multi-line pat */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000113 colnr_T startcol; /* in win_line() points to char where HL starts */
114 colnr_T endcol; /* in win_line() points to char where HL ends */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115} match_T;
116
117static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000118static match_T match_hl[3]; /* used for ":match" highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#endif
120
121#ifdef FEAT_FOLDING
122static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
123#endif
124
125/*
126 * Buffer for one screen line (characters and attributes).
127 */
128static schar_T *current_ScreenLine;
129
130static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000131static 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 +0000132#ifdef FEAT_FOLDING
133static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
134static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
135static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
136#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000137static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
139#ifdef FEAT_RIGHTLEFT
140static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
141# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
142#else
143static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
144# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#ifdef FEAT_VERTSPLIT
147static void draw_vsep_win __ARGS((win_T *wp, int row));
148#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000149#ifdef FEAT_STL_OPT
150static void redraw_custum_statusline __ARGS((win_T *wp));
151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#ifdef FEAT_SEARCH_EXTRA
153static void start_search_hl __ARGS((void));
154static void end_search_hl __ARGS((void));
155static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
156static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
157#endif
158static void screen_start_highlight __ARGS((int attr));
159static void screen_char __ARGS((unsigned off, int row, int col));
160#ifdef FEAT_MBYTE
161static void screen_char_2 __ARGS((unsigned off, int row, int col));
162#endif
163static void screenclear2 __ARGS((void));
164static void lineclear __ARGS((unsigned off, int width));
165static void lineinvalid __ARGS((unsigned off, int width));
166#ifdef FEAT_VERTSPLIT
167static void linecopy __ARGS((int to, int from, win_T *wp));
168static void redraw_block __ARGS((int row, int end, win_T *wp));
169#endif
170static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
171static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000174static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
177static int fillchar_status __ARGS((int *attr, int is_curwin));
178#endif
179#ifdef FEAT_VERTSPLIT
180static int fillchar_vsep __ARGS((int *attr));
181#endif
182#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000183static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#endif
185#ifdef FEAT_CMDL_INFO
186static void win_redr_ruler __ARGS((win_T *wp, int always));
187#endif
188
189#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
190/* Ugly global: overrule attribute used by screen_char() */
191static int screen_char_attr = 0;
192#endif
193
194/*
195 * Redraw the current window later, with update_screen(type).
196 * Set must_redraw only if not already set to a higher value.
197 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
198 */
199 void
200redraw_later(type)
201 int type;
202{
203 redraw_win_later(curwin, type);
204}
205
206 void
207redraw_win_later(wp, type)
208 win_T *wp;
209 int type;
210{
211 if (wp->w_redr_type < type)
212 {
213 wp->w_redr_type = type;
214 if (type >= NOT_VALID)
215 wp->w_lines_valid = 0;
216 if (must_redraw < type) /* must_redraw is the maximum of all windows */
217 must_redraw = type;
218 }
219}
220
221/*
222 * Force a complete redraw later. Also resets the highlighting. To be used
223 * after executing a shell command that messes up the screen.
224 */
225 void
226redraw_later_clear()
227{
228 redraw_all_later(CLEAR);
229 screen_attr = HL_BOLD | HL_UNDERLINE;
230}
231
232/*
233 * Mark all windows to be redrawn later.
234 */
235 void
236redraw_all_later(type)
237 int type;
238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
251redraw_curbuf_later(type)
252 int type;
253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
258redraw_buf_later(buf, type)
259 buf_T *buf;
260 int type;
261{
262 win_T *wp;
263
264 FOR_ALL_WINDOWS(wp)
265 {
266 if (wp->w_buffer == buf)
267 redraw_win_later(wp, type);
268 }
269}
270
271/*
272 * Changed something in the current window, at buffer line "lnum", that
273 * requires that line and possibly other lines to be redrawn.
274 * Used when entering/leaving Insert mode with the cursor on a folded line.
275 * Used to remove the "$" from a change command.
276 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
277 * may become invalid and the whole window will have to be redrawn.
278 */
279/*ARGSUSED*/
280 void
281redrawWinline(lnum, invalid)
282 linenr_T lnum;
283 int invalid; /* window line height is invalid now */
284{
285#ifdef FEAT_FOLDING
286 int i;
287#endif
288
289 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
290 curwin->w_redraw_top = lnum;
291 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
292 curwin->w_redraw_bot = lnum;
293 redraw_later(VALID);
294
295#ifdef FEAT_FOLDING
296 if (invalid)
297 {
298 /* A w_lines[] entry for this lnum has become invalid. */
299 i = find_wl_entry(curwin, lnum);
300 if (i >= 0)
301 curwin->w_lines[i].wl_valid = FALSE;
302 }
303#endif
304}
305
306/*
307 * update all windows that are editing the current buffer
308 */
309 void
310update_curbuf(type)
311 int type;
312{
313 redraw_curbuf_later(type);
314 update_screen(type);
315}
316
317/*
318 * update_screen()
319 *
320 * Based on the current value of curwin->w_topline, transfer a screenfull
321 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
322 */
323 void
324update_screen(type)
325 int type;
326{
327 win_T *wp;
328 static int did_intro = FALSE;
329#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
330 int did_one;
331#endif
332
333 if (!screen_valid(TRUE))
334 return;
335
336 if (must_redraw)
337 {
338 if (type < must_redraw) /* use maximal type */
339 type = must_redraw;
340 must_redraw = 0;
341 }
342
343 /* Need to update w_lines[]. */
344 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
345 type = NOT_VALID;
346
347 if (!redrawing())
348 {
349 redraw_later(type); /* remember type for next time */
350 must_redraw = type;
351 if (type > INVERTED_ALL)
352 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
353 return;
354 }
355
356 updating_screen = TRUE;
357#ifdef FEAT_SYN_HL
358 ++display_tick; /* let syntax code know we're in a next round of
359 * display updating */
360#endif
361
362 /*
363 * if the screen was scrolled up when displaying a message, scroll it down
364 */
365 if (msg_scrolled)
366 {
367 clear_cmdline = TRUE;
368 if (msg_scrolled > Rows - 5) /* clearing is faster */
369 type = CLEAR;
370 else if (type != CLEAR)
371 {
372 check_for_delay(FALSE);
373 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
374 type = CLEAR;
375 FOR_ALL_WINDOWS(wp)
376 {
377 if (W_WINROW(wp) < msg_scrolled)
378 {
379 if (W_WINROW(wp) + wp->w_height > msg_scrolled
380 && wp->w_redr_type < REDRAW_TOP
381 && wp->w_lines_valid > 0
382 && wp->w_topline == wp->w_lines[0].wl_lnum)
383 {
384 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
385 wp->w_redr_type = REDRAW_TOP;
386 }
387 else
388 {
389 wp->w_redr_type = NOT_VALID;
390#ifdef FEAT_WINDOWS
391 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
392 <= msg_scrolled)
393 wp->w_redr_status = TRUE;
394#endif
395 }
396 }
397 }
398 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000399#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000400 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 }
403 msg_scrolled = 0;
404 need_wait_return = FALSE;
405 }
406
407 /* reset cmdline_row now (may have been changed temporarily) */
408 compute_cmdrow();
409
410 /* Check for changed highlighting */
411 if (need_highlight_changed)
412 highlight_changed();
413
414 if (type == CLEAR) /* first clear screen */
415 {
416 screenclear(); /* will reset clear_cmdline */
417 type = NOT_VALID;
418 }
419
420 if (clear_cmdline) /* going to clear cmdline (done below) */
421 check_for_delay(FALSE);
422
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000423#ifdef FEAT_LINEBREAK
424 /* Force redraw when width of 'number' column changes. */
425 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000426 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000427 curwin->w_redr_type = NOT_VALID;
428#endif
429
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 /*
431 * Only start redrawing if there is really something to do.
432 */
433 if (type == INVERTED)
434 update_curswant();
435 if (curwin->w_redr_type < type
436 && !((type == VALID
437 && curwin->w_lines[0].wl_valid
438#ifdef FEAT_DIFF
439 && curwin->w_topfill == curwin->w_old_topfill
440 && curwin->w_botfill == curwin->w_old_botfill
441#endif
442 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
443#ifdef FEAT_VISUAL
444 || (type == INVERTED
445 && 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
453#ifdef FEAT_SYN_HL
454 /*
455 * Correct stored syntax highlighting info for changes in each displayed
456 * buffer. Each buffer must only be done once.
457 */
458 FOR_ALL_WINDOWS(wp)
459 {
460 if (wp->w_buffer->b_mod_set)
461 {
462# ifdef FEAT_WINDOWS
463 win_T *wwp;
464
465 /* Check if we already did this buffer. */
466 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
467 if (wwp->w_buffer == wp->w_buffer)
468 break;
469# endif
470 if (
471# ifdef FEAT_WINDOWS
472 wwp == wp &&
473# endif
474 syntax_present(wp->w_buffer))
475 syn_stack_apply_changes(wp->w_buffer);
476 }
477 }
478#endif
479
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000480#ifdef FEAT_WINDOWS
481 /* Redraw the tab pages line if needed. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000482 if (redraw_tabline || type >= NOT_VALID)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000483 draw_tabline();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000484#endif
485
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 /*
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
739 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
740 * INVERTED redraw the changed part of the Visual area
741 * INVERTED_ALL redraw the whole Visual area
742 * VALID 1. scroll up/down to adjust for a changed w_topline
743 * 2. update lines at the top when scrolled down
744 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000745 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * b_mod_top and b_mod_bot.
747 * - if wp->w_redraw_top non-zero, redraw lines between
748 * wp->w_redraw_top and wp->w_redr_bot.
749 * - continue redrawing when syntax status is invalid.
750 * 4. if scrolled up, update lines at the bottom.
751 * This results in three areas that may need updating:
752 * top: from first row to top_end (when scrolled down)
753 * mid: from mid_start to mid_end (update inversion or changed text)
754 * bot: from bot_start to last row (when scrolled up)
755 */
756 static void
757win_update(wp)
758 win_T *wp;
759{
760 buf_T *buf = wp->w_buffer;
761 int type;
762 int top_end = 0; /* Below last row of the top area that needs
763 updating. 0 when no top area updating. */
764 int mid_start = 999;/* first row of the mid area that needs
765 updating. 999 when no mid area updating. */
766 int mid_end = 0; /* Below last row of the mid area that needs
767 updating. 0 when no mid area updating. */
768 int bot_start = 999;/* first row of the bot area that needs
769 updating. 999 when no bot area updating */
770#ifdef FEAT_VISUAL
771 int scrolled_down = FALSE; /* TRUE when scrolled down when
772 w_topline got smaller a bit */
773#endif
774#ifdef FEAT_SEARCH_EXTRA
775 int top_to_mod = FALSE; /* redraw above mod_top */
776#endif
777
778 int row; /* current window row to display */
779 linenr_T lnum; /* current buffer lnum to display */
780 int idx; /* current index in w_lines[] */
781 int srow; /* starting row of the current line */
782
783 int eof = FALSE; /* if TRUE, we hit the end of the file */
784 int didline = FALSE; /* if TRUE, we finished the last line */
785 int i;
786 long j;
787 static int recursive = FALSE; /* being called recursively */
788 int old_botline = wp->w_botline;
789#ifdef FEAT_FOLDING
790 long fold_count;
791#endif
792#ifdef FEAT_SYN_HL
793 /* remember what happened to the previous line, to know if
794 * check_visual_highlight() can be used */
795#define DID_NONE 1 /* didn't update a line */
796#define DID_LINE 2 /* updated a normal line */
797#define DID_FOLD 3 /* updated a folded line */
798 int did_update = DID_NONE;
799 linenr_T syntax_last_parsed = 0; /* last parsed text line */
800#endif
801 linenr_T mod_top = 0;
802 linenr_T mod_bot = 0;
803#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
804 int save_got_int;
805#endif
806
807 type = wp->w_redr_type;
808
809 if (type == NOT_VALID)
810 {
811#ifdef FEAT_WINDOWS
812 wp->w_redr_status = TRUE;
813#endif
814 wp->w_lines_valid = 0;
815 }
816
817 /* Window is zero-height: nothing to draw. */
818 if (wp->w_height == 0)
819 {
820 wp->w_redr_type = 0;
821 return;
822 }
823
824#ifdef FEAT_VERTSPLIT
825 /* Window is zero-width: Only need to draw the separator. */
826 if (wp->w_width == 0)
827 {
828 /* draw the vertical separator right of this window */
829 draw_vsep_win(wp, 0);
830 wp->w_redr_type = 0;
831 return;
832 }
833#endif
834
835#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000836 /* Setup for ":match" and 'hlsearch' highlighting. Disable any previous
837 * match */
838 for (i = 0; i < 3; ++i)
839 {
840 match_hl[i].rm = wp->w_match[i];
841 if (wp->w_match_id[i] == 0)
842 match_hl[i].attr = 0;
843 else
844 match_hl[i].attr = syn_id2attr(wp->w_match_id[i]);
845 match_hl[i].buf = buf;
846 match_hl[i].lnum = 0;
847 match_hl[i].first_lnum = 0;
848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 search_hl.buf = buf;
850 search_hl.lnum = 0;
851 search_hl.first_lnum = 0;
852#endif
853
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000854#ifdef FEAT_LINEBREAK
855 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000856 i = wp->w_p_nu ? number_width(wp) : 0;
857 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000858 {
859 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000860 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000861 }
862 else
863#endif
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
866 {
867 /*
868 * When there are both inserted/deleted lines and specific lines to be
869 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
870 * everything (only happens when redrawing is off for while).
871 */
872 type = NOT_VALID;
873 }
874 else
875 {
876 /*
877 * Set mod_top to the first line that needs displaying because of
878 * changes. Set mod_bot to the first line after the changes.
879 */
880 mod_top = wp->w_redraw_top;
881 if (wp->w_redraw_bot != 0)
882 mod_bot = wp->w_redraw_bot + 1;
883 else
884 mod_bot = 0;
885 wp->w_redraw_top = 0; /* reset for next time */
886 wp->w_redraw_bot = 0;
887 if (buf->b_mod_set)
888 {
889 if (mod_top == 0 || mod_top > buf->b_mod_top)
890 {
891 mod_top = buf->b_mod_top;
892#ifdef FEAT_SYN_HL
893 /* Need to redraw lines above the change that may be included
894 * in a pattern match. */
895 if (syntax_present(buf))
896 {
897 mod_top -= buf->b_syn_sync_linebreaks;
898 if (mod_top < 1)
899 mod_top = 1;
900 }
901#endif
902 }
903 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
904 mod_bot = buf->b_mod_bot;
905
906#ifdef FEAT_SEARCH_EXTRA
907 /* When 'hlsearch' is on and using a multi-line search pattern, a
908 * change in one line may make the Search highlighting in a
909 * previous line invalid. Simple solution: redraw all visible
910 * lines above the change.
911 * Same for a ":match" pattern.
912 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000913 if (search_hl.rm.regprog != NULL
914 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000916 else
917 for (i = 0; i < 3; ++i)
918 if (match_hl[i].rm.regprog != NULL
919 && re_multiline(match_hl[i].rm.regprog))
920 {
921 top_to_mod = TRUE;
922 break;
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924#endif
925 }
926#ifdef FEAT_FOLDING
927 if (mod_top != 0 && hasAnyFolding(wp))
928 {
929 linenr_T lnumt, lnumb;
930
931 /*
932 * A change in a line can cause lines above it to become folded or
933 * unfolded. Find the top most buffer line that may be affected.
934 * If the line was previously folded and displayed, get the first
935 * line of that fold. If the line is folded now, get the first
936 * folded line. Use the minimum of these two.
937 */
938
939 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
940 * the line below it. If there is no valid entry, use w_topline.
941 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
942 * to this line. If there is no valid entry, use MAXLNUM. */
943 lnumt = wp->w_topline;
944 lnumb = MAXLNUM;
945 for (i = 0; i < wp->w_lines_valid; ++i)
946 if (wp->w_lines[i].wl_valid)
947 {
948 if (wp->w_lines[i].wl_lastlnum < mod_top)
949 lnumt = wp->w_lines[i].wl_lastlnum + 1;
950 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
951 {
952 lnumb = wp->w_lines[i].wl_lnum;
953 /* When there is a fold column it might need updating
954 * in the next line ("J" just above an open fold). */
955 if (wp->w_p_fdc > 0)
956 ++lnumb;
957 }
958 }
959
960 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
961 if (mod_top > lnumt)
962 mod_top = lnumt;
963
964 /* Now do the same for the bottom line (one above mod_bot). */
965 --mod_bot;
966 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
967 ++mod_bot;
968 if (mod_bot < lnumb)
969 mod_bot = lnumb;
970 }
971#endif
972
973 /* When a change starts above w_topline and the end is below
974 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000975 * If the end of the change is above w_topline: do like no change was
976 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (mod_top != 0 && mod_top < wp->w_topline)
978 {
979 if (mod_bot > wp->w_topline)
980 mod_top = wp->w_topline;
981#ifdef FEAT_SYN_HL
982 else if (syntax_present(buf))
983 top_end = 1;
984#endif
985 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000986
987 /* When line numbers are displayed need to redraw all lines below
988 * inserted/deleted lines. */
989 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
990 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 }
992
993 /*
994 * When only displaying the lines at the top, set top_end. Used when
995 * window has scrolled down for msg_scrolled.
996 */
997 if (type == REDRAW_TOP)
998 {
999 j = 0;
1000 for (i = 0; i < wp->w_lines_valid; ++i)
1001 {
1002 j += wp->w_lines[i].wl_size;
1003 if (j >= wp->w_upd_rows)
1004 {
1005 top_end = j;
1006 break;
1007 }
1008 }
1009 if (top_end == 0)
1010 /* not found (cannot happen?): redraw everything */
1011 type = NOT_VALID;
1012 else
1013 /* top area defined, the rest is VALID */
1014 type = VALID;
1015 }
1016
1017 /*
1018 * If there are no changes on the screen that require a complete redraw,
1019 * handle three cases:
1020 * 1: we are off the top of the screen by a few lines: scroll down
1021 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1022 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1023 * w_lines[] that needs updating.
1024 */
1025 if ((type == VALID || type == INVERTED || type == INVERTED_ALL)
1026#ifdef FEAT_DIFF
1027 && !wp->w_botfill && !wp->w_old_botfill
1028#endif
1029 )
1030 {
1031 if (mod_top != 0 && wp->w_topline == mod_top)
1032 {
1033 /*
1034 * w_topline is the first changed line, the scrolling will be done
1035 * further down.
1036 */
1037 }
1038 else if (wp->w_lines[0].wl_valid
1039 && (wp->w_topline < wp->w_lines[0].wl_lnum
1040#ifdef FEAT_DIFF
1041 || (wp->w_topline == wp->w_lines[0].wl_lnum
1042 && wp->w_topfill > wp->w_old_topfill)
1043#endif
1044 ))
1045 {
1046 /*
1047 * New topline is above old topline: May scroll down.
1048 */
1049#ifdef FEAT_FOLDING
1050 if (hasAnyFolding(wp))
1051 {
1052 linenr_T ln;
1053
1054 /* count the number of lines we are off, counting a sequence
1055 * of folded lines as one */
1056 j = 0;
1057 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1058 {
1059 ++j;
1060 if (j >= wp->w_height - 2)
1061 break;
1062 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1063 }
1064 }
1065 else
1066#endif
1067 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1068 if (j < wp->w_height - 2) /* not too far off */
1069 {
1070 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1071#ifdef FEAT_DIFF
1072 /* insert extra lines for previously invisible filler lines */
1073 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1074 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1075 - wp->w_old_topfill;
1076#endif
1077 if (i < wp->w_height - 2) /* less than a screen off */
1078 {
1079 /*
1080 * Try to insert the correct number of lines.
1081 * If not the last window, delete the lines at the bottom.
1082 * win_ins_lines may fail when the terminal can't do it.
1083 */
1084 if (i > 0)
1085 check_for_delay(FALSE);
1086 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1087 {
1088 if (wp->w_lines_valid != 0)
1089 {
1090 /* Need to update rows that are new, stop at the
1091 * first one that scrolled down. */
1092 top_end = i;
1093#ifdef FEAT_VISUAL
1094 scrolled_down = TRUE;
1095#endif
1096
1097 /* Move the entries that were scrolled, disable
1098 * the entries for the lines to be redrawn. */
1099 if ((wp->w_lines_valid += j) > wp->w_height)
1100 wp->w_lines_valid = wp->w_height;
1101 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1102 wp->w_lines[idx] = wp->w_lines[idx - j];
1103 while (idx >= 0)
1104 wp->w_lines[idx--].wl_valid = FALSE;
1105 }
1106 }
1107 else
1108 mid_start = 0; /* redraw all lines */
1109 }
1110 else
1111 mid_start = 0; /* redraw all lines */
1112 }
1113 else
1114 mid_start = 0; /* redraw all lines */
1115 }
1116 else
1117 {
1118 /*
1119 * New topline is at or below old topline: May scroll up.
1120 * When topline didn't change, find first entry in w_lines[] that
1121 * needs updating.
1122 */
1123
1124 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1125 j = -1;
1126 row = 0;
1127 for (i = 0; i < wp->w_lines_valid; i++)
1128 {
1129 if (wp->w_lines[i].wl_valid
1130 && wp->w_lines[i].wl_lnum == wp->w_topline)
1131 {
1132 j = i;
1133 break;
1134 }
1135 row += wp->w_lines[i].wl_size;
1136 }
1137 if (j == -1)
1138 {
1139 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1140 * lines */
1141 mid_start = 0;
1142 }
1143 else
1144 {
1145 /*
1146 * Try to delete the correct number of lines.
1147 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1148 */
1149#ifdef FEAT_DIFF
1150 /* If the topline didn't change, delete old filler lines,
1151 * otherwise delete filler lines of the new topline... */
1152 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1153 row += wp->w_old_topfill;
1154 else
1155 row += diff_check_fill(wp, wp->w_topline);
1156 /* ... but don't delete new filler lines. */
1157 row -= wp->w_topfill;
1158#endif
1159 if (row > 0)
1160 {
1161 check_for_delay(FALSE);
1162 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1163 bot_start = wp->w_height - row;
1164 else
1165 mid_start = 0; /* redraw all lines */
1166 }
1167 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1168 {
1169 /*
1170 * Skip the lines (below the deleted lines) that are still
1171 * valid and don't need redrawing. Copy their info
1172 * upwards, to compensate for the deleted lines. Set
1173 * bot_start to the first row that needs redrawing.
1174 */
1175 bot_start = 0;
1176 idx = 0;
1177 for (;;)
1178 {
1179 wp->w_lines[idx] = wp->w_lines[j];
1180 /* stop at line that didn't fit, unless it is still
1181 * valid (no lines deleted) */
1182 if (row > 0 && bot_start + row
1183 + (int)wp->w_lines[j].wl_size > wp->w_height)
1184 {
1185 wp->w_lines_valid = idx + 1;
1186 break;
1187 }
1188 bot_start += wp->w_lines[idx++].wl_size;
1189
1190 /* stop at the last valid entry in w_lines[].wl_size */
1191 if (++j >= wp->w_lines_valid)
1192 {
1193 wp->w_lines_valid = idx;
1194 break;
1195 }
1196 }
1197#ifdef FEAT_DIFF
1198 /* Correct the first entry for filler lines at the top
1199 * when it won't get updated below. */
1200 if (wp->w_p_diff && bot_start > 0)
1201 wp->w_lines[0].wl_size =
1202 plines_win_nofill(wp, wp->w_topline, TRUE)
1203 + wp->w_topfill;
1204#endif
1205 }
1206 }
1207 }
1208
1209 /* When starting redraw in the first line, redraw all lines. When
1210 * there is only one window it's probably faster to clear the screen
1211 * first. */
1212 if (mid_start == 0)
1213 {
1214 mid_end = wp->w_height;
1215 if (lastwin == firstwin)
1216 screenclear();
1217 }
1218 }
1219 else
1220 {
1221 /* Not VALID or INVERTED: redraw all lines. */
1222 mid_start = 0;
1223 mid_end = wp->w_height;
1224 }
1225
1226#ifdef FEAT_VISUAL
1227 /* check if we are updating or removing the inverted part */
1228 if ((VIsual_active && buf == curwin->w_buffer)
1229 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1230 {
1231 linenr_T from, to;
1232
1233 if (VIsual_active)
1234 {
1235 if (VIsual_active
1236 && (VIsual_mode != wp->w_old_visual_mode
1237 || type == INVERTED_ALL))
1238 {
1239 /*
1240 * If the type of Visual selection changed, redraw the whole
1241 * selection. Also when the ownership of the X selection is
1242 * gained or lost.
1243 */
1244 if (curwin->w_cursor.lnum < VIsual.lnum)
1245 {
1246 from = curwin->w_cursor.lnum;
1247 to = VIsual.lnum;
1248 }
1249 else
1250 {
1251 from = VIsual.lnum;
1252 to = curwin->w_cursor.lnum;
1253 }
1254 /* redraw more when the cursor moved as well */
1255 if (wp->w_old_cursor_lnum < from)
1256 from = wp->w_old_cursor_lnum;
1257 if (wp->w_old_cursor_lnum > to)
1258 to = wp->w_old_cursor_lnum;
1259 if (wp->w_old_visual_lnum < from)
1260 from = wp->w_old_visual_lnum;
1261 if (wp->w_old_visual_lnum > to)
1262 to = wp->w_old_visual_lnum;
1263 }
1264 else
1265 {
1266 /*
1267 * Find the line numbers that need to be updated: The lines
1268 * between the old cursor position and the current cursor
1269 * position. Also check if the Visual position changed.
1270 */
1271 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1272 {
1273 from = curwin->w_cursor.lnum;
1274 to = wp->w_old_cursor_lnum;
1275 }
1276 else
1277 {
1278 from = wp->w_old_cursor_lnum;
1279 to = curwin->w_cursor.lnum;
1280 if (from == 0) /* Visual mode just started */
1281 from = to;
1282 }
1283
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001284 if (VIsual.lnum != wp->w_old_visual_lnum
1285 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
1287 if (wp->w_old_visual_lnum < from
1288 && wp->w_old_visual_lnum != 0)
1289 from = wp->w_old_visual_lnum;
1290 if (wp->w_old_visual_lnum > to)
1291 to = wp->w_old_visual_lnum;
1292 if (VIsual.lnum < from)
1293 from = VIsual.lnum;
1294 if (VIsual.lnum > to)
1295 to = VIsual.lnum;
1296 }
1297 }
1298
1299 /*
1300 * If in block mode and changed column or curwin->w_curswant:
1301 * update all lines.
1302 * First compute the actual start and end column.
1303 */
1304 if (VIsual_mode == Ctrl_V)
1305 {
1306 colnr_T fromc, toc;
1307
1308 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1309 ++toc;
1310 if (curwin->w_curswant == MAXCOL)
1311 toc = MAXCOL;
1312
1313 if (fromc != wp->w_old_cursor_fcol
1314 || toc != wp->w_old_cursor_lcol)
1315 {
1316 if (from > VIsual.lnum)
1317 from = VIsual.lnum;
1318 if (to < VIsual.lnum)
1319 to = VIsual.lnum;
1320 }
1321 wp->w_old_cursor_fcol = fromc;
1322 wp->w_old_cursor_lcol = toc;
1323 }
1324 }
1325 else
1326 {
1327 /* Use the line numbers of the old Visual area. */
1328 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1329 {
1330 from = wp->w_old_cursor_lnum;
1331 to = wp->w_old_visual_lnum;
1332 }
1333 else
1334 {
1335 from = wp->w_old_visual_lnum;
1336 to = wp->w_old_cursor_lnum;
1337 }
1338 }
1339
1340 /*
1341 * There is no need to update lines above the top of the window.
1342 */
1343 if (from < wp->w_topline)
1344 from = wp->w_topline;
1345
1346 /*
1347 * If we know the value of w_botline, use it to restrict the update to
1348 * the lines that are visible in the window.
1349 */
1350 if (wp->w_valid & VALID_BOTLINE)
1351 {
1352 if (from >= wp->w_botline)
1353 from = wp->w_botline - 1;
1354 if (to >= wp->w_botline)
1355 to = wp->w_botline - 1;
1356 }
1357
1358 /*
1359 * Find the minimal part to be updated.
1360 * Watch out for scrolling that made entries in w_lines[] invalid.
1361 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1362 * top_end; need to redraw from top_end to the "to" line.
1363 * A middle mouse click with a Visual selection may change the text
1364 * above the Visual area and reset wl_valid, do count these for
1365 * mid_end (in srow).
1366 */
1367 if (mid_start > 0)
1368 {
1369 lnum = wp->w_topline;
1370 idx = 0;
1371 srow = 0;
1372 if (scrolled_down)
1373 mid_start = top_end;
1374 else
1375 mid_start = 0;
1376 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1377 {
1378 if (wp->w_lines[idx].wl_valid)
1379 mid_start += wp->w_lines[idx].wl_size;
1380 else if (!scrolled_down)
1381 srow += wp->w_lines[idx].wl_size;
1382 ++idx;
1383# ifdef FEAT_FOLDING
1384 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1385 lnum = wp->w_lines[idx].wl_lnum;
1386 else
1387# endif
1388 ++lnum;
1389 }
1390 srow += mid_start;
1391 mid_end = wp->w_height;
1392 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1393 {
1394 if (wp->w_lines[idx].wl_valid
1395 && wp->w_lines[idx].wl_lnum >= to + 1)
1396 {
1397 /* Only update until first row of this line */
1398 mid_end = srow;
1399 break;
1400 }
1401 srow += wp->w_lines[idx].wl_size;
1402 }
1403 }
1404 }
1405
1406 if (VIsual_active && buf == curwin->w_buffer)
1407 {
1408 wp->w_old_visual_mode = VIsual_mode;
1409 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1410 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001411 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 wp->w_old_curswant = curwin->w_curswant;
1413 }
1414 else
1415 {
1416 wp->w_old_visual_mode = 0;
1417 wp->w_old_cursor_lnum = 0;
1418 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001419 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421#endif /* FEAT_VISUAL */
1422
1423#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1424 /* reset got_int, otherwise regexp won't work */
1425 save_got_int = got_int;
1426 got_int = 0;
1427#endif
1428#ifdef FEAT_FOLDING
1429 win_foldinfo.fi_level = 0;
1430#endif
1431
1432 /*
1433 * Update all the window rows.
1434 */
1435 idx = 0; /* first entry in w_lines[].wl_size */
1436 row = 0;
1437 srow = 0;
1438 lnum = wp->w_topline; /* first line shown in window */
1439 for (;;)
1440 {
1441 /* stop updating when reached the end of the window (check for _past_
1442 * the end of the window is at the end of the loop) */
1443 if (row == wp->w_height)
1444 {
1445 didline = TRUE;
1446 break;
1447 }
1448
1449 /* stop updating when hit the end of the file */
1450 if (lnum > buf->b_ml.ml_line_count)
1451 {
1452 eof = TRUE;
1453 break;
1454 }
1455
1456 /* Remember the starting row of the line that is going to be dealt
1457 * with. It is used further down when the line doesn't fit. */
1458 srow = row;
1459
1460 /*
1461 * Update a line when it is in an area that needs updating, when it
1462 * has changes or w_lines[idx] is invalid.
1463 * bot_start may be halfway a wrapped line after using
1464 * win_del_lines(), check if the current line includes it.
1465 * When syntax folding is being used, the saved syntax states will
1466 * already have been updated, we can't see where the syntax state is
1467 * the same again, just update until the end of the window.
1468 */
1469 if (row < top_end
1470 || (row >= mid_start && row < mid_end)
1471#ifdef FEAT_SEARCH_EXTRA
1472 || top_to_mod
1473#endif
1474 || idx >= wp->w_lines_valid
1475 || (row + wp->w_lines[idx].wl_size > bot_start)
1476 || (mod_top != 0
1477 && (lnum == mod_top
1478 || (lnum >= mod_top
1479 && (lnum < mod_bot
1480#ifdef FEAT_SYN_HL
1481 || did_update == DID_FOLD
1482 || (did_update == DID_LINE
1483 && syntax_present(buf)
1484 && (
1485# ifdef FEAT_FOLDING
1486 (foldmethodIsSyntax(wp)
1487 && hasAnyFolding(wp)) ||
1488# endif
1489 syntax_check_changed(lnum)))
1490#endif
1491 )))))
1492 {
1493#ifdef FEAT_SEARCH_EXTRA
1494 if (lnum == mod_top)
1495 top_to_mod = FALSE;
1496#endif
1497
1498 /*
1499 * When at start of changed lines: May scroll following lines
1500 * up or down to minimize redrawing.
1501 * Don't do this when the change continues until the end.
1502 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1503 */
1504 if (lnum == mod_top
1505 && mod_bot != MAXLNUM
1506 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1507 {
1508 int old_rows = 0;
1509 int new_rows = 0;
1510 int xtra_rows;
1511 linenr_T l;
1512
1513 /* Count the old number of window rows, using w_lines[], which
1514 * should still contain the sizes for the lines as they are
1515 * currently displayed. */
1516 for (i = idx; i < wp->w_lines_valid; ++i)
1517 {
1518 /* Only valid lines have a meaningful wl_lnum. Invalid
1519 * lines are part of the changed area. */
1520 if (wp->w_lines[i].wl_valid
1521 && wp->w_lines[i].wl_lnum == mod_bot)
1522 break;
1523 old_rows += wp->w_lines[i].wl_size;
1524#ifdef FEAT_FOLDING
1525 if (wp->w_lines[i].wl_valid
1526 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1527 {
1528 /* Must have found the last valid entry above mod_bot.
1529 * Add following invalid entries. */
1530 ++i;
1531 while (i < wp->w_lines_valid
1532 && !wp->w_lines[i].wl_valid)
1533 old_rows += wp->w_lines[i++].wl_size;
1534 break;
1535 }
1536#endif
1537 }
1538
1539 if (i >= wp->w_lines_valid)
1540 {
1541 /* We can't find a valid line below the changed lines,
1542 * need to redraw until the end of the window.
1543 * Inserting/deleting lines has no use. */
1544 bot_start = 0;
1545 }
1546 else
1547 {
1548 /* Able to count old number of rows: Count new window
1549 * rows, and may insert/delete lines */
1550 j = idx;
1551 for (l = lnum; l < mod_bot; ++l)
1552 {
1553#ifdef FEAT_FOLDING
1554 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1555 ++new_rows;
1556 else
1557#endif
1558#ifdef FEAT_DIFF
1559 if (l == wp->w_topline)
1560 new_rows += plines_win_nofill(wp, l, TRUE)
1561 + wp->w_topfill;
1562 else
1563#endif
1564 new_rows += plines_win(wp, l, TRUE);
1565 ++j;
1566 if (new_rows > wp->w_height - row - 2)
1567 {
1568 /* it's getting too much, must redraw the rest */
1569 new_rows = 9999;
1570 break;
1571 }
1572 }
1573 xtra_rows = new_rows - old_rows;
1574 if (xtra_rows < 0)
1575 {
1576 /* May scroll text up. If there is not enough
1577 * remaining text or scrolling fails, must redraw the
1578 * rest. If scrolling works, must redraw the text
1579 * below the scrolled text. */
1580 if (row - xtra_rows >= wp->w_height - 2)
1581 mod_bot = MAXLNUM;
1582 else
1583 {
1584 check_for_delay(FALSE);
1585 if (win_del_lines(wp, row,
1586 -xtra_rows, FALSE, FALSE) == FAIL)
1587 mod_bot = MAXLNUM;
1588 else
1589 bot_start = wp->w_height + xtra_rows;
1590 }
1591 }
1592 else if (xtra_rows > 0)
1593 {
1594 /* May scroll text down. If there is not enough
1595 * remaining text of scrolling fails, must redraw the
1596 * rest. */
1597 if (row + xtra_rows >= wp->w_height - 2)
1598 mod_bot = MAXLNUM;
1599 else
1600 {
1601 check_for_delay(FALSE);
1602 if (win_ins_lines(wp, row + old_rows,
1603 xtra_rows, FALSE, FALSE) == FAIL)
1604 mod_bot = MAXLNUM;
1605 else if (top_end > row + old_rows)
1606 /* Scrolled the part at the top that requires
1607 * updating down. */
1608 top_end += xtra_rows;
1609 }
1610 }
1611
1612 /* When not updating the rest, may need to move w_lines[]
1613 * entries. */
1614 if (mod_bot != MAXLNUM && i != j)
1615 {
1616 if (j < i)
1617 {
1618 int x = row + new_rows;
1619
1620 /* move entries in w_lines[] upwards */
1621 for (;;)
1622 {
1623 /* stop at last valid entry in w_lines[] */
1624 if (i >= wp->w_lines_valid)
1625 {
1626 wp->w_lines_valid = j;
1627 break;
1628 }
1629 wp->w_lines[j] = wp->w_lines[i];
1630 /* stop at a line that won't fit */
1631 if (x + (int)wp->w_lines[j].wl_size
1632 > wp->w_height)
1633 {
1634 wp->w_lines_valid = j + 1;
1635 break;
1636 }
1637 x += wp->w_lines[j++].wl_size;
1638 ++i;
1639 }
1640 if (bot_start > x)
1641 bot_start = x;
1642 }
1643 else /* j > i */
1644 {
1645 /* move entries in w_lines[] downwards */
1646 j -= i;
1647 wp->w_lines_valid += j;
1648 if (wp->w_lines_valid > wp->w_height)
1649 wp->w_lines_valid = wp->w_height;
1650 for (i = wp->w_lines_valid; i - j >= idx; --i)
1651 wp->w_lines[i] = wp->w_lines[i - j];
1652
1653 /* The w_lines[] entries for inserted lines are
1654 * now invalid, but wl_size may be used above.
1655 * Reset to zero. */
1656 while (i >= idx)
1657 {
1658 wp->w_lines[i].wl_size = 0;
1659 wp->w_lines[i--].wl_valid = FALSE;
1660 }
1661 }
1662 }
1663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
1665
1666#ifdef FEAT_FOLDING
1667 /*
1668 * When lines are folded, display one line for all of them.
1669 * Otherwise, display normally (can be several display lines when
1670 * 'wrap' is on).
1671 */
1672 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1673 if (fold_count != 0)
1674 {
1675 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1676 ++row;
1677 --fold_count;
1678 wp->w_lines[idx].wl_folded = TRUE;
1679 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1680# ifdef FEAT_SYN_HL
1681 did_update = DID_FOLD;
1682# endif
1683 }
1684 else
1685#endif
1686 if (idx < wp->w_lines_valid
1687 && wp->w_lines[idx].wl_valid
1688 && wp->w_lines[idx].wl_lnum == lnum
1689 && lnum > wp->w_topline
1690 && !(dy_flags & DY_LASTLINE)
1691 && srow + wp->w_lines[idx].wl_size > wp->w_height
1692#ifdef FEAT_DIFF
1693 && diff_check_fill(wp, lnum) == 0
1694#endif
1695 )
1696 {
1697 /* This line is not going to fit. Don't draw anything here,
1698 * will draw "@ " lines below. */
1699 row = wp->w_height + 1;
1700 }
1701 else
1702 {
1703#ifdef FEAT_SEARCH_EXTRA
1704 prepare_search_hl(wp, lnum);
1705#endif
1706#ifdef FEAT_SYN_HL
1707 /* Let the syntax stuff know we skipped a few lines. */
1708 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1709 && syntax_present(buf))
1710 syntax_end_parsing(syntax_last_parsed + 1);
1711#endif
1712
1713 /*
1714 * Display one line.
1715 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001716 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
1718#ifdef FEAT_FOLDING
1719 wp->w_lines[idx].wl_folded = FALSE;
1720 wp->w_lines[idx].wl_lastlnum = lnum;
1721#endif
1722#ifdef FEAT_SYN_HL
1723 did_update = DID_LINE;
1724 syntax_last_parsed = lnum;
1725#endif
1726 }
1727
1728 wp->w_lines[idx].wl_lnum = lnum;
1729 wp->w_lines[idx].wl_valid = TRUE;
1730 if (row > wp->w_height) /* past end of screen */
1731 {
1732 /* we may need the size of that too long line later on */
1733 if (dollar_vcol == 0)
1734 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1735 ++idx;
1736 break;
1737 }
1738 if (dollar_vcol == 0)
1739 wp->w_lines[idx].wl_size = row - srow;
1740 ++idx;
1741#ifdef FEAT_FOLDING
1742 lnum += fold_count + 1;
1743#else
1744 ++lnum;
1745#endif
1746 }
1747 else
1748 {
1749 /* This line does not need updating, advance to the next one */
1750 row += wp->w_lines[idx++].wl_size;
1751 if (row > wp->w_height) /* past end of screen */
1752 break;
1753#ifdef FEAT_FOLDING
1754 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1755#else
1756 ++lnum;
1757#endif
1758#ifdef FEAT_SYN_HL
1759 did_update = DID_NONE;
1760#endif
1761 }
1762
1763 if (lnum > buf->b_ml.ml_line_count)
1764 {
1765 eof = TRUE;
1766 break;
1767 }
1768 }
1769 /*
1770 * End of loop over all window lines.
1771 */
1772
1773
1774 if (idx > wp->w_lines_valid)
1775 wp->w_lines_valid = idx;
1776
1777#ifdef FEAT_SYN_HL
1778 /*
1779 * Let the syntax stuff know we stop parsing here.
1780 */
1781 if (syntax_last_parsed != 0 && syntax_present(buf))
1782 syntax_end_parsing(syntax_last_parsed + 1);
1783#endif
1784
1785 /*
1786 * If we didn't hit the end of the file, and we didn't finish the last
1787 * line we were working on, then the line didn't fit.
1788 */
1789 wp->w_empty_rows = 0;
1790#ifdef FEAT_DIFF
1791 wp->w_filler_rows = 0;
1792#endif
1793 if (!eof && !didline)
1794 {
1795 if (lnum == wp->w_topline)
1796 {
1797 /*
1798 * Single line that does not fit!
1799 * Don't overwrite it, it can be edited.
1800 */
1801 wp->w_botline = lnum + 1;
1802 }
1803#ifdef FEAT_DIFF
1804 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1805 {
1806 /* Window ends in filler lines. */
1807 wp->w_botline = lnum;
1808 wp->w_filler_rows = wp->w_height - srow;
1809 }
1810#endif
1811 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1812 {
1813 /*
1814 * Last line isn't finished: Display "@@@" at the end.
1815 */
1816 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1817 W_WINROW(wp) + wp->w_height,
1818 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1819 '@', '@', hl_attr(HLF_AT));
1820 set_empty_rows(wp, srow);
1821 wp->w_botline = lnum;
1822 }
1823 else
1824 {
1825 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1826 wp->w_botline = lnum;
1827 }
1828 }
1829 else
1830 {
1831#ifdef FEAT_VERTSPLIT
1832 draw_vsep_win(wp, row);
1833#endif
1834 if (eof) /* we hit the end of the file */
1835 {
1836 wp->w_botline = buf->b_ml.ml_line_count + 1;
1837#ifdef FEAT_DIFF
1838 j = diff_check_fill(wp, wp->w_botline);
1839 if (j > 0 && !wp->w_botfill)
1840 {
1841 /*
1842 * Display filler lines at the end of the file
1843 */
1844 if (char2cells(fill_diff) > 1)
1845 i = '-';
1846 else
1847 i = fill_diff;
1848 if (row + j > wp->w_height)
1849 j = wp->w_height - row;
1850 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1851 row += j;
1852 }
1853#endif
1854 }
1855 else if (dollar_vcol == 0)
1856 wp->w_botline = lnum;
1857
1858 /* make sure the rest of the screen is blank */
1859 /* put '~'s on rows that aren't part of the file. */
1860 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1861 }
1862
1863 /* Reset the type of redrawing required, the window has been updated. */
1864 wp->w_redr_type = 0;
1865#ifdef FEAT_DIFF
1866 wp->w_old_topfill = wp->w_topfill;
1867 wp->w_old_botfill = wp->w_botfill;
1868#endif
1869
1870 if (dollar_vcol == 0)
1871 {
1872 /*
1873 * There is a trick with w_botline. If we invalidate it on each
1874 * change that might modify it, this will cause a lot of expensive
1875 * calls to plines() in update_topline() each time. Therefore the
1876 * value of w_botline is often approximated, and this value is used to
1877 * compute the value of w_topline. If the value of w_botline was
1878 * wrong, check that the value of w_topline is correct (cursor is on
1879 * the visible part of the text). If it's not, we need to redraw
1880 * again. Mostly this just means scrolling up a few lines, so it
1881 * doesn't look too bad. Only do this for the current window (where
1882 * changes are relevant).
1883 */
1884 wp->w_valid |= VALID_BOTLINE;
1885 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1886 {
1887 recursive = TRUE;
1888 curwin->w_valid &= ~VALID_TOPLINE;
1889 update_topline(); /* may invalidate w_botline again */
1890 if (must_redraw != 0)
1891 {
1892 /* Don't update for changes in buffer again. */
1893 i = curbuf->b_mod_set;
1894 curbuf->b_mod_set = FALSE;
1895 win_update(curwin);
1896 must_redraw = 0;
1897 curbuf->b_mod_set = i;
1898 }
1899 recursive = FALSE;
1900 }
1901 }
1902
1903#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1904 /* restore got_int, unless CTRL-C was hit while redrawing */
1905 if (!got_int)
1906 got_int = save_got_int;
1907#endif
1908}
1909
1910#ifdef FEAT_SIGNS
1911static int draw_signcolumn __ARGS((win_T *wp));
1912
1913/*
1914 * Return TRUE when window "wp" has a column to draw signs in.
1915 */
1916 static int
1917draw_signcolumn(wp)
1918 win_T *wp;
1919{
1920 return (wp->w_buffer->b_signlist != NULL
1921# ifdef FEAT_NETBEANS_INTG
1922 || usingNetbeans
1923# endif
1924 );
1925}
1926#endif
1927
1928/*
1929 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1930 * as the filler character.
1931 */
1932 static void
1933win_draw_end(wp, c1, c2, row, endrow, hl)
1934 win_T *wp;
1935 int c1;
1936 int c2;
1937 int row;
1938 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001939 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940{
1941#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1942 int n = 0;
1943# define FDC_OFF n
1944#else
1945# define FDC_OFF 0
1946#endif
1947
1948#ifdef FEAT_RIGHTLEFT
1949 if (wp->w_p_rl)
1950 {
1951 /* No check for cmdline window: should never be right-left. */
1952# ifdef FEAT_FOLDING
1953 n = wp->w_p_fdc;
1954
1955 if (n > 0)
1956 {
1957 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001958 if (n > W_WIDTH(wp))
1959 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1961 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
1962 ' ', ' ', hl_attr(HLF_FC));
1963 }
1964# endif
1965# ifdef FEAT_SIGNS
1966 if (draw_signcolumn(wp))
1967 {
1968 int nn = n + 2;
1969
1970 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001971 if (nn > W_WIDTH(wp))
1972 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1974 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
1975 ' ', ' ', hl_attr(HLF_SC));
1976 n = nn;
1977 }
1978# endif
1979 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1980 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
1981 c2, c2, hl_attr(hl));
1982 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1983 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
1984 c1, c2, hl_attr(hl));
1985 }
1986 else
1987#endif
1988 {
1989#ifdef FEAT_CMDWIN
1990 if (cmdwin_type != 0 && wp == curwin)
1991 {
1992 /* draw the cmdline character in the leftmost column */
1993 n = 1;
1994 if (n > wp->w_width)
1995 n = wp->w_width;
1996 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1997 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
1998 cmdwin_type, ' ', hl_attr(HLF_AT));
1999 }
2000#endif
2001#ifdef FEAT_FOLDING
2002 if (wp->w_p_fdc > 0)
2003 {
2004 int nn = n + wp->w_p_fdc;
2005
2006 /* draw the fold column at the left */
2007 if (nn > W_WIDTH(wp))
2008 nn = W_WIDTH(wp);
2009 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2010 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2011 ' ', ' ', hl_attr(HLF_FC));
2012 n = nn;
2013 }
2014#endif
2015#ifdef FEAT_SIGNS
2016 if (draw_signcolumn(wp))
2017 {
2018 int nn = n + 2;
2019
2020 /* draw the sign column after the fold column */
2021 if (nn > W_WIDTH(wp))
2022 nn = W_WIDTH(wp);
2023 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2024 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2025 ' ', ' ', hl_attr(HLF_SC));
2026 n = nn;
2027 }
2028#endif
2029 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2030 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2031 c1, c2, hl_attr(hl));
2032 }
2033 set_empty_rows(wp, row);
2034}
2035
2036#ifdef FEAT_FOLDING
2037/*
2038 * Display one folded line.
2039 */
2040 static void
2041fold_line(wp, fold_count, foldinfo, lnum, row)
2042 win_T *wp;
2043 long fold_count;
2044 foldinfo_T *foldinfo;
2045 linenr_T lnum;
2046 int row;
2047{
2048 char_u buf[51];
2049 pos_T *top, *bot;
2050 linenr_T lnume = lnum + fold_count - 1;
2051 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002052 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 int col;
2055 int txtcol;
2056 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002057 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 /* Build the fold line:
2060 * 1. Add the cmdwin_type for the command-line window
2061 * 2. Add the 'foldcolumn'
2062 * 3. Add the 'number' column
2063 * 4. Compose the text
2064 * 5. Add the text
2065 * 6. set highlighting for the Visual area an other text
2066 */
2067 col = 0;
2068
2069 /*
2070 * 1. Add the cmdwin_type for the command-line window
2071 * Ignores 'rightleft', this window is never right-left.
2072 */
2073#ifdef FEAT_CMDWIN
2074 if (cmdwin_type != 0 && wp == curwin)
2075 {
2076 ScreenLines[off] = cmdwin_type;
2077 ScreenAttrs[off] = hl_attr(HLF_AT);
2078#ifdef FEAT_MBYTE
2079 if (enc_utf8)
2080 ScreenLinesUC[off] = 0;
2081#endif
2082 ++col;
2083 }
2084#endif
2085
2086 /*
2087 * 2. Add the 'foldcolumn'
2088 */
2089 fdc = wp->w_p_fdc;
2090 if (fdc > W_WIDTH(wp) - col)
2091 fdc = W_WIDTH(wp) - col;
2092 if (fdc > 0)
2093 {
2094 fill_foldcolumn(buf, wp, TRUE, lnum);
2095#ifdef FEAT_RIGHTLEFT
2096 if (wp->w_p_rl)
2097 {
2098 int i;
2099
2100 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2101 hl_attr(HLF_FC));
2102 /* reverse the fold column */
2103 for (i = 0; i < fdc; ++i)
2104 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2105 }
2106 else
2107#endif
2108 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2109 col += fdc;
2110 }
2111
2112#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002113# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2114 for (ri = 0; ri < l; ++ri) \
2115 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2116 else \
2117 for (ri = 0; ri < l; ++ri) \
2118 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002120# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2121 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#endif
2123
2124 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002125 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
2127#ifdef FEAT_SIGNS
2128 /* If signs are being displayed, add two spaces. */
2129 if (draw_signcolumn(wp))
2130 {
2131 len = W_WIDTH(wp) - col;
2132 if (len > 0)
2133 {
2134 if (len > 2)
2135 len = 2;
2136# ifdef FEAT_RIGHTLEFT
2137 if (wp->w_p_rl)
2138 /* the line number isn't reversed */
2139 copy_text_attr(off + W_WIDTH(wp) - len - col,
2140 (char_u *)" ", len, hl_attr(HLF_FL));
2141 else
2142# endif
2143 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2144 col += len;
2145 }
2146 }
2147#endif
2148
2149 /*
2150 * 3. Add the 'number' column
2151 */
2152 if (wp->w_p_nu)
2153 {
2154 len = W_WIDTH(wp) - col;
2155 if (len > 0)
2156 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002157 int w = number_width(wp);
2158
2159 if (len > w + 1)
2160 len = w + 1;
2161 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#ifdef FEAT_RIGHTLEFT
2163 if (wp->w_p_rl)
2164 /* the line number isn't reversed */
2165 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2166 hl_attr(HLF_FL));
2167 else
2168#endif
2169 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2170 col += len;
2171 }
2172 }
2173
2174 /*
2175 * 4. Compose the folded-line string with 'foldtext', if set.
2176 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002177 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179 txtcol = col; /* remember where text starts */
2180
2181 /*
2182 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2183 * Right-left text is put in columns 0 - number-col, normal text is put
2184 * in columns number-col - window-width.
2185 */
2186#ifdef FEAT_MBYTE
2187 if (has_mbyte)
2188 {
2189 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002190 int u8c, u8cc[MAX_MCO];
2191 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 int idx;
2193 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002194 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195# ifdef FEAT_ARABIC
2196 int prev_c = 0; /* previous Arabic character */
2197 int prev_c1 = 0; /* first composing char for prev_c */
2198# endif
2199
2200# ifdef FEAT_RIGHTLEFT
2201 if (wp->w_p_rl)
2202 idx = off;
2203 else
2204# endif
2205 idx = off + col;
2206
2207 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2208 for (p = text; *p != NUL; )
2209 {
2210 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002211 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if (col + cells > W_WIDTH(wp)
2213# ifdef FEAT_RIGHTLEFT
2214 - (wp->w_p_rl ? col : 0)
2215# endif
2216 )
2217 break;
2218 ScreenLines[idx] = *p;
2219 if (enc_utf8)
2220 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002221 u8c = utfc_ptr2char(p, u8cc);
2222 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224 ScreenLinesUC[idx] = 0;
2225#ifdef FEAT_ARABIC
2226 prev_c = u8c;
2227#endif
2228 }
2229 else
2230 {
2231#ifdef FEAT_ARABIC
2232 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2233 {
2234 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002235 int pc, pc1, nc;
2236 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 int firstbyte = *p;
2238
2239 /* The idea of what is the previous and next
2240 * character depends on 'rightleft'. */
2241 if (wp->w_p_rl)
2242 {
2243 pc = prev_c;
2244 pc1 = prev_c1;
2245 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002246 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
2248 else
2249 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002250 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002252 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254 prev_c = u8c;
2255
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002256 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 pc, pc1, nc);
2258 ScreenLines[idx] = firstbyte;
2259 }
2260 else
2261 prev_c = u8c;
2262#endif
2263 /* Non-BMP character: display as ? or fullwidth ?. */
2264 if (u8c >= 0x10000)
2265 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2266 else
2267 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002268 for (i = 0; i < Screen_mco; ++i)
2269 {
2270 ScreenLinesC[i][idx] = u8cc[i];
2271 if (u8cc[i] == 0)
2272 break;
2273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275 if (cells > 1)
2276 ScreenLines[idx + 1] = 0;
2277 }
2278 else if (cells > 1) /* double-byte character */
2279 {
2280 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2281 ScreenLines2[idx] = p[1];
2282 else
2283 ScreenLines[idx + 1] = p[1];
2284 }
2285 col += cells;
2286 idx += cells;
2287 p += c_len;
2288 }
2289 }
2290 else
2291#endif
2292 {
2293 len = (int)STRLEN(text);
2294 if (len > W_WIDTH(wp) - col)
2295 len = W_WIDTH(wp) - col;
2296 if (len > 0)
2297 {
2298#ifdef FEAT_RIGHTLEFT
2299 if (wp->w_p_rl)
2300 STRNCPY(current_ScreenLine, text, len);
2301 else
2302#endif
2303 STRNCPY(current_ScreenLine + col, text, len);
2304 col += len;
2305 }
2306 }
2307
2308 /* Fill the rest of the line with the fold filler */
2309#ifdef FEAT_RIGHTLEFT
2310 if (wp->w_p_rl)
2311 col -= txtcol;
2312#endif
2313 while (col < W_WIDTH(wp)
2314#ifdef FEAT_RIGHTLEFT
2315 - (wp->w_p_rl ? txtcol : 0)
2316#endif
2317 )
2318 {
2319#ifdef FEAT_MBYTE
2320 if (enc_utf8)
2321 {
2322 if (fill_fold >= 0x80)
2323 {
2324 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002325 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327 else
2328 ScreenLinesUC[off + col] = 0;
2329 }
2330#endif
2331 ScreenLines[off + col++] = fill_fold;
2332 }
2333
2334 if (text != buf)
2335 vim_free(text);
2336
2337 /*
2338 * 6. set highlighting for the Visual area an other text.
2339 * If all folded lines are in the Visual area, highlight the line.
2340 */
2341#ifdef FEAT_VISUAL
2342 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2343 {
2344 if (ltoreq(curwin->w_cursor, VIsual))
2345 {
2346 /* Visual is after curwin->w_cursor */
2347 top = &curwin->w_cursor;
2348 bot = &VIsual;
2349 }
2350 else
2351 {
2352 /* Visual is before curwin->w_cursor */
2353 top = &VIsual;
2354 bot = &curwin->w_cursor;
2355 }
2356 if (lnum >= top->lnum
2357 && lnume <= bot->lnum
2358 && (VIsual_mode != 'v'
2359 || ((lnum > top->lnum
2360 || (lnum == top->lnum
2361 && top->col == 0))
2362 && (lnume < bot->lnum
2363 || (lnume == bot->lnum
2364 && (bot->col - (*p_sel == 'e'))
2365 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2366 {
2367 if (VIsual_mode == Ctrl_V)
2368 {
2369 /* Visual block mode: highlight the chars part of the block */
2370 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2371 {
2372 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2373 len = wp->w_old_cursor_lcol;
2374 else
2375 len = W_WIDTH(wp) - txtcol;
2376 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002377 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379 }
2380 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002383 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 }
2386 }
2387#endif
2388
2389
2390 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2391 (int)W_WIDTH(wp), FALSE);
2392
2393 /*
2394 * Update w_cline_height and w_cline_folded if the cursor line was
2395 * updated (saves a call to plines() later).
2396 */
2397 if (wp == curwin
2398 && lnum <= curwin->w_cursor.lnum
2399 && lnume >= curwin->w_cursor.lnum)
2400 {
2401 curwin->w_cline_row = row;
2402 curwin->w_cline_height = 1;
2403 curwin->w_cline_folded = TRUE;
2404 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2405 }
2406}
2407
2408/*
2409 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2410 */
2411 static void
2412copy_text_attr(off, buf, len, attr)
2413 int off;
2414 char_u *buf;
2415 int len;
2416 int attr;
2417{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002418 int i;
2419
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 mch_memmove(ScreenLines + off, buf, (size_t)len);
2421# ifdef FEAT_MBYTE
2422 if (enc_utf8)
2423 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2424# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002425 for (i = 0; i < len; ++i)
2426 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427}
2428
2429/*
2430 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002431 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
2433 static void
2434fill_foldcolumn(p, wp, closed, lnum)
2435 char_u *p;
2436 win_T *wp;
2437 int closed; /* TRUE of FALSE */
2438 linenr_T lnum; /* current line number */
2439{
2440 int i = 0;
2441 int level;
2442 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002443 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 /* Init to all spaces. */
2446 copy_spaces(p, (size_t)wp->w_p_fdc);
2447
2448 level = win_foldinfo.fi_level;
2449 if (level > 0)
2450 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002451 /* If there is only one column put more info in it. */
2452 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2453
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 /* If the column is too narrow, we start at the lowest level that
2455 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002456 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (first_level < 1)
2458 first_level = 1;
2459
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002460 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
2462 if (win_foldinfo.fi_lnum == lnum
2463 && first_level + i >= win_foldinfo.fi_low_level)
2464 p[i] = '-';
2465 else if (first_level == 1)
2466 p[i] = '|';
2467 else if (first_level + i <= 9)
2468 p[i] = '0' + first_level + i;
2469 else
2470 p[i] = '>';
2471 if (first_level + i == level)
2472 break;
2473 }
2474 }
2475 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002476 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477}
2478#endif /* FEAT_FOLDING */
2479
2480/*
2481 * Display line "lnum" of window 'wp' on the screen.
2482 * Start at row "startrow", stop when "endrow" is reached.
2483 * wp->w_virtcol needs to be valid.
2484 *
2485 * Return the number of last row the line occupies.
2486 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002487/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002489win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 win_T *wp;
2491 linenr_T lnum;
2492 int startrow;
2493 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002494 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495{
2496 int col; /* visual column on screen */
2497 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2498 int c = 0; /* init for GCC */
2499 long vcol = 0; /* virtual column (for tabs) */
2500 long vcol_prev = -1; /* "vcol" of previous character */
2501 char_u *line; /* current line */
2502 char_u *ptr; /* current position in "line" */
2503 int row; /* row in the window, excl w_winrow */
2504 int screen_row; /* row on the screen, incl w_winrow */
2505
2506 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2507 int n_extra = 0; /* number of extra chars */
2508 char_u *p_extra = NULL; /* string of extra chars */
2509 int c_extra = NUL; /* extra chars, all the same */
2510 int extra_attr = 0; /* attributes when n_extra != 0 */
2511 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2512 displaying lcs_eol at end-of-line */
2513 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2514 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2515
2516 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2517 int saved_n_extra = 0;
2518 char_u *saved_p_extra = NULL;
2519 int saved_c_extra = 0;
2520 int saved_char_attr = 0;
2521
2522 int n_attr = 0; /* chars with special attr */
2523 int saved_attr2 = 0; /* char_attr saved for n_attr */
2524 int n_attr3 = 0; /* chars with overruling special attr */
2525 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2526
2527 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2528
2529 int fromcol, tocol; /* start/end of inverting */
2530 int fromcol_prev = -2; /* start of inverting after cursor */
2531 int noinvcur = FALSE; /* don't invert the cursor */
2532#ifdef FEAT_VISUAL
2533 pos_T *top, *bot;
2534#endif
2535 pos_T pos;
2536 long v;
2537
2538 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002539 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2541 in this line */
2542 int attr = 0; /* attributes for area highlighting */
2543 int area_attr = 0; /* attributes desired by highlighting */
2544 int search_attr = 0; /* attributes desired by 'hlsearch' */
2545#ifdef FEAT_SYN_HL
2546 int syntax_attr = 0; /* attributes desired by syntax */
2547 int has_syntax = FALSE; /* this buffer has syntax highl. */
2548 int save_did_emsg;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002549 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002550# define SPWORDLEN 150
2551 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002552 int nextlinecol = 0; /* column where nextline[] starts */
2553 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002554 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002555 int spell_attr = 0; /* attributes desired by spelling */
2556 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002557 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2558 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002559 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002560 static int cap_col = -1; /* column to check for Cap word */
2561 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002562 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563#endif
2564 int extra_check; /* has syntax or linebreak */
2565#ifdef FEAT_MBYTE
2566 int multi_attr = 0; /* attributes desired by multibyte */
2567 int mb_l = 1; /* multi-byte byte length */
2568 int mb_c = 0; /* decoded multi-byte character */
2569 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002570 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#endif
2572#ifdef FEAT_DIFF
2573 int filler_lines; /* nr of filler lines to be drawn */
2574 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002575 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 int change_start = MAXCOL; /* first col of changed area */
2577 int change_end = -1; /* last col of changed area */
2578#endif
2579 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2580#ifdef FEAT_LINEBREAK
2581 int need_showbreak = FALSE;
2582#endif
2583#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS))
2584# define LINE_ATTR
2585 int line_attr = 0; /* atrribute for the whole line */
2586#endif
2587#ifdef FEAT_SEARCH_EXTRA
2588 match_T *shl; /* points to search_hl or match_hl */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002589#endif
2590#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_MBYTE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002591 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592#endif
2593#ifdef FEAT_ARABIC
2594 int prev_c = 0; /* previous Arabic character */
2595 int prev_c1 = 0; /* first composing char for prev_c */
2596#endif
2597
2598 /* draw_state: items that are drawn in sequence: */
2599#define WL_START 0 /* nothing done yet */
2600#ifdef FEAT_CMDWIN
2601# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2602#else
2603# define WL_CMDLINE WL_START
2604#endif
2605#ifdef FEAT_FOLDING
2606# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2607#else
2608# define WL_FOLD WL_CMDLINE
2609#endif
2610#ifdef FEAT_SIGNS
2611# define WL_SIGN WL_FOLD + 1 /* column for signs */
2612#else
2613# define WL_SIGN WL_FOLD /* column for signs */
2614#endif
2615#define WL_NR WL_SIGN + 1 /* line number */
2616#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2617# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2618#else
2619# define WL_SBR WL_NR
2620#endif
2621#define WL_LINE WL_SBR + 1 /* text in the line */
2622 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002623#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 int feedback_col = 0;
2625 int feedback_old_attr = -1;
2626#endif
2627
2628
2629 if (startrow > endrow) /* past the end already! */
2630 return startrow;
2631
2632 row = startrow;
2633 screen_row = row + W_WINROW(wp);
2634
2635 /*
2636 * To speed up the loop below, set extra_check when there is linebreak,
2637 * trailing white space and/or syntax processing to be done.
2638 */
2639#ifdef FEAT_LINEBREAK
2640 extra_check = wp->w_p_lbr;
2641#else
2642 extra_check = 0;
2643#endif
2644#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002645 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
2647 /* Prepare for syntax highlighting in this line. When there is an
2648 * error, stop syntax highlighting. */
2649 save_did_emsg = did_emsg;
2650 did_emsg = FALSE;
2651 syntax_start(wp, lnum);
2652 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002653 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 else
2655 {
2656 did_emsg = save_did_emsg;
2657 has_syntax = TRUE;
2658 extra_check = TRUE;
2659 }
2660 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002661
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002662 if (wp->w_p_spell
2663 && *wp->w_buffer->b_p_spl != NUL
2664 && wp->w_buffer->b_langp.ga_len > 0
2665 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002666 {
2667 /* Prepare for spell checking. */
2668 has_spell = TRUE;
2669 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002670
2671 /* Get the start of the next line, so that words that wrap to the next
2672 * line are found too: "et<line-break>al.".
2673 * Trick: skip a few chars for C/shell/Vim comments */
2674 nextline[SPWORDLEN] = NUL;
2675 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2676 {
2677 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2678 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2679 }
2680
2681 /* When a word wrapped from the previous line the start of the current
2682 * line is valid. */
2683 if (lnum == checked_lnum)
2684 cur_checked_col = checked_col;
2685 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002686
2687 /* When there was a sentence end in the previous line may require a
2688 * word starting with capital in this line. In line 1 always check
2689 * the first word. */
2690 if (lnum != capcol_lnum)
2691 cap_col = -1;
2692 if (lnum == 1)
2693 cap_col = 0;
2694 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696#endif
2697
2698 /*
2699 * handle visual active in this window
2700 */
2701 fromcol = -10;
2702 tocol = MAXCOL;
2703#ifdef FEAT_VISUAL
2704 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2705 {
2706 /* Visual is after curwin->w_cursor */
2707 if (ltoreq(curwin->w_cursor, VIsual))
2708 {
2709 top = &curwin->w_cursor;
2710 bot = &VIsual;
2711 }
2712 else /* Visual is before curwin->w_cursor */
2713 {
2714 top = &VIsual;
2715 bot = &curwin->w_cursor;
2716 }
2717 if (VIsual_mode == Ctrl_V) /* block mode */
2718 {
2719 if (lnum >= top->lnum && lnum <= bot->lnum)
2720 {
2721 fromcol = wp->w_old_cursor_fcol;
2722 tocol = wp->w_old_cursor_lcol;
2723 }
2724 }
2725 else /* non-block mode */
2726 {
2727 if (lnum > top->lnum && lnum <= bot->lnum)
2728 fromcol = 0;
2729 else if (lnum == top->lnum)
2730 {
2731 if (VIsual_mode == 'V') /* linewise */
2732 fromcol = 0;
2733 else
2734 {
2735 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2736 if (gchar_pos(top) == NUL)
2737 tocol = fromcol + 1;
2738 }
2739 }
2740 if (VIsual_mode != 'V' && lnum == bot->lnum)
2741 {
2742 if (*p_sel == 'e' && bot->col == 0
2743#ifdef FEAT_VIRTUALEDIT
2744 && bot->coladd == 0
2745#endif
2746 )
2747 {
2748 fromcol = -10;
2749 tocol = MAXCOL;
2750 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002751 else if (bot->col == MAXCOL)
2752 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 else
2754 {
2755 pos = *bot;
2756 if (*p_sel == 'e')
2757 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2758 else
2759 {
2760 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2761 ++tocol;
2762 }
2763 }
2764 }
2765 }
2766
2767#ifndef MSDOS
2768 /* Check if the character under the cursor should not be inverted */
2769 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2770# ifdef FEAT_GUI
2771 && !gui.in_use
2772# endif
2773 )
2774 noinvcur = TRUE;
2775#endif
2776
2777 /* if inverting in this line set area_highlighting */
2778 if (fromcol >= 0)
2779 {
2780 area_highlighting = TRUE;
2781 attr = hl_attr(HLF_V);
2782#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2783 if (clip_star.available && !clip_star.owned && clip_isautosel())
2784 attr = hl_attr(HLF_VNC);
2785#endif
2786 }
2787 }
2788
2789 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002790 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 */
2792 else
2793#endif /* FEAT_VISUAL */
2794 if (highlight_match
2795 && wp == curwin
2796 && lnum >= curwin->w_cursor.lnum
2797 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2798 {
2799 if (lnum == curwin->w_cursor.lnum)
2800 getvcol(curwin, &(curwin->w_cursor),
2801 (colnr_T *)&fromcol, NULL, NULL);
2802 else
2803 fromcol = 0;
2804 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2805 {
2806 pos.lnum = lnum;
2807 pos.col = search_match_endcol;
2808 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2809 }
2810 else
2811 tocol = MAXCOL;
2812 if (fromcol == tocol) /* do at least one character */
2813 tocol = fromcol + 1; /* happens when past end of line */
2814 area_highlighting = TRUE;
2815 attr = hl_attr(HLF_I);
2816 }
2817
2818#ifdef FEAT_DIFF
2819 filler_lines = diff_check(wp, lnum);
2820 if (filler_lines < 0)
2821 {
2822 if (filler_lines == -1)
2823 {
2824 if (diff_find_change(wp, lnum, &change_start, &change_end))
2825 diff_hlf = HLF_ADD; /* added line */
2826 else if (change_start == 0)
2827 diff_hlf = HLF_TXD; /* changed text */
2828 else
2829 diff_hlf = HLF_CHD; /* changed line */
2830 }
2831 else
2832 diff_hlf = HLF_ADD; /* added line */
2833 filler_lines = 0;
2834 area_highlighting = TRUE;
2835 }
2836 if (lnum == wp->w_topline)
2837 filler_lines = wp->w_topfill;
2838 filler_todo = filler_lines;
2839#endif
2840
2841#ifdef LINE_ATTR
2842# ifdef FEAT_SIGNS
2843 /* If this line has a sign with line highlighting set line_attr. */
2844 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2845 if (v != 0)
2846 line_attr = sign_get_attr((int)v, TRUE);
2847# endif
2848# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2849 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002850 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 line_attr = hl_attr(HLF_L);
2852# endif
2853 if (line_attr != 0)
2854 area_highlighting = TRUE;
2855#endif
2856
2857 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2858 ptr = line;
2859
Bram Moolenaar30abd282005-06-22 22:35:10 +00002860#ifdef FEAT_SYN_HL
2861 if (has_spell)
2862 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002863 /* For checking first word with a capital skip white space. */
2864 if (cap_col == 0)
2865 cap_col = skipwhite(line) - line;
2866
Bram Moolenaar30abd282005-06-22 22:35:10 +00002867 /* To be able to spell-check over line boundaries copy the end of the
2868 * current line into nextline[]. Above the start of the next line was
2869 * copied to nextline[SPWORDLEN]. */
2870 if (nextline[SPWORDLEN] == NUL)
2871 {
2872 /* No next line or it is empty. */
2873 nextlinecol = MAXCOL;
2874 nextline_idx = 0;
2875 }
2876 else
2877 {
2878 v = STRLEN(line);
2879 if (v < SPWORDLEN)
2880 {
2881 /* Short line, use it completely and append the start of the
2882 * next line. */
2883 nextlinecol = 0;
2884 mch_memmove(nextline, line, (size_t)v);
2885 mch_memmove(nextline + v, nextline + SPWORDLEN,
2886 STRLEN(nextline + SPWORDLEN) + 1);
2887 nextline_idx = v + 1;
2888 }
2889 else
2890 {
2891 /* Long line, use only the last SPWORDLEN bytes. */
2892 nextlinecol = v - SPWORDLEN;
2893 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2894 nextline_idx = SPWORDLEN + 1;
2895 }
2896 }
2897 }
2898#endif
2899
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 /* find start of trailing whitespace */
2901 if (wp->w_p_list && lcs_trail)
2902 {
2903 trailcol = (colnr_T)STRLEN(ptr);
2904 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2905 --trailcol;
2906 trailcol += (colnr_T) (ptr - line);
2907 extra_check = TRUE;
2908 }
2909
2910 /*
2911 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2912 * first character to be displayed.
2913 */
2914 if (wp->w_p_wrap)
2915 v = wp->w_skipcol;
2916 else
2917 v = wp->w_leftcol;
2918 if (v > 0)
2919 {
2920#ifdef FEAT_MBYTE
2921 char_u *prev_ptr = ptr;
2922#endif
2923 while (vcol < v && *ptr != NUL)
2924 {
2925 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2926 vcol += c;
2927#ifdef FEAT_MBYTE
2928 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002930 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
2932
2933#ifdef FEAT_VIRTUALEDIT
2934 /* When 'virtualedit' is set the end of the line may be before the
2935 * start of the displayed part. */
2936 if (vcol < v && *ptr == NUL && virtual_active())
2937 vcol = v;
2938#endif
2939
2940 /* Handle a character that's not completely on the screen: Put ptr at
2941 * that character but skip the first few screen characters. */
2942 if (vcol > v)
2943 {
2944 vcol -= c;
2945#ifdef FEAT_MBYTE
2946 ptr = prev_ptr;
2947#else
2948 --ptr;
2949#endif
2950 n_skip = v - vcol;
2951 }
2952
2953 /*
2954 * Adjust for when the inverted text is before the screen,
2955 * and when the start of the inverted text is before the screen.
2956 */
2957 if (tocol <= vcol)
2958 fromcol = 0;
2959 else if (fromcol >= 0 && fromcol < vcol)
2960 fromcol = vcol;
2961
2962#ifdef FEAT_LINEBREAK
2963 /* When w_skipcol is non-zero, first line needs 'showbreak' */
2964 if (wp->w_p_wrap)
2965 need_showbreak = TRUE;
2966#endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00002967#ifdef FEAT_SYN_HL
2968 /* When spell checking a word we need to figure out the start of the
2969 * word and if it's badly spelled or not. */
2970 if (has_spell)
2971 {
2972 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002973 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00002974
2975 pos = wp->w_cursor;
2976 wp->w_cursor.lnum = lnum;
2977 wp->w_cursor.col = ptr - line;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002978 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002979 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00002980 {
2981 /* no bad word found at line start, don't check until end of a
2982 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002983 spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00002984 word_end = spell_to_word_end(ptr, wp->w_buffer) - line + 1;
2985 }
2986 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002987 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00002988 /* bad word found, use attributes until end of word */
2989 word_end = wp->w_cursor.col + len + 1;
2990
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002991 /* Turn index into actual attributes. */
2992 if (spell_hlf != HLF_COUNT)
2993 spell_attr = highlight_attr[spell_hlf];
2994 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00002995 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002996
2997 /* Need to restart syntax highlighting for this line. */
2998 if (has_syntax)
2999 syntax_start(wp, lnum);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003000 }
3001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003
3004 /*
3005 * Correct highlighting for cursor that can't be disabled.
3006 * Avoids having to check this for each character.
3007 */
3008 if (fromcol >= 0)
3009 {
3010 if (noinvcur)
3011 {
3012 if ((colnr_T)fromcol == wp->w_virtcol)
3013 {
3014 /* highlighting starts at cursor, let it start just after the
3015 * cursor */
3016 fromcol_prev = fromcol;
3017 fromcol = -1;
3018 }
3019 else if ((colnr_T)fromcol < wp->w_virtcol)
3020 /* restart highlighting after the cursor */
3021 fromcol_prev = wp->w_virtcol;
3022 }
3023 if (fromcol >= tocol)
3024 fromcol = -1;
3025 }
3026
3027#ifdef FEAT_SEARCH_EXTRA
3028 /*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003029 * Handle highlighting the last used search pattern and ":match".
3030 * Do this for both search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003032 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003034 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003035 shl->startcol = MAXCOL;
3036 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 shl->attr_cur = 0;
3038 if (shl->rm.regprog != NULL)
3039 {
3040 v = (long)(ptr - line);
3041 next_search_hl(wp, shl, lnum, (colnr_T)v);
3042
3043 /* Need to get the line again, a multi-line regexp may have made it
3044 * invalid. */
3045 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3046 ptr = line + v;
3047
3048 if (shl->lnum != 0 && shl->lnum <= lnum)
3049 {
3050 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003051 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003053 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3055 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003056 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003058 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003060 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 {
3062#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003063 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003064 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 else
3066#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003067 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003069 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
3071 shl->attr_cur = shl->attr;
3072 search_attr = shl->attr;
3073 }
3074 area_highlighting = TRUE;
3075 }
3076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
3078#endif
3079
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003080 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 col = 0;
3082#ifdef FEAT_RIGHTLEFT
3083 if (wp->w_p_rl)
3084 {
3085 /* Rightleft window: process the text in the normal direction, but put
3086 * it in current_ScreenLine[] from right to left. Start at the
3087 * rightmost column of the window. */
3088 col = W_WIDTH(wp) - 1;
3089 off += col;
3090 }
3091#endif
3092
3093 /*
3094 * Repeat for the whole displayed line.
3095 */
3096 for (;;)
3097 {
3098 /* Skip this quickly when working on the text. */
3099 if (draw_state != WL_LINE)
3100 {
3101#ifdef FEAT_CMDWIN
3102 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3103 {
3104 draw_state = WL_CMDLINE;
3105 if (cmdwin_type != 0 && wp == curwin)
3106 {
3107 /* Draw the cmdline character. */
3108 *extra = cmdwin_type;
3109 n_extra = 1;
3110 p_extra = extra;
3111 c_extra = NUL;
3112 char_attr = hl_attr(HLF_AT);
3113 }
3114 }
3115#endif
3116
3117#ifdef FEAT_FOLDING
3118 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3119 {
3120 draw_state = WL_FOLD;
3121 if (wp->w_p_fdc > 0)
3122 {
3123 /* Draw the 'foldcolumn'. */
3124 fill_foldcolumn(extra, wp, FALSE, lnum);
3125 n_extra = wp->w_p_fdc;
3126 p_extra = extra;
3127 c_extra = NUL;
3128 char_attr = hl_attr(HLF_FC);
3129 }
3130 }
3131#endif
3132
3133#ifdef FEAT_SIGNS
3134 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3135 {
3136 draw_state = WL_SIGN;
3137 /* Show the sign column when there are any signs in this
3138 * buffer or when using Netbeans. */
3139 if (draw_signcolumn(wp)
3140# ifdef FEAT_DIFF
3141 && filler_todo <= 0
3142# endif
3143 )
3144 {
3145 int_u text_sign;
3146# ifdef FEAT_SIGN_ICONS
3147 int_u icon_sign;
3148# endif
3149
3150 /* Draw two cells with the sign value or blank. */
3151 c_extra = ' ';
3152 char_attr = hl_attr(HLF_SC);
3153 n_extra = 2;
3154
3155 if (row == startrow)
3156 {
3157 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3158 SIGN_TEXT);
3159# ifdef FEAT_SIGN_ICONS
3160 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3161 SIGN_ICON);
3162 if (gui.in_use && icon_sign != 0)
3163 {
3164 /* Use the image in this position. */
3165 c_extra = SIGN_BYTE;
3166# ifdef FEAT_NETBEANS_INTG
3167 if (buf_signcount(wp->w_buffer, lnum) > 1)
3168 c_extra = MULTISIGN_BYTE;
3169# endif
3170 char_attr = icon_sign;
3171 }
3172 else
3173# endif
3174 if (text_sign != 0)
3175 {
3176 p_extra = sign_get_text(text_sign);
3177 if (p_extra != NULL)
3178 {
3179 c_extra = NUL;
3180 n_extra = STRLEN(p_extra);
3181 }
3182 char_attr = sign_get_attr(text_sign, FALSE);
3183 }
3184 }
3185 }
3186 }
3187#endif
3188
3189 if (draw_state == WL_NR - 1 && n_extra == 0)
3190 {
3191 draw_state = WL_NR;
3192 /* Display the line number. After the first fill with blanks
3193 * when the 'n' flag isn't in 'cpo' */
3194 if (wp->w_p_nu
3195 && (row == startrow
3196#ifdef FEAT_DIFF
3197 + filler_lines
3198#endif
3199 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3200 {
3201 /* Draw the line number (empty space after wrapping). */
3202 if (row == startrow
3203#ifdef FEAT_DIFF
3204 + filler_lines
3205#endif
3206 )
3207 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003208 sprintf((char *)extra, "%*ld ",
3209 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (wp->w_skipcol > 0)
3211 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3212 *p_extra = '-';
3213#ifdef FEAT_RIGHTLEFT
3214 if (wp->w_p_rl) /* reverse line numbers */
3215 rl_mirror(extra);
3216#endif
3217 p_extra = extra;
3218 c_extra = NUL;
3219 }
3220 else
3221 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003222 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 char_attr = hl_attr(HLF_N);
3224 }
3225 }
3226
3227#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3228 if (draw_state == WL_SBR - 1 && n_extra == 0)
3229 {
3230 draw_state = WL_SBR;
3231# ifdef FEAT_DIFF
3232 if (filler_todo > 0)
3233 {
3234 /* Draw "deleted" diff line(s). */
3235 if (char2cells(fill_diff) > 1)
3236 c_extra = '-';
3237 else
3238 c_extra = fill_diff;
3239# ifdef FEAT_RIGHTLEFT
3240 if (wp->w_p_rl)
3241 n_extra = col + 1;
3242 else
3243# endif
3244 n_extra = W_WIDTH(wp) - col;
3245 char_attr = hl_attr(HLF_DED);
3246 }
3247# endif
3248# ifdef FEAT_LINEBREAK
3249 if (*p_sbr != NUL && need_showbreak)
3250 {
3251 /* Draw 'showbreak' at the start of each broken line. */
3252 p_extra = p_sbr;
3253 c_extra = NUL;
3254 n_extra = (int)STRLEN(p_sbr);
3255 char_attr = hl_attr(HLF_AT);
3256 need_showbreak = FALSE;
3257 /* Correct end of highlighted area for 'showbreak',
3258 * required when 'linebreak' is also set. */
3259 if (tocol == vcol)
3260 tocol += n_extra;
3261 }
3262# endif
3263 }
3264#endif
3265
3266 if (draw_state == WL_LINE - 1 && n_extra == 0)
3267 {
3268 draw_state = WL_LINE;
3269 if (saved_n_extra)
3270 {
3271 /* Continue item from end of wrapped line. */
3272 n_extra = saved_n_extra;
3273 c_extra = saved_c_extra;
3274 p_extra = saved_p_extra;
3275 char_attr = saved_char_attr;
3276 }
3277 else
3278 char_attr = 0;
3279 }
3280 }
3281
3282 /* When still displaying '$' of change command, stop at cursor */
3283 if (dollar_vcol != 0 && wp == curwin && vcol >= (long)wp->w_virtcol
3284#ifdef FEAT_DIFF
3285 && filler_todo <= 0
3286#endif
3287 )
3288 {
3289 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3290 wp->w_p_rl);
3291 /* Pretend we have finished updating the window. */
3292 row = wp->w_height;
3293 break;
3294 }
3295
3296 if (draw_state == WL_LINE && area_highlighting)
3297 {
3298 /* handle Visual or match highlighting in this line */
3299 if (vcol == fromcol
3300#ifdef FEAT_MBYTE
3301 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3302 && (*mb_ptr2cells)(ptr) > 1)
3303#endif
3304 || ((int)vcol_prev == fromcol_prev
3305 && vcol < tocol))
3306 area_attr = attr; /* start highlighting */
3307 else if (area_attr != 0
3308 && (vcol == tocol
3309 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
3312#ifdef FEAT_SEARCH_EXTRA
3313 if (!n_extra)
3314 {
3315 /*
3316 * Check for start/end of search pattern match.
3317 * After end, check for start/end of next match.
3318 * When another match, have to check for start again.
3319 * Watch out for matching an empty string!
3320 * Do this first for search_hl, then for match_hl, so that
3321 * ":match" overrules 'hlsearch'.
3322 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003323 v = (long)(ptr - line);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003324 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003326 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 while (shl->rm.regprog != NULL)
3328 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003329 if (shl->startcol != MAXCOL
3330 && v >= (long)shl->startcol
3331 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
3333 shl->attr_cur = shl->attr;
3334 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003335 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
3337 shl->attr_cur = 0;
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 next_search_hl(wp, shl, lnum, (colnr_T)v);
3340
3341 /* Need to get the line again, a multi-line regexp
3342 * may have made it invalid. */
3343 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3344 ptr = line + v;
3345
3346 if (shl->lnum == lnum)
3347 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003348 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003350 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003352 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003354 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
3356 /* highlight empty match, try again after
3357 * it */
3358#ifdef FEAT_MBYTE
3359 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003360 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003361 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 else
3363#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003364 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366
3367 /* Loop to check if the match starts at the
3368 * current position */
3369 continue;
3370 }
3371 }
3372 break;
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 /* ":match" highlighting overrules 'hlsearch' */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003377 for (i = 0; i <= 3; ++i)
3378 if (i == 3)
3379 search_attr = search_hl.attr_cur;
3380 else if (match_hl[i].attr_cur != 0)
3381 {
3382 search_attr = match_hl[i].attr_cur;
3383 break;
3384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 }
3386#endif
3387
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003389 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
3391 if (diff_hlf == HLF_CHD && ptr - line >= change_start)
3392 diff_hlf = HLF_TXD; /* changed text */
3393 if (diff_hlf == HLF_TXD && ptr - line > change_end)
3394 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003395 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
3397#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003398
3399 /* Decide which of the highlight attributes to use. */
3400 attr_pri = TRUE;
3401 if (area_attr != 0)
3402 char_attr = area_attr;
3403 else if (search_attr != 0)
3404 char_attr = search_attr;
3405#ifdef LINE_ATTR
3406 /* Use line_attr when not in the Visual or 'incsearch' area
3407 * (area_attr may be 0 when "noinvcur" is set). */
3408 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3409 || (vcol < fromcol || vcol >= tocol)))
3410 char_attr = line_attr;
3411#endif
3412 else
3413 {
3414 attr_pri = FALSE;
3415#ifdef FEAT_SYN_HL
3416 if (has_syntax)
3417 char_attr = syntax_attr;
3418 else
3419#endif
3420 char_attr = 0;
3421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423
3424 /*
3425 * Get the next character to put on the screen.
3426 */
3427 /*
3428 * The 'extra' array contains the extra stuff that is inserted to
3429 * represent special characters (non-printable stuff). When all
3430 * characters are the same, c_extra is used.
3431 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3432 */
3433 if (n_extra > 0)
3434 {
3435 if (c_extra != NUL)
3436 {
3437 c = c_extra;
3438#ifdef FEAT_MBYTE
3439 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3440 if (enc_utf8 && (*mb_char2len)(c) > 1)
3441 {
3442 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003443 u8cc[0] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445 else
3446 mb_utf8 = FALSE;
3447#endif
3448 }
3449 else
3450 {
3451 c = *p_extra;
3452#ifdef FEAT_MBYTE
3453 if (has_mbyte)
3454 {
3455 mb_c = c;
3456 if (enc_utf8)
3457 {
3458 /* If the UTF-8 character is more than one byte:
3459 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003460 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 mb_utf8 = FALSE;
3462 if (mb_l > n_extra)
3463 mb_l = 1;
3464 else if (mb_l > 1)
3465 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003466 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 mb_utf8 = TRUE;
3468 }
3469 }
3470 else
3471 {
3472 /* if this is a DBCS character, put it in "mb_c" */
3473 mb_l = MB_BYTE2LEN(c);
3474 if (mb_l >= n_extra)
3475 mb_l = 1;
3476 else if (mb_l > 1)
3477 mb_c = (c << 8) + p_extra[1];
3478 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003479 if (mb_l == 0) /* at the NUL at end-of-line */
3480 mb_l = 1;
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 /* If a double-width char doesn't fit display a '>' in the
3483 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003484 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485# ifdef FEAT_RIGHTLEFT
3486 wp->w_p_rl ? (col <= 0) :
3487# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003488 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 && (*mb_char2cells)(mb_c) == 2)
3490 {
3491 c = '>';
3492 mb_c = c;
3493 mb_l = 1;
3494 mb_utf8 = FALSE;
3495 multi_attr = hl_attr(HLF_AT);
3496 /* put the pointer back to output the double-width
3497 * character at the start of the next line. */
3498 ++n_extra;
3499 --p_extra;
3500 }
3501 else
3502 {
3503 n_extra -= mb_l - 1;
3504 p_extra += mb_l - 1;
3505 }
3506 }
3507#endif
3508 ++p_extra;
3509 }
3510 --n_extra;
3511 }
3512 else
3513 {
3514 /*
3515 * Get a character from the line itself.
3516 */
3517 c = *ptr;
3518#ifdef FEAT_MBYTE
3519 if (has_mbyte)
3520 {
3521 mb_c = c;
3522 if (enc_utf8)
3523 {
3524 /* If the UTF-8 character is more than one byte: Decode it
3525 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003526 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 mb_utf8 = FALSE;
3528 if (mb_l > 1)
3529 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003530 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 /* Overlong encoded ASCII or ASCII with composing char
3532 * is displayed normally, except a NUL. */
3533 if (mb_c < 0x80)
3534 c = mb_c;
3535 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003536
3537 /* At start of the line we can have a composing char.
3538 * Draw it as a space with a composing char. */
3539 if (utf_iscomposing(mb_c))
3540 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003541 for (i = Screen_mco - 1; i > 0; --i)
3542 u8cc[i] = u8cc[i - 1];
3543 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003544 mb_c = ' ';
3545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
3548 if ((mb_l == 1 && c >= 0x80)
3549 || (mb_l >= 1 && mb_c == 0)
3550 || (mb_l > 1 && (!vim_isprintc(mb_c)
3551 || mb_c >= 0x10000)))
3552 {
3553 /*
3554 * Illegal UTF-8 byte: display as <xx>.
3555 * Non-BMP character : display as ? or fullwidth ?.
3556 */
3557 if (mb_c < 0x10000)
3558 {
3559 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003560# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (wp->w_p_rl) /* reverse */
3562 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 else if (utf_char2cells(mb_c) != 2)
3566 STRCPY(extra, "?");
3567 else
3568 /* 0xff1f in UTF-8: full-width '?' */
3569 STRCPY(extra, "\357\274\237");
3570
3571 p_extra = extra;
3572 c = *p_extra;
3573 mb_c = mb_ptr2char_adv(&p_extra);
3574 mb_utf8 = (c >= 0x80);
3575 n_extra = (int)STRLEN(p_extra);
3576 c_extra = NUL;
3577 if (area_attr == 0 && search_attr == 0)
3578 {
3579 n_attr = n_extra + 1;
3580 extra_attr = hl_attr(HLF_8);
3581 saved_attr2 = char_attr; /* save current attr */
3582 }
3583 }
3584 else if (mb_l == 0) /* at the NUL at end-of-line */
3585 mb_l = 1;
3586#ifdef FEAT_ARABIC
3587 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3588 {
3589 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003590 int pc, pc1, nc;
3591 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
3593 /* The idea of what is the previous and next
3594 * character depends on 'rightleft'. */
3595 if (wp->w_p_rl)
3596 {
3597 pc = prev_c;
3598 pc1 = prev_c1;
3599 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003600 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
3602 else
3603 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003604 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003606 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608 prev_c = mb_c;
3609
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003610 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 else
3613 prev_c = mb_c;
3614#endif
3615 }
3616 else /* enc_dbcs */
3617 {
3618 mb_l = MB_BYTE2LEN(c);
3619 if (mb_l == 0) /* at the NUL at end-of-line */
3620 mb_l = 1;
3621 else if (mb_l > 1)
3622 {
3623 /* We assume a second byte below 32 is illegal.
3624 * Hopefully this is OK for all double-byte encodings!
3625 */
3626 if (ptr[1] >= 32)
3627 mb_c = (c << 8) + ptr[1];
3628 else
3629 {
3630 if (ptr[1] == NUL)
3631 {
3632 /* head byte at end of line */
3633 mb_l = 1;
3634 transchar_nonprint(extra, c);
3635 }
3636 else
3637 {
3638 /* illegal tail byte */
3639 mb_l = 2;
3640 STRCPY(extra, "XX");
3641 }
3642 p_extra = extra;
3643 n_extra = (int)STRLEN(extra) - 1;
3644 c_extra = NUL;
3645 c = *p_extra++;
3646 if (area_attr == 0 && search_attr == 0)
3647 {
3648 n_attr = n_extra + 1;
3649 extra_attr = hl_attr(HLF_8);
3650 saved_attr2 = char_attr; /* save current attr */
3651 }
3652 mb_c = c;
3653 }
3654 }
3655 }
3656 /* If a double-width char doesn't fit display a '>' in the
3657 * last column; the character is displayed at the start of the
3658 * next line. */
3659 if ((
3660# ifdef FEAT_RIGHTLEFT
3661 wp->w_p_rl ? (col <= 0) :
3662# endif
3663 (col >= W_WIDTH(wp) - 1))
3664 && (*mb_char2cells)(mb_c) == 2)
3665 {
3666 c = '>';
3667 mb_c = c;
3668 mb_utf8 = FALSE;
3669 mb_l = 1;
3670 multi_attr = hl_attr(HLF_AT);
3671 /* Put pointer back so that the character will be
3672 * displayed at the start of the next line. */
3673 --ptr;
3674 }
3675 else if (*ptr != NUL)
3676 ptr += mb_l - 1;
3677
3678 /* If a double-width char doesn't fit at the left side display
3679 * a '<' in the first column. */
3680 if (n_skip > 0 && mb_l > 1)
3681 {
3682 extra[0] = '<';
3683 p_extra = extra;
3684 n_extra = 1;
3685 c_extra = NUL;
3686 c = ' ';
3687 if (area_attr == 0 && search_attr == 0)
3688 {
3689 n_attr = n_extra + 1;
3690 extra_attr = hl_attr(HLF_AT);
3691 saved_attr2 = char_attr; /* save current attr */
3692 }
3693 mb_c = c;
3694 mb_utf8 = FALSE;
3695 mb_l = 1;
3696 }
3697
3698 }
3699#endif
3700 ++ptr;
3701
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003702 /* 'list' : change char 160 to lcs_nbsp. */
3703 if (wp->w_p_list && c == 160 && lcs_nbsp)
3704 {
3705 c = lcs_nbsp;
3706 if (area_attr == 0 && search_attr == 0)
3707 {
3708 n_attr = 1;
3709 extra_attr = hl_attr(HLF_8);
3710 saved_attr2 = char_attr; /* save current attr */
3711 }
3712#ifdef FEAT_MBYTE
3713 mb_c = c;
3714 if (enc_utf8 && (*mb_char2len)(c) > 1)
3715 {
3716 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003717 u8cc[0] = 0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003718 }
3719 else
3720 mb_utf8 = FALSE;
3721#endif
3722 }
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (extra_check)
3725 {
3726#ifdef FEAT_SYN_HL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003727 int can_spell = TRUE;
3728
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 /* Get syntax attribute, unless still at the start of the line
3730 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003731 v = (long)(ptr - line);
3732 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
3734 /* Get the syntax attribute for the character. If there
3735 * is an error, disable syntax highlighting. */
3736 save_did_emsg = did_emsg;
3737 did_emsg = FALSE;
3738
Bram Moolenaar217ad922005-03-20 22:37:15 +00003739 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3740 has_spell ? &can_spell : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003743 {
3744 wp->w_buffer->b_syn_error = TRUE;
3745 has_syntax = FALSE;
3746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 else
3748 did_emsg = save_did_emsg;
3749
3750 /* Need to get the line again, a multi-line regexp may
3751 * have made it invalid. */
3752 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3753 ptr = line + v;
3754
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003755 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003757 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003758 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003760
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003761 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003762 * Only do this when there is no syntax highlighting, the
3763 * @Spell cluster is not used or the current syntax item
3764 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003765 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003766 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003767 spell_attr = 0;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003768 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003769 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003770 if (c != 0 && (!has_syntax || can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003771 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003772 char_u *prev_ptr, *p;
3773 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003774 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003775# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003776 if (has_mbyte)
3777 {
3778 prev_ptr = ptr - mb_l;
3779 v -= mb_l - 1;
3780 }
3781 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003782# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003783 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003784
3785 /* Use nextline[] if possible, it has the start of the
3786 * next line concatenated. */
3787 if ((prev_ptr - line) - nextlinecol >= 0)
3788 p = nextline + (prev_ptr - line) - nextlinecol;
3789 else
3790 p = prev_ptr;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003791 cap_col -= (prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003792 len = spell_check(wp, p, &spell_hlf, &cap_col,
3793 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003794 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003795
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003796 /* In Insert mode only highlight a word that
3797 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003798 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003799 && (State & INSERT) != 0
3800 && wp->w_cursor.lnum == lnum
3801 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003802 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003803 && wp->w_cursor.col < (colnr_T)word_end)
3804 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003805 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003806 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003807 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003808
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003809 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003810 && (p - nextline) + len > nextline_idx)
3811 {
3812 /* Remember that the good word continues at the
3813 * start of the next line. */
3814 checked_lnum = lnum + 1;
3815 checked_col = (p - nextline) + len - nextline_idx;
3816 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003817
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003818 /* Turn index into actual attributes. */
3819 if (spell_hlf != HLF_COUNT)
3820 spell_attr = highlight_attr[spell_hlf];
3821
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003822 if (cap_col > 0)
3823 {
3824 if (p != prev_ptr
3825 && (p - nextline) + cap_col >= nextline_idx)
3826 {
3827 /* Remember that the word in the next line
3828 * must start with a capital. */
3829 capcol_lnum = lnum + 1;
3830 cap_col = (p - nextline) + cap_col
3831 - nextline_idx;
3832 }
3833 else
3834 /* Compute the actual column. */
3835 cap_col += (prev_ptr - line);
3836 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003837 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003838 }
3839 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003840 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003841 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003842 char_attr = hl_combine_attr(char_attr, spell_attr);
3843 else
3844 char_attr = hl_combine_attr(spell_attr, char_attr);
3845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#endif
3847#ifdef FEAT_LINEBREAK
3848 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00003849 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 */
3851 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
3852 && !wp->w_p_list)
3853 {
3854 n_extra = win_lbr_chartabsize(wp, ptr - (
3855# ifdef FEAT_MBYTE
3856 has_mbyte ? mb_l :
3857# endif
3858 1), (colnr_T)vcol, NULL) - 1;
3859 c_extra = ' ';
3860 if (vim_iswhite(c))
3861 c = ' ';
3862 }
3863#endif
3864
3865 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3866 {
3867 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003868 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 n_attr = 1;
3871 extra_attr = hl_attr(HLF_8);
3872 saved_attr2 = char_attr; /* save current attr */
3873 }
3874#ifdef FEAT_MBYTE
3875 mb_c = c;
3876 if (enc_utf8 && (*mb_char2len)(c) > 1)
3877 {
3878 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003879 u8cc[0] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881 else
3882 mb_utf8 = FALSE;
3883#endif
3884 }
3885 }
3886
3887 /*
3888 * Handling of non-printable characters.
3889 */
3890 if (!(chartab[c] & CT_PRINT_CHAR))
3891 {
3892 /*
3893 * when getting a character from the file, we may have to
3894 * turn it into something else on the way to putting it
3895 * into "ScreenLines".
3896 */
3897 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3898 {
3899 /* tab amount depends on current column */
3900 n_extra = (int)wp->w_buffer->b_p_ts
3901 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3902#ifdef FEAT_MBYTE
3903 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3904#endif
3905 if (wp->w_p_list)
3906 {
3907 c = lcs_tab1;
3908 c_extra = lcs_tab2;
3909 n_attr = n_extra + 1;
3910 extra_attr = hl_attr(HLF_8);
3911 saved_attr2 = char_attr; /* save current attr */
3912#ifdef FEAT_MBYTE
3913 mb_c = c;
3914 if (enc_utf8 && (*mb_char2len)(c) > 1)
3915 {
3916 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003917 u8cc[0] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919#endif
3920 }
3921 else
3922 {
3923 c_extra = ' ';
3924 c = ' ';
3925 }
3926 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003927 else if (c == NUL
3928 && ((wp->w_p_list && lcs_eol > 0)
3929 || ((fromcol >= 0 || fromcol_prev >= 0)
3930 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003931#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003932 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003933#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003934 && (
3935# ifdef FEAT_RIGHTLEFT
3936 wp->w_p_rl ? (col >= 0) :
3937# endif
3938 (col < W_WIDTH(wp)))
3939 && !(noinvcur
3940 && (colnr_T)vcol == wp->w_virtcol)))
3941 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003943 /* Display a '$' after the line or highlight an extra
3944 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945#if defined(FEAT_DIFF) || defined(LINE_ATTR)
3946 /* For a diff line the highlighting continues after the
3947 * "$". */
3948 if (
3949# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003950 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951# ifdef LINE_ATTR
3952 &&
3953# endif
3954# endif
3955# ifdef LINE_ATTR
3956 line_attr == 0
3957# endif
3958 )
3959#endif
3960 {
3961#ifdef FEAT_VIRTUALEDIT
3962 /* In virtualedit, visual selections may extend
3963 * beyond end of line. */
3964 if (area_highlighting && virtual_active()
3965 && tocol != MAXCOL && vcol < tocol)
3966 n_extra = 0;
3967 else
3968#endif
3969 {
3970 p_extra = at_end_str;
3971 n_extra = 1;
3972 c_extra = NUL;
3973 }
3974 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003975 if (wp->w_p_list)
3976 c = lcs_eol;
3977 else
3978 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 lcs_eol_one = -1;
3980 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003981 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
3983 extra_attr = hl_attr(HLF_AT);
3984 n_attr = 1;
3985 }
3986#ifdef FEAT_MBYTE
3987 mb_c = c;
3988 if (enc_utf8 && (*mb_char2len)(c) > 1)
3989 {
3990 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003991 u8cc[0] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 else
3994 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3995#endif
3996 }
3997 else if (c != NUL)
3998 {
3999 p_extra = transchar(c);
4000#ifdef FEAT_RIGHTLEFT
4001 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4002 rl_mirror(p_extra); /* reverse "<12>" */
4003#endif
4004 n_extra = byte2cells(c) - 1;
4005 c_extra = NUL;
4006 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004007 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
4009 n_attr = n_extra + 1;
4010 extra_attr = hl_attr(HLF_8);
4011 saved_attr2 = char_attr; /* save current attr */
4012 }
4013#ifdef FEAT_MBYTE
4014 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4015#endif
4016 }
4017#ifdef FEAT_VIRTUALEDIT
4018 else if (VIsual_active
4019 && (VIsual_mode == Ctrl_V
4020 || VIsual_mode == 'v')
4021 && virtual_active()
4022 && tocol != MAXCOL
4023 && vcol < tocol
4024 && (
4025# ifdef FEAT_RIGHTLEFT
4026 wp->w_p_rl ? (col >= 0) :
4027# endif
4028 (col < W_WIDTH(wp))))
4029 {
4030 c = ' ';
4031 --ptr; /* put it back at the NUL */
4032 }
4033#endif
4034#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4035 else if ((
4036# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004037 diff_hlf != (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038# ifdef LINE_ATTR
4039 ||
4040# endif
4041# endif
4042# ifdef LINE_ATTR
4043 line_attr != 0
4044# endif
4045 ) && (
4046# ifdef FEAT_RIGHTLEFT
4047 wp->w_p_rl ? (col >= 0) :
4048# endif
4049 (col < W_WIDTH(wp))))
4050 {
4051 /* Highlight until the right side of the window */
4052 c = ' ';
4053 --ptr; /* put it back at the NUL */
4054# ifdef FEAT_DIFF
4055 if (diff_hlf == HLF_TXD)
4056 {
4057 diff_hlf = HLF_CHD;
4058 if (attr == 0 || char_attr != attr)
4059 char_attr = hl_attr(diff_hlf);
4060 }
4061# endif
4062 }
4063#endif
4064 }
4065 }
4066
4067 /* Don't override visual selection highlighting. */
4068 if (n_attr > 0
4069 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004070 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 char_attr = extra_attr;
4072
Bram Moolenaar81695252004-12-29 20:58:21 +00004073#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 /* XIM don't send preedit_start and preedit_end, but they send
4075 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4076 * im_is_preediting() here. */
4077 if (xic != NULL
4078 && lnum == curwin->w_cursor.lnum
4079 && (State & INSERT)
4080 && !p_imdisable
4081 && im_is_preediting()
4082 && draw_state == WL_LINE)
4083 {
4084 colnr_T tcol;
4085
4086 if (preedit_end_col == MAXCOL)
4087 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4088 else
4089 tcol = preedit_end_col;
4090 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4091 {
4092 if (feedback_old_attr < 0)
4093 {
4094 feedback_col = 0;
4095 feedback_old_attr = char_attr;
4096 }
4097 char_attr = im_get_feedback_attr(feedback_col);
4098 if (char_attr < 0)
4099 char_attr = feedback_old_attr;
4100 feedback_col++;
4101 }
4102 else if (feedback_old_attr >= 0)
4103 {
4104 char_attr = feedback_old_attr;
4105 feedback_old_attr = -1;
4106 feedback_col = 0;
4107 }
4108 }
4109#endif
4110 /*
4111 * Handle the case where we are in column 0 but not on the first
4112 * character of the line and the user wants us to show us a
4113 * special character (via 'listchars' option "precedes:<char>".
4114 */
4115 if (lcs_prec_todo != NUL
4116 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4117#ifdef FEAT_DIFF
4118 && filler_todo <= 0
4119#endif
4120 && draw_state > WL_NR
4121 && c != NUL)
4122 {
4123 c = lcs_prec;
4124 lcs_prec_todo = NUL;
4125#ifdef FEAT_MBYTE
4126 mb_c = c;
4127 if (enc_utf8 && (*mb_char2len)(c) > 1)
4128 {
4129 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004130 u8cc[0] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132 else
4133 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4134#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004135 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
4137 saved_attr3 = char_attr; /* save current attr */
4138 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4139 n_attr3 = 1;
4140 }
4141 }
4142
4143 /*
4144 * At end of the text line.
4145 */
4146 if (c == NUL)
4147 {
4148 /* invert at least one char, used for Visual and empty line or
4149 * highlight match at end of line. If it's beyond the last
4150 * char on the screen, just overwrite that one (tricky!) Not
4151 * needed when a '$' was displayed for 'list'. */
4152 if (lcs_eol == lcs_eol_one
4153 && ((area_attr != 0 && vcol == fromcol)
4154#ifdef FEAT_SEARCH_EXTRA
4155 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004156 || (ptr - line) - 1 == (long)search_hl.startcol
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004157 || (ptr - line) - 1 == (long)match_hl[0].startcol
4158 || (ptr - line) - 1 == (long)match_hl[1].startcol
4159 || (ptr - line) - 1 == (long)match_hl[2].startcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160#endif
4161 ))
4162 {
4163 int n = 0;
4164
4165#ifdef FEAT_RIGHTLEFT
4166 if (wp->w_p_rl)
4167 {
4168 if (col < 0)
4169 n = 1;
4170 }
4171 else
4172#endif
4173 {
4174 if (col >= W_WIDTH(wp))
4175 n = -1;
4176 }
4177 if (n != 0)
4178 {
4179 /* At the window boundary, highlight the last character
4180 * instead (better than nothing). */
4181 off += n;
4182 col += n;
4183 }
4184 else
4185 {
4186 /* Add a blank character to highlight. */
4187 ScreenLines[off] = ' ';
4188#ifdef FEAT_MBYTE
4189 if (enc_utf8)
4190 ScreenLinesUC[off] = 0;
4191#endif
4192 }
4193#ifdef FEAT_SEARCH_EXTRA
4194 if (area_attr == 0)
4195 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004196 for (i = 0; i <= 3; ++i)
4197 {
4198 if (i == 3)
4199 char_attr = search_hl.attr;
4200 else if ((ptr - line) - 1 == (long)match_hl[i].startcol)
4201 {
4202 char_attr = match_hl[i].attr;
4203 break;
4204 }
4205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207#endif
4208 ScreenAttrs[off] = char_attr;
4209#ifdef FEAT_RIGHTLEFT
4210 if (wp->w_p_rl)
4211 --col;
4212 else
4213#endif
4214 ++col;
4215 }
4216
4217 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4218 wp->w_p_rl);
4219 row++;
4220
4221 /*
4222 * Update w_cline_height and w_cline_folded if the cursor line was
4223 * updated (saves a call to plines() later).
4224 */
4225 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4226 {
4227 curwin->w_cline_row = startrow;
4228 curwin->w_cline_height = row - startrow;
4229#ifdef FEAT_FOLDING
4230 curwin->w_cline_folded = FALSE;
4231#endif
4232 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4233 }
4234
4235 break;
4236 }
4237
4238 /* line continues beyond line end */
4239 if (lcs_ext
4240 && !wp->w_p_wrap
4241#ifdef FEAT_DIFF
4242 && filler_todo <= 0
4243#endif
4244 && (
4245#ifdef FEAT_RIGHTLEFT
4246 wp->w_p_rl ? col == 0 :
4247#endif
4248 col == W_WIDTH(wp) - 1)
4249 && (*ptr != NUL
4250 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4251 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4252 {
4253 c = lcs_ext;
4254 char_attr = hl_attr(HLF_AT);
4255#ifdef FEAT_MBYTE
4256 mb_c = c;
4257 if (enc_utf8 && (*mb_char2len)(c) > 1)
4258 {
4259 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004260 u8cc[0] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 else
4263 mb_utf8 = FALSE;
4264#endif
4265 }
4266
4267 /*
4268 * Store character to be displayed.
4269 * Skip characters that are left of the screen for 'nowrap'.
4270 */
4271 vcol_prev = vcol;
4272 if (draw_state < WL_LINE || n_skip <= 0)
4273 {
4274 /*
4275 * Store the character.
4276 */
4277#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4278 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4279 {
4280 /* A double-wide character is: put first halve in left cell. */
4281 --off;
4282 --col;
4283 }
4284#endif
4285 ScreenLines[off] = c;
4286#ifdef FEAT_MBYTE
4287 if (enc_dbcs == DBCS_JPNU)
4288 ScreenLines2[off] = mb_c & 0xff;
4289 else if (enc_utf8)
4290 {
4291 if (mb_utf8)
4292 {
4293 ScreenLinesUC[off] = mb_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004294 for (i = 0; i < Screen_mco; ++i)
4295 {
4296 ScreenLinesC[i][off] = u8cc[i];
4297 if (u8cc[i] == 0)
4298 break;
4299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 else
4302 ScreenLinesUC[off] = 0;
4303 }
4304 if (multi_attr)
4305 {
4306 ScreenAttrs[off] = multi_attr;
4307 multi_attr = 0;
4308 }
4309 else
4310#endif
4311 ScreenAttrs[off] = char_attr;
4312
4313#ifdef FEAT_MBYTE
4314 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4315 {
4316 /* Need to fill two screen columns. */
4317 ++off;
4318 ++col;
4319 if (enc_utf8)
4320 /* UTF-8: Put a 0 in the second screen char. */
4321 ScreenLines[off] = 0;
4322 else
4323 /* DBCS: Put second byte in the second screen char. */
4324 ScreenLines[off] = mb_c & 0xff;
4325 ++vcol;
4326 /* When "tocol" is halfway a character, set it to the end of
4327 * the character, otherwise highlighting won't stop. */
4328 if (tocol == vcol)
4329 ++tocol;
4330#ifdef FEAT_RIGHTLEFT
4331 if (wp->w_p_rl)
4332 {
4333 /* now it's time to backup one cell */
4334 --off;
4335 --col;
4336 }
4337#endif
4338 }
4339#endif
4340#ifdef FEAT_RIGHTLEFT
4341 if (wp->w_p_rl)
4342 {
4343 --off;
4344 --col;
4345 }
4346 else
4347#endif
4348 {
4349 ++off;
4350 ++col;
4351 }
4352 }
4353 else
4354 --n_skip;
4355
4356 /* Only advance the "vcol" when after the 'number' column. */
4357 if (draw_state >= WL_SBR
4358#ifdef FEAT_DIFF
4359 && filler_todo <= 0
4360#endif
4361 )
4362 ++vcol;
4363
4364 /* restore attributes after "predeces" in 'listchars' */
4365 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4366 char_attr = saved_attr3;
4367
4368 /* restore attributes after last 'listchars' or 'number' char */
4369 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4370 char_attr = saved_attr2;
4371
4372 /*
4373 * At end of screen line and there is more to come: Display the line
4374 * so far. If there is no more to display it is catched above.
4375 */
4376 if ((
4377#ifdef FEAT_RIGHTLEFT
4378 wp->w_p_rl ? (col < 0) :
4379#endif
4380 (col >= W_WIDTH(wp)))
4381 && (*ptr != NUL
4382#ifdef FEAT_DIFF
4383 || filler_todo > 0
4384#endif
4385 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4386 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4387 )
4388 {
4389 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4390 wp->w_p_rl);
4391 ++row;
4392 ++screen_row;
4393
4394 /* When not wrapping and finished diff lines, or when displayed
4395 * '$' and highlighting until last column, break here. */
4396 if ((!wp->w_p_wrap
4397#ifdef FEAT_DIFF
4398 && filler_todo <= 0
4399#endif
4400 ) || lcs_eol_one == -1)
4401 break;
4402
4403 /* When the window is too narrow draw all "@" lines. */
4404 if (draw_state != WL_LINE
4405#ifdef FEAT_DIFF
4406 && filler_todo <= 0
4407#endif
4408 )
4409 {
4410 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4411#ifdef FEAT_VERTSPLIT
4412 draw_vsep_win(wp, row);
4413#endif
4414 row = endrow;
4415 }
4416
4417 /* When line got too long for screen break here. */
4418 if (row == endrow)
4419 {
4420 ++row;
4421 break;
4422 }
4423
4424 if (screen_cur_row == screen_row - 1
4425#ifdef FEAT_DIFF
4426 && filler_todo <= 0
4427#endif
4428 && W_WIDTH(wp) == Columns)
4429 {
4430 /* Remember that the line wraps, used for modeless copy. */
4431 LineWraps[screen_row - 1] = TRUE;
4432
4433 /*
4434 * Special trick to make copy/paste of wrapped lines work with
4435 * xterm/screen: write an extra character beyond the end of
4436 * the line. This will work with all terminal types
4437 * (regardless of the xn,am settings).
4438 * Only do this on a fast tty.
4439 * Only do this if the cursor is on the current line
4440 * (something has been written in it).
4441 * Don't do this for the GUI.
4442 * Don't do this for double-width characters.
4443 * Don't do this for a window not at the right screen border.
4444 */
4445 if (p_tf
4446#ifdef FEAT_GUI
4447 && !gui.in_use
4448#endif
4449#ifdef FEAT_MBYTE
4450 && !(has_mbyte
4451 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4452 || (*mb_off2cells)(LineOffset[screen_row - 1]
4453 + (int)Columns - 2) == 2))
4454#endif
4455 )
4456 {
4457 /* First make sure we are at the end of the screen line,
4458 * then output the same character again to let the
4459 * terminal know about the wrap. If the terminal doesn't
4460 * auto-wrap, we overwrite the character. */
4461 if (screen_cur_col != W_WIDTH(wp))
4462 screen_char(LineOffset[screen_row - 1]
4463 + (unsigned)Columns - 1,
4464 screen_row - 1, (int)(Columns - 1));
4465
4466#ifdef FEAT_MBYTE
4467 /* When there is a multi-byte character, just output a
4468 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004469 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4470 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 out_char(' ');
4472 else
4473#endif
4474 out_char(ScreenLines[LineOffset[screen_row - 1]
4475 + (Columns - 1)]);
4476 /* force a redraw of the first char on the next line */
4477 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4478 screen_start(); /* don't know where cursor is now */
4479 }
4480 }
4481
4482 col = 0;
4483 off = (unsigned)(current_ScreenLine - ScreenLines);
4484#ifdef FEAT_RIGHTLEFT
4485 if (wp->w_p_rl)
4486 {
4487 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4488 off += col;
4489 }
4490#endif
4491
4492 /* reset the drawing state for the start of a wrapped line */
4493 draw_state = WL_START;
4494 saved_n_extra = n_extra;
4495 saved_p_extra = p_extra;
4496 saved_c_extra = c_extra;
4497 saved_char_attr = char_attr;
4498 n_extra = 0;
4499 lcs_prec_todo = lcs_prec;
4500#ifdef FEAT_LINEBREAK
4501# ifdef FEAT_DIFF
4502 if (filler_todo <= 0)
4503# endif
4504 need_showbreak = TRUE;
4505#endif
4506#ifdef FEAT_DIFF
4507 --filler_todo;
4508 /* When the filler lines are actually below the last line of the
4509 * file, don't draw the line itself, break here. */
4510 if (filler_todo == 0 && wp->w_botfill)
4511 break;
4512#endif
4513 }
4514
4515 } /* for every character in the line */
4516
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004517#ifdef FEAT_SYN_HL
4518 /* After an empty line check first word for capital. */
4519 if (*skipwhite(line) == NUL)
4520 {
4521 capcol_lnum = lnum + 1;
4522 cap_col = 0;
4523 }
4524#endif
4525
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 return row;
4527}
4528
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004529#ifdef FEAT_MBYTE
4530static int comp_char_differs __ARGS((int, int));
4531
4532/*
4533 * Return if the composing characters at "off_from" and "off_to" differ.
4534 */
4535 static int
4536comp_char_differs(off_from, off_to)
4537 int off_from;
4538 int off_to;
4539{
4540 int i;
4541
4542 for (i = 0; i < Screen_mco; ++i)
4543 {
4544 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4545 return TRUE;
4546 if (ScreenLinesC[i][off_from] == 0)
4547 break;
4548 }
4549 return FALSE;
4550}
4551#endif
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553/*
4554 * Check whether the given character needs redrawing:
4555 * - the (first byte of the) character is different
4556 * - the attributes are different
4557 * - the character is multi-byte and the next byte is different
4558 */
4559 static int
4560char_needs_redraw(off_from, off_to, cols)
4561 int off_from;
4562 int off_to;
4563 int cols;
4564{
4565 if (cols > 0
4566 && ((ScreenLines[off_from] != ScreenLines[off_to]
4567 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4568
4569#ifdef FEAT_MBYTE
4570 || (enc_dbcs != 0
4571 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4572 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4573 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4574 : (cols > 1 && ScreenLines[off_from + 1]
4575 != ScreenLines[off_to + 1])))
4576 || (enc_utf8
4577 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4578 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004579 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#endif
4581 ))
4582 return TRUE;
4583 return FALSE;
4584}
4585
4586/*
4587 * Move one "cooked" screen line to the screen, but only the characters that
4588 * have actually changed. Handle insert/delete character.
4589 * "coloff" gives the first column on the screen for this line.
4590 * "endcol" gives the columns where valid characters are.
4591 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4592 * needs to be cleared, negative otherwise.
4593 * "rlflag" is TRUE in a rightleft window:
4594 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4595 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4596 */
4597 static void
4598screen_line(row, coloff, endcol, clear_width
4599#ifdef FEAT_RIGHTLEFT
4600 , rlflag
4601#endif
4602 )
4603 int row;
4604 int coloff;
4605 int endcol;
4606 int clear_width;
4607#ifdef FEAT_RIGHTLEFT
4608 int rlflag;
4609#endif
4610{
4611 unsigned off_from;
4612 unsigned off_to;
4613 int col = 0;
4614#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4615 int hl;
4616#endif
4617 int force = FALSE; /* force update rest of the line */
4618 int redraw_this /* bool: does character need redraw? */
4619#ifdef FEAT_GUI
4620 = TRUE /* For GUI when while-loop empty */
4621#endif
4622 ;
4623 int redraw_next; /* redraw_this for next character */
4624#ifdef FEAT_MBYTE
4625 int clear_next = FALSE;
4626 int char_cells; /* 1: normal char */
4627 /* 2: occupies two display cells */
4628# define CHAR_CELLS char_cells
4629#else
4630# define CHAR_CELLS 1
4631#endif
4632
4633# ifdef FEAT_CLIPBOARD
4634 clip_may_clear_selection(row, row);
4635# endif
4636
4637 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4638 off_to = LineOffset[row] + coloff;
4639
4640#ifdef FEAT_RIGHTLEFT
4641 if (rlflag)
4642 {
4643 /* Clear rest first, because it's left of the text. */
4644 if (clear_width > 0)
4645 {
4646 while (col <= endcol && ScreenLines[off_to] == ' '
4647 && ScreenAttrs[off_to] == 0
4648# ifdef FEAT_MBYTE
4649 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4650# endif
4651 )
4652 {
4653 ++off_to;
4654 ++col;
4655 }
4656 if (col <= endcol)
4657 screen_fill(row, row + 1, col + coloff,
4658 endcol + coloff + 1, ' ', ' ', 0);
4659 }
4660 col = endcol + 1;
4661 off_to = LineOffset[row] + col + coloff;
4662 off_from += col;
4663 endcol = (clear_width > 0 ? clear_width : -clear_width);
4664 }
4665#endif /* FEAT_RIGHTLEFT */
4666
4667 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4668
4669 while (col < endcol)
4670 {
4671#ifdef FEAT_MBYTE
4672 if (has_mbyte && (col + 1 < endcol))
4673 char_cells = (*mb_off2cells)(off_from);
4674 else
4675 char_cells = 1;
4676#endif
4677
4678 redraw_this = redraw_next;
4679 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4680 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4681
4682#ifdef FEAT_GUI
4683 /* If the next character was bold, then redraw the current character to
4684 * remove any pixels that might have spilt over into us. This only
4685 * happens in the GUI.
4686 */
4687 if (redraw_next && gui.in_use)
4688 {
4689 hl = ScreenAttrs[off_to + CHAR_CELLS];
4690 if (hl > HL_ALL || (hl & HL_BOLD))
4691 redraw_this = TRUE;
4692 }
4693#endif
4694
4695 if (redraw_this)
4696 {
4697 /*
4698 * Special handling when 'xs' termcap flag set (hpterm):
4699 * Attributes for characters are stored at the position where the
4700 * cursor is when writing the highlighting code. The
4701 * start-highlighting code must be written with the cursor on the
4702 * first highlighted character. The stop-highlighting code must
4703 * be written with the cursor just after the last highlighted
4704 * character.
4705 * Overwriting a character doesn't remove it's highlighting. Need
4706 * to clear the rest of the line, and force redrawing it
4707 * completely.
4708 */
4709 if ( p_wiv
4710 && !force
4711#ifdef FEAT_GUI
4712 && !gui.in_use
4713#endif
4714 && ScreenAttrs[off_to] != 0
4715 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4716 {
4717 /*
4718 * Need to remove highlighting attributes here.
4719 */
4720 windgoto(row, col + coloff);
4721 out_str(T_CE); /* clear rest of this screen line */
4722 screen_start(); /* don't know where cursor is now */
4723 force = TRUE; /* force redraw of rest of the line */
4724 redraw_next = TRUE; /* or else next char would miss out */
4725
4726 /*
4727 * If the previous character was highlighted, need to stop
4728 * highlighting at this character.
4729 */
4730 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4731 {
4732 screen_attr = ScreenAttrs[off_to - 1];
4733 term_windgoto(row, col + coloff);
4734 screen_stop_highlight();
4735 }
4736 else
4737 screen_attr = 0; /* highlighting has stopped */
4738 }
4739#ifdef FEAT_MBYTE
4740 if (enc_dbcs != 0)
4741 {
4742 /* Check if overwriting a double-byte with a single-byte or
4743 * the other way around requires another character to be
4744 * redrawn. For UTF-8 this isn't needed, because comparing
4745 * ScreenLinesUC[] is sufficient. */
4746 if (char_cells == 1
4747 && col + 1 < endcol
4748 && (*mb_off2cells)(off_to) > 1)
4749 {
4750 /* Writing a single-cell character over a double-cell
4751 * character: need to redraw the next cell. */
4752 ScreenLines[off_to + 1] = 0;
4753 redraw_next = TRUE;
4754 }
4755 else if (char_cells == 2
4756 && col + 2 < endcol
4757 && (*mb_off2cells)(off_to) == 1
4758 && (*mb_off2cells)(off_to + 1) > 1)
4759 {
4760 /* Writing the second half of a double-cell character over
4761 * a double-cell character: need to redraw the second
4762 * cell. */
4763 ScreenLines[off_to + 2] = 0;
4764 redraw_next = TRUE;
4765 }
4766
4767 if (enc_dbcs == DBCS_JPNU)
4768 ScreenLines2[off_to] = ScreenLines2[off_from];
4769 }
4770 /* When writing a single-width character over a double-width
4771 * character and at the end of the redrawn text, need to clear out
4772 * the right halve of the old character.
4773 * Also required when writing the right halve of a double-width
4774 * char over the left halve of an existing one. */
4775 if (has_mbyte && col + char_cells == endcol
4776 && ((char_cells == 1
4777 && (*mb_off2cells)(off_to) > 1)
4778 || (char_cells == 2
4779 && (*mb_off2cells)(off_to) == 1
4780 && (*mb_off2cells)(off_to + 1) > 1)))
4781 clear_next = TRUE;
4782#endif
4783
4784 ScreenLines[off_to] = ScreenLines[off_from];
4785#ifdef FEAT_MBYTE
4786 if (enc_utf8)
4787 {
4788 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4789 if (ScreenLinesUC[off_from] != 0)
4790 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004791 int i;
4792
4793 for (i = 0; i < Screen_mco; ++i)
4794 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796 }
4797 if (char_cells == 2)
4798 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4799#endif
4800
4801#if defined(FEAT_GUI) || defined(UNIX)
4802 /* The bold trick makes a single row of pixels appear in the next
4803 * character. When a bold character is removed, the next
4804 * character should be redrawn too. This happens for our own GUI
4805 * and for some xterms. */
4806 if (
4807# ifdef FEAT_GUI
4808 gui.in_use
4809# endif
4810# if defined(FEAT_GUI) && defined(UNIX)
4811 ||
4812# endif
4813# ifdef UNIX
4814 term_is_xterm
4815# endif
4816 )
4817 {
4818 hl = ScreenAttrs[off_to];
4819 if (hl > HL_ALL || (hl & HL_BOLD))
4820 redraw_next = TRUE;
4821 }
4822#endif
4823 ScreenAttrs[off_to] = ScreenAttrs[off_from];
4824#ifdef FEAT_MBYTE
4825 if (enc_dbcs != 0 && char_cells == 2)
4826 {
4827 /* just a hack: It makes two bytes of DBCS have same attr */
4828 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
4829 screen_char_2(off_to, row, col + coloff);
4830 }
4831 else
4832#endif
4833 screen_char(off_to, row, col + coloff);
4834 }
4835 else if ( p_wiv
4836#ifdef FEAT_GUI
4837 && !gui.in_use
4838#endif
4839 && col + coloff > 0)
4840 {
4841 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
4842 {
4843 /*
4844 * Don't output stop-highlight when moving the cursor, it will
4845 * stop the highlighting when it should continue.
4846 */
4847 screen_attr = 0;
4848 }
4849 else if (screen_attr != 0)
4850 screen_stop_highlight();
4851 }
4852
4853 off_to += CHAR_CELLS;
4854 off_from += CHAR_CELLS;
4855 col += CHAR_CELLS;
4856 }
4857
4858#ifdef FEAT_MBYTE
4859 if (clear_next)
4860 {
4861 /* Clear the second half of a double-wide character of which the left
4862 * half was overwritten with a single-wide character. */
4863 ScreenLines[off_to] = ' ';
4864 if (enc_utf8)
4865 ScreenLinesUC[off_to] = 0;
4866 screen_char(off_to, row, col + coloff);
4867 }
4868#endif
4869
4870 if (clear_width > 0
4871#ifdef FEAT_RIGHTLEFT
4872 && !rlflag
4873#endif
4874 )
4875 {
4876#ifdef FEAT_GUI
4877 int startCol = col;
4878#endif
4879
4880 /* blank out the rest of the line */
4881 while (col < clear_width && ScreenLines[off_to] == ' '
4882 && ScreenAttrs[off_to] == 0
4883#ifdef FEAT_MBYTE
4884 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4885#endif
4886 )
4887 {
4888 ++off_to;
4889 ++col;
4890 }
4891 if (col < clear_width)
4892 {
4893#ifdef FEAT_GUI
4894 /*
4895 * In the GUI, clearing the rest of the line may leave pixels
4896 * behind if the first character cleared was bold. Some bold
4897 * fonts spill over the left. In this case we redraw the previous
4898 * character too. If we didn't skip any blanks above, then we
4899 * only redraw if the character wasn't already redrawn anyway.
4900 */
4901 if (gui.in_use && (col > startCol || !redraw_this)
4902# ifdef FEAT_MBYTE
4903 && enc_dbcs == 0
4904# endif
4905 )
4906 {
4907 hl = ScreenAttrs[off_to];
4908 if (hl > HL_ALL || (hl & HL_BOLD))
4909 screen_char(off_to - 1, row, col + coloff - 1);
4910 }
4911#endif
4912 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
4913 ' ', ' ', 0);
4914#ifdef FEAT_VERTSPLIT
4915 off_to += clear_width - col;
4916 col = clear_width;
4917#endif
4918 }
4919 }
4920
4921 if (clear_width > 0)
4922 {
4923#ifdef FEAT_VERTSPLIT
4924 /* For a window that's left of another, draw the separator char. */
4925 if (col + coloff < Columns)
4926 {
4927 int c;
4928
4929 c = fillchar_vsep(&hl);
4930 if (ScreenLines[off_to] != c
4931# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004932 || (enc_utf8 && (int)ScreenLinesUC[off_to]
4933 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934# endif
4935 || ScreenAttrs[off_to] != hl)
4936 {
4937 ScreenLines[off_to] = c;
4938 ScreenAttrs[off_to] = hl;
4939# ifdef FEAT_MBYTE
4940 if (enc_utf8)
4941 {
4942 if (c >= 0x80)
4943 {
4944 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004945 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947 else
4948 ScreenLinesUC[off_to] = 0;
4949 }
4950# endif
4951 screen_char(off_to, row, col + coloff);
4952 }
4953 }
4954 else
4955#endif
4956 LineWraps[row] = FALSE;
4957 }
4958}
4959
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004960#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004962 * Mirror text "str" for right-left displaying.
4963 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004965 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966rl_mirror(str)
4967 char_u *str;
4968{
4969 char_u *p1, *p2;
4970 int t;
4971
4972 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
4973 {
4974 t = *p1;
4975 *p1 = *p2;
4976 *p2 = t;
4977 }
4978}
4979#endif
4980
4981#if defined(FEAT_WINDOWS) || defined(PROTO)
4982/*
4983 * mark all status lines for redraw; used after first :cd
4984 */
4985 void
4986status_redraw_all()
4987{
4988 win_T *wp;
4989
4990 for (wp = firstwin; wp; wp = wp->w_next)
4991 if (wp->w_status_height)
4992 {
4993 wp->w_redr_status = TRUE;
4994 redraw_later(VALID);
4995 }
4996}
4997
4998/*
4999 * mark all status lines of the current buffer for redraw
5000 */
5001 void
5002status_redraw_curbuf()
5003{
5004 win_T *wp;
5005
5006 for (wp = firstwin; wp; wp = wp->w_next)
5007 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5008 {
5009 wp->w_redr_status = TRUE;
5010 redraw_later(VALID);
5011 }
5012}
5013
5014/*
5015 * Redraw all status lines that need to be redrawn.
5016 */
5017 void
5018redraw_statuslines()
5019{
5020 win_T *wp;
5021
5022 for (wp = firstwin; wp; wp = wp->w_next)
5023 if (wp->w_redr_status)
5024 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005025 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005026 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027}
5028#endif
5029
5030#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5031/*
5032 * Redraw all status lines at the bottom of frame "frp".
5033 */
5034 void
5035win_redraw_last_status(frp)
5036 frame_T *frp;
5037{
5038 if (frp->fr_layout == FR_LEAF)
5039 frp->fr_win->w_redr_status = TRUE;
5040 else if (frp->fr_layout == FR_ROW)
5041 {
5042 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5043 win_redraw_last_status(frp);
5044 }
5045 else /* frp->fr_layout == FR_COL */
5046 {
5047 frp = frp->fr_child;
5048 while (frp->fr_next != NULL)
5049 frp = frp->fr_next;
5050 win_redraw_last_status(frp);
5051 }
5052}
5053#endif
5054
5055#ifdef FEAT_VERTSPLIT
5056/*
5057 * Draw the verticap separator right of window "wp" starting with line "row".
5058 */
5059 static void
5060draw_vsep_win(wp, row)
5061 win_T *wp;
5062 int row;
5063{
5064 int hl;
5065 int c;
5066
5067 if (wp->w_vsep_width)
5068 {
5069 /* draw the vertical separator right of this window */
5070 c = fillchar_vsep(&hl);
5071 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5072 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5073 c, ' ', hl);
5074 }
5075}
5076#endif
5077
5078#ifdef FEAT_WILDMENU
5079static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005080static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
5082/*
5083 * Get the lenght of an item as it will be shown in the status line.
5084 */
5085 static int
5086status_match_len(xp, s)
5087 expand_T *xp;
5088 char_u *s;
5089{
5090 int len = 0;
5091
5092#ifdef FEAT_MENU
5093 int emenu = (xp->xp_context == EXPAND_MENUS
5094 || xp->xp_context == EXPAND_MENUNAMES);
5095
5096 /* Check for menu separators - replace with '|'. */
5097 if (emenu && menu_is_separator(s))
5098 return 1;
5099#endif
5100
5101 while (*s != NUL)
5102 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005103 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005105 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005106 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108
5109 return len;
5110}
5111
5112/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005113 * Return TRUE for characters that are not displayed in a status match.
5114 * These are backslashes used for escaping. Do show backslashes in help tags.
5115 */
5116 static int
5117skip_status_match_char(xp, s)
5118 expand_T *xp;
5119 char_u *s;
5120{
5121 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5122#ifdef FEAT_MENU
5123 || ((xp->xp_context == EXPAND_MENUS
5124 || xp->xp_context == EXPAND_MENUNAMES)
5125 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5126#endif
5127 );
5128}
5129
5130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 * Show wildchar matches in the status line.
5132 * Show at least the "match" item.
5133 * We start at item 'first_match' in the list and show all matches that fit.
5134 *
5135 * If inversion is possible we use it. Else '=' characters are used.
5136 */
5137 void
5138win_redr_status_matches(xp, num_matches, matches, match, showtail)
5139 expand_T *xp;
5140 int num_matches;
5141 char_u **matches; /* list of matches */
5142 int match;
5143 int showtail;
5144{
5145#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5146 int row;
5147 char_u *buf;
5148 int len;
5149 int clen; /* lenght in screen cells */
5150 int fillchar;
5151 int attr;
5152 int i;
5153 int highlight = TRUE;
5154 char_u *selstart = NULL;
5155 int selstart_col = 0;
5156 char_u *selend = NULL;
5157 static int first_match = 0;
5158 int add_left = FALSE;
5159 char_u *s;
5160#ifdef FEAT_MENU
5161 int emenu;
5162#endif
5163#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5164 int l;
5165#endif
5166
5167 if (matches == NULL) /* interrupted completion? */
5168 return;
5169
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005170#ifdef FEAT_MBYTE
5171 if (has_mbyte)
5172 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5173 else
5174#endif
5175 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 if (buf == NULL)
5177 return;
5178
5179 if (match == -1) /* don't show match but original text */
5180 {
5181 match = 0;
5182 highlight = FALSE;
5183 }
5184 /* count 1 for the ending ">" */
5185 clen = status_match_len(xp, L_MATCH(match)) + 3;
5186 if (match == 0)
5187 first_match = 0;
5188 else if (match < first_match)
5189 {
5190 /* jumping left, as far as we can go */
5191 first_match = match;
5192 add_left = TRUE;
5193 }
5194 else
5195 {
5196 /* check if match fits on the screen */
5197 for (i = first_match; i < match; ++i)
5198 clen += status_match_len(xp, L_MATCH(i)) + 2;
5199 if (first_match > 0)
5200 clen += 2;
5201 /* jumping right, put match at the left */
5202 if ((long)clen > Columns)
5203 {
5204 first_match = match;
5205 /* if showing the last match, we can add some on the left */
5206 clen = 2;
5207 for (i = match; i < num_matches; ++i)
5208 {
5209 clen += status_match_len(xp, L_MATCH(i)) + 2;
5210 if ((long)clen >= Columns)
5211 break;
5212 }
5213 if (i == num_matches)
5214 add_left = TRUE;
5215 }
5216 }
5217 if (add_left)
5218 while (first_match > 0)
5219 {
5220 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5221 if ((long)clen >= Columns)
5222 break;
5223 --first_match;
5224 }
5225
5226 fillchar = fillchar_status(&attr, TRUE);
5227
5228 if (first_match == 0)
5229 {
5230 *buf = NUL;
5231 len = 0;
5232 }
5233 else
5234 {
5235 STRCPY(buf, "< ");
5236 len = 2;
5237 }
5238 clen = len;
5239
5240 i = first_match;
5241 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5242 {
5243 if (i == match)
5244 {
5245 selstart = buf + len;
5246 selstart_col = clen;
5247 }
5248
5249 s = L_MATCH(i);
5250 /* Check for menu separators - replace with '|' */
5251#ifdef FEAT_MENU
5252 emenu = (xp->xp_context == EXPAND_MENUS
5253 || xp->xp_context == EXPAND_MENUNAMES);
5254 if (emenu && menu_is_separator(s))
5255 {
5256 STRCPY(buf + len, transchar('|'));
5257 l = (int)STRLEN(buf + len);
5258 len += l;
5259 clen += l;
5260 }
5261 else
5262#endif
5263 for ( ; *s != NUL; ++s)
5264 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005265 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 ++s;
5267 clen += ptr2cells(s);
5268#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005269 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
5271 STRNCPY(buf + len, s, l);
5272 s += l - 1;
5273 len += l;
5274 }
5275 else
5276#endif
5277 {
5278 STRCPY(buf + len, transchar_byte(*s));
5279 len += (int)STRLEN(buf + len);
5280 }
5281 }
5282 if (i == match)
5283 selend = buf + len;
5284
5285 *(buf + len++) = ' ';
5286 *(buf + len++) = ' ';
5287 clen += 2;
5288 if (++i == num_matches)
5289 break;
5290 }
5291
5292 if (i != num_matches)
5293 {
5294 *(buf + len++) = '>';
5295 ++clen;
5296 }
5297
5298 buf[len] = NUL;
5299
5300 row = cmdline_row - 1;
5301 if (row >= 0)
5302 {
5303 if (wild_menu_showing == 0)
5304 {
5305 if (msg_scrolled > 0)
5306 {
5307 /* Put the wildmenu just above the command line. If there is
5308 * no room, scroll the screen one line up. */
5309 if (cmdline_row == Rows - 1)
5310 {
5311 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5312 ++msg_scrolled;
5313 }
5314 else
5315 {
5316 ++cmdline_row;
5317 ++row;
5318 }
5319 wild_menu_showing = WM_SCROLLED;
5320 }
5321 else
5322 {
5323 /* Create status line if needed by setting 'laststatus' to 2.
5324 * Set 'winminheight' to zero to avoid that the window is
5325 * resized. */
5326 if (lastwin->w_status_height == 0)
5327 {
5328 save_p_ls = p_ls;
5329 save_p_wmh = p_wmh;
5330 p_ls = 2;
5331 p_wmh = 0;
5332 last_status(FALSE);
5333 }
5334 wild_menu_showing = WM_SHOWN;
5335 }
5336 }
5337
5338 screen_puts(buf, row, 0, attr);
5339 if (selstart != NULL && highlight)
5340 {
5341 *selend = NUL;
5342 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5343 }
5344
5345 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5346 }
5347
5348#ifdef FEAT_VERTSPLIT
5349 win_redraw_last_status(topframe);
5350#else
5351 lastwin->w_redr_status = TRUE;
5352#endif
5353 vim_free(buf);
5354}
5355#endif
5356
5357#if defined(FEAT_WINDOWS) || defined(PROTO)
5358/*
5359 * Redraw the status line of window wp.
5360 *
5361 * If inversion is possible we use it. Else '=' characters are used.
5362 */
5363 void
5364win_redr_status(wp)
5365 win_T *wp;
5366{
5367 int row;
5368 char_u *p;
5369 int len;
5370 int fillchar;
5371 int attr;
5372 int this_ru_col;
5373
5374 wp->w_redr_status = FALSE;
5375 if (wp->w_status_height == 0)
5376 {
5377 /* no status line, can only be last window */
5378 redraw_cmdline = TRUE;
5379 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005380 else if (!redrawing()
5381#ifdef FEAT_INS_EXPAND
5382 /* don't update status line when popup menu is visible and may be
5383 * drawn over it */
5384 || pum_visible()
5385#endif
5386 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
5388 /* Don't redraw right now, do it later. */
5389 wp->w_redr_status = TRUE;
5390 }
5391#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005392 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
5394 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005395 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397#endif
5398 else
5399 {
5400 fillchar = fillchar_status(&attr, wp == curwin);
5401
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005402 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 p = NameBuff;
5404 len = (int)STRLEN(p);
5405
5406 if (wp->w_buffer->b_help
5407#ifdef FEAT_QUICKFIX
5408 || wp->w_p_pvw
5409#endif
5410 || bufIsChanged(wp->w_buffer)
5411 || wp->w_buffer->b_p_ro)
5412 *(p + len++) = ' ';
5413 if (wp->w_buffer->b_help)
5414 {
5415 STRCPY(p + len, _("[help]"));
5416 len += (int)STRLEN(p + len);
5417 }
5418#ifdef FEAT_QUICKFIX
5419 if (wp->w_p_pvw)
5420 {
5421 STRCPY(p + len, _("[Preview]"));
5422 len += (int)STRLEN(p + len);
5423 }
5424#endif
5425 if (bufIsChanged(wp->w_buffer))
5426 {
5427 STRCPY(p + len, "[+]");
5428 len += 3;
5429 }
5430 if (wp->w_buffer->b_p_ro)
5431 {
5432 STRCPY(p + len, "[RO]");
5433 len += 4;
5434 }
5435
5436#ifndef FEAT_VERTSPLIT
5437 this_ru_col = ru_col;
5438 if (this_ru_col < (Columns + 1) / 2)
5439 this_ru_col = (Columns + 1) / 2;
5440#else
5441 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5442 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5443 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5444 if (this_ru_col <= 1)
5445 {
5446 p = (char_u *)"<"; /* No room for file name! */
5447 len = 1;
5448 }
5449 else
5450#endif
5451#ifdef FEAT_MBYTE
5452 if (has_mbyte)
5453 {
5454 int clen = 0, i;
5455
5456 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005457 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 clen += (*mb_ptr2cells)(p + i);
5459 /* Find first character that will fit.
5460 * Going from start to end is much faster for DBCS. */
5461 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005462 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 clen -= (*mb_ptr2cells)(p + i);
5464 len = clen;
5465 if (i > 0)
5466 {
5467 p = p + i - 1;
5468 *p = '<';
5469 ++len;
5470 }
5471
5472 }
5473 else
5474#endif
5475 if (len > this_ru_col - 1)
5476 {
5477 p += len - (this_ru_col - 1);
5478 *p = '<';
5479 len = this_ru_col - 1;
5480 }
5481
5482 row = W_WINROW(wp) + wp->w_height;
5483 screen_puts(p, row, W_WINCOL(wp), attr);
5484 screen_fill(row, row + 1, len + W_WINCOL(wp),
5485 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5486
5487 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5488 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5489 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5490 - 1 + W_WINCOL(wp)), attr);
5491
5492#ifdef FEAT_CMDL_INFO
5493 win_redr_ruler(wp, TRUE);
5494#endif
5495 }
5496
5497#ifdef FEAT_VERTSPLIT
5498 /*
5499 * May need to draw the character below the vertical separator.
5500 */
5501 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5502 {
5503 if (stl_connected(wp))
5504 fillchar = fillchar_status(&attr, wp == curwin);
5505 else
5506 fillchar = fillchar_vsep(&attr);
5507 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5508 attr);
5509 }
5510#endif
5511}
5512
Bram Moolenaar238a5642006-02-21 22:12:05 +00005513#ifdef FEAT_STL_OPT
5514/*
5515 * Redraw the status line according to 'statusline' and take care of any
5516 * errors encountered.
5517 */
5518 static void
5519redraw_custum_statusline(wp)
5520 win_T *wp;
5521{
5522 int save_called_emsg = called_emsg;
5523
5524 called_emsg = FALSE;
5525 win_redr_custom(wp, FALSE);
5526 if (called_emsg)
5527 set_string_option_direct((char_u *)"statusline", -1,
5528 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005529 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005530 called_emsg |= save_called_emsg;
5531}
5532#endif
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534# ifdef FEAT_VERTSPLIT
5535/*
5536 * Return TRUE if the status line of window "wp" is connected to the status
5537 * line of the window right of it. If not, then it's a vertical separator.
5538 * Only call if (wp->w_vsep_width != 0).
5539 */
5540 int
5541stl_connected(wp)
5542 win_T *wp;
5543{
5544 frame_T *fr;
5545
5546 fr = wp->w_frame;
5547 while (fr->fr_parent != NULL)
5548 {
5549 if (fr->fr_parent->fr_layout == FR_COL)
5550 {
5551 if (fr->fr_next != NULL)
5552 break;
5553 }
5554 else
5555 {
5556 if (fr->fr_next != NULL)
5557 return TRUE;
5558 }
5559 fr = fr->fr_parent;
5560 }
5561 return FALSE;
5562}
5563# endif
5564
5565#endif /* FEAT_WINDOWS */
5566
5567#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5568/*
5569 * Get the value to show for the language mappings, active 'keymap'.
5570 */
5571 int
5572get_keymap_str(wp, buf, len)
5573 win_T *wp;
5574 char_u *buf; /* buffer for the result */
5575 int len; /* length of buffer */
5576{
5577 char_u *p;
5578
5579 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5580 return FALSE;
5581
5582 {
5583#ifdef FEAT_EVAL
5584 buf_T *old_curbuf = curbuf;
5585 win_T *old_curwin = curwin;
5586 char_u *s;
5587
5588 curbuf = wp->w_buffer;
5589 curwin = wp;
5590 STRCPY(buf, "b:keymap_name"); /* must be writable */
5591 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005592 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 --emsg_skip;
5594 curbuf = old_curbuf;
5595 curwin = old_curwin;
5596 if (p == NULL || *p == NUL)
5597#endif
5598 {
5599#ifdef FEAT_KEYMAP
5600 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5601 p = wp->w_buffer->b_p_keymap;
5602 else
5603#endif
5604 p = (char_u *)"lang";
5605 }
5606 if ((int)(STRLEN(p) + 3) < len)
5607 sprintf((char *)buf, "<%s>", p);
5608 else
5609 buf[0] = NUL;
5610#ifdef FEAT_EVAL
5611 vim_free(s);
5612#endif
5613 }
5614 return buf[0] != NUL;
5615}
5616#endif
5617
5618#if defined(FEAT_STL_OPT) || defined(PROTO)
5619/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005620 * Redraw the status line or ruler of window "wp".
5621 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 */
5623 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005624win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005626 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 int attr;
5629 int curattr;
5630 int row;
5631 int col = 0;
5632 int maxwidth;
5633 int width;
5634 int n;
5635 int len;
5636 int fillchar;
5637 char_u buf[MAXPATHL];
5638 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005639 struct stl_hlrec hltab[STL_MAX_ITEM];
5640 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005641 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
5643 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005644 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005646 /* Use 'tabline'. Always at the first line of the screen. */
5647 p = p_tal;
5648 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005649 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005650 attr = hl_attr(HLF_TPF);
5651 maxwidth = Columns;
5652# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005653 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005656 else
5657 {
5658 row = W_WINROW(wp) + wp->w_height;
5659 fillchar = fillchar_status(&attr, wp == curwin);
5660 maxwidth = W_WIDTH(wp);
5661
5662 if (draw_ruler)
5663 {
5664 p = p_ruf;
5665 /* advance past any leading group spec - implicit in ru_col */
5666 if (*p == '%')
5667 {
5668 if (*++p == '-')
5669 p++;
5670 if (atoi((char *) p))
5671 while (VIM_ISDIGIT(*p))
5672 p++;
5673 if (*p++ != '(')
5674 p = p_ruf;
5675 }
5676#ifdef FEAT_VERTSPLIT
5677 col = ru_col - (Columns - W_WIDTH(wp));
5678 if (col < (W_WIDTH(wp) + 1) / 2)
5679 col = (W_WIDTH(wp) + 1) / 2;
5680#else
5681 col = ru_col;
5682 if (col > (Columns + 1) / 2)
5683 col = (Columns + 1) / 2;
5684#endif
5685 maxwidth = W_WIDTH(wp) - col;
5686#ifdef FEAT_WINDOWS
5687 if (!wp->w_status_height)
5688#endif
5689 {
5690 row = Rows - 1;
5691 --maxwidth; /* writing in last column may cause scrolling */
5692 fillchar = ' ';
5693 attr = 0;
5694 }
5695
5696# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005697 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005698# endif
5699 }
5700 else
5701 {
5702 if (*wp->w_p_stl != NUL)
5703 p = wp->w_p_stl;
5704 else
5705 p = p_stl;
5706# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005707 use_sandbox = was_set_insecurely((char_u *)"statusline",
5708 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005709# endif
5710 }
5711
5712#ifdef FEAT_VERTSPLIT
5713 col += W_WINCOL(wp);
5714#endif
5715 }
5716
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (maxwidth <= 0)
5718 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005720 width = build_stl_str_hl(wp == NULL ? curwin : wp,
5721 buf, sizeof(buf),
5722 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005723 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 len = STRLEN(buf);
5725
5726 while (width < maxwidth && len < sizeof(buf) - 1)
5727 {
5728#ifdef FEAT_MBYTE
5729 len += (*mb_char2bytes)(fillchar, buf + len);
5730#else
5731 buf[len++] = fillchar;
5732#endif
5733 ++width;
5734 }
5735 buf[len] = NUL;
5736
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005737 /*
5738 * Draw each snippet with the specified highlighting.
5739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 curattr = attr;
5741 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005742 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005744 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 screen_puts_len(p, len, row, col, curattr);
5746 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005747 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005749 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005751 else if (hltab[n].userhl < 0)
5752 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00005754 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005755 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756#endif
5757 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005758 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
5760 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005761
5762 if (wp == NULL)
5763 {
5764 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
5765 col = 0;
5766 len = 0;
5767 p = buf;
5768 fillchar = 0;
5769 for (n = 0; tabtab[n].start != NULL; n++)
5770 {
5771 len += vim_strnsize(p, (int)(tabtab[n].start - p));
5772 while (col < len)
5773 TabPageIdxs[col++] = fillchar;
5774 p = tabtab[n].start;
5775 fillchar = tabtab[n].userhl;
5776 }
5777 while (col < Columns)
5778 TabPageIdxs[col++] = fillchar;
5779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780}
5781
5782#endif /* FEAT_STL_OPT */
5783
5784/*
5785 * Output a single character directly to the screen and update ScreenLines.
5786 */
5787 void
5788screen_putchar(c, row, col, attr)
5789 int c;
5790 int row, col;
5791 int attr;
5792{
5793#ifdef FEAT_MBYTE
5794 char_u buf[MB_MAXBYTES + 1];
5795
5796 buf[(*mb_char2bytes)(c, buf)] = NUL;
5797#else
5798 char_u buf[2];
5799
5800 buf[0] = c;
5801 buf[1] = NUL;
5802#endif
5803 screen_puts(buf, row, col, attr);
5804}
5805
5806/*
5807 * Get a single character directly from ScreenLines into "bytes[]".
5808 * Also return its attribute in *attrp;
5809 */
5810 void
5811screen_getbytes(row, col, bytes, attrp)
5812 int row, col;
5813 char_u *bytes;
5814 int *attrp;
5815{
5816 unsigned off;
5817
5818 /* safety check */
5819 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
5820 {
5821 off = LineOffset[row] + col;
5822 *attrp = ScreenAttrs[off];
5823 bytes[0] = ScreenLines[off];
5824 bytes[1] = NUL;
5825
5826#ifdef FEAT_MBYTE
5827 if (enc_utf8 && ScreenLinesUC[off] != 0)
5828 bytes[utfc_char2bytes(off, bytes)] = NUL;
5829 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
5830 {
5831 bytes[0] = ScreenLines[off];
5832 bytes[1] = ScreenLines2[off];
5833 bytes[2] = NUL;
5834 }
5835 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
5836 {
5837 bytes[1] = ScreenLines[off + 1];
5838 bytes[2] = NUL;
5839 }
5840#endif
5841 }
5842}
5843
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005844#ifdef FEAT_MBYTE
5845static int screen_comp_differs __ARGS((int, int*));
5846
5847/*
5848 * Return TRUE if composing characters for screen posn "off" differs from
5849 * composing characters in "u8cc".
5850 */
5851 static int
5852screen_comp_differs(off, u8cc)
5853 int off;
5854 int *u8cc;
5855{
5856 int i;
5857
5858 for (i = 0; i < Screen_mco; ++i)
5859 {
5860 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
5861 return TRUE;
5862 if (u8cc[i] == 0)
5863 break;
5864 }
5865 return FALSE;
5866}
5867#endif
5868
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869/*
5870 * Put string '*text' on the screen at position 'row' and 'col', with
5871 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
5872 * Note: only outputs within one row, message is truncated at screen boundary!
5873 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
5874 */
5875 void
5876screen_puts(text, row, col, attr)
5877 char_u *text;
5878 int row;
5879 int col;
5880 int attr;
5881{
5882 screen_puts_len(text, -1, row, col, attr);
5883}
5884
5885/*
5886 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
5887 * a NUL.
5888 */
5889 void
5890screen_puts_len(text, len, row, col, attr)
5891 char_u *text;
5892 int len;
5893 int row;
5894 int col;
5895 int attr;
5896{
5897 unsigned off;
5898 char_u *ptr = text;
5899 int c;
5900#ifdef FEAT_MBYTE
5901 int mbyte_blen = 1;
5902 int mbyte_cells = 1;
5903 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005904 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 int clear_next_cell = FALSE;
5906# ifdef FEAT_ARABIC
5907 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005908 int pc, nc, nc1;
5909 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910# endif
5911#endif
5912
5913 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
5914 return;
5915
5916 off = LineOffset[row] + col;
5917 while (*ptr != NUL && col < screen_Columns
5918 && (len < 0 || (int)(ptr - text) < len))
5919 {
5920 c = *ptr;
5921#ifdef FEAT_MBYTE
5922 /* check if this is the first byte of a multibyte */
5923 if (has_mbyte)
5924 {
5925 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005926 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005928 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
5930 mbyte_cells = 1;
5931 else if (enc_dbcs != 0)
5932 mbyte_cells = mbyte_blen;
5933 else /* enc_utf8 */
5934 {
5935 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005936 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 (int)((text + len) - ptr));
5938 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005939 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 mbyte_cells = utf_char2cells(u8c);
5941 /* Non-BMP character: display as ? or fullwidth ?. */
5942 if (u8c >= 0x10000)
5943 {
5944 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
5945 if (attr == 0)
5946 attr = hl_attr(HLF_8);
5947 }
5948# ifdef FEAT_ARABIC
5949 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
5950 {
5951 /* Do Arabic shaping. */
5952 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
5953 {
5954 /* Past end of string to be displayed. */
5955 nc = NUL;
5956 nc1 = NUL;
5957 }
5958 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005959 {
5960 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
5961 nc1 = pcc[0];
5962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 pc = prev_c;
5964 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005965 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
5967 else
5968 prev_c = u8c;
5969# endif
5970 }
5971 }
5972#endif
5973
5974 if (ScreenLines[off] != c
5975#ifdef FEAT_MBYTE
5976 || (mbyte_cells == 2
5977 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
5978 || (enc_dbcs == DBCS_JPNU
5979 && c == 0x8e
5980 && ScreenLines2[off] != ptr[1])
5981 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005982 && (ScreenLinesUC[off] != (u8char_T)u8c
5983 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984#endif
5985 || ScreenAttrs[off] != attr
5986 || exmode_active
5987 )
5988 {
5989#if defined(FEAT_GUI) || defined(UNIX)
5990 /* The bold trick makes a single row of pixels appear in the next
5991 * character. When a bold character is removed, the next
5992 * character should be redrawn too. This happens for our own GUI
5993 * and for some xterms.
5994 * Force the redraw by setting the attribute to a different value
5995 * than "attr", the contents of ScreenLines[] may be needed by
5996 * mb_off2cells() further on.
5997 * Don't do this for the last drawn character, because the next
5998 * character may not be redrawn. */
5999 if (
6000# ifdef FEAT_GUI
6001 gui.in_use
6002# endif
6003# if defined(FEAT_GUI) && defined(UNIX)
6004 ||
6005# endif
6006# ifdef UNIX
6007 term_is_xterm
6008# endif
6009 )
6010 {
6011 int n;
6012
6013 n = ScreenAttrs[off];
6014# ifdef FEAT_MBYTE
6015 if (col + mbyte_cells < screen_Columns
6016 && (n > HL_ALL || (n & HL_BOLD))
6017 && (len < 0 ? ptr[mbyte_blen] != NUL
6018 : ptr + mbyte_blen < text + len))
6019 ScreenAttrs[off + mbyte_cells] = attr + 1;
6020# else
6021 if (col + 1 < screen_Columns
6022 && (n > HL_ALL || (n & HL_BOLD))
6023 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6024 ScreenLines[off + 1] = 0;
6025# endif
6026 }
6027#endif
6028#ifdef FEAT_MBYTE
6029 /* When at the end of the text and overwriting a two-cell
6030 * character with a one-cell character, need to clear the next
6031 * cell. Also when overwriting the left halve of a two-cell char
6032 * with the right halve of a two-cell char. Do this only once
6033 * (mb_off2cells() may return 2 on the right halve). */
6034 if (clear_next_cell)
6035 clear_next_cell = FALSE;
6036 else if (has_mbyte
6037 && (len < 0 ? ptr[mbyte_blen] == NUL
6038 : ptr + mbyte_blen >= text + len)
6039 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6040 || (mbyte_cells == 2
6041 && (*mb_off2cells)(off) == 1
6042 && (*mb_off2cells)(off + 1) > 1)))
6043 clear_next_cell = TRUE;
6044
6045 /* Make sure we never leave a second byte of a double-byte behind,
6046 * it confuses mb_off2cells(). */
6047 if (enc_dbcs
6048 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6049 || (mbyte_cells == 2
6050 && (*mb_off2cells)(off) == 1
6051 && (*mb_off2cells)(off + 1) > 1)))
6052 ScreenLines[off + mbyte_blen] = 0;
6053#endif
6054 ScreenLines[off] = c;
6055 ScreenAttrs[off] = attr;
6056#ifdef FEAT_MBYTE
6057 if (enc_utf8)
6058 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006059 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 ScreenLinesUC[off] = 0;
6061 else
6062 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006063 int i;
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006066 for (i = 0; i < Screen_mco; ++i)
6067 {
6068 ScreenLinesC[i][off] = u8cc[i];
6069 if (u8cc[i] == 0)
6070 break;
6071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 }
6073 if (mbyte_cells == 2)
6074 {
6075 ScreenLines[off + 1] = 0;
6076 ScreenAttrs[off + 1] = attr;
6077 }
6078 screen_char(off, row, col);
6079 }
6080 else if (mbyte_cells == 2)
6081 {
6082 ScreenLines[off + 1] = ptr[1];
6083 ScreenAttrs[off + 1] = attr;
6084 screen_char_2(off, row, col);
6085 }
6086 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6087 {
6088 ScreenLines2[off] = ptr[1];
6089 screen_char(off, row, col);
6090 }
6091 else
6092#endif
6093 screen_char(off, row, col);
6094 }
6095#ifdef FEAT_MBYTE
6096 if (has_mbyte)
6097 {
6098 off += mbyte_cells;
6099 col += mbyte_cells;
6100 ptr += mbyte_blen;
6101 if (clear_next_cell)
6102 ptr = (char_u *)" ";
6103 }
6104 else
6105#endif
6106 {
6107 ++off;
6108 ++col;
6109 ++ptr;
6110 }
6111 }
6112}
6113
6114#ifdef FEAT_SEARCH_EXTRA
6115/*
6116 * Prepare for 'searchhl' highlighting.
6117 */
6118 static void
6119start_search_hl()
6120{
6121 if (p_hls && !no_hlsearch)
6122 {
6123 last_pat_prog(&search_hl.rm);
6124 search_hl.attr = hl_attr(HLF_L);
6125 }
6126}
6127
6128/*
6129 * Clean up for 'searchhl' highlighting.
6130 */
6131 static void
6132end_search_hl()
6133{
6134 if (search_hl.rm.regprog != NULL)
6135 {
6136 vim_free(search_hl.rm.regprog);
6137 search_hl.rm.regprog = NULL;
6138 }
6139}
6140
6141/*
6142 * Advance to the match in window "wp" line "lnum" or past it.
6143 */
6144 static void
6145prepare_search_hl(wp, lnum)
6146 win_T *wp;
6147 linenr_T lnum;
6148{
6149 match_T *shl; /* points to search_hl or match_hl */
6150 int n;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006151 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
6153 /*
6154 * When using a multi-line pattern, start searching at the top
6155 * of the window or just after a closed fold.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006156 * Do this both for search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006158 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006160 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 if (shl->rm.regprog != NULL
6162 && shl->lnum == 0
6163 && re_multiline(shl->rm.regprog))
6164 {
6165 if (shl->first_lnum == 0)
6166 {
6167# ifdef FEAT_FOLDING
6168 for (shl->first_lnum = lnum;
6169 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6170 if (hasFoldingWin(wp, shl->first_lnum - 1,
6171 NULL, NULL, TRUE, NULL))
6172 break;
6173# else
6174 shl->first_lnum = wp->w_topline;
6175# endif
6176 }
6177 n = 0;
6178 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6179 {
6180 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6181 if (shl->lnum != 0)
6182 {
6183 shl->first_lnum = shl->lnum
6184 + shl->rm.endpos[0].lnum
6185 - shl->rm.startpos[0].lnum;
6186 n = shl->rm.endpos[0].col;
6187 }
6188 else
6189 {
6190 ++shl->first_lnum;
6191 n = 0;
6192 }
6193 }
6194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 }
6196}
6197
6198/*
6199 * Search for a next 'searchl' or ":match" match.
6200 * Uses shl->buf.
6201 * Sets shl->lnum and shl->rm contents.
6202 * Note: Assumes a previous match is always before "lnum", unless
6203 * shl->lnum is zero.
6204 * Careful: Any pointers for buffer lines will become invalid.
6205 */
6206 static void
6207next_search_hl(win, shl, lnum, mincol)
6208 win_T *win;
6209 match_T *shl; /* points to search_hl or match_hl */
6210 linenr_T lnum;
6211 colnr_T mincol; /* minimal column for a match */
6212{
6213 linenr_T l;
6214 colnr_T matchcol;
6215 long nmatched;
6216
6217 if (shl->lnum != 0)
6218 {
6219 /* Check for three situations:
6220 * 1. If the "lnum" is below a previous match, start a new search.
6221 * 2. If the previous match includes "mincol", use it.
6222 * 3. Continue after the previous match.
6223 */
6224 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6225 if (lnum > l)
6226 shl->lnum = 0;
6227 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6228 return;
6229 }
6230
6231 /*
6232 * Repeat searching for a match until one is found that includes "mincol"
6233 * or none is found in this line.
6234 */
6235 called_emsg = FALSE;
6236 for (;;)
6237 {
6238 /* Three situations:
6239 * 1. No useful previous match: search from start of line.
6240 * 2. Not Vi compatible or empty match: continue at next character.
6241 * Break the loop if this is beyond the end of the line.
6242 * 3. Vi compatible searching: continue at end of previous match.
6243 */
6244 if (shl->lnum == 0)
6245 matchcol = 0;
6246 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6247 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006248 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006250 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006251
6252 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006253 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006254 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006256 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 shl->lnum = 0;
6258 break;
6259 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006260#ifdef FEAT_MBYTE
6261 if (has_mbyte)
6262 matchcol += mb_ptr2len(ml);
6263 else
6264#endif
6265 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 }
6267 else
6268 matchcol = shl->rm.endpos[0].col;
6269
6270 shl->lnum = lnum;
6271 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6272 if (called_emsg)
6273 {
6274 /* Error while handling regexp: stop using this regexp. */
6275 vim_free(shl->rm.regprog);
6276 shl->rm.regprog = NULL;
6277 no_hlsearch = TRUE;
6278 break;
6279 }
6280 if (nmatched == 0)
6281 {
6282 shl->lnum = 0; /* no match found */
6283 break;
6284 }
6285 if (shl->rm.startpos[0].lnum > 0
6286 || shl->rm.startpos[0].col >= mincol
6287 || nmatched > 1
6288 || shl->rm.endpos[0].col > mincol)
6289 {
6290 shl->lnum += shl->rm.startpos[0].lnum;
6291 break; /* useful match found */
6292 }
6293 }
6294}
6295#endif
6296
6297 static void
6298screen_start_highlight(attr)
6299 int attr;
6300{
6301 attrentry_T *aep = NULL;
6302
6303 screen_attr = attr;
6304 if (full_screen
6305#ifdef WIN3264
6306 && termcap_active
6307#endif
6308 )
6309 {
6310#ifdef FEAT_GUI
6311 if (gui.in_use)
6312 {
6313 char buf[20];
6314
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006315 /* The GUI handles this internally. */
6316 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 OUT_STR(buf);
6318 }
6319 else
6320#endif
6321 {
6322 if (attr > HL_ALL) /* special HL attr. */
6323 {
6324 if (t_colors > 1)
6325 aep = syn_cterm_attr2entry(attr);
6326 else
6327 aep = syn_term_attr2entry(attr);
6328 if (aep == NULL) /* did ":syntax clear" */
6329 attr = 0;
6330 else
6331 attr = aep->ae_attr;
6332 }
6333 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6334 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006335 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6336 && cterm_normal_fg_bold)
6337 /* If the Normal FG color has BOLD attribute and the new HL
6338 * has a FG color defined, clear BOLD. */
6339 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6341 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006342 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6343 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 out_str(T_US);
6345 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6346 out_str(T_CZH);
6347 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6348 out_str(T_MR);
6349
6350 /*
6351 * Output the color or start string after bold etc., in case the
6352 * bold etc. override the color setting.
6353 */
6354 if (aep != NULL)
6355 {
6356 if (t_colors > 1)
6357 {
6358 if (aep->ae_u.cterm.fg_color)
6359 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6360 if (aep->ae_u.cterm.bg_color)
6361 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6362 }
6363 else
6364 {
6365 if (aep->ae_u.term.start != NULL)
6366 out_str(aep->ae_u.term.start);
6367 }
6368 }
6369 }
6370 }
6371}
6372
6373 void
6374screen_stop_highlight()
6375{
6376 int do_ME = FALSE; /* output T_ME code */
6377
6378 if (screen_attr != 0
6379#ifdef WIN3264
6380 && termcap_active
6381#endif
6382 )
6383 {
6384#ifdef FEAT_GUI
6385 if (gui.in_use)
6386 {
6387 char buf[20];
6388
6389 /* use internal GUI code */
6390 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6391 OUT_STR(buf);
6392 }
6393 else
6394#endif
6395 {
6396 if (screen_attr > HL_ALL) /* special HL attr. */
6397 {
6398 attrentry_T *aep;
6399
6400 if (t_colors > 1)
6401 {
6402 /*
6403 * Assume that t_me restores the original colors!
6404 */
6405 aep = syn_cterm_attr2entry(screen_attr);
6406 if (aep != NULL && (aep->ae_u.cterm.fg_color
6407 || aep->ae_u.cterm.bg_color))
6408 do_ME = TRUE;
6409 }
6410 else
6411 {
6412 aep = syn_term_attr2entry(screen_attr);
6413 if (aep != NULL && aep->ae_u.term.stop != NULL)
6414 {
6415 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6416 do_ME = TRUE;
6417 else
6418 out_str(aep->ae_u.term.stop);
6419 }
6420 }
6421 if (aep == NULL) /* did ":syntax clear" */
6422 screen_attr = 0;
6423 else
6424 screen_attr = aep->ae_attr;
6425 }
6426
6427 /*
6428 * Often all ending-codes are equal to T_ME. Avoid outputting the
6429 * same sequence several times.
6430 */
6431 if (screen_attr & HL_STANDOUT)
6432 {
6433 if (STRCMP(T_SE, T_ME) == 0)
6434 do_ME = TRUE;
6435 else
6436 out_str(T_SE);
6437 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006438 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 {
6440 if (STRCMP(T_UE, T_ME) == 0)
6441 do_ME = TRUE;
6442 else
6443 out_str(T_UE);
6444 }
6445 if (screen_attr & HL_ITALIC)
6446 {
6447 if (STRCMP(T_CZR, T_ME) == 0)
6448 do_ME = TRUE;
6449 else
6450 out_str(T_CZR);
6451 }
6452 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6453 out_str(T_ME);
6454
6455 if (t_colors > 1)
6456 {
6457 /* set Normal cterm colors */
6458 if (cterm_normal_fg_color != 0)
6459 term_fg_color(cterm_normal_fg_color - 1);
6460 if (cterm_normal_bg_color != 0)
6461 term_bg_color(cterm_normal_bg_color - 1);
6462 if (cterm_normal_fg_bold)
6463 out_str(T_MD);
6464 }
6465 }
6466 }
6467 screen_attr = 0;
6468}
6469
6470/*
6471 * Reset the colors for a cterm. Used when leaving Vim.
6472 * The machine specific code may override this again.
6473 */
6474 void
6475reset_cterm_colors()
6476{
6477 if (t_colors > 1)
6478 {
6479 /* set Normal cterm colors */
6480 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6481 {
6482 out_str(T_OP);
6483 screen_attr = -1;
6484 }
6485 if (cterm_normal_fg_bold)
6486 {
6487 out_str(T_ME);
6488 screen_attr = -1;
6489 }
6490 }
6491}
6492
6493/*
6494 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6495 * using the attributes from ScreenAttrs["off"].
6496 */
6497 static void
6498screen_char(off, row, col)
6499 unsigned off;
6500 int row;
6501 int col;
6502{
6503 int attr;
6504
6505 /* Check for illegal values, just in case (could happen just after
6506 * resizing). */
6507 if (row >= screen_Rows || col >= screen_Columns)
6508 return;
6509
6510 /* Outputting the last character on the screen may scrollup the screen.
6511 * Don't to it! Mark the character invalid (update it when scrolled up) */
6512 if (row == screen_Rows - 1 && col == screen_Columns - 1
6513#ifdef FEAT_RIGHTLEFT
6514 /* account for first command-line character in rightleft mode */
6515 && !cmdmsg_rl
6516#endif
6517 )
6518 {
6519 ScreenAttrs[off] = (sattr_T)-1;
6520 return;
6521 }
6522
6523 /*
6524 * Stop highlighting first, so it's easier to move the cursor.
6525 */
6526#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6527 if (screen_char_attr != 0)
6528 attr = screen_char_attr;
6529 else
6530#endif
6531 attr = ScreenAttrs[off];
6532 if (screen_attr != attr)
6533 screen_stop_highlight();
6534
6535 windgoto(row, col);
6536
6537 if (screen_attr != attr)
6538 screen_start_highlight(attr);
6539
6540#ifdef FEAT_MBYTE
6541 if (enc_utf8 && ScreenLinesUC[off] != 0)
6542 {
6543 char_u buf[MB_MAXBYTES + 1];
6544
6545 /* Convert UTF-8 character to bytes and write it. */
6546
6547 buf[utfc_char2bytes(off, buf)] = NUL;
6548
6549 out_str(buf);
6550 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6551 ++screen_cur_col;
6552 }
6553 else
6554#endif
6555 {
6556#ifdef FEAT_MBYTE
6557 out_flush_check();
6558#endif
6559 out_char(ScreenLines[off]);
6560#ifdef FEAT_MBYTE
6561 /* double-byte character in single-width cell */
6562 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6563 out_char(ScreenLines2[off]);
6564#endif
6565 }
6566
6567 screen_cur_col++;
6568}
6569
6570#ifdef FEAT_MBYTE
6571
6572/*
6573 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6574 * on the screen at position 'row' and 'col'.
6575 * The attributes of the first byte is used for all. This is required to
6576 * output the two bytes of a double-byte character with nothing in between.
6577 */
6578 static void
6579screen_char_2(off, row, col)
6580 unsigned off;
6581 int row;
6582 int col;
6583{
6584 /* Check for illegal values (could be wrong when screen was resized). */
6585 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6586 return;
6587
6588 /* Outputting the last character on the screen may scrollup the screen.
6589 * Don't to it! Mark the character invalid (update it when scrolled up) */
6590 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6591 {
6592 ScreenAttrs[off] = (sattr_T)-1;
6593 return;
6594 }
6595
6596 /* Output the first byte normally (positions the cursor), then write the
6597 * second byte directly. */
6598 screen_char(off, row, col);
6599 out_char(ScreenLines[off + 1]);
6600 ++screen_cur_col;
6601}
6602#endif
6603
6604#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6605/*
6606 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6607 * This uses the contents of ScreenLines[] and doesn't change it.
6608 */
6609 void
6610screen_draw_rectangle(row, col, height, width, invert)
6611 int row;
6612 int col;
6613 int height;
6614 int width;
6615 int invert;
6616{
6617 int r, c;
6618 int off;
6619
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006620 /* Can't use ScreenLines unless initialized */
6621 if (ScreenLines == NULL)
6622 return;
6623
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 if (invert)
6625 screen_char_attr = HL_INVERSE;
6626 for (r = row; r < row + height; ++r)
6627 {
6628 off = LineOffset[r];
6629 for (c = col; c < col + width; ++c)
6630 {
6631#ifdef FEAT_MBYTE
6632 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6633 {
6634 screen_char_2(off + c, r, c);
6635 ++c;
6636 }
6637 else
6638#endif
6639 {
6640 screen_char(off + c, r, c);
6641#ifdef FEAT_MBYTE
6642 if (utf_off2cells(off + c) > 1)
6643 ++c;
6644#endif
6645 }
6646 }
6647 }
6648 screen_char_attr = 0;
6649}
6650#endif
6651
6652#ifdef FEAT_VERTSPLIT
6653/*
6654 * Redraw the characters for a vertically split window.
6655 */
6656 static void
6657redraw_block(row, end, wp)
6658 int row;
6659 int end;
6660 win_T *wp;
6661{
6662 int col;
6663 int width;
6664
6665# ifdef FEAT_CLIPBOARD
6666 clip_may_clear_selection(row, end - 1);
6667# endif
6668
6669 if (wp == NULL)
6670 {
6671 col = 0;
6672 width = Columns;
6673 }
6674 else
6675 {
6676 col = wp->w_wincol;
6677 width = wp->w_width;
6678 }
6679 screen_draw_rectangle(row, col, end - row, width, FALSE);
6680}
6681#endif
6682
6683/*
6684 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6685 * with character 'c1' in first column followed by 'c2' in the other columns.
6686 * Use attributes 'attr'.
6687 */
6688 void
6689screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6690 int start_row, end_row;
6691 int start_col, end_col;
6692 int c1, c2;
6693 int attr;
6694{
6695 int row;
6696 int col;
6697 int off;
6698 int end_off;
6699 int did_delete;
6700 int c;
6701 int norm_term;
6702#if defined(FEAT_GUI) || defined(UNIX)
6703 int force_next = FALSE;
6704#endif
6705
6706 if (end_row > screen_Rows) /* safety check */
6707 end_row = screen_Rows;
6708 if (end_col > screen_Columns) /* safety check */
6709 end_col = screen_Columns;
6710 if (ScreenLines == NULL
6711 || start_row >= end_row
6712 || start_col >= end_col) /* nothing to do */
6713 return;
6714
6715 /* it's a "normal" terminal when not in a GUI or cterm */
6716 norm_term = (
6717#ifdef FEAT_GUI
6718 !gui.in_use &&
6719#endif
6720 t_colors <= 1);
6721 for (row = start_row; row < end_row; ++row)
6722 {
6723 /*
6724 * Try to use delete-line termcap code, when no attributes or in a
6725 * "normal" terminal, where a bold/italic space is just a
6726 * space.
6727 */
6728 did_delete = FALSE;
6729 if (c2 == ' '
6730 && end_col == Columns
6731 && can_clear(T_CE)
6732 && (attr == 0
6733 || (norm_term
6734 && attr <= HL_ALL
6735 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6736 {
6737 /*
6738 * check if we really need to clear something
6739 */
6740 col = start_col;
6741 if (c1 != ' ') /* don't clear first char */
6742 ++col;
6743
6744 off = LineOffset[row] + col;
6745 end_off = LineOffset[row] + end_col;
6746
6747 /* skip blanks (used often, keep it fast!) */
6748#ifdef FEAT_MBYTE
6749 if (enc_utf8)
6750 while (off < end_off && ScreenLines[off] == ' '
6751 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6752 ++off;
6753 else
6754#endif
6755 while (off < end_off && ScreenLines[off] == ' '
6756 && ScreenAttrs[off] == 0)
6757 ++off;
6758 if (off < end_off) /* something to be cleared */
6759 {
6760 col = off - LineOffset[row];
6761 screen_stop_highlight();
6762 term_windgoto(row, col);/* clear rest of this screen line */
6763 out_str(T_CE);
6764 screen_start(); /* don't know where cursor is now */
6765 col = end_col - col;
6766 while (col--) /* clear chars in ScreenLines */
6767 {
6768 ScreenLines[off] = ' ';
6769#ifdef FEAT_MBYTE
6770 if (enc_utf8)
6771 ScreenLinesUC[off] = 0;
6772#endif
6773 ScreenAttrs[off] = 0;
6774 ++off;
6775 }
6776 }
6777 did_delete = TRUE; /* the chars are cleared now */
6778 }
6779
6780 off = LineOffset[row] + start_col;
6781 c = c1;
6782 for (col = start_col; col < end_col; ++col)
6783 {
6784 if (ScreenLines[off] != c
6785#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006786 || (enc_utf8 && (int)ScreenLinesUC[off]
6787 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788#endif
6789 || ScreenAttrs[off] != attr
6790#if defined(FEAT_GUI) || defined(UNIX)
6791 || force_next
6792#endif
6793 )
6794 {
6795#if defined(FEAT_GUI) || defined(UNIX)
6796 /* The bold trick may make a single row of pixels appear in
6797 * the next character. When a bold character is removed, the
6798 * next character should be redrawn too. This happens for our
6799 * own GUI and for some xterms. */
6800 if (
6801# ifdef FEAT_GUI
6802 gui.in_use
6803# endif
6804# if defined(FEAT_GUI) && defined(UNIX)
6805 ||
6806# endif
6807# ifdef UNIX
6808 term_is_xterm
6809# endif
6810 )
6811 {
6812 if (ScreenLines[off] != ' '
6813 && (ScreenAttrs[off] > HL_ALL
6814 || ScreenAttrs[off] & HL_BOLD))
6815 force_next = TRUE;
6816 else
6817 force_next = FALSE;
6818 }
6819#endif
6820 ScreenLines[off] = c;
6821#ifdef FEAT_MBYTE
6822 if (enc_utf8)
6823 {
6824 if (c >= 0x80)
6825 {
6826 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006827 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829 else
6830 ScreenLinesUC[off] = 0;
6831 }
6832#endif
6833 ScreenAttrs[off] = attr;
6834 if (!did_delete || c != ' ')
6835 screen_char(off, row, col);
6836 }
6837 ++off;
6838 if (col == start_col)
6839 {
6840 if (did_delete)
6841 break;
6842 c = c2;
6843 }
6844 }
6845 if (end_col == Columns)
6846 LineWraps[row] = FALSE;
6847 if (row == Rows - 1) /* overwritten the command line */
6848 {
6849 redraw_cmdline = TRUE;
6850 if (c1 == ' ' && c2 == ' ')
6851 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006852 if (start_col == 0)
6853 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
6855 }
6856}
6857
6858/*
6859 * Check if there should be a delay. Used before clearing or redrawing the
6860 * screen or the command line.
6861 */
6862 void
6863check_for_delay(check_msg_scroll)
6864 int check_msg_scroll;
6865{
6866 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
6867 && !did_wait_return
6868 && emsg_silent == 0)
6869 {
6870 out_flush();
6871 ui_delay(1000L, TRUE);
6872 emsg_on_display = FALSE;
6873 if (check_msg_scroll)
6874 msg_scroll = FALSE;
6875 }
6876}
6877
6878/*
6879 * screen_valid - allocate screen buffers if size changed
6880 * If "clear" is TRUE: clear screen if it has been resized.
6881 * Returns TRUE if there is a valid screen to write to.
6882 * Returns FALSE when starting up and screen not initialized yet.
6883 */
6884 int
6885screen_valid(clear)
6886 int clear;
6887{
6888 screenalloc(clear); /* allocate screen buffers if size changed */
6889 return (ScreenLines != NULL);
6890}
6891
6892/*
6893 * Resize the shell to Rows and Columns.
6894 * Allocate ScreenLines[] and associated items.
6895 *
6896 * There may be some time between setting Rows and Columns and (re)allocating
6897 * ScreenLines[]. This happens when starting up and when (manually) changing
6898 * the shell size. Always use screen_Rows and screen_Columns to access items
6899 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
6900 * final size of the shell is needed.
6901 */
6902 void
6903screenalloc(clear)
6904 int clear;
6905{
6906 int new_row, old_row;
6907#ifdef FEAT_GUI
6908 int old_Rows;
6909#endif
6910 win_T *wp;
6911 int outofmem = FALSE;
6912 int len;
6913 schar_T *new_ScreenLines;
6914#ifdef FEAT_MBYTE
6915 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006916 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006918 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919#endif
6920 sattr_T *new_ScreenAttrs;
6921 unsigned *new_LineOffset;
6922 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006923#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006924 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006925 tabpage_T *tp;
6926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006928 static int did_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
6930 /*
6931 * Allocation of the screen buffers is done only when the size changes and
6932 * when Rows and Columns have been set and we have started doing full
6933 * screen stuff.
6934 */
6935 if ((ScreenLines != NULL
6936 && Rows == screen_Rows
6937 && Columns == screen_Columns
6938#ifdef FEAT_MBYTE
6939 && enc_utf8 == (ScreenLinesUC != NULL)
6940 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006941 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942#endif
6943 )
6944 || Rows == 0
6945 || Columns == 0
6946 || (!full_screen && ScreenLines == NULL))
6947 return;
6948
6949 /*
6950 * It's possible that we produce an out-of-memory message below, which
6951 * will cause this function to be called again. To break the loop, just
6952 * return here.
6953 */
6954 if (entered)
6955 return;
6956 entered = TRUE;
6957
6958 win_new_shellsize(); /* fit the windows in the new sized shell */
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 comp_col(); /* recompute columns for shown command and ruler */
6961
6962 /*
6963 * We're changing the size of the screen.
6964 * - Allocate new arrays for ScreenLines and ScreenAttrs.
6965 * - Move lines from the old arrays into the new arrays, clear extra
6966 * lines (unless the screen is going to be cleared).
6967 * - Free the old arrays.
6968 *
6969 * If anything fails, make ScreenLines NULL, so we don't do anything!
6970 * Continuing with the old ScreenLines may result in a crash, because the
6971 * size is wrong.
6972 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006973 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
6976 new_ScreenLines = (schar_T *)lalloc((long_u)(
6977 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
6978#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006979 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 if (enc_utf8)
6981 {
6982 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
6983 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006984 for (i = 0; i < p_mco; ++i)
6985 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
6987 }
6988 if (enc_dbcs == DBCS_JPNU)
6989 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
6990 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
6991#endif
6992 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
6993 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
6994 new_LineOffset = (unsigned *)lalloc((long_u)(
6995 Rows * sizeof(unsigned)), FALSE);
6996 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006997#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006998 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007001 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
7003 if (win_alloc_lines(wp) == FAIL)
7004 {
7005 outofmem = TRUE;
7006#ifdef FEAT_WINDOWS
7007 break;
7008#endif
7009 }
7010 }
7011
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007012#ifdef FEAT_MBYTE
7013 for (i = 0; i < p_mco; ++i)
7014 if (new_ScreenLinesC[i] == NULL)
7015 break;
7016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (new_ScreenLines == NULL
7018#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007019 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7021#endif
7022 || new_ScreenAttrs == NULL
7023 || new_LineOffset == NULL
7024 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007025#ifdef FEAT_WINDOWS
7026 || new_TabPageIdxs == NULL
7027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 || outofmem)
7029 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007030 if (ScreenLines != NULL || !did_outofmem_msg)
7031 {
7032 /* guess the size */
7033 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7034
7035 /* Remember we did this to avoid getting outofmem messages over
7036 * and over again. */
7037 did_outofmem_msg = TRUE;
7038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 vim_free(new_ScreenLines);
7040 new_ScreenLines = NULL;
7041#ifdef FEAT_MBYTE
7042 vim_free(new_ScreenLinesUC);
7043 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007044 for (i = 0; i < p_mco; ++i)
7045 {
7046 vim_free(new_ScreenLinesC[i]);
7047 new_ScreenLinesC[i] = NULL;
7048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 vim_free(new_ScreenLines2);
7050 new_ScreenLines2 = NULL;
7051#endif
7052 vim_free(new_ScreenAttrs);
7053 new_ScreenAttrs = NULL;
7054 vim_free(new_LineOffset);
7055 new_LineOffset = NULL;
7056 vim_free(new_LineWraps);
7057 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007058#ifdef FEAT_WINDOWS
7059 vim_free(new_TabPageIdxs);
7060 new_TabPageIdxs = NULL;
7061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 }
7063 else
7064 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007065 did_outofmem_msg = FALSE;
7066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 for (new_row = 0; new_row < Rows; ++new_row)
7068 {
7069 new_LineOffset[new_row] = new_row * Columns;
7070 new_LineWraps[new_row] = FALSE;
7071
7072 /*
7073 * If the screen is not going to be cleared, copy as much as
7074 * possible from the old screen to the new one and clear the rest
7075 * (used when resizing the window at the "--more--" prompt or when
7076 * executing an external command, for the GUI).
7077 */
7078 if (!clear)
7079 {
7080 (void)vim_memset(new_ScreenLines + new_row * Columns,
7081 ' ', (size_t)Columns * sizeof(schar_T));
7082#ifdef FEAT_MBYTE
7083 if (enc_utf8)
7084 {
7085 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7086 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007087 for (i = 0; i < p_mco; ++i)
7088 (void)vim_memset(new_ScreenLinesC[i]
7089 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 0, (size_t)Columns * sizeof(u8char_T));
7091 }
7092 if (enc_dbcs == DBCS_JPNU)
7093 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7094 0, (size_t)Columns * sizeof(schar_T));
7095#endif
7096 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7097 0, (size_t)Columns * sizeof(sattr_T));
7098 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007099 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
7101 if (screen_Columns < Columns)
7102 len = screen_Columns;
7103 else
7104 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007105#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007106 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007107 * may be invalid now. Also when p_mco changes. */
7108 if (!(enc_utf8 && ScreenLinesUC == NULL)
7109 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007110#endif
7111 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7112 ScreenLines + LineOffset[old_row],
7113 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007115 if (enc_utf8 && ScreenLinesUC != NULL
7116 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {
7118 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7119 ScreenLinesUC + LineOffset[old_row],
7120 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007121 for (i = 0; i < p_mco; ++i)
7122 mch_memmove(new_ScreenLinesC[i]
7123 + new_LineOffset[new_row],
7124 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 (size_t)len * sizeof(u8char_T));
7126 }
7127 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7128 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7129 ScreenLines2 + LineOffset[old_row],
7130 (size_t)len * sizeof(schar_T));
7131#endif
7132 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7133 ScreenAttrs + LineOffset[old_row],
7134 (size_t)len * sizeof(sattr_T));
7135 }
7136 }
7137 }
7138 /* Use the last line of the screen for the current line. */
7139 current_ScreenLine = new_ScreenLines + Rows * Columns;
7140 }
7141
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007142 free_screenlines();
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 ScreenLines = new_ScreenLines;
7145#ifdef FEAT_MBYTE
7146 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007147 for (i = 0; i < p_mco; ++i)
7148 ScreenLinesC[i] = new_ScreenLinesC[i];
7149 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 ScreenLines2 = new_ScreenLines2;
7151#endif
7152 ScreenAttrs = new_ScreenAttrs;
7153 LineOffset = new_LineOffset;
7154 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007155#ifdef FEAT_WINDOWS
7156 TabPageIdxs = new_TabPageIdxs;
7157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159 /* It's important that screen_Rows and screen_Columns reflect the actual
7160 * size of ScreenLines[]. Set them before calling anything. */
7161#ifdef FEAT_GUI
7162 old_Rows = screen_Rows;
7163#endif
7164 screen_Rows = Rows;
7165 screen_Columns = Columns;
7166
7167 must_redraw = CLEAR; /* need to clear the screen later */
7168 if (clear)
7169 screenclear2();
7170
7171#ifdef FEAT_GUI
7172 else if (gui.in_use
7173 && !gui.starting
7174 && ScreenLines != NULL
7175 && old_Rows != Rows)
7176 {
7177 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7178 /*
7179 * Adjust the position of the cursor, for when executing an external
7180 * command.
7181 */
7182 if (msg_row >= Rows) /* Rows got smaller */
7183 msg_row = Rows - 1; /* put cursor at last row */
7184 else if (Rows > old_Rows) /* Rows got bigger */
7185 msg_row += Rows - old_Rows; /* put cursor in same place */
7186 if (msg_col >= Columns) /* Columns got smaller */
7187 msg_col = Columns - 1; /* put cursor at last column */
7188 }
7189#endif
7190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 entered = FALSE;
7192}
7193
7194 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007195free_screenlines()
7196{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007197#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007198 int i;
7199
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007200 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007201 for (i = 0; i < Screen_mco; ++i)
7202 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007203 vim_free(ScreenLines2);
7204#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007205 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007206 vim_free(ScreenAttrs);
7207 vim_free(LineOffset);
7208 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007209#ifdef FEAT_WINDOWS
7210 vim_free(TabPageIdxs);
7211#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007212}
7213
7214 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215screenclear()
7216{
7217 check_for_delay(FALSE);
7218 screenalloc(FALSE); /* allocate screen buffers if size changed */
7219 screenclear2(); /* clear the screen */
7220}
7221
7222 static void
7223screenclear2()
7224{
7225 int i;
7226
7227 if (starting == NO_SCREEN || ScreenLines == NULL
7228#ifdef FEAT_GUI
7229 || (gui.in_use && gui.starting)
7230#endif
7231 )
7232 return;
7233
7234#ifdef FEAT_GUI
7235 if (!gui.in_use)
7236#endif
7237 screen_attr = -1; /* force setting the Normal colors */
7238 screen_stop_highlight(); /* don't want highlighting here */
7239
7240#ifdef FEAT_CLIPBOARD
7241 /* disable selection without redrawing it */
7242 clip_scroll_selection(9999);
7243#endif
7244
7245 /* blank out ScreenLines */
7246 for (i = 0; i < Rows; ++i)
7247 {
7248 lineclear(LineOffset[i], (int)Columns);
7249 LineWraps[i] = FALSE;
7250 }
7251
7252 if (can_clear(T_CL))
7253 {
7254 out_str(T_CL); /* clear the display */
7255 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007256 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
7258 else
7259 {
7260 /* can't clear the screen, mark all chars with invalid attributes */
7261 for (i = 0; i < Rows; ++i)
7262 lineinvalid(LineOffset[i], (int)Columns);
7263 clear_cmdline = TRUE;
7264 }
7265
7266 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7267
7268 win_rest_invalid(firstwin);
7269 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007270#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007271 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 if (must_redraw == CLEAR) /* no need to clear again */
7274 must_redraw = NOT_VALID;
7275 compute_cmdrow();
7276 msg_row = cmdline_row; /* put cursor on last line for messages */
7277 msg_col = 0;
7278 screen_start(); /* don't know where cursor is now */
7279 msg_scrolled = 0; /* can't scroll back */
7280 msg_didany = FALSE;
7281 msg_didout = FALSE;
7282}
7283
7284/*
7285 * Clear one line in ScreenLines.
7286 */
7287 static void
7288lineclear(off, width)
7289 unsigned off;
7290 int width;
7291{
7292 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7293#ifdef FEAT_MBYTE
7294 if (enc_utf8)
7295 (void)vim_memset(ScreenLinesUC + off, 0,
7296 (size_t)width * sizeof(u8char_T));
7297#endif
7298 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7299}
7300
7301/*
7302 * Mark one line in ScreenLines invalid by setting the attributes to an
7303 * invalid value.
7304 */
7305 static void
7306lineinvalid(off, width)
7307 unsigned off;
7308 int width;
7309{
7310 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7311}
7312
7313#ifdef FEAT_VERTSPLIT
7314/*
7315 * Copy part of a Screenline for vertically split window "wp".
7316 */
7317 static void
7318linecopy(to, from, wp)
7319 int to;
7320 int from;
7321 win_T *wp;
7322{
7323 unsigned off_to = LineOffset[to] + wp->w_wincol;
7324 unsigned off_from = LineOffset[from] + wp->w_wincol;
7325
7326 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7327 wp->w_width * sizeof(schar_T));
7328# ifdef FEAT_MBYTE
7329 if (enc_utf8)
7330 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007331 int i;
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7334 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007335 for (i = 0; i < p_mco; ++i)
7336 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7337 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 }
7339 if (enc_dbcs == DBCS_JPNU)
7340 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7341 wp->w_width * sizeof(schar_T));
7342# endif
7343 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7344 wp->w_width * sizeof(sattr_T));
7345}
7346#endif
7347
7348/*
7349 * Return TRUE if clearing with term string "p" would work.
7350 * It can't work when the string is empty or it won't set the right background.
7351 */
7352 int
7353can_clear(p)
7354 char_u *p;
7355{
7356 return (*p != NUL && (t_colors <= 1
7357#ifdef FEAT_GUI
7358 || gui.in_use
7359#endif
7360 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7361}
7362
7363/*
7364 * Reset cursor position. Use whenever cursor was moved because of outputting
7365 * something directly to the screen (shell commands) or a terminal control
7366 * code.
7367 */
7368 void
7369screen_start()
7370{
7371 screen_cur_row = screen_cur_col = 9999;
7372}
7373
7374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 * Move the cursor to position "row","col" in the screen.
7376 * This tries to find the most efficient way to move, minimizing the number of
7377 * characters sent to the terminal.
7378 */
7379 void
7380windgoto(row, col)
7381 int row;
7382 int col;
7383{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007384 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 int i;
7386 int plan;
7387 int cost;
7388 int wouldbe_col;
7389 int noinvcurs;
7390 char_u *bs;
7391 int goto_cost;
7392 int attr;
7393
7394#define GOTO_COST 7 /* asssume a term_windgoto() takes about 7 chars */
7395#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7396
7397#define PLAN_LE 1
7398#define PLAN_CR 2
7399#define PLAN_NL 3
7400#define PLAN_WRITE 4
7401 /* Can't use ScreenLines unless initialized */
7402 if (ScreenLines == NULL)
7403 return;
7404
7405 if (col != screen_cur_col || row != screen_cur_row)
7406 {
7407 /* Check for valid position. */
7408 if (row < 0) /* window without text lines? */
7409 row = 0;
7410 if (row >= screen_Rows)
7411 row = screen_Rows - 1;
7412 if (col >= screen_Columns)
7413 col = screen_Columns - 1;
7414
7415 /* check if no cursor movement is allowed in highlight mode */
7416 if (screen_attr && *T_MS == NUL)
7417 noinvcurs = HIGHL_COST;
7418 else
7419 noinvcurs = 0;
7420 goto_cost = GOTO_COST + noinvcurs;
7421
7422 /*
7423 * Plan how to do the positioning:
7424 * 1. Use CR to move it to column 0, same row.
7425 * 2. Use T_LE to move it a few columns to the left.
7426 * 3. Use NL to move a few lines down, column 0.
7427 * 4. Move a few columns to the right with T_ND or by writing chars.
7428 *
7429 * Don't do this if the cursor went beyond the last column, the cursor
7430 * position is unknown then (some terminals wrap, some don't )
7431 *
7432 * First check if the highlighting attibutes allow us to write
7433 * characters to move the cursor to the right.
7434 */
7435 if (row >= screen_cur_row && screen_cur_col < Columns)
7436 {
7437 /*
7438 * If the cursor is in the same row, bigger col, we can use CR
7439 * or T_LE.
7440 */
7441 bs = NULL; /* init for GCC */
7442 attr = screen_attr;
7443 if (row == screen_cur_row && col < screen_cur_col)
7444 {
7445 /* "le" is preferred over "bc", because "bc" is obsolete */
7446 if (*T_LE)
7447 bs = T_LE; /* "cursor left" */
7448 else
7449 bs = T_BC; /* "backspace character (old) */
7450 if (*bs)
7451 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7452 else
7453 cost = 999;
7454 if (col + 1 < cost) /* using CR is less characters */
7455 {
7456 plan = PLAN_CR;
7457 wouldbe_col = 0;
7458 cost = 1; /* CR is just one character */
7459 }
7460 else
7461 {
7462 plan = PLAN_LE;
7463 wouldbe_col = col;
7464 }
7465 if (noinvcurs) /* will stop highlighting */
7466 {
7467 cost += noinvcurs;
7468 attr = 0;
7469 }
7470 }
7471
7472 /*
7473 * If the cursor is above where we want to be, we can use CR LF.
7474 */
7475 else if (row > screen_cur_row)
7476 {
7477 plan = PLAN_NL;
7478 wouldbe_col = 0;
7479 cost = (row - screen_cur_row) * 2; /* CR LF */
7480 if (noinvcurs) /* will stop highlighting */
7481 {
7482 cost += noinvcurs;
7483 attr = 0;
7484 }
7485 }
7486
7487 /*
7488 * If the cursor is in the same row, smaller col, just use write.
7489 */
7490 else
7491 {
7492 plan = PLAN_WRITE;
7493 wouldbe_col = screen_cur_col;
7494 cost = 0;
7495 }
7496
7497 /*
7498 * Check if any characters that need to be written have the
7499 * correct attributes. Also avoid UTF-8 characters.
7500 */
7501 i = col - wouldbe_col;
7502 if (i > 0)
7503 cost += i;
7504 if (cost < goto_cost && i > 0)
7505 {
7506 /*
7507 * Check if the attributes are correct without additionally
7508 * stopping highlighting.
7509 */
7510 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7511 while (i && *p++ == attr)
7512 --i;
7513 if (i != 0)
7514 {
7515 /*
7516 * Try if it works when highlighting is stopped here.
7517 */
7518 if (*--p == 0)
7519 {
7520 cost += noinvcurs;
7521 while (i && *p++ == 0)
7522 --i;
7523 }
7524 if (i != 0)
7525 cost = 999; /* different attributes, don't do it */
7526 }
7527#ifdef FEAT_MBYTE
7528 if (enc_utf8)
7529 {
7530 /* Don't use an UTF-8 char for positioning, it's slow. */
7531 for (i = wouldbe_col; i < col; ++i)
7532 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7533 {
7534 cost = 999;
7535 break;
7536 }
7537 }
7538#endif
7539 }
7540
7541 /*
7542 * We can do it without term_windgoto()!
7543 */
7544 if (cost < goto_cost)
7545 {
7546 if (plan == PLAN_LE)
7547 {
7548 if (noinvcurs)
7549 screen_stop_highlight();
7550 while (screen_cur_col > col)
7551 {
7552 out_str(bs);
7553 --screen_cur_col;
7554 }
7555 }
7556 else if (plan == PLAN_CR)
7557 {
7558 if (noinvcurs)
7559 screen_stop_highlight();
7560 out_char('\r');
7561 screen_cur_col = 0;
7562 }
7563 else if (plan == PLAN_NL)
7564 {
7565 if (noinvcurs)
7566 screen_stop_highlight();
7567 while (screen_cur_row < row)
7568 {
7569 out_char('\n');
7570 ++screen_cur_row;
7571 }
7572 screen_cur_col = 0;
7573 }
7574
7575 i = col - screen_cur_col;
7576 if (i > 0)
7577 {
7578 /*
7579 * Use cursor-right if it's one character only. Avoids
7580 * removing a line of pixels from the last bold char, when
7581 * using the bold trick in the GUI.
7582 */
7583 if (T_ND[0] != NUL && T_ND[1] == NUL)
7584 {
7585 while (i-- > 0)
7586 out_char(*T_ND);
7587 }
7588 else
7589 {
7590 int off;
7591
7592 off = LineOffset[row] + screen_cur_col;
7593 while (i-- > 0)
7594 {
7595 if (ScreenAttrs[off] != screen_attr)
7596 screen_stop_highlight();
7597#ifdef FEAT_MBYTE
7598 out_flush_check();
7599#endif
7600 out_char(ScreenLines[off]);
7601#ifdef FEAT_MBYTE
7602 if (enc_dbcs == DBCS_JPNU
7603 && ScreenLines[off] == 0x8e)
7604 out_char(ScreenLines2[off]);
7605#endif
7606 ++off;
7607 }
7608 }
7609 }
7610 }
7611 }
7612 else
7613 cost = 999;
7614
7615 if (cost >= goto_cost)
7616 {
7617 if (noinvcurs)
7618 screen_stop_highlight();
7619 if (row == screen_cur_row && (col > screen_cur_col) &&
7620 *T_CRI != NUL)
7621 term_cursor_right(col - screen_cur_col);
7622 else
7623 term_windgoto(row, col);
7624 }
7625 screen_cur_row = row;
7626 screen_cur_col = col;
7627 }
7628}
7629
7630/*
7631 * Set cursor to its position in the current window.
7632 */
7633 void
7634setcursor()
7635{
7636 if (redrawing())
7637 {
7638 validate_cursor();
7639 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7640 W_WINCOL(curwin) + (
7641#ifdef FEAT_RIGHTLEFT
7642 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7643# ifdef FEAT_MBYTE
7644 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7645# endif
7646 1)) :
7647#endif
7648 curwin->w_wcol));
7649 }
7650}
7651
7652
7653/*
7654 * insert 'line_count' lines at 'row' in window 'wp'
7655 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7656 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7657 * scrolling.
7658 * Returns FAIL if the lines are not inserted, OK for success.
7659 */
7660 int
7661win_ins_lines(wp, row, line_count, invalid, mayclear)
7662 win_T *wp;
7663 int row;
7664 int line_count;
7665 int invalid;
7666 int mayclear;
7667{
7668 int did_delete;
7669 int nextrow;
7670 int lastrow;
7671 int retval;
7672
7673 if (invalid)
7674 wp->w_lines_valid = 0;
7675
7676 if (wp->w_height < 5)
7677 return FAIL;
7678
7679 if (line_count > wp->w_height - row)
7680 line_count = wp->w_height - row;
7681
7682 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7683 if (retval != MAYBE)
7684 return retval;
7685
7686 /*
7687 * If there is a next window or a status line, we first try to delete the
7688 * lines at the bottom to avoid messing what is after the window.
7689 * If this fails and there are following windows, don't do anything to avoid
7690 * messing up those windows, better just redraw.
7691 */
7692 did_delete = FALSE;
7693#ifdef FEAT_WINDOWS
7694 if (wp->w_next != NULL || wp->w_status_height)
7695 {
7696 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7697 line_count, (int)Rows, FALSE, NULL) == OK)
7698 did_delete = TRUE;
7699 else if (wp->w_next)
7700 return FAIL;
7701 }
7702#endif
7703 /*
7704 * if no lines deleted, blank the lines that will end up below the window
7705 */
7706 if (!did_delete)
7707 {
7708#ifdef FEAT_WINDOWS
7709 wp->w_redr_status = TRUE;
7710#endif
7711 redraw_cmdline = TRUE;
7712 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7713 lastrow = nextrow + line_count;
7714 if (lastrow > Rows)
7715 lastrow = Rows;
7716 screen_fill(nextrow - line_count, lastrow - line_count,
7717 W_WINCOL(wp), (int)W_ENDCOL(wp),
7718 ' ', ' ', 0);
7719 }
7720
7721 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7722 == FAIL)
7723 {
7724 /* deletion will have messed up other windows */
7725 if (did_delete)
7726 {
7727#ifdef FEAT_WINDOWS
7728 wp->w_redr_status = TRUE;
7729#endif
7730 win_rest_invalid(W_NEXT(wp));
7731 }
7732 return FAIL;
7733 }
7734
7735 return OK;
7736}
7737
7738/*
7739 * delete "line_count" window lines at "row" in window "wp"
7740 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7741 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7742 * scrolling
7743 * Return OK for success, FAIL if the lines are not deleted.
7744 */
7745 int
7746win_del_lines(wp, row, line_count, invalid, mayclear)
7747 win_T *wp;
7748 int row;
7749 int line_count;
7750 int invalid;
7751 int mayclear;
7752{
7753 int retval;
7754
7755 if (invalid)
7756 wp->w_lines_valid = 0;
7757
7758 if (line_count > wp->w_height - row)
7759 line_count = wp->w_height - row;
7760
7761 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7762 if (retval != MAYBE)
7763 return retval;
7764
7765 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7766 (int)Rows, FALSE, NULL) == FAIL)
7767 return FAIL;
7768
7769#ifdef FEAT_WINDOWS
7770 /*
7771 * If there are windows or status lines below, try to put them at the
7772 * correct place. If we can't do that, they have to be redrawn.
7773 */
7774 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
7775 {
7776 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7777 line_count, (int)Rows, NULL) == FAIL)
7778 {
7779 wp->w_redr_status = TRUE;
7780 win_rest_invalid(wp->w_next);
7781 }
7782 }
7783 /*
7784 * If this is the last window and there is no status line, redraw the
7785 * command line later.
7786 */
7787 else
7788#endif
7789 redraw_cmdline = TRUE;
7790 return OK;
7791}
7792
7793/*
7794 * Common code for win_ins_lines() and win_del_lines().
7795 * Returns OK or FAIL when the work has been done.
7796 * Returns MAYBE when not finished yet.
7797 */
7798 static int
7799win_do_lines(wp, row, line_count, mayclear, del)
7800 win_T *wp;
7801 int row;
7802 int line_count;
7803 int mayclear;
7804 int del;
7805{
7806 int retval;
7807
7808 if (!redrawing() || line_count <= 0)
7809 return FAIL;
7810
7811 /* only a few lines left: redraw is faster */
7812 if (mayclear && Rows - line_count < 5
7813#ifdef FEAT_VERTSPLIT
7814 && wp->w_width == Columns
7815#endif
7816 )
7817 {
7818 screenclear(); /* will set wp->w_lines_valid to 0 */
7819 return FAIL;
7820 }
7821
7822 /*
7823 * Delete all remaining lines
7824 */
7825 if (row + line_count >= wp->w_height)
7826 {
7827 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
7828 W_WINCOL(wp), (int)W_ENDCOL(wp),
7829 ' ', ' ', 0);
7830 return OK;
7831 }
7832
7833 /*
7834 * when scrolling, the message on the command line should be cleared,
7835 * otherwise it will stay there forever.
7836 */
7837 clear_cmdline = TRUE;
7838
7839 /*
7840 * If the terminal can set a scroll region, use that.
7841 * Always do this in a vertically split window. This will redraw from
7842 * ScreenLines[] when t_CV isn't defined. That's faster than using
7843 * win_line().
7844 * Don't use a scroll region when we are going to redraw the text, writing
7845 * a character in the lower right corner of the scroll region causes a
7846 * scroll-up in the DJGPP version.
7847 */
7848 if (scroll_region
7849#ifdef FEAT_VERTSPLIT
7850 || W_WIDTH(wp) != Columns
7851#endif
7852 )
7853 {
7854#ifdef FEAT_VERTSPLIT
7855 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
7856#endif
7857 scroll_region_set(wp, row);
7858 if (del)
7859 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
7860 wp->w_height - row, FALSE, wp);
7861 else
7862 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
7863 wp->w_height - row, wp);
7864#ifdef FEAT_VERTSPLIT
7865 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
7866#endif
7867 scroll_region_reset();
7868 return retval;
7869 }
7870
7871#ifdef FEAT_WINDOWS
7872 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
7873 return FAIL;
7874#endif
7875
7876 return MAYBE;
7877}
7878
7879/*
7880 * window 'wp' and everything after it is messed up, mark it for redraw
7881 */
7882 static void
7883win_rest_invalid(wp)
7884 win_T *wp;
7885{
7886#ifdef FEAT_WINDOWS
7887 while (wp != NULL)
7888#else
7889 if (wp != NULL)
7890#endif
7891 {
7892 redraw_win_later(wp, NOT_VALID);
7893#ifdef FEAT_WINDOWS
7894 wp->w_redr_status = TRUE;
7895 wp = wp->w_next;
7896#endif
7897 }
7898 redraw_cmdline = TRUE;
7899}
7900
7901/*
7902 * The rest of the routines in this file perform screen manipulations. The
7903 * given operation is performed physically on the screen. The corresponding
7904 * change is also made to the internal screen image. In this way, the editor
7905 * anticipates the effect of editing changes on the appearance of the screen.
7906 * That way, when we call screenupdate a complete redraw isn't usually
7907 * necessary. Another advantage is that we can keep adding code to anticipate
7908 * screen changes, and in the meantime, everything still works.
7909 */
7910
7911/*
7912 * types for inserting or deleting lines
7913 */
7914#define USE_T_CAL 1
7915#define USE_T_CDL 2
7916#define USE_T_AL 3
7917#define USE_T_CE 4
7918#define USE_T_DL 5
7919#define USE_T_SR 6
7920#define USE_NL 7
7921#define USE_T_CD 8
7922#define USE_REDRAW 9
7923
7924/*
7925 * insert lines on the screen and update ScreenLines[]
7926 * 'end' is the line after the scrolled part. Normally it is Rows.
7927 * When scrolling region used 'off' is the offset from the top for the region.
7928 * 'row' and 'end' are relative to the start of the region.
7929 *
7930 * return FAIL for failure, OK for success.
7931 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007932 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933screen_ins_lines(off, row, line_count, end, wp)
7934 int off;
7935 int row;
7936 int line_count;
7937 int end;
7938 win_T *wp; /* NULL or window to use width from */
7939{
7940 int i;
7941 int j;
7942 unsigned temp;
7943 int cursor_row;
7944 int type;
7945 int result_empty;
7946 int can_ce = can_clear(T_CE);
7947
7948 /*
7949 * FAIL if
7950 * - there is no valid screen
7951 * - the screen has to be redrawn completely
7952 * - the line count is less than one
7953 * - the line count is more than 'ttyscroll'
7954 */
7955 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
7956 return FAIL;
7957
7958 /*
7959 * There are seven ways to insert lines:
7960 * 0. When in a vertically split window and t_CV isn't set, redraw the
7961 * characters from ScreenLines[].
7962 * 1. Use T_CD (clear to end of display) if it exists and the result of
7963 * the insert is just empty lines
7964 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
7965 * present or line_count > 1. It looks better if we do all the inserts
7966 * at once.
7967 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
7968 * insert is just empty lines and T_CE is not present or line_count >
7969 * 1.
7970 * 4. Use T_AL (insert line) if it exists.
7971 * 5. Use T_CE (erase line) if it exists and the result of the insert is
7972 * just empty lines.
7973 * 6. Use T_DL (delete line) if it exists and the result of the insert is
7974 * just empty lines.
7975 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
7976 * the 'da' flag is not set or we have clear line capability.
7977 * 8. redraw the characters from ScreenLines[].
7978 *
7979 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
7980 * the scrollbar for the window. It does have insert line, use that if it
7981 * exists.
7982 */
7983 result_empty = (row + line_count >= end);
7984#ifdef FEAT_VERTSPLIT
7985 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
7986 type = USE_REDRAW;
7987 else
7988#endif
7989 if (can_clear(T_CD) && result_empty)
7990 type = USE_T_CD;
7991 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
7992 type = USE_T_CAL;
7993 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
7994 type = USE_T_CDL;
7995 else if (*T_AL != NUL)
7996 type = USE_T_AL;
7997 else if (can_ce && result_empty)
7998 type = USE_T_CE;
7999 else if (*T_DL != NUL && result_empty)
8000 type = USE_T_DL;
8001 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8002 type = USE_T_SR;
8003 else
8004 return FAIL;
8005
8006 /*
8007 * For clearing the lines screen_del_lines() is used. This will also take
8008 * care of t_db if necessary.
8009 */
8010 if (type == USE_T_CD || type == USE_T_CDL ||
8011 type == USE_T_CE || type == USE_T_DL)
8012 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8013
8014 /*
8015 * If text is retained below the screen, first clear or delete as many
8016 * lines at the bottom of the window as are about to be inserted so that
8017 * the deleted lines won't later surface during a screen_del_lines.
8018 */
8019 if (*T_DB)
8020 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8021
8022#ifdef FEAT_CLIPBOARD
8023 /* Remove a modeless selection when inserting lines halfway the screen
8024 * or not the full width of the screen. */
8025 if (off + row > 0
8026# ifdef FEAT_VERTSPLIT
8027 || (wp != NULL && wp->w_width != Columns)
8028# endif
8029 )
8030 clip_clear_selection();
8031 else
8032 clip_scroll_selection(-line_count);
8033#endif
8034
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035#ifdef FEAT_GUI
8036 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8037 * scrolling is actually carried out. */
8038 gui_dont_update_cursor();
8039#endif
8040
8041 if (*T_CCS != NUL) /* cursor relative to region */
8042 cursor_row = row;
8043 else
8044 cursor_row = row + off;
8045
8046 /*
8047 * Shift LineOffset[] line_count down to reflect the inserted lines.
8048 * Clear the inserted lines in ScreenLines[].
8049 */
8050 row += off;
8051 end += off;
8052 for (i = 0; i < line_count; ++i)
8053 {
8054#ifdef FEAT_VERTSPLIT
8055 if (wp != NULL && wp->w_width != Columns)
8056 {
8057 /* need to copy part of a line */
8058 j = end - 1 - i;
8059 while ((j -= line_count) >= row)
8060 linecopy(j + line_count, j, wp);
8061 j += line_count;
8062 if (can_clear((char_u *)" "))
8063 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8064 else
8065 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8066 LineWraps[j] = FALSE;
8067 }
8068 else
8069#endif
8070 {
8071 j = end - 1 - i;
8072 temp = LineOffset[j];
8073 while ((j -= line_count) >= row)
8074 {
8075 LineOffset[j + line_count] = LineOffset[j];
8076 LineWraps[j + line_count] = LineWraps[j];
8077 }
8078 LineOffset[j + line_count] = temp;
8079 LineWraps[j + line_count] = FALSE;
8080 if (can_clear((char_u *)" "))
8081 lineclear(temp, (int)Columns);
8082 else
8083 lineinvalid(temp, (int)Columns);
8084 }
8085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086
8087 screen_stop_highlight();
8088 windgoto(cursor_row, 0);
8089
8090#ifdef FEAT_VERTSPLIT
8091 /* redraw the characters */
8092 if (type == USE_REDRAW)
8093 redraw_block(row, end, wp);
8094 else
8095#endif
8096 if (type == USE_T_CAL)
8097 {
8098 term_append_lines(line_count);
8099 screen_start(); /* don't know where cursor is now */
8100 }
8101 else
8102 {
8103 for (i = 0; i < line_count; i++)
8104 {
8105 if (type == USE_T_AL)
8106 {
8107 if (i && cursor_row != 0)
8108 windgoto(cursor_row, 0);
8109 out_str(T_AL);
8110 }
8111 else /* type == USE_T_SR */
8112 out_str(T_SR);
8113 screen_start(); /* don't know where cursor is now */
8114 }
8115 }
8116
8117 /*
8118 * With scroll-reverse and 'da' flag set we need to clear the lines that
8119 * have been scrolled down into the region.
8120 */
8121 if (type == USE_T_SR && *T_DA)
8122 {
8123 for (i = 0; i < line_count; ++i)
8124 {
8125 windgoto(off + i, 0);
8126 out_str(T_CE);
8127 screen_start(); /* don't know where cursor is now */
8128 }
8129 }
8130
8131#ifdef FEAT_GUI
8132 gui_can_update_cursor();
8133 if (gui.in_use)
8134 out_flush(); /* always flush after a scroll */
8135#endif
8136 return OK;
8137}
8138
8139/*
8140 * delete lines on the screen and update ScreenLines[]
8141 * 'end' is the line after the scrolled part. Normally it is Rows.
8142 * When scrolling region used 'off' is the offset from the top for the region.
8143 * 'row' and 'end' are relative to the start of the region.
8144 *
8145 * Return OK for success, FAIL if the lines are not deleted.
8146 */
8147/*ARGSUSED*/
8148 int
8149screen_del_lines(off, row, line_count, end, force, wp)
8150 int off;
8151 int row;
8152 int line_count;
8153 int end;
8154 int force; /* even when line_count > p_ttyscroll */
8155 win_T *wp; /* NULL or window to use width from */
8156{
8157 int j;
8158 int i;
8159 unsigned temp;
8160 int cursor_row;
8161 int cursor_end;
8162 int result_empty; /* result is empty until end of region */
8163 int can_delete; /* deleting line codes can be used */
8164 int type;
8165
8166 /*
8167 * FAIL if
8168 * - there is no valid screen
8169 * - the screen has to be redrawn completely
8170 * - the line count is less than one
8171 * - the line count is more than 'ttyscroll'
8172 */
8173 if (!screen_valid(TRUE) || line_count <= 0 ||
8174 (!force && line_count > p_ttyscroll))
8175 return FAIL;
8176
8177 /*
8178 * Check if the rest of the current region will become empty.
8179 */
8180 result_empty = row + line_count >= end;
8181
8182 /*
8183 * We can delete lines only when 'db' flag not set or when 'ce' option
8184 * available.
8185 */
8186 can_delete = (*T_DB == NUL || can_clear(T_CE));
8187
8188 /*
8189 * There are six ways to delete lines:
8190 * 0. When in a vertically split window and t_CV isn't set, redraw the
8191 * characters from ScreenLines[].
8192 * 1. Use T_CD if it exists and the result is empty.
8193 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8194 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8195 * none of the other ways work.
8196 * 4. Use T_CE (erase line) if the result is empty.
8197 * 5. Use T_DL (delete line) if it exists.
8198 * 6. redraw the characters from ScreenLines[].
8199 */
8200#ifdef FEAT_VERTSPLIT
8201 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8202 type = USE_REDRAW;
8203 else
8204#endif
8205 if (can_clear(T_CD) && result_empty)
8206 type = USE_T_CD;
8207#if defined(__BEOS__) && defined(BEOS_DR8)
8208 /*
8209 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8210 * its internal termcap... this works okay for tests which test *T_DB !=
8211 * NUL. It has the disadvantage that the user cannot use any :set t_*
8212 * command to get T_DB (back) to empty_option, only :set term=... will do
8213 * the trick...
8214 * Anyway, this hack will hopefully go away with the next OS release.
8215 * (Olaf Seibert)
8216 */
8217 else if (row == 0 && T_DB == empty_option
8218 && (line_count == 1 || *T_CDL == NUL))
8219#else
8220 else if (row == 0 && (
8221#ifndef AMIGA
8222 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8223 * up, so use delete-line command */
8224 line_count == 1 ||
8225#endif
8226 *T_CDL == NUL))
8227#endif
8228 type = USE_NL;
8229 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8230 type = USE_T_CDL;
8231 else if (can_clear(T_CE) && result_empty
8232#ifdef FEAT_VERTSPLIT
8233 && (wp == NULL || wp->w_width == Columns)
8234#endif
8235 )
8236 type = USE_T_CE;
8237 else if (*T_DL != NUL && can_delete)
8238 type = USE_T_DL;
8239 else if (*T_CDL != NUL && can_delete)
8240 type = USE_T_CDL;
8241 else
8242 return FAIL;
8243
8244#ifdef FEAT_CLIPBOARD
8245 /* Remove a modeless selection when deleting lines halfway the screen or
8246 * not the full width of the screen. */
8247 if (off + row > 0
8248# ifdef FEAT_VERTSPLIT
8249 || (wp != NULL && wp->w_width != Columns)
8250# endif
8251 )
8252 clip_clear_selection();
8253 else
8254 clip_scroll_selection(line_count);
8255#endif
8256
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257#ifdef FEAT_GUI
8258 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8259 * scrolling is actually carried out. */
8260 gui_dont_update_cursor();
8261#endif
8262
8263 if (*T_CCS != NUL) /* cursor relative to region */
8264 {
8265 cursor_row = row;
8266 cursor_end = end;
8267 }
8268 else
8269 {
8270 cursor_row = row + off;
8271 cursor_end = end + off;
8272 }
8273
8274 /*
8275 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8276 * Clear the inserted lines in ScreenLines[].
8277 */
8278 row += off;
8279 end += off;
8280 for (i = 0; i < line_count; ++i)
8281 {
8282#ifdef FEAT_VERTSPLIT
8283 if (wp != NULL && wp->w_width != Columns)
8284 {
8285 /* need to copy part of a line */
8286 j = row + i;
8287 while ((j += line_count) <= end - 1)
8288 linecopy(j - line_count, j, wp);
8289 j -= line_count;
8290 if (can_clear((char_u *)" "))
8291 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8292 else
8293 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8294 LineWraps[j] = FALSE;
8295 }
8296 else
8297#endif
8298 {
8299 /* whole width, moving the line pointers is faster */
8300 j = row + i;
8301 temp = LineOffset[j];
8302 while ((j += line_count) <= end - 1)
8303 {
8304 LineOffset[j - line_count] = LineOffset[j];
8305 LineWraps[j - line_count] = LineWraps[j];
8306 }
8307 LineOffset[j - line_count] = temp;
8308 LineWraps[j - line_count] = FALSE;
8309 if (can_clear((char_u *)" "))
8310 lineclear(temp, (int)Columns);
8311 else
8312 lineinvalid(temp, (int)Columns);
8313 }
8314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315
8316 screen_stop_highlight();
8317
8318#ifdef FEAT_VERTSPLIT
8319 /* redraw the characters */
8320 if (type == USE_REDRAW)
8321 redraw_block(row, end, wp);
8322 else
8323#endif
8324 if (type == USE_T_CD) /* delete the lines */
8325 {
8326 windgoto(cursor_row, 0);
8327 out_str(T_CD);
8328 screen_start(); /* don't know where cursor is now */
8329 }
8330 else if (type == USE_T_CDL)
8331 {
8332 windgoto(cursor_row, 0);
8333 term_delete_lines(line_count);
8334 screen_start(); /* don't know where cursor is now */
8335 }
8336 /*
8337 * Deleting lines at top of the screen or scroll region: Just scroll
8338 * the whole screen (scroll region) up by outputting newlines on the
8339 * last line.
8340 */
8341 else if (type == USE_NL)
8342 {
8343 windgoto(cursor_end - 1, 0);
8344 for (i = line_count; --i >= 0; )
8345 out_char('\n'); /* cursor will remain on same line */
8346 }
8347 else
8348 {
8349 for (i = line_count; --i >= 0; )
8350 {
8351 if (type == USE_T_DL)
8352 {
8353 windgoto(cursor_row, 0);
8354 out_str(T_DL); /* delete a line */
8355 }
8356 else /* type == USE_T_CE */
8357 {
8358 windgoto(cursor_row + i, 0);
8359 out_str(T_CE); /* erase a line */
8360 }
8361 screen_start(); /* don't know where cursor is now */
8362 }
8363 }
8364
8365 /*
8366 * If the 'db' flag is set, we need to clear the lines that have been
8367 * scrolled up at the bottom of the region.
8368 */
8369 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8370 {
8371 for (i = line_count; i > 0; --i)
8372 {
8373 windgoto(cursor_end - i, 0);
8374 out_str(T_CE); /* erase a line */
8375 screen_start(); /* don't know where cursor is now */
8376 }
8377 }
8378
8379#ifdef FEAT_GUI
8380 gui_can_update_cursor();
8381 if (gui.in_use)
8382 out_flush(); /* always flush after a scroll */
8383#endif
8384
8385 return OK;
8386}
8387
8388/*
8389 * show the current mode and ruler
8390 *
8391 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8392 * If clear_cmdline is FALSE there may be a message there that needs to be
8393 * cleared only if a mode is shown.
8394 * Return the length of the message (0 if no message).
8395 */
8396 int
8397showmode()
8398{
8399 int need_clear;
8400 int length = 0;
8401 int do_mode;
8402 int attr;
8403 int nwr_save;
8404#ifdef FEAT_INS_EXPAND
8405 int sub_attr;
8406#endif
8407
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008408 do_mode = ((p_smd && msg_silent == 0)
8409 && ((State & INSERT)
8410 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411#ifdef FEAT_VISUAL
8412 || VIsual_active
8413#endif
8414 ));
8415 if (do_mode || Recording)
8416 {
8417 /*
8418 * Don't show mode right now, when not redrawing or inside a mapping.
8419 * Call char_avail() only when we are going to show something, because
8420 * it takes a bit of time.
8421 */
8422 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8423 {
8424 redraw_cmdline = TRUE; /* show mode later */
8425 return 0;
8426 }
8427
8428 nwr_save = need_wait_return;
8429
8430 /* wait a bit before overwriting an important message */
8431 check_for_delay(FALSE);
8432
8433 /* if the cmdline is more than one line high, erase top lines */
8434 need_clear = clear_cmdline;
8435 if (clear_cmdline && cmdline_row < Rows - 1)
8436 msg_clr_cmdline(); /* will reset clear_cmdline */
8437
8438 /* Position on the last line in the window, column 0 */
8439 msg_pos_mode();
8440 cursor_off();
8441 attr = hl_attr(HLF_CM); /* Highlight mode */
8442 if (do_mode)
8443 {
8444 MSG_PUTS_ATTR("--", attr);
8445#if defined(FEAT_XIM)
8446 if (xic != NULL && im_get_status() && !p_imdisable
8447 && curbuf->b_p_iminsert == B_IMODE_IM)
8448# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8449 MSG_PUTS_ATTR(" IM", attr);
8450# else
8451 MSG_PUTS_ATTR(" XIM", attr);
8452# endif
8453#endif
8454#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8455 if (gui.in_use)
8456 {
8457 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008458 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 }
8460#endif
8461#ifdef FEAT_INS_EXPAND
8462 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8463 {
8464 /* These messages can get long, avoid a wrap in a narrow
8465 * window. Prefer showing edit_submode_extra. */
8466 length = (Rows - msg_row) * Columns - 3;
8467 if (edit_submode_extra != NULL)
8468 length -= vim_strsize(edit_submode_extra);
8469 if (length > 0)
8470 {
8471 if (edit_submode_pre != NULL)
8472 length -= vim_strsize(edit_submode_pre);
8473 if (length - vim_strsize(edit_submode) > 0)
8474 {
8475 if (edit_submode_pre != NULL)
8476 msg_puts_attr(edit_submode_pre, attr);
8477 msg_puts_attr(edit_submode, attr);
8478 }
8479 if (edit_submode_extra != NULL)
8480 {
8481 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8482 if ((int)edit_submode_highl < (int)HLF_COUNT)
8483 sub_attr = hl_attr(edit_submode_highl);
8484 else
8485 sub_attr = attr;
8486 msg_puts_attr(edit_submode_extra, sub_attr);
8487 }
8488 }
8489 length = 0;
8490 }
8491 else
8492#endif
8493 {
8494#ifdef FEAT_VREPLACE
8495 if (State & VREPLACE_FLAG)
8496 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8497 else
8498#endif
8499 if (State & REPLACE_FLAG)
8500 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8501 else if (State & INSERT)
8502 {
8503#ifdef FEAT_RIGHTLEFT
8504 if (p_ri)
8505 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8506#endif
8507 MSG_PUTS_ATTR(_(" INSERT"), attr);
8508 }
8509 else if (restart_edit == 'I')
8510 MSG_PUTS_ATTR(_(" (insert)"), attr);
8511 else if (restart_edit == 'R')
8512 MSG_PUTS_ATTR(_(" (replace)"), attr);
8513 else if (restart_edit == 'V')
8514 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8515#ifdef FEAT_RIGHTLEFT
8516 if (p_hkmap)
8517 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8518# ifdef FEAT_FKMAP
8519 if (p_fkmap)
8520 MSG_PUTS_ATTR(farsi_text_5, attr);
8521# endif
8522#endif
8523#ifdef FEAT_KEYMAP
8524 if (State & LANGMAP)
8525 {
8526# ifdef FEAT_ARABIC
8527 if (curwin->w_p_arab)
8528 MSG_PUTS_ATTR(_(" Arabic"), attr);
8529 else
8530# endif
8531 MSG_PUTS_ATTR(_(" (lang)"), attr);
8532 }
8533#endif
8534 if ((State & INSERT) && p_paste)
8535 MSG_PUTS_ATTR(_(" (paste)"), attr);
8536
8537#ifdef FEAT_VISUAL
8538 if (VIsual_active)
8539 {
8540 char *p;
8541
8542 /* Don't concatenate separate words to avoid translation
8543 * problems. */
8544 switch ((VIsual_select ? 4 : 0)
8545 + (VIsual_mode == Ctrl_V) * 2
8546 + (VIsual_mode == 'V'))
8547 {
8548 case 0: p = N_(" VISUAL"); break;
8549 case 1: p = N_(" VISUAL LINE"); break;
8550 case 2: p = N_(" VISUAL BLOCK"); break;
8551 case 4: p = N_(" SELECT"); break;
8552 case 5: p = N_(" SELECT LINE"); break;
8553 default: p = N_(" SELECT BLOCK"); break;
8554 }
8555 MSG_PUTS_ATTR(_(p), attr);
8556 }
8557#endif
8558 MSG_PUTS_ATTR(" --", attr);
8559 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008560
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 need_clear = TRUE;
8562 }
8563 if (Recording
8564#ifdef FEAT_INS_EXPAND
8565 && edit_submode == NULL /* otherwise it gets too long */
8566#endif
8567 )
8568 {
8569 MSG_PUTS_ATTR(_("recording"), attr);
8570 need_clear = TRUE;
8571 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008572
8573 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (need_clear || clear_cmdline)
8575 msg_clr_eos();
8576 msg_didout = FALSE; /* overwrite this message */
8577 length = msg_col;
8578 msg_col = 0;
8579 need_wait_return = nwr_save; /* never ask for hit-return for this */
8580 }
8581 else if (clear_cmdline && msg_silent == 0)
8582 /* Clear the whole command line. Will reset "clear_cmdline". */
8583 msg_clr_cmdline();
8584
8585#ifdef FEAT_CMDL_INFO
8586# ifdef FEAT_VISUAL
8587 /* In Visual mode the size of the selected area must be redrawn. */
8588 if (VIsual_active)
8589 clear_showcmd();
8590# endif
8591
8592 /* If the last window has no status line, the ruler is after the mode
8593 * message and must be redrawn */
8594 if (redrawing()
8595# ifdef FEAT_WINDOWS
8596 && lastwin->w_status_height == 0
8597# endif
8598 )
8599 win_redr_ruler(lastwin, TRUE);
8600#endif
8601 redraw_cmdline = FALSE;
8602 clear_cmdline = FALSE;
8603
8604 return length;
8605}
8606
8607/*
8608 * Position for a mode message.
8609 */
8610 static void
8611msg_pos_mode()
8612{
8613 msg_col = 0;
8614 msg_row = Rows - 1;
8615}
8616
8617/*
8618 * Delete mode message. Used when ESC is typed which is expected to end
8619 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008620 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 */
8622 void
8623unshowmode(force)
8624 int force;
8625{
8626 /*
8627 * Don't delete it right now, when not redrawing or insided a mapping.
8628 */
8629 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8630 redraw_cmdline = TRUE; /* delete mode later */
8631 else
8632 {
8633 msg_pos_mode();
8634 if (Recording)
8635 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8636 msg_clr_eos();
8637 }
8638}
8639
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008640#if defined(FEAT_WINDOWS)
8641/*
8642 * Draw the tab pages line at the top of the Vim window.
8643 */
8644 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008645draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008646{
8647 int tabcount = 0;
8648 tabpage_T *tp;
8649 int tabwidth;
8650 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008651 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008652 int attr;
8653 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008654 win_T *cwp;
8655 int wincount;
8656 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008657 int c;
8658 int len;
8659 int attr_sel = hl_attr(HLF_TPS);
8660 int attr_nosel = hl_attr(HLF_TP);
8661 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008662 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008663 int room;
8664 int use_sep_chars = (t_colors < 8
8665#ifdef FEAT_GUI
8666 && !gui.in_use
8667#endif
8668 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008669
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008670 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008671
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008672#ifdef FEAT_GUI_TABLINE
8673 /* When the GUI has the tabline then this always returns zero. */
8674 if (gui_use_tabline())
8675 {
8676 gui_update_tabline();
8677 return;
8678 }
8679#endif
8680
8681 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008682 return;
8683
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008684#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008685
8686 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
8687 for (scol = 0; scol < Columns; ++scol)
8688 TabPageIdxs[scol] = 0;
8689
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008690 /* Use the 'tabline' option if it's set. */
8691 if (*p_tal != NUL)
8692 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008693 int save_called_emsg = called_emsg;
8694
8695 /* Check for an error. If there is one we would loop in redrawing the
8696 * screen. Avoid that by making 'tabline' empty. */
8697 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008698 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008699 if (called_emsg)
8700 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008701 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008702 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008703 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00008704 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008705#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008706 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008707 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
8708 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008709
Bram Moolenaar238a5642006-02-21 22:12:05 +00008710 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
8711 if (tabwidth < 6)
8712 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008713
Bram Moolenaar238a5642006-02-21 22:12:05 +00008714 attr = attr_nosel;
8715 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008716 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008717 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
8718 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008719 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008720 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008721
Bram Moolenaar238a5642006-02-21 22:12:05 +00008722 if (tp->tp_topframe == topframe)
8723 attr = attr_sel;
8724 if (use_sep_chars && col > 0)
8725 screen_putchar('|', 0, col++, attr);
8726
8727 if (tp->tp_topframe != topframe)
8728 attr = attr_nosel;
8729
8730 screen_putchar(' ', 0, col++, attr);
8731
8732 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008733 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008734 cwp = curwin;
8735 wp = firstwin;
8736 }
8737 else
8738 {
8739 cwp = tp->tp_curwin;
8740 wp = tp->tp_firstwin;
8741 }
8742
8743 modified = FALSE;
8744 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
8745 if (bufIsChanged(wp->w_buffer))
8746 modified = TRUE;
8747 if (modified || wincount > 1)
8748 {
8749 if (wincount > 1)
8750 {
8751 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
8752 len = STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008753 if (col + len >= Columns - 3)
8754 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008755 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008756#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00008757 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008758#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00008759 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008760#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00008761 );
8762 col += len;
8763 }
8764 if (modified)
8765 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
8766 screen_putchar(' ', 0, col++, attr);
8767 }
8768
8769 room = scol - col + tabwidth - 1;
8770 if (room > 0)
8771 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008772 /* Get buffer name in NameBuff[] */
8773 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008774 len = vim_strsize(NameBuff);
8775 p = NameBuff;
8776#ifdef FEAT_MBYTE
8777 if (has_mbyte)
8778 while (len > room)
8779 {
8780 len -= ptr2cells(p);
8781 mb_ptr_adv(p);
8782 }
8783 else
8784#endif
8785 if (len > room)
8786 {
8787 p += len - room;
8788 len = room;
8789 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008790 if (len > Columns - col - 1)
8791 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008792
8793 screen_puts_len(p, STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008794 col += len;
8795 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00008796 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008797
8798 /* Store the tab page number in TabPageIdxs[], so that
8799 * jump_to_mouse() knows where each one is. */
8800 ++tabcount;
8801 while (scol < col)
8802 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008803 }
8804
Bram Moolenaar238a5642006-02-21 22:12:05 +00008805 if (use_sep_chars)
8806 c = '_';
8807 else
8808 c = ' ';
8809 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008810
8811 /* Put an "X" for closing the current tab if there are several. */
8812 if (first_tabpage->tp_next != NULL)
8813 {
8814 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
8815 TabPageIdxs[Columns - 1] = -999;
8816 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008817 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008818}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008819
8820/*
8821 * Get buffer name for "buf" into NameBuff[].
8822 * Takes care of special buffer names and translates special characters.
8823 */
8824 void
8825get_trans_bufname(buf)
8826 buf_T *buf;
8827{
8828 if (buf_spname(buf) != NULL)
8829 STRCPY(NameBuff, buf_spname(buf));
8830 else
8831 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
8832 trans_characters(NameBuff, MAXPATHL);
8833}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008834#endif
8835
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
8837/*
8838 * Get the character to use in a status line. Get its attributes in "*attr".
8839 */
8840 static int
8841fillchar_status(attr, is_curwin)
8842 int *attr;
8843 int is_curwin;
8844{
8845 int fill;
8846 if (is_curwin)
8847 {
8848 *attr = hl_attr(HLF_S);
8849 fill = fill_stl;
8850 }
8851 else
8852 {
8853 *attr = hl_attr(HLF_SNC);
8854 fill = fill_stlnc;
8855 }
8856 /* Use fill when there is highlighting, and highlighting of current
8857 * window differs, or the fillchars differ, or this is not the
8858 * current window */
8859 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
8860 || !is_curwin || firstwin == lastwin)
8861 || (fill_stl != fill_stlnc)))
8862 return fill;
8863 if (is_curwin)
8864 return '^';
8865 return '=';
8866}
8867#endif
8868
8869#ifdef FEAT_VERTSPLIT
8870/*
8871 * Get the character to use in a separator between vertically split windows.
8872 * Get its attributes in "*attr".
8873 */
8874 static int
8875fillchar_vsep(attr)
8876 int *attr;
8877{
8878 *attr = hl_attr(HLF_C);
8879 if (*attr == 0 && fill_vert == ' ')
8880 return '|';
8881 else
8882 return fill_vert;
8883}
8884#endif
8885
8886/*
8887 * Return TRUE if redrawing should currently be done.
8888 */
8889 int
8890redrawing()
8891{
8892 return (!RedrawingDisabled
8893 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
8894}
8895
8896/*
8897 * Return TRUE if printing messages should currently be done.
8898 */
8899 int
8900messaging()
8901{
8902 return (!(p_lz && char_avail() && !KeyTyped));
8903}
8904
8905/*
8906 * Show current status info in ruler and various other places
8907 * If always is FALSE, only show ruler if position has changed.
8908 */
8909 void
8910showruler(always)
8911 int always;
8912{
8913 if (!always && !redrawing())
8914 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00008915#ifdef FEAT_INS_EXPAND
8916 if (pum_visible())
8917 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00008918# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00008919 /* Don't redraw right now, do it later. */
8920 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00008921# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00008922 return;
8923 }
8924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008926 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00008927 {
8928 redraw_custum_statusline(curwin);
8929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 else
8931#endif
8932#ifdef FEAT_CMDL_INFO
8933 win_redr_ruler(curwin, always);
8934#endif
8935
8936#ifdef FEAT_TITLE
8937 if (need_maketitle
8938# ifdef FEAT_STL_OPT
8939 || (p_icon && (stl_syntax & STL_IN_ICON))
8940 || (p_title && (stl_syntax & STL_IN_TITLE))
8941# endif
8942 )
8943 maketitle();
8944#endif
8945}
8946
8947#ifdef FEAT_CMDL_INFO
8948 static void
8949win_redr_ruler(wp, always)
8950 win_T *wp;
8951 int always;
8952{
8953 char_u buffer[70];
8954 int row;
8955 int fillchar;
8956 int attr;
8957 int empty_line = FALSE;
8958 colnr_T virtcol;
8959 int i;
8960 int o;
8961#ifdef FEAT_VERTSPLIT
8962 int this_ru_col;
8963 int off = 0;
8964 int width = Columns;
8965# define WITH_OFF(x) x
8966# define WITH_WIDTH(x) x
8967#else
8968# define WITH_OFF(x) 0
8969# define WITH_WIDTH(x) Columns
8970# define this_ru_col ru_col
8971#endif
8972
8973 /* If 'ruler' off or redrawing disabled, don't do anything */
8974 if (!p_ru)
8975 return;
8976
8977 /*
8978 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
8979 * after deleting lines, before cursor.lnum is corrected.
8980 */
8981 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
8982 return;
8983
8984#ifdef FEAT_INS_EXPAND
8985 /* Don't draw the ruler while doing insert-completion, it might overwrite
8986 * the (long) mode message. */
8987# ifdef FEAT_WINDOWS
8988 if (wp == lastwin && lastwin->w_status_height == 0)
8989# endif
8990 if (edit_submode != NULL)
8991 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00008992 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
8993 if (pum_visible())
8994 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995#endif
8996
8997#ifdef FEAT_STL_OPT
8998 if (*p_ruf)
8999 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009000 int save_called_emsg = called_emsg;
9001
9002 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009004 if (called_emsg)
9005 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009006 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009007 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 return;
9009 }
9010#endif
9011
9012 /*
9013 * Check if not in Insert mode and the line is empty (will show "0-1").
9014 */
9015 if (!(State & INSERT)
9016 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9017 empty_line = TRUE;
9018
9019 /*
9020 * Only draw the ruler when something changed.
9021 */
9022 validate_virtcol_win(wp);
9023 if ( redraw_cmdline
9024 || always
9025 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9026 || wp->w_cursor.col != wp->w_ru_cursor.col
9027 || wp->w_virtcol != wp->w_ru_virtcol
9028#ifdef FEAT_VIRTUALEDIT
9029 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9030#endif
9031 || wp->w_topline != wp->w_ru_topline
9032 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9033#ifdef FEAT_DIFF
9034 || wp->w_topfill != wp->w_ru_topfill
9035#endif
9036 || empty_line != wp->w_ru_empty)
9037 {
9038 cursor_off();
9039#ifdef FEAT_WINDOWS
9040 if (wp->w_status_height)
9041 {
9042 row = W_WINROW(wp) + wp->w_height;
9043 fillchar = fillchar_status(&attr, wp == curwin);
9044# ifdef FEAT_VERTSPLIT
9045 off = W_WINCOL(wp);
9046 width = W_WIDTH(wp);
9047# endif
9048 }
9049 else
9050#endif
9051 {
9052 row = Rows - 1;
9053 fillchar = ' ';
9054 attr = 0;
9055#ifdef FEAT_VERTSPLIT
9056 width = Columns;
9057 off = 0;
9058#endif
9059 }
9060
9061 /* In list mode virtcol needs to be recomputed */
9062 virtcol = wp->w_virtcol;
9063 if (wp->w_p_list && lcs_tab1 == NUL)
9064 {
9065 wp->w_p_list = FALSE;
9066 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9067 wp->w_p_list = TRUE;
9068 }
9069
9070 /*
9071 * Some sprintfs return the length, some return a pointer.
9072 * To avoid portability problems we use strlen() here.
9073 */
9074 sprintf((char *)buffer, "%ld,",
9075 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9076 ? 0L
9077 : (long)(wp->w_cursor.lnum));
9078 col_print(buffer + STRLEN(buffer),
9079 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9080 (int)virtcol + 1);
9081
9082 /*
9083 * Add a "50%" if there is room for it.
9084 * On the last line, don't print in the last column (scrolls the
9085 * screen up on some terminals).
9086 */
9087 i = (int)STRLEN(buffer);
9088 get_rel_pos(wp, buffer + i + 1);
9089 o = i + vim_strsize(buffer + i + 1);
9090#ifdef FEAT_WINDOWS
9091 if (wp->w_status_height == 0) /* can't use last char of screen */
9092#endif
9093 ++o;
9094#ifdef FEAT_VERTSPLIT
9095 this_ru_col = ru_col - (Columns - width);
9096 if (this_ru_col < 0)
9097 this_ru_col = 0;
9098#endif
9099 /* Never use more than half the window/screen width, leave the other
9100 * half for the filename. */
9101 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9102 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9103 if (this_ru_col + o < WITH_WIDTH(width))
9104 {
9105 while (this_ru_col + o < WITH_WIDTH(width))
9106 {
9107#ifdef FEAT_MBYTE
9108 if (has_mbyte)
9109 i += (*mb_char2bytes)(fillchar, buffer + i);
9110 else
9111#endif
9112 buffer[i++] = fillchar;
9113 ++o;
9114 }
9115 get_rel_pos(wp, buffer + i);
9116 }
9117 /* Truncate at window boundary. */
9118#ifdef FEAT_MBYTE
9119 if (has_mbyte)
9120 {
9121 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009122 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 {
9124 o += (*mb_ptr2cells)(buffer + i);
9125 if (this_ru_col + o > WITH_WIDTH(width))
9126 {
9127 buffer[i] = NUL;
9128 break;
9129 }
9130 }
9131 }
9132 else
9133#endif
9134 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9135 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9136
9137 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9138 i = redraw_cmdline;
9139 screen_fill(row, row + 1,
9140 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9141 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9142 fillchar, fillchar, attr);
9143 /* don't redraw the cmdline because of showing the ruler */
9144 redraw_cmdline = i;
9145 wp->w_ru_cursor = wp->w_cursor;
9146 wp->w_ru_virtcol = wp->w_virtcol;
9147 wp->w_ru_empty = empty_line;
9148 wp->w_ru_topline = wp->w_topline;
9149 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9150#ifdef FEAT_DIFF
9151 wp->w_ru_topfill = wp->w_topfill;
9152#endif
9153 }
9154}
9155#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009156
9157#if defined(FEAT_LINEBREAK) || defined(PROTO)
9158/*
9159 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009160 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009161 * Otherwise it depends on 'numberwidth' and the line count.
9162 */
9163 int
9164number_width(wp)
9165 win_T *wp;
9166{
9167 int n;
9168 linenr_T lnum;
9169
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009170 lnum = wp->w_buffer->b_ml.ml_line_count;
9171 if (lnum == wp->w_nrwidth_line_count)
9172 return wp->w_nrwidth_width;
9173 wp->w_nrwidth_line_count = lnum;
9174
9175 n = 0;
9176 do
9177 {
9178 lnum /= 10;
9179 ++n;
9180 } while (lnum > 0);
9181
9182 /* 'numberwidth' gives the minimal width plus one */
9183 if (n < wp->w_p_nuw - 1)
9184 n = wp->w_p_nuw - 1;
9185
9186 wp->w_nrwidth_width = n;
9187 return n;
9188}
9189#endif