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