blob: 6c6403e0bac6ac4b2b045c3f822a7ff0ad7a508f [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.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000030 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 * (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 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000073 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
77 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
80 *
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
83 *
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
87 */
88
89#include "vim.h"
90
91/*
92 * The attributes that are actually active for writing to the screen.
93 */
94static int screen_attr = 0;
95
96/*
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
99 */
100static int screen_cur_row, screen_cur_col; /* last known cursor position */
101
102#ifdef FEAT_SEARCH_EXTRA
103/*
104 * Struct used for highlighting 'hlsearch' matches for the last use search
105 * pattern or a ":match" item.
106 * For 'hlsearch' there is one pattern for all windows. For ":match" there is
107 * a different pattern for each window.
108 */
109typedef struct
110{
111 regmmatch_T rm; /* points to the regexp program; contains last found
112 match (may continue in next line) */
113 buf_T *buf; /* the buffer to search for a match */
114 linenr_T lnum; /* the line to search for a match */
115 int attr; /* attributes to be used for a match */
116 int attr_cur; /* attributes currently active in win_line() */
117 linenr_T first_lnum; /* first lnum to search for multi-line pat */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000118 colnr_T startcol; /* in win_line() points to char where HL starts */
119 colnr_T endcol; /* in win_line() points to char where HL ends */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120} match_T;
121
122static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000123static match_T match_hl[3]; /* used for ":match" highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#endif
125
126#ifdef FEAT_FOLDING
127static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
128#endif
129
130/*
131 * Buffer for one screen line (characters and attributes).
132 */
133static schar_T *current_ScreenLine;
134
135static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000136static 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 +0000137#ifdef FEAT_FOLDING
138static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
139static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
140static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
141#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000142static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
144#ifdef FEAT_RIGHTLEFT
145static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
146# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
147#else
148static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
149# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_VERTSPLIT
152static void draw_vsep_win __ARGS((win_T *wp, int row));
153#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000154#ifdef FEAT_STL_OPT
155static void redraw_custum_statusline __ARGS((win_T *wp));
156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_SEARCH_EXTRA
158static void start_search_hl __ARGS((void));
159static void end_search_hl __ARGS((void));
160static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
161static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
162#endif
163static void screen_start_highlight __ARGS((int attr));
164static void screen_char __ARGS((unsigned off, int row, int col));
165#ifdef FEAT_MBYTE
166static void screen_char_2 __ARGS((unsigned off, int row, int col));
167#endif
168static void screenclear2 __ARGS((void));
169static void lineclear __ARGS((unsigned off, int width));
170static void lineinvalid __ARGS((unsigned off, int width));
171#ifdef FEAT_VERTSPLIT
172static void linecopy __ARGS((int to, int from, win_T *wp));
173static void redraw_block __ARGS((int row, int end, win_T *wp));
174#endif
175static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
176static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000178#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000179static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
182static int fillchar_status __ARGS((int *attr, int is_curwin));
183#endif
184#ifdef FEAT_VERTSPLIT
185static int fillchar_vsep __ARGS((int *attr));
186#endif
187#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000188static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
190#ifdef FEAT_CMDL_INFO
191static void win_redr_ruler __ARGS((win_T *wp, int always));
192#endif
193
194#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
195/* Ugly global: overrule attribute used by screen_char() */
196static int screen_char_attr = 0;
197#endif
198
199/*
200 * Redraw the current window later, with update_screen(type).
201 * Set must_redraw only if not already set to a higher value.
202 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
203 */
204 void
205redraw_later(type)
206 int type;
207{
208 redraw_win_later(curwin, type);
209}
210
211 void
212redraw_win_later(wp, type)
213 win_T *wp;
214 int type;
215{
216 if (wp->w_redr_type < type)
217 {
218 wp->w_redr_type = type;
219 if (type >= NOT_VALID)
220 wp->w_lines_valid = 0;
221 if (must_redraw < type) /* must_redraw is the maximum of all windows */
222 must_redraw = type;
223 }
224}
225
226/*
227 * Force a complete redraw later. Also resets the highlighting. To be used
228 * after executing a shell command that messes up the screen.
229 */
230 void
231redraw_later_clear()
232{
233 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000234#ifdef FEAT_GUI
235 if (gui.in_use)
236 /* Use a code that will reset gui.highlight_mask in
237 * gui_stop_highlight(). */
238 screen_attr = HL_ALL + 1;
239 else
240#endif
241 /* Use attributes that is very unlikely to appear in text. */
242 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
246 * Mark all windows to be redrawn later.
247 */
248 void
249redraw_all_later(type)
250 int type;
251{
252 win_T *wp;
253
254 FOR_ALL_WINDOWS(wp)
255 {
256 redraw_win_later(wp, type);
257 }
258}
259
260/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000261 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 */
263 void
264redraw_curbuf_later(type)
265 int type;
266{
267 redraw_buf_later(curbuf, type);
268}
269
270 void
271redraw_buf_later(buf, type)
272 buf_T *buf;
273 int type;
274{
275 win_T *wp;
276
277 FOR_ALL_WINDOWS(wp)
278 {
279 if (wp->w_buffer == buf)
280 redraw_win_later(wp, type);
281 }
282}
283
284/*
285 * Changed something in the current window, at buffer line "lnum", that
286 * requires that line and possibly other lines to be redrawn.
287 * Used when entering/leaving Insert mode with the cursor on a folded line.
288 * Used to remove the "$" from a change command.
289 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
290 * may become invalid and the whole window will have to be redrawn.
291 */
292/*ARGSUSED*/
293 void
294redrawWinline(lnum, invalid)
295 linenr_T lnum;
296 int invalid; /* window line height is invalid now */
297{
298#ifdef FEAT_FOLDING
299 int i;
300#endif
301
302 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
303 curwin->w_redraw_top = lnum;
304 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
305 curwin->w_redraw_bot = lnum;
306 redraw_later(VALID);
307
308#ifdef FEAT_FOLDING
309 if (invalid)
310 {
311 /* A w_lines[] entry for this lnum has become invalid. */
312 i = find_wl_entry(curwin, lnum);
313 if (i >= 0)
314 curwin->w_lines[i].wl_valid = FALSE;
315 }
316#endif
317}
318
319/*
320 * update all windows that are editing the current buffer
321 */
322 void
323update_curbuf(type)
324 int type;
325{
326 redraw_curbuf_later(type);
327 update_screen(type);
328}
329
330/*
331 * update_screen()
332 *
333 * Based on the current value of curwin->w_topline, transfer a screenfull
334 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
335 */
336 void
337update_screen(type)
338 int type;
339{
340 win_T *wp;
341 static int did_intro = FALSE;
342#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
343 int did_one;
344#endif
345
346 if (!screen_valid(TRUE))
347 return;
348
349 if (must_redraw)
350 {
351 if (type < must_redraw) /* use maximal type */
352 type = must_redraw;
353 must_redraw = 0;
354 }
355
356 /* Need to update w_lines[]. */
357 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
358 type = NOT_VALID;
359
360 if (!redrawing())
361 {
362 redraw_later(type); /* remember type for next time */
363 must_redraw = type;
364 if (type > INVERTED_ALL)
365 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
366 return;
367 }
368
369 updating_screen = TRUE;
370#ifdef FEAT_SYN_HL
371 ++display_tick; /* let syntax code know we're in a next round of
372 * display updating */
373#endif
374
375 /*
376 * if the screen was scrolled up when displaying a message, scroll it down
377 */
378 if (msg_scrolled)
379 {
380 clear_cmdline = TRUE;
381 if (msg_scrolled > Rows - 5) /* clearing is faster */
382 type = CLEAR;
383 else if (type != CLEAR)
384 {
385 check_for_delay(FALSE);
386 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
387 type = CLEAR;
388 FOR_ALL_WINDOWS(wp)
389 {
390 if (W_WINROW(wp) < msg_scrolled)
391 {
392 if (W_WINROW(wp) + wp->w_height > msg_scrolled
393 && wp->w_redr_type < REDRAW_TOP
394 && wp->w_lines_valid > 0
395 && wp->w_topline == wp->w_lines[0].wl_lnum)
396 {
397 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
398 wp->w_redr_type = REDRAW_TOP;
399 }
400 else
401 {
402 wp->w_redr_type = NOT_VALID;
403#ifdef FEAT_WINDOWS
404 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
405 <= msg_scrolled)
406 wp->w_redr_status = TRUE;
407#endif
408 }
409 }
410 }
411 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000412#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000413 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 }
416 msg_scrolled = 0;
417 need_wait_return = FALSE;
418 }
419
420 /* reset cmdline_row now (may have been changed temporarily) */
421 compute_cmdrow();
422
423 /* Check for changed highlighting */
424 if (need_highlight_changed)
425 highlight_changed();
426
427 if (type == CLEAR) /* first clear screen */
428 {
429 screenclear(); /* will reset clear_cmdline */
430 type = NOT_VALID;
431 }
432
433 if (clear_cmdline) /* going to clear cmdline (done below) */
434 check_for_delay(FALSE);
435
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000436#ifdef FEAT_LINEBREAK
437 /* Force redraw when width of 'number' column changes. */
438 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000439 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000440 curwin->w_redr_type = NOT_VALID;
441#endif
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 /*
444 * Only start redrawing if there is really something to do.
445 */
446 if (type == INVERTED)
447 update_curswant();
448 if (curwin->w_redr_type < type
449 && !((type == VALID
450 && curwin->w_lines[0].wl_valid
451#ifdef FEAT_DIFF
452 && curwin->w_topfill == curwin->w_old_topfill
453 && curwin->w_botfill == curwin->w_old_botfill
454#endif
455 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
456#ifdef FEAT_VISUAL
457 || (type == INVERTED
458 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
459 && curwin->w_old_visual_mode == VIsual_mode
460 && (curwin->w_valid & VALID_VIRTCOL)
461 && curwin->w_old_curswant == curwin->w_curswant)
462#endif
463 ))
464 curwin->w_redr_type = type;
465
Bram Moolenaar5a305422006-04-28 22:38:25 +0000466#ifdef FEAT_WINDOWS
467 /* Redraw the tab pages line if needed. */
468 if (redraw_tabline || type >= NOT_VALID)
469 draw_tabline();
470#endif
471
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472#ifdef FEAT_SYN_HL
473 /*
474 * Correct stored syntax highlighting info for changes in each displayed
475 * buffer. Each buffer must only be done once.
476 */
477 FOR_ALL_WINDOWS(wp)
478 {
479 if (wp->w_buffer->b_mod_set)
480 {
481# ifdef FEAT_WINDOWS
482 win_T *wwp;
483
484 /* Check if we already did this buffer. */
485 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
486 if (wwp->w_buffer == wp->w_buffer)
487 break;
488# endif
489 if (
490# ifdef FEAT_WINDOWS
491 wwp == wp &&
492# endif
493 syntax_present(wp->w_buffer))
494 syn_stack_apply_changes(wp->w_buffer);
495 }
496 }
497#endif
498
499 /*
500 * Go from top to bottom through the windows, redrawing the ones that need
501 * it.
502 */
503#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
504 did_one = FALSE;
505#endif
506#ifdef FEAT_SEARCH_EXTRA
507 search_hl.rm.regprog = NULL;
508#endif
509 FOR_ALL_WINDOWS(wp)
510 {
511 if (wp->w_redr_type != 0)
512 {
513 cursor_off();
514#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
515 if (!did_one)
516 {
517 did_one = TRUE;
518# ifdef FEAT_SEARCH_EXTRA
519 start_search_hl();
520# endif
521# ifdef FEAT_CLIPBOARD
522 /* When Visual area changed, may have to update selection. */
523 if (clip_star.available && clip_isautosel())
524 clip_update_selection();
525# endif
526#ifdef FEAT_GUI
527 /* Remove the cursor before starting to do anything, because
528 * scrolling may make it difficult to redraw the text under
529 * it. */
530 if (gui.in_use)
531 gui_undraw_cursor();
532#endif
533 }
534#endif
535 win_update(wp);
536 }
537
538#ifdef FEAT_WINDOWS
539 /* redraw status line after the window to minimize cursor movement */
540 if (wp->w_redr_status)
541 {
542 cursor_off();
543 win_redr_status(wp);
544 }
545#endif
546 }
547#if defined(FEAT_SEARCH_EXTRA)
548 end_search_hl();
549#endif
550
551#ifdef FEAT_WINDOWS
552 /* Reset b_mod_set flags. Going through all windows is probably faster
553 * than going through all buffers (there could be many buffers). */
554 for (wp = firstwin; wp != NULL; wp = wp->w_next)
555 wp->w_buffer->b_mod_set = FALSE;
556#else
557 curbuf->b_mod_set = FALSE;
558#endif
559
560 updating_screen = FALSE;
561#ifdef FEAT_GUI
562 gui_may_resize_shell();
563#endif
564
565 /* Clear or redraw the command line. Done last, because scrolling may
566 * mess up the command line. */
567 if (clear_cmdline || redraw_cmdline)
568 showmode();
569
570 /* May put up an introductory message when not editing a file */
571 if (!did_intro && bufempty()
572 && curbuf->b_fname == NULL
573#ifdef FEAT_WINDOWS
574 && firstwin->w_next == NULL
575#endif
576 && vim_strchr(p_shm, SHM_INTRO) == NULL)
577 intro_message(FALSE);
578 did_intro = TRUE;
579
580#ifdef FEAT_GUI
581 /* Redraw the cursor and update the scrollbars when all screen updating is
582 * done. */
583 if (gui.in_use)
584 {
585 out_flush(); /* required before updating the cursor */
586 if (did_one)
587 gui_update_cursor(FALSE, FALSE);
588 gui_update_scrollbars(FALSE);
589 }
590#endif
591}
592
593#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
594static void update_prepare __ARGS((void));
595static void update_finish __ARGS((void));
596
597/*
598 * Prepare for updating one or more windows.
599 */
600 static void
601update_prepare()
602{
603 cursor_off();
604 updating_screen = TRUE;
605#ifdef FEAT_GUI
606 /* Remove the cursor before starting to do anything, because scrolling may
607 * make it difficult to redraw the text under it. */
608 if (gui.in_use)
609 gui_undraw_cursor();
610#endif
611#ifdef FEAT_SEARCH_EXTRA
612 start_search_hl();
613#endif
614}
615
616/*
617 * Finish updating one or more windows.
618 */
619 static void
620update_finish()
621{
622 if (redraw_cmdline)
623 showmode();
624
625# ifdef FEAT_SEARCH_EXTRA
626 end_search_hl();
627# endif
628
629 updating_screen = FALSE;
630
631# ifdef FEAT_GUI
632 gui_may_resize_shell();
633
634 /* Redraw the cursor and update the scrollbars when all screen updating is
635 * done. */
636 if (gui.in_use)
637 {
638 out_flush(); /* required before updating the cursor */
639 gui_update_cursor(FALSE, FALSE);
640 gui_update_scrollbars(FALSE);
641 }
642# endif
643}
644#endif
645
646#if defined(FEAT_SIGNS) || defined(PROTO)
647 void
648update_debug_sign(buf, lnum)
649 buf_T *buf;
650 linenr_T lnum;
651{
652 win_T *wp;
653 int doit = FALSE;
654
655# ifdef FEAT_FOLDING
656 win_foldinfo.fi_level = 0;
657# endif
658
659 /* update/delete a specific mark */
660 FOR_ALL_WINDOWS(wp)
661 {
662 if (buf != NULL && lnum > 0)
663 {
664 if (wp->w_buffer == buf && lnum >= wp->w_topline
665 && lnum < wp->w_botline)
666 {
667 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
668 wp->w_redraw_top = lnum;
669 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
670 wp->w_redraw_bot = lnum;
671 redraw_win_later(wp, VALID);
672 }
673 }
674 else
675 redraw_win_later(wp, VALID);
676 if (wp->w_redr_type != 0)
677 doit = TRUE;
678 }
679
680 if (!doit)
681 return;
682
683 /* update all windows that need updating */
684 update_prepare();
685
686# ifdef FEAT_WINDOWS
687 for (wp = firstwin; wp; wp = wp->w_next)
688 {
689 if (wp->w_redr_type != 0)
690 win_update(wp);
691 if (wp->w_redr_status)
692 win_redr_status(wp);
693 }
694# else
695 if (curwin->w_redr_type != 0)
696 win_update(curwin);
697# endif
698
699 update_finish();
700}
701#endif
702
703
704#if defined(FEAT_GUI) || defined(PROTO)
705/*
706 * Update a single window, its status line and maybe the command line msg.
707 * Used for the GUI scrollbar.
708 */
709 void
710updateWindow(wp)
711 win_T *wp;
712{
713 update_prepare();
714
715#ifdef FEAT_CLIPBOARD
716 /* When Visual area changed, may have to update selection. */
717 if (clip_star.available && clip_isautosel())
718 clip_update_selection();
719#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000724 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000725 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000726 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (wp->w_redr_status
729# ifdef FEAT_CMDL_INFO
730 || p_ru
731# endif
732# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000733 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734# endif
735 )
736 win_redr_status(wp);
737#endif
738
739 update_finish();
740}
741#endif
742
743/*
744 * Update a single window.
745 *
746 * This may cause the windows below it also to be redrawn (when clearing the
747 * screen or scrolling lines).
748 *
749 * How the window is redrawn depends on wp->w_redr_type. Each type also
750 * implies the one below it.
751 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000752 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
754 * INVERTED redraw the changed part of the Visual area
755 * INVERTED_ALL redraw the whole Visual area
756 * VALID 1. scroll up/down to adjust for a changed w_topline
757 * 2. update lines at the top when scrolled down
758 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000759 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 * b_mod_top and b_mod_bot.
761 * - if wp->w_redraw_top non-zero, redraw lines between
762 * wp->w_redraw_top and wp->w_redr_bot.
763 * - continue redrawing when syntax status is invalid.
764 * 4. if scrolled up, update lines at the bottom.
765 * This results in three areas that may need updating:
766 * top: from first row to top_end (when scrolled down)
767 * mid: from mid_start to mid_end (update inversion or changed text)
768 * bot: from bot_start to last row (when scrolled up)
769 */
770 static void
771win_update(wp)
772 win_T *wp;
773{
774 buf_T *buf = wp->w_buffer;
775 int type;
776 int top_end = 0; /* Below last row of the top area that needs
777 updating. 0 when no top area updating. */
778 int mid_start = 999;/* first row of the mid area that needs
779 updating. 999 when no mid area updating. */
780 int mid_end = 0; /* Below last row of the mid area that needs
781 updating. 0 when no mid area updating. */
782 int bot_start = 999;/* first row of the bot area that needs
783 updating. 999 when no bot area updating */
784#ifdef FEAT_VISUAL
785 int scrolled_down = FALSE; /* TRUE when scrolled down when
786 w_topline got smaller a bit */
787#endif
788#ifdef FEAT_SEARCH_EXTRA
789 int top_to_mod = FALSE; /* redraw above mod_top */
790#endif
791
792 int row; /* current window row to display */
793 linenr_T lnum; /* current buffer lnum to display */
794 int idx; /* current index in w_lines[] */
795 int srow; /* starting row of the current line */
796
797 int eof = FALSE; /* if TRUE, we hit the end of the file */
798 int didline = FALSE; /* if TRUE, we finished the last line */
799 int i;
800 long j;
801 static int recursive = FALSE; /* being called recursively */
802 int old_botline = wp->w_botline;
803#ifdef FEAT_FOLDING
804 long fold_count;
805#endif
806#ifdef FEAT_SYN_HL
807 /* remember what happened to the previous line, to know if
808 * check_visual_highlight() can be used */
809#define DID_NONE 1 /* didn't update a line */
810#define DID_LINE 2 /* updated a normal line */
811#define DID_FOLD 3 /* updated a folded line */
812 int did_update = DID_NONE;
813 linenr_T syntax_last_parsed = 0; /* last parsed text line */
814#endif
815 linenr_T mod_top = 0;
816 linenr_T mod_bot = 0;
817#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
818 int save_got_int;
819#endif
820
821 type = wp->w_redr_type;
822
823 if (type == NOT_VALID)
824 {
825#ifdef FEAT_WINDOWS
826 wp->w_redr_status = TRUE;
827#endif
828 wp->w_lines_valid = 0;
829 }
830
831 /* Window is zero-height: nothing to draw. */
832 if (wp->w_height == 0)
833 {
834 wp->w_redr_type = 0;
835 return;
836 }
837
838#ifdef FEAT_VERTSPLIT
839 /* Window is zero-width: Only need to draw the separator. */
840 if (wp->w_width == 0)
841 {
842 /* draw the vertical separator right of this window */
843 draw_vsep_win(wp, 0);
844 wp->w_redr_type = 0;
845 return;
846 }
847#endif
848
849#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000850 /* Setup for ":match" and 'hlsearch' highlighting. Disable any previous
851 * match */
852 for (i = 0; i < 3; ++i)
853 {
854 match_hl[i].rm = wp->w_match[i];
855 if (wp->w_match_id[i] == 0)
856 match_hl[i].attr = 0;
857 else
858 match_hl[i].attr = syn_id2attr(wp->w_match_id[i]);
859 match_hl[i].buf = buf;
860 match_hl[i].lnum = 0;
861 match_hl[i].first_lnum = 0;
862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 search_hl.buf = buf;
864 search_hl.lnum = 0;
865 search_hl.first_lnum = 0;
866#endif
867
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000868#ifdef FEAT_LINEBREAK
869 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000870 i = wp->w_p_nu ? number_width(wp) : 0;
871 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000872 {
873 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000874 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000875 }
876 else
877#endif
878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
880 {
881 /*
882 * When there are both inserted/deleted lines and specific lines to be
883 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
884 * everything (only happens when redrawing is off for while).
885 */
886 type = NOT_VALID;
887 }
888 else
889 {
890 /*
891 * Set mod_top to the first line that needs displaying because of
892 * changes. Set mod_bot to the first line after the changes.
893 */
894 mod_top = wp->w_redraw_top;
895 if (wp->w_redraw_bot != 0)
896 mod_bot = wp->w_redraw_bot + 1;
897 else
898 mod_bot = 0;
899 wp->w_redraw_top = 0; /* reset for next time */
900 wp->w_redraw_bot = 0;
901 if (buf->b_mod_set)
902 {
903 if (mod_top == 0 || mod_top > buf->b_mod_top)
904 {
905 mod_top = buf->b_mod_top;
906#ifdef FEAT_SYN_HL
907 /* Need to redraw lines above the change that may be included
908 * in a pattern match. */
909 if (syntax_present(buf))
910 {
911 mod_top -= buf->b_syn_sync_linebreaks;
912 if (mod_top < 1)
913 mod_top = 1;
914 }
915#endif
916 }
917 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
918 mod_bot = buf->b_mod_bot;
919
920#ifdef FEAT_SEARCH_EXTRA
921 /* When 'hlsearch' is on and using a multi-line search pattern, a
922 * change in one line may make the Search highlighting in a
923 * previous line invalid. Simple solution: redraw all visible
924 * lines above the change.
925 * Same for a ":match" pattern.
926 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000927 if (search_hl.rm.regprog != NULL
928 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000930 else
931 for (i = 0; i < 3; ++i)
932 if (match_hl[i].rm.regprog != NULL
933 && re_multiline(match_hl[i].rm.regprog))
934 {
935 top_to_mod = TRUE;
936 break;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#endif
939 }
940#ifdef FEAT_FOLDING
941 if (mod_top != 0 && hasAnyFolding(wp))
942 {
943 linenr_T lnumt, lnumb;
944
945 /*
946 * A change in a line can cause lines above it to become folded or
947 * unfolded. Find the top most buffer line that may be affected.
948 * If the line was previously folded and displayed, get the first
949 * line of that fold. If the line is folded now, get the first
950 * folded line. Use the minimum of these two.
951 */
952
953 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
954 * the line below it. If there is no valid entry, use w_topline.
955 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
956 * to this line. If there is no valid entry, use MAXLNUM. */
957 lnumt = wp->w_topline;
958 lnumb = MAXLNUM;
959 for (i = 0; i < wp->w_lines_valid; ++i)
960 if (wp->w_lines[i].wl_valid)
961 {
962 if (wp->w_lines[i].wl_lastlnum < mod_top)
963 lnumt = wp->w_lines[i].wl_lastlnum + 1;
964 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
965 {
966 lnumb = wp->w_lines[i].wl_lnum;
967 /* When there is a fold column it might need updating
968 * in the next line ("J" just above an open fold). */
969 if (wp->w_p_fdc > 0)
970 ++lnumb;
971 }
972 }
973
974 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
975 if (mod_top > lnumt)
976 mod_top = lnumt;
977
978 /* Now do the same for the bottom line (one above mod_bot). */
979 --mod_bot;
980 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
981 ++mod_bot;
982 if (mod_bot < lnumb)
983 mod_bot = lnumb;
984 }
985#endif
986
987 /* When a change starts above w_topline and the end is below
988 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000989 * If the end of the change is above w_topline: do like no change was
990 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (mod_top != 0 && mod_top < wp->w_topline)
992 {
993 if (mod_bot > wp->w_topline)
994 mod_top = wp->w_topline;
995#ifdef FEAT_SYN_HL
996 else if (syntax_present(buf))
997 top_end = 1;
998#endif
999 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001000
1001 /* When line numbers are displayed need to redraw all lines below
1002 * inserted/deleted lines. */
1003 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1004 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006
1007 /*
1008 * When only displaying the lines at the top, set top_end. Used when
1009 * window has scrolled down for msg_scrolled.
1010 */
1011 if (type == REDRAW_TOP)
1012 {
1013 j = 0;
1014 for (i = 0; i < wp->w_lines_valid; ++i)
1015 {
1016 j += wp->w_lines[i].wl_size;
1017 if (j >= wp->w_upd_rows)
1018 {
1019 top_end = j;
1020 break;
1021 }
1022 }
1023 if (top_end == 0)
1024 /* not found (cannot happen?): redraw everything */
1025 type = NOT_VALID;
1026 else
1027 /* top area defined, the rest is VALID */
1028 type = VALID;
1029 }
1030
1031 /*
1032 * If there are no changes on the screen that require a complete redraw,
1033 * handle three cases:
1034 * 1: we are off the top of the screen by a few lines: scroll down
1035 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1036 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1037 * w_lines[] that needs updating.
1038 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001039 if ((type == VALID || type == SOME_VALID
1040 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041#ifdef FEAT_DIFF
1042 && !wp->w_botfill && !wp->w_old_botfill
1043#endif
1044 )
1045 {
1046 if (mod_top != 0 && wp->w_topline == mod_top)
1047 {
1048 /*
1049 * w_topline is the first changed line, the scrolling will be done
1050 * further down.
1051 */
1052 }
1053 else if (wp->w_lines[0].wl_valid
1054 && (wp->w_topline < wp->w_lines[0].wl_lnum
1055#ifdef FEAT_DIFF
1056 || (wp->w_topline == wp->w_lines[0].wl_lnum
1057 && wp->w_topfill > wp->w_old_topfill)
1058#endif
1059 ))
1060 {
1061 /*
1062 * New topline is above old topline: May scroll down.
1063 */
1064#ifdef FEAT_FOLDING
1065 if (hasAnyFolding(wp))
1066 {
1067 linenr_T ln;
1068
1069 /* count the number of lines we are off, counting a sequence
1070 * of folded lines as one */
1071 j = 0;
1072 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1073 {
1074 ++j;
1075 if (j >= wp->w_height - 2)
1076 break;
1077 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1078 }
1079 }
1080 else
1081#endif
1082 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1083 if (j < wp->w_height - 2) /* not too far off */
1084 {
1085 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1086#ifdef FEAT_DIFF
1087 /* insert extra lines for previously invisible filler lines */
1088 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1089 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1090 - wp->w_old_topfill;
1091#endif
1092 if (i < wp->w_height - 2) /* less than a screen off */
1093 {
1094 /*
1095 * Try to insert the correct number of lines.
1096 * If not the last window, delete the lines at the bottom.
1097 * win_ins_lines may fail when the terminal can't do it.
1098 */
1099 if (i > 0)
1100 check_for_delay(FALSE);
1101 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1102 {
1103 if (wp->w_lines_valid != 0)
1104 {
1105 /* Need to update rows that are new, stop at the
1106 * first one that scrolled down. */
1107 top_end = i;
1108#ifdef FEAT_VISUAL
1109 scrolled_down = TRUE;
1110#endif
1111
1112 /* Move the entries that were scrolled, disable
1113 * the entries for the lines to be redrawn. */
1114 if ((wp->w_lines_valid += j) > wp->w_height)
1115 wp->w_lines_valid = wp->w_height;
1116 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1117 wp->w_lines[idx] = wp->w_lines[idx - j];
1118 while (idx >= 0)
1119 wp->w_lines[idx--].wl_valid = FALSE;
1120 }
1121 }
1122 else
1123 mid_start = 0; /* redraw all lines */
1124 }
1125 else
1126 mid_start = 0; /* redraw all lines */
1127 }
1128 else
1129 mid_start = 0; /* redraw all lines */
1130 }
1131 else
1132 {
1133 /*
1134 * New topline is at or below old topline: May scroll up.
1135 * When topline didn't change, find first entry in w_lines[] that
1136 * needs updating.
1137 */
1138
1139 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1140 j = -1;
1141 row = 0;
1142 for (i = 0; i < wp->w_lines_valid; i++)
1143 {
1144 if (wp->w_lines[i].wl_valid
1145 && wp->w_lines[i].wl_lnum == wp->w_topline)
1146 {
1147 j = i;
1148 break;
1149 }
1150 row += wp->w_lines[i].wl_size;
1151 }
1152 if (j == -1)
1153 {
1154 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1155 * lines */
1156 mid_start = 0;
1157 }
1158 else
1159 {
1160 /*
1161 * Try to delete the correct number of lines.
1162 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1163 */
1164#ifdef FEAT_DIFF
1165 /* If the topline didn't change, delete old filler lines,
1166 * otherwise delete filler lines of the new topline... */
1167 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1168 row += wp->w_old_topfill;
1169 else
1170 row += diff_check_fill(wp, wp->w_topline);
1171 /* ... but don't delete new filler lines. */
1172 row -= wp->w_topfill;
1173#endif
1174 if (row > 0)
1175 {
1176 check_for_delay(FALSE);
1177 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1178 bot_start = wp->w_height - row;
1179 else
1180 mid_start = 0; /* redraw all lines */
1181 }
1182 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1183 {
1184 /*
1185 * Skip the lines (below the deleted lines) that are still
1186 * valid and don't need redrawing. Copy their info
1187 * upwards, to compensate for the deleted lines. Set
1188 * bot_start to the first row that needs redrawing.
1189 */
1190 bot_start = 0;
1191 idx = 0;
1192 for (;;)
1193 {
1194 wp->w_lines[idx] = wp->w_lines[j];
1195 /* stop at line that didn't fit, unless it is still
1196 * valid (no lines deleted) */
1197 if (row > 0 && bot_start + row
1198 + (int)wp->w_lines[j].wl_size > wp->w_height)
1199 {
1200 wp->w_lines_valid = idx + 1;
1201 break;
1202 }
1203 bot_start += wp->w_lines[idx++].wl_size;
1204
1205 /* stop at the last valid entry in w_lines[].wl_size */
1206 if (++j >= wp->w_lines_valid)
1207 {
1208 wp->w_lines_valid = idx;
1209 break;
1210 }
1211 }
1212#ifdef FEAT_DIFF
1213 /* Correct the first entry for filler lines at the top
1214 * when it won't get updated below. */
1215 if (wp->w_p_diff && bot_start > 0)
1216 wp->w_lines[0].wl_size =
1217 plines_win_nofill(wp, wp->w_topline, TRUE)
1218 + wp->w_topfill;
1219#endif
1220 }
1221 }
1222 }
1223
1224 /* When starting redraw in the first line, redraw all lines. When
1225 * there is only one window it's probably faster to clear the screen
1226 * first. */
1227 if (mid_start == 0)
1228 {
1229 mid_end = wp->w_height;
1230 if (lastwin == firstwin)
1231 screenclear();
1232 }
1233 }
1234 else
1235 {
1236 /* Not VALID or INVERTED: redraw all lines. */
1237 mid_start = 0;
1238 mid_end = wp->w_height;
1239 }
1240
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001241 if (type == SOME_VALID)
1242 {
1243 /* SOME_VALID: redraw all lines. */
1244 mid_start = 0;
1245 mid_end = wp->w_height;
1246 type = NOT_VALID;
1247 }
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249#ifdef FEAT_VISUAL
1250 /* check if we are updating or removing the inverted part */
1251 if ((VIsual_active && buf == curwin->w_buffer)
1252 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1253 {
1254 linenr_T from, to;
1255
1256 if (VIsual_active)
1257 {
1258 if (VIsual_active
1259 && (VIsual_mode != wp->w_old_visual_mode
1260 || type == INVERTED_ALL))
1261 {
1262 /*
1263 * If the type of Visual selection changed, redraw the whole
1264 * selection. Also when the ownership of the X selection is
1265 * gained or lost.
1266 */
1267 if (curwin->w_cursor.lnum < VIsual.lnum)
1268 {
1269 from = curwin->w_cursor.lnum;
1270 to = VIsual.lnum;
1271 }
1272 else
1273 {
1274 from = VIsual.lnum;
1275 to = curwin->w_cursor.lnum;
1276 }
1277 /* redraw more when the cursor moved as well */
1278 if (wp->w_old_cursor_lnum < from)
1279 from = wp->w_old_cursor_lnum;
1280 if (wp->w_old_cursor_lnum > to)
1281 to = wp->w_old_cursor_lnum;
1282 if (wp->w_old_visual_lnum < from)
1283 from = wp->w_old_visual_lnum;
1284 if (wp->w_old_visual_lnum > to)
1285 to = wp->w_old_visual_lnum;
1286 }
1287 else
1288 {
1289 /*
1290 * Find the line numbers that need to be updated: The lines
1291 * between the old cursor position and the current cursor
1292 * position. Also check if the Visual position changed.
1293 */
1294 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1295 {
1296 from = curwin->w_cursor.lnum;
1297 to = wp->w_old_cursor_lnum;
1298 }
1299 else
1300 {
1301 from = wp->w_old_cursor_lnum;
1302 to = curwin->w_cursor.lnum;
1303 if (from == 0) /* Visual mode just started */
1304 from = to;
1305 }
1306
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001307 if (VIsual.lnum != wp->w_old_visual_lnum
1308 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
1310 if (wp->w_old_visual_lnum < from
1311 && wp->w_old_visual_lnum != 0)
1312 from = wp->w_old_visual_lnum;
1313 if (wp->w_old_visual_lnum > to)
1314 to = wp->w_old_visual_lnum;
1315 if (VIsual.lnum < from)
1316 from = VIsual.lnum;
1317 if (VIsual.lnum > to)
1318 to = VIsual.lnum;
1319 }
1320 }
1321
1322 /*
1323 * If in block mode and changed column or curwin->w_curswant:
1324 * update all lines.
1325 * First compute the actual start and end column.
1326 */
1327 if (VIsual_mode == Ctrl_V)
1328 {
1329 colnr_T fromc, toc;
1330
1331 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1332 ++toc;
1333 if (curwin->w_curswant == MAXCOL)
1334 toc = MAXCOL;
1335
1336 if (fromc != wp->w_old_cursor_fcol
1337 || toc != wp->w_old_cursor_lcol)
1338 {
1339 if (from > VIsual.lnum)
1340 from = VIsual.lnum;
1341 if (to < VIsual.lnum)
1342 to = VIsual.lnum;
1343 }
1344 wp->w_old_cursor_fcol = fromc;
1345 wp->w_old_cursor_lcol = toc;
1346 }
1347 }
1348 else
1349 {
1350 /* Use the line numbers of the old Visual area. */
1351 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1352 {
1353 from = wp->w_old_cursor_lnum;
1354 to = wp->w_old_visual_lnum;
1355 }
1356 else
1357 {
1358 from = wp->w_old_visual_lnum;
1359 to = wp->w_old_cursor_lnum;
1360 }
1361 }
1362
1363 /*
1364 * There is no need to update lines above the top of the window.
1365 */
1366 if (from < wp->w_topline)
1367 from = wp->w_topline;
1368
1369 /*
1370 * If we know the value of w_botline, use it to restrict the update to
1371 * the lines that are visible in the window.
1372 */
1373 if (wp->w_valid & VALID_BOTLINE)
1374 {
1375 if (from >= wp->w_botline)
1376 from = wp->w_botline - 1;
1377 if (to >= wp->w_botline)
1378 to = wp->w_botline - 1;
1379 }
1380
1381 /*
1382 * Find the minimal part to be updated.
1383 * Watch out for scrolling that made entries in w_lines[] invalid.
1384 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1385 * top_end; need to redraw from top_end to the "to" line.
1386 * A middle mouse click with a Visual selection may change the text
1387 * above the Visual area and reset wl_valid, do count these for
1388 * mid_end (in srow).
1389 */
1390 if (mid_start > 0)
1391 {
1392 lnum = wp->w_topline;
1393 idx = 0;
1394 srow = 0;
1395 if (scrolled_down)
1396 mid_start = top_end;
1397 else
1398 mid_start = 0;
1399 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1400 {
1401 if (wp->w_lines[idx].wl_valid)
1402 mid_start += wp->w_lines[idx].wl_size;
1403 else if (!scrolled_down)
1404 srow += wp->w_lines[idx].wl_size;
1405 ++idx;
1406# ifdef FEAT_FOLDING
1407 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1408 lnum = wp->w_lines[idx].wl_lnum;
1409 else
1410# endif
1411 ++lnum;
1412 }
1413 srow += mid_start;
1414 mid_end = wp->w_height;
1415 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1416 {
1417 if (wp->w_lines[idx].wl_valid
1418 && wp->w_lines[idx].wl_lnum >= to + 1)
1419 {
1420 /* Only update until first row of this line */
1421 mid_end = srow;
1422 break;
1423 }
1424 srow += wp->w_lines[idx].wl_size;
1425 }
1426 }
1427 }
1428
1429 if (VIsual_active && buf == curwin->w_buffer)
1430 {
1431 wp->w_old_visual_mode = VIsual_mode;
1432 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1433 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001434 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 wp->w_old_curswant = curwin->w_curswant;
1436 }
1437 else
1438 {
1439 wp->w_old_visual_mode = 0;
1440 wp->w_old_cursor_lnum = 0;
1441 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001442 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
1444#endif /* FEAT_VISUAL */
1445
1446#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1447 /* reset got_int, otherwise regexp won't work */
1448 save_got_int = got_int;
1449 got_int = 0;
1450#endif
1451#ifdef FEAT_FOLDING
1452 win_foldinfo.fi_level = 0;
1453#endif
1454
1455 /*
1456 * Update all the window rows.
1457 */
1458 idx = 0; /* first entry in w_lines[].wl_size */
1459 row = 0;
1460 srow = 0;
1461 lnum = wp->w_topline; /* first line shown in window */
1462 for (;;)
1463 {
1464 /* stop updating when reached the end of the window (check for _past_
1465 * the end of the window is at the end of the loop) */
1466 if (row == wp->w_height)
1467 {
1468 didline = TRUE;
1469 break;
1470 }
1471
1472 /* stop updating when hit the end of the file */
1473 if (lnum > buf->b_ml.ml_line_count)
1474 {
1475 eof = TRUE;
1476 break;
1477 }
1478
1479 /* Remember the starting row of the line that is going to be dealt
1480 * with. It is used further down when the line doesn't fit. */
1481 srow = row;
1482
1483 /*
1484 * Update a line when it is in an area that needs updating, when it
1485 * has changes or w_lines[idx] is invalid.
1486 * bot_start may be halfway a wrapped line after using
1487 * win_del_lines(), check if the current line includes it.
1488 * When syntax folding is being used, the saved syntax states will
1489 * already have been updated, we can't see where the syntax state is
1490 * the same again, just update until the end of the window.
1491 */
1492 if (row < top_end
1493 || (row >= mid_start && row < mid_end)
1494#ifdef FEAT_SEARCH_EXTRA
1495 || top_to_mod
1496#endif
1497 || idx >= wp->w_lines_valid
1498 || (row + wp->w_lines[idx].wl_size > bot_start)
1499 || (mod_top != 0
1500 && (lnum == mod_top
1501 || (lnum >= mod_top
1502 && (lnum < mod_bot
1503#ifdef FEAT_SYN_HL
1504 || did_update == DID_FOLD
1505 || (did_update == DID_LINE
1506 && syntax_present(buf)
1507 && (
1508# ifdef FEAT_FOLDING
1509 (foldmethodIsSyntax(wp)
1510 && hasAnyFolding(wp)) ||
1511# endif
1512 syntax_check_changed(lnum)))
1513#endif
1514 )))))
1515 {
1516#ifdef FEAT_SEARCH_EXTRA
1517 if (lnum == mod_top)
1518 top_to_mod = FALSE;
1519#endif
1520
1521 /*
1522 * When at start of changed lines: May scroll following lines
1523 * up or down to minimize redrawing.
1524 * Don't do this when the change continues until the end.
1525 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1526 */
1527 if (lnum == mod_top
1528 && mod_bot != MAXLNUM
1529 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1530 {
1531 int old_rows = 0;
1532 int new_rows = 0;
1533 int xtra_rows;
1534 linenr_T l;
1535
1536 /* Count the old number of window rows, using w_lines[], which
1537 * should still contain the sizes for the lines as they are
1538 * currently displayed. */
1539 for (i = idx; i < wp->w_lines_valid; ++i)
1540 {
1541 /* Only valid lines have a meaningful wl_lnum. Invalid
1542 * lines are part of the changed area. */
1543 if (wp->w_lines[i].wl_valid
1544 && wp->w_lines[i].wl_lnum == mod_bot)
1545 break;
1546 old_rows += wp->w_lines[i].wl_size;
1547#ifdef FEAT_FOLDING
1548 if (wp->w_lines[i].wl_valid
1549 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1550 {
1551 /* Must have found the last valid entry above mod_bot.
1552 * Add following invalid entries. */
1553 ++i;
1554 while (i < wp->w_lines_valid
1555 && !wp->w_lines[i].wl_valid)
1556 old_rows += wp->w_lines[i++].wl_size;
1557 break;
1558 }
1559#endif
1560 }
1561
1562 if (i >= wp->w_lines_valid)
1563 {
1564 /* We can't find a valid line below the changed lines,
1565 * need to redraw until the end of the window.
1566 * Inserting/deleting lines has no use. */
1567 bot_start = 0;
1568 }
1569 else
1570 {
1571 /* Able to count old number of rows: Count new window
1572 * rows, and may insert/delete lines */
1573 j = idx;
1574 for (l = lnum; l < mod_bot; ++l)
1575 {
1576#ifdef FEAT_FOLDING
1577 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1578 ++new_rows;
1579 else
1580#endif
1581#ifdef FEAT_DIFF
1582 if (l == wp->w_topline)
1583 new_rows += plines_win_nofill(wp, l, TRUE)
1584 + wp->w_topfill;
1585 else
1586#endif
1587 new_rows += plines_win(wp, l, TRUE);
1588 ++j;
1589 if (new_rows > wp->w_height - row - 2)
1590 {
1591 /* it's getting too much, must redraw the rest */
1592 new_rows = 9999;
1593 break;
1594 }
1595 }
1596 xtra_rows = new_rows - old_rows;
1597 if (xtra_rows < 0)
1598 {
1599 /* May scroll text up. If there is not enough
1600 * remaining text or scrolling fails, must redraw the
1601 * rest. If scrolling works, must redraw the text
1602 * below the scrolled text. */
1603 if (row - xtra_rows >= wp->w_height - 2)
1604 mod_bot = MAXLNUM;
1605 else
1606 {
1607 check_for_delay(FALSE);
1608 if (win_del_lines(wp, row,
1609 -xtra_rows, FALSE, FALSE) == FAIL)
1610 mod_bot = MAXLNUM;
1611 else
1612 bot_start = wp->w_height + xtra_rows;
1613 }
1614 }
1615 else if (xtra_rows > 0)
1616 {
1617 /* May scroll text down. If there is not enough
1618 * remaining text of scrolling fails, must redraw the
1619 * rest. */
1620 if (row + xtra_rows >= wp->w_height - 2)
1621 mod_bot = MAXLNUM;
1622 else
1623 {
1624 check_for_delay(FALSE);
1625 if (win_ins_lines(wp, row + old_rows,
1626 xtra_rows, FALSE, FALSE) == FAIL)
1627 mod_bot = MAXLNUM;
1628 else if (top_end > row + old_rows)
1629 /* Scrolled the part at the top that requires
1630 * updating down. */
1631 top_end += xtra_rows;
1632 }
1633 }
1634
1635 /* When not updating the rest, may need to move w_lines[]
1636 * entries. */
1637 if (mod_bot != MAXLNUM && i != j)
1638 {
1639 if (j < i)
1640 {
1641 int x = row + new_rows;
1642
1643 /* move entries in w_lines[] upwards */
1644 for (;;)
1645 {
1646 /* stop at last valid entry in w_lines[] */
1647 if (i >= wp->w_lines_valid)
1648 {
1649 wp->w_lines_valid = j;
1650 break;
1651 }
1652 wp->w_lines[j] = wp->w_lines[i];
1653 /* stop at a line that won't fit */
1654 if (x + (int)wp->w_lines[j].wl_size
1655 > wp->w_height)
1656 {
1657 wp->w_lines_valid = j + 1;
1658 break;
1659 }
1660 x += wp->w_lines[j++].wl_size;
1661 ++i;
1662 }
1663 if (bot_start > x)
1664 bot_start = x;
1665 }
1666 else /* j > i */
1667 {
1668 /* move entries in w_lines[] downwards */
1669 j -= i;
1670 wp->w_lines_valid += j;
1671 if (wp->w_lines_valid > wp->w_height)
1672 wp->w_lines_valid = wp->w_height;
1673 for (i = wp->w_lines_valid; i - j >= idx; --i)
1674 wp->w_lines[i] = wp->w_lines[i - j];
1675
1676 /* The w_lines[] entries for inserted lines are
1677 * now invalid, but wl_size may be used above.
1678 * Reset to zero. */
1679 while (i >= idx)
1680 {
1681 wp->w_lines[i].wl_size = 0;
1682 wp->w_lines[i--].wl_valid = FALSE;
1683 }
1684 }
1685 }
1686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
1688
1689#ifdef FEAT_FOLDING
1690 /*
1691 * When lines are folded, display one line for all of them.
1692 * Otherwise, display normally (can be several display lines when
1693 * 'wrap' is on).
1694 */
1695 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1696 if (fold_count != 0)
1697 {
1698 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1699 ++row;
1700 --fold_count;
1701 wp->w_lines[idx].wl_folded = TRUE;
1702 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1703# ifdef FEAT_SYN_HL
1704 did_update = DID_FOLD;
1705# endif
1706 }
1707 else
1708#endif
1709 if (idx < wp->w_lines_valid
1710 && wp->w_lines[idx].wl_valid
1711 && wp->w_lines[idx].wl_lnum == lnum
1712 && lnum > wp->w_topline
1713 && !(dy_flags & DY_LASTLINE)
1714 && srow + wp->w_lines[idx].wl_size > wp->w_height
1715#ifdef FEAT_DIFF
1716 && diff_check_fill(wp, lnum) == 0
1717#endif
1718 )
1719 {
1720 /* This line is not going to fit. Don't draw anything here,
1721 * will draw "@ " lines below. */
1722 row = wp->w_height + 1;
1723 }
1724 else
1725 {
1726#ifdef FEAT_SEARCH_EXTRA
1727 prepare_search_hl(wp, lnum);
1728#endif
1729#ifdef FEAT_SYN_HL
1730 /* Let the syntax stuff know we skipped a few lines. */
1731 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1732 && syntax_present(buf))
1733 syntax_end_parsing(syntax_last_parsed + 1);
1734#endif
1735
1736 /*
1737 * Display one line.
1738 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001739 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
1741#ifdef FEAT_FOLDING
1742 wp->w_lines[idx].wl_folded = FALSE;
1743 wp->w_lines[idx].wl_lastlnum = lnum;
1744#endif
1745#ifdef FEAT_SYN_HL
1746 did_update = DID_LINE;
1747 syntax_last_parsed = lnum;
1748#endif
1749 }
1750
1751 wp->w_lines[idx].wl_lnum = lnum;
1752 wp->w_lines[idx].wl_valid = TRUE;
1753 if (row > wp->w_height) /* past end of screen */
1754 {
1755 /* we may need the size of that too long line later on */
1756 if (dollar_vcol == 0)
1757 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1758 ++idx;
1759 break;
1760 }
1761 if (dollar_vcol == 0)
1762 wp->w_lines[idx].wl_size = row - srow;
1763 ++idx;
1764#ifdef FEAT_FOLDING
1765 lnum += fold_count + 1;
1766#else
1767 ++lnum;
1768#endif
1769 }
1770 else
1771 {
1772 /* This line does not need updating, advance to the next one */
1773 row += wp->w_lines[idx++].wl_size;
1774 if (row > wp->w_height) /* past end of screen */
1775 break;
1776#ifdef FEAT_FOLDING
1777 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1778#else
1779 ++lnum;
1780#endif
1781#ifdef FEAT_SYN_HL
1782 did_update = DID_NONE;
1783#endif
1784 }
1785
1786 if (lnum > buf->b_ml.ml_line_count)
1787 {
1788 eof = TRUE;
1789 break;
1790 }
1791 }
1792 /*
1793 * End of loop over all window lines.
1794 */
1795
1796
1797 if (idx > wp->w_lines_valid)
1798 wp->w_lines_valid = idx;
1799
1800#ifdef FEAT_SYN_HL
1801 /*
1802 * Let the syntax stuff know we stop parsing here.
1803 */
1804 if (syntax_last_parsed != 0 && syntax_present(buf))
1805 syntax_end_parsing(syntax_last_parsed + 1);
1806#endif
1807
1808 /*
1809 * If we didn't hit the end of the file, and we didn't finish the last
1810 * line we were working on, then the line didn't fit.
1811 */
1812 wp->w_empty_rows = 0;
1813#ifdef FEAT_DIFF
1814 wp->w_filler_rows = 0;
1815#endif
1816 if (!eof && !didline)
1817 {
1818 if (lnum == wp->w_topline)
1819 {
1820 /*
1821 * Single line that does not fit!
1822 * Don't overwrite it, it can be edited.
1823 */
1824 wp->w_botline = lnum + 1;
1825 }
1826#ifdef FEAT_DIFF
1827 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1828 {
1829 /* Window ends in filler lines. */
1830 wp->w_botline = lnum;
1831 wp->w_filler_rows = wp->w_height - srow;
1832 }
1833#endif
1834 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1835 {
1836 /*
1837 * Last line isn't finished: Display "@@@" at the end.
1838 */
1839 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1840 W_WINROW(wp) + wp->w_height,
1841 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1842 '@', '@', hl_attr(HLF_AT));
1843 set_empty_rows(wp, srow);
1844 wp->w_botline = lnum;
1845 }
1846 else
1847 {
1848 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1849 wp->w_botline = lnum;
1850 }
1851 }
1852 else
1853 {
1854#ifdef FEAT_VERTSPLIT
1855 draw_vsep_win(wp, row);
1856#endif
1857 if (eof) /* we hit the end of the file */
1858 {
1859 wp->w_botline = buf->b_ml.ml_line_count + 1;
1860#ifdef FEAT_DIFF
1861 j = diff_check_fill(wp, wp->w_botline);
1862 if (j > 0 && !wp->w_botfill)
1863 {
1864 /*
1865 * Display filler lines at the end of the file
1866 */
1867 if (char2cells(fill_diff) > 1)
1868 i = '-';
1869 else
1870 i = fill_diff;
1871 if (row + j > wp->w_height)
1872 j = wp->w_height - row;
1873 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1874 row += j;
1875 }
1876#endif
1877 }
1878 else if (dollar_vcol == 0)
1879 wp->w_botline = lnum;
1880
1881 /* make sure the rest of the screen is blank */
1882 /* put '~'s on rows that aren't part of the file. */
1883 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1884 }
1885
1886 /* Reset the type of redrawing required, the window has been updated. */
1887 wp->w_redr_type = 0;
1888#ifdef FEAT_DIFF
1889 wp->w_old_topfill = wp->w_topfill;
1890 wp->w_old_botfill = wp->w_botfill;
1891#endif
1892
1893 if (dollar_vcol == 0)
1894 {
1895 /*
1896 * There is a trick with w_botline. If we invalidate it on each
1897 * change that might modify it, this will cause a lot of expensive
1898 * calls to plines() in update_topline() each time. Therefore the
1899 * value of w_botline is often approximated, and this value is used to
1900 * compute the value of w_topline. If the value of w_botline was
1901 * wrong, check that the value of w_topline is correct (cursor is on
1902 * the visible part of the text). If it's not, we need to redraw
1903 * again. Mostly this just means scrolling up a few lines, so it
1904 * doesn't look too bad. Only do this for the current window (where
1905 * changes are relevant).
1906 */
1907 wp->w_valid |= VALID_BOTLINE;
1908 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1909 {
1910 recursive = TRUE;
1911 curwin->w_valid &= ~VALID_TOPLINE;
1912 update_topline(); /* may invalidate w_botline again */
1913 if (must_redraw != 0)
1914 {
1915 /* Don't update for changes in buffer again. */
1916 i = curbuf->b_mod_set;
1917 curbuf->b_mod_set = FALSE;
1918 win_update(curwin);
1919 must_redraw = 0;
1920 curbuf->b_mod_set = i;
1921 }
1922 recursive = FALSE;
1923 }
1924 }
1925
1926#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1927 /* restore got_int, unless CTRL-C was hit while redrawing */
1928 if (!got_int)
1929 got_int = save_got_int;
1930#endif
1931}
1932
1933#ifdef FEAT_SIGNS
1934static int draw_signcolumn __ARGS((win_T *wp));
1935
1936/*
1937 * Return TRUE when window "wp" has a column to draw signs in.
1938 */
1939 static int
1940draw_signcolumn(wp)
1941 win_T *wp;
1942{
1943 return (wp->w_buffer->b_signlist != NULL
1944# ifdef FEAT_NETBEANS_INTG
1945 || usingNetbeans
1946# endif
1947 );
1948}
1949#endif
1950
1951/*
1952 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1953 * as the filler character.
1954 */
1955 static void
1956win_draw_end(wp, c1, c2, row, endrow, hl)
1957 win_T *wp;
1958 int c1;
1959 int c2;
1960 int row;
1961 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001962 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
1964#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1965 int n = 0;
1966# define FDC_OFF n
1967#else
1968# define FDC_OFF 0
1969#endif
1970
1971#ifdef FEAT_RIGHTLEFT
1972 if (wp->w_p_rl)
1973 {
1974 /* No check for cmdline window: should never be right-left. */
1975# ifdef FEAT_FOLDING
1976 n = wp->w_p_fdc;
1977
1978 if (n > 0)
1979 {
1980 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001981 if (n > W_WIDTH(wp))
1982 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1984 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
1985 ' ', ' ', hl_attr(HLF_FC));
1986 }
1987# endif
1988# ifdef FEAT_SIGNS
1989 if (draw_signcolumn(wp))
1990 {
1991 int nn = n + 2;
1992
1993 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001994 if (nn > W_WIDTH(wp))
1995 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1997 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
1998 ' ', ' ', hl_attr(HLF_SC));
1999 n = nn;
2000 }
2001# endif
2002 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2003 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2004 c2, c2, hl_attr(hl));
2005 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2006 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2007 c1, c2, hl_attr(hl));
2008 }
2009 else
2010#endif
2011 {
2012#ifdef FEAT_CMDWIN
2013 if (cmdwin_type != 0 && wp == curwin)
2014 {
2015 /* draw the cmdline character in the leftmost column */
2016 n = 1;
2017 if (n > wp->w_width)
2018 n = wp->w_width;
2019 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2020 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2021 cmdwin_type, ' ', hl_attr(HLF_AT));
2022 }
2023#endif
2024#ifdef FEAT_FOLDING
2025 if (wp->w_p_fdc > 0)
2026 {
2027 int nn = n + wp->w_p_fdc;
2028
2029 /* draw the fold column at the left */
2030 if (nn > W_WIDTH(wp))
2031 nn = W_WIDTH(wp);
2032 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2033 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2034 ' ', ' ', hl_attr(HLF_FC));
2035 n = nn;
2036 }
2037#endif
2038#ifdef FEAT_SIGNS
2039 if (draw_signcolumn(wp))
2040 {
2041 int nn = n + 2;
2042
2043 /* draw the sign column after the fold column */
2044 if (nn > W_WIDTH(wp))
2045 nn = W_WIDTH(wp);
2046 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2047 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2048 ' ', ' ', hl_attr(HLF_SC));
2049 n = nn;
2050 }
2051#endif
2052 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2053 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2054 c1, c2, hl_attr(hl));
2055 }
2056 set_empty_rows(wp, row);
2057}
2058
2059#ifdef FEAT_FOLDING
2060/*
2061 * Display one folded line.
2062 */
2063 static void
2064fold_line(wp, fold_count, foldinfo, lnum, row)
2065 win_T *wp;
2066 long fold_count;
2067 foldinfo_T *foldinfo;
2068 linenr_T lnum;
2069 int row;
2070{
2071 char_u buf[51];
2072 pos_T *top, *bot;
2073 linenr_T lnume = lnum + fold_count - 1;
2074 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002075 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 int col;
2078 int txtcol;
2079 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002080 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 /* Build the fold line:
2083 * 1. Add the cmdwin_type for the command-line window
2084 * 2. Add the 'foldcolumn'
2085 * 3. Add the 'number' column
2086 * 4. Compose the text
2087 * 5. Add the text
2088 * 6. set highlighting for the Visual area an other text
2089 */
2090 col = 0;
2091
2092 /*
2093 * 1. Add the cmdwin_type for the command-line window
2094 * Ignores 'rightleft', this window is never right-left.
2095 */
2096#ifdef FEAT_CMDWIN
2097 if (cmdwin_type != 0 && wp == curwin)
2098 {
2099 ScreenLines[off] = cmdwin_type;
2100 ScreenAttrs[off] = hl_attr(HLF_AT);
2101#ifdef FEAT_MBYTE
2102 if (enc_utf8)
2103 ScreenLinesUC[off] = 0;
2104#endif
2105 ++col;
2106 }
2107#endif
2108
2109 /*
2110 * 2. Add the 'foldcolumn'
2111 */
2112 fdc = wp->w_p_fdc;
2113 if (fdc > W_WIDTH(wp) - col)
2114 fdc = W_WIDTH(wp) - col;
2115 if (fdc > 0)
2116 {
2117 fill_foldcolumn(buf, wp, TRUE, lnum);
2118#ifdef FEAT_RIGHTLEFT
2119 if (wp->w_p_rl)
2120 {
2121 int i;
2122
2123 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2124 hl_attr(HLF_FC));
2125 /* reverse the fold column */
2126 for (i = 0; i < fdc; ++i)
2127 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2128 }
2129 else
2130#endif
2131 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2132 col += fdc;
2133 }
2134
2135#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002136# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2137 for (ri = 0; ri < l; ++ri) \
2138 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2139 else \
2140 for (ri = 0; ri < l; ++ri) \
2141 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002143# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2144 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145#endif
2146
2147 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002148 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150#ifdef FEAT_SIGNS
2151 /* If signs are being displayed, add two spaces. */
2152 if (draw_signcolumn(wp))
2153 {
2154 len = W_WIDTH(wp) - col;
2155 if (len > 0)
2156 {
2157 if (len > 2)
2158 len = 2;
2159# ifdef FEAT_RIGHTLEFT
2160 if (wp->w_p_rl)
2161 /* the line number isn't reversed */
2162 copy_text_attr(off + W_WIDTH(wp) - len - col,
2163 (char_u *)" ", len, hl_attr(HLF_FL));
2164 else
2165# endif
2166 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2167 col += len;
2168 }
2169 }
2170#endif
2171
2172 /*
2173 * 3. Add the 'number' column
2174 */
2175 if (wp->w_p_nu)
2176 {
2177 len = W_WIDTH(wp) - col;
2178 if (len > 0)
2179 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002180 int w = number_width(wp);
2181
2182 if (len > w + 1)
2183 len = w + 1;
2184 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185#ifdef FEAT_RIGHTLEFT
2186 if (wp->w_p_rl)
2187 /* the line number isn't reversed */
2188 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2189 hl_attr(HLF_FL));
2190 else
2191#endif
2192 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2193 col += len;
2194 }
2195 }
2196
2197 /*
2198 * 4. Compose the folded-line string with 'foldtext', if set.
2199 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002200 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
2202 txtcol = col; /* remember where text starts */
2203
2204 /*
2205 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2206 * Right-left text is put in columns 0 - number-col, normal text is put
2207 * in columns number-col - window-width.
2208 */
2209#ifdef FEAT_MBYTE
2210 if (has_mbyte)
2211 {
2212 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002213 int u8c, u8cc[MAX_MCO];
2214 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 int idx;
2216 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002217 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218# ifdef FEAT_ARABIC
2219 int prev_c = 0; /* previous Arabic character */
2220 int prev_c1 = 0; /* first composing char for prev_c */
2221# endif
2222
2223# ifdef FEAT_RIGHTLEFT
2224 if (wp->w_p_rl)
2225 idx = off;
2226 else
2227# endif
2228 idx = off + col;
2229
2230 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2231 for (p = text; *p != NUL; )
2232 {
2233 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002234 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if (col + cells > W_WIDTH(wp)
2236# ifdef FEAT_RIGHTLEFT
2237 - (wp->w_p_rl ? col : 0)
2238# endif
2239 )
2240 break;
2241 ScreenLines[idx] = *p;
2242 if (enc_utf8)
2243 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002244 u8c = utfc_ptr2char(p, u8cc);
2245 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
2247 ScreenLinesUC[idx] = 0;
2248#ifdef FEAT_ARABIC
2249 prev_c = u8c;
2250#endif
2251 }
2252 else
2253 {
2254#ifdef FEAT_ARABIC
2255 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2256 {
2257 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002258 int pc, pc1, nc;
2259 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 int firstbyte = *p;
2261
2262 /* The idea of what is the previous and next
2263 * character depends on 'rightleft'. */
2264 if (wp->w_p_rl)
2265 {
2266 pc = prev_c;
2267 pc1 = prev_c1;
2268 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002269 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 else
2272 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002273 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002275 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277 prev_c = u8c;
2278
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002279 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 pc, pc1, nc);
2281 ScreenLines[idx] = firstbyte;
2282 }
2283 else
2284 prev_c = u8c;
2285#endif
2286 /* Non-BMP character: display as ? or fullwidth ?. */
2287 if (u8c >= 0x10000)
2288 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2289 else
2290 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002291 for (i = 0; i < Screen_mco; ++i)
2292 {
2293 ScreenLinesC[i][idx] = u8cc[i];
2294 if (u8cc[i] == 0)
2295 break;
2296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298 if (cells > 1)
2299 ScreenLines[idx + 1] = 0;
2300 }
2301 else if (cells > 1) /* double-byte character */
2302 {
2303 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2304 ScreenLines2[idx] = p[1];
2305 else
2306 ScreenLines[idx + 1] = p[1];
2307 }
2308 col += cells;
2309 idx += cells;
2310 p += c_len;
2311 }
2312 }
2313 else
2314#endif
2315 {
2316 len = (int)STRLEN(text);
2317 if (len > W_WIDTH(wp) - col)
2318 len = W_WIDTH(wp) - col;
2319 if (len > 0)
2320 {
2321#ifdef FEAT_RIGHTLEFT
2322 if (wp->w_p_rl)
2323 STRNCPY(current_ScreenLine, text, len);
2324 else
2325#endif
2326 STRNCPY(current_ScreenLine + col, text, len);
2327 col += len;
2328 }
2329 }
2330
2331 /* Fill the rest of the line with the fold filler */
2332#ifdef FEAT_RIGHTLEFT
2333 if (wp->w_p_rl)
2334 col -= txtcol;
2335#endif
2336 while (col < W_WIDTH(wp)
2337#ifdef FEAT_RIGHTLEFT
2338 - (wp->w_p_rl ? txtcol : 0)
2339#endif
2340 )
2341 {
2342#ifdef FEAT_MBYTE
2343 if (enc_utf8)
2344 {
2345 if (fill_fold >= 0x80)
2346 {
2347 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002348 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350 else
2351 ScreenLinesUC[off + col] = 0;
2352 }
2353#endif
2354 ScreenLines[off + col++] = fill_fold;
2355 }
2356
2357 if (text != buf)
2358 vim_free(text);
2359
2360 /*
2361 * 6. set highlighting for the Visual area an other text.
2362 * If all folded lines are in the Visual area, highlight the line.
2363 */
2364#ifdef FEAT_VISUAL
2365 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2366 {
2367 if (ltoreq(curwin->w_cursor, VIsual))
2368 {
2369 /* Visual is after curwin->w_cursor */
2370 top = &curwin->w_cursor;
2371 bot = &VIsual;
2372 }
2373 else
2374 {
2375 /* Visual is before curwin->w_cursor */
2376 top = &VIsual;
2377 bot = &curwin->w_cursor;
2378 }
2379 if (lnum >= top->lnum
2380 && lnume <= bot->lnum
2381 && (VIsual_mode != 'v'
2382 || ((lnum > top->lnum
2383 || (lnum == top->lnum
2384 && top->col == 0))
2385 && (lnume < bot->lnum
2386 || (lnume == bot->lnum
2387 && (bot->col - (*p_sel == 'e'))
2388 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2389 {
2390 if (VIsual_mode == Ctrl_V)
2391 {
2392 /* Visual block mode: highlight the chars part of the block */
2393 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2394 {
2395 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2396 len = wp->w_old_cursor_lcol;
2397 else
2398 len = W_WIDTH(wp) - txtcol;
2399 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002400 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402 }
2403 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002406 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 }
2410#endif
2411
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002412#ifdef FEAT_SYN_HL
2413 /* Show 'cursorcolumn' in the fold line. */
2414 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2415 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2416 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
2419 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2420 (int)W_WIDTH(wp), FALSE);
2421
2422 /*
2423 * Update w_cline_height and w_cline_folded if the cursor line was
2424 * updated (saves a call to plines() later).
2425 */
2426 if (wp == curwin
2427 && lnum <= curwin->w_cursor.lnum
2428 && lnume >= curwin->w_cursor.lnum)
2429 {
2430 curwin->w_cline_row = row;
2431 curwin->w_cline_height = 1;
2432 curwin->w_cline_folded = TRUE;
2433 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2434 }
2435}
2436
2437/*
2438 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2439 */
2440 static void
2441copy_text_attr(off, buf, len, attr)
2442 int off;
2443 char_u *buf;
2444 int len;
2445 int attr;
2446{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002447 int i;
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 mch_memmove(ScreenLines + off, buf, (size_t)len);
2450# ifdef FEAT_MBYTE
2451 if (enc_utf8)
2452 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2453# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002454 for (i = 0; i < len; ++i)
2455 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456}
2457
2458/*
2459 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002460 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 */
2462 static void
2463fill_foldcolumn(p, wp, closed, lnum)
2464 char_u *p;
2465 win_T *wp;
2466 int closed; /* TRUE of FALSE */
2467 linenr_T lnum; /* current line number */
2468{
2469 int i = 0;
2470 int level;
2471 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002472 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 /* Init to all spaces. */
2475 copy_spaces(p, (size_t)wp->w_p_fdc);
2476
2477 level = win_foldinfo.fi_level;
2478 if (level > 0)
2479 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002480 /* If there is only one column put more info in it. */
2481 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 /* If the column is too narrow, we start at the lowest level that
2484 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002485 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (first_level < 1)
2487 first_level = 1;
2488
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002489 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
2491 if (win_foldinfo.fi_lnum == lnum
2492 && first_level + i >= win_foldinfo.fi_low_level)
2493 p[i] = '-';
2494 else if (first_level == 1)
2495 p[i] = '|';
2496 else if (first_level + i <= 9)
2497 p[i] = '0' + first_level + i;
2498 else
2499 p[i] = '>';
2500 if (first_level + i == level)
2501 break;
2502 }
2503 }
2504 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002505 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506}
2507#endif /* FEAT_FOLDING */
2508
2509/*
2510 * Display line "lnum" of window 'wp' on the screen.
2511 * Start at row "startrow", stop when "endrow" is reached.
2512 * wp->w_virtcol needs to be valid.
2513 *
2514 * Return the number of last row the line occupies.
2515 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002516/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002518win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 win_T *wp;
2520 linenr_T lnum;
2521 int startrow;
2522 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002523 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 int col; /* visual column on screen */
2526 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2527 int c = 0; /* init for GCC */
2528 long vcol = 0; /* virtual column (for tabs) */
2529 long vcol_prev = -1; /* "vcol" of previous character */
2530 char_u *line; /* current line */
2531 char_u *ptr; /* current position in "line" */
2532 int row; /* row in the window, excl w_winrow */
2533 int screen_row; /* row on the screen, incl w_winrow */
2534
2535 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2536 int n_extra = 0; /* number of extra chars */
2537 char_u *p_extra = NULL; /* string of extra chars */
2538 int c_extra = NUL; /* extra chars, all the same */
2539 int extra_attr = 0; /* attributes when n_extra != 0 */
2540 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2541 displaying lcs_eol at end-of-line */
2542 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2543 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2544
2545 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2546 int saved_n_extra = 0;
2547 char_u *saved_p_extra = NULL;
2548 int saved_c_extra = 0;
2549 int saved_char_attr = 0;
2550
2551 int n_attr = 0; /* chars with special attr */
2552 int saved_attr2 = 0; /* char_attr saved for n_attr */
2553 int n_attr3 = 0; /* chars with overruling special attr */
2554 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2555
2556 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2557
2558 int fromcol, tocol; /* start/end of inverting */
2559 int fromcol_prev = -2; /* start of inverting after cursor */
2560 int noinvcur = FALSE; /* don't invert the cursor */
2561#ifdef FEAT_VISUAL
2562 pos_T *top, *bot;
2563#endif
2564 pos_T pos;
2565 long v;
2566
2567 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002568 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2570 in this line */
2571 int attr = 0; /* attributes for area highlighting */
2572 int area_attr = 0; /* attributes desired by highlighting */
2573 int search_attr = 0; /* attributes desired by 'hlsearch' */
2574#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002575 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 int syntax_attr = 0; /* attributes desired by syntax */
2577 int has_syntax = FALSE; /* this buffer has syntax highl. */
2578 int save_did_emsg;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002579#endif
2580#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002581 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002582# define SPWORDLEN 150
2583 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002584 int nextlinecol = 0; /* column where nextline[] starts */
2585 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002586 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002587 int spell_attr = 0; /* attributes desired by spelling */
2588 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002589 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2590 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002591 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002592 static int cap_col = -1; /* column to check for Cap word */
2593 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002594 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595#endif
2596 int extra_check; /* has syntax or linebreak */
2597#ifdef FEAT_MBYTE
2598 int multi_attr = 0; /* attributes desired by multibyte */
2599 int mb_l = 1; /* multi-byte byte length */
2600 int mb_c = 0; /* decoded multi-byte character */
2601 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002602 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603#endif
2604#ifdef FEAT_DIFF
2605 int filler_lines; /* nr of filler lines to be drawn */
2606 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002607 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 int change_start = MAXCOL; /* first col of changed area */
2609 int change_end = -1; /* last col of changed area */
2610#endif
2611 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2612#ifdef FEAT_LINEBREAK
2613 int need_showbreak = FALSE;
2614#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002615#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2616 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617# define LINE_ATTR
2618 int line_attr = 0; /* atrribute for the whole line */
2619#endif
2620#ifdef FEAT_SEARCH_EXTRA
2621 match_T *shl; /* points to search_hl or match_hl */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002622#endif
2623#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_MBYTE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002624 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625#endif
2626#ifdef FEAT_ARABIC
2627 int prev_c = 0; /* previous Arabic character */
2628 int prev_c1 = 0; /* first composing char for prev_c */
2629#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002630#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002631 int did_line_attr = 0;
2632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634 /* draw_state: items that are drawn in sequence: */
2635#define WL_START 0 /* nothing done yet */
2636#ifdef FEAT_CMDWIN
2637# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2638#else
2639# define WL_CMDLINE WL_START
2640#endif
2641#ifdef FEAT_FOLDING
2642# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2643#else
2644# define WL_FOLD WL_CMDLINE
2645#endif
2646#ifdef FEAT_SIGNS
2647# define WL_SIGN WL_FOLD + 1 /* column for signs */
2648#else
2649# define WL_SIGN WL_FOLD /* column for signs */
2650#endif
2651#define WL_NR WL_SIGN + 1 /* line number */
2652#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2653# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2654#else
2655# define WL_SBR WL_NR
2656#endif
2657#define WL_LINE WL_SBR + 1 /* text in the line */
2658 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002659#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 int feedback_col = 0;
2661 int feedback_old_attr = -1;
2662#endif
2663
2664
2665 if (startrow > endrow) /* past the end already! */
2666 return startrow;
2667
2668 row = startrow;
2669 screen_row = row + W_WINROW(wp);
2670
2671 /*
2672 * To speed up the loop below, set extra_check when there is linebreak,
2673 * trailing white space and/or syntax processing to be done.
2674 */
2675#ifdef FEAT_LINEBREAK
2676 extra_check = wp->w_p_lbr;
2677#else
2678 extra_check = 0;
2679#endif
2680#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002681 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
2683 /* Prepare for syntax highlighting in this line. When there is an
2684 * error, stop syntax highlighting. */
2685 save_did_emsg = did_emsg;
2686 did_emsg = FALSE;
2687 syntax_start(wp, lnum);
2688 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002689 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 else
2691 {
2692 did_emsg = save_did_emsg;
2693 has_syntax = TRUE;
2694 extra_check = TRUE;
2695 }
2696 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002697#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002698
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002699#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002700 if (wp->w_p_spell
2701 && *wp->w_buffer->b_p_spl != NUL
2702 && wp->w_buffer->b_langp.ga_len > 0
2703 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002704 {
2705 /* Prepare for spell checking. */
2706 has_spell = TRUE;
2707 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002708
2709 /* Get the start of the next line, so that words that wrap to the next
2710 * line are found too: "et<line-break>al.".
2711 * Trick: skip a few chars for C/shell/Vim comments */
2712 nextline[SPWORDLEN] = NUL;
2713 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2714 {
2715 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2716 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2717 }
2718
2719 /* When a word wrapped from the previous line the start of the current
2720 * line is valid. */
2721 if (lnum == checked_lnum)
2722 cur_checked_col = checked_col;
2723 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002724
2725 /* When there was a sentence end in the previous line may require a
2726 * word starting with capital in this line. In line 1 always check
2727 * the first word. */
2728 if (lnum != capcol_lnum)
2729 cap_col = -1;
2730 if (lnum == 1)
2731 cap_col = 0;
2732 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#endif
2735
2736 /*
2737 * handle visual active in this window
2738 */
2739 fromcol = -10;
2740 tocol = MAXCOL;
2741#ifdef FEAT_VISUAL
2742 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2743 {
2744 /* Visual is after curwin->w_cursor */
2745 if (ltoreq(curwin->w_cursor, VIsual))
2746 {
2747 top = &curwin->w_cursor;
2748 bot = &VIsual;
2749 }
2750 else /* Visual is before curwin->w_cursor */
2751 {
2752 top = &VIsual;
2753 bot = &curwin->w_cursor;
2754 }
2755 if (VIsual_mode == Ctrl_V) /* block mode */
2756 {
2757 if (lnum >= top->lnum && lnum <= bot->lnum)
2758 {
2759 fromcol = wp->w_old_cursor_fcol;
2760 tocol = wp->w_old_cursor_lcol;
2761 }
2762 }
2763 else /* non-block mode */
2764 {
2765 if (lnum > top->lnum && lnum <= bot->lnum)
2766 fromcol = 0;
2767 else if (lnum == top->lnum)
2768 {
2769 if (VIsual_mode == 'V') /* linewise */
2770 fromcol = 0;
2771 else
2772 {
2773 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2774 if (gchar_pos(top) == NUL)
2775 tocol = fromcol + 1;
2776 }
2777 }
2778 if (VIsual_mode != 'V' && lnum == bot->lnum)
2779 {
2780 if (*p_sel == 'e' && bot->col == 0
2781#ifdef FEAT_VIRTUALEDIT
2782 && bot->coladd == 0
2783#endif
2784 )
2785 {
2786 fromcol = -10;
2787 tocol = MAXCOL;
2788 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002789 else if (bot->col == MAXCOL)
2790 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else
2792 {
2793 pos = *bot;
2794 if (*p_sel == 'e')
2795 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2796 else
2797 {
2798 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2799 ++tocol;
2800 }
2801 }
2802 }
2803 }
2804
2805#ifndef MSDOS
2806 /* Check if the character under the cursor should not be inverted */
2807 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2808# ifdef FEAT_GUI
2809 && !gui.in_use
2810# endif
2811 )
2812 noinvcur = TRUE;
2813#endif
2814
2815 /* if inverting in this line set area_highlighting */
2816 if (fromcol >= 0)
2817 {
2818 area_highlighting = TRUE;
2819 attr = hl_attr(HLF_V);
2820#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2821 if (clip_star.available && !clip_star.owned && clip_isautosel())
2822 attr = hl_attr(HLF_VNC);
2823#endif
2824 }
2825 }
2826
2827 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002828 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 */
2830 else
2831#endif /* FEAT_VISUAL */
2832 if (highlight_match
2833 && wp == curwin
2834 && lnum >= curwin->w_cursor.lnum
2835 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2836 {
2837 if (lnum == curwin->w_cursor.lnum)
2838 getvcol(curwin, &(curwin->w_cursor),
2839 (colnr_T *)&fromcol, NULL, NULL);
2840 else
2841 fromcol = 0;
2842 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2843 {
2844 pos.lnum = lnum;
2845 pos.col = search_match_endcol;
2846 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2847 }
2848 else
2849 tocol = MAXCOL;
2850 if (fromcol == tocol) /* do at least one character */
2851 tocol = fromcol + 1; /* happens when past end of line */
2852 area_highlighting = TRUE;
2853 attr = hl_attr(HLF_I);
2854 }
2855
2856#ifdef FEAT_DIFF
2857 filler_lines = diff_check(wp, lnum);
2858 if (filler_lines < 0)
2859 {
2860 if (filler_lines == -1)
2861 {
2862 if (diff_find_change(wp, lnum, &change_start, &change_end))
2863 diff_hlf = HLF_ADD; /* added line */
2864 else if (change_start == 0)
2865 diff_hlf = HLF_TXD; /* changed text */
2866 else
2867 diff_hlf = HLF_CHD; /* changed line */
2868 }
2869 else
2870 diff_hlf = HLF_ADD; /* added line */
2871 filler_lines = 0;
2872 area_highlighting = TRUE;
2873 }
2874 if (lnum == wp->w_topline)
2875 filler_lines = wp->w_topfill;
2876 filler_todo = filler_lines;
2877#endif
2878
2879#ifdef LINE_ATTR
2880# ifdef FEAT_SIGNS
2881 /* If this line has a sign with line highlighting set line_attr. */
2882 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2883 if (v != 0)
2884 line_attr = sign_get_attr((int)v, TRUE);
2885# endif
2886# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2887 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002888 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 line_attr = hl_attr(HLF_L);
2890# endif
2891 if (line_attr != 0)
2892 area_highlighting = TRUE;
2893#endif
2894
2895 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2896 ptr = line;
2897
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002898#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002899 if (has_spell)
2900 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002901 /* For checking first word with a capital skip white space. */
2902 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002903 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002904
Bram Moolenaar30abd282005-06-22 22:35:10 +00002905 /* To be able to spell-check over line boundaries copy the end of the
2906 * current line into nextline[]. Above the start of the next line was
2907 * copied to nextline[SPWORDLEN]. */
2908 if (nextline[SPWORDLEN] == NUL)
2909 {
2910 /* No next line or it is empty. */
2911 nextlinecol = MAXCOL;
2912 nextline_idx = 0;
2913 }
2914 else
2915 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002916 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002917 if (v < SPWORDLEN)
2918 {
2919 /* Short line, use it completely and append the start of the
2920 * next line. */
2921 nextlinecol = 0;
2922 mch_memmove(nextline, line, (size_t)v);
2923 mch_memmove(nextline + v, nextline + SPWORDLEN,
2924 STRLEN(nextline + SPWORDLEN) + 1);
2925 nextline_idx = v + 1;
2926 }
2927 else
2928 {
2929 /* Long line, use only the last SPWORDLEN bytes. */
2930 nextlinecol = v - SPWORDLEN;
2931 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2932 nextline_idx = SPWORDLEN + 1;
2933 }
2934 }
2935 }
2936#endif
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 /* find start of trailing whitespace */
2939 if (wp->w_p_list && lcs_trail)
2940 {
2941 trailcol = (colnr_T)STRLEN(ptr);
2942 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2943 --trailcol;
2944 trailcol += (colnr_T) (ptr - line);
2945 extra_check = TRUE;
2946 }
2947
2948 /*
2949 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2950 * first character to be displayed.
2951 */
2952 if (wp->w_p_wrap)
2953 v = wp->w_skipcol;
2954 else
2955 v = wp->w_leftcol;
2956 if (v > 0)
2957 {
2958#ifdef FEAT_MBYTE
2959 char_u *prev_ptr = ptr;
2960#endif
2961 while (vcol < v && *ptr != NUL)
2962 {
2963 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2964 vcol += c;
2965#ifdef FEAT_MBYTE
2966 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002968 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
2970
2971#ifdef FEAT_VIRTUALEDIT
2972 /* When 'virtualedit' is set the end of the line may be before the
2973 * start of the displayed part. */
2974 if (vcol < v && *ptr == NUL && virtual_active())
2975 vcol = v;
2976#endif
2977
2978 /* Handle a character that's not completely on the screen: Put ptr at
2979 * that character but skip the first few screen characters. */
2980 if (vcol > v)
2981 {
2982 vcol -= c;
2983#ifdef FEAT_MBYTE
2984 ptr = prev_ptr;
2985#else
2986 --ptr;
2987#endif
2988 n_skip = v - vcol;
2989 }
2990
2991 /*
2992 * Adjust for when the inverted text is before the screen,
2993 * and when the start of the inverted text is before the screen.
2994 */
2995 if (tocol <= vcol)
2996 fromcol = 0;
2997 else if (fromcol >= 0 && fromcol < vcol)
2998 fromcol = vcol;
2999
3000#ifdef FEAT_LINEBREAK
3001 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3002 if (wp->w_p_wrap)
3003 need_showbreak = TRUE;
3004#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003005#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003006 /* When spell checking a word we need to figure out the start of the
3007 * word and if it's badly spelled or not. */
3008 if (has_spell)
3009 {
3010 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003011 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003012
3013 pos = wp->w_cursor;
3014 wp->w_cursor.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003015 wp->w_cursor.col = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003016 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003017 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003018 {
3019 /* no bad word found at line start, don't check until end of a
3020 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003021 spell_hlf = HLF_COUNT;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003022 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003023 }
3024 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003025 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003026 /* bad word found, use attributes until end of word */
3027 word_end = wp->w_cursor.col + len + 1;
3028
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003029 /* Turn index into actual attributes. */
3030 if (spell_hlf != HLF_COUNT)
3031 spell_attr = highlight_attr[spell_hlf];
3032 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003033 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003034
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003035# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003036 /* Need to restart syntax highlighting for this line. */
3037 if (has_syntax)
3038 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003039# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003040 }
3041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043
3044 /*
3045 * Correct highlighting for cursor that can't be disabled.
3046 * Avoids having to check this for each character.
3047 */
3048 if (fromcol >= 0)
3049 {
3050 if (noinvcur)
3051 {
3052 if ((colnr_T)fromcol == wp->w_virtcol)
3053 {
3054 /* highlighting starts at cursor, let it start just after the
3055 * cursor */
3056 fromcol_prev = fromcol;
3057 fromcol = -1;
3058 }
3059 else if ((colnr_T)fromcol < wp->w_virtcol)
3060 /* restart highlighting after the cursor */
3061 fromcol_prev = wp->w_virtcol;
3062 }
3063 if (fromcol >= tocol)
3064 fromcol = -1;
3065 }
3066
3067#ifdef FEAT_SEARCH_EXTRA
3068 /*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003069 * Handle highlighting the last used search pattern and ":match".
3070 * Do this for both search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003072 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003074 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003075 shl->startcol = MAXCOL;
3076 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 shl->attr_cur = 0;
3078 if (shl->rm.regprog != NULL)
3079 {
3080 v = (long)(ptr - line);
3081 next_search_hl(wp, shl, lnum, (colnr_T)v);
3082
3083 /* Need to get the line again, a multi-line regexp may have made it
3084 * invalid. */
3085 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3086 ptr = line + v;
3087
3088 if (shl->lnum != 0 && shl->lnum <= lnum)
3089 {
3090 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003091 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003093 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3095 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003096 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003098 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003100 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
3102#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003103 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003104 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 else
3106#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003107 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003109 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
3111 shl->attr_cur = shl->attr;
3112 search_attr = shl->attr;
3113 }
3114 area_highlighting = TRUE;
3115 }
3116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 }
3118#endif
3119
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003120#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003121 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3122 * active, because it's not clear what is selected then. */
3123 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003124 {
3125 line_attr = hl_attr(HLF_CUL);
3126 area_highlighting = TRUE;
3127 }
3128#endif
3129
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003130 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 col = 0;
3132#ifdef FEAT_RIGHTLEFT
3133 if (wp->w_p_rl)
3134 {
3135 /* Rightleft window: process the text in the normal direction, but put
3136 * it in current_ScreenLine[] from right to left. Start at the
3137 * rightmost column of the window. */
3138 col = W_WIDTH(wp) - 1;
3139 off += col;
3140 }
3141#endif
3142
3143 /*
3144 * Repeat for the whole displayed line.
3145 */
3146 for (;;)
3147 {
3148 /* Skip this quickly when working on the text. */
3149 if (draw_state != WL_LINE)
3150 {
3151#ifdef FEAT_CMDWIN
3152 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3153 {
3154 draw_state = WL_CMDLINE;
3155 if (cmdwin_type != 0 && wp == curwin)
3156 {
3157 /* Draw the cmdline character. */
3158 *extra = cmdwin_type;
3159 n_extra = 1;
3160 p_extra = extra;
3161 c_extra = NUL;
3162 char_attr = hl_attr(HLF_AT);
3163 }
3164 }
3165#endif
3166
3167#ifdef FEAT_FOLDING
3168 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3169 {
3170 draw_state = WL_FOLD;
3171 if (wp->w_p_fdc > 0)
3172 {
3173 /* Draw the 'foldcolumn'. */
3174 fill_foldcolumn(extra, wp, FALSE, lnum);
3175 n_extra = wp->w_p_fdc;
3176 p_extra = extra;
3177 c_extra = NUL;
3178 char_attr = hl_attr(HLF_FC);
3179 }
3180 }
3181#endif
3182
3183#ifdef FEAT_SIGNS
3184 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3185 {
3186 draw_state = WL_SIGN;
3187 /* Show the sign column when there are any signs in this
3188 * buffer or when using Netbeans. */
3189 if (draw_signcolumn(wp)
3190# ifdef FEAT_DIFF
3191 && filler_todo <= 0
3192# endif
3193 )
3194 {
3195 int_u text_sign;
3196# ifdef FEAT_SIGN_ICONS
3197 int_u icon_sign;
3198# endif
3199
3200 /* Draw two cells with the sign value or blank. */
3201 c_extra = ' ';
3202 char_attr = hl_attr(HLF_SC);
3203 n_extra = 2;
3204
3205 if (row == startrow)
3206 {
3207 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3208 SIGN_TEXT);
3209# ifdef FEAT_SIGN_ICONS
3210 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3211 SIGN_ICON);
3212 if (gui.in_use && icon_sign != 0)
3213 {
3214 /* Use the image in this position. */
3215 c_extra = SIGN_BYTE;
3216# ifdef FEAT_NETBEANS_INTG
3217 if (buf_signcount(wp->w_buffer, lnum) > 1)
3218 c_extra = MULTISIGN_BYTE;
3219# endif
3220 char_attr = icon_sign;
3221 }
3222 else
3223# endif
3224 if (text_sign != 0)
3225 {
3226 p_extra = sign_get_text(text_sign);
3227 if (p_extra != NULL)
3228 {
3229 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003230 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232 char_attr = sign_get_attr(text_sign, FALSE);
3233 }
3234 }
3235 }
3236 }
3237#endif
3238
3239 if (draw_state == WL_NR - 1 && n_extra == 0)
3240 {
3241 draw_state = WL_NR;
3242 /* Display the line number. After the first fill with blanks
3243 * when the 'n' flag isn't in 'cpo' */
3244 if (wp->w_p_nu
3245 && (row == startrow
3246#ifdef FEAT_DIFF
3247 + filler_lines
3248#endif
3249 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3250 {
3251 /* Draw the line number (empty space after wrapping). */
3252 if (row == startrow
3253#ifdef FEAT_DIFF
3254 + filler_lines
3255#endif
3256 )
3257 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003258 sprintf((char *)extra, "%*ld ",
3259 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (wp->w_skipcol > 0)
3261 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3262 *p_extra = '-';
3263#ifdef FEAT_RIGHTLEFT
3264 if (wp->w_p_rl) /* reverse line numbers */
3265 rl_mirror(extra);
3266#endif
3267 p_extra = extra;
3268 c_extra = NUL;
3269 }
3270 else
3271 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003272 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003274#ifdef FEAT_SYN_HL
3275 /* When 'cursorline' is set highlight the line number of
3276 * the current line differently. */
3277 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3278 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281 }
3282
3283#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3284 if (draw_state == WL_SBR - 1 && n_extra == 0)
3285 {
3286 draw_state = WL_SBR;
3287# ifdef FEAT_DIFF
3288 if (filler_todo > 0)
3289 {
3290 /* Draw "deleted" diff line(s). */
3291 if (char2cells(fill_diff) > 1)
3292 c_extra = '-';
3293 else
3294 c_extra = fill_diff;
3295# ifdef FEAT_RIGHTLEFT
3296 if (wp->w_p_rl)
3297 n_extra = col + 1;
3298 else
3299# endif
3300 n_extra = W_WIDTH(wp) - col;
3301 char_attr = hl_attr(HLF_DED);
3302 }
3303# endif
3304# ifdef FEAT_LINEBREAK
3305 if (*p_sbr != NUL && need_showbreak)
3306 {
3307 /* Draw 'showbreak' at the start of each broken line. */
3308 p_extra = p_sbr;
3309 c_extra = NUL;
3310 n_extra = (int)STRLEN(p_sbr);
3311 char_attr = hl_attr(HLF_AT);
3312 need_showbreak = FALSE;
3313 /* Correct end of highlighted area for 'showbreak',
3314 * required when 'linebreak' is also set. */
3315 if (tocol == vcol)
3316 tocol += n_extra;
3317 }
3318# endif
3319 }
3320#endif
3321
3322 if (draw_state == WL_LINE - 1 && n_extra == 0)
3323 {
3324 draw_state = WL_LINE;
3325 if (saved_n_extra)
3326 {
3327 /* Continue item from end of wrapped line. */
3328 n_extra = saved_n_extra;
3329 c_extra = saved_c_extra;
3330 p_extra = saved_p_extra;
3331 char_attr = saved_char_attr;
3332 }
3333 else
3334 char_attr = 0;
3335 }
3336 }
3337
3338 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003339 if (dollar_vcol != 0 && wp == curwin
3340 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341#ifdef FEAT_DIFF
3342 && filler_todo <= 0
3343#endif
3344 )
3345 {
3346 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3347 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003348 /* Pretend we have finished updating the window. Except when
3349 * 'cursorcolumn' is set. */
3350#ifdef FEAT_SYN_HL
3351 if (wp->w_p_cuc)
3352 row = wp->w_cline_row + wp->w_cline_height;
3353 else
3354#endif
3355 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 break;
3357 }
3358
3359 if (draw_state == WL_LINE && area_highlighting)
3360 {
3361 /* handle Visual or match highlighting in this line */
3362 if (vcol == fromcol
3363#ifdef FEAT_MBYTE
3364 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3365 && (*mb_ptr2cells)(ptr) > 1)
3366#endif
3367 || ((int)vcol_prev == fromcol_prev
3368 && vcol < tocol))
3369 area_attr = attr; /* start highlighting */
3370 else if (area_attr != 0
3371 && (vcol == tocol
3372 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
3375#ifdef FEAT_SEARCH_EXTRA
3376 if (!n_extra)
3377 {
3378 /*
3379 * Check for start/end of search pattern match.
3380 * After end, check for start/end of next match.
3381 * When another match, have to check for start again.
3382 * Watch out for matching an empty string!
3383 * Do this first for search_hl, then for match_hl, so that
3384 * ":match" overrules 'hlsearch'.
3385 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003386 v = (long)(ptr - line);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003387 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003389 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 while (shl->rm.regprog != NULL)
3391 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003392 if (shl->startcol != MAXCOL
3393 && v >= (long)shl->startcol
3394 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 {
3396 shl->attr_cur = shl->attr;
3397 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003398 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
3400 shl->attr_cur = 0;
3401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 next_search_hl(wp, shl, lnum, (colnr_T)v);
3403
3404 /* Need to get the line again, a multi-line regexp
3405 * may have made it invalid. */
3406 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3407 ptr = line + v;
3408
3409 if (shl->lnum == lnum)
3410 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003411 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003413 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003415 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003417 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
3419 /* highlight empty match, try again after
3420 * it */
3421#ifdef FEAT_MBYTE
3422 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003423 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003424 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 else
3426#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003427 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429
3430 /* Loop to check if the match starts at the
3431 * current position */
3432 continue;
3433 }
3434 }
3435 break;
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 /* ":match" highlighting overrules 'hlsearch' */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003440 for (i = 0; i <= 3; ++i)
3441 if (i == 3)
3442 search_attr = search_hl.attr_cur;
3443 else if (match_hl[i].attr_cur != 0)
3444 {
3445 search_attr = match_hl[i].attr_cur;
3446 break;
3447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
3449#endif
3450
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003452 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 if (diff_hlf == HLF_CHD && ptr - line >= change_start)
3455 diff_hlf = HLF_TXD; /* changed text */
3456 if (diff_hlf == HLF_TXD && ptr - line > change_end)
3457 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003458 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003461
3462 /* Decide which of the highlight attributes to use. */
3463 attr_pri = TRUE;
3464 if (area_attr != 0)
3465 char_attr = area_attr;
3466 else if (search_attr != 0)
3467 char_attr = search_attr;
3468#ifdef LINE_ATTR
3469 /* Use line_attr when not in the Visual or 'incsearch' area
3470 * (area_attr may be 0 when "noinvcur" is set). */
3471 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3472 || (vcol < fromcol || vcol >= tocol)))
3473 char_attr = line_attr;
3474#endif
3475 else
3476 {
3477 attr_pri = FALSE;
3478#ifdef FEAT_SYN_HL
3479 if (has_syntax)
3480 char_attr = syntax_attr;
3481 else
3482#endif
3483 char_attr = 0;
3484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
3486
3487 /*
3488 * Get the next character to put on the screen.
3489 */
3490 /*
3491 * The 'extra' array contains the extra stuff that is inserted to
3492 * represent special characters (non-printable stuff). When all
3493 * characters are the same, c_extra is used.
3494 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3495 */
3496 if (n_extra > 0)
3497 {
3498 if (c_extra != NUL)
3499 {
3500 c = c_extra;
3501#ifdef FEAT_MBYTE
3502 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3503 if (enc_utf8 && (*mb_char2len)(c) > 1)
3504 {
3505 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003506 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003507 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
3509 else
3510 mb_utf8 = FALSE;
3511#endif
3512 }
3513 else
3514 {
3515 c = *p_extra;
3516#ifdef FEAT_MBYTE
3517 if (has_mbyte)
3518 {
3519 mb_c = c;
3520 if (enc_utf8)
3521 {
3522 /* If the UTF-8 character is more than one byte:
3523 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003524 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 mb_utf8 = FALSE;
3526 if (mb_l > n_extra)
3527 mb_l = 1;
3528 else if (mb_l > 1)
3529 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003530 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003532 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 }
3535 else
3536 {
3537 /* if this is a DBCS character, put it in "mb_c" */
3538 mb_l = MB_BYTE2LEN(c);
3539 if (mb_l >= n_extra)
3540 mb_l = 1;
3541 else if (mb_l > 1)
3542 mb_c = (c << 8) + p_extra[1];
3543 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003544 if (mb_l == 0) /* at the NUL at end-of-line */
3545 mb_l = 1;
3546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 /* If a double-width char doesn't fit display a '>' in the
3548 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003549 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550# ifdef FEAT_RIGHTLEFT
3551 wp->w_p_rl ? (col <= 0) :
3552# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003553 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 && (*mb_char2cells)(mb_c) == 2)
3555 {
3556 c = '>';
3557 mb_c = c;
3558 mb_l = 1;
3559 mb_utf8 = FALSE;
3560 multi_attr = hl_attr(HLF_AT);
3561 /* put the pointer back to output the double-width
3562 * character at the start of the next line. */
3563 ++n_extra;
3564 --p_extra;
3565 }
3566 else
3567 {
3568 n_extra -= mb_l - 1;
3569 p_extra += mb_l - 1;
3570 }
3571 }
3572#endif
3573 ++p_extra;
3574 }
3575 --n_extra;
3576 }
3577 else
3578 {
3579 /*
3580 * Get a character from the line itself.
3581 */
3582 c = *ptr;
3583#ifdef FEAT_MBYTE
3584 if (has_mbyte)
3585 {
3586 mb_c = c;
3587 if (enc_utf8)
3588 {
3589 /* If the UTF-8 character is more than one byte: Decode it
3590 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003591 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 mb_utf8 = FALSE;
3593 if (mb_l > 1)
3594 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003595 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 /* Overlong encoded ASCII or ASCII with composing char
3597 * is displayed normally, except a NUL. */
3598 if (mb_c < 0x80)
3599 c = mb_c;
3600 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003601
3602 /* At start of the line we can have a composing char.
3603 * Draw it as a space with a composing char. */
3604 if (utf_iscomposing(mb_c))
3605 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003606 for (i = Screen_mco - 1; i > 0; --i)
3607 u8cc[i] = u8cc[i - 1];
3608 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003609 mb_c = ' ';
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612
3613 if ((mb_l == 1 && c >= 0x80)
3614 || (mb_l >= 1 && mb_c == 0)
3615 || (mb_l > 1 && (!vim_isprintc(mb_c)
3616 || mb_c >= 0x10000)))
3617 {
3618 /*
3619 * Illegal UTF-8 byte: display as <xx>.
3620 * Non-BMP character : display as ? or fullwidth ?.
3621 */
3622 if (mb_c < 0x10000)
3623 {
3624 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003625# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (wp->w_p_rl) /* reverse */
3627 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630 else if (utf_char2cells(mb_c) != 2)
3631 STRCPY(extra, "?");
3632 else
3633 /* 0xff1f in UTF-8: full-width '?' */
3634 STRCPY(extra, "\357\274\237");
3635
3636 p_extra = extra;
3637 c = *p_extra;
3638 mb_c = mb_ptr2char_adv(&p_extra);
3639 mb_utf8 = (c >= 0x80);
3640 n_extra = (int)STRLEN(p_extra);
3641 c_extra = NUL;
3642 if (area_attr == 0 && search_attr == 0)
3643 {
3644 n_attr = n_extra + 1;
3645 extra_attr = hl_attr(HLF_8);
3646 saved_attr2 = char_attr; /* save current attr */
3647 }
3648 }
3649 else if (mb_l == 0) /* at the NUL at end-of-line */
3650 mb_l = 1;
3651#ifdef FEAT_ARABIC
3652 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3653 {
3654 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003655 int pc, pc1, nc;
3656 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
3658 /* The idea of what is the previous and next
3659 * character depends on 'rightleft'. */
3660 if (wp->w_p_rl)
3661 {
3662 pc = prev_c;
3663 pc1 = prev_c1;
3664 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003665 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667 else
3668 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003669 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003671 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 prev_c = mb_c;
3674
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003675 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677 else
3678 prev_c = mb_c;
3679#endif
3680 }
3681 else /* enc_dbcs */
3682 {
3683 mb_l = MB_BYTE2LEN(c);
3684 if (mb_l == 0) /* at the NUL at end-of-line */
3685 mb_l = 1;
3686 else if (mb_l > 1)
3687 {
3688 /* We assume a second byte below 32 is illegal.
3689 * Hopefully this is OK for all double-byte encodings!
3690 */
3691 if (ptr[1] >= 32)
3692 mb_c = (c << 8) + ptr[1];
3693 else
3694 {
3695 if (ptr[1] == NUL)
3696 {
3697 /* head byte at end of line */
3698 mb_l = 1;
3699 transchar_nonprint(extra, c);
3700 }
3701 else
3702 {
3703 /* illegal tail byte */
3704 mb_l = 2;
3705 STRCPY(extra, "XX");
3706 }
3707 p_extra = extra;
3708 n_extra = (int)STRLEN(extra) - 1;
3709 c_extra = NUL;
3710 c = *p_extra++;
3711 if (area_attr == 0 && search_attr == 0)
3712 {
3713 n_attr = n_extra + 1;
3714 extra_attr = hl_attr(HLF_8);
3715 saved_attr2 = char_attr; /* save current attr */
3716 }
3717 mb_c = c;
3718 }
3719 }
3720 }
3721 /* If a double-width char doesn't fit display a '>' in the
3722 * last column; the character is displayed at the start of the
3723 * next line. */
3724 if ((
3725# ifdef FEAT_RIGHTLEFT
3726 wp->w_p_rl ? (col <= 0) :
3727# endif
3728 (col >= W_WIDTH(wp) - 1))
3729 && (*mb_char2cells)(mb_c) == 2)
3730 {
3731 c = '>';
3732 mb_c = c;
3733 mb_utf8 = FALSE;
3734 mb_l = 1;
3735 multi_attr = hl_attr(HLF_AT);
3736 /* Put pointer back so that the character will be
3737 * displayed at the start of the next line. */
3738 --ptr;
3739 }
3740 else if (*ptr != NUL)
3741 ptr += mb_l - 1;
3742
3743 /* If a double-width char doesn't fit at the left side display
3744 * a '<' in the first column. */
3745 if (n_skip > 0 && mb_l > 1)
3746 {
3747 extra[0] = '<';
3748 p_extra = extra;
3749 n_extra = 1;
3750 c_extra = NUL;
3751 c = ' ';
3752 if (area_attr == 0 && search_attr == 0)
3753 {
3754 n_attr = n_extra + 1;
3755 extra_attr = hl_attr(HLF_AT);
3756 saved_attr2 = char_attr; /* save current attr */
3757 }
3758 mb_c = c;
3759 mb_utf8 = FALSE;
3760 mb_l = 1;
3761 }
3762
3763 }
3764#endif
3765 ++ptr;
3766
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003767 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003768 if (wp->w_p_list && (c == 160
3769#ifdef FEAT_MBYTE
3770 || (mb_utf8 && mb_c == 160)
3771#endif
3772 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003773 {
3774 c = lcs_nbsp;
3775 if (area_attr == 0 && search_attr == 0)
3776 {
3777 n_attr = 1;
3778 extra_attr = hl_attr(HLF_8);
3779 saved_attr2 = char_attr; /* save current attr */
3780 }
3781#ifdef FEAT_MBYTE
3782 mb_c = c;
3783 if (enc_utf8 && (*mb_char2len)(c) > 1)
3784 {
3785 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003786 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003787 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003788 }
3789 else
3790 mb_utf8 = FALSE;
3791#endif
3792 }
3793
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (extra_check)
3795 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003796#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003797 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003798#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003799
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003800#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 /* Get syntax attribute, unless still at the start of the line
3802 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003803 v = (long)(ptr - line);
3804 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
3806 /* Get the syntax attribute for the character. If there
3807 * is an error, disable syntax highlighting. */
3808 save_did_emsg = did_emsg;
3809 did_emsg = FALSE;
3810
Bram Moolenaar217ad922005-03-20 22:37:15 +00003811 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003812# ifdef FEAT_SPELL
3813 has_spell ? &can_spell :
3814# endif
3815 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
3817 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003818 {
3819 wp->w_buffer->b_syn_error = TRUE;
3820 has_syntax = FALSE;
3821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 else
3823 did_emsg = save_did_emsg;
3824
3825 /* Need to get the line again, a multi-line regexp may
3826 * have made it invalid. */
3827 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3828 ptr = line + v;
3829
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003830 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003832 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003833 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003835#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003836
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003837#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003838 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003839 * Only do this when there is no syntax highlighting, the
3840 * @Spell cluster is not used or the current syntax item
3841 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003842 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003843 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003844 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003845# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003846 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003847 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003848# endif
3849 if (c != 0 && (
3850# ifdef FEAT_SYN_HL
3851 !has_syntax ||
3852# endif
3853 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003854 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003855 char_u *prev_ptr, *p;
3856 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003857 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003858# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003859 if (has_mbyte)
3860 {
3861 prev_ptr = ptr - mb_l;
3862 v -= mb_l - 1;
3863 }
3864 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003865# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003866 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003867
3868 /* Use nextline[] if possible, it has the start of the
3869 * next line concatenated. */
3870 if ((prev_ptr - line) - nextlinecol >= 0)
3871 p = nextline + (prev_ptr - line) - nextlinecol;
3872 else
3873 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003874 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003875 len = spell_check(wp, p, &spell_hlf, &cap_col,
3876 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003877 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003878
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003879 /* In Insert mode only highlight a word that
3880 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003881 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003882 && (State & INSERT) != 0
3883 && wp->w_cursor.lnum == lnum
3884 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003885 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003886 && wp->w_cursor.col < (colnr_T)word_end)
3887 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003888 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003889 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003890 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003891
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003892 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003893 && (p - nextline) + len > nextline_idx)
3894 {
3895 /* Remember that the good word continues at the
3896 * start of the next line. */
3897 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003898 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003899 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003900
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003901 /* Turn index into actual attributes. */
3902 if (spell_hlf != HLF_COUNT)
3903 spell_attr = highlight_attr[spell_hlf];
3904
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003905 if (cap_col > 0)
3906 {
3907 if (p != prev_ptr
3908 && (p - nextline) + cap_col >= nextline_idx)
3909 {
3910 /* Remember that the word in the next line
3911 * must start with a capital. */
3912 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003913 cap_col = (int)((p - nextline) + cap_col
3914 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003915 }
3916 else
3917 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003918 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003919 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003920 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003921 }
3922 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003923 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003924 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003925 char_attr = hl_combine_attr(char_attr, spell_attr);
3926 else
3927 char_attr = hl_combine_attr(spell_attr, char_attr);
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929#endif
3930#ifdef FEAT_LINEBREAK
3931 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00003932 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 */
3934 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003935 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
3937 n_extra = win_lbr_chartabsize(wp, ptr - (
3938# ifdef FEAT_MBYTE
3939 has_mbyte ? mb_l :
3940# endif
3941 1), (colnr_T)vcol, NULL) - 1;
3942 c_extra = ' ';
3943 if (vim_iswhite(c))
3944 c = ' ';
3945 }
3946#endif
3947
3948 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3949 {
3950 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003951 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
3953 n_attr = 1;
3954 extra_attr = hl_attr(HLF_8);
3955 saved_attr2 = char_attr; /* save current attr */
3956 }
3957#ifdef FEAT_MBYTE
3958 mb_c = c;
3959 if (enc_utf8 && (*mb_char2len)(c) > 1)
3960 {
3961 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003962 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003963 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
3965 else
3966 mb_utf8 = FALSE;
3967#endif
3968 }
3969 }
3970
3971 /*
3972 * Handling of non-printable characters.
3973 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003974 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
3976 /*
3977 * when getting a character from the file, we may have to
3978 * turn it into something else on the way to putting it
3979 * into "ScreenLines".
3980 */
3981 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3982 {
3983 /* tab amount depends on current column */
3984 n_extra = (int)wp->w_buffer->b_p_ts
3985 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3986#ifdef FEAT_MBYTE
3987 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3988#endif
3989 if (wp->w_p_list)
3990 {
3991 c = lcs_tab1;
3992 c_extra = lcs_tab2;
3993 n_attr = n_extra + 1;
3994 extra_attr = hl_attr(HLF_8);
3995 saved_attr2 = char_attr; /* save current attr */
3996#ifdef FEAT_MBYTE
3997 mb_c = c;
3998 if (enc_utf8 && (*mb_char2len)(c) > 1)
3999 {
4000 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004001 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004002 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004#endif
4005 }
4006 else
4007 {
4008 c_extra = ' ';
4009 c = ' ';
4010 }
4011 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004012 else if (c == NUL
4013 && ((wp->w_p_list && lcs_eol > 0)
4014 || ((fromcol >= 0 || fromcol_prev >= 0)
4015 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004016#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004017 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004018#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004019 && (
4020# ifdef FEAT_RIGHTLEFT
4021 wp->w_p_rl ? (col >= 0) :
4022# endif
4023 (col < W_WIDTH(wp)))
4024 && !(noinvcur
4025 && (colnr_T)vcol == wp->w_virtcol)))
4026 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004028 /* Display a '$' after the line or highlight an extra
4029 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4031 /* For a diff line the highlighting continues after the
4032 * "$". */
4033 if (
4034# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004035 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036# ifdef LINE_ATTR
4037 &&
4038# endif
4039# endif
4040# ifdef LINE_ATTR
4041 line_attr == 0
4042# endif
4043 )
4044#endif
4045 {
4046#ifdef FEAT_VIRTUALEDIT
4047 /* In virtualedit, visual selections may extend
4048 * beyond end of line. */
4049 if (area_highlighting && virtual_active()
4050 && tocol != MAXCOL && vcol < tocol)
4051 n_extra = 0;
4052 else
4053#endif
4054 {
4055 p_extra = at_end_str;
4056 n_extra = 1;
4057 c_extra = NUL;
4058 }
4059 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004060 if (wp->w_p_list)
4061 c = lcs_eol;
4062 else
4063 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 lcs_eol_one = -1;
4065 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004066 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
4068 extra_attr = hl_attr(HLF_AT);
4069 n_attr = 1;
4070 }
4071#ifdef FEAT_MBYTE
4072 mb_c = c;
4073 if (enc_utf8 && (*mb_char2len)(c) > 1)
4074 {
4075 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004076 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004077 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 else
4080 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4081#endif
4082 }
4083 else if (c != NUL)
4084 {
4085 p_extra = transchar(c);
4086#ifdef FEAT_RIGHTLEFT
4087 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4088 rl_mirror(p_extra); /* reverse "<12>" */
4089#endif
4090 n_extra = byte2cells(c) - 1;
4091 c_extra = NUL;
4092 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004093 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
4095 n_attr = n_extra + 1;
4096 extra_attr = hl_attr(HLF_8);
4097 saved_attr2 = char_attr; /* save current attr */
4098 }
4099#ifdef FEAT_MBYTE
4100 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4101#endif
4102 }
4103#ifdef FEAT_VIRTUALEDIT
4104 else if (VIsual_active
4105 && (VIsual_mode == Ctrl_V
4106 || VIsual_mode == 'v')
4107 && virtual_active()
4108 && tocol != MAXCOL
4109 && vcol < tocol
4110 && (
4111# ifdef FEAT_RIGHTLEFT
4112 wp->w_p_rl ? (col >= 0) :
4113# endif
4114 (col < W_WIDTH(wp))))
4115 {
4116 c = ' ';
4117 --ptr; /* put it back at the NUL */
4118 }
4119#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004120#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 else if ((
4122# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004123 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 ) && (
4127# ifdef FEAT_RIGHTLEFT
4128 wp->w_p_rl ? (col >= 0) :
4129# endif
4130 (col < W_WIDTH(wp))))
4131 {
4132 /* Highlight until the right side of the window */
4133 c = ' ';
4134 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004135
4136 /* Remember we do the char for line highlighting. */
4137 ++did_line_attr;
4138
4139 /* don't do search HL for the rest of the line */
4140 if (line_attr != 0 && char_attr == search_attr && col > 0)
4141 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142# ifdef FEAT_DIFF
4143 if (diff_hlf == HLF_TXD)
4144 {
4145 diff_hlf = HLF_CHD;
4146 if (attr == 0 || char_attr != attr)
4147 char_attr = hl_attr(diff_hlf);
4148 }
4149# endif
4150 }
4151#endif
4152 }
4153 }
4154
4155 /* Don't override visual selection highlighting. */
4156 if (n_attr > 0
4157 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004158 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 char_attr = extra_attr;
4160
Bram Moolenaar81695252004-12-29 20:58:21 +00004161#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 /* XIM don't send preedit_start and preedit_end, but they send
4163 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4164 * im_is_preediting() here. */
4165 if (xic != NULL
4166 && lnum == curwin->w_cursor.lnum
4167 && (State & INSERT)
4168 && !p_imdisable
4169 && im_is_preediting()
4170 && draw_state == WL_LINE)
4171 {
4172 colnr_T tcol;
4173
4174 if (preedit_end_col == MAXCOL)
4175 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4176 else
4177 tcol = preedit_end_col;
4178 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4179 {
4180 if (feedback_old_attr < 0)
4181 {
4182 feedback_col = 0;
4183 feedback_old_attr = char_attr;
4184 }
4185 char_attr = im_get_feedback_attr(feedback_col);
4186 if (char_attr < 0)
4187 char_attr = feedback_old_attr;
4188 feedback_col++;
4189 }
4190 else if (feedback_old_attr >= 0)
4191 {
4192 char_attr = feedback_old_attr;
4193 feedback_old_attr = -1;
4194 feedback_col = 0;
4195 }
4196 }
4197#endif
4198 /*
4199 * Handle the case where we are in column 0 but not on the first
4200 * character of the line and the user wants us to show us a
4201 * special character (via 'listchars' option "precedes:<char>".
4202 */
4203 if (lcs_prec_todo != NUL
4204 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4205#ifdef FEAT_DIFF
4206 && filler_todo <= 0
4207#endif
4208 && draw_state > WL_NR
4209 && c != NUL)
4210 {
4211 c = lcs_prec;
4212 lcs_prec_todo = NUL;
4213#ifdef FEAT_MBYTE
4214 mb_c = c;
4215 if (enc_utf8 && (*mb_char2len)(c) > 1)
4216 {
4217 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004218 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004219 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221 else
4222 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4223#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004224 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
4226 saved_attr3 = char_attr; /* save current attr */
4227 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4228 n_attr3 = 1;
4229 }
4230 }
4231
4232 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004233 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004235 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004236#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004237 || did_line_attr == 1
4238#endif
4239 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004241#ifdef FEAT_SEARCH_EXTRA
4242 long prevcol = (long)(ptr - line) - (c == NUL);
4243#endif
4244
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 /* invert at least one char, used for Visual and empty line or
4246 * highlight match at end of line. If it's beyond the last
4247 * char on the screen, just overwrite that one (tricky!) Not
4248 * needed when a '$' was displayed for 'list'. */
4249 if (lcs_eol == lcs_eol_one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004250 && ((area_attr != 0 && vcol == fromcol && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251#ifdef FEAT_SEARCH_EXTRA
4252 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004253 || ((prevcol == (long)search_hl.startcol
4254 || prevcol == (long)match_hl[0].startcol
4255 || prevcol == (long)match_hl[1].startcol
4256 || prevcol == (long)match_hl[2].startcol)
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004257# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004258 && did_line_attr <= 1
4259# endif
4260 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261#endif
4262 ))
4263 {
4264 int n = 0;
4265
4266#ifdef FEAT_RIGHTLEFT
4267 if (wp->w_p_rl)
4268 {
4269 if (col < 0)
4270 n = 1;
4271 }
4272 else
4273#endif
4274 {
4275 if (col >= W_WIDTH(wp))
4276 n = -1;
4277 }
4278 if (n != 0)
4279 {
4280 /* At the window boundary, highlight the last character
4281 * instead (better than nothing). */
4282 off += n;
4283 col += n;
4284 }
4285 else
4286 {
4287 /* Add a blank character to highlight. */
4288 ScreenLines[off] = ' ';
4289#ifdef FEAT_MBYTE
4290 if (enc_utf8)
4291 ScreenLinesUC[off] = 0;
4292#endif
4293 }
4294#ifdef FEAT_SEARCH_EXTRA
4295 if (area_attr == 0)
4296 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004297 for (i = 0; i <= 3; ++i)
4298 {
4299 if (i == 3)
4300 char_attr = search_hl.attr;
4301 else if ((ptr - line) - 1 == (long)match_hl[i].startcol)
4302 {
4303 char_attr = match_hl[i].attr;
4304 break;
4305 }
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308#endif
4309 ScreenAttrs[off] = char_attr;
4310#ifdef FEAT_RIGHTLEFT
4311 if (wp->w_p_rl)
4312 --col;
4313 else
4314#endif
4315 ++col;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004316 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaar91170f82006-05-05 21:15:17 +00004320 /*
4321 * At end of the text line.
4322 */
4323 if (c == NUL)
4324 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004325#ifdef FEAT_SYN_HL
4326 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004327 if (wp->w_p_wrap)
4328 v = wp->w_skipcol;
4329 else
4330 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004331 /* check if line ends before left margin */
4332 if (vcol < v + col - win_col_off(wp))
4333
4334 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004335 if (wp->w_p_cuc
4336 && (int)wp->w_virtcol >= vcol
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004337 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4338 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004339 && lnum != wp->w_cursor.lnum
4340# ifdef FEAT_RIGHTLEFT
4341 && !wp->w_p_rl
4342# endif
4343 )
4344 {
4345 while (col < W_WIDTH(wp))
4346 {
4347 ScreenLines[off] = ' ';
4348#ifdef FEAT_MBYTE
4349 if (enc_utf8)
4350 ScreenLinesUC[off] = 0;
4351#endif
4352 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004353 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004354 {
4355 ScreenAttrs[off] = hl_attr(HLF_CUC);
4356 break;
4357 }
4358 ScreenAttrs[off++] = 0;
4359 ++vcol;
4360 }
4361 }
4362#endif
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4365 wp->w_p_rl);
4366 row++;
4367
4368 /*
4369 * Update w_cline_height and w_cline_folded if the cursor line was
4370 * updated (saves a call to plines() later).
4371 */
4372 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4373 {
4374 curwin->w_cline_row = startrow;
4375 curwin->w_cline_height = row - startrow;
4376#ifdef FEAT_FOLDING
4377 curwin->w_cline_folded = FALSE;
4378#endif
4379 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4380 }
4381
4382 break;
4383 }
4384
4385 /* line continues beyond line end */
4386 if (lcs_ext
4387 && !wp->w_p_wrap
4388#ifdef FEAT_DIFF
4389 && filler_todo <= 0
4390#endif
4391 && (
4392#ifdef FEAT_RIGHTLEFT
4393 wp->w_p_rl ? col == 0 :
4394#endif
4395 col == W_WIDTH(wp) - 1)
4396 && (*ptr != NUL
4397 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4398 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4399 {
4400 c = lcs_ext;
4401 char_attr = hl_attr(HLF_AT);
4402#ifdef FEAT_MBYTE
4403 mb_c = c;
4404 if (enc_utf8 && (*mb_char2len)(c) > 1)
4405 {
4406 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004407 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004408 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410 else
4411 mb_utf8 = FALSE;
4412#endif
4413 }
4414
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004415#ifdef FEAT_SYN_HL
4416 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4417 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004418 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004419 && lnum != wp->w_cursor.lnum
4420 && draw_state == WL_LINE)
4421 {
4422 vcol_save_attr = char_attr;
4423 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4424 }
4425 else
4426 vcol_save_attr = -1;
4427#endif
4428
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 /*
4430 * Store character to be displayed.
4431 * Skip characters that are left of the screen for 'nowrap'.
4432 */
4433 vcol_prev = vcol;
4434 if (draw_state < WL_LINE || n_skip <= 0)
4435 {
4436 /*
4437 * Store the character.
4438 */
4439#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4440 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4441 {
4442 /* A double-wide character is: put first halve in left cell. */
4443 --off;
4444 --col;
4445 }
4446#endif
4447 ScreenLines[off] = c;
4448#ifdef FEAT_MBYTE
4449 if (enc_dbcs == DBCS_JPNU)
4450 ScreenLines2[off] = mb_c & 0xff;
4451 else if (enc_utf8)
4452 {
4453 if (mb_utf8)
4454 {
4455 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004456 if ((c & 0xff) == 0)
4457 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004458 for (i = 0; i < Screen_mco; ++i)
4459 {
4460 ScreenLinesC[i][off] = u8cc[i];
4461 if (u8cc[i] == 0)
4462 break;
4463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 else
4466 ScreenLinesUC[off] = 0;
4467 }
4468 if (multi_attr)
4469 {
4470 ScreenAttrs[off] = multi_attr;
4471 multi_attr = 0;
4472 }
4473 else
4474#endif
4475 ScreenAttrs[off] = char_attr;
4476
4477#ifdef FEAT_MBYTE
4478 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4479 {
4480 /* Need to fill two screen columns. */
4481 ++off;
4482 ++col;
4483 if (enc_utf8)
4484 /* UTF-8: Put a 0 in the second screen char. */
4485 ScreenLines[off] = 0;
4486 else
4487 /* DBCS: Put second byte in the second screen char. */
4488 ScreenLines[off] = mb_c & 0xff;
4489 ++vcol;
4490 /* When "tocol" is halfway a character, set it to the end of
4491 * the character, otherwise highlighting won't stop. */
4492 if (tocol == vcol)
4493 ++tocol;
4494#ifdef FEAT_RIGHTLEFT
4495 if (wp->w_p_rl)
4496 {
4497 /* now it's time to backup one cell */
4498 --off;
4499 --col;
4500 }
4501#endif
4502 }
4503#endif
4504#ifdef FEAT_RIGHTLEFT
4505 if (wp->w_p_rl)
4506 {
4507 --off;
4508 --col;
4509 }
4510 else
4511#endif
4512 {
4513 ++off;
4514 ++col;
4515 }
4516 }
4517 else
4518 --n_skip;
4519
4520 /* Only advance the "vcol" when after the 'number' column. */
4521 if (draw_state >= WL_SBR
4522#ifdef FEAT_DIFF
4523 && filler_todo <= 0
4524#endif
4525 )
4526 ++vcol;
4527
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004528#ifdef FEAT_SYN_HL
4529 if (vcol_save_attr >= 0)
4530 char_attr = vcol_save_attr;
4531#endif
4532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 /* restore attributes after "predeces" in 'listchars' */
4534 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4535 char_attr = saved_attr3;
4536
4537 /* restore attributes after last 'listchars' or 'number' char */
4538 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4539 char_attr = saved_attr2;
4540
4541 /*
4542 * At end of screen line and there is more to come: Display the line
4543 * so far. If there is no more to display it is catched above.
4544 */
4545 if ((
4546#ifdef FEAT_RIGHTLEFT
4547 wp->w_p_rl ? (col < 0) :
4548#endif
4549 (col >= W_WIDTH(wp)))
4550 && (*ptr != NUL
4551#ifdef FEAT_DIFF
4552 || filler_todo > 0
4553#endif
4554 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4555 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4556 )
4557 {
4558 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4559 wp->w_p_rl);
4560 ++row;
4561 ++screen_row;
4562
4563 /* When not wrapping and finished diff lines, or when displayed
4564 * '$' and highlighting until last column, break here. */
4565 if ((!wp->w_p_wrap
4566#ifdef FEAT_DIFF
4567 && filler_todo <= 0
4568#endif
4569 ) || lcs_eol_one == -1)
4570 break;
4571
4572 /* When the window is too narrow draw all "@" lines. */
4573 if (draw_state != WL_LINE
4574#ifdef FEAT_DIFF
4575 && filler_todo <= 0
4576#endif
4577 )
4578 {
4579 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4580#ifdef FEAT_VERTSPLIT
4581 draw_vsep_win(wp, row);
4582#endif
4583 row = endrow;
4584 }
4585
4586 /* When line got too long for screen break here. */
4587 if (row == endrow)
4588 {
4589 ++row;
4590 break;
4591 }
4592
4593 if (screen_cur_row == screen_row - 1
4594#ifdef FEAT_DIFF
4595 && filler_todo <= 0
4596#endif
4597 && W_WIDTH(wp) == Columns)
4598 {
4599 /* Remember that the line wraps, used for modeless copy. */
4600 LineWraps[screen_row - 1] = TRUE;
4601
4602 /*
4603 * Special trick to make copy/paste of wrapped lines work with
4604 * xterm/screen: write an extra character beyond the end of
4605 * the line. This will work with all terminal types
4606 * (regardless of the xn,am settings).
4607 * Only do this on a fast tty.
4608 * Only do this if the cursor is on the current line
4609 * (something has been written in it).
4610 * Don't do this for the GUI.
4611 * Don't do this for double-width characters.
4612 * Don't do this for a window not at the right screen border.
4613 */
4614 if (p_tf
4615#ifdef FEAT_GUI
4616 && !gui.in_use
4617#endif
4618#ifdef FEAT_MBYTE
4619 && !(has_mbyte
4620 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4621 || (*mb_off2cells)(LineOffset[screen_row - 1]
4622 + (int)Columns - 2) == 2))
4623#endif
4624 )
4625 {
4626 /* First make sure we are at the end of the screen line,
4627 * then output the same character again to let the
4628 * terminal know about the wrap. If the terminal doesn't
4629 * auto-wrap, we overwrite the character. */
4630 if (screen_cur_col != W_WIDTH(wp))
4631 screen_char(LineOffset[screen_row - 1]
4632 + (unsigned)Columns - 1,
4633 screen_row - 1, (int)(Columns - 1));
4634
4635#ifdef FEAT_MBYTE
4636 /* When there is a multi-byte character, just output a
4637 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004638 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4639 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 out_char(' ');
4641 else
4642#endif
4643 out_char(ScreenLines[LineOffset[screen_row - 1]
4644 + (Columns - 1)]);
4645 /* force a redraw of the first char on the next line */
4646 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4647 screen_start(); /* don't know where cursor is now */
4648 }
4649 }
4650
4651 col = 0;
4652 off = (unsigned)(current_ScreenLine - ScreenLines);
4653#ifdef FEAT_RIGHTLEFT
4654 if (wp->w_p_rl)
4655 {
4656 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4657 off += col;
4658 }
4659#endif
4660
4661 /* reset the drawing state for the start of a wrapped line */
4662 draw_state = WL_START;
4663 saved_n_extra = n_extra;
4664 saved_p_extra = p_extra;
4665 saved_c_extra = c_extra;
4666 saved_char_attr = char_attr;
4667 n_extra = 0;
4668 lcs_prec_todo = lcs_prec;
4669#ifdef FEAT_LINEBREAK
4670# ifdef FEAT_DIFF
4671 if (filler_todo <= 0)
4672# endif
4673 need_showbreak = TRUE;
4674#endif
4675#ifdef FEAT_DIFF
4676 --filler_todo;
4677 /* When the filler lines are actually below the last line of the
4678 * file, don't draw the line itself, break here. */
4679 if (filler_todo == 0 && wp->w_botfill)
4680 break;
4681#endif
4682 }
4683
4684 } /* for every character in the line */
4685
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004686#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004687 /* After an empty line check first word for capital. */
4688 if (*skipwhite(line) == NUL)
4689 {
4690 capcol_lnum = lnum + 1;
4691 cap_col = 0;
4692 }
4693#endif
4694
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 return row;
4696}
4697
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004698#ifdef FEAT_MBYTE
4699static int comp_char_differs __ARGS((int, int));
4700
4701/*
4702 * Return if the composing characters at "off_from" and "off_to" differ.
4703 */
4704 static int
4705comp_char_differs(off_from, off_to)
4706 int off_from;
4707 int off_to;
4708{
4709 int i;
4710
4711 for (i = 0; i < Screen_mco; ++i)
4712 {
4713 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4714 return TRUE;
4715 if (ScreenLinesC[i][off_from] == 0)
4716 break;
4717 }
4718 return FALSE;
4719}
4720#endif
4721
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722/*
4723 * Check whether the given character needs redrawing:
4724 * - the (first byte of the) character is different
4725 * - the attributes are different
4726 * - the character is multi-byte and the next byte is different
4727 */
4728 static int
4729char_needs_redraw(off_from, off_to, cols)
4730 int off_from;
4731 int off_to;
4732 int cols;
4733{
4734 if (cols > 0
4735 && ((ScreenLines[off_from] != ScreenLines[off_to]
4736 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4737
4738#ifdef FEAT_MBYTE
4739 || (enc_dbcs != 0
4740 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4741 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4742 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4743 : (cols > 1 && ScreenLines[off_from + 1]
4744 != ScreenLines[off_to + 1])))
4745 || (enc_utf8
4746 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4747 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004748 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749#endif
4750 ))
4751 return TRUE;
4752 return FALSE;
4753}
4754
4755/*
4756 * Move one "cooked" screen line to the screen, but only the characters that
4757 * have actually changed. Handle insert/delete character.
4758 * "coloff" gives the first column on the screen for this line.
4759 * "endcol" gives the columns where valid characters are.
4760 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4761 * needs to be cleared, negative otherwise.
4762 * "rlflag" is TRUE in a rightleft window:
4763 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4764 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4765 */
4766 static void
4767screen_line(row, coloff, endcol, clear_width
4768#ifdef FEAT_RIGHTLEFT
4769 , rlflag
4770#endif
4771 )
4772 int row;
4773 int coloff;
4774 int endcol;
4775 int clear_width;
4776#ifdef FEAT_RIGHTLEFT
4777 int rlflag;
4778#endif
4779{
4780 unsigned off_from;
4781 unsigned off_to;
4782 int col = 0;
4783#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4784 int hl;
4785#endif
4786 int force = FALSE; /* force update rest of the line */
4787 int redraw_this /* bool: does character need redraw? */
4788#ifdef FEAT_GUI
4789 = TRUE /* For GUI when while-loop empty */
4790#endif
4791 ;
4792 int redraw_next; /* redraw_this for next character */
4793#ifdef FEAT_MBYTE
4794 int clear_next = FALSE;
4795 int char_cells; /* 1: normal char */
4796 /* 2: occupies two display cells */
4797# define CHAR_CELLS char_cells
4798#else
4799# define CHAR_CELLS 1
4800#endif
4801
4802# ifdef FEAT_CLIPBOARD
4803 clip_may_clear_selection(row, row);
4804# endif
4805
4806 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4807 off_to = LineOffset[row] + coloff;
4808
4809#ifdef FEAT_RIGHTLEFT
4810 if (rlflag)
4811 {
4812 /* Clear rest first, because it's left of the text. */
4813 if (clear_width > 0)
4814 {
4815 while (col <= endcol && ScreenLines[off_to] == ' '
4816 && ScreenAttrs[off_to] == 0
4817# ifdef FEAT_MBYTE
4818 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4819# endif
4820 )
4821 {
4822 ++off_to;
4823 ++col;
4824 }
4825 if (col <= endcol)
4826 screen_fill(row, row + 1, col + coloff,
4827 endcol + coloff + 1, ' ', ' ', 0);
4828 }
4829 col = endcol + 1;
4830 off_to = LineOffset[row] + col + coloff;
4831 off_from += col;
4832 endcol = (clear_width > 0 ? clear_width : -clear_width);
4833 }
4834#endif /* FEAT_RIGHTLEFT */
4835
4836 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4837
4838 while (col < endcol)
4839 {
4840#ifdef FEAT_MBYTE
4841 if (has_mbyte && (col + 1 < endcol))
4842 char_cells = (*mb_off2cells)(off_from);
4843 else
4844 char_cells = 1;
4845#endif
4846
4847 redraw_this = redraw_next;
4848 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4849 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4850
4851#ifdef FEAT_GUI
4852 /* If the next character was bold, then redraw the current character to
4853 * remove any pixels that might have spilt over into us. This only
4854 * happens in the GUI.
4855 */
4856 if (redraw_next && gui.in_use)
4857 {
4858 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004859 if (hl > HL_ALL)
4860 hl = syn_attr2attr(hl);
4861 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 redraw_this = TRUE;
4863 }
4864#endif
4865
4866 if (redraw_this)
4867 {
4868 /*
4869 * Special handling when 'xs' termcap flag set (hpterm):
4870 * Attributes for characters are stored at the position where the
4871 * cursor is when writing the highlighting code. The
4872 * start-highlighting code must be written with the cursor on the
4873 * first highlighted character. The stop-highlighting code must
4874 * be written with the cursor just after the last highlighted
4875 * character.
4876 * Overwriting a character doesn't remove it's highlighting. Need
4877 * to clear the rest of the line, and force redrawing it
4878 * completely.
4879 */
4880 if ( p_wiv
4881 && !force
4882#ifdef FEAT_GUI
4883 && !gui.in_use
4884#endif
4885 && ScreenAttrs[off_to] != 0
4886 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4887 {
4888 /*
4889 * Need to remove highlighting attributes here.
4890 */
4891 windgoto(row, col + coloff);
4892 out_str(T_CE); /* clear rest of this screen line */
4893 screen_start(); /* don't know where cursor is now */
4894 force = TRUE; /* force redraw of rest of the line */
4895 redraw_next = TRUE; /* or else next char would miss out */
4896
4897 /*
4898 * If the previous character was highlighted, need to stop
4899 * highlighting at this character.
4900 */
4901 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4902 {
4903 screen_attr = ScreenAttrs[off_to - 1];
4904 term_windgoto(row, col + coloff);
4905 screen_stop_highlight();
4906 }
4907 else
4908 screen_attr = 0; /* highlighting has stopped */
4909 }
4910#ifdef FEAT_MBYTE
4911 if (enc_dbcs != 0)
4912 {
4913 /* Check if overwriting a double-byte with a single-byte or
4914 * the other way around requires another character to be
4915 * redrawn. For UTF-8 this isn't needed, because comparing
4916 * ScreenLinesUC[] is sufficient. */
4917 if (char_cells == 1
4918 && col + 1 < endcol
4919 && (*mb_off2cells)(off_to) > 1)
4920 {
4921 /* Writing a single-cell character over a double-cell
4922 * character: need to redraw the next cell. */
4923 ScreenLines[off_to + 1] = 0;
4924 redraw_next = TRUE;
4925 }
4926 else if (char_cells == 2
4927 && col + 2 < endcol
4928 && (*mb_off2cells)(off_to) == 1
4929 && (*mb_off2cells)(off_to + 1) > 1)
4930 {
4931 /* Writing the second half of a double-cell character over
4932 * a double-cell character: need to redraw the second
4933 * cell. */
4934 ScreenLines[off_to + 2] = 0;
4935 redraw_next = TRUE;
4936 }
4937
4938 if (enc_dbcs == DBCS_JPNU)
4939 ScreenLines2[off_to] = ScreenLines2[off_from];
4940 }
4941 /* When writing a single-width character over a double-width
4942 * character and at the end of the redrawn text, need to clear out
4943 * the right halve of the old character.
4944 * Also required when writing the right halve of a double-width
4945 * char over the left halve of an existing one. */
4946 if (has_mbyte && col + char_cells == endcol
4947 && ((char_cells == 1
4948 && (*mb_off2cells)(off_to) > 1)
4949 || (char_cells == 2
4950 && (*mb_off2cells)(off_to) == 1
4951 && (*mb_off2cells)(off_to + 1) > 1)))
4952 clear_next = TRUE;
4953#endif
4954
4955 ScreenLines[off_to] = ScreenLines[off_from];
4956#ifdef FEAT_MBYTE
4957 if (enc_utf8)
4958 {
4959 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4960 if (ScreenLinesUC[off_from] != 0)
4961 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004962 int i;
4963
4964 for (i = 0; i < Screen_mco; ++i)
4965 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
4967 }
4968 if (char_cells == 2)
4969 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4970#endif
4971
4972#if defined(FEAT_GUI) || defined(UNIX)
4973 /* The bold trick makes a single row of pixels appear in the next
4974 * character. When a bold character is removed, the next
4975 * character should be redrawn too. This happens for our own GUI
4976 * and for some xterms. */
4977 if (
4978# ifdef FEAT_GUI
4979 gui.in_use
4980# endif
4981# if defined(FEAT_GUI) && defined(UNIX)
4982 ||
4983# endif
4984# ifdef UNIX
4985 term_is_xterm
4986# endif
4987 )
4988 {
4989 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004990 if (hl > HL_ALL)
4991 hl = syn_attr2attr(hl);
4992 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 redraw_next = TRUE;
4994 }
4995#endif
4996 ScreenAttrs[off_to] = ScreenAttrs[off_from];
4997#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004998 /* For simplicity set the attributes of second half of a
4999 * double-wide character equal to the first half. */
5000 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005002
5003 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 else
5006#endif
5007 screen_char(off_to, row, col + coloff);
5008 }
5009 else if ( p_wiv
5010#ifdef FEAT_GUI
5011 && !gui.in_use
5012#endif
5013 && col + coloff > 0)
5014 {
5015 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5016 {
5017 /*
5018 * Don't output stop-highlight when moving the cursor, it will
5019 * stop the highlighting when it should continue.
5020 */
5021 screen_attr = 0;
5022 }
5023 else if (screen_attr != 0)
5024 screen_stop_highlight();
5025 }
5026
5027 off_to += CHAR_CELLS;
5028 off_from += CHAR_CELLS;
5029 col += CHAR_CELLS;
5030 }
5031
5032#ifdef FEAT_MBYTE
5033 if (clear_next)
5034 {
5035 /* Clear the second half of a double-wide character of which the left
5036 * half was overwritten with a single-wide character. */
5037 ScreenLines[off_to] = ' ';
5038 if (enc_utf8)
5039 ScreenLinesUC[off_to] = 0;
5040 screen_char(off_to, row, col + coloff);
5041 }
5042#endif
5043
5044 if (clear_width > 0
5045#ifdef FEAT_RIGHTLEFT
5046 && !rlflag
5047#endif
5048 )
5049 {
5050#ifdef FEAT_GUI
5051 int startCol = col;
5052#endif
5053
5054 /* blank out the rest of the line */
5055 while (col < clear_width && ScreenLines[off_to] == ' '
5056 && ScreenAttrs[off_to] == 0
5057#ifdef FEAT_MBYTE
5058 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5059#endif
5060 )
5061 {
5062 ++off_to;
5063 ++col;
5064 }
5065 if (col < clear_width)
5066 {
5067#ifdef FEAT_GUI
5068 /*
5069 * In the GUI, clearing the rest of the line may leave pixels
5070 * behind if the first character cleared was bold. Some bold
5071 * fonts spill over the left. In this case we redraw the previous
5072 * character too. If we didn't skip any blanks above, then we
5073 * only redraw if the character wasn't already redrawn anyway.
5074 */
5075 if (gui.in_use && (col > startCol || !redraw_this)
5076# ifdef FEAT_MBYTE
5077 && enc_dbcs == 0
5078# endif
5079 )
5080 {
5081 hl = ScreenAttrs[off_to];
5082 if (hl > HL_ALL || (hl & HL_BOLD))
5083 screen_char(off_to - 1, row, col + coloff - 1);
5084 }
5085#endif
5086 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5087 ' ', ' ', 0);
5088#ifdef FEAT_VERTSPLIT
5089 off_to += clear_width - col;
5090 col = clear_width;
5091#endif
5092 }
5093 }
5094
5095 if (clear_width > 0)
5096 {
5097#ifdef FEAT_VERTSPLIT
5098 /* For a window that's left of another, draw the separator char. */
5099 if (col + coloff < Columns)
5100 {
5101 int c;
5102
5103 c = fillchar_vsep(&hl);
5104 if (ScreenLines[off_to] != c
5105# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005106 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5107 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108# endif
5109 || ScreenAttrs[off_to] != hl)
5110 {
5111 ScreenLines[off_to] = c;
5112 ScreenAttrs[off_to] = hl;
5113# ifdef FEAT_MBYTE
5114 if (enc_utf8)
5115 {
5116 if (c >= 0x80)
5117 {
5118 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005119 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 else
5122 ScreenLinesUC[off_to] = 0;
5123 }
5124# endif
5125 screen_char(off_to, row, col + coloff);
5126 }
5127 }
5128 else
5129#endif
5130 LineWraps[row] = FALSE;
5131 }
5132}
5133
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005134#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005136 * Mirror text "str" for right-left displaying.
5137 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005139 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140rl_mirror(str)
5141 char_u *str;
5142{
5143 char_u *p1, *p2;
5144 int t;
5145
5146 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5147 {
5148 t = *p1;
5149 *p1 = *p2;
5150 *p2 = t;
5151 }
5152}
5153#endif
5154
5155#if defined(FEAT_WINDOWS) || defined(PROTO)
5156/*
5157 * mark all status lines for redraw; used after first :cd
5158 */
5159 void
5160status_redraw_all()
5161{
5162 win_T *wp;
5163
5164 for (wp = firstwin; wp; wp = wp->w_next)
5165 if (wp->w_status_height)
5166 {
5167 wp->w_redr_status = TRUE;
5168 redraw_later(VALID);
5169 }
5170}
5171
5172/*
5173 * mark all status lines of the current buffer for redraw
5174 */
5175 void
5176status_redraw_curbuf()
5177{
5178 win_T *wp;
5179
5180 for (wp = firstwin; wp; wp = wp->w_next)
5181 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5182 {
5183 wp->w_redr_status = TRUE;
5184 redraw_later(VALID);
5185 }
5186}
5187
5188/*
5189 * Redraw all status lines that need to be redrawn.
5190 */
5191 void
5192redraw_statuslines()
5193{
5194 win_T *wp;
5195
5196 for (wp = firstwin; wp; wp = wp->w_next)
5197 if (wp->w_redr_status)
5198 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005199 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005200 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201}
5202#endif
5203
5204#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5205/*
5206 * Redraw all status lines at the bottom of frame "frp".
5207 */
5208 void
5209win_redraw_last_status(frp)
5210 frame_T *frp;
5211{
5212 if (frp->fr_layout == FR_LEAF)
5213 frp->fr_win->w_redr_status = TRUE;
5214 else if (frp->fr_layout == FR_ROW)
5215 {
5216 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5217 win_redraw_last_status(frp);
5218 }
5219 else /* frp->fr_layout == FR_COL */
5220 {
5221 frp = frp->fr_child;
5222 while (frp->fr_next != NULL)
5223 frp = frp->fr_next;
5224 win_redraw_last_status(frp);
5225 }
5226}
5227#endif
5228
5229#ifdef FEAT_VERTSPLIT
5230/*
5231 * Draw the verticap separator right of window "wp" starting with line "row".
5232 */
5233 static void
5234draw_vsep_win(wp, row)
5235 win_T *wp;
5236 int row;
5237{
5238 int hl;
5239 int c;
5240
5241 if (wp->w_vsep_width)
5242 {
5243 /* draw the vertical separator right of this window */
5244 c = fillchar_vsep(&hl);
5245 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5246 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5247 c, ' ', hl);
5248 }
5249}
5250#endif
5251
5252#ifdef FEAT_WILDMENU
5253static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005254static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256/*
5257 * Get the lenght of an item as it will be shown in the status line.
5258 */
5259 static int
5260status_match_len(xp, s)
5261 expand_T *xp;
5262 char_u *s;
5263{
5264 int len = 0;
5265
5266#ifdef FEAT_MENU
5267 int emenu = (xp->xp_context == EXPAND_MENUS
5268 || xp->xp_context == EXPAND_MENUNAMES);
5269
5270 /* Check for menu separators - replace with '|'. */
5271 if (emenu && menu_is_separator(s))
5272 return 1;
5273#endif
5274
5275 while (*s != NUL)
5276 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005277 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005279 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005280 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282
5283 return len;
5284}
5285
5286/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005287 * Return TRUE for characters that are not displayed in a status match.
5288 * These are backslashes used for escaping. Do show backslashes in help tags.
5289 */
5290 static int
5291skip_status_match_char(xp, s)
5292 expand_T *xp;
5293 char_u *s;
5294{
5295 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5296#ifdef FEAT_MENU
5297 || ((xp->xp_context == EXPAND_MENUS
5298 || xp->xp_context == EXPAND_MENUNAMES)
5299 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5300#endif
5301 );
5302}
5303
5304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 * Show wildchar matches in the status line.
5306 * Show at least the "match" item.
5307 * We start at item 'first_match' in the list and show all matches that fit.
5308 *
5309 * If inversion is possible we use it. Else '=' characters are used.
5310 */
5311 void
5312win_redr_status_matches(xp, num_matches, matches, match, showtail)
5313 expand_T *xp;
5314 int num_matches;
5315 char_u **matches; /* list of matches */
5316 int match;
5317 int showtail;
5318{
5319#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5320 int row;
5321 char_u *buf;
5322 int len;
5323 int clen; /* lenght in screen cells */
5324 int fillchar;
5325 int attr;
5326 int i;
5327 int highlight = TRUE;
5328 char_u *selstart = NULL;
5329 int selstart_col = 0;
5330 char_u *selend = NULL;
5331 static int first_match = 0;
5332 int add_left = FALSE;
5333 char_u *s;
5334#ifdef FEAT_MENU
5335 int emenu;
5336#endif
5337#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5338 int l;
5339#endif
5340
5341 if (matches == NULL) /* interrupted completion? */
5342 return;
5343
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005344#ifdef FEAT_MBYTE
5345 if (has_mbyte)
5346 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5347 else
5348#endif
5349 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (buf == NULL)
5351 return;
5352
5353 if (match == -1) /* don't show match but original text */
5354 {
5355 match = 0;
5356 highlight = FALSE;
5357 }
5358 /* count 1 for the ending ">" */
5359 clen = status_match_len(xp, L_MATCH(match)) + 3;
5360 if (match == 0)
5361 first_match = 0;
5362 else if (match < first_match)
5363 {
5364 /* jumping left, as far as we can go */
5365 first_match = match;
5366 add_left = TRUE;
5367 }
5368 else
5369 {
5370 /* check if match fits on the screen */
5371 for (i = first_match; i < match; ++i)
5372 clen += status_match_len(xp, L_MATCH(i)) + 2;
5373 if (first_match > 0)
5374 clen += 2;
5375 /* jumping right, put match at the left */
5376 if ((long)clen > Columns)
5377 {
5378 first_match = match;
5379 /* if showing the last match, we can add some on the left */
5380 clen = 2;
5381 for (i = match; i < num_matches; ++i)
5382 {
5383 clen += status_match_len(xp, L_MATCH(i)) + 2;
5384 if ((long)clen >= Columns)
5385 break;
5386 }
5387 if (i == num_matches)
5388 add_left = TRUE;
5389 }
5390 }
5391 if (add_left)
5392 while (first_match > 0)
5393 {
5394 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5395 if ((long)clen >= Columns)
5396 break;
5397 --first_match;
5398 }
5399
5400 fillchar = fillchar_status(&attr, TRUE);
5401
5402 if (first_match == 0)
5403 {
5404 *buf = NUL;
5405 len = 0;
5406 }
5407 else
5408 {
5409 STRCPY(buf, "< ");
5410 len = 2;
5411 }
5412 clen = len;
5413
5414 i = first_match;
5415 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5416 {
5417 if (i == match)
5418 {
5419 selstart = buf + len;
5420 selstart_col = clen;
5421 }
5422
5423 s = L_MATCH(i);
5424 /* Check for menu separators - replace with '|' */
5425#ifdef FEAT_MENU
5426 emenu = (xp->xp_context == EXPAND_MENUS
5427 || xp->xp_context == EXPAND_MENUNAMES);
5428 if (emenu && menu_is_separator(s))
5429 {
5430 STRCPY(buf + len, transchar('|'));
5431 l = (int)STRLEN(buf + len);
5432 len += l;
5433 clen += l;
5434 }
5435 else
5436#endif
5437 for ( ; *s != NUL; ++s)
5438 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005439 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 ++s;
5441 clen += ptr2cells(s);
5442#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005443 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
5445 STRNCPY(buf + len, s, l);
5446 s += l - 1;
5447 len += l;
5448 }
5449 else
5450#endif
5451 {
5452 STRCPY(buf + len, transchar_byte(*s));
5453 len += (int)STRLEN(buf + len);
5454 }
5455 }
5456 if (i == match)
5457 selend = buf + len;
5458
5459 *(buf + len++) = ' ';
5460 *(buf + len++) = ' ';
5461 clen += 2;
5462 if (++i == num_matches)
5463 break;
5464 }
5465
5466 if (i != num_matches)
5467 {
5468 *(buf + len++) = '>';
5469 ++clen;
5470 }
5471
5472 buf[len] = NUL;
5473
5474 row = cmdline_row - 1;
5475 if (row >= 0)
5476 {
5477 if (wild_menu_showing == 0)
5478 {
5479 if (msg_scrolled > 0)
5480 {
5481 /* Put the wildmenu just above the command line. If there is
5482 * no room, scroll the screen one line up. */
5483 if (cmdline_row == Rows - 1)
5484 {
5485 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5486 ++msg_scrolled;
5487 }
5488 else
5489 {
5490 ++cmdline_row;
5491 ++row;
5492 }
5493 wild_menu_showing = WM_SCROLLED;
5494 }
5495 else
5496 {
5497 /* Create status line if needed by setting 'laststatus' to 2.
5498 * Set 'winminheight' to zero to avoid that the window is
5499 * resized. */
5500 if (lastwin->w_status_height == 0)
5501 {
5502 save_p_ls = p_ls;
5503 save_p_wmh = p_wmh;
5504 p_ls = 2;
5505 p_wmh = 0;
5506 last_status(FALSE);
5507 }
5508 wild_menu_showing = WM_SHOWN;
5509 }
5510 }
5511
5512 screen_puts(buf, row, 0, attr);
5513 if (selstart != NULL && highlight)
5514 {
5515 *selend = NUL;
5516 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5517 }
5518
5519 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5520 }
5521
5522#ifdef FEAT_VERTSPLIT
5523 win_redraw_last_status(topframe);
5524#else
5525 lastwin->w_redr_status = TRUE;
5526#endif
5527 vim_free(buf);
5528}
5529#endif
5530
5531#if defined(FEAT_WINDOWS) || defined(PROTO)
5532/*
5533 * Redraw the status line of window wp.
5534 *
5535 * If inversion is possible we use it. Else '=' characters are used.
5536 */
5537 void
5538win_redr_status(wp)
5539 win_T *wp;
5540{
5541 int row;
5542 char_u *p;
5543 int len;
5544 int fillchar;
5545 int attr;
5546 int this_ru_col;
5547
5548 wp->w_redr_status = FALSE;
5549 if (wp->w_status_height == 0)
5550 {
5551 /* no status line, can only be last window */
5552 redraw_cmdline = TRUE;
5553 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005554 else if (!redrawing()
5555#ifdef FEAT_INS_EXPAND
5556 /* don't update status line when popup menu is visible and may be
5557 * drawn over it */
5558 || pum_visible()
5559#endif
5560 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
5562 /* Don't redraw right now, do it later. */
5563 wp->w_redr_status = TRUE;
5564 }
5565#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005566 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
5568 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005569 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571#endif
5572 else
5573 {
5574 fillchar = fillchar_status(&attr, wp == curwin);
5575
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005576 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 p = NameBuff;
5578 len = (int)STRLEN(p);
5579
5580 if (wp->w_buffer->b_help
5581#ifdef FEAT_QUICKFIX
5582 || wp->w_p_pvw
5583#endif
5584 || bufIsChanged(wp->w_buffer)
5585 || wp->w_buffer->b_p_ro)
5586 *(p + len++) = ' ';
5587 if (wp->w_buffer->b_help)
5588 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005589 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 len += (int)STRLEN(p + len);
5591 }
5592#ifdef FEAT_QUICKFIX
5593 if (wp->w_p_pvw)
5594 {
5595 STRCPY(p + len, _("[Preview]"));
5596 len += (int)STRLEN(p + len);
5597 }
5598#endif
5599 if (bufIsChanged(wp->w_buffer))
5600 {
5601 STRCPY(p + len, "[+]");
5602 len += 3;
5603 }
5604 if (wp->w_buffer->b_p_ro)
5605 {
5606 STRCPY(p + len, "[RO]");
5607 len += 4;
5608 }
5609
5610#ifndef FEAT_VERTSPLIT
5611 this_ru_col = ru_col;
5612 if (this_ru_col < (Columns + 1) / 2)
5613 this_ru_col = (Columns + 1) / 2;
5614#else
5615 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5616 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5617 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5618 if (this_ru_col <= 1)
5619 {
5620 p = (char_u *)"<"; /* No room for file name! */
5621 len = 1;
5622 }
5623 else
5624#endif
5625#ifdef FEAT_MBYTE
5626 if (has_mbyte)
5627 {
5628 int clen = 0, i;
5629
5630 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005631 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 clen += (*mb_ptr2cells)(p + i);
5633 /* Find first character that will fit.
5634 * Going from start to end is much faster for DBCS. */
5635 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005636 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 clen -= (*mb_ptr2cells)(p + i);
5638 len = clen;
5639 if (i > 0)
5640 {
5641 p = p + i - 1;
5642 *p = '<';
5643 ++len;
5644 }
5645
5646 }
5647 else
5648#endif
5649 if (len > this_ru_col - 1)
5650 {
5651 p += len - (this_ru_col - 1);
5652 *p = '<';
5653 len = this_ru_col - 1;
5654 }
5655
5656 row = W_WINROW(wp) + wp->w_height;
5657 screen_puts(p, row, W_WINCOL(wp), attr);
5658 screen_fill(row, row + 1, len + W_WINCOL(wp),
5659 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5660
5661 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5662 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5663 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5664 - 1 + W_WINCOL(wp)), attr);
5665
5666#ifdef FEAT_CMDL_INFO
5667 win_redr_ruler(wp, TRUE);
5668#endif
5669 }
5670
5671#ifdef FEAT_VERTSPLIT
5672 /*
5673 * May need to draw the character below the vertical separator.
5674 */
5675 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5676 {
5677 if (stl_connected(wp))
5678 fillchar = fillchar_status(&attr, wp == curwin);
5679 else
5680 fillchar = fillchar_vsep(&attr);
5681 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5682 attr);
5683 }
5684#endif
5685}
5686
Bram Moolenaar238a5642006-02-21 22:12:05 +00005687#ifdef FEAT_STL_OPT
5688/*
5689 * Redraw the status line according to 'statusline' and take care of any
5690 * errors encountered.
5691 */
5692 static void
5693redraw_custum_statusline(wp)
5694 win_T *wp;
5695{
5696 int save_called_emsg = called_emsg;
5697
5698 called_emsg = FALSE;
5699 win_redr_custom(wp, FALSE);
5700 if (called_emsg)
5701 set_string_option_direct((char_u *)"statusline", -1,
5702 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005703 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005704 called_emsg |= save_called_emsg;
5705}
5706#endif
5707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708# ifdef FEAT_VERTSPLIT
5709/*
5710 * Return TRUE if the status line of window "wp" is connected to the status
5711 * line of the window right of it. If not, then it's a vertical separator.
5712 * Only call if (wp->w_vsep_width != 0).
5713 */
5714 int
5715stl_connected(wp)
5716 win_T *wp;
5717{
5718 frame_T *fr;
5719
5720 fr = wp->w_frame;
5721 while (fr->fr_parent != NULL)
5722 {
5723 if (fr->fr_parent->fr_layout == FR_COL)
5724 {
5725 if (fr->fr_next != NULL)
5726 break;
5727 }
5728 else
5729 {
5730 if (fr->fr_next != NULL)
5731 return TRUE;
5732 }
5733 fr = fr->fr_parent;
5734 }
5735 return FALSE;
5736}
5737# endif
5738
5739#endif /* FEAT_WINDOWS */
5740
5741#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5742/*
5743 * Get the value to show for the language mappings, active 'keymap'.
5744 */
5745 int
5746get_keymap_str(wp, buf, len)
5747 win_T *wp;
5748 char_u *buf; /* buffer for the result */
5749 int len; /* length of buffer */
5750{
5751 char_u *p;
5752
5753 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5754 return FALSE;
5755
5756 {
5757#ifdef FEAT_EVAL
5758 buf_T *old_curbuf = curbuf;
5759 win_T *old_curwin = curwin;
5760 char_u *s;
5761
5762 curbuf = wp->w_buffer;
5763 curwin = wp;
5764 STRCPY(buf, "b:keymap_name"); /* must be writable */
5765 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005766 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 --emsg_skip;
5768 curbuf = old_curbuf;
5769 curwin = old_curwin;
5770 if (p == NULL || *p == NUL)
5771#endif
5772 {
5773#ifdef FEAT_KEYMAP
5774 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5775 p = wp->w_buffer->b_p_keymap;
5776 else
5777#endif
5778 p = (char_u *)"lang";
5779 }
5780 if ((int)(STRLEN(p) + 3) < len)
5781 sprintf((char *)buf, "<%s>", p);
5782 else
5783 buf[0] = NUL;
5784#ifdef FEAT_EVAL
5785 vim_free(s);
5786#endif
5787 }
5788 return buf[0] != NUL;
5789}
5790#endif
5791
5792#if defined(FEAT_STL_OPT) || defined(PROTO)
5793/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005794 * Redraw the status line or ruler of window "wp".
5795 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 */
5797 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005798win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005800 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801{
5802 int attr;
5803 int curattr;
5804 int row;
5805 int col = 0;
5806 int maxwidth;
5807 int width;
5808 int n;
5809 int len;
5810 int fillchar;
5811 char_u buf[MAXPATHL];
5812 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005813 struct stl_hlrec hltab[STL_MAX_ITEM];
5814 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005815 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
5817 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005818 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005820 /* Use 'tabline'. Always at the first line of the screen. */
5821 p = p_tal;
5822 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005823 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005824 attr = hl_attr(HLF_TPF);
5825 maxwidth = Columns;
5826# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005827 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005830 else
5831 {
5832 row = W_WINROW(wp) + wp->w_height;
5833 fillchar = fillchar_status(&attr, wp == curwin);
5834 maxwidth = W_WIDTH(wp);
5835
5836 if (draw_ruler)
5837 {
5838 p = p_ruf;
5839 /* advance past any leading group spec - implicit in ru_col */
5840 if (*p == '%')
5841 {
5842 if (*++p == '-')
5843 p++;
5844 if (atoi((char *) p))
5845 while (VIM_ISDIGIT(*p))
5846 p++;
5847 if (*p++ != '(')
5848 p = p_ruf;
5849 }
5850#ifdef FEAT_VERTSPLIT
5851 col = ru_col - (Columns - W_WIDTH(wp));
5852 if (col < (W_WIDTH(wp) + 1) / 2)
5853 col = (W_WIDTH(wp) + 1) / 2;
5854#else
5855 col = ru_col;
5856 if (col > (Columns + 1) / 2)
5857 col = (Columns + 1) / 2;
5858#endif
5859 maxwidth = W_WIDTH(wp) - col;
5860#ifdef FEAT_WINDOWS
5861 if (!wp->w_status_height)
5862#endif
5863 {
5864 row = Rows - 1;
5865 --maxwidth; /* writing in last column may cause scrolling */
5866 fillchar = ' ';
5867 attr = 0;
5868 }
5869
5870# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005871 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005872# endif
5873 }
5874 else
5875 {
5876 if (*wp->w_p_stl != NUL)
5877 p = wp->w_p_stl;
5878 else
5879 p = p_stl;
5880# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005881 use_sandbox = was_set_insecurely((char_u *)"statusline",
5882 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005883# endif
5884 }
5885
5886#ifdef FEAT_VERTSPLIT
5887 col += W_WINCOL(wp);
5888#endif
5889 }
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 if (maxwidth <= 0)
5892 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005894 width = build_stl_str_hl(wp == NULL ? curwin : wp,
5895 buf, sizeof(buf),
5896 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005897 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005898 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
5900 while (width < maxwidth && len < sizeof(buf) - 1)
5901 {
5902#ifdef FEAT_MBYTE
5903 len += (*mb_char2bytes)(fillchar, buf + len);
5904#else
5905 buf[len++] = fillchar;
5906#endif
5907 ++width;
5908 }
5909 buf[len] = NUL;
5910
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005911 /*
5912 * Draw each snippet with the specified highlighting.
5913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 curattr = attr;
5915 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005916 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005918 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 screen_puts_len(p, len, row, col, curattr);
5920 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005921 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005923 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005925 else if (hltab[n].userhl < 0)
5926 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00005928 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005929 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930#endif
5931 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005932 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 }
5934 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005935
5936 if (wp == NULL)
5937 {
5938 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
5939 col = 0;
5940 len = 0;
5941 p = buf;
5942 fillchar = 0;
5943 for (n = 0; tabtab[n].start != NULL; n++)
5944 {
5945 len += vim_strnsize(p, (int)(tabtab[n].start - p));
5946 while (col < len)
5947 TabPageIdxs[col++] = fillchar;
5948 p = tabtab[n].start;
5949 fillchar = tabtab[n].userhl;
5950 }
5951 while (col < Columns)
5952 TabPageIdxs[col++] = fillchar;
5953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954}
5955
5956#endif /* FEAT_STL_OPT */
5957
5958/*
5959 * Output a single character directly to the screen and update ScreenLines.
5960 */
5961 void
5962screen_putchar(c, row, col, attr)
5963 int c;
5964 int row, col;
5965 int attr;
5966{
5967#ifdef FEAT_MBYTE
5968 char_u buf[MB_MAXBYTES + 1];
5969
5970 buf[(*mb_char2bytes)(c, buf)] = NUL;
5971#else
5972 char_u buf[2];
5973
5974 buf[0] = c;
5975 buf[1] = NUL;
5976#endif
5977 screen_puts(buf, row, col, attr);
5978}
5979
5980/*
5981 * Get a single character directly from ScreenLines into "bytes[]".
5982 * Also return its attribute in *attrp;
5983 */
5984 void
5985screen_getbytes(row, col, bytes, attrp)
5986 int row, col;
5987 char_u *bytes;
5988 int *attrp;
5989{
5990 unsigned off;
5991
5992 /* safety check */
5993 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
5994 {
5995 off = LineOffset[row] + col;
5996 *attrp = ScreenAttrs[off];
5997 bytes[0] = ScreenLines[off];
5998 bytes[1] = NUL;
5999
6000#ifdef FEAT_MBYTE
6001 if (enc_utf8 && ScreenLinesUC[off] != 0)
6002 bytes[utfc_char2bytes(off, bytes)] = NUL;
6003 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6004 {
6005 bytes[0] = ScreenLines[off];
6006 bytes[1] = ScreenLines2[off];
6007 bytes[2] = NUL;
6008 }
6009 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6010 {
6011 bytes[1] = ScreenLines[off + 1];
6012 bytes[2] = NUL;
6013 }
6014#endif
6015 }
6016}
6017
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006018#ifdef FEAT_MBYTE
6019static int screen_comp_differs __ARGS((int, int*));
6020
6021/*
6022 * Return TRUE if composing characters for screen posn "off" differs from
6023 * composing characters in "u8cc".
6024 */
6025 static int
6026screen_comp_differs(off, u8cc)
6027 int off;
6028 int *u8cc;
6029{
6030 int i;
6031
6032 for (i = 0; i < Screen_mco; ++i)
6033 {
6034 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6035 return TRUE;
6036 if (u8cc[i] == 0)
6037 break;
6038 }
6039 return FALSE;
6040}
6041#endif
6042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043/*
6044 * Put string '*text' on the screen at position 'row' and 'col', with
6045 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6046 * Note: only outputs within one row, message is truncated at screen boundary!
6047 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6048 */
6049 void
6050screen_puts(text, row, col, attr)
6051 char_u *text;
6052 int row;
6053 int col;
6054 int attr;
6055{
6056 screen_puts_len(text, -1, row, col, attr);
6057}
6058
6059/*
6060 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6061 * a NUL.
6062 */
6063 void
6064screen_puts_len(text, len, row, col, attr)
6065 char_u *text;
6066 int len;
6067 int row;
6068 int col;
6069 int attr;
6070{
6071 unsigned off;
6072 char_u *ptr = text;
6073 int c;
6074#ifdef FEAT_MBYTE
6075 int mbyte_blen = 1;
6076 int mbyte_cells = 1;
6077 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006078 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 int clear_next_cell = FALSE;
6080# ifdef FEAT_ARABIC
6081 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006082 int pc, nc, nc1;
6083 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084# endif
6085#endif
6086
6087 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6088 return;
6089
6090 off = LineOffset[row] + col;
6091 while (*ptr != NUL && col < screen_Columns
6092 && (len < 0 || (int)(ptr - text) < len))
6093 {
6094 c = *ptr;
6095#ifdef FEAT_MBYTE
6096 /* check if this is the first byte of a multibyte */
6097 if (has_mbyte)
6098 {
6099 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006100 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006102 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6104 mbyte_cells = 1;
6105 else if (enc_dbcs != 0)
6106 mbyte_cells = mbyte_blen;
6107 else /* enc_utf8 */
6108 {
6109 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006110 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 (int)((text + len) - ptr));
6112 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006113 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 mbyte_cells = utf_char2cells(u8c);
6115 /* Non-BMP character: display as ? or fullwidth ?. */
6116 if (u8c >= 0x10000)
6117 {
6118 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6119 if (attr == 0)
6120 attr = hl_attr(HLF_8);
6121 }
6122# ifdef FEAT_ARABIC
6123 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6124 {
6125 /* Do Arabic shaping. */
6126 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6127 {
6128 /* Past end of string to be displayed. */
6129 nc = NUL;
6130 nc1 = NUL;
6131 }
6132 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006133 {
6134 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6135 nc1 = pcc[0];
6136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 pc = prev_c;
6138 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006139 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 }
6141 else
6142 prev_c = u8c;
6143# endif
6144 }
6145 }
6146#endif
6147
6148 if (ScreenLines[off] != c
6149#ifdef FEAT_MBYTE
6150 || (mbyte_cells == 2
6151 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6152 || (enc_dbcs == DBCS_JPNU
6153 && c == 0x8e
6154 && ScreenLines2[off] != ptr[1])
6155 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006156 && (ScreenLinesUC[off] != (u8char_T)u8c
6157 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158#endif
6159 || ScreenAttrs[off] != attr
6160 || exmode_active
6161 )
6162 {
6163#if defined(FEAT_GUI) || defined(UNIX)
6164 /* The bold trick makes a single row of pixels appear in the next
6165 * character. When a bold character is removed, the next
6166 * character should be redrawn too. This happens for our own GUI
6167 * and for some xterms.
6168 * Force the redraw by setting the attribute to a different value
6169 * than "attr", the contents of ScreenLines[] may be needed by
6170 * mb_off2cells() further on.
6171 * Don't do this for the last drawn character, because the next
6172 * character may not be redrawn. */
6173 if (
6174# ifdef FEAT_GUI
6175 gui.in_use
6176# endif
6177# if defined(FEAT_GUI) && defined(UNIX)
6178 ||
6179# endif
6180# ifdef UNIX
6181 term_is_xterm
6182# endif
6183 )
6184 {
6185 int n;
6186
6187 n = ScreenAttrs[off];
6188# ifdef FEAT_MBYTE
6189 if (col + mbyte_cells < screen_Columns
6190 && (n > HL_ALL || (n & HL_BOLD))
6191 && (len < 0 ? ptr[mbyte_blen] != NUL
6192 : ptr + mbyte_blen < text + len))
6193 ScreenAttrs[off + mbyte_cells] = attr + 1;
6194# else
6195 if (col + 1 < screen_Columns
6196 && (n > HL_ALL || (n & HL_BOLD))
6197 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6198 ScreenLines[off + 1] = 0;
6199# endif
6200 }
6201#endif
6202#ifdef FEAT_MBYTE
6203 /* When at the end of the text and overwriting a two-cell
6204 * character with a one-cell character, need to clear the next
6205 * cell. Also when overwriting the left halve of a two-cell char
6206 * with the right halve of a two-cell char. Do this only once
6207 * (mb_off2cells() may return 2 on the right halve). */
6208 if (clear_next_cell)
6209 clear_next_cell = FALSE;
6210 else if (has_mbyte
6211 && (len < 0 ? ptr[mbyte_blen] == NUL
6212 : ptr + mbyte_blen >= text + len)
6213 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6214 || (mbyte_cells == 2
6215 && (*mb_off2cells)(off) == 1
6216 && (*mb_off2cells)(off + 1) > 1)))
6217 clear_next_cell = TRUE;
6218
6219 /* Make sure we never leave a second byte of a double-byte behind,
6220 * it confuses mb_off2cells(). */
6221 if (enc_dbcs
6222 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6223 || (mbyte_cells == 2
6224 && (*mb_off2cells)(off) == 1
6225 && (*mb_off2cells)(off + 1) > 1)))
6226 ScreenLines[off + mbyte_blen] = 0;
6227#endif
6228 ScreenLines[off] = c;
6229 ScreenAttrs[off] = attr;
6230#ifdef FEAT_MBYTE
6231 if (enc_utf8)
6232 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006233 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 ScreenLinesUC[off] = 0;
6235 else
6236 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006237 int i;
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006240 for (i = 0; i < Screen_mco; ++i)
6241 {
6242 ScreenLinesC[i][off] = u8cc[i];
6243 if (u8cc[i] == 0)
6244 break;
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
6247 if (mbyte_cells == 2)
6248 {
6249 ScreenLines[off + 1] = 0;
6250 ScreenAttrs[off + 1] = attr;
6251 }
6252 screen_char(off, row, col);
6253 }
6254 else if (mbyte_cells == 2)
6255 {
6256 ScreenLines[off + 1] = ptr[1];
6257 ScreenAttrs[off + 1] = attr;
6258 screen_char_2(off, row, col);
6259 }
6260 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6261 {
6262 ScreenLines2[off] = ptr[1];
6263 screen_char(off, row, col);
6264 }
6265 else
6266#endif
6267 screen_char(off, row, col);
6268 }
6269#ifdef FEAT_MBYTE
6270 if (has_mbyte)
6271 {
6272 off += mbyte_cells;
6273 col += mbyte_cells;
6274 ptr += mbyte_blen;
6275 if (clear_next_cell)
6276 ptr = (char_u *)" ";
6277 }
6278 else
6279#endif
6280 {
6281 ++off;
6282 ++col;
6283 ++ptr;
6284 }
6285 }
6286}
6287
6288#ifdef FEAT_SEARCH_EXTRA
6289/*
6290 * Prepare for 'searchhl' highlighting.
6291 */
6292 static void
6293start_search_hl()
6294{
6295 if (p_hls && !no_hlsearch)
6296 {
6297 last_pat_prog(&search_hl.rm);
6298 search_hl.attr = hl_attr(HLF_L);
6299 }
6300}
6301
6302/*
6303 * Clean up for 'searchhl' highlighting.
6304 */
6305 static void
6306end_search_hl()
6307{
6308 if (search_hl.rm.regprog != NULL)
6309 {
6310 vim_free(search_hl.rm.regprog);
6311 search_hl.rm.regprog = NULL;
6312 }
6313}
6314
6315/*
6316 * Advance to the match in window "wp" line "lnum" or past it.
6317 */
6318 static void
6319prepare_search_hl(wp, lnum)
6320 win_T *wp;
6321 linenr_T lnum;
6322{
6323 match_T *shl; /* points to search_hl or match_hl */
6324 int n;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006325 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
6327 /*
6328 * When using a multi-line pattern, start searching at the top
6329 * of the window or just after a closed fold.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006330 * Do this both for search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006332 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006334 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 if (shl->rm.regprog != NULL
6336 && shl->lnum == 0
6337 && re_multiline(shl->rm.regprog))
6338 {
6339 if (shl->first_lnum == 0)
6340 {
6341# ifdef FEAT_FOLDING
6342 for (shl->first_lnum = lnum;
6343 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6344 if (hasFoldingWin(wp, shl->first_lnum - 1,
6345 NULL, NULL, TRUE, NULL))
6346 break;
6347# else
6348 shl->first_lnum = wp->w_topline;
6349# endif
6350 }
6351 n = 0;
6352 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6353 {
6354 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6355 if (shl->lnum != 0)
6356 {
6357 shl->first_lnum = shl->lnum
6358 + shl->rm.endpos[0].lnum
6359 - shl->rm.startpos[0].lnum;
6360 n = shl->rm.endpos[0].col;
6361 }
6362 else
6363 {
6364 ++shl->first_lnum;
6365 n = 0;
6366 }
6367 }
6368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
6370}
6371
6372/*
6373 * Search for a next 'searchl' or ":match" match.
6374 * Uses shl->buf.
6375 * Sets shl->lnum and shl->rm contents.
6376 * Note: Assumes a previous match is always before "lnum", unless
6377 * shl->lnum is zero.
6378 * Careful: Any pointers for buffer lines will become invalid.
6379 */
6380 static void
6381next_search_hl(win, shl, lnum, mincol)
6382 win_T *win;
6383 match_T *shl; /* points to search_hl or match_hl */
6384 linenr_T lnum;
6385 colnr_T mincol; /* minimal column for a match */
6386{
6387 linenr_T l;
6388 colnr_T matchcol;
6389 long nmatched;
6390
6391 if (shl->lnum != 0)
6392 {
6393 /* Check for three situations:
6394 * 1. If the "lnum" is below a previous match, start a new search.
6395 * 2. If the previous match includes "mincol", use it.
6396 * 3. Continue after the previous match.
6397 */
6398 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6399 if (lnum > l)
6400 shl->lnum = 0;
6401 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6402 return;
6403 }
6404
6405 /*
6406 * Repeat searching for a match until one is found that includes "mincol"
6407 * or none is found in this line.
6408 */
6409 called_emsg = FALSE;
6410 for (;;)
6411 {
6412 /* Three situations:
6413 * 1. No useful previous match: search from start of line.
6414 * 2. Not Vi compatible or empty match: continue at next character.
6415 * Break the loop if this is beyond the end of the line.
6416 * 3. Vi compatible searching: continue at end of previous match.
6417 */
6418 if (shl->lnum == 0)
6419 matchcol = 0;
6420 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6421 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006422 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006424 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006425
6426 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006427 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006428 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006430 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 shl->lnum = 0;
6432 break;
6433 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006434#ifdef FEAT_MBYTE
6435 if (has_mbyte)
6436 matchcol += mb_ptr2len(ml);
6437 else
6438#endif
6439 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
6441 else
6442 matchcol = shl->rm.endpos[0].col;
6443
6444 shl->lnum = lnum;
6445 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6446 if (called_emsg)
6447 {
6448 /* Error while handling regexp: stop using this regexp. */
6449 vim_free(shl->rm.regprog);
6450 shl->rm.regprog = NULL;
6451 no_hlsearch = TRUE;
6452 break;
6453 }
6454 if (nmatched == 0)
6455 {
6456 shl->lnum = 0; /* no match found */
6457 break;
6458 }
6459 if (shl->rm.startpos[0].lnum > 0
6460 || shl->rm.startpos[0].col >= mincol
6461 || nmatched > 1
6462 || shl->rm.endpos[0].col > mincol)
6463 {
6464 shl->lnum += shl->rm.startpos[0].lnum;
6465 break; /* useful match found */
6466 }
6467 }
6468}
6469#endif
6470
6471 static void
6472screen_start_highlight(attr)
6473 int attr;
6474{
6475 attrentry_T *aep = NULL;
6476
6477 screen_attr = attr;
6478 if (full_screen
6479#ifdef WIN3264
6480 && termcap_active
6481#endif
6482 )
6483 {
6484#ifdef FEAT_GUI
6485 if (gui.in_use)
6486 {
6487 char buf[20];
6488
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006489 /* The GUI handles this internally. */
6490 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 OUT_STR(buf);
6492 }
6493 else
6494#endif
6495 {
6496 if (attr > HL_ALL) /* special HL attr. */
6497 {
6498 if (t_colors > 1)
6499 aep = syn_cterm_attr2entry(attr);
6500 else
6501 aep = syn_term_attr2entry(attr);
6502 if (aep == NULL) /* did ":syntax clear" */
6503 attr = 0;
6504 else
6505 attr = aep->ae_attr;
6506 }
6507 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6508 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006509 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6510 && cterm_normal_fg_bold)
6511 /* If the Normal FG color has BOLD attribute and the new HL
6512 * has a FG color defined, clear BOLD. */
6513 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6515 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006516 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6517 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 out_str(T_US);
6519 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6520 out_str(T_CZH);
6521 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6522 out_str(T_MR);
6523
6524 /*
6525 * Output the color or start string after bold etc., in case the
6526 * bold etc. override the color setting.
6527 */
6528 if (aep != NULL)
6529 {
6530 if (t_colors > 1)
6531 {
6532 if (aep->ae_u.cterm.fg_color)
6533 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6534 if (aep->ae_u.cterm.bg_color)
6535 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6536 }
6537 else
6538 {
6539 if (aep->ae_u.term.start != NULL)
6540 out_str(aep->ae_u.term.start);
6541 }
6542 }
6543 }
6544 }
6545}
6546
6547 void
6548screen_stop_highlight()
6549{
6550 int do_ME = FALSE; /* output T_ME code */
6551
6552 if (screen_attr != 0
6553#ifdef WIN3264
6554 && termcap_active
6555#endif
6556 )
6557 {
6558#ifdef FEAT_GUI
6559 if (gui.in_use)
6560 {
6561 char buf[20];
6562
6563 /* use internal GUI code */
6564 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6565 OUT_STR(buf);
6566 }
6567 else
6568#endif
6569 {
6570 if (screen_attr > HL_ALL) /* special HL attr. */
6571 {
6572 attrentry_T *aep;
6573
6574 if (t_colors > 1)
6575 {
6576 /*
6577 * Assume that t_me restores the original colors!
6578 */
6579 aep = syn_cterm_attr2entry(screen_attr);
6580 if (aep != NULL && (aep->ae_u.cterm.fg_color
6581 || aep->ae_u.cterm.bg_color))
6582 do_ME = TRUE;
6583 }
6584 else
6585 {
6586 aep = syn_term_attr2entry(screen_attr);
6587 if (aep != NULL && aep->ae_u.term.stop != NULL)
6588 {
6589 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6590 do_ME = TRUE;
6591 else
6592 out_str(aep->ae_u.term.stop);
6593 }
6594 }
6595 if (aep == NULL) /* did ":syntax clear" */
6596 screen_attr = 0;
6597 else
6598 screen_attr = aep->ae_attr;
6599 }
6600
6601 /*
6602 * Often all ending-codes are equal to T_ME. Avoid outputting the
6603 * same sequence several times.
6604 */
6605 if (screen_attr & HL_STANDOUT)
6606 {
6607 if (STRCMP(T_SE, T_ME) == 0)
6608 do_ME = TRUE;
6609 else
6610 out_str(T_SE);
6611 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006612 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 {
6614 if (STRCMP(T_UE, T_ME) == 0)
6615 do_ME = TRUE;
6616 else
6617 out_str(T_UE);
6618 }
6619 if (screen_attr & HL_ITALIC)
6620 {
6621 if (STRCMP(T_CZR, T_ME) == 0)
6622 do_ME = TRUE;
6623 else
6624 out_str(T_CZR);
6625 }
6626 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6627 out_str(T_ME);
6628
6629 if (t_colors > 1)
6630 {
6631 /* set Normal cterm colors */
6632 if (cterm_normal_fg_color != 0)
6633 term_fg_color(cterm_normal_fg_color - 1);
6634 if (cterm_normal_bg_color != 0)
6635 term_bg_color(cterm_normal_bg_color - 1);
6636 if (cterm_normal_fg_bold)
6637 out_str(T_MD);
6638 }
6639 }
6640 }
6641 screen_attr = 0;
6642}
6643
6644/*
6645 * Reset the colors for a cterm. Used when leaving Vim.
6646 * The machine specific code may override this again.
6647 */
6648 void
6649reset_cterm_colors()
6650{
6651 if (t_colors > 1)
6652 {
6653 /* set Normal cterm colors */
6654 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6655 {
6656 out_str(T_OP);
6657 screen_attr = -1;
6658 }
6659 if (cterm_normal_fg_bold)
6660 {
6661 out_str(T_ME);
6662 screen_attr = -1;
6663 }
6664 }
6665}
6666
6667/*
6668 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6669 * using the attributes from ScreenAttrs["off"].
6670 */
6671 static void
6672screen_char(off, row, col)
6673 unsigned off;
6674 int row;
6675 int col;
6676{
6677 int attr;
6678
6679 /* Check for illegal values, just in case (could happen just after
6680 * resizing). */
6681 if (row >= screen_Rows || col >= screen_Columns)
6682 return;
6683
6684 /* Outputting the last character on the screen may scrollup the screen.
6685 * Don't to it! Mark the character invalid (update it when scrolled up) */
6686 if (row == screen_Rows - 1 && col == screen_Columns - 1
6687#ifdef FEAT_RIGHTLEFT
6688 /* account for first command-line character in rightleft mode */
6689 && !cmdmsg_rl
6690#endif
6691 )
6692 {
6693 ScreenAttrs[off] = (sattr_T)-1;
6694 return;
6695 }
6696
6697 /*
6698 * Stop highlighting first, so it's easier to move the cursor.
6699 */
6700#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6701 if (screen_char_attr != 0)
6702 attr = screen_char_attr;
6703 else
6704#endif
6705 attr = ScreenAttrs[off];
6706 if (screen_attr != attr)
6707 screen_stop_highlight();
6708
6709 windgoto(row, col);
6710
6711 if (screen_attr != attr)
6712 screen_start_highlight(attr);
6713
6714#ifdef FEAT_MBYTE
6715 if (enc_utf8 && ScreenLinesUC[off] != 0)
6716 {
6717 char_u buf[MB_MAXBYTES + 1];
6718
6719 /* Convert UTF-8 character to bytes and write it. */
6720
6721 buf[utfc_char2bytes(off, buf)] = NUL;
6722
6723 out_str(buf);
6724 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6725 ++screen_cur_col;
6726 }
6727 else
6728#endif
6729 {
6730#ifdef FEAT_MBYTE
6731 out_flush_check();
6732#endif
6733 out_char(ScreenLines[off]);
6734#ifdef FEAT_MBYTE
6735 /* double-byte character in single-width cell */
6736 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6737 out_char(ScreenLines2[off]);
6738#endif
6739 }
6740
6741 screen_cur_col++;
6742}
6743
6744#ifdef FEAT_MBYTE
6745
6746/*
6747 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6748 * on the screen at position 'row' and 'col'.
6749 * The attributes of the first byte is used for all. This is required to
6750 * output the two bytes of a double-byte character with nothing in between.
6751 */
6752 static void
6753screen_char_2(off, row, col)
6754 unsigned off;
6755 int row;
6756 int col;
6757{
6758 /* Check for illegal values (could be wrong when screen was resized). */
6759 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6760 return;
6761
6762 /* Outputting the last character on the screen may scrollup the screen.
6763 * Don't to it! Mark the character invalid (update it when scrolled up) */
6764 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6765 {
6766 ScreenAttrs[off] = (sattr_T)-1;
6767 return;
6768 }
6769
6770 /* Output the first byte normally (positions the cursor), then write the
6771 * second byte directly. */
6772 screen_char(off, row, col);
6773 out_char(ScreenLines[off + 1]);
6774 ++screen_cur_col;
6775}
6776#endif
6777
6778#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6779/*
6780 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6781 * This uses the contents of ScreenLines[] and doesn't change it.
6782 */
6783 void
6784screen_draw_rectangle(row, col, height, width, invert)
6785 int row;
6786 int col;
6787 int height;
6788 int width;
6789 int invert;
6790{
6791 int r, c;
6792 int off;
6793
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006794 /* Can't use ScreenLines unless initialized */
6795 if (ScreenLines == NULL)
6796 return;
6797
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 if (invert)
6799 screen_char_attr = HL_INVERSE;
6800 for (r = row; r < row + height; ++r)
6801 {
6802 off = LineOffset[r];
6803 for (c = col; c < col + width; ++c)
6804 {
6805#ifdef FEAT_MBYTE
6806 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6807 {
6808 screen_char_2(off + c, r, c);
6809 ++c;
6810 }
6811 else
6812#endif
6813 {
6814 screen_char(off + c, r, c);
6815#ifdef FEAT_MBYTE
6816 if (utf_off2cells(off + c) > 1)
6817 ++c;
6818#endif
6819 }
6820 }
6821 }
6822 screen_char_attr = 0;
6823}
6824#endif
6825
6826#ifdef FEAT_VERTSPLIT
6827/*
6828 * Redraw the characters for a vertically split window.
6829 */
6830 static void
6831redraw_block(row, end, wp)
6832 int row;
6833 int end;
6834 win_T *wp;
6835{
6836 int col;
6837 int width;
6838
6839# ifdef FEAT_CLIPBOARD
6840 clip_may_clear_selection(row, end - 1);
6841# endif
6842
6843 if (wp == NULL)
6844 {
6845 col = 0;
6846 width = Columns;
6847 }
6848 else
6849 {
6850 col = wp->w_wincol;
6851 width = wp->w_width;
6852 }
6853 screen_draw_rectangle(row, col, end - row, width, FALSE);
6854}
6855#endif
6856
6857/*
6858 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6859 * with character 'c1' in first column followed by 'c2' in the other columns.
6860 * Use attributes 'attr'.
6861 */
6862 void
6863screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6864 int start_row, end_row;
6865 int start_col, end_col;
6866 int c1, c2;
6867 int attr;
6868{
6869 int row;
6870 int col;
6871 int off;
6872 int end_off;
6873 int did_delete;
6874 int c;
6875 int norm_term;
6876#if defined(FEAT_GUI) || defined(UNIX)
6877 int force_next = FALSE;
6878#endif
6879
6880 if (end_row > screen_Rows) /* safety check */
6881 end_row = screen_Rows;
6882 if (end_col > screen_Columns) /* safety check */
6883 end_col = screen_Columns;
6884 if (ScreenLines == NULL
6885 || start_row >= end_row
6886 || start_col >= end_col) /* nothing to do */
6887 return;
6888
6889 /* it's a "normal" terminal when not in a GUI or cterm */
6890 norm_term = (
6891#ifdef FEAT_GUI
6892 !gui.in_use &&
6893#endif
6894 t_colors <= 1);
6895 for (row = start_row; row < end_row; ++row)
6896 {
6897 /*
6898 * Try to use delete-line termcap code, when no attributes or in a
6899 * "normal" terminal, where a bold/italic space is just a
6900 * space.
6901 */
6902 did_delete = FALSE;
6903 if (c2 == ' '
6904 && end_col == Columns
6905 && can_clear(T_CE)
6906 && (attr == 0
6907 || (norm_term
6908 && attr <= HL_ALL
6909 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6910 {
6911 /*
6912 * check if we really need to clear something
6913 */
6914 col = start_col;
6915 if (c1 != ' ') /* don't clear first char */
6916 ++col;
6917
6918 off = LineOffset[row] + col;
6919 end_off = LineOffset[row] + end_col;
6920
6921 /* skip blanks (used often, keep it fast!) */
6922#ifdef FEAT_MBYTE
6923 if (enc_utf8)
6924 while (off < end_off && ScreenLines[off] == ' '
6925 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6926 ++off;
6927 else
6928#endif
6929 while (off < end_off && ScreenLines[off] == ' '
6930 && ScreenAttrs[off] == 0)
6931 ++off;
6932 if (off < end_off) /* something to be cleared */
6933 {
6934 col = off - LineOffset[row];
6935 screen_stop_highlight();
6936 term_windgoto(row, col);/* clear rest of this screen line */
6937 out_str(T_CE);
6938 screen_start(); /* don't know where cursor is now */
6939 col = end_col - col;
6940 while (col--) /* clear chars in ScreenLines */
6941 {
6942 ScreenLines[off] = ' ';
6943#ifdef FEAT_MBYTE
6944 if (enc_utf8)
6945 ScreenLinesUC[off] = 0;
6946#endif
6947 ScreenAttrs[off] = 0;
6948 ++off;
6949 }
6950 }
6951 did_delete = TRUE; /* the chars are cleared now */
6952 }
6953
6954 off = LineOffset[row] + start_col;
6955 c = c1;
6956 for (col = start_col; col < end_col; ++col)
6957 {
6958 if (ScreenLines[off] != c
6959#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006960 || (enc_utf8 && (int)ScreenLinesUC[off]
6961 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962#endif
6963 || ScreenAttrs[off] != attr
6964#if defined(FEAT_GUI) || defined(UNIX)
6965 || force_next
6966#endif
6967 )
6968 {
6969#if defined(FEAT_GUI) || defined(UNIX)
6970 /* The bold trick may make a single row of pixels appear in
6971 * the next character. When a bold character is removed, the
6972 * next character should be redrawn too. This happens for our
6973 * own GUI and for some xterms. */
6974 if (
6975# ifdef FEAT_GUI
6976 gui.in_use
6977# endif
6978# if defined(FEAT_GUI) && defined(UNIX)
6979 ||
6980# endif
6981# ifdef UNIX
6982 term_is_xterm
6983# endif
6984 )
6985 {
6986 if (ScreenLines[off] != ' '
6987 && (ScreenAttrs[off] > HL_ALL
6988 || ScreenAttrs[off] & HL_BOLD))
6989 force_next = TRUE;
6990 else
6991 force_next = FALSE;
6992 }
6993#endif
6994 ScreenLines[off] = c;
6995#ifdef FEAT_MBYTE
6996 if (enc_utf8)
6997 {
6998 if (c >= 0x80)
6999 {
7000 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007001 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
7003 else
7004 ScreenLinesUC[off] = 0;
7005 }
7006#endif
7007 ScreenAttrs[off] = attr;
7008 if (!did_delete || c != ' ')
7009 screen_char(off, row, col);
7010 }
7011 ++off;
7012 if (col == start_col)
7013 {
7014 if (did_delete)
7015 break;
7016 c = c2;
7017 }
7018 }
7019 if (end_col == Columns)
7020 LineWraps[row] = FALSE;
7021 if (row == Rows - 1) /* overwritten the command line */
7022 {
7023 redraw_cmdline = TRUE;
7024 if (c1 == ' ' && c2 == ' ')
7025 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007026 if (start_col == 0)
7027 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 }
7029 }
7030}
7031
7032/*
7033 * Check if there should be a delay. Used before clearing or redrawing the
7034 * screen or the command line.
7035 */
7036 void
7037check_for_delay(check_msg_scroll)
7038 int check_msg_scroll;
7039{
7040 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7041 && !did_wait_return
7042 && emsg_silent == 0)
7043 {
7044 out_flush();
7045 ui_delay(1000L, TRUE);
7046 emsg_on_display = FALSE;
7047 if (check_msg_scroll)
7048 msg_scroll = FALSE;
7049 }
7050}
7051
7052/*
7053 * screen_valid - allocate screen buffers if size changed
7054 * If "clear" is TRUE: clear screen if it has been resized.
7055 * Returns TRUE if there is a valid screen to write to.
7056 * Returns FALSE when starting up and screen not initialized yet.
7057 */
7058 int
7059screen_valid(clear)
7060 int clear;
7061{
7062 screenalloc(clear); /* allocate screen buffers if size changed */
7063 return (ScreenLines != NULL);
7064}
7065
7066/*
7067 * Resize the shell to Rows and Columns.
7068 * Allocate ScreenLines[] and associated items.
7069 *
7070 * There may be some time between setting Rows and Columns and (re)allocating
7071 * ScreenLines[]. This happens when starting up and when (manually) changing
7072 * the shell size. Always use screen_Rows and screen_Columns to access items
7073 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7074 * final size of the shell is needed.
7075 */
7076 void
7077screenalloc(clear)
7078 int clear;
7079{
7080 int new_row, old_row;
7081#ifdef FEAT_GUI
7082 int old_Rows;
7083#endif
7084 win_T *wp;
7085 int outofmem = FALSE;
7086 int len;
7087 schar_T *new_ScreenLines;
7088#ifdef FEAT_MBYTE
7089 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007090 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007092 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093#endif
7094 sattr_T *new_ScreenAttrs;
7095 unsigned *new_LineOffset;
7096 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007097#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007098 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007099 tabpage_T *tp;
7100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007102 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
7104 /*
7105 * Allocation of the screen buffers is done only when the size changes and
7106 * when Rows and Columns have been set and we have started doing full
7107 * screen stuff.
7108 */
7109 if ((ScreenLines != NULL
7110 && Rows == screen_Rows
7111 && Columns == screen_Columns
7112#ifdef FEAT_MBYTE
7113 && enc_utf8 == (ScreenLinesUC != NULL)
7114 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007115 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116#endif
7117 )
7118 || Rows == 0
7119 || Columns == 0
7120 || (!full_screen && ScreenLines == NULL))
7121 return;
7122
7123 /*
7124 * It's possible that we produce an out-of-memory message below, which
7125 * will cause this function to be called again. To break the loop, just
7126 * return here.
7127 */
7128 if (entered)
7129 return;
7130 entered = TRUE;
7131
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007132 /*
7133 * Note that the window sizes are updated before reallocating the arrays,
7134 * thus we must not redraw here!
7135 */
7136 ++RedrawingDisabled;
7137
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 win_new_shellsize(); /* fit the windows in the new sized shell */
7139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 comp_col(); /* recompute columns for shown command and ruler */
7141
7142 /*
7143 * We're changing the size of the screen.
7144 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7145 * - Move lines from the old arrays into the new arrays, clear extra
7146 * lines (unless the screen is going to be cleared).
7147 * - Free the old arrays.
7148 *
7149 * If anything fails, make ScreenLines NULL, so we don't do anything!
7150 * Continuing with the old ScreenLines may result in a crash, because the
7151 * size is wrong.
7152 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007153 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156 new_ScreenLines = (schar_T *)lalloc((long_u)(
7157 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7158#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007159 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (enc_utf8)
7161 {
7162 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7163 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007164 for (i = 0; i < p_mco; ++i)
7165 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7167 }
7168 if (enc_dbcs == DBCS_JPNU)
7169 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7170 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7171#endif
7172 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7173 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7174 new_LineOffset = (unsigned *)lalloc((long_u)(
7175 Rows * sizeof(unsigned)), FALSE);
7176 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007177#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007178 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 {
7183 if (win_alloc_lines(wp) == FAIL)
7184 {
7185 outofmem = TRUE;
7186#ifdef FEAT_WINDOWS
7187 break;
7188#endif
7189 }
7190 }
7191
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007192#ifdef FEAT_MBYTE
7193 for (i = 0; i < p_mco; ++i)
7194 if (new_ScreenLinesC[i] == NULL)
7195 break;
7196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 if (new_ScreenLines == NULL
7198#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007199 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7201#endif
7202 || new_ScreenAttrs == NULL
7203 || new_LineOffset == NULL
7204 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007205#ifdef FEAT_WINDOWS
7206 || new_TabPageIdxs == NULL
7207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 || outofmem)
7209 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007210 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007211 {
7212 /* guess the size */
7213 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7214
7215 /* Remember we did this to avoid getting outofmem messages over
7216 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007217 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 vim_free(new_ScreenLines);
7220 new_ScreenLines = NULL;
7221#ifdef FEAT_MBYTE
7222 vim_free(new_ScreenLinesUC);
7223 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007224 for (i = 0; i < p_mco; ++i)
7225 {
7226 vim_free(new_ScreenLinesC[i]);
7227 new_ScreenLinesC[i] = NULL;
7228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 vim_free(new_ScreenLines2);
7230 new_ScreenLines2 = NULL;
7231#endif
7232 vim_free(new_ScreenAttrs);
7233 new_ScreenAttrs = NULL;
7234 vim_free(new_LineOffset);
7235 new_LineOffset = NULL;
7236 vim_free(new_LineWraps);
7237 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007238#ifdef FEAT_WINDOWS
7239 vim_free(new_TabPageIdxs);
7240 new_TabPageIdxs = NULL;
7241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 }
7243 else
7244 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007245 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 for (new_row = 0; new_row < Rows; ++new_row)
7248 {
7249 new_LineOffset[new_row] = new_row * Columns;
7250 new_LineWraps[new_row] = FALSE;
7251
7252 /*
7253 * If the screen is not going to be cleared, copy as much as
7254 * possible from the old screen to the new one and clear the rest
7255 * (used when resizing the window at the "--more--" prompt or when
7256 * executing an external command, for the GUI).
7257 */
7258 if (!clear)
7259 {
7260 (void)vim_memset(new_ScreenLines + new_row * Columns,
7261 ' ', (size_t)Columns * sizeof(schar_T));
7262#ifdef FEAT_MBYTE
7263 if (enc_utf8)
7264 {
7265 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7266 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007267 for (i = 0; i < p_mco; ++i)
7268 (void)vim_memset(new_ScreenLinesC[i]
7269 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 0, (size_t)Columns * sizeof(u8char_T));
7271 }
7272 if (enc_dbcs == DBCS_JPNU)
7273 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7274 0, (size_t)Columns * sizeof(schar_T));
7275#endif
7276 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7277 0, (size_t)Columns * sizeof(sattr_T));
7278 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007279 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {
7281 if (screen_Columns < Columns)
7282 len = screen_Columns;
7283 else
7284 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007285#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007286 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007287 * may be invalid now. Also when p_mco changes. */
7288 if (!(enc_utf8 && ScreenLinesUC == NULL)
7289 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007290#endif
7291 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7292 ScreenLines + LineOffset[old_row],
7293 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007295 if (enc_utf8 && ScreenLinesUC != NULL
7296 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
7298 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7299 ScreenLinesUC + LineOffset[old_row],
7300 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007301 for (i = 0; i < p_mco; ++i)
7302 mch_memmove(new_ScreenLinesC[i]
7303 + new_LineOffset[new_row],
7304 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 (size_t)len * sizeof(u8char_T));
7306 }
7307 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7308 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7309 ScreenLines2 + LineOffset[old_row],
7310 (size_t)len * sizeof(schar_T));
7311#endif
7312 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7313 ScreenAttrs + LineOffset[old_row],
7314 (size_t)len * sizeof(sattr_T));
7315 }
7316 }
7317 }
7318 /* Use the last line of the screen for the current line. */
7319 current_ScreenLine = new_ScreenLines + Rows * Columns;
7320 }
7321
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007322 free_screenlines();
7323
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 ScreenLines = new_ScreenLines;
7325#ifdef FEAT_MBYTE
7326 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007327 for (i = 0; i < p_mco; ++i)
7328 ScreenLinesC[i] = new_ScreenLinesC[i];
7329 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 ScreenLines2 = new_ScreenLines2;
7331#endif
7332 ScreenAttrs = new_ScreenAttrs;
7333 LineOffset = new_LineOffset;
7334 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007335#ifdef FEAT_WINDOWS
7336 TabPageIdxs = new_TabPageIdxs;
7337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
7339 /* It's important that screen_Rows and screen_Columns reflect the actual
7340 * size of ScreenLines[]. Set them before calling anything. */
7341#ifdef FEAT_GUI
7342 old_Rows = screen_Rows;
7343#endif
7344 screen_Rows = Rows;
7345 screen_Columns = Columns;
7346
7347 must_redraw = CLEAR; /* need to clear the screen later */
7348 if (clear)
7349 screenclear2();
7350
7351#ifdef FEAT_GUI
7352 else if (gui.in_use
7353 && !gui.starting
7354 && ScreenLines != NULL
7355 && old_Rows != Rows)
7356 {
7357 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7358 /*
7359 * Adjust the position of the cursor, for when executing an external
7360 * command.
7361 */
7362 if (msg_row >= Rows) /* Rows got smaller */
7363 msg_row = Rows - 1; /* put cursor at last row */
7364 else if (Rows > old_Rows) /* Rows got bigger */
7365 msg_row += Rows - old_Rows; /* put cursor in same place */
7366 if (msg_col >= Columns) /* Columns got smaller */
7367 msg_col = Columns - 1; /* put cursor at last column */
7368 }
7369#endif
7370
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007372 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007373
7374#ifdef FEAT_AUTOCMD
7375 if (starting == 0)
7376 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378}
7379
7380 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007381free_screenlines()
7382{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007383#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007384 int i;
7385
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007386 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007387 for (i = 0; i < Screen_mco; ++i)
7388 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007389 vim_free(ScreenLines2);
7390#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007391 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007392 vim_free(ScreenAttrs);
7393 vim_free(LineOffset);
7394 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007395#ifdef FEAT_WINDOWS
7396 vim_free(TabPageIdxs);
7397#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007398}
7399
7400 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401screenclear()
7402{
7403 check_for_delay(FALSE);
7404 screenalloc(FALSE); /* allocate screen buffers if size changed */
7405 screenclear2(); /* clear the screen */
7406}
7407
7408 static void
7409screenclear2()
7410{
7411 int i;
7412
7413 if (starting == NO_SCREEN || ScreenLines == NULL
7414#ifdef FEAT_GUI
7415 || (gui.in_use && gui.starting)
7416#endif
7417 )
7418 return;
7419
7420#ifdef FEAT_GUI
7421 if (!gui.in_use)
7422#endif
7423 screen_attr = -1; /* force setting the Normal colors */
7424 screen_stop_highlight(); /* don't want highlighting here */
7425
7426#ifdef FEAT_CLIPBOARD
7427 /* disable selection without redrawing it */
7428 clip_scroll_selection(9999);
7429#endif
7430
7431 /* blank out ScreenLines */
7432 for (i = 0; i < Rows; ++i)
7433 {
7434 lineclear(LineOffset[i], (int)Columns);
7435 LineWraps[i] = FALSE;
7436 }
7437
7438 if (can_clear(T_CL))
7439 {
7440 out_str(T_CL); /* clear the display */
7441 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007442 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 }
7444 else
7445 {
7446 /* can't clear the screen, mark all chars with invalid attributes */
7447 for (i = 0; i < Rows; ++i)
7448 lineinvalid(LineOffset[i], (int)Columns);
7449 clear_cmdline = TRUE;
7450 }
7451
7452 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7453
7454 win_rest_invalid(firstwin);
7455 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007456#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007457 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (must_redraw == CLEAR) /* no need to clear again */
7460 must_redraw = NOT_VALID;
7461 compute_cmdrow();
7462 msg_row = cmdline_row; /* put cursor on last line for messages */
7463 msg_col = 0;
7464 screen_start(); /* don't know where cursor is now */
7465 msg_scrolled = 0; /* can't scroll back */
7466 msg_didany = FALSE;
7467 msg_didout = FALSE;
7468}
7469
7470/*
7471 * Clear one line in ScreenLines.
7472 */
7473 static void
7474lineclear(off, width)
7475 unsigned off;
7476 int width;
7477{
7478 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7479#ifdef FEAT_MBYTE
7480 if (enc_utf8)
7481 (void)vim_memset(ScreenLinesUC + off, 0,
7482 (size_t)width * sizeof(u8char_T));
7483#endif
7484 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7485}
7486
7487/*
7488 * Mark one line in ScreenLines invalid by setting the attributes to an
7489 * invalid value.
7490 */
7491 static void
7492lineinvalid(off, width)
7493 unsigned off;
7494 int width;
7495{
7496 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7497}
7498
7499#ifdef FEAT_VERTSPLIT
7500/*
7501 * Copy part of a Screenline for vertically split window "wp".
7502 */
7503 static void
7504linecopy(to, from, wp)
7505 int to;
7506 int from;
7507 win_T *wp;
7508{
7509 unsigned off_to = LineOffset[to] + wp->w_wincol;
7510 unsigned off_from = LineOffset[from] + wp->w_wincol;
7511
7512 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7513 wp->w_width * sizeof(schar_T));
7514# ifdef FEAT_MBYTE
7515 if (enc_utf8)
7516 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007517 int i;
7518
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7520 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007521 for (i = 0; i < p_mco; ++i)
7522 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7523 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 }
7525 if (enc_dbcs == DBCS_JPNU)
7526 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7527 wp->w_width * sizeof(schar_T));
7528# endif
7529 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7530 wp->w_width * sizeof(sattr_T));
7531}
7532#endif
7533
7534/*
7535 * Return TRUE if clearing with term string "p" would work.
7536 * It can't work when the string is empty or it won't set the right background.
7537 */
7538 int
7539can_clear(p)
7540 char_u *p;
7541{
7542 return (*p != NUL && (t_colors <= 1
7543#ifdef FEAT_GUI
7544 || gui.in_use
7545#endif
7546 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7547}
7548
7549/*
7550 * Reset cursor position. Use whenever cursor was moved because of outputting
7551 * something directly to the screen (shell commands) or a terminal control
7552 * code.
7553 */
7554 void
7555screen_start()
7556{
7557 screen_cur_row = screen_cur_col = 9999;
7558}
7559
7560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 * Move the cursor to position "row","col" in the screen.
7562 * This tries to find the most efficient way to move, minimizing the number of
7563 * characters sent to the terminal.
7564 */
7565 void
7566windgoto(row, col)
7567 int row;
7568 int col;
7569{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007570 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 int i;
7572 int plan;
7573 int cost;
7574 int wouldbe_col;
7575 int noinvcurs;
7576 char_u *bs;
7577 int goto_cost;
7578 int attr;
7579
7580#define GOTO_COST 7 /* asssume a term_windgoto() takes about 7 chars */
7581#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7582
7583#define PLAN_LE 1
7584#define PLAN_CR 2
7585#define PLAN_NL 3
7586#define PLAN_WRITE 4
7587 /* Can't use ScreenLines unless initialized */
7588 if (ScreenLines == NULL)
7589 return;
7590
7591 if (col != screen_cur_col || row != screen_cur_row)
7592 {
7593 /* Check for valid position. */
7594 if (row < 0) /* window without text lines? */
7595 row = 0;
7596 if (row >= screen_Rows)
7597 row = screen_Rows - 1;
7598 if (col >= screen_Columns)
7599 col = screen_Columns - 1;
7600
7601 /* check if no cursor movement is allowed in highlight mode */
7602 if (screen_attr && *T_MS == NUL)
7603 noinvcurs = HIGHL_COST;
7604 else
7605 noinvcurs = 0;
7606 goto_cost = GOTO_COST + noinvcurs;
7607
7608 /*
7609 * Plan how to do the positioning:
7610 * 1. Use CR to move it to column 0, same row.
7611 * 2. Use T_LE to move it a few columns to the left.
7612 * 3. Use NL to move a few lines down, column 0.
7613 * 4. Move a few columns to the right with T_ND or by writing chars.
7614 *
7615 * Don't do this if the cursor went beyond the last column, the cursor
7616 * position is unknown then (some terminals wrap, some don't )
7617 *
7618 * First check if the highlighting attibutes allow us to write
7619 * characters to move the cursor to the right.
7620 */
7621 if (row >= screen_cur_row && screen_cur_col < Columns)
7622 {
7623 /*
7624 * If the cursor is in the same row, bigger col, we can use CR
7625 * or T_LE.
7626 */
7627 bs = NULL; /* init for GCC */
7628 attr = screen_attr;
7629 if (row == screen_cur_row && col < screen_cur_col)
7630 {
7631 /* "le" is preferred over "bc", because "bc" is obsolete */
7632 if (*T_LE)
7633 bs = T_LE; /* "cursor left" */
7634 else
7635 bs = T_BC; /* "backspace character (old) */
7636 if (*bs)
7637 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7638 else
7639 cost = 999;
7640 if (col + 1 < cost) /* using CR is less characters */
7641 {
7642 plan = PLAN_CR;
7643 wouldbe_col = 0;
7644 cost = 1; /* CR is just one character */
7645 }
7646 else
7647 {
7648 plan = PLAN_LE;
7649 wouldbe_col = col;
7650 }
7651 if (noinvcurs) /* will stop highlighting */
7652 {
7653 cost += noinvcurs;
7654 attr = 0;
7655 }
7656 }
7657
7658 /*
7659 * If the cursor is above where we want to be, we can use CR LF.
7660 */
7661 else if (row > screen_cur_row)
7662 {
7663 plan = PLAN_NL;
7664 wouldbe_col = 0;
7665 cost = (row - screen_cur_row) * 2; /* CR LF */
7666 if (noinvcurs) /* will stop highlighting */
7667 {
7668 cost += noinvcurs;
7669 attr = 0;
7670 }
7671 }
7672
7673 /*
7674 * If the cursor is in the same row, smaller col, just use write.
7675 */
7676 else
7677 {
7678 plan = PLAN_WRITE;
7679 wouldbe_col = screen_cur_col;
7680 cost = 0;
7681 }
7682
7683 /*
7684 * Check if any characters that need to be written have the
7685 * correct attributes. Also avoid UTF-8 characters.
7686 */
7687 i = col - wouldbe_col;
7688 if (i > 0)
7689 cost += i;
7690 if (cost < goto_cost && i > 0)
7691 {
7692 /*
7693 * Check if the attributes are correct without additionally
7694 * stopping highlighting.
7695 */
7696 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7697 while (i && *p++ == attr)
7698 --i;
7699 if (i != 0)
7700 {
7701 /*
7702 * Try if it works when highlighting is stopped here.
7703 */
7704 if (*--p == 0)
7705 {
7706 cost += noinvcurs;
7707 while (i && *p++ == 0)
7708 --i;
7709 }
7710 if (i != 0)
7711 cost = 999; /* different attributes, don't do it */
7712 }
7713#ifdef FEAT_MBYTE
7714 if (enc_utf8)
7715 {
7716 /* Don't use an UTF-8 char for positioning, it's slow. */
7717 for (i = wouldbe_col; i < col; ++i)
7718 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7719 {
7720 cost = 999;
7721 break;
7722 }
7723 }
7724#endif
7725 }
7726
7727 /*
7728 * We can do it without term_windgoto()!
7729 */
7730 if (cost < goto_cost)
7731 {
7732 if (plan == PLAN_LE)
7733 {
7734 if (noinvcurs)
7735 screen_stop_highlight();
7736 while (screen_cur_col > col)
7737 {
7738 out_str(bs);
7739 --screen_cur_col;
7740 }
7741 }
7742 else if (plan == PLAN_CR)
7743 {
7744 if (noinvcurs)
7745 screen_stop_highlight();
7746 out_char('\r');
7747 screen_cur_col = 0;
7748 }
7749 else if (plan == PLAN_NL)
7750 {
7751 if (noinvcurs)
7752 screen_stop_highlight();
7753 while (screen_cur_row < row)
7754 {
7755 out_char('\n');
7756 ++screen_cur_row;
7757 }
7758 screen_cur_col = 0;
7759 }
7760
7761 i = col - screen_cur_col;
7762 if (i > 0)
7763 {
7764 /*
7765 * Use cursor-right if it's one character only. Avoids
7766 * removing a line of pixels from the last bold char, when
7767 * using the bold trick in the GUI.
7768 */
7769 if (T_ND[0] != NUL && T_ND[1] == NUL)
7770 {
7771 while (i-- > 0)
7772 out_char(*T_ND);
7773 }
7774 else
7775 {
7776 int off;
7777
7778 off = LineOffset[row] + screen_cur_col;
7779 while (i-- > 0)
7780 {
7781 if (ScreenAttrs[off] != screen_attr)
7782 screen_stop_highlight();
7783#ifdef FEAT_MBYTE
7784 out_flush_check();
7785#endif
7786 out_char(ScreenLines[off]);
7787#ifdef FEAT_MBYTE
7788 if (enc_dbcs == DBCS_JPNU
7789 && ScreenLines[off] == 0x8e)
7790 out_char(ScreenLines2[off]);
7791#endif
7792 ++off;
7793 }
7794 }
7795 }
7796 }
7797 }
7798 else
7799 cost = 999;
7800
7801 if (cost >= goto_cost)
7802 {
7803 if (noinvcurs)
7804 screen_stop_highlight();
7805 if (row == screen_cur_row && (col > screen_cur_col) &&
7806 *T_CRI != NUL)
7807 term_cursor_right(col - screen_cur_col);
7808 else
7809 term_windgoto(row, col);
7810 }
7811 screen_cur_row = row;
7812 screen_cur_col = col;
7813 }
7814}
7815
7816/*
7817 * Set cursor to its position in the current window.
7818 */
7819 void
7820setcursor()
7821{
7822 if (redrawing())
7823 {
7824 validate_cursor();
7825 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7826 W_WINCOL(curwin) + (
7827#ifdef FEAT_RIGHTLEFT
7828 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7829# ifdef FEAT_MBYTE
7830 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7831# endif
7832 1)) :
7833#endif
7834 curwin->w_wcol));
7835 }
7836}
7837
7838
7839/*
7840 * insert 'line_count' lines at 'row' in window 'wp'
7841 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7842 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7843 * scrolling.
7844 * Returns FAIL if the lines are not inserted, OK for success.
7845 */
7846 int
7847win_ins_lines(wp, row, line_count, invalid, mayclear)
7848 win_T *wp;
7849 int row;
7850 int line_count;
7851 int invalid;
7852 int mayclear;
7853{
7854 int did_delete;
7855 int nextrow;
7856 int lastrow;
7857 int retval;
7858
7859 if (invalid)
7860 wp->w_lines_valid = 0;
7861
7862 if (wp->w_height < 5)
7863 return FAIL;
7864
7865 if (line_count > wp->w_height - row)
7866 line_count = wp->w_height - row;
7867
7868 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7869 if (retval != MAYBE)
7870 return retval;
7871
7872 /*
7873 * If there is a next window or a status line, we first try to delete the
7874 * lines at the bottom to avoid messing what is after the window.
7875 * If this fails and there are following windows, don't do anything to avoid
7876 * messing up those windows, better just redraw.
7877 */
7878 did_delete = FALSE;
7879#ifdef FEAT_WINDOWS
7880 if (wp->w_next != NULL || wp->w_status_height)
7881 {
7882 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7883 line_count, (int)Rows, FALSE, NULL) == OK)
7884 did_delete = TRUE;
7885 else if (wp->w_next)
7886 return FAIL;
7887 }
7888#endif
7889 /*
7890 * if no lines deleted, blank the lines that will end up below the window
7891 */
7892 if (!did_delete)
7893 {
7894#ifdef FEAT_WINDOWS
7895 wp->w_redr_status = TRUE;
7896#endif
7897 redraw_cmdline = TRUE;
7898 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7899 lastrow = nextrow + line_count;
7900 if (lastrow > Rows)
7901 lastrow = Rows;
7902 screen_fill(nextrow - line_count, lastrow - line_count,
7903 W_WINCOL(wp), (int)W_ENDCOL(wp),
7904 ' ', ' ', 0);
7905 }
7906
7907 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7908 == FAIL)
7909 {
7910 /* deletion will have messed up other windows */
7911 if (did_delete)
7912 {
7913#ifdef FEAT_WINDOWS
7914 wp->w_redr_status = TRUE;
7915#endif
7916 win_rest_invalid(W_NEXT(wp));
7917 }
7918 return FAIL;
7919 }
7920
7921 return OK;
7922}
7923
7924/*
7925 * delete "line_count" window lines at "row" in window "wp"
7926 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7927 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7928 * scrolling
7929 * Return OK for success, FAIL if the lines are not deleted.
7930 */
7931 int
7932win_del_lines(wp, row, line_count, invalid, mayclear)
7933 win_T *wp;
7934 int row;
7935 int line_count;
7936 int invalid;
7937 int mayclear;
7938{
7939 int retval;
7940
7941 if (invalid)
7942 wp->w_lines_valid = 0;
7943
7944 if (line_count > wp->w_height - row)
7945 line_count = wp->w_height - row;
7946
7947 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7948 if (retval != MAYBE)
7949 return retval;
7950
7951 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7952 (int)Rows, FALSE, NULL) == FAIL)
7953 return FAIL;
7954
7955#ifdef FEAT_WINDOWS
7956 /*
7957 * If there are windows or status lines below, try to put them at the
7958 * correct place. If we can't do that, they have to be redrawn.
7959 */
7960 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
7961 {
7962 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7963 line_count, (int)Rows, NULL) == FAIL)
7964 {
7965 wp->w_redr_status = TRUE;
7966 win_rest_invalid(wp->w_next);
7967 }
7968 }
7969 /*
7970 * If this is the last window and there is no status line, redraw the
7971 * command line later.
7972 */
7973 else
7974#endif
7975 redraw_cmdline = TRUE;
7976 return OK;
7977}
7978
7979/*
7980 * Common code for win_ins_lines() and win_del_lines().
7981 * Returns OK or FAIL when the work has been done.
7982 * Returns MAYBE when not finished yet.
7983 */
7984 static int
7985win_do_lines(wp, row, line_count, mayclear, del)
7986 win_T *wp;
7987 int row;
7988 int line_count;
7989 int mayclear;
7990 int del;
7991{
7992 int retval;
7993
7994 if (!redrawing() || line_count <= 0)
7995 return FAIL;
7996
7997 /* only a few lines left: redraw is faster */
7998 if (mayclear && Rows - line_count < 5
7999#ifdef FEAT_VERTSPLIT
8000 && wp->w_width == Columns
8001#endif
8002 )
8003 {
8004 screenclear(); /* will set wp->w_lines_valid to 0 */
8005 return FAIL;
8006 }
8007
8008 /*
8009 * Delete all remaining lines
8010 */
8011 if (row + line_count >= wp->w_height)
8012 {
8013 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8014 W_WINCOL(wp), (int)W_ENDCOL(wp),
8015 ' ', ' ', 0);
8016 return OK;
8017 }
8018
8019 /*
8020 * when scrolling, the message on the command line should be cleared,
8021 * otherwise it will stay there forever.
8022 */
8023 clear_cmdline = TRUE;
8024
8025 /*
8026 * If the terminal can set a scroll region, use that.
8027 * Always do this in a vertically split window. This will redraw from
8028 * ScreenLines[] when t_CV isn't defined. That's faster than using
8029 * win_line().
8030 * Don't use a scroll region when we are going to redraw the text, writing
8031 * a character in the lower right corner of the scroll region causes a
8032 * scroll-up in the DJGPP version.
8033 */
8034 if (scroll_region
8035#ifdef FEAT_VERTSPLIT
8036 || W_WIDTH(wp) != Columns
8037#endif
8038 )
8039 {
8040#ifdef FEAT_VERTSPLIT
8041 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8042#endif
8043 scroll_region_set(wp, row);
8044 if (del)
8045 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8046 wp->w_height - row, FALSE, wp);
8047 else
8048 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8049 wp->w_height - row, wp);
8050#ifdef FEAT_VERTSPLIT
8051 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8052#endif
8053 scroll_region_reset();
8054 return retval;
8055 }
8056
8057#ifdef FEAT_WINDOWS
8058 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8059 return FAIL;
8060#endif
8061
8062 return MAYBE;
8063}
8064
8065/*
8066 * window 'wp' and everything after it is messed up, mark it for redraw
8067 */
8068 static void
8069win_rest_invalid(wp)
8070 win_T *wp;
8071{
8072#ifdef FEAT_WINDOWS
8073 while (wp != NULL)
8074#else
8075 if (wp != NULL)
8076#endif
8077 {
8078 redraw_win_later(wp, NOT_VALID);
8079#ifdef FEAT_WINDOWS
8080 wp->w_redr_status = TRUE;
8081 wp = wp->w_next;
8082#endif
8083 }
8084 redraw_cmdline = TRUE;
8085}
8086
8087/*
8088 * The rest of the routines in this file perform screen manipulations. The
8089 * given operation is performed physically on the screen. The corresponding
8090 * change is also made to the internal screen image. In this way, the editor
8091 * anticipates the effect of editing changes on the appearance of the screen.
8092 * That way, when we call screenupdate a complete redraw isn't usually
8093 * necessary. Another advantage is that we can keep adding code to anticipate
8094 * screen changes, and in the meantime, everything still works.
8095 */
8096
8097/*
8098 * types for inserting or deleting lines
8099 */
8100#define USE_T_CAL 1
8101#define USE_T_CDL 2
8102#define USE_T_AL 3
8103#define USE_T_CE 4
8104#define USE_T_DL 5
8105#define USE_T_SR 6
8106#define USE_NL 7
8107#define USE_T_CD 8
8108#define USE_REDRAW 9
8109
8110/*
8111 * insert lines on the screen and update ScreenLines[]
8112 * 'end' is the line after the scrolled part. Normally it is Rows.
8113 * When scrolling region used 'off' is the offset from the top for the region.
8114 * 'row' and 'end' are relative to the start of the region.
8115 *
8116 * return FAIL for failure, OK for success.
8117 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008118 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119screen_ins_lines(off, row, line_count, end, wp)
8120 int off;
8121 int row;
8122 int line_count;
8123 int end;
8124 win_T *wp; /* NULL or window to use width from */
8125{
8126 int i;
8127 int j;
8128 unsigned temp;
8129 int cursor_row;
8130 int type;
8131 int result_empty;
8132 int can_ce = can_clear(T_CE);
8133
8134 /*
8135 * FAIL if
8136 * - there is no valid screen
8137 * - the screen has to be redrawn completely
8138 * - the line count is less than one
8139 * - the line count is more than 'ttyscroll'
8140 */
8141 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8142 return FAIL;
8143
8144 /*
8145 * There are seven ways to insert lines:
8146 * 0. When in a vertically split window and t_CV isn't set, redraw the
8147 * characters from ScreenLines[].
8148 * 1. Use T_CD (clear to end of display) if it exists and the result of
8149 * the insert is just empty lines
8150 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8151 * present or line_count > 1. It looks better if we do all the inserts
8152 * at once.
8153 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8154 * insert is just empty lines and T_CE is not present or line_count >
8155 * 1.
8156 * 4. Use T_AL (insert line) if it exists.
8157 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8158 * just empty lines.
8159 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8160 * just empty lines.
8161 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8162 * the 'da' flag is not set or we have clear line capability.
8163 * 8. redraw the characters from ScreenLines[].
8164 *
8165 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8166 * the scrollbar for the window. It does have insert line, use that if it
8167 * exists.
8168 */
8169 result_empty = (row + line_count >= end);
8170#ifdef FEAT_VERTSPLIT
8171 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8172 type = USE_REDRAW;
8173 else
8174#endif
8175 if (can_clear(T_CD) && result_empty)
8176 type = USE_T_CD;
8177 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8178 type = USE_T_CAL;
8179 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8180 type = USE_T_CDL;
8181 else if (*T_AL != NUL)
8182 type = USE_T_AL;
8183 else if (can_ce && result_empty)
8184 type = USE_T_CE;
8185 else if (*T_DL != NUL && result_empty)
8186 type = USE_T_DL;
8187 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8188 type = USE_T_SR;
8189 else
8190 return FAIL;
8191
8192 /*
8193 * For clearing the lines screen_del_lines() is used. This will also take
8194 * care of t_db if necessary.
8195 */
8196 if (type == USE_T_CD || type == USE_T_CDL ||
8197 type == USE_T_CE || type == USE_T_DL)
8198 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8199
8200 /*
8201 * If text is retained below the screen, first clear or delete as many
8202 * lines at the bottom of the window as are about to be inserted so that
8203 * the deleted lines won't later surface during a screen_del_lines.
8204 */
8205 if (*T_DB)
8206 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8207
8208#ifdef FEAT_CLIPBOARD
8209 /* Remove a modeless selection when inserting lines halfway the screen
8210 * or not the full width of the screen. */
8211 if (off + row > 0
8212# ifdef FEAT_VERTSPLIT
8213 || (wp != NULL && wp->w_width != Columns)
8214# endif
8215 )
8216 clip_clear_selection();
8217 else
8218 clip_scroll_selection(-line_count);
8219#endif
8220
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221#ifdef FEAT_GUI
8222 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8223 * scrolling is actually carried out. */
8224 gui_dont_update_cursor();
8225#endif
8226
8227 if (*T_CCS != NUL) /* cursor relative to region */
8228 cursor_row = row;
8229 else
8230 cursor_row = row + off;
8231
8232 /*
8233 * Shift LineOffset[] line_count down to reflect the inserted lines.
8234 * Clear the inserted lines in ScreenLines[].
8235 */
8236 row += off;
8237 end += off;
8238 for (i = 0; i < line_count; ++i)
8239 {
8240#ifdef FEAT_VERTSPLIT
8241 if (wp != NULL && wp->w_width != Columns)
8242 {
8243 /* need to copy part of a line */
8244 j = end - 1 - i;
8245 while ((j -= line_count) >= row)
8246 linecopy(j + line_count, j, wp);
8247 j += line_count;
8248 if (can_clear((char_u *)" "))
8249 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8250 else
8251 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8252 LineWraps[j] = FALSE;
8253 }
8254 else
8255#endif
8256 {
8257 j = end - 1 - i;
8258 temp = LineOffset[j];
8259 while ((j -= line_count) >= row)
8260 {
8261 LineOffset[j + line_count] = LineOffset[j];
8262 LineWraps[j + line_count] = LineWraps[j];
8263 }
8264 LineOffset[j + line_count] = temp;
8265 LineWraps[j + line_count] = FALSE;
8266 if (can_clear((char_u *)" "))
8267 lineclear(temp, (int)Columns);
8268 else
8269 lineinvalid(temp, (int)Columns);
8270 }
8271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272
8273 screen_stop_highlight();
8274 windgoto(cursor_row, 0);
8275
8276#ifdef FEAT_VERTSPLIT
8277 /* redraw the characters */
8278 if (type == USE_REDRAW)
8279 redraw_block(row, end, wp);
8280 else
8281#endif
8282 if (type == USE_T_CAL)
8283 {
8284 term_append_lines(line_count);
8285 screen_start(); /* don't know where cursor is now */
8286 }
8287 else
8288 {
8289 for (i = 0; i < line_count; i++)
8290 {
8291 if (type == USE_T_AL)
8292 {
8293 if (i && cursor_row != 0)
8294 windgoto(cursor_row, 0);
8295 out_str(T_AL);
8296 }
8297 else /* type == USE_T_SR */
8298 out_str(T_SR);
8299 screen_start(); /* don't know where cursor is now */
8300 }
8301 }
8302
8303 /*
8304 * With scroll-reverse and 'da' flag set we need to clear the lines that
8305 * have been scrolled down into the region.
8306 */
8307 if (type == USE_T_SR && *T_DA)
8308 {
8309 for (i = 0; i < line_count; ++i)
8310 {
8311 windgoto(off + i, 0);
8312 out_str(T_CE);
8313 screen_start(); /* don't know where cursor is now */
8314 }
8315 }
8316
8317#ifdef FEAT_GUI
8318 gui_can_update_cursor();
8319 if (gui.in_use)
8320 out_flush(); /* always flush after a scroll */
8321#endif
8322 return OK;
8323}
8324
8325/*
8326 * delete lines on the screen and update ScreenLines[]
8327 * 'end' is the line after the scrolled part. Normally it is Rows.
8328 * When scrolling region used 'off' is the offset from the top for the region.
8329 * 'row' and 'end' are relative to the start of the region.
8330 *
8331 * Return OK for success, FAIL if the lines are not deleted.
8332 */
8333/*ARGSUSED*/
8334 int
8335screen_del_lines(off, row, line_count, end, force, wp)
8336 int off;
8337 int row;
8338 int line_count;
8339 int end;
8340 int force; /* even when line_count > p_ttyscroll */
8341 win_T *wp; /* NULL or window to use width from */
8342{
8343 int j;
8344 int i;
8345 unsigned temp;
8346 int cursor_row;
8347 int cursor_end;
8348 int result_empty; /* result is empty until end of region */
8349 int can_delete; /* deleting line codes can be used */
8350 int type;
8351
8352 /*
8353 * FAIL if
8354 * - there is no valid screen
8355 * - the screen has to be redrawn completely
8356 * - the line count is less than one
8357 * - the line count is more than 'ttyscroll'
8358 */
8359 if (!screen_valid(TRUE) || line_count <= 0 ||
8360 (!force && line_count > p_ttyscroll))
8361 return FAIL;
8362
8363 /*
8364 * Check if the rest of the current region will become empty.
8365 */
8366 result_empty = row + line_count >= end;
8367
8368 /*
8369 * We can delete lines only when 'db' flag not set or when 'ce' option
8370 * available.
8371 */
8372 can_delete = (*T_DB == NUL || can_clear(T_CE));
8373
8374 /*
8375 * There are six ways to delete lines:
8376 * 0. When in a vertically split window and t_CV isn't set, redraw the
8377 * characters from ScreenLines[].
8378 * 1. Use T_CD if it exists and the result is empty.
8379 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8380 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8381 * none of the other ways work.
8382 * 4. Use T_CE (erase line) if the result is empty.
8383 * 5. Use T_DL (delete line) if it exists.
8384 * 6. redraw the characters from ScreenLines[].
8385 */
8386#ifdef FEAT_VERTSPLIT
8387 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8388 type = USE_REDRAW;
8389 else
8390#endif
8391 if (can_clear(T_CD) && result_empty)
8392 type = USE_T_CD;
8393#if defined(__BEOS__) && defined(BEOS_DR8)
8394 /*
8395 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8396 * its internal termcap... this works okay for tests which test *T_DB !=
8397 * NUL. It has the disadvantage that the user cannot use any :set t_*
8398 * command to get T_DB (back) to empty_option, only :set term=... will do
8399 * the trick...
8400 * Anyway, this hack will hopefully go away with the next OS release.
8401 * (Olaf Seibert)
8402 */
8403 else if (row == 0 && T_DB == empty_option
8404 && (line_count == 1 || *T_CDL == NUL))
8405#else
8406 else if (row == 0 && (
8407#ifndef AMIGA
8408 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8409 * up, so use delete-line command */
8410 line_count == 1 ||
8411#endif
8412 *T_CDL == NUL))
8413#endif
8414 type = USE_NL;
8415 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8416 type = USE_T_CDL;
8417 else if (can_clear(T_CE) && result_empty
8418#ifdef FEAT_VERTSPLIT
8419 && (wp == NULL || wp->w_width == Columns)
8420#endif
8421 )
8422 type = USE_T_CE;
8423 else if (*T_DL != NUL && can_delete)
8424 type = USE_T_DL;
8425 else if (*T_CDL != NUL && can_delete)
8426 type = USE_T_CDL;
8427 else
8428 return FAIL;
8429
8430#ifdef FEAT_CLIPBOARD
8431 /* Remove a modeless selection when deleting lines halfway the screen or
8432 * not the full width of the screen. */
8433 if (off + row > 0
8434# ifdef FEAT_VERTSPLIT
8435 || (wp != NULL && wp->w_width != Columns)
8436# endif
8437 )
8438 clip_clear_selection();
8439 else
8440 clip_scroll_selection(line_count);
8441#endif
8442
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443#ifdef FEAT_GUI
8444 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8445 * scrolling is actually carried out. */
8446 gui_dont_update_cursor();
8447#endif
8448
8449 if (*T_CCS != NUL) /* cursor relative to region */
8450 {
8451 cursor_row = row;
8452 cursor_end = end;
8453 }
8454 else
8455 {
8456 cursor_row = row + off;
8457 cursor_end = end + off;
8458 }
8459
8460 /*
8461 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8462 * Clear the inserted lines in ScreenLines[].
8463 */
8464 row += off;
8465 end += off;
8466 for (i = 0; i < line_count; ++i)
8467 {
8468#ifdef FEAT_VERTSPLIT
8469 if (wp != NULL && wp->w_width != Columns)
8470 {
8471 /* need to copy part of a line */
8472 j = row + i;
8473 while ((j += line_count) <= end - 1)
8474 linecopy(j - line_count, j, wp);
8475 j -= line_count;
8476 if (can_clear((char_u *)" "))
8477 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8478 else
8479 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8480 LineWraps[j] = FALSE;
8481 }
8482 else
8483#endif
8484 {
8485 /* whole width, moving the line pointers is faster */
8486 j = row + i;
8487 temp = LineOffset[j];
8488 while ((j += line_count) <= end - 1)
8489 {
8490 LineOffset[j - line_count] = LineOffset[j];
8491 LineWraps[j - line_count] = LineWraps[j];
8492 }
8493 LineOffset[j - line_count] = temp;
8494 LineWraps[j - line_count] = FALSE;
8495 if (can_clear((char_u *)" "))
8496 lineclear(temp, (int)Columns);
8497 else
8498 lineinvalid(temp, (int)Columns);
8499 }
8500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501
8502 screen_stop_highlight();
8503
8504#ifdef FEAT_VERTSPLIT
8505 /* redraw the characters */
8506 if (type == USE_REDRAW)
8507 redraw_block(row, end, wp);
8508 else
8509#endif
8510 if (type == USE_T_CD) /* delete the lines */
8511 {
8512 windgoto(cursor_row, 0);
8513 out_str(T_CD);
8514 screen_start(); /* don't know where cursor is now */
8515 }
8516 else if (type == USE_T_CDL)
8517 {
8518 windgoto(cursor_row, 0);
8519 term_delete_lines(line_count);
8520 screen_start(); /* don't know where cursor is now */
8521 }
8522 /*
8523 * Deleting lines at top of the screen or scroll region: Just scroll
8524 * the whole screen (scroll region) up by outputting newlines on the
8525 * last line.
8526 */
8527 else if (type == USE_NL)
8528 {
8529 windgoto(cursor_end - 1, 0);
8530 for (i = line_count; --i >= 0; )
8531 out_char('\n'); /* cursor will remain on same line */
8532 }
8533 else
8534 {
8535 for (i = line_count; --i >= 0; )
8536 {
8537 if (type == USE_T_DL)
8538 {
8539 windgoto(cursor_row, 0);
8540 out_str(T_DL); /* delete a line */
8541 }
8542 else /* type == USE_T_CE */
8543 {
8544 windgoto(cursor_row + i, 0);
8545 out_str(T_CE); /* erase a line */
8546 }
8547 screen_start(); /* don't know where cursor is now */
8548 }
8549 }
8550
8551 /*
8552 * If the 'db' flag is set, we need to clear the lines that have been
8553 * scrolled up at the bottom of the region.
8554 */
8555 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8556 {
8557 for (i = line_count; i > 0; --i)
8558 {
8559 windgoto(cursor_end - i, 0);
8560 out_str(T_CE); /* erase a line */
8561 screen_start(); /* don't know where cursor is now */
8562 }
8563 }
8564
8565#ifdef FEAT_GUI
8566 gui_can_update_cursor();
8567 if (gui.in_use)
8568 out_flush(); /* always flush after a scroll */
8569#endif
8570
8571 return OK;
8572}
8573
8574/*
8575 * show the current mode and ruler
8576 *
8577 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8578 * If clear_cmdline is FALSE there may be a message there that needs to be
8579 * cleared only if a mode is shown.
8580 * Return the length of the message (0 if no message).
8581 */
8582 int
8583showmode()
8584{
8585 int need_clear;
8586 int length = 0;
8587 int do_mode;
8588 int attr;
8589 int nwr_save;
8590#ifdef FEAT_INS_EXPAND
8591 int sub_attr;
8592#endif
8593
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008594 do_mode = ((p_smd && msg_silent == 0)
8595 && ((State & INSERT)
8596 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#ifdef FEAT_VISUAL
8598 || VIsual_active
8599#endif
8600 ));
8601 if (do_mode || Recording)
8602 {
8603 /*
8604 * Don't show mode right now, when not redrawing or inside a mapping.
8605 * Call char_avail() only when we are going to show something, because
8606 * it takes a bit of time.
8607 */
8608 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8609 {
8610 redraw_cmdline = TRUE; /* show mode later */
8611 return 0;
8612 }
8613
8614 nwr_save = need_wait_return;
8615
8616 /* wait a bit before overwriting an important message */
8617 check_for_delay(FALSE);
8618
8619 /* if the cmdline is more than one line high, erase top lines */
8620 need_clear = clear_cmdline;
8621 if (clear_cmdline && cmdline_row < Rows - 1)
8622 msg_clr_cmdline(); /* will reset clear_cmdline */
8623
8624 /* Position on the last line in the window, column 0 */
8625 msg_pos_mode();
8626 cursor_off();
8627 attr = hl_attr(HLF_CM); /* Highlight mode */
8628 if (do_mode)
8629 {
8630 MSG_PUTS_ATTR("--", attr);
8631#if defined(FEAT_XIM)
8632 if (xic != NULL && im_get_status() && !p_imdisable
8633 && curbuf->b_p_iminsert == B_IMODE_IM)
8634# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8635 MSG_PUTS_ATTR(" IM", attr);
8636# else
8637 MSG_PUTS_ATTR(" XIM", attr);
8638# endif
8639#endif
8640#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8641 if (gui.in_use)
8642 {
8643 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008644 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 }
8646#endif
8647#ifdef FEAT_INS_EXPAND
8648 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8649 {
8650 /* These messages can get long, avoid a wrap in a narrow
8651 * window. Prefer showing edit_submode_extra. */
8652 length = (Rows - msg_row) * Columns - 3;
8653 if (edit_submode_extra != NULL)
8654 length -= vim_strsize(edit_submode_extra);
8655 if (length > 0)
8656 {
8657 if (edit_submode_pre != NULL)
8658 length -= vim_strsize(edit_submode_pre);
8659 if (length - vim_strsize(edit_submode) > 0)
8660 {
8661 if (edit_submode_pre != NULL)
8662 msg_puts_attr(edit_submode_pre, attr);
8663 msg_puts_attr(edit_submode, attr);
8664 }
8665 if (edit_submode_extra != NULL)
8666 {
8667 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8668 if ((int)edit_submode_highl < (int)HLF_COUNT)
8669 sub_attr = hl_attr(edit_submode_highl);
8670 else
8671 sub_attr = attr;
8672 msg_puts_attr(edit_submode_extra, sub_attr);
8673 }
8674 }
8675 length = 0;
8676 }
8677 else
8678#endif
8679 {
8680#ifdef FEAT_VREPLACE
8681 if (State & VREPLACE_FLAG)
8682 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8683 else
8684#endif
8685 if (State & REPLACE_FLAG)
8686 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8687 else if (State & INSERT)
8688 {
8689#ifdef FEAT_RIGHTLEFT
8690 if (p_ri)
8691 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8692#endif
8693 MSG_PUTS_ATTR(_(" INSERT"), attr);
8694 }
8695 else if (restart_edit == 'I')
8696 MSG_PUTS_ATTR(_(" (insert)"), attr);
8697 else if (restart_edit == 'R')
8698 MSG_PUTS_ATTR(_(" (replace)"), attr);
8699 else if (restart_edit == 'V')
8700 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8701#ifdef FEAT_RIGHTLEFT
8702 if (p_hkmap)
8703 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8704# ifdef FEAT_FKMAP
8705 if (p_fkmap)
8706 MSG_PUTS_ATTR(farsi_text_5, attr);
8707# endif
8708#endif
8709#ifdef FEAT_KEYMAP
8710 if (State & LANGMAP)
8711 {
8712# ifdef FEAT_ARABIC
8713 if (curwin->w_p_arab)
8714 MSG_PUTS_ATTR(_(" Arabic"), attr);
8715 else
8716# endif
8717 MSG_PUTS_ATTR(_(" (lang)"), attr);
8718 }
8719#endif
8720 if ((State & INSERT) && p_paste)
8721 MSG_PUTS_ATTR(_(" (paste)"), attr);
8722
8723#ifdef FEAT_VISUAL
8724 if (VIsual_active)
8725 {
8726 char *p;
8727
8728 /* Don't concatenate separate words to avoid translation
8729 * problems. */
8730 switch ((VIsual_select ? 4 : 0)
8731 + (VIsual_mode == Ctrl_V) * 2
8732 + (VIsual_mode == 'V'))
8733 {
8734 case 0: p = N_(" VISUAL"); break;
8735 case 1: p = N_(" VISUAL LINE"); break;
8736 case 2: p = N_(" VISUAL BLOCK"); break;
8737 case 4: p = N_(" SELECT"); break;
8738 case 5: p = N_(" SELECT LINE"); break;
8739 default: p = N_(" SELECT BLOCK"); break;
8740 }
8741 MSG_PUTS_ATTR(_(p), attr);
8742 }
8743#endif
8744 MSG_PUTS_ATTR(" --", attr);
8745 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008746
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 need_clear = TRUE;
8748 }
8749 if (Recording
8750#ifdef FEAT_INS_EXPAND
8751 && edit_submode == NULL /* otherwise it gets too long */
8752#endif
8753 )
8754 {
8755 MSG_PUTS_ATTR(_("recording"), attr);
8756 need_clear = TRUE;
8757 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008758
8759 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 if (need_clear || clear_cmdline)
8761 msg_clr_eos();
8762 msg_didout = FALSE; /* overwrite this message */
8763 length = msg_col;
8764 msg_col = 0;
8765 need_wait_return = nwr_save; /* never ask for hit-return for this */
8766 }
8767 else if (clear_cmdline && msg_silent == 0)
8768 /* Clear the whole command line. Will reset "clear_cmdline". */
8769 msg_clr_cmdline();
8770
8771#ifdef FEAT_CMDL_INFO
8772# ifdef FEAT_VISUAL
8773 /* In Visual mode the size of the selected area must be redrawn. */
8774 if (VIsual_active)
8775 clear_showcmd();
8776# endif
8777
8778 /* If the last window has no status line, the ruler is after the mode
8779 * message and must be redrawn */
8780 if (redrawing()
8781# ifdef FEAT_WINDOWS
8782 && lastwin->w_status_height == 0
8783# endif
8784 )
8785 win_redr_ruler(lastwin, TRUE);
8786#endif
8787 redraw_cmdline = FALSE;
8788 clear_cmdline = FALSE;
8789
8790 return length;
8791}
8792
8793/*
8794 * Position for a mode message.
8795 */
8796 static void
8797msg_pos_mode()
8798{
8799 msg_col = 0;
8800 msg_row = Rows - 1;
8801}
8802
8803/*
8804 * Delete mode message. Used when ESC is typed which is expected to end
8805 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008806 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 */
8808 void
8809unshowmode(force)
8810 int force;
8811{
8812 /*
8813 * Don't delete it right now, when not redrawing or insided a mapping.
8814 */
8815 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8816 redraw_cmdline = TRUE; /* delete mode later */
8817 else
8818 {
8819 msg_pos_mode();
8820 if (Recording)
8821 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8822 msg_clr_eos();
8823 }
8824}
8825
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008826#if defined(FEAT_WINDOWS)
8827/*
8828 * Draw the tab pages line at the top of the Vim window.
8829 */
8830 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008831draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008832{
8833 int tabcount = 0;
8834 tabpage_T *tp;
8835 int tabwidth;
8836 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008837 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008838 int attr;
8839 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008840 win_T *cwp;
8841 int wincount;
8842 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008843 int c;
8844 int len;
8845 int attr_sel = hl_attr(HLF_TPS);
8846 int attr_nosel = hl_attr(HLF_TP);
8847 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008848 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008849 int room;
8850 int use_sep_chars = (t_colors < 8
8851#ifdef FEAT_GUI
8852 && !gui.in_use
8853#endif
8854 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008855
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008856 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008857
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008858#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00008859 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008860 if (gui_use_tabline())
8861 {
8862 gui_update_tabline();
8863 return;
8864 }
8865#endif
8866
8867 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008868 return;
8869
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008870#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008871
8872 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
8873 for (scol = 0; scol < Columns; ++scol)
8874 TabPageIdxs[scol] = 0;
8875
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008876 /* Use the 'tabline' option if it's set. */
8877 if (*p_tal != NUL)
8878 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008879 int save_called_emsg = called_emsg;
8880
8881 /* Check for an error. If there is one we would loop in redrawing the
8882 * screen. Avoid that by making 'tabline' empty. */
8883 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008884 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008885 if (called_emsg)
8886 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008887 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008888 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008889 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00008890 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008891#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008892 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008893 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
8894 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008895
Bram Moolenaar238a5642006-02-21 22:12:05 +00008896 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
8897 if (tabwidth < 6)
8898 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008899
Bram Moolenaar238a5642006-02-21 22:12:05 +00008900 attr = attr_nosel;
8901 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008902 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008903 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
8904 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008905 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008906 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008907
Bram Moolenaar238a5642006-02-21 22:12:05 +00008908 if (tp->tp_topframe == topframe)
8909 attr = attr_sel;
8910 if (use_sep_chars && col > 0)
8911 screen_putchar('|', 0, col++, attr);
8912
8913 if (tp->tp_topframe != topframe)
8914 attr = attr_nosel;
8915
8916 screen_putchar(' ', 0, col++, attr);
8917
8918 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008919 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008920 cwp = curwin;
8921 wp = firstwin;
8922 }
8923 else
8924 {
8925 cwp = tp->tp_curwin;
8926 wp = tp->tp_firstwin;
8927 }
8928
8929 modified = FALSE;
8930 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
8931 if (bufIsChanged(wp->w_buffer))
8932 modified = TRUE;
8933 if (modified || wincount > 1)
8934 {
8935 if (wincount > 1)
8936 {
8937 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008938 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008939 if (col + len >= Columns - 3)
8940 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008941 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008942#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00008943 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008944#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00008945 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008946#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00008947 );
8948 col += len;
8949 }
8950 if (modified)
8951 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
8952 screen_putchar(' ', 0, col++, attr);
8953 }
8954
8955 room = scol - col + tabwidth - 1;
8956 if (room > 0)
8957 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008958 /* Get buffer name in NameBuff[] */
8959 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008960 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008961 len = vim_strsize(NameBuff);
8962 p = NameBuff;
8963#ifdef FEAT_MBYTE
8964 if (has_mbyte)
8965 while (len > room)
8966 {
8967 len -= ptr2cells(p);
8968 mb_ptr_adv(p);
8969 }
8970 else
8971#endif
8972 if (len > room)
8973 {
8974 p += len - room;
8975 len = room;
8976 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008977 if (len > Columns - col - 1)
8978 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008979
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008980 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008981 col += len;
8982 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00008983 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008984
8985 /* Store the tab page number in TabPageIdxs[], so that
8986 * jump_to_mouse() knows where each one is. */
8987 ++tabcount;
8988 while (scol < col)
8989 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008990 }
8991
Bram Moolenaar238a5642006-02-21 22:12:05 +00008992 if (use_sep_chars)
8993 c = '_';
8994 else
8995 c = ' ';
8996 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008997
8998 /* Put an "X" for closing the current tab if there are several. */
8999 if (first_tabpage->tp_next != NULL)
9000 {
9001 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9002 TabPageIdxs[Columns - 1] = -999;
9003 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009004 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009005
9006 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9007 * set. */
9008 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009009}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009010
9011/*
9012 * Get buffer name for "buf" into NameBuff[].
9013 * Takes care of special buffer names and translates special characters.
9014 */
9015 void
9016get_trans_bufname(buf)
9017 buf_T *buf;
9018{
9019 if (buf_spname(buf) != NULL)
9020 STRCPY(NameBuff, buf_spname(buf));
9021 else
9022 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9023 trans_characters(NameBuff, MAXPATHL);
9024}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009025#endif
9026
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9028/*
9029 * Get the character to use in a status line. Get its attributes in "*attr".
9030 */
9031 static int
9032fillchar_status(attr, is_curwin)
9033 int *attr;
9034 int is_curwin;
9035{
9036 int fill;
9037 if (is_curwin)
9038 {
9039 *attr = hl_attr(HLF_S);
9040 fill = fill_stl;
9041 }
9042 else
9043 {
9044 *attr = hl_attr(HLF_SNC);
9045 fill = fill_stlnc;
9046 }
9047 /* Use fill when there is highlighting, and highlighting of current
9048 * window differs, or the fillchars differ, or this is not the
9049 * current window */
9050 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9051 || !is_curwin || firstwin == lastwin)
9052 || (fill_stl != fill_stlnc)))
9053 return fill;
9054 if (is_curwin)
9055 return '^';
9056 return '=';
9057}
9058#endif
9059
9060#ifdef FEAT_VERTSPLIT
9061/*
9062 * Get the character to use in a separator between vertically split windows.
9063 * Get its attributes in "*attr".
9064 */
9065 static int
9066fillchar_vsep(attr)
9067 int *attr;
9068{
9069 *attr = hl_attr(HLF_C);
9070 if (*attr == 0 && fill_vert == ' ')
9071 return '|';
9072 else
9073 return fill_vert;
9074}
9075#endif
9076
9077/*
9078 * Return TRUE if redrawing should currently be done.
9079 */
9080 int
9081redrawing()
9082{
9083 return (!RedrawingDisabled
9084 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9085}
9086
9087/*
9088 * Return TRUE if printing messages should currently be done.
9089 */
9090 int
9091messaging()
9092{
9093 return (!(p_lz && char_avail() && !KeyTyped));
9094}
9095
9096/*
9097 * Show current status info in ruler and various other places
9098 * If always is FALSE, only show ruler if position has changed.
9099 */
9100 void
9101showruler(always)
9102 int always;
9103{
9104 if (!always && !redrawing())
9105 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009106#ifdef FEAT_INS_EXPAND
9107 if (pum_visible())
9108 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009109# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009110 /* Don't redraw right now, do it later. */
9111 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009112# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009113 return;
9114 }
9115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009117 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009118 {
9119 redraw_custum_statusline(curwin);
9120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 else
9122#endif
9123#ifdef FEAT_CMDL_INFO
9124 win_redr_ruler(curwin, always);
9125#endif
9126
9127#ifdef FEAT_TITLE
9128 if (need_maketitle
9129# ifdef FEAT_STL_OPT
9130 || (p_icon && (stl_syntax & STL_IN_ICON))
9131 || (p_title && (stl_syntax & STL_IN_TITLE))
9132# endif
9133 )
9134 maketitle();
9135#endif
9136}
9137
9138#ifdef FEAT_CMDL_INFO
9139 static void
9140win_redr_ruler(wp, always)
9141 win_T *wp;
9142 int always;
9143{
9144 char_u buffer[70];
9145 int row;
9146 int fillchar;
9147 int attr;
9148 int empty_line = FALSE;
9149 colnr_T virtcol;
9150 int i;
9151 int o;
9152#ifdef FEAT_VERTSPLIT
9153 int this_ru_col;
9154 int off = 0;
9155 int width = Columns;
9156# define WITH_OFF(x) x
9157# define WITH_WIDTH(x) x
9158#else
9159# define WITH_OFF(x) 0
9160# define WITH_WIDTH(x) Columns
9161# define this_ru_col ru_col
9162#endif
9163
9164 /* If 'ruler' off or redrawing disabled, don't do anything */
9165 if (!p_ru)
9166 return;
9167
9168 /*
9169 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9170 * after deleting lines, before cursor.lnum is corrected.
9171 */
9172 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9173 return;
9174
9175#ifdef FEAT_INS_EXPAND
9176 /* Don't draw the ruler while doing insert-completion, it might overwrite
9177 * the (long) mode message. */
9178# ifdef FEAT_WINDOWS
9179 if (wp == lastwin && lastwin->w_status_height == 0)
9180# endif
9181 if (edit_submode != NULL)
9182 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009183 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9184 if (pum_visible())
9185 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186#endif
9187
9188#ifdef FEAT_STL_OPT
9189 if (*p_ruf)
9190 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009191 int save_called_emsg = called_emsg;
9192
9193 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009195 if (called_emsg)
9196 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009197 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009198 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 return;
9200 }
9201#endif
9202
9203 /*
9204 * Check if not in Insert mode and the line is empty (will show "0-1").
9205 */
9206 if (!(State & INSERT)
9207 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9208 empty_line = TRUE;
9209
9210 /*
9211 * Only draw the ruler when something changed.
9212 */
9213 validate_virtcol_win(wp);
9214 if ( redraw_cmdline
9215 || always
9216 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9217 || wp->w_cursor.col != wp->w_ru_cursor.col
9218 || wp->w_virtcol != wp->w_ru_virtcol
9219#ifdef FEAT_VIRTUALEDIT
9220 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9221#endif
9222 || wp->w_topline != wp->w_ru_topline
9223 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9224#ifdef FEAT_DIFF
9225 || wp->w_topfill != wp->w_ru_topfill
9226#endif
9227 || empty_line != wp->w_ru_empty)
9228 {
9229 cursor_off();
9230#ifdef FEAT_WINDOWS
9231 if (wp->w_status_height)
9232 {
9233 row = W_WINROW(wp) + wp->w_height;
9234 fillchar = fillchar_status(&attr, wp == curwin);
9235# ifdef FEAT_VERTSPLIT
9236 off = W_WINCOL(wp);
9237 width = W_WIDTH(wp);
9238# endif
9239 }
9240 else
9241#endif
9242 {
9243 row = Rows - 1;
9244 fillchar = ' ';
9245 attr = 0;
9246#ifdef FEAT_VERTSPLIT
9247 width = Columns;
9248 off = 0;
9249#endif
9250 }
9251
9252 /* In list mode virtcol needs to be recomputed */
9253 virtcol = wp->w_virtcol;
9254 if (wp->w_p_list && lcs_tab1 == NUL)
9255 {
9256 wp->w_p_list = FALSE;
9257 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9258 wp->w_p_list = TRUE;
9259 }
9260
9261 /*
9262 * Some sprintfs return the length, some return a pointer.
9263 * To avoid portability problems we use strlen() here.
9264 */
9265 sprintf((char *)buffer, "%ld,",
9266 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9267 ? 0L
9268 : (long)(wp->w_cursor.lnum));
9269 col_print(buffer + STRLEN(buffer),
9270 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9271 (int)virtcol + 1);
9272
9273 /*
9274 * Add a "50%" if there is room for it.
9275 * On the last line, don't print in the last column (scrolls the
9276 * screen up on some terminals).
9277 */
9278 i = (int)STRLEN(buffer);
9279 get_rel_pos(wp, buffer + i + 1);
9280 o = i + vim_strsize(buffer + i + 1);
9281#ifdef FEAT_WINDOWS
9282 if (wp->w_status_height == 0) /* can't use last char of screen */
9283#endif
9284 ++o;
9285#ifdef FEAT_VERTSPLIT
9286 this_ru_col = ru_col - (Columns - width);
9287 if (this_ru_col < 0)
9288 this_ru_col = 0;
9289#endif
9290 /* Never use more than half the window/screen width, leave the other
9291 * half for the filename. */
9292 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9293 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9294 if (this_ru_col + o < WITH_WIDTH(width))
9295 {
9296 while (this_ru_col + o < WITH_WIDTH(width))
9297 {
9298#ifdef FEAT_MBYTE
9299 if (has_mbyte)
9300 i += (*mb_char2bytes)(fillchar, buffer + i);
9301 else
9302#endif
9303 buffer[i++] = fillchar;
9304 ++o;
9305 }
9306 get_rel_pos(wp, buffer + i);
9307 }
9308 /* Truncate at window boundary. */
9309#ifdef FEAT_MBYTE
9310 if (has_mbyte)
9311 {
9312 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009313 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 {
9315 o += (*mb_ptr2cells)(buffer + i);
9316 if (this_ru_col + o > WITH_WIDTH(width))
9317 {
9318 buffer[i] = NUL;
9319 break;
9320 }
9321 }
9322 }
9323 else
9324#endif
9325 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9326 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9327
9328 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9329 i = redraw_cmdline;
9330 screen_fill(row, row + 1,
9331 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9332 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9333 fillchar, fillchar, attr);
9334 /* don't redraw the cmdline because of showing the ruler */
9335 redraw_cmdline = i;
9336 wp->w_ru_cursor = wp->w_cursor;
9337 wp->w_ru_virtcol = wp->w_virtcol;
9338 wp->w_ru_empty = empty_line;
9339 wp->w_ru_topline = wp->w_topline;
9340 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9341#ifdef FEAT_DIFF
9342 wp->w_ru_topfill = wp->w_topfill;
9343#endif
9344 }
9345}
9346#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009347
9348#if defined(FEAT_LINEBREAK) || defined(PROTO)
9349/*
9350 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009351 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009352 * Otherwise it depends on 'numberwidth' and the line count.
9353 */
9354 int
9355number_width(wp)
9356 win_T *wp;
9357{
9358 int n;
9359 linenr_T lnum;
9360
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009361 lnum = wp->w_buffer->b_ml.ml_line_count;
9362 if (lnum == wp->w_nrwidth_line_count)
9363 return wp->w_nrwidth_width;
9364 wp->w_nrwidth_line_count = lnum;
9365
9366 n = 0;
9367 do
9368 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009369 lnum /= 10;
9370 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009371 } while (lnum > 0);
9372
9373 /* 'numberwidth' gives the minimal width plus one */
9374 if (n < wp->w_p_nuw - 1)
9375 n = wp->w_p_nuw - 1;
9376
9377 wp->w_nrwidth_width = n;
9378 return n;
9379}
9380#endif