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