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