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