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