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