blob: d3c9552e1ca8bcddb27fa626ce42e6fea52a0a8d [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.
30 * ScreenLinesC1[] and ScreenLinesC2[] contain up to two composing characters
31 * (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 */
118static match_T match_hl; /* used for ":match" highlight matching */
119#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));
131static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, enum hlf_value hl));
132#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
137static int win_line __ARGS((win_T *, linenr_T, int, int));
138static 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
146#ifdef FEAT_RIGHTLEFT
147static void rl_mirror __ARGS((char_u *str));
148#endif
149#ifdef FEAT_VERTSPLIT
150static void draw_vsep_win __ARGS((win_T *wp, int row));
151#endif
152#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));
172static int screen_ins_lines __ARGS((int, int, int, int, win_T *wp));
173static void msg_pos_mode __ARGS((void));
174#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
175static int fillchar_status __ARGS((int *attr, int is_curwin));
176#endif
177#ifdef FEAT_VERTSPLIT
178static int fillchar_vsep __ARGS((int *attr));
179#endif
180#ifdef FEAT_STL_OPT
181static void win_redr_custom __ARGS((win_T *wp, int Ruler));
182#endif
183#ifdef FEAT_CMDL_INFO
184static void win_redr_ruler __ARGS((win_T *wp, int always));
185#endif
186
187#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
188/* Ugly global: overrule attribute used by screen_char() */
189static int screen_char_attr = 0;
190#endif
191
192/*
193 * Redraw the current window later, with update_screen(type).
194 * Set must_redraw only if not already set to a higher value.
195 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
196 */
197 void
198redraw_later(type)
199 int type;
200{
201 redraw_win_later(curwin, type);
202}
203
204 void
205redraw_win_later(wp, type)
206 win_T *wp;
207 int type;
208{
209 if (wp->w_redr_type < type)
210 {
211 wp->w_redr_type = type;
212 if (type >= NOT_VALID)
213 wp->w_lines_valid = 0;
214 if (must_redraw < type) /* must_redraw is the maximum of all windows */
215 must_redraw = type;
216 }
217}
218
219/*
220 * Force a complete redraw later. Also resets the highlighting. To be used
221 * after executing a shell command that messes up the screen.
222 */
223 void
224redraw_later_clear()
225{
226 redraw_all_later(CLEAR);
227 screen_attr = HL_BOLD | HL_UNDERLINE;
228}
229
230/*
231 * Mark all windows to be redrawn later.
232 */
233 void
234redraw_all_later(type)
235 int type;
236{
237 win_T *wp;
238
239 FOR_ALL_WINDOWS(wp)
240 {
241 redraw_win_later(wp, type);
242 }
243}
244
245/*
246 * Mark all windows that are editing the current buffer to be udpated later.
247 */
248 void
249redraw_curbuf_later(type)
250 int type;
251{
252 redraw_buf_later(curbuf, type);
253}
254
255 void
256redraw_buf_later(buf, type)
257 buf_T *buf;
258 int type;
259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 {
264 if (wp->w_buffer == buf)
265 redraw_win_later(wp, type);
266 }
267}
268
269/*
270 * Changed something in the current window, at buffer line "lnum", that
271 * requires that line and possibly other lines to be redrawn.
272 * Used when entering/leaving Insert mode with the cursor on a folded line.
273 * Used to remove the "$" from a change command.
274 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
275 * may become invalid and the whole window will have to be redrawn.
276 */
277/*ARGSUSED*/
278 void
279redrawWinline(lnum, invalid)
280 linenr_T lnum;
281 int invalid; /* window line height is invalid now */
282{
283#ifdef FEAT_FOLDING
284 int i;
285#endif
286
287 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
288 curwin->w_redraw_top = lnum;
289 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
290 curwin->w_redraw_bot = lnum;
291 redraw_later(VALID);
292
293#ifdef FEAT_FOLDING
294 if (invalid)
295 {
296 /* A w_lines[] entry for this lnum has become invalid. */
297 i = find_wl_entry(curwin, lnum);
298 if (i >= 0)
299 curwin->w_lines[i].wl_valid = FALSE;
300 }
301#endif
302}
303
304/*
305 * update all windows that are editing the current buffer
306 */
307 void
308update_curbuf(type)
309 int type;
310{
311 redraw_curbuf_later(type);
312 update_screen(type);
313}
314
315/*
316 * update_screen()
317 *
318 * Based on the current value of curwin->w_topline, transfer a screenfull
319 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
320 */
321 void
322update_screen(type)
323 int type;
324{
325 win_T *wp;
326 static int did_intro = FALSE;
327#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
328 int did_one;
329#endif
330
331 if (!screen_valid(TRUE))
332 return;
333
334 if (must_redraw)
335 {
336 if (type < must_redraw) /* use maximal type */
337 type = must_redraw;
338 must_redraw = 0;
339 }
340
341 /* Need to update w_lines[]. */
342 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
343 type = NOT_VALID;
344
345 if (!redrawing())
346 {
347 redraw_later(type); /* remember type for next time */
348 must_redraw = type;
349 if (type > INVERTED_ALL)
350 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
351 return;
352 }
353
354 updating_screen = TRUE;
355#ifdef FEAT_SYN_HL
356 ++display_tick; /* let syntax code know we're in a next round of
357 * display updating */
358#endif
359
360 /*
361 * if the screen was scrolled up when displaying a message, scroll it down
362 */
363 if (msg_scrolled)
364 {
365 clear_cmdline = TRUE;
366 if (msg_scrolled > Rows - 5) /* clearing is faster */
367 type = CLEAR;
368 else if (type != CLEAR)
369 {
370 check_for_delay(FALSE);
371 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
372 type = CLEAR;
373 FOR_ALL_WINDOWS(wp)
374 {
375 if (W_WINROW(wp) < msg_scrolled)
376 {
377 if (W_WINROW(wp) + wp->w_height > msg_scrolled
378 && wp->w_redr_type < REDRAW_TOP
379 && wp->w_lines_valid > 0
380 && wp->w_topline == wp->w_lines[0].wl_lnum)
381 {
382 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
383 wp->w_redr_type = REDRAW_TOP;
384 }
385 else
386 {
387 wp->w_redr_type = NOT_VALID;
388#ifdef FEAT_WINDOWS
389 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
390 <= msg_scrolled)
391 wp->w_redr_status = TRUE;
392#endif
393 }
394 }
395 }
396 redraw_cmdline = TRUE;
397 }
398 msg_scrolled = 0;
399 need_wait_return = FALSE;
400 }
401
402 /* reset cmdline_row now (may have been changed temporarily) */
403 compute_cmdrow();
404
405 /* Check for changed highlighting */
406 if (need_highlight_changed)
407 highlight_changed();
408
409 if (type == CLEAR) /* first clear screen */
410 {
411 screenclear(); /* will reset clear_cmdline */
412 type = NOT_VALID;
413 }
414
415 if (clear_cmdline) /* going to clear cmdline (done below) */
416 check_for_delay(FALSE);
417
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000418#ifdef FEAT_LINEBREAK
419 /* Force redraw when width of 'number' column changes. */
420 if (curwin->w_redr_type < NOT_VALID
421 && curwin->w_nrwidth != number_width(curwin))
422 curwin->w_redr_type = NOT_VALID;
423#endif
424
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 /*
426 * Only start redrawing if there is really something to do.
427 */
428 if (type == INVERTED)
429 update_curswant();
430 if (curwin->w_redr_type < type
431 && !((type == VALID
432 && curwin->w_lines[0].wl_valid
433#ifdef FEAT_DIFF
434 && curwin->w_topfill == curwin->w_old_topfill
435 && curwin->w_botfill == curwin->w_old_botfill
436#endif
437 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
438#ifdef FEAT_VISUAL
439 || (type == INVERTED
440 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
441 && curwin->w_old_visual_mode == VIsual_mode
442 && (curwin->w_valid & VALID_VIRTCOL)
443 && curwin->w_old_curswant == curwin->w_curswant)
444#endif
445 ))
446 curwin->w_redr_type = type;
447
448#ifdef FEAT_SYN_HL
449 /*
450 * Correct stored syntax highlighting info for changes in each displayed
451 * buffer. Each buffer must only be done once.
452 */
453 FOR_ALL_WINDOWS(wp)
454 {
455 if (wp->w_buffer->b_mod_set)
456 {
457# ifdef FEAT_WINDOWS
458 win_T *wwp;
459
460 /* Check if we already did this buffer. */
461 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
462 if (wwp->w_buffer == wp->w_buffer)
463 break;
464# endif
465 if (
466# ifdef FEAT_WINDOWS
467 wwp == wp &&
468# endif
469 syntax_present(wp->w_buffer))
470 syn_stack_apply_changes(wp->w_buffer);
471 }
472 }
473#endif
474
475 /*
476 * Go from top to bottom through the windows, redrawing the ones that need
477 * it.
478 */
479#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
480 did_one = FALSE;
481#endif
482#ifdef FEAT_SEARCH_EXTRA
483 search_hl.rm.regprog = NULL;
484#endif
485 FOR_ALL_WINDOWS(wp)
486 {
487 if (wp->w_redr_type != 0)
488 {
489 cursor_off();
490#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
491 if (!did_one)
492 {
493 did_one = TRUE;
494# ifdef FEAT_SEARCH_EXTRA
495 start_search_hl();
496# endif
497# ifdef FEAT_CLIPBOARD
498 /* When Visual area changed, may have to update selection. */
499 if (clip_star.available && clip_isautosel())
500 clip_update_selection();
501# endif
502#ifdef FEAT_GUI
503 /* Remove the cursor before starting to do anything, because
504 * scrolling may make it difficult to redraw the text under
505 * it. */
506 if (gui.in_use)
507 gui_undraw_cursor();
508#endif
509 }
510#endif
511 win_update(wp);
512 }
513
514#ifdef FEAT_WINDOWS
515 /* redraw status line after the window to minimize cursor movement */
516 if (wp->w_redr_status)
517 {
518 cursor_off();
519 win_redr_status(wp);
520 }
521#endif
522 }
523#if defined(FEAT_SEARCH_EXTRA)
524 end_search_hl();
525#endif
526
527#ifdef FEAT_WINDOWS
528 /* Reset b_mod_set flags. Going through all windows is probably faster
529 * than going through all buffers (there could be many buffers). */
530 for (wp = firstwin; wp != NULL; wp = wp->w_next)
531 wp->w_buffer->b_mod_set = FALSE;
532#else
533 curbuf->b_mod_set = FALSE;
534#endif
535
536 updating_screen = FALSE;
537#ifdef FEAT_GUI
538 gui_may_resize_shell();
539#endif
540
541 /* Clear or redraw the command line. Done last, because scrolling may
542 * mess up the command line. */
543 if (clear_cmdline || redraw_cmdline)
544 showmode();
545
546 /* May put up an introductory message when not editing a file */
547 if (!did_intro && bufempty()
548 && curbuf->b_fname == NULL
549#ifdef FEAT_WINDOWS
550 && firstwin->w_next == NULL
551#endif
552 && vim_strchr(p_shm, SHM_INTRO) == NULL)
553 intro_message(FALSE);
554 did_intro = TRUE;
555
556#ifdef FEAT_GUI
557 /* Redraw the cursor and update the scrollbars when all screen updating is
558 * done. */
559 if (gui.in_use)
560 {
561 out_flush(); /* required before updating the cursor */
562 if (did_one)
563 gui_update_cursor(FALSE, FALSE);
564 gui_update_scrollbars(FALSE);
565 }
566#endif
567}
568
569#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
570static void update_prepare __ARGS((void));
571static void update_finish __ARGS((void));
572
573/*
574 * Prepare for updating one or more windows.
575 */
576 static void
577update_prepare()
578{
579 cursor_off();
580 updating_screen = TRUE;
581#ifdef FEAT_GUI
582 /* Remove the cursor before starting to do anything, because scrolling may
583 * make it difficult to redraw the text under it. */
584 if (gui.in_use)
585 gui_undraw_cursor();
586#endif
587#ifdef FEAT_SEARCH_EXTRA
588 start_search_hl();
589#endif
590}
591
592/*
593 * Finish updating one or more windows.
594 */
595 static void
596update_finish()
597{
598 if (redraw_cmdline)
599 showmode();
600
601# ifdef FEAT_SEARCH_EXTRA
602 end_search_hl();
603# endif
604
605 updating_screen = FALSE;
606
607# ifdef FEAT_GUI
608 gui_may_resize_shell();
609
610 /* Redraw the cursor and update the scrollbars when all screen updating is
611 * done. */
612 if (gui.in_use)
613 {
614 out_flush(); /* required before updating the cursor */
615 gui_update_cursor(FALSE, FALSE);
616 gui_update_scrollbars(FALSE);
617 }
618# endif
619}
620#endif
621
622#if defined(FEAT_SIGNS) || defined(PROTO)
623 void
624update_debug_sign(buf, lnum)
625 buf_T *buf;
626 linenr_T lnum;
627{
628 win_T *wp;
629 int doit = FALSE;
630
631# ifdef FEAT_FOLDING
632 win_foldinfo.fi_level = 0;
633# endif
634
635 /* update/delete a specific mark */
636 FOR_ALL_WINDOWS(wp)
637 {
638 if (buf != NULL && lnum > 0)
639 {
640 if (wp->w_buffer == buf && lnum >= wp->w_topline
641 && lnum < wp->w_botline)
642 {
643 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
644 wp->w_redraw_top = lnum;
645 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
646 wp->w_redraw_bot = lnum;
647 redraw_win_later(wp, VALID);
648 }
649 }
650 else
651 redraw_win_later(wp, VALID);
652 if (wp->w_redr_type != 0)
653 doit = TRUE;
654 }
655
656 if (!doit)
657 return;
658
659 /* update all windows that need updating */
660 update_prepare();
661
662# ifdef FEAT_WINDOWS
663 for (wp = firstwin; wp; wp = wp->w_next)
664 {
665 if (wp->w_redr_type != 0)
666 win_update(wp);
667 if (wp->w_redr_status)
668 win_redr_status(wp);
669 }
670# else
671 if (curwin->w_redr_type != 0)
672 win_update(curwin);
673# endif
674
675 update_finish();
676}
677#endif
678
679
680#if defined(FEAT_GUI) || defined(PROTO)
681/*
682 * Update a single window, its status line and maybe the command line msg.
683 * Used for the GUI scrollbar.
684 */
685 void
686updateWindow(wp)
687 win_T *wp;
688{
689 update_prepare();
690
691#ifdef FEAT_CLIPBOARD
692 /* When Visual area changed, may have to update selection. */
693 if (clip_star.available && clip_isautosel())
694 clip_update_selection();
695#endif
696 win_update(wp);
697#ifdef FEAT_WINDOWS
698 if (wp->w_redr_status
699# ifdef FEAT_CMDL_INFO
700 || p_ru
701# endif
702# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000703 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704# endif
705 )
706 win_redr_status(wp);
707#endif
708
709 update_finish();
710}
711#endif
712
713/*
714 * Update a single window.
715 *
716 * This may cause the windows below it also to be redrawn (when clearing the
717 * screen or scrolling lines).
718 *
719 * How the window is redrawn depends on wp->w_redr_type. Each type also
720 * implies the one below it.
721 * NOT_VALID redraw the whole window
722 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
723 * INVERTED redraw the changed part of the Visual area
724 * INVERTED_ALL redraw the whole Visual area
725 * VALID 1. scroll up/down to adjust for a changed w_topline
726 * 2. update lines at the top when scrolled down
727 * 3. redraw changed text:
728 * - if wp->w_buffer->b_mod_set set, udpate lines between
729 * b_mod_top and b_mod_bot.
730 * - if wp->w_redraw_top non-zero, redraw lines between
731 * wp->w_redraw_top and wp->w_redr_bot.
732 * - continue redrawing when syntax status is invalid.
733 * 4. if scrolled up, update lines at the bottom.
734 * This results in three areas that may need updating:
735 * top: from first row to top_end (when scrolled down)
736 * mid: from mid_start to mid_end (update inversion or changed text)
737 * bot: from bot_start to last row (when scrolled up)
738 */
739 static void
740win_update(wp)
741 win_T *wp;
742{
743 buf_T *buf = wp->w_buffer;
744 int type;
745 int top_end = 0; /* Below last row of the top area that needs
746 updating. 0 when no top area updating. */
747 int mid_start = 999;/* first row of the mid area that needs
748 updating. 999 when no mid area updating. */
749 int mid_end = 0; /* Below last row of the mid area that needs
750 updating. 0 when no mid area updating. */
751 int bot_start = 999;/* first row of the bot area that needs
752 updating. 999 when no bot area updating */
753#ifdef FEAT_VISUAL
754 int scrolled_down = FALSE; /* TRUE when scrolled down when
755 w_topline got smaller a bit */
756#endif
757#ifdef FEAT_SEARCH_EXTRA
758 int top_to_mod = FALSE; /* redraw above mod_top */
759#endif
760
761 int row; /* current window row to display */
762 linenr_T lnum; /* current buffer lnum to display */
763 int idx; /* current index in w_lines[] */
764 int srow; /* starting row of the current line */
765
766 int eof = FALSE; /* if TRUE, we hit the end of the file */
767 int didline = FALSE; /* if TRUE, we finished the last line */
768 int i;
769 long j;
770 static int recursive = FALSE; /* being called recursively */
771 int old_botline = wp->w_botline;
772#ifdef FEAT_FOLDING
773 long fold_count;
774#endif
775#ifdef FEAT_SYN_HL
776 /* remember what happened to the previous line, to know if
777 * check_visual_highlight() can be used */
778#define DID_NONE 1 /* didn't update a line */
779#define DID_LINE 2 /* updated a normal line */
780#define DID_FOLD 3 /* updated a folded line */
781 int did_update = DID_NONE;
782 linenr_T syntax_last_parsed = 0; /* last parsed text line */
783#endif
784 linenr_T mod_top = 0;
785 linenr_T mod_bot = 0;
786#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
787 int save_got_int;
788#endif
789
790 type = wp->w_redr_type;
791
792 if (type == NOT_VALID)
793 {
794#ifdef FEAT_WINDOWS
795 wp->w_redr_status = TRUE;
796#endif
797 wp->w_lines_valid = 0;
798 }
799
800 /* Window is zero-height: nothing to draw. */
801 if (wp->w_height == 0)
802 {
803 wp->w_redr_type = 0;
804 return;
805 }
806
807#ifdef FEAT_VERTSPLIT
808 /* Window is zero-width: Only need to draw the separator. */
809 if (wp->w_width == 0)
810 {
811 /* draw the vertical separator right of this window */
812 draw_vsep_win(wp, 0);
813 wp->w_redr_type = 0;
814 return;
815 }
816#endif
817
818#ifdef FEAT_SEARCH_EXTRA
819 /* Setup for ":match" highlighting. Disable any previous match */
820 match_hl.rm = wp->w_match;
821 if (wp->w_match_id == 0)
822 match_hl.attr = 0;
823 else
824 match_hl.attr = syn_id2attr(wp->w_match_id);
825 match_hl.buf = buf;
826 match_hl.lnum = 0;
827 search_hl.buf = buf;
828 search_hl.lnum = 0;
829 search_hl.first_lnum = 0;
830#endif
831
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000832#ifdef FEAT_LINEBREAK
833 /* Force redraw when width of 'number' column changes. */
834 i = number_width(curwin);
835 if (curwin->w_nrwidth != i)
836 {
837 type = NOT_VALID;
838 curwin->w_nrwidth = i;
839 }
840 else
841#endif
842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
844 {
845 /*
846 * When there are both inserted/deleted lines and specific lines to be
847 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
848 * everything (only happens when redrawing is off for while).
849 */
850 type = NOT_VALID;
851 }
852 else
853 {
854 /*
855 * Set mod_top to the first line that needs displaying because of
856 * changes. Set mod_bot to the first line after the changes.
857 */
858 mod_top = wp->w_redraw_top;
859 if (wp->w_redraw_bot != 0)
860 mod_bot = wp->w_redraw_bot + 1;
861 else
862 mod_bot = 0;
863 wp->w_redraw_top = 0; /* reset for next time */
864 wp->w_redraw_bot = 0;
865 if (buf->b_mod_set)
866 {
867 if (mod_top == 0 || mod_top > buf->b_mod_top)
868 {
869 mod_top = buf->b_mod_top;
870#ifdef FEAT_SYN_HL
871 /* Need to redraw lines above the change that may be included
872 * in a pattern match. */
873 if (syntax_present(buf))
874 {
875 mod_top -= buf->b_syn_sync_linebreaks;
876 if (mod_top < 1)
877 mod_top = 1;
878 }
879#endif
880 }
881 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
882 mod_bot = buf->b_mod_bot;
883
884#ifdef FEAT_SEARCH_EXTRA
885 /* When 'hlsearch' is on and using a multi-line search pattern, a
886 * change in one line may make the Search highlighting in a
887 * previous line invalid. Simple solution: redraw all visible
888 * lines above the change.
889 * Same for a ":match" pattern.
890 */
891 if ((search_hl.rm.regprog != NULL
892 && re_multiline(search_hl.rm.regprog))
893 || (match_hl.rm.regprog != NULL
894 && re_multiline(match_hl.rm.regprog)))
895 top_to_mod = TRUE;
896#endif
897 }
898#ifdef FEAT_FOLDING
899 if (mod_top != 0 && hasAnyFolding(wp))
900 {
901 linenr_T lnumt, lnumb;
902
903 /*
904 * A change in a line can cause lines above it to become folded or
905 * unfolded. Find the top most buffer line that may be affected.
906 * If the line was previously folded and displayed, get the first
907 * line of that fold. If the line is folded now, get the first
908 * folded line. Use the minimum of these two.
909 */
910
911 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
912 * the line below it. If there is no valid entry, use w_topline.
913 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
914 * to this line. If there is no valid entry, use MAXLNUM. */
915 lnumt = wp->w_topline;
916 lnumb = MAXLNUM;
917 for (i = 0; i < wp->w_lines_valid; ++i)
918 if (wp->w_lines[i].wl_valid)
919 {
920 if (wp->w_lines[i].wl_lastlnum < mod_top)
921 lnumt = wp->w_lines[i].wl_lastlnum + 1;
922 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
923 {
924 lnumb = wp->w_lines[i].wl_lnum;
925 /* When there is a fold column it might need updating
926 * in the next line ("J" just above an open fold). */
927 if (wp->w_p_fdc > 0)
928 ++lnumb;
929 }
930 }
931
932 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
933 if (mod_top > lnumt)
934 mod_top = lnumt;
935
936 /* Now do the same for the bottom line (one above mod_bot). */
937 --mod_bot;
938 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
939 ++mod_bot;
940 if (mod_bot < lnumb)
941 mod_bot = lnumb;
942 }
943#endif
944
945 /* When a change starts above w_topline and the end is below
946 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000947 * If the end of the change is above w_topline: do like no change was
948 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (mod_top != 0 && mod_top < wp->w_topline)
950 {
951 if (mod_bot > wp->w_topline)
952 mod_top = wp->w_topline;
953#ifdef FEAT_SYN_HL
954 else if (syntax_present(buf))
955 top_end = 1;
956#endif
957 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000958
959 /* When line numbers are displayed need to redraw all lines below
960 * inserted/deleted lines. */
961 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
962 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 }
964
965 /*
966 * When only displaying the lines at the top, set top_end. Used when
967 * window has scrolled down for msg_scrolled.
968 */
969 if (type == REDRAW_TOP)
970 {
971 j = 0;
972 for (i = 0; i < wp->w_lines_valid; ++i)
973 {
974 j += wp->w_lines[i].wl_size;
975 if (j >= wp->w_upd_rows)
976 {
977 top_end = j;
978 break;
979 }
980 }
981 if (top_end == 0)
982 /* not found (cannot happen?): redraw everything */
983 type = NOT_VALID;
984 else
985 /* top area defined, the rest is VALID */
986 type = VALID;
987 }
988
989 /*
990 * If there are no changes on the screen that require a complete redraw,
991 * handle three cases:
992 * 1: we are off the top of the screen by a few lines: scroll down
993 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
994 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
995 * w_lines[] that needs updating.
996 */
997 if ((type == VALID || type == INVERTED || type == INVERTED_ALL)
998#ifdef FEAT_DIFF
999 && !wp->w_botfill && !wp->w_old_botfill
1000#endif
1001 )
1002 {
1003 if (mod_top != 0 && wp->w_topline == mod_top)
1004 {
1005 /*
1006 * w_topline is the first changed line, the scrolling will be done
1007 * further down.
1008 */
1009 }
1010 else if (wp->w_lines[0].wl_valid
1011 && (wp->w_topline < wp->w_lines[0].wl_lnum
1012#ifdef FEAT_DIFF
1013 || (wp->w_topline == wp->w_lines[0].wl_lnum
1014 && wp->w_topfill > wp->w_old_topfill)
1015#endif
1016 ))
1017 {
1018 /*
1019 * New topline is above old topline: May scroll down.
1020 */
1021#ifdef FEAT_FOLDING
1022 if (hasAnyFolding(wp))
1023 {
1024 linenr_T ln;
1025
1026 /* count the number of lines we are off, counting a sequence
1027 * of folded lines as one */
1028 j = 0;
1029 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1030 {
1031 ++j;
1032 if (j >= wp->w_height - 2)
1033 break;
1034 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1035 }
1036 }
1037 else
1038#endif
1039 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1040 if (j < wp->w_height - 2) /* not too far off */
1041 {
1042 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1043#ifdef FEAT_DIFF
1044 /* insert extra lines for previously invisible filler lines */
1045 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1046 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1047 - wp->w_old_topfill;
1048#endif
1049 if (i < wp->w_height - 2) /* less than a screen off */
1050 {
1051 /*
1052 * Try to insert the correct number of lines.
1053 * If not the last window, delete the lines at the bottom.
1054 * win_ins_lines may fail when the terminal can't do it.
1055 */
1056 if (i > 0)
1057 check_for_delay(FALSE);
1058 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1059 {
1060 if (wp->w_lines_valid != 0)
1061 {
1062 /* Need to update rows that are new, stop at the
1063 * first one that scrolled down. */
1064 top_end = i;
1065#ifdef FEAT_VISUAL
1066 scrolled_down = TRUE;
1067#endif
1068
1069 /* Move the entries that were scrolled, disable
1070 * the entries for the lines to be redrawn. */
1071 if ((wp->w_lines_valid += j) > wp->w_height)
1072 wp->w_lines_valid = wp->w_height;
1073 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1074 wp->w_lines[idx] = wp->w_lines[idx - j];
1075 while (idx >= 0)
1076 wp->w_lines[idx--].wl_valid = FALSE;
1077 }
1078 }
1079 else
1080 mid_start = 0; /* redraw all lines */
1081 }
1082 else
1083 mid_start = 0; /* redraw all lines */
1084 }
1085 else
1086 mid_start = 0; /* redraw all lines */
1087 }
1088 else
1089 {
1090 /*
1091 * New topline is at or below old topline: May scroll up.
1092 * When topline didn't change, find first entry in w_lines[] that
1093 * needs updating.
1094 */
1095
1096 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1097 j = -1;
1098 row = 0;
1099 for (i = 0; i < wp->w_lines_valid; i++)
1100 {
1101 if (wp->w_lines[i].wl_valid
1102 && wp->w_lines[i].wl_lnum == wp->w_topline)
1103 {
1104 j = i;
1105 break;
1106 }
1107 row += wp->w_lines[i].wl_size;
1108 }
1109 if (j == -1)
1110 {
1111 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1112 * lines */
1113 mid_start = 0;
1114 }
1115 else
1116 {
1117 /*
1118 * Try to delete the correct number of lines.
1119 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1120 */
1121#ifdef FEAT_DIFF
1122 /* If the topline didn't change, delete old filler lines,
1123 * otherwise delete filler lines of the new topline... */
1124 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1125 row += wp->w_old_topfill;
1126 else
1127 row += diff_check_fill(wp, wp->w_topline);
1128 /* ... but don't delete new filler lines. */
1129 row -= wp->w_topfill;
1130#endif
1131 if (row > 0)
1132 {
1133 check_for_delay(FALSE);
1134 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1135 bot_start = wp->w_height - row;
1136 else
1137 mid_start = 0; /* redraw all lines */
1138 }
1139 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1140 {
1141 /*
1142 * Skip the lines (below the deleted lines) that are still
1143 * valid and don't need redrawing. Copy their info
1144 * upwards, to compensate for the deleted lines. Set
1145 * bot_start to the first row that needs redrawing.
1146 */
1147 bot_start = 0;
1148 idx = 0;
1149 for (;;)
1150 {
1151 wp->w_lines[idx] = wp->w_lines[j];
1152 /* stop at line that didn't fit, unless it is still
1153 * valid (no lines deleted) */
1154 if (row > 0 && bot_start + row
1155 + (int)wp->w_lines[j].wl_size > wp->w_height)
1156 {
1157 wp->w_lines_valid = idx + 1;
1158 break;
1159 }
1160 bot_start += wp->w_lines[idx++].wl_size;
1161
1162 /* stop at the last valid entry in w_lines[].wl_size */
1163 if (++j >= wp->w_lines_valid)
1164 {
1165 wp->w_lines_valid = idx;
1166 break;
1167 }
1168 }
1169#ifdef FEAT_DIFF
1170 /* Correct the first entry for filler lines at the top
1171 * when it won't get updated below. */
1172 if (wp->w_p_diff && bot_start > 0)
1173 wp->w_lines[0].wl_size =
1174 plines_win_nofill(wp, wp->w_topline, TRUE)
1175 + wp->w_topfill;
1176#endif
1177 }
1178 }
1179 }
1180
1181 /* When starting redraw in the first line, redraw all lines. When
1182 * there is only one window it's probably faster to clear the screen
1183 * first. */
1184 if (mid_start == 0)
1185 {
1186 mid_end = wp->w_height;
1187 if (lastwin == firstwin)
1188 screenclear();
1189 }
1190 }
1191 else
1192 {
1193 /* Not VALID or INVERTED: redraw all lines. */
1194 mid_start = 0;
1195 mid_end = wp->w_height;
1196 }
1197
1198#ifdef FEAT_VISUAL
1199 /* check if we are updating or removing the inverted part */
1200 if ((VIsual_active && buf == curwin->w_buffer)
1201 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1202 {
1203 linenr_T from, to;
1204
1205 if (VIsual_active)
1206 {
1207 if (VIsual_active
1208 && (VIsual_mode != wp->w_old_visual_mode
1209 || type == INVERTED_ALL))
1210 {
1211 /*
1212 * If the type of Visual selection changed, redraw the whole
1213 * selection. Also when the ownership of the X selection is
1214 * gained or lost.
1215 */
1216 if (curwin->w_cursor.lnum < VIsual.lnum)
1217 {
1218 from = curwin->w_cursor.lnum;
1219 to = VIsual.lnum;
1220 }
1221 else
1222 {
1223 from = VIsual.lnum;
1224 to = curwin->w_cursor.lnum;
1225 }
1226 /* redraw more when the cursor moved as well */
1227 if (wp->w_old_cursor_lnum < from)
1228 from = wp->w_old_cursor_lnum;
1229 if (wp->w_old_cursor_lnum > to)
1230 to = wp->w_old_cursor_lnum;
1231 if (wp->w_old_visual_lnum < from)
1232 from = wp->w_old_visual_lnum;
1233 if (wp->w_old_visual_lnum > to)
1234 to = wp->w_old_visual_lnum;
1235 }
1236 else
1237 {
1238 /*
1239 * Find the line numbers that need to be updated: The lines
1240 * between the old cursor position and the current cursor
1241 * position. Also check if the Visual position changed.
1242 */
1243 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1244 {
1245 from = curwin->w_cursor.lnum;
1246 to = wp->w_old_cursor_lnum;
1247 }
1248 else
1249 {
1250 from = wp->w_old_cursor_lnum;
1251 to = curwin->w_cursor.lnum;
1252 if (from == 0) /* Visual mode just started */
1253 from = to;
1254 }
1255
1256 if (VIsual.lnum != wp->w_old_visual_lnum)
1257 {
1258 if (wp->w_old_visual_lnum < from
1259 && wp->w_old_visual_lnum != 0)
1260 from = wp->w_old_visual_lnum;
1261 if (wp->w_old_visual_lnum > to)
1262 to = wp->w_old_visual_lnum;
1263 if (VIsual.lnum < from)
1264 from = VIsual.lnum;
1265 if (VIsual.lnum > to)
1266 to = VIsual.lnum;
1267 }
1268 }
1269
1270 /*
1271 * If in block mode and changed column or curwin->w_curswant:
1272 * update all lines.
1273 * First compute the actual start and end column.
1274 */
1275 if (VIsual_mode == Ctrl_V)
1276 {
1277 colnr_T fromc, toc;
1278
1279 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1280 ++toc;
1281 if (curwin->w_curswant == MAXCOL)
1282 toc = MAXCOL;
1283
1284 if (fromc != wp->w_old_cursor_fcol
1285 || toc != wp->w_old_cursor_lcol)
1286 {
1287 if (from > VIsual.lnum)
1288 from = VIsual.lnum;
1289 if (to < VIsual.lnum)
1290 to = VIsual.lnum;
1291 }
1292 wp->w_old_cursor_fcol = fromc;
1293 wp->w_old_cursor_lcol = toc;
1294 }
1295 }
1296 else
1297 {
1298 /* Use the line numbers of the old Visual area. */
1299 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1300 {
1301 from = wp->w_old_cursor_lnum;
1302 to = wp->w_old_visual_lnum;
1303 }
1304 else
1305 {
1306 from = wp->w_old_visual_lnum;
1307 to = wp->w_old_cursor_lnum;
1308 }
1309 }
1310
1311 /*
1312 * There is no need to update lines above the top of the window.
1313 */
1314 if (from < wp->w_topline)
1315 from = wp->w_topline;
1316
1317 /*
1318 * If we know the value of w_botline, use it to restrict the update to
1319 * the lines that are visible in the window.
1320 */
1321 if (wp->w_valid & VALID_BOTLINE)
1322 {
1323 if (from >= wp->w_botline)
1324 from = wp->w_botline - 1;
1325 if (to >= wp->w_botline)
1326 to = wp->w_botline - 1;
1327 }
1328
1329 /*
1330 * Find the minimal part to be updated.
1331 * Watch out for scrolling that made entries in w_lines[] invalid.
1332 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1333 * top_end; need to redraw from top_end to the "to" line.
1334 * A middle mouse click with a Visual selection may change the text
1335 * above the Visual area and reset wl_valid, do count these for
1336 * mid_end (in srow).
1337 */
1338 if (mid_start > 0)
1339 {
1340 lnum = wp->w_topline;
1341 idx = 0;
1342 srow = 0;
1343 if (scrolled_down)
1344 mid_start = top_end;
1345 else
1346 mid_start = 0;
1347 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1348 {
1349 if (wp->w_lines[idx].wl_valid)
1350 mid_start += wp->w_lines[idx].wl_size;
1351 else if (!scrolled_down)
1352 srow += wp->w_lines[idx].wl_size;
1353 ++idx;
1354# ifdef FEAT_FOLDING
1355 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1356 lnum = wp->w_lines[idx].wl_lnum;
1357 else
1358# endif
1359 ++lnum;
1360 }
1361 srow += mid_start;
1362 mid_end = wp->w_height;
1363 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1364 {
1365 if (wp->w_lines[idx].wl_valid
1366 && wp->w_lines[idx].wl_lnum >= to + 1)
1367 {
1368 /* Only update until first row of this line */
1369 mid_end = srow;
1370 break;
1371 }
1372 srow += wp->w_lines[idx].wl_size;
1373 }
1374 }
1375 }
1376
1377 if (VIsual_active && buf == curwin->w_buffer)
1378 {
1379 wp->w_old_visual_mode = VIsual_mode;
1380 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1381 wp->w_old_visual_lnum = VIsual.lnum;
1382 wp->w_old_curswant = curwin->w_curswant;
1383 }
1384 else
1385 {
1386 wp->w_old_visual_mode = 0;
1387 wp->w_old_cursor_lnum = 0;
1388 wp->w_old_visual_lnum = 0;
1389 }
1390#endif /* FEAT_VISUAL */
1391
1392#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1393 /* reset got_int, otherwise regexp won't work */
1394 save_got_int = got_int;
1395 got_int = 0;
1396#endif
1397#ifdef FEAT_FOLDING
1398 win_foldinfo.fi_level = 0;
1399#endif
1400
1401 /*
1402 * Update all the window rows.
1403 */
1404 idx = 0; /* first entry in w_lines[].wl_size */
1405 row = 0;
1406 srow = 0;
1407 lnum = wp->w_topline; /* first line shown in window */
1408 for (;;)
1409 {
1410 /* stop updating when reached the end of the window (check for _past_
1411 * the end of the window is at the end of the loop) */
1412 if (row == wp->w_height)
1413 {
1414 didline = TRUE;
1415 break;
1416 }
1417
1418 /* stop updating when hit the end of the file */
1419 if (lnum > buf->b_ml.ml_line_count)
1420 {
1421 eof = TRUE;
1422 break;
1423 }
1424
1425 /* Remember the starting row of the line that is going to be dealt
1426 * with. It is used further down when the line doesn't fit. */
1427 srow = row;
1428
1429 /*
1430 * Update a line when it is in an area that needs updating, when it
1431 * has changes or w_lines[idx] is invalid.
1432 * bot_start may be halfway a wrapped line after using
1433 * win_del_lines(), check if the current line includes it.
1434 * When syntax folding is being used, the saved syntax states will
1435 * already have been updated, we can't see where the syntax state is
1436 * the same again, just update until the end of the window.
1437 */
1438 if (row < top_end
1439 || (row >= mid_start && row < mid_end)
1440#ifdef FEAT_SEARCH_EXTRA
1441 || top_to_mod
1442#endif
1443 || idx >= wp->w_lines_valid
1444 || (row + wp->w_lines[idx].wl_size > bot_start)
1445 || (mod_top != 0
1446 && (lnum == mod_top
1447 || (lnum >= mod_top
1448 && (lnum < mod_bot
1449#ifdef FEAT_SYN_HL
1450 || did_update == DID_FOLD
1451 || (did_update == DID_LINE
1452 && syntax_present(buf)
1453 && (
1454# ifdef FEAT_FOLDING
1455 (foldmethodIsSyntax(wp)
1456 && hasAnyFolding(wp)) ||
1457# endif
1458 syntax_check_changed(lnum)))
1459#endif
1460 )))))
1461 {
1462#ifdef FEAT_SEARCH_EXTRA
1463 if (lnum == mod_top)
1464 top_to_mod = FALSE;
1465#endif
1466
1467 /*
1468 * When at start of changed lines: May scroll following lines
1469 * up or down to minimize redrawing.
1470 * Don't do this when the change continues until the end.
1471 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1472 */
1473 if (lnum == mod_top
1474 && mod_bot != MAXLNUM
1475 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1476 {
1477 int old_rows = 0;
1478 int new_rows = 0;
1479 int xtra_rows;
1480 linenr_T l;
1481
1482 /* Count the old number of window rows, using w_lines[], which
1483 * should still contain the sizes for the lines as they are
1484 * currently displayed. */
1485 for (i = idx; i < wp->w_lines_valid; ++i)
1486 {
1487 /* Only valid lines have a meaningful wl_lnum. Invalid
1488 * lines are part of the changed area. */
1489 if (wp->w_lines[i].wl_valid
1490 && wp->w_lines[i].wl_lnum == mod_bot)
1491 break;
1492 old_rows += wp->w_lines[i].wl_size;
1493#ifdef FEAT_FOLDING
1494 if (wp->w_lines[i].wl_valid
1495 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1496 {
1497 /* Must have found the last valid entry above mod_bot.
1498 * Add following invalid entries. */
1499 ++i;
1500 while (i < wp->w_lines_valid
1501 && !wp->w_lines[i].wl_valid)
1502 old_rows += wp->w_lines[i++].wl_size;
1503 break;
1504 }
1505#endif
1506 }
1507
1508 if (i >= wp->w_lines_valid)
1509 {
1510 /* We can't find a valid line below the changed lines,
1511 * need to redraw until the end of the window.
1512 * Inserting/deleting lines has no use. */
1513 bot_start = 0;
1514 }
1515 else
1516 {
1517 /* Able to count old number of rows: Count new window
1518 * rows, and may insert/delete lines */
1519 j = idx;
1520 for (l = lnum; l < mod_bot; ++l)
1521 {
1522#ifdef FEAT_FOLDING
1523 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1524 ++new_rows;
1525 else
1526#endif
1527#ifdef FEAT_DIFF
1528 if (l == wp->w_topline)
1529 new_rows += plines_win_nofill(wp, l, TRUE)
1530 + wp->w_topfill;
1531 else
1532#endif
1533 new_rows += plines_win(wp, l, TRUE);
1534 ++j;
1535 if (new_rows > wp->w_height - row - 2)
1536 {
1537 /* it's getting too much, must redraw the rest */
1538 new_rows = 9999;
1539 break;
1540 }
1541 }
1542 xtra_rows = new_rows - old_rows;
1543 if (xtra_rows < 0)
1544 {
1545 /* May scroll text up. If there is not enough
1546 * remaining text or scrolling fails, must redraw the
1547 * rest. If scrolling works, must redraw the text
1548 * below the scrolled text. */
1549 if (row - xtra_rows >= wp->w_height - 2)
1550 mod_bot = MAXLNUM;
1551 else
1552 {
1553 check_for_delay(FALSE);
1554 if (win_del_lines(wp, row,
1555 -xtra_rows, FALSE, FALSE) == FAIL)
1556 mod_bot = MAXLNUM;
1557 else
1558 bot_start = wp->w_height + xtra_rows;
1559 }
1560 }
1561 else if (xtra_rows > 0)
1562 {
1563 /* May scroll text down. If there is not enough
1564 * remaining text of scrolling fails, must redraw the
1565 * rest. */
1566 if (row + xtra_rows >= wp->w_height - 2)
1567 mod_bot = MAXLNUM;
1568 else
1569 {
1570 check_for_delay(FALSE);
1571 if (win_ins_lines(wp, row + old_rows,
1572 xtra_rows, FALSE, FALSE) == FAIL)
1573 mod_bot = MAXLNUM;
1574 else if (top_end > row + old_rows)
1575 /* Scrolled the part at the top that requires
1576 * updating down. */
1577 top_end += xtra_rows;
1578 }
1579 }
1580
1581 /* When not updating the rest, may need to move w_lines[]
1582 * entries. */
1583 if (mod_bot != MAXLNUM && i != j)
1584 {
1585 if (j < i)
1586 {
1587 int x = row + new_rows;
1588
1589 /* move entries in w_lines[] upwards */
1590 for (;;)
1591 {
1592 /* stop at last valid entry in w_lines[] */
1593 if (i >= wp->w_lines_valid)
1594 {
1595 wp->w_lines_valid = j;
1596 break;
1597 }
1598 wp->w_lines[j] = wp->w_lines[i];
1599 /* stop at a line that won't fit */
1600 if (x + (int)wp->w_lines[j].wl_size
1601 > wp->w_height)
1602 {
1603 wp->w_lines_valid = j + 1;
1604 break;
1605 }
1606 x += wp->w_lines[j++].wl_size;
1607 ++i;
1608 }
1609 if (bot_start > x)
1610 bot_start = x;
1611 }
1612 else /* j > i */
1613 {
1614 /* move entries in w_lines[] downwards */
1615 j -= i;
1616 wp->w_lines_valid += j;
1617 if (wp->w_lines_valid > wp->w_height)
1618 wp->w_lines_valid = wp->w_height;
1619 for (i = wp->w_lines_valid; i - j >= idx; --i)
1620 wp->w_lines[i] = wp->w_lines[i - j];
1621
1622 /* The w_lines[] entries for inserted lines are
1623 * now invalid, but wl_size may be used above.
1624 * Reset to zero. */
1625 while (i >= idx)
1626 {
1627 wp->w_lines[i].wl_size = 0;
1628 wp->w_lines[i--].wl_valid = FALSE;
1629 }
1630 }
1631 }
1632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 }
1634
1635#ifdef FEAT_FOLDING
1636 /*
1637 * When lines are folded, display one line for all of them.
1638 * Otherwise, display normally (can be several display lines when
1639 * 'wrap' is on).
1640 */
1641 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1642 if (fold_count != 0)
1643 {
1644 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1645 ++row;
1646 --fold_count;
1647 wp->w_lines[idx].wl_folded = TRUE;
1648 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1649# ifdef FEAT_SYN_HL
1650 did_update = DID_FOLD;
1651# endif
1652 }
1653 else
1654#endif
1655 if (idx < wp->w_lines_valid
1656 && wp->w_lines[idx].wl_valid
1657 && wp->w_lines[idx].wl_lnum == lnum
1658 && lnum > wp->w_topline
1659 && !(dy_flags & DY_LASTLINE)
1660 && srow + wp->w_lines[idx].wl_size > wp->w_height
1661#ifdef FEAT_DIFF
1662 && diff_check_fill(wp, lnum) == 0
1663#endif
1664 )
1665 {
1666 /* This line is not going to fit. Don't draw anything here,
1667 * will draw "@ " lines below. */
1668 row = wp->w_height + 1;
1669 }
1670 else
1671 {
1672#ifdef FEAT_SEARCH_EXTRA
1673 prepare_search_hl(wp, lnum);
1674#endif
1675#ifdef FEAT_SYN_HL
1676 /* Let the syntax stuff know we skipped a few lines. */
1677 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1678 && syntax_present(buf))
1679 syntax_end_parsing(syntax_last_parsed + 1);
1680#endif
1681
1682 /*
1683 * Display one line.
1684 */
1685 row = win_line(wp, lnum, srow, wp->w_height);
1686
1687#ifdef FEAT_FOLDING
1688 wp->w_lines[idx].wl_folded = FALSE;
1689 wp->w_lines[idx].wl_lastlnum = lnum;
1690#endif
1691#ifdef FEAT_SYN_HL
1692 did_update = DID_LINE;
1693 syntax_last_parsed = lnum;
1694#endif
1695 }
1696
1697 wp->w_lines[idx].wl_lnum = lnum;
1698 wp->w_lines[idx].wl_valid = TRUE;
1699 if (row > wp->w_height) /* past end of screen */
1700 {
1701 /* we may need the size of that too long line later on */
1702 if (dollar_vcol == 0)
1703 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1704 ++idx;
1705 break;
1706 }
1707 if (dollar_vcol == 0)
1708 wp->w_lines[idx].wl_size = row - srow;
1709 ++idx;
1710#ifdef FEAT_FOLDING
1711 lnum += fold_count + 1;
1712#else
1713 ++lnum;
1714#endif
1715 }
1716 else
1717 {
1718 /* This line does not need updating, advance to the next one */
1719 row += wp->w_lines[idx++].wl_size;
1720 if (row > wp->w_height) /* past end of screen */
1721 break;
1722#ifdef FEAT_FOLDING
1723 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1724#else
1725 ++lnum;
1726#endif
1727#ifdef FEAT_SYN_HL
1728 did_update = DID_NONE;
1729#endif
1730 }
1731
1732 if (lnum > buf->b_ml.ml_line_count)
1733 {
1734 eof = TRUE;
1735 break;
1736 }
1737 }
1738 /*
1739 * End of loop over all window lines.
1740 */
1741
1742
1743 if (idx > wp->w_lines_valid)
1744 wp->w_lines_valid = idx;
1745
1746#ifdef FEAT_SYN_HL
1747 /*
1748 * Let the syntax stuff know we stop parsing here.
1749 */
1750 if (syntax_last_parsed != 0 && syntax_present(buf))
1751 syntax_end_parsing(syntax_last_parsed + 1);
1752#endif
1753
1754 /*
1755 * If we didn't hit the end of the file, and we didn't finish the last
1756 * line we were working on, then the line didn't fit.
1757 */
1758 wp->w_empty_rows = 0;
1759#ifdef FEAT_DIFF
1760 wp->w_filler_rows = 0;
1761#endif
1762 if (!eof && !didline)
1763 {
1764 if (lnum == wp->w_topline)
1765 {
1766 /*
1767 * Single line that does not fit!
1768 * Don't overwrite it, it can be edited.
1769 */
1770 wp->w_botline = lnum + 1;
1771 }
1772#ifdef FEAT_DIFF
1773 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1774 {
1775 /* Window ends in filler lines. */
1776 wp->w_botline = lnum;
1777 wp->w_filler_rows = wp->w_height - srow;
1778 }
1779#endif
1780 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1781 {
1782 /*
1783 * Last line isn't finished: Display "@@@" at the end.
1784 */
1785 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1786 W_WINROW(wp) + wp->w_height,
1787 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1788 '@', '@', hl_attr(HLF_AT));
1789 set_empty_rows(wp, srow);
1790 wp->w_botline = lnum;
1791 }
1792 else
1793 {
1794 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1795 wp->w_botline = lnum;
1796 }
1797 }
1798 else
1799 {
1800#ifdef FEAT_VERTSPLIT
1801 draw_vsep_win(wp, row);
1802#endif
1803 if (eof) /* we hit the end of the file */
1804 {
1805 wp->w_botline = buf->b_ml.ml_line_count + 1;
1806#ifdef FEAT_DIFF
1807 j = diff_check_fill(wp, wp->w_botline);
1808 if (j > 0 && !wp->w_botfill)
1809 {
1810 /*
1811 * Display filler lines at the end of the file
1812 */
1813 if (char2cells(fill_diff) > 1)
1814 i = '-';
1815 else
1816 i = fill_diff;
1817 if (row + j > wp->w_height)
1818 j = wp->w_height - row;
1819 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1820 row += j;
1821 }
1822#endif
1823 }
1824 else if (dollar_vcol == 0)
1825 wp->w_botline = lnum;
1826
1827 /* make sure the rest of the screen is blank */
1828 /* put '~'s on rows that aren't part of the file. */
1829 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1830 }
1831
1832 /* Reset the type of redrawing required, the window has been updated. */
1833 wp->w_redr_type = 0;
1834#ifdef FEAT_DIFF
1835 wp->w_old_topfill = wp->w_topfill;
1836 wp->w_old_botfill = wp->w_botfill;
1837#endif
1838
1839 if (dollar_vcol == 0)
1840 {
1841 /*
1842 * There is a trick with w_botline. If we invalidate it on each
1843 * change that might modify it, this will cause a lot of expensive
1844 * calls to plines() in update_topline() each time. Therefore the
1845 * value of w_botline is often approximated, and this value is used to
1846 * compute the value of w_topline. If the value of w_botline was
1847 * wrong, check that the value of w_topline is correct (cursor is on
1848 * the visible part of the text). If it's not, we need to redraw
1849 * again. Mostly this just means scrolling up a few lines, so it
1850 * doesn't look too bad. Only do this for the current window (where
1851 * changes are relevant).
1852 */
1853 wp->w_valid |= VALID_BOTLINE;
1854 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1855 {
1856 recursive = TRUE;
1857 curwin->w_valid &= ~VALID_TOPLINE;
1858 update_topline(); /* may invalidate w_botline again */
1859 if (must_redraw != 0)
1860 {
1861 /* Don't update for changes in buffer again. */
1862 i = curbuf->b_mod_set;
1863 curbuf->b_mod_set = FALSE;
1864 win_update(curwin);
1865 must_redraw = 0;
1866 curbuf->b_mod_set = i;
1867 }
1868 recursive = FALSE;
1869 }
1870 }
1871
1872#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1873 /* restore got_int, unless CTRL-C was hit while redrawing */
1874 if (!got_int)
1875 got_int = save_got_int;
1876#endif
1877}
1878
1879#ifdef FEAT_SIGNS
1880static int draw_signcolumn __ARGS((win_T *wp));
1881
1882/*
1883 * Return TRUE when window "wp" has a column to draw signs in.
1884 */
1885 static int
1886draw_signcolumn(wp)
1887 win_T *wp;
1888{
1889 return (wp->w_buffer->b_signlist != NULL
1890# ifdef FEAT_NETBEANS_INTG
1891 || usingNetbeans
1892# endif
1893 );
1894}
1895#endif
1896
1897/*
1898 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1899 * as the filler character.
1900 */
1901 static void
1902win_draw_end(wp, c1, c2, row, endrow, hl)
1903 win_T *wp;
1904 int c1;
1905 int c2;
1906 int row;
1907 int endrow;
1908 enum hlf_value hl;
1909{
1910#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1911 int n = 0;
1912# define FDC_OFF n
1913#else
1914# define FDC_OFF 0
1915#endif
1916
1917#ifdef FEAT_RIGHTLEFT
1918 if (wp->w_p_rl)
1919 {
1920 /* No check for cmdline window: should never be right-left. */
1921# ifdef FEAT_FOLDING
1922 n = wp->w_p_fdc;
1923
1924 if (n > 0)
1925 {
1926 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001927 if (n > W_WIDTH(wp))
1928 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1930 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
1931 ' ', ' ', hl_attr(HLF_FC));
1932 }
1933# endif
1934# ifdef FEAT_SIGNS
1935 if (draw_signcolumn(wp))
1936 {
1937 int nn = n + 2;
1938
1939 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001940 if (nn > W_WIDTH(wp))
1941 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1943 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
1944 ' ', ' ', hl_attr(HLF_SC));
1945 n = nn;
1946 }
1947# endif
1948 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1949 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
1950 c2, c2, hl_attr(hl));
1951 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1952 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
1953 c1, c2, hl_attr(hl));
1954 }
1955 else
1956#endif
1957 {
1958#ifdef FEAT_CMDWIN
1959 if (cmdwin_type != 0 && wp == curwin)
1960 {
1961 /* draw the cmdline character in the leftmost column */
1962 n = 1;
1963 if (n > wp->w_width)
1964 n = wp->w_width;
1965 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1966 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
1967 cmdwin_type, ' ', hl_attr(HLF_AT));
1968 }
1969#endif
1970#ifdef FEAT_FOLDING
1971 if (wp->w_p_fdc > 0)
1972 {
1973 int nn = n + wp->w_p_fdc;
1974
1975 /* draw the fold column at the left */
1976 if (nn > W_WIDTH(wp))
1977 nn = W_WIDTH(wp);
1978 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1979 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
1980 ' ', ' ', hl_attr(HLF_FC));
1981 n = nn;
1982 }
1983#endif
1984#ifdef FEAT_SIGNS
1985 if (draw_signcolumn(wp))
1986 {
1987 int nn = n + 2;
1988
1989 /* draw the sign column after the fold column */
1990 if (nn > W_WIDTH(wp))
1991 nn = W_WIDTH(wp);
1992 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1993 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
1994 ' ', ' ', hl_attr(HLF_SC));
1995 n = nn;
1996 }
1997#endif
1998 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1999 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2000 c1, c2, hl_attr(hl));
2001 }
2002 set_empty_rows(wp, row);
2003}
2004
2005#ifdef FEAT_FOLDING
2006/*
2007 * Display one folded line.
2008 */
2009 static void
2010fold_line(wp, fold_count, foldinfo, lnum, row)
2011 win_T *wp;
2012 long fold_count;
2013 foldinfo_T *foldinfo;
2014 linenr_T lnum;
2015 int row;
2016{
2017 char_u buf[51];
2018 pos_T *top, *bot;
2019 linenr_T lnume = lnum + fold_count - 1;
2020 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002021 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 int col;
2024 int txtcol;
2025 int off = (int)(current_ScreenLine - ScreenLines);
2026
2027 /* Build the fold line:
2028 * 1. Add the cmdwin_type for the command-line window
2029 * 2. Add the 'foldcolumn'
2030 * 3. Add the 'number' column
2031 * 4. Compose the text
2032 * 5. Add the text
2033 * 6. set highlighting for the Visual area an other text
2034 */
2035 col = 0;
2036
2037 /*
2038 * 1. Add the cmdwin_type for the command-line window
2039 * Ignores 'rightleft', this window is never right-left.
2040 */
2041#ifdef FEAT_CMDWIN
2042 if (cmdwin_type != 0 && wp == curwin)
2043 {
2044 ScreenLines[off] = cmdwin_type;
2045 ScreenAttrs[off] = hl_attr(HLF_AT);
2046#ifdef FEAT_MBYTE
2047 if (enc_utf8)
2048 ScreenLinesUC[off] = 0;
2049#endif
2050 ++col;
2051 }
2052#endif
2053
2054 /*
2055 * 2. Add the 'foldcolumn'
2056 */
2057 fdc = wp->w_p_fdc;
2058 if (fdc > W_WIDTH(wp) - col)
2059 fdc = W_WIDTH(wp) - col;
2060 if (fdc > 0)
2061 {
2062 fill_foldcolumn(buf, wp, TRUE, lnum);
2063#ifdef FEAT_RIGHTLEFT
2064 if (wp->w_p_rl)
2065 {
2066 int i;
2067
2068 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2069 hl_attr(HLF_FC));
2070 /* reverse the fold column */
2071 for (i = 0; i < fdc; ++i)
2072 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2073 }
2074 else
2075#endif
2076 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2077 col += fdc;
2078 }
2079
2080#ifdef FEAT_RIGHTLEFT
2081# define RL_MEMSET(p, v, l) vim_memset(ScreenAttrs + off + (wp->w_p_rl ? (W_WIDTH(wp) - (p) - (l)) : (p)), v, l)
2082#else
2083# define RL_MEMSET(p, v, l) vim_memset(ScreenAttrs + off + p, v, l)
2084#endif
2085
2086 /* Set all attributes of the 'number' column and the text */
2087 RL_MEMSET(col, hl_attr(HLF_FL), (size_t)(W_WIDTH(wp) - col));
2088
2089#ifdef FEAT_SIGNS
2090 /* If signs are being displayed, add two spaces. */
2091 if (draw_signcolumn(wp))
2092 {
2093 len = W_WIDTH(wp) - col;
2094 if (len > 0)
2095 {
2096 if (len > 2)
2097 len = 2;
2098# ifdef FEAT_RIGHTLEFT
2099 if (wp->w_p_rl)
2100 /* the line number isn't reversed */
2101 copy_text_attr(off + W_WIDTH(wp) - len - col,
2102 (char_u *)" ", len, hl_attr(HLF_FL));
2103 else
2104# endif
2105 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2106 col += len;
2107 }
2108 }
2109#endif
2110
2111 /*
2112 * 3. Add the 'number' column
2113 */
2114 if (wp->w_p_nu)
2115 {
2116 len = W_WIDTH(wp) - col;
2117 if (len > 0)
2118 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002119 int w = number_width(wp);
2120
2121 if (len > w + 1)
2122 len = w + 1;
2123 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#ifdef FEAT_RIGHTLEFT
2125 if (wp->w_p_rl)
2126 /* the line number isn't reversed */
2127 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2128 hl_attr(HLF_FL));
2129 else
2130#endif
2131 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2132 col += len;
2133 }
2134 }
2135
2136 /*
2137 * 4. Compose the folded-line string with 'foldtext', if set.
2138 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002139 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 txtcol = col; /* remember where text starts */
2142
2143 /*
2144 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2145 * Right-left text is put in columns 0 - number-col, normal text is put
2146 * in columns number-col - window-width.
2147 */
2148#ifdef FEAT_MBYTE
2149 if (has_mbyte)
2150 {
2151 int cells;
2152 int u8c, u8c_c1, u8c_c2;
2153 int idx;
2154 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002155 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156# ifdef FEAT_ARABIC
2157 int prev_c = 0; /* previous Arabic character */
2158 int prev_c1 = 0; /* first composing char for prev_c */
2159# endif
2160
2161# ifdef FEAT_RIGHTLEFT
2162 if (wp->w_p_rl)
2163 idx = off;
2164 else
2165# endif
2166 idx = off + col;
2167
2168 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2169 for (p = text; *p != NUL; )
2170 {
2171 cells = (*mb_ptr2cells)(p);
2172 c_len = (*mb_ptr2len_check)(p);
2173 if (col + cells > W_WIDTH(wp)
2174# ifdef FEAT_RIGHTLEFT
2175 - (wp->w_p_rl ? col : 0)
2176# endif
2177 )
2178 break;
2179 ScreenLines[idx] = *p;
2180 if (enc_utf8)
2181 {
2182 u8c = utfc_ptr2char(p, &u8c_c1, &u8c_c2);
2183 if (*p < 0x80 && u8c_c1 == 0 && u8c_c2 == 0)
2184 {
2185 ScreenLinesUC[idx] = 0;
2186#ifdef FEAT_ARABIC
2187 prev_c = u8c;
2188#endif
2189 }
2190 else
2191 {
2192#ifdef FEAT_ARABIC
2193 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2194 {
2195 /* Do Arabic shaping. */
2196 int pc, pc1, nc, dummy;
2197 int firstbyte = *p;
2198
2199 /* The idea of what is the previous and next
2200 * character depends on 'rightleft'. */
2201 if (wp->w_p_rl)
2202 {
2203 pc = prev_c;
2204 pc1 = prev_c1;
2205 nc = utf_ptr2char(p + c_len);
2206 prev_c1 = u8c_c1;
2207 }
2208 else
2209 {
2210 pc = utfc_ptr2char(p + c_len, &pc1, &dummy);
2211 nc = prev_c;
2212 }
2213 prev_c = u8c;
2214
2215 u8c = arabic_shape(u8c, &firstbyte, &u8c_c1,
2216 pc, pc1, nc);
2217 ScreenLines[idx] = firstbyte;
2218 }
2219 else
2220 prev_c = u8c;
2221#endif
2222 /* Non-BMP character: display as ? or fullwidth ?. */
2223 if (u8c >= 0x10000)
2224 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2225 else
2226 ScreenLinesUC[idx] = u8c;
2227 ScreenLinesC1[idx] = u8c_c1;
2228 ScreenLinesC2[idx] = u8c_c2;
2229 }
2230 if (cells > 1)
2231 ScreenLines[idx + 1] = 0;
2232 }
2233 else if (cells > 1) /* double-byte character */
2234 {
2235 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2236 ScreenLines2[idx] = p[1];
2237 else
2238 ScreenLines[idx + 1] = p[1];
2239 }
2240 col += cells;
2241 idx += cells;
2242 p += c_len;
2243 }
2244 }
2245 else
2246#endif
2247 {
2248 len = (int)STRLEN(text);
2249 if (len > W_WIDTH(wp) - col)
2250 len = W_WIDTH(wp) - col;
2251 if (len > 0)
2252 {
2253#ifdef FEAT_RIGHTLEFT
2254 if (wp->w_p_rl)
2255 STRNCPY(current_ScreenLine, text, len);
2256 else
2257#endif
2258 STRNCPY(current_ScreenLine + col, text, len);
2259 col += len;
2260 }
2261 }
2262
2263 /* Fill the rest of the line with the fold filler */
2264#ifdef FEAT_RIGHTLEFT
2265 if (wp->w_p_rl)
2266 col -= txtcol;
2267#endif
2268 while (col < W_WIDTH(wp)
2269#ifdef FEAT_RIGHTLEFT
2270 - (wp->w_p_rl ? txtcol : 0)
2271#endif
2272 )
2273 {
2274#ifdef FEAT_MBYTE
2275 if (enc_utf8)
2276 {
2277 if (fill_fold >= 0x80)
2278 {
2279 ScreenLinesUC[off + col] = fill_fold;
2280 ScreenLinesC1[off + col] = 0;
2281 ScreenLinesC2[off + col] = 0;
2282 }
2283 else
2284 ScreenLinesUC[off + col] = 0;
2285 }
2286#endif
2287 ScreenLines[off + col++] = fill_fold;
2288 }
2289
2290 if (text != buf)
2291 vim_free(text);
2292
2293 /*
2294 * 6. set highlighting for the Visual area an other text.
2295 * If all folded lines are in the Visual area, highlight the line.
2296 */
2297#ifdef FEAT_VISUAL
2298 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2299 {
2300 if (ltoreq(curwin->w_cursor, VIsual))
2301 {
2302 /* Visual is after curwin->w_cursor */
2303 top = &curwin->w_cursor;
2304 bot = &VIsual;
2305 }
2306 else
2307 {
2308 /* Visual is before curwin->w_cursor */
2309 top = &VIsual;
2310 bot = &curwin->w_cursor;
2311 }
2312 if (lnum >= top->lnum
2313 && lnume <= bot->lnum
2314 && (VIsual_mode != 'v'
2315 || ((lnum > top->lnum
2316 || (lnum == top->lnum
2317 && top->col == 0))
2318 && (lnume < bot->lnum
2319 || (lnume == bot->lnum
2320 && (bot->col - (*p_sel == 'e'))
2321 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2322 {
2323 if (VIsual_mode == Ctrl_V)
2324 {
2325 /* Visual block mode: highlight the chars part of the block */
2326 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2327 {
2328 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2329 len = wp->w_old_cursor_lcol;
2330 else
2331 len = W_WIDTH(wp) - txtcol;
2332 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
2333 (size_t)(len - wp->w_old_cursor_fcol));
2334 }
2335 }
2336 else
2337 /* Set all attributes of the text */
2338 RL_MEMSET(txtcol, hl_attr(HLF_V),
2339 (size_t)(W_WIDTH(wp) - txtcol));
2340 }
2341 }
2342#endif
2343
2344
2345 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2346 (int)W_WIDTH(wp), FALSE);
2347
2348 /*
2349 * Update w_cline_height and w_cline_folded if the cursor line was
2350 * updated (saves a call to plines() later).
2351 */
2352 if (wp == curwin
2353 && lnum <= curwin->w_cursor.lnum
2354 && lnume >= curwin->w_cursor.lnum)
2355 {
2356 curwin->w_cline_row = row;
2357 curwin->w_cline_height = 1;
2358 curwin->w_cline_folded = TRUE;
2359 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2360 }
2361}
2362
2363/*
2364 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2365 */
2366 static void
2367copy_text_attr(off, buf, len, attr)
2368 int off;
2369 char_u *buf;
2370 int len;
2371 int attr;
2372{
2373 mch_memmove(ScreenLines + off, buf, (size_t)len);
2374# ifdef FEAT_MBYTE
2375 if (enc_utf8)
2376 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2377# endif
2378 vim_memset(ScreenAttrs + off, attr, (size_t)len);
2379}
2380
2381/*
2382 * Fill the foldcolumn at "p" for window "wp".
2383 */
2384 static void
2385fill_foldcolumn(p, wp, closed, lnum)
2386 char_u *p;
2387 win_T *wp;
2388 int closed; /* TRUE of FALSE */
2389 linenr_T lnum; /* current line number */
2390{
2391 int i = 0;
2392 int level;
2393 int first_level;
2394
2395 /* Init to all spaces. */
2396 copy_spaces(p, (size_t)wp->w_p_fdc);
2397
2398 level = win_foldinfo.fi_level;
2399 if (level > 0)
2400 {
2401 /* If the column is too narrow, we start at the lowest level that
2402 * fits and use numbers to indicated the depth. */
2403 first_level = level - wp->w_p_fdc - closed + 2;
2404 if (first_level < 1)
2405 first_level = 1;
2406
2407 for (i = 0; i + 1 < wp->w_p_fdc; ++i)
2408 {
2409 if (win_foldinfo.fi_lnum == lnum
2410 && first_level + i >= win_foldinfo.fi_low_level)
2411 p[i] = '-';
2412 else if (first_level == 1)
2413 p[i] = '|';
2414 else if (first_level + i <= 9)
2415 p[i] = '0' + first_level + i;
2416 else
2417 p[i] = '>';
2418 if (first_level + i == level)
2419 break;
2420 }
2421 }
2422 if (closed)
2423 p[i] = '+';
2424}
2425#endif /* FEAT_FOLDING */
2426
2427/*
2428 * Display line "lnum" of window 'wp' on the screen.
2429 * Start at row "startrow", stop when "endrow" is reached.
2430 * wp->w_virtcol needs to be valid.
2431 *
2432 * Return the number of last row the line occupies.
2433 */
2434 static int
2435win_line(wp, lnum, startrow, endrow)
2436 win_T *wp;
2437 linenr_T lnum;
2438 int startrow;
2439 int endrow;
2440{
2441 int col; /* visual column on screen */
2442 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2443 int c = 0; /* init for GCC */
2444 long vcol = 0; /* virtual column (for tabs) */
2445 long vcol_prev = -1; /* "vcol" of previous character */
2446 char_u *line; /* current line */
2447 char_u *ptr; /* current position in "line" */
2448 int row; /* row in the window, excl w_winrow */
2449 int screen_row; /* row on the screen, incl w_winrow */
2450
2451 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2452 int n_extra = 0; /* number of extra chars */
2453 char_u *p_extra = NULL; /* string of extra chars */
2454 int c_extra = NUL; /* extra chars, all the same */
2455 int extra_attr = 0; /* attributes when n_extra != 0 */
2456 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2457 displaying lcs_eol at end-of-line */
2458 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2459 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2460
2461 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2462 int saved_n_extra = 0;
2463 char_u *saved_p_extra = NULL;
2464 int saved_c_extra = 0;
2465 int saved_char_attr = 0;
2466
2467 int n_attr = 0; /* chars with special attr */
2468 int saved_attr2 = 0; /* char_attr saved for n_attr */
2469 int n_attr3 = 0; /* chars with overruling special attr */
2470 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2471
2472 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2473
2474 int fromcol, tocol; /* start/end of inverting */
2475 int fromcol_prev = -2; /* start of inverting after cursor */
2476 int noinvcur = FALSE; /* don't invert the cursor */
2477#ifdef FEAT_VISUAL
2478 pos_T *top, *bot;
2479#endif
2480 pos_T pos;
2481 long v;
2482
2483 int char_attr = 0; /* attributes for next character */
2484 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2485 in this line */
2486 int attr = 0; /* attributes for area highlighting */
2487 int area_attr = 0; /* attributes desired by highlighting */
2488 int search_attr = 0; /* attributes desired by 'hlsearch' */
2489#ifdef FEAT_SYN_HL
2490 int syntax_attr = 0; /* attributes desired by syntax */
2491 int has_syntax = FALSE; /* this buffer has syntax highl. */
2492 int save_did_emsg;
2493#endif
2494 int extra_check; /* has syntax or linebreak */
2495#ifdef FEAT_MBYTE
2496 int multi_attr = 0; /* attributes desired by multibyte */
2497 int mb_l = 1; /* multi-byte byte length */
2498 int mb_c = 0; /* decoded multi-byte character */
2499 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2500 int u8c_c1 = 0; /* first composing UTF-8 char */
2501 int u8c_c2 = 0; /* second composing UTF-8 char */
2502#endif
2503#ifdef FEAT_DIFF
2504 int filler_lines; /* nr of filler lines to be drawn */
2505 int filler_todo; /* nr of filler lines still to do + 1 */
2506 enum hlf_value diff_hlf = (enum hlf_value)0; /* type of diff highlighting */
2507 int change_start = MAXCOL; /* first col of changed area */
2508 int change_end = -1; /* last col of changed area */
2509#endif
2510 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2511#ifdef FEAT_LINEBREAK
2512 int need_showbreak = FALSE;
2513#endif
2514#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS))
2515# define LINE_ATTR
2516 int line_attr = 0; /* atrribute for the whole line */
2517#endif
2518#ifdef FEAT_SEARCH_EXTRA
2519 match_T *shl; /* points to search_hl or match_hl */
2520#endif
2521#ifdef FEAT_ARABIC
2522 int prev_c = 0; /* previous Arabic character */
2523 int prev_c1 = 0; /* first composing char for prev_c */
2524#endif
2525
2526 /* draw_state: items that are drawn in sequence: */
2527#define WL_START 0 /* nothing done yet */
2528#ifdef FEAT_CMDWIN
2529# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2530#else
2531# define WL_CMDLINE WL_START
2532#endif
2533#ifdef FEAT_FOLDING
2534# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2535#else
2536# define WL_FOLD WL_CMDLINE
2537#endif
2538#ifdef FEAT_SIGNS
2539# define WL_SIGN WL_FOLD + 1 /* column for signs */
2540#else
2541# define WL_SIGN WL_FOLD /* column for signs */
2542#endif
2543#define WL_NR WL_SIGN + 1 /* line number */
2544#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2545# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2546#else
2547# define WL_SBR WL_NR
2548#endif
2549#define WL_LINE WL_SBR + 1 /* text in the line */
2550 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar843ee412004-06-30 16:16:41 +00002551#if defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 int feedback_col = 0;
2553 int feedback_old_attr = -1;
2554#endif
2555
2556
2557 if (startrow > endrow) /* past the end already! */
2558 return startrow;
2559
2560 row = startrow;
2561 screen_row = row + W_WINROW(wp);
2562
2563 /*
2564 * To speed up the loop below, set extra_check when there is linebreak,
2565 * trailing white space and/or syntax processing to be done.
2566 */
2567#ifdef FEAT_LINEBREAK
2568 extra_check = wp->w_p_lbr;
2569#else
2570 extra_check = 0;
2571#endif
2572#ifdef FEAT_SYN_HL
2573 if (syntax_present(wp->w_buffer))
2574 {
2575 /* Prepare for syntax highlighting in this line. When there is an
2576 * error, stop syntax highlighting. */
2577 save_did_emsg = did_emsg;
2578 did_emsg = FALSE;
2579 syntax_start(wp, lnum);
2580 if (did_emsg)
2581 syntax_clear(wp->w_buffer);
2582 else
2583 {
2584 did_emsg = save_did_emsg;
2585 has_syntax = TRUE;
2586 extra_check = TRUE;
2587 }
2588 }
2589#endif
2590
2591 /*
2592 * handle visual active in this window
2593 */
2594 fromcol = -10;
2595 tocol = MAXCOL;
2596#ifdef FEAT_VISUAL
2597 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2598 {
2599 /* Visual is after curwin->w_cursor */
2600 if (ltoreq(curwin->w_cursor, VIsual))
2601 {
2602 top = &curwin->w_cursor;
2603 bot = &VIsual;
2604 }
2605 else /* Visual is before curwin->w_cursor */
2606 {
2607 top = &VIsual;
2608 bot = &curwin->w_cursor;
2609 }
2610 if (VIsual_mode == Ctrl_V) /* block mode */
2611 {
2612 if (lnum >= top->lnum && lnum <= bot->lnum)
2613 {
2614 fromcol = wp->w_old_cursor_fcol;
2615 tocol = wp->w_old_cursor_lcol;
2616 }
2617 }
2618 else /* non-block mode */
2619 {
2620 if (lnum > top->lnum && lnum <= bot->lnum)
2621 fromcol = 0;
2622 else if (lnum == top->lnum)
2623 {
2624 if (VIsual_mode == 'V') /* linewise */
2625 fromcol = 0;
2626 else
2627 {
2628 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2629 if (gchar_pos(top) == NUL)
2630 tocol = fromcol + 1;
2631 }
2632 }
2633 if (VIsual_mode != 'V' && lnum == bot->lnum)
2634 {
2635 if (*p_sel == 'e' && bot->col == 0
2636#ifdef FEAT_VIRTUALEDIT
2637 && bot->coladd == 0
2638#endif
2639 )
2640 {
2641 fromcol = -10;
2642 tocol = MAXCOL;
2643 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002644 else if (bot->col == MAXCOL)
2645 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 else
2647 {
2648 pos = *bot;
2649 if (*p_sel == 'e')
2650 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2651 else
2652 {
2653 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2654 ++tocol;
2655 }
2656 }
2657 }
2658 }
2659
2660#ifndef MSDOS
2661 /* Check if the character under the cursor should not be inverted */
2662 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2663# ifdef FEAT_GUI
2664 && !gui.in_use
2665# endif
2666 )
2667 noinvcur = TRUE;
2668#endif
2669
2670 /* if inverting in this line set area_highlighting */
2671 if (fromcol >= 0)
2672 {
2673 area_highlighting = TRUE;
2674 attr = hl_attr(HLF_V);
2675#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2676 if (clip_star.available && !clip_star.owned && clip_isautosel())
2677 attr = hl_attr(HLF_VNC);
2678#endif
2679 }
2680 }
2681
2682 /*
2683 * handle 'insearch' and ":s///c" highlighting
2684 */
2685 else
2686#endif /* FEAT_VISUAL */
2687 if (highlight_match
2688 && wp == curwin
2689 && lnum >= curwin->w_cursor.lnum
2690 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2691 {
2692 if (lnum == curwin->w_cursor.lnum)
2693 getvcol(curwin, &(curwin->w_cursor),
2694 (colnr_T *)&fromcol, NULL, NULL);
2695 else
2696 fromcol = 0;
2697 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2698 {
2699 pos.lnum = lnum;
2700 pos.col = search_match_endcol;
2701 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2702 }
2703 else
2704 tocol = MAXCOL;
2705 if (fromcol == tocol) /* do at least one character */
2706 tocol = fromcol + 1; /* happens when past end of line */
2707 area_highlighting = TRUE;
2708 attr = hl_attr(HLF_I);
2709 }
2710
2711#ifdef FEAT_DIFF
2712 filler_lines = diff_check(wp, lnum);
2713 if (filler_lines < 0)
2714 {
2715 if (filler_lines == -1)
2716 {
2717 if (diff_find_change(wp, lnum, &change_start, &change_end))
2718 diff_hlf = HLF_ADD; /* added line */
2719 else if (change_start == 0)
2720 diff_hlf = HLF_TXD; /* changed text */
2721 else
2722 diff_hlf = HLF_CHD; /* changed line */
2723 }
2724 else
2725 diff_hlf = HLF_ADD; /* added line */
2726 filler_lines = 0;
2727 area_highlighting = TRUE;
2728 }
2729 if (lnum == wp->w_topline)
2730 filler_lines = wp->w_topfill;
2731 filler_todo = filler_lines;
2732#endif
2733
2734#ifdef LINE_ATTR
2735# ifdef FEAT_SIGNS
2736 /* If this line has a sign with line highlighting set line_attr. */
2737 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2738 if (v != 0)
2739 line_attr = sign_get_attr((int)v, TRUE);
2740# endif
2741# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2742 /* Highlight the current line in the quickfix window. */
2743 if (bt_quickfix(wp->w_buffer) && qf_current_entry() == lnum)
2744 line_attr = hl_attr(HLF_L);
2745# endif
2746 if (line_attr != 0)
2747 area_highlighting = TRUE;
2748#endif
2749
2750 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2751 ptr = line;
2752
2753 /* find start of trailing whitespace */
2754 if (wp->w_p_list && lcs_trail)
2755 {
2756 trailcol = (colnr_T)STRLEN(ptr);
2757 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2758 --trailcol;
2759 trailcol += (colnr_T) (ptr - line);
2760 extra_check = TRUE;
2761 }
2762
2763 /*
2764 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2765 * first character to be displayed.
2766 */
2767 if (wp->w_p_wrap)
2768 v = wp->w_skipcol;
2769 else
2770 v = wp->w_leftcol;
2771 if (v > 0)
2772 {
2773#ifdef FEAT_MBYTE
2774 char_u *prev_ptr = ptr;
2775#endif
2776 while (vcol < v && *ptr != NUL)
2777 {
2778 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2779 vcol += c;
2780#ifdef FEAT_MBYTE
2781 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002783 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785
2786#ifdef FEAT_VIRTUALEDIT
2787 /* When 'virtualedit' is set the end of the line may be before the
2788 * start of the displayed part. */
2789 if (vcol < v && *ptr == NUL && virtual_active())
2790 vcol = v;
2791#endif
2792
2793 /* Handle a character that's not completely on the screen: Put ptr at
2794 * that character but skip the first few screen characters. */
2795 if (vcol > v)
2796 {
2797 vcol -= c;
2798#ifdef FEAT_MBYTE
2799 ptr = prev_ptr;
2800#else
2801 --ptr;
2802#endif
2803 n_skip = v - vcol;
2804 }
2805
2806 /*
2807 * Adjust for when the inverted text is before the screen,
2808 * and when the start of the inverted text is before the screen.
2809 */
2810 if (tocol <= vcol)
2811 fromcol = 0;
2812 else if (fromcol >= 0 && fromcol < vcol)
2813 fromcol = vcol;
2814
2815#ifdef FEAT_LINEBREAK
2816 /* When w_skipcol is non-zero, first line needs 'showbreak' */
2817 if (wp->w_p_wrap)
2818 need_showbreak = TRUE;
2819#endif
2820 }
2821
2822 /*
2823 * Correct highlighting for cursor that can't be disabled.
2824 * Avoids having to check this for each character.
2825 */
2826 if (fromcol >= 0)
2827 {
2828 if (noinvcur)
2829 {
2830 if ((colnr_T)fromcol == wp->w_virtcol)
2831 {
2832 /* highlighting starts at cursor, let it start just after the
2833 * cursor */
2834 fromcol_prev = fromcol;
2835 fromcol = -1;
2836 }
2837 else if ((colnr_T)fromcol < wp->w_virtcol)
2838 /* restart highlighting after the cursor */
2839 fromcol_prev = wp->w_virtcol;
2840 }
2841 if (fromcol >= tocol)
2842 fromcol = -1;
2843 }
2844
2845#ifdef FEAT_SEARCH_EXTRA
2846 /*
2847 * Handle highlighting the last used search pattern.
2848 * Do this for both search_hl and match_hl.
2849 */
2850 shl = &search_hl;
2851 for (;;)
2852 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002853 shl->startcol = MAXCOL;
2854 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 shl->attr_cur = 0;
2856 if (shl->rm.regprog != NULL)
2857 {
2858 v = (long)(ptr - line);
2859 next_search_hl(wp, shl, lnum, (colnr_T)v);
2860
2861 /* Need to get the line again, a multi-line regexp may have made it
2862 * invalid. */
2863 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2864 ptr = line + v;
2865
2866 if (shl->lnum != 0 && shl->lnum <= lnum)
2867 {
2868 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002869 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002871 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
2873 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002874 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002876 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002878 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
2880#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002881 if (has_mbyte && line[shl->endcol] != NUL)
2882 shl->endcol += (*mb_ptr2len_check)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 else
2884#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002885 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002887 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
2889 shl->attr_cur = shl->attr;
2890 search_attr = shl->attr;
2891 }
2892 area_highlighting = TRUE;
2893 }
2894 }
2895 if (shl == &match_hl)
2896 break;
2897 shl = &match_hl;
2898 }
2899#endif
2900
2901 off = (unsigned) (current_ScreenLine - ScreenLines);
2902 col = 0;
2903#ifdef FEAT_RIGHTLEFT
2904 if (wp->w_p_rl)
2905 {
2906 /* Rightleft window: process the text in the normal direction, but put
2907 * it in current_ScreenLine[] from right to left. Start at the
2908 * rightmost column of the window. */
2909 col = W_WIDTH(wp) - 1;
2910 off += col;
2911 }
2912#endif
2913
2914 /*
2915 * Repeat for the whole displayed line.
2916 */
2917 for (;;)
2918 {
2919 /* Skip this quickly when working on the text. */
2920 if (draw_state != WL_LINE)
2921 {
2922#ifdef FEAT_CMDWIN
2923 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
2924 {
2925 draw_state = WL_CMDLINE;
2926 if (cmdwin_type != 0 && wp == curwin)
2927 {
2928 /* Draw the cmdline character. */
2929 *extra = cmdwin_type;
2930 n_extra = 1;
2931 p_extra = extra;
2932 c_extra = NUL;
2933 char_attr = hl_attr(HLF_AT);
2934 }
2935 }
2936#endif
2937
2938#ifdef FEAT_FOLDING
2939 if (draw_state == WL_FOLD - 1 && n_extra == 0)
2940 {
2941 draw_state = WL_FOLD;
2942 if (wp->w_p_fdc > 0)
2943 {
2944 /* Draw the 'foldcolumn'. */
2945 fill_foldcolumn(extra, wp, FALSE, lnum);
2946 n_extra = wp->w_p_fdc;
2947 p_extra = extra;
2948 c_extra = NUL;
2949 char_attr = hl_attr(HLF_FC);
2950 }
2951 }
2952#endif
2953
2954#ifdef FEAT_SIGNS
2955 if (draw_state == WL_SIGN - 1 && n_extra == 0)
2956 {
2957 draw_state = WL_SIGN;
2958 /* Show the sign column when there are any signs in this
2959 * buffer or when using Netbeans. */
2960 if (draw_signcolumn(wp)
2961# ifdef FEAT_DIFF
2962 && filler_todo <= 0
2963# endif
2964 )
2965 {
2966 int_u text_sign;
2967# ifdef FEAT_SIGN_ICONS
2968 int_u icon_sign;
2969# endif
2970
2971 /* Draw two cells with the sign value or blank. */
2972 c_extra = ' ';
2973 char_attr = hl_attr(HLF_SC);
2974 n_extra = 2;
2975
2976 if (row == startrow)
2977 {
2978 text_sign = buf_getsigntype(wp->w_buffer, lnum,
2979 SIGN_TEXT);
2980# ifdef FEAT_SIGN_ICONS
2981 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
2982 SIGN_ICON);
2983 if (gui.in_use && icon_sign != 0)
2984 {
2985 /* Use the image in this position. */
2986 c_extra = SIGN_BYTE;
2987# ifdef FEAT_NETBEANS_INTG
2988 if (buf_signcount(wp->w_buffer, lnum) > 1)
2989 c_extra = MULTISIGN_BYTE;
2990# endif
2991 char_attr = icon_sign;
2992 }
2993 else
2994# endif
2995 if (text_sign != 0)
2996 {
2997 p_extra = sign_get_text(text_sign);
2998 if (p_extra != NULL)
2999 {
3000 c_extra = NUL;
3001 n_extra = STRLEN(p_extra);
3002 }
3003 char_attr = sign_get_attr(text_sign, FALSE);
3004 }
3005 }
3006 }
3007 }
3008#endif
3009
3010 if (draw_state == WL_NR - 1 && n_extra == 0)
3011 {
3012 draw_state = WL_NR;
3013 /* Display the line number. After the first fill with blanks
3014 * when the 'n' flag isn't in 'cpo' */
3015 if (wp->w_p_nu
3016 && (row == startrow
3017#ifdef FEAT_DIFF
3018 + filler_lines
3019#endif
3020 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3021 {
3022 /* Draw the line number (empty space after wrapping). */
3023 if (row == startrow
3024#ifdef FEAT_DIFF
3025 + filler_lines
3026#endif
3027 )
3028 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003029 sprintf((char *)extra, "%*ld ",
3030 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (wp->w_skipcol > 0)
3032 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3033 *p_extra = '-';
3034#ifdef FEAT_RIGHTLEFT
3035 if (wp->w_p_rl) /* reverse line numbers */
3036 rl_mirror(extra);
3037#endif
3038 p_extra = extra;
3039 c_extra = NUL;
3040 }
3041 else
3042 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003043 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 char_attr = hl_attr(HLF_N);
3045 }
3046 }
3047
3048#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3049 if (draw_state == WL_SBR - 1 && n_extra == 0)
3050 {
3051 draw_state = WL_SBR;
3052# ifdef FEAT_DIFF
3053 if (filler_todo > 0)
3054 {
3055 /* Draw "deleted" diff line(s). */
3056 if (char2cells(fill_diff) > 1)
3057 c_extra = '-';
3058 else
3059 c_extra = fill_diff;
3060# ifdef FEAT_RIGHTLEFT
3061 if (wp->w_p_rl)
3062 n_extra = col + 1;
3063 else
3064# endif
3065 n_extra = W_WIDTH(wp) - col;
3066 char_attr = hl_attr(HLF_DED);
3067 }
3068# endif
3069# ifdef FEAT_LINEBREAK
3070 if (*p_sbr != NUL && need_showbreak)
3071 {
3072 /* Draw 'showbreak' at the start of each broken line. */
3073 p_extra = p_sbr;
3074 c_extra = NUL;
3075 n_extra = (int)STRLEN(p_sbr);
3076 char_attr = hl_attr(HLF_AT);
3077 need_showbreak = FALSE;
3078 /* Correct end of highlighted area for 'showbreak',
3079 * required when 'linebreak' is also set. */
3080 if (tocol == vcol)
3081 tocol += n_extra;
3082 }
3083# endif
3084 }
3085#endif
3086
3087 if (draw_state == WL_LINE - 1 && n_extra == 0)
3088 {
3089 draw_state = WL_LINE;
3090 if (saved_n_extra)
3091 {
3092 /* Continue item from end of wrapped line. */
3093 n_extra = saved_n_extra;
3094 c_extra = saved_c_extra;
3095 p_extra = saved_p_extra;
3096 char_attr = saved_char_attr;
3097 }
3098 else
3099 char_attr = 0;
3100 }
3101 }
3102
3103 /* When still displaying '$' of change command, stop at cursor */
3104 if (dollar_vcol != 0 && wp == curwin && vcol >= (long)wp->w_virtcol
3105#ifdef FEAT_DIFF
3106 && filler_todo <= 0
3107#endif
3108 )
3109 {
3110 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3111 wp->w_p_rl);
3112 /* Pretend we have finished updating the window. */
3113 row = wp->w_height;
3114 break;
3115 }
3116
3117 if (draw_state == WL_LINE && area_highlighting)
3118 {
3119 /* handle Visual or match highlighting in this line */
3120 if (vcol == fromcol
3121#ifdef FEAT_MBYTE
3122 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3123 && (*mb_ptr2cells)(ptr) > 1)
3124#endif
3125 || ((int)vcol_prev == fromcol_prev
3126 && vcol < tocol))
3127 area_attr = attr; /* start highlighting */
3128 else if (area_attr != 0
3129 && (vcol == tocol
3130 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3131#ifdef LINE_ATTR
3132 area_attr = line_attr; /* stop highlighting */
3133 else if (line_attr && ((fromcol == -10 && tocol == MAXCOL)
3134 || (vcol < fromcol || vcol > tocol)))
3135 area_attr = line_attr;
3136#else
3137 area_attr = 0; /* stop highlighting */
3138#endif
3139
3140#ifdef FEAT_SEARCH_EXTRA
3141 if (!n_extra)
3142 {
3143 /*
3144 * Check for start/end of search pattern match.
3145 * After end, check for start/end of next match.
3146 * When another match, have to check for start again.
3147 * Watch out for matching an empty string!
3148 * Do this first for search_hl, then for match_hl, so that
3149 * ":match" overrules 'hlsearch'.
3150 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003151 v = (long)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 shl = &search_hl;
3153 for (;;)
3154 {
3155 while (shl->rm.regprog != NULL)
3156 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003157 if (shl->startcol != MAXCOL
3158 && v >= (long)shl->startcol
3159 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
3161 shl->attr_cur = shl->attr;
3162 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003163 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
3165 shl->attr_cur = 0;
3166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 next_search_hl(wp, shl, lnum, (colnr_T)v);
3168
3169 /* Need to get the line again, a multi-line regexp
3170 * may have made it invalid. */
3171 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3172 ptr = line + v;
3173
3174 if (shl->lnum == lnum)
3175 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003176 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003178 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003180 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003182 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 /* highlight empty match, try again after
3185 * it */
3186#ifdef FEAT_MBYTE
3187 if (has_mbyte)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003188 shl->endcol += (*mb_ptr2len_check)(line
3189 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else
3191#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003192 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
3194
3195 /* Loop to check if the match starts at the
3196 * current position */
3197 continue;
3198 }
3199 }
3200 break;
3201 }
3202 if (shl == &match_hl)
3203 break;
3204 shl = &match_hl;
3205 }
3206 /* ":match" highlighting overrules 'hlsearch' */
3207 if (match_hl.attr_cur != 0)
3208 search_attr = match_hl.attr_cur;
3209 else
3210 search_attr = search_hl.attr_cur;
3211 }
3212#endif
3213
3214 if (area_attr != 0)
3215 char_attr = area_attr;
3216#ifdef FEAT_SYN_HL
3217 else if (search_attr == 0 && has_syntax)
3218 char_attr = syntax_attr;
3219#endif
3220 else
3221 char_attr = search_attr;
3222
3223#ifdef FEAT_DIFF
3224 if (diff_hlf != (enum hlf_value)0 && n_extra == 0)
3225 {
3226 if (diff_hlf == HLF_CHD && ptr - line >= change_start)
3227 diff_hlf = HLF_TXD; /* changed text */
3228 if (diff_hlf == HLF_TXD && ptr - line > change_end)
3229 diff_hlf = HLF_CHD; /* changed line */
3230 if (attr == 0 || area_attr != attr)
3231 area_attr = hl_attr(diff_hlf);
3232 if (attr == 0 || char_attr != attr)
3233 {
3234 if (search_attr != 0)
3235 char_attr = search_attr;
3236 else
3237 char_attr = hl_attr(diff_hlf);
3238 }
3239 }
3240#endif
3241 }
3242
3243 /*
3244 * Get the next character to put on the screen.
3245 */
3246 /*
3247 * The 'extra' array contains the extra stuff that is inserted to
3248 * represent special characters (non-printable stuff). When all
3249 * characters are the same, c_extra is used.
3250 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3251 */
3252 if (n_extra > 0)
3253 {
3254 if (c_extra != NUL)
3255 {
3256 c = c_extra;
3257#ifdef FEAT_MBYTE
3258 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3259 if (enc_utf8 && (*mb_char2len)(c) > 1)
3260 {
3261 mb_utf8 = TRUE;
3262 u8c_c1 = u8c_c2 = 0;
3263 }
3264 else
3265 mb_utf8 = FALSE;
3266#endif
3267 }
3268 else
3269 {
3270 c = *p_extra;
3271#ifdef FEAT_MBYTE
3272 if (has_mbyte)
3273 {
3274 mb_c = c;
3275 if (enc_utf8)
3276 {
3277 /* If the UTF-8 character is more than one byte:
3278 * Decode it into "mb_c". */
3279 mb_l = (*mb_ptr2len_check)(p_extra);
3280 mb_utf8 = FALSE;
3281 if (mb_l > n_extra)
3282 mb_l = 1;
3283 else if (mb_l > 1)
3284 {
3285 mb_c = utfc_ptr2char(p_extra, &u8c_c1, &u8c_c2);
3286 mb_utf8 = TRUE;
3287 }
3288 }
3289 else
3290 {
3291 /* if this is a DBCS character, put it in "mb_c" */
3292 mb_l = MB_BYTE2LEN(c);
3293 if (mb_l >= n_extra)
3294 mb_l = 1;
3295 else if (mb_l > 1)
3296 mb_c = (c << 8) + p_extra[1];
3297 }
3298 /* If a double-width char doesn't fit display a '>' in the
3299 * last column. */
3300 if (
3301# ifdef FEAT_RIGHTLEFT
3302 wp->w_p_rl ? (col <= 0) :
3303# endif
3304 (col >= W_WIDTH(wp) - 1)
3305 && (*mb_char2cells)(mb_c) == 2)
3306 {
3307 c = '>';
3308 mb_c = c;
3309 mb_l = 1;
3310 mb_utf8 = FALSE;
3311 multi_attr = hl_attr(HLF_AT);
3312 /* put the pointer back to output the double-width
3313 * character at the start of the next line. */
3314 ++n_extra;
3315 --p_extra;
3316 }
3317 else
3318 {
3319 n_extra -= mb_l - 1;
3320 p_extra += mb_l - 1;
3321 }
3322 }
3323#endif
3324 ++p_extra;
3325 }
3326 --n_extra;
3327 }
3328 else
3329 {
3330 /*
3331 * Get a character from the line itself.
3332 */
3333 c = *ptr;
3334#ifdef FEAT_MBYTE
3335 if (has_mbyte)
3336 {
3337 mb_c = c;
3338 if (enc_utf8)
3339 {
3340 /* If the UTF-8 character is more than one byte: Decode it
3341 * into "mb_c". */
3342 mb_l = (*mb_ptr2len_check)(ptr);
3343 mb_utf8 = FALSE;
3344 if (mb_l > 1)
3345 {
3346 mb_c = utfc_ptr2char(ptr, &u8c_c1, &u8c_c2);
3347 /* Overlong encoded ASCII or ASCII with composing char
3348 * is displayed normally, except a NUL. */
3349 if (mb_c < 0x80)
3350 c = mb_c;
3351 mb_utf8 = TRUE;
3352 }
3353
3354 if ((mb_l == 1 && c >= 0x80)
3355 || (mb_l >= 1 && mb_c == 0)
3356 || (mb_l > 1 && (!vim_isprintc(mb_c)
3357 || mb_c >= 0x10000)))
3358 {
3359 /*
3360 * Illegal UTF-8 byte: display as <xx>.
3361 * Non-BMP character : display as ? or fullwidth ?.
3362 */
3363 if (mb_c < 0x10000)
3364 {
3365 transchar_hex(extra, mb_c);
3366#ifdef FEAT_RIGHTLEFT
3367 if (wp->w_p_rl) /* reverse */
3368 rl_mirror(extra);
3369#endif
3370 }
3371 else if (utf_char2cells(mb_c) != 2)
3372 STRCPY(extra, "?");
3373 else
3374 /* 0xff1f in UTF-8: full-width '?' */
3375 STRCPY(extra, "\357\274\237");
3376
3377 p_extra = extra;
3378 c = *p_extra;
3379 mb_c = mb_ptr2char_adv(&p_extra);
3380 mb_utf8 = (c >= 0x80);
3381 n_extra = (int)STRLEN(p_extra);
3382 c_extra = NUL;
3383 if (area_attr == 0 && search_attr == 0)
3384 {
3385 n_attr = n_extra + 1;
3386 extra_attr = hl_attr(HLF_8);
3387 saved_attr2 = char_attr; /* save current attr */
3388 }
3389 }
3390 else if (mb_l == 0) /* at the NUL at end-of-line */
3391 mb_l = 1;
3392#ifdef FEAT_ARABIC
3393 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3394 {
3395 /* Do Arabic shaping. */
3396 int pc, pc1, nc, dummy;
3397
3398 /* The idea of what is the previous and next
3399 * character depends on 'rightleft'. */
3400 if (wp->w_p_rl)
3401 {
3402 pc = prev_c;
3403 pc1 = prev_c1;
3404 nc = utf_ptr2char(ptr + mb_l);
3405 prev_c1 = u8c_c1;
3406 }
3407 else
3408 {
3409 pc = utfc_ptr2char(ptr + mb_l, &pc1, &dummy);
3410 nc = prev_c;
3411 }
3412 prev_c = mb_c;
3413
3414 mb_c = arabic_shape(mb_c, &c, &u8c_c1, pc, pc1, nc);
3415 }
3416 else
3417 prev_c = mb_c;
3418#endif
3419 }
3420 else /* enc_dbcs */
3421 {
3422 mb_l = MB_BYTE2LEN(c);
3423 if (mb_l == 0) /* at the NUL at end-of-line */
3424 mb_l = 1;
3425 else if (mb_l > 1)
3426 {
3427 /* We assume a second byte below 32 is illegal.
3428 * Hopefully this is OK for all double-byte encodings!
3429 */
3430 if (ptr[1] >= 32)
3431 mb_c = (c << 8) + ptr[1];
3432 else
3433 {
3434 if (ptr[1] == NUL)
3435 {
3436 /* head byte at end of line */
3437 mb_l = 1;
3438 transchar_nonprint(extra, c);
3439 }
3440 else
3441 {
3442 /* illegal tail byte */
3443 mb_l = 2;
3444 STRCPY(extra, "XX");
3445 }
3446 p_extra = extra;
3447 n_extra = (int)STRLEN(extra) - 1;
3448 c_extra = NUL;
3449 c = *p_extra++;
3450 if (area_attr == 0 && search_attr == 0)
3451 {
3452 n_attr = n_extra + 1;
3453 extra_attr = hl_attr(HLF_8);
3454 saved_attr2 = char_attr; /* save current attr */
3455 }
3456 mb_c = c;
3457 }
3458 }
3459 }
3460 /* If a double-width char doesn't fit display a '>' in the
3461 * last column; the character is displayed at the start of the
3462 * next line. */
3463 if ((
3464# ifdef FEAT_RIGHTLEFT
3465 wp->w_p_rl ? (col <= 0) :
3466# endif
3467 (col >= W_WIDTH(wp) - 1))
3468 && (*mb_char2cells)(mb_c) == 2)
3469 {
3470 c = '>';
3471 mb_c = c;
3472 mb_utf8 = FALSE;
3473 mb_l = 1;
3474 multi_attr = hl_attr(HLF_AT);
3475 /* Put pointer back so that the character will be
3476 * displayed at the start of the next line. */
3477 --ptr;
3478 }
3479 else if (*ptr != NUL)
3480 ptr += mb_l - 1;
3481
3482 /* If a double-width char doesn't fit at the left side display
3483 * a '<' in the first column. */
3484 if (n_skip > 0 && mb_l > 1)
3485 {
3486 extra[0] = '<';
3487 p_extra = extra;
3488 n_extra = 1;
3489 c_extra = NUL;
3490 c = ' ';
3491 if (area_attr == 0 && search_attr == 0)
3492 {
3493 n_attr = n_extra + 1;
3494 extra_attr = hl_attr(HLF_AT);
3495 saved_attr2 = char_attr; /* save current attr */
3496 }
3497 mb_c = c;
3498 mb_utf8 = FALSE;
3499 mb_l = 1;
3500 }
3501
3502 }
3503#endif
3504 ++ptr;
3505
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003506 /* 'list' : change char 160 to lcs_nbsp. */
3507 if (wp->w_p_list && c == 160 && lcs_nbsp)
3508 {
3509 c = lcs_nbsp;
3510 if (area_attr == 0 && search_attr == 0)
3511 {
3512 n_attr = 1;
3513 extra_attr = hl_attr(HLF_8);
3514 saved_attr2 = char_attr; /* save current attr */
3515 }
3516#ifdef FEAT_MBYTE
3517 mb_c = c;
3518 if (enc_utf8 && (*mb_char2len)(c) > 1)
3519 {
3520 mb_utf8 = TRUE;
3521 u8c_c1 = u8c_c2 = 0;
3522 }
3523 else
3524 mb_utf8 = FALSE;
3525#endif
3526 }
3527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (extra_check)
3529 {
3530#ifdef FEAT_SYN_HL
3531 /* Get syntax attribute, unless still at the start of the line
3532 * (double-wide char that doesn't fit). */
3533 if (has_syntax && (v = (long)(ptr - line)) > 0)
3534 {
3535 /* Get the syntax attribute for the character. If there
3536 * is an error, disable syntax highlighting. */
3537 save_did_emsg = did_emsg;
3538 did_emsg = FALSE;
3539
3540 syntax_attr = get_syntax_attr((colnr_T)v - 1);
3541
3542 if (did_emsg)
3543 syntax_clear(wp->w_buffer);
3544 else
3545 did_emsg = save_did_emsg;
3546
3547 /* Need to get the line again, a multi-line regexp may
3548 * have made it invalid. */
3549 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3550 ptr = line + v;
3551
3552 if (area_attr == 0 && search_attr == 0)
3553 char_attr = syntax_attr;
3554 }
3555#endif
3556#ifdef FEAT_LINEBREAK
3557 /*
3558 * Found last space before word: check for line break
3559 */
3560 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
3561 && !wp->w_p_list)
3562 {
3563 n_extra = win_lbr_chartabsize(wp, ptr - (
3564# ifdef FEAT_MBYTE
3565 has_mbyte ? mb_l :
3566# endif
3567 1), (colnr_T)vcol, NULL) - 1;
3568 c_extra = ' ';
3569 if (vim_iswhite(c))
3570 c = ' ';
3571 }
3572#endif
3573
3574 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3575 {
3576 c = lcs_trail;
3577 if (area_attr == 0 && search_attr == 0)
3578 {
3579 n_attr = 1;
3580 extra_attr = hl_attr(HLF_8);
3581 saved_attr2 = char_attr; /* save current attr */
3582 }
3583#ifdef FEAT_MBYTE
3584 mb_c = c;
3585 if (enc_utf8 && (*mb_char2len)(c) > 1)
3586 {
3587 mb_utf8 = TRUE;
3588 u8c_c1 = u8c_c2 = 0;
3589 }
3590 else
3591 mb_utf8 = FALSE;
3592#endif
3593 }
3594 }
3595
3596 /*
3597 * Handling of non-printable characters.
3598 */
3599 if (!(chartab[c] & CT_PRINT_CHAR))
3600 {
3601 /*
3602 * when getting a character from the file, we may have to
3603 * turn it into something else on the way to putting it
3604 * into "ScreenLines".
3605 */
3606 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3607 {
3608 /* tab amount depends on current column */
3609 n_extra = (int)wp->w_buffer->b_p_ts
3610 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3611#ifdef FEAT_MBYTE
3612 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3613#endif
3614 if (wp->w_p_list)
3615 {
3616 c = lcs_tab1;
3617 c_extra = lcs_tab2;
3618 n_attr = n_extra + 1;
3619 extra_attr = hl_attr(HLF_8);
3620 saved_attr2 = char_attr; /* save current attr */
3621#ifdef FEAT_MBYTE
3622 mb_c = c;
3623 if (enc_utf8 && (*mb_char2len)(c) > 1)
3624 {
3625 mb_utf8 = TRUE;
3626 u8c_c1 = u8c_c2 = 0;
3627 }
3628#endif
3629 }
3630 else
3631 {
3632 c_extra = ' ';
3633 c = ' ';
3634 }
3635 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003636 else if (c == NUL
3637 && ((wp->w_p_list && lcs_eol > 0)
3638 || ((fromcol >= 0 || fromcol_prev >= 0)
3639 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003640#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003641 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003642#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003643 && (
3644# ifdef FEAT_RIGHTLEFT
3645 wp->w_p_rl ? (col >= 0) :
3646# endif
3647 (col < W_WIDTH(wp)))
3648 && !(noinvcur
3649 && (colnr_T)vcol == wp->w_virtcol)))
3650 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003652 /* Display a '$' after the line or highlight an extra
3653 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654#if defined(FEAT_DIFF) || defined(LINE_ATTR)
3655 /* For a diff line the highlighting continues after the
3656 * "$". */
3657 if (
3658# ifdef FEAT_DIFF
3659 diff_hlf == (enum hlf_value)0
3660# ifdef LINE_ATTR
3661 &&
3662# endif
3663# endif
3664# ifdef LINE_ATTR
3665 line_attr == 0
3666# endif
3667 )
3668#endif
3669 {
3670#ifdef FEAT_VIRTUALEDIT
3671 /* In virtualedit, visual selections may extend
3672 * beyond end of line. */
3673 if (area_highlighting && virtual_active()
3674 && tocol != MAXCOL && vcol < tocol)
3675 n_extra = 0;
3676 else
3677#endif
3678 {
3679 p_extra = at_end_str;
3680 n_extra = 1;
3681 c_extra = NUL;
3682 }
3683 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003684 if (wp->w_p_list)
3685 c = lcs_eol;
3686 else
3687 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 lcs_eol_one = -1;
3689 --ptr; /* put it back at the NUL */
3690 if (area_attr == 0 && search_attr == 0)
3691 {
3692 extra_attr = hl_attr(HLF_AT);
3693 n_attr = 1;
3694 }
3695#ifdef FEAT_MBYTE
3696 mb_c = c;
3697 if (enc_utf8 && (*mb_char2len)(c) > 1)
3698 {
3699 mb_utf8 = TRUE;
3700 u8c_c1 = u8c_c2 = 0;
3701 }
3702 else
3703 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3704#endif
3705 }
3706 else if (c != NUL)
3707 {
3708 p_extra = transchar(c);
3709#ifdef FEAT_RIGHTLEFT
3710 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
3711 rl_mirror(p_extra); /* reverse "<12>" */
3712#endif
3713 n_extra = byte2cells(c) - 1;
3714 c_extra = NUL;
3715 c = *p_extra++;
3716 if (area_attr == 0 && search_attr == 0)
3717 {
3718 n_attr = n_extra + 1;
3719 extra_attr = hl_attr(HLF_8);
3720 saved_attr2 = char_attr; /* save current attr */
3721 }
3722#ifdef FEAT_MBYTE
3723 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3724#endif
3725 }
3726#ifdef FEAT_VIRTUALEDIT
3727 else if (VIsual_active
3728 && (VIsual_mode == Ctrl_V
3729 || VIsual_mode == 'v')
3730 && virtual_active()
3731 && tocol != MAXCOL
3732 && vcol < tocol
3733 && (
3734# ifdef FEAT_RIGHTLEFT
3735 wp->w_p_rl ? (col >= 0) :
3736# endif
3737 (col < W_WIDTH(wp))))
3738 {
3739 c = ' ';
3740 --ptr; /* put it back at the NUL */
3741 }
3742#endif
3743#if defined(FEAT_DIFF) || defined(LINE_ATTR)
3744 else if ((
3745# ifdef FEAT_DIFF
3746 diff_hlf != (enum hlf_value)0
3747# ifdef LINE_ATTR
3748 ||
3749# endif
3750# endif
3751# ifdef LINE_ATTR
3752 line_attr != 0
3753# endif
3754 ) && (
3755# ifdef FEAT_RIGHTLEFT
3756 wp->w_p_rl ? (col >= 0) :
3757# endif
3758 (col < W_WIDTH(wp))))
3759 {
3760 /* Highlight until the right side of the window */
3761 c = ' ';
3762 --ptr; /* put it back at the NUL */
3763# ifdef FEAT_DIFF
3764 if (diff_hlf == HLF_TXD)
3765 {
3766 diff_hlf = HLF_CHD;
3767 if (attr == 0 || char_attr != attr)
3768 char_attr = hl_attr(diff_hlf);
3769 }
3770# endif
3771 }
3772#endif
3773 }
3774 }
3775
3776 /* Don't override visual selection highlighting. */
3777 if (n_attr > 0
3778 && draw_state == WL_LINE
3779 && (area_attr == 0 || char_attr != area_attr)
3780 && (search_attr == 0 || char_attr != search_attr))
3781 char_attr = extra_attr;
3782
Bram Moolenaar81695252004-12-29 20:58:21 +00003783#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 /* XIM don't send preedit_start and preedit_end, but they send
3785 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
3786 * im_is_preediting() here. */
3787 if (xic != NULL
3788 && lnum == curwin->w_cursor.lnum
3789 && (State & INSERT)
3790 && !p_imdisable
3791 && im_is_preediting()
3792 && draw_state == WL_LINE)
3793 {
3794 colnr_T tcol;
3795
3796 if (preedit_end_col == MAXCOL)
3797 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
3798 else
3799 tcol = preedit_end_col;
3800 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
3801 {
3802 if (feedback_old_attr < 0)
3803 {
3804 feedback_col = 0;
3805 feedback_old_attr = char_attr;
3806 }
3807 char_attr = im_get_feedback_attr(feedback_col);
3808 if (char_attr < 0)
3809 char_attr = feedback_old_attr;
3810 feedback_col++;
3811 }
3812 else if (feedback_old_attr >= 0)
3813 {
3814 char_attr = feedback_old_attr;
3815 feedback_old_attr = -1;
3816 feedback_col = 0;
3817 }
3818 }
3819#endif
3820 /*
3821 * Handle the case where we are in column 0 but not on the first
3822 * character of the line and the user wants us to show us a
3823 * special character (via 'listchars' option "precedes:<char>".
3824 */
3825 if (lcs_prec_todo != NUL
3826 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
3827#ifdef FEAT_DIFF
3828 && filler_todo <= 0
3829#endif
3830 && draw_state > WL_NR
3831 && c != NUL)
3832 {
3833 c = lcs_prec;
3834 lcs_prec_todo = NUL;
3835#ifdef FEAT_MBYTE
3836 mb_c = c;
3837 if (enc_utf8 && (*mb_char2len)(c) > 1)
3838 {
3839 mb_utf8 = TRUE;
3840 u8c_c1 = u8c_c2 = 0;
3841 }
3842 else
3843 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3844#endif
3845 if ((area_attr == 0 || char_attr != area_attr)
3846 && (search_attr == 0 || char_attr != search_attr))
3847 {
3848 saved_attr3 = char_attr; /* save current attr */
3849 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
3850 n_attr3 = 1;
3851 }
3852 }
3853
3854 /*
3855 * At end of the text line.
3856 */
3857 if (c == NUL)
3858 {
3859 /* invert at least one char, used for Visual and empty line or
3860 * highlight match at end of line. If it's beyond the last
3861 * char on the screen, just overwrite that one (tricky!) Not
3862 * needed when a '$' was displayed for 'list'. */
3863 if (lcs_eol == lcs_eol_one
3864 && ((area_attr != 0 && vcol == fromcol)
3865#ifdef FEAT_SEARCH_EXTRA
3866 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003867 || (ptr - line) - 1 == (long)search_hl.startcol
3868 || (ptr - line) - 1 == (long)match_hl.startcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#endif
3870 ))
3871 {
3872 int n = 0;
3873
3874#ifdef FEAT_RIGHTLEFT
3875 if (wp->w_p_rl)
3876 {
3877 if (col < 0)
3878 n = 1;
3879 }
3880 else
3881#endif
3882 {
3883 if (col >= W_WIDTH(wp))
3884 n = -1;
3885 }
3886 if (n != 0)
3887 {
3888 /* At the window boundary, highlight the last character
3889 * instead (better than nothing). */
3890 off += n;
3891 col += n;
3892 }
3893 else
3894 {
3895 /* Add a blank character to highlight. */
3896 ScreenLines[off] = ' ';
3897#ifdef FEAT_MBYTE
3898 if (enc_utf8)
3899 ScreenLinesUC[off] = 0;
3900#endif
3901 }
3902#ifdef FEAT_SEARCH_EXTRA
3903 if (area_attr == 0)
3904 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003905 if ((ptr - line) - 1 == (long)match_hl.startcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 char_attr = match_hl.attr;
3907 else
3908 char_attr = search_hl.attr;
3909 }
3910#endif
3911 ScreenAttrs[off] = char_attr;
3912#ifdef FEAT_RIGHTLEFT
3913 if (wp->w_p_rl)
3914 --col;
3915 else
3916#endif
3917 ++col;
3918 }
3919
3920 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
3921 wp->w_p_rl);
3922 row++;
3923
3924 /*
3925 * Update w_cline_height and w_cline_folded if the cursor line was
3926 * updated (saves a call to plines() later).
3927 */
3928 if (wp == curwin && lnum == curwin->w_cursor.lnum)
3929 {
3930 curwin->w_cline_row = startrow;
3931 curwin->w_cline_height = row - startrow;
3932#ifdef FEAT_FOLDING
3933 curwin->w_cline_folded = FALSE;
3934#endif
3935 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3936 }
3937
3938 break;
3939 }
3940
3941 /* line continues beyond line end */
3942 if (lcs_ext
3943 && !wp->w_p_wrap
3944#ifdef FEAT_DIFF
3945 && filler_todo <= 0
3946#endif
3947 && (
3948#ifdef FEAT_RIGHTLEFT
3949 wp->w_p_rl ? col == 0 :
3950#endif
3951 col == W_WIDTH(wp) - 1)
3952 && (*ptr != NUL
3953 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
3954 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
3955 {
3956 c = lcs_ext;
3957 char_attr = hl_attr(HLF_AT);
3958#ifdef FEAT_MBYTE
3959 mb_c = c;
3960 if (enc_utf8 && (*mb_char2len)(c) > 1)
3961 {
3962 mb_utf8 = TRUE;
3963 u8c_c1 = u8c_c2 = 0;
3964 }
3965 else
3966 mb_utf8 = FALSE;
3967#endif
3968 }
3969
3970 /*
3971 * Store character to be displayed.
3972 * Skip characters that are left of the screen for 'nowrap'.
3973 */
3974 vcol_prev = vcol;
3975 if (draw_state < WL_LINE || n_skip <= 0)
3976 {
3977 /*
3978 * Store the character.
3979 */
3980#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
3981 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
3982 {
3983 /* A double-wide character is: put first halve in left cell. */
3984 --off;
3985 --col;
3986 }
3987#endif
3988 ScreenLines[off] = c;
3989#ifdef FEAT_MBYTE
3990 if (enc_dbcs == DBCS_JPNU)
3991 ScreenLines2[off] = mb_c & 0xff;
3992 else if (enc_utf8)
3993 {
3994 if (mb_utf8)
3995 {
3996 ScreenLinesUC[off] = mb_c;
3997 ScreenLinesC1[off] = u8c_c1;
3998 ScreenLinesC2[off] = u8c_c2;
3999 }
4000 else
4001 ScreenLinesUC[off] = 0;
4002 }
4003 if (multi_attr)
4004 {
4005 ScreenAttrs[off] = multi_attr;
4006 multi_attr = 0;
4007 }
4008 else
4009#endif
4010 ScreenAttrs[off] = char_attr;
4011
4012#ifdef FEAT_MBYTE
4013 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4014 {
4015 /* Need to fill two screen columns. */
4016 ++off;
4017 ++col;
4018 if (enc_utf8)
4019 /* UTF-8: Put a 0 in the second screen char. */
4020 ScreenLines[off] = 0;
4021 else
4022 /* DBCS: Put second byte in the second screen char. */
4023 ScreenLines[off] = mb_c & 0xff;
4024 ++vcol;
4025 /* When "tocol" is halfway a character, set it to the end of
4026 * the character, otherwise highlighting won't stop. */
4027 if (tocol == vcol)
4028 ++tocol;
4029#ifdef FEAT_RIGHTLEFT
4030 if (wp->w_p_rl)
4031 {
4032 /* now it's time to backup one cell */
4033 --off;
4034 --col;
4035 }
4036#endif
4037 }
4038#endif
4039#ifdef FEAT_RIGHTLEFT
4040 if (wp->w_p_rl)
4041 {
4042 --off;
4043 --col;
4044 }
4045 else
4046#endif
4047 {
4048 ++off;
4049 ++col;
4050 }
4051 }
4052 else
4053 --n_skip;
4054
4055 /* Only advance the "vcol" when after the 'number' column. */
4056 if (draw_state >= WL_SBR
4057#ifdef FEAT_DIFF
4058 && filler_todo <= 0
4059#endif
4060 )
4061 ++vcol;
4062
4063 /* restore attributes after "predeces" in 'listchars' */
4064 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4065 char_attr = saved_attr3;
4066
4067 /* restore attributes after last 'listchars' or 'number' char */
4068 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4069 char_attr = saved_attr2;
4070
4071 /*
4072 * At end of screen line and there is more to come: Display the line
4073 * so far. If there is no more to display it is catched above.
4074 */
4075 if ((
4076#ifdef FEAT_RIGHTLEFT
4077 wp->w_p_rl ? (col < 0) :
4078#endif
4079 (col >= W_WIDTH(wp)))
4080 && (*ptr != NUL
4081#ifdef FEAT_DIFF
4082 || filler_todo > 0
4083#endif
4084 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4085 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4086 )
4087 {
4088 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4089 wp->w_p_rl);
4090 ++row;
4091 ++screen_row;
4092
4093 /* When not wrapping and finished diff lines, or when displayed
4094 * '$' and highlighting until last column, break here. */
4095 if ((!wp->w_p_wrap
4096#ifdef FEAT_DIFF
4097 && filler_todo <= 0
4098#endif
4099 ) || lcs_eol_one == -1)
4100 break;
4101
4102 /* When the window is too narrow draw all "@" lines. */
4103 if (draw_state != WL_LINE
4104#ifdef FEAT_DIFF
4105 && filler_todo <= 0
4106#endif
4107 )
4108 {
4109 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4110#ifdef FEAT_VERTSPLIT
4111 draw_vsep_win(wp, row);
4112#endif
4113 row = endrow;
4114 }
4115
4116 /* When line got too long for screen break here. */
4117 if (row == endrow)
4118 {
4119 ++row;
4120 break;
4121 }
4122
4123 if (screen_cur_row == screen_row - 1
4124#ifdef FEAT_DIFF
4125 && filler_todo <= 0
4126#endif
4127 && W_WIDTH(wp) == Columns)
4128 {
4129 /* Remember that the line wraps, used for modeless copy. */
4130 LineWraps[screen_row - 1] = TRUE;
4131
4132 /*
4133 * Special trick to make copy/paste of wrapped lines work with
4134 * xterm/screen: write an extra character beyond the end of
4135 * the line. This will work with all terminal types
4136 * (regardless of the xn,am settings).
4137 * Only do this on a fast tty.
4138 * Only do this if the cursor is on the current line
4139 * (something has been written in it).
4140 * Don't do this for the GUI.
4141 * Don't do this for double-width characters.
4142 * Don't do this for a window not at the right screen border.
4143 */
4144 if (p_tf
4145#ifdef FEAT_GUI
4146 && !gui.in_use
4147#endif
4148#ifdef FEAT_MBYTE
4149 && !(has_mbyte
4150 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4151 || (*mb_off2cells)(LineOffset[screen_row - 1]
4152 + (int)Columns - 2) == 2))
4153#endif
4154 )
4155 {
4156 /* First make sure we are at the end of the screen line,
4157 * then output the same character again to let the
4158 * terminal know about the wrap. If the terminal doesn't
4159 * auto-wrap, we overwrite the character. */
4160 if (screen_cur_col != W_WIDTH(wp))
4161 screen_char(LineOffset[screen_row - 1]
4162 + (unsigned)Columns - 1,
4163 screen_row - 1, (int)(Columns - 1));
4164
4165#ifdef FEAT_MBYTE
4166 /* When there is a multi-byte character, just output a
4167 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004168 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4169 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 out_char(' ');
4171 else
4172#endif
4173 out_char(ScreenLines[LineOffset[screen_row - 1]
4174 + (Columns - 1)]);
4175 /* force a redraw of the first char on the next line */
4176 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4177 screen_start(); /* don't know where cursor is now */
4178 }
4179 }
4180
4181 col = 0;
4182 off = (unsigned)(current_ScreenLine - ScreenLines);
4183#ifdef FEAT_RIGHTLEFT
4184 if (wp->w_p_rl)
4185 {
4186 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4187 off += col;
4188 }
4189#endif
4190
4191 /* reset the drawing state for the start of a wrapped line */
4192 draw_state = WL_START;
4193 saved_n_extra = n_extra;
4194 saved_p_extra = p_extra;
4195 saved_c_extra = c_extra;
4196 saved_char_attr = char_attr;
4197 n_extra = 0;
4198 lcs_prec_todo = lcs_prec;
4199#ifdef FEAT_LINEBREAK
4200# ifdef FEAT_DIFF
4201 if (filler_todo <= 0)
4202# endif
4203 need_showbreak = TRUE;
4204#endif
4205#ifdef FEAT_DIFF
4206 --filler_todo;
4207 /* When the filler lines are actually below the last line of the
4208 * file, don't draw the line itself, break here. */
4209 if (filler_todo == 0 && wp->w_botfill)
4210 break;
4211#endif
4212 }
4213
4214 } /* for every character in the line */
4215
4216 return row;
4217}
4218
4219/*
4220 * Check whether the given character needs redrawing:
4221 * - the (first byte of the) character is different
4222 * - the attributes are different
4223 * - the character is multi-byte and the next byte is different
4224 */
4225 static int
4226char_needs_redraw(off_from, off_to, cols)
4227 int off_from;
4228 int off_to;
4229 int cols;
4230{
4231 if (cols > 0
4232 && ((ScreenLines[off_from] != ScreenLines[off_to]
4233 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4234
4235#ifdef FEAT_MBYTE
4236 || (enc_dbcs != 0
4237 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4238 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4239 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4240 : (cols > 1 && ScreenLines[off_from + 1]
4241 != ScreenLines[off_to + 1])))
4242 || (enc_utf8
4243 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4244 || (ScreenLinesUC[off_from] != 0
4245 && (ScreenLinesC1[off_from]
4246 != ScreenLinesC1[off_to]
4247 || ScreenLinesC2[off_from]
4248 != ScreenLinesC2[off_to]))))
4249#endif
4250 ))
4251 return TRUE;
4252 return FALSE;
4253}
4254
4255/*
4256 * Move one "cooked" screen line to the screen, but only the characters that
4257 * have actually changed. Handle insert/delete character.
4258 * "coloff" gives the first column on the screen for this line.
4259 * "endcol" gives the columns where valid characters are.
4260 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4261 * needs to be cleared, negative otherwise.
4262 * "rlflag" is TRUE in a rightleft window:
4263 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4264 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4265 */
4266 static void
4267screen_line(row, coloff, endcol, clear_width
4268#ifdef FEAT_RIGHTLEFT
4269 , rlflag
4270#endif
4271 )
4272 int row;
4273 int coloff;
4274 int endcol;
4275 int clear_width;
4276#ifdef FEAT_RIGHTLEFT
4277 int rlflag;
4278#endif
4279{
4280 unsigned off_from;
4281 unsigned off_to;
4282 int col = 0;
4283#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4284 int hl;
4285#endif
4286 int force = FALSE; /* force update rest of the line */
4287 int redraw_this /* bool: does character need redraw? */
4288#ifdef FEAT_GUI
4289 = TRUE /* For GUI when while-loop empty */
4290#endif
4291 ;
4292 int redraw_next; /* redraw_this for next character */
4293#ifdef FEAT_MBYTE
4294 int clear_next = FALSE;
4295 int char_cells; /* 1: normal char */
4296 /* 2: occupies two display cells */
4297# define CHAR_CELLS char_cells
4298#else
4299# define CHAR_CELLS 1
4300#endif
4301
4302# ifdef FEAT_CLIPBOARD
4303 clip_may_clear_selection(row, row);
4304# endif
4305
4306 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4307 off_to = LineOffset[row] + coloff;
4308
4309#ifdef FEAT_RIGHTLEFT
4310 if (rlflag)
4311 {
4312 /* Clear rest first, because it's left of the text. */
4313 if (clear_width > 0)
4314 {
4315 while (col <= endcol && ScreenLines[off_to] == ' '
4316 && ScreenAttrs[off_to] == 0
4317# ifdef FEAT_MBYTE
4318 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4319# endif
4320 )
4321 {
4322 ++off_to;
4323 ++col;
4324 }
4325 if (col <= endcol)
4326 screen_fill(row, row + 1, col + coloff,
4327 endcol + coloff + 1, ' ', ' ', 0);
4328 }
4329 col = endcol + 1;
4330 off_to = LineOffset[row] + col + coloff;
4331 off_from += col;
4332 endcol = (clear_width > 0 ? clear_width : -clear_width);
4333 }
4334#endif /* FEAT_RIGHTLEFT */
4335
4336 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4337
4338 while (col < endcol)
4339 {
4340#ifdef FEAT_MBYTE
4341 if (has_mbyte && (col + 1 < endcol))
4342 char_cells = (*mb_off2cells)(off_from);
4343 else
4344 char_cells = 1;
4345#endif
4346
4347 redraw_this = redraw_next;
4348 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4349 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4350
4351#ifdef FEAT_GUI
4352 /* If the next character was bold, then redraw the current character to
4353 * remove any pixels that might have spilt over into us. This only
4354 * happens in the GUI.
4355 */
4356 if (redraw_next && gui.in_use)
4357 {
4358 hl = ScreenAttrs[off_to + CHAR_CELLS];
4359 if (hl > HL_ALL || (hl & HL_BOLD))
4360 redraw_this = TRUE;
4361 }
4362#endif
4363
4364 if (redraw_this)
4365 {
4366 /*
4367 * Special handling when 'xs' termcap flag set (hpterm):
4368 * Attributes for characters are stored at the position where the
4369 * cursor is when writing the highlighting code. The
4370 * start-highlighting code must be written with the cursor on the
4371 * first highlighted character. The stop-highlighting code must
4372 * be written with the cursor just after the last highlighted
4373 * character.
4374 * Overwriting a character doesn't remove it's highlighting. Need
4375 * to clear the rest of the line, and force redrawing it
4376 * completely.
4377 */
4378 if ( p_wiv
4379 && !force
4380#ifdef FEAT_GUI
4381 && !gui.in_use
4382#endif
4383 && ScreenAttrs[off_to] != 0
4384 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4385 {
4386 /*
4387 * Need to remove highlighting attributes here.
4388 */
4389 windgoto(row, col + coloff);
4390 out_str(T_CE); /* clear rest of this screen line */
4391 screen_start(); /* don't know where cursor is now */
4392 force = TRUE; /* force redraw of rest of the line */
4393 redraw_next = TRUE; /* or else next char would miss out */
4394
4395 /*
4396 * If the previous character was highlighted, need to stop
4397 * highlighting at this character.
4398 */
4399 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4400 {
4401 screen_attr = ScreenAttrs[off_to - 1];
4402 term_windgoto(row, col + coloff);
4403 screen_stop_highlight();
4404 }
4405 else
4406 screen_attr = 0; /* highlighting has stopped */
4407 }
4408#ifdef FEAT_MBYTE
4409 if (enc_dbcs != 0)
4410 {
4411 /* Check if overwriting a double-byte with a single-byte or
4412 * the other way around requires another character to be
4413 * redrawn. For UTF-8 this isn't needed, because comparing
4414 * ScreenLinesUC[] is sufficient. */
4415 if (char_cells == 1
4416 && col + 1 < endcol
4417 && (*mb_off2cells)(off_to) > 1)
4418 {
4419 /* Writing a single-cell character over a double-cell
4420 * character: need to redraw the next cell. */
4421 ScreenLines[off_to + 1] = 0;
4422 redraw_next = TRUE;
4423 }
4424 else if (char_cells == 2
4425 && col + 2 < endcol
4426 && (*mb_off2cells)(off_to) == 1
4427 && (*mb_off2cells)(off_to + 1) > 1)
4428 {
4429 /* Writing the second half of a double-cell character over
4430 * a double-cell character: need to redraw the second
4431 * cell. */
4432 ScreenLines[off_to + 2] = 0;
4433 redraw_next = TRUE;
4434 }
4435
4436 if (enc_dbcs == DBCS_JPNU)
4437 ScreenLines2[off_to] = ScreenLines2[off_from];
4438 }
4439 /* When writing a single-width character over a double-width
4440 * character and at the end of the redrawn text, need to clear out
4441 * the right halve of the old character.
4442 * Also required when writing the right halve of a double-width
4443 * char over the left halve of an existing one. */
4444 if (has_mbyte && col + char_cells == endcol
4445 && ((char_cells == 1
4446 && (*mb_off2cells)(off_to) > 1)
4447 || (char_cells == 2
4448 && (*mb_off2cells)(off_to) == 1
4449 && (*mb_off2cells)(off_to + 1) > 1)))
4450 clear_next = TRUE;
4451#endif
4452
4453 ScreenLines[off_to] = ScreenLines[off_from];
4454#ifdef FEAT_MBYTE
4455 if (enc_utf8)
4456 {
4457 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4458 if (ScreenLinesUC[off_from] != 0)
4459 {
4460 ScreenLinesC1[off_to] = ScreenLinesC1[off_from];
4461 ScreenLinesC2[off_to] = ScreenLinesC2[off_from];
4462 }
4463 }
4464 if (char_cells == 2)
4465 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4466#endif
4467
4468#if defined(FEAT_GUI) || defined(UNIX)
4469 /* The bold trick makes a single row of pixels appear in the next
4470 * character. When a bold character is removed, the next
4471 * character should be redrawn too. This happens for our own GUI
4472 * and for some xterms. */
4473 if (
4474# ifdef FEAT_GUI
4475 gui.in_use
4476# endif
4477# if defined(FEAT_GUI) && defined(UNIX)
4478 ||
4479# endif
4480# ifdef UNIX
4481 term_is_xterm
4482# endif
4483 )
4484 {
4485 hl = ScreenAttrs[off_to];
4486 if (hl > HL_ALL || (hl & HL_BOLD))
4487 redraw_next = TRUE;
4488 }
4489#endif
4490 ScreenAttrs[off_to] = ScreenAttrs[off_from];
4491#ifdef FEAT_MBYTE
4492 if (enc_dbcs != 0 && char_cells == 2)
4493 {
4494 /* just a hack: It makes two bytes of DBCS have same attr */
4495 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
4496 screen_char_2(off_to, row, col + coloff);
4497 }
4498 else
4499#endif
4500 screen_char(off_to, row, col + coloff);
4501 }
4502 else if ( p_wiv
4503#ifdef FEAT_GUI
4504 && !gui.in_use
4505#endif
4506 && col + coloff > 0)
4507 {
4508 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
4509 {
4510 /*
4511 * Don't output stop-highlight when moving the cursor, it will
4512 * stop the highlighting when it should continue.
4513 */
4514 screen_attr = 0;
4515 }
4516 else if (screen_attr != 0)
4517 screen_stop_highlight();
4518 }
4519
4520 off_to += CHAR_CELLS;
4521 off_from += CHAR_CELLS;
4522 col += CHAR_CELLS;
4523 }
4524
4525#ifdef FEAT_MBYTE
4526 if (clear_next)
4527 {
4528 /* Clear the second half of a double-wide character of which the left
4529 * half was overwritten with a single-wide character. */
4530 ScreenLines[off_to] = ' ';
4531 if (enc_utf8)
4532 ScreenLinesUC[off_to] = 0;
4533 screen_char(off_to, row, col + coloff);
4534 }
4535#endif
4536
4537 if (clear_width > 0
4538#ifdef FEAT_RIGHTLEFT
4539 && !rlflag
4540#endif
4541 )
4542 {
4543#ifdef FEAT_GUI
4544 int startCol = col;
4545#endif
4546
4547 /* blank out the rest of the line */
4548 while (col < clear_width && ScreenLines[off_to] == ' '
4549 && ScreenAttrs[off_to] == 0
4550#ifdef FEAT_MBYTE
4551 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4552#endif
4553 )
4554 {
4555 ++off_to;
4556 ++col;
4557 }
4558 if (col < clear_width)
4559 {
4560#ifdef FEAT_GUI
4561 /*
4562 * In the GUI, clearing the rest of the line may leave pixels
4563 * behind if the first character cleared was bold. Some bold
4564 * fonts spill over the left. In this case we redraw the previous
4565 * character too. If we didn't skip any blanks above, then we
4566 * only redraw if the character wasn't already redrawn anyway.
4567 */
4568 if (gui.in_use && (col > startCol || !redraw_this)
4569# ifdef FEAT_MBYTE
4570 && enc_dbcs == 0
4571# endif
4572 )
4573 {
4574 hl = ScreenAttrs[off_to];
4575 if (hl > HL_ALL || (hl & HL_BOLD))
4576 screen_char(off_to - 1, row, col + coloff - 1);
4577 }
4578#endif
4579 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
4580 ' ', ' ', 0);
4581#ifdef FEAT_VERTSPLIT
4582 off_to += clear_width - col;
4583 col = clear_width;
4584#endif
4585 }
4586 }
4587
4588 if (clear_width > 0)
4589 {
4590#ifdef FEAT_VERTSPLIT
4591 /* For a window that's left of another, draw the separator char. */
4592 if (col + coloff < Columns)
4593 {
4594 int c;
4595
4596 c = fillchar_vsep(&hl);
4597 if (ScreenLines[off_to] != c
4598# ifdef FEAT_MBYTE
4599 || (enc_utf8
4600 && ScreenLinesUC[off_to] != (c >= 0x80 ? c : 0))
4601# endif
4602 || ScreenAttrs[off_to] != hl)
4603 {
4604 ScreenLines[off_to] = c;
4605 ScreenAttrs[off_to] = hl;
4606# ifdef FEAT_MBYTE
4607 if (enc_utf8)
4608 {
4609 if (c >= 0x80)
4610 {
4611 ScreenLinesUC[off_to] = c;
4612 ScreenLinesC1[off_to] = 0;
4613 ScreenLinesC2[off_to] = 0;
4614 }
4615 else
4616 ScreenLinesUC[off_to] = 0;
4617 }
4618# endif
4619 screen_char(off_to, row, col + coloff);
4620 }
4621 }
4622 else
4623#endif
4624 LineWraps[row] = FALSE;
4625 }
4626}
4627
4628#ifdef FEAT_RIGHTLEFT
4629/*
4630 * Mirror text "str" for right-lieft displaying.
4631 */
4632 static void
4633rl_mirror(str)
4634 char_u *str;
4635{
4636 char_u *p1, *p2;
4637 int t;
4638
4639 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
4640 {
4641 t = *p1;
4642 *p1 = *p2;
4643 *p2 = t;
4644 }
4645}
4646#endif
4647
4648#if defined(FEAT_WINDOWS) || defined(PROTO)
4649/*
4650 * mark all status lines for redraw; used after first :cd
4651 */
4652 void
4653status_redraw_all()
4654{
4655 win_T *wp;
4656
4657 for (wp = firstwin; wp; wp = wp->w_next)
4658 if (wp->w_status_height)
4659 {
4660 wp->w_redr_status = TRUE;
4661 redraw_later(VALID);
4662 }
4663}
4664
4665/*
4666 * mark all status lines of the current buffer for redraw
4667 */
4668 void
4669status_redraw_curbuf()
4670{
4671 win_T *wp;
4672
4673 for (wp = firstwin; wp; wp = wp->w_next)
4674 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
4675 {
4676 wp->w_redr_status = TRUE;
4677 redraw_later(VALID);
4678 }
4679}
4680
4681/*
4682 * Redraw all status lines that need to be redrawn.
4683 */
4684 void
4685redraw_statuslines()
4686{
4687 win_T *wp;
4688
4689 for (wp = firstwin; wp; wp = wp->w_next)
4690 if (wp->w_redr_status)
4691 win_redr_status(wp);
4692}
4693#endif
4694
4695#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
4696/*
4697 * Redraw all status lines at the bottom of frame "frp".
4698 */
4699 void
4700win_redraw_last_status(frp)
4701 frame_T *frp;
4702{
4703 if (frp->fr_layout == FR_LEAF)
4704 frp->fr_win->w_redr_status = TRUE;
4705 else if (frp->fr_layout == FR_ROW)
4706 {
4707 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
4708 win_redraw_last_status(frp);
4709 }
4710 else /* frp->fr_layout == FR_COL */
4711 {
4712 frp = frp->fr_child;
4713 while (frp->fr_next != NULL)
4714 frp = frp->fr_next;
4715 win_redraw_last_status(frp);
4716 }
4717}
4718#endif
4719
4720#ifdef FEAT_VERTSPLIT
4721/*
4722 * Draw the verticap separator right of window "wp" starting with line "row".
4723 */
4724 static void
4725draw_vsep_win(wp, row)
4726 win_T *wp;
4727 int row;
4728{
4729 int hl;
4730 int c;
4731
4732 if (wp->w_vsep_width)
4733 {
4734 /* draw the vertical separator right of this window */
4735 c = fillchar_vsep(&hl);
4736 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
4737 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
4738 c, ' ', hl);
4739 }
4740}
4741#endif
4742
4743#ifdef FEAT_WILDMENU
4744static int status_match_len __ARGS((expand_T *xp, char_u *s));
4745
4746/*
4747 * Get the lenght of an item as it will be shown in the status line.
4748 */
4749 static int
4750status_match_len(xp, s)
4751 expand_T *xp;
4752 char_u *s;
4753{
4754 int len = 0;
4755
4756#ifdef FEAT_MENU
4757 int emenu = (xp->xp_context == EXPAND_MENUS
4758 || xp->xp_context == EXPAND_MENUNAMES);
4759
4760 /* Check for menu separators - replace with '|'. */
4761 if (emenu && menu_is_separator(s))
4762 return 1;
4763#endif
4764
4765 while (*s != NUL)
4766 {
4767 /* Don't display backslashes used for escaping, they look ugly. */
4768 if (rem_backslash(s)
4769#ifdef FEAT_MENU
4770 || (emenu && (s[0] == '\\' && s[1] != NUL))
4771#endif
4772 )
4773 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00004774 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004775 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777
4778 return len;
4779}
4780
4781/*
4782 * Show wildchar matches in the status line.
4783 * Show at least the "match" item.
4784 * We start at item 'first_match' in the list and show all matches that fit.
4785 *
4786 * If inversion is possible we use it. Else '=' characters are used.
4787 */
4788 void
4789win_redr_status_matches(xp, num_matches, matches, match, showtail)
4790 expand_T *xp;
4791 int num_matches;
4792 char_u **matches; /* list of matches */
4793 int match;
4794 int showtail;
4795{
4796#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
4797 int row;
4798 char_u *buf;
4799 int len;
4800 int clen; /* lenght in screen cells */
4801 int fillchar;
4802 int attr;
4803 int i;
4804 int highlight = TRUE;
4805 char_u *selstart = NULL;
4806 int selstart_col = 0;
4807 char_u *selend = NULL;
4808 static int first_match = 0;
4809 int add_left = FALSE;
4810 char_u *s;
4811#ifdef FEAT_MENU
4812 int emenu;
4813#endif
4814#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
4815 int l;
4816#endif
4817
4818 if (matches == NULL) /* interrupted completion? */
4819 return;
4820
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004821#ifdef FEAT_MBYTE
4822 if (has_mbyte)
4823 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
4824 else
4825#endif
4826 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if (buf == NULL)
4828 return;
4829
4830 if (match == -1) /* don't show match but original text */
4831 {
4832 match = 0;
4833 highlight = FALSE;
4834 }
4835 /* count 1 for the ending ">" */
4836 clen = status_match_len(xp, L_MATCH(match)) + 3;
4837 if (match == 0)
4838 first_match = 0;
4839 else if (match < first_match)
4840 {
4841 /* jumping left, as far as we can go */
4842 first_match = match;
4843 add_left = TRUE;
4844 }
4845 else
4846 {
4847 /* check if match fits on the screen */
4848 for (i = first_match; i < match; ++i)
4849 clen += status_match_len(xp, L_MATCH(i)) + 2;
4850 if (first_match > 0)
4851 clen += 2;
4852 /* jumping right, put match at the left */
4853 if ((long)clen > Columns)
4854 {
4855 first_match = match;
4856 /* if showing the last match, we can add some on the left */
4857 clen = 2;
4858 for (i = match; i < num_matches; ++i)
4859 {
4860 clen += status_match_len(xp, L_MATCH(i)) + 2;
4861 if ((long)clen >= Columns)
4862 break;
4863 }
4864 if (i == num_matches)
4865 add_left = TRUE;
4866 }
4867 }
4868 if (add_left)
4869 while (first_match > 0)
4870 {
4871 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
4872 if ((long)clen >= Columns)
4873 break;
4874 --first_match;
4875 }
4876
4877 fillchar = fillchar_status(&attr, TRUE);
4878
4879 if (first_match == 0)
4880 {
4881 *buf = NUL;
4882 len = 0;
4883 }
4884 else
4885 {
4886 STRCPY(buf, "< ");
4887 len = 2;
4888 }
4889 clen = len;
4890
4891 i = first_match;
4892 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
4893 {
4894 if (i == match)
4895 {
4896 selstart = buf + len;
4897 selstart_col = clen;
4898 }
4899
4900 s = L_MATCH(i);
4901 /* Check for menu separators - replace with '|' */
4902#ifdef FEAT_MENU
4903 emenu = (xp->xp_context == EXPAND_MENUS
4904 || xp->xp_context == EXPAND_MENUNAMES);
4905 if (emenu && menu_is_separator(s))
4906 {
4907 STRCPY(buf + len, transchar('|'));
4908 l = (int)STRLEN(buf + len);
4909 len += l;
4910 clen += l;
4911 }
4912 else
4913#endif
4914 for ( ; *s != NUL; ++s)
4915 {
4916 /* Don't display backslashes used for escaping, they look ugly. */
4917 if (rem_backslash(s)
4918#ifdef FEAT_MENU
4919 || (emenu
4920 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
4921#endif
4922 )
4923 ++s;
4924 clen += ptr2cells(s);
4925#ifdef FEAT_MBYTE
4926 if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
4927 {
4928 STRNCPY(buf + len, s, l);
4929 s += l - 1;
4930 len += l;
4931 }
4932 else
4933#endif
4934 {
4935 STRCPY(buf + len, transchar_byte(*s));
4936 len += (int)STRLEN(buf + len);
4937 }
4938 }
4939 if (i == match)
4940 selend = buf + len;
4941
4942 *(buf + len++) = ' ';
4943 *(buf + len++) = ' ';
4944 clen += 2;
4945 if (++i == num_matches)
4946 break;
4947 }
4948
4949 if (i != num_matches)
4950 {
4951 *(buf + len++) = '>';
4952 ++clen;
4953 }
4954
4955 buf[len] = NUL;
4956
4957 row = cmdline_row - 1;
4958 if (row >= 0)
4959 {
4960 if (wild_menu_showing == 0)
4961 {
4962 if (msg_scrolled > 0)
4963 {
4964 /* Put the wildmenu just above the command line. If there is
4965 * no room, scroll the screen one line up. */
4966 if (cmdline_row == Rows - 1)
4967 {
4968 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
4969 ++msg_scrolled;
4970 }
4971 else
4972 {
4973 ++cmdline_row;
4974 ++row;
4975 }
4976 wild_menu_showing = WM_SCROLLED;
4977 }
4978 else
4979 {
4980 /* Create status line if needed by setting 'laststatus' to 2.
4981 * Set 'winminheight' to zero to avoid that the window is
4982 * resized. */
4983 if (lastwin->w_status_height == 0)
4984 {
4985 save_p_ls = p_ls;
4986 save_p_wmh = p_wmh;
4987 p_ls = 2;
4988 p_wmh = 0;
4989 last_status(FALSE);
4990 }
4991 wild_menu_showing = WM_SHOWN;
4992 }
4993 }
4994
4995 screen_puts(buf, row, 0, attr);
4996 if (selstart != NULL && highlight)
4997 {
4998 *selend = NUL;
4999 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5000 }
5001
5002 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5003 }
5004
5005#ifdef FEAT_VERTSPLIT
5006 win_redraw_last_status(topframe);
5007#else
5008 lastwin->w_redr_status = TRUE;
5009#endif
5010 vim_free(buf);
5011}
5012#endif
5013
5014#if defined(FEAT_WINDOWS) || defined(PROTO)
5015/*
5016 * Redraw the status line of window wp.
5017 *
5018 * If inversion is possible we use it. Else '=' characters are used.
5019 */
5020 void
5021win_redr_status(wp)
5022 win_T *wp;
5023{
5024 int row;
5025 char_u *p;
5026 int len;
5027 int fillchar;
5028 int attr;
5029 int this_ru_col;
5030
5031 wp->w_redr_status = FALSE;
5032 if (wp->w_status_height == 0)
5033 {
5034 /* no status line, can only be last window */
5035 redraw_cmdline = TRUE;
5036 }
5037 else if (!redrawing())
5038 {
5039 /* Don't redraw right now, do it later. */
5040 wp->w_redr_status = TRUE;
5041 }
5042#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005043 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 /* redraw custom status line */
5046 win_redr_custom(wp, FALSE);
5047 }
5048#endif
5049 else
5050 {
5051 fillchar = fillchar_status(&attr, wp == curwin);
5052
5053 if (buf_spname(wp->w_buffer) != NULL)
5054 STRCPY(NameBuff, buf_spname(wp->w_buffer));
5055 else
5056 home_replace(wp->w_buffer, wp->w_buffer->b_fname, NameBuff,
5057 MAXPATHL, TRUE);
5058 trans_characters(NameBuff, MAXPATHL);
5059 p = NameBuff;
5060 len = (int)STRLEN(p);
5061
5062 if (wp->w_buffer->b_help
5063#ifdef FEAT_QUICKFIX
5064 || wp->w_p_pvw
5065#endif
5066 || bufIsChanged(wp->w_buffer)
5067 || wp->w_buffer->b_p_ro)
5068 *(p + len++) = ' ';
5069 if (wp->w_buffer->b_help)
5070 {
5071 STRCPY(p + len, _("[help]"));
5072 len += (int)STRLEN(p + len);
5073 }
5074#ifdef FEAT_QUICKFIX
5075 if (wp->w_p_pvw)
5076 {
5077 STRCPY(p + len, _("[Preview]"));
5078 len += (int)STRLEN(p + len);
5079 }
5080#endif
5081 if (bufIsChanged(wp->w_buffer))
5082 {
5083 STRCPY(p + len, "[+]");
5084 len += 3;
5085 }
5086 if (wp->w_buffer->b_p_ro)
5087 {
5088 STRCPY(p + len, "[RO]");
5089 len += 4;
5090 }
5091
5092#ifndef FEAT_VERTSPLIT
5093 this_ru_col = ru_col;
5094 if (this_ru_col < (Columns + 1) / 2)
5095 this_ru_col = (Columns + 1) / 2;
5096#else
5097 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5098 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5099 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5100 if (this_ru_col <= 1)
5101 {
5102 p = (char_u *)"<"; /* No room for file name! */
5103 len = 1;
5104 }
5105 else
5106#endif
5107#ifdef FEAT_MBYTE
5108 if (has_mbyte)
5109 {
5110 int clen = 0, i;
5111
5112 /* Count total number of display cells. */
5113 for (i = 0; p[i] != NUL; i += (*mb_ptr2len_check)(p + i))
5114 clen += (*mb_ptr2cells)(p + i);
5115 /* Find first character that will fit.
5116 * Going from start to end is much faster for DBCS. */
5117 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5118 i += (*mb_ptr2len_check)(p + i))
5119 clen -= (*mb_ptr2cells)(p + i);
5120 len = clen;
5121 if (i > 0)
5122 {
5123 p = p + i - 1;
5124 *p = '<';
5125 ++len;
5126 }
5127
5128 }
5129 else
5130#endif
5131 if (len > this_ru_col - 1)
5132 {
5133 p += len - (this_ru_col - 1);
5134 *p = '<';
5135 len = this_ru_col - 1;
5136 }
5137
5138 row = W_WINROW(wp) + wp->w_height;
5139 screen_puts(p, row, W_WINCOL(wp), attr);
5140 screen_fill(row, row + 1, len + W_WINCOL(wp),
5141 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5142
5143 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5144 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5145 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5146 - 1 + W_WINCOL(wp)), attr);
5147
5148#ifdef FEAT_CMDL_INFO
5149 win_redr_ruler(wp, TRUE);
5150#endif
5151 }
5152
5153#ifdef FEAT_VERTSPLIT
5154 /*
5155 * May need to draw the character below the vertical separator.
5156 */
5157 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5158 {
5159 if (stl_connected(wp))
5160 fillchar = fillchar_status(&attr, wp == curwin);
5161 else
5162 fillchar = fillchar_vsep(&attr);
5163 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5164 attr);
5165 }
5166#endif
5167}
5168
5169# ifdef FEAT_VERTSPLIT
5170/*
5171 * Return TRUE if the status line of window "wp" is connected to the status
5172 * line of the window right of it. If not, then it's a vertical separator.
5173 * Only call if (wp->w_vsep_width != 0).
5174 */
5175 int
5176stl_connected(wp)
5177 win_T *wp;
5178{
5179 frame_T *fr;
5180
5181 fr = wp->w_frame;
5182 while (fr->fr_parent != NULL)
5183 {
5184 if (fr->fr_parent->fr_layout == FR_COL)
5185 {
5186 if (fr->fr_next != NULL)
5187 break;
5188 }
5189 else
5190 {
5191 if (fr->fr_next != NULL)
5192 return TRUE;
5193 }
5194 fr = fr->fr_parent;
5195 }
5196 return FALSE;
5197}
5198# endif
5199
5200#endif /* FEAT_WINDOWS */
5201
5202#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5203/*
5204 * Get the value to show for the language mappings, active 'keymap'.
5205 */
5206 int
5207get_keymap_str(wp, buf, len)
5208 win_T *wp;
5209 char_u *buf; /* buffer for the result */
5210 int len; /* length of buffer */
5211{
5212 char_u *p;
5213
5214 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5215 return FALSE;
5216
5217 {
5218#ifdef FEAT_EVAL
5219 buf_T *old_curbuf = curbuf;
5220 win_T *old_curwin = curwin;
5221 char_u *s;
5222
5223 curbuf = wp->w_buffer;
5224 curwin = wp;
5225 STRCPY(buf, "b:keymap_name"); /* must be writable */
5226 ++emsg_skip;
5227 s = p = eval_to_string(buf, NULL);
5228 --emsg_skip;
5229 curbuf = old_curbuf;
5230 curwin = old_curwin;
5231 if (p == NULL || *p == NUL)
5232#endif
5233 {
5234#ifdef FEAT_KEYMAP
5235 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5236 p = wp->w_buffer->b_p_keymap;
5237 else
5238#endif
5239 p = (char_u *)"lang";
5240 }
5241 if ((int)(STRLEN(p) + 3) < len)
5242 sprintf((char *)buf, "<%s>", p);
5243 else
5244 buf[0] = NUL;
5245#ifdef FEAT_EVAL
5246 vim_free(s);
5247#endif
5248 }
5249 return buf[0] != NUL;
5250}
5251#endif
5252
5253#if defined(FEAT_STL_OPT) || defined(PROTO)
5254/*
5255 * Redraw the status line or ruler of window wp.
5256 */
5257 static void
5258win_redr_custom(wp, Ruler)
5259 win_T *wp;
5260 int Ruler;
5261{
5262 int attr;
5263 int curattr;
5264 int row;
5265 int col = 0;
5266 int maxwidth;
5267 int width;
5268 int n;
5269 int len;
5270 int fillchar;
5271 char_u buf[MAXPATHL];
5272 char_u *p;
5273 struct stl_hlrec hl[STL_MAX_ITEM];
5274
5275 /* setup environment for the task at hand */
5276 row = W_WINROW(wp) + wp->w_height;
5277 fillchar = fillchar_status(&attr, wp == curwin);
5278 maxwidth = W_WIDTH(wp);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005279 if (*wp->w_p_stl != NUL)
5280 p = wp->w_p_stl;
5281 else
5282 p = p_stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (Ruler)
5284 {
5285 p = p_ruf;
5286 /* advance past any leading group spec - implicit in ru_col */
5287 if (*p == '%')
5288 {
5289 if (*++p == '-')
5290 p++;
5291 if (atoi((char *) p))
5292 while (VIM_ISDIGIT(*p))
5293 p++;
5294 if (*p++ != '(')
5295 p = p_ruf;
5296 }
5297#ifdef FEAT_VERTSPLIT
5298 col = ru_col - (Columns - W_WIDTH(wp));
5299 if (col < (W_WIDTH(wp) + 1) / 2)
5300 col = (W_WIDTH(wp) + 1) / 2;
5301#else
5302 col = ru_col;
5303 if (col > (Columns + 1) / 2)
5304 col = (Columns + 1) / 2;
5305#endif
5306 maxwidth = W_WIDTH(wp) - col;
5307#ifdef FEAT_WINDOWS
5308 if (!wp->w_status_height)
5309#endif
5310 {
5311 row = Rows - 1;
5312 --maxwidth; /* writing in last column may cause scrolling */
5313 fillchar = ' ';
5314 attr = 0;
5315 }
5316 }
5317 if (maxwidth <= 0)
5318 return;
5319#ifdef FEAT_VERTSPLIT
5320 col += W_WINCOL(wp);
5321#endif
5322
5323 width = build_stl_str_hl(wp, buf, sizeof(buf), p, fillchar, maxwidth, hl);
5324 len = STRLEN(buf);
5325
5326 while (width < maxwidth && len < sizeof(buf) - 1)
5327 {
5328#ifdef FEAT_MBYTE
5329 len += (*mb_char2bytes)(fillchar, buf + len);
5330#else
5331 buf[len++] = fillchar;
5332#endif
5333 ++width;
5334 }
5335 buf[len] = NUL;
5336
5337 curattr = attr;
5338 p = buf;
5339 for (n = 0; hl[n].start != NULL; n++)
5340 {
5341 len = (int)(hl[n].start - p);
5342 screen_puts_len(p, len, row, col, curattr);
5343 col += vim_strnsize(p, len);
5344 p = hl[n].start;
5345
5346 if (hl[n].userhl == 0)
5347 curattr = attr;
5348#ifdef FEAT_WINDOWS
5349 else if (wp != curwin && wp->w_status_height != 0)
5350 curattr = highlight_stlnc[hl[n].userhl - 1];
5351#endif
5352 else
5353 curattr = highlight_user[hl[n].userhl - 1];
5354 }
5355 screen_puts(p, row, col, curattr);
5356}
5357
5358#endif /* FEAT_STL_OPT */
5359
5360/*
5361 * Output a single character directly to the screen and update ScreenLines.
5362 */
5363 void
5364screen_putchar(c, row, col, attr)
5365 int c;
5366 int row, col;
5367 int attr;
5368{
5369#ifdef FEAT_MBYTE
5370 char_u buf[MB_MAXBYTES + 1];
5371
5372 buf[(*mb_char2bytes)(c, buf)] = NUL;
5373#else
5374 char_u buf[2];
5375
5376 buf[0] = c;
5377 buf[1] = NUL;
5378#endif
5379 screen_puts(buf, row, col, attr);
5380}
5381
5382/*
5383 * Get a single character directly from ScreenLines into "bytes[]".
5384 * Also return its attribute in *attrp;
5385 */
5386 void
5387screen_getbytes(row, col, bytes, attrp)
5388 int row, col;
5389 char_u *bytes;
5390 int *attrp;
5391{
5392 unsigned off;
5393
5394 /* safety check */
5395 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
5396 {
5397 off = LineOffset[row] + col;
5398 *attrp = ScreenAttrs[off];
5399 bytes[0] = ScreenLines[off];
5400 bytes[1] = NUL;
5401
5402#ifdef FEAT_MBYTE
5403 if (enc_utf8 && ScreenLinesUC[off] != 0)
5404 bytes[utfc_char2bytes(off, bytes)] = NUL;
5405 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
5406 {
5407 bytes[0] = ScreenLines[off];
5408 bytes[1] = ScreenLines2[off];
5409 bytes[2] = NUL;
5410 }
5411 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
5412 {
5413 bytes[1] = ScreenLines[off + 1];
5414 bytes[2] = NUL;
5415 }
5416#endif
5417 }
5418}
5419
5420/*
5421 * Put string '*text' on the screen at position 'row' and 'col', with
5422 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
5423 * Note: only outputs within one row, message is truncated at screen boundary!
5424 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
5425 */
5426 void
5427screen_puts(text, row, col, attr)
5428 char_u *text;
5429 int row;
5430 int col;
5431 int attr;
5432{
5433 screen_puts_len(text, -1, row, col, attr);
5434}
5435
5436/*
5437 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
5438 * a NUL.
5439 */
5440 void
5441screen_puts_len(text, len, row, col, attr)
5442 char_u *text;
5443 int len;
5444 int row;
5445 int col;
5446 int attr;
5447{
5448 unsigned off;
5449 char_u *ptr = text;
5450 int c;
5451#ifdef FEAT_MBYTE
5452 int mbyte_blen = 1;
5453 int mbyte_cells = 1;
5454 int u8c = 0;
5455 int u8c_c1 = 0;
5456 int u8c_c2 = 0;
5457 int clear_next_cell = FALSE;
5458# ifdef FEAT_ARABIC
5459 int prev_c = 0; /* previous Arabic character */
5460 int pc, nc, nc1, dummy;
5461# endif
5462#endif
5463
5464 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
5465 return;
5466
5467 off = LineOffset[row] + col;
5468 while (*ptr != NUL && col < screen_Columns
5469 && (len < 0 || (int)(ptr - text) < len))
5470 {
5471 c = *ptr;
5472#ifdef FEAT_MBYTE
5473 /* check if this is the first byte of a multibyte */
5474 if (has_mbyte)
5475 {
5476 if (enc_utf8 && len > 0)
5477 mbyte_blen = utfc_ptr2len_check_len(ptr,
5478 (int)((text + len) - ptr));
5479 else
5480 mbyte_blen = (*mb_ptr2len_check)(ptr);
5481 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
5482 mbyte_cells = 1;
5483 else if (enc_dbcs != 0)
5484 mbyte_cells = mbyte_blen;
5485 else /* enc_utf8 */
5486 {
5487 if (len >= 0)
5488 u8c = utfc_ptr2char_len(ptr, &u8c_c1, &u8c_c2,
5489 (int)((text + len) - ptr));
5490 else
5491 u8c = utfc_ptr2char(ptr, &u8c_c1, &u8c_c2);
5492 mbyte_cells = utf_char2cells(u8c);
5493 /* Non-BMP character: display as ? or fullwidth ?. */
5494 if (u8c >= 0x10000)
5495 {
5496 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
5497 if (attr == 0)
5498 attr = hl_attr(HLF_8);
5499 }
5500# ifdef FEAT_ARABIC
5501 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
5502 {
5503 /* Do Arabic shaping. */
5504 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
5505 {
5506 /* Past end of string to be displayed. */
5507 nc = NUL;
5508 nc1 = NUL;
5509 }
5510 else
5511 nc = utfc_ptr2char(ptr + mbyte_blen, &nc1, &dummy);
5512 pc = prev_c;
5513 prev_c = u8c;
5514 u8c = arabic_shape(u8c, &c, &u8c_c1, nc, nc1, pc);
5515 }
5516 else
5517 prev_c = u8c;
5518# endif
5519 }
5520 }
5521#endif
5522
5523 if (ScreenLines[off] != c
5524#ifdef FEAT_MBYTE
5525 || (mbyte_cells == 2
5526 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
5527 || (enc_dbcs == DBCS_JPNU
5528 && c == 0x8e
5529 && ScreenLines2[off] != ptr[1])
5530 || (enc_utf8
5531 && mbyte_blen > 1
5532 && (ScreenLinesUC[off] != u8c
5533 || ScreenLinesC1[off] != u8c_c1
5534 || ScreenLinesC2[off] != u8c_c2))
5535#endif
5536 || ScreenAttrs[off] != attr
5537 || exmode_active
5538 )
5539 {
5540#if defined(FEAT_GUI) || defined(UNIX)
5541 /* The bold trick makes a single row of pixels appear in the next
5542 * character. When a bold character is removed, the next
5543 * character should be redrawn too. This happens for our own GUI
5544 * and for some xterms.
5545 * Force the redraw by setting the attribute to a different value
5546 * than "attr", the contents of ScreenLines[] may be needed by
5547 * mb_off2cells() further on.
5548 * Don't do this for the last drawn character, because the next
5549 * character may not be redrawn. */
5550 if (
5551# ifdef FEAT_GUI
5552 gui.in_use
5553# endif
5554# if defined(FEAT_GUI) && defined(UNIX)
5555 ||
5556# endif
5557# ifdef UNIX
5558 term_is_xterm
5559# endif
5560 )
5561 {
5562 int n;
5563
5564 n = ScreenAttrs[off];
5565# ifdef FEAT_MBYTE
5566 if (col + mbyte_cells < screen_Columns
5567 && (n > HL_ALL || (n & HL_BOLD))
5568 && (len < 0 ? ptr[mbyte_blen] != NUL
5569 : ptr + mbyte_blen < text + len))
5570 ScreenAttrs[off + mbyte_cells] = attr + 1;
5571# else
5572 if (col + 1 < screen_Columns
5573 && (n > HL_ALL || (n & HL_BOLD))
5574 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
5575 ScreenLines[off + 1] = 0;
5576# endif
5577 }
5578#endif
5579#ifdef FEAT_MBYTE
5580 /* When at the end of the text and overwriting a two-cell
5581 * character with a one-cell character, need to clear the next
5582 * cell. Also when overwriting the left halve of a two-cell char
5583 * with the right halve of a two-cell char. Do this only once
5584 * (mb_off2cells() may return 2 on the right halve). */
5585 if (clear_next_cell)
5586 clear_next_cell = FALSE;
5587 else if (has_mbyte
5588 && (len < 0 ? ptr[mbyte_blen] == NUL
5589 : ptr + mbyte_blen >= text + len)
5590 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
5591 || (mbyte_cells == 2
5592 && (*mb_off2cells)(off) == 1
5593 && (*mb_off2cells)(off + 1) > 1)))
5594 clear_next_cell = TRUE;
5595
5596 /* Make sure we never leave a second byte of a double-byte behind,
5597 * it confuses mb_off2cells(). */
5598 if (enc_dbcs
5599 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
5600 || (mbyte_cells == 2
5601 && (*mb_off2cells)(off) == 1
5602 && (*mb_off2cells)(off + 1) > 1)))
5603 ScreenLines[off + mbyte_blen] = 0;
5604#endif
5605 ScreenLines[off] = c;
5606 ScreenAttrs[off] = attr;
5607#ifdef FEAT_MBYTE
5608 if (enc_utf8)
5609 {
5610 if (c < 0x80 && u8c_c1 == 0 && u8c_c2 == 0)
5611 ScreenLinesUC[off] = 0;
5612 else
5613 {
5614 ScreenLinesUC[off] = u8c;
5615 ScreenLinesC1[off] = u8c_c1;
5616 ScreenLinesC2[off] = u8c_c2;
5617 }
5618 if (mbyte_cells == 2)
5619 {
5620 ScreenLines[off + 1] = 0;
5621 ScreenAttrs[off + 1] = attr;
5622 }
5623 screen_char(off, row, col);
5624 }
5625 else if (mbyte_cells == 2)
5626 {
5627 ScreenLines[off + 1] = ptr[1];
5628 ScreenAttrs[off + 1] = attr;
5629 screen_char_2(off, row, col);
5630 }
5631 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
5632 {
5633 ScreenLines2[off] = ptr[1];
5634 screen_char(off, row, col);
5635 }
5636 else
5637#endif
5638 screen_char(off, row, col);
5639 }
5640#ifdef FEAT_MBYTE
5641 if (has_mbyte)
5642 {
5643 off += mbyte_cells;
5644 col += mbyte_cells;
5645 ptr += mbyte_blen;
5646 if (clear_next_cell)
5647 ptr = (char_u *)" ";
5648 }
5649 else
5650#endif
5651 {
5652 ++off;
5653 ++col;
5654 ++ptr;
5655 }
5656 }
5657}
5658
5659#ifdef FEAT_SEARCH_EXTRA
5660/*
5661 * Prepare for 'searchhl' highlighting.
5662 */
5663 static void
5664start_search_hl()
5665{
5666 if (p_hls && !no_hlsearch)
5667 {
5668 last_pat_prog(&search_hl.rm);
5669 search_hl.attr = hl_attr(HLF_L);
5670 }
5671}
5672
5673/*
5674 * Clean up for 'searchhl' highlighting.
5675 */
5676 static void
5677end_search_hl()
5678{
5679 if (search_hl.rm.regprog != NULL)
5680 {
5681 vim_free(search_hl.rm.regprog);
5682 search_hl.rm.regprog = NULL;
5683 }
5684}
5685
5686/*
5687 * Advance to the match in window "wp" line "lnum" or past it.
5688 */
5689 static void
5690prepare_search_hl(wp, lnum)
5691 win_T *wp;
5692 linenr_T lnum;
5693{
5694 match_T *shl; /* points to search_hl or match_hl */
5695 int n;
5696
5697 /*
5698 * When using a multi-line pattern, start searching at the top
5699 * of the window or just after a closed fold.
5700 * Do this both for search_hl and match_hl.
5701 */
5702 shl = &search_hl;
5703 for (;;)
5704 {
5705 if (shl->rm.regprog != NULL
5706 && shl->lnum == 0
5707 && re_multiline(shl->rm.regprog))
5708 {
5709 if (shl->first_lnum == 0)
5710 {
5711# ifdef FEAT_FOLDING
5712 for (shl->first_lnum = lnum;
5713 shl->first_lnum > wp->w_topline; --shl->first_lnum)
5714 if (hasFoldingWin(wp, shl->first_lnum - 1,
5715 NULL, NULL, TRUE, NULL))
5716 break;
5717# else
5718 shl->first_lnum = wp->w_topline;
5719# endif
5720 }
5721 n = 0;
5722 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
5723 {
5724 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
5725 if (shl->lnum != 0)
5726 {
5727 shl->first_lnum = shl->lnum
5728 + shl->rm.endpos[0].lnum
5729 - shl->rm.startpos[0].lnum;
5730 n = shl->rm.endpos[0].col;
5731 }
5732 else
5733 {
5734 ++shl->first_lnum;
5735 n = 0;
5736 }
5737 }
5738 }
5739 if (shl == &match_hl)
5740 break;
5741 shl = &match_hl;
5742 }
5743}
5744
5745/*
5746 * Search for a next 'searchl' or ":match" match.
5747 * Uses shl->buf.
5748 * Sets shl->lnum and shl->rm contents.
5749 * Note: Assumes a previous match is always before "lnum", unless
5750 * shl->lnum is zero.
5751 * Careful: Any pointers for buffer lines will become invalid.
5752 */
5753 static void
5754next_search_hl(win, shl, lnum, mincol)
5755 win_T *win;
5756 match_T *shl; /* points to search_hl or match_hl */
5757 linenr_T lnum;
5758 colnr_T mincol; /* minimal column for a match */
5759{
5760 linenr_T l;
5761 colnr_T matchcol;
5762 long nmatched;
5763
5764 if (shl->lnum != 0)
5765 {
5766 /* Check for three situations:
5767 * 1. If the "lnum" is below a previous match, start a new search.
5768 * 2. If the previous match includes "mincol", use it.
5769 * 3. Continue after the previous match.
5770 */
5771 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
5772 if (lnum > l)
5773 shl->lnum = 0;
5774 else if (lnum < l || shl->rm.endpos[0].col > mincol)
5775 return;
5776 }
5777
5778 /*
5779 * Repeat searching for a match until one is found that includes "mincol"
5780 * or none is found in this line.
5781 */
5782 called_emsg = FALSE;
5783 for (;;)
5784 {
5785 /* Three situations:
5786 * 1. No useful previous match: search from start of line.
5787 * 2. Not Vi compatible or empty match: continue at next character.
5788 * Break the loop if this is beyond the end of the line.
5789 * 3. Vi compatible searching: continue at end of previous match.
5790 */
5791 if (shl->lnum == 0)
5792 matchcol = 0;
5793 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
5794 || (shl->rm.endpos[0].lnum == 0
5795 && shl->rm.endpos[0].col == shl->rm.startpos[0].col))
5796 {
5797 matchcol = shl->rm.startpos[0].col + 1;
5798 if (ml_get_buf(shl->buf, lnum, FALSE)[matchcol - 1] == NUL)
5799 {
5800 shl->lnum = 0;
5801 break;
5802 }
5803 }
5804 else
5805 matchcol = shl->rm.endpos[0].col;
5806
5807 shl->lnum = lnum;
5808 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
5809 if (called_emsg)
5810 {
5811 /* Error while handling regexp: stop using this regexp. */
5812 vim_free(shl->rm.regprog);
5813 shl->rm.regprog = NULL;
5814 no_hlsearch = TRUE;
5815 break;
5816 }
5817 if (nmatched == 0)
5818 {
5819 shl->lnum = 0; /* no match found */
5820 break;
5821 }
5822 if (shl->rm.startpos[0].lnum > 0
5823 || shl->rm.startpos[0].col >= mincol
5824 || nmatched > 1
5825 || shl->rm.endpos[0].col > mincol)
5826 {
5827 shl->lnum += shl->rm.startpos[0].lnum;
5828 break; /* useful match found */
5829 }
5830 }
5831}
5832#endif
5833
5834 static void
5835screen_start_highlight(attr)
5836 int attr;
5837{
5838 attrentry_T *aep = NULL;
5839
5840 screen_attr = attr;
5841 if (full_screen
5842#ifdef WIN3264
5843 && termcap_active
5844#endif
5845 )
5846 {
5847#ifdef FEAT_GUI
5848 if (gui.in_use)
5849 {
5850 char buf[20];
5851
5852 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr); /* internal GUI code */
5853 OUT_STR(buf);
5854 }
5855 else
5856#endif
5857 {
5858 if (attr > HL_ALL) /* special HL attr. */
5859 {
5860 if (t_colors > 1)
5861 aep = syn_cterm_attr2entry(attr);
5862 else
5863 aep = syn_term_attr2entry(attr);
5864 if (aep == NULL) /* did ":syntax clear" */
5865 attr = 0;
5866 else
5867 attr = aep->ae_attr;
5868 }
5869 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
5870 out_str(T_MD);
5871 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
5872 out_str(T_SO);
5873 if ((attr & HL_UNDERLINE) && T_US != NULL) /* underline */
5874 out_str(T_US);
5875 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
5876 out_str(T_CZH);
5877 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
5878 out_str(T_MR);
5879
5880 /*
5881 * Output the color or start string after bold etc., in case the
5882 * bold etc. override the color setting.
5883 */
5884 if (aep != NULL)
5885 {
5886 if (t_colors > 1)
5887 {
5888 if (aep->ae_u.cterm.fg_color)
5889 term_fg_color(aep->ae_u.cterm.fg_color - 1);
5890 if (aep->ae_u.cterm.bg_color)
5891 term_bg_color(aep->ae_u.cterm.bg_color - 1);
5892 }
5893 else
5894 {
5895 if (aep->ae_u.term.start != NULL)
5896 out_str(aep->ae_u.term.start);
5897 }
5898 }
5899 }
5900 }
5901}
5902
5903 void
5904screen_stop_highlight()
5905{
5906 int do_ME = FALSE; /* output T_ME code */
5907
5908 if (screen_attr != 0
5909#ifdef WIN3264
5910 && termcap_active
5911#endif
5912 )
5913 {
5914#ifdef FEAT_GUI
5915 if (gui.in_use)
5916 {
5917 char buf[20];
5918
5919 /* use internal GUI code */
5920 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
5921 OUT_STR(buf);
5922 }
5923 else
5924#endif
5925 {
5926 if (screen_attr > HL_ALL) /* special HL attr. */
5927 {
5928 attrentry_T *aep;
5929
5930 if (t_colors > 1)
5931 {
5932 /*
5933 * Assume that t_me restores the original colors!
5934 */
5935 aep = syn_cterm_attr2entry(screen_attr);
5936 if (aep != NULL && (aep->ae_u.cterm.fg_color
5937 || aep->ae_u.cterm.bg_color))
5938 do_ME = TRUE;
5939 }
5940 else
5941 {
5942 aep = syn_term_attr2entry(screen_attr);
5943 if (aep != NULL && aep->ae_u.term.stop != NULL)
5944 {
5945 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
5946 do_ME = TRUE;
5947 else
5948 out_str(aep->ae_u.term.stop);
5949 }
5950 }
5951 if (aep == NULL) /* did ":syntax clear" */
5952 screen_attr = 0;
5953 else
5954 screen_attr = aep->ae_attr;
5955 }
5956
5957 /*
5958 * Often all ending-codes are equal to T_ME. Avoid outputting the
5959 * same sequence several times.
5960 */
5961 if (screen_attr & HL_STANDOUT)
5962 {
5963 if (STRCMP(T_SE, T_ME) == 0)
5964 do_ME = TRUE;
5965 else
5966 out_str(T_SE);
5967 }
5968 if (screen_attr & HL_UNDERLINE)
5969 {
5970 if (STRCMP(T_UE, T_ME) == 0)
5971 do_ME = TRUE;
5972 else
5973 out_str(T_UE);
5974 }
5975 if (screen_attr & HL_ITALIC)
5976 {
5977 if (STRCMP(T_CZR, T_ME) == 0)
5978 do_ME = TRUE;
5979 else
5980 out_str(T_CZR);
5981 }
5982 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
5983 out_str(T_ME);
5984
5985 if (t_colors > 1)
5986 {
5987 /* set Normal cterm colors */
5988 if (cterm_normal_fg_color != 0)
5989 term_fg_color(cterm_normal_fg_color - 1);
5990 if (cterm_normal_bg_color != 0)
5991 term_bg_color(cterm_normal_bg_color - 1);
5992 if (cterm_normal_fg_bold)
5993 out_str(T_MD);
5994 }
5995 }
5996 }
5997 screen_attr = 0;
5998}
5999
6000/*
6001 * Reset the colors for a cterm. Used when leaving Vim.
6002 * The machine specific code may override this again.
6003 */
6004 void
6005reset_cterm_colors()
6006{
6007 if (t_colors > 1)
6008 {
6009 /* set Normal cterm colors */
6010 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6011 {
6012 out_str(T_OP);
6013 screen_attr = -1;
6014 }
6015 if (cterm_normal_fg_bold)
6016 {
6017 out_str(T_ME);
6018 screen_attr = -1;
6019 }
6020 }
6021}
6022
6023/*
6024 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6025 * using the attributes from ScreenAttrs["off"].
6026 */
6027 static void
6028screen_char(off, row, col)
6029 unsigned off;
6030 int row;
6031 int col;
6032{
6033 int attr;
6034
6035 /* Check for illegal values, just in case (could happen just after
6036 * resizing). */
6037 if (row >= screen_Rows || col >= screen_Columns)
6038 return;
6039
6040 /* Outputting the last character on the screen may scrollup the screen.
6041 * Don't to it! Mark the character invalid (update it when scrolled up) */
6042 if (row == screen_Rows - 1 && col == screen_Columns - 1
6043#ifdef FEAT_RIGHTLEFT
6044 /* account for first command-line character in rightleft mode */
6045 && !cmdmsg_rl
6046#endif
6047 )
6048 {
6049 ScreenAttrs[off] = (sattr_T)-1;
6050 return;
6051 }
6052
6053 /*
6054 * Stop highlighting first, so it's easier to move the cursor.
6055 */
6056#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6057 if (screen_char_attr != 0)
6058 attr = screen_char_attr;
6059 else
6060#endif
6061 attr = ScreenAttrs[off];
6062 if (screen_attr != attr)
6063 screen_stop_highlight();
6064
6065 windgoto(row, col);
6066
6067 if (screen_attr != attr)
6068 screen_start_highlight(attr);
6069
6070#ifdef FEAT_MBYTE
6071 if (enc_utf8 && ScreenLinesUC[off] != 0)
6072 {
6073 char_u buf[MB_MAXBYTES + 1];
6074
6075 /* Convert UTF-8 character to bytes and write it. */
6076
6077 buf[utfc_char2bytes(off, buf)] = NUL;
6078
6079 out_str(buf);
6080 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6081 ++screen_cur_col;
6082 }
6083 else
6084#endif
6085 {
6086#ifdef FEAT_MBYTE
6087 out_flush_check();
6088#endif
6089 out_char(ScreenLines[off]);
6090#ifdef FEAT_MBYTE
6091 /* double-byte character in single-width cell */
6092 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6093 out_char(ScreenLines2[off]);
6094#endif
6095 }
6096
6097 screen_cur_col++;
6098}
6099
6100#ifdef FEAT_MBYTE
6101
6102/*
6103 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6104 * on the screen at position 'row' and 'col'.
6105 * The attributes of the first byte is used for all. This is required to
6106 * output the two bytes of a double-byte character with nothing in between.
6107 */
6108 static void
6109screen_char_2(off, row, col)
6110 unsigned off;
6111 int row;
6112 int col;
6113{
6114 /* Check for illegal values (could be wrong when screen was resized). */
6115 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6116 return;
6117
6118 /* Outputting the last character on the screen may scrollup the screen.
6119 * Don't to it! Mark the character invalid (update it when scrolled up) */
6120 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6121 {
6122 ScreenAttrs[off] = (sattr_T)-1;
6123 return;
6124 }
6125
6126 /* Output the first byte normally (positions the cursor), then write the
6127 * second byte directly. */
6128 screen_char(off, row, col);
6129 out_char(ScreenLines[off + 1]);
6130 ++screen_cur_col;
6131}
6132#endif
6133
6134#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6135/*
6136 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6137 * This uses the contents of ScreenLines[] and doesn't change it.
6138 */
6139 void
6140screen_draw_rectangle(row, col, height, width, invert)
6141 int row;
6142 int col;
6143 int height;
6144 int width;
6145 int invert;
6146{
6147 int r, c;
6148 int off;
6149
6150 if (invert)
6151 screen_char_attr = HL_INVERSE;
6152 for (r = row; r < row + height; ++r)
6153 {
6154 off = LineOffset[r];
6155 for (c = col; c < col + width; ++c)
6156 {
6157#ifdef FEAT_MBYTE
6158 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6159 {
6160 screen_char_2(off + c, r, c);
6161 ++c;
6162 }
6163 else
6164#endif
6165 {
6166 screen_char(off + c, r, c);
6167#ifdef FEAT_MBYTE
6168 if (utf_off2cells(off + c) > 1)
6169 ++c;
6170#endif
6171 }
6172 }
6173 }
6174 screen_char_attr = 0;
6175}
6176#endif
6177
6178#ifdef FEAT_VERTSPLIT
6179/*
6180 * Redraw the characters for a vertically split window.
6181 */
6182 static void
6183redraw_block(row, end, wp)
6184 int row;
6185 int end;
6186 win_T *wp;
6187{
6188 int col;
6189 int width;
6190
6191# ifdef FEAT_CLIPBOARD
6192 clip_may_clear_selection(row, end - 1);
6193# endif
6194
6195 if (wp == NULL)
6196 {
6197 col = 0;
6198 width = Columns;
6199 }
6200 else
6201 {
6202 col = wp->w_wincol;
6203 width = wp->w_width;
6204 }
6205 screen_draw_rectangle(row, col, end - row, width, FALSE);
6206}
6207#endif
6208
6209/*
6210 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6211 * with character 'c1' in first column followed by 'c2' in the other columns.
6212 * Use attributes 'attr'.
6213 */
6214 void
6215screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6216 int start_row, end_row;
6217 int start_col, end_col;
6218 int c1, c2;
6219 int attr;
6220{
6221 int row;
6222 int col;
6223 int off;
6224 int end_off;
6225 int did_delete;
6226 int c;
6227 int norm_term;
6228#if defined(FEAT_GUI) || defined(UNIX)
6229 int force_next = FALSE;
6230#endif
6231
6232 if (end_row > screen_Rows) /* safety check */
6233 end_row = screen_Rows;
6234 if (end_col > screen_Columns) /* safety check */
6235 end_col = screen_Columns;
6236 if (ScreenLines == NULL
6237 || start_row >= end_row
6238 || start_col >= end_col) /* nothing to do */
6239 return;
6240
6241 /* it's a "normal" terminal when not in a GUI or cterm */
6242 norm_term = (
6243#ifdef FEAT_GUI
6244 !gui.in_use &&
6245#endif
6246 t_colors <= 1);
6247 for (row = start_row; row < end_row; ++row)
6248 {
6249 /*
6250 * Try to use delete-line termcap code, when no attributes or in a
6251 * "normal" terminal, where a bold/italic space is just a
6252 * space.
6253 */
6254 did_delete = FALSE;
6255 if (c2 == ' '
6256 && end_col == Columns
6257 && can_clear(T_CE)
6258 && (attr == 0
6259 || (norm_term
6260 && attr <= HL_ALL
6261 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6262 {
6263 /*
6264 * check if we really need to clear something
6265 */
6266 col = start_col;
6267 if (c1 != ' ') /* don't clear first char */
6268 ++col;
6269
6270 off = LineOffset[row] + col;
6271 end_off = LineOffset[row] + end_col;
6272
6273 /* skip blanks (used often, keep it fast!) */
6274#ifdef FEAT_MBYTE
6275 if (enc_utf8)
6276 while (off < end_off && ScreenLines[off] == ' '
6277 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6278 ++off;
6279 else
6280#endif
6281 while (off < end_off && ScreenLines[off] == ' '
6282 && ScreenAttrs[off] == 0)
6283 ++off;
6284 if (off < end_off) /* something to be cleared */
6285 {
6286 col = off - LineOffset[row];
6287 screen_stop_highlight();
6288 term_windgoto(row, col);/* clear rest of this screen line */
6289 out_str(T_CE);
6290 screen_start(); /* don't know where cursor is now */
6291 col = end_col - col;
6292 while (col--) /* clear chars in ScreenLines */
6293 {
6294 ScreenLines[off] = ' ';
6295#ifdef FEAT_MBYTE
6296 if (enc_utf8)
6297 ScreenLinesUC[off] = 0;
6298#endif
6299 ScreenAttrs[off] = 0;
6300 ++off;
6301 }
6302 }
6303 did_delete = TRUE; /* the chars are cleared now */
6304 }
6305
6306 off = LineOffset[row] + start_col;
6307 c = c1;
6308 for (col = start_col; col < end_col; ++col)
6309 {
6310 if (ScreenLines[off] != c
6311#ifdef FEAT_MBYTE
6312 || (enc_utf8 && ScreenLinesUC[off] != (c >= 0x80 ? c : 0))
6313#endif
6314 || ScreenAttrs[off] != attr
6315#if defined(FEAT_GUI) || defined(UNIX)
6316 || force_next
6317#endif
6318 )
6319 {
6320#if defined(FEAT_GUI) || defined(UNIX)
6321 /* The bold trick may make a single row of pixels appear in
6322 * the next character. When a bold character is removed, the
6323 * next character should be redrawn too. This happens for our
6324 * own GUI and for some xterms. */
6325 if (
6326# ifdef FEAT_GUI
6327 gui.in_use
6328# endif
6329# if defined(FEAT_GUI) && defined(UNIX)
6330 ||
6331# endif
6332# ifdef UNIX
6333 term_is_xterm
6334# endif
6335 )
6336 {
6337 if (ScreenLines[off] != ' '
6338 && (ScreenAttrs[off] > HL_ALL
6339 || ScreenAttrs[off] & HL_BOLD))
6340 force_next = TRUE;
6341 else
6342 force_next = FALSE;
6343 }
6344#endif
6345 ScreenLines[off] = c;
6346#ifdef FEAT_MBYTE
6347 if (enc_utf8)
6348 {
6349 if (c >= 0x80)
6350 {
6351 ScreenLinesUC[off] = c;
6352 ScreenLinesC1[off] = 0;
6353 ScreenLinesC2[off] = 0;
6354 }
6355 else
6356 ScreenLinesUC[off] = 0;
6357 }
6358#endif
6359 ScreenAttrs[off] = attr;
6360 if (!did_delete || c != ' ')
6361 screen_char(off, row, col);
6362 }
6363 ++off;
6364 if (col == start_col)
6365 {
6366 if (did_delete)
6367 break;
6368 c = c2;
6369 }
6370 }
6371 if (end_col == Columns)
6372 LineWraps[row] = FALSE;
6373 if (row == Rows - 1) /* overwritten the command line */
6374 {
6375 redraw_cmdline = TRUE;
6376 if (c1 == ' ' && c2 == ' ')
6377 clear_cmdline = FALSE; /* command line has been cleared */
6378 }
6379 }
6380}
6381
6382/*
6383 * Check if there should be a delay. Used before clearing or redrawing the
6384 * screen or the command line.
6385 */
6386 void
6387check_for_delay(check_msg_scroll)
6388 int check_msg_scroll;
6389{
6390 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
6391 && !did_wait_return
6392 && emsg_silent == 0)
6393 {
6394 out_flush();
6395 ui_delay(1000L, TRUE);
6396 emsg_on_display = FALSE;
6397 if (check_msg_scroll)
6398 msg_scroll = FALSE;
6399 }
6400}
6401
6402/*
6403 * screen_valid - allocate screen buffers if size changed
6404 * If "clear" is TRUE: clear screen if it has been resized.
6405 * Returns TRUE if there is a valid screen to write to.
6406 * Returns FALSE when starting up and screen not initialized yet.
6407 */
6408 int
6409screen_valid(clear)
6410 int clear;
6411{
6412 screenalloc(clear); /* allocate screen buffers if size changed */
6413 return (ScreenLines != NULL);
6414}
6415
6416/*
6417 * Resize the shell to Rows and Columns.
6418 * Allocate ScreenLines[] and associated items.
6419 *
6420 * There may be some time between setting Rows and Columns and (re)allocating
6421 * ScreenLines[]. This happens when starting up and when (manually) changing
6422 * the shell size. Always use screen_Rows and screen_Columns to access items
6423 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
6424 * final size of the shell is needed.
6425 */
6426 void
6427screenalloc(clear)
6428 int clear;
6429{
6430 int new_row, old_row;
6431#ifdef FEAT_GUI
6432 int old_Rows;
6433#endif
6434 win_T *wp;
6435 int outofmem = FALSE;
6436 int len;
6437 schar_T *new_ScreenLines;
6438#ifdef FEAT_MBYTE
6439 u8char_T *new_ScreenLinesUC = NULL;
6440 u8char_T *new_ScreenLinesC1 = NULL;
6441 u8char_T *new_ScreenLinesC2 = NULL;
6442 schar_T *new_ScreenLines2 = NULL;
6443#endif
6444 sattr_T *new_ScreenAttrs;
6445 unsigned *new_LineOffset;
6446 char_u *new_LineWraps;
6447 static int entered = FALSE; /* avoid recursiveness */
6448
6449 /*
6450 * Allocation of the screen buffers is done only when the size changes and
6451 * when Rows and Columns have been set and we have started doing full
6452 * screen stuff.
6453 */
6454 if ((ScreenLines != NULL
6455 && Rows == screen_Rows
6456 && Columns == screen_Columns
6457#ifdef FEAT_MBYTE
6458 && enc_utf8 == (ScreenLinesUC != NULL)
6459 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
6460#endif
6461 )
6462 || Rows == 0
6463 || Columns == 0
6464 || (!full_screen && ScreenLines == NULL))
6465 return;
6466
6467 /*
6468 * It's possible that we produce an out-of-memory message below, which
6469 * will cause this function to be called again. To break the loop, just
6470 * return here.
6471 */
6472 if (entered)
6473 return;
6474 entered = TRUE;
6475
6476 win_new_shellsize(); /* fit the windows in the new sized shell */
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 comp_col(); /* recompute columns for shown command and ruler */
6479
6480 /*
6481 * We're changing the size of the screen.
6482 * - Allocate new arrays for ScreenLines and ScreenAttrs.
6483 * - Move lines from the old arrays into the new arrays, clear extra
6484 * lines (unless the screen is going to be cleared).
6485 * - Free the old arrays.
6486 *
6487 * If anything fails, make ScreenLines NULL, so we don't do anything!
6488 * Continuing with the old ScreenLines may result in a crash, because the
6489 * size is wrong.
6490 */
6491#ifdef FEAT_WINDOWS
6492 for (wp = firstwin; wp; wp = wp->w_next)
6493 win_free_lsize(wp);
6494#else
6495 win_free_lsize(curwin);
6496#endif
6497
6498 new_ScreenLines = (schar_T *)lalloc((long_u)(
6499 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
6500#ifdef FEAT_MBYTE
6501 if (enc_utf8)
6502 {
6503 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
6504 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
6505 new_ScreenLinesC1 = (u8char_T *)lalloc((long_u)(
6506 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
6507 new_ScreenLinesC2 = (u8char_T *)lalloc((long_u)(
6508 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
6509 }
6510 if (enc_dbcs == DBCS_JPNU)
6511 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
6512 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
6513#endif
6514 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
6515 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
6516 new_LineOffset = (unsigned *)lalloc((long_u)(
6517 Rows * sizeof(unsigned)), FALSE);
6518 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
6519
6520 FOR_ALL_WINDOWS(wp)
6521 {
6522 if (win_alloc_lines(wp) == FAIL)
6523 {
6524 outofmem = TRUE;
6525#ifdef FEAT_WINDOWS
6526 break;
6527#endif
6528 }
6529 }
6530
6531 if (new_ScreenLines == NULL
6532#ifdef FEAT_MBYTE
6533 || (enc_utf8 && (new_ScreenLinesUC == NULL
6534 || new_ScreenLinesC1 == NULL || new_ScreenLinesC2 == NULL))
6535 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
6536#endif
6537 || new_ScreenAttrs == NULL
6538 || new_LineOffset == NULL
6539 || new_LineWraps == NULL
6540 || outofmem)
6541 {
6542 do_outofmem_msg((long_u)((Rows + 1) * Columns)); /* guess the size */
6543 vim_free(new_ScreenLines);
6544 new_ScreenLines = NULL;
6545#ifdef FEAT_MBYTE
6546 vim_free(new_ScreenLinesUC);
6547 new_ScreenLinesUC = NULL;
6548 vim_free(new_ScreenLinesC1);
6549 new_ScreenLinesC1 = NULL;
6550 vim_free(new_ScreenLinesC2);
6551 new_ScreenLinesC2 = NULL;
6552 vim_free(new_ScreenLines2);
6553 new_ScreenLines2 = NULL;
6554#endif
6555 vim_free(new_ScreenAttrs);
6556 new_ScreenAttrs = NULL;
6557 vim_free(new_LineOffset);
6558 new_LineOffset = NULL;
6559 vim_free(new_LineWraps);
6560 new_LineWraps = NULL;
6561 }
6562 else
6563 {
6564 for (new_row = 0; new_row < Rows; ++new_row)
6565 {
6566 new_LineOffset[new_row] = new_row * Columns;
6567 new_LineWraps[new_row] = FALSE;
6568
6569 /*
6570 * If the screen is not going to be cleared, copy as much as
6571 * possible from the old screen to the new one and clear the rest
6572 * (used when resizing the window at the "--more--" prompt or when
6573 * executing an external command, for the GUI).
6574 */
6575 if (!clear)
6576 {
6577 (void)vim_memset(new_ScreenLines + new_row * Columns,
6578 ' ', (size_t)Columns * sizeof(schar_T));
6579#ifdef FEAT_MBYTE
6580 if (enc_utf8)
6581 {
6582 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
6583 0, (size_t)Columns * sizeof(u8char_T));
6584 (void)vim_memset(new_ScreenLinesC1 + new_row * Columns,
6585 0, (size_t)Columns * sizeof(u8char_T));
6586 (void)vim_memset(new_ScreenLinesC2 + new_row * Columns,
6587 0, (size_t)Columns * sizeof(u8char_T));
6588 }
6589 if (enc_dbcs == DBCS_JPNU)
6590 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
6591 0, (size_t)Columns * sizeof(schar_T));
6592#endif
6593 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
6594 0, (size_t)Columns * sizeof(sattr_T));
6595 old_row = new_row + (screen_Rows - Rows);
6596 if (old_row >= 0)
6597 {
6598 if (screen_Columns < Columns)
6599 len = screen_Columns;
6600 else
6601 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006602#ifdef FEAT_MBYTE
6603 /* When switching to utf-8 dont copy characters, they
6604 * may be invalid now. */
6605 if (!(enc_utf8 && ScreenLinesUC == NULL))
6606#endif
6607 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
6608 ScreenLines + LineOffset[old_row],
6609 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610#ifdef FEAT_MBYTE
6611 if (enc_utf8 && ScreenLinesUC != NULL)
6612 {
6613 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
6614 ScreenLinesUC + LineOffset[old_row],
6615 (size_t)len * sizeof(u8char_T));
6616 mch_memmove(new_ScreenLinesC1 + new_LineOffset[new_row],
6617 ScreenLinesC1 + LineOffset[old_row],
6618 (size_t)len * sizeof(u8char_T));
6619 mch_memmove(new_ScreenLinesC2 + new_LineOffset[new_row],
6620 ScreenLinesC2 + LineOffset[old_row],
6621 (size_t)len * sizeof(u8char_T));
6622 }
6623 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
6624 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
6625 ScreenLines2 + LineOffset[old_row],
6626 (size_t)len * sizeof(schar_T));
6627#endif
6628 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
6629 ScreenAttrs + LineOffset[old_row],
6630 (size_t)len * sizeof(sattr_T));
6631 }
6632 }
6633 }
6634 /* Use the last line of the screen for the current line. */
6635 current_ScreenLine = new_ScreenLines + Rows * Columns;
6636 }
6637
6638 vim_free(ScreenLines);
6639#ifdef FEAT_MBYTE
6640 vim_free(ScreenLinesUC);
6641 vim_free(ScreenLinesC1);
6642 vim_free(ScreenLinesC2);
6643 vim_free(ScreenLines2);
6644#endif
6645 vim_free(ScreenAttrs);
6646 vim_free(LineOffset);
6647 vim_free(LineWraps);
6648 ScreenLines = new_ScreenLines;
6649#ifdef FEAT_MBYTE
6650 ScreenLinesUC = new_ScreenLinesUC;
6651 ScreenLinesC1 = new_ScreenLinesC1;
6652 ScreenLinesC2 = new_ScreenLinesC2;
6653 ScreenLines2 = new_ScreenLines2;
6654#endif
6655 ScreenAttrs = new_ScreenAttrs;
6656 LineOffset = new_LineOffset;
6657 LineWraps = new_LineWraps;
6658
6659 /* It's important that screen_Rows and screen_Columns reflect the actual
6660 * size of ScreenLines[]. Set them before calling anything. */
6661#ifdef FEAT_GUI
6662 old_Rows = screen_Rows;
6663#endif
6664 screen_Rows = Rows;
6665 screen_Columns = Columns;
6666
6667 must_redraw = CLEAR; /* need to clear the screen later */
6668 if (clear)
6669 screenclear2();
6670
6671#ifdef FEAT_GUI
6672 else if (gui.in_use
6673 && !gui.starting
6674 && ScreenLines != NULL
6675 && old_Rows != Rows)
6676 {
6677 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
6678 /*
6679 * Adjust the position of the cursor, for when executing an external
6680 * command.
6681 */
6682 if (msg_row >= Rows) /* Rows got smaller */
6683 msg_row = Rows - 1; /* put cursor at last row */
6684 else if (Rows > old_Rows) /* Rows got bigger */
6685 msg_row += Rows - old_Rows; /* put cursor in same place */
6686 if (msg_col >= Columns) /* Columns got smaller */
6687 msg_col = Columns - 1; /* put cursor at last column */
6688 }
6689#endif
6690
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 entered = FALSE;
6692}
6693
6694 void
6695screenclear()
6696{
6697 check_for_delay(FALSE);
6698 screenalloc(FALSE); /* allocate screen buffers if size changed */
6699 screenclear2(); /* clear the screen */
6700}
6701
6702 static void
6703screenclear2()
6704{
6705 int i;
6706
6707 if (starting == NO_SCREEN || ScreenLines == NULL
6708#ifdef FEAT_GUI
6709 || (gui.in_use && gui.starting)
6710#endif
6711 )
6712 return;
6713
6714#ifdef FEAT_GUI
6715 if (!gui.in_use)
6716#endif
6717 screen_attr = -1; /* force setting the Normal colors */
6718 screen_stop_highlight(); /* don't want highlighting here */
6719
6720#ifdef FEAT_CLIPBOARD
6721 /* disable selection without redrawing it */
6722 clip_scroll_selection(9999);
6723#endif
6724
6725 /* blank out ScreenLines */
6726 for (i = 0; i < Rows; ++i)
6727 {
6728 lineclear(LineOffset[i], (int)Columns);
6729 LineWraps[i] = FALSE;
6730 }
6731
6732 if (can_clear(T_CL))
6733 {
6734 out_str(T_CL); /* clear the display */
6735 clear_cmdline = FALSE;
6736 }
6737 else
6738 {
6739 /* can't clear the screen, mark all chars with invalid attributes */
6740 for (i = 0; i < Rows; ++i)
6741 lineinvalid(LineOffset[i], (int)Columns);
6742 clear_cmdline = TRUE;
6743 }
6744
6745 screen_cleared = TRUE; /* can use contents of ScreenLines now */
6746
6747 win_rest_invalid(firstwin);
6748 redraw_cmdline = TRUE;
6749 if (must_redraw == CLEAR) /* no need to clear again */
6750 must_redraw = NOT_VALID;
6751 compute_cmdrow();
6752 msg_row = cmdline_row; /* put cursor on last line for messages */
6753 msg_col = 0;
6754 screen_start(); /* don't know where cursor is now */
6755 msg_scrolled = 0; /* can't scroll back */
6756 msg_didany = FALSE;
6757 msg_didout = FALSE;
6758}
6759
6760/*
6761 * Clear one line in ScreenLines.
6762 */
6763 static void
6764lineclear(off, width)
6765 unsigned off;
6766 int width;
6767{
6768 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
6769#ifdef FEAT_MBYTE
6770 if (enc_utf8)
6771 (void)vim_memset(ScreenLinesUC + off, 0,
6772 (size_t)width * sizeof(u8char_T));
6773#endif
6774 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
6775}
6776
6777/*
6778 * Mark one line in ScreenLines invalid by setting the attributes to an
6779 * invalid value.
6780 */
6781 static void
6782lineinvalid(off, width)
6783 unsigned off;
6784 int width;
6785{
6786 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
6787}
6788
6789#ifdef FEAT_VERTSPLIT
6790/*
6791 * Copy part of a Screenline for vertically split window "wp".
6792 */
6793 static void
6794linecopy(to, from, wp)
6795 int to;
6796 int from;
6797 win_T *wp;
6798{
6799 unsigned off_to = LineOffset[to] + wp->w_wincol;
6800 unsigned off_from = LineOffset[from] + wp->w_wincol;
6801
6802 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
6803 wp->w_width * sizeof(schar_T));
6804# ifdef FEAT_MBYTE
6805 if (enc_utf8)
6806 {
6807 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
6808 wp->w_width * sizeof(u8char_T));
6809 mch_memmove(ScreenLinesC1 + off_to, ScreenLinesC1 + off_from,
6810 wp->w_width * sizeof(u8char_T));
6811 mch_memmove(ScreenLinesC2 + off_to, ScreenLinesC2 + off_from,
6812 wp->w_width * sizeof(u8char_T));
6813 }
6814 if (enc_dbcs == DBCS_JPNU)
6815 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
6816 wp->w_width * sizeof(schar_T));
6817# endif
6818 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
6819 wp->w_width * sizeof(sattr_T));
6820}
6821#endif
6822
6823/*
6824 * Return TRUE if clearing with term string "p" would work.
6825 * It can't work when the string is empty or it won't set the right background.
6826 */
6827 int
6828can_clear(p)
6829 char_u *p;
6830{
6831 return (*p != NUL && (t_colors <= 1
6832#ifdef FEAT_GUI
6833 || gui.in_use
6834#endif
6835 || cterm_normal_bg_color == 0 || *T_UT != NUL));
6836}
6837
6838/*
6839 * Reset cursor position. Use whenever cursor was moved because of outputting
6840 * something directly to the screen (shell commands) or a terminal control
6841 * code.
6842 */
6843 void
6844screen_start()
6845{
6846 screen_cur_row = screen_cur_col = 9999;
6847}
6848
6849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 * Move the cursor to position "row","col" in the screen.
6851 * This tries to find the most efficient way to move, minimizing the number of
6852 * characters sent to the terminal.
6853 */
6854 void
6855windgoto(row, col)
6856 int row;
6857 int col;
6858{
6859 char_u *p;
6860 int i;
6861 int plan;
6862 int cost;
6863 int wouldbe_col;
6864 int noinvcurs;
6865 char_u *bs;
6866 int goto_cost;
6867 int attr;
6868
6869#define GOTO_COST 7 /* asssume a term_windgoto() takes about 7 chars */
6870#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
6871
6872#define PLAN_LE 1
6873#define PLAN_CR 2
6874#define PLAN_NL 3
6875#define PLAN_WRITE 4
6876 /* Can't use ScreenLines unless initialized */
6877 if (ScreenLines == NULL)
6878 return;
6879
6880 if (col != screen_cur_col || row != screen_cur_row)
6881 {
6882 /* Check for valid position. */
6883 if (row < 0) /* window without text lines? */
6884 row = 0;
6885 if (row >= screen_Rows)
6886 row = screen_Rows - 1;
6887 if (col >= screen_Columns)
6888 col = screen_Columns - 1;
6889
6890 /* check if no cursor movement is allowed in highlight mode */
6891 if (screen_attr && *T_MS == NUL)
6892 noinvcurs = HIGHL_COST;
6893 else
6894 noinvcurs = 0;
6895 goto_cost = GOTO_COST + noinvcurs;
6896
6897 /*
6898 * Plan how to do the positioning:
6899 * 1. Use CR to move it to column 0, same row.
6900 * 2. Use T_LE to move it a few columns to the left.
6901 * 3. Use NL to move a few lines down, column 0.
6902 * 4. Move a few columns to the right with T_ND or by writing chars.
6903 *
6904 * Don't do this if the cursor went beyond the last column, the cursor
6905 * position is unknown then (some terminals wrap, some don't )
6906 *
6907 * First check if the highlighting attibutes allow us to write
6908 * characters to move the cursor to the right.
6909 */
6910 if (row >= screen_cur_row && screen_cur_col < Columns)
6911 {
6912 /*
6913 * If the cursor is in the same row, bigger col, we can use CR
6914 * or T_LE.
6915 */
6916 bs = NULL; /* init for GCC */
6917 attr = screen_attr;
6918 if (row == screen_cur_row && col < screen_cur_col)
6919 {
6920 /* "le" is preferred over "bc", because "bc" is obsolete */
6921 if (*T_LE)
6922 bs = T_LE; /* "cursor left" */
6923 else
6924 bs = T_BC; /* "backspace character (old) */
6925 if (*bs)
6926 cost = (screen_cur_col - col) * (int)STRLEN(bs);
6927 else
6928 cost = 999;
6929 if (col + 1 < cost) /* using CR is less characters */
6930 {
6931 plan = PLAN_CR;
6932 wouldbe_col = 0;
6933 cost = 1; /* CR is just one character */
6934 }
6935 else
6936 {
6937 plan = PLAN_LE;
6938 wouldbe_col = col;
6939 }
6940 if (noinvcurs) /* will stop highlighting */
6941 {
6942 cost += noinvcurs;
6943 attr = 0;
6944 }
6945 }
6946
6947 /*
6948 * If the cursor is above where we want to be, we can use CR LF.
6949 */
6950 else if (row > screen_cur_row)
6951 {
6952 plan = PLAN_NL;
6953 wouldbe_col = 0;
6954 cost = (row - screen_cur_row) * 2; /* CR LF */
6955 if (noinvcurs) /* will stop highlighting */
6956 {
6957 cost += noinvcurs;
6958 attr = 0;
6959 }
6960 }
6961
6962 /*
6963 * If the cursor is in the same row, smaller col, just use write.
6964 */
6965 else
6966 {
6967 plan = PLAN_WRITE;
6968 wouldbe_col = screen_cur_col;
6969 cost = 0;
6970 }
6971
6972 /*
6973 * Check if any characters that need to be written have the
6974 * correct attributes. Also avoid UTF-8 characters.
6975 */
6976 i = col - wouldbe_col;
6977 if (i > 0)
6978 cost += i;
6979 if (cost < goto_cost && i > 0)
6980 {
6981 /*
6982 * Check if the attributes are correct without additionally
6983 * stopping highlighting.
6984 */
6985 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
6986 while (i && *p++ == attr)
6987 --i;
6988 if (i != 0)
6989 {
6990 /*
6991 * Try if it works when highlighting is stopped here.
6992 */
6993 if (*--p == 0)
6994 {
6995 cost += noinvcurs;
6996 while (i && *p++ == 0)
6997 --i;
6998 }
6999 if (i != 0)
7000 cost = 999; /* different attributes, don't do it */
7001 }
7002#ifdef FEAT_MBYTE
7003 if (enc_utf8)
7004 {
7005 /* Don't use an UTF-8 char for positioning, it's slow. */
7006 for (i = wouldbe_col; i < col; ++i)
7007 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7008 {
7009 cost = 999;
7010 break;
7011 }
7012 }
7013#endif
7014 }
7015
7016 /*
7017 * We can do it without term_windgoto()!
7018 */
7019 if (cost < goto_cost)
7020 {
7021 if (plan == PLAN_LE)
7022 {
7023 if (noinvcurs)
7024 screen_stop_highlight();
7025 while (screen_cur_col > col)
7026 {
7027 out_str(bs);
7028 --screen_cur_col;
7029 }
7030 }
7031 else if (plan == PLAN_CR)
7032 {
7033 if (noinvcurs)
7034 screen_stop_highlight();
7035 out_char('\r');
7036 screen_cur_col = 0;
7037 }
7038 else if (plan == PLAN_NL)
7039 {
7040 if (noinvcurs)
7041 screen_stop_highlight();
7042 while (screen_cur_row < row)
7043 {
7044 out_char('\n');
7045 ++screen_cur_row;
7046 }
7047 screen_cur_col = 0;
7048 }
7049
7050 i = col - screen_cur_col;
7051 if (i > 0)
7052 {
7053 /*
7054 * Use cursor-right if it's one character only. Avoids
7055 * removing a line of pixels from the last bold char, when
7056 * using the bold trick in the GUI.
7057 */
7058 if (T_ND[0] != NUL && T_ND[1] == NUL)
7059 {
7060 while (i-- > 0)
7061 out_char(*T_ND);
7062 }
7063 else
7064 {
7065 int off;
7066
7067 off = LineOffset[row] + screen_cur_col;
7068 while (i-- > 0)
7069 {
7070 if (ScreenAttrs[off] != screen_attr)
7071 screen_stop_highlight();
7072#ifdef FEAT_MBYTE
7073 out_flush_check();
7074#endif
7075 out_char(ScreenLines[off]);
7076#ifdef FEAT_MBYTE
7077 if (enc_dbcs == DBCS_JPNU
7078 && ScreenLines[off] == 0x8e)
7079 out_char(ScreenLines2[off]);
7080#endif
7081 ++off;
7082 }
7083 }
7084 }
7085 }
7086 }
7087 else
7088 cost = 999;
7089
7090 if (cost >= goto_cost)
7091 {
7092 if (noinvcurs)
7093 screen_stop_highlight();
7094 if (row == screen_cur_row && (col > screen_cur_col) &&
7095 *T_CRI != NUL)
7096 term_cursor_right(col - screen_cur_col);
7097 else
7098 term_windgoto(row, col);
7099 }
7100 screen_cur_row = row;
7101 screen_cur_col = col;
7102 }
7103}
7104
7105/*
7106 * Set cursor to its position in the current window.
7107 */
7108 void
7109setcursor()
7110{
7111 if (redrawing())
7112 {
7113 validate_cursor();
7114 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7115 W_WINCOL(curwin) + (
7116#ifdef FEAT_RIGHTLEFT
7117 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7118# ifdef FEAT_MBYTE
7119 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7120# endif
7121 1)) :
7122#endif
7123 curwin->w_wcol));
7124 }
7125}
7126
7127
7128/*
7129 * insert 'line_count' lines at 'row' in window 'wp'
7130 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7131 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7132 * scrolling.
7133 * Returns FAIL if the lines are not inserted, OK for success.
7134 */
7135 int
7136win_ins_lines(wp, row, line_count, invalid, mayclear)
7137 win_T *wp;
7138 int row;
7139 int line_count;
7140 int invalid;
7141 int mayclear;
7142{
7143 int did_delete;
7144 int nextrow;
7145 int lastrow;
7146 int retval;
7147
7148 if (invalid)
7149 wp->w_lines_valid = 0;
7150
7151 if (wp->w_height < 5)
7152 return FAIL;
7153
7154 if (line_count > wp->w_height - row)
7155 line_count = wp->w_height - row;
7156
7157 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7158 if (retval != MAYBE)
7159 return retval;
7160
7161 /*
7162 * If there is a next window or a status line, we first try to delete the
7163 * lines at the bottom to avoid messing what is after the window.
7164 * If this fails and there are following windows, don't do anything to avoid
7165 * messing up those windows, better just redraw.
7166 */
7167 did_delete = FALSE;
7168#ifdef FEAT_WINDOWS
7169 if (wp->w_next != NULL || wp->w_status_height)
7170 {
7171 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7172 line_count, (int)Rows, FALSE, NULL) == OK)
7173 did_delete = TRUE;
7174 else if (wp->w_next)
7175 return FAIL;
7176 }
7177#endif
7178 /*
7179 * if no lines deleted, blank the lines that will end up below the window
7180 */
7181 if (!did_delete)
7182 {
7183#ifdef FEAT_WINDOWS
7184 wp->w_redr_status = TRUE;
7185#endif
7186 redraw_cmdline = TRUE;
7187 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7188 lastrow = nextrow + line_count;
7189 if (lastrow > Rows)
7190 lastrow = Rows;
7191 screen_fill(nextrow - line_count, lastrow - line_count,
7192 W_WINCOL(wp), (int)W_ENDCOL(wp),
7193 ' ', ' ', 0);
7194 }
7195
7196 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7197 == FAIL)
7198 {
7199 /* deletion will have messed up other windows */
7200 if (did_delete)
7201 {
7202#ifdef FEAT_WINDOWS
7203 wp->w_redr_status = TRUE;
7204#endif
7205 win_rest_invalid(W_NEXT(wp));
7206 }
7207 return FAIL;
7208 }
7209
7210 return OK;
7211}
7212
7213/*
7214 * delete "line_count" window lines at "row" in window "wp"
7215 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7216 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7217 * scrolling
7218 * Return OK for success, FAIL if the lines are not deleted.
7219 */
7220 int
7221win_del_lines(wp, row, line_count, invalid, mayclear)
7222 win_T *wp;
7223 int row;
7224 int line_count;
7225 int invalid;
7226 int mayclear;
7227{
7228 int retval;
7229
7230 if (invalid)
7231 wp->w_lines_valid = 0;
7232
7233 if (line_count > wp->w_height - row)
7234 line_count = wp->w_height - row;
7235
7236 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7237 if (retval != MAYBE)
7238 return retval;
7239
7240 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7241 (int)Rows, FALSE, NULL) == FAIL)
7242 return FAIL;
7243
7244#ifdef FEAT_WINDOWS
7245 /*
7246 * If there are windows or status lines below, try to put them at the
7247 * correct place. If we can't do that, they have to be redrawn.
7248 */
7249 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
7250 {
7251 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7252 line_count, (int)Rows, NULL) == FAIL)
7253 {
7254 wp->w_redr_status = TRUE;
7255 win_rest_invalid(wp->w_next);
7256 }
7257 }
7258 /*
7259 * If this is the last window and there is no status line, redraw the
7260 * command line later.
7261 */
7262 else
7263#endif
7264 redraw_cmdline = TRUE;
7265 return OK;
7266}
7267
7268/*
7269 * Common code for win_ins_lines() and win_del_lines().
7270 * Returns OK or FAIL when the work has been done.
7271 * Returns MAYBE when not finished yet.
7272 */
7273 static int
7274win_do_lines(wp, row, line_count, mayclear, del)
7275 win_T *wp;
7276 int row;
7277 int line_count;
7278 int mayclear;
7279 int del;
7280{
7281 int retval;
7282
7283 if (!redrawing() || line_count <= 0)
7284 return FAIL;
7285
7286 /* only a few lines left: redraw is faster */
7287 if (mayclear && Rows - line_count < 5
7288#ifdef FEAT_VERTSPLIT
7289 && wp->w_width == Columns
7290#endif
7291 )
7292 {
7293 screenclear(); /* will set wp->w_lines_valid to 0 */
7294 return FAIL;
7295 }
7296
7297 /*
7298 * Delete all remaining lines
7299 */
7300 if (row + line_count >= wp->w_height)
7301 {
7302 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
7303 W_WINCOL(wp), (int)W_ENDCOL(wp),
7304 ' ', ' ', 0);
7305 return OK;
7306 }
7307
7308 /*
7309 * when scrolling, the message on the command line should be cleared,
7310 * otherwise it will stay there forever.
7311 */
7312 clear_cmdline = TRUE;
7313
7314 /*
7315 * If the terminal can set a scroll region, use that.
7316 * Always do this in a vertically split window. This will redraw from
7317 * ScreenLines[] when t_CV isn't defined. That's faster than using
7318 * win_line().
7319 * Don't use a scroll region when we are going to redraw the text, writing
7320 * a character in the lower right corner of the scroll region causes a
7321 * scroll-up in the DJGPP version.
7322 */
7323 if (scroll_region
7324#ifdef FEAT_VERTSPLIT
7325 || W_WIDTH(wp) != Columns
7326#endif
7327 )
7328 {
7329#ifdef FEAT_VERTSPLIT
7330 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
7331#endif
7332 scroll_region_set(wp, row);
7333 if (del)
7334 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
7335 wp->w_height - row, FALSE, wp);
7336 else
7337 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
7338 wp->w_height - row, wp);
7339#ifdef FEAT_VERTSPLIT
7340 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
7341#endif
7342 scroll_region_reset();
7343 return retval;
7344 }
7345
7346#ifdef FEAT_WINDOWS
7347 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
7348 return FAIL;
7349#endif
7350
7351 return MAYBE;
7352}
7353
7354/*
7355 * window 'wp' and everything after it is messed up, mark it for redraw
7356 */
7357 static void
7358win_rest_invalid(wp)
7359 win_T *wp;
7360{
7361#ifdef FEAT_WINDOWS
7362 while (wp != NULL)
7363#else
7364 if (wp != NULL)
7365#endif
7366 {
7367 redraw_win_later(wp, NOT_VALID);
7368#ifdef FEAT_WINDOWS
7369 wp->w_redr_status = TRUE;
7370 wp = wp->w_next;
7371#endif
7372 }
7373 redraw_cmdline = TRUE;
7374}
7375
7376/*
7377 * The rest of the routines in this file perform screen manipulations. The
7378 * given operation is performed physically on the screen. The corresponding
7379 * change is also made to the internal screen image. In this way, the editor
7380 * anticipates the effect of editing changes on the appearance of the screen.
7381 * That way, when we call screenupdate a complete redraw isn't usually
7382 * necessary. Another advantage is that we can keep adding code to anticipate
7383 * screen changes, and in the meantime, everything still works.
7384 */
7385
7386/*
7387 * types for inserting or deleting lines
7388 */
7389#define USE_T_CAL 1
7390#define USE_T_CDL 2
7391#define USE_T_AL 3
7392#define USE_T_CE 4
7393#define USE_T_DL 5
7394#define USE_T_SR 6
7395#define USE_NL 7
7396#define USE_T_CD 8
7397#define USE_REDRAW 9
7398
7399/*
7400 * insert lines on the screen and update ScreenLines[]
7401 * 'end' is the line after the scrolled part. Normally it is Rows.
7402 * When scrolling region used 'off' is the offset from the top for the region.
7403 * 'row' and 'end' are relative to the start of the region.
7404 *
7405 * return FAIL for failure, OK for success.
7406 */
7407 static int
7408screen_ins_lines(off, row, line_count, end, wp)
7409 int off;
7410 int row;
7411 int line_count;
7412 int end;
7413 win_T *wp; /* NULL or window to use width from */
7414{
7415 int i;
7416 int j;
7417 unsigned temp;
7418 int cursor_row;
7419 int type;
7420 int result_empty;
7421 int can_ce = can_clear(T_CE);
7422
7423 /*
7424 * FAIL if
7425 * - there is no valid screen
7426 * - the screen has to be redrawn completely
7427 * - the line count is less than one
7428 * - the line count is more than 'ttyscroll'
7429 */
7430 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
7431 return FAIL;
7432
7433 /*
7434 * There are seven ways to insert lines:
7435 * 0. When in a vertically split window and t_CV isn't set, redraw the
7436 * characters from ScreenLines[].
7437 * 1. Use T_CD (clear to end of display) if it exists and the result of
7438 * the insert is just empty lines
7439 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
7440 * present or line_count > 1. It looks better if we do all the inserts
7441 * at once.
7442 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
7443 * insert is just empty lines and T_CE is not present or line_count >
7444 * 1.
7445 * 4. Use T_AL (insert line) if it exists.
7446 * 5. Use T_CE (erase line) if it exists and the result of the insert is
7447 * just empty lines.
7448 * 6. Use T_DL (delete line) if it exists and the result of the insert is
7449 * just empty lines.
7450 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
7451 * the 'da' flag is not set or we have clear line capability.
7452 * 8. redraw the characters from ScreenLines[].
7453 *
7454 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
7455 * the scrollbar for the window. It does have insert line, use that if it
7456 * exists.
7457 */
7458 result_empty = (row + line_count >= end);
7459#ifdef FEAT_VERTSPLIT
7460 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
7461 type = USE_REDRAW;
7462 else
7463#endif
7464 if (can_clear(T_CD) && result_empty)
7465 type = USE_T_CD;
7466 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
7467 type = USE_T_CAL;
7468 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
7469 type = USE_T_CDL;
7470 else if (*T_AL != NUL)
7471 type = USE_T_AL;
7472 else if (can_ce && result_empty)
7473 type = USE_T_CE;
7474 else if (*T_DL != NUL && result_empty)
7475 type = USE_T_DL;
7476 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
7477 type = USE_T_SR;
7478 else
7479 return FAIL;
7480
7481 /*
7482 * For clearing the lines screen_del_lines() is used. This will also take
7483 * care of t_db if necessary.
7484 */
7485 if (type == USE_T_CD || type == USE_T_CDL ||
7486 type == USE_T_CE || type == USE_T_DL)
7487 return screen_del_lines(off, row, line_count, end, FALSE, wp);
7488
7489 /*
7490 * If text is retained below the screen, first clear or delete as many
7491 * lines at the bottom of the window as are about to be inserted so that
7492 * the deleted lines won't later surface during a screen_del_lines.
7493 */
7494 if (*T_DB)
7495 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
7496
7497#ifdef FEAT_CLIPBOARD
7498 /* Remove a modeless selection when inserting lines halfway the screen
7499 * or not the full width of the screen. */
7500 if (off + row > 0
7501# ifdef FEAT_VERTSPLIT
7502 || (wp != NULL && wp->w_width != Columns)
7503# endif
7504 )
7505 clip_clear_selection();
7506 else
7507 clip_scroll_selection(-line_count);
7508#endif
7509
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510#ifdef FEAT_GUI
7511 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
7512 * scrolling is actually carried out. */
7513 gui_dont_update_cursor();
7514#endif
7515
7516 if (*T_CCS != NUL) /* cursor relative to region */
7517 cursor_row = row;
7518 else
7519 cursor_row = row + off;
7520
7521 /*
7522 * Shift LineOffset[] line_count down to reflect the inserted lines.
7523 * Clear the inserted lines in ScreenLines[].
7524 */
7525 row += off;
7526 end += off;
7527 for (i = 0; i < line_count; ++i)
7528 {
7529#ifdef FEAT_VERTSPLIT
7530 if (wp != NULL && wp->w_width != Columns)
7531 {
7532 /* need to copy part of a line */
7533 j = end - 1 - i;
7534 while ((j -= line_count) >= row)
7535 linecopy(j + line_count, j, wp);
7536 j += line_count;
7537 if (can_clear((char_u *)" "))
7538 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
7539 else
7540 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
7541 LineWraps[j] = FALSE;
7542 }
7543 else
7544#endif
7545 {
7546 j = end - 1 - i;
7547 temp = LineOffset[j];
7548 while ((j -= line_count) >= row)
7549 {
7550 LineOffset[j + line_count] = LineOffset[j];
7551 LineWraps[j + line_count] = LineWraps[j];
7552 }
7553 LineOffset[j + line_count] = temp;
7554 LineWraps[j + line_count] = FALSE;
7555 if (can_clear((char_u *)" "))
7556 lineclear(temp, (int)Columns);
7557 else
7558 lineinvalid(temp, (int)Columns);
7559 }
7560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561
7562 screen_stop_highlight();
7563 windgoto(cursor_row, 0);
7564
7565#ifdef FEAT_VERTSPLIT
7566 /* redraw the characters */
7567 if (type == USE_REDRAW)
7568 redraw_block(row, end, wp);
7569 else
7570#endif
7571 if (type == USE_T_CAL)
7572 {
7573 term_append_lines(line_count);
7574 screen_start(); /* don't know where cursor is now */
7575 }
7576 else
7577 {
7578 for (i = 0; i < line_count; i++)
7579 {
7580 if (type == USE_T_AL)
7581 {
7582 if (i && cursor_row != 0)
7583 windgoto(cursor_row, 0);
7584 out_str(T_AL);
7585 }
7586 else /* type == USE_T_SR */
7587 out_str(T_SR);
7588 screen_start(); /* don't know where cursor is now */
7589 }
7590 }
7591
7592 /*
7593 * With scroll-reverse and 'da' flag set we need to clear the lines that
7594 * have been scrolled down into the region.
7595 */
7596 if (type == USE_T_SR && *T_DA)
7597 {
7598 for (i = 0; i < line_count; ++i)
7599 {
7600 windgoto(off + i, 0);
7601 out_str(T_CE);
7602 screen_start(); /* don't know where cursor is now */
7603 }
7604 }
7605
7606#ifdef FEAT_GUI
7607 gui_can_update_cursor();
7608 if (gui.in_use)
7609 out_flush(); /* always flush after a scroll */
7610#endif
7611 return OK;
7612}
7613
7614/*
7615 * delete lines on the screen and update ScreenLines[]
7616 * 'end' is the line after the scrolled part. Normally it is Rows.
7617 * When scrolling region used 'off' is the offset from the top for the region.
7618 * 'row' and 'end' are relative to the start of the region.
7619 *
7620 * Return OK for success, FAIL if the lines are not deleted.
7621 */
7622/*ARGSUSED*/
7623 int
7624screen_del_lines(off, row, line_count, end, force, wp)
7625 int off;
7626 int row;
7627 int line_count;
7628 int end;
7629 int force; /* even when line_count > p_ttyscroll */
7630 win_T *wp; /* NULL or window to use width from */
7631{
7632 int j;
7633 int i;
7634 unsigned temp;
7635 int cursor_row;
7636 int cursor_end;
7637 int result_empty; /* result is empty until end of region */
7638 int can_delete; /* deleting line codes can be used */
7639 int type;
7640
7641 /*
7642 * FAIL if
7643 * - there is no valid screen
7644 * - the screen has to be redrawn completely
7645 * - the line count is less than one
7646 * - the line count is more than 'ttyscroll'
7647 */
7648 if (!screen_valid(TRUE) || line_count <= 0 ||
7649 (!force && line_count > p_ttyscroll))
7650 return FAIL;
7651
7652 /*
7653 * Check if the rest of the current region will become empty.
7654 */
7655 result_empty = row + line_count >= end;
7656
7657 /*
7658 * We can delete lines only when 'db' flag not set or when 'ce' option
7659 * available.
7660 */
7661 can_delete = (*T_DB == NUL || can_clear(T_CE));
7662
7663 /*
7664 * There are six ways to delete lines:
7665 * 0. When in a vertically split window and t_CV isn't set, redraw the
7666 * characters from ScreenLines[].
7667 * 1. Use T_CD if it exists and the result is empty.
7668 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
7669 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
7670 * none of the other ways work.
7671 * 4. Use T_CE (erase line) if the result is empty.
7672 * 5. Use T_DL (delete line) if it exists.
7673 * 6. redraw the characters from ScreenLines[].
7674 */
7675#ifdef FEAT_VERTSPLIT
7676 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
7677 type = USE_REDRAW;
7678 else
7679#endif
7680 if (can_clear(T_CD) && result_empty)
7681 type = USE_T_CD;
7682#if defined(__BEOS__) && defined(BEOS_DR8)
7683 /*
7684 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
7685 * its internal termcap... this works okay for tests which test *T_DB !=
7686 * NUL. It has the disadvantage that the user cannot use any :set t_*
7687 * command to get T_DB (back) to empty_option, only :set term=... will do
7688 * the trick...
7689 * Anyway, this hack will hopefully go away with the next OS release.
7690 * (Olaf Seibert)
7691 */
7692 else if (row == 0 && T_DB == empty_option
7693 && (line_count == 1 || *T_CDL == NUL))
7694#else
7695 else if (row == 0 && (
7696#ifndef AMIGA
7697 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
7698 * up, so use delete-line command */
7699 line_count == 1 ||
7700#endif
7701 *T_CDL == NUL))
7702#endif
7703 type = USE_NL;
7704 else if (*T_CDL != NUL && line_count > 1 && can_delete)
7705 type = USE_T_CDL;
7706 else if (can_clear(T_CE) && result_empty
7707#ifdef FEAT_VERTSPLIT
7708 && (wp == NULL || wp->w_width == Columns)
7709#endif
7710 )
7711 type = USE_T_CE;
7712 else if (*T_DL != NUL && can_delete)
7713 type = USE_T_DL;
7714 else if (*T_CDL != NUL && can_delete)
7715 type = USE_T_CDL;
7716 else
7717 return FAIL;
7718
7719#ifdef FEAT_CLIPBOARD
7720 /* Remove a modeless selection when deleting lines halfway the screen or
7721 * not the full width of the screen. */
7722 if (off + row > 0
7723# ifdef FEAT_VERTSPLIT
7724 || (wp != NULL && wp->w_width != Columns)
7725# endif
7726 )
7727 clip_clear_selection();
7728 else
7729 clip_scroll_selection(line_count);
7730#endif
7731
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732#ifdef FEAT_GUI
7733 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
7734 * scrolling is actually carried out. */
7735 gui_dont_update_cursor();
7736#endif
7737
7738 if (*T_CCS != NUL) /* cursor relative to region */
7739 {
7740 cursor_row = row;
7741 cursor_end = end;
7742 }
7743 else
7744 {
7745 cursor_row = row + off;
7746 cursor_end = end + off;
7747 }
7748
7749 /*
7750 * Now shift LineOffset[] line_count up to reflect the deleted lines.
7751 * Clear the inserted lines in ScreenLines[].
7752 */
7753 row += off;
7754 end += off;
7755 for (i = 0; i < line_count; ++i)
7756 {
7757#ifdef FEAT_VERTSPLIT
7758 if (wp != NULL && wp->w_width != Columns)
7759 {
7760 /* need to copy part of a line */
7761 j = row + i;
7762 while ((j += line_count) <= end - 1)
7763 linecopy(j - line_count, j, wp);
7764 j -= line_count;
7765 if (can_clear((char_u *)" "))
7766 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
7767 else
7768 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
7769 LineWraps[j] = FALSE;
7770 }
7771 else
7772#endif
7773 {
7774 /* whole width, moving the line pointers is faster */
7775 j = row + i;
7776 temp = LineOffset[j];
7777 while ((j += line_count) <= end - 1)
7778 {
7779 LineOffset[j - line_count] = LineOffset[j];
7780 LineWraps[j - line_count] = LineWraps[j];
7781 }
7782 LineOffset[j - line_count] = temp;
7783 LineWraps[j - line_count] = FALSE;
7784 if (can_clear((char_u *)" "))
7785 lineclear(temp, (int)Columns);
7786 else
7787 lineinvalid(temp, (int)Columns);
7788 }
7789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 screen_stop_highlight();
7792
7793#ifdef FEAT_VERTSPLIT
7794 /* redraw the characters */
7795 if (type == USE_REDRAW)
7796 redraw_block(row, end, wp);
7797 else
7798#endif
7799 if (type == USE_T_CD) /* delete the lines */
7800 {
7801 windgoto(cursor_row, 0);
7802 out_str(T_CD);
7803 screen_start(); /* don't know where cursor is now */
7804 }
7805 else if (type == USE_T_CDL)
7806 {
7807 windgoto(cursor_row, 0);
7808 term_delete_lines(line_count);
7809 screen_start(); /* don't know where cursor is now */
7810 }
7811 /*
7812 * Deleting lines at top of the screen or scroll region: Just scroll
7813 * the whole screen (scroll region) up by outputting newlines on the
7814 * last line.
7815 */
7816 else if (type == USE_NL)
7817 {
7818 windgoto(cursor_end - 1, 0);
7819 for (i = line_count; --i >= 0; )
7820 out_char('\n'); /* cursor will remain on same line */
7821 }
7822 else
7823 {
7824 for (i = line_count; --i >= 0; )
7825 {
7826 if (type == USE_T_DL)
7827 {
7828 windgoto(cursor_row, 0);
7829 out_str(T_DL); /* delete a line */
7830 }
7831 else /* type == USE_T_CE */
7832 {
7833 windgoto(cursor_row + i, 0);
7834 out_str(T_CE); /* erase a line */
7835 }
7836 screen_start(); /* don't know where cursor is now */
7837 }
7838 }
7839
7840 /*
7841 * If the 'db' flag is set, we need to clear the lines that have been
7842 * scrolled up at the bottom of the region.
7843 */
7844 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
7845 {
7846 for (i = line_count; i > 0; --i)
7847 {
7848 windgoto(cursor_end - i, 0);
7849 out_str(T_CE); /* erase a line */
7850 screen_start(); /* don't know where cursor is now */
7851 }
7852 }
7853
7854#ifdef FEAT_GUI
7855 gui_can_update_cursor();
7856 if (gui.in_use)
7857 out_flush(); /* always flush after a scroll */
7858#endif
7859
7860 return OK;
7861}
7862
7863/*
7864 * show the current mode and ruler
7865 *
7866 * If clear_cmdline is TRUE, clear the rest of the cmdline.
7867 * If clear_cmdline is FALSE there may be a message there that needs to be
7868 * cleared only if a mode is shown.
7869 * Return the length of the message (0 if no message).
7870 */
7871 int
7872showmode()
7873{
7874 int need_clear;
7875 int length = 0;
7876 int do_mode;
7877 int attr;
7878 int nwr_save;
7879#ifdef FEAT_INS_EXPAND
7880 int sub_attr;
7881#endif
7882
7883 do_mode = (p_smd && ((State & INSERT) || restart_edit
7884#ifdef FEAT_VISUAL
7885 || VIsual_active
7886#endif
7887 ));
7888 if (do_mode || Recording)
7889 {
7890 /*
7891 * Don't show mode right now, when not redrawing or inside a mapping.
7892 * Call char_avail() only when we are going to show something, because
7893 * it takes a bit of time.
7894 */
7895 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
7896 {
7897 redraw_cmdline = TRUE; /* show mode later */
7898 return 0;
7899 }
7900
7901 nwr_save = need_wait_return;
7902
7903 /* wait a bit before overwriting an important message */
7904 check_for_delay(FALSE);
7905
7906 /* if the cmdline is more than one line high, erase top lines */
7907 need_clear = clear_cmdline;
7908 if (clear_cmdline && cmdline_row < Rows - 1)
7909 msg_clr_cmdline(); /* will reset clear_cmdline */
7910
7911 /* Position on the last line in the window, column 0 */
7912 msg_pos_mode();
7913 cursor_off();
7914 attr = hl_attr(HLF_CM); /* Highlight mode */
7915 if (do_mode)
7916 {
7917 MSG_PUTS_ATTR("--", attr);
7918#if defined(FEAT_XIM)
7919 if (xic != NULL && im_get_status() && !p_imdisable
7920 && curbuf->b_p_iminsert == B_IMODE_IM)
7921# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
7922 MSG_PUTS_ATTR(" IM", attr);
7923# else
7924 MSG_PUTS_ATTR(" XIM", attr);
7925# endif
7926#endif
7927#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
7928 if (gui.in_use)
7929 {
7930 if (hangul_input_state_get())
7931 MSG_PUTS_ATTR(" ÇѱÛ", attr); /* HANGUL */
7932 }
7933#endif
7934#ifdef FEAT_INS_EXPAND
7935 if (edit_submode != NULL) /* CTRL-X in Insert mode */
7936 {
7937 /* These messages can get long, avoid a wrap in a narrow
7938 * window. Prefer showing edit_submode_extra. */
7939 length = (Rows - msg_row) * Columns - 3;
7940 if (edit_submode_extra != NULL)
7941 length -= vim_strsize(edit_submode_extra);
7942 if (length > 0)
7943 {
7944 if (edit_submode_pre != NULL)
7945 length -= vim_strsize(edit_submode_pre);
7946 if (length - vim_strsize(edit_submode) > 0)
7947 {
7948 if (edit_submode_pre != NULL)
7949 msg_puts_attr(edit_submode_pre, attr);
7950 msg_puts_attr(edit_submode, attr);
7951 }
7952 if (edit_submode_extra != NULL)
7953 {
7954 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
7955 if ((int)edit_submode_highl < (int)HLF_COUNT)
7956 sub_attr = hl_attr(edit_submode_highl);
7957 else
7958 sub_attr = attr;
7959 msg_puts_attr(edit_submode_extra, sub_attr);
7960 }
7961 }
7962 length = 0;
7963 }
7964 else
7965#endif
7966 {
7967#ifdef FEAT_VREPLACE
7968 if (State & VREPLACE_FLAG)
7969 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
7970 else
7971#endif
7972 if (State & REPLACE_FLAG)
7973 MSG_PUTS_ATTR(_(" REPLACE"), attr);
7974 else if (State & INSERT)
7975 {
7976#ifdef FEAT_RIGHTLEFT
7977 if (p_ri)
7978 MSG_PUTS_ATTR(_(" REVERSE"), attr);
7979#endif
7980 MSG_PUTS_ATTR(_(" INSERT"), attr);
7981 }
7982 else if (restart_edit == 'I')
7983 MSG_PUTS_ATTR(_(" (insert)"), attr);
7984 else if (restart_edit == 'R')
7985 MSG_PUTS_ATTR(_(" (replace)"), attr);
7986 else if (restart_edit == 'V')
7987 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
7988#ifdef FEAT_RIGHTLEFT
7989 if (p_hkmap)
7990 MSG_PUTS_ATTR(_(" Hebrew"), attr);
7991# ifdef FEAT_FKMAP
7992 if (p_fkmap)
7993 MSG_PUTS_ATTR(farsi_text_5, attr);
7994# endif
7995#endif
7996#ifdef FEAT_KEYMAP
7997 if (State & LANGMAP)
7998 {
7999# ifdef FEAT_ARABIC
8000 if (curwin->w_p_arab)
8001 MSG_PUTS_ATTR(_(" Arabic"), attr);
8002 else
8003# endif
8004 MSG_PUTS_ATTR(_(" (lang)"), attr);
8005 }
8006#endif
8007 if ((State & INSERT) && p_paste)
8008 MSG_PUTS_ATTR(_(" (paste)"), attr);
8009
8010#ifdef FEAT_VISUAL
8011 if (VIsual_active)
8012 {
8013 char *p;
8014
8015 /* Don't concatenate separate words to avoid translation
8016 * problems. */
8017 switch ((VIsual_select ? 4 : 0)
8018 + (VIsual_mode == Ctrl_V) * 2
8019 + (VIsual_mode == 'V'))
8020 {
8021 case 0: p = N_(" VISUAL"); break;
8022 case 1: p = N_(" VISUAL LINE"); break;
8023 case 2: p = N_(" VISUAL BLOCK"); break;
8024 case 4: p = N_(" SELECT"); break;
8025 case 5: p = N_(" SELECT LINE"); break;
8026 default: p = N_(" SELECT BLOCK"); break;
8027 }
8028 MSG_PUTS_ATTR(_(p), attr);
8029 }
8030#endif
8031 MSG_PUTS_ATTR(" --", attr);
8032 }
8033 need_clear = TRUE;
8034 }
8035 if (Recording
8036#ifdef FEAT_INS_EXPAND
8037 && edit_submode == NULL /* otherwise it gets too long */
8038#endif
8039 )
8040 {
8041 MSG_PUTS_ATTR(_("recording"), attr);
8042 need_clear = TRUE;
8043 }
8044 if (need_clear || clear_cmdline)
8045 msg_clr_eos();
8046 msg_didout = FALSE; /* overwrite this message */
8047 length = msg_col;
8048 msg_col = 0;
8049 need_wait_return = nwr_save; /* never ask for hit-return for this */
8050 }
8051 else if (clear_cmdline && msg_silent == 0)
8052 /* Clear the whole command line. Will reset "clear_cmdline". */
8053 msg_clr_cmdline();
8054
8055#ifdef FEAT_CMDL_INFO
8056# ifdef FEAT_VISUAL
8057 /* In Visual mode the size of the selected area must be redrawn. */
8058 if (VIsual_active)
8059 clear_showcmd();
8060# endif
8061
8062 /* If the last window has no status line, the ruler is after the mode
8063 * message and must be redrawn */
8064 if (redrawing()
8065# ifdef FEAT_WINDOWS
8066 && lastwin->w_status_height == 0
8067# endif
8068 )
8069 win_redr_ruler(lastwin, TRUE);
8070#endif
8071 redraw_cmdline = FALSE;
8072 clear_cmdline = FALSE;
8073
8074 return length;
8075}
8076
8077/*
8078 * Position for a mode message.
8079 */
8080 static void
8081msg_pos_mode()
8082{
8083 msg_col = 0;
8084 msg_row = Rows - 1;
8085}
8086
8087/*
8088 * Delete mode message. Used when ESC is typed which is expected to end
8089 * Insert mode (but Insert mode didn't end yet!).
8090 */
8091 void
8092unshowmode(force)
8093 int force;
8094{
8095 /*
8096 * Don't delete it right now, when not redrawing or insided a mapping.
8097 */
8098 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8099 redraw_cmdline = TRUE; /* delete mode later */
8100 else
8101 {
8102 msg_pos_mode();
8103 if (Recording)
8104 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8105 msg_clr_eos();
8106 }
8107}
8108
8109#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
8110/*
8111 * Get the character to use in a status line. Get its attributes in "*attr".
8112 */
8113 static int
8114fillchar_status(attr, is_curwin)
8115 int *attr;
8116 int is_curwin;
8117{
8118 int fill;
8119 if (is_curwin)
8120 {
8121 *attr = hl_attr(HLF_S);
8122 fill = fill_stl;
8123 }
8124 else
8125 {
8126 *attr = hl_attr(HLF_SNC);
8127 fill = fill_stlnc;
8128 }
8129 /* Use fill when there is highlighting, and highlighting of current
8130 * window differs, or the fillchars differ, or this is not the
8131 * current window */
8132 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
8133 || !is_curwin || firstwin == lastwin)
8134 || (fill_stl != fill_stlnc)))
8135 return fill;
8136 if (is_curwin)
8137 return '^';
8138 return '=';
8139}
8140#endif
8141
8142#ifdef FEAT_VERTSPLIT
8143/*
8144 * Get the character to use in a separator between vertically split windows.
8145 * Get its attributes in "*attr".
8146 */
8147 static int
8148fillchar_vsep(attr)
8149 int *attr;
8150{
8151 *attr = hl_attr(HLF_C);
8152 if (*attr == 0 && fill_vert == ' ')
8153 return '|';
8154 else
8155 return fill_vert;
8156}
8157#endif
8158
8159/*
8160 * Return TRUE if redrawing should currently be done.
8161 */
8162 int
8163redrawing()
8164{
8165 return (!RedrawingDisabled
8166 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
8167}
8168
8169/*
8170 * Return TRUE if printing messages should currently be done.
8171 */
8172 int
8173messaging()
8174{
8175 return (!(p_lz && char_avail() && !KeyTyped));
8176}
8177
8178/*
8179 * Show current status info in ruler and various other places
8180 * If always is FALSE, only show ruler if position has changed.
8181 */
8182 void
8183showruler(always)
8184 int always;
8185{
8186 if (!always && !redrawing())
8187 return;
8188#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008189 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 win_redr_custom(curwin, FALSE);
8191 else
8192#endif
8193#ifdef FEAT_CMDL_INFO
8194 win_redr_ruler(curwin, always);
8195#endif
8196
8197#ifdef FEAT_TITLE
8198 if (need_maketitle
8199# ifdef FEAT_STL_OPT
8200 || (p_icon && (stl_syntax & STL_IN_ICON))
8201 || (p_title && (stl_syntax & STL_IN_TITLE))
8202# endif
8203 )
8204 maketitle();
8205#endif
8206}
8207
8208#ifdef FEAT_CMDL_INFO
8209 static void
8210win_redr_ruler(wp, always)
8211 win_T *wp;
8212 int always;
8213{
8214 char_u buffer[70];
8215 int row;
8216 int fillchar;
8217 int attr;
8218 int empty_line = FALSE;
8219 colnr_T virtcol;
8220 int i;
8221 int o;
8222#ifdef FEAT_VERTSPLIT
8223 int this_ru_col;
8224 int off = 0;
8225 int width = Columns;
8226# define WITH_OFF(x) x
8227# define WITH_WIDTH(x) x
8228#else
8229# define WITH_OFF(x) 0
8230# define WITH_WIDTH(x) Columns
8231# define this_ru_col ru_col
8232#endif
8233
8234 /* If 'ruler' off or redrawing disabled, don't do anything */
8235 if (!p_ru)
8236 return;
8237
8238 /*
8239 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
8240 * after deleting lines, before cursor.lnum is corrected.
8241 */
8242 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
8243 return;
8244
8245#ifdef FEAT_INS_EXPAND
8246 /* Don't draw the ruler while doing insert-completion, it might overwrite
8247 * the (long) mode message. */
8248# ifdef FEAT_WINDOWS
8249 if (wp == lastwin && lastwin->w_status_height == 0)
8250# endif
8251 if (edit_submode != NULL)
8252 return;
8253#endif
8254
8255#ifdef FEAT_STL_OPT
8256 if (*p_ruf)
8257 {
8258 win_redr_custom(wp, TRUE);
8259 return;
8260 }
8261#endif
8262
8263 /*
8264 * Check if not in Insert mode and the line is empty (will show "0-1").
8265 */
8266 if (!(State & INSERT)
8267 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
8268 empty_line = TRUE;
8269
8270 /*
8271 * Only draw the ruler when something changed.
8272 */
8273 validate_virtcol_win(wp);
8274 if ( redraw_cmdline
8275 || always
8276 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
8277 || wp->w_cursor.col != wp->w_ru_cursor.col
8278 || wp->w_virtcol != wp->w_ru_virtcol
8279#ifdef FEAT_VIRTUALEDIT
8280 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
8281#endif
8282 || wp->w_topline != wp->w_ru_topline
8283 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
8284#ifdef FEAT_DIFF
8285 || wp->w_topfill != wp->w_ru_topfill
8286#endif
8287 || empty_line != wp->w_ru_empty)
8288 {
8289 cursor_off();
8290#ifdef FEAT_WINDOWS
8291 if (wp->w_status_height)
8292 {
8293 row = W_WINROW(wp) + wp->w_height;
8294 fillchar = fillchar_status(&attr, wp == curwin);
8295# ifdef FEAT_VERTSPLIT
8296 off = W_WINCOL(wp);
8297 width = W_WIDTH(wp);
8298# endif
8299 }
8300 else
8301#endif
8302 {
8303 row = Rows - 1;
8304 fillchar = ' ';
8305 attr = 0;
8306#ifdef FEAT_VERTSPLIT
8307 width = Columns;
8308 off = 0;
8309#endif
8310 }
8311
8312 /* In list mode virtcol needs to be recomputed */
8313 virtcol = wp->w_virtcol;
8314 if (wp->w_p_list && lcs_tab1 == NUL)
8315 {
8316 wp->w_p_list = FALSE;
8317 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
8318 wp->w_p_list = TRUE;
8319 }
8320
8321 /*
8322 * Some sprintfs return the length, some return a pointer.
8323 * To avoid portability problems we use strlen() here.
8324 */
8325 sprintf((char *)buffer, "%ld,",
8326 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
8327 ? 0L
8328 : (long)(wp->w_cursor.lnum));
8329 col_print(buffer + STRLEN(buffer),
8330 empty_line ? 0 : (int)wp->w_cursor.col + 1,
8331 (int)virtcol + 1);
8332
8333 /*
8334 * Add a "50%" if there is room for it.
8335 * On the last line, don't print in the last column (scrolls the
8336 * screen up on some terminals).
8337 */
8338 i = (int)STRLEN(buffer);
8339 get_rel_pos(wp, buffer + i + 1);
8340 o = i + vim_strsize(buffer + i + 1);
8341#ifdef FEAT_WINDOWS
8342 if (wp->w_status_height == 0) /* can't use last char of screen */
8343#endif
8344 ++o;
8345#ifdef FEAT_VERTSPLIT
8346 this_ru_col = ru_col - (Columns - width);
8347 if (this_ru_col < 0)
8348 this_ru_col = 0;
8349#endif
8350 /* Never use more than half the window/screen width, leave the other
8351 * half for the filename. */
8352 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
8353 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
8354 if (this_ru_col + o < WITH_WIDTH(width))
8355 {
8356 while (this_ru_col + o < WITH_WIDTH(width))
8357 {
8358#ifdef FEAT_MBYTE
8359 if (has_mbyte)
8360 i += (*mb_char2bytes)(fillchar, buffer + i);
8361 else
8362#endif
8363 buffer[i++] = fillchar;
8364 ++o;
8365 }
8366 get_rel_pos(wp, buffer + i);
8367 }
8368 /* Truncate at window boundary. */
8369#ifdef FEAT_MBYTE
8370 if (has_mbyte)
8371 {
8372 o = 0;
8373 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len_check)(buffer + i))
8374 {
8375 o += (*mb_ptr2cells)(buffer + i);
8376 if (this_ru_col + o > WITH_WIDTH(width))
8377 {
8378 buffer[i] = NUL;
8379 break;
8380 }
8381 }
8382 }
8383 else
8384#endif
8385 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
8386 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
8387
8388 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
8389 i = redraw_cmdline;
8390 screen_fill(row, row + 1,
8391 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
8392 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
8393 fillchar, fillchar, attr);
8394 /* don't redraw the cmdline because of showing the ruler */
8395 redraw_cmdline = i;
8396 wp->w_ru_cursor = wp->w_cursor;
8397 wp->w_ru_virtcol = wp->w_virtcol;
8398 wp->w_ru_empty = empty_line;
8399 wp->w_ru_topline = wp->w_topline;
8400 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
8401#ifdef FEAT_DIFF
8402 wp->w_ru_topfill = wp->w_topfill;
8403#endif
8404 }
8405}
8406#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00008407
8408#if defined(FEAT_LINEBREAK) || defined(PROTO)
8409/*
8410 * Return the width of the 'number' column.
8411 * Zero when 'number' isn't set.
8412 * Otherwise it depends on 'numberwidth' and the line count.
8413 */
8414 int
8415number_width(wp)
8416 win_T *wp;
8417{
8418 int n;
8419 linenr_T lnum;
8420
8421 if (!wp->w_p_nu)
8422 return 0;
8423
8424 lnum = wp->w_buffer->b_ml.ml_line_count;
8425 if (lnum == wp->w_nrwidth_line_count)
8426 return wp->w_nrwidth_width;
8427 wp->w_nrwidth_line_count = lnum;
8428
8429 n = 0;
8430 do
8431 {
8432 lnum /= 10;
8433 ++n;
8434 } while (lnum > 0);
8435
8436 /* 'numberwidth' gives the minimal width plus one */
8437 if (n < wp->w_p_nuw - 1)
8438 n = wp->w_p_nuw - 1;
8439
8440 wp->w_nrwidth_width = n;
8441 return n;
8442}
8443#endif