blob: ece886bec5191bc28defd7b72a6f3af79a659652 [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 */
2505 int spell_attr = 0; /* attributes desired by spelling */
2506 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507#endif
2508 int extra_check; /* has syntax or linebreak */
2509#ifdef FEAT_MBYTE
2510 int multi_attr = 0; /* attributes desired by multibyte */
2511 int mb_l = 1; /* multi-byte byte length */
2512 int mb_c = 0; /* decoded multi-byte character */
2513 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
2514 int u8c_c1 = 0; /* first composing UTF-8 char */
2515 int u8c_c2 = 0; /* second composing UTF-8 char */
2516#endif
2517#ifdef FEAT_DIFF
2518 int filler_lines; /* nr of filler lines to be drawn */
2519 int filler_todo; /* nr of filler lines still to do + 1 */
2520 enum hlf_value diff_hlf = (enum hlf_value)0; /* type of diff highlighting */
2521 int change_start = MAXCOL; /* first col of changed area */
2522 int change_end = -1; /* last col of changed area */
2523#endif
2524 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2525#ifdef FEAT_LINEBREAK
2526 int need_showbreak = FALSE;
2527#endif
2528#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS))
2529# define LINE_ATTR
2530 int line_attr = 0; /* atrribute for the whole line */
2531#endif
2532#ifdef FEAT_SEARCH_EXTRA
2533 match_T *shl; /* points to search_hl or match_hl */
2534#endif
2535#ifdef FEAT_ARABIC
2536 int prev_c = 0; /* previous Arabic character */
2537 int prev_c1 = 0; /* first composing char for prev_c */
2538#endif
2539
2540 /* draw_state: items that are drawn in sequence: */
2541#define WL_START 0 /* nothing done yet */
2542#ifdef FEAT_CMDWIN
2543# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2544#else
2545# define WL_CMDLINE WL_START
2546#endif
2547#ifdef FEAT_FOLDING
2548# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2549#else
2550# define WL_FOLD WL_CMDLINE
2551#endif
2552#ifdef FEAT_SIGNS
2553# define WL_SIGN WL_FOLD + 1 /* column for signs */
2554#else
2555# define WL_SIGN WL_FOLD /* column for signs */
2556#endif
2557#define WL_NR WL_SIGN + 1 /* line number */
2558#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2559# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2560#else
2561# define WL_SBR WL_NR
2562#endif
2563#define WL_LINE WL_SBR + 1 /* text in the line */
2564 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar843ee412004-06-30 16:16:41 +00002565#if defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 int feedback_col = 0;
2567 int feedback_old_attr = -1;
2568#endif
2569
2570
2571 if (startrow > endrow) /* past the end already! */
2572 return startrow;
2573
2574 row = startrow;
2575 screen_row = row + W_WINROW(wp);
2576
2577 /*
2578 * To speed up the loop below, set extra_check when there is linebreak,
2579 * trailing white space and/or syntax processing to be done.
2580 */
2581#ifdef FEAT_LINEBREAK
2582 extra_check = wp->w_p_lbr;
2583#else
2584 extra_check = 0;
2585#endif
2586#ifdef FEAT_SYN_HL
2587 if (syntax_present(wp->w_buffer))
2588 {
2589 /* Prepare for syntax highlighting in this line. When there is an
2590 * error, stop syntax highlighting. */
2591 save_did_emsg = did_emsg;
2592 did_emsg = FALSE;
2593 syntax_start(wp, lnum);
2594 if (did_emsg)
2595 syntax_clear(wp->w_buffer);
2596 else
2597 {
2598 did_emsg = save_did_emsg;
2599 has_syntax = TRUE;
2600 extra_check = TRUE;
2601 }
2602 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00002603
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002604 if (wp->w_p_spell
2605 && *wp->w_buffer->b_p_spl != NUL
2606 && wp->w_buffer->b_langp.ga_len > 0
2607 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002608 {
2609 /* Prepare for spell checking. */
2610 has_spell = TRUE;
2611 extra_check = TRUE;
2612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614
2615 /*
2616 * handle visual active in this window
2617 */
2618 fromcol = -10;
2619 tocol = MAXCOL;
2620#ifdef FEAT_VISUAL
2621 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2622 {
2623 /* Visual is after curwin->w_cursor */
2624 if (ltoreq(curwin->w_cursor, VIsual))
2625 {
2626 top = &curwin->w_cursor;
2627 bot = &VIsual;
2628 }
2629 else /* Visual is before curwin->w_cursor */
2630 {
2631 top = &VIsual;
2632 bot = &curwin->w_cursor;
2633 }
2634 if (VIsual_mode == Ctrl_V) /* block mode */
2635 {
2636 if (lnum >= top->lnum && lnum <= bot->lnum)
2637 {
2638 fromcol = wp->w_old_cursor_fcol;
2639 tocol = wp->w_old_cursor_lcol;
2640 }
2641 }
2642 else /* non-block mode */
2643 {
2644 if (lnum > top->lnum && lnum <= bot->lnum)
2645 fromcol = 0;
2646 else if (lnum == top->lnum)
2647 {
2648 if (VIsual_mode == 'V') /* linewise */
2649 fromcol = 0;
2650 else
2651 {
2652 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2653 if (gchar_pos(top) == NUL)
2654 tocol = fromcol + 1;
2655 }
2656 }
2657 if (VIsual_mode != 'V' && lnum == bot->lnum)
2658 {
2659 if (*p_sel == 'e' && bot->col == 0
2660#ifdef FEAT_VIRTUALEDIT
2661 && bot->coladd == 0
2662#endif
2663 )
2664 {
2665 fromcol = -10;
2666 tocol = MAXCOL;
2667 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002668 else if (bot->col == MAXCOL)
2669 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 else
2671 {
2672 pos = *bot;
2673 if (*p_sel == 'e')
2674 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2675 else
2676 {
2677 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2678 ++tocol;
2679 }
2680 }
2681 }
2682 }
2683
2684#ifndef MSDOS
2685 /* Check if the character under the cursor should not be inverted */
2686 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2687# ifdef FEAT_GUI
2688 && !gui.in_use
2689# endif
2690 )
2691 noinvcur = TRUE;
2692#endif
2693
2694 /* if inverting in this line set area_highlighting */
2695 if (fromcol >= 0)
2696 {
2697 area_highlighting = TRUE;
2698 attr = hl_attr(HLF_V);
2699#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2700 if (clip_star.available && !clip_star.owned && clip_isautosel())
2701 attr = hl_attr(HLF_VNC);
2702#endif
2703 }
2704 }
2705
2706 /*
2707 * handle 'insearch' and ":s///c" highlighting
2708 */
2709 else
2710#endif /* FEAT_VISUAL */
2711 if (highlight_match
2712 && wp == curwin
2713 && lnum >= curwin->w_cursor.lnum
2714 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2715 {
2716 if (lnum == curwin->w_cursor.lnum)
2717 getvcol(curwin, &(curwin->w_cursor),
2718 (colnr_T *)&fromcol, NULL, NULL);
2719 else
2720 fromcol = 0;
2721 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2722 {
2723 pos.lnum = lnum;
2724 pos.col = search_match_endcol;
2725 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2726 }
2727 else
2728 tocol = MAXCOL;
2729 if (fromcol == tocol) /* do at least one character */
2730 tocol = fromcol + 1; /* happens when past end of line */
2731 area_highlighting = TRUE;
2732 attr = hl_attr(HLF_I);
2733 }
2734
2735#ifdef FEAT_DIFF
2736 filler_lines = diff_check(wp, lnum);
2737 if (filler_lines < 0)
2738 {
2739 if (filler_lines == -1)
2740 {
2741 if (diff_find_change(wp, lnum, &change_start, &change_end))
2742 diff_hlf = HLF_ADD; /* added line */
2743 else if (change_start == 0)
2744 diff_hlf = HLF_TXD; /* changed text */
2745 else
2746 diff_hlf = HLF_CHD; /* changed line */
2747 }
2748 else
2749 diff_hlf = HLF_ADD; /* added line */
2750 filler_lines = 0;
2751 area_highlighting = TRUE;
2752 }
2753 if (lnum == wp->w_topline)
2754 filler_lines = wp->w_topfill;
2755 filler_todo = filler_lines;
2756#endif
2757
2758#ifdef LINE_ATTR
2759# ifdef FEAT_SIGNS
2760 /* If this line has a sign with line highlighting set line_attr. */
2761 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2762 if (v != 0)
2763 line_attr = sign_get_attr((int)v, TRUE);
2764# endif
2765# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2766 /* Highlight the current line in the quickfix window. */
2767 if (bt_quickfix(wp->w_buffer) && qf_current_entry() == lnum)
2768 line_attr = hl_attr(HLF_L);
2769# endif
2770 if (line_attr != 0)
2771 area_highlighting = TRUE;
2772#endif
2773
2774 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2775 ptr = line;
2776
2777 /* find start of trailing whitespace */
2778 if (wp->w_p_list && lcs_trail)
2779 {
2780 trailcol = (colnr_T)STRLEN(ptr);
2781 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2782 --trailcol;
2783 trailcol += (colnr_T) (ptr - line);
2784 extra_check = TRUE;
2785 }
2786
2787 /*
2788 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2789 * first character to be displayed.
2790 */
2791 if (wp->w_p_wrap)
2792 v = wp->w_skipcol;
2793 else
2794 v = wp->w_leftcol;
2795 if (v > 0)
2796 {
2797#ifdef FEAT_MBYTE
2798 char_u *prev_ptr = ptr;
2799#endif
2800 while (vcol < v && *ptr != NUL)
2801 {
2802 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2803 vcol += c;
2804#ifdef FEAT_MBYTE
2805 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002807 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
2809
2810#ifdef FEAT_VIRTUALEDIT
2811 /* When 'virtualedit' is set the end of the line may be before the
2812 * start of the displayed part. */
2813 if (vcol < v && *ptr == NUL && virtual_active())
2814 vcol = v;
2815#endif
2816
2817 /* Handle a character that's not completely on the screen: Put ptr at
2818 * that character but skip the first few screen characters. */
2819 if (vcol > v)
2820 {
2821 vcol -= c;
2822#ifdef FEAT_MBYTE
2823 ptr = prev_ptr;
2824#else
2825 --ptr;
2826#endif
2827 n_skip = v - vcol;
2828 }
2829
2830 /*
2831 * Adjust for when the inverted text is before the screen,
2832 * and when the start of the inverted text is before the screen.
2833 */
2834 if (tocol <= vcol)
2835 fromcol = 0;
2836 else if (fromcol >= 0 && fromcol < vcol)
2837 fromcol = vcol;
2838
2839#ifdef FEAT_LINEBREAK
2840 /* When w_skipcol is non-zero, first line needs 'showbreak' */
2841 if (wp->w_p_wrap)
2842 need_showbreak = TRUE;
2843#endif
2844 }
2845
2846 /*
2847 * Correct highlighting for cursor that can't be disabled.
2848 * Avoids having to check this for each character.
2849 */
2850 if (fromcol >= 0)
2851 {
2852 if (noinvcur)
2853 {
2854 if ((colnr_T)fromcol == wp->w_virtcol)
2855 {
2856 /* highlighting starts at cursor, let it start just after the
2857 * cursor */
2858 fromcol_prev = fromcol;
2859 fromcol = -1;
2860 }
2861 else if ((colnr_T)fromcol < wp->w_virtcol)
2862 /* restart highlighting after the cursor */
2863 fromcol_prev = wp->w_virtcol;
2864 }
2865 if (fromcol >= tocol)
2866 fromcol = -1;
2867 }
2868
2869#ifdef FEAT_SEARCH_EXTRA
2870 /*
2871 * Handle highlighting the last used search pattern.
2872 * Do this for both search_hl and match_hl.
2873 */
2874 shl = &search_hl;
2875 for (;;)
2876 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002877 shl->startcol = MAXCOL;
2878 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 shl->attr_cur = 0;
2880 if (shl->rm.regprog != NULL)
2881 {
2882 v = (long)(ptr - line);
2883 next_search_hl(wp, shl, lnum, (colnr_T)v);
2884
2885 /* Need to get the line again, a multi-line regexp may have made it
2886 * invalid. */
2887 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2888 ptr = line + v;
2889
2890 if (shl->lnum != 0 && shl->lnum <= lnum)
2891 {
2892 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002893 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002895 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
2897 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002898 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002900 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002902 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
2904#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002905 if (has_mbyte && line[shl->endcol] != NUL)
2906 shl->endcol += (*mb_ptr2len_check)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 else
2908#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002909 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002911 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
2913 shl->attr_cur = shl->attr;
2914 search_attr = shl->attr;
2915 }
2916 area_highlighting = TRUE;
2917 }
2918 }
2919 if (shl == &match_hl)
2920 break;
2921 shl = &match_hl;
2922 }
2923#endif
2924
2925 off = (unsigned) (current_ScreenLine - ScreenLines);
2926 col = 0;
2927#ifdef FEAT_RIGHTLEFT
2928 if (wp->w_p_rl)
2929 {
2930 /* Rightleft window: process the text in the normal direction, but put
2931 * it in current_ScreenLine[] from right to left. Start at the
2932 * rightmost column of the window. */
2933 col = W_WIDTH(wp) - 1;
2934 off += col;
2935 }
2936#endif
2937
2938 /*
2939 * Repeat for the whole displayed line.
2940 */
2941 for (;;)
2942 {
2943 /* Skip this quickly when working on the text. */
2944 if (draw_state != WL_LINE)
2945 {
2946#ifdef FEAT_CMDWIN
2947 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
2948 {
2949 draw_state = WL_CMDLINE;
2950 if (cmdwin_type != 0 && wp == curwin)
2951 {
2952 /* Draw the cmdline character. */
2953 *extra = cmdwin_type;
2954 n_extra = 1;
2955 p_extra = extra;
2956 c_extra = NUL;
2957 char_attr = hl_attr(HLF_AT);
2958 }
2959 }
2960#endif
2961
2962#ifdef FEAT_FOLDING
2963 if (draw_state == WL_FOLD - 1 && n_extra == 0)
2964 {
2965 draw_state = WL_FOLD;
2966 if (wp->w_p_fdc > 0)
2967 {
2968 /* Draw the 'foldcolumn'. */
2969 fill_foldcolumn(extra, wp, FALSE, lnum);
2970 n_extra = wp->w_p_fdc;
2971 p_extra = extra;
2972 c_extra = NUL;
2973 char_attr = hl_attr(HLF_FC);
2974 }
2975 }
2976#endif
2977
2978#ifdef FEAT_SIGNS
2979 if (draw_state == WL_SIGN - 1 && n_extra == 0)
2980 {
2981 draw_state = WL_SIGN;
2982 /* Show the sign column when there are any signs in this
2983 * buffer or when using Netbeans. */
2984 if (draw_signcolumn(wp)
2985# ifdef FEAT_DIFF
2986 && filler_todo <= 0
2987# endif
2988 )
2989 {
2990 int_u text_sign;
2991# ifdef FEAT_SIGN_ICONS
2992 int_u icon_sign;
2993# endif
2994
2995 /* Draw two cells with the sign value or blank. */
2996 c_extra = ' ';
2997 char_attr = hl_attr(HLF_SC);
2998 n_extra = 2;
2999
3000 if (row == startrow)
3001 {
3002 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3003 SIGN_TEXT);
3004# ifdef FEAT_SIGN_ICONS
3005 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3006 SIGN_ICON);
3007 if (gui.in_use && icon_sign != 0)
3008 {
3009 /* Use the image in this position. */
3010 c_extra = SIGN_BYTE;
3011# ifdef FEAT_NETBEANS_INTG
3012 if (buf_signcount(wp->w_buffer, lnum) > 1)
3013 c_extra = MULTISIGN_BYTE;
3014# endif
3015 char_attr = icon_sign;
3016 }
3017 else
3018# endif
3019 if (text_sign != 0)
3020 {
3021 p_extra = sign_get_text(text_sign);
3022 if (p_extra != NULL)
3023 {
3024 c_extra = NUL;
3025 n_extra = STRLEN(p_extra);
3026 }
3027 char_attr = sign_get_attr(text_sign, FALSE);
3028 }
3029 }
3030 }
3031 }
3032#endif
3033
3034 if (draw_state == WL_NR - 1 && n_extra == 0)
3035 {
3036 draw_state = WL_NR;
3037 /* Display the line number. After the first fill with blanks
3038 * when the 'n' flag isn't in 'cpo' */
3039 if (wp->w_p_nu
3040 && (row == startrow
3041#ifdef FEAT_DIFF
3042 + filler_lines
3043#endif
3044 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3045 {
3046 /* Draw the line number (empty space after wrapping). */
3047 if (row == startrow
3048#ifdef FEAT_DIFF
3049 + filler_lines
3050#endif
3051 )
3052 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003053 sprintf((char *)extra, "%*ld ",
3054 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 if (wp->w_skipcol > 0)
3056 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3057 *p_extra = '-';
3058#ifdef FEAT_RIGHTLEFT
3059 if (wp->w_p_rl) /* reverse line numbers */
3060 rl_mirror(extra);
3061#endif
3062 p_extra = extra;
3063 c_extra = NUL;
3064 }
3065 else
3066 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003067 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 char_attr = hl_attr(HLF_N);
3069 }
3070 }
3071
3072#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3073 if (draw_state == WL_SBR - 1 && n_extra == 0)
3074 {
3075 draw_state = WL_SBR;
3076# ifdef FEAT_DIFF
3077 if (filler_todo > 0)
3078 {
3079 /* Draw "deleted" diff line(s). */
3080 if (char2cells(fill_diff) > 1)
3081 c_extra = '-';
3082 else
3083 c_extra = fill_diff;
3084# ifdef FEAT_RIGHTLEFT
3085 if (wp->w_p_rl)
3086 n_extra = col + 1;
3087 else
3088# endif
3089 n_extra = W_WIDTH(wp) - col;
3090 char_attr = hl_attr(HLF_DED);
3091 }
3092# endif
3093# ifdef FEAT_LINEBREAK
3094 if (*p_sbr != NUL && need_showbreak)
3095 {
3096 /* Draw 'showbreak' at the start of each broken line. */
3097 p_extra = p_sbr;
3098 c_extra = NUL;
3099 n_extra = (int)STRLEN(p_sbr);
3100 char_attr = hl_attr(HLF_AT);
3101 need_showbreak = FALSE;
3102 /* Correct end of highlighted area for 'showbreak',
3103 * required when 'linebreak' is also set. */
3104 if (tocol == vcol)
3105 tocol += n_extra;
3106 }
3107# endif
3108 }
3109#endif
3110
3111 if (draw_state == WL_LINE - 1 && n_extra == 0)
3112 {
3113 draw_state = WL_LINE;
3114 if (saved_n_extra)
3115 {
3116 /* Continue item from end of wrapped line. */
3117 n_extra = saved_n_extra;
3118 c_extra = saved_c_extra;
3119 p_extra = saved_p_extra;
3120 char_attr = saved_char_attr;
3121 }
3122 else
3123 char_attr = 0;
3124 }
3125 }
3126
3127 /* When still displaying '$' of change command, stop at cursor */
3128 if (dollar_vcol != 0 && wp == curwin && vcol >= (long)wp->w_virtcol
3129#ifdef FEAT_DIFF
3130 && filler_todo <= 0
3131#endif
3132 )
3133 {
3134 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3135 wp->w_p_rl);
3136 /* Pretend we have finished updating the window. */
3137 row = wp->w_height;
3138 break;
3139 }
3140
3141 if (draw_state == WL_LINE && area_highlighting)
3142 {
3143 /* handle Visual or match highlighting in this line */
3144 if (vcol == fromcol
3145#ifdef FEAT_MBYTE
3146 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3147 && (*mb_ptr2cells)(ptr) > 1)
3148#endif
3149 || ((int)vcol_prev == fromcol_prev
3150 && vcol < tocol))
3151 area_attr = attr; /* start highlighting */
3152 else if (area_attr != 0
3153 && (vcol == tocol
3154 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
3155#ifdef LINE_ATTR
3156 area_attr = line_attr; /* stop highlighting */
3157 else if (line_attr && ((fromcol == -10 && tocol == MAXCOL)
3158 || (vcol < fromcol || vcol > tocol)))
3159 area_attr = line_attr;
3160#else
3161 area_attr = 0; /* stop highlighting */
3162#endif
3163
3164#ifdef FEAT_SEARCH_EXTRA
3165 if (!n_extra)
3166 {
3167 /*
3168 * Check for start/end of search pattern match.
3169 * After end, check for start/end of next match.
3170 * When another match, have to check for start again.
3171 * Watch out for matching an empty string!
3172 * Do this first for search_hl, then for match_hl, so that
3173 * ":match" overrules 'hlsearch'.
3174 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003175 v = (long)(ptr - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 shl = &search_hl;
3177 for (;;)
3178 {
3179 while (shl->rm.regprog != NULL)
3180 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003181 if (shl->startcol != MAXCOL
3182 && v >= (long)shl->startcol
3183 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
3185 shl->attr_cur = shl->attr;
3186 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003187 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 {
3189 shl->attr_cur = 0;
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 next_search_hl(wp, shl, lnum, (colnr_T)v);
3192
3193 /* Need to get the line again, a multi-line regexp
3194 * may have made it invalid. */
3195 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3196 ptr = line + v;
3197
3198 if (shl->lnum == lnum)
3199 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003200 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003202 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003204 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003206 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
3208 /* highlight empty match, try again after
3209 * it */
3210#ifdef FEAT_MBYTE
3211 if (has_mbyte)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003212 shl->endcol += (*mb_ptr2len_check)(line
3213 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 else
3215#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003216 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
3218
3219 /* Loop to check if the match starts at the
3220 * current position */
3221 continue;
3222 }
3223 }
3224 break;
3225 }
3226 if (shl == &match_hl)
3227 break;
3228 shl = &match_hl;
3229 }
3230 /* ":match" highlighting overrules 'hlsearch' */
3231 if (match_hl.attr_cur != 0)
3232 search_attr = match_hl.attr_cur;
3233 else
3234 search_attr = search_hl.attr_cur;
3235 }
3236#endif
3237
3238 if (area_attr != 0)
3239 char_attr = area_attr;
3240#ifdef FEAT_SYN_HL
3241 else if (search_attr == 0 && has_syntax)
3242 char_attr = syntax_attr;
3243#endif
3244 else
3245 char_attr = search_attr;
3246
3247#ifdef FEAT_DIFF
3248 if (diff_hlf != (enum hlf_value)0 && n_extra == 0)
3249 {
3250 if (diff_hlf == HLF_CHD && ptr - line >= change_start)
3251 diff_hlf = HLF_TXD; /* changed text */
3252 if (diff_hlf == HLF_TXD && ptr - line > change_end)
3253 diff_hlf = HLF_CHD; /* changed line */
3254 if (attr == 0 || area_attr != attr)
3255 area_attr = hl_attr(diff_hlf);
3256 if (attr == 0 || char_attr != attr)
3257 {
3258 if (search_attr != 0)
3259 char_attr = search_attr;
3260 else
3261 char_attr = hl_attr(diff_hlf);
3262 }
3263 }
3264#endif
3265 }
3266
3267 /*
3268 * Get the next character to put on the screen.
3269 */
3270 /*
3271 * The 'extra' array contains the extra stuff that is inserted to
3272 * represent special characters (non-printable stuff). When all
3273 * characters are the same, c_extra is used.
3274 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3275 */
3276 if (n_extra > 0)
3277 {
3278 if (c_extra != NUL)
3279 {
3280 c = c_extra;
3281#ifdef FEAT_MBYTE
3282 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3283 if (enc_utf8 && (*mb_char2len)(c) > 1)
3284 {
3285 mb_utf8 = TRUE;
3286 u8c_c1 = u8c_c2 = 0;
3287 }
3288 else
3289 mb_utf8 = FALSE;
3290#endif
3291 }
3292 else
3293 {
3294 c = *p_extra;
3295#ifdef FEAT_MBYTE
3296 if (has_mbyte)
3297 {
3298 mb_c = c;
3299 if (enc_utf8)
3300 {
3301 /* If the UTF-8 character is more than one byte:
3302 * Decode it into "mb_c". */
3303 mb_l = (*mb_ptr2len_check)(p_extra);
3304 mb_utf8 = FALSE;
3305 if (mb_l > n_extra)
3306 mb_l = 1;
3307 else if (mb_l > 1)
3308 {
3309 mb_c = utfc_ptr2char(p_extra, &u8c_c1, &u8c_c2);
3310 mb_utf8 = TRUE;
3311 }
3312 }
3313 else
3314 {
3315 /* if this is a DBCS character, put it in "mb_c" */
3316 mb_l = MB_BYTE2LEN(c);
3317 if (mb_l >= n_extra)
3318 mb_l = 1;
3319 else if (mb_l > 1)
3320 mb_c = (c << 8) + p_extra[1];
3321 }
3322 /* If a double-width char doesn't fit display a '>' in the
3323 * last column. */
3324 if (
3325# ifdef FEAT_RIGHTLEFT
3326 wp->w_p_rl ? (col <= 0) :
3327# endif
3328 (col >= W_WIDTH(wp) - 1)
3329 && (*mb_char2cells)(mb_c) == 2)
3330 {
3331 c = '>';
3332 mb_c = c;
3333 mb_l = 1;
3334 mb_utf8 = FALSE;
3335 multi_attr = hl_attr(HLF_AT);
3336 /* put the pointer back to output the double-width
3337 * character at the start of the next line. */
3338 ++n_extra;
3339 --p_extra;
3340 }
3341 else
3342 {
3343 n_extra -= mb_l - 1;
3344 p_extra += mb_l - 1;
3345 }
3346 }
3347#endif
3348 ++p_extra;
3349 }
3350 --n_extra;
3351 }
3352 else
3353 {
3354 /*
3355 * Get a character from the line itself.
3356 */
3357 c = *ptr;
3358#ifdef FEAT_MBYTE
3359 if (has_mbyte)
3360 {
3361 mb_c = c;
3362 if (enc_utf8)
3363 {
3364 /* If the UTF-8 character is more than one byte: Decode it
3365 * into "mb_c". */
3366 mb_l = (*mb_ptr2len_check)(ptr);
3367 mb_utf8 = FALSE;
3368 if (mb_l > 1)
3369 {
3370 mb_c = utfc_ptr2char(ptr, &u8c_c1, &u8c_c2);
3371 /* Overlong encoded ASCII or ASCII with composing char
3372 * is displayed normally, except a NUL. */
3373 if (mb_c < 0x80)
3374 c = mb_c;
3375 mb_utf8 = TRUE;
3376 }
3377
3378 if ((mb_l == 1 && c >= 0x80)
3379 || (mb_l >= 1 && mb_c == 0)
3380 || (mb_l > 1 && (!vim_isprintc(mb_c)
3381 || mb_c >= 0x10000)))
3382 {
3383 /*
3384 * Illegal UTF-8 byte: display as <xx>.
3385 * Non-BMP character : display as ? or fullwidth ?.
3386 */
3387 if (mb_c < 0x10000)
3388 {
3389 transchar_hex(extra, mb_c);
3390#ifdef FEAT_RIGHTLEFT
3391 if (wp->w_p_rl) /* reverse */
3392 rl_mirror(extra);
3393#endif
3394 }
3395 else if (utf_char2cells(mb_c) != 2)
3396 STRCPY(extra, "?");
3397 else
3398 /* 0xff1f in UTF-8: full-width '?' */
3399 STRCPY(extra, "\357\274\237");
3400
3401 p_extra = extra;
3402 c = *p_extra;
3403 mb_c = mb_ptr2char_adv(&p_extra);
3404 mb_utf8 = (c >= 0x80);
3405 n_extra = (int)STRLEN(p_extra);
3406 c_extra = NUL;
3407 if (area_attr == 0 && search_attr == 0)
3408 {
3409 n_attr = n_extra + 1;
3410 extra_attr = hl_attr(HLF_8);
3411 saved_attr2 = char_attr; /* save current attr */
3412 }
3413 }
3414 else if (mb_l == 0) /* at the NUL at end-of-line */
3415 mb_l = 1;
3416#ifdef FEAT_ARABIC
3417 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3418 {
3419 /* Do Arabic shaping. */
3420 int pc, pc1, nc, dummy;
3421
3422 /* The idea of what is the previous and next
3423 * character depends on 'rightleft'. */
3424 if (wp->w_p_rl)
3425 {
3426 pc = prev_c;
3427 pc1 = prev_c1;
3428 nc = utf_ptr2char(ptr + mb_l);
3429 prev_c1 = u8c_c1;
3430 }
3431 else
3432 {
3433 pc = utfc_ptr2char(ptr + mb_l, &pc1, &dummy);
3434 nc = prev_c;
3435 }
3436 prev_c = mb_c;
3437
3438 mb_c = arabic_shape(mb_c, &c, &u8c_c1, pc, pc1, nc);
3439 }
3440 else
3441 prev_c = mb_c;
3442#endif
3443 }
3444 else /* enc_dbcs */
3445 {
3446 mb_l = MB_BYTE2LEN(c);
3447 if (mb_l == 0) /* at the NUL at end-of-line */
3448 mb_l = 1;
3449 else if (mb_l > 1)
3450 {
3451 /* We assume a second byte below 32 is illegal.
3452 * Hopefully this is OK for all double-byte encodings!
3453 */
3454 if (ptr[1] >= 32)
3455 mb_c = (c << 8) + ptr[1];
3456 else
3457 {
3458 if (ptr[1] == NUL)
3459 {
3460 /* head byte at end of line */
3461 mb_l = 1;
3462 transchar_nonprint(extra, c);
3463 }
3464 else
3465 {
3466 /* illegal tail byte */
3467 mb_l = 2;
3468 STRCPY(extra, "XX");
3469 }
3470 p_extra = extra;
3471 n_extra = (int)STRLEN(extra) - 1;
3472 c_extra = NUL;
3473 c = *p_extra++;
3474 if (area_attr == 0 && search_attr == 0)
3475 {
3476 n_attr = n_extra + 1;
3477 extra_attr = hl_attr(HLF_8);
3478 saved_attr2 = char_attr; /* save current attr */
3479 }
3480 mb_c = c;
3481 }
3482 }
3483 }
3484 /* If a double-width char doesn't fit display a '>' in the
3485 * last column; the character is displayed at the start of the
3486 * next line. */
3487 if ((
3488# ifdef FEAT_RIGHTLEFT
3489 wp->w_p_rl ? (col <= 0) :
3490# endif
3491 (col >= W_WIDTH(wp) - 1))
3492 && (*mb_char2cells)(mb_c) == 2)
3493 {
3494 c = '>';
3495 mb_c = c;
3496 mb_utf8 = FALSE;
3497 mb_l = 1;
3498 multi_attr = hl_attr(HLF_AT);
3499 /* Put pointer back so that the character will be
3500 * displayed at the start of the next line. */
3501 --ptr;
3502 }
3503 else if (*ptr != NUL)
3504 ptr += mb_l - 1;
3505
3506 /* If a double-width char doesn't fit at the left side display
3507 * a '<' in the first column. */
3508 if (n_skip > 0 && mb_l > 1)
3509 {
3510 extra[0] = '<';
3511 p_extra = extra;
3512 n_extra = 1;
3513 c_extra = NUL;
3514 c = ' ';
3515 if (area_attr == 0 && search_attr == 0)
3516 {
3517 n_attr = n_extra + 1;
3518 extra_attr = hl_attr(HLF_AT);
3519 saved_attr2 = char_attr; /* save current attr */
3520 }
3521 mb_c = c;
3522 mb_utf8 = FALSE;
3523 mb_l = 1;
3524 }
3525
3526 }
3527#endif
3528 ++ptr;
3529
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003530 /* 'list' : change char 160 to lcs_nbsp. */
3531 if (wp->w_p_list && c == 160 && lcs_nbsp)
3532 {
3533 c = lcs_nbsp;
3534 if (area_attr == 0 && search_attr == 0)
3535 {
3536 n_attr = 1;
3537 extra_attr = hl_attr(HLF_8);
3538 saved_attr2 = char_attr; /* save current attr */
3539 }
3540#ifdef FEAT_MBYTE
3541 mb_c = c;
3542 if (enc_utf8 && (*mb_char2len)(c) > 1)
3543 {
3544 mb_utf8 = TRUE;
3545 u8c_c1 = u8c_c2 = 0;
3546 }
3547 else
3548 mb_utf8 = FALSE;
3549#endif
3550 }
3551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (extra_check)
3553 {
3554#ifdef FEAT_SYN_HL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003555 int can_spell = TRUE;
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /* Get syntax attribute, unless still at the start of the line
3558 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003559 v = (long)(ptr - line);
3560 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
3562 /* Get the syntax attribute for the character. If there
3563 * is an error, disable syntax highlighting. */
3564 save_did_emsg = did_emsg;
3565 did_emsg = FALSE;
3566
Bram Moolenaar217ad922005-03-20 22:37:15 +00003567 syntax_attr = get_syntax_attr((colnr_T)v - 1,
3568 has_spell ? &can_spell : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
3570 if (did_emsg)
3571 syntax_clear(wp->w_buffer);
3572 else
3573 did_emsg = save_did_emsg;
3574
3575 /* Need to get the line again, a multi-line regexp may
3576 * have made it invalid. */
3577 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3578 ptr = line + v;
3579
3580 if (area_attr == 0 && search_attr == 0)
3581 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003582 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003583 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003585
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003586 /* Check spelling (unless at the end of the line).
Bram Moolenaar217ad922005-03-20 22:37:15 +00003587 * Only do this when there is no syntax highlighting, there is
3588 * on @Spell cluster or the current syntax item contains the
3589 * @Spell cluster. */
3590 if (has_spell && v >= word_end)
3591 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003592 spell_attr = 0;
3593 if (area_attr == 0 && search_attr == 0)
3594 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003595 if (c != 0 && (!has_syntax || can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003596 {
Bram Moolenaar217ad922005-03-20 22:37:15 +00003597# ifdef FEAT_MBYTE
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003598 char_u *prev_ptr = ptr - (has_mbyte ? mb_l : 1);
3599# else
3600 char_u *prev_ptr = ptr - 1;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003601# endif
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003602 word_end = v + spell_check(wp, prev_ptr, &spell_attr);
Bram Moolenaar217ad922005-03-20 22:37:15 +00003603
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003604 /* In Insert mode only highlight a word that
3605 * doesn't touch the cursor. */
3606 if (spell_attr != 0
3607 && (State & INSERT) != 0
3608 && wp->w_cursor.lnum == lnum
3609 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003610 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003611 && wp->w_cursor.col < (colnr_T)word_end)
3612 {
3613 spell_attr = 0;
3614 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003615 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003616 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003617 }
3618 if (spell_attr != 0)
3619 char_attr = hl_combine_attr(char_attr, spell_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620#endif
3621#ifdef FEAT_LINEBREAK
3622 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00003623 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 */
3625 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
3626 && !wp->w_p_list)
3627 {
3628 n_extra = win_lbr_chartabsize(wp, ptr - (
3629# ifdef FEAT_MBYTE
3630 has_mbyte ? mb_l :
3631# endif
3632 1), (colnr_T)vcol, NULL) - 1;
3633 c_extra = ' ';
3634 if (vim_iswhite(c))
3635 c = ' ';
3636 }
3637#endif
3638
3639 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3640 {
3641 c = lcs_trail;
3642 if (area_attr == 0 && search_attr == 0)
3643 {
3644 n_attr = 1;
3645 extra_attr = hl_attr(HLF_8);
3646 saved_attr2 = char_attr; /* save current attr */
3647 }
3648#ifdef FEAT_MBYTE
3649 mb_c = c;
3650 if (enc_utf8 && (*mb_char2len)(c) > 1)
3651 {
3652 mb_utf8 = TRUE;
3653 u8c_c1 = u8c_c2 = 0;
3654 }
3655 else
3656 mb_utf8 = FALSE;
3657#endif
3658 }
3659 }
3660
3661 /*
3662 * Handling of non-printable characters.
3663 */
3664 if (!(chartab[c] & CT_PRINT_CHAR))
3665 {
3666 /*
3667 * when getting a character from the file, we may have to
3668 * turn it into something else on the way to putting it
3669 * into "ScreenLines".
3670 */
3671 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3672 {
3673 /* tab amount depends on current column */
3674 n_extra = (int)wp->w_buffer->b_p_ts
3675 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3676#ifdef FEAT_MBYTE
3677 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3678#endif
3679 if (wp->w_p_list)
3680 {
3681 c = lcs_tab1;
3682 c_extra = lcs_tab2;
3683 n_attr = n_extra + 1;
3684 extra_attr = hl_attr(HLF_8);
3685 saved_attr2 = char_attr; /* save current attr */
3686#ifdef FEAT_MBYTE
3687 mb_c = c;
3688 if (enc_utf8 && (*mb_char2len)(c) > 1)
3689 {
3690 mb_utf8 = TRUE;
3691 u8c_c1 = u8c_c2 = 0;
3692 }
3693#endif
3694 }
3695 else
3696 {
3697 c_extra = ' ';
3698 c = ' ';
3699 }
3700 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003701 else if (c == NUL
3702 && ((wp->w_p_list && lcs_eol > 0)
3703 || ((fromcol >= 0 || fromcol_prev >= 0)
3704 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003705#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003706 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003707#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003708 && (
3709# ifdef FEAT_RIGHTLEFT
3710 wp->w_p_rl ? (col >= 0) :
3711# endif
3712 (col < W_WIDTH(wp)))
3713 && !(noinvcur
3714 && (colnr_T)vcol == wp->w_virtcol)))
3715 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003717 /* Display a '$' after the line or highlight an extra
3718 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719#if defined(FEAT_DIFF) || defined(LINE_ATTR)
3720 /* For a diff line the highlighting continues after the
3721 * "$". */
3722 if (
3723# ifdef FEAT_DIFF
3724 diff_hlf == (enum hlf_value)0
3725# ifdef LINE_ATTR
3726 &&
3727# endif
3728# endif
3729# ifdef LINE_ATTR
3730 line_attr == 0
3731# endif
3732 )
3733#endif
3734 {
3735#ifdef FEAT_VIRTUALEDIT
3736 /* In virtualedit, visual selections may extend
3737 * beyond end of line. */
3738 if (area_highlighting && virtual_active()
3739 && tocol != MAXCOL && vcol < tocol)
3740 n_extra = 0;
3741 else
3742#endif
3743 {
3744 p_extra = at_end_str;
3745 n_extra = 1;
3746 c_extra = NUL;
3747 }
3748 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003749 if (wp->w_p_list)
3750 c = lcs_eol;
3751 else
3752 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 lcs_eol_one = -1;
3754 --ptr; /* put it back at the NUL */
3755 if (area_attr == 0 && search_attr == 0)
3756 {
3757 extra_attr = hl_attr(HLF_AT);
3758 n_attr = 1;
3759 }
3760#ifdef FEAT_MBYTE
3761 mb_c = c;
3762 if (enc_utf8 && (*mb_char2len)(c) > 1)
3763 {
3764 mb_utf8 = TRUE;
3765 u8c_c1 = u8c_c2 = 0;
3766 }
3767 else
3768 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3769#endif
3770 }
3771 else if (c != NUL)
3772 {
3773 p_extra = transchar(c);
3774#ifdef FEAT_RIGHTLEFT
3775 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
3776 rl_mirror(p_extra); /* reverse "<12>" */
3777#endif
3778 n_extra = byte2cells(c) - 1;
3779 c_extra = NUL;
3780 c = *p_extra++;
3781 if (area_attr == 0 && search_attr == 0)
3782 {
3783 n_attr = n_extra + 1;
3784 extra_attr = hl_attr(HLF_8);
3785 saved_attr2 = char_attr; /* save current attr */
3786 }
3787#ifdef FEAT_MBYTE
3788 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3789#endif
3790 }
3791#ifdef FEAT_VIRTUALEDIT
3792 else if (VIsual_active
3793 && (VIsual_mode == Ctrl_V
3794 || VIsual_mode == 'v')
3795 && virtual_active()
3796 && tocol != MAXCOL
3797 && vcol < tocol
3798 && (
3799# ifdef FEAT_RIGHTLEFT
3800 wp->w_p_rl ? (col >= 0) :
3801# endif
3802 (col < W_WIDTH(wp))))
3803 {
3804 c = ' ';
3805 --ptr; /* put it back at the NUL */
3806 }
3807#endif
3808#if defined(FEAT_DIFF) || defined(LINE_ATTR)
3809 else if ((
3810# ifdef FEAT_DIFF
3811 diff_hlf != (enum hlf_value)0
3812# ifdef LINE_ATTR
3813 ||
3814# endif
3815# endif
3816# ifdef LINE_ATTR
3817 line_attr != 0
3818# endif
3819 ) && (
3820# ifdef FEAT_RIGHTLEFT
3821 wp->w_p_rl ? (col >= 0) :
3822# endif
3823 (col < W_WIDTH(wp))))
3824 {
3825 /* Highlight until the right side of the window */
3826 c = ' ';
3827 --ptr; /* put it back at the NUL */
3828# ifdef FEAT_DIFF
3829 if (diff_hlf == HLF_TXD)
3830 {
3831 diff_hlf = HLF_CHD;
3832 if (attr == 0 || char_attr != attr)
3833 char_attr = hl_attr(diff_hlf);
3834 }
3835# endif
3836 }
3837#endif
3838 }
3839 }
3840
3841 /* Don't override visual selection highlighting. */
3842 if (n_attr > 0
3843 && draw_state == WL_LINE
3844 && (area_attr == 0 || char_attr != area_attr)
3845 && (search_attr == 0 || char_attr != search_attr))
3846 char_attr = extra_attr;
3847
Bram Moolenaar81695252004-12-29 20:58:21 +00003848#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 /* XIM don't send preedit_start and preedit_end, but they send
3850 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
3851 * im_is_preediting() here. */
3852 if (xic != NULL
3853 && lnum == curwin->w_cursor.lnum
3854 && (State & INSERT)
3855 && !p_imdisable
3856 && im_is_preediting()
3857 && draw_state == WL_LINE)
3858 {
3859 colnr_T tcol;
3860
3861 if (preedit_end_col == MAXCOL)
3862 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
3863 else
3864 tcol = preedit_end_col;
3865 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
3866 {
3867 if (feedback_old_attr < 0)
3868 {
3869 feedback_col = 0;
3870 feedback_old_attr = char_attr;
3871 }
3872 char_attr = im_get_feedback_attr(feedback_col);
3873 if (char_attr < 0)
3874 char_attr = feedback_old_attr;
3875 feedback_col++;
3876 }
3877 else if (feedback_old_attr >= 0)
3878 {
3879 char_attr = feedback_old_attr;
3880 feedback_old_attr = -1;
3881 feedback_col = 0;
3882 }
3883 }
3884#endif
3885 /*
3886 * Handle the case where we are in column 0 but not on the first
3887 * character of the line and the user wants us to show us a
3888 * special character (via 'listchars' option "precedes:<char>".
3889 */
3890 if (lcs_prec_todo != NUL
3891 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
3892#ifdef FEAT_DIFF
3893 && filler_todo <= 0
3894#endif
3895 && draw_state > WL_NR
3896 && c != NUL)
3897 {
3898 c = lcs_prec;
3899 lcs_prec_todo = NUL;
3900#ifdef FEAT_MBYTE
3901 mb_c = c;
3902 if (enc_utf8 && (*mb_char2len)(c) > 1)
3903 {
3904 mb_utf8 = TRUE;
3905 u8c_c1 = u8c_c2 = 0;
3906 }
3907 else
3908 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3909#endif
3910 if ((area_attr == 0 || char_attr != area_attr)
3911 && (search_attr == 0 || char_attr != search_attr))
3912 {
3913 saved_attr3 = char_attr; /* save current attr */
3914 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
3915 n_attr3 = 1;
3916 }
3917 }
3918
3919 /*
3920 * At end of the text line.
3921 */
3922 if (c == NUL)
3923 {
3924 /* invert at least one char, used for Visual and empty line or
3925 * highlight match at end of line. If it's beyond the last
3926 * char on the screen, just overwrite that one (tricky!) Not
3927 * needed when a '$' was displayed for 'list'. */
3928 if (lcs_eol == lcs_eol_one
3929 && ((area_attr != 0 && vcol == fromcol)
3930#ifdef FEAT_SEARCH_EXTRA
3931 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003932 || (ptr - line) - 1 == (long)search_hl.startcol
3933 || (ptr - line) - 1 == (long)match_hl.startcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#endif
3935 ))
3936 {
3937 int n = 0;
3938
3939#ifdef FEAT_RIGHTLEFT
3940 if (wp->w_p_rl)
3941 {
3942 if (col < 0)
3943 n = 1;
3944 }
3945 else
3946#endif
3947 {
3948 if (col >= W_WIDTH(wp))
3949 n = -1;
3950 }
3951 if (n != 0)
3952 {
3953 /* At the window boundary, highlight the last character
3954 * instead (better than nothing). */
3955 off += n;
3956 col += n;
3957 }
3958 else
3959 {
3960 /* Add a blank character to highlight. */
3961 ScreenLines[off] = ' ';
3962#ifdef FEAT_MBYTE
3963 if (enc_utf8)
3964 ScreenLinesUC[off] = 0;
3965#endif
3966 }
3967#ifdef FEAT_SEARCH_EXTRA
3968 if (area_attr == 0)
3969 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003970 if ((ptr - line) - 1 == (long)match_hl.startcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 char_attr = match_hl.attr;
3972 else
3973 char_attr = search_hl.attr;
3974 }
3975#endif
3976 ScreenAttrs[off] = char_attr;
3977#ifdef FEAT_RIGHTLEFT
3978 if (wp->w_p_rl)
3979 --col;
3980 else
3981#endif
3982 ++col;
3983 }
3984
3985 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
3986 wp->w_p_rl);
3987 row++;
3988
3989 /*
3990 * Update w_cline_height and w_cline_folded if the cursor line was
3991 * updated (saves a call to plines() later).
3992 */
3993 if (wp == curwin && lnum == curwin->w_cursor.lnum)
3994 {
3995 curwin->w_cline_row = startrow;
3996 curwin->w_cline_height = row - startrow;
3997#ifdef FEAT_FOLDING
3998 curwin->w_cline_folded = FALSE;
3999#endif
4000 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4001 }
4002
4003 break;
4004 }
4005
4006 /* line continues beyond line end */
4007 if (lcs_ext
4008 && !wp->w_p_wrap
4009#ifdef FEAT_DIFF
4010 && filler_todo <= 0
4011#endif
4012 && (
4013#ifdef FEAT_RIGHTLEFT
4014 wp->w_p_rl ? col == 0 :
4015#endif
4016 col == W_WIDTH(wp) - 1)
4017 && (*ptr != NUL
4018 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4019 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4020 {
4021 c = lcs_ext;
4022 char_attr = hl_attr(HLF_AT);
4023#ifdef FEAT_MBYTE
4024 mb_c = c;
4025 if (enc_utf8 && (*mb_char2len)(c) > 1)
4026 {
4027 mb_utf8 = TRUE;
4028 u8c_c1 = u8c_c2 = 0;
4029 }
4030 else
4031 mb_utf8 = FALSE;
4032#endif
4033 }
4034
4035 /*
4036 * Store character to be displayed.
4037 * Skip characters that are left of the screen for 'nowrap'.
4038 */
4039 vcol_prev = vcol;
4040 if (draw_state < WL_LINE || n_skip <= 0)
4041 {
4042 /*
4043 * Store the character.
4044 */
4045#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4046 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4047 {
4048 /* A double-wide character is: put first halve in left cell. */
4049 --off;
4050 --col;
4051 }
4052#endif
4053 ScreenLines[off] = c;
4054#ifdef FEAT_MBYTE
4055 if (enc_dbcs == DBCS_JPNU)
4056 ScreenLines2[off] = mb_c & 0xff;
4057 else if (enc_utf8)
4058 {
4059 if (mb_utf8)
4060 {
4061 ScreenLinesUC[off] = mb_c;
4062 ScreenLinesC1[off] = u8c_c1;
4063 ScreenLinesC2[off] = u8c_c2;
4064 }
4065 else
4066 ScreenLinesUC[off] = 0;
4067 }
4068 if (multi_attr)
4069 {
4070 ScreenAttrs[off] = multi_attr;
4071 multi_attr = 0;
4072 }
4073 else
4074#endif
4075 ScreenAttrs[off] = char_attr;
4076
4077#ifdef FEAT_MBYTE
4078 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4079 {
4080 /* Need to fill two screen columns. */
4081 ++off;
4082 ++col;
4083 if (enc_utf8)
4084 /* UTF-8: Put a 0 in the second screen char. */
4085 ScreenLines[off] = 0;
4086 else
4087 /* DBCS: Put second byte in the second screen char. */
4088 ScreenLines[off] = mb_c & 0xff;
4089 ++vcol;
4090 /* When "tocol" is halfway a character, set it to the end of
4091 * the character, otherwise highlighting won't stop. */
4092 if (tocol == vcol)
4093 ++tocol;
4094#ifdef FEAT_RIGHTLEFT
4095 if (wp->w_p_rl)
4096 {
4097 /* now it's time to backup one cell */
4098 --off;
4099 --col;
4100 }
4101#endif
4102 }
4103#endif
4104#ifdef FEAT_RIGHTLEFT
4105 if (wp->w_p_rl)
4106 {
4107 --off;
4108 --col;
4109 }
4110 else
4111#endif
4112 {
4113 ++off;
4114 ++col;
4115 }
4116 }
4117 else
4118 --n_skip;
4119
4120 /* Only advance the "vcol" when after the 'number' column. */
4121 if (draw_state >= WL_SBR
4122#ifdef FEAT_DIFF
4123 && filler_todo <= 0
4124#endif
4125 )
4126 ++vcol;
4127
4128 /* restore attributes after "predeces" in 'listchars' */
4129 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4130 char_attr = saved_attr3;
4131
4132 /* restore attributes after last 'listchars' or 'number' char */
4133 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4134 char_attr = saved_attr2;
4135
4136 /*
4137 * At end of screen line and there is more to come: Display the line
4138 * so far. If there is no more to display it is catched above.
4139 */
4140 if ((
4141#ifdef FEAT_RIGHTLEFT
4142 wp->w_p_rl ? (col < 0) :
4143#endif
4144 (col >= W_WIDTH(wp)))
4145 && (*ptr != NUL
4146#ifdef FEAT_DIFF
4147 || filler_todo > 0
4148#endif
4149 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4150 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4151 )
4152 {
4153 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4154 wp->w_p_rl);
4155 ++row;
4156 ++screen_row;
4157
4158 /* When not wrapping and finished diff lines, or when displayed
4159 * '$' and highlighting until last column, break here. */
4160 if ((!wp->w_p_wrap
4161#ifdef FEAT_DIFF
4162 && filler_todo <= 0
4163#endif
4164 ) || lcs_eol_one == -1)
4165 break;
4166
4167 /* When the window is too narrow draw all "@" lines. */
4168 if (draw_state != WL_LINE
4169#ifdef FEAT_DIFF
4170 && filler_todo <= 0
4171#endif
4172 )
4173 {
4174 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4175#ifdef FEAT_VERTSPLIT
4176 draw_vsep_win(wp, row);
4177#endif
4178 row = endrow;
4179 }
4180
4181 /* When line got too long for screen break here. */
4182 if (row == endrow)
4183 {
4184 ++row;
4185 break;
4186 }
4187
4188 if (screen_cur_row == screen_row - 1
4189#ifdef FEAT_DIFF
4190 && filler_todo <= 0
4191#endif
4192 && W_WIDTH(wp) == Columns)
4193 {
4194 /* Remember that the line wraps, used for modeless copy. */
4195 LineWraps[screen_row - 1] = TRUE;
4196
4197 /*
4198 * Special trick to make copy/paste of wrapped lines work with
4199 * xterm/screen: write an extra character beyond the end of
4200 * the line. This will work with all terminal types
4201 * (regardless of the xn,am settings).
4202 * Only do this on a fast tty.
4203 * Only do this if the cursor is on the current line
4204 * (something has been written in it).
4205 * Don't do this for the GUI.
4206 * Don't do this for double-width characters.
4207 * Don't do this for a window not at the right screen border.
4208 */
4209 if (p_tf
4210#ifdef FEAT_GUI
4211 && !gui.in_use
4212#endif
4213#ifdef FEAT_MBYTE
4214 && !(has_mbyte
4215 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4216 || (*mb_off2cells)(LineOffset[screen_row - 1]
4217 + (int)Columns - 2) == 2))
4218#endif
4219 )
4220 {
4221 /* First make sure we are at the end of the screen line,
4222 * then output the same character again to let the
4223 * terminal know about the wrap. If the terminal doesn't
4224 * auto-wrap, we overwrite the character. */
4225 if (screen_cur_col != W_WIDTH(wp))
4226 screen_char(LineOffset[screen_row - 1]
4227 + (unsigned)Columns - 1,
4228 screen_row - 1, (int)(Columns - 1));
4229
4230#ifdef FEAT_MBYTE
4231 /* When there is a multi-byte character, just output a
4232 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004233 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4234 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 out_char(' ');
4236 else
4237#endif
4238 out_char(ScreenLines[LineOffset[screen_row - 1]
4239 + (Columns - 1)]);
4240 /* force a redraw of the first char on the next line */
4241 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4242 screen_start(); /* don't know where cursor is now */
4243 }
4244 }
4245
4246 col = 0;
4247 off = (unsigned)(current_ScreenLine - ScreenLines);
4248#ifdef FEAT_RIGHTLEFT
4249 if (wp->w_p_rl)
4250 {
4251 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4252 off += col;
4253 }
4254#endif
4255
4256 /* reset the drawing state for the start of a wrapped line */
4257 draw_state = WL_START;
4258 saved_n_extra = n_extra;
4259 saved_p_extra = p_extra;
4260 saved_c_extra = c_extra;
4261 saved_char_attr = char_attr;
4262 n_extra = 0;
4263 lcs_prec_todo = lcs_prec;
4264#ifdef FEAT_LINEBREAK
4265# ifdef FEAT_DIFF
4266 if (filler_todo <= 0)
4267# endif
4268 need_showbreak = TRUE;
4269#endif
4270#ifdef FEAT_DIFF
4271 --filler_todo;
4272 /* When the filler lines are actually below the last line of the
4273 * file, don't draw the line itself, break here. */
4274 if (filler_todo == 0 && wp->w_botfill)
4275 break;
4276#endif
4277 }
4278
4279 } /* for every character in the line */
4280
4281 return row;
4282}
4283
4284/*
4285 * Check whether the given character needs redrawing:
4286 * - the (first byte of the) character is different
4287 * - the attributes are different
4288 * - the character is multi-byte and the next byte is different
4289 */
4290 static int
4291char_needs_redraw(off_from, off_to, cols)
4292 int off_from;
4293 int off_to;
4294 int cols;
4295{
4296 if (cols > 0
4297 && ((ScreenLines[off_from] != ScreenLines[off_to]
4298 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4299
4300#ifdef FEAT_MBYTE
4301 || (enc_dbcs != 0
4302 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4303 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4304 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4305 : (cols > 1 && ScreenLines[off_from + 1]
4306 != ScreenLines[off_to + 1])))
4307 || (enc_utf8
4308 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4309 || (ScreenLinesUC[off_from] != 0
4310 && (ScreenLinesC1[off_from]
4311 != ScreenLinesC1[off_to]
4312 || ScreenLinesC2[off_from]
4313 != ScreenLinesC2[off_to]))))
4314#endif
4315 ))
4316 return TRUE;
4317 return FALSE;
4318}
4319
4320/*
4321 * Move one "cooked" screen line to the screen, but only the characters that
4322 * have actually changed. Handle insert/delete character.
4323 * "coloff" gives the first column on the screen for this line.
4324 * "endcol" gives the columns where valid characters are.
4325 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4326 * needs to be cleared, negative otherwise.
4327 * "rlflag" is TRUE in a rightleft window:
4328 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4329 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4330 */
4331 static void
4332screen_line(row, coloff, endcol, clear_width
4333#ifdef FEAT_RIGHTLEFT
4334 , rlflag
4335#endif
4336 )
4337 int row;
4338 int coloff;
4339 int endcol;
4340 int clear_width;
4341#ifdef FEAT_RIGHTLEFT
4342 int rlflag;
4343#endif
4344{
4345 unsigned off_from;
4346 unsigned off_to;
4347 int col = 0;
4348#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4349 int hl;
4350#endif
4351 int force = FALSE; /* force update rest of the line */
4352 int redraw_this /* bool: does character need redraw? */
4353#ifdef FEAT_GUI
4354 = TRUE /* For GUI when while-loop empty */
4355#endif
4356 ;
4357 int redraw_next; /* redraw_this for next character */
4358#ifdef FEAT_MBYTE
4359 int clear_next = FALSE;
4360 int char_cells; /* 1: normal char */
4361 /* 2: occupies two display cells */
4362# define CHAR_CELLS char_cells
4363#else
4364# define CHAR_CELLS 1
4365#endif
4366
4367# ifdef FEAT_CLIPBOARD
4368 clip_may_clear_selection(row, row);
4369# endif
4370
4371 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4372 off_to = LineOffset[row] + coloff;
4373
4374#ifdef FEAT_RIGHTLEFT
4375 if (rlflag)
4376 {
4377 /* Clear rest first, because it's left of the text. */
4378 if (clear_width > 0)
4379 {
4380 while (col <= endcol && ScreenLines[off_to] == ' '
4381 && ScreenAttrs[off_to] == 0
4382# ifdef FEAT_MBYTE
4383 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4384# endif
4385 )
4386 {
4387 ++off_to;
4388 ++col;
4389 }
4390 if (col <= endcol)
4391 screen_fill(row, row + 1, col + coloff,
4392 endcol + coloff + 1, ' ', ' ', 0);
4393 }
4394 col = endcol + 1;
4395 off_to = LineOffset[row] + col + coloff;
4396 off_from += col;
4397 endcol = (clear_width > 0 ? clear_width : -clear_width);
4398 }
4399#endif /* FEAT_RIGHTLEFT */
4400
4401 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4402
4403 while (col < endcol)
4404 {
4405#ifdef FEAT_MBYTE
4406 if (has_mbyte && (col + 1 < endcol))
4407 char_cells = (*mb_off2cells)(off_from);
4408 else
4409 char_cells = 1;
4410#endif
4411
4412 redraw_this = redraw_next;
4413 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4414 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4415
4416#ifdef FEAT_GUI
4417 /* If the next character was bold, then redraw the current character to
4418 * remove any pixels that might have spilt over into us. This only
4419 * happens in the GUI.
4420 */
4421 if (redraw_next && gui.in_use)
4422 {
4423 hl = ScreenAttrs[off_to + CHAR_CELLS];
4424 if (hl > HL_ALL || (hl & HL_BOLD))
4425 redraw_this = TRUE;
4426 }
4427#endif
4428
4429 if (redraw_this)
4430 {
4431 /*
4432 * Special handling when 'xs' termcap flag set (hpterm):
4433 * Attributes for characters are stored at the position where the
4434 * cursor is when writing the highlighting code. The
4435 * start-highlighting code must be written with the cursor on the
4436 * first highlighted character. The stop-highlighting code must
4437 * be written with the cursor just after the last highlighted
4438 * character.
4439 * Overwriting a character doesn't remove it's highlighting. Need
4440 * to clear the rest of the line, and force redrawing it
4441 * completely.
4442 */
4443 if ( p_wiv
4444 && !force
4445#ifdef FEAT_GUI
4446 && !gui.in_use
4447#endif
4448 && ScreenAttrs[off_to] != 0
4449 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4450 {
4451 /*
4452 * Need to remove highlighting attributes here.
4453 */
4454 windgoto(row, col + coloff);
4455 out_str(T_CE); /* clear rest of this screen line */
4456 screen_start(); /* don't know where cursor is now */
4457 force = TRUE; /* force redraw of rest of the line */
4458 redraw_next = TRUE; /* or else next char would miss out */
4459
4460 /*
4461 * If the previous character was highlighted, need to stop
4462 * highlighting at this character.
4463 */
4464 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4465 {
4466 screen_attr = ScreenAttrs[off_to - 1];
4467 term_windgoto(row, col + coloff);
4468 screen_stop_highlight();
4469 }
4470 else
4471 screen_attr = 0; /* highlighting has stopped */
4472 }
4473#ifdef FEAT_MBYTE
4474 if (enc_dbcs != 0)
4475 {
4476 /* Check if overwriting a double-byte with a single-byte or
4477 * the other way around requires another character to be
4478 * redrawn. For UTF-8 this isn't needed, because comparing
4479 * ScreenLinesUC[] is sufficient. */
4480 if (char_cells == 1
4481 && col + 1 < endcol
4482 && (*mb_off2cells)(off_to) > 1)
4483 {
4484 /* Writing a single-cell character over a double-cell
4485 * character: need to redraw the next cell. */
4486 ScreenLines[off_to + 1] = 0;
4487 redraw_next = TRUE;
4488 }
4489 else if (char_cells == 2
4490 && col + 2 < endcol
4491 && (*mb_off2cells)(off_to) == 1
4492 && (*mb_off2cells)(off_to + 1) > 1)
4493 {
4494 /* Writing the second half of a double-cell character over
4495 * a double-cell character: need to redraw the second
4496 * cell. */
4497 ScreenLines[off_to + 2] = 0;
4498 redraw_next = TRUE;
4499 }
4500
4501 if (enc_dbcs == DBCS_JPNU)
4502 ScreenLines2[off_to] = ScreenLines2[off_from];
4503 }
4504 /* When writing a single-width character over a double-width
4505 * character and at the end of the redrawn text, need to clear out
4506 * the right halve of the old character.
4507 * Also required when writing the right halve of a double-width
4508 * char over the left halve of an existing one. */
4509 if (has_mbyte && col + char_cells == endcol
4510 && ((char_cells == 1
4511 && (*mb_off2cells)(off_to) > 1)
4512 || (char_cells == 2
4513 && (*mb_off2cells)(off_to) == 1
4514 && (*mb_off2cells)(off_to + 1) > 1)))
4515 clear_next = TRUE;
4516#endif
4517
4518 ScreenLines[off_to] = ScreenLines[off_from];
4519#ifdef FEAT_MBYTE
4520 if (enc_utf8)
4521 {
4522 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4523 if (ScreenLinesUC[off_from] != 0)
4524 {
4525 ScreenLinesC1[off_to] = ScreenLinesC1[off_from];
4526 ScreenLinesC2[off_to] = ScreenLinesC2[off_from];
4527 }
4528 }
4529 if (char_cells == 2)
4530 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4531#endif
4532
4533#if defined(FEAT_GUI) || defined(UNIX)
4534 /* The bold trick makes a single row of pixels appear in the next
4535 * character. When a bold character is removed, the next
4536 * character should be redrawn too. This happens for our own GUI
4537 * and for some xterms. */
4538 if (
4539# ifdef FEAT_GUI
4540 gui.in_use
4541# endif
4542# if defined(FEAT_GUI) && defined(UNIX)
4543 ||
4544# endif
4545# ifdef UNIX
4546 term_is_xterm
4547# endif
4548 )
4549 {
4550 hl = ScreenAttrs[off_to];
4551 if (hl > HL_ALL || (hl & HL_BOLD))
4552 redraw_next = TRUE;
4553 }
4554#endif
4555 ScreenAttrs[off_to] = ScreenAttrs[off_from];
4556#ifdef FEAT_MBYTE
4557 if (enc_dbcs != 0 && char_cells == 2)
4558 {
4559 /* just a hack: It makes two bytes of DBCS have same attr */
4560 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
4561 screen_char_2(off_to, row, col + coloff);
4562 }
4563 else
4564#endif
4565 screen_char(off_to, row, col + coloff);
4566 }
4567 else if ( p_wiv
4568#ifdef FEAT_GUI
4569 && !gui.in_use
4570#endif
4571 && col + coloff > 0)
4572 {
4573 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
4574 {
4575 /*
4576 * Don't output stop-highlight when moving the cursor, it will
4577 * stop the highlighting when it should continue.
4578 */
4579 screen_attr = 0;
4580 }
4581 else if (screen_attr != 0)
4582 screen_stop_highlight();
4583 }
4584
4585 off_to += CHAR_CELLS;
4586 off_from += CHAR_CELLS;
4587 col += CHAR_CELLS;
4588 }
4589
4590#ifdef FEAT_MBYTE
4591 if (clear_next)
4592 {
4593 /* Clear the second half of a double-wide character of which the left
4594 * half was overwritten with a single-wide character. */
4595 ScreenLines[off_to] = ' ';
4596 if (enc_utf8)
4597 ScreenLinesUC[off_to] = 0;
4598 screen_char(off_to, row, col + coloff);
4599 }
4600#endif
4601
4602 if (clear_width > 0
4603#ifdef FEAT_RIGHTLEFT
4604 && !rlflag
4605#endif
4606 )
4607 {
4608#ifdef FEAT_GUI
4609 int startCol = col;
4610#endif
4611
4612 /* blank out the rest of the line */
4613 while (col < clear_width && ScreenLines[off_to] == ' '
4614 && ScreenAttrs[off_to] == 0
4615#ifdef FEAT_MBYTE
4616 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4617#endif
4618 )
4619 {
4620 ++off_to;
4621 ++col;
4622 }
4623 if (col < clear_width)
4624 {
4625#ifdef FEAT_GUI
4626 /*
4627 * In the GUI, clearing the rest of the line may leave pixels
4628 * behind if the first character cleared was bold. Some bold
4629 * fonts spill over the left. In this case we redraw the previous
4630 * character too. If we didn't skip any blanks above, then we
4631 * only redraw if the character wasn't already redrawn anyway.
4632 */
4633 if (gui.in_use && (col > startCol || !redraw_this)
4634# ifdef FEAT_MBYTE
4635 && enc_dbcs == 0
4636# endif
4637 )
4638 {
4639 hl = ScreenAttrs[off_to];
4640 if (hl > HL_ALL || (hl & HL_BOLD))
4641 screen_char(off_to - 1, row, col + coloff - 1);
4642 }
4643#endif
4644 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
4645 ' ', ' ', 0);
4646#ifdef FEAT_VERTSPLIT
4647 off_to += clear_width - col;
4648 col = clear_width;
4649#endif
4650 }
4651 }
4652
4653 if (clear_width > 0)
4654 {
4655#ifdef FEAT_VERTSPLIT
4656 /* For a window that's left of another, draw the separator char. */
4657 if (col + coloff < Columns)
4658 {
4659 int c;
4660
4661 c = fillchar_vsep(&hl);
4662 if (ScreenLines[off_to] != c
4663# ifdef FEAT_MBYTE
4664 || (enc_utf8
4665 && ScreenLinesUC[off_to] != (c >= 0x80 ? c : 0))
4666# endif
4667 || ScreenAttrs[off_to] != hl)
4668 {
4669 ScreenLines[off_to] = c;
4670 ScreenAttrs[off_to] = hl;
4671# ifdef FEAT_MBYTE
4672 if (enc_utf8)
4673 {
4674 if (c >= 0x80)
4675 {
4676 ScreenLinesUC[off_to] = c;
4677 ScreenLinesC1[off_to] = 0;
4678 ScreenLinesC2[off_to] = 0;
4679 }
4680 else
4681 ScreenLinesUC[off_to] = 0;
4682 }
4683# endif
4684 screen_char(off_to, row, col + coloff);
4685 }
4686 }
4687 else
4688#endif
4689 LineWraps[row] = FALSE;
4690 }
4691}
4692
4693#ifdef FEAT_RIGHTLEFT
4694/*
4695 * Mirror text "str" for right-lieft displaying.
4696 */
4697 static void
4698rl_mirror(str)
4699 char_u *str;
4700{
4701 char_u *p1, *p2;
4702 int t;
4703
4704 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
4705 {
4706 t = *p1;
4707 *p1 = *p2;
4708 *p2 = t;
4709 }
4710}
4711#endif
4712
4713#if defined(FEAT_WINDOWS) || defined(PROTO)
4714/*
4715 * mark all status lines for redraw; used after first :cd
4716 */
4717 void
4718status_redraw_all()
4719{
4720 win_T *wp;
4721
4722 for (wp = firstwin; wp; wp = wp->w_next)
4723 if (wp->w_status_height)
4724 {
4725 wp->w_redr_status = TRUE;
4726 redraw_later(VALID);
4727 }
4728}
4729
4730/*
4731 * mark all status lines of the current buffer for redraw
4732 */
4733 void
4734status_redraw_curbuf()
4735{
4736 win_T *wp;
4737
4738 for (wp = firstwin; wp; wp = wp->w_next)
4739 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
4740 {
4741 wp->w_redr_status = TRUE;
4742 redraw_later(VALID);
4743 }
4744}
4745
4746/*
4747 * Redraw all status lines that need to be redrawn.
4748 */
4749 void
4750redraw_statuslines()
4751{
4752 win_T *wp;
4753
4754 for (wp = firstwin; wp; wp = wp->w_next)
4755 if (wp->w_redr_status)
4756 win_redr_status(wp);
4757}
4758#endif
4759
4760#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
4761/*
4762 * Redraw all status lines at the bottom of frame "frp".
4763 */
4764 void
4765win_redraw_last_status(frp)
4766 frame_T *frp;
4767{
4768 if (frp->fr_layout == FR_LEAF)
4769 frp->fr_win->w_redr_status = TRUE;
4770 else if (frp->fr_layout == FR_ROW)
4771 {
4772 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
4773 win_redraw_last_status(frp);
4774 }
4775 else /* frp->fr_layout == FR_COL */
4776 {
4777 frp = frp->fr_child;
4778 while (frp->fr_next != NULL)
4779 frp = frp->fr_next;
4780 win_redraw_last_status(frp);
4781 }
4782}
4783#endif
4784
4785#ifdef FEAT_VERTSPLIT
4786/*
4787 * Draw the verticap separator right of window "wp" starting with line "row".
4788 */
4789 static void
4790draw_vsep_win(wp, row)
4791 win_T *wp;
4792 int row;
4793{
4794 int hl;
4795 int c;
4796
4797 if (wp->w_vsep_width)
4798 {
4799 /* draw the vertical separator right of this window */
4800 c = fillchar_vsep(&hl);
4801 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
4802 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
4803 c, ' ', hl);
4804 }
4805}
4806#endif
4807
4808#ifdef FEAT_WILDMENU
4809static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004810static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812/*
4813 * Get the lenght of an item as it will be shown in the status line.
4814 */
4815 static int
4816status_match_len(xp, s)
4817 expand_T *xp;
4818 char_u *s;
4819{
4820 int len = 0;
4821
4822#ifdef FEAT_MENU
4823 int emenu = (xp->xp_context == EXPAND_MENUS
4824 || xp->xp_context == EXPAND_MENUNAMES);
4825
4826 /* Check for menu separators - replace with '|'. */
4827 if (emenu && menu_is_separator(s))
4828 return 1;
4829#endif
4830
4831 while (*s != NUL)
4832 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004833 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00004835 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004836 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838
4839 return len;
4840}
4841
4842/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004843 * Return TRUE for characters that are not displayed in a status match.
4844 * These are backslashes used for escaping. Do show backslashes in help tags.
4845 */
4846 static int
4847skip_status_match_char(xp, s)
4848 expand_T *xp;
4849 char_u *s;
4850{
4851 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
4852#ifdef FEAT_MENU
4853 || ((xp->xp_context == EXPAND_MENUS
4854 || xp->xp_context == EXPAND_MENUNAMES)
4855 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
4856#endif
4857 );
4858}
4859
4860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 * Show wildchar matches in the status line.
4862 * Show at least the "match" item.
4863 * We start at item 'first_match' in the list and show all matches that fit.
4864 *
4865 * If inversion is possible we use it. Else '=' characters are used.
4866 */
4867 void
4868win_redr_status_matches(xp, num_matches, matches, match, showtail)
4869 expand_T *xp;
4870 int num_matches;
4871 char_u **matches; /* list of matches */
4872 int match;
4873 int showtail;
4874{
4875#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
4876 int row;
4877 char_u *buf;
4878 int len;
4879 int clen; /* lenght in screen cells */
4880 int fillchar;
4881 int attr;
4882 int i;
4883 int highlight = TRUE;
4884 char_u *selstart = NULL;
4885 int selstart_col = 0;
4886 char_u *selend = NULL;
4887 static int first_match = 0;
4888 int add_left = FALSE;
4889 char_u *s;
4890#ifdef FEAT_MENU
4891 int emenu;
4892#endif
4893#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
4894 int l;
4895#endif
4896
4897 if (matches == NULL) /* interrupted completion? */
4898 return;
4899
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004900#ifdef FEAT_MBYTE
4901 if (has_mbyte)
4902 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
4903 else
4904#endif
4905 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (buf == NULL)
4907 return;
4908
4909 if (match == -1) /* don't show match but original text */
4910 {
4911 match = 0;
4912 highlight = FALSE;
4913 }
4914 /* count 1 for the ending ">" */
4915 clen = status_match_len(xp, L_MATCH(match)) + 3;
4916 if (match == 0)
4917 first_match = 0;
4918 else if (match < first_match)
4919 {
4920 /* jumping left, as far as we can go */
4921 first_match = match;
4922 add_left = TRUE;
4923 }
4924 else
4925 {
4926 /* check if match fits on the screen */
4927 for (i = first_match; i < match; ++i)
4928 clen += status_match_len(xp, L_MATCH(i)) + 2;
4929 if (first_match > 0)
4930 clen += 2;
4931 /* jumping right, put match at the left */
4932 if ((long)clen > Columns)
4933 {
4934 first_match = match;
4935 /* if showing the last match, we can add some on the left */
4936 clen = 2;
4937 for (i = match; i < num_matches; ++i)
4938 {
4939 clen += status_match_len(xp, L_MATCH(i)) + 2;
4940 if ((long)clen >= Columns)
4941 break;
4942 }
4943 if (i == num_matches)
4944 add_left = TRUE;
4945 }
4946 }
4947 if (add_left)
4948 while (first_match > 0)
4949 {
4950 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
4951 if ((long)clen >= Columns)
4952 break;
4953 --first_match;
4954 }
4955
4956 fillchar = fillchar_status(&attr, TRUE);
4957
4958 if (first_match == 0)
4959 {
4960 *buf = NUL;
4961 len = 0;
4962 }
4963 else
4964 {
4965 STRCPY(buf, "< ");
4966 len = 2;
4967 }
4968 clen = len;
4969
4970 i = first_match;
4971 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
4972 {
4973 if (i == match)
4974 {
4975 selstart = buf + len;
4976 selstart_col = clen;
4977 }
4978
4979 s = L_MATCH(i);
4980 /* Check for menu separators - replace with '|' */
4981#ifdef FEAT_MENU
4982 emenu = (xp->xp_context == EXPAND_MENUS
4983 || xp->xp_context == EXPAND_MENUNAMES);
4984 if (emenu && menu_is_separator(s))
4985 {
4986 STRCPY(buf + len, transchar('|'));
4987 l = (int)STRLEN(buf + len);
4988 len += l;
4989 clen += l;
4990 }
4991 else
4992#endif
4993 for ( ; *s != NUL; ++s)
4994 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00004995 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 ++s;
4997 clen += ptr2cells(s);
4998#ifdef FEAT_MBYTE
4999 if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
5000 {
5001 STRNCPY(buf + len, s, l);
5002 s += l - 1;
5003 len += l;
5004 }
5005 else
5006#endif
5007 {
5008 STRCPY(buf + len, transchar_byte(*s));
5009 len += (int)STRLEN(buf + len);
5010 }
5011 }
5012 if (i == match)
5013 selend = buf + len;
5014
5015 *(buf + len++) = ' ';
5016 *(buf + len++) = ' ';
5017 clen += 2;
5018 if (++i == num_matches)
5019 break;
5020 }
5021
5022 if (i != num_matches)
5023 {
5024 *(buf + len++) = '>';
5025 ++clen;
5026 }
5027
5028 buf[len] = NUL;
5029
5030 row = cmdline_row - 1;
5031 if (row >= 0)
5032 {
5033 if (wild_menu_showing == 0)
5034 {
5035 if (msg_scrolled > 0)
5036 {
5037 /* Put the wildmenu just above the command line. If there is
5038 * no room, scroll the screen one line up. */
5039 if (cmdline_row == Rows - 1)
5040 {
5041 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5042 ++msg_scrolled;
5043 }
5044 else
5045 {
5046 ++cmdline_row;
5047 ++row;
5048 }
5049 wild_menu_showing = WM_SCROLLED;
5050 }
5051 else
5052 {
5053 /* Create status line if needed by setting 'laststatus' to 2.
5054 * Set 'winminheight' to zero to avoid that the window is
5055 * resized. */
5056 if (lastwin->w_status_height == 0)
5057 {
5058 save_p_ls = p_ls;
5059 save_p_wmh = p_wmh;
5060 p_ls = 2;
5061 p_wmh = 0;
5062 last_status(FALSE);
5063 }
5064 wild_menu_showing = WM_SHOWN;
5065 }
5066 }
5067
5068 screen_puts(buf, row, 0, attr);
5069 if (selstart != NULL && highlight)
5070 {
5071 *selend = NUL;
5072 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5073 }
5074
5075 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5076 }
5077
5078#ifdef FEAT_VERTSPLIT
5079 win_redraw_last_status(topframe);
5080#else
5081 lastwin->w_redr_status = TRUE;
5082#endif
5083 vim_free(buf);
5084}
5085#endif
5086
5087#if defined(FEAT_WINDOWS) || defined(PROTO)
5088/*
5089 * Redraw the status line of window wp.
5090 *
5091 * If inversion is possible we use it. Else '=' characters are used.
5092 */
5093 void
5094win_redr_status(wp)
5095 win_T *wp;
5096{
5097 int row;
5098 char_u *p;
5099 int len;
5100 int fillchar;
5101 int attr;
5102 int this_ru_col;
5103
5104 wp->w_redr_status = FALSE;
5105 if (wp->w_status_height == 0)
5106 {
5107 /* no status line, can only be last window */
5108 redraw_cmdline = TRUE;
5109 }
5110 else if (!redrawing())
5111 {
5112 /* Don't redraw right now, do it later. */
5113 wp->w_redr_status = TRUE;
5114 }
5115#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005116 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 /* redraw custom status line */
5119 win_redr_custom(wp, FALSE);
5120 }
5121#endif
5122 else
5123 {
5124 fillchar = fillchar_status(&attr, wp == curwin);
5125
5126 if (buf_spname(wp->w_buffer) != NULL)
5127 STRCPY(NameBuff, buf_spname(wp->w_buffer));
5128 else
5129 home_replace(wp->w_buffer, wp->w_buffer->b_fname, NameBuff,
5130 MAXPATHL, TRUE);
5131 trans_characters(NameBuff, MAXPATHL);
5132 p = NameBuff;
5133 len = (int)STRLEN(p);
5134
5135 if (wp->w_buffer->b_help
5136#ifdef FEAT_QUICKFIX
5137 || wp->w_p_pvw
5138#endif
5139 || bufIsChanged(wp->w_buffer)
5140 || wp->w_buffer->b_p_ro)
5141 *(p + len++) = ' ';
5142 if (wp->w_buffer->b_help)
5143 {
5144 STRCPY(p + len, _("[help]"));
5145 len += (int)STRLEN(p + len);
5146 }
5147#ifdef FEAT_QUICKFIX
5148 if (wp->w_p_pvw)
5149 {
5150 STRCPY(p + len, _("[Preview]"));
5151 len += (int)STRLEN(p + len);
5152 }
5153#endif
5154 if (bufIsChanged(wp->w_buffer))
5155 {
5156 STRCPY(p + len, "[+]");
5157 len += 3;
5158 }
5159 if (wp->w_buffer->b_p_ro)
5160 {
5161 STRCPY(p + len, "[RO]");
5162 len += 4;
5163 }
5164
5165#ifndef FEAT_VERTSPLIT
5166 this_ru_col = ru_col;
5167 if (this_ru_col < (Columns + 1) / 2)
5168 this_ru_col = (Columns + 1) / 2;
5169#else
5170 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5171 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5172 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5173 if (this_ru_col <= 1)
5174 {
5175 p = (char_u *)"<"; /* No room for file name! */
5176 len = 1;
5177 }
5178 else
5179#endif
5180#ifdef FEAT_MBYTE
5181 if (has_mbyte)
5182 {
5183 int clen = 0, i;
5184
5185 /* Count total number of display cells. */
5186 for (i = 0; p[i] != NUL; i += (*mb_ptr2len_check)(p + i))
5187 clen += (*mb_ptr2cells)(p + i);
5188 /* Find first character that will fit.
5189 * Going from start to end is much faster for DBCS. */
5190 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
5191 i += (*mb_ptr2len_check)(p + i))
5192 clen -= (*mb_ptr2cells)(p + i);
5193 len = clen;
5194 if (i > 0)
5195 {
5196 p = p + i - 1;
5197 *p = '<';
5198 ++len;
5199 }
5200
5201 }
5202 else
5203#endif
5204 if (len > this_ru_col - 1)
5205 {
5206 p += len - (this_ru_col - 1);
5207 *p = '<';
5208 len = this_ru_col - 1;
5209 }
5210
5211 row = W_WINROW(wp) + wp->w_height;
5212 screen_puts(p, row, W_WINCOL(wp), attr);
5213 screen_fill(row, row + 1, len + W_WINCOL(wp),
5214 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5215
5216 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5217 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5218 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5219 - 1 + W_WINCOL(wp)), attr);
5220
5221#ifdef FEAT_CMDL_INFO
5222 win_redr_ruler(wp, TRUE);
5223#endif
5224 }
5225
5226#ifdef FEAT_VERTSPLIT
5227 /*
5228 * May need to draw the character below the vertical separator.
5229 */
5230 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5231 {
5232 if (stl_connected(wp))
5233 fillchar = fillchar_status(&attr, wp == curwin);
5234 else
5235 fillchar = fillchar_vsep(&attr);
5236 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5237 attr);
5238 }
5239#endif
5240}
5241
5242# ifdef FEAT_VERTSPLIT
5243/*
5244 * Return TRUE if the status line of window "wp" is connected to the status
5245 * line of the window right of it. If not, then it's a vertical separator.
5246 * Only call if (wp->w_vsep_width != 0).
5247 */
5248 int
5249stl_connected(wp)
5250 win_T *wp;
5251{
5252 frame_T *fr;
5253
5254 fr = wp->w_frame;
5255 while (fr->fr_parent != NULL)
5256 {
5257 if (fr->fr_parent->fr_layout == FR_COL)
5258 {
5259 if (fr->fr_next != NULL)
5260 break;
5261 }
5262 else
5263 {
5264 if (fr->fr_next != NULL)
5265 return TRUE;
5266 }
5267 fr = fr->fr_parent;
5268 }
5269 return FALSE;
5270}
5271# endif
5272
5273#endif /* FEAT_WINDOWS */
5274
5275#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5276/*
5277 * Get the value to show for the language mappings, active 'keymap'.
5278 */
5279 int
5280get_keymap_str(wp, buf, len)
5281 win_T *wp;
5282 char_u *buf; /* buffer for the result */
5283 int len; /* length of buffer */
5284{
5285 char_u *p;
5286
5287 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5288 return FALSE;
5289
5290 {
5291#ifdef FEAT_EVAL
5292 buf_T *old_curbuf = curbuf;
5293 win_T *old_curwin = curwin;
5294 char_u *s;
5295
5296 curbuf = wp->w_buffer;
5297 curwin = wp;
5298 STRCPY(buf, "b:keymap_name"); /* must be writable */
5299 ++emsg_skip;
5300 s = p = eval_to_string(buf, NULL);
5301 --emsg_skip;
5302 curbuf = old_curbuf;
5303 curwin = old_curwin;
5304 if (p == NULL || *p == NUL)
5305#endif
5306 {
5307#ifdef FEAT_KEYMAP
5308 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5309 p = wp->w_buffer->b_p_keymap;
5310 else
5311#endif
5312 p = (char_u *)"lang";
5313 }
5314 if ((int)(STRLEN(p) + 3) < len)
5315 sprintf((char *)buf, "<%s>", p);
5316 else
5317 buf[0] = NUL;
5318#ifdef FEAT_EVAL
5319 vim_free(s);
5320#endif
5321 }
5322 return buf[0] != NUL;
5323}
5324#endif
5325
5326#if defined(FEAT_STL_OPT) || defined(PROTO)
5327/*
5328 * Redraw the status line or ruler of window wp.
5329 */
5330 static void
5331win_redr_custom(wp, Ruler)
5332 win_T *wp;
5333 int Ruler;
5334{
5335 int attr;
5336 int curattr;
5337 int row;
5338 int col = 0;
5339 int maxwidth;
5340 int width;
5341 int n;
5342 int len;
5343 int fillchar;
5344 char_u buf[MAXPATHL];
5345 char_u *p;
5346 struct stl_hlrec hl[STL_MAX_ITEM];
5347
5348 /* setup environment for the task at hand */
5349 row = W_WINROW(wp) + wp->w_height;
5350 fillchar = fillchar_status(&attr, wp == curwin);
5351 maxwidth = W_WIDTH(wp);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005352 if (*wp->w_p_stl != NUL)
5353 p = wp->w_p_stl;
5354 else
5355 p = p_stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (Ruler)
5357 {
5358 p = p_ruf;
5359 /* advance past any leading group spec - implicit in ru_col */
5360 if (*p == '%')
5361 {
5362 if (*++p == '-')
5363 p++;
5364 if (atoi((char *) p))
5365 while (VIM_ISDIGIT(*p))
5366 p++;
5367 if (*p++ != '(')
5368 p = p_ruf;
5369 }
5370#ifdef FEAT_VERTSPLIT
5371 col = ru_col - (Columns - W_WIDTH(wp));
5372 if (col < (W_WIDTH(wp) + 1) / 2)
5373 col = (W_WIDTH(wp) + 1) / 2;
5374#else
5375 col = ru_col;
5376 if (col > (Columns + 1) / 2)
5377 col = (Columns + 1) / 2;
5378#endif
5379 maxwidth = W_WIDTH(wp) - col;
5380#ifdef FEAT_WINDOWS
5381 if (!wp->w_status_height)
5382#endif
5383 {
5384 row = Rows - 1;
5385 --maxwidth; /* writing in last column may cause scrolling */
5386 fillchar = ' ';
5387 attr = 0;
5388 }
5389 }
5390 if (maxwidth <= 0)
5391 return;
5392#ifdef FEAT_VERTSPLIT
5393 col += W_WINCOL(wp);
5394#endif
5395
5396 width = build_stl_str_hl(wp, buf, sizeof(buf), p, fillchar, maxwidth, hl);
5397 len = STRLEN(buf);
5398
5399 while (width < maxwidth && len < sizeof(buf) - 1)
5400 {
5401#ifdef FEAT_MBYTE
5402 len += (*mb_char2bytes)(fillchar, buf + len);
5403#else
5404 buf[len++] = fillchar;
5405#endif
5406 ++width;
5407 }
5408 buf[len] = NUL;
5409
5410 curattr = attr;
5411 p = buf;
5412 for (n = 0; hl[n].start != NULL; n++)
5413 {
5414 len = (int)(hl[n].start - p);
5415 screen_puts_len(p, len, row, col, curattr);
5416 col += vim_strnsize(p, len);
5417 p = hl[n].start;
5418
5419 if (hl[n].userhl == 0)
5420 curattr = attr;
5421#ifdef FEAT_WINDOWS
5422 else if (wp != curwin && wp->w_status_height != 0)
5423 curattr = highlight_stlnc[hl[n].userhl - 1];
5424#endif
5425 else
5426 curattr = highlight_user[hl[n].userhl - 1];
5427 }
5428 screen_puts(p, row, col, curattr);
5429}
5430
5431#endif /* FEAT_STL_OPT */
5432
5433/*
5434 * Output a single character directly to the screen and update ScreenLines.
5435 */
5436 void
5437screen_putchar(c, row, col, attr)
5438 int c;
5439 int row, col;
5440 int attr;
5441{
5442#ifdef FEAT_MBYTE
5443 char_u buf[MB_MAXBYTES + 1];
5444
5445 buf[(*mb_char2bytes)(c, buf)] = NUL;
5446#else
5447 char_u buf[2];
5448
5449 buf[0] = c;
5450 buf[1] = NUL;
5451#endif
5452 screen_puts(buf, row, col, attr);
5453}
5454
5455/*
5456 * Get a single character directly from ScreenLines into "bytes[]".
5457 * Also return its attribute in *attrp;
5458 */
5459 void
5460screen_getbytes(row, col, bytes, attrp)
5461 int row, col;
5462 char_u *bytes;
5463 int *attrp;
5464{
5465 unsigned off;
5466
5467 /* safety check */
5468 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
5469 {
5470 off = LineOffset[row] + col;
5471 *attrp = ScreenAttrs[off];
5472 bytes[0] = ScreenLines[off];
5473 bytes[1] = NUL;
5474
5475#ifdef FEAT_MBYTE
5476 if (enc_utf8 && ScreenLinesUC[off] != 0)
5477 bytes[utfc_char2bytes(off, bytes)] = NUL;
5478 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
5479 {
5480 bytes[0] = ScreenLines[off];
5481 bytes[1] = ScreenLines2[off];
5482 bytes[2] = NUL;
5483 }
5484 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
5485 {
5486 bytes[1] = ScreenLines[off + 1];
5487 bytes[2] = NUL;
5488 }
5489#endif
5490 }
5491}
5492
5493/*
5494 * Put string '*text' on the screen at position 'row' and 'col', with
5495 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
5496 * Note: only outputs within one row, message is truncated at screen boundary!
5497 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
5498 */
5499 void
5500screen_puts(text, row, col, attr)
5501 char_u *text;
5502 int row;
5503 int col;
5504 int attr;
5505{
5506 screen_puts_len(text, -1, row, col, attr);
5507}
5508
5509/*
5510 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
5511 * a NUL.
5512 */
5513 void
5514screen_puts_len(text, len, row, col, attr)
5515 char_u *text;
5516 int len;
5517 int row;
5518 int col;
5519 int attr;
5520{
5521 unsigned off;
5522 char_u *ptr = text;
5523 int c;
5524#ifdef FEAT_MBYTE
5525 int mbyte_blen = 1;
5526 int mbyte_cells = 1;
5527 int u8c = 0;
5528 int u8c_c1 = 0;
5529 int u8c_c2 = 0;
5530 int clear_next_cell = FALSE;
5531# ifdef FEAT_ARABIC
5532 int prev_c = 0; /* previous Arabic character */
5533 int pc, nc, nc1, dummy;
5534# endif
5535#endif
5536
5537 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
5538 return;
5539
5540 off = LineOffset[row] + col;
5541 while (*ptr != NUL && col < screen_Columns
5542 && (len < 0 || (int)(ptr - text) < len))
5543 {
5544 c = *ptr;
5545#ifdef FEAT_MBYTE
5546 /* check if this is the first byte of a multibyte */
5547 if (has_mbyte)
5548 {
5549 if (enc_utf8 && len > 0)
5550 mbyte_blen = utfc_ptr2len_check_len(ptr,
5551 (int)((text + len) - ptr));
5552 else
5553 mbyte_blen = (*mb_ptr2len_check)(ptr);
5554 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
5555 mbyte_cells = 1;
5556 else if (enc_dbcs != 0)
5557 mbyte_cells = mbyte_blen;
5558 else /* enc_utf8 */
5559 {
5560 if (len >= 0)
5561 u8c = utfc_ptr2char_len(ptr, &u8c_c1, &u8c_c2,
5562 (int)((text + len) - ptr));
5563 else
5564 u8c = utfc_ptr2char(ptr, &u8c_c1, &u8c_c2);
5565 mbyte_cells = utf_char2cells(u8c);
5566 /* Non-BMP character: display as ? or fullwidth ?. */
5567 if (u8c >= 0x10000)
5568 {
5569 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
5570 if (attr == 0)
5571 attr = hl_attr(HLF_8);
5572 }
5573# ifdef FEAT_ARABIC
5574 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
5575 {
5576 /* Do Arabic shaping. */
5577 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
5578 {
5579 /* Past end of string to be displayed. */
5580 nc = NUL;
5581 nc1 = NUL;
5582 }
5583 else
5584 nc = utfc_ptr2char(ptr + mbyte_blen, &nc1, &dummy);
5585 pc = prev_c;
5586 prev_c = u8c;
5587 u8c = arabic_shape(u8c, &c, &u8c_c1, nc, nc1, pc);
5588 }
5589 else
5590 prev_c = u8c;
5591# endif
5592 }
5593 }
5594#endif
5595
5596 if (ScreenLines[off] != c
5597#ifdef FEAT_MBYTE
5598 || (mbyte_cells == 2
5599 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
5600 || (enc_dbcs == DBCS_JPNU
5601 && c == 0x8e
5602 && ScreenLines2[off] != ptr[1])
5603 || (enc_utf8
5604 && mbyte_blen > 1
5605 && (ScreenLinesUC[off] != u8c
5606 || ScreenLinesC1[off] != u8c_c1
5607 || ScreenLinesC2[off] != u8c_c2))
5608#endif
5609 || ScreenAttrs[off] != attr
5610 || exmode_active
5611 )
5612 {
5613#if defined(FEAT_GUI) || defined(UNIX)
5614 /* The bold trick makes a single row of pixels appear in the next
5615 * character. When a bold character is removed, the next
5616 * character should be redrawn too. This happens for our own GUI
5617 * and for some xterms.
5618 * Force the redraw by setting the attribute to a different value
5619 * than "attr", the contents of ScreenLines[] may be needed by
5620 * mb_off2cells() further on.
5621 * Don't do this for the last drawn character, because the next
5622 * character may not be redrawn. */
5623 if (
5624# ifdef FEAT_GUI
5625 gui.in_use
5626# endif
5627# if defined(FEAT_GUI) && defined(UNIX)
5628 ||
5629# endif
5630# ifdef UNIX
5631 term_is_xterm
5632# endif
5633 )
5634 {
5635 int n;
5636
5637 n = ScreenAttrs[off];
5638# ifdef FEAT_MBYTE
5639 if (col + mbyte_cells < screen_Columns
5640 && (n > HL_ALL || (n & HL_BOLD))
5641 && (len < 0 ? ptr[mbyte_blen] != NUL
5642 : ptr + mbyte_blen < text + len))
5643 ScreenAttrs[off + mbyte_cells] = attr + 1;
5644# else
5645 if (col + 1 < screen_Columns
5646 && (n > HL_ALL || (n & HL_BOLD))
5647 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
5648 ScreenLines[off + 1] = 0;
5649# endif
5650 }
5651#endif
5652#ifdef FEAT_MBYTE
5653 /* When at the end of the text and overwriting a two-cell
5654 * character with a one-cell character, need to clear the next
5655 * cell. Also when overwriting the left halve of a two-cell char
5656 * with the right halve of a two-cell char. Do this only once
5657 * (mb_off2cells() may return 2 on the right halve). */
5658 if (clear_next_cell)
5659 clear_next_cell = FALSE;
5660 else if (has_mbyte
5661 && (len < 0 ? ptr[mbyte_blen] == NUL
5662 : ptr + mbyte_blen >= text + len)
5663 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
5664 || (mbyte_cells == 2
5665 && (*mb_off2cells)(off) == 1
5666 && (*mb_off2cells)(off + 1) > 1)))
5667 clear_next_cell = TRUE;
5668
5669 /* Make sure we never leave a second byte of a double-byte behind,
5670 * it confuses mb_off2cells(). */
5671 if (enc_dbcs
5672 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
5673 || (mbyte_cells == 2
5674 && (*mb_off2cells)(off) == 1
5675 && (*mb_off2cells)(off + 1) > 1)))
5676 ScreenLines[off + mbyte_blen] = 0;
5677#endif
5678 ScreenLines[off] = c;
5679 ScreenAttrs[off] = attr;
5680#ifdef FEAT_MBYTE
5681 if (enc_utf8)
5682 {
5683 if (c < 0x80 && u8c_c1 == 0 && u8c_c2 == 0)
5684 ScreenLinesUC[off] = 0;
5685 else
5686 {
5687 ScreenLinesUC[off] = u8c;
5688 ScreenLinesC1[off] = u8c_c1;
5689 ScreenLinesC2[off] = u8c_c2;
5690 }
5691 if (mbyte_cells == 2)
5692 {
5693 ScreenLines[off + 1] = 0;
5694 ScreenAttrs[off + 1] = attr;
5695 }
5696 screen_char(off, row, col);
5697 }
5698 else if (mbyte_cells == 2)
5699 {
5700 ScreenLines[off + 1] = ptr[1];
5701 ScreenAttrs[off + 1] = attr;
5702 screen_char_2(off, row, col);
5703 }
5704 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
5705 {
5706 ScreenLines2[off] = ptr[1];
5707 screen_char(off, row, col);
5708 }
5709 else
5710#endif
5711 screen_char(off, row, col);
5712 }
5713#ifdef FEAT_MBYTE
5714 if (has_mbyte)
5715 {
5716 off += mbyte_cells;
5717 col += mbyte_cells;
5718 ptr += mbyte_blen;
5719 if (clear_next_cell)
5720 ptr = (char_u *)" ";
5721 }
5722 else
5723#endif
5724 {
5725 ++off;
5726 ++col;
5727 ++ptr;
5728 }
5729 }
5730}
5731
5732#ifdef FEAT_SEARCH_EXTRA
5733/*
5734 * Prepare for 'searchhl' highlighting.
5735 */
5736 static void
5737start_search_hl()
5738{
5739 if (p_hls && !no_hlsearch)
5740 {
5741 last_pat_prog(&search_hl.rm);
5742 search_hl.attr = hl_attr(HLF_L);
5743 }
5744}
5745
5746/*
5747 * Clean up for 'searchhl' highlighting.
5748 */
5749 static void
5750end_search_hl()
5751{
5752 if (search_hl.rm.regprog != NULL)
5753 {
5754 vim_free(search_hl.rm.regprog);
5755 search_hl.rm.regprog = NULL;
5756 }
5757}
5758
5759/*
5760 * Advance to the match in window "wp" line "lnum" or past it.
5761 */
5762 static void
5763prepare_search_hl(wp, lnum)
5764 win_T *wp;
5765 linenr_T lnum;
5766{
5767 match_T *shl; /* points to search_hl or match_hl */
5768 int n;
5769
5770 /*
5771 * When using a multi-line pattern, start searching at the top
5772 * of the window or just after a closed fold.
5773 * Do this both for search_hl and match_hl.
5774 */
5775 shl = &search_hl;
5776 for (;;)
5777 {
5778 if (shl->rm.regprog != NULL
5779 && shl->lnum == 0
5780 && re_multiline(shl->rm.regprog))
5781 {
5782 if (shl->first_lnum == 0)
5783 {
5784# ifdef FEAT_FOLDING
5785 for (shl->first_lnum = lnum;
5786 shl->first_lnum > wp->w_topline; --shl->first_lnum)
5787 if (hasFoldingWin(wp, shl->first_lnum - 1,
5788 NULL, NULL, TRUE, NULL))
5789 break;
5790# else
5791 shl->first_lnum = wp->w_topline;
5792# endif
5793 }
5794 n = 0;
5795 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
5796 {
5797 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
5798 if (shl->lnum != 0)
5799 {
5800 shl->first_lnum = shl->lnum
5801 + shl->rm.endpos[0].lnum
5802 - shl->rm.startpos[0].lnum;
5803 n = shl->rm.endpos[0].col;
5804 }
5805 else
5806 {
5807 ++shl->first_lnum;
5808 n = 0;
5809 }
5810 }
5811 }
5812 if (shl == &match_hl)
5813 break;
5814 shl = &match_hl;
5815 }
5816}
5817
5818/*
5819 * Search for a next 'searchl' or ":match" match.
5820 * Uses shl->buf.
5821 * Sets shl->lnum and shl->rm contents.
5822 * Note: Assumes a previous match is always before "lnum", unless
5823 * shl->lnum is zero.
5824 * Careful: Any pointers for buffer lines will become invalid.
5825 */
5826 static void
5827next_search_hl(win, shl, lnum, mincol)
5828 win_T *win;
5829 match_T *shl; /* points to search_hl or match_hl */
5830 linenr_T lnum;
5831 colnr_T mincol; /* minimal column for a match */
5832{
5833 linenr_T l;
5834 colnr_T matchcol;
5835 long nmatched;
5836
5837 if (shl->lnum != 0)
5838 {
5839 /* Check for three situations:
5840 * 1. If the "lnum" is below a previous match, start a new search.
5841 * 2. If the previous match includes "mincol", use it.
5842 * 3. Continue after the previous match.
5843 */
5844 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
5845 if (lnum > l)
5846 shl->lnum = 0;
5847 else if (lnum < l || shl->rm.endpos[0].col > mincol)
5848 return;
5849 }
5850
5851 /*
5852 * Repeat searching for a match until one is found that includes "mincol"
5853 * or none is found in this line.
5854 */
5855 called_emsg = FALSE;
5856 for (;;)
5857 {
5858 /* Three situations:
5859 * 1. No useful previous match: search from start of line.
5860 * 2. Not Vi compatible or empty match: continue at next character.
5861 * Break the loop if this is beyond the end of the line.
5862 * 3. Vi compatible searching: continue at end of previous match.
5863 */
5864 if (shl->lnum == 0)
5865 matchcol = 0;
5866 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
5867 || (shl->rm.endpos[0].lnum == 0
5868 && shl->rm.endpos[0].col == shl->rm.startpos[0].col))
5869 {
5870 matchcol = shl->rm.startpos[0].col + 1;
5871 if (ml_get_buf(shl->buf, lnum, FALSE)[matchcol - 1] == NUL)
5872 {
5873 shl->lnum = 0;
5874 break;
5875 }
5876 }
5877 else
5878 matchcol = shl->rm.endpos[0].col;
5879
5880 shl->lnum = lnum;
5881 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
5882 if (called_emsg)
5883 {
5884 /* Error while handling regexp: stop using this regexp. */
5885 vim_free(shl->rm.regprog);
5886 shl->rm.regprog = NULL;
5887 no_hlsearch = TRUE;
5888 break;
5889 }
5890 if (nmatched == 0)
5891 {
5892 shl->lnum = 0; /* no match found */
5893 break;
5894 }
5895 if (shl->rm.startpos[0].lnum > 0
5896 || shl->rm.startpos[0].col >= mincol
5897 || nmatched > 1
5898 || shl->rm.endpos[0].col > mincol)
5899 {
5900 shl->lnum += shl->rm.startpos[0].lnum;
5901 break; /* useful match found */
5902 }
5903 }
5904}
5905#endif
5906
5907 static void
5908screen_start_highlight(attr)
5909 int attr;
5910{
5911 attrentry_T *aep = NULL;
5912
5913 screen_attr = attr;
5914 if (full_screen
5915#ifdef WIN3264
5916 && termcap_active
5917#endif
5918 )
5919 {
5920#ifdef FEAT_GUI
5921 if (gui.in_use)
5922 {
5923 char buf[20];
5924
5925 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr); /* internal GUI code */
5926 OUT_STR(buf);
5927 }
5928 else
5929#endif
5930 {
5931 if (attr > HL_ALL) /* special HL attr. */
5932 {
5933 if (t_colors > 1)
5934 aep = syn_cterm_attr2entry(attr);
5935 else
5936 aep = syn_term_attr2entry(attr);
5937 if (aep == NULL) /* did ":syntax clear" */
5938 attr = 0;
5939 else
5940 attr = aep->ae_attr;
5941 }
5942 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
5943 out_str(T_MD);
5944 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
5945 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005946 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
5947 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 out_str(T_US);
5949 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
5950 out_str(T_CZH);
5951 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
5952 out_str(T_MR);
5953
5954 /*
5955 * Output the color or start string after bold etc., in case the
5956 * bold etc. override the color setting.
5957 */
5958 if (aep != NULL)
5959 {
5960 if (t_colors > 1)
5961 {
5962 if (aep->ae_u.cterm.fg_color)
5963 term_fg_color(aep->ae_u.cterm.fg_color - 1);
5964 if (aep->ae_u.cterm.bg_color)
5965 term_bg_color(aep->ae_u.cterm.bg_color - 1);
5966 }
5967 else
5968 {
5969 if (aep->ae_u.term.start != NULL)
5970 out_str(aep->ae_u.term.start);
5971 }
5972 }
5973 }
5974 }
5975}
5976
5977 void
5978screen_stop_highlight()
5979{
5980 int do_ME = FALSE; /* output T_ME code */
5981
5982 if (screen_attr != 0
5983#ifdef WIN3264
5984 && termcap_active
5985#endif
5986 )
5987 {
5988#ifdef FEAT_GUI
5989 if (gui.in_use)
5990 {
5991 char buf[20];
5992
5993 /* use internal GUI code */
5994 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
5995 OUT_STR(buf);
5996 }
5997 else
5998#endif
5999 {
6000 if (screen_attr > HL_ALL) /* special HL attr. */
6001 {
6002 attrentry_T *aep;
6003
6004 if (t_colors > 1)
6005 {
6006 /*
6007 * Assume that t_me restores the original colors!
6008 */
6009 aep = syn_cterm_attr2entry(screen_attr);
6010 if (aep != NULL && (aep->ae_u.cterm.fg_color
6011 || aep->ae_u.cterm.bg_color))
6012 do_ME = TRUE;
6013 }
6014 else
6015 {
6016 aep = syn_term_attr2entry(screen_attr);
6017 if (aep != NULL && aep->ae_u.term.stop != NULL)
6018 {
6019 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6020 do_ME = TRUE;
6021 else
6022 out_str(aep->ae_u.term.stop);
6023 }
6024 }
6025 if (aep == NULL) /* did ":syntax clear" */
6026 screen_attr = 0;
6027 else
6028 screen_attr = aep->ae_attr;
6029 }
6030
6031 /*
6032 * Often all ending-codes are equal to T_ME. Avoid outputting the
6033 * same sequence several times.
6034 */
6035 if (screen_attr & HL_STANDOUT)
6036 {
6037 if (STRCMP(T_SE, T_ME) == 0)
6038 do_ME = TRUE;
6039 else
6040 out_str(T_SE);
6041 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006042 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 {
6044 if (STRCMP(T_UE, T_ME) == 0)
6045 do_ME = TRUE;
6046 else
6047 out_str(T_UE);
6048 }
6049 if (screen_attr & HL_ITALIC)
6050 {
6051 if (STRCMP(T_CZR, T_ME) == 0)
6052 do_ME = TRUE;
6053 else
6054 out_str(T_CZR);
6055 }
6056 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6057 out_str(T_ME);
6058
6059 if (t_colors > 1)
6060 {
6061 /* set Normal cterm colors */
6062 if (cterm_normal_fg_color != 0)
6063 term_fg_color(cterm_normal_fg_color - 1);
6064 if (cterm_normal_bg_color != 0)
6065 term_bg_color(cterm_normal_bg_color - 1);
6066 if (cterm_normal_fg_bold)
6067 out_str(T_MD);
6068 }
6069 }
6070 }
6071 screen_attr = 0;
6072}
6073
6074/*
6075 * Reset the colors for a cterm. Used when leaving Vim.
6076 * The machine specific code may override this again.
6077 */
6078 void
6079reset_cterm_colors()
6080{
6081 if (t_colors > 1)
6082 {
6083 /* set Normal cterm colors */
6084 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6085 {
6086 out_str(T_OP);
6087 screen_attr = -1;
6088 }
6089 if (cterm_normal_fg_bold)
6090 {
6091 out_str(T_ME);
6092 screen_attr = -1;
6093 }
6094 }
6095}
6096
6097/*
6098 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6099 * using the attributes from ScreenAttrs["off"].
6100 */
6101 static void
6102screen_char(off, row, col)
6103 unsigned off;
6104 int row;
6105 int col;
6106{
6107 int attr;
6108
6109 /* Check for illegal values, just in case (could happen just after
6110 * resizing). */
6111 if (row >= screen_Rows || col >= screen_Columns)
6112 return;
6113
6114 /* Outputting the last character on the screen may scrollup the screen.
6115 * Don't to it! Mark the character invalid (update it when scrolled up) */
6116 if (row == screen_Rows - 1 && col == screen_Columns - 1
6117#ifdef FEAT_RIGHTLEFT
6118 /* account for first command-line character in rightleft mode */
6119 && !cmdmsg_rl
6120#endif
6121 )
6122 {
6123 ScreenAttrs[off] = (sattr_T)-1;
6124 return;
6125 }
6126
6127 /*
6128 * Stop highlighting first, so it's easier to move the cursor.
6129 */
6130#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6131 if (screen_char_attr != 0)
6132 attr = screen_char_attr;
6133 else
6134#endif
6135 attr = ScreenAttrs[off];
6136 if (screen_attr != attr)
6137 screen_stop_highlight();
6138
6139 windgoto(row, col);
6140
6141 if (screen_attr != attr)
6142 screen_start_highlight(attr);
6143
6144#ifdef FEAT_MBYTE
6145 if (enc_utf8 && ScreenLinesUC[off] != 0)
6146 {
6147 char_u buf[MB_MAXBYTES + 1];
6148
6149 /* Convert UTF-8 character to bytes and write it. */
6150
6151 buf[utfc_char2bytes(off, buf)] = NUL;
6152
6153 out_str(buf);
6154 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6155 ++screen_cur_col;
6156 }
6157 else
6158#endif
6159 {
6160#ifdef FEAT_MBYTE
6161 out_flush_check();
6162#endif
6163 out_char(ScreenLines[off]);
6164#ifdef FEAT_MBYTE
6165 /* double-byte character in single-width cell */
6166 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6167 out_char(ScreenLines2[off]);
6168#endif
6169 }
6170
6171 screen_cur_col++;
6172}
6173
6174#ifdef FEAT_MBYTE
6175
6176/*
6177 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6178 * on the screen at position 'row' and 'col'.
6179 * The attributes of the first byte is used for all. This is required to
6180 * output the two bytes of a double-byte character with nothing in between.
6181 */
6182 static void
6183screen_char_2(off, row, col)
6184 unsigned off;
6185 int row;
6186 int col;
6187{
6188 /* Check for illegal values (could be wrong when screen was resized). */
6189 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6190 return;
6191
6192 /* Outputting the last character on the screen may scrollup the screen.
6193 * Don't to it! Mark the character invalid (update it when scrolled up) */
6194 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6195 {
6196 ScreenAttrs[off] = (sattr_T)-1;
6197 return;
6198 }
6199
6200 /* Output the first byte normally (positions the cursor), then write the
6201 * second byte directly. */
6202 screen_char(off, row, col);
6203 out_char(ScreenLines[off + 1]);
6204 ++screen_cur_col;
6205}
6206#endif
6207
6208#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6209/*
6210 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6211 * This uses the contents of ScreenLines[] and doesn't change it.
6212 */
6213 void
6214screen_draw_rectangle(row, col, height, width, invert)
6215 int row;
6216 int col;
6217 int height;
6218 int width;
6219 int invert;
6220{
6221 int r, c;
6222 int off;
6223
6224 if (invert)
6225 screen_char_attr = HL_INVERSE;
6226 for (r = row; r < row + height; ++r)
6227 {
6228 off = LineOffset[r];
6229 for (c = col; c < col + width; ++c)
6230 {
6231#ifdef FEAT_MBYTE
6232 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6233 {
6234 screen_char_2(off + c, r, c);
6235 ++c;
6236 }
6237 else
6238#endif
6239 {
6240 screen_char(off + c, r, c);
6241#ifdef FEAT_MBYTE
6242 if (utf_off2cells(off + c) > 1)
6243 ++c;
6244#endif
6245 }
6246 }
6247 }
6248 screen_char_attr = 0;
6249}
6250#endif
6251
6252#ifdef FEAT_VERTSPLIT
6253/*
6254 * Redraw the characters for a vertically split window.
6255 */
6256 static void
6257redraw_block(row, end, wp)
6258 int row;
6259 int end;
6260 win_T *wp;
6261{
6262 int col;
6263 int width;
6264
6265# ifdef FEAT_CLIPBOARD
6266 clip_may_clear_selection(row, end - 1);
6267# endif
6268
6269 if (wp == NULL)
6270 {
6271 col = 0;
6272 width = Columns;
6273 }
6274 else
6275 {
6276 col = wp->w_wincol;
6277 width = wp->w_width;
6278 }
6279 screen_draw_rectangle(row, col, end - row, width, FALSE);
6280}
6281#endif
6282
6283/*
6284 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6285 * with character 'c1' in first column followed by 'c2' in the other columns.
6286 * Use attributes 'attr'.
6287 */
6288 void
6289screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6290 int start_row, end_row;
6291 int start_col, end_col;
6292 int c1, c2;
6293 int attr;
6294{
6295 int row;
6296 int col;
6297 int off;
6298 int end_off;
6299 int did_delete;
6300 int c;
6301 int norm_term;
6302#if defined(FEAT_GUI) || defined(UNIX)
6303 int force_next = FALSE;
6304#endif
6305
6306 if (end_row > screen_Rows) /* safety check */
6307 end_row = screen_Rows;
6308 if (end_col > screen_Columns) /* safety check */
6309 end_col = screen_Columns;
6310 if (ScreenLines == NULL
6311 || start_row >= end_row
6312 || start_col >= end_col) /* nothing to do */
6313 return;
6314
6315 /* it's a "normal" terminal when not in a GUI or cterm */
6316 norm_term = (
6317#ifdef FEAT_GUI
6318 !gui.in_use &&
6319#endif
6320 t_colors <= 1);
6321 for (row = start_row; row < end_row; ++row)
6322 {
6323 /*
6324 * Try to use delete-line termcap code, when no attributes or in a
6325 * "normal" terminal, where a bold/italic space is just a
6326 * space.
6327 */
6328 did_delete = FALSE;
6329 if (c2 == ' '
6330 && end_col == Columns
6331 && can_clear(T_CE)
6332 && (attr == 0
6333 || (norm_term
6334 && attr <= HL_ALL
6335 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6336 {
6337 /*
6338 * check if we really need to clear something
6339 */
6340 col = start_col;
6341 if (c1 != ' ') /* don't clear first char */
6342 ++col;
6343
6344 off = LineOffset[row] + col;
6345 end_off = LineOffset[row] + end_col;
6346
6347 /* skip blanks (used often, keep it fast!) */
6348#ifdef FEAT_MBYTE
6349 if (enc_utf8)
6350 while (off < end_off && ScreenLines[off] == ' '
6351 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6352 ++off;
6353 else
6354#endif
6355 while (off < end_off && ScreenLines[off] == ' '
6356 && ScreenAttrs[off] == 0)
6357 ++off;
6358 if (off < end_off) /* something to be cleared */
6359 {
6360 col = off - LineOffset[row];
6361 screen_stop_highlight();
6362 term_windgoto(row, col);/* clear rest of this screen line */
6363 out_str(T_CE);
6364 screen_start(); /* don't know where cursor is now */
6365 col = end_col - col;
6366 while (col--) /* clear chars in ScreenLines */
6367 {
6368 ScreenLines[off] = ' ';
6369#ifdef FEAT_MBYTE
6370 if (enc_utf8)
6371 ScreenLinesUC[off] = 0;
6372#endif
6373 ScreenAttrs[off] = 0;
6374 ++off;
6375 }
6376 }
6377 did_delete = TRUE; /* the chars are cleared now */
6378 }
6379
6380 off = LineOffset[row] + start_col;
6381 c = c1;
6382 for (col = start_col; col < end_col; ++col)
6383 {
6384 if (ScreenLines[off] != c
6385#ifdef FEAT_MBYTE
6386 || (enc_utf8 && ScreenLinesUC[off] != (c >= 0x80 ? c : 0))
6387#endif
6388 || ScreenAttrs[off] != attr
6389#if defined(FEAT_GUI) || defined(UNIX)
6390 || force_next
6391#endif
6392 )
6393 {
6394#if defined(FEAT_GUI) || defined(UNIX)
6395 /* The bold trick may make a single row of pixels appear in
6396 * the next character. When a bold character is removed, the
6397 * next character should be redrawn too. This happens for our
6398 * own GUI and for some xterms. */
6399 if (
6400# ifdef FEAT_GUI
6401 gui.in_use
6402# endif
6403# if defined(FEAT_GUI) && defined(UNIX)
6404 ||
6405# endif
6406# ifdef UNIX
6407 term_is_xterm
6408# endif
6409 )
6410 {
6411 if (ScreenLines[off] != ' '
6412 && (ScreenAttrs[off] > HL_ALL
6413 || ScreenAttrs[off] & HL_BOLD))
6414 force_next = TRUE;
6415 else
6416 force_next = FALSE;
6417 }
6418#endif
6419 ScreenLines[off] = c;
6420#ifdef FEAT_MBYTE
6421 if (enc_utf8)
6422 {
6423 if (c >= 0x80)
6424 {
6425 ScreenLinesUC[off] = c;
6426 ScreenLinesC1[off] = 0;
6427 ScreenLinesC2[off] = 0;
6428 }
6429 else
6430 ScreenLinesUC[off] = 0;
6431 }
6432#endif
6433 ScreenAttrs[off] = attr;
6434 if (!did_delete || c != ' ')
6435 screen_char(off, row, col);
6436 }
6437 ++off;
6438 if (col == start_col)
6439 {
6440 if (did_delete)
6441 break;
6442 c = c2;
6443 }
6444 }
6445 if (end_col == Columns)
6446 LineWraps[row] = FALSE;
6447 if (row == Rows - 1) /* overwritten the command line */
6448 {
6449 redraw_cmdline = TRUE;
6450 if (c1 == ' ' && c2 == ' ')
6451 clear_cmdline = FALSE; /* command line has been cleared */
6452 }
6453 }
6454}
6455
6456/*
6457 * Check if there should be a delay. Used before clearing or redrawing the
6458 * screen or the command line.
6459 */
6460 void
6461check_for_delay(check_msg_scroll)
6462 int check_msg_scroll;
6463{
6464 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
6465 && !did_wait_return
6466 && emsg_silent == 0)
6467 {
6468 out_flush();
6469 ui_delay(1000L, TRUE);
6470 emsg_on_display = FALSE;
6471 if (check_msg_scroll)
6472 msg_scroll = FALSE;
6473 }
6474}
6475
6476/*
6477 * screen_valid - allocate screen buffers if size changed
6478 * If "clear" is TRUE: clear screen if it has been resized.
6479 * Returns TRUE if there is a valid screen to write to.
6480 * Returns FALSE when starting up and screen not initialized yet.
6481 */
6482 int
6483screen_valid(clear)
6484 int clear;
6485{
6486 screenalloc(clear); /* allocate screen buffers if size changed */
6487 return (ScreenLines != NULL);
6488}
6489
6490/*
6491 * Resize the shell to Rows and Columns.
6492 * Allocate ScreenLines[] and associated items.
6493 *
6494 * There may be some time between setting Rows and Columns and (re)allocating
6495 * ScreenLines[]. This happens when starting up and when (manually) changing
6496 * the shell size. Always use screen_Rows and screen_Columns to access items
6497 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
6498 * final size of the shell is needed.
6499 */
6500 void
6501screenalloc(clear)
6502 int clear;
6503{
6504 int new_row, old_row;
6505#ifdef FEAT_GUI
6506 int old_Rows;
6507#endif
6508 win_T *wp;
6509 int outofmem = FALSE;
6510 int len;
6511 schar_T *new_ScreenLines;
6512#ifdef FEAT_MBYTE
6513 u8char_T *new_ScreenLinesUC = NULL;
6514 u8char_T *new_ScreenLinesC1 = NULL;
6515 u8char_T *new_ScreenLinesC2 = NULL;
6516 schar_T *new_ScreenLines2 = NULL;
6517#endif
6518 sattr_T *new_ScreenAttrs;
6519 unsigned *new_LineOffset;
6520 char_u *new_LineWraps;
6521 static int entered = FALSE; /* avoid recursiveness */
6522
6523 /*
6524 * Allocation of the screen buffers is done only when the size changes and
6525 * when Rows and Columns have been set and we have started doing full
6526 * screen stuff.
6527 */
6528 if ((ScreenLines != NULL
6529 && Rows == screen_Rows
6530 && Columns == screen_Columns
6531#ifdef FEAT_MBYTE
6532 && enc_utf8 == (ScreenLinesUC != NULL)
6533 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
6534#endif
6535 )
6536 || Rows == 0
6537 || Columns == 0
6538 || (!full_screen && ScreenLines == NULL))
6539 return;
6540
6541 /*
6542 * It's possible that we produce an out-of-memory message below, which
6543 * will cause this function to be called again. To break the loop, just
6544 * return here.
6545 */
6546 if (entered)
6547 return;
6548 entered = TRUE;
6549
6550 win_new_shellsize(); /* fit the windows in the new sized shell */
6551
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 comp_col(); /* recompute columns for shown command and ruler */
6553
6554 /*
6555 * We're changing the size of the screen.
6556 * - Allocate new arrays for ScreenLines and ScreenAttrs.
6557 * - Move lines from the old arrays into the new arrays, clear extra
6558 * lines (unless the screen is going to be cleared).
6559 * - Free the old arrays.
6560 *
6561 * If anything fails, make ScreenLines NULL, so we don't do anything!
6562 * Continuing with the old ScreenLines may result in a crash, because the
6563 * size is wrong.
6564 */
6565#ifdef FEAT_WINDOWS
6566 for (wp = firstwin; wp; wp = wp->w_next)
6567 win_free_lsize(wp);
6568#else
6569 win_free_lsize(curwin);
6570#endif
6571
6572 new_ScreenLines = (schar_T *)lalloc((long_u)(
6573 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
6574#ifdef FEAT_MBYTE
6575 if (enc_utf8)
6576 {
6577 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
6578 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
6579 new_ScreenLinesC1 = (u8char_T *)lalloc((long_u)(
6580 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
6581 new_ScreenLinesC2 = (u8char_T *)lalloc((long_u)(
6582 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
6583 }
6584 if (enc_dbcs == DBCS_JPNU)
6585 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
6586 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
6587#endif
6588 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
6589 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
6590 new_LineOffset = (unsigned *)lalloc((long_u)(
6591 Rows * sizeof(unsigned)), FALSE);
6592 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
6593
6594 FOR_ALL_WINDOWS(wp)
6595 {
6596 if (win_alloc_lines(wp) == FAIL)
6597 {
6598 outofmem = TRUE;
6599#ifdef FEAT_WINDOWS
6600 break;
6601#endif
6602 }
6603 }
6604
6605 if (new_ScreenLines == NULL
6606#ifdef FEAT_MBYTE
6607 || (enc_utf8 && (new_ScreenLinesUC == NULL
6608 || new_ScreenLinesC1 == NULL || new_ScreenLinesC2 == NULL))
6609 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
6610#endif
6611 || new_ScreenAttrs == NULL
6612 || new_LineOffset == NULL
6613 || new_LineWraps == NULL
6614 || outofmem)
6615 {
6616 do_outofmem_msg((long_u)((Rows + 1) * Columns)); /* guess the size */
6617 vim_free(new_ScreenLines);
6618 new_ScreenLines = NULL;
6619#ifdef FEAT_MBYTE
6620 vim_free(new_ScreenLinesUC);
6621 new_ScreenLinesUC = NULL;
6622 vim_free(new_ScreenLinesC1);
6623 new_ScreenLinesC1 = NULL;
6624 vim_free(new_ScreenLinesC2);
6625 new_ScreenLinesC2 = NULL;
6626 vim_free(new_ScreenLines2);
6627 new_ScreenLines2 = NULL;
6628#endif
6629 vim_free(new_ScreenAttrs);
6630 new_ScreenAttrs = NULL;
6631 vim_free(new_LineOffset);
6632 new_LineOffset = NULL;
6633 vim_free(new_LineWraps);
6634 new_LineWraps = NULL;
6635 }
6636 else
6637 {
6638 for (new_row = 0; new_row < Rows; ++new_row)
6639 {
6640 new_LineOffset[new_row] = new_row * Columns;
6641 new_LineWraps[new_row] = FALSE;
6642
6643 /*
6644 * If the screen is not going to be cleared, copy as much as
6645 * possible from the old screen to the new one and clear the rest
6646 * (used when resizing the window at the "--more--" prompt or when
6647 * executing an external command, for the GUI).
6648 */
6649 if (!clear)
6650 {
6651 (void)vim_memset(new_ScreenLines + new_row * Columns,
6652 ' ', (size_t)Columns * sizeof(schar_T));
6653#ifdef FEAT_MBYTE
6654 if (enc_utf8)
6655 {
6656 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
6657 0, (size_t)Columns * sizeof(u8char_T));
6658 (void)vim_memset(new_ScreenLinesC1 + new_row * Columns,
6659 0, (size_t)Columns * sizeof(u8char_T));
6660 (void)vim_memset(new_ScreenLinesC2 + new_row * Columns,
6661 0, (size_t)Columns * sizeof(u8char_T));
6662 }
6663 if (enc_dbcs == DBCS_JPNU)
6664 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
6665 0, (size_t)Columns * sizeof(schar_T));
6666#endif
6667 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
6668 0, (size_t)Columns * sizeof(sattr_T));
6669 old_row = new_row + (screen_Rows - Rows);
6670 if (old_row >= 0)
6671 {
6672 if (screen_Columns < Columns)
6673 len = screen_Columns;
6674 else
6675 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006676#ifdef FEAT_MBYTE
6677 /* When switching to utf-8 dont copy characters, they
6678 * may be invalid now. */
6679 if (!(enc_utf8 && ScreenLinesUC == NULL))
6680#endif
6681 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
6682 ScreenLines + LineOffset[old_row],
6683 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#ifdef FEAT_MBYTE
6685 if (enc_utf8 && ScreenLinesUC != NULL)
6686 {
6687 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
6688 ScreenLinesUC + LineOffset[old_row],
6689 (size_t)len * sizeof(u8char_T));
6690 mch_memmove(new_ScreenLinesC1 + new_LineOffset[new_row],
6691 ScreenLinesC1 + LineOffset[old_row],
6692 (size_t)len * sizeof(u8char_T));
6693 mch_memmove(new_ScreenLinesC2 + new_LineOffset[new_row],
6694 ScreenLinesC2 + LineOffset[old_row],
6695 (size_t)len * sizeof(u8char_T));
6696 }
6697 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
6698 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
6699 ScreenLines2 + LineOffset[old_row],
6700 (size_t)len * sizeof(schar_T));
6701#endif
6702 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
6703 ScreenAttrs + LineOffset[old_row],
6704 (size_t)len * sizeof(sattr_T));
6705 }
6706 }
6707 }
6708 /* Use the last line of the screen for the current line. */
6709 current_ScreenLine = new_ScreenLines + Rows * Columns;
6710 }
6711
6712 vim_free(ScreenLines);
6713#ifdef FEAT_MBYTE
6714 vim_free(ScreenLinesUC);
6715 vim_free(ScreenLinesC1);
6716 vim_free(ScreenLinesC2);
6717 vim_free(ScreenLines2);
6718#endif
6719 vim_free(ScreenAttrs);
6720 vim_free(LineOffset);
6721 vim_free(LineWraps);
6722 ScreenLines = new_ScreenLines;
6723#ifdef FEAT_MBYTE
6724 ScreenLinesUC = new_ScreenLinesUC;
6725 ScreenLinesC1 = new_ScreenLinesC1;
6726 ScreenLinesC2 = new_ScreenLinesC2;
6727 ScreenLines2 = new_ScreenLines2;
6728#endif
6729 ScreenAttrs = new_ScreenAttrs;
6730 LineOffset = new_LineOffset;
6731 LineWraps = new_LineWraps;
6732
6733 /* It's important that screen_Rows and screen_Columns reflect the actual
6734 * size of ScreenLines[]. Set them before calling anything. */
6735#ifdef FEAT_GUI
6736 old_Rows = screen_Rows;
6737#endif
6738 screen_Rows = Rows;
6739 screen_Columns = Columns;
6740
6741 must_redraw = CLEAR; /* need to clear the screen later */
6742 if (clear)
6743 screenclear2();
6744
6745#ifdef FEAT_GUI
6746 else if (gui.in_use
6747 && !gui.starting
6748 && ScreenLines != NULL
6749 && old_Rows != Rows)
6750 {
6751 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
6752 /*
6753 * Adjust the position of the cursor, for when executing an external
6754 * command.
6755 */
6756 if (msg_row >= Rows) /* Rows got smaller */
6757 msg_row = Rows - 1; /* put cursor at last row */
6758 else if (Rows > old_Rows) /* Rows got bigger */
6759 msg_row += Rows - old_Rows; /* put cursor in same place */
6760 if (msg_col >= Columns) /* Columns got smaller */
6761 msg_col = Columns - 1; /* put cursor at last column */
6762 }
6763#endif
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 entered = FALSE;
6766}
6767
6768 void
6769screenclear()
6770{
6771 check_for_delay(FALSE);
6772 screenalloc(FALSE); /* allocate screen buffers if size changed */
6773 screenclear2(); /* clear the screen */
6774}
6775
6776 static void
6777screenclear2()
6778{
6779 int i;
6780
6781 if (starting == NO_SCREEN || ScreenLines == NULL
6782#ifdef FEAT_GUI
6783 || (gui.in_use && gui.starting)
6784#endif
6785 )
6786 return;
6787
6788#ifdef FEAT_GUI
6789 if (!gui.in_use)
6790#endif
6791 screen_attr = -1; /* force setting the Normal colors */
6792 screen_stop_highlight(); /* don't want highlighting here */
6793
6794#ifdef FEAT_CLIPBOARD
6795 /* disable selection without redrawing it */
6796 clip_scroll_selection(9999);
6797#endif
6798
6799 /* blank out ScreenLines */
6800 for (i = 0; i < Rows; ++i)
6801 {
6802 lineclear(LineOffset[i], (int)Columns);
6803 LineWraps[i] = FALSE;
6804 }
6805
6806 if (can_clear(T_CL))
6807 {
6808 out_str(T_CL); /* clear the display */
6809 clear_cmdline = FALSE;
6810 }
6811 else
6812 {
6813 /* can't clear the screen, mark all chars with invalid attributes */
6814 for (i = 0; i < Rows; ++i)
6815 lineinvalid(LineOffset[i], (int)Columns);
6816 clear_cmdline = TRUE;
6817 }
6818
6819 screen_cleared = TRUE; /* can use contents of ScreenLines now */
6820
6821 win_rest_invalid(firstwin);
6822 redraw_cmdline = TRUE;
6823 if (must_redraw == CLEAR) /* no need to clear again */
6824 must_redraw = NOT_VALID;
6825 compute_cmdrow();
6826 msg_row = cmdline_row; /* put cursor on last line for messages */
6827 msg_col = 0;
6828 screen_start(); /* don't know where cursor is now */
6829 msg_scrolled = 0; /* can't scroll back */
6830 msg_didany = FALSE;
6831 msg_didout = FALSE;
6832}
6833
6834/*
6835 * Clear one line in ScreenLines.
6836 */
6837 static void
6838lineclear(off, width)
6839 unsigned off;
6840 int width;
6841{
6842 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
6843#ifdef FEAT_MBYTE
6844 if (enc_utf8)
6845 (void)vim_memset(ScreenLinesUC + off, 0,
6846 (size_t)width * sizeof(u8char_T));
6847#endif
6848 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
6849}
6850
6851/*
6852 * Mark one line in ScreenLines invalid by setting the attributes to an
6853 * invalid value.
6854 */
6855 static void
6856lineinvalid(off, width)
6857 unsigned off;
6858 int width;
6859{
6860 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
6861}
6862
6863#ifdef FEAT_VERTSPLIT
6864/*
6865 * Copy part of a Screenline for vertically split window "wp".
6866 */
6867 static void
6868linecopy(to, from, wp)
6869 int to;
6870 int from;
6871 win_T *wp;
6872{
6873 unsigned off_to = LineOffset[to] + wp->w_wincol;
6874 unsigned off_from = LineOffset[from] + wp->w_wincol;
6875
6876 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
6877 wp->w_width * sizeof(schar_T));
6878# ifdef FEAT_MBYTE
6879 if (enc_utf8)
6880 {
6881 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
6882 wp->w_width * sizeof(u8char_T));
6883 mch_memmove(ScreenLinesC1 + off_to, ScreenLinesC1 + off_from,
6884 wp->w_width * sizeof(u8char_T));
6885 mch_memmove(ScreenLinesC2 + off_to, ScreenLinesC2 + off_from,
6886 wp->w_width * sizeof(u8char_T));
6887 }
6888 if (enc_dbcs == DBCS_JPNU)
6889 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
6890 wp->w_width * sizeof(schar_T));
6891# endif
6892 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
6893 wp->w_width * sizeof(sattr_T));
6894}
6895#endif
6896
6897/*
6898 * Return TRUE if clearing with term string "p" would work.
6899 * It can't work when the string is empty or it won't set the right background.
6900 */
6901 int
6902can_clear(p)
6903 char_u *p;
6904{
6905 return (*p != NUL && (t_colors <= 1
6906#ifdef FEAT_GUI
6907 || gui.in_use
6908#endif
6909 || cterm_normal_bg_color == 0 || *T_UT != NUL));
6910}
6911
6912/*
6913 * Reset cursor position. Use whenever cursor was moved because of outputting
6914 * something directly to the screen (shell commands) or a terminal control
6915 * code.
6916 */
6917 void
6918screen_start()
6919{
6920 screen_cur_row = screen_cur_col = 9999;
6921}
6922
6923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 * Move the cursor to position "row","col" in the screen.
6925 * This tries to find the most efficient way to move, minimizing the number of
6926 * characters sent to the terminal.
6927 */
6928 void
6929windgoto(row, col)
6930 int row;
6931 int col;
6932{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006933 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 int i;
6935 int plan;
6936 int cost;
6937 int wouldbe_col;
6938 int noinvcurs;
6939 char_u *bs;
6940 int goto_cost;
6941 int attr;
6942
6943#define GOTO_COST 7 /* asssume a term_windgoto() takes about 7 chars */
6944#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
6945
6946#define PLAN_LE 1
6947#define PLAN_CR 2
6948#define PLAN_NL 3
6949#define PLAN_WRITE 4
6950 /* Can't use ScreenLines unless initialized */
6951 if (ScreenLines == NULL)
6952 return;
6953
6954 if (col != screen_cur_col || row != screen_cur_row)
6955 {
6956 /* Check for valid position. */
6957 if (row < 0) /* window without text lines? */
6958 row = 0;
6959 if (row >= screen_Rows)
6960 row = screen_Rows - 1;
6961 if (col >= screen_Columns)
6962 col = screen_Columns - 1;
6963
6964 /* check if no cursor movement is allowed in highlight mode */
6965 if (screen_attr && *T_MS == NUL)
6966 noinvcurs = HIGHL_COST;
6967 else
6968 noinvcurs = 0;
6969 goto_cost = GOTO_COST + noinvcurs;
6970
6971 /*
6972 * Plan how to do the positioning:
6973 * 1. Use CR to move it to column 0, same row.
6974 * 2. Use T_LE to move it a few columns to the left.
6975 * 3. Use NL to move a few lines down, column 0.
6976 * 4. Move a few columns to the right with T_ND or by writing chars.
6977 *
6978 * Don't do this if the cursor went beyond the last column, the cursor
6979 * position is unknown then (some terminals wrap, some don't )
6980 *
6981 * First check if the highlighting attibutes allow us to write
6982 * characters to move the cursor to the right.
6983 */
6984 if (row >= screen_cur_row && screen_cur_col < Columns)
6985 {
6986 /*
6987 * If the cursor is in the same row, bigger col, we can use CR
6988 * or T_LE.
6989 */
6990 bs = NULL; /* init for GCC */
6991 attr = screen_attr;
6992 if (row == screen_cur_row && col < screen_cur_col)
6993 {
6994 /* "le" is preferred over "bc", because "bc" is obsolete */
6995 if (*T_LE)
6996 bs = T_LE; /* "cursor left" */
6997 else
6998 bs = T_BC; /* "backspace character (old) */
6999 if (*bs)
7000 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7001 else
7002 cost = 999;
7003 if (col + 1 < cost) /* using CR is less characters */
7004 {
7005 plan = PLAN_CR;
7006 wouldbe_col = 0;
7007 cost = 1; /* CR is just one character */
7008 }
7009 else
7010 {
7011 plan = PLAN_LE;
7012 wouldbe_col = col;
7013 }
7014 if (noinvcurs) /* will stop highlighting */
7015 {
7016 cost += noinvcurs;
7017 attr = 0;
7018 }
7019 }
7020
7021 /*
7022 * If the cursor is above where we want to be, we can use CR LF.
7023 */
7024 else if (row > screen_cur_row)
7025 {
7026 plan = PLAN_NL;
7027 wouldbe_col = 0;
7028 cost = (row - screen_cur_row) * 2; /* CR LF */
7029 if (noinvcurs) /* will stop highlighting */
7030 {
7031 cost += noinvcurs;
7032 attr = 0;
7033 }
7034 }
7035
7036 /*
7037 * If the cursor is in the same row, smaller col, just use write.
7038 */
7039 else
7040 {
7041 plan = PLAN_WRITE;
7042 wouldbe_col = screen_cur_col;
7043 cost = 0;
7044 }
7045
7046 /*
7047 * Check if any characters that need to be written have the
7048 * correct attributes. Also avoid UTF-8 characters.
7049 */
7050 i = col - wouldbe_col;
7051 if (i > 0)
7052 cost += i;
7053 if (cost < goto_cost && i > 0)
7054 {
7055 /*
7056 * Check if the attributes are correct without additionally
7057 * stopping highlighting.
7058 */
7059 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7060 while (i && *p++ == attr)
7061 --i;
7062 if (i != 0)
7063 {
7064 /*
7065 * Try if it works when highlighting is stopped here.
7066 */
7067 if (*--p == 0)
7068 {
7069 cost += noinvcurs;
7070 while (i && *p++ == 0)
7071 --i;
7072 }
7073 if (i != 0)
7074 cost = 999; /* different attributes, don't do it */
7075 }
7076#ifdef FEAT_MBYTE
7077 if (enc_utf8)
7078 {
7079 /* Don't use an UTF-8 char for positioning, it's slow. */
7080 for (i = wouldbe_col; i < col; ++i)
7081 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7082 {
7083 cost = 999;
7084 break;
7085 }
7086 }
7087#endif
7088 }
7089
7090 /*
7091 * We can do it without term_windgoto()!
7092 */
7093 if (cost < goto_cost)
7094 {
7095 if (plan == PLAN_LE)
7096 {
7097 if (noinvcurs)
7098 screen_stop_highlight();
7099 while (screen_cur_col > col)
7100 {
7101 out_str(bs);
7102 --screen_cur_col;
7103 }
7104 }
7105 else if (plan == PLAN_CR)
7106 {
7107 if (noinvcurs)
7108 screen_stop_highlight();
7109 out_char('\r');
7110 screen_cur_col = 0;
7111 }
7112 else if (plan == PLAN_NL)
7113 {
7114 if (noinvcurs)
7115 screen_stop_highlight();
7116 while (screen_cur_row < row)
7117 {
7118 out_char('\n');
7119 ++screen_cur_row;
7120 }
7121 screen_cur_col = 0;
7122 }
7123
7124 i = col - screen_cur_col;
7125 if (i > 0)
7126 {
7127 /*
7128 * Use cursor-right if it's one character only. Avoids
7129 * removing a line of pixels from the last bold char, when
7130 * using the bold trick in the GUI.
7131 */
7132 if (T_ND[0] != NUL && T_ND[1] == NUL)
7133 {
7134 while (i-- > 0)
7135 out_char(*T_ND);
7136 }
7137 else
7138 {
7139 int off;
7140
7141 off = LineOffset[row] + screen_cur_col;
7142 while (i-- > 0)
7143 {
7144 if (ScreenAttrs[off] != screen_attr)
7145 screen_stop_highlight();
7146#ifdef FEAT_MBYTE
7147 out_flush_check();
7148#endif
7149 out_char(ScreenLines[off]);
7150#ifdef FEAT_MBYTE
7151 if (enc_dbcs == DBCS_JPNU
7152 && ScreenLines[off] == 0x8e)
7153 out_char(ScreenLines2[off]);
7154#endif
7155 ++off;
7156 }
7157 }
7158 }
7159 }
7160 }
7161 else
7162 cost = 999;
7163
7164 if (cost >= goto_cost)
7165 {
7166 if (noinvcurs)
7167 screen_stop_highlight();
7168 if (row == screen_cur_row && (col > screen_cur_col) &&
7169 *T_CRI != NUL)
7170 term_cursor_right(col - screen_cur_col);
7171 else
7172 term_windgoto(row, col);
7173 }
7174 screen_cur_row = row;
7175 screen_cur_col = col;
7176 }
7177}
7178
7179/*
7180 * Set cursor to its position in the current window.
7181 */
7182 void
7183setcursor()
7184{
7185 if (redrawing())
7186 {
7187 validate_cursor();
7188 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7189 W_WINCOL(curwin) + (
7190#ifdef FEAT_RIGHTLEFT
7191 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7192# ifdef FEAT_MBYTE
7193 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7194# endif
7195 1)) :
7196#endif
7197 curwin->w_wcol));
7198 }
7199}
7200
7201
7202/*
7203 * insert 'line_count' lines at 'row' in window 'wp'
7204 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7205 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7206 * scrolling.
7207 * Returns FAIL if the lines are not inserted, OK for success.
7208 */
7209 int
7210win_ins_lines(wp, row, line_count, invalid, mayclear)
7211 win_T *wp;
7212 int row;
7213 int line_count;
7214 int invalid;
7215 int mayclear;
7216{
7217 int did_delete;
7218 int nextrow;
7219 int lastrow;
7220 int retval;
7221
7222 if (invalid)
7223 wp->w_lines_valid = 0;
7224
7225 if (wp->w_height < 5)
7226 return FAIL;
7227
7228 if (line_count > wp->w_height - row)
7229 line_count = wp->w_height - row;
7230
7231 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7232 if (retval != MAYBE)
7233 return retval;
7234
7235 /*
7236 * If there is a next window or a status line, we first try to delete the
7237 * lines at the bottom to avoid messing what is after the window.
7238 * If this fails and there are following windows, don't do anything to avoid
7239 * messing up those windows, better just redraw.
7240 */
7241 did_delete = FALSE;
7242#ifdef FEAT_WINDOWS
7243 if (wp->w_next != NULL || wp->w_status_height)
7244 {
7245 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7246 line_count, (int)Rows, FALSE, NULL) == OK)
7247 did_delete = TRUE;
7248 else if (wp->w_next)
7249 return FAIL;
7250 }
7251#endif
7252 /*
7253 * if no lines deleted, blank the lines that will end up below the window
7254 */
7255 if (!did_delete)
7256 {
7257#ifdef FEAT_WINDOWS
7258 wp->w_redr_status = TRUE;
7259#endif
7260 redraw_cmdline = TRUE;
7261 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7262 lastrow = nextrow + line_count;
7263 if (lastrow > Rows)
7264 lastrow = Rows;
7265 screen_fill(nextrow - line_count, lastrow - line_count,
7266 W_WINCOL(wp), (int)W_ENDCOL(wp),
7267 ' ', ' ', 0);
7268 }
7269
7270 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7271 == FAIL)
7272 {
7273 /* deletion will have messed up other windows */
7274 if (did_delete)
7275 {
7276#ifdef FEAT_WINDOWS
7277 wp->w_redr_status = TRUE;
7278#endif
7279 win_rest_invalid(W_NEXT(wp));
7280 }
7281 return FAIL;
7282 }
7283
7284 return OK;
7285}
7286
7287/*
7288 * delete "line_count" window lines at "row" in window "wp"
7289 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7290 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7291 * scrolling
7292 * Return OK for success, FAIL if the lines are not deleted.
7293 */
7294 int
7295win_del_lines(wp, row, line_count, invalid, mayclear)
7296 win_T *wp;
7297 int row;
7298 int line_count;
7299 int invalid;
7300 int mayclear;
7301{
7302 int retval;
7303
7304 if (invalid)
7305 wp->w_lines_valid = 0;
7306
7307 if (line_count > wp->w_height - row)
7308 line_count = wp->w_height - row;
7309
7310 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7311 if (retval != MAYBE)
7312 return retval;
7313
7314 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7315 (int)Rows, FALSE, NULL) == FAIL)
7316 return FAIL;
7317
7318#ifdef FEAT_WINDOWS
7319 /*
7320 * If there are windows or status lines below, try to put them at the
7321 * correct place. If we can't do that, they have to be redrawn.
7322 */
7323 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
7324 {
7325 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7326 line_count, (int)Rows, NULL) == FAIL)
7327 {
7328 wp->w_redr_status = TRUE;
7329 win_rest_invalid(wp->w_next);
7330 }
7331 }
7332 /*
7333 * If this is the last window and there is no status line, redraw the
7334 * command line later.
7335 */
7336 else
7337#endif
7338 redraw_cmdline = TRUE;
7339 return OK;
7340}
7341
7342/*
7343 * Common code for win_ins_lines() and win_del_lines().
7344 * Returns OK or FAIL when the work has been done.
7345 * Returns MAYBE when not finished yet.
7346 */
7347 static int
7348win_do_lines(wp, row, line_count, mayclear, del)
7349 win_T *wp;
7350 int row;
7351 int line_count;
7352 int mayclear;
7353 int del;
7354{
7355 int retval;
7356
7357 if (!redrawing() || line_count <= 0)
7358 return FAIL;
7359
7360 /* only a few lines left: redraw is faster */
7361 if (mayclear && Rows - line_count < 5
7362#ifdef FEAT_VERTSPLIT
7363 && wp->w_width == Columns
7364#endif
7365 )
7366 {
7367 screenclear(); /* will set wp->w_lines_valid to 0 */
7368 return FAIL;
7369 }
7370
7371 /*
7372 * Delete all remaining lines
7373 */
7374 if (row + line_count >= wp->w_height)
7375 {
7376 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
7377 W_WINCOL(wp), (int)W_ENDCOL(wp),
7378 ' ', ' ', 0);
7379 return OK;
7380 }
7381
7382 /*
7383 * when scrolling, the message on the command line should be cleared,
7384 * otherwise it will stay there forever.
7385 */
7386 clear_cmdline = TRUE;
7387
7388 /*
7389 * If the terminal can set a scroll region, use that.
7390 * Always do this in a vertically split window. This will redraw from
7391 * ScreenLines[] when t_CV isn't defined. That's faster than using
7392 * win_line().
7393 * Don't use a scroll region when we are going to redraw the text, writing
7394 * a character in the lower right corner of the scroll region causes a
7395 * scroll-up in the DJGPP version.
7396 */
7397 if (scroll_region
7398#ifdef FEAT_VERTSPLIT
7399 || W_WIDTH(wp) != Columns
7400#endif
7401 )
7402 {
7403#ifdef FEAT_VERTSPLIT
7404 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
7405#endif
7406 scroll_region_set(wp, row);
7407 if (del)
7408 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
7409 wp->w_height - row, FALSE, wp);
7410 else
7411 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
7412 wp->w_height - row, wp);
7413#ifdef FEAT_VERTSPLIT
7414 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
7415#endif
7416 scroll_region_reset();
7417 return retval;
7418 }
7419
7420#ifdef FEAT_WINDOWS
7421 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
7422 return FAIL;
7423#endif
7424
7425 return MAYBE;
7426}
7427
7428/*
7429 * window 'wp' and everything after it is messed up, mark it for redraw
7430 */
7431 static void
7432win_rest_invalid(wp)
7433 win_T *wp;
7434{
7435#ifdef FEAT_WINDOWS
7436 while (wp != NULL)
7437#else
7438 if (wp != NULL)
7439#endif
7440 {
7441 redraw_win_later(wp, NOT_VALID);
7442#ifdef FEAT_WINDOWS
7443 wp->w_redr_status = TRUE;
7444 wp = wp->w_next;
7445#endif
7446 }
7447 redraw_cmdline = TRUE;
7448}
7449
7450/*
7451 * The rest of the routines in this file perform screen manipulations. The
7452 * given operation is performed physically on the screen. The corresponding
7453 * change is also made to the internal screen image. In this way, the editor
7454 * anticipates the effect of editing changes on the appearance of the screen.
7455 * That way, when we call screenupdate a complete redraw isn't usually
7456 * necessary. Another advantage is that we can keep adding code to anticipate
7457 * screen changes, and in the meantime, everything still works.
7458 */
7459
7460/*
7461 * types for inserting or deleting lines
7462 */
7463#define USE_T_CAL 1
7464#define USE_T_CDL 2
7465#define USE_T_AL 3
7466#define USE_T_CE 4
7467#define USE_T_DL 5
7468#define USE_T_SR 6
7469#define USE_NL 7
7470#define USE_T_CD 8
7471#define USE_REDRAW 9
7472
7473/*
7474 * insert lines on the screen and update ScreenLines[]
7475 * 'end' is the line after the scrolled part. Normally it is Rows.
7476 * When scrolling region used 'off' is the offset from the top for the region.
7477 * 'row' and 'end' are relative to the start of the region.
7478 *
7479 * return FAIL for failure, OK for success.
7480 */
7481 static int
7482screen_ins_lines(off, row, line_count, end, wp)
7483 int off;
7484 int row;
7485 int line_count;
7486 int end;
7487 win_T *wp; /* NULL or window to use width from */
7488{
7489 int i;
7490 int j;
7491 unsigned temp;
7492 int cursor_row;
7493 int type;
7494 int result_empty;
7495 int can_ce = can_clear(T_CE);
7496
7497 /*
7498 * FAIL if
7499 * - there is no valid screen
7500 * - the screen has to be redrawn completely
7501 * - the line count is less than one
7502 * - the line count is more than 'ttyscroll'
7503 */
7504 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
7505 return FAIL;
7506
7507 /*
7508 * There are seven ways to insert lines:
7509 * 0. When in a vertically split window and t_CV isn't set, redraw the
7510 * characters from ScreenLines[].
7511 * 1. Use T_CD (clear to end of display) if it exists and the result of
7512 * the insert is just empty lines
7513 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
7514 * present or line_count > 1. It looks better if we do all the inserts
7515 * at once.
7516 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
7517 * insert is just empty lines and T_CE is not present or line_count >
7518 * 1.
7519 * 4. Use T_AL (insert line) if it exists.
7520 * 5. Use T_CE (erase line) if it exists and the result of the insert is
7521 * just empty lines.
7522 * 6. Use T_DL (delete line) if it exists and the result of the insert is
7523 * just empty lines.
7524 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
7525 * the 'da' flag is not set or we have clear line capability.
7526 * 8. redraw the characters from ScreenLines[].
7527 *
7528 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
7529 * the scrollbar for the window. It does have insert line, use that if it
7530 * exists.
7531 */
7532 result_empty = (row + line_count >= end);
7533#ifdef FEAT_VERTSPLIT
7534 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
7535 type = USE_REDRAW;
7536 else
7537#endif
7538 if (can_clear(T_CD) && result_empty)
7539 type = USE_T_CD;
7540 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
7541 type = USE_T_CAL;
7542 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
7543 type = USE_T_CDL;
7544 else if (*T_AL != NUL)
7545 type = USE_T_AL;
7546 else if (can_ce && result_empty)
7547 type = USE_T_CE;
7548 else if (*T_DL != NUL && result_empty)
7549 type = USE_T_DL;
7550 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
7551 type = USE_T_SR;
7552 else
7553 return FAIL;
7554
7555 /*
7556 * For clearing the lines screen_del_lines() is used. This will also take
7557 * care of t_db if necessary.
7558 */
7559 if (type == USE_T_CD || type == USE_T_CDL ||
7560 type == USE_T_CE || type == USE_T_DL)
7561 return screen_del_lines(off, row, line_count, end, FALSE, wp);
7562
7563 /*
7564 * If text is retained below the screen, first clear or delete as many
7565 * lines at the bottom of the window as are about to be inserted so that
7566 * the deleted lines won't later surface during a screen_del_lines.
7567 */
7568 if (*T_DB)
7569 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
7570
7571#ifdef FEAT_CLIPBOARD
7572 /* Remove a modeless selection when inserting lines halfway the screen
7573 * or not the full width of the screen. */
7574 if (off + row > 0
7575# ifdef FEAT_VERTSPLIT
7576 || (wp != NULL && wp->w_width != Columns)
7577# endif
7578 )
7579 clip_clear_selection();
7580 else
7581 clip_scroll_selection(-line_count);
7582#endif
7583
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584#ifdef FEAT_GUI
7585 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
7586 * scrolling is actually carried out. */
7587 gui_dont_update_cursor();
7588#endif
7589
7590 if (*T_CCS != NUL) /* cursor relative to region */
7591 cursor_row = row;
7592 else
7593 cursor_row = row + off;
7594
7595 /*
7596 * Shift LineOffset[] line_count down to reflect the inserted lines.
7597 * Clear the inserted lines in ScreenLines[].
7598 */
7599 row += off;
7600 end += off;
7601 for (i = 0; i < line_count; ++i)
7602 {
7603#ifdef FEAT_VERTSPLIT
7604 if (wp != NULL && wp->w_width != Columns)
7605 {
7606 /* need to copy part of a line */
7607 j = end - 1 - i;
7608 while ((j -= line_count) >= row)
7609 linecopy(j + line_count, j, wp);
7610 j += line_count;
7611 if (can_clear((char_u *)" "))
7612 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
7613 else
7614 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
7615 LineWraps[j] = FALSE;
7616 }
7617 else
7618#endif
7619 {
7620 j = end - 1 - i;
7621 temp = LineOffset[j];
7622 while ((j -= line_count) >= row)
7623 {
7624 LineOffset[j + line_count] = LineOffset[j];
7625 LineWraps[j + line_count] = LineWraps[j];
7626 }
7627 LineOffset[j + line_count] = temp;
7628 LineWraps[j + line_count] = FALSE;
7629 if (can_clear((char_u *)" "))
7630 lineclear(temp, (int)Columns);
7631 else
7632 lineinvalid(temp, (int)Columns);
7633 }
7634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635
7636 screen_stop_highlight();
7637 windgoto(cursor_row, 0);
7638
7639#ifdef FEAT_VERTSPLIT
7640 /* redraw the characters */
7641 if (type == USE_REDRAW)
7642 redraw_block(row, end, wp);
7643 else
7644#endif
7645 if (type == USE_T_CAL)
7646 {
7647 term_append_lines(line_count);
7648 screen_start(); /* don't know where cursor is now */
7649 }
7650 else
7651 {
7652 for (i = 0; i < line_count; i++)
7653 {
7654 if (type == USE_T_AL)
7655 {
7656 if (i && cursor_row != 0)
7657 windgoto(cursor_row, 0);
7658 out_str(T_AL);
7659 }
7660 else /* type == USE_T_SR */
7661 out_str(T_SR);
7662 screen_start(); /* don't know where cursor is now */
7663 }
7664 }
7665
7666 /*
7667 * With scroll-reverse and 'da' flag set we need to clear the lines that
7668 * have been scrolled down into the region.
7669 */
7670 if (type == USE_T_SR && *T_DA)
7671 {
7672 for (i = 0; i < line_count; ++i)
7673 {
7674 windgoto(off + i, 0);
7675 out_str(T_CE);
7676 screen_start(); /* don't know where cursor is now */
7677 }
7678 }
7679
7680#ifdef FEAT_GUI
7681 gui_can_update_cursor();
7682 if (gui.in_use)
7683 out_flush(); /* always flush after a scroll */
7684#endif
7685 return OK;
7686}
7687
7688/*
7689 * delete lines on the screen and update ScreenLines[]
7690 * 'end' is the line after the scrolled part. Normally it is Rows.
7691 * When scrolling region used 'off' is the offset from the top for the region.
7692 * 'row' and 'end' are relative to the start of the region.
7693 *
7694 * Return OK for success, FAIL if the lines are not deleted.
7695 */
7696/*ARGSUSED*/
7697 int
7698screen_del_lines(off, row, line_count, end, force, wp)
7699 int off;
7700 int row;
7701 int line_count;
7702 int end;
7703 int force; /* even when line_count > p_ttyscroll */
7704 win_T *wp; /* NULL or window to use width from */
7705{
7706 int j;
7707 int i;
7708 unsigned temp;
7709 int cursor_row;
7710 int cursor_end;
7711 int result_empty; /* result is empty until end of region */
7712 int can_delete; /* deleting line codes can be used */
7713 int type;
7714
7715 /*
7716 * FAIL if
7717 * - there is no valid screen
7718 * - the screen has to be redrawn completely
7719 * - the line count is less than one
7720 * - the line count is more than 'ttyscroll'
7721 */
7722 if (!screen_valid(TRUE) || line_count <= 0 ||
7723 (!force && line_count > p_ttyscroll))
7724 return FAIL;
7725
7726 /*
7727 * Check if the rest of the current region will become empty.
7728 */
7729 result_empty = row + line_count >= end;
7730
7731 /*
7732 * We can delete lines only when 'db' flag not set or when 'ce' option
7733 * available.
7734 */
7735 can_delete = (*T_DB == NUL || can_clear(T_CE));
7736
7737 /*
7738 * There are six ways to delete lines:
7739 * 0. When in a vertically split window and t_CV isn't set, redraw the
7740 * characters from ScreenLines[].
7741 * 1. Use T_CD if it exists and the result is empty.
7742 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
7743 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
7744 * none of the other ways work.
7745 * 4. Use T_CE (erase line) if the result is empty.
7746 * 5. Use T_DL (delete line) if it exists.
7747 * 6. redraw the characters from ScreenLines[].
7748 */
7749#ifdef FEAT_VERTSPLIT
7750 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
7751 type = USE_REDRAW;
7752 else
7753#endif
7754 if (can_clear(T_CD) && result_empty)
7755 type = USE_T_CD;
7756#if defined(__BEOS__) && defined(BEOS_DR8)
7757 /*
7758 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
7759 * its internal termcap... this works okay for tests which test *T_DB !=
7760 * NUL. It has the disadvantage that the user cannot use any :set t_*
7761 * command to get T_DB (back) to empty_option, only :set term=... will do
7762 * the trick...
7763 * Anyway, this hack will hopefully go away with the next OS release.
7764 * (Olaf Seibert)
7765 */
7766 else if (row == 0 && T_DB == empty_option
7767 && (line_count == 1 || *T_CDL == NUL))
7768#else
7769 else if (row == 0 && (
7770#ifndef AMIGA
7771 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
7772 * up, so use delete-line command */
7773 line_count == 1 ||
7774#endif
7775 *T_CDL == NUL))
7776#endif
7777 type = USE_NL;
7778 else if (*T_CDL != NUL && line_count > 1 && can_delete)
7779 type = USE_T_CDL;
7780 else if (can_clear(T_CE) && result_empty
7781#ifdef FEAT_VERTSPLIT
7782 && (wp == NULL || wp->w_width == Columns)
7783#endif
7784 )
7785 type = USE_T_CE;
7786 else if (*T_DL != NUL && can_delete)
7787 type = USE_T_DL;
7788 else if (*T_CDL != NUL && can_delete)
7789 type = USE_T_CDL;
7790 else
7791 return FAIL;
7792
7793#ifdef FEAT_CLIPBOARD
7794 /* Remove a modeless selection when deleting lines halfway the screen or
7795 * not the full width of the screen. */
7796 if (off + row > 0
7797# ifdef FEAT_VERTSPLIT
7798 || (wp != NULL && wp->w_width != Columns)
7799# endif
7800 )
7801 clip_clear_selection();
7802 else
7803 clip_scroll_selection(line_count);
7804#endif
7805
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806#ifdef FEAT_GUI
7807 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
7808 * scrolling is actually carried out. */
7809 gui_dont_update_cursor();
7810#endif
7811
7812 if (*T_CCS != NUL) /* cursor relative to region */
7813 {
7814 cursor_row = row;
7815 cursor_end = end;
7816 }
7817 else
7818 {
7819 cursor_row = row + off;
7820 cursor_end = end + off;
7821 }
7822
7823 /*
7824 * Now shift LineOffset[] line_count up to reflect the deleted lines.
7825 * Clear the inserted lines in ScreenLines[].
7826 */
7827 row += off;
7828 end += off;
7829 for (i = 0; i < line_count; ++i)
7830 {
7831#ifdef FEAT_VERTSPLIT
7832 if (wp != NULL && wp->w_width != Columns)
7833 {
7834 /* need to copy part of a line */
7835 j = row + i;
7836 while ((j += line_count) <= end - 1)
7837 linecopy(j - line_count, j, wp);
7838 j -= line_count;
7839 if (can_clear((char_u *)" "))
7840 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
7841 else
7842 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
7843 LineWraps[j] = FALSE;
7844 }
7845 else
7846#endif
7847 {
7848 /* whole width, moving the line pointers is faster */
7849 j = row + i;
7850 temp = LineOffset[j];
7851 while ((j += line_count) <= end - 1)
7852 {
7853 LineOffset[j - line_count] = LineOffset[j];
7854 LineWraps[j - line_count] = LineWraps[j];
7855 }
7856 LineOffset[j - line_count] = temp;
7857 LineWraps[j - line_count] = FALSE;
7858 if (can_clear((char_u *)" "))
7859 lineclear(temp, (int)Columns);
7860 else
7861 lineinvalid(temp, (int)Columns);
7862 }
7863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864
7865 screen_stop_highlight();
7866
7867#ifdef FEAT_VERTSPLIT
7868 /* redraw the characters */
7869 if (type == USE_REDRAW)
7870 redraw_block(row, end, wp);
7871 else
7872#endif
7873 if (type == USE_T_CD) /* delete the lines */
7874 {
7875 windgoto(cursor_row, 0);
7876 out_str(T_CD);
7877 screen_start(); /* don't know where cursor is now */
7878 }
7879 else if (type == USE_T_CDL)
7880 {
7881 windgoto(cursor_row, 0);
7882 term_delete_lines(line_count);
7883 screen_start(); /* don't know where cursor is now */
7884 }
7885 /*
7886 * Deleting lines at top of the screen or scroll region: Just scroll
7887 * the whole screen (scroll region) up by outputting newlines on the
7888 * last line.
7889 */
7890 else if (type == USE_NL)
7891 {
7892 windgoto(cursor_end - 1, 0);
7893 for (i = line_count; --i >= 0; )
7894 out_char('\n'); /* cursor will remain on same line */
7895 }
7896 else
7897 {
7898 for (i = line_count; --i >= 0; )
7899 {
7900 if (type == USE_T_DL)
7901 {
7902 windgoto(cursor_row, 0);
7903 out_str(T_DL); /* delete a line */
7904 }
7905 else /* type == USE_T_CE */
7906 {
7907 windgoto(cursor_row + i, 0);
7908 out_str(T_CE); /* erase a line */
7909 }
7910 screen_start(); /* don't know where cursor is now */
7911 }
7912 }
7913
7914 /*
7915 * If the 'db' flag is set, we need to clear the lines that have been
7916 * scrolled up at the bottom of the region.
7917 */
7918 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
7919 {
7920 for (i = line_count; i > 0; --i)
7921 {
7922 windgoto(cursor_end - i, 0);
7923 out_str(T_CE); /* erase a line */
7924 screen_start(); /* don't know where cursor is now */
7925 }
7926 }
7927
7928#ifdef FEAT_GUI
7929 gui_can_update_cursor();
7930 if (gui.in_use)
7931 out_flush(); /* always flush after a scroll */
7932#endif
7933
7934 return OK;
7935}
7936
7937/*
7938 * show the current mode and ruler
7939 *
7940 * If clear_cmdline is TRUE, clear the rest of the cmdline.
7941 * If clear_cmdline is FALSE there may be a message there that needs to be
7942 * cleared only if a mode is shown.
7943 * Return the length of the message (0 if no message).
7944 */
7945 int
7946showmode()
7947{
7948 int need_clear;
7949 int length = 0;
7950 int do_mode;
7951 int attr;
7952 int nwr_save;
7953#ifdef FEAT_INS_EXPAND
7954 int sub_attr;
7955#endif
7956
7957 do_mode = (p_smd && ((State & INSERT) || restart_edit
7958#ifdef FEAT_VISUAL
7959 || VIsual_active
7960#endif
7961 ));
7962 if (do_mode || Recording)
7963 {
7964 /*
7965 * Don't show mode right now, when not redrawing or inside a mapping.
7966 * Call char_avail() only when we are going to show something, because
7967 * it takes a bit of time.
7968 */
7969 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
7970 {
7971 redraw_cmdline = TRUE; /* show mode later */
7972 return 0;
7973 }
7974
7975 nwr_save = need_wait_return;
7976
7977 /* wait a bit before overwriting an important message */
7978 check_for_delay(FALSE);
7979
7980 /* if the cmdline is more than one line high, erase top lines */
7981 need_clear = clear_cmdline;
7982 if (clear_cmdline && cmdline_row < Rows - 1)
7983 msg_clr_cmdline(); /* will reset clear_cmdline */
7984
7985 /* Position on the last line in the window, column 0 */
7986 msg_pos_mode();
7987 cursor_off();
7988 attr = hl_attr(HLF_CM); /* Highlight mode */
7989 if (do_mode)
7990 {
7991 MSG_PUTS_ATTR("--", attr);
7992#if defined(FEAT_XIM)
7993 if (xic != NULL && im_get_status() && !p_imdisable
7994 && curbuf->b_p_iminsert == B_IMODE_IM)
7995# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
7996 MSG_PUTS_ATTR(" IM", attr);
7997# else
7998 MSG_PUTS_ATTR(" XIM", attr);
7999# endif
8000#endif
8001#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8002 if (gui.in_use)
8003 {
8004 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008005 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 }
8007#endif
8008#ifdef FEAT_INS_EXPAND
8009 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8010 {
8011 /* These messages can get long, avoid a wrap in a narrow
8012 * window. Prefer showing edit_submode_extra. */
8013 length = (Rows - msg_row) * Columns - 3;
8014 if (edit_submode_extra != NULL)
8015 length -= vim_strsize(edit_submode_extra);
8016 if (length > 0)
8017 {
8018 if (edit_submode_pre != NULL)
8019 length -= vim_strsize(edit_submode_pre);
8020 if (length - vim_strsize(edit_submode) > 0)
8021 {
8022 if (edit_submode_pre != NULL)
8023 msg_puts_attr(edit_submode_pre, attr);
8024 msg_puts_attr(edit_submode, attr);
8025 }
8026 if (edit_submode_extra != NULL)
8027 {
8028 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8029 if ((int)edit_submode_highl < (int)HLF_COUNT)
8030 sub_attr = hl_attr(edit_submode_highl);
8031 else
8032 sub_attr = attr;
8033 msg_puts_attr(edit_submode_extra, sub_attr);
8034 }
8035 }
8036 length = 0;
8037 }
8038 else
8039#endif
8040 {
8041#ifdef FEAT_VREPLACE
8042 if (State & VREPLACE_FLAG)
8043 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8044 else
8045#endif
8046 if (State & REPLACE_FLAG)
8047 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8048 else if (State & INSERT)
8049 {
8050#ifdef FEAT_RIGHTLEFT
8051 if (p_ri)
8052 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8053#endif
8054 MSG_PUTS_ATTR(_(" INSERT"), attr);
8055 }
8056 else if (restart_edit == 'I')
8057 MSG_PUTS_ATTR(_(" (insert)"), attr);
8058 else if (restart_edit == 'R')
8059 MSG_PUTS_ATTR(_(" (replace)"), attr);
8060 else if (restart_edit == 'V')
8061 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8062#ifdef FEAT_RIGHTLEFT
8063 if (p_hkmap)
8064 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8065# ifdef FEAT_FKMAP
8066 if (p_fkmap)
8067 MSG_PUTS_ATTR(farsi_text_5, attr);
8068# endif
8069#endif
8070#ifdef FEAT_KEYMAP
8071 if (State & LANGMAP)
8072 {
8073# ifdef FEAT_ARABIC
8074 if (curwin->w_p_arab)
8075 MSG_PUTS_ATTR(_(" Arabic"), attr);
8076 else
8077# endif
8078 MSG_PUTS_ATTR(_(" (lang)"), attr);
8079 }
8080#endif
8081 if ((State & INSERT) && p_paste)
8082 MSG_PUTS_ATTR(_(" (paste)"), attr);
8083
8084#ifdef FEAT_VISUAL
8085 if (VIsual_active)
8086 {
8087 char *p;
8088
8089 /* Don't concatenate separate words to avoid translation
8090 * problems. */
8091 switch ((VIsual_select ? 4 : 0)
8092 + (VIsual_mode == Ctrl_V) * 2
8093 + (VIsual_mode == 'V'))
8094 {
8095 case 0: p = N_(" VISUAL"); break;
8096 case 1: p = N_(" VISUAL LINE"); break;
8097 case 2: p = N_(" VISUAL BLOCK"); break;
8098 case 4: p = N_(" SELECT"); break;
8099 case 5: p = N_(" SELECT LINE"); break;
8100 default: p = N_(" SELECT BLOCK"); break;
8101 }
8102 MSG_PUTS_ATTR(_(p), attr);
8103 }
8104#endif
8105 MSG_PUTS_ATTR(" --", attr);
8106 }
8107 need_clear = TRUE;
8108 }
8109 if (Recording
8110#ifdef FEAT_INS_EXPAND
8111 && edit_submode == NULL /* otherwise it gets too long */
8112#endif
8113 )
8114 {
8115 MSG_PUTS_ATTR(_("recording"), attr);
8116 need_clear = TRUE;
8117 }
8118 if (need_clear || clear_cmdline)
8119 msg_clr_eos();
8120 msg_didout = FALSE; /* overwrite this message */
8121 length = msg_col;
8122 msg_col = 0;
8123 need_wait_return = nwr_save; /* never ask for hit-return for this */
8124 }
8125 else if (clear_cmdline && msg_silent == 0)
8126 /* Clear the whole command line. Will reset "clear_cmdline". */
8127 msg_clr_cmdline();
8128
8129#ifdef FEAT_CMDL_INFO
8130# ifdef FEAT_VISUAL
8131 /* In Visual mode the size of the selected area must be redrawn. */
8132 if (VIsual_active)
8133 clear_showcmd();
8134# endif
8135
8136 /* If the last window has no status line, the ruler is after the mode
8137 * message and must be redrawn */
8138 if (redrawing()
8139# ifdef FEAT_WINDOWS
8140 && lastwin->w_status_height == 0
8141# endif
8142 )
8143 win_redr_ruler(lastwin, TRUE);
8144#endif
8145 redraw_cmdline = FALSE;
8146 clear_cmdline = FALSE;
8147
8148 return length;
8149}
8150
8151/*
8152 * Position for a mode message.
8153 */
8154 static void
8155msg_pos_mode()
8156{
8157 msg_col = 0;
8158 msg_row = Rows - 1;
8159}
8160
8161/*
8162 * Delete mode message. Used when ESC is typed which is expected to end
8163 * Insert mode (but Insert mode didn't end yet!).
8164 */
8165 void
8166unshowmode(force)
8167 int force;
8168{
8169 /*
8170 * Don't delete it right now, when not redrawing or insided a mapping.
8171 */
8172 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8173 redraw_cmdline = TRUE; /* delete mode later */
8174 else
8175 {
8176 msg_pos_mode();
8177 if (Recording)
8178 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8179 msg_clr_eos();
8180 }
8181}
8182
8183#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
8184/*
8185 * Get the character to use in a status line. Get its attributes in "*attr".
8186 */
8187 static int
8188fillchar_status(attr, is_curwin)
8189 int *attr;
8190 int is_curwin;
8191{
8192 int fill;
8193 if (is_curwin)
8194 {
8195 *attr = hl_attr(HLF_S);
8196 fill = fill_stl;
8197 }
8198 else
8199 {
8200 *attr = hl_attr(HLF_SNC);
8201 fill = fill_stlnc;
8202 }
8203 /* Use fill when there is highlighting, and highlighting of current
8204 * window differs, or the fillchars differ, or this is not the
8205 * current window */
8206 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
8207 || !is_curwin || firstwin == lastwin)
8208 || (fill_stl != fill_stlnc)))
8209 return fill;
8210 if (is_curwin)
8211 return '^';
8212 return '=';
8213}
8214#endif
8215
8216#ifdef FEAT_VERTSPLIT
8217/*
8218 * Get the character to use in a separator between vertically split windows.
8219 * Get its attributes in "*attr".
8220 */
8221 static int
8222fillchar_vsep(attr)
8223 int *attr;
8224{
8225 *attr = hl_attr(HLF_C);
8226 if (*attr == 0 && fill_vert == ' ')
8227 return '|';
8228 else
8229 return fill_vert;
8230}
8231#endif
8232
8233/*
8234 * Return TRUE if redrawing should currently be done.
8235 */
8236 int
8237redrawing()
8238{
8239 return (!RedrawingDisabled
8240 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
8241}
8242
8243/*
8244 * Return TRUE if printing messages should currently be done.
8245 */
8246 int
8247messaging()
8248{
8249 return (!(p_lz && char_avail() && !KeyTyped));
8250}
8251
8252/*
8253 * Show current status info in ruler and various other places
8254 * If always is FALSE, only show ruler if position has changed.
8255 */
8256 void
8257showruler(always)
8258 int always;
8259{
8260 if (!always && !redrawing())
8261 return;
8262#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008263 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 win_redr_custom(curwin, FALSE);
8265 else
8266#endif
8267#ifdef FEAT_CMDL_INFO
8268 win_redr_ruler(curwin, always);
8269#endif
8270
8271#ifdef FEAT_TITLE
8272 if (need_maketitle
8273# ifdef FEAT_STL_OPT
8274 || (p_icon && (stl_syntax & STL_IN_ICON))
8275 || (p_title && (stl_syntax & STL_IN_TITLE))
8276# endif
8277 )
8278 maketitle();
8279#endif
8280}
8281
8282#ifdef FEAT_CMDL_INFO
8283 static void
8284win_redr_ruler(wp, always)
8285 win_T *wp;
8286 int always;
8287{
8288 char_u buffer[70];
8289 int row;
8290 int fillchar;
8291 int attr;
8292 int empty_line = FALSE;
8293 colnr_T virtcol;
8294 int i;
8295 int o;
8296#ifdef FEAT_VERTSPLIT
8297 int this_ru_col;
8298 int off = 0;
8299 int width = Columns;
8300# define WITH_OFF(x) x
8301# define WITH_WIDTH(x) x
8302#else
8303# define WITH_OFF(x) 0
8304# define WITH_WIDTH(x) Columns
8305# define this_ru_col ru_col
8306#endif
8307
8308 /* If 'ruler' off or redrawing disabled, don't do anything */
8309 if (!p_ru)
8310 return;
8311
8312 /*
8313 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
8314 * after deleting lines, before cursor.lnum is corrected.
8315 */
8316 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
8317 return;
8318
8319#ifdef FEAT_INS_EXPAND
8320 /* Don't draw the ruler while doing insert-completion, it might overwrite
8321 * the (long) mode message. */
8322# ifdef FEAT_WINDOWS
8323 if (wp == lastwin && lastwin->w_status_height == 0)
8324# endif
8325 if (edit_submode != NULL)
8326 return;
8327#endif
8328
8329#ifdef FEAT_STL_OPT
8330 if (*p_ruf)
8331 {
8332 win_redr_custom(wp, TRUE);
8333 return;
8334 }
8335#endif
8336
8337 /*
8338 * Check if not in Insert mode and the line is empty (will show "0-1").
8339 */
8340 if (!(State & INSERT)
8341 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
8342 empty_line = TRUE;
8343
8344 /*
8345 * Only draw the ruler when something changed.
8346 */
8347 validate_virtcol_win(wp);
8348 if ( redraw_cmdline
8349 || always
8350 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
8351 || wp->w_cursor.col != wp->w_ru_cursor.col
8352 || wp->w_virtcol != wp->w_ru_virtcol
8353#ifdef FEAT_VIRTUALEDIT
8354 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
8355#endif
8356 || wp->w_topline != wp->w_ru_topline
8357 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
8358#ifdef FEAT_DIFF
8359 || wp->w_topfill != wp->w_ru_topfill
8360#endif
8361 || empty_line != wp->w_ru_empty)
8362 {
8363 cursor_off();
8364#ifdef FEAT_WINDOWS
8365 if (wp->w_status_height)
8366 {
8367 row = W_WINROW(wp) + wp->w_height;
8368 fillchar = fillchar_status(&attr, wp == curwin);
8369# ifdef FEAT_VERTSPLIT
8370 off = W_WINCOL(wp);
8371 width = W_WIDTH(wp);
8372# endif
8373 }
8374 else
8375#endif
8376 {
8377 row = Rows - 1;
8378 fillchar = ' ';
8379 attr = 0;
8380#ifdef FEAT_VERTSPLIT
8381 width = Columns;
8382 off = 0;
8383#endif
8384 }
8385
8386 /* In list mode virtcol needs to be recomputed */
8387 virtcol = wp->w_virtcol;
8388 if (wp->w_p_list && lcs_tab1 == NUL)
8389 {
8390 wp->w_p_list = FALSE;
8391 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
8392 wp->w_p_list = TRUE;
8393 }
8394
8395 /*
8396 * Some sprintfs return the length, some return a pointer.
8397 * To avoid portability problems we use strlen() here.
8398 */
8399 sprintf((char *)buffer, "%ld,",
8400 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
8401 ? 0L
8402 : (long)(wp->w_cursor.lnum));
8403 col_print(buffer + STRLEN(buffer),
8404 empty_line ? 0 : (int)wp->w_cursor.col + 1,
8405 (int)virtcol + 1);
8406
8407 /*
8408 * Add a "50%" if there is room for it.
8409 * On the last line, don't print in the last column (scrolls the
8410 * screen up on some terminals).
8411 */
8412 i = (int)STRLEN(buffer);
8413 get_rel_pos(wp, buffer + i + 1);
8414 o = i + vim_strsize(buffer + i + 1);
8415#ifdef FEAT_WINDOWS
8416 if (wp->w_status_height == 0) /* can't use last char of screen */
8417#endif
8418 ++o;
8419#ifdef FEAT_VERTSPLIT
8420 this_ru_col = ru_col - (Columns - width);
8421 if (this_ru_col < 0)
8422 this_ru_col = 0;
8423#endif
8424 /* Never use more than half the window/screen width, leave the other
8425 * half for the filename. */
8426 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
8427 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
8428 if (this_ru_col + o < WITH_WIDTH(width))
8429 {
8430 while (this_ru_col + o < WITH_WIDTH(width))
8431 {
8432#ifdef FEAT_MBYTE
8433 if (has_mbyte)
8434 i += (*mb_char2bytes)(fillchar, buffer + i);
8435 else
8436#endif
8437 buffer[i++] = fillchar;
8438 ++o;
8439 }
8440 get_rel_pos(wp, buffer + i);
8441 }
8442 /* Truncate at window boundary. */
8443#ifdef FEAT_MBYTE
8444 if (has_mbyte)
8445 {
8446 o = 0;
8447 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len_check)(buffer + i))
8448 {
8449 o += (*mb_ptr2cells)(buffer + i);
8450 if (this_ru_col + o > WITH_WIDTH(width))
8451 {
8452 buffer[i] = NUL;
8453 break;
8454 }
8455 }
8456 }
8457 else
8458#endif
8459 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
8460 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
8461
8462 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
8463 i = redraw_cmdline;
8464 screen_fill(row, row + 1,
8465 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
8466 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
8467 fillchar, fillchar, attr);
8468 /* don't redraw the cmdline because of showing the ruler */
8469 redraw_cmdline = i;
8470 wp->w_ru_cursor = wp->w_cursor;
8471 wp->w_ru_virtcol = wp->w_virtcol;
8472 wp->w_ru_empty = empty_line;
8473 wp->w_ru_topline = wp->w_topline;
8474 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
8475#ifdef FEAT_DIFF
8476 wp->w_ru_topfill = wp->w_topfill;
8477#endif
8478 }
8479}
8480#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00008481
8482#if defined(FEAT_LINEBREAK) || defined(PROTO)
8483/*
8484 * Return the width of the 'number' column.
8485 * Zero when 'number' isn't set.
8486 * Otherwise it depends on 'numberwidth' and the line count.
8487 */
8488 int
8489number_width(wp)
8490 win_T *wp;
8491{
8492 int n;
8493 linenr_T lnum;
8494
8495 if (!wp->w_p_nu)
8496 return 0;
8497
8498 lnum = wp->w_buffer->b_ml.ml_line_count;
8499 if (lnum == wp->w_nrwidth_line_count)
8500 return wp->w_nrwidth_width;
8501 wp->w_nrwidth_line_count = lnum;
8502
8503 n = 0;
8504 do
8505 {
8506 lnum /= 10;
8507 ++n;
8508 } while (lnum > 0);
8509
8510 /* 'numberwidth' gives the minimal width plus one */
8511 if (n < wp->w_p_nuw - 1)
8512 n = wp->w_p_nuw - 1;
8513
8514 wp->w_nrwidth_width = n;
8515 return n;
8516}
8517#endif