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