blob: 222cd32a0c3022c5c297f7339af4ddc196152d1a [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
2615#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS))
2616# define LINE_ATTR
2617 int line_attr = 0; /* atrribute for the whole line */
2618#endif
2619#ifdef FEAT_SEARCH_EXTRA
2620 match_T *shl; /* points to search_hl or match_hl */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002621#endif
2622#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_MBYTE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002623 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624#endif
2625#ifdef FEAT_ARABIC
2626 int prev_c = 0; /* previous Arabic character */
2627 int prev_c1 = 0; /* first composing char for prev_c */
2628#endif
Bram Moolenaar91170f82006-05-05 21:15:17 +00002629#if defined(FEAT_DIFF) || defined(LINE_ATTR)
2630 int did_line_attr = 0;
2631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633 /* draw_state: items that are drawn in sequence: */
2634#define WL_START 0 /* nothing done yet */
2635#ifdef FEAT_CMDWIN
2636# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2637#else
2638# define WL_CMDLINE WL_START
2639#endif
2640#ifdef FEAT_FOLDING
2641# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2642#else
2643# define WL_FOLD WL_CMDLINE
2644#endif
2645#ifdef FEAT_SIGNS
2646# define WL_SIGN WL_FOLD + 1 /* column for signs */
2647#else
2648# define WL_SIGN WL_FOLD /* column for signs */
2649#endif
2650#define WL_NR WL_SIGN + 1 /* line number */
2651#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2652# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2653#else
2654# define WL_SBR WL_NR
2655#endif
2656#define WL_LINE WL_SBR + 1 /* text in the line */
2657 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002658#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 int feedback_col = 0;
2660 int feedback_old_attr = -1;
2661#endif
2662
2663
2664 if (startrow > endrow) /* past the end already! */
2665 return startrow;
2666
2667 row = startrow;
2668 screen_row = row + W_WINROW(wp);
2669
2670 /*
2671 * To speed up the loop below, set extra_check when there is linebreak,
2672 * trailing white space and/or syntax processing to be done.
2673 */
2674#ifdef FEAT_LINEBREAK
2675 extra_check = wp->w_p_lbr;
2676#else
2677 extra_check = 0;
2678#endif
2679#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002680 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 {
2682 /* Prepare for syntax highlighting in this line. When there is an
2683 * error, stop syntax highlighting. */
2684 save_did_emsg = did_emsg;
2685 did_emsg = FALSE;
2686 syntax_start(wp, lnum);
2687 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002688 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 else
2690 {
2691 did_emsg = save_did_emsg;
2692 has_syntax = TRUE;
2693 extra_check = TRUE;
2694 }
2695 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002696#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002697
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002698#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002699 if (wp->w_p_spell
2700 && *wp->w_buffer->b_p_spl != NUL
2701 && wp->w_buffer->b_langp.ga_len > 0
2702 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002703 {
2704 /* Prepare for spell checking. */
2705 has_spell = TRUE;
2706 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002707
2708 /* Get the start of the next line, so that words that wrap to the next
2709 * line are found too: "et<line-break>al.".
2710 * Trick: skip a few chars for C/shell/Vim comments */
2711 nextline[SPWORDLEN] = NUL;
2712 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2713 {
2714 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2715 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2716 }
2717
2718 /* When a word wrapped from the previous line the start of the current
2719 * line is valid. */
2720 if (lnum == checked_lnum)
2721 cur_checked_col = checked_col;
2722 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002723
2724 /* When there was a sentence end in the previous line may require a
2725 * word starting with capital in this line. In line 1 always check
2726 * the first word. */
2727 if (lnum != capcol_lnum)
2728 cap_col = -1;
2729 if (lnum == 1)
2730 cap_col = 0;
2731 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733#endif
2734
2735 /*
2736 * handle visual active in this window
2737 */
2738 fromcol = -10;
2739 tocol = MAXCOL;
2740#ifdef FEAT_VISUAL
2741 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2742 {
2743 /* Visual is after curwin->w_cursor */
2744 if (ltoreq(curwin->w_cursor, VIsual))
2745 {
2746 top = &curwin->w_cursor;
2747 bot = &VIsual;
2748 }
2749 else /* Visual is before curwin->w_cursor */
2750 {
2751 top = &VIsual;
2752 bot = &curwin->w_cursor;
2753 }
2754 if (VIsual_mode == Ctrl_V) /* block mode */
2755 {
2756 if (lnum >= top->lnum && lnum <= bot->lnum)
2757 {
2758 fromcol = wp->w_old_cursor_fcol;
2759 tocol = wp->w_old_cursor_lcol;
2760 }
2761 }
2762 else /* non-block mode */
2763 {
2764 if (lnum > top->lnum && lnum <= bot->lnum)
2765 fromcol = 0;
2766 else if (lnum == top->lnum)
2767 {
2768 if (VIsual_mode == 'V') /* linewise */
2769 fromcol = 0;
2770 else
2771 {
2772 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2773 if (gchar_pos(top) == NUL)
2774 tocol = fromcol + 1;
2775 }
2776 }
2777 if (VIsual_mode != 'V' && lnum == bot->lnum)
2778 {
2779 if (*p_sel == 'e' && bot->col == 0
2780#ifdef FEAT_VIRTUALEDIT
2781 && bot->coladd == 0
2782#endif
2783 )
2784 {
2785 fromcol = -10;
2786 tocol = MAXCOL;
2787 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002788 else if (bot->col == MAXCOL)
2789 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 else
2791 {
2792 pos = *bot;
2793 if (*p_sel == 'e')
2794 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2795 else
2796 {
2797 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2798 ++tocol;
2799 }
2800 }
2801 }
2802 }
2803
2804#ifndef MSDOS
2805 /* Check if the character under the cursor should not be inverted */
2806 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2807# ifdef FEAT_GUI
2808 && !gui.in_use
2809# endif
2810 )
2811 noinvcur = TRUE;
2812#endif
2813
2814 /* if inverting in this line set area_highlighting */
2815 if (fromcol >= 0)
2816 {
2817 area_highlighting = TRUE;
2818 attr = hl_attr(HLF_V);
2819#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2820 if (clip_star.available && !clip_star.owned && clip_isautosel())
2821 attr = hl_attr(HLF_VNC);
2822#endif
2823 }
2824 }
2825
2826 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002827 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 */
2829 else
2830#endif /* FEAT_VISUAL */
2831 if (highlight_match
2832 && wp == curwin
2833 && lnum >= curwin->w_cursor.lnum
2834 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2835 {
2836 if (lnum == curwin->w_cursor.lnum)
2837 getvcol(curwin, &(curwin->w_cursor),
2838 (colnr_T *)&fromcol, NULL, NULL);
2839 else
2840 fromcol = 0;
2841 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2842 {
2843 pos.lnum = lnum;
2844 pos.col = search_match_endcol;
2845 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2846 }
2847 else
2848 tocol = MAXCOL;
2849 if (fromcol == tocol) /* do at least one character */
2850 tocol = fromcol + 1; /* happens when past end of line */
2851 area_highlighting = TRUE;
2852 attr = hl_attr(HLF_I);
2853 }
2854
2855#ifdef FEAT_DIFF
2856 filler_lines = diff_check(wp, lnum);
2857 if (filler_lines < 0)
2858 {
2859 if (filler_lines == -1)
2860 {
2861 if (diff_find_change(wp, lnum, &change_start, &change_end))
2862 diff_hlf = HLF_ADD; /* added line */
2863 else if (change_start == 0)
2864 diff_hlf = HLF_TXD; /* changed text */
2865 else
2866 diff_hlf = HLF_CHD; /* changed line */
2867 }
2868 else
2869 diff_hlf = HLF_ADD; /* added line */
2870 filler_lines = 0;
2871 area_highlighting = TRUE;
2872 }
2873 if (lnum == wp->w_topline)
2874 filler_lines = wp->w_topfill;
2875 filler_todo = filler_lines;
2876#endif
2877
2878#ifdef LINE_ATTR
2879# ifdef FEAT_SIGNS
2880 /* If this line has a sign with line highlighting set line_attr. */
2881 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2882 if (v != 0)
2883 line_attr = sign_get_attr((int)v, TRUE);
2884# endif
2885# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2886 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002887 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 line_attr = hl_attr(HLF_L);
2889# endif
2890 if (line_attr != 0)
2891 area_highlighting = TRUE;
2892#endif
2893
2894 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2895 ptr = line;
2896
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002897#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002898 if (has_spell)
2899 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002900 /* For checking first word with a capital skip white space. */
2901 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002902 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002903
Bram Moolenaar30abd282005-06-22 22:35:10 +00002904 /* To be able to spell-check over line boundaries copy the end of the
2905 * current line into nextline[]. Above the start of the next line was
2906 * copied to nextline[SPWORDLEN]. */
2907 if (nextline[SPWORDLEN] == NUL)
2908 {
2909 /* No next line or it is empty. */
2910 nextlinecol = MAXCOL;
2911 nextline_idx = 0;
2912 }
2913 else
2914 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002915 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002916 if (v < SPWORDLEN)
2917 {
2918 /* Short line, use it completely and append the start of the
2919 * next line. */
2920 nextlinecol = 0;
2921 mch_memmove(nextline, line, (size_t)v);
2922 mch_memmove(nextline + v, nextline + SPWORDLEN,
2923 STRLEN(nextline + SPWORDLEN) + 1);
2924 nextline_idx = v + 1;
2925 }
2926 else
2927 {
2928 /* Long line, use only the last SPWORDLEN bytes. */
2929 nextlinecol = v - SPWORDLEN;
2930 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2931 nextline_idx = SPWORDLEN + 1;
2932 }
2933 }
2934 }
2935#endif
2936
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 /* find start of trailing whitespace */
2938 if (wp->w_p_list && lcs_trail)
2939 {
2940 trailcol = (colnr_T)STRLEN(ptr);
2941 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2942 --trailcol;
2943 trailcol += (colnr_T) (ptr - line);
2944 extra_check = TRUE;
2945 }
2946
2947 /*
2948 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2949 * first character to be displayed.
2950 */
2951 if (wp->w_p_wrap)
2952 v = wp->w_skipcol;
2953 else
2954 v = wp->w_leftcol;
2955 if (v > 0)
2956 {
2957#ifdef FEAT_MBYTE
2958 char_u *prev_ptr = ptr;
2959#endif
2960 while (vcol < v && *ptr != NUL)
2961 {
2962 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2963 vcol += c;
2964#ifdef FEAT_MBYTE
2965 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002967 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
2969
2970#ifdef FEAT_VIRTUALEDIT
2971 /* When 'virtualedit' is set the end of the line may be before the
2972 * start of the displayed part. */
2973 if (vcol < v && *ptr == NUL && virtual_active())
2974 vcol = v;
2975#endif
2976
2977 /* Handle a character that's not completely on the screen: Put ptr at
2978 * that character but skip the first few screen characters. */
2979 if (vcol > v)
2980 {
2981 vcol -= c;
2982#ifdef FEAT_MBYTE
2983 ptr = prev_ptr;
2984#else
2985 --ptr;
2986#endif
2987 n_skip = v - vcol;
2988 }
2989
2990 /*
2991 * Adjust for when the inverted text is before the screen,
2992 * and when the start of the inverted text is before the screen.
2993 */
2994 if (tocol <= vcol)
2995 fromcol = 0;
2996 else if (fromcol >= 0 && fromcol < vcol)
2997 fromcol = vcol;
2998
2999#ifdef FEAT_LINEBREAK
3000 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3001 if (wp->w_p_wrap)
3002 need_showbreak = TRUE;
3003#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003004#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003005 /* When spell checking a word we need to figure out the start of the
3006 * word and if it's badly spelled or not. */
3007 if (has_spell)
3008 {
3009 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003010 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003011
3012 pos = wp->w_cursor;
3013 wp->w_cursor.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003014 wp->w_cursor.col = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003015 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003016 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003017 {
3018 /* no bad word found at line start, don't check until end of a
3019 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003020 spell_hlf = HLF_COUNT;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003021 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003022 }
3023 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003024 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003025 /* bad word found, use attributes until end of word */
3026 word_end = wp->w_cursor.col + len + 1;
3027
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003028 /* Turn index into actual attributes. */
3029 if (spell_hlf != HLF_COUNT)
3030 spell_attr = highlight_attr[spell_hlf];
3031 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003032 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003033
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003034# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003035 /* Need to restart syntax highlighting for this line. */
3036 if (has_syntax)
3037 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003038# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003039 }
3040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
3042
3043 /*
3044 * Correct highlighting for cursor that can't be disabled.
3045 * Avoids having to check this for each character.
3046 */
3047 if (fromcol >= 0)
3048 {
3049 if (noinvcur)
3050 {
3051 if ((colnr_T)fromcol == wp->w_virtcol)
3052 {
3053 /* highlighting starts at cursor, let it start just after the
3054 * cursor */
3055 fromcol_prev = fromcol;
3056 fromcol = -1;
3057 }
3058 else if ((colnr_T)fromcol < wp->w_virtcol)
3059 /* restart highlighting after the cursor */
3060 fromcol_prev = wp->w_virtcol;
3061 }
3062 if (fromcol >= tocol)
3063 fromcol = -1;
3064 }
3065
3066#ifdef FEAT_SEARCH_EXTRA
3067 /*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003068 * Handle highlighting the last used search pattern and ":match".
3069 * Do this for both search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003071 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003073 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003074 shl->startcol = MAXCOL;
3075 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 shl->attr_cur = 0;
3077 if (shl->rm.regprog != NULL)
3078 {
3079 v = (long)(ptr - line);
3080 next_search_hl(wp, shl, lnum, (colnr_T)v);
3081
3082 /* Need to get the line again, a multi-line regexp may have made it
3083 * invalid. */
3084 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3085 ptr = line + v;
3086
3087 if (shl->lnum != 0 && shl->lnum <= lnum)
3088 {
3089 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003090 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003092 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3094 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003095 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003097 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003099 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 {
3101#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003102 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003103 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 else
3105#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003106 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003108 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 shl->attr_cur = shl->attr;
3111 search_attr = shl->attr;
3112 }
3113 area_highlighting = TRUE;
3114 }
3115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117#endif
3118
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003119#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003120 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3121 * active, because it's not clear what is selected then. */
3122 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003123 {
3124 line_attr = hl_attr(HLF_CUL);
3125 area_highlighting = TRUE;
3126 }
3127#endif
3128
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003129 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 col = 0;
3131#ifdef FEAT_RIGHTLEFT
3132 if (wp->w_p_rl)
3133 {
3134 /* Rightleft window: process the text in the normal direction, but put
3135 * it in current_ScreenLine[] from right to left. Start at the
3136 * rightmost column of the window. */
3137 col = W_WIDTH(wp) - 1;
3138 off += col;
3139 }
3140#endif
3141
3142 /*
3143 * Repeat for the whole displayed line.
3144 */
3145 for (;;)
3146 {
3147 /* Skip this quickly when working on the text. */
3148 if (draw_state != WL_LINE)
3149 {
3150#ifdef FEAT_CMDWIN
3151 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3152 {
3153 draw_state = WL_CMDLINE;
3154 if (cmdwin_type != 0 && wp == curwin)
3155 {
3156 /* Draw the cmdline character. */
3157 *extra = cmdwin_type;
3158 n_extra = 1;
3159 p_extra = extra;
3160 c_extra = NUL;
3161 char_attr = hl_attr(HLF_AT);
3162 }
3163 }
3164#endif
3165
3166#ifdef FEAT_FOLDING
3167 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3168 {
3169 draw_state = WL_FOLD;
3170 if (wp->w_p_fdc > 0)
3171 {
3172 /* Draw the 'foldcolumn'. */
3173 fill_foldcolumn(extra, wp, FALSE, lnum);
3174 n_extra = wp->w_p_fdc;
3175 p_extra = extra;
3176 c_extra = NUL;
3177 char_attr = hl_attr(HLF_FC);
3178 }
3179 }
3180#endif
3181
3182#ifdef FEAT_SIGNS
3183 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3184 {
3185 draw_state = WL_SIGN;
3186 /* Show the sign column when there are any signs in this
3187 * buffer or when using Netbeans. */
3188 if (draw_signcolumn(wp)
3189# ifdef FEAT_DIFF
3190 && filler_todo <= 0
3191# endif
3192 )
3193 {
3194 int_u text_sign;
3195# ifdef FEAT_SIGN_ICONS
3196 int_u icon_sign;
3197# endif
3198
3199 /* Draw two cells with the sign value or blank. */
3200 c_extra = ' ';
3201 char_attr = hl_attr(HLF_SC);
3202 n_extra = 2;
3203
3204 if (row == startrow)
3205 {
3206 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3207 SIGN_TEXT);
3208# ifdef FEAT_SIGN_ICONS
3209 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3210 SIGN_ICON);
3211 if (gui.in_use && icon_sign != 0)
3212 {
3213 /* Use the image in this position. */
3214 c_extra = SIGN_BYTE;
3215# ifdef FEAT_NETBEANS_INTG
3216 if (buf_signcount(wp->w_buffer, lnum) > 1)
3217 c_extra = MULTISIGN_BYTE;
3218# endif
3219 char_attr = icon_sign;
3220 }
3221 else
3222# endif
3223 if (text_sign != 0)
3224 {
3225 p_extra = sign_get_text(text_sign);
3226 if (p_extra != NULL)
3227 {
3228 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003229 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
3231 char_attr = sign_get_attr(text_sign, FALSE);
3232 }
3233 }
3234 }
3235 }
3236#endif
3237
3238 if (draw_state == WL_NR - 1 && n_extra == 0)
3239 {
3240 draw_state = WL_NR;
3241 /* Display the line number. After the first fill with blanks
3242 * when the 'n' flag isn't in 'cpo' */
3243 if (wp->w_p_nu
3244 && (row == startrow
3245#ifdef FEAT_DIFF
3246 + filler_lines
3247#endif
3248 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3249 {
3250 /* Draw the line number (empty space after wrapping). */
3251 if (row == startrow
3252#ifdef FEAT_DIFF
3253 + filler_lines
3254#endif
3255 )
3256 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003257 sprintf((char *)extra, "%*ld ",
3258 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 if (wp->w_skipcol > 0)
3260 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3261 *p_extra = '-';
3262#ifdef FEAT_RIGHTLEFT
3263 if (wp->w_p_rl) /* reverse line numbers */
3264 rl_mirror(extra);
3265#endif
3266 p_extra = extra;
3267 c_extra = NUL;
3268 }
3269 else
3270 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003271 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003273#ifdef FEAT_SYN_HL
3274 /* When 'cursorline' is set highlight the line number of
3275 * the current line differently. */
3276 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3277 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 }
3281
3282#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3283 if (draw_state == WL_SBR - 1 && n_extra == 0)
3284 {
3285 draw_state = WL_SBR;
3286# ifdef FEAT_DIFF
3287 if (filler_todo > 0)
3288 {
3289 /* Draw "deleted" diff line(s). */
3290 if (char2cells(fill_diff) > 1)
3291 c_extra = '-';
3292 else
3293 c_extra = fill_diff;
3294# ifdef FEAT_RIGHTLEFT
3295 if (wp->w_p_rl)
3296 n_extra = col + 1;
3297 else
3298# endif
3299 n_extra = W_WIDTH(wp) - col;
3300 char_attr = hl_attr(HLF_DED);
3301 }
3302# endif
3303# ifdef FEAT_LINEBREAK
3304 if (*p_sbr != NUL && need_showbreak)
3305 {
3306 /* Draw 'showbreak' at the start of each broken line. */
3307 p_extra = p_sbr;
3308 c_extra = NUL;
3309 n_extra = (int)STRLEN(p_sbr);
3310 char_attr = hl_attr(HLF_AT);
3311 need_showbreak = FALSE;
3312 /* Correct end of highlighted area for 'showbreak',
3313 * required when 'linebreak' is also set. */
3314 if (tocol == vcol)
3315 tocol += n_extra;
3316 }
3317# endif
3318 }
3319#endif
3320
3321 if (draw_state == WL_LINE - 1 && n_extra == 0)
3322 {
3323 draw_state = WL_LINE;
3324 if (saved_n_extra)
3325 {
3326 /* Continue item from end of wrapped line. */
3327 n_extra = saved_n_extra;
3328 c_extra = saved_c_extra;
3329 p_extra = saved_p_extra;
3330 char_attr = saved_char_attr;
3331 }
3332 else
3333 char_attr = 0;
3334 }
3335 }
3336
3337 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003338 if (dollar_vcol != 0 && wp == curwin
3339 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#ifdef FEAT_DIFF
3341 && filler_todo <= 0
3342#endif
3343 )
3344 {
3345 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3346 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003347 /* Pretend we have finished updating the window. Except when
3348 * 'cursorcolumn' is set. */
3349#ifdef FEAT_SYN_HL
3350 if (wp->w_p_cuc)
3351 row = wp->w_cline_row + wp->w_cline_height;
3352 else
3353#endif
3354 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 break;
3356 }
3357
3358 if (draw_state == WL_LINE && area_highlighting)
3359 {
3360 /* handle Visual or match highlighting in this line */
3361 if (vcol == fromcol
3362#ifdef FEAT_MBYTE
3363 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3364 && (*mb_ptr2cells)(ptr) > 1)
3365#endif
3366 || ((int)vcol_prev == fromcol_prev
3367 && vcol < tocol))
3368 area_attr = attr; /* start highlighting */
3369 else if (area_attr != 0
3370 && (vcol == tocol
3371 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374#ifdef FEAT_SEARCH_EXTRA
3375 if (!n_extra)
3376 {
3377 /*
3378 * Check for start/end of search pattern match.
3379 * After end, check for start/end of next match.
3380 * When another match, have to check for start again.
3381 * Watch out for matching an empty string!
3382 * Do this first for search_hl, then for match_hl, so that
3383 * ":match" overrules 'hlsearch'.
3384 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003385 v = (long)(ptr - line);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003386 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003388 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 while (shl->rm.regprog != NULL)
3390 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003391 if (shl->startcol != MAXCOL
3392 && v >= (long)shl->startcol
3393 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
3395 shl->attr_cur = shl->attr;
3396 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003397 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
3399 shl->attr_cur = 0;
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 next_search_hl(wp, shl, lnum, (colnr_T)v);
3402
3403 /* Need to get the line again, a multi-line regexp
3404 * may have made it invalid. */
3405 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3406 ptr = line + v;
3407
3408 if (shl->lnum == lnum)
3409 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003410 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003412 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003414 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003416 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
3418 /* highlight empty match, try again after
3419 * it */
3420#ifdef FEAT_MBYTE
3421 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003422 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003423 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 else
3425#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003426 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428
3429 /* Loop to check if the match starts at the
3430 * current position */
3431 continue;
3432 }
3433 }
3434 break;
3435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003437
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 /* ":match" highlighting overrules 'hlsearch' */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003439 for (i = 0; i <= 3; ++i)
3440 if (i == 3)
3441 search_attr = search_hl.attr_cur;
3442 else if (match_hl[i].attr_cur != 0)
3443 {
3444 search_attr = match_hl[i].attr_cur;
3445 break;
3446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448#endif
3449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003451 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 if (diff_hlf == HLF_CHD && ptr - line >= change_start)
3454 diff_hlf = HLF_TXD; /* changed text */
3455 if (diff_hlf == HLF_TXD && ptr - line > change_end)
3456 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003457 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003460
3461 /* Decide which of the highlight attributes to use. */
3462 attr_pri = TRUE;
3463 if (area_attr != 0)
3464 char_attr = area_attr;
3465 else if (search_attr != 0)
3466 char_attr = search_attr;
3467#ifdef LINE_ATTR
3468 /* Use line_attr when not in the Visual or 'incsearch' area
3469 * (area_attr may be 0 when "noinvcur" is set). */
3470 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3471 || (vcol < fromcol || vcol >= tocol)))
3472 char_attr = line_attr;
3473#endif
3474 else
3475 {
3476 attr_pri = FALSE;
3477#ifdef FEAT_SYN_HL
3478 if (has_syntax)
3479 char_attr = syntax_attr;
3480 else
3481#endif
3482 char_attr = 0;
3483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485
3486 /*
3487 * Get the next character to put on the screen.
3488 */
3489 /*
3490 * The 'extra' array contains the extra stuff that is inserted to
3491 * represent special characters (non-printable stuff). When all
3492 * characters are the same, c_extra is used.
3493 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3494 */
3495 if (n_extra > 0)
3496 {
3497 if (c_extra != NUL)
3498 {
3499 c = c_extra;
3500#ifdef FEAT_MBYTE
3501 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3502 if (enc_utf8 && (*mb_char2len)(c) > 1)
3503 {
3504 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003505 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003506 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508 else
3509 mb_utf8 = FALSE;
3510#endif
3511 }
3512 else
3513 {
3514 c = *p_extra;
3515#ifdef FEAT_MBYTE
3516 if (has_mbyte)
3517 {
3518 mb_c = c;
3519 if (enc_utf8)
3520 {
3521 /* If the UTF-8 character is more than one byte:
3522 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003523 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 mb_utf8 = FALSE;
3525 if (mb_l > n_extra)
3526 mb_l = 1;
3527 else if (mb_l > 1)
3528 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003529 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003531 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
3533 }
3534 else
3535 {
3536 /* if this is a DBCS character, put it in "mb_c" */
3537 mb_l = MB_BYTE2LEN(c);
3538 if (mb_l >= n_extra)
3539 mb_l = 1;
3540 else if (mb_l > 1)
3541 mb_c = (c << 8) + p_extra[1];
3542 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003543 if (mb_l == 0) /* at the NUL at end-of-line */
3544 mb_l = 1;
3545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 /* If a double-width char doesn't fit display a '>' in the
3547 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003548 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549# ifdef FEAT_RIGHTLEFT
3550 wp->w_p_rl ? (col <= 0) :
3551# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003552 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 && (*mb_char2cells)(mb_c) == 2)
3554 {
3555 c = '>';
3556 mb_c = c;
3557 mb_l = 1;
3558 mb_utf8 = FALSE;
3559 multi_attr = hl_attr(HLF_AT);
3560 /* put the pointer back to output the double-width
3561 * character at the start of the next line. */
3562 ++n_extra;
3563 --p_extra;
3564 }
3565 else
3566 {
3567 n_extra -= mb_l - 1;
3568 p_extra += mb_l - 1;
3569 }
3570 }
3571#endif
3572 ++p_extra;
3573 }
3574 --n_extra;
3575 }
3576 else
3577 {
3578 /*
3579 * Get a character from the line itself.
3580 */
3581 c = *ptr;
3582#ifdef FEAT_MBYTE
3583 if (has_mbyte)
3584 {
3585 mb_c = c;
3586 if (enc_utf8)
3587 {
3588 /* If the UTF-8 character is more than one byte: Decode it
3589 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003590 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 mb_utf8 = FALSE;
3592 if (mb_l > 1)
3593 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003594 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 /* Overlong encoded ASCII or ASCII with composing char
3596 * is displayed normally, except a NUL. */
3597 if (mb_c < 0x80)
3598 c = mb_c;
3599 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003600
3601 /* At start of the line we can have a composing char.
3602 * Draw it as a space with a composing char. */
3603 if (utf_iscomposing(mb_c))
3604 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003605 for (i = Screen_mco - 1; i > 0; --i)
3606 u8cc[i] = u8cc[i - 1];
3607 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003608 mb_c = ' ';
3609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611
3612 if ((mb_l == 1 && c >= 0x80)
3613 || (mb_l >= 1 && mb_c == 0)
3614 || (mb_l > 1 && (!vim_isprintc(mb_c)
3615 || mb_c >= 0x10000)))
3616 {
3617 /*
3618 * Illegal UTF-8 byte: display as <xx>.
3619 * Non-BMP character : display as ? or fullwidth ?.
3620 */
3621 if (mb_c < 0x10000)
3622 {
3623 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003624# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (wp->w_p_rl) /* reverse */
3626 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629 else if (utf_char2cells(mb_c) != 2)
3630 STRCPY(extra, "?");
3631 else
3632 /* 0xff1f in UTF-8: full-width '?' */
3633 STRCPY(extra, "\357\274\237");
3634
3635 p_extra = extra;
3636 c = *p_extra;
3637 mb_c = mb_ptr2char_adv(&p_extra);
3638 mb_utf8 = (c >= 0x80);
3639 n_extra = (int)STRLEN(p_extra);
3640 c_extra = NUL;
3641 if (area_attr == 0 && search_attr == 0)
3642 {
3643 n_attr = n_extra + 1;
3644 extra_attr = hl_attr(HLF_8);
3645 saved_attr2 = char_attr; /* save current attr */
3646 }
3647 }
3648 else if (mb_l == 0) /* at the NUL at end-of-line */
3649 mb_l = 1;
3650#ifdef FEAT_ARABIC
3651 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3652 {
3653 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003654 int pc, pc1, nc;
3655 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
3657 /* The idea of what is the previous and next
3658 * character depends on 'rightleft'. */
3659 if (wp->w_p_rl)
3660 {
3661 pc = prev_c;
3662 pc1 = prev_c1;
3663 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003664 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666 else
3667 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003668 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003670 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672 prev_c = mb_c;
3673
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003674 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 else
3677 prev_c = mb_c;
3678#endif
3679 }
3680 else /* enc_dbcs */
3681 {
3682 mb_l = MB_BYTE2LEN(c);
3683 if (mb_l == 0) /* at the NUL at end-of-line */
3684 mb_l = 1;
3685 else if (mb_l > 1)
3686 {
3687 /* We assume a second byte below 32 is illegal.
3688 * Hopefully this is OK for all double-byte encodings!
3689 */
3690 if (ptr[1] >= 32)
3691 mb_c = (c << 8) + ptr[1];
3692 else
3693 {
3694 if (ptr[1] == NUL)
3695 {
3696 /* head byte at end of line */
3697 mb_l = 1;
3698 transchar_nonprint(extra, c);
3699 }
3700 else
3701 {
3702 /* illegal tail byte */
3703 mb_l = 2;
3704 STRCPY(extra, "XX");
3705 }
3706 p_extra = extra;
3707 n_extra = (int)STRLEN(extra) - 1;
3708 c_extra = NUL;
3709 c = *p_extra++;
3710 if (area_attr == 0 && search_attr == 0)
3711 {
3712 n_attr = n_extra + 1;
3713 extra_attr = hl_attr(HLF_8);
3714 saved_attr2 = char_attr; /* save current attr */
3715 }
3716 mb_c = c;
3717 }
3718 }
3719 }
3720 /* If a double-width char doesn't fit display a '>' in the
3721 * last column; the character is displayed at the start of the
3722 * next line. */
3723 if ((
3724# ifdef FEAT_RIGHTLEFT
3725 wp->w_p_rl ? (col <= 0) :
3726# endif
3727 (col >= W_WIDTH(wp) - 1))
3728 && (*mb_char2cells)(mb_c) == 2)
3729 {
3730 c = '>';
3731 mb_c = c;
3732 mb_utf8 = FALSE;
3733 mb_l = 1;
3734 multi_attr = hl_attr(HLF_AT);
3735 /* Put pointer back so that the character will be
3736 * displayed at the start of the next line. */
3737 --ptr;
3738 }
3739 else if (*ptr != NUL)
3740 ptr += mb_l - 1;
3741
3742 /* If a double-width char doesn't fit at the left side display
3743 * a '<' in the first column. */
3744 if (n_skip > 0 && mb_l > 1)
3745 {
3746 extra[0] = '<';
3747 p_extra = extra;
3748 n_extra = 1;
3749 c_extra = NUL;
3750 c = ' ';
3751 if (area_attr == 0 && search_attr == 0)
3752 {
3753 n_attr = n_extra + 1;
3754 extra_attr = hl_attr(HLF_AT);
3755 saved_attr2 = char_attr; /* save current attr */
3756 }
3757 mb_c = c;
3758 mb_utf8 = FALSE;
3759 mb_l = 1;
3760 }
3761
3762 }
3763#endif
3764 ++ptr;
3765
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003766 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003767 if (wp->w_p_list && (c == 160
3768#ifdef FEAT_MBYTE
3769 || (mb_utf8 && mb_c == 160)
3770#endif
3771 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003772 {
3773 c = lcs_nbsp;
3774 if (area_attr == 0 && search_attr == 0)
3775 {
3776 n_attr = 1;
3777 extra_attr = hl_attr(HLF_8);
3778 saved_attr2 = char_attr; /* save current attr */
3779 }
3780#ifdef FEAT_MBYTE
3781 mb_c = c;
3782 if (enc_utf8 && (*mb_char2len)(c) > 1)
3783 {
3784 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003785 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003786 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003787 }
3788 else
3789 mb_utf8 = FALSE;
3790#endif
3791 }
3792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (extra_check)
3794 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003795#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003796 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003797#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003798
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003799#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 /* Get syntax attribute, unless still at the start of the line
3801 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003802 v = (long)(ptr - line);
3803 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 /* Get the syntax attribute for the character. If there
3806 * is an error, disable syntax highlighting. */
3807 save_did_emsg = did_emsg;
3808 did_emsg = FALSE;
3809
Bram Moolenaar217ad922005-03-20 22:37:15 +00003810 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003811# ifdef FEAT_SPELL
3812 has_spell ? &can_spell :
3813# endif
3814 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
3816 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003817 {
3818 wp->w_buffer->b_syn_error = TRUE;
3819 has_syntax = FALSE;
3820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 else
3822 did_emsg = save_did_emsg;
3823
3824 /* Need to get the line again, a multi-line regexp may
3825 * have made it invalid. */
3826 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3827 ptr = line + v;
3828
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003829 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003831 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003832 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003834#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003835
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003836#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003837 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003838 * Only do this when there is no syntax highlighting, the
3839 * @Spell cluster is not used or the current syntax item
3840 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003841 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003842 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003843 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003844# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003845 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003846 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003847# endif
3848 if (c != 0 && (
3849# ifdef FEAT_SYN_HL
3850 !has_syntax ||
3851# endif
3852 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003853 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003854 char_u *prev_ptr, *p;
3855 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003856 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003857# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003858 if (has_mbyte)
3859 {
3860 prev_ptr = ptr - mb_l;
3861 v -= mb_l - 1;
3862 }
3863 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003864# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003865 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003866
3867 /* Use nextline[] if possible, it has the start of the
3868 * next line concatenated. */
3869 if ((prev_ptr - line) - nextlinecol >= 0)
3870 p = nextline + (prev_ptr - line) - nextlinecol;
3871 else
3872 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003873 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003874 len = spell_check(wp, p, &spell_hlf, &cap_col,
3875 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003876 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003877
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003878 /* In Insert mode only highlight a word that
3879 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003880 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003881 && (State & INSERT) != 0
3882 && wp->w_cursor.lnum == lnum
3883 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003884 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003885 && wp->w_cursor.col < (colnr_T)word_end)
3886 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003887 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003888 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003889 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003890
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003891 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003892 && (p - nextline) + len > nextline_idx)
3893 {
3894 /* Remember that the good word continues at the
3895 * start of the next line. */
3896 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003897 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003898 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003899
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003900 /* Turn index into actual attributes. */
3901 if (spell_hlf != HLF_COUNT)
3902 spell_attr = highlight_attr[spell_hlf];
3903
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003904 if (cap_col > 0)
3905 {
3906 if (p != prev_ptr
3907 && (p - nextline) + cap_col >= nextline_idx)
3908 {
3909 /* Remember that the word in the next line
3910 * must start with a capital. */
3911 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003912 cap_col = (int)((p - nextline) + cap_col
3913 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003914 }
3915 else
3916 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003917 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003918 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003919 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003920 }
3921 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003922 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003923 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003924 char_attr = hl_combine_attr(char_attr, spell_attr);
3925 else
3926 char_attr = hl_combine_attr(spell_attr, char_attr);
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928#endif
3929#ifdef FEAT_LINEBREAK
3930 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00003931 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 */
3933 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003934 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 n_extra = win_lbr_chartabsize(wp, ptr - (
3937# ifdef FEAT_MBYTE
3938 has_mbyte ? mb_l :
3939# endif
3940 1), (colnr_T)vcol, NULL) - 1;
3941 c_extra = ' ';
3942 if (vim_iswhite(c))
3943 c = ' ';
3944 }
3945#endif
3946
3947 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3948 {
3949 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003950 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
3952 n_attr = 1;
3953 extra_attr = hl_attr(HLF_8);
3954 saved_attr2 = char_attr; /* save current attr */
3955 }
3956#ifdef FEAT_MBYTE
3957 mb_c = c;
3958 if (enc_utf8 && (*mb_char2len)(c) > 1)
3959 {
3960 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003961 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003962 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 else
3965 mb_utf8 = FALSE;
3966#endif
3967 }
3968 }
3969
3970 /*
3971 * Handling of non-printable characters.
3972 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
3975 /*
3976 * when getting a character from the file, we may have to
3977 * turn it into something else on the way to putting it
3978 * into "ScreenLines".
3979 */
3980 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3981 {
3982 /* tab amount depends on current column */
3983 n_extra = (int)wp->w_buffer->b_p_ts
3984 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3985#ifdef FEAT_MBYTE
3986 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3987#endif
3988 if (wp->w_p_list)
3989 {
3990 c = lcs_tab1;
3991 c_extra = lcs_tab2;
3992 n_attr = n_extra + 1;
3993 extra_attr = hl_attr(HLF_8);
3994 saved_attr2 = char_attr; /* save current attr */
3995#ifdef FEAT_MBYTE
3996 mb_c = c;
3997 if (enc_utf8 && (*mb_char2len)(c) > 1)
3998 {
3999 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004000 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004001 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003#endif
4004 }
4005 else
4006 {
4007 c_extra = ' ';
4008 c = ' ';
4009 }
4010 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004011 else if (c == NUL
4012 && ((wp->w_p_list && lcs_eol > 0)
4013 || ((fromcol >= 0 || fromcol_prev >= 0)
4014 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004015#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004016 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004017#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004018 && (
4019# ifdef FEAT_RIGHTLEFT
4020 wp->w_p_rl ? (col >= 0) :
4021# endif
4022 (col < W_WIDTH(wp)))
4023 && !(noinvcur
4024 && (colnr_T)vcol == wp->w_virtcol)))
4025 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004027 /* Display a '$' after the line or highlight an extra
4028 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4030 /* For a diff line the highlighting continues after the
4031 * "$". */
4032 if (
4033# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004034 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035# ifdef LINE_ATTR
4036 &&
4037# endif
4038# endif
4039# ifdef LINE_ATTR
4040 line_attr == 0
4041# endif
4042 )
4043#endif
4044 {
4045#ifdef FEAT_VIRTUALEDIT
4046 /* In virtualedit, visual selections may extend
4047 * beyond end of line. */
4048 if (area_highlighting && virtual_active()
4049 && tocol != MAXCOL && vcol < tocol)
4050 n_extra = 0;
4051 else
4052#endif
4053 {
4054 p_extra = at_end_str;
4055 n_extra = 1;
4056 c_extra = NUL;
4057 }
4058 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004059 if (wp->w_p_list)
4060 c = lcs_eol;
4061 else
4062 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 lcs_eol_one = -1;
4064 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004065 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
4067 extra_attr = hl_attr(HLF_AT);
4068 n_attr = 1;
4069 }
4070#ifdef FEAT_MBYTE
4071 mb_c = c;
4072 if (enc_utf8 && (*mb_char2len)(c) > 1)
4073 {
4074 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004075 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004076 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078 else
4079 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4080#endif
4081 }
4082 else if (c != NUL)
4083 {
4084 p_extra = transchar(c);
4085#ifdef FEAT_RIGHTLEFT
4086 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4087 rl_mirror(p_extra); /* reverse "<12>" */
4088#endif
4089 n_extra = byte2cells(c) - 1;
4090 c_extra = NUL;
4091 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004092 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
4094 n_attr = n_extra + 1;
4095 extra_attr = hl_attr(HLF_8);
4096 saved_attr2 = char_attr; /* save current attr */
4097 }
4098#ifdef FEAT_MBYTE
4099 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4100#endif
4101 }
4102#ifdef FEAT_VIRTUALEDIT
4103 else if (VIsual_active
4104 && (VIsual_mode == Ctrl_V
4105 || VIsual_mode == 'v')
4106 && virtual_active()
4107 && tocol != MAXCOL
4108 && vcol < tocol
4109 && (
4110# ifdef FEAT_RIGHTLEFT
4111 wp->w_p_rl ? (col >= 0) :
4112# endif
4113 (col < W_WIDTH(wp))))
4114 {
4115 c = ' ';
4116 --ptr; /* put it back at the NUL */
4117 }
4118#endif
4119#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4120 else if ((
4121# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004122 diff_hlf != (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123# ifdef LINE_ATTR
4124 ||
4125# endif
4126# endif
4127# ifdef LINE_ATTR
4128 line_attr != 0
4129# endif
4130 ) && (
4131# ifdef FEAT_RIGHTLEFT
4132 wp->w_p_rl ? (col >= 0) :
4133# endif
4134 (col < W_WIDTH(wp))))
4135 {
4136 /* Highlight until the right side of the window */
4137 c = ' ';
4138 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004139
4140 /* Remember we do the char for line highlighting. */
4141 ++did_line_attr;
4142
4143 /* don't do search HL for the rest of the line */
4144 if (line_attr != 0 && char_attr == search_attr && col > 0)
4145 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146# ifdef FEAT_DIFF
4147 if (diff_hlf == HLF_TXD)
4148 {
4149 diff_hlf = HLF_CHD;
4150 if (attr == 0 || char_attr != attr)
4151 char_attr = hl_attr(diff_hlf);
4152 }
4153# endif
4154 }
4155#endif
4156 }
4157 }
4158
4159 /* Don't override visual selection highlighting. */
4160 if (n_attr > 0
4161 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004162 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 char_attr = extra_attr;
4164
Bram Moolenaar81695252004-12-29 20:58:21 +00004165#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 /* XIM don't send preedit_start and preedit_end, but they send
4167 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4168 * im_is_preediting() here. */
4169 if (xic != NULL
4170 && lnum == curwin->w_cursor.lnum
4171 && (State & INSERT)
4172 && !p_imdisable
4173 && im_is_preediting()
4174 && draw_state == WL_LINE)
4175 {
4176 colnr_T tcol;
4177
4178 if (preedit_end_col == MAXCOL)
4179 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4180 else
4181 tcol = preedit_end_col;
4182 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4183 {
4184 if (feedback_old_attr < 0)
4185 {
4186 feedback_col = 0;
4187 feedback_old_attr = char_attr;
4188 }
4189 char_attr = im_get_feedback_attr(feedback_col);
4190 if (char_attr < 0)
4191 char_attr = feedback_old_attr;
4192 feedback_col++;
4193 }
4194 else if (feedback_old_attr >= 0)
4195 {
4196 char_attr = feedback_old_attr;
4197 feedback_old_attr = -1;
4198 feedback_col = 0;
4199 }
4200 }
4201#endif
4202 /*
4203 * Handle the case where we are in column 0 but not on the first
4204 * character of the line and the user wants us to show us a
4205 * special character (via 'listchars' option "precedes:<char>".
4206 */
4207 if (lcs_prec_todo != NUL
4208 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4209#ifdef FEAT_DIFF
4210 && filler_todo <= 0
4211#endif
4212 && draw_state > WL_NR
4213 && c != NUL)
4214 {
4215 c = lcs_prec;
4216 lcs_prec_todo = NUL;
4217#ifdef FEAT_MBYTE
4218 mb_c = c;
4219 if (enc_utf8 && (*mb_char2len)(c) > 1)
4220 {
4221 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004222 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004223 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 else
4226 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4227#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004228 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
4230 saved_attr3 = char_attr; /* save current attr */
4231 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4232 n_attr3 = 1;
4233 }
4234 }
4235
4236 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004237 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004239 if (c == NUL
4240#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4241 || did_line_attr == 1
4242#endif
4243 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004245#ifdef FEAT_SEARCH_EXTRA
4246 long prevcol = (long)(ptr - line) - (c == NUL);
4247#endif
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 /* invert at least one char, used for Visual and empty line or
4250 * highlight match at end of line. If it's beyond the last
4251 * char on the screen, just overwrite that one (tricky!) Not
4252 * needed when a '$' was displayed for 'list'. */
4253 if (lcs_eol == lcs_eol_one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004254 && ((area_attr != 0 && vcol == fromcol && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255#ifdef FEAT_SEARCH_EXTRA
4256 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004257 || ((prevcol == (long)search_hl.startcol
4258 || prevcol == (long)match_hl[0].startcol
4259 || prevcol == (long)match_hl[1].startcol
4260 || prevcol == (long)match_hl[2].startcol)
4261# if defined(FEAT_DIFF) || defined(LINE_ATTR)
4262 && did_line_attr <= 1
4263# endif
4264 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265#endif
4266 ))
4267 {
4268 int n = 0;
4269
4270#ifdef FEAT_RIGHTLEFT
4271 if (wp->w_p_rl)
4272 {
4273 if (col < 0)
4274 n = 1;
4275 }
4276 else
4277#endif
4278 {
4279 if (col >= W_WIDTH(wp))
4280 n = -1;
4281 }
4282 if (n != 0)
4283 {
4284 /* At the window boundary, highlight the last character
4285 * instead (better than nothing). */
4286 off += n;
4287 col += n;
4288 }
4289 else
4290 {
4291 /* Add a blank character to highlight. */
4292 ScreenLines[off] = ' ';
4293#ifdef FEAT_MBYTE
4294 if (enc_utf8)
4295 ScreenLinesUC[off] = 0;
4296#endif
4297 }
4298#ifdef FEAT_SEARCH_EXTRA
4299 if (area_attr == 0)
4300 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004301 for (i = 0; i <= 3; ++i)
4302 {
4303 if (i == 3)
4304 char_attr = search_hl.attr;
4305 else if ((ptr - line) - 1 == (long)match_hl[i].startcol)
4306 {
4307 char_attr = match_hl[i].attr;
4308 break;
4309 }
4310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312#endif
4313 ScreenAttrs[off] = char_attr;
4314#ifdef FEAT_RIGHTLEFT
4315 if (wp->w_p_rl)
4316 --col;
4317 else
4318#endif
4319 ++col;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004320 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
Bram Moolenaar91170f82006-05-05 21:15:17 +00004324 /*
4325 * At end of the text line.
4326 */
4327 if (c == NUL)
4328 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004329#ifdef FEAT_SYN_HL
4330 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004331 if (wp->w_p_wrap)
4332 v = wp->w_skipcol;
4333 else
4334 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004335 /* check if line ends before left margin */
4336 if (vcol < v + col - win_col_off(wp))
4337
4338 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004339 if (wp->w_p_cuc
4340 && (int)wp->w_virtcol >= vcol
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004341 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4342 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004343 && lnum != wp->w_cursor.lnum
4344# ifdef FEAT_RIGHTLEFT
4345 && !wp->w_p_rl
4346# endif
4347 )
4348 {
4349 while (col < W_WIDTH(wp))
4350 {
4351 ScreenLines[off] = ' ';
4352#ifdef FEAT_MBYTE
4353 if (enc_utf8)
4354 ScreenLinesUC[off] = 0;
4355#endif
4356 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004357 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004358 {
4359 ScreenAttrs[off] = hl_attr(HLF_CUC);
4360 break;
4361 }
4362 ScreenAttrs[off++] = 0;
4363 ++vcol;
4364 }
4365 }
4366#endif
4367
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4369 wp->w_p_rl);
4370 row++;
4371
4372 /*
4373 * Update w_cline_height and w_cline_folded if the cursor line was
4374 * updated (saves a call to plines() later).
4375 */
4376 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4377 {
4378 curwin->w_cline_row = startrow;
4379 curwin->w_cline_height = row - startrow;
4380#ifdef FEAT_FOLDING
4381 curwin->w_cline_folded = FALSE;
4382#endif
4383 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4384 }
4385
4386 break;
4387 }
4388
4389 /* line continues beyond line end */
4390 if (lcs_ext
4391 && !wp->w_p_wrap
4392#ifdef FEAT_DIFF
4393 && filler_todo <= 0
4394#endif
4395 && (
4396#ifdef FEAT_RIGHTLEFT
4397 wp->w_p_rl ? col == 0 :
4398#endif
4399 col == W_WIDTH(wp) - 1)
4400 && (*ptr != NUL
4401 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4402 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4403 {
4404 c = lcs_ext;
4405 char_attr = hl_attr(HLF_AT);
4406#ifdef FEAT_MBYTE
4407 mb_c = c;
4408 if (enc_utf8 && (*mb_char2len)(c) > 1)
4409 {
4410 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004411 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004412 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 else
4415 mb_utf8 = FALSE;
4416#endif
4417 }
4418
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004419#ifdef FEAT_SYN_HL
4420 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4421 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004422 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004423 && lnum != wp->w_cursor.lnum
4424 && draw_state == WL_LINE)
4425 {
4426 vcol_save_attr = char_attr;
4427 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4428 }
4429 else
4430 vcol_save_attr = -1;
4431#endif
4432
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 /*
4434 * Store character to be displayed.
4435 * Skip characters that are left of the screen for 'nowrap'.
4436 */
4437 vcol_prev = vcol;
4438 if (draw_state < WL_LINE || n_skip <= 0)
4439 {
4440 /*
4441 * Store the character.
4442 */
4443#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4444 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4445 {
4446 /* A double-wide character is: put first halve in left cell. */
4447 --off;
4448 --col;
4449 }
4450#endif
4451 ScreenLines[off] = c;
4452#ifdef FEAT_MBYTE
4453 if (enc_dbcs == DBCS_JPNU)
4454 ScreenLines2[off] = mb_c & 0xff;
4455 else if (enc_utf8)
4456 {
4457 if (mb_utf8)
4458 {
4459 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004460 if ((c & 0xff) == 0)
4461 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004462 for (i = 0; i < Screen_mco; ++i)
4463 {
4464 ScreenLinesC[i][off] = u8cc[i];
4465 if (u8cc[i] == 0)
4466 break;
4467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 else
4470 ScreenLinesUC[off] = 0;
4471 }
4472 if (multi_attr)
4473 {
4474 ScreenAttrs[off] = multi_attr;
4475 multi_attr = 0;
4476 }
4477 else
4478#endif
4479 ScreenAttrs[off] = char_attr;
4480
4481#ifdef FEAT_MBYTE
4482 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4483 {
4484 /* Need to fill two screen columns. */
4485 ++off;
4486 ++col;
4487 if (enc_utf8)
4488 /* UTF-8: Put a 0 in the second screen char. */
4489 ScreenLines[off] = 0;
4490 else
4491 /* DBCS: Put second byte in the second screen char. */
4492 ScreenLines[off] = mb_c & 0xff;
4493 ++vcol;
4494 /* When "tocol" is halfway a character, set it to the end of
4495 * the character, otherwise highlighting won't stop. */
4496 if (tocol == vcol)
4497 ++tocol;
4498#ifdef FEAT_RIGHTLEFT
4499 if (wp->w_p_rl)
4500 {
4501 /* now it's time to backup one cell */
4502 --off;
4503 --col;
4504 }
4505#endif
4506 }
4507#endif
4508#ifdef FEAT_RIGHTLEFT
4509 if (wp->w_p_rl)
4510 {
4511 --off;
4512 --col;
4513 }
4514 else
4515#endif
4516 {
4517 ++off;
4518 ++col;
4519 }
4520 }
4521 else
4522 --n_skip;
4523
4524 /* Only advance the "vcol" when after the 'number' column. */
4525 if (draw_state >= WL_SBR
4526#ifdef FEAT_DIFF
4527 && filler_todo <= 0
4528#endif
4529 )
4530 ++vcol;
4531
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004532#ifdef FEAT_SYN_HL
4533 if (vcol_save_attr >= 0)
4534 char_attr = vcol_save_attr;
4535#endif
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 /* restore attributes after "predeces" in 'listchars' */
4538 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4539 char_attr = saved_attr3;
4540
4541 /* restore attributes after last 'listchars' or 'number' char */
4542 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4543 char_attr = saved_attr2;
4544
4545 /*
4546 * At end of screen line and there is more to come: Display the line
4547 * so far. If there is no more to display it is catched above.
4548 */
4549 if ((
4550#ifdef FEAT_RIGHTLEFT
4551 wp->w_p_rl ? (col < 0) :
4552#endif
4553 (col >= W_WIDTH(wp)))
4554 && (*ptr != NUL
4555#ifdef FEAT_DIFF
4556 || filler_todo > 0
4557#endif
4558 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4559 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4560 )
4561 {
4562 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4563 wp->w_p_rl);
4564 ++row;
4565 ++screen_row;
4566
4567 /* When not wrapping and finished diff lines, or when displayed
4568 * '$' and highlighting until last column, break here. */
4569 if ((!wp->w_p_wrap
4570#ifdef FEAT_DIFF
4571 && filler_todo <= 0
4572#endif
4573 ) || lcs_eol_one == -1)
4574 break;
4575
4576 /* When the window is too narrow draw all "@" lines. */
4577 if (draw_state != WL_LINE
4578#ifdef FEAT_DIFF
4579 && filler_todo <= 0
4580#endif
4581 )
4582 {
4583 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4584#ifdef FEAT_VERTSPLIT
4585 draw_vsep_win(wp, row);
4586#endif
4587 row = endrow;
4588 }
4589
4590 /* When line got too long for screen break here. */
4591 if (row == endrow)
4592 {
4593 ++row;
4594 break;
4595 }
4596
4597 if (screen_cur_row == screen_row - 1
4598#ifdef FEAT_DIFF
4599 && filler_todo <= 0
4600#endif
4601 && W_WIDTH(wp) == Columns)
4602 {
4603 /* Remember that the line wraps, used for modeless copy. */
4604 LineWraps[screen_row - 1] = TRUE;
4605
4606 /*
4607 * Special trick to make copy/paste of wrapped lines work with
4608 * xterm/screen: write an extra character beyond the end of
4609 * the line. This will work with all terminal types
4610 * (regardless of the xn,am settings).
4611 * Only do this on a fast tty.
4612 * Only do this if the cursor is on the current line
4613 * (something has been written in it).
4614 * Don't do this for the GUI.
4615 * Don't do this for double-width characters.
4616 * Don't do this for a window not at the right screen border.
4617 */
4618 if (p_tf
4619#ifdef FEAT_GUI
4620 && !gui.in_use
4621#endif
4622#ifdef FEAT_MBYTE
4623 && !(has_mbyte
4624 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4625 || (*mb_off2cells)(LineOffset[screen_row - 1]
4626 + (int)Columns - 2) == 2))
4627#endif
4628 )
4629 {
4630 /* First make sure we are at the end of the screen line,
4631 * then output the same character again to let the
4632 * terminal know about the wrap. If the terminal doesn't
4633 * auto-wrap, we overwrite the character. */
4634 if (screen_cur_col != W_WIDTH(wp))
4635 screen_char(LineOffset[screen_row - 1]
4636 + (unsigned)Columns - 1,
4637 screen_row - 1, (int)(Columns - 1));
4638
4639#ifdef FEAT_MBYTE
4640 /* When there is a multi-byte character, just output a
4641 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004642 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4643 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 out_char(' ');
4645 else
4646#endif
4647 out_char(ScreenLines[LineOffset[screen_row - 1]
4648 + (Columns - 1)]);
4649 /* force a redraw of the first char on the next line */
4650 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4651 screen_start(); /* don't know where cursor is now */
4652 }
4653 }
4654
4655 col = 0;
4656 off = (unsigned)(current_ScreenLine - ScreenLines);
4657#ifdef FEAT_RIGHTLEFT
4658 if (wp->w_p_rl)
4659 {
4660 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4661 off += col;
4662 }
4663#endif
4664
4665 /* reset the drawing state for the start of a wrapped line */
4666 draw_state = WL_START;
4667 saved_n_extra = n_extra;
4668 saved_p_extra = p_extra;
4669 saved_c_extra = c_extra;
4670 saved_char_attr = char_attr;
4671 n_extra = 0;
4672 lcs_prec_todo = lcs_prec;
4673#ifdef FEAT_LINEBREAK
4674# ifdef FEAT_DIFF
4675 if (filler_todo <= 0)
4676# endif
4677 need_showbreak = TRUE;
4678#endif
4679#ifdef FEAT_DIFF
4680 --filler_todo;
4681 /* When the filler lines are actually below the last line of the
4682 * file, don't draw the line itself, break here. */
4683 if (filler_todo == 0 && wp->w_botfill)
4684 break;
4685#endif
4686 }
4687
4688 } /* for every character in the line */
4689
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004690#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004691 /* After an empty line check first word for capital. */
4692 if (*skipwhite(line) == NUL)
4693 {
4694 capcol_lnum = lnum + 1;
4695 cap_col = 0;
4696 }
4697#endif
4698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 return row;
4700}
4701
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004702#ifdef FEAT_MBYTE
4703static int comp_char_differs __ARGS((int, int));
4704
4705/*
4706 * Return if the composing characters at "off_from" and "off_to" differ.
4707 */
4708 static int
4709comp_char_differs(off_from, off_to)
4710 int off_from;
4711 int off_to;
4712{
4713 int i;
4714
4715 for (i = 0; i < Screen_mco; ++i)
4716 {
4717 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4718 return TRUE;
4719 if (ScreenLinesC[i][off_from] == 0)
4720 break;
4721 }
4722 return FALSE;
4723}
4724#endif
4725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726/*
4727 * Check whether the given character needs redrawing:
4728 * - the (first byte of the) character is different
4729 * - the attributes are different
4730 * - the character is multi-byte and the next byte is different
4731 */
4732 static int
4733char_needs_redraw(off_from, off_to, cols)
4734 int off_from;
4735 int off_to;
4736 int cols;
4737{
4738 if (cols > 0
4739 && ((ScreenLines[off_from] != ScreenLines[off_to]
4740 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4741
4742#ifdef FEAT_MBYTE
4743 || (enc_dbcs != 0
4744 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4745 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4746 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4747 : (cols > 1 && ScreenLines[off_from + 1]
4748 != ScreenLines[off_to + 1])))
4749 || (enc_utf8
4750 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4751 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004752 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753#endif
4754 ))
4755 return TRUE;
4756 return FALSE;
4757}
4758
4759/*
4760 * Move one "cooked" screen line to the screen, but only the characters that
4761 * have actually changed. Handle insert/delete character.
4762 * "coloff" gives the first column on the screen for this line.
4763 * "endcol" gives the columns where valid characters are.
4764 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4765 * needs to be cleared, negative otherwise.
4766 * "rlflag" is TRUE in a rightleft window:
4767 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4768 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4769 */
4770 static void
4771screen_line(row, coloff, endcol, clear_width
4772#ifdef FEAT_RIGHTLEFT
4773 , rlflag
4774#endif
4775 )
4776 int row;
4777 int coloff;
4778 int endcol;
4779 int clear_width;
4780#ifdef FEAT_RIGHTLEFT
4781 int rlflag;
4782#endif
4783{
4784 unsigned off_from;
4785 unsigned off_to;
4786 int col = 0;
4787#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4788 int hl;
4789#endif
4790 int force = FALSE; /* force update rest of the line */
4791 int redraw_this /* bool: does character need redraw? */
4792#ifdef FEAT_GUI
4793 = TRUE /* For GUI when while-loop empty */
4794#endif
4795 ;
4796 int redraw_next; /* redraw_this for next character */
4797#ifdef FEAT_MBYTE
4798 int clear_next = FALSE;
4799 int char_cells; /* 1: normal char */
4800 /* 2: occupies two display cells */
4801# define CHAR_CELLS char_cells
4802#else
4803# define CHAR_CELLS 1
4804#endif
4805
4806# ifdef FEAT_CLIPBOARD
4807 clip_may_clear_selection(row, row);
4808# endif
4809
4810 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4811 off_to = LineOffset[row] + coloff;
4812
4813#ifdef FEAT_RIGHTLEFT
4814 if (rlflag)
4815 {
4816 /* Clear rest first, because it's left of the text. */
4817 if (clear_width > 0)
4818 {
4819 while (col <= endcol && ScreenLines[off_to] == ' '
4820 && ScreenAttrs[off_to] == 0
4821# ifdef FEAT_MBYTE
4822 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4823# endif
4824 )
4825 {
4826 ++off_to;
4827 ++col;
4828 }
4829 if (col <= endcol)
4830 screen_fill(row, row + 1, col + coloff,
4831 endcol + coloff + 1, ' ', ' ', 0);
4832 }
4833 col = endcol + 1;
4834 off_to = LineOffset[row] + col + coloff;
4835 off_from += col;
4836 endcol = (clear_width > 0 ? clear_width : -clear_width);
4837 }
4838#endif /* FEAT_RIGHTLEFT */
4839
4840 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4841
4842 while (col < endcol)
4843 {
4844#ifdef FEAT_MBYTE
4845 if (has_mbyte && (col + 1 < endcol))
4846 char_cells = (*mb_off2cells)(off_from);
4847 else
4848 char_cells = 1;
4849#endif
4850
4851 redraw_this = redraw_next;
4852 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4853 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4854
4855#ifdef FEAT_GUI
4856 /* If the next character was bold, then redraw the current character to
4857 * remove any pixels that might have spilt over into us. This only
4858 * happens in the GUI.
4859 */
4860 if (redraw_next && gui.in_use)
4861 {
4862 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004863 if (hl > HL_ALL)
4864 hl = syn_attr2attr(hl);
4865 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 redraw_this = TRUE;
4867 }
4868#endif
4869
4870 if (redraw_this)
4871 {
4872 /*
4873 * Special handling when 'xs' termcap flag set (hpterm):
4874 * Attributes for characters are stored at the position where the
4875 * cursor is when writing the highlighting code. The
4876 * start-highlighting code must be written with the cursor on the
4877 * first highlighted character. The stop-highlighting code must
4878 * be written with the cursor just after the last highlighted
4879 * character.
4880 * Overwriting a character doesn't remove it's highlighting. Need
4881 * to clear the rest of the line, and force redrawing it
4882 * completely.
4883 */
4884 if ( p_wiv
4885 && !force
4886#ifdef FEAT_GUI
4887 && !gui.in_use
4888#endif
4889 && ScreenAttrs[off_to] != 0
4890 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4891 {
4892 /*
4893 * Need to remove highlighting attributes here.
4894 */
4895 windgoto(row, col + coloff);
4896 out_str(T_CE); /* clear rest of this screen line */
4897 screen_start(); /* don't know where cursor is now */
4898 force = TRUE; /* force redraw of rest of the line */
4899 redraw_next = TRUE; /* or else next char would miss out */
4900
4901 /*
4902 * If the previous character was highlighted, need to stop
4903 * highlighting at this character.
4904 */
4905 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4906 {
4907 screen_attr = ScreenAttrs[off_to - 1];
4908 term_windgoto(row, col + coloff);
4909 screen_stop_highlight();
4910 }
4911 else
4912 screen_attr = 0; /* highlighting has stopped */
4913 }
4914#ifdef FEAT_MBYTE
4915 if (enc_dbcs != 0)
4916 {
4917 /* Check if overwriting a double-byte with a single-byte or
4918 * the other way around requires another character to be
4919 * redrawn. For UTF-8 this isn't needed, because comparing
4920 * ScreenLinesUC[] is sufficient. */
4921 if (char_cells == 1
4922 && col + 1 < endcol
4923 && (*mb_off2cells)(off_to) > 1)
4924 {
4925 /* Writing a single-cell character over a double-cell
4926 * character: need to redraw the next cell. */
4927 ScreenLines[off_to + 1] = 0;
4928 redraw_next = TRUE;
4929 }
4930 else if (char_cells == 2
4931 && col + 2 < endcol
4932 && (*mb_off2cells)(off_to) == 1
4933 && (*mb_off2cells)(off_to + 1) > 1)
4934 {
4935 /* Writing the second half of a double-cell character over
4936 * a double-cell character: need to redraw the second
4937 * cell. */
4938 ScreenLines[off_to + 2] = 0;
4939 redraw_next = TRUE;
4940 }
4941
4942 if (enc_dbcs == DBCS_JPNU)
4943 ScreenLines2[off_to] = ScreenLines2[off_from];
4944 }
4945 /* When writing a single-width character over a double-width
4946 * character and at the end of the redrawn text, need to clear out
4947 * the right halve of the old character.
4948 * Also required when writing the right halve of a double-width
4949 * char over the left halve of an existing one. */
4950 if (has_mbyte && col + char_cells == endcol
4951 && ((char_cells == 1
4952 && (*mb_off2cells)(off_to) > 1)
4953 || (char_cells == 2
4954 && (*mb_off2cells)(off_to) == 1
4955 && (*mb_off2cells)(off_to + 1) > 1)))
4956 clear_next = TRUE;
4957#endif
4958
4959 ScreenLines[off_to] = ScreenLines[off_from];
4960#ifdef FEAT_MBYTE
4961 if (enc_utf8)
4962 {
4963 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4964 if (ScreenLinesUC[off_from] != 0)
4965 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004966 int i;
4967
4968 for (i = 0; i < Screen_mco; ++i)
4969 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
4971 }
4972 if (char_cells == 2)
4973 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4974#endif
4975
4976#if defined(FEAT_GUI) || defined(UNIX)
4977 /* The bold trick makes a single row of pixels appear in the next
4978 * character. When a bold character is removed, the next
4979 * character should be redrawn too. This happens for our own GUI
4980 * and for some xterms. */
4981 if (
4982# ifdef FEAT_GUI
4983 gui.in_use
4984# endif
4985# if defined(FEAT_GUI) && defined(UNIX)
4986 ||
4987# endif
4988# ifdef UNIX
4989 term_is_xterm
4990# endif
4991 )
4992 {
4993 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004994 if (hl > HL_ALL)
4995 hl = syn_attr2attr(hl);
4996 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 redraw_next = TRUE;
4998 }
4999#endif
5000 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5001#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005002 /* For simplicity set the attributes of second half of a
5003 * double-wide character equal to the first half. */
5004 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005006
5007 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 else
5010#endif
5011 screen_char(off_to, row, col + coloff);
5012 }
5013 else if ( p_wiv
5014#ifdef FEAT_GUI
5015 && !gui.in_use
5016#endif
5017 && col + coloff > 0)
5018 {
5019 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5020 {
5021 /*
5022 * Don't output stop-highlight when moving the cursor, it will
5023 * stop the highlighting when it should continue.
5024 */
5025 screen_attr = 0;
5026 }
5027 else if (screen_attr != 0)
5028 screen_stop_highlight();
5029 }
5030
5031 off_to += CHAR_CELLS;
5032 off_from += CHAR_CELLS;
5033 col += CHAR_CELLS;
5034 }
5035
5036#ifdef FEAT_MBYTE
5037 if (clear_next)
5038 {
5039 /* Clear the second half of a double-wide character of which the left
5040 * half was overwritten with a single-wide character. */
5041 ScreenLines[off_to] = ' ';
5042 if (enc_utf8)
5043 ScreenLinesUC[off_to] = 0;
5044 screen_char(off_to, row, col + coloff);
5045 }
5046#endif
5047
5048 if (clear_width > 0
5049#ifdef FEAT_RIGHTLEFT
5050 && !rlflag
5051#endif
5052 )
5053 {
5054#ifdef FEAT_GUI
5055 int startCol = col;
5056#endif
5057
5058 /* blank out the rest of the line */
5059 while (col < clear_width && ScreenLines[off_to] == ' '
5060 && ScreenAttrs[off_to] == 0
5061#ifdef FEAT_MBYTE
5062 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5063#endif
5064 )
5065 {
5066 ++off_to;
5067 ++col;
5068 }
5069 if (col < clear_width)
5070 {
5071#ifdef FEAT_GUI
5072 /*
5073 * In the GUI, clearing the rest of the line may leave pixels
5074 * behind if the first character cleared was bold. Some bold
5075 * fonts spill over the left. In this case we redraw the previous
5076 * character too. If we didn't skip any blanks above, then we
5077 * only redraw if the character wasn't already redrawn anyway.
5078 */
5079 if (gui.in_use && (col > startCol || !redraw_this)
5080# ifdef FEAT_MBYTE
5081 && enc_dbcs == 0
5082# endif
5083 )
5084 {
5085 hl = ScreenAttrs[off_to];
5086 if (hl > HL_ALL || (hl & HL_BOLD))
5087 screen_char(off_to - 1, row, col + coloff - 1);
5088 }
5089#endif
5090 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5091 ' ', ' ', 0);
5092#ifdef FEAT_VERTSPLIT
5093 off_to += clear_width - col;
5094 col = clear_width;
5095#endif
5096 }
5097 }
5098
5099 if (clear_width > 0)
5100 {
5101#ifdef FEAT_VERTSPLIT
5102 /* For a window that's left of another, draw the separator char. */
5103 if (col + coloff < Columns)
5104 {
5105 int c;
5106
5107 c = fillchar_vsep(&hl);
5108 if (ScreenLines[off_to] != c
5109# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005110 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5111 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112# endif
5113 || ScreenAttrs[off_to] != hl)
5114 {
5115 ScreenLines[off_to] = c;
5116 ScreenAttrs[off_to] = hl;
5117# ifdef FEAT_MBYTE
5118 if (enc_utf8)
5119 {
5120 if (c >= 0x80)
5121 {
5122 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005123 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 else
5126 ScreenLinesUC[off_to] = 0;
5127 }
5128# endif
5129 screen_char(off_to, row, col + coloff);
5130 }
5131 }
5132 else
5133#endif
5134 LineWraps[row] = FALSE;
5135 }
5136}
5137
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005138#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005140 * Mirror text "str" for right-left displaying.
5141 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005143 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144rl_mirror(str)
5145 char_u *str;
5146{
5147 char_u *p1, *p2;
5148 int t;
5149
5150 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5151 {
5152 t = *p1;
5153 *p1 = *p2;
5154 *p2 = t;
5155 }
5156}
5157#endif
5158
5159#if defined(FEAT_WINDOWS) || defined(PROTO)
5160/*
5161 * mark all status lines for redraw; used after first :cd
5162 */
5163 void
5164status_redraw_all()
5165{
5166 win_T *wp;
5167
5168 for (wp = firstwin; wp; wp = wp->w_next)
5169 if (wp->w_status_height)
5170 {
5171 wp->w_redr_status = TRUE;
5172 redraw_later(VALID);
5173 }
5174}
5175
5176/*
5177 * mark all status lines of the current buffer for redraw
5178 */
5179 void
5180status_redraw_curbuf()
5181{
5182 win_T *wp;
5183
5184 for (wp = firstwin; wp; wp = wp->w_next)
5185 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5186 {
5187 wp->w_redr_status = TRUE;
5188 redraw_later(VALID);
5189 }
5190}
5191
5192/*
5193 * Redraw all status lines that need to be redrawn.
5194 */
5195 void
5196redraw_statuslines()
5197{
5198 win_T *wp;
5199
5200 for (wp = firstwin; wp; wp = wp->w_next)
5201 if (wp->w_redr_status)
5202 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005203 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005204 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205}
5206#endif
5207
5208#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5209/*
5210 * Redraw all status lines at the bottom of frame "frp".
5211 */
5212 void
5213win_redraw_last_status(frp)
5214 frame_T *frp;
5215{
5216 if (frp->fr_layout == FR_LEAF)
5217 frp->fr_win->w_redr_status = TRUE;
5218 else if (frp->fr_layout == FR_ROW)
5219 {
5220 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5221 win_redraw_last_status(frp);
5222 }
5223 else /* frp->fr_layout == FR_COL */
5224 {
5225 frp = frp->fr_child;
5226 while (frp->fr_next != NULL)
5227 frp = frp->fr_next;
5228 win_redraw_last_status(frp);
5229 }
5230}
5231#endif
5232
5233#ifdef FEAT_VERTSPLIT
5234/*
5235 * Draw the verticap separator right of window "wp" starting with line "row".
5236 */
5237 static void
5238draw_vsep_win(wp, row)
5239 win_T *wp;
5240 int row;
5241{
5242 int hl;
5243 int c;
5244
5245 if (wp->w_vsep_width)
5246 {
5247 /* draw the vertical separator right of this window */
5248 c = fillchar_vsep(&hl);
5249 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5250 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5251 c, ' ', hl);
5252 }
5253}
5254#endif
5255
5256#ifdef FEAT_WILDMENU
5257static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005258static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
5260/*
5261 * Get the lenght of an item as it will be shown in the status line.
5262 */
5263 static int
5264status_match_len(xp, s)
5265 expand_T *xp;
5266 char_u *s;
5267{
5268 int len = 0;
5269
5270#ifdef FEAT_MENU
5271 int emenu = (xp->xp_context == EXPAND_MENUS
5272 || xp->xp_context == EXPAND_MENUNAMES);
5273
5274 /* Check for menu separators - replace with '|'. */
5275 if (emenu && menu_is_separator(s))
5276 return 1;
5277#endif
5278
5279 while (*s != NUL)
5280 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005281 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005283 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005284 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286
5287 return len;
5288}
5289
5290/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005291 * Return TRUE for characters that are not displayed in a status match.
5292 * These are backslashes used for escaping. Do show backslashes in help tags.
5293 */
5294 static int
5295skip_status_match_char(xp, s)
5296 expand_T *xp;
5297 char_u *s;
5298{
5299 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5300#ifdef FEAT_MENU
5301 || ((xp->xp_context == EXPAND_MENUS
5302 || xp->xp_context == EXPAND_MENUNAMES)
5303 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5304#endif
5305 );
5306}
5307
5308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 * Show wildchar matches in the status line.
5310 * Show at least the "match" item.
5311 * We start at item 'first_match' in the list and show all matches that fit.
5312 *
5313 * If inversion is possible we use it. Else '=' characters are used.
5314 */
5315 void
5316win_redr_status_matches(xp, num_matches, matches, match, showtail)
5317 expand_T *xp;
5318 int num_matches;
5319 char_u **matches; /* list of matches */
5320 int match;
5321 int showtail;
5322{
5323#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5324 int row;
5325 char_u *buf;
5326 int len;
5327 int clen; /* lenght in screen cells */
5328 int fillchar;
5329 int attr;
5330 int i;
5331 int highlight = TRUE;
5332 char_u *selstart = NULL;
5333 int selstart_col = 0;
5334 char_u *selend = NULL;
5335 static int first_match = 0;
5336 int add_left = FALSE;
5337 char_u *s;
5338#ifdef FEAT_MENU
5339 int emenu;
5340#endif
5341#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5342 int l;
5343#endif
5344
5345 if (matches == NULL) /* interrupted completion? */
5346 return;
5347
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005348#ifdef FEAT_MBYTE
5349 if (has_mbyte)
5350 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5351 else
5352#endif
5353 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (buf == NULL)
5355 return;
5356
5357 if (match == -1) /* don't show match but original text */
5358 {
5359 match = 0;
5360 highlight = FALSE;
5361 }
5362 /* count 1 for the ending ">" */
5363 clen = status_match_len(xp, L_MATCH(match)) + 3;
5364 if (match == 0)
5365 first_match = 0;
5366 else if (match < first_match)
5367 {
5368 /* jumping left, as far as we can go */
5369 first_match = match;
5370 add_left = TRUE;
5371 }
5372 else
5373 {
5374 /* check if match fits on the screen */
5375 for (i = first_match; i < match; ++i)
5376 clen += status_match_len(xp, L_MATCH(i)) + 2;
5377 if (first_match > 0)
5378 clen += 2;
5379 /* jumping right, put match at the left */
5380 if ((long)clen > Columns)
5381 {
5382 first_match = match;
5383 /* if showing the last match, we can add some on the left */
5384 clen = 2;
5385 for (i = match; i < num_matches; ++i)
5386 {
5387 clen += status_match_len(xp, L_MATCH(i)) + 2;
5388 if ((long)clen >= Columns)
5389 break;
5390 }
5391 if (i == num_matches)
5392 add_left = TRUE;
5393 }
5394 }
5395 if (add_left)
5396 while (first_match > 0)
5397 {
5398 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5399 if ((long)clen >= Columns)
5400 break;
5401 --first_match;
5402 }
5403
5404 fillchar = fillchar_status(&attr, TRUE);
5405
5406 if (first_match == 0)
5407 {
5408 *buf = NUL;
5409 len = 0;
5410 }
5411 else
5412 {
5413 STRCPY(buf, "< ");
5414 len = 2;
5415 }
5416 clen = len;
5417
5418 i = first_match;
5419 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5420 {
5421 if (i == match)
5422 {
5423 selstart = buf + len;
5424 selstart_col = clen;
5425 }
5426
5427 s = L_MATCH(i);
5428 /* Check for menu separators - replace with '|' */
5429#ifdef FEAT_MENU
5430 emenu = (xp->xp_context == EXPAND_MENUS
5431 || xp->xp_context == EXPAND_MENUNAMES);
5432 if (emenu && menu_is_separator(s))
5433 {
5434 STRCPY(buf + len, transchar('|'));
5435 l = (int)STRLEN(buf + len);
5436 len += l;
5437 clen += l;
5438 }
5439 else
5440#endif
5441 for ( ; *s != NUL; ++s)
5442 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005443 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 ++s;
5445 clen += ptr2cells(s);
5446#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005447 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
5449 STRNCPY(buf + len, s, l);
5450 s += l - 1;
5451 len += l;
5452 }
5453 else
5454#endif
5455 {
5456 STRCPY(buf + len, transchar_byte(*s));
5457 len += (int)STRLEN(buf + len);
5458 }
5459 }
5460 if (i == match)
5461 selend = buf + len;
5462
5463 *(buf + len++) = ' ';
5464 *(buf + len++) = ' ';
5465 clen += 2;
5466 if (++i == num_matches)
5467 break;
5468 }
5469
5470 if (i != num_matches)
5471 {
5472 *(buf + len++) = '>';
5473 ++clen;
5474 }
5475
5476 buf[len] = NUL;
5477
5478 row = cmdline_row - 1;
5479 if (row >= 0)
5480 {
5481 if (wild_menu_showing == 0)
5482 {
5483 if (msg_scrolled > 0)
5484 {
5485 /* Put the wildmenu just above the command line. If there is
5486 * no room, scroll the screen one line up. */
5487 if (cmdline_row == Rows - 1)
5488 {
5489 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5490 ++msg_scrolled;
5491 }
5492 else
5493 {
5494 ++cmdline_row;
5495 ++row;
5496 }
5497 wild_menu_showing = WM_SCROLLED;
5498 }
5499 else
5500 {
5501 /* Create status line if needed by setting 'laststatus' to 2.
5502 * Set 'winminheight' to zero to avoid that the window is
5503 * resized. */
5504 if (lastwin->w_status_height == 0)
5505 {
5506 save_p_ls = p_ls;
5507 save_p_wmh = p_wmh;
5508 p_ls = 2;
5509 p_wmh = 0;
5510 last_status(FALSE);
5511 }
5512 wild_menu_showing = WM_SHOWN;
5513 }
5514 }
5515
5516 screen_puts(buf, row, 0, attr);
5517 if (selstart != NULL && highlight)
5518 {
5519 *selend = NUL;
5520 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5521 }
5522
5523 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5524 }
5525
5526#ifdef FEAT_VERTSPLIT
5527 win_redraw_last_status(topframe);
5528#else
5529 lastwin->w_redr_status = TRUE;
5530#endif
5531 vim_free(buf);
5532}
5533#endif
5534
5535#if defined(FEAT_WINDOWS) || defined(PROTO)
5536/*
5537 * Redraw the status line of window wp.
5538 *
5539 * If inversion is possible we use it. Else '=' characters are used.
5540 */
5541 void
5542win_redr_status(wp)
5543 win_T *wp;
5544{
5545 int row;
5546 char_u *p;
5547 int len;
5548 int fillchar;
5549 int attr;
5550 int this_ru_col;
5551
5552 wp->w_redr_status = FALSE;
5553 if (wp->w_status_height == 0)
5554 {
5555 /* no status line, can only be last window */
5556 redraw_cmdline = TRUE;
5557 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005558 else if (!redrawing()
5559#ifdef FEAT_INS_EXPAND
5560 /* don't update status line when popup menu is visible and may be
5561 * drawn over it */
5562 || pum_visible()
5563#endif
5564 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
5566 /* Don't redraw right now, do it later. */
5567 wp->w_redr_status = TRUE;
5568 }
5569#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005570 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
5572 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005573 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575#endif
5576 else
5577 {
5578 fillchar = fillchar_status(&attr, wp == curwin);
5579
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005580 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 p = NameBuff;
5582 len = (int)STRLEN(p);
5583
5584 if (wp->w_buffer->b_help
5585#ifdef FEAT_QUICKFIX
5586 || wp->w_p_pvw
5587#endif
5588 || bufIsChanged(wp->w_buffer)
5589 || wp->w_buffer->b_p_ro)
5590 *(p + len++) = ' ';
5591 if (wp->w_buffer->b_help)
5592 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005593 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 len += (int)STRLEN(p + len);
5595 }
5596#ifdef FEAT_QUICKFIX
5597 if (wp->w_p_pvw)
5598 {
5599 STRCPY(p + len, _("[Preview]"));
5600 len += (int)STRLEN(p + len);
5601 }
5602#endif
5603 if (bufIsChanged(wp->w_buffer))
5604 {
5605 STRCPY(p + len, "[+]");
5606 len += 3;
5607 }
5608 if (wp->w_buffer->b_p_ro)
5609 {
5610 STRCPY(p + len, "[RO]");
5611 len += 4;
5612 }
5613
5614#ifndef FEAT_VERTSPLIT
5615 this_ru_col = ru_col;
5616 if (this_ru_col < (Columns + 1) / 2)
5617 this_ru_col = (Columns + 1) / 2;
5618#else
5619 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5620 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5621 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5622 if (this_ru_col <= 1)
5623 {
5624 p = (char_u *)"<"; /* No room for file name! */
5625 len = 1;
5626 }
5627 else
5628#endif
5629#ifdef FEAT_MBYTE
5630 if (has_mbyte)
5631 {
5632 int clen = 0, i;
5633
5634 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005635 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 clen += (*mb_ptr2cells)(p + i);
5637 /* Find first character that will fit.
5638 * Going from start to end is much faster for DBCS. */
5639 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005640 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 clen -= (*mb_ptr2cells)(p + i);
5642 len = clen;
5643 if (i > 0)
5644 {
5645 p = p + i - 1;
5646 *p = '<';
5647 ++len;
5648 }
5649
5650 }
5651 else
5652#endif
5653 if (len > this_ru_col - 1)
5654 {
5655 p += len - (this_ru_col - 1);
5656 *p = '<';
5657 len = this_ru_col - 1;
5658 }
5659
5660 row = W_WINROW(wp) + wp->w_height;
5661 screen_puts(p, row, W_WINCOL(wp), attr);
5662 screen_fill(row, row + 1, len + W_WINCOL(wp),
5663 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5664
5665 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5666 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5667 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5668 - 1 + W_WINCOL(wp)), attr);
5669
5670#ifdef FEAT_CMDL_INFO
5671 win_redr_ruler(wp, TRUE);
5672#endif
5673 }
5674
5675#ifdef FEAT_VERTSPLIT
5676 /*
5677 * May need to draw the character below the vertical separator.
5678 */
5679 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5680 {
5681 if (stl_connected(wp))
5682 fillchar = fillchar_status(&attr, wp == curwin);
5683 else
5684 fillchar = fillchar_vsep(&attr);
5685 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5686 attr);
5687 }
5688#endif
5689}
5690
Bram Moolenaar238a5642006-02-21 22:12:05 +00005691#ifdef FEAT_STL_OPT
5692/*
5693 * Redraw the status line according to 'statusline' and take care of any
5694 * errors encountered.
5695 */
5696 static void
5697redraw_custum_statusline(wp)
5698 win_T *wp;
5699{
5700 int save_called_emsg = called_emsg;
5701
5702 called_emsg = FALSE;
5703 win_redr_custom(wp, FALSE);
5704 if (called_emsg)
5705 set_string_option_direct((char_u *)"statusline", -1,
5706 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005707 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005708 called_emsg |= save_called_emsg;
5709}
5710#endif
5711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712# ifdef FEAT_VERTSPLIT
5713/*
5714 * Return TRUE if the status line of window "wp" is connected to the status
5715 * line of the window right of it. If not, then it's a vertical separator.
5716 * Only call if (wp->w_vsep_width != 0).
5717 */
5718 int
5719stl_connected(wp)
5720 win_T *wp;
5721{
5722 frame_T *fr;
5723
5724 fr = wp->w_frame;
5725 while (fr->fr_parent != NULL)
5726 {
5727 if (fr->fr_parent->fr_layout == FR_COL)
5728 {
5729 if (fr->fr_next != NULL)
5730 break;
5731 }
5732 else
5733 {
5734 if (fr->fr_next != NULL)
5735 return TRUE;
5736 }
5737 fr = fr->fr_parent;
5738 }
5739 return FALSE;
5740}
5741# endif
5742
5743#endif /* FEAT_WINDOWS */
5744
5745#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5746/*
5747 * Get the value to show for the language mappings, active 'keymap'.
5748 */
5749 int
5750get_keymap_str(wp, buf, len)
5751 win_T *wp;
5752 char_u *buf; /* buffer for the result */
5753 int len; /* length of buffer */
5754{
5755 char_u *p;
5756
5757 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5758 return FALSE;
5759
5760 {
5761#ifdef FEAT_EVAL
5762 buf_T *old_curbuf = curbuf;
5763 win_T *old_curwin = curwin;
5764 char_u *s;
5765
5766 curbuf = wp->w_buffer;
5767 curwin = wp;
5768 STRCPY(buf, "b:keymap_name"); /* must be writable */
5769 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005770 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 --emsg_skip;
5772 curbuf = old_curbuf;
5773 curwin = old_curwin;
5774 if (p == NULL || *p == NUL)
5775#endif
5776 {
5777#ifdef FEAT_KEYMAP
5778 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5779 p = wp->w_buffer->b_p_keymap;
5780 else
5781#endif
5782 p = (char_u *)"lang";
5783 }
5784 if ((int)(STRLEN(p) + 3) < len)
5785 sprintf((char *)buf, "<%s>", p);
5786 else
5787 buf[0] = NUL;
5788#ifdef FEAT_EVAL
5789 vim_free(s);
5790#endif
5791 }
5792 return buf[0] != NUL;
5793}
5794#endif
5795
5796#if defined(FEAT_STL_OPT) || defined(PROTO)
5797/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005798 * Redraw the status line or ruler of window "wp".
5799 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 */
5801 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005802win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005804 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 int attr;
5807 int curattr;
5808 int row;
5809 int col = 0;
5810 int maxwidth;
5811 int width;
5812 int n;
5813 int len;
5814 int fillchar;
5815 char_u buf[MAXPATHL];
5816 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005817 struct stl_hlrec hltab[STL_MAX_ITEM];
5818 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005819 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
5821 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005822 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005824 /* Use 'tabline'. Always at the first line of the screen. */
5825 p = p_tal;
5826 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005827 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005828 attr = hl_attr(HLF_TPF);
5829 maxwidth = Columns;
5830# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005831 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005834 else
5835 {
5836 row = W_WINROW(wp) + wp->w_height;
5837 fillchar = fillchar_status(&attr, wp == curwin);
5838 maxwidth = W_WIDTH(wp);
5839
5840 if (draw_ruler)
5841 {
5842 p = p_ruf;
5843 /* advance past any leading group spec - implicit in ru_col */
5844 if (*p == '%')
5845 {
5846 if (*++p == '-')
5847 p++;
5848 if (atoi((char *) p))
5849 while (VIM_ISDIGIT(*p))
5850 p++;
5851 if (*p++ != '(')
5852 p = p_ruf;
5853 }
5854#ifdef FEAT_VERTSPLIT
5855 col = ru_col - (Columns - W_WIDTH(wp));
5856 if (col < (W_WIDTH(wp) + 1) / 2)
5857 col = (W_WIDTH(wp) + 1) / 2;
5858#else
5859 col = ru_col;
5860 if (col > (Columns + 1) / 2)
5861 col = (Columns + 1) / 2;
5862#endif
5863 maxwidth = W_WIDTH(wp) - col;
5864#ifdef FEAT_WINDOWS
5865 if (!wp->w_status_height)
5866#endif
5867 {
5868 row = Rows - 1;
5869 --maxwidth; /* writing in last column may cause scrolling */
5870 fillchar = ' ';
5871 attr = 0;
5872 }
5873
5874# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005875 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005876# endif
5877 }
5878 else
5879 {
5880 if (*wp->w_p_stl != NUL)
5881 p = wp->w_p_stl;
5882 else
5883 p = p_stl;
5884# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005885 use_sandbox = was_set_insecurely((char_u *)"statusline",
5886 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005887# endif
5888 }
5889
5890#ifdef FEAT_VERTSPLIT
5891 col += W_WINCOL(wp);
5892#endif
5893 }
5894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (maxwidth <= 0)
5896 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005898 width = build_stl_str_hl(wp == NULL ? curwin : wp,
5899 buf, sizeof(buf),
5900 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005901 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005902 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
5904 while (width < maxwidth && len < sizeof(buf) - 1)
5905 {
5906#ifdef FEAT_MBYTE
5907 len += (*mb_char2bytes)(fillchar, buf + len);
5908#else
5909 buf[len++] = fillchar;
5910#endif
5911 ++width;
5912 }
5913 buf[len] = NUL;
5914
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005915 /*
5916 * Draw each snippet with the specified highlighting.
5917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 curattr = attr;
5919 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005920 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005922 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 screen_puts_len(p, len, row, col, curattr);
5924 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005925 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005927 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005929 else if (hltab[n].userhl < 0)
5930 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00005932 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005933 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934#endif
5935 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005936 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005939
5940 if (wp == NULL)
5941 {
5942 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
5943 col = 0;
5944 len = 0;
5945 p = buf;
5946 fillchar = 0;
5947 for (n = 0; tabtab[n].start != NULL; n++)
5948 {
5949 len += vim_strnsize(p, (int)(tabtab[n].start - p));
5950 while (col < len)
5951 TabPageIdxs[col++] = fillchar;
5952 p = tabtab[n].start;
5953 fillchar = tabtab[n].userhl;
5954 }
5955 while (col < Columns)
5956 TabPageIdxs[col++] = fillchar;
5957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958}
5959
5960#endif /* FEAT_STL_OPT */
5961
5962/*
5963 * Output a single character directly to the screen and update ScreenLines.
5964 */
5965 void
5966screen_putchar(c, row, col, attr)
5967 int c;
5968 int row, col;
5969 int attr;
5970{
5971#ifdef FEAT_MBYTE
5972 char_u buf[MB_MAXBYTES + 1];
5973
5974 buf[(*mb_char2bytes)(c, buf)] = NUL;
5975#else
5976 char_u buf[2];
5977
5978 buf[0] = c;
5979 buf[1] = NUL;
5980#endif
5981 screen_puts(buf, row, col, attr);
5982}
5983
5984/*
5985 * Get a single character directly from ScreenLines into "bytes[]".
5986 * Also return its attribute in *attrp;
5987 */
5988 void
5989screen_getbytes(row, col, bytes, attrp)
5990 int row, col;
5991 char_u *bytes;
5992 int *attrp;
5993{
5994 unsigned off;
5995
5996 /* safety check */
5997 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
5998 {
5999 off = LineOffset[row] + col;
6000 *attrp = ScreenAttrs[off];
6001 bytes[0] = ScreenLines[off];
6002 bytes[1] = NUL;
6003
6004#ifdef FEAT_MBYTE
6005 if (enc_utf8 && ScreenLinesUC[off] != 0)
6006 bytes[utfc_char2bytes(off, bytes)] = NUL;
6007 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6008 {
6009 bytes[0] = ScreenLines[off];
6010 bytes[1] = ScreenLines2[off];
6011 bytes[2] = NUL;
6012 }
6013 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6014 {
6015 bytes[1] = ScreenLines[off + 1];
6016 bytes[2] = NUL;
6017 }
6018#endif
6019 }
6020}
6021
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006022#ifdef FEAT_MBYTE
6023static int screen_comp_differs __ARGS((int, int*));
6024
6025/*
6026 * Return TRUE if composing characters for screen posn "off" differs from
6027 * composing characters in "u8cc".
6028 */
6029 static int
6030screen_comp_differs(off, u8cc)
6031 int off;
6032 int *u8cc;
6033{
6034 int i;
6035
6036 for (i = 0; i < Screen_mco; ++i)
6037 {
6038 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6039 return TRUE;
6040 if (u8cc[i] == 0)
6041 break;
6042 }
6043 return FALSE;
6044}
6045#endif
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047/*
6048 * Put string '*text' on the screen at position 'row' and 'col', with
6049 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6050 * Note: only outputs within one row, message is truncated at screen boundary!
6051 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6052 */
6053 void
6054screen_puts(text, row, col, attr)
6055 char_u *text;
6056 int row;
6057 int col;
6058 int attr;
6059{
6060 screen_puts_len(text, -1, row, col, attr);
6061}
6062
6063/*
6064 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6065 * a NUL.
6066 */
6067 void
6068screen_puts_len(text, len, row, col, attr)
6069 char_u *text;
6070 int len;
6071 int row;
6072 int col;
6073 int attr;
6074{
6075 unsigned off;
6076 char_u *ptr = text;
6077 int c;
6078#ifdef FEAT_MBYTE
6079 int mbyte_blen = 1;
6080 int mbyte_cells = 1;
6081 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006082 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 int clear_next_cell = FALSE;
6084# ifdef FEAT_ARABIC
6085 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006086 int pc, nc, nc1;
6087 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088# endif
6089#endif
6090
6091 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6092 return;
6093
6094 off = LineOffset[row] + col;
6095 while (*ptr != NUL && col < screen_Columns
6096 && (len < 0 || (int)(ptr - text) < len))
6097 {
6098 c = *ptr;
6099#ifdef FEAT_MBYTE
6100 /* check if this is the first byte of a multibyte */
6101 if (has_mbyte)
6102 {
6103 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006104 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006106 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6108 mbyte_cells = 1;
6109 else if (enc_dbcs != 0)
6110 mbyte_cells = mbyte_blen;
6111 else /* enc_utf8 */
6112 {
6113 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006114 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 (int)((text + len) - ptr));
6116 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006117 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 mbyte_cells = utf_char2cells(u8c);
6119 /* Non-BMP character: display as ? or fullwidth ?. */
6120 if (u8c >= 0x10000)
6121 {
6122 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6123 if (attr == 0)
6124 attr = hl_attr(HLF_8);
6125 }
6126# ifdef FEAT_ARABIC
6127 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6128 {
6129 /* Do Arabic shaping. */
6130 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6131 {
6132 /* Past end of string to be displayed. */
6133 nc = NUL;
6134 nc1 = NUL;
6135 }
6136 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006137 {
6138 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6139 nc1 = pcc[0];
6140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 pc = prev_c;
6142 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006143 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 }
6145 else
6146 prev_c = u8c;
6147# endif
6148 }
6149 }
6150#endif
6151
6152 if (ScreenLines[off] != c
6153#ifdef FEAT_MBYTE
6154 || (mbyte_cells == 2
6155 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6156 || (enc_dbcs == DBCS_JPNU
6157 && c == 0x8e
6158 && ScreenLines2[off] != ptr[1])
6159 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006160 && (ScreenLinesUC[off] != (u8char_T)u8c
6161 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162#endif
6163 || ScreenAttrs[off] != attr
6164 || exmode_active
6165 )
6166 {
6167#if defined(FEAT_GUI) || defined(UNIX)
6168 /* The bold trick makes a single row of pixels appear in the next
6169 * character. When a bold character is removed, the next
6170 * character should be redrawn too. This happens for our own GUI
6171 * and for some xterms.
6172 * Force the redraw by setting the attribute to a different value
6173 * than "attr", the contents of ScreenLines[] may be needed by
6174 * mb_off2cells() further on.
6175 * Don't do this for the last drawn character, because the next
6176 * character may not be redrawn. */
6177 if (
6178# ifdef FEAT_GUI
6179 gui.in_use
6180# endif
6181# if defined(FEAT_GUI) && defined(UNIX)
6182 ||
6183# endif
6184# ifdef UNIX
6185 term_is_xterm
6186# endif
6187 )
6188 {
6189 int n;
6190
6191 n = ScreenAttrs[off];
6192# ifdef FEAT_MBYTE
6193 if (col + mbyte_cells < screen_Columns
6194 && (n > HL_ALL || (n & HL_BOLD))
6195 && (len < 0 ? ptr[mbyte_blen] != NUL
6196 : ptr + mbyte_blen < text + len))
6197 ScreenAttrs[off + mbyte_cells] = attr + 1;
6198# else
6199 if (col + 1 < screen_Columns
6200 && (n > HL_ALL || (n & HL_BOLD))
6201 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6202 ScreenLines[off + 1] = 0;
6203# endif
6204 }
6205#endif
6206#ifdef FEAT_MBYTE
6207 /* When at the end of the text and overwriting a two-cell
6208 * character with a one-cell character, need to clear the next
6209 * cell. Also when overwriting the left halve of a two-cell char
6210 * with the right halve of a two-cell char. Do this only once
6211 * (mb_off2cells() may return 2 on the right halve). */
6212 if (clear_next_cell)
6213 clear_next_cell = FALSE;
6214 else if (has_mbyte
6215 && (len < 0 ? ptr[mbyte_blen] == NUL
6216 : ptr + mbyte_blen >= text + len)
6217 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6218 || (mbyte_cells == 2
6219 && (*mb_off2cells)(off) == 1
6220 && (*mb_off2cells)(off + 1) > 1)))
6221 clear_next_cell = TRUE;
6222
6223 /* Make sure we never leave a second byte of a double-byte behind,
6224 * it confuses mb_off2cells(). */
6225 if (enc_dbcs
6226 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6227 || (mbyte_cells == 2
6228 && (*mb_off2cells)(off) == 1
6229 && (*mb_off2cells)(off + 1) > 1)))
6230 ScreenLines[off + mbyte_blen] = 0;
6231#endif
6232 ScreenLines[off] = c;
6233 ScreenAttrs[off] = attr;
6234#ifdef FEAT_MBYTE
6235 if (enc_utf8)
6236 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006237 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 ScreenLinesUC[off] = 0;
6239 else
6240 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006241 int i;
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006244 for (i = 0; i < Screen_mco; ++i)
6245 {
6246 ScreenLinesC[i][off] = u8cc[i];
6247 if (u8cc[i] == 0)
6248 break;
6249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251 if (mbyte_cells == 2)
6252 {
6253 ScreenLines[off + 1] = 0;
6254 ScreenAttrs[off + 1] = attr;
6255 }
6256 screen_char(off, row, col);
6257 }
6258 else if (mbyte_cells == 2)
6259 {
6260 ScreenLines[off + 1] = ptr[1];
6261 ScreenAttrs[off + 1] = attr;
6262 screen_char_2(off, row, col);
6263 }
6264 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6265 {
6266 ScreenLines2[off] = ptr[1];
6267 screen_char(off, row, col);
6268 }
6269 else
6270#endif
6271 screen_char(off, row, col);
6272 }
6273#ifdef FEAT_MBYTE
6274 if (has_mbyte)
6275 {
6276 off += mbyte_cells;
6277 col += mbyte_cells;
6278 ptr += mbyte_blen;
6279 if (clear_next_cell)
6280 ptr = (char_u *)" ";
6281 }
6282 else
6283#endif
6284 {
6285 ++off;
6286 ++col;
6287 ++ptr;
6288 }
6289 }
6290}
6291
6292#ifdef FEAT_SEARCH_EXTRA
6293/*
6294 * Prepare for 'searchhl' highlighting.
6295 */
6296 static void
6297start_search_hl()
6298{
6299 if (p_hls && !no_hlsearch)
6300 {
6301 last_pat_prog(&search_hl.rm);
6302 search_hl.attr = hl_attr(HLF_L);
6303 }
6304}
6305
6306/*
6307 * Clean up for 'searchhl' highlighting.
6308 */
6309 static void
6310end_search_hl()
6311{
6312 if (search_hl.rm.regprog != NULL)
6313 {
6314 vim_free(search_hl.rm.regprog);
6315 search_hl.rm.regprog = NULL;
6316 }
6317}
6318
6319/*
6320 * Advance to the match in window "wp" line "lnum" or past it.
6321 */
6322 static void
6323prepare_search_hl(wp, lnum)
6324 win_T *wp;
6325 linenr_T lnum;
6326{
6327 match_T *shl; /* points to search_hl or match_hl */
6328 int n;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006329 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 /*
6332 * When using a multi-line pattern, start searching at the top
6333 * of the window or just after a closed fold.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006334 * Do this both for search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006336 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006338 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 if (shl->rm.regprog != NULL
6340 && shl->lnum == 0
6341 && re_multiline(shl->rm.regprog))
6342 {
6343 if (shl->first_lnum == 0)
6344 {
6345# ifdef FEAT_FOLDING
6346 for (shl->first_lnum = lnum;
6347 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6348 if (hasFoldingWin(wp, shl->first_lnum - 1,
6349 NULL, NULL, TRUE, NULL))
6350 break;
6351# else
6352 shl->first_lnum = wp->w_topline;
6353# endif
6354 }
6355 n = 0;
6356 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6357 {
6358 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6359 if (shl->lnum != 0)
6360 {
6361 shl->first_lnum = shl->lnum
6362 + shl->rm.endpos[0].lnum
6363 - shl->rm.startpos[0].lnum;
6364 n = shl->rm.endpos[0].col;
6365 }
6366 else
6367 {
6368 ++shl->first_lnum;
6369 n = 0;
6370 }
6371 }
6372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 }
6374}
6375
6376/*
6377 * Search for a next 'searchl' or ":match" match.
6378 * Uses shl->buf.
6379 * Sets shl->lnum and shl->rm contents.
6380 * Note: Assumes a previous match is always before "lnum", unless
6381 * shl->lnum is zero.
6382 * Careful: Any pointers for buffer lines will become invalid.
6383 */
6384 static void
6385next_search_hl(win, shl, lnum, mincol)
6386 win_T *win;
6387 match_T *shl; /* points to search_hl or match_hl */
6388 linenr_T lnum;
6389 colnr_T mincol; /* minimal column for a match */
6390{
6391 linenr_T l;
6392 colnr_T matchcol;
6393 long nmatched;
6394
6395 if (shl->lnum != 0)
6396 {
6397 /* Check for three situations:
6398 * 1. If the "lnum" is below a previous match, start a new search.
6399 * 2. If the previous match includes "mincol", use it.
6400 * 3. Continue after the previous match.
6401 */
6402 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6403 if (lnum > l)
6404 shl->lnum = 0;
6405 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6406 return;
6407 }
6408
6409 /*
6410 * Repeat searching for a match until one is found that includes "mincol"
6411 * or none is found in this line.
6412 */
6413 called_emsg = FALSE;
6414 for (;;)
6415 {
6416 /* Three situations:
6417 * 1. No useful previous match: search from start of line.
6418 * 2. Not Vi compatible or empty match: continue at next character.
6419 * Break the loop if this is beyond the end of the line.
6420 * 3. Vi compatible searching: continue at end of previous match.
6421 */
6422 if (shl->lnum == 0)
6423 matchcol = 0;
6424 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6425 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006426 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006428 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006429
6430 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006431 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006432 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006434 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 shl->lnum = 0;
6436 break;
6437 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006438#ifdef FEAT_MBYTE
6439 if (has_mbyte)
6440 matchcol += mb_ptr2len(ml);
6441 else
6442#endif
6443 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 }
6445 else
6446 matchcol = shl->rm.endpos[0].col;
6447
6448 shl->lnum = lnum;
6449 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6450 if (called_emsg)
6451 {
6452 /* Error while handling regexp: stop using this regexp. */
6453 vim_free(shl->rm.regprog);
6454 shl->rm.regprog = NULL;
6455 no_hlsearch = TRUE;
6456 break;
6457 }
6458 if (nmatched == 0)
6459 {
6460 shl->lnum = 0; /* no match found */
6461 break;
6462 }
6463 if (shl->rm.startpos[0].lnum > 0
6464 || shl->rm.startpos[0].col >= mincol
6465 || nmatched > 1
6466 || shl->rm.endpos[0].col > mincol)
6467 {
6468 shl->lnum += shl->rm.startpos[0].lnum;
6469 break; /* useful match found */
6470 }
6471 }
6472}
6473#endif
6474
6475 static void
6476screen_start_highlight(attr)
6477 int attr;
6478{
6479 attrentry_T *aep = NULL;
6480
6481 screen_attr = attr;
6482 if (full_screen
6483#ifdef WIN3264
6484 && termcap_active
6485#endif
6486 )
6487 {
6488#ifdef FEAT_GUI
6489 if (gui.in_use)
6490 {
6491 char buf[20];
6492
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006493 /* The GUI handles this internally. */
6494 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 OUT_STR(buf);
6496 }
6497 else
6498#endif
6499 {
6500 if (attr > HL_ALL) /* special HL attr. */
6501 {
6502 if (t_colors > 1)
6503 aep = syn_cterm_attr2entry(attr);
6504 else
6505 aep = syn_term_attr2entry(attr);
6506 if (aep == NULL) /* did ":syntax clear" */
6507 attr = 0;
6508 else
6509 attr = aep->ae_attr;
6510 }
6511 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6512 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006513 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6514 && cterm_normal_fg_bold)
6515 /* If the Normal FG color has BOLD attribute and the new HL
6516 * has a FG color defined, clear BOLD. */
6517 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6519 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006520 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6521 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 out_str(T_US);
6523 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6524 out_str(T_CZH);
6525 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6526 out_str(T_MR);
6527
6528 /*
6529 * Output the color or start string after bold etc., in case the
6530 * bold etc. override the color setting.
6531 */
6532 if (aep != NULL)
6533 {
6534 if (t_colors > 1)
6535 {
6536 if (aep->ae_u.cterm.fg_color)
6537 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6538 if (aep->ae_u.cterm.bg_color)
6539 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6540 }
6541 else
6542 {
6543 if (aep->ae_u.term.start != NULL)
6544 out_str(aep->ae_u.term.start);
6545 }
6546 }
6547 }
6548 }
6549}
6550
6551 void
6552screen_stop_highlight()
6553{
6554 int do_ME = FALSE; /* output T_ME code */
6555
6556 if (screen_attr != 0
6557#ifdef WIN3264
6558 && termcap_active
6559#endif
6560 )
6561 {
6562#ifdef FEAT_GUI
6563 if (gui.in_use)
6564 {
6565 char buf[20];
6566
6567 /* use internal GUI code */
6568 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6569 OUT_STR(buf);
6570 }
6571 else
6572#endif
6573 {
6574 if (screen_attr > HL_ALL) /* special HL attr. */
6575 {
6576 attrentry_T *aep;
6577
6578 if (t_colors > 1)
6579 {
6580 /*
6581 * Assume that t_me restores the original colors!
6582 */
6583 aep = syn_cterm_attr2entry(screen_attr);
6584 if (aep != NULL && (aep->ae_u.cterm.fg_color
6585 || aep->ae_u.cterm.bg_color))
6586 do_ME = TRUE;
6587 }
6588 else
6589 {
6590 aep = syn_term_attr2entry(screen_attr);
6591 if (aep != NULL && aep->ae_u.term.stop != NULL)
6592 {
6593 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6594 do_ME = TRUE;
6595 else
6596 out_str(aep->ae_u.term.stop);
6597 }
6598 }
6599 if (aep == NULL) /* did ":syntax clear" */
6600 screen_attr = 0;
6601 else
6602 screen_attr = aep->ae_attr;
6603 }
6604
6605 /*
6606 * Often all ending-codes are equal to T_ME. Avoid outputting the
6607 * same sequence several times.
6608 */
6609 if (screen_attr & HL_STANDOUT)
6610 {
6611 if (STRCMP(T_SE, T_ME) == 0)
6612 do_ME = TRUE;
6613 else
6614 out_str(T_SE);
6615 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006616 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 {
6618 if (STRCMP(T_UE, T_ME) == 0)
6619 do_ME = TRUE;
6620 else
6621 out_str(T_UE);
6622 }
6623 if (screen_attr & HL_ITALIC)
6624 {
6625 if (STRCMP(T_CZR, T_ME) == 0)
6626 do_ME = TRUE;
6627 else
6628 out_str(T_CZR);
6629 }
6630 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6631 out_str(T_ME);
6632
6633 if (t_colors > 1)
6634 {
6635 /* set Normal cterm colors */
6636 if (cterm_normal_fg_color != 0)
6637 term_fg_color(cterm_normal_fg_color - 1);
6638 if (cterm_normal_bg_color != 0)
6639 term_bg_color(cterm_normal_bg_color - 1);
6640 if (cterm_normal_fg_bold)
6641 out_str(T_MD);
6642 }
6643 }
6644 }
6645 screen_attr = 0;
6646}
6647
6648/*
6649 * Reset the colors for a cterm. Used when leaving Vim.
6650 * The machine specific code may override this again.
6651 */
6652 void
6653reset_cterm_colors()
6654{
6655 if (t_colors > 1)
6656 {
6657 /* set Normal cterm colors */
6658 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6659 {
6660 out_str(T_OP);
6661 screen_attr = -1;
6662 }
6663 if (cterm_normal_fg_bold)
6664 {
6665 out_str(T_ME);
6666 screen_attr = -1;
6667 }
6668 }
6669}
6670
6671/*
6672 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6673 * using the attributes from ScreenAttrs["off"].
6674 */
6675 static void
6676screen_char(off, row, col)
6677 unsigned off;
6678 int row;
6679 int col;
6680{
6681 int attr;
6682
6683 /* Check for illegal values, just in case (could happen just after
6684 * resizing). */
6685 if (row >= screen_Rows || col >= screen_Columns)
6686 return;
6687
6688 /* Outputting the last character on the screen may scrollup the screen.
6689 * Don't to it! Mark the character invalid (update it when scrolled up) */
6690 if (row == screen_Rows - 1 && col == screen_Columns - 1
6691#ifdef FEAT_RIGHTLEFT
6692 /* account for first command-line character in rightleft mode */
6693 && !cmdmsg_rl
6694#endif
6695 )
6696 {
6697 ScreenAttrs[off] = (sattr_T)-1;
6698 return;
6699 }
6700
6701 /*
6702 * Stop highlighting first, so it's easier to move the cursor.
6703 */
6704#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6705 if (screen_char_attr != 0)
6706 attr = screen_char_attr;
6707 else
6708#endif
6709 attr = ScreenAttrs[off];
6710 if (screen_attr != attr)
6711 screen_stop_highlight();
6712
6713 windgoto(row, col);
6714
6715 if (screen_attr != attr)
6716 screen_start_highlight(attr);
6717
6718#ifdef FEAT_MBYTE
6719 if (enc_utf8 && ScreenLinesUC[off] != 0)
6720 {
6721 char_u buf[MB_MAXBYTES + 1];
6722
6723 /* Convert UTF-8 character to bytes and write it. */
6724
6725 buf[utfc_char2bytes(off, buf)] = NUL;
6726
6727 out_str(buf);
6728 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6729 ++screen_cur_col;
6730 }
6731 else
6732#endif
6733 {
6734#ifdef FEAT_MBYTE
6735 out_flush_check();
6736#endif
6737 out_char(ScreenLines[off]);
6738#ifdef FEAT_MBYTE
6739 /* double-byte character in single-width cell */
6740 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6741 out_char(ScreenLines2[off]);
6742#endif
6743 }
6744
6745 screen_cur_col++;
6746}
6747
6748#ifdef FEAT_MBYTE
6749
6750/*
6751 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6752 * on the screen at position 'row' and 'col'.
6753 * The attributes of the first byte is used for all. This is required to
6754 * output the two bytes of a double-byte character with nothing in between.
6755 */
6756 static void
6757screen_char_2(off, row, col)
6758 unsigned off;
6759 int row;
6760 int col;
6761{
6762 /* Check for illegal values (could be wrong when screen was resized). */
6763 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6764 return;
6765
6766 /* Outputting the last character on the screen may scrollup the screen.
6767 * Don't to it! Mark the character invalid (update it when scrolled up) */
6768 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6769 {
6770 ScreenAttrs[off] = (sattr_T)-1;
6771 return;
6772 }
6773
6774 /* Output the first byte normally (positions the cursor), then write the
6775 * second byte directly. */
6776 screen_char(off, row, col);
6777 out_char(ScreenLines[off + 1]);
6778 ++screen_cur_col;
6779}
6780#endif
6781
6782#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6783/*
6784 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6785 * This uses the contents of ScreenLines[] and doesn't change it.
6786 */
6787 void
6788screen_draw_rectangle(row, col, height, width, invert)
6789 int row;
6790 int col;
6791 int height;
6792 int width;
6793 int invert;
6794{
6795 int r, c;
6796 int off;
6797
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006798 /* Can't use ScreenLines unless initialized */
6799 if (ScreenLines == NULL)
6800 return;
6801
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 if (invert)
6803 screen_char_attr = HL_INVERSE;
6804 for (r = row; r < row + height; ++r)
6805 {
6806 off = LineOffset[r];
6807 for (c = col; c < col + width; ++c)
6808 {
6809#ifdef FEAT_MBYTE
6810 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6811 {
6812 screen_char_2(off + c, r, c);
6813 ++c;
6814 }
6815 else
6816#endif
6817 {
6818 screen_char(off + c, r, c);
6819#ifdef FEAT_MBYTE
6820 if (utf_off2cells(off + c) > 1)
6821 ++c;
6822#endif
6823 }
6824 }
6825 }
6826 screen_char_attr = 0;
6827}
6828#endif
6829
6830#ifdef FEAT_VERTSPLIT
6831/*
6832 * Redraw the characters for a vertically split window.
6833 */
6834 static void
6835redraw_block(row, end, wp)
6836 int row;
6837 int end;
6838 win_T *wp;
6839{
6840 int col;
6841 int width;
6842
6843# ifdef FEAT_CLIPBOARD
6844 clip_may_clear_selection(row, end - 1);
6845# endif
6846
6847 if (wp == NULL)
6848 {
6849 col = 0;
6850 width = Columns;
6851 }
6852 else
6853 {
6854 col = wp->w_wincol;
6855 width = wp->w_width;
6856 }
6857 screen_draw_rectangle(row, col, end - row, width, FALSE);
6858}
6859#endif
6860
6861/*
6862 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6863 * with character 'c1' in first column followed by 'c2' in the other columns.
6864 * Use attributes 'attr'.
6865 */
6866 void
6867screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6868 int start_row, end_row;
6869 int start_col, end_col;
6870 int c1, c2;
6871 int attr;
6872{
6873 int row;
6874 int col;
6875 int off;
6876 int end_off;
6877 int did_delete;
6878 int c;
6879 int norm_term;
6880#if defined(FEAT_GUI) || defined(UNIX)
6881 int force_next = FALSE;
6882#endif
6883
6884 if (end_row > screen_Rows) /* safety check */
6885 end_row = screen_Rows;
6886 if (end_col > screen_Columns) /* safety check */
6887 end_col = screen_Columns;
6888 if (ScreenLines == NULL
6889 || start_row >= end_row
6890 || start_col >= end_col) /* nothing to do */
6891 return;
6892
6893 /* it's a "normal" terminal when not in a GUI or cterm */
6894 norm_term = (
6895#ifdef FEAT_GUI
6896 !gui.in_use &&
6897#endif
6898 t_colors <= 1);
6899 for (row = start_row; row < end_row; ++row)
6900 {
6901 /*
6902 * Try to use delete-line termcap code, when no attributes or in a
6903 * "normal" terminal, where a bold/italic space is just a
6904 * space.
6905 */
6906 did_delete = FALSE;
6907 if (c2 == ' '
6908 && end_col == Columns
6909 && can_clear(T_CE)
6910 && (attr == 0
6911 || (norm_term
6912 && attr <= HL_ALL
6913 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6914 {
6915 /*
6916 * check if we really need to clear something
6917 */
6918 col = start_col;
6919 if (c1 != ' ') /* don't clear first char */
6920 ++col;
6921
6922 off = LineOffset[row] + col;
6923 end_off = LineOffset[row] + end_col;
6924
6925 /* skip blanks (used often, keep it fast!) */
6926#ifdef FEAT_MBYTE
6927 if (enc_utf8)
6928 while (off < end_off && ScreenLines[off] == ' '
6929 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6930 ++off;
6931 else
6932#endif
6933 while (off < end_off && ScreenLines[off] == ' '
6934 && ScreenAttrs[off] == 0)
6935 ++off;
6936 if (off < end_off) /* something to be cleared */
6937 {
6938 col = off - LineOffset[row];
6939 screen_stop_highlight();
6940 term_windgoto(row, col);/* clear rest of this screen line */
6941 out_str(T_CE);
6942 screen_start(); /* don't know where cursor is now */
6943 col = end_col - col;
6944 while (col--) /* clear chars in ScreenLines */
6945 {
6946 ScreenLines[off] = ' ';
6947#ifdef FEAT_MBYTE
6948 if (enc_utf8)
6949 ScreenLinesUC[off] = 0;
6950#endif
6951 ScreenAttrs[off] = 0;
6952 ++off;
6953 }
6954 }
6955 did_delete = TRUE; /* the chars are cleared now */
6956 }
6957
6958 off = LineOffset[row] + start_col;
6959 c = c1;
6960 for (col = start_col; col < end_col; ++col)
6961 {
6962 if (ScreenLines[off] != c
6963#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006964 || (enc_utf8 && (int)ScreenLinesUC[off]
6965 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966#endif
6967 || ScreenAttrs[off] != attr
6968#if defined(FEAT_GUI) || defined(UNIX)
6969 || force_next
6970#endif
6971 )
6972 {
6973#if defined(FEAT_GUI) || defined(UNIX)
6974 /* The bold trick may make a single row of pixels appear in
6975 * the next character. When a bold character is removed, the
6976 * next character should be redrawn too. This happens for our
6977 * own GUI and for some xterms. */
6978 if (
6979# ifdef FEAT_GUI
6980 gui.in_use
6981# endif
6982# if defined(FEAT_GUI) && defined(UNIX)
6983 ||
6984# endif
6985# ifdef UNIX
6986 term_is_xterm
6987# endif
6988 )
6989 {
6990 if (ScreenLines[off] != ' '
6991 && (ScreenAttrs[off] > HL_ALL
6992 || ScreenAttrs[off] & HL_BOLD))
6993 force_next = TRUE;
6994 else
6995 force_next = FALSE;
6996 }
6997#endif
6998 ScreenLines[off] = c;
6999#ifdef FEAT_MBYTE
7000 if (enc_utf8)
7001 {
7002 if (c >= 0x80)
7003 {
7004 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007005 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 }
7007 else
7008 ScreenLinesUC[off] = 0;
7009 }
7010#endif
7011 ScreenAttrs[off] = attr;
7012 if (!did_delete || c != ' ')
7013 screen_char(off, row, col);
7014 }
7015 ++off;
7016 if (col == start_col)
7017 {
7018 if (did_delete)
7019 break;
7020 c = c2;
7021 }
7022 }
7023 if (end_col == Columns)
7024 LineWraps[row] = FALSE;
7025 if (row == Rows - 1) /* overwritten the command line */
7026 {
7027 redraw_cmdline = TRUE;
7028 if (c1 == ' ' && c2 == ' ')
7029 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007030 if (start_col == 0)
7031 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033 }
7034}
7035
7036/*
7037 * Check if there should be a delay. Used before clearing or redrawing the
7038 * screen or the command line.
7039 */
7040 void
7041check_for_delay(check_msg_scroll)
7042 int check_msg_scroll;
7043{
7044 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7045 && !did_wait_return
7046 && emsg_silent == 0)
7047 {
7048 out_flush();
7049 ui_delay(1000L, TRUE);
7050 emsg_on_display = FALSE;
7051 if (check_msg_scroll)
7052 msg_scroll = FALSE;
7053 }
7054}
7055
7056/*
7057 * screen_valid - allocate screen buffers if size changed
7058 * If "clear" is TRUE: clear screen if it has been resized.
7059 * Returns TRUE if there is a valid screen to write to.
7060 * Returns FALSE when starting up and screen not initialized yet.
7061 */
7062 int
7063screen_valid(clear)
7064 int clear;
7065{
7066 screenalloc(clear); /* allocate screen buffers if size changed */
7067 return (ScreenLines != NULL);
7068}
7069
7070/*
7071 * Resize the shell to Rows and Columns.
7072 * Allocate ScreenLines[] and associated items.
7073 *
7074 * There may be some time between setting Rows and Columns and (re)allocating
7075 * ScreenLines[]. This happens when starting up and when (manually) changing
7076 * the shell size. Always use screen_Rows and screen_Columns to access items
7077 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7078 * final size of the shell is needed.
7079 */
7080 void
7081screenalloc(clear)
7082 int clear;
7083{
7084 int new_row, old_row;
7085#ifdef FEAT_GUI
7086 int old_Rows;
7087#endif
7088 win_T *wp;
7089 int outofmem = FALSE;
7090 int len;
7091 schar_T *new_ScreenLines;
7092#ifdef FEAT_MBYTE
7093 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007094 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007096 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097#endif
7098 sattr_T *new_ScreenAttrs;
7099 unsigned *new_LineOffset;
7100 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007101#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007102 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007103 tabpage_T *tp;
7104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007106 static int did_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 /*
7109 * Allocation of the screen buffers is done only when the size changes and
7110 * when Rows and Columns have been set and we have started doing full
7111 * screen stuff.
7112 */
7113 if ((ScreenLines != NULL
7114 && Rows == screen_Rows
7115 && Columns == screen_Columns
7116#ifdef FEAT_MBYTE
7117 && enc_utf8 == (ScreenLinesUC != NULL)
7118 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007119 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120#endif
7121 )
7122 || Rows == 0
7123 || Columns == 0
7124 || (!full_screen && ScreenLines == NULL))
7125 return;
7126
7127 /*
7128 * It's possible that we produce an out-of-memory message below, which
7129 * will cause this function to be called again. To break the loop, just
7130 * return here.
7131 */
7132 if (entered)
7133 return;
7134 entered = TRUE;
7135
7136 win_new_shellsize(); /* fit the windows in the new sized shell */
7137
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 comp_col(); /* recompute columns for shown command and ruler */
7139
7140 /*
7141 * We're changing the size of the screen.
7142 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7143 * - Move lines from the old arrays into the new arrays, clear extra
7144 * lines (unless the screen is going to be cleared).
7145 * - Free the old arrays.
7146 *
7147 * If anything fails, make ScreenLines NULL, so we don't do anything!
7148 * Continuing with the old ScreenLines may result in a crash, because the
7149 * size is wrong.
7150 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007151 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154 new_ScreenLines = (schar_T *)lalloc((long_u)(
7155 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7156#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007157 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (enc_utf8)
7159 {
7160 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7161 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007162 for (i = 0; i < p_mco; ++i)
7163 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7165 }
7166 if (enc_dbcs == DBCS_JPNU)
7167 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7168 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7169#endif
7170 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7171 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7172 new_LineOffset = (unsigned *)lalloc((long_u)(
7173 Rows * sizeof(unsigned)), FALSE);
7174 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007175#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007176 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007179 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {
7181 if (win_alloc_lines(wp) == FAIL)
7182 {
7183 outofmem = TRUE;
7184#ifdef FEAT_WINDOWS
7185 break;
7186#endif
7187 }
7188 }
7189
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007190#ifdef FEAT_MBYTE
7191 for (i = 0; i < p_mco; ++i)
7192 if (new_ScreenLinesC[i] == NULL)
7193 break;
7194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 if (new_ScreenLines == NULL
7196#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007197 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7199#endif
7200 || new_ScreenAttrs == NULL
7201 || new_LineOffset == NULL
7202 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007203#ifdef FEAT_WINDOWS
7204 || new_TabPageIdxs == NULL
7205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 || outofmem)
7207 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007208 if (ScreenLines != NULL || !did_outofmem_msg)
7209 {
7210 /* guess the size */
7211 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7212
7213 /* Remember we did this to avoid getting outofmem messages over
7214 * and over again. */
7215 did_outofmem_msg = TRUE;
7216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 vim_free(new_ScreenLines);
7218 new_ScreenLines = NULL;
7219#ifdef FEAT_MBYTE
7220 vim_free(new_ScreenLinesUC);
7221 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007222 for (i = 0; i < p_mco; ++i)
7223 {
7224 vim_free(new_ScreenLinesC[i]);
7225 new_ScreenLinesC[i] = NULL;
7226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 vim_free(new_ScreenLines2);
7228 new_ScreenLines2 = NULL;
7229#endif
7230 vim_free(new_ScreenAttrs);
7231 new_ScreenAttrs = NULL;
7232 vim_free(new_LineOffset);
7233 new_LineOffset = NULL;
7234 vim_free(new_LineWraps);
7235 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007236#ifdef FEAT_WINDOWS
7237 vim_free(new_TabPageIdxs);
7238 new_TabPageIdxs = NULL;
7239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
7241 else
7242 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007243 did_outofmem_msg = FALSE;
7244
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 for (new_row = 0; new_row < Rows; ++new_row)
7246 {
7247 new_LineOffset[new_row] = new_row * Columns;
7248 new_LineWraps[new_row] = FALSE;
7249
7250 /*
7251 * If the screen is not going to be cleared, copy as much as
7252 * possible from the old screen to the new one and clear the rest
7253 * (used when resizing the window at the "--more--" prompt or when
7254 * executing an external command, for the GUI).
7255 */
7256 if (!clear)
7257 {
7258 (void)vim_memset(new_ScreenLines + new_row * Columns,
7259 ' ', (size_t)Columns * sizeof(schar_T));
7260#ifdef FEAT_MBYTE
7261 if (enc_utf8)
7262 {
7263 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7264 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007265 for (i = 0; i < p_mco; ++i)
7266 (void)vim_memset(new_ScreenLinesC[i]
7267 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 0, (size_t)Columns * sizeof(u8char_T));
7269 }
7270 if (enc_dbcs == DBCS_JPNU)
7271 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7272 0, (size_t)Columns * sizeof(schar_T));
7273#endif
7274 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7275 0, (size_t)Columns * sizeof(sattr_T));
7276 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007277 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 {
7279 if (screen_Columns < Columns)
7280 len = screen_Columns;
7281 else
7282 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007283#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007284 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007285 * may be invalid now. Also when p_mco changes. */
7286 if (!(enc_utf8 && ScreenLinesUC == NULL)
7287 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007288#endif
7289 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7290 ScreenLines + LineOffset[old_row],
7291 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007293 if (enc_utf8 && ScreenLinesUC != NULL
7294 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
7296 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7297 ScreenLinesUC + LineOffset[old_row],
7298 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007299 for (i = 0; i < p_mco; ++i)
7300 mch_memmove(new_ScreenLinesC[i]
7301 + new_LineOffset[new_row],
7302 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 (size_t)len * sizeof(u8char_T));
7304 }
7305 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7306 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7307 ScreenLines2 + LineOffset[old_row],
7308 (size_t)len * sizeof(schar_T));
7309#endif
7310 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7311 ScreenAttrs + LineOffset[old_row],
7312 (size_t)len * sizeof(sattr_T));
7313 }
7314 }
7315 }
7316 /* Use the last line of the screen for the current line. */
7317 current_ScreenLine = new_ScreenLines + Rows * Columns;
7318 }
7319
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007320 free_screenlines();
7321
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 ScreenLines = new_ScreenLines;
7323#ifdef FEAT_MBYTE
7324 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007325 for (i = 0; i < p_mco; ++i)
7326 ScreenLinesC[i] = new_ScreenLinesC[i];
7327 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 ScreenLines2 = new_ScreenLines2;
7329#endif
7330 ScreenAttrs = new_ScreenAttrs;
7331 LineOffset = new_LineOffset;
7332 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007333#ifdef FEAT_WINDOWS
7334 TabPageIdxs = new_TabPageIdxs;
7335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
7337 /* It's important that screen_Rows and screen_Columns reflect the actual
7338 * size of ScreenLines[]. Set them before calling anything. */
7339#ifdef FEAT_GUI
7340 old_Rows = screen_Rows;
7341#endif
7342 screen_Rows = Rows;
7343 screen_Columns = Columns;
7344
7345 must_redraw = CLEAR; /* need to clear the screen later */
7346 if (clear)
7347 screenclear2();
7348
7349#ifdef FEAT_GUI
7350 else if (gui.in_use
7351 && !gui.starting
7352 && ScreenLines != NULL
7353 && old_Rows != Rows)
7354 {
7355 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7356 /*
7357 * Adjust the position of the cursor, for when executing an external
7358 * command.
7359 */
7360 if (msg_row >= Rows) /* Rows got smaller */
7361 msg_row = Rows - 1; /* put cursor at last row */
7362 else if (Rows > old_Rows) /* Rows got bigger */
7363 msg_row += Rows - old_Rows; /* put cursor in same place */
7364 if (msg_col >= Columns) /* Columns got smaller */
7365 msg_col = Columns - 1; /* put cursor at last column */
7366 }
7367#endif
7368
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 entered = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007370
7371#ifdef FEAT_AUTOCMD
7372 if (starting == 0)
7373 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375}
7376
7377 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007378free_screenlines()
7379{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007380#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007381 int i;
7382
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007383 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007384 for (i = 0; i < Screen_mco; ++i)
7385 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007386 vim_free(ScreenLines2);
7387#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007388 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007389 vim_free(ScreenAttrs);
7390 vim_free(LineOffset);
7391 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007392#ifdef FEAT_WINDOWS
7393 vim_free(TabPageIdxs);
7394#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007395}
7396
7397 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398screenclear()
7399{
7400 check_for_delay(FALSE);
7401 screenalloc(FALSE); /* allocate screen buffers if size changed */
7402 screenclear2(); /* clear the screen */
7403}
7404
7405 static void
7406screenclear2()
7407{
7408 int i;
7409
7410 if (starting == NO_SCREEN || ScreenLines == NULL
7411#ifdef FEAT_GUI
7412 || (gui.in_use && gui.starting)
7413#endif
7414 )
7415 return;
7416
7417#ifdef FEAT_GUI
7418 if (!gui.in_use)
7419#endif
7420 screen_attr = -1; /* force setting the Normal colors */
7421 screen_stop_highlight(); /* don't want highlighting here */
7422
7423#ifdef FEAT_CLIPBOARD
7424 /* disable selection without redrawing it */
7425 clip_scroll_selection(9999);
7426#endif
7427
7428 /* blank out ScreenLines */
7429 for (i = 0; i < Rows; ++i)
7430 {
7431 lineclear(LineOffset[i], (int)Columns);
7432 LineWraps[i] = FALSE;
7433 }
7434
7435 if (can_clear(T_CL))
7436 {
7437 out_str(T_CL); /* clear the display */
7438 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007439 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 }
7441 else
7442 {
7443 /* can't clear the screen, mark all chars with invalid attributes */
7444 for (i = 0; i < Rows; ++i)
7445 lineinvalid(LineOffset[i], (int)Columns);
7446 clear_cmdline = TRUE;
7447 }
7448
7449 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7450
7451 win_rest_invalid(firstwin);
7452 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007453#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007454 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 if (must_redraw == CLEAR) /* no need to clear again */
7457 must_redraw = NOT_VALID;
7458 compute_cmdrow();
7459 msg_row = cmdline_row; /* put cursor on last line for messages */
7460 msg_col = 0;
7461 screen_start(); /* don't know where cursor is now */
7462 msg_scrolled = 0; /* can't scroll back */
7463 msg_didany = FALSE;
7464 msg_didout = FALSE;
7465}
7466
7467/*
7468 * Clear one line in ScreenLines.
7469 */
7470 static void
7471lineclear(off, width)
7472 unsigned off;
7473 int width;
7474{
7475 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7476#ifdef FEAT_MBYTE
7477 if (enc_utf8)
7478 (void)vim_memset(ScreenLinesUC + off, 0,
7479 (size_t)width * sizeof(u8char_T));
7480#endif
7481 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7482}
7483
7484/*
7485 * Mark one line in ScreenLines invalid by setting the attributes to an
7486 * invalid value.
7487 */
7488 static void
7489lineinvalid(off, width)
7490 unsigned off;
7491 int width;
7492{
7493 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7494}
7495
7496#ifdef FEAT_VERTSPLIT
7497/*
7498 * Copy part of a Screenline for vertically split window "wp".
7499 */
7500 static void
7501linecopy(to, from, wp)
7502 int to;
7503 int from;
7504 win_T *wp;
7505{
7506 unsigned off_to = LineOffset[to] + wp->w_wincol;
7507 unsigned off_from = LineOffset[from] + wp->w_wincol;
7508
7509 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7510 wp->w_width * sizeof(schar_T));
7511# ifdef FEAT_MBYTE
7512 if (enc_utf8)
7513 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007514 int i;
7515
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7517 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007518 for (i = 0; i < p_mco; ++i)
7519 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7520 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522 if (enc_dbcs == DBCS_JPNU)
7523 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7524 wp->w_width * sizeof(schar_T));
7525# endif
7526 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7527 wp->w_width * sizeof(sattr_T));
7528}
7529#endif
7530
7531/*
7532 * Return TRUE if clearing with term string "p" would work.
7533 * It can't work when the string is empty or it won't set the right background.
7534 */
7535 int
7536can_clear(p)
7537 char_u *p;
7538{
7539 return (*p != NUL && (t_colors <= 1
7540#ifdef FEAT_GUI
7541 || gui.in_use
7542#endif
7543 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7544}
7545
7546/*
7547 * Reset cursor position. Use whenever cursor was moved because of outputting
7548 * something directly to the screen (shell commands) or a terminal control
7549 * code.
7550 */
7551 void
7552screen_start()
7553{
7554 screen_cur_row = screen_cur_col = 9999;
7555}
7556
7557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 * Move the cursor to position "row","col" in the screen.
7559 * This tries to find the most efficient way to move, minimizing the number of
7560 * characters sent to the terminal.
7561 */
7562 void
7563windgoto(row, col)
7564 int row;
7565 int col;
7566{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007567 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 int i;
7569 int plan;
7570 int cost;
7571 int wouldbe_col;
7572 int noinvcurs;
7573 char_u *bs;
7574 int goto_cost;
7575 int attr;
7576
7577#define GOTO_COST 7 /* asssume a term_windgoto() takes about 7 chars */
7578#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7579
7580#define PLAN_LE 1
7581#define PLAN_CR 2
7582#define PLAN_NL 3
7583#define PLAN_WRITE 4
7584 /* Can't use ScreenLines unless initialized */
7585 if (ScreenLines == NULL)
7586 return;
7587
7588 if (col != screen_cur_col || row != screen_cur_row)
7589 {
7590 /* Check for valid position. */
7591 if (row < 0) /* window without text lines? */
7592 row = 0;
7593 if (row >= screen_Rows)
7594 row = screen_Rows - 1;
7595 if (col >= screen_Columns)
7596 col = screen_Columns - 1;
7597
7598 /* check if no cursor movement is allowed in highlight mode */
7599 if (screen_attr && *T_MS == NUL)
7600 noinvcurs = HIGHL_COST;
7601 else
7602 noinvcurs = 0;
7603 goto_cost = GOTO_COST + noinvcurs;
7604
7605 /*
7606 * Plan how to do the positioning:
7607 * 1. Use CR to move it to column 0, same row.
7608 * 2. Use T_LE to move it a few columns to the left.
7609 * 3. Use NL to move a few lines down, column 0.
7610 * 4. Move a few columns to the right with T_ND or by writing chars.
7611 *
7612 * Don't do this if the cursor went beyond the last column, the cursor
7613 * position is unknown then (some terminals wrap, some don't )
7614 *
7615 * First check if the highlighting attibutes allow us to write
7616 * characters to move the cursor to the right.
7617 */
7618 if (row >= screen_cur_row && screen_cur_col < Columns)
7619 {
7620 /*
7621 * If the cursor is in the same row, bigger col, we can use CR
7622 * or T_LE.
7623 */
7624 bs = NULL; /* init for GCC */
7625 attr = screen_attr;
7626 if (row == screen_cur_row && col < screen_cur_col)
7627 {
7628 /* "le" is preferred over "bc", because "bc" is obsolete */
7629 if (*T_LE)
7630 bs = T_LE; /* "cursor left" */
7631 else
7632 bs = T_BC; /* "backspace character (old) */
7633 if (*bs)
7634 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7635 else
7636 cost = 999;
7637 if (col + 1 < cost) /* using CR is less characters */
7638 {
7639 plan = PLAN_CR;
7640 wouldbe_col = 0;
7641 cost = 1; /* CR is just one character */
7642 }
7643 else
7644 {
7645 plan = PLAN_LE;
7646 wouldbe_col = col;
7647 }
7648 if (noinvcurs) /* will stop highlighting */
7649 {
7650 cost += noinvcurs;
7651 attr = 0;
7652 }
7653 }
7654
7655 /*
7656 * If the cursor is above where we want to be, we can use CR LF.
7657 */
7658 else if (row > screen_cur_row)
7659 {
7660 plan = PLAN_NL;
7661 wouldbe_col = 0;
7662 cost = (row - screen_cur_row) * 2; /* CR LF */
7663 if (noinvcurs) /* will stop highlighting */
7664 {
7665 cost += noinvcurs;
7666 attr = 0;
7667 }
7668 }
7669
7670 /*
7671 * If the cursor is in the same row, smaller col, just use write.
7672 */
7673 else
7674 {
7675 plan = PLAN_WRITE;
7676 wouldbe_col = screen_cur_col;
7677 cost = 0;
7678 }
7679
7680 /*
7681 * Check if any characters that need to be written have the
7682 * correct attributes. Also avoid UTF-8 characters.
7683 */
7684 i = col - wouldbe_col;
7685 if (i > 0)
7686 cost += i;
7687 if (cost < goto_cost && i > 0)
7688 {
7689 /*
7690 * Check if the attributes are correct without additionally
7691 * stopping highlighting.
7692 */
7693 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7694 while (i && *p++ == attr)
7695 --i;
7696 if (i != 0)
7697 {
7698 /*
7699 * Try if it works when highlighting is stopped here.
7700 */
7701 if (*--p == 0)
7702 {
7703 cost += noinvcurs;
7704 while (i && *p++ == 0)
7705 --i;
7706 }
7707 if (i != 0)
7708 cost = 999; /* different attributes, don't do it */
7709 }
7710#ifdef FEAT_MBYTE
7711 if (enc_utf8)
7712 {
7713 /* Don't use an UTF-8 char for positioning, it's slow. */
7714 for (i = wouldbe_col; i < col; ++i)
7715 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7716 {
7717 cost = 999;
7718 break;
7719 }
7720 }
7721#endif
7722 }
7723
7724 /*
7725 * We can do it without term_windgoto()!
7726 */
7727 if (cost < goto_cost)
7728 {
7729 if (plan == PLAN_LE)
7730 {
7731 if (noinvcurs)
7732 screen_stop_highlight();
7733 while (screen_cur_col > col)
7734 {
7735 out_str(bs);
7736 --screen_cur_col;
7737 }
7738 }
7739 else if (plan == PLAN_CR)
7740 {
7741 if (noinvcurs)
7742 screen_stop_highlight();
7743 out_char('\r');
7744 screen_cur_col = 0;
7745 }
7746 else if (plan == PLAN_NL)
7747 {
7748 if (noinvcurs)
7749 screen_stop_highlight();
7750 while (screen_cur_row < row)
7751 {
7752 out_char('\n');
7753 ++screen_cur_row;
7754 }
7755 screen_cur_col = 0;
7756 }
7757
7758 i = col - screen_cur_col;
7759 if (i > 0)
7760 {
7761 /*
7762 * Use cursor-right if it's one character only. Avoids
7763 * removing a line of pixels from the last bold char, when
7764 * using the bold trick in the GUI.
7765 */
7766 if (T_ND[0] != NUL && T_ND[1] == NUL)
7767 {
7768 while (i-- > 0)
7769 out_char(*T_ND);
7770 }
7771 else
7772 {
7773 int off;
7774
7775 off = LineOffset[row] + screen_cur_col;
7776 while (i-- > 0)
7777 {
7778 if (ScreenAttrs[off] != screen_attr)
7779 screen_stop_highlight();
7780#ifdef FEAT_MBYTE
7781 out_flush_check();
7782#endif
7783 out_char(ScreenLines[off]);
7784#ifdef FEAT_MBYTE
7785 if (enc_dbcs == DBCS_JPNU
7786 && ScreenLines[off] == 0x8e)
7787 out_char(ScreenLines2[off]);
7788#endif
7789 ++off;
7790 }
7791 }
7792 }
7793 }
7794 }
7795 else
7796 cost = 999;
7797
7798 if (cost >= goto_cost)
7799 {
7800 if (noinvcurs)
7801 screen_stop_highlight();
7802 if (row == screen_cur_row && (col > screen_cur_col) &&
7803 *T_CRI != NUL)
7804 term_cursor_right(col - screen_cur_col);
7805 else
7806 term_windgoto(row, col);
7807 }
7808 screen_cur_row = row;
7809 screen_cur_col = col;
7810 }
7811}
7812
7813/*
7814 * Set cursor to its position in the current window.
7815 */
7816 void
7817setcursor()
7818{
7819 if (redrawing())
7820 {
7821 validate_cursor();
7822 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7823 W_WINCOL(curwin) + (
7824#ifdef FEAT_RIGHTLEFT
7825 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7826# ifdef FEAT_MBYTE
7827 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7828# endif
7829 1)) :
7830#endif
7831 curwin->w_wcol));
7832 }
7833}
7834
7835
7836/*
7837 * insert 'line_count' lines at 'row' in window 'wp'
7838 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7839 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7840 * scrolling.
7841 * Returns FAIL if the lines are not inserted, OK for success.
7842 */
7843 int
7844win_ins_lines(wp, row, line_count, invalid, mayclear)
7845 win_T *wp;
7846 int row;
7847 int line_count;
7848 int invalid;
7849 int mayclear;
7850{
7851 int did_delete;
7852 int nextrow;
7853 int lastrow;
7854 int retval;
7855
7856 if (invalid)
7857 wp->w_lines_valid = 0;
7858
7859 if (wp->w_height < 5)
7860 return FAIL;
7861
7862 if (line_count > wp->w_height - row)
7863 line_count = wp->w_height - row;
7864
7865 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7866 if (retval != MAYBE)
7867 return retval;
7868
7869 /*
7870 * If there is a next window or a status line, we first try to delete the
7871 * lines at the bottom to avoid messing what is after the window.
7872 * If this fails and there are following windows, don't do anything to avoid
7873 * messing up those windows, better just redraw.
7874 */
7875 did_delete = FALSE;
7876#ifdef FEAT_WINDOWS
7877 if (wp->w_next != NULL || wp->w_status_height)
7878 {
7879 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7880 line_count, (int)Rows, FALSE, NULL) == OK)
7881 did_delete = TRUE;
7882 else if (wp->w_next)
7883 return FAIL;
7884 }
7885#endif
7886 /*
7887 * if no lines deleted, blank the lines that will end up below the window
7888 */
7889 if (!did_delete)
7890 {
7891#ifdef FEAT_WINDOWS
7892 wp->w_redr_status = TRUE;
7893#endif
7894 redraw_cmdline = TRUE;
7895 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7896 lastrow = nextrow + line_count;
7897 if (lastrow > Rows)
7898 lastrow = Rows;
7899 screen_fill(nextrow - line_count, lastrow - line_count,
7900 W_WINCOL(wp), (int)W_ENDCOL(wp),
7901 ' ', ' ', 0);
7902 }
7903
7904 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7905 == FAIL)
7906 {
7907 /* deletion will have messed up other windows */
7908 if (did_delete)
7909 {
7910#ifdef FEAT_WINDOWS
7911 wp->w_redr_status = TRUE;
7912#endif
7913 win_rest_invalid(W_NEXT(wp));
7914 }
7915 return FAIL;
7916 }
7917
7918 return OK;
7919}
7920
7921/*
7922 * delete "line_count" window lines at "row" in window "wp"
7923 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7924 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7925 * scrolling
7926 * Return OK for success, FAIL if the lines are not deleted.
7927 */
7928 int
7929win_del_lines(wp, row, line_count, invalid, mayclear)
7930 win_T *wp;
7931 int row;
7932 int line_count;
7933 int invalid;
7934 int mayclear;
7935{
7936 int retval;
7937
7938 if (invalid)
7939 wp->w_lines_valid = 0;
7940
7941 if (line_count > wp->w_height - row)
7942 line_count = wp->w_height - row;
7943
7944 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7945 if (retval != MAYBE)
7946 return retval;
7947
7948 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7949 (int)Rows, FALSE, NULL) == FAIL)
7950 return FAIL;
7951
7952#ifdef FEAT_WINDOWS
7953 /*
7954 * If there are windows or status lines below, try to put them at the
7955 * correct place. If we can't do that, they have to be redrawn.
7956 */
7957 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
7958 {
7959 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7960 line_count, (int)Rows, NULL) == FAIL)
7961 {
7962 wp->w_redr_status = TRUE;
7963 win_rest_invalid(wp->w_next);
7964 }
7965 }
7966 /*
7967 * If this is the last window and there is no status line, redraw the
7968 * command line later.
7969 */
7970 else
7971#endif
7972 redraw_cmdline = TRUE;
7973 return OK;
7974}
7975
7976/*
7977 * Common code for win_ins_lines() and win_del_lines().
7978 * Returns OK or FAIL when the work has been done.
7979 * Returns MAYBE when not finished yet.
7980 */
7981 static int
7982win_do_lines(wp, row, line_count, mayclear, del)
7983 win_T *wp;
7984 int row;
7985 int line_count;
7986 int mayclear;
7987 int del;
7988{
7989 int retval;
7990
7991 if (!redrawing() || line_count <= 0)
7992 return FAIL;
7993
7994 /* only a few lines left: redraw is faster */
7995 if (mayclear && Rows - line_count < 5
7996#ifdef FEAT_VERTSPLIT
7997 && wp->w_width == Columns
7998#endif
7999 )
8000 {
8001 screenclear(); /* will set wp->w_lines_valid to 0 */
8002 return FAIL;
8003 }
8004
8005 /*
8006 * Delete all remaining lines
8007 */
8008 if (row + line_count >= wp->w_height)
8009 {
8010 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8011 W_WINCOL(wp), (int)W_ENDCOL(wp),
8012 ' ', ' ', 0);
8013 return OK;
8014 }
8015
8016 /*
8017 * when scrolling, the message on the command line should be cleared,
8018 * otherwise it will stay there forever.
8019 */
8020 clear_cmdline = TRUE;
8021
8022 /*
8023 * If the terminal can set a scroll region, use that.
8024 * Always do this in a vertically split window. This will redraw from
8025 * ScreenLines[] when t_CV isn't defined. That's faster than using
8026 * win_line().
8027 * Don't use a scroll region when we are going to redraw the text, writing
8028 * a character in the lower right corner of the scroll region causes a
8029 * scroll-up in the DJGPP version.
8030 */
8031 if (scroll_region
8032#ifdef FEAT_VERTSPLIT
8033 || W_WIDTH(wp) != Columns
8034#endif
8035 )
8036 {
8037#ifdef FEAT_VERTSPLIT
8038 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8039#endif
8040 scroll_region_set(wp, row);
8041 if (del)
8042 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8043 wp->w_height - row, FALSE, wp);
8044 else
8045 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8046 wp->w_height - row, wp);
8047#ifdef FEAT_VERTSPLIT
8048 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8049#endif
8050 scroll_region_reset();
8051 return retval;
8052 }
8053
8054#ifdef FEAT_WINDOWS
8055 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8056 return FAIL;
8057#endif
8058
8059 return MAYBE;
8060}
8061
8062/*
8063 * window 'wp' and everything after it is messed up, mark it for redraw
8064 */
8065 static void
8066win_rest_invalid(wp)
8067 win_T *wp;
8068{
8069#ifdef FEAT_WINDOWS
8070 while (wp != NULL)
8071#else
8072 if (wp != NULL)
8073#endif
8074 {
8075 redraw_win_later(wp, NOT_VALID);
8076#ifdef FEAT_WINDOWS
8077 wp->w_redr_status = TRUE;
8078 wp = wp->w_next;
8079#endif
8080 }
8081 redraw_cmdline = TRUE;
8082}
8083
8084/*
8085 * The rest of the routines in this file perform screen manipulations. The
8086 * given operation is performed physically on the screen. The corresponding
8087 * change is also made to the internal screen image. In this way, the editor
8088 * anticipates the effect of editing changes on the appearance of the screen.
8089 * That way, when we call screenupdate a complete redraw isn't usually
8090 * necessary. Another advantage is that we can keep adding code to anticipate
8091 * screen changes, and in the meantime, everything still works.
8092 */
8093
8094/*
8095 * types for inserting or deleting lines
8096 */
8097#define USE_T_CAL 1
8098#define USE_T_CDL 2
8099#define USE_T_AL 3
8100#define USE_T_CE 4
8101#define USE_T_DL 5
8102#define USE_T_SR 6
8103#define USE_NL 7
8104#define USE_T_CD 8
8105#define USE_REDRAW 9
8106
8107/*
8108 * insert lines on the screen and update ScreenLines[]
8109 * 'end' is the line after the scrolled part. Normally it is Rows.
8110 * When scrolling region used 'off' is the offset from the top for the region.
8111 * 'row' and 'end' are relative to the start of the region.
8112 *
8113 * return FAIL for failure, OK for success.
8114 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008115 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116screen_ins_lines(off, row, line_count, end, wp)
8117 int off;
8118 int row;
8119 int line_count;
8120 int end;
8121 win_T *wp; /* NULL or window to use width from */
8122{
8123 int i;
8124 int j;
8125 unsigned temp;
8126 int cursor_row;
8127 int type;
8128 int result_empty;
8129 int can_ce = can_clear(T_CE);
8130
8131 /*
8132 * FAIL if
8133 * - there is no valid screen
8134 * - the screen has to be redrawn completely
8135 * - the line count is less than one
8136 * - the line count is more than 'ttyscroll'
8137 */
8138 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8139 return FAIL;
8140
8141 /*
8142 * There are seven ways to insert lines:
8143 * 0. When in a vertically split window and t_CV isn't set, redraw the
8144 * characters from ScreenLines[].
8145 * 1. Use T_CD (clear to end of display) if it exists and the result of
8146 * the insert is just empty lines
8147 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8148 * present or line_count > 1. It looks better if we do all the inserts
8149 * at once.
8150 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8151 * insert is just empty lines and T_CE is not present or line_count >
8152 * 1.
8153 * 4. Use T_AL (insert line) if it exists.
8154 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8155 * just empty lines.
8156 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8157 * just empty lines.
8158 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8159 * the 'da' flag is not set or we have clear line capability.
8160 * 8. redraw the characters from ScreenLines[].
8161 *
8162 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8163 * the scrollbar for the window. It does have insert line, use that if it
8164 * exists.
8165 */
8166 result_empty = (row + line_count >= end);
8167#ifdef FEAT_VERTSPLIT
8168 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8169 type = USE_REDRAW;
8170 else
8171#endif
8172 if (can_clear(T_CD) && result_empty)
8173 type = USE_T_CD;
8174 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8175 type = USE_T_CAL;
8176 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8177 type = USE_T_CDL;
8178 else if (*T_AL != NUL)
8179 type = USE_T_AL;
8180 else if (can_ce && result_empty)
8181 type = USE_T_CE;
8182 else if (*T_DL != NUL && result_empty)
8183 type = USE_T_DL;
8184 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8185 type = USE_T_SR;
8186 else
8187 return FAIL;
8188
8189 /*
8190 * For clearing the lines screen_del_lines() is used. This will also take
8191 * care of t_db if necessary.
8192 */
8193 if (type == USE_T_CD || type == USE_T_CDL ||
8194 type == USE_T_CE || type == USE_T_DL)
8195 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8196
8197 /*
8198 * If text is retained below the screen, first clear or delete as many
8199 * lines at the bottom of the window as are about to be inserted so that
8200 * the deleted lines won't later surface during a screen_del_lines.
8201 */
8202 if (*T_DB)
8203 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8204
8205#ifdef FEAT_CLIPBOARD
8206 /* Remove a modeless selection when inserting lines halfway the screen
8207 * or not the full width of the screen. */
8208 if (off + row > 0
8209# ifdef FEAT_VERTSPLIT
8210 || (wp != NULL && wp->w_width != Columns)
8211# endif
8212 )
8213 clip_clear_selection();
8214 else
8215 clip_scroll_selection(-line_count);
8216#endif
8217
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218#ifdef FEAT_GUI
8219 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8220 * scrolling is actually carried out. */
8221 gui_dont_update_cursor();
8222#endif
8223
8224 if (*T_CCS != NUL) /* cursor relative to region */
8225 cursor_row = row;
8226 else
8227 cursor_row = row + off;
8228
8229 /*
8230 * Shift LineOffset[] line_count down to reflect the inserted lines.
8231 * Clear the inserted lines in ScreenLines[].
8232 */
8233 row += off;
8234 end += off;
8235 for (i = 0; i < line_count; ++i)
8236 {
8237#ifdef FEAT_VERTSPLIT
8238 if (wp != NULL && wp->w_width != Columns)
8239 {
8240 /* need to copy part of a line */
8241 j = end - 1 - i;
8242 while ((j -= line_count) >= row)
8243 linecopy(j + line_count, j, wp);
8244 j += line_count;
8245 if (can_clear((char_u *)" "))
8246 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8247 else
8248 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8249 LineWraps[j] = FALSE;
8250 }
8251 else
8252#endif
8253 {
8254 j = end - 1 - i;
8255 temp = LineOffset[j];
8256 while ((j -= line_count) >= row)
8257 {
8258 LineOffset[j + line_count] = LineOffset[j];
8259 LineWraps[j + line_count] = LineWraps[j];
8260 }
8261 LineOffset[j + line_count] = temp;
8262 LineWraps[j + line_count] = FALSE;
8263 if (can_clear((char_u *)" "))
8264 lineclear(temp, (int)Columns);
8265 else
8266 lineinvalid(temp, (int)Columns);
8267 }
8268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269
8270 screen_stop_highlight();
8271 windgoto(cursor_row, 0);
8272
8273#ifdef FEAT_VERTSPLIT
8274 /* redraw the characters */
8275 if (type == USE_REDRAW)
8276 redraw_block(row, end, wp);
8277 else
8278#endif
8279 if (type == USE_T_CAL)
8280 {
8281 term_append_lines(line_count);
8282 screen_start(); /* don't know where cursor is now */
8283 }
8284 else
8285 {
8286 for (i = 0; i < line_count; i++)
8287 {
8288 if (type == USE_T_AL)
8289 {
8290 if (i && cursor_row != 0)
8291 windgoto(cursor_row, 0);
8292 out_str(T_AL);
8293 }
8294 else /* type == USE_T_SR */
8295 out_str(T_SR);
8296 screen_start(); /* don't know where cursor is now */
8297 }
8298 }
8299
8300 /*
8301 * With scroll-reverse and 'da' flag set we need to clear the lines that
8302 * have been scrolled down into the region.
8303 */
8304 if (type == USE_T_SR && *T_DA)
8305 {
8306 for (i = 0; i < line_count; ++i)
8307 {
8308 windgoto(off + i, 0);
8309 out_str(T_CE);
8310 screen_start(); /* don't know where cursor is now */
8311 }
8312 }
8313
8314#ifdef FEAT_GUI
8315 gui_can_update_cursor();
8316 if (gui.in_use)
8317 out_flush(); /* always flush after a scroll */
8318#endif
8319 return OK;
8320}
8321
8322/*
8323 * delete lines on the screen and update ScreenLines[]
8324 * 'end' is the line after the scrolled part. Normally it is Rows.
8325 * When scrolling region used 'off' is the offset from the top for the region.
8326 * 'row' and 'end' are relative to the start of the region.
8327 *
8328 * Return OK for success, FAIL if the lines are not deleted.
8329 */
8330/*ARGSUSED*/
8331 int
8332screen_del_lines(off, row, line_count, end, force, wp)
8333 int off;
8334 int row;
8335 int line_count;
8336 int end;
8337 int force; /* even when line_count > p_ttyscroll */
8338 win_T *wp; /* NULL or window to use width from */
8339{
8340 int j;
8341 int i;
8342 unsigned temp;
8343 int cursor_row;
8344 int cursor_end;
8345 int result_empty; /* result is empty until end of region */
8346 int can_delete; /* deleting line codes can be used */
8347 int type;
8348
8349 /*
8350 * FAIL if
8351 * - there is no valid screen
8352 * - the screen has to be redrawn completely
8353 * - the line count is less than one
8354 * - the line count is more than 'ttyscroll'
8355 */
8356 if (!screen_valid(TRUE) || line_count <= 0 ||
8357 (!force && line_count > p_ttyscroll))
8358 return FAIL;
8359
8360 /*
8361 * Check if the rest of the current region will become empty.
8362 */
8363 result_empty = row + line_count >= end;
8364
8365 /*
8366 * We can delete lines only when 'db' flag not set or when 'ce' option
8367 * available.
8368 */
8369 can_delete = (*T_DB == NUL || can_clear(T_CE));
8370
8371 /*
8372 * There are six ways to delete lines:
8373 * 0. When in a vertically split window and t_CV isn't set, redraw the
8374 * characters from ScreenLines[].
8375 * 1. Use T_CD if it exists and the result is empty.
8376 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8377 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8378 * none of the other ways work.
8379 * 4. Use T_CE (erase line) if the result is empty.
8380 * 5. Use T_DL (delete line) if it exists.
8381 * 6. redraw the characters from ScreenLines[].
8382 */
8383#ifdef FEAT_VERTSPLIT
8384 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8385 type = USE_REDRAW;
8386 else
8387#endif
8388 if (can_clear(T_CD) && result_empty)
8389 type = USE_T_CD;
8390#if defined(__BEOS__) && defined(BEOS_DR8)
8391 /*
8392 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8393 * its internal termcap... this works okay for tests which test *T_DB !=
8394 * NUL. It has the disadvantage that the user cannot use any :set t_*
8395 * command to get T_DB (back) to empty_option, only :set term=... will do
8396 * the trick...
8397 * Anyway, this hack will hopefully go away with the next OS release.
8398 * (Olaf Seibert)
8399 */
8400 else if (row == 0 && T_DB == empty_option
8401 && (line_count == 1 || *T_CDL == NUL))
8402#else
8403 else if (row == 0 && (
8404#ifndef AMIGA
8405 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8406 * up, so use delete-line command */
8407 line_count == 1 ||
8408#endif
8409 *T_CDL == NUL))
8410#endif
8411 type = USE_NL;
8412 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8413 type = USE_T_CDL;
8414 else if (can_clear(T_CE) && result_empty
8415#ifdef FEAT_VERTSPLIT
8416 && (wp == NULL || wp->w_width == Columns)
8417#endif
8418 )
8419 type = USE_T_CE;
8420 else if (*T_DL != NUL && can_delete)
8421 type = USE_T_DL;
8422 else if (*T_CDL != NUL && can_delete)
8423 type = USE_T_CDL;
8424 else
8425 return FAIL;
8426
8427#ifdef FEAT_CLIPBOARD
8428 /* Remove a modeless selection when deleting lines halfway the screen or
8429 * not the full width of the screen. */
8430 if (off + row > 0
8431# ifdef FEAT_VERTSPLIT
8432 || (wp != NULL && wp->w_width != Columns)
8433# endif
8434 )
8435 clip_clear_selection();
8436 else
8437 clip_scroll_selection(line_count);
8438#endif
8439
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440#ifdef FEAT_GUI
8441 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8442 * scrolling is actually carried out. */
8443 gui_dont_update_cursor();
8444#endif
8445
8446 if (*T_CCS != NUL) /* cursor relative to region */
8447 {
8448 cursor_row = row;
8449 cursor_end = end;
8450 }
8451 else
8452 {
8453 cursor_row = row + off;
8454 cursor_end = end + off;
8455 }
8456
8457 /*
8458 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8459 * Clear the inserted lines in ScreenLines[].
8460 */
8461 row += off;
8462 end += off;
8463 for (i = 0; i < line_count; ++i)
8464 {
8465#ifdef FEAT_VERTSPLIT
8466 if (wp != NULL && wp->w_width != Columns)
8467 {
8468 /* need to copy part of a line */
8469 j = row + i;
8470 while ((j += line_count) <= end - 1)
8471 linecopy(j - line_count, j, wp);
8472 j -= line_count;
8473 if (can_clear((char_u *)" "))
8474 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8475 else
8476 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8477 LineWraps[j] = FALSE;
8478 }
8479 else
8480#endif
8481 {
8482 /* whole width, moving the line pointers is faster */
8483 j = row + i;
8484 temp = LineOffset[j];
8485 while ((j += line_count) <= end - 1)
8486 {
8487 LineOffset[j - line_count] = LineOffset[j];
8488 LineWraps[j - line_count] = LineWraps[j];
8489 }
8490 LineOffset[j - line_count] = temp;
8491 LineWraps[j - line_count] = FALSE;
8492 if (can_clear((char_u *)" "))
8493 lineclear(temp, (int)Columns);
8494 else
8495 lineinvalid(temp, (int)Columns);
8496 }
8497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498
8499 screen_stop_highlight();
8500
8501#ifdef FEAT_VERTSPLIT
8502 /* redraw the characters */
8503 if (type == USE_REDRAW)
8504 redraw_block(row, end, wp);
8505 else
8506#endif
8507 if (type == USE_T_CD) /* delete the lines */
8508 {
8509 windgoto(cursor_row, 0);
8510 out_str(T_CD);
8511 screen_start(); /* don't know where cursor is now */
8512 }
8513 else if (type == USE_T_CDL)
8514 {
8515 windgoto(cursor_row, 0);
8516 term_delete_lines(line_count);
8517 screen_start(); /* don't know where cursor is now */
8518 }
8519 /*
8520 * Deleting lines at top of the screen or scroll region: Just scroll
8521 * the whole screen (scroll region) up by outputting newlines on the
8522 * last line.
8523 */
8524 else if (type == USE_NL)
8525 {
8526 windgoto(cursor_end - 1, 0);
8527 for (i = line_count; --i >= 0; )
8528 out_char('\n'); /* cursor will remain on same line */
8529 }
8530 else
8531 {
8532 for (i = line_count; --i >= 0; )
8533 {
8534 if (type == USE_T_DL)
8535 {
8536 windgoto(cursor_row, 0);
8537 out_str(T_DL); /* delete a line */
8538 }
8539 else /* type == USE_T_CE */
8540 {
8541 windgoto(cursor_row + i, 0);
8542 out_str(T_CE); /* erase a line */
8543 }
8544 screen_start(); /* don't know where cursor is now */
8545 }
8546 }
8547
8548 /*
8549 * If the 'db' flag is set, we need to clear the lines that have been
8550 * scrolled up at the bottom of the region.
8551 */
8552 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8553 {
8554 for (i = line_count; i > 0; --i)
8555 {
8556 windgoto(cursor_end - i, 0);
8557 out_str(T_CE); /* erase a line */
8558 screen_start(); /* don't know where cursor is now */
8559 }
8560 }
8561
8562#ifdef FEAT_GUI
8563 gui_can_update_cursor();
8564 if (gui.in_use)
8565 out_flush(); /* always flush after a scroll */
8566#endif
8567
8568 return OK;
8569}
8570
8571/*
8572 * show the current mode and ruler
8573 *
8574 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8575 * If clear_cmdline is FALSE there may be a message there that needs to be
8576 * cleared only if a mode is shown.
8577 * Return the length of the message (0 if no message).
8578 */
8579 int
8580showmode()
8581{
8582 int need_clear;
8583 int length = 0;
8584 int do_mode;
8585 int attr;
8586 int nwr_save;
8587#ifdef FEAT_INS_EXPAND
8588 int sub_attr;
8589#endif
8590
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008591 do_mode = ((p_smd && msg_silent == 0)
8592 && ((State & INSERT)
8593 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594#ifdef FEAT_VISUAL
8595 || VIsual_active
8596#endif
8597 ));
8598 if (do_mode || Recording)
8599 {
8600 /*
8601 * Don't show mode right now, when not redrawing or inside a mapping.
8602 * Call char_avail() only when we are going to show something, because
8603 * it takes a bit of time.
8604 */
8605 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8606 {
8607 redraw_cmdline = TRUE; /* show mode later */
8608 return 0;
8609 }
8610
8611 nwr_save = need_wait_return;
8612
8613 /* wait a bit before overwriting an important message */
8614 check_for_delay(FALSE);
8615
8616 /* if the cmdline is more than one line high, erase top lines */
8617 need_clear = clear_cmdline;
8618 if (clear_cmdline && cmdline_row < Rows - 1)
8619 msg_clr_cmdline(); /* will reset clear_cmdline */
8620
8621 /* Position on the last line in the window, column 0 */
8622 msg_pos_mode();
8623 cursor_off();
8624 attr = hl_attr(HLF_CM); /* Highlight mode */
8625 if (do_mode)
8626 {
8627 MSG_PUTS_ATTR("--", attr);
8628#if defined(FEAT_XIM)
8629 if (xic != NULL && im_get_status() && !p_imdisable
8630 && curbuf->b_p_iminsert == B_IMODE_IM)
8631# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8632 MSG_PUTS_ATTR(" IM", attr);
8633# else
8634 MSG_PUTS_ATTR(" XIM", attr);
8635# endif
8636#endif
8637#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8638 if (gui.in_use)
8639 {
8640 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008641 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 }
8643#endif
8644#ifdef FEAT_INS_EXPAND
8645 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8646 {
8647 /* These messages can get long, avoid a wrap in a narrow
8648 * window. Prefer showing edit_submode_extra. */
8649 length = (Rows - msg_row) * Columns - 3;
8650 if (edit_submode_extra != NULL)
8651 length -= vim_strsize(edit_submode_extra);
8652 if (length > 0)
8653 {
8654 if (edit_submode_pre != NULL)
8655 length -= vim_strsize(edit_submode_pre);
8656 if (length - vim_strsize(edit_submode) > 0)
8657 {
8658 if (edit_submode_pre != NULL)
8659 msg_puts_attr(edit_submode_pre, attr);
8660 msg_puts_attr(edit_submode, attr);
8661 }
8662 if (edit_submode_extra != NULL)
8663 {
8664 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8665 if ((int)edit_submode_highl < (int)HLF_COUNT)
8666 sub_attr = hl_attr(edit_submode_highl);
8667 else
8668 sub_attr = attr;
8669 msg_puts_attr(edit_submode_extra, sub_attr);
8670 }
8671 }
8672 length = 0;
8673 }
8674 else
8675#endif
8676 {
8677#ifdef FEAT_VREPLACE
8678 if (State & VREPLACE_FLAG)
8679 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8680 else
8681#endif
8682 if (State & REPLACE_FLAG)
8683 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8684 else if (State & INSERT)
8685 {
8686#ifdef FEAT_RIGHTLEFT
8687 if (p_ri)
8688 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8689#endif
8690 MSG_PUTS_ATTR(_(" INSERT"), attr);
8691 }
8692 else if (restart_edit == 'I')
8693 MSG_PUTS_ATTR(_(" (insert)"), attr);
8694 else if (restart_edit == 'R')
8695 MSG_PUTS_ATTR(_(" (replace)"), attr);
8696 else if (restart_edit == 'V')
8697 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8698#ifdef FEAT_RIGHTLEFT
8699 if (p_hkmap)
8700 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8701# ifdef FEAT_FKMAP
8702 if (p_fkmap)
8703 MSG_PUTS_ATTR(farsi_text_5, attr);
8704# endif
8705#endif
8706#ifdef FEAT_KEYMAP
8707 if (State & LANGMAP)
8708 {
8709# ifdef FEAT_ARABIC
8710 if (curwin->w_p_arab)
8711 MSG_PUTS_ATTR(_(" Arabic"), attr);
8712 else
8713# endif
8714 MSG_PUTS_ATTR(_(" (lang)"), attr);
8715 }
8716#endif
8717 if ((State & INSERT) && p_paste)
8718 MSG_PUTS_ATTR(_(" (paste)"), attr);
8719
8720#ifdef FEAT_VISUAL
8721 if (VIsual_active)
8722 {
8723 char *p;
8724
8725 /* Don't concatenate separate words to avoid translation
8726 * problems. */
8727 switch ((VIsual_select ? 4 : 0)
8728 + (VIsual_mode == Ctrl_V) * 2
8729 + (VIsual_mode == 'V'))
8730 {
8731 case 0: p = N_(" VISUAL"); break;
8732 case 1: p = N_(" VISUAL LINE"); break;
8733 case 2: p = N_(" VISUAL BLOCK"); break;
8734 case 4: p = N_(" SELECT"); break;
8735 case 5: p = N_(" SELECT LINE"); break;
8736 default: p = N_(" SELECT BLOCK"); break;
8737 }
8738 MSG_PUTS_ATTR(_(p), attr);
8739 }
8740#endif
8741 MSG_PUTS_ATTR(" --", attr);
8742 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008743
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 need_clear = TRUE;
8745 }
8746 if (Recording
8747#ifdef FEAT_INS_EXPAND
8748 && edit_submode == NULL /* otherwise it gets too long */
8749#endif
8750 )
8751 {
8752 MSG_PUTS_ATTR(_("recording"), attr);
8753 need_clear = TRUE;
8754 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008755
8756 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 if (need_clear || clear_cmdline)
8758 msg_clr_eos();
8759 msg_didout = FALSE; /* overwrite this message */
8760 length = msg_col;
8761 msg_col = 0;
8762 need_wait_return = nwr_save; /* never ask for hit-return for this */
8763 }
8764 else if (clear_cmdline && msg_silent == 0)
8765 /* Clear the whole command line. Will reset "clear_cmdline". */
8766 msg_clr_cmdline();
8767
8768#ifdef FEAT_CMDL_INFO
8769# ifdef FEAT_VISUAL
8770 /* In Visual mode the size of the selected area must be redrawn. */
8771 if (VIsual_active)
8772 clear_showcmd();
8773# endif
8774
8775 /* If the last window has no status line, the ruler is after the mode
8776 * message and must be redrawn */
8777 if (redrawing()
8778# ifdef FEAT_WINDOWS
8779 && lastwin->w_status_height == 0
8780# endif
8781 )
8782 win_redr_ruler(lastwin, TRUE);
8783#endif
8784 redraw_cmdline = FALSE;
8785 clear_cmdline = FALSE;
8786
8787 return length;
8788}
8789
8790/*
8791 * Position for a mode message.
8792 */
8793 static void
8794msg_pos_mode()
8795{
8796 msg_col = 0;
8797 msg_row = Rows - 1;
8798}
8799
8800/*
8801 * Delete mode message. Used when ESC is typed which is expected to end
8802 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008803 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 */
8805 void
8806unshowmode(force)
8807 int force;
8808{
8809 /*
8810 * Don't delete it right now, when not redrawing or insided a mapping.
8811 */
8812 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8813 redraw_cmdline = TRUE; /* delete mode later */
8814 else
8815 {
8816 msg_pos_mode();
8817 if (Recording)
8818 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8819 msg_clr_eos();
8820 }
8821}
8822
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008823#if defined(FEAT_WINDOWS)
8824/*
8825 * Draw the tab pages line at the top of the Vim window.
8826 */
8827 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008828draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008829{
8830 int tabcount = 0;
8831 tabpage_T *tp;
8832 int tabwidth;
8833 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008834 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008835 int attr;
8836 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008837 win_T *cwp;
8838 int wincount;
8839 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008840 int c;
8841 int len;
8842 int attr_sel = hl_attr(HLF_TPS);
8843 int attr_nosel = hl_attr(HLF_TP);
8844 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008845 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008846 int room;
8847 int use_sep_chars = (t_colors < 8
8848#ifdef FEAT_GUI
8849 && !gui.in_use
8850#endif
8851 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008852
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008853 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008854
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008855#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00008856 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008857 if (gui_use_tabline())
8858 {
8859 gui_update_tabline();
8860 return;
8861 }
8862#endif
8863
8864 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008865 return;
8866
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008867#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008868
8869 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
8870 for (scol = 0; scol < Columns; ++scol)
8871 TabPageIdxs[scol] = 0;
8872
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008873 /* Use the 'tabline' option if it's set. */
8874 if (*p_tal != NUL)
8875 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008876 int save_called_emsg = called_emsg;
8877
8878 /* Check for an error. If there is one we would loop in redrawing the
8879 * screen. Avoid that by making 'tabline' empty. */
8880 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008881 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008882 if (called_emsg)
8883 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008884 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008885 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008886 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00008887 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008888#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008889 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008890 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
8891 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008892
Bram Moolenaar238a5642006-02-21 22:12:05 +00008893 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
8894 if (tabwidth < 6)
8895 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008896
Bram Moolenaar238a5642006-02-21 22:12:05 +00008897 attr = attr_nosel;
8898 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008899 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008900 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
8901 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008902 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008903 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008904
Bram Moolenaar238a5642006-02-21 22:12:05 +00008905 if (tp->tp_topframe == topframe)
8906 attr = attr_sel;
8907 if (use_sep_chars && col > 0)
8908 screen_putchar('|', 0, col++, attr);
8909
8910 if (tp->tp_topframe != topframe)
8911 attr = attr_nosel;
8912
8913 screen_putchar(' ', 0, col++, attr);
8914
8915 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008916 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008917 cwp = curwin;
8918 wp = firstwin;
8919 }
8920 else
8921 {
8922 cwp = tp->tp_curwin;
8923 wp = tp->tp_firstwin;
8924 }
8925
8926 modified = FALSE;
8927 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
8928 if (bufIsChanged(wp->w_buffer))
8929 modified = TRUE;
8930 if (modified || wincount > 1)
8931 {
8932 if (wincount > 1)
8933 {
8934 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008935 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008936 if (col + len >= Columns - 3)
8937 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008938 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008939#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00008940 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008941#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00008942 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008943#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00008944 );
8945 col += len;
8946 }
8947 if (modified)
8948 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
8949 screen_putchar(' ', 0, col++, attr);
8950 }
8951
8952 room = scol - col + tabwidth - 1;
8953 if (room > 0)
8954 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008955 /* Get buffer name in NameBuff[] */
8956 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008957 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008958 len = vim_strsize(NameBuff);
8959 p = NameBuff;
8960#ifdef FEAT_MBYTE
8961 if (has_mbyte)
8962 while (len > room)
8963 {
8964 len -= ptr2cells(p);
8965 mb_ptr_adv(p);
8966 }
8967 else
8968#endif
8969 if (len > room)
8970 {
8971 p += len - room;
8972 len = room;
8973 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008974 if (len > Columns - col - 1)
8975 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008976
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008977 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008978 col += len;
8979 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00008980 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008981
8982 /* Store the tab page number in TabPageIdxs[], so that
8983 * jump_to_mouse() knows where each one is. */
8984 ++tabcount;
8985 while (scol < col)
8986 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008987 }
8988
Bram Moolenaar238a5642006-02-21 22:12:05 +00008989 if (use_sep_chars)
8990 c = '_';
8991 else
8992 c = ' ';
8993 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008994
8995 /* Put an "X" for closing the current tab if there are several. */
8996 if (first_tabpage->tp_next != NULL)
8997 {
8998 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
8999 TabPageIdxs[Columns - 1] = -999;
9000 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009001 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009002
9003 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9004 * set. */
9005 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009006}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009007
9008/*
9009 * Get buffer name for "buf" into NameBuff[].
9010 * Takes care of special buffer names and translates special characters.
9011 */
9012 void
9013get_trans_bufname(buf)
9014 buf_T *buf;
9015{
9016 if (buf_spname(buf) != NULL)
9017 STRCPY(NameBuff, buf_spname(buf));
9018 else
9019 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9020 trans_characters(NameBuff, MAXPATHL);
9021}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009022#endif
9023
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9025/*
9026 * Get the character to use in a status line. Get its attributes in "*attr".
9027 */
9028 static int
9029fillchar_status(attr, is_curwin)
9030 int *attr;
9031 int is_curwin;
9032{
9033 int fill;
9034 if (is_curwin)
9035 {
9036 *attr = hl_attr(HLF_S);
9037 fill = fill_stl;
9038 }
9039 else
9040 {
9041 *attr = hl_attr(HLF_SNC);
9042 fill = fill_stlnc;
9043 }
9044 /* Use fill when there is highlighting, and highlighting of current
9045 * window differs, or the fillchars differ, or this is not the
9046 * current window */
9047 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9048 || !is_curwin || firstwin == lastwin)
9049 || (fill_stl != fill_stlnc)))
9050 return fill;
9051 if (is_curwin)
9052 return '^';
9053 return '=';
9054}
9055#endif
9056
9057#ifdef FEAT_VERTSPLIT
9058/*
9059 * Get the character to use in a separator between vertically split windows.
9060 * Get its attributes in "*attr".
9061 */
9062 static int
9063fillchar_vsep(attr)
9064 int *attr;
9065{
9066 *attr = hl_attr(HLF_C);
9067 if (*attr == 0 && fill_vert == ' ')
9068 return '|';
9069 else
9070 return fill_vert;
9071}
9072#endif
9073
9074/*
9075 * Return TRUE if redrawing should currently be done.
9076 */
9077 int
9078redrawing()
9079{
9080 return (!RedrawingDisabled
9081 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9082}
9083
9084/*
9085 * Return TRUE if printing messages should currently be done.
9086 */
9087 int
9088messaging()
9089{
9090 return (!(p_lz && char_avail() && !KeyTyped));
9091}
9092
9093/*
9094 * Show current status info in ruler and various other places
9095 * If always is FALSE, only show ruler if position has changed.
9096 */
9097 void
9098showruler(always)
9099 int always;
9100{
9101 if (!always && !redrawing())
9102 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009103#ifdef FEAT_INS_EXPAND
9104 if (pum_visible())
9105 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009106# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009107 /* Don't redraw right now, do it later. */
9108 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009109# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009110 return;
9111 }
9112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009114 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009115 {
9116 redraw_custum_statusline(curwin);
9117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 else
9119#endif
9120#ifdef FEAT_CMDL_INFO
9121 win_redr_ruler(curwin, always);
9122#endif
9123
9124#ifdef FEAT_TITLE
9125 if (need_maketitle
9126# ifdef FEAT_STL_OPT
9127 || (p_icon && (stl_syntax & STL_IN_ICON))
9128 || (p_title && (stl_syntax & STL_IN_TITLE))
9129# endif
9130 )
9131 maketitle();
9132#endif
9133}
9134
9135#ifdef FEAT_CMDL_INFO
9136 static void
9137win_redr_ruler(wp, always)
9138 win_T *wp;
9139 int always;
9140{
9141 char_u buffer[70];
9142 int row;
9143 int fillchar;
9144 int attr;
9145 int empty_line = FALSE;
9146 colnr_T virtcol;
9147 int i;
9148 int o;
9149#ifdef FEAT_VERTSPLIT
9150 int this_ru_col;
9151 int off = 0;
9152 int width = Columns;
9153# define WITH_OFF(x) x
9154# define WITH_WIDTH(x) x
9155#else
9156# define WITH_OFF(x) 0
9157# define WITH_WIDTH(x) Columns
9158# define this_ru_col ru_col
9159#endif
9160
9161 /* If 'ruler' off or redrawing disabled, don't do anything */
9162 if (!p_ru)
9163 return;
9164
9165 /*
9166 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9167 * after deleting lines, before cursor.lnum is corrected.
9168 */
9169 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9170 return;
9171
9172#ifdef FEAT_INS_EXPAND
9173 /* Don't draw the ruler while doing insert-completion, it might overwrite
9174 * the (long) mode message. */
9175# ifdef FEAT_WINDOWS
9176 if (wp == lastwin && lastwin->w_status_height == 0)
9177# endif
9178 if (edit_submode != NULL)
9179 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009180 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9181 if (pum_visible())
9182 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183#endif
9184
9185#ifdef FEAT_STL_OPT
9186 if (*p_ruf)
9187 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009188 int save_called_emsg = called_emsg;
9189
9190 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009192 if (called_emsg)
9193 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009194 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009195 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 return;
9197 }
9198#endif
9199
9200 /*
9201 * Check if not in Insert mode and the line is empty (will show "0-1").
9202 */
9203 if (!(State & INSERT)
9204 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9205 empty_line = TRUE;
9206
9207 /*
9208 * Only draw the ruler when something changed.
9209 */
9210 validate_virtcol_win(wp);
9211 if ( redraw_cmdline
9212 || always
9213 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9214 || wp->w_cursor.col != wp->w_ru_cursor.col
9215 || wp->w_virtcol != wp->w_ru_virtcol
9216#ifdef FEAT_VIRTUALEDIT
9217 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9218#endif
9219 || wp->w_topline != wp->w_ru_topline
9220 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9221#ifdef FEAT_DIFF
9222 || wp->w_topfill != wp->w_ru_topfill
9223#endif
9224 || empty_line != wp->w_ru_empty)
9225 {
9226 cursor_off();
9227#ifdef FEAT_WINDOWS
9228 if (wp->w_status_height)
9229 {
9230 row = W_WINROW(wp) + wp->w_height;
9231 fillchar = fillchar_status(&attr, wp == curwin);
9232# ifdef FEAT_VERTSPLIT
9233 off = W_WINCOL(wp);
9234 width = W_WIDTH(wp);
9235# endif
9236 }
9237 else
9238#endif
9239 {
9240 row = Rows - 1;
9241 fillchar = ' ';
9242 attr = 0;
9243#ifdef FEAT_VERTSPLIT
9244 width = Columns;
9245 off = 0;
9246#endif
9247 }
9248
9249 /* In list mode virtcol needs to be recomputed */
9250 virtcol = wp->w_virtcol;
9251 if (wp->w_p_list && lcs_tab1 == NUL)
9252 {
9253 wp->w_p_list = FALSE;
9254 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9255 wp->w_p_list = TRUE;
9256 }
9257
9258 /*
9259 * Some sprintfs return the length, some return a pointer.
9260 * To avoid portability problems we use strlen() here.
9261 */
9262 sprintf((char *)buffer, "%ld,",
9263 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9264 ? 0L
9265 : (long)(wp->w_cursor.lnum));
9266 col_print(buffer + STRLEN(buffer),
9267 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9268 (int)virtcol + 1);
9269
9270 /*
9271 * Add a "50%" if there is room for it.
9272 * On the last line, don't print in the last column (scrolls the
9273 * screen up on some terminals).
9274 */
9275 i = (int)STRLEN(buffer);
9276 get_rel_pos(wp, buffer + i + 1);
9277 o = i + vim_strsize(buffer + i + 1);
9278#ifdef FEAT_WINDOWS
9279 if (wp->w_status_height == 0) /* can't use last char of screen */
9280#endif
9281 ++o;
9282#ifdef FEAT_VERTSPLIT
9283 this_ru_col = ru_col - (Columns - width);
9284 if (this_ru_col < 0)
9285 this_ru_col = 0;
9286#endif
9287 /* Never use more than half the window/screen width, leave the other
9288 * half for the filename. */
9289 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9290 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9291 if (this_ru_col + o < WITH_WIDTH(width))
9292 {
9293 while (this_ru_col + o < WITH_WIDTH(width))
9294 {
9295#ifdef FEAT_MBYTE
9296 if (has_mbyte)
9297 i += (*mb_char2bytes)(fillchar, buffer + i);
9298 else
9299#endif
9300 buffer[i++] = fillchar;
9301 ++o;
9302 }
9303 get_rel_pos(wp, buffer + i);
9304 }
9305 /* Truncate at window boundary. */
9306#ifdef FEAT_MBYTE
9307 if (has_mbyte)
9308 {
9309 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009310 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 {
9312 o += (*mb_ptr2cells)(buffer + i);
9313 if (this_ru_col + o > WITH_WIDTH(width))
9314 {
9315 buffer[i] = NUL;
9316 break;
9317 }
9318 }
9319 }
9320 else
9321#endif
9322 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9323 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9324
9325 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9326 i = redraw_cmdline;
9327 screen_fill(row, row + 1,
9328 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9329 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9330 fillchar, fillchar, attr);
9331 /* don't redraw the cmdline because of showing the ruler */
9332 redraw_cmdline = i;
9333 wp->w_ru_cursor = wp->w_cursor;
9334 wp->w_ru_virtcol = wp->w_virtcol;
9335 wp->w_ru_empty = empty_line;
9336 wp->w_ru_topline = wp->w_topline;
9337 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9338#ifdef FEAT_DIFF
9339 wp->w_ru_topfill = wp->w_topfill;
9340#endif
9341 }
9342}
9343#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009344
9345#if defined(FEAT_LINEBREAK) || defined(PROTO)
9346/*
9347 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009348 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009349 * Otherwise it depends on 'numberwidth' and the line count.
9350 */
9351 int
9352number_width(wp)
9353 win_T *wp;
9354{
9355 int n;
9356 linenr_T lnum;
9357
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009358 lnum = wp->w_buffer->b_ml.ml_line_count;
9359 if (lnum == wp->w_nrwidth_line_count)
9360 return wp->w_nrwidth_width;
9361 wp->w_nrwidth_line_count = lnum;
9362
9363 n = 0;
9364 do
9365 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009366 lnum /= 10;
9367 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009368 } while (lnum > 0);
9369
9370 /* 'numberwidth' gives the minimal width plus one */
9371 if (n < wp->w_p_nuw - 1)
9372 n = wp->w_p_nuw - 1;
9373
9374 wp->w_nrwidth_width = n;
9375 return n;
9376}
9377#endif