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