blob: c293de1158095c85f6a840cf056ebf21b70aea25 [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
466#ifdef FEAT_SYN_HL
467 /*
468 * Correct stored syntax highlighting info for changes in each displayed
469 * buffer. Each buffer must only be done once.
470 */
471 FOR_ALL_WINDOWS(wp)
472 {
473 if (wp->w_buffer->b_mod_set)
474 {
475# ifdef FEAT_WINDOWS
476 win_T *wwp;
477
478 /* Check if we already did this buffer. */
479 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
480 if (wwp->w_buffer == wp->w_buffer)
481 break;
482# endif
483 if (
484# ifdef FEAT_WINDOWS
485 wwp == wp &&
486# endif
487 syntax_present(wp->w_buffer))
488 syn_stack_apply_changes(wp->w_buffer);
489 }
490 }
491#endif
492
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000493#ifdef FEAT_WINDOWS
494 /* Redraw the tab pages line if needed. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000495 if (redraw_tabline || type >= NOT_VALID)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000496 draw_tabline();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000497#endif
498
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 /*
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
2629
2630 /* draw_state: items that are drawn in sequence: */
2631#define WL_START 0 /* nothing done yet */
2632#ifdef FEAT_CMDWIN
2633# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2634#else
2635# define WL_CMDLINE WL_START
2636#endif
2637#ifdef FEAT_FOLDING
2638# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2639#else
2640# define WL_FOLD WL_CMDLINE
2641#endif
2642#ifdef FEAT_SIGNS
2643# define WL_SIGN WL_FOLD + 1 /* column for signs */
2644#else
2645# define WL_SIGN WL_FOLD /* column for signs */
2646#endif
2647#define WL_NR WL_SIGN + 1 /* line number */
2648#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2649# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2650#else
2651# define WL_SBR WL_NR
2652#endif
2653#define WL_LINE WL_SBR + 1 /* text in the line */
2654 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002655#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 int feedback_col = 0;
2657 int feedback_old_attr = -1;
2658#endif
2659
2660
2661 if (startrow > endrow) /* past the end already! */
2662 return startrow;
2663
2664 row = startrow;
2665 screen_row = row + W_WINROW(wp);
2666
2667 /*
2668 * To speed up the loop below, set extra_check when there is linebreak,
2669 * trailing white space and/or syntax processing to be done.
2670 */
2671#ifdef FEAT_LINEBREAK
2672 extra_check = wp->w_p_lbr;
2673#else
2674 extra_check = 0;
2675#endif
2676#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002677 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
2679 /* Prepare for syntax highlighting in this line. When there is an
2680 * error, stop syntax highlighting. */
2681 save_did_emsg = did_emsg;
2682 did_emsg = FALSE;
2683 syntax_start(wp, lnum);
2684 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002685 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 else
2687 {
2688 did_emsg = save_did_emsg;
2689 has_syntax = TRUE;
2690 extra_check = TRUE;
2691 }
2692 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002693#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002694
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002695#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002696 if (wp->w_p_spell
2697 && *wp->w_buffer->b_p_spl != NUL
2698 && wp->w_buffer->b_langp.ga_len > 0
2699 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002700 {
2701 /* Prepare for spell checking. */
2702 has_spell = TRUE;
2703 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002704
2705 /* Get the start of the next line, so that words that wrap to the next
2706 * line are found too: "et<line-break>al.".
2707 * Trick: skip a few chars for C/shell/Vim comments */
2708 nextline[SPWORDLEN] = NUL;
2709 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2710 {
2711 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2712 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2713 }
2714
2715 /* When a word wrapped from the previous line the start of the current
2716 * line is valid. */
2717 if (lnum == checked_lnum)
2718 cur_checked_col = checked_col;
2719 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002720
2721 /* When there was a sentence end in the previous line may require a
2722 * word starting with capital in this line. In line 1 always check
2723 * the first word. */
2724 if (lnum != capcol_lnum)
2725 cap_col = -1;
2726 if (lnum == 1)
2727 cap_col = 0;
2728 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730#endif
2731
2732 /*
2733 * handle visual active in this window
2734 */
2735 fromcol = -10;
2736 tocol = MAXCOL;
2737#ifdef FEAT_VISUAL
2738 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2739 {
2740 /* Visual is after curwin->w_cursor */
2741 if (ltoreq(curwin->w_cursor, VIsual))
2742 {
2743 top = &curwin->w_cursor;
2744 bot = &VIsual;
2745 }
2746 else /* Visual is before curwin->w_cursor */
2747 {
2748 top = &VIsual;
2749 bot = &curwin->w_cursor;
2750 }
2751 if (VIsual_mode == Ctrl_V) /* block mode */
2752 {
2753 if (lnum >= top->lnum && lnum <= bot->lnum)
2754 {
2755 fromcol = wp->w_old_cursor_fcol;
2756 tocol = wp->w_old_cursor_lcol;
2757 }
2758 }
2759 else /* non-block mode */
2760 {
2761 if (lnum > top->lnum && lnum <= bot->lnum)
2762 fromcol = 0;
2763 else if (lnum == top->lnum)
2764 {
2765 if (VIsual_mode == 'V') /* linewise */
2766 fromcol = 0;
2767 else
2768 {
2769 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2770 if (gchar_pos(top) == NUL)
2771 tocol = fromcol + 1;
2772 }
2773 }
2774 if (VIsual_mode != 'V' && lnum == bot->lnum)
2775 {
2776 if (*p_sel == 'e' && bot->col == 0
2777#ifdef FEAT_VIRTUALEDIT
2778 && bot->coladd == 0
2779#endif
2780 )
2781 {
2782 fromcol = -10;
2783 tocol = MAXCOL;
2784 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002785 else if (bot->col == MAXCOL)
2786 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 else
2788 {
2789 pos = *bot;
2790 if (*p_sel == 'e')
2791 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2792 else
2793 {
2794 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2795 ++tocol;
2796 }
2797 }
2798 }
2799 }
2800
2801#ifndef MSDOS
2802 /* Check if the character under the cursor should not be inverted */
2803 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2804# ifdef FEAT_GUI
2805 && !gui.in_use
2806# endif
2807 )
2808 noinvcur = TRUE;
2809#endif
2810
2811 /* if inverting in this line set area_highlighting */
2812 if (fromcol >= 0)
2813 {
2814 area_highlighting = TRUE;
2815 attr = hl_attr(HLF_V);
2816#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2817 if (clip_star.available && !clip_star.owned && clip_isautosel())
2818 attr = hl_attr(HLF_VNC);
2819#endif
2820 }
2821 }
2822
2823 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002824 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 */
2826 else
2827#endif /* FEAT_VISUAL */
2828 if (highlight_match
2829 && wp == curwin
2830 && lnum >= curwin->w_cursor.lnum
2831 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2832 {
2833 if (lnum == curwin->w_cursor.lnum)
2834 getvcol(curwin, &(curwin->w_cursor),
2835 (colnr_T *)&fromcol, NULL, NULL);
2836 else
2837 fromcol = 0;
2838 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2839 {
2840 pos.lnum = lnum;
2841 pos.col = search_match_endcol;
2842 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2843 }
2844 else
2845 tocol = MAXCOL;
2846 if (fromcol == tocol) /* do at least one character */
2847 tocol = fromcol + 1; /* happens when past end of line */
2848 area_highlighting = TRUE;
2849 attr = hl_attr(HLF_I);
2850 }
2851
2852#ifdef FEAT_DIFF
2853 filler_lines = diff_check(wp, lnum);
2854 if (filler_lines < 0)
2855 {
2856 if (filler_lines == -1)
2857 {
2858 if (diff_find_change(wp, lnum, &change_start, &change_end))
2859 diff_hlf = HLF_ADD; /* added line */
2860 else if (change_start == 0)
2861 diff_hlf = HLF_TXD; /* changed text */
2862 else
2863 diff_hlf = HLF_CHD; /* changed line */
2864 }
2865 else
2866 diff_hlf = HLF_ADD; /* added line */
2867 filler_lines = 0;
2868 area_highlighting = TRUE;
2869 }
2870 if (lnum == wp->w_topline)
2871 filler_lines = wp->w_topfill;
2872 filler_todo = filler_lines;
2873#endif
2874
2875#ifdef LINE_ATTR
2876# ifdef FEAT_SIGNS
2877 /* If this line has a sign with line highlighting set line_attr. */
2878 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2879 if (v != 0)
2880 line_attr = sign_get_attr((int)v, TRUE);
2881# endif
2882# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2883 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002884 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 line_attr = hl_attr(HLF_L);
2886# endif
2887 if (line_attr != 0)
2888 area_highlighting = TRUE;
2889#endif
2890
2891 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2892 ptr = line;
2893
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002894#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002895 if (has_spell)
2896 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002897 /* For checking first word with a capital skip white space. */
2898 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002899 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002900
Bram Moolenaar30abd282005-06-22 22:35:10 +00002901 /* To be able to spell-check over line boundaries copy the end of the
2902 * current line into nextline[]. Above the start of the next line was
2903 * copied to nextline[SPWORDLEN]. */
2904 if (nextline[SPWORDLEN] == NUL)
2905 {
2906 /* No next line or it is empty. */
2907 nextlinecol = MAXCOL;
2908 nextline_idx = 0;
2909 }
2910 else
2911 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002912 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002913 if (v < SPWORDLEN)
2914 {
2915 /* Short line, use it completely and append the start of the
2916 * next line. */
2917 nextlinecol = 0;
2918 mch_memmove(nextline, line, (size_t)v);
2919 mch_memmove(nextline + v, nextline + SPWORDLEN,
2920 STRLEN(nextline + SPWORDLEN) + 1);
2921 nextline_idx = v + 1;
2922 }
2923 else
2924 {
2925 /* Long line, use only the last SPWORDLEN bytes. */
2926 nextlinecol = v - SPWORDLEN;
2927 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2928 nextline_idx = SPWORDLEN + 1;
2929 }
2930 }
2931 }
2932#endif
2933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 /* find start of trailing whitespace */
2935 if (wp->w_p_list && lcs_trail)
2936 {
2937 trailcol = (colnr_T)STRLEN(ptr);
2938 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2939 --trailcol;
2940 trailcol += (colnr_T) (ptr - line);
2941 extra_check = TRUE;
2942 }
2943
2944 /*
2945 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2946 * first character to be displayed.
2947 */
2948 if (wp->w_p_wrap)
2949 v = wp->w_skipcol;
2950 else
2951 v = wp->w_leftcol;
2952 if (v > 0)
2953 {
2954#ifdef FEAT_MBYTE
2955 char_u *prev_ptr = ptr;
2956#endif
2957 while (vcol < v && *ptr != NUL)
2958 {
2959 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2960 vcol += c;
2961#ifdef FEAT_MBYTE
2962 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002964 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966
2967#ifdef FEAT_VIRTUALEDIT
2968 /* When 'virtualedit' is set the end of the line may be before the
2969 * start of the displayed part. */
2970 if (vcol < v && *ptr == NUL && virtual_active())
2971 vcol = v;
2972#endif
2973
2974 /* Handle a character that's not completely on the screen: Put ptr at
2975 * that character but skip the first few screen characters. */
2976 if (vcol > v)
2977 {
2978 vcol -= c;
2979#ifdef FEAT_MBYTE
2980 ptr = prev_ptr;
2981#else
2982 --ptr;
2983#endif
2984 n_skip = v - vcol;
2985 }
2986
2987 /*
2988 * Adjust for when the inverted text is before the screen,
2989 * and when the start of the inverted text is before the screen.
2990 */
2991 if (tocol <= vcol)
2992 fromcol = 0;
2993 else if (fromcol >= 0 && fromcol < vcol)
2994 fromcol = vcol;
2995
2996#ifdef FEAT_LINEBREAK
2997 /* When w_skipcol is non-zero, first line needs 'showbreak' */
2998 if (wp->w_p_wrap)
2999 need_showbreak = TRUE;
3000#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003001#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003002 /* When spell checking a word we need to figure out the start of the
3003 * word and if it's badly spelled or not. */
3004 if (has_spell)
3005 {
3006 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003007 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003008
3009 pos = wp->w_cursor;
3010 wp->w_cursor.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003011 wp->w_cursor.col = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003012 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003013 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003014 {
3015 /* no bad word found at line start, don't check until end of a
3016 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003017 spell_hlf = HLF_COUNT;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003018 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003019 }
3020 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003021 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003022 /* bad word found, use attributes until end of word */
3023 word_end = wp->w_cursor.col + len + 1;
3024
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003025 /* Turn index into actual attributes. */
3026 if (spell_hlf != HLF_COUNT)
3027 spell_attr = highlight_attr[spell_hlf];
3028 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003029 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003030
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003031# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003032 /* Need to restart syntax highlighting for this line. */
3033 if (has_syntax)
3034 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003035# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003036 }
3037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
3039
3040 /*
3041 * Correct highlighting for cursor that can't be disabled.
3042 * Avoids having to check this for each character.
3043 */
3044 if (fromcol >= 0)
3045 {
3046 if (noinvcur)
3047 {
3048 if ((colnr_T)fromcol == wp->w_virtcol)
3049 {
3050 /* highlighting starts at cursor, let it start just after the
3051 * cursor */
3052 fromcol_prev = fromcol;
3053 fromcol = -1;
3054 }
3055 else if ((colnr_T)fromcol < wp->w_virtcol)
3056 /* restart highlighting after the cursor */
3057 fromcol_prev = wp->w_virtcol;
3058 }
3059 if (fromcol >= tocol)
3060 fromcol = -1;
3061 }
3062
3063#ifdef FEAT_SEARCH_EXTRA
3064 /*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003065 * Handle highlighting the last used search pattern and ":match".
3066 * Do this for both search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003068 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003070 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003071 shl->startcol = MAXCOL;
3072 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 shl->attr_cur = 0;
3074 if (shl->rm.regprog != NULL)
3075 {
3076 v = (long)(ptr - line);
3077 next_search_hl(wp, shl, lnum, (colnr_T)v);
3078
3079 /* Need to get the line again, a multi-line regexp may have made it
3080 * invalid. */
3081 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3082 ptr = line + v;
3083
3084 if (shl->lnum != 0 && shl->lnum <= lnum)
3085 {
3086 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003087 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003089 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3091 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003092 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003094 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003096 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
3098#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003099 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003100 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 else
3102#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003103 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003105 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 {
3107 shl->attr_cur = shl->attr;
3108 search_attr = shl->attr;
3109 }
3110 area_highlighting = TRUE;
3111 }
3112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 }
3114#endif
3115
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003116#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003117 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3118 * active, because it's not clear what is selected then. */
3119 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003120 {
3121 line_attr = hl_attr(HLF_CUL);
3122 area_highlighting = TRUE;
3123 }
3124#endif
3125
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003126 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 col = 0;
3128#ifdef FEAT_RIGHTLEFT
3129 if (wp->w_p_rl)
3130 {
3131 /* Rightleft window: process the text in the normal direction, but put
3132 * it in current_ScreenLine[] from right to left. Start at the
3133 * rightmost column of the window. */
3134 col = W_WIDTH(wp) - 1;
3135 off += col;
3136 }
3137#endif
3138
3139 /*
3140 * Repeat for the whole displayed line.
3141 */
3142 for (;;)
3143 {
3144 /* Skip this quickly when working on the text. */
3145 if (draw_state != WL_LINE)
3146 {
3147#ifdef FEAT_CMDWIN
3148 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3149 {
3150 draw_state = WL_CMDLINE;
3151 if (cmdwin_type != 0 && wp == curwin)
3152 {
3153 /* Draw the cmdline character. */
3154 *extra = cmdwin_type;
3155 n_extra = 1;
3156 p_extra = extra;
3157 c_extra = NUL;
3158 char_attr = hl_attr(HLF_AT);
3159 }
3160 }
3161#endif
3162
3163#ifdef FEAT_FOLDING
3164 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3165 {
3166 draw_state = WL_FOLD;
3167 if (wp->w_p_fdc > 0)
3168 {
3169 /* Draw the 'foldcolumn'. */
3170 fill_foldcolumn(extra, wp, FALSE, lnum);
3171 n_extra = wp->w_p_fdc;
3172 p_extra = extra;
3173 c_extra = NUL;
3174 char_attr = hl_attr(HLF_FC);
3175 }
3176 }
3177#endif
3178
3179#ifdef FEAT_SIGNS
3180 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3181 {
3182 draw_state = WL_SIGN;
3183 /* Show the sign column when there are any signs in this
3184 * buffer or when using Netbeans. */
3185 if (draw_signcolumn(wp)
3186# ifdef FEAT_DIFF
3187 && filler_todo <= 0
3188# endif
3189 )
3190 {
3191 int_u text_sign;
3192# ifdef FEAT_SIGN_ICONS
3193 int_u icon_sign;
3194# endif
3195
3196 /* Draw two cells with the sign value or blank. */
3197 c_extra = ' ';
3198 char_attr = hl_attr(HLF_SC);
3199 n_extra = 2;
3200
3201 if (row == startrow)
3202 {
3203 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3204 SIGN_TEXT);
3205# ifdef FEAT_SIGN_ICONS
3206 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3207 SIGN_ICON);
3208 if (gui.in_use && icon_sign != 0)
3209 {
3210 /* Use the image in this position. */
3211 c_extra = SIGN_BYTE;
3212# ifdef FEAT_NETBEANS_INTG
3213 if (buf_signcount(wp->w_buffer, lnum) > 1)
3214 c_extra = MULTISIGN_BYTE;
3215# endif
3216 char_attr = icon_sign;
3217 }
3218 else
3219# endif
3220 if (text_sign != 0)
3221 {
3222 p_extra = sign_get_text(text_sign);
3223 if (p_extra != NULL)
3224 {
3225 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003226 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228 char_attr = sign_get_attr(text_sign, FALSE);
3229 }
3230 }
3231 }
3232 }
3233#endif
3234
3235 if (draw_state == WL_NR - 1 && n_extra == 0)
3236 {
3237 draw_state = WL_NR;
3238 /* Display the line number. After the first fill with blanks
3239 * when the 'n' flag isn't in 'cpo' */
3240 if (wp->w_p_nu
3241 && (row == startrow
3242#ifdef FEAT_DIFF
3243 + filler_lines
3244#endif
3245 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3246 {
3247 /* Draw the line number (empty space after wrapping). */
3248 if (row == startrow
3249#ifdef FEAT_DIFF
3250 + filler_lines
3251#endif
3252 )
3253 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003254 sprintf((char *)extra, "%*ld ",
3255 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (wp->w_skipcol > 0)
3257 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3258 *p_extra = '-';
3259#ifdef FEAT_RIGHTLEFT
3260 if (wp->w_p_rl) /* reverse line numbers */
3261 rl_mirror(extra);
3262#endif
3263 p_extra = extra;
3264 c_extra = NUL;
3265 }
3266 else
3267 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003268 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003270#ifdef FEAT_SYN_HL
3271 /* When 'cursorline' is set highlight the line number of
3272 * the current line differently. */
3273 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3274 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277 }
3278
3279#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3280 if (draw_state == WL_SBR - 1 && n_extra == 0)
3281 {
3282 draw_state = WL_SBR;
3283# ifdef FEAT_DIFF
3284 if (filler_todo > 0)
3285 {
3286 /* Draw "deleted" diff line(s). */
3287 if (char2cells(fill_diff) > 1)
3288 c_extra = '-';
3289 else
3290 c_extra = fill_diff;
3291# ifdef FEAT_RIGHTLEFT
3292 if (wp->w_p_rl)
3293 n_extra = col + 1;
3294 else
3295# endif
3296 n_extra = W_WIDTH(wp) - col;
3297 char_attr = hl_attr(HLF_DED);
3298 }
3299# endif
3300# ifdef FEAT_LINEBREAK
3301 if (*p_sbr != NUL && need_showbreak)
3302 {
3303 /* Draw 'showbreak' at the start of each broken line. */
3304 p_extra = p_sbr;
3305 c_extra = NUL;
3306 n_extra = (int)STRLEN(p_sbr);
3307 char_attr = hl_attr(HLF_AT);
3308 need_showbreak = FALSE;
3309 /* Correct end of highlighted area for 'showbreak',
3310 * required when 'linebreak' is also set. */
3311 if (tocol == vcol)
3312 tocol += n_extra;
3313 }
3314# endif
3315 }
3316#endif
3317
3318 if (draw_state == WL_LINE - 1 && n_extra == 0)
3319 {
3320 draw_state = WL_LINE;
3321 if (saved_n_extra)
3322 {
3323 /* Continue item from end of wrapped line. */
3324 n_extra = saved_n_extra;
3325 c_extra = saved_c_extra;
3326 p_extra = saved_p_extra;
3327 char_attr = saved_char_attr;
3328 }
3329 else
3330 char_attr = 0;
3331 }
3332 }
3333
3334 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003335 if (dollar_vcol != 0 && wp == curwin
3336 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337#ifdef FEAT_DIFF
3338 && filler_todo <= 0
3339#endif
3340 )
3341 {
3342 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3343 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003344 /* Pretend we have finished updating the window. Except when
3345 * 'cursorcolumn' is set. */
3346#ifdef FEAT_SYN_HL
3347 if (wp->w_p_cuc)
3348 row = wp->w_cline_row + wp->w_cline_height;
3349 else
3350#endif
3351 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 break;
3353 }
3354
3355 if (draw_state == WL_LINE && area_highlighting)
3356 {
3357 /* handle Visual or match highlighting in this line */
3358 if (vcol == fromcol
3359#ifdef FEAT_MBYTE
3360 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3361 && (*mb_ptr2cells)(ptr) > 1)
3362#endif
3363 || ((int)vcol_prev == fromcol_prev
3364 && vcol < tocol))
3365 area_attr = attr; /* start highlighting */
3366 else if (area_attr != 0
3367 && (vcol == tocol
3368 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371#ifdef FEAT_SEARCH_EXTRA
3372 if (!n_extra)
3373 {
3374 /*
3375 * Check for start/end of search pattern match.
3376 * After end, check for start/end of next match.
3377 * When another match, have to check for start again.
3378 * Watch out for matching an empty string!
3379 * Do this first for search_hl, then for match_hl, so that
3380 * ":match" overrules 'hlsearch'.
3381 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003382 v = (long)(ptr - line);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003383 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003385 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 while (shl->rm.regprog != NULL)
3387 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003388 if (shl->startcol != MAXCOL
3389 && v >= (long)shl->startcol
3390 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 {
3392 shl->attr_cur = shl->attr;
3393 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003394 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 {
3396 shl->attr_cur = 0;
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 next_search_hl(wp, shl, lnum, (colnr_T)v);
3399
3400 /* Need to get the line again, a multi-line regexp
3401 * may have made it invalid. */
3402 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3403 ptr = line + v;
3404
3405 if (shl->lnum == lnum)
3406 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003407 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003409 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003411 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003413 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
3415 /* highlight empty match, try again after
3416 * it */
3417#ifdef FEAT_MBYTE
3418 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003419 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003420 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 else
3422#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003423 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
3425
3426 /* Loop to check if the match starts at the
3427 * current position */
3428 continue;
3429 }
3430 }
3431 break;
3432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 /* ":match" highlighting overrules 'hlsearch' */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003436 for (i = 0; i <= 3; ++i)
3437 if (i == 3)
3438 search_attr = search_hl.attr_cur;
3439 else if (match_hl[i].attr_cur != 0)
3440 {
3441 search_attr = match_hl[i].attr_cur;
3442 break;
3443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445#endif
3446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003448 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 if (diff_hlf == HLF_CHD && ptr - line >= change_start)
3451 diff_hlf = HLF_TXD; /* changed text */
3452 if (diff_hlf == HLF_TXD && ptr - line > change_end)
3453 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003454 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003457
3458 /* Decide which of the highlight attributes to use. */
3459 attr_pri = TRUE;
3460 if (area_attr != 0)
3461 char_attr = area_attr;
3462 else if (search_attr != 0)
3463 char_attr = search_attr;
3464#ifdef LINE_ATTR
3465 /* Use line_attr when not in the Visual or 'incsearch' area
3466 * (area_attr may be 0 when "noinvcur" is set). */
3467 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3468 || (vcol < fromcol || vcol >= tocol)))
3469 char_attr = line_attr;
3470#endif
3471 else
3472 {
3473 attr_pri = FALSE;
3474#ifdef FEAT_SYN_HL
3475 if (has_syntax)
3476 char_attr = syntax_attr;
3477 else
3478#endif
3479 char_attr = 0;
3480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
3482
3483 /*
3484 * Get the next character to put on the screen.
3485 */
3486 /*
3487 * The 'extra' array contains the extra stuff that is inserted to
3488 * represent special characters (non-printable stuff). When all
3489 * characters are the same, c_extra is used.
3490 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3491 */
3492 if (n_extra > 0)
3493 {
3494 if (c_extra != NUL)
3495 {
3496 c = c_extra;
3497#ifdef FEAT_MBYTE
3498 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3499 if (enc_utf8 && (*mb_char2len)(c) > 1)
3500 {
3501 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003502 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003503 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505 else
3506 mb_utf8 = FALSE;
3507#endif
3508 }
3509 else
3510 {
3511 c = *p_extra;
3512#ifdef FEAT_MBYTE
3513 if (has_mbyte)
3514 {
3515 mb_c = c;
3516 if (enc_utf8)
3517 {
3518 /* If the UTF-8 character is more than one byte:
3519 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003520 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 mb_utf8 = FALSE;
3522 if (mb_l > n_extra)
3523 mb_l = 1;
3524 else if (mb_l > 1)
3525 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003526 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003528 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530 }
3531 else
3532 {
3533 /* if this is a DBCS character, put it in "mb_c" */
3534 mb_l = MB_BYTE2LEN(c);
3535 if (mb_l >= n_extra)
3536 mb_l = 1;
3537 else if (mb_l > 1)
3538 mb_c = (c << 8) + p_extra[1];
3539 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003540 if (mb_l == 0) /* at the NUL at end-of-line */
3541 mb_l = 1;
3542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* If a double-width char doesn't fit display a '>' in the
3544 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003545 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546# ifdef FEAT_RIGHTLEFT
3547 wp->w_p_rl ? (col <= 0) :
3548# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003549 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 && (*mb_char2cells)(mb_c) == 2)
3551 {
3552 c = '>';
3553 mb_c = c;
3554 mb_l = 1;
3555 mb_utf8 = FALSE;
3556 multi_attr = hl_attr(HLF_AT);
3557 /* put the pointer back to output the double-width
3558 * character at the start of the next line. */
3559 ++n_extra;
3560 --p_extra;
3561 }
3562 else
3563 {
3564 n_extra -= mb_l - 1;
3565 p_extra += mb_l - 1;
3566 }
3567 }
3568#endif
3569 ++p_extra;
3570 }
3571 --n_extra;
3572 }
3573 else
3574 {
3575 /*
3576 * Get a character from the line itself.
3577 */
3578 c = *ptr;
3579#ifdef FEAT_MBYTE
3580 if (has_mbyte)
3581 {
3582 mb_c = c;
3583 if (enc_utf8)
3584 {
3585 /* If the UTF-8 character is more than one byte: Decode it
3586 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003587 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 mb_utf8 = FALSE;
3589 if (mb_l > 1)
3590 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003591 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 /* Overlong encoded ASCII or ASCII with composing char
3593 * is displayed normally, except a NUL. */
3594 if (mb_c < 0x80)
3595 c = mb_c;
3596 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003597
3598 /* At start of the line we can have a composing char.
3599 * Draw it as a space with a composing char. */
3600 if (utf_iscomposing(mb_c))
3601 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003602 for (i = Screen_mco - 1; i > 0; --i)
3603 u8cc[i] = u8cc[i - 1];
3604 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003605 mb_c = ' ';
3606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608
3609 if ((mb_l == 1 && c >= 0x80)
3610 || (mb_l >= 1 && mb_c == 0)
3611 || (mb_l > 1 && (!vim_isprintc(mb_c)
3612 || mb_c >= 0x10000)))
3613 {
3614 /*
3615 * Illegal UTF-8 byte: display as <xx>.
3616 * Non-BMP character : display as ? or fullwidth ?.
3617 */
3618 if (mb_c < 0x10000)
3619 {
3620 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003621# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 if (wp->w_p_rl) /* reverse */
3623 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 else if (utf_char2cells(mb_c) != 2)
3627 STRCPY(extra, "?");
3628 else
3629 /* 0xff1f in UTF-8: full-width '?' */
3630 STRCPY(extra, "\357\274\237");
3631
3632 p_extra = extra;
3633 c = *p_extra;
3634 mb_c = mb_ptr2char_adv(&p_extra);
3635 mb_utf8 = (c >= 0x80);
3636 n_extra = (int)STRLEN(p_extra);
3637 c_extra = NUL;
3638 if (area_attr == 0 && search_attr == 0)
3639 {
3640 n_attr = n_extra + 1;
3641 extra_attr = hl_attr(HLF_8);
3642 saved_attr2 = char_attr; /* save current attr */
3643 }
3644 }
3645 else if (mb_l == 0) /* at the NUL at end-of-line */
3646 mb_l = 1;
3647#ifdef FEAT_ARABIC
3648 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3649 {
3650 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003651 int pc, pc1, nc;
3652 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 /* The idea of what is the previous and next
3655 * character depends on 'rightleft'. */
3656 if (wp->w_p_rl)
3657 {
3658 pc = prev_c;
3659 pc1 = prev_c1;
3660 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003661 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663 else
3664 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003665 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003667 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669 prev_c = mb_c;
3670
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003671 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673 else
3674 prev_c = mb_c;
3675#endif
3676 }
3677 else /* enc_dbcs */
3678 {
3679 mb_l = MB_BYTE2LEN(c);
3680 if (mb_l == 0) /* at the NUL at end-of-line */
3681 mb_l = 1;
3682 else if (mb_l > 1)
3683 {
3684 /* We assume a second byte below 32 is illegal.
3685 * Hopefully this is OK for all double-byte encodings!
3686 */
3687 if (ptr[1] >= 32)
3688 mb_c = (c << 8) + ptr[1];
3689 else
3690 {
3691 if (ptr[1] == NUL)
3692 {
3693 /* head byte at end of line */
3694 mb_l = 1;
3695 transchar_nonprint(extra, c);
3696 }
3697 else
3698 {
3699 /* illegal tail byte */
3700 mb_l = 2;
3701 STRCPY(extra, "XX");
3702 }
3703 p_extra = extra;
3704 n_extra = (int)STRLEN(extra) - 1;
3705 c_extra = NUL;
3706 c = *p_extra++;
3707 if (area_attr == 0 && search_attr == 0)
3708 {
3709 n_attr = n_extra + 1;
3710 extra_attr = hl_attr(HLF_8);
3711 saved_attr2 = char_attr; /* save current attr */
3712 }
3713 mb_c = c;
3714 }
3715 }
3716 }
3717 /* If a double-width char doesn't fit display a '>' in the
3718 * last column; the character is displayed at the start of the
3719 * next line. */
3720 if ((
3721# ifdef FEAT_RIGHTLEFT
3722 wp->w_p_rl ? (col <= 0) :
3723# endif
3724 (col >= W_WIDTH(wp) - 1))
3725 && (*mb_char2cells)(mb_c) == 2)
3726 {
3727 c = '>';
3728 mb_c = c;
3729 mb_utf8 = FALSE;
3730 mb_l = 1;
3731 multi_attr = hl_attr(HLF_AT);
3732 /* Put pointer back so that the character will be
3733 * displayed at the start of the next line. */
3734 --ptr;
3735 }
3736 else if (*ptr != NUL)
3737 ptr += mb_l - 1;
3738
3739 /* If a double-width char doesn't fit at the left side display
3740 * a '<' in the first column. */
3741 if (n_skip > 0 && mb_l > 1)
3742 {
3743 extra[0] = '<';
3744 p_extra = extra;
3745 n_extra = 1;
3746 c_extra = NUL;
3747 c = ' ';
3748 if (area_attr == 0 && search_attr == 0)
3749 {
3750 n_attr = n_extra + 1;
3751 extra_attr = hl_attr(HLF_AT);
3752 saved_attr2 = char_attr; /* save current attr */
3753 }
3754 mb_c = c;
3755 mb_utf8 = FALSE;
3756 mb_l = 1;
3757 }
3758
3759 }
3760#endif
3761 ++ptr;
3762
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003763 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003764 if (wp->w_p_list && (c == 160
3765#ifdef FEAT_MBYTE
3766 || (mb_utf8 && mb_c == 160)
3767#endif
3768 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003769 {
3770 c = lcs_nbsp;
3771 if (area_attr == 0 && search_attr == 0)
3772 {
3773 n_attr = 1;
3774 extra_attr = hl_attr(HLF_8);
3775 saved_attr2 = char_attr; /* save current attr */
3776 }
3777#ifdef FEAT_MBYTE
3778 mb_c = c;
3779 if (enc_utf8 && (*mb_char2len)(c) > 1)
3780 {
3781 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003782 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003783 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003784 }
3785 else
3786 mb_utf8 = FALSE;
3787#endif
3788 }
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (extra_check)
3791 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003792#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003793 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003794#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003795
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003796#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 /* Get syntax attribute, unless still at the start of the line
3798 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003799 v = (long)(ptr - line);
3800 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
3802 /* Get the syntax attribute for the character. If there
3803 * is an error, disable syntax highlighting. */
3804 save_did_emsg = did_emsg;
3805 did_emsg = FALSE;
3806
Bram Moolenaar217ad922005-03-20 22:37:15 +00003807 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003808# ifdef FEAT_SPELL
3809 has_spell ? &can_spell :
3810# endif
3811 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003814 {
3815 wp->w_buffer->b_syn_error = TRUE;
3816 has_syntax = FALSE;
3817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 else
3819 did_emsg = save_did_emsg;
3820
3821 /* Need to get the line again, a multi-line regexp may
3822 * have made it invalid. */
3823 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3824 ptr = line + v;
3825
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003826 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003828 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003829 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003831#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003832
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003833#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003834 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003835 * Only do this when there is no syntax highlighting, the
3836 * @Spell cluster is not used or the current syntax item
3837 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003838 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003839 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003840 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003841# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003842 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003843 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003844# endif
3845 if (c != 0 && (
3846# ifdef FEAT_SYN_HL
3847 !has_syntax ||
3848# endif
3849 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003850 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003851 char_u *prev_ptr, *p;
3852 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003853 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003854# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003855 if (has_mbyte)
3856 {
3857 prev_ptr = ptr - mb_l;
3858 v -= mb_l - 1;
3859 }
3860 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003861# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003862 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003863
3864 /* Use nextline[] if possible, it has the start of the
3865 * next line concatenated. */
3866 if ((prev_ptr - line) - nextlinecol >= 0)
3867 p = nextline + (prev_ptr - line) - nextlinecol;
3868 else
3869 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003870 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003871 len = spell_check(wp, p, &spell_hlf, &cap_col,
3872 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003873 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003874
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003875 /* In Insert mode only highlight a word that
3876 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003877 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003878 && (State & INSERT) != 0
3879 && wp->w_cursor.lnum == lnum
3880 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003881 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003882 && wp->w_cursor.col < (colnr_T)word_end)
3883 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003884 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003885 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003886 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003887
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003888 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003889 && (p - nextline) + len > nextline_idx)
3890 {
3891 /* Remember that the good word continues at the
3892 * start of the next line. */
3893 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003894 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003895 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003896
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003897 /* Turn index into actual attributes. */
3898 if (spell_hlf != HLF_COUNT)
3899 spell_attr = highlight_attr[spell_hlf];
3900
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003901 if (cap_col > 0)
3902 {
3903 if (p != prev_ptr
3904 && (p - nextline) + cap_col >= nextline_idx)
3905 {
3906 /* Remember that the word in the next line
3907 * must start with a capital. */
3908 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003909 cap_col = (int)((p - nextline) + cap_col
3910 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003911 }
3912 else
3913 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003914 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003915 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003916 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003917 }
3918 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003919 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003920 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003921 char_attr = hl_combine_attr(char_attr, spell_attr);
3922 else
3923 char_attr = hl_combine_attr(spell_attr, char_attr);
3924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#endif
3926#ifdef FEAT_LINEBREAK
3927 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00003928 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 */
3930 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
3933 n_extra = win_lbr_chartabsize(wp, ptr - (
3934# ifdef FEAT_MBYTE
3935 has_mbyte ? mb_l :
3936# endif
3937 1), (colnr_T)vcol, NULL) - 1;
3938 c_extra = ' ';
3939 if (vim_iswhite(c))
3940 c = ' ';
3941 }
3942#endif
3943
3944 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3945 {
3946 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003947 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
3949 n_attr = 1;
3950 extra_attr = hl_attr(HLF_8);
3951 saved_attr2 = char_attr; /* save current attr */
3952 }
3953#ifdef FEAT_MBYTE
3954 mb_c = c;
3955 if (enc_utf8 && (*mb_char2len)(c) > 1)
3956 {
3957 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003958 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003959 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 else
3962 mb_utf8 = FALSE;
3963#endif
3964 }
3965 }
3966
3967 /*
3968 * Handling of non-printable characters.
3969 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003970 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
3972 /*
3973 * when getting a character from the file, we may have to
3974 * turn it into something else on the way to putting it
3975 * into "ScreenLines".
3976 */
3977 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3978 {
3979 /* tab amount depends on current column */
3980 n_extra = (int)wp->w_buffer->b_p_ts
3981 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3982#ifdef FEAT_MBYTE
3983 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3984#endif
3985 if (wp->w_p_list)
3986 {
3987 c = lcs_tab1;
3988 c_extra = lcs_tab2;
3989 n_attr = n_extra + 1;
3990 extra_attr = hl_attr(HLF_8);
3991 saved_attr2 = char_attr; /* save current attr */
3992#ifdef FEAT_MBYTE
3993 mb_c = c;
3994 if (enc_utf8 && (*mb_char2len)(c) > 1)
3995 {
3996 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003997 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003998 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000#endif
4001 }
4002 else
4003 {
4004 c_extra = ' ';
4005 c = ' ';
4006 }
4007 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004008 else if (c == NUL
4009 && ((wp->w_p_list && lcs_eol > 0)
4010 || ((fromcol >= 0 || fromcol_prev >= 0)
4011 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004012#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004013 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004014#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004015 && (
4016# ifdef FEAT_RIGHTLEFT
4017 wp->w_p_rl ? (col >= 0) :
4018# endif
4019 (col < W_WIDTH(wp)))
4020 && !(noinvcur
4021 && (colnr_T)vcol == wp->w_virtcol)))
4022 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004024 /* Display a '$' after the line or highlight an extra
4025 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4027 /* For a diff line the highlighting continues after the
4028 * "$". */
4029 if (
4030# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004031 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032# ifdef LINE_ATTR
4033 &&
4034# endif
4035# endif
4036# ifdef LINE_ATTR
4037 line_attr == 0
4038# endif
4039 )
4040#endif
4041 {
4042#ifdef FEAT_VIRTUALEDIT
4043 /* In virtualedit, visual selections may extend
4044 * beyond end of line. */
4045 if (area_highlighting && virtual_active()
4046 && tocol != MAXCOL && vcol < tocol)
4047 n_extra = 0;
4048 else
4049#endif
4050 {
4051 p_extra = at_end_str;
4052 n_extra = 1;
4053 c_extra = NUL;
4054 }
4055 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004056 if (wp->w_p_list)
4057 c = lcs_eol;
4058 else
4059 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 lcs_eol_one = -1;
4061 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004062 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
4064 extra_attr = hl_attr(HLF_AT);
4065 n_attr = 1;
4066 }
4067#ifdef FEAT_MBYTE
4068 mb_c = c;
4069 if (enc_utf8 && (*mb_char2len)(c) > 1)
4070 {
4071 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004072 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 else
4076 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4077#endif
4078 }
4079 else if (c != NUL)
4080 {
4081 p_extra = transchar(c);
4082#ifdef FEAT_RIGHTLEFT
4083 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4084 rl_mirror(p_extra); /* reverse "<12>" */
4085#endif
4086 n_extra = byte2cells(c) - 1;
4087 c_extra = NUL;
4088 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004089 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
4091 n_attr = n_extra + 1;
4092 extra_attr = hl_attr(HLF_8);
4093 saved_attr2 = char_attr; /* save current attr */
4094 }
4095#ifdef FEAT_MBYTE
4096 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4097#endif
4098 }
4099#ifdef FEAT_VIRTUALEDIT
4100 else if (VIsual_active
4101 && (VIsual_mode == Ctrl_V
4102 || VIsual_mode == 'v')
4103 && virtual_active()
4104 && tocol != MAXCOL
4105 && vcol < tocol
4106 && (
4107# ifdef FEAT_RIGHTLEFT
4108 wp->w_p_rl ? (col >= 0) :
4109# endif
4110 (col < W_WIDTH(wp))))
4111 {
4112 c = ' ';
4113 --ptr; /* put it back at the NUL */
4114 }
4115#endif
4116#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4117 else if ((
4118# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004119 diff_hlf != (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120# ifdef LINE_ATTR
4121 ||
4122# endif
4123# endif
4124# ifdef LINE_ATTR
4125 line_attr != 0
4126# endif
4127 ) && (
4128# ifdef FEAT_RIGHTLEFT
4129 wp->w_p_rl ? (col >= 0) :
4130# endif
4131 (col < W_WIDTH(wp))))
4132 {
4133 /* Highlight until the right side of the window */
4134 c = ' ';
4135 --ptr; /* put it back at the NUL */
4136# ifdef FEAT_DIFF
4137 if (diff_hlf == HLF_TXD)
4138 {
4139 diff_hlf = HLF_CHD;
4140 if (attr == 0 || char_attr != attr)
4141 char_attr = hl_attr(diff_hlf);
4142 }
4143# endif
4144 }
4145#endif
4146 }
4147 }
4148
4149 /* Don't override visual selection highlighting. */
4150 if (n_attr > 0
4151 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004152 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 char_attr = extra_attr;
4154
Bram Moolenaar81695252004-12-29 20:58:21 +00004155#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 /* XIM don't send preedit_start and preedit_end, but they send
4157 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4158 * im_is_preediting() here. */
4159 if (xic != NULL
4160 && lnum == curwin->w_cursor.lnum
4161 && (State & INSERT)
4162 && !p_imdisable
4163 && im_is_preediting()
4164 && draw_state == WL_LINE)
4165 {
4166 colnr_T tcol;
4167
4168 if (preedit_end_col == MAXCOL)
4169 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4170 else
4171 tcol = preedit_end_col;
4172 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4173 {
4174 if (feedback_old_attr < 0)
4175 {
4176 feedback_col = 0;
4177 feedback_old_attr = char_attr;
4178 }
4179 char_attr = im_get_feedback_attr(feedback_col);
4180 if (char_attr < 0)
4181 char_attr = feedback_old_attr;
4182 feedback_col++;
4183 }
4184 else if (feedback_old_attr >= 0)
4185 {
4186 char_attr = feedback_old_attr;
4187 feedback_old_attr = -1;
4188 feedback_col = 0;
4189 }
4190 }
4191#endif
4192 /*
4193 * Handle the case where we are in column 0 but not on the first
4194 * character of the line and the user wants us to show us a
4195 * special character (via 'listchars' option "precedes:<char>".
4196 */
4197 if (lcs_prec_todo != NUL
4198 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4199#ifdef FEAT_DIFF
4200 && filler_todo <= 0
4201#endif
4202 && draw_state > WL_NR
4203 && c != NUL)
4204 {
4205 c = lcs_prec;
4206 lcs_prec_todo = NUL;
4207#ifdef FEAT_MBYTE
4208 mb_c = c;
4209 if (enc_utf8 && (*mb_char2len)(c) > 1)
4210 {
4211 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004212 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004213 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 else
4216 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4217#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004218 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
4220 saved_attr3 = char_attr; /* save current attr */
4221 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4222 n_attr3 = 1;
4223 }
4224 }
4225
4226 /*
4227 * At end of the text line.
4228 */
4229 if (c == NUL)
4230 {
4231 /* invert at least one char, used for Visual and empty line or
4232 * highlight match at end of line. If it's beyond the last
4233 * char on the screen, just overwrite that one (tricky!) Not
4234 * needed when a '$' was displayed for 'list'. */
4235 if (lcs_eol == lcs_eol_one
4236 && ((area_attr != 0 && vcol == fromcol)
4237#ifdef FEAT_SEARCH_EXTRA
4238 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004239 || (ptr - line) - 1 == (long)search_hl.startcol
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004240 || (ptr - line) - 1 == (long)match_hl[0].startcol
4241 || (ptr - line) - 1 == (long)match_hl[1].startcol
4242 || (ptr - line) - 1 == (long)match_hl[2].startcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243#endif
4244 ))
4245 {
4246 int n = 0;
4247
4248#ifdef FEAT_RIGHTLEFT
4249 if (wp->w_p_rl)
4250 {
4251 if (col < 0)
4252 n = 1;
4253 }
4254 else
4255#endif
4256 {
4257 if (col >= W_WIDTH(wp))
4258 n = -1;
4259 }
4260 if (n != 0)
4261 {
4262 /* At the window boundary, highlight the last character
4263 * instead (better than nothing). */
4264 off += n;
4265 col += n;
4266 }
4267 else
4268 {
4269 /* Add a blank character to highlight. */
4270 ScreenLines[off] = ' ';
4271#ifdef FEAT_MBYTE
4272 if (enc_utf8)
4273 ScreenLinesUC[off] = 0;
4274#endif
4275 }
4276#ifdef FEAT_SEARCH_EXTRA
4277 if (area_attr == 0)
4278 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004279 for (i = 0; i <= 3; ++i)
4280 {
4281 if (i == 3)
4282 char_attr = search_hl.attr;
4283 else if ((ptr - line) - 1 == (long)match_hl[i].startcol)
4284 {
4285 char_attr = match_hl[i].attr;
4286 break;
4287 }
4288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290#endif
4291 ScreenAttrs[off] = char_attr;
4292#ifdef FEAT_RIGHTLEFT
4293 if (wp->w_p_rl)
4294 --col;
4295 else
4296#endif
4297 ++col;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004298 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004301#ifdef FEAT_SYN_HL
4302 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004303 if (wp->w_p_wrap)
4304 v = wp->w_skipcol;
4305 else
4306 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004307 /* check if line ends before left margin */
4308 if (vcol < v + col - win_col_off(wp))
4309
4310 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004311 if (wp->w_p_cuc
4312 && (int)wp->w_virtcol >= vcol
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004313 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4314 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004315 && lnum != wp->w_cursor.lnum
4316# ifdef FEAT_RIGHTLEFT
4317 && !wp->w_p_rl
4318# endif
4319 )
4320 {
4321 while (col < W_WIDTH(wp))
4322 {
4323 ScreenLines[off] = ' ';
4324#ifdef FEAT_MBYTE
4325 if (enc_utf8)
4326 ScreenLinesUC[off] = 0;
4327#endif
4328 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004329 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004330 {
4331 ScreenAttrs[off] = hl_attr(HLF_CUC);
4332 break;
4333 }
4334 ScreenAttrs[off++] = 0;
4335 ++vcol;
4336 }
4337 }
4338#endif
4339
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4341 wp->w_p_rl);
4342 row++;
4343
4344 /*
4345 * Update w_cline_height and w_cline_folded if the cursor line was
4346 * updated (saves a call to plines() later).
4347 */
4348 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4349 {
4350 curwin->w_cline_row = startrow;
4351 curwin->w_cline_height = row - startrow;
4352#ifdef FEAT_FOLDING
4353 curwin->w_cline_folded = FALSE;
4354#endif
4355 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4356 }
4357
4358 break;
4359 }
4360
4361 /* line continues beyond line end */
4362 if (lcs_ext
4363 && !wp->w_p_wrap
4364#ifdef FEAT_DIFF
4365 && filler_todo <= 0
4366#endif
4367 && (
4368#ifdef FEAT_RIGHTLEFT
4369 wp->w_p_rl ? col == 0 :
4370#endif
4371 col == W_WIDTH(wp) - 1)
4372 && (*ptr != NUL
4373 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4374 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4375 {
4376 c = lcs_ext;
4377 char_attr = hl_attr(HLF_AT);
4378#ifdef FEAT_MBYTE
4379 mb_c = c;
4380 if (enc_utf8 && (*mb_char2len)(c) > 1)
4381 {
4382 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004383 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004384 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386 else
4387 mb_utf8 = FALSE;
4388#endif
4389 }
4390
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004391#ifdef FEAT_SYN_HL
4392 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4393 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004394 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004395 && lnum != wp->w_cursor.lnum
4396 && draw_state == WL_LINE)
4397 {
4398 vcol_save_attr = char_attr;
4399 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4400 }
4401 else
4402 vcol_save_attr = -1;
4403#endif
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 /*
4406 * Store character to be displayed.
4407 * Skip characters that are left of the screen for 'nowrap'.
4408 */
4409 vcol_prev = vcol;
4410 if (draw_state < WL_LINE || n_skip <= 0)
4411 {
4412 /*
4413 * Store the character.
4414 */
4415#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4416 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4417 {
4418 /* A double-wide character is: put first halve in left cell. */
4419 --off;
4420 --col;
4421 }
4422#endif
4423 ScreenLines[off] = c;
4424#ifdef FEAT_MBYTE
4425 if (enc_dbcs == DBCS_JPNU)
4426 ScreenLines2[off] = mb_c & 0xff;
4427 else if (enc_utf8)
4428 {
4429 if (mb_utf8)
4430 {
4431 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004432 if ((c & 0xff) == 0)
4433 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004434 for (i = 0; i < Screen_mco; ++i)
4435 {
4436 ScreenLinesC[i][off] = u8cc[i];
4437 if (u8cc[i] == 0)
4438 break;
4439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 else
4442 ScreenLinesUC[off] = 0;
4443 }
4444 if (multi_attr)
4445 {
4446 ScreenAttrs[off] = multi_attr;
4447 multi_attr = 0;
4448 }
4449 else
4450#endif
4451 ScreenAttrs[off] = char_attr;
4452
4453#ifdef FEAT_MBYTE
4454 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4455 {
4456 /* Need to fill two screen columns. */
4457 ++off;
4458 ++col;
4459 if (enc_utf8)
4460 /* UTF-8: Put a 0 in the second screen char. */
4461 ScreenLines[off] = 0;
4462 else
4463 /* DBCS: Put second byte in the second screen char. */
4464 ScreenLines[off] = mb_c & 0xff;
4465 ++vcol;
4466 /* When "tocol" is halfway a character, set it to the end of
4467 * the character, otherwise highlighting won't stop. */
4468 if (tocol == vcol)
4469 ++tocol;
4470#ifdef FEAT_RIGHTLEFT
4471 if (wp->w_p_rl)
4472 {
4473 /* now it's time to backup one cell */
4474 --off;
4475 --col;
4476 }
4477#endif
4478 }
4479#endif
4480#ifdef FEAT_RIGHTLEFT
4481 if (wp->w_p_rl)
4482 {
4483 --off;
4484 --col;
4485 }
4486 else
4487#endif
4488 {
4489 ++off;
4490 ++col;
4491 }
4492 }
4493 else
4494 --n_skip;
4495
4496 /* Only advance the "vcol" when after the 'number' column. */
4497 if (draw_state >= WL_SBR
4498#ifdef FEAT_DIFF
4499 && filler_todo <= 0
4500#endif
4501 )
4502 ++vcol;
4503
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004504#ifdef FEAT_SYN_HL
4505 if (vcol_save_attr >= 0)
4506 char_attr = vcol_save_attr;
4507#endif
4508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 /* restore attributes after "predeces" in 'listchars' */
4510 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4511 char_attr = saved_attr3;
4512
4513 /* restore attributes after last 'listchars' or 'number' char */
4514 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4515 char_attr = saved_attr2;
4516
4517 /*
4518 * At end of screen line and there is more to come: Display the line
4519 * so far. If there is no more to display it is catched above.
4520 */
4521 if ((
4522#ifdef FEAT_RIGHTLEFT
4523 wp->w_p_rl ? (col < 0) :
4524#endif
4525 (col >= W_WIDTH(wp)))
4526 && (*ptr != NUL
4527#ifdef FEAT_DIFF
4528 || filler_todo > 0
4529#endif
4530 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4531 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4532 )
4533 {
4534 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4535 wp->w_p_rl);
4536 ++row;
4537 ++screen_row;
4538
4539 /* When not wrapping and finished diff lines, or when displayed
4540 * '$' and highlighting until last column, break here. */
4541 if ((!wp->w_p_wrap
4542#ifdef FEAT_DIFF
4543 && filler_todo <= 0
4544#endif
4545 ) || lcs_eol_one == -1)
4546 break;
4547
4548 /* When the window is too narrow draw all "@" lines. */
4549 if (draw_state != WL_LINE
4550#ifdef FEAT_DIFF
4551 && filler_todo <= 0
4552#endif
4553 )
4554 {
4555 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4556#ifdef FEAT_VERTSPLIT
4557 draw_vsep_win(wp, row);
4558#endif
4559 row = endrow;
4560 }
4561
4562 /* When line got too long for screen break here. */
4563 if (row == endrow)
4564 {
4565 ++row;
4566 break;
4567 }
4568
4569 if (screen_cur_row == screen_row - 1
4570#ifdef FEAT_DIFF
4571 && filler_todo <= 0
4572#endif
4573 && W_WIDTH(wp) == Columns)
4574 {
4575 /* Remember that the line wraps, used for modeless copy. */
4576 LineWraps[screen_row - 1] = TRUE;
4577
4578 /*
4579 * Special trick to make copy/paste of wrapped lines work with
4580 * xterm/screen: write an extra character beyond the end of
4581 * the line. This will work with all terminal types
4582 * (regardless of the xn,am settings).
4583 * Only do this on a fast tty.
4584 * Only do this if the cursor is on the current line
4585 * (something has been written in it).
4586 * Don't do this for the GUI.
4587 * Don't do this for double-width characters.
4588 * Don't do this for a window not at the right screen border.
4589 */
4590 if (p_tf
4591#ifdef FEAT_GUI
4592 && !gui.in_use
4593#endif
4594#ifdef FEAT_MBYTE
4595 && !(has_mbyte
4596 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4597 || (*mb_off2cells)(LineOffset[screen_row - 1]
4598 + (int)Columns - 2) == 2))
4599#endif
4600 )
4601 {
4602 /* First make sure we are at the end of the screen line,
4603 * then output the same character again to let the
4604 * terminal know about the wrap. If the terminal doesn't
4605 * auto-wrap, we overwrite the character. */
4606 if (screen_cur_col != W_WIDTH(wp))
4607 screen_char(LineOffset[screen_row - 1]
4608 + (unsigned)Columns - 1,
4609 screen_row - 1, (int)(Columns - 1));
4610
4611#ifdef FEAT_MBYTE
4612 /* When there is a multi-byte character, just output a
4613 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004614 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4615 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 out_char(' ');
4617 else
4618#endif
4619 out_char(ScreenLines[LineOffset[screen_row - 1]
4620 + (Columns - 1)]);
4621 /* force a redraw of the first char on the next line */
4622 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4623 screen_start(); /* don't know where cursor is now */
4624 }
4625 }
4626
4627 col = 0;
4628 off = (unsigned)(current_ScreenLine - ScreenLines);
4629#ifdef FEAT_RIGHTLEFT
4630 if (wp->w_p_rl)
4631 {
4632 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4633 off += col;
4634 }
4635#endif
4636
4637 /* reset the drawing state for the start of a wrapped line */
4638 draw_state = WL_START;
4639 saved_n_extra = n_extra;
4640 saved_p_extra = p_extra;
4641 saved_c_extra = c_extra;
4642 saved_char_attr = char_attr;
4643 n_extra = 0;
4644 lcs_prec_todo = lcs_prec;
4645#ifdef FEAT_LINEBREAK
4646# ifdef FEAT_DIFF
4647 if (filler_todo <= 0)
4648# endif
4649 need_showbreak = TRUE;
4650#endif
4651#ifdef FEAT_DIFF
4652 --filler_todo;
4653 /* When the filler lines are actually below the last line of the
4654 * file, don't draw the line itself, break here. */
4655 if (filler_todo == 0 && wp->w_botfill)
4656 break;
4657#endif
4658 }
4659
4660 } /* for every character in the line */
4661
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004662#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004663 /* After an empty line check first word for capital. */
4664 if (*skipwhite(line) == NUL)
4665 {
4666 capcol_lnum = lnum + 1;
4667 cap_col = 0;
4668 }
4669#endif
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return row;
4672}
4673
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004674#ifdef FEAT_MBYTE
4675static int comp_char_differs __ARGS((int, int));
4676
4677/*
4678 * Return if the composing characters at "off_from" and "off_to" differ.
4679 */
4680 static int
4681comp_char_differs(off_from, off_to)
4682 int off_from;
4683 int off_to;
4684{
4685 int i;
4686
4687 for (i = 0; i < Screen_mco; ++i)
4688 {
4689 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4690 return TRUE;
4691 if (ScreenLinesC[i][off_from] == 0)
4692 break;
4693 }
4694 return FALSE;
4695}
4696#endif
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698/*
4699 * Check whether the given character needs redrawing:
4700 * - the (first byte of the) character is different
4701 * - the attributes are different
4702 * - the character is multi-byte and the next byte is different
4703 */
4704 static int
4705char_needs_redraw(off_from, off_to, cols)
4706 int off_from;
4707 int off_to;
4708 int cols;
4709{
4710 if (cols > 0
4711 && ((ScreenLines[off_from] != ScreenLines[off_to]
4712 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4713
4714#ifdef FEAT_MBYTE
4715 || (enc_dbcs != 0
4716 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4717 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4718 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4719 : (cols > 1 && ScreenLines[off_from + 1]
4720 != ScreenLines[off_to + 1])))
4721 || (enc_utf8
4722 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4723 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004724 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725#endif
4726 ))
4727 return TRUE;
4728 return FALSE;
4729}
4730
4731/*
4732 * Move one "cooked" screen line to the screen, but only the characters that
4733 * have actually changed. Handle insert/delete character.
4734 * "coloff" gives the first column on the screen for this line.
4735 * "endcol" gives the columns where valid characters are.
4736 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4737 * needs to be cleared, negative otherwise.
4738 * "rlflag" is TRUE in a rightleft window:
4739 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4740 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4741 */
4742 static void
4743screen_line(row, coloff, endcol, clear_width
4744#ifdef FEAT_RIGHTLEFT
4745 , rlflag
4746#endif
4747 )
4748 int row;
4749 int coloff;
4750 int endcol;
4751 int clear_width;
4752#ifdef FEAT_RIGHTLEFT
4753 int rlflag;
4754#endif
4755{
4756 unsigned off_from;
4757 unsigned off_to;
4758 int col = 0;
4759#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4760 int hl;
4761#endif
4762 int force = FALSE; /* force update rest of the line */
4763 int redraw_this /* bool: does character need redraw? */
4764#ifdef FEAT_GUI
4765 = TRUE /* For GUI when while-loop empty */
4766#endif
4767 ;
4768 int redraw_next; /* redraw_this for next character */
4769#ifdef FEAT_MBYTE
4770 int clear_next = FALSE;
4771 int char_cells; /* 1: normal char */
4772 /* 2: occupies two display cells */
4773# define CHAR_CELLS char_cells
4774#else
4775# define CHAR_CELLS 1
4776#endif
4777
4778# ifdef FEAT_CLIPBOARD
4779 clip_may_clear_selection(row, row);
4780# endif
4781
4782 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4783 off_to = LineOffset[row] + coloff;
4784
4785#ifdef FEAT_RIGHTLEFT
4786 if (rlflag)
4787 {
4788 /* Clear rest first, because it's left of the text. */
4789 if (clear_width > 0)
4790 {
4791 while (col <= endcol && ScreenLines[off_to] == ' '
4792 && ScreenAttrs[off_to] == 0
4793# ifdef FEAT_MBYTE
4794 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4795# endif
4796 )
4797 {
4798 ++off_to;
4799 ++col;
4800 }
4801 if (col <= endcol)
4802 screen_fill(row, row + 1, col + coloff,
4803 endcol + coloff + 1, ' ', ' ', 0);
4804 }
4805 col = endcol + 1;
4806 off_to = LineOffset[row] + col + coloff;
4807 off_from += col;
4808 endcol = (clear_width > 0 ? clear_width : -clear_width);
4809 }
4810#endif /* FEAT_RIGHTLEFT */
4811
4812 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4813
4814 while (col < endcol)
4815 {
4816#ifdef FEAT_MBYTE
4817 if (has_mbyte && (col + 1 < endcol))
4818 char_cells = (*mb_off2cells)(off_from);
4819 else
4820 char_cells = 1;
4821#endif
4822
4823 redraw_this = redraw_next;
4824 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4825 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4826
4827#ifdef FEAT_GUI
4828 /* If the next character was bold, then redraw the current character to
4829 * remove any pixels that might have spilt over into us. This only
4830 * happens in the GUI.
4831 */
4832 if (redraw_next && gui.in_use)
4833 {
4834 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004835 if (hl > HL_ALL)
4836 hl = syn_attr2attr(hl);
4837 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 redraw_this = TRUE;
4839 }
4840#endif
4841
4842 if (redraw_this)
4843 {
4844 /*
4845 * Special handling when 'xs' termcap flag set (hpterm):
4846 * Attributes for characters are stored at the position where the
4847 * cursor is when writing the highlighting code. The
4848 * start-highlighting code must be written with the cursor on the
4849 * first highlighted character. The stop-highlighting code must
4850 * be written with the cursor just after the last highlighted
4851 * character.
4852 * Overwriting a character doesn't remove it's highlighting. Need
4853 * to clear the rest of the line, and force redrawing it
4854 * completely.
4855 */
4856 if ( p_wiv
4857 && !force
4858#ifdef FEAT_GUI
4859 && !gui.in_use
4860#endif
4861 && ScreenAttrs[off_to] != 0
4862 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4863 {
4864 /*
4865 * Need to remove highlighting attributes here.
4866 */
4867 windgoto(row, col + coloff);
4868 out_str(T_CE); /* clear rest of this screen line */
4869 screen_start(); /* don't know where cursor is now */
4870 force = TRUE; /* force redraw of rest of the line */
4871 redraw_next = TRUE; /* or else next char would miss out */
4872
4873 /*
4874 * If the previous character was highlighted, need to stop
4875 * highlighting at this character.
4876 */
4877 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4878 {
4879 screen_attr = ScreenAttrs[off_to - 1];
4880 term_windgoto(row, col + coloff);
4881 screen_stop_highlight();
4882 }
4883 else
4884 screen_attr = 0; /* highlighting has stopped */
4885 }
4886#ifdef FEAT_MBYTE
4887 if (enc_dbcs != 0)
4888 {
4889 /* Check if overwriting a double-byte with a single-byte or
4890 * the other way around requires another character to be
4891 * redrawn. For UTF-8 this isn't needed, because comparing
4892 * ScreenLinesUC[] is sufficient. */
4893 if (char_cells == 1
4894 && col + 1 < endcol
4895 && (*mb_off2cells)(off_to) > 1)
4896 {
4897 /* Writing a single-cell character over a double-cell
4898 * character: need to redraw the next cell. */
4899 ScreenLines[off_to + 1] = 0;
4900 redraw_next = TRUE;
4901 }
4902 else if (char_cells == 2
4903 && col + 2 < endcol
4904 && (*mb_off2cells)(off_to) == 1
4905 && (*mb_off2cells)(off_to + 1) > 1)
4906 {
4907 /* Writing the second half of a double-cell character over
4908 * a double-cell character: need to redraw the second
4909 * cell. */
4910 ScreenLines[off_to + 2] = 0;
4911 redraw_next = TRUE;
4912 }
4913
4914 if (enc_dbcs == DBCS_JPNU)
4915 ScreenLines2[off_to] = ScreenLines2[off_from];
4916 }
4917 /* When writing a single-width character over a double-width
4918 * character and at the end of the redrawn text, need to clear out
4919 * the right halve of the old character.
4920 * Also required when writing the right halve of a double-width
4921 * char over the left halve of an existing one. */
4922 if (has_mbyte && col + char_cells == endcol
4923 && ((char_cells == 1
4924 && (*mb_off2cells)(off_to) > 1)
4925 || (char_cells == 2
4926 && (*mb_off2cells)(off_to) == 1
4927 && (*mb_off2cells)(off_to + 1) > 1)))
4928 clear_next = TRUE;
4929#endif
4930
4931 ScreenLines[off_to] = ScreenLines[off_from];
4932#ifdef FEAT_MBYTE
4933 if (enc_utf8)
4934 {
4935 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4936 if (ScreenLinesUC[off_from] != 0)
4937 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004938 int i;
4939
4940 for (i = 0; i < Screen_mco; ++i)
4941 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943 }
4944 if (char_cells == 2)
4945 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4946#endif
4947
4948#if defined(FEAT_GUI) || defined(UNIX)
4949 /* The bold trick makes a single row of pixels appear in the next
4950 * character. When a bold character is removed, the next
4951 * character should be redrawn too. This happens for our own GUI
4952 * and for some xterms. */
4953 if (
4954# ifdef FEAT_GUI
4955 gui.in_use
4956# endif
4957# if defined(FEAT_GUI) && defined(UNIX)
4958 ||
4959# endif
4960# ifdef UNIX
4961 term_is_xterm
4962# endif
4963 )
4964 {
4965 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004966 if (hl > HL_ALL)
4967 hl = syn_attr2attr(hl);
4968 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 redraw_next = TRUE;
4970 }
4971#endif
4972 ScreenAttrs[off_to] = ScreenAttrs[off_from];
4973#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004974 /* For simplicity set the attributes of second half of a
4975 * double-wide character equal to the first half. */
4976 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004978
4979 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
4982#endif
4983 screen_char(off_to, row, col + coloff);
4984 }
4985 else if ( p_wiv
4986#ifdef FEAT_GUI
4987 && !gui.in_use
4988#endif
4989 && col + coloff > 0)
4990 {
4991 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
4992 {
4993 /*
4994 * Don't output stop-highlight when moving the cursor, it will
4995 * stop the highlighting when it should continue.
4996 */
4997 screen_attr = 0;
4998 }
4999 else if (screen_attr != 0)
5000 screen_stop_highlight();
5001 }
5002
5003 off_to += CHAR_CELLS;
5004 off_from += CHAR_CELLS;
5005 col += CHAR_CELLS;
5006 }
5007
5008#ifdef FEAT_MBYTE
5009 if (clear_next)
5010 {
5011 /* Clear the second half of a double-wide character of which the left
5012 * half was overwritten with a single-wide character. */
5013 ScreenLines[off_to] = ' ';
5014 if (enc_utf8)
5015 ScreenLinesUC[off_to] = 0;
5016 screen_char(off_to, row, col + coloff);
5017 }
5018#endif
5019
5020 if (clear_width > 0
5021#ifdef FEAT_RIGHTLEFT
5022 && !rlflag
5023#endif
5024 )
5025 {
5026#ifdef FEAT_GUI
5027 int startCol = col;
5028#endif
5029
5030 /* blank out the rest of the line */
5031 while (col < clear_width && ScreenLines[off_to] == ' '
5032 && ScreenAttrs[off_to] == 0
5033#ifdef FEAT_MBYTE
5034 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5035#endif
5036 )
5037 {
5038 ++off_to;
5039 ++col;
5040 }
5041 if (col < clear_width)
5042 {
5043#ifdef FEAT_GUI
5044 /*
5045 * In the GUI, clearing the rest of the line may leave pixels
5046 * behind if the first character cleared was bold. Some bold
5047 * fonts spill over the left. In this case we redraw the previous
5048 * character too. If we didn't skip any blanks above, then we
5049 * only redraw if the character wasn't already redrawn anyway.
5050 */
5051 if (gui.in_use && (col > startCol || !redraw_this)
5052# ifdef FEAT_MBYTE
5053 && enc_dbcs == 0
5054# endif
5055 )
5056 {
5057 hl = ScreenAttrs[off_to];
5058 if (hl > HL_ALL || (hl & HL_BOLD))
5059 screen_char(off_to - 1, row, col + coloff - 1);
5060 }
5061#endif
5062 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5063 ' ', ' ', 0);
5064#ifdef FEAT_VERTSPLIT
5065 off_to += clear_width - col;
5066 col = clear_width;
5067#endif
5068 }
5069 }
5070
5071 if (clear_width > 0)
5072 {
5073#ifdef FEAT_VERTSPLIT
5074 /* For a window that's left of another, draw the separator char. */
5075 if (col + coloff < Columns)
5076 {
5077 int c;
5078
5079 c = fillchar_vsep(&hl);
5080 if (ScreenLines[off_to] != c
5081# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005082 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5083 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084# endif
5085 || ScreenAttrs[off_to] != hl)
5086 {
5087 ScreenLines[off_to] = c;
5088 ScreenAttrs[off_to] = hl;
5089# ifdef FEAT_MBYTE
5090 if (enc_utf8)
5091 {
5092 if (c >= 0x80)
5093 {
5094 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005095 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 else
5098 ScreenLinesUC[off_to] = 0;
5099 }
5100# endif
5101 screen_char(off_to, row, col + coloff);
5102 }
5103 }
5104 else
5105#endif
5106 LineWraps[row] = FALSE;
5107 }
5108}
5109
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005110#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005112 * Mirror text "str" for right-left displaying.
5113 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005115 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116rl_mirror(str)
5117 char_u *str;
5118{
5119 char_u *p1, *p2;
5120 int t;
5121
5122 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5123 {
5124 t = *p1;
5125 *p1 = *p2;
5126 *p2 = t;
5127 }
5128}
5129#endif
5130
5131#if defined(FEAT_WINDOWS) || defined(PROTO)
5132/*
5133 * mark all status lines for redraw; used after first :cd
5134 */
5135 void
5136status_redraw_all()
5137{
5138 win_T *wp;
5139
5140 for (wp = firstwin; wp; wp = wp->w_next)
5141 if (wp->w_status_height)
5142 {
5143 wp->w_redr_status = TRUE;
5144 redraw_later(VALID);
5145 }
5146}
5147
5148/*
5149 * mark all status lines of the current buffer for redraw
5150 */
5151 void
5152status_redraw_curbuf()
5153{
5154 win_T *wp;
5155
5156 for (wp = firstwin; wp; wp = wp->w_next)
5157 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5158 {
5159 wp->w_redr_status = TRUE;
5160 redraw_later(VALID);
5161 }
5162}
5163
5164/*
5165 * Redraw all status lines that need to be redrawn.
5166 */
5167 void
5168redraw_statuslines()
5169{
5170 win_T *wp;
5171
5172 for (wp = firstwin; wp; wp = wp->w_next)
5173 if (wp->w_redr_status)
5174 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005175 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005176 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177}
5178#endif
5179
5180#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5181/*
5182 * Redraw all status lines at the bottom of frame "frp".
5183 */
5184 void
5185win_redraw_last_status(frp)
5186 frame_T *frp;
5187{
5188 if (frp->fr_layout == FR_LEAF)
5189 frp->fr_win->w_redr_status = TRUE;
5190 else if (frp->fr_layout == FR_ROW)
5191 {
5192 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5193 win_redraw_last_status(frp);
5194 }
5195 else /* frp->fr_layout == FR_COL */
5196 {
5197 frp = frp->fr_child;
5198 while (frp->fr_next != NULL)
5199 frp = frp->fr_next;
5200 win_redraw_last_status(frp);
5201 }
5202}
5203#endif
5204
5205#ifdef FEAT_VERTSPLIT
5206/*
5207 * Draw the verticap separator right of window "wp" starting with line "row".
5208 */
5209 static void
5210draw_vsep_win(wp, row)
5211 win_T *wp;
5212 int row;
5213{
5214 int hl;
5215 int c;
5216
5217 if (wp->w_vsep_width)
5218 {
5219 /* draw the vertical separator right of this window */
5220 c = fillchar_vsep(&hl);
5221 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5222 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5223 c, ' ', hl);
5224 }
5225}
5226#endif
5227
5228#ifdef FEAT_WILDMENU
5229static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005230static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
5232/*
5233 * Get the lenght of an item as it will be shown in the status line.
5234 */
5235 static int
5236status_match_len(xp, s)
5237 expand_T *xp;
5238 char_u *s;
5239{
5240 int len = 0;
5241
5242#ifdef FEAT_MENU
5243 int emenu = (xp->xp_context == EXPAND_MENUS
5244 || xp->xp_context == EXPAND_MENUNAMES);
5245
5246 /* Check for menu separators - replace with '|'. */
5247 if (emenu && menu_is_separator(s))
5248 return 1;
5249#endif
5250
5251 while (*s != NUL)
5252 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005253 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005255 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005256 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258
5259 return len;
5260}
5261
5262/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005263 * Return TRUE for characters that are not displayed in a status match.
5264 * These are backslashes used for escaping. Do show backslashes in help tags.
5265 */
5266 static int
5267skip_status_match_char(xp, s)
5268 expand_T *xp;
5269 char_u *s;
5270{
5271 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5272#ifdef FEAT_MENU
5273 || ((xp->xp_context == EXPAND_MENUS
5274 || xp->xp_context == EXPAND_MENUNAMES)
5275 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5276#endif
5277 );
5278}
5279
5280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 * Show wildchar matches in the status line.
5282 * Show at least the "match" item.
5283 * We start at item 'first_match' in the list and show all matches that fit.
5284 *
5285 * If inversion is possible we use it. Else '=' characters are used.
5286 */
5287 void
5288win_redr_status_matches(xp, num_matches, matches, match, showtail)
5289 expand_T *xp;
5290 int num_matches;
5291 char_u **matches; /* list of matches */
5292 int match;
5293 int showtail;
5294{
5295#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5296 int row;
5297 char_u *buf;
5298 int len;
5299 int clen; /* lenght in screen cells */
5300 int fillchar;
5301 int attr;
5302 int i;
5303 int highlight = TRUE;
5304 char_u *selstart = NULL;
5305 int selstart_col = 0;
5306 char_u *selend = NULL;
5307 static int first_match = 0;
5308 int add_left = FALSE;
5309 char_u *s;
5310#ifdef FEAT_MENU
5311 int emenu;
5312#endif
5313#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5314 int l;
5315#endif
5316
5317 if (matches == NULL) /* interrupted completion? */
5318 return;
5319
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005320#ifdef FEAT_MBYTE
5321 if (has_mbyte)
5322 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5323 else
5324#endif
5325 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 if (buf == NULL)
5327 return;
5328
5329 if (match == -1) /* don't show match but original text */
5330 {
5331 match = 0;
5332 highlight = FALSE;
5333 }
5334 /* count 1 for the ending ">" */
5335 clen = status_match_len(xp, L_MATCH(match)) + 3;
5336 if (match == 0)
5337 first_match = 0;
5338 else if (match < first_match)
5339 {
5340 /* jumping left, as far as we can go */
5341 first_match = match;
5342 add_left = TRUE;
5343 }
5344 else
5345 {
5346 /* check if match fits on the screen */
5347 for (i = first_match; i < match; ++i)
5348 clen += status_match_len(xp, L_MATCH(i)) + 2;
5349 if (first_match > 0)
5350 clen += 2;
5351 /* jumping right, put match at the left */
5352 if ((long)clen > Columns)
5353 {
5354 first_match = match;
5355 /* if showing the last match, we can add some on the left */
5356 clen = 2;
5357 for (i = match; i < num_matches; ++i)
5358 {
5359 clen += status_match_len(xp, L_MATCH(i)) + 2;
5360 if ((long)clen >= Columns)
5361 break;
5362 }
5363 if (i == num_matches)
5364 add_left = TRUE;
5365 }
5366 }
5367 if (add_left)
5368 while (first_match > 0)
5369 {
5370 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5371 if ((long)clen >= Columns)
5372 break;
5373 --first_match;
5374 }
5375
5376 fillchar = fillchar_status(&attr, TRUE);
5377
5378 if (first_match == 0)
5379 {
5380 *buf = NUL;
5381 len = 0;
5382 }
5383 else
5384 {
5385 STRCPY(buf, "< ");
5386 len = 2;
5387 }
5388 clen = len;
5389
5390 i = first_match;
5391 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5392 {
5393 if (i == match)
5394 {
5395 selstart = buf + len;
5396 selstart_col = clen;
5397 }
5398
5399 s = L_MATCH(i);
5400 /* Check for menu separators - replace with '|' */
5401#ifdef FEAT_MENU
5402 emenu = (xp->xp_context == EXPAND_MENUS
5403 || xp->xp_context == EXPAND_MENUNAMES);
5404 if (emenu && menu_is_separator(s))
5405 {
5406 STRCPY(buf + len, transchar('|'));
5407 l = (int)STRLEN(buf + len);
5408 len += l;
5409 clen += l;
5410 }
5411 else
5412#endif
5413 for ( ; *s != NUL; ++s)
5414 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005415 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 ++s;
5417 clen += ptr2cells(s);
5418#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005419 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
5421 STRNCPY(buf + len, s, l);
5422 s += l - 1;
5423 len += l;
5424 }
5425 else
5426#endif
5427 {
5428 STRCPY(buf + len, transchar_byte(*s));
5429 len += (int)STRLEN(buf + len);
5430 }
5431 }
5432 if (i == match)
5433 selend = buf + len;
5434
5435 *(buf + len++) = ' ';
5436 *(buf + len++) = ' ';
5437 clen += 2;
5438 if (++i == num_matches)
5439 break;
5440 }
5441
5442 if (i != num_matches)
5443 {
5444 *(buf + len++) = '>';
5445 ++clen;
5446 }
5447
5448 buf[len] = NUL;
5449
5450 row = cmdline_row - 1;
5451 if (row >= 0)
5452 {
5453 if (wild_menu_showing == 0)
5454 {
5455 if (msg_scrolled > 0)
5456 {
5457 /* Put the wildmenu just above the command line. If there is
5458 * no room, scroll the screen one line up. */
5459 if (cmdline_row == Rows - 1)
5460 {
5461 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5462 ++msg_scrolled;
5463 }
5464 else
5465 {
5466 ++cmdline_row;
5467 ++row;
5468 }
5469 wild_menu_showing = WM_SCROLLED;
5470 }
5471 else
5472 {
5473 /* Create status line if needed by setting 'laststatus' to 2.
5474 * Set 'winminheight' to zero to avoid that the window is
5475 * resized. */
5476 if (lastwin->w_status_height == 0)
5477 {
5478 save_p_ls = p_ls;
5479 save_p_wmh = p_wmh;
5480 p_ls = 2;
5481 p_wmh = 0;
5482 last_status(FALSE);
5483 }
5484 wild_menu_showing = WM_SHOWN;
5485 }
5486 }
5487
5488 screen_puts(buf, row, 0, attr);
5489 if (selstart != NULL && highlight)
5490 {
5491 *selend = NUL;
5492 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5493 }
5494
5495 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5496 }
5497
5498#ifdef FEAT_VERTSPLIT
5499 win_redraw_last_status(topframe);
5500#else
5501 lastwin->w_redr_status = TRUE;
5502#endif
5503 vim_free(buf);
5504}
5505#endif
5506
5507#if defined(FEAT_WINDOWS) || defined(PROTO)
5508/*
5509 * Redraw the status line of window wp.
5510 *
5511 * If inversion is possible we use it. Else '=' characters are used.
5512 */
5513 void
5514win_redr_status(wp)
5515 win_T *wp;
5516{
5517 int row;
5518 char_u *p;
5519 int len;
5520 int fillchar;
5521 int attr;
5522 int this_ru_col;
5523
5524 wp->w_redr_status = FALSE;
5525 if (wp->w_status_height == 0)
5526 {
5527 /* no status line, can only be last window */
5528 redraw_cmdline = TRUE;
5529 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005530 else if (!redrawing()
5531#ifdef FEAT_INS_EXPAND
5532 /* don't update status line when popup menu is visible and may be
5533 * drawn over it */
5534 || pum_visible()
5535#endif
5536 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 /* Don't redraw right now, do it later. */
5539 wp->w_redr_status = TRUE;
5540 }
5541#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005542 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
5544 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005545 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 }
5547#endif
5548 else
5549 {
5550 fillchar = fillchar_status(&attr, wp == curwin);
5551
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005552 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 p = NameBuff;
5554 len = (int)STRLEN(p);
5555
5556 if (wp->w_buffer->b_help
5557#ifdef FEAT_QUICKFIX
5558 || wp->w_p_pvw
5559#endif
5560 || bufIsChanged(wp->w_buffer)
5561 || wp->w_buffer->b_p_ro)
5562 *(p + len++) = ' ';
5563 if (wp->w_buffer->b_help)
5564 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005565 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 len += (int)STRLEN(p + len);
5567 }
5568#ifdef FEAT_QUICKFIX
5569 if (wp->w_p_pvw)
5570 {
5571 STRCPY(p + len, _("[Preview]"));
5572 len += (int)STRLEN(p + len);
5573 }
5574#endif
5575 if (bufIsChanged(wp->w_buffer))
5576 {
5577 STRCPY(p + len, "[+]");
5578 len += 3;
5579 }
5580 if (wp->w_buffer->b_p_ro)
5581 {
5582 STRCPY(p + len, "[RO]");
5583 len += 4;
5584 }
5585
5586#ifndef FEAT_VERTSPLIT
5587 this_ru_col = ru_col;
5588 if (this_ru_col < (Columns + 1) / 2)
5589 this_ru_col = (Columns + 1) / 2;
5590#else
5591 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5592 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5593 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5594 if (this_ru_col <= 1)
5595 {
5596 p = (char_u *)"<"; /* No room for file name! */
5597 len = 1;
5598 }
5599 else
5600#endif
5601#ifdef FEAT_MBYTE
5602 if (has_mbyte)
5603 {
5604 int clen = 0, i;
5605
5606 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005607 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 clen += (*mb_ptr2cells)(p + i);
5609 /* Find first character that will fit.
5610 * Going from start to end is much faster for DBCS. */
5611 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005612 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 clen -= (*mb_ptr2cells)(p + i);
5614 len = clen;
5615 if (i > 0)
5616 {
5617 p = p + i - 1;
5618 *p = '<';
5619 ++len;
5620 }
5621
5622 }
5623 else
5624#endif
5625 if (len > this_ru_col - 1)
5626 {
5627 p += len - (this_ru_col - 1);
5628 *p = '<';
5629 len = this_ru_col - 1;
5630 }
5631
5632 row = W_WINROW(wp) + wp->w_height;
5633 screen_puts(p, row, W_WINCOL(wp), attr);
5634 screen_fill(row, row + 1, len + W_WINCOL(wp),
5635 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5636
5637 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5638 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5639 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5640 - 1 + W_WINCOL(wp)), attr);
5641
5642#ifdef FEAT_CMDL_INFO
5643 win_redr_ruler(wp, TRUE);
5644#endif
5645 }
5646
5647#ifdef FEAT_VERTSPLIT
5648 /*
5649 * May need to draw the character below the vertical separator.
5650 */
5651 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5652 {
5653 if (stl_connected(wp))
5654 fillchar = fillchar_status(&attr, wp == curwin);
5655 else
5656 fillchar = fillchar_vsep(&attr);
5657 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5658 attr);
5659 }
5660#endif
5661}
5662
Bram Moolenaar238a5642006-02-21 22:12:05 +00005663#ifdef FEAT_STL_OPT
5664/*
5665 * Redraw the status line according to 'statusline' and take care of any
5666 * errors encountered.
5667 */
5668 static void
5669redraw_custum_statusline(wp)
5670 win_T *wp;
5671{
5672 int save_called_emsg = called_emsg;
5673
5674 called_emsg = FALSE;
5675 win_redr_custom(wp, FALSE);
5676 if (called_emsg)
5677 set_string_option_direct((char_u *)"statusline", -1,
5678 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005679 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005680 called_emsg |= save_called_emsg;
5681}
5682#endif
5683
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684# ifdef FEAT_VERTSPLIT
5685/*
5686 * Return TRUE if the status line of window "wp" is connected to the status
5687 * line of the window right of it. If not, then it's a vertical separator.
5688 * Only call if (wp->w_vsep_width != 0).
5689 */
5690 int
5691stl_connected(wp)
5692 win_T *wp;
5693{
5694 frame_T *fr;
5695
5696 fr = wp->w_frame;
5697 while (fr->fr_parent != NULL)
5698 {
5699 if (fr->fr_parent->fr_layout == FR_COL)
5700 {
5701 if (fr->fr_next != NULL)
5702 break;
5703 }
5704 else
5705 {
5706 if (fr->fr_next != NULL)
5707 return TRUE;
5708 }
5709 fr = fr->fr_parent;
5710 }
5711 return FALSE;
5712}
5713# endif
5714
5715#endif /* FEAT_WINDOWS */
5716
5717#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5718/*
5719 * Get the value to show for the language mappings, active 'keymap'.
5720 */
5721 int
5722get_keymap_str(wp, buf, len)
5723 win_T *wp;
5724 char_u *buf; /* buffer for the result */
5725 int len; /* length of buffer */
5726{
5727 char_u *p;
5728
5729 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5730 return FALSE;
5731
5732 {
5733#ifdef FEAT_EVAL
5734 buf_T *old_curbuf = curbuf;
5735 win_T *old_curwin = curwin;
5736 char_u *s;
5737
5738 curbuf = wp->w_buffer;
5739 curwin = wp;
5740 STRCPY(buf, "b:keymap_name"); /* must be writable */
5741 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005742 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 --emsg_skip;
5744 curbuf = old_curbuf;
5745 curwin = old_curwin;
5746 if (p == NULL || *p == NUL)
5747#endif
5748 {
5749#ifdef FEAT_KEYMAP
5750 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5751 p = wp->w_buffer->b_p_keymap;
5752 else
5753#endif
5754 p = (char_u *)"lang";
5755 }
5756 if ((int)(STRLEN(p) + 3) < len)
5757 sprintf((char *)buf, "<%s>", p);
5758 else
5759 buf[0] = NUL;
5760#ifdef FEAT_EVAL
5761 vim_free(s);
5762#endif
5763 }
5764 return buf[0] != NUL;
5765}
5766#endif
5767
5768#if defined(FEAT_STL_OPT) || defined(PROTO)
5769/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005770 * Redraw the status line or ruler of window "wp".
5771 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 */
5773 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005774win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005776 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
5778 int attr;
5779 int curattr;
5780 int row;
5781 int col = 0;
5782 int maxwidth;
5783 int width;
5784 int n;
5785 int len;
5786 int fillchar;
5787 char_u buf[MAXPATHL];
5788 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005789 struct stl_hlrec hltab[STL_MAX_ITEM];
5790 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005791 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
5793 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005794 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005796 /* Use 'tabline'. Always at the first line of the screen. */
5797 p = p_tal;
5798 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005799 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005800 attr = hl_attr(HLF_TPF);
5801 maxwidth = Columns;
5802# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005803 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005806 else
5807 {
5808 row = W_WINROW(wp) + wp->w_height;
5809 fillchar = fillchar_status(&attr, wp == curwin);
5810 maxwidth = W_WIDTH(wp);
5811
5812 if (draw_ruler)
5813 {
5814 p = p_ruf;
5815 /* advance past any leading group spec - implicit in ru_col */
5816 if (*p == '%')
5817 {
5818 if (*++p == '-')
5819 p++;
5820 if (atoi((char *) p))
5821 while (VIM_ISDIGIT(*p))
5822 p++;
5823 if (*p++ != '(')
5824 p = p_ruf;
5825 }
5826#ifdef FEAT_VERTSPLIT
5827 col = ru_col - (Columns - W_WIDTH(wp));
5828 if (col < (W_WIDTH(wp) + 1) / 2)
5829 col = (W_WIDTH(wp) + 1) / 2;
5830#else
5831 col = ru_col;
5832 if (col > (Columns + 1) / 2)
5833 col = (Columns + 1) / 2;
5834#endif
5835 maxwidth = W_WIDTH(wp) - col;
5836#ifdef FEAT_WINDOWS
5837 if (!wp->w_status_height)
5838#endif
5839 {
5840 row = Rows - 1;
5841 --maxwidth; /* writing in last column may cause scrolling */
5842 fillchar = ' ';
5843 attr = 0;
5844 }
5845
5846# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005847 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005848# endif
5849 }
5850 else
5851 {
5852 if (*wp->w_p_stl != NUL)
5853 p = wp->w_p_stl;
5854 else
5855 p = p_stl;
5856# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005857 use_sandbox = was_set_insecurely((char_u *)"statusline",
5858 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005859# endif
5860 }
5861
5862#ifdef FEAT_VERTSPLIT
5863 col += W_WINCOL(wp);
5864#endif
5865 }
5866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 if (maxwidth <= 0)
5868 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005870 width = build_stl_str_hl(wp == NULL ? curwin : wp,
5871 buf, sizeof(buf),
5872 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005873 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005874 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876 while (width < maxwidth && len < sizeof(buf) - 1)
5877 {
5878#ifdef FEAT_MBYTE
5879 len += (*mb_char2bytes)(fillchar, buf + len);
5880#else
5881 buf[len++] = fillchar;
5882#endif
5883 ++width;
5884 }
5885 buf[len] = NUL;
5886
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005887 /*
5888 * Draw each snippet with the specified highlighting.
5889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 curattr = attr;
5891 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005892 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005894 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 screen_puts_len(p, len, row, col, curattr);
5896 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005897 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005899 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005901 else if (hltab[n].userhl < 0)
5902 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00005904 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005905 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#endif
5907 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005908 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
5910 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005911
5912 if (wp == NULL)
5913 {
5914 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
5915 col = 0;
5916 len = 0;
5917 p = buf;
5918 fillchar = 0;
5919 for (n = 0; tabtab[n].start != NULL; n++)
5920 {
5921 len += vim_strnsize(p, (int)(tabtab[n].start - p));
5922 while (col < len)
5923 TabPageIdxs[col++] = fillchar;
5924 p = tabtab[n].start;
5925 fillchar = tabtab[n].userhl;
5926 }
5927 while (col < Columns)
5928 TabPageIdxs[col++] = fillchar;
5929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930}
5931
5932#endif /* FEAT_STL_OPT */
5933
5934/*
5935 * Output a single character directly to the screen and update ScreenLines.
5936 */
5937 void
5938screen_putchar(c, row, col, attr)
5939 int c;
5940 int row, col;
5941 int attr;
5942{
5943#ifdef FEAT_MBYTE
5944 char_u buf[MB_MAXBYTES + 1];
5945
5946 buf[(*mb_char2bytes)(c, buf)] = NUL;
5947#else
5948 char_u buf[2];
5949
5950 buf[0] = c;
5951 buf[1] = NUL;
5952#endif
5953 screen_puts(buf, row, col, attr);
5954}
5955
5956/*
5957 * Get a single character directly from ScreenLines into "bytes[]".
5958 * Also return its attribute in *attrp;
5959 */
5960 void
5961screen_getbytes(row, col, bytes, attrp)
5962 int row, col;
5963 char_u *bytes;
5964 int *attrp;
5965{
5966 unsigned off;
5967
5968 /* safety check */
5969 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
5970 {
5971 off = LineOffset[row] + col;
5972 *attrp = ScreenAttrs[off];
5973 bytes[0] = ScreenLines[off];
5974 bytes[1] = NUL;
5975
5976#ifdef FEAT_MBYTE
5977 if (enc_utf8 && ScreenLinesUC[off] != 0)
5978 bytes[utfc_char2bytes(off, bytes)] = NUL;
5979 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
5980 {
5981 bytes[0] = ScreenLines[off];
5982 bytes[1] = ScreenLines2[off];
5983 bytes[2] = NUL;
5984 }
5985 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
5986 {
5987 bytes[1] = ScreenLines[off + 1];
5988 bytes[2] = NUL;
5989 }
5990#endif
5991 }
5992}
5993
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005994#ifdef FEAT_MBYTE
5995static int screen_comp_differs __ARGS((int, int*));
5996
5997/*
5998 * Return TRUE if composing characters for screen posn "off" differs from
5999 * composing characters in "u8cc".
6000 */
6001 static int
6002screen_comp_differs(off, u8cc)
6003 int off;
6004 int *u8cc;
6005{
6006 int i;
6007
6008 for (i = 0; i < Screen_mco; ++i)
6009 {
6010 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6011 return TRUE;
6012 if (u8cc[i] == 0)
6013 break;
6014 }
6015 return FALSE;
6016}
6017#endif
6018
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019/*
6020 * Put string '*text' on the screen at position 'row' and 'col', with
6021 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6022 * Note: only outputs within one row, message is truncated at screen boundary!
6023 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6024 */
6025 void
6026screen_puts(text, row, col, attr)
6027 char_u *text;
6028 int row;
6029 int col;
6030 int attr;
6031{
6032 screen_puts_len(text, -1, row, col, attr);
6033}
6034
6035/*
6036 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6037 * a NUL.
6038 */
6039 void
6040screen_puts_len(text, len, row, col, attr)
6041 char_u *text;
6042 int len;
6043 int row;
6044 int col;
6045 int attr;
6046{
6047 unsigned off;
6048 char_u *ptr = text;
6049 int c;
6050#ifdef FEAT_MBYTE
6051 int mbyte_blen = 1;
6052 int mbyte_cells = 1;
6053 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006054 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 int clear_next_cell = FALSE;
6056# ifdef FEAT_ARABIC
6057 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006058 int pc, nc, nc1;
6059 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060# endif
6061#endif
6062
6063 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6064 return;
6065
6066 off = LineOffset[row] + col;
6067 while (*ptr != NUL && col < screen_Columns
6068 && (len < 0 || (int)(ptr - text) < len))
6069 {
6070 c = *ptr;
6071#ifdef FEAT_MBYTE
6072 /* check if this is the first byte of a multibyte */
6073 if (has_mbyte)
6074 {
6075 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006076 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006078 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6080 mbyte_cells = 1;
6081 else if (enc_dbcs != 0)
6082 mbyte_cells = mbyte_blen;
6083 else /* enc_utf8 */
6084 {
6085 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006086 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 (int)((text + len) - ptr));
6088 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006089 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 mbyte_cells = utf_char2cells(u8c);
6091 /* Non-BMP character: display as ? or fullwidth ?. */
6092 if (u8c >= 0x10000)
6093 {
6094 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6095 if (attr == 0)
6096 attr = hl_attr(HLF_8);
6097 }
6098# ifdef FEAT_ARABIC
6099 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6100 {
6101 /* Do Arabic shaping. */
6102 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6103 {
6104 /* Past end of string to be displayed. */
6105 nc = NUL;
6106 nc1 = NUL;
6107 }
6108 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006109 {
6110 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6111 nc1 = pcc[0];
6112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 pc = prev_c;
6114 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006115 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 }
6117 else
6118 prev_c = u8c;
6119# endif
6120 }
6121 }
6122#endif
6123
6124 if (ScreenLines[off] != c
6125#ifdef FEAT_MBYTE
6126 || (mbyte_cells == 2
6127 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6128 || (enc_dbcs == DBCS_JPNU
6129 && c == 0x8e
6130 && ScreenLines2[off] != ptr[1])
6131 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006132 && (ScreenLinesUC[off] != (u8char_T)u8c
6133 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134#endif
6135 || ScreenAttrs[off] != attr
6136 || exmode_active
6137 )
6138 {
6139#if defined(FEAT_GUI) || defined(UNIX)
6140 /* The bold trick makes a single row of pixels appear in the next
6141 * character. When a bold character is removed, the next
6142 * character should be redrawn too. This happens for our own GUI
6143 * and for some xterms.
6144 * Force the redraw by setting the attribute to a different value
6145 * than "attr", the contents of ScreenLines[] may be needed by
6146 * mb_off2cells() further on.
6147 * Don't do this for the last drawn character, because the next
6148 * character may not be redrawn. */
6149 if (
6150# ifdef FEAT_GUI
6151 gui.in_use
6152# endif
6153# if defined(FEAT_GUI) && defined(UNIX)
6154 ||
6155# endif
6156# ifdef UNIX
6157 term_is_xterm
6158# endif
6159 )
6160 {
6161 int n;
6162
6163 n = ScreenAttrs[off];
6164# ifdef FEAT_MBYTE
6165 if (col + mbyte_cells < screen_Columns
6166 && (n > HL_ALL || (n & HL_BOLD))
6167 && (len < 0 ? ptr[mbyte_blen] != NUL
6168 : ptr + mbyte_blen < text + len))
6169 ScreenAttrs[off + mbyte_cells] = attr + 1;
6170# else
6171 if (col + 1 < screen_Columns
6172 && (n > HL_ALL || (n & HL_BOLD))
6173 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6174 ScreenLines[off + 1] = 0;
6175# endif
6176 }
6177#endif
6178#ifdef FEAT_MBYTE
6179 /* When at the end of the text and overwriting a two-cell
6180 * character with a one-cell character, need to clear the next
6181 * cell. Also when overwriting the left halve of a two-cell char
6182 * with the right halve of a two-cell char. Do this only once
6183 * (mb_off2cells() may return 2 on the right halve). */
6184 if (clear_next_cell)
6185 clear_next_cell = FALSE;
6186 else if (has_mbyte
6187 && (len < 0 ? ptr[mbyte_blen] == NUL
6188 : ptr + mbyte_blen >= text + len)
6189 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6190 || (mbyte_cells == 2
6191 && (*mb_off2cells)(off) == 1
6192 && (*mb_off2cells)(off + 1) > 1)))
6193 clear_next_cell = TRUE;
6194
6195 /* Make sure we never leave a second byte of a double-byte behind,
6196 * it confuses mb_off2cells(). */
6197 if (enc_dbcs
6198 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6199 || (mbyte_cells == 2
6200 && (*mb_off2cells)(off) == 1
6201 && (*mb_off2cells)(off + 1) > 1)))
6202 ScreenLines[off + mbyte_blen] = 0;
6203#endif
6204 ScreenLines[off] = c;
6205 ScreenAttrs[off] = attr;
6206#ifdef FEAT_MBYTE
6207 if (enc_utf8)
6208 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006209 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 ScreenLinesUC[off] = 0;
6211 else
6212 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006213 int i;
6214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006216 for (i = 0; i < Screen_mco; ++i)
6217 {
6218 ScreenLinesC[i][off] = u8cc[i];
6219 if (u8cc[i] == 0)
6220 break;
6221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 }
6223 if (mbyte_cells == 2)
6224 {
6225 ScreenLines[off + 1] = 0;
6226 ScreenAttrs[off + 1] = attr;
6227 }
6228 screen_char(off, row, col);
6229 }
6230 else if (mbyte_cells == 2)
6231 {
6232 ScreenLines[off + 1] = ptr[1];
6233 ScreenAttrs[off + 1] = attr;
6234 screen_char_2(off, row, col);
6235 }
6236 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6237 {
6238 ScreenLines2[off] = ptr[1];
6239 screen_char(off, row, col);
6240 }
6241 else
6242#endif
6243 screen_char(off, row, col);
6244 }
6245#ifdef FEAT_MBYTE
6246 if (has_mbyte)
6247 {
6248 off += mbyte_cells;
6249 col += mbyte_cells;
6250 ptr += mbyte_blen;
6251 if (clear_next_cell)
6252 ptr = (char_u *)" ";
6253 }
6254 else
6255#endif
6256 {
6257 ++off;
6258 ++col;
6259 ++ptr;
6260 }
6261 }
6262}
6263
6264#ifdef FEAT_SEARCH_EXTRA
6265/*
6266 * Prepare for 'searchhl' highlighting.
6267 */
6268 static void
6269start_search_hl()
6270{
6271 if (p_hls && !no_hlsearch)
6272 {
6273 last_pat_prog(&search_hl.rm);
6274 search_hl.attr = hl_attr(HLF_L);
6275 }
6276}
6277
6278/*
6279 * Clean up for 'searchhl' highlighting.
6280 */
6281 static void
6282end_search_hl()
6283{
6284 if (search_hl.rm.regprog != NULL)
6285 {
6286 vim_free(search_hl.rm.regprog);
6287 search_hl.rm.regprog = NULL;
6288 }
6289}
6290
6291/*
6292 * Advance to the match in window "wp" line "lnum" or past it.
6293 */
6294 static void
6295prepare_search_hl(wp, lnum)
6296 win_T *wp;
6297 linenr_T lnum;
6298{
6299 match_T *shl; /* points to search_hl or match_hl */
6300 int n;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006301 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303 /*
6304 * When using a multi-line pattern, start searching at the top
6305 * of the window or just after a closed fold.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006306 * Do this both for search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006308 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006310 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (shl->rm.regprog != NULL
6312 && shl->lnum == 0
6313 && re_multiline(shl->rm.regprog))
6314 {
6315 if (shl->first_lnum == 0)
6316 {
6317# ifdef FEAT_FOLDING
6318 for (shl->first_lnum = lnum;
6319 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6320 if (hasFoldingWin(wp, shl->first_lnum - 1,
6321 NULL, NULL, TRUE, NULL))
6322 break;
6323# else
6324 shl->first_lnum = wp->w_topline;
6325# endif
6326 }
6327 n = 0;
6328 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6329 {
6330 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6331 if (shl->lnum != 0)
6332 {
6333 shl->first_lnum = shl->lnum
6334 + shl->rm.endpos[0].lnum
6335 - shl->rm.startpos[0].lnum;
6336 n = shl->rm.endpos[0].col;
6337 }
6338 else
6339 {
6340 ++shl->first_lnum;
6341 n = 0;
6342 }
6343 }
6344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 }
6346}
6347
6348/*
6349 * Search for a next 'searchl' or ":match" match.
6350 * Uses shl->buf.
6351 * Sets shl->lnum and shl->rm contents.
6352 * Note: Assumes a previous match is always before "lnum", unless
6353 * shl->lnum is zero.
6354 * Careful: Any pointers for buffer lines will become invalid.
6355 */
6356 static void
6357next_search_hl(win, shl, lnum, mincol)
6358 win_T *win;
6359 match_T *shl; /* points to search_hl or match_hl */
6360 linenr_T lnum;
6361 colnr_T mincol; /* minimal column for a match */
6362{
6363 linenr_T l;
6364 colnr_T matchcol;
6365 long nmatched;
6366
6367 if (shl->lnum != 0)
6368 {
6369 /* Check for three situations:
6370 * 1. If the "lnum" is below a previous match, start a new search.
6371 * 2. If the previous match includes "mincol", use it.
6372 * 3. Continue after the previous match.
6373 */
6374 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6375 if (lnum > l)
6376 shl->lnum = 0;
6377 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6378 return;
6379 }
6380
6381 /*
6382 * Repeat searching for a match until one is found that includes "mincol"
6383 * or none is found in this line.
6384 */
6385 called_emsg = FALSE;
6386 for (;;)
6387 {
6388 /* Three situations:
6389 * 1. No useful previous match: search from start of line.
6390 * 2. Not Vi compatible or empty match: continue at next character.
6391 * Break the loop if this is beyond the end of the line.
6392 * 3. Vi compatible searching: continue at end of previous match.
6393 */
6394 if (shl->lnum == 0)
6395 matchcol = 0;
6396 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6397 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006398 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006400 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006401
6402 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006403 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006404 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006406 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 shl->lnum = 0;
6408 break;
6409 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006410#ifdef FEAT_MBYTE
6411 if (has_mbyte)
6412 matchcol += mb_ptr2len(ml);
6413 else
6414#endif
6415 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
6417 else
6418 matchcol = shl->rm.endpos[0].col;
6419
6420 shl->lnum = lnum;
6421 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6422 if (called_emsg)
6423 {
6424 /* Error while handling regexp: stop using this regexp. */
6425 vim_free(shl->rm.regprog);
6426 shl->rm.regprog = NULL;
6427 no_hlsearch = TRUE;
6428 break;
6429 }
6430 if (nmatched == 0)
6431 {
6432 shl->lnum = 0; /* no match found */
6433 break;
6434 }
6435 if (shl->rm.startpos[0].lnum > 0
6436 || shl->rm.startpos[0].col >= mincol
6437 || nmatched > 1
6438 || shl->rm.endpos[0].col > mincol)
6439 {
6440 shl->lnum += shl->rm.startpos[0].lnum;
6441 break; /* useful match found */
6442 }
6443 }
6444}
6445#endif
6446
6447 static void
6448screen_start_highlight(attr)
6449 int attr;
6450{
6451 attrentry_T *aep = NULL;
6452
6453 screen_attr = attr;
6454 if (full_screen
6455#ifdef WIN3264
6456 && termcap_active
6457#endif
6458 )
6459 {
6460#ifdef FEAT_GUI
6461 if (gui.in_use)
6462 {
6463 char buf[20];
6464
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006465 /* The GUI handles this internally. */
6466 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 OUT_STR(buf);
6468 }
6469 else
6470#endif
6471 {
6472 if (attr > HL_ALL) /* special HL attr. */
6473 {
6474 if (t_colors > 1)
6475 aep = syn_cterm_attr2entry(attr);
6476 else
6477 aep = syn_term_attr2entry(attr);
6478 if (aep == NULL) /* did ":syntax clear" */
6479 attr = 0;
6480 else
6481 attr = aep->ae_attr;
6482 }
6483 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6484 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006485 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6486 && cterm_normal_fg_bold)
6487 /* If the Normal FG color has BOLD attribute and the new HL
6488 * has a FG color defined, clear BOLD. */
6489 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6491 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006492 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6493 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 out_str(T_US);
6495 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6496 out_str(T_CZH);
6497 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6498 out_str(T_MR);
6499
6500 /*
6501 * Output the color or start string after bold etc., in case the
6502 * bold etc. override the color setting.
6503 */
6504 if (aep != NULL)
6505 {
6506 if (t_colors > 1)
6507 {
6508 if (aep->ae_u.cterm.fg_color)
6509 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6510 if (aep->ae_u.cterm.bg_color)
6511 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6512 }
6513 else
6514 {
6515 if (aep->ae_u.term.start != NULL)
6516 out_str(aep->ae_u.term.start);
6517 }
6518 }
6519 }
6520 }
6521}
6522
6523 void
6524screen_stop_highlight()
6525{
6526 int do_ME = FALSE; /* output T_ME code */
6527
6528 if (screen_attr != 0
6529#ifdef WIN3264
6530 && termcap_active
6531#endif
6532 )
6533 {
6534#ifdef FEAT_GUI
6535 if (gui.in_use)
6536 {
6537 char buf[20];
6538
6539 /* use internal GUI code */
6540 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6541 OUT_STR(buf);
6542 }
6543 else
6544#endif
6545 {
6546 if (screen_attr > HL_ALL) /* special HL attr. */
6547 {
6548 attrentry_T *aep;
6549
6550 if (t_colors > 1)
6551 {
6552 /*
6553 * Assume that t_me restores the original colors!
6554 */
6555 aep = syn_cterm_attr2entry(screen_attr);
6556 if (aep != NULL && (aep->ae_u.cterm.fg_color
6557 || aep->ae_u.cterm.bg_color))
6558 do_ME = TRUE;
6559 }
6560 else
6561 {
6562 aep = syn_term_attr2entry(screen_attr);
6563 if (aep != NULL && aep->ae_u.term.stop != NULL)
6564 {
6565 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6566 do_ME = TRUE;
6567 else
6568 out_str(aep->ae_u.term.stop);
6569 }
6570 }
6571 if (aep == NULL) /* did ":syntax clear" */
6572 screen_attr = 0;
6573 else
6574 screen_attr = aep->ae_attr;
6575 }
6576
6577 /*
6578 * Often all ending-codes are equal to T_ME. Avoid outputting the
6579 * same sequence several times.
6580 */
6581 if (screen_attr & HL_STANDOUT)
6582 {
6583 if (STRCMP(T_SE, T_ME) == 0)
6584 do_ME = TRUE;
6585 else
6586 out_str(T_SE);
6587 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006588 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {
6590 if (STRCMP(T_UE, T_ME) == 0)
6591 do_ME = TRUE;
6592 else
6593 out_str(T_UE);
6594 }
6595 if (screen_attr & HL_ITALIC)
6596 {
6597 if (STRCMP(T_CZR, T_ME) == 0)
6598 do_ME = TRUE;
6599 else
6600 out_str(T_CZR);
6601 }
6602 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6603 out_str(T_ME);
6604
6605 if (t_colors > 1)
6606 {
6607 /* set Normal cterm colors */
6608 if (cterm_normal_fg_color != 0)
6609 term_fg_color(cterm_normal_fg_color - 1);
6610 if (cterm_normal_bg_color != 0)
6611 term_bg_color(cterm_normal_bg_color - 1);
6612 if (cterm_normal_fg_bold)
6613 out_str(T_MD);
6614 }
6615 }
6616 }
6617 screen_attr = 0;
6618}
6619
6620/*
6621 * Reset the colors for a cterm. Used when leaving Vim.
6622 * The machine specific code may override this again.
6623 */
6624 void
6625reset_cterm_colors()
6626{
6627 if (t_colors > 1)
6628 {
6629 /* set Normal cterm colors */
6630 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6631 {
6632 out_str(T_OP);
6633 screen_attr = -1;
6634 }
6635 if (cterm_normal_fg_bold)
6636 {
6637 out_str(T_ME);
6638 screen_attr = -1;
6639 }
6640 }
6641}
6642
6643/*
6644 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6645 * using the attributes from ScreenAttrs["off"].
6646 */
6647 static void
6648screen_char(off, row, col)
6649 unsigned off;
6650 int row;
6651 int col;
6652{
6653 int attr;
6654
6655 /* Check for illegal values, just in case (could happen just after
6656 * resizing). */
6657 if (row >= screen_Rows || col >= screen_Columns)
6658 return;
6659
6660 /* Outputting the last character on the screen may scrollup the screen.
6661 * Don't to it! Mark the character invalid (update it when scrolled up) */
6662 if (row == screen_Rows - 1 && col == screen_Columns - 1
6663#ifdef FEAT_RIGHTLEFT
6664 /* account for first command-line character in rightleft mode */
6665 && !cmdmsg_rl
6666#endif
6667 )
6668 {
6669 ScreenAttrs[off] = (sattr_T)-1;
6670 return;
6671 }
6672
6673 /*
6674 * Stop highlighting first, so it's easier to move the cursor.
6675 */
6676#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6677 if (screen_char_attr != 0)
6678 attr = screen_char_attr;
6679 else
6680#endif
6681 attr = ScreenAttrs[off];
6682 if (screen_attr != attr)
6683 screen_stop_highlight();
6684
6685 windgoto(row, col);
6686
6687 if (screen_attr != attr)
6688 screen_start_highlight(attr);
6689
6690#ifdef FEAT_MBYTE
6691 if (enc_utf8 && ScreenLinesUC[off] != 0)
6692 {
6693 char_u buf[MB_MAXBYTES + 1];
6694
6695 /* Convert UTF-8 character to bytes and write it. */
6696
6697 buf[utfc_char2bytes(off, buf)] = NUL;
6698
6699 out_str(buf);
6700 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6701 ++screen_cur_col;
6702 }
6703 else
6704#endif
6705 {
6706#ifdef FEAT_MBYTE
6707 out_flush_check();
6708#endif
6709 out_char(ScreenLines[off]);
6710#ifdef FEAT_MBYTE
6711 /* double-byte character in single-width cell */
6712 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6713 out_char(ScreenLines2[off]);
6714#endif
6715 }
6716
6717 screen_cur_col++;
6718}
6719
6720#ifdef FEAT_MBYTE
6721
6722/*
6723 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6724 * on the screen at position 'row' and 'col'.
6725 * The attributes of the first byte is used for all. This is required to
6726 * output the two bytes of a double-byte character with nothing in between.
6727 */
6728 static void
6729screen_char_2(off, row, col)
6730 unsigned off;
6731 int row;
6732 int col;
6733{
6734 /* Check for illegal values (could be wrong when screen was resized). */
6735 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6736 return;
6737
6738 /* Outputting the last character on the screen may scrollup the screen.
6739 * Don't to it! Mark the character invalid (update it when scrolled up) */
6740 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6741 {
6742 ScreenAttrs[off] = (sattr_T)-1;
6743 return;
6744 }
6745
6746 /* Output the first byte normally (positions the cursor), then write the
6747 * second byte directly. */
6748 screen_char(off, row, col);
6749 out_char(ScreenLines[off + 1]);
6750 ++screen_cur_col;
6751}
6752#endif
6753
6754#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6755/*
6756 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6757 * This uses the contents of ScreenLines[] and doesn't change it.
6758 */
6759 void
6760screen_draw_rectangle(row, col, height, width, invert)
6761 int row;
6762 int col;
6763 int height;
6764 int width;
6765 int invert;
6766{
6767 int r, c;
6768 int off;
6769
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006770 /* Can't use ScreenLines unless initialized */
6771 if (ScreenLines == NULL)
6772 return;
6773
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 if (invert)
6775 screen_char_attr = HL_INVERSE;
6776 for (r = row; r < row + height; ++r)
6777 {
6778 off = LineOffset[r];
6779 for (c = col; c < col + width; ++c)
6780 {
6781#ifdef FEAT_MBYTE
6782 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6783 {
6784 screen_char_2(off + c, r, c);
6785 ++c;
6786 }
6787 else
6788#endif
6789 {
6790 screen_char(off + c, r, c);
6791#ifdef FEAT_MBYTE
6792 if (utf_off2cells(off + c) > 1)
6793 ++c;
6794#endif
6795 }
6796 }
6797 }
6798 screen_char_attr = 0;
6799}
6800#endif
6801
6802#ifdef FEAT_VERTSPLIT
6803/*
6804 * Redraw the characters for a vertically split window.
6805 */
6806 static void
6807redraw_block(row, end, wp)
6808 int row;
6809 int end;
6810 win_T *wp;
6811{
6812 int col;
6813 int width;
6814
6815# ifdef FEAT_CLIPBOARD
6816 clip_may_clear_selection(row, end - 1);
6817# endif
6818
6819 if (wp == NULL)
6820 {
6821 col = 0;
6822 width = Columns;
6823 }
6824 else
6825 {
6826 col = wp->w_wincol;
6827 width = wp->w_width;
6828 }
6829 screen_draw_rectangle(row, col, end - row, width, FALSE);
6830}
6831#endif
6832
6833/*
6834 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6835 * with character 'c1' in first column followed by 'c2' in the other columns.
6836 * Use attributes 'attr'.
6837 */
6838 void
6839screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6840 int start_row, end_row;
6841 int start_col, end_col;
6842 int c1, c2;
6843 int attr;
6844{
6845 int row;
6846 int col;
6847 int off;
6848 int end_off;
6849 int did_delete;
6850 int c;
6851 int norm_term;
6852#if defined(FEAT_GUI) || defined(UNIX)
6853 int force_next = FALSE;
6854#endif
6855
6856 if (end_row > screen_Rows) /* safety check */
6857 end_row = screen_Rows;
6858 if (end_col > screen_Columns) /* safety check */
6859 end_col = screen_Columns;
6860 if (ScreenLines == NULL
6861 || start_row >= end_row
6862 || start_col >= end_col) /* nothing to do */
6863 return;
6864
6865 /* it's a "normal" terminal when not in a GUI or cterm */
6866 norm_term = (
6867#ifdef FEAT_GUI
6868 !gui.in_use &&
6869#endif
6870 t_colors <= 1);
6871 for (row = start_row; row < end_row; ++row)
6872 {
6873 /*
6874 * Try to use delete-line termcap code, when no attributes or in a
6875 * "normal" terminal, where a bold/italic space is just a
6876 * space.
6877 */
6878 did_delete = FALSE;
6879 if (c2 == ' '
6880 && end_col == Columns
6881 && can_clear(T_CE)
6882 && (attr == 0
6883 || (norm_term
6884 && attr <= HL_ALL
6885 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6886 {
6887 /*
6888 * check if we really need to clear something
6889 */
6890 col = start_col;
6891 if (c1 != ' ') /* don't clear first char */
6892 ++col;
6893
6894 off = LineOffset[row] + col;
6895 end_off = LineOffset[row] + end_col;
6896
6897 /* skip blanks (used often, keep it fast!) */
6898#ifdef FEAT_MBYTE
6899 if (enc_utf8)
6900 while (off < end_off && ScreenLines[off] == ' '
6901 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6902 ++off;
6903 else
6904#endif
6905 while (off < end_off && ScreenLines[off] == ' '
6906 && ScreenAttrs[off] == 0)
6907 ++off;
6908 if (off < end_off) /* something to be cleared */
6909 {
6910 col = off - LineOffset[row];
6911 screen_stop_highlight();
6912 term_windgoto(row, col);/* clear rest of this screen line */
6913 out_str(T_CE);
6914 screen_start(); /* don't know where cursor is now */
6915 col = end_col - col;
6916 while (col--) /* clear chars in ScreenLines */
6917 {
6918 ScreenLines[off] = ' ';
6919#ifdef FEAT_MBYTE
6920 if (enc_utf8)
6921 ScreenLinesUC[off] = 0;
6922#endif
6923 ScreenAttrs[off] = 0;
6924 ++off;
6925 }
6926 }
6927 did_delete = TRUE; /* the chars are cleared now */
6928 }
6929
6930 off = LineOffset[row] + start_col;
6931 c = c1;
6932 for (col = start_col; col < end_col; ++col)
6933 {
6934 if (ScreenLines[off] != c
6935#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006936 || (enc_utf8 && (int)ScreenLinesUC[off]
6937 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938#endif
6939 || ScreenAttrs[off] != attr
6940#if defined(FEAT_GUI) || defined(UNIX)
6941 || force_next
6942#endif
6943 )
6944 {
6945#if defined(FEAT_GUI) || defined(UNIX)
6946 /* The bold trick may make a single row of pixels appear in
6947 * the next character. When a bold character is removed, the
6948 * next character should be redrawn too. This happens for our
6949 * own GUI and for some xterms. */
6950 if (
6951# ifdef FEAT_GUI
6952 gui.in_use
6953# endif
6954# if defined(FEAT_GUI) && defined(UNIX)
6955 ||
6956# endif
6957# ifdef UNIX
6958 term_is_xterm
6959# endif
6960 )
6961 {
6962 if (ScreenLines[off] != ' '
6963 && (ScreenAttrs[off] > HL_ALL
6964 || ScreenAttrs[off] & HL_BOLD))
6965 force_next = TRUE;
6966 else
6967 force_next = FALSE;
6968 }
6969#endif
6970 ScreenLines[off] = c;
6971#ifdef FEAT_MBYTE
6972 if (enc_utf8)
6973 {
6974 if (c >= 0x80)
6975 {
6976 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006977 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 }
6979 else
6980 ScreenLinesUC[off] = 0;
6981 }
6982#endif
6983 ScreenAttrs[off] = attr;
6984 if (!did_delete || c != ' ')
6985 screen_char(off, row, col);
6986 }
6987 ++off;
6988 if (col == start_col)
6989 {
6990 if (did_delete)
6991 break;
6992 c = c2;
6993 }
6994 }
6995 if (end_col == Columns)
6996 LineWraps[row] = FALSE;
6997 if (row == Rows - 1) /* overwritten the command line */
6998 {
6999 redraw_cmdline = TRUE;
7000 if (c1 == ' ' && c2 == ' ')
7001 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007002 if (start_col == 0)
7003 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 }
7005 }
7006}
7007
7008/*
7009 * Check if there should be a delay. Used before clearing or redrawing the
7010 * screen or the command line.
7011 */
7012 void
7013check_for_delay(check_msg_scroll)
7014 int check_msg_scroll;
7015{
7016 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7017 && !did_wait_return
7018 && emsg_silent == 0)
7019 {
7020 out_flush();
7021 ui_delay(1000L, TRUE);
7022 emsg_on_display = FALSE;
7023 if (check_msg_scroll)
7024 msg_scroll = FALSE;
7025 }
7026}
7027
7028/*
7029 * screen_valid - allocate screen buffers if size changed
7030 * If "clear" is TRUE: clear screen if it has been resized.
7031 * Returns TRUE if there is a valid screen to write to.
7032 * Returns FALSE when starting up and screen not initialized yet.
7033 */
7034 int
7035screen_valid(clear)
7036 int clear;
7037{
7038 screenalloc(clear); /* allocate screen buffers if size changed */
7039 return (ScreenLines != NULL);
7040}
7041
7042/*
7043 * Resize the shell to Rows and Columns.
7044 * Allocate ScreenLines[] and associated items.
7045 *
7046 * There may be some time between setting Rows and Columns and (re)allocating
7047 * ScreenLines[]. This happens when starting up and when (manually) changing
7048 * the shell size. Always use screen_Rows and screen_Columns to access items
7049 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7050 * final size of the shell is needed.
7051 */
7052 void
7053screenalloc(clear)
7054 int clear;
7055{
7056 int new_row, old_row;
7057#ifdef FEAT_GUI
7058 int old_Rows;
7059#endif
7060 win_T *wp;
7061 int outofmem = FALSE;
7062 int len;
7063 schar_T *new_ScreenLines;
7064#ifdef FEAT_MBYTE
7065 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007066 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007068 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069#endif
7070 sattr_T *new_ScreenAttrs;
7071 unsigned *new_LineOffset;
7072 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007073#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007074 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007075 tabpage_T *tp;
7076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007078 static int did_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
7080 /*
7081 * Allocation of the screen buffers is done only when the size changes and
7082 * when Rows and Columns have been set and we have started doing full
7083 * screen stuff.
7084 */
7085 if ((ScreenLines != NULL
7086 && Rows == screen_Rows
7087 && Columns == screen_Columns
7088#ifdef FEAT_MBYTE
7089 && enc_utf8 == (ScreenLinesUC != NULL)
7090 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007091 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092#endif
7093 )
7094 || Rows == 0
7095 || Columns == 0
7096 || (!full_screen && ScreenLines == NULL))
7097 return;
7098
7099 /*
7100 * It's possible that we produce an out-of-memory message below, which
7101 * will cause this function to be called again. To break the loop, just
7102 * return here.
7103 */
7104 if (entered)
7105 return;
7106 entered = TRUE;
7107
7108 win_new_shellsize(); /* fit the windows in the new sized shell */
7109
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 comp_col(); /* recompute columns for shown command and ruler */
7111
7112 /*
7113 * We're changing the size of the screen.
7114 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7115 * - Move lines from the old arrays into the new arrays, clear extra
7116 * lines (unless the screen is going to be cleared).
7117 * - Free the old arrays.
7118 *
7119 * If anything fails, make ScreenLines NULL, so we don't do anything!
7120 * Continuing with the old ScreenLines may result in a crash, because the
7121 * size is wrong.
7122 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007123 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126 new_ScreenLines = (schar_T *)lalloc((long_u)(
7127 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7128#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007129 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 if (enc_utf8)
7131 {
7132 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7133 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007134 for (i = 0; i < p_mco; ++i)
7135 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7137 }
7138 if (enc_dbcs == DBCS_JPNU)
7139 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7140 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7141#endif
7142 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7143 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7144 new_LineOffset = (unsigned *)lalloc((long_u)(
7145 Rows * sizeof(unsigned)), FALSE);
7146 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007147#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007148 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007151 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {
7153 if (win_alloc_lines(wp) == FAIL)
7154 {
7155 outofmem = TRUE;
7156#ifdef FEAT_WINDOWS
7157 break;
7158#endif
7159 }
7160 }
7161
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007162#ifdef FEAT_MBYTE
7163 for (i = 0; i < p_mco; ++i)
7164 if (new_ScreenLinesC[i] == NULL)
7165 break;
7166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 if (new_ScreenLines == NULL
7168#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007169 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7171#endif
7172 || new_ScreenAttrs == NULL
7173 || new_LineOffset == NULL
7174 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007175#ifdef FEAT_WINDOWS
7176 || new_TabPageIdxs == NULL
7177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 || outofmem)
7179 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007180 if (ScreenLines != NULL || !did_outofmem_msg)
7181 {
7182 /* guess the size */
7183 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7184
7185 /* Remember we did this to avoid getting outofmem messages over
7186 * and over again. */
7187 did_outofmem_msg = TRUE;
7188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 vim_free(new_ScreenLines);
7190 new_ScreenLines = NULL;
7191#ifdef FEAT_MBYTE
7192 vim_free(new_ScreenLinesUC);
7193 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007194 for (i = 0; i < p_mco; ++i)
7195 {
7196 vim_free(new_ScreenLinesC[i]);
7197 new_ScreenLinesC[i] = NULL;
7198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 vim_free(new_ScreenLines2);
7200 new_ScreenLines2 = NULL;
7201#endif
7202 vim_free(new_ScreenAttrs);
7203 new_ScreenAttrs = NULL;
7204 vim_free(new_LineOffset);
7205 new_LineOffset = NULL;
7206 vim_free(new_LineWraps);
7207 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007208#ifdef FEAT_WINDOWS
7209 vim_free(new_TabPageIdxs);
7210 new_TabPageIdxs = NULL;
7211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213 else
7214 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007215 did_outofmem_msg = FALSE;
7216
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 for (new_row = 0; new_row < Rows; ++new_row)
7218 {
7219 new_LineOffset[new_row] = new_row * Columns;
7220 new_LineWraps[new_row] = FALSE;
7221
7222 /*
7223 * If the screen is not going to be cleared, copy as much as
7224 * possible from the old screen to the new one and clear the rest
7225 * (used when resizing the window at the "--more--" prompt or when
7226 * executing an external command, for the GUI).
7227 */
7228 if (!clear)
7229 {
7230 (void)vim_memset(new_ScreenLines + new_row * Columns,
7231 ' ', (size_t)Columns * sizeof(schar_T));
7232#ifdef FEAT_MBYTE
7233 if (enc_utf8)
7234 {
7235 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7236 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007237 for (i = 0; i < p_mco; ++i)
7238 (void)vim_memset(new_ScreenLinesC[i]
7239 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 0, (size_t)Columns * sizeof(u8char_T));
7241 }
7242 if (enc_dbcs == DBCS_JPNU)
7243 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7244 0, (size_t)Columns * sizeof(schar_T));
7245#endif
7246 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7247 0, (size_t)Columns * sizeof(sattr_T));
7248 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007249 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
7251 if (screen_Columns < Columns)
7252 len = screen_Columns;
7253 else
7254 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007255#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007256 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007257 * may be invalid now. Also when p_mco changes. */
7258 if (!(enc_utf8 && ScreenLinesUC == NULL)
7259 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007260#endif
7261 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7262 ScreenLines + LineOffset[old_row],
7263 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007265 if (enc_utf8 && ScreenLinesUC != NULL
7266 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {
7268 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7269 ScreenLinesUC + LineOffset[old_row],
7270 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007271 for (i = 0; i < p_mco; ++i)
7272 mch_memmove(new_ScreenLinesC[i]
7273 + new_LineOffset[new_row],
7274 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 (size_t)len * sizeof(u8char_T));
7276 }
7277 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7278 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7279 ScreenLines2 + LineOffset[old_row],
7280 (size_t)len * sizeof(schar_T));
7281#endif
7282 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7283 ScreenAttrs + LineOffset[old_row],
7284 (size_t)len * sizeof(sattr_T));
7285 }
7286 }
7287 }
7288 /* Use the last line of the screen for the current line. */
7289 current_ScreenLine = new_ScreenLines + Rows * Columns;
7290 }
7291
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007292 free_screenlines();
7293
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 ScreenLines = new_ScreenLines;
7295#ifdef FEAT_MBYTE
7296 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007297 for (i = 0; i < p_mco; ++i)
7298 ScreenLinesC[i] = new_ScreenLinesC[i];
7299 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 ScreenLines2 = new_ScreenLines2;
7301#endif
7302 ScreenAttrs = new_ScreenAttrs;
7303 LineOffset = new_LineOffset;
7304 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007305#ifdef FEAT_WINDOWS
7306 TabPageIdxs = new_TabPageIdxs;
7307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
7309 /* It's important that screen_Rows and screen_Columns reflect the actual
7310 * size of ScreenLines[]. Set them before calling anything. */
7311#ifdef FEAT_GUI
7312 old_Rows = screen_Rows;
7313#endif
7314 screen_Rows = Rows;
7315 screen_Columns = Columns;
7316
7317 must_redraw = CLEAR; /* need to clear the screen later */
7318 if (clear)
7319 screenclear2();
7320
7321#ifdef FEAT_GUI
7322 else if (gui.in_use
7323 && !gui.starting
7324 && ScreenLines != NULL
7325 && old_Rows != Rows)
7326 {
7327 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7328 /*
7329 * Adjust the position of the cursor, for when executing an external
7330 * command.
7331 */
7332 if (msg_row >= Rows) /* Rows got smaller */
7333 msg_row = Rows - 1; /* put cursor at last row */
7334 else if (Rows > old_Rows) /* Rows got bigger */
7335 msg_row += Rows - old_Rows; /* put cursor in same place */
7336 if (msg_col >= Columns) /* Columns got smaller */
7337 msg_col = Columns - 1; /* put cursor at last column */
7338 }
7339#endif
7340
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 entered = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007342
7343#ifdef FEAT_AUTOCMD
7344 if (starting == 0)
7345 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347}
7348
7349 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007350free_screenlines()
7351{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007352#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007353 int i;
7354
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007355 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007356 for (i = 0; i < Screen_mco; ++i)
7357 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007358 vim_free(ScreenLines2);
7359#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007360 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007361 vim_free(ScreenAttrs);
7362 vim_free(LineOffset);
7363 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007364#ifdef FEAT_WINDOWS
7365 vim_free(TabPageIdxs);
7366#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007367}
7368
7369 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370screenclear()
7371{
7372 check_for_delay(FALSE);
7373 screenalloc(FALSE); /* allocate screen buffers if size changed */
7374 screenclear2(); /* clear the screen */
7375}
7376
7377 static void
7378screenclear2()
7379{
7380 int i;
7381
7382 if (starting == NO_SCREEN || ScreenLines == NULL
7383#ifdef FEAT_GUI
7384 || (gui.in_use && gui.starting)
7385#endif
7386 )
7387 return;
7388
7389#ifdef FEAT_GUI
7390 if (!gui.in_use)
7391#endif
7392 screen_attr = -1; /* force setting the Normal colors */
7393 screen_stop_highlight(); /* don't want highlighting here */
7394
7395#ifdef FEAT_CLIPBOARD
7396 /* disable selection without redrawing it */
7397 clip_scroll_selection(9999);
7398#endif
7399
7400 /* blank out ScreenLines */
7401 for (i = 0; i < Rows; ++i)
7402 {
7403 lineclear(LineOffset[i], (int)Columns);
7404 LineWraps[i] = FALSE;
7405 }
7406
7407 if (can_clear(T_CL))
7408 {
7409 out_str(T_CL); /* clear the display */
7410 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007411 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413 else
7414 {
7415 /* can't clear the screen, mark all chars with invalid attributes */
7416 for (i = 0; i < Rows; ++i)
7417 lineinvalid(LineOffset[i], (int)Columns);
7418 clear_cmdline = TRUE;
7419 }
7420
7421 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7422
7423 win_rest_invalid(firstwin);
7424 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007425#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007426 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 if (must_redraw == CLEAR) /* no need to clear again */
7429 must_redraw = NOT_VALID;
7430 compute_cmdrow();
7431 msg_row = cmdline_row; /* put cursor on last line for messages */
7432 msg_col = 0;
7433 screen_start(); /* don't know where cursor is now */
7434 msg_scrolled = 0; /* can't scroll back */
7435 msg_didany = FALSE;
7436 msg_didout = FALSE;
7437}
7438
7439/*
7440 * Clear one line in ScreenLines.
7441 */
7442 static void
7443lineclear(off, width)
7444 unsigned off;
7445 int width;
7446{
7447 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7448#ifdef FEAT_MBYTE
7449 if (enc_utf8)
7450 (void)vim_memset(ScreenLinesUC + off, 0,
7451 (size_t)width * sizeof(u8char_T));
7452#endif
7453 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7454}
7455
7456/*
7457 * Mark one line in ScreenLines invalid by setting the attributes to an
7458 * invalid value.
7459 */
7460 static void
7461lineinvalid(off, width)
7462 unsigned off;
7463 int width;
7464{
7465 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7466}
7467
7468#ifdef FEAT_VERTSPLIT
7469/*
7470 * Copy part of a Screenline for vertically split window "wp".
7471 */
7472 static void
7473linecopy(to, from, wp)
7474 int to;
7475 int from;
7476 win_T *wp;
7477{
7478 unsigned off_to = LineOffset[to] + wp->w_wincol;
7479 unsigned off_from = LineOffset[from] + wp->w_wincol;
7480
7481 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7482 wp->w_width * sizeof(schar_T));
7483# ifdef FEAT_MBYTE
7484 if (enc_utf8)
7485 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007486 int i;
7487
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7489 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007490 for (i = 0; i < p_mco; ++i)
7491 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7492 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494 if (enc_dbcs == DBCS_JPNU)
7495 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7496 wp->w_width * sizeof(schar_T));
7497# endif
7498 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7499 wp->w_width * sizeof(sattr_T));
7500}
7501#endif
7502
7503/*
7504 * Return TRUE if clearing with term string "p" would work.
7505 * It can't work when the string is empty or it won't set the right background.
7506 */
7507 int
7508can_clear(p)
7509 char_u *p;
7510{
7511 return (*p != NUL && (t_colors <= 1
7512#ifdef FEAT_GUI
7513 || gui.in_use
7514#endif
7515 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7516}
7517
7518/*
7519 * Reset cursor position. Use whenever cursor was moved because of outputting
7520 * something directly to the screen (shell commands) or a terminal control
7521 * code.
7522 */
7523 void
7524screen_start()
7525{
7526 screen_cur_row = screen_cur_col = 9999;
7527}
7528
7529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 * Move the cursor to position "row","col" in the screen.
7531 * This tries to find the most efficient way to move, minimizing the number of
7532 * characters sent to the terminal.
7533 */
7534 void
7535windgoto(row, col)
7536 int row;
7537 int col;
7538{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007539 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 int i;
7541 int plan;
7542 int cost;
7543 int wouldbe_col;
7544 int noinvcurs;
7545 char_u *bs;
7546 int goto_cost;
7547 int attr;
7548
7549#define GOTO_COST 7 /* asssume a term_windgoto() takes about 7 chars */
7550#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7551
7552#define PLAN_LE 1
7553#define PLAN_CR 2
7554#define PLAN_NL 3
7555#define PLAN_WRITE 4
7556 /* Can't use ScreenLines unless initialized */
7557 if (ScreenLines == NULL)
7558 return;
7559
7560 if (col != screen_cur_col || row != screen_cur_row)
7561 {
7562 /* Check for valid position. */
7563 if (row < 0) /* window without text lines? */
7564 row = 0;
7565 if (row >= screen_Rows)
7566 row = screen_Rows - 1;
7567 if (col >= screen_Columns)
7568 col = screen_Columns - 1;
7569
7570 /* check if no cursor movement is allowed in highlight mode */
7571 if (screen_attr && *T_MS == NUL)
7572 noinvcurs = HIGHL_COST;
7573 else
7574 noinvcurs = 0;
7575 goto_cost = GOTO_COST + noinvcurs;
7576
7577 /*
7578 * Plan how to do the positioning:
7579 * 1. Use CR to move it to column 0, same row.
7580 * 2. Use T_LE to move it a few columns to the left.
7581 * 3. Use NL to move a few lines down, column 0.
7582 * 4. Move a few columns to the right with T_ND or by writing chars.
7583 *
7584 * Don't do this if the cursor went beyond the last column, the cursor
7585 * position is unknown then (some terminals wrap, some don't )
7586 *
7587 * First check if the highlighting attibutes allow us to write
7588 * characters to move the cursor to the right.
7589 */
7590 if (row >= screen_cur_row && screen_cur_col < Columns)
7591 {
7592 /*
7593 * If the cursor is in the same row, bigger col, we can use CR
7594 * or T_LE.
7595 */
7596 bs = NULL; /* init for GCC */
7597 attr = screen_attr;
7598 if (row == screen_cur_row && col < screen_cur_col)
7599 {
7600 /* "le" is preferred over "bc", because "bc" is obsolete */
7601 if (*T_LE)
7602 bs = T_LE; /* "cursor left" */
7603 else
7604 bs = T_BC; /* "backspace character (old) */
7605 if (*bs)
7606 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7607 else
7608 cost = 999;
7609 if (col + 1 < cost) /* using CR is less characters */
7610 {
7611 plan = PLAN_CR;
7612 wouldbe_col = 0;
7613 cost = 1; /* CR is just one character */
7614 }
7615 else
7616 {
7617 plan = PLAN_LE;
7618 wouldbe_col = col;
7619 }
7620 if (noinvcurs) /* will stop highlighting */
7621 {
7622 cost += noinvcurs;
7623 attr = 0;
7624 }
7625 }
7626
7627 /*
7628 * If the cursor is above where we want to be, we can use CR LF.
7629 */
7630 else if (row > screen_cur_row)
7631 {
7632 plan = PLAN_NL;
7633 wouldbe_col = 0;
7634 cost = (row - screen_cur_row) * 2; /* CR LF */
7635 if (noinvcurs) /* will stop highlighting */
7636 {
7637 cost += noinvcurs;
7638 attr = 0;
7639 }
7640 }
7641
7642 /*
7643 * If the cursor is in the same row, smaller col, just use write.
7644 */
7645 else
7646 {
7647 plan = PLAN_WRITE;
7648 wouldbe_col = screen_cur_col;
7649 cost = 0;
7650 }
7651
7652 /*
7653 * Check if any characters that need to be written have the
7654 * correct attributes. Also avoid UTF-8 characters.
7655 */
7656 i = col - wouldbe_col;
7657 if (i > 0)
7658 cost += i;
7659 if (cost < goto_cost && i > 0)
7660 {
7661 /*
7662 * Check if the attributes are correct without additionally
7663 * stopping highlighting.
7664 */
7665 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7666 while (i && *p++ == attr)
7667 --i;
7668 if (i != 0)
7669 {
7670 /*
7671 * Try if it works when highlighting is stopped here.
7672 */
7673 if (*--p == 0)
7674 {
7675 cost += noinvcurs;
7676 while (i && *p++ == 0)
7677 --i;
7678 }
7679 if (i != 0)
7680 cost = 999; /* different attributes, don't do it */
7681 }
7682#ifdef FEAT_MBYTE
7683 if (enc_utf8)
7684 {
7685 /* Don't use an UTF-8 char for positioning, it's slow. */
7686 for (i = wouldbe_col; i < col; ++i)
7687 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7688 {
7689 cost = 999;
7690 break;
7691 }
7692 }
7693#endif
7694 }
7695
7696 /*
7697 * We can do it without term_windgoto()!
7698 */
7699 if (cost < goto_cost)
7700 {
7701 if (plan == PLAN_LE)
7702 {
7703 if (noinvcurs)
7704 screen_stop_highlight();
7705 while (screen_cur_col > col)
7706 {
7707 out_str(bs);
7708 --screen_cur_col;
7709 }
7710 }
7711 else if (plan == PLAN_CR)
7712 {
7713 if (noinvcurs)
7714 screen_stop_highlight();
7715 out_char('\r');
7716 screen_cur_col = 0;
7717 }
7718 else if (plan == PLAN_NL)
7719 {
7720 if (noinvcurs)
7721 screen_stop_highlight();
7722 while (screen_cur_row < row)
7723 {
7724 out_char('\n');
7725 ++screen_cur_row;
7726 }
7727 screen_cur_col = 0;
7728 }
7729
7730 i = col - screen_cur_col;
7731 if (i > 0)
7732 {
7733 /*
7734 * Use cursor-right if it's one character only. Avoids
7735 * removing a line of pixels from the last bold char, when
7736 * using the bold trick in the GUI.
7737 */
7738 if (T_ND[0] != NUL && T_ND[1] == NUL)
7739 {
7740 while (i-- > 0)
7741 out_char(*T_ND);
7742 }
7743 else
7744 {
7745 int off;
7746
7747 off = LineOffset[row] + screen_cur_col;
7748 while (i-- > 0)
7749 {
7750 if (ScreenAttrs[off] != screen_attr)
7751 screen_stop_highlight();
7752#ifdef FEAT_MBYTE
7753 out_flush_check();
7754#endif
7755 out_char(ScreenLines[off]);
7756#ifdef FEAT_MBYTE
7757 if (enc_dbcs == DBCS_JPNU
7758 && ScreenLines[off] == 0x8e)
7759 out_char(ScreenLines2[off]);
7760#endif
7761 ++off;
7762 }
7763 }
7764 }
7765 }
7766 }
7767 else
7768 cost = 999;
7769
7770 if (cost >= goto_cost)
7771 {
7772 if (noinvcurs)
7773 screen_stop_highlight();
7774 if (row == screen_cur_row && (col > screen_cur_col) &&
7775 *T_CRI != NUL)
7776 term_cursor_right(col - screen_cur_col);
7777 else
7778 term_windgoto(row, col);
7779 }
7780 screen_cur_row = row;
7781 screen_cur_col = col;
7782 }
7783}
7784
7785/*
7786 * Set cursor to its position in the current window.
7787 */
7788 void
7789setcursor()
7790{
7791 if (redrawing())
7792 {
7793 validate_cursor();
7794 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7795 W_WINCOL(curwin) + (
7796#ifdef FEAT_RIGHTLEFT
7797 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7798# ifdef FEAT_MBYTE
7799 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7800# endif
7801 1)) :
7802#endif
7803 curwin->w_wcol));
7804 }
7805}
7806
7807
7808/*
7809 * insert 'line_count' lines at 'row' in window 'wp'
7810 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7811 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7812 * scrolling.
7813 * Returns FAIL if the lines are not inserted, OK for success.
7814 */
7815 int
7816win_ins_lines(wp, row, line_count, invalid, mayclear)
7817 win_T *wp;
7818 int row;
7819 int line_count;
7820 int invalid;
7821 int mayclear;
7822{
7823 int did_delete;
7824 int nextrow;
7825 int lastrow;
7826 int retval;
7827
7828 if (invalid)
7829 wp->w_lines_valid = 0;
7830
7831 if (wp->w_height < 5)
7832 return FAIL;
7833
7834 if (line_count > wp->w_height - row)
7835 line_count = wp->w_height - row;
7836
7837 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7838 if (retval != MAYBE)
7839 return retval;
7840
7841 /*
7842 * If there is a next window or a status line, we first try to delete the
7843 * lines at the bottom to avoid messing what is after the window.
7844 * If this fails and there are following windows, don't do anything to avoid
7845 * messing up those windows, better just redraw.
7846 */
7847 did_delete = FALSE;
7848#ifdef FEAT_WINDOWS
7849 if (wp->w_next != NULL || wp->w_status_height)
7850 {
7851 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7852 line_count, (int)Rows, FALSE, NULL) == OK)
7853 did_delete = TRUE;
7854 else if (wp->w_next)
7855 return FAIL;
7856 }
7857#endif
7858 /*
7859 * if no lines deleted, blank the lines that will end up below the window
7860 */
7861 if (!did_delete)
7862 {
7863#ifdef FEAT_WINDOWS
7864 wp->w_redr_status = TRUE;
7865#endif
7866 redraw_cmdline = TRUE;
7867 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7868 lastrow = nextrow + line_count;
7869 if (lastrow > Rows)
7870 lastrow = Rows;
7871 screen_fill(nextrow - line_count, lastrow - line_count,
7872 W_WINCOL(wp), (int)W_ENDCOL(wp),
7873 ' ', ' ', 0);
7874 }
7875
7876 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7877 == FAIL)
7878 {
7879 /* deletion will have messed up other windows */
7880 if (did_delete)
7881 {
7882#ifdef FEAT_WINDOWS
7883 wp->w_redr_status = TRUE;
7884#endif
7885 win_rest_invalid(W_NEXT(wp));
7886 }
7887 return FAIL;
7888 }
7889
7890 return OK;
7891}
7892
7893/*
7894 * delete "line_count" window lines at "row" in window "wp"
7895 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7896 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7897 * scrolling
7898 * Return OK for success, FAIL if the lines are not deleted.
7899 */
7900 int
7901win_del_lines(wp, row, line_count, invalid, mayclear)
7902 win_T *wp;
7903 int row;
7904 int line_count;
7905 int invalid;
7906 int mayclear;
7907{
7908 int retval;
7909
7910 if (invalid)
7911 wp->w_lines_valid = 0;
7912
7913 if (line_count > wp->w_height - row)
7914 line_count = wp->w_height - row;
7915
7916 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7917 if (retval != MAYBE)
7918 return retval;
7919
7920 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7921 (int)Rows, FALSE, NULL) == FAIL)
7922 return FAIL;
7923
7924#ifdef FEAT_WINDOWS
7925 /*
7926 * If there are windows or status lines below, try to put them at the
7927 * correct place. If we can't do that, they have to be redrawn.
7928 */
7929 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
7930 {
7931 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7932 line_count, (int)Rows, NULL) == FAIL)
7933 {
7934 wp->w_redr_status = TRUE;
7935 win_rest_invalid(wp->w_next);
7936 }
7937 }
7938 /*
7939 * If this is the last window and there is no status line, redraw the
7940 * command line later.
7941 */
7942 else
7943#endif
7944 redraw_cmdline = TRUE;
7945 return OK;
7946}
7947
7948/*
7949 * Common code for win_ins_lines() and win_del_lines().
7950 * Returns OK or FAIL when the work has been done.
7951 * Returns MAYBE when not finished yet.
7952 */
7953 static int
7954win_do_lines(wp, row, line_count, mayclear, del)
7955 win_T *wp;
7956 int row;
7957 int line_count;
7958 int mayclear;
7959 int del;
7960{
7961 int retval;
7962
7963 if (!redrawing() || line_count <= 0)
7964 return FAIL;
7965
7966 /* only a few lines left: redraw is faster */
7967 if (mayclear && Rows - line_count < 5
7968#ifdef FEAT_VERTSPLIT
7969 && wp->w_width == Columns
7970#endif
7971 )
7972 {
7973 screenclear(); /* will set wp->w_lines_valid to 0 */
7974 return FAIL;
7975 }
7976
7977 /*
7978 * Delete all remaining lines
7979 */
7980 if (row + line_count >= wp->w_height)
7981 {
7982 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
7983 W_WINCOL(wp), (int)W_ENDCOL(wp),
7984 ' ', ' ', 0);
7985 return OK;
7986 }
7987
7988 /*
7989 * when scrolling, the message on the command line should be cleared,
7990 * otherwise it will stay there forever.
7991 */
7992 clear_cmdline = TRUE;
7993
7994 /*
7995 * If the terminal can set a scroll region, use that.
7996 * Always do this in a vertically split window. This will redraw from
7997 * ScreenLines[] when t_CV isn't defined. That's faster than using
7998 * win_line().
7999 * Don't use a scroll region when we are going to redraw the text, writing
8000 * a character in the lower right corner of the scroll region causes a
8001 * scroll-up in the DJGPP version.
8002 */
8003 if (scroll_region
8004#ifdef FEAT_VERTSPLIT
8005 || W_WIDTH(wp) != Columns
8006#endif
8007 )
8008 {
8009#ifdef FEAT_VERTSPLIT
8010 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8011#endif
8012 scroll_region_set(wp, row);
8013 if (del)
8014 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8015 wp->w_height - row, FALSE, wp);
8016 else
8017 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8018 wp->w_height - row, wp);
8019#ifdef FEAT_VERTSPLIT
8020 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8021#endif
8022 scroll_region_reset();
8023 return retval;
8024 }
8025
8026#ifdef FEAT_WINDOWS
8027 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8028 return FAIL;
8029#endif
8030
8031 return MAYBE;
8032}
8033
8034/*
8035 * window 'wp' and everything after it is messed up, mark it for redraw
8036 */
8037 static void
8038win_rest_invalid(wp)
8039 win_T *wp;
8040{
8041#ifdef FEAT_WINDOWS
8042 while (wp != NULL)
8043#else
8044 if (wp != NULL)
8045#endif
8046 {
8047 redraw_win_later(wp, NOT_VALID);
8048#ifdef FEAT_WINDOWS
8049 wp->w_redr_status = TRUE;
8050 wp = wp->w_next;
8051#endif
8052 }
8053 redraw_cmdline = TRUE;
8054}
8055
8056/*
8057 * The rest of the routines in this file perform screen manipulations. The
8058 * given operation is performed physically on the screen. The corresponding
8059 * change is also made to the internal screen image. In this way, the editor
8060 * anticipates the effect of editing changes on the appearance of the screen.
8061 * That way, when we call screenupdate a complete redraw isn't usually
8062 * necessary. Another advantage is that we can keep adding code to anticipate
8063 * screen changes, and in the meantime, everything still works.
8064 */
8065
8066/*
8067 * types for inserting or deleting lines
8068 */
8069#define USE_T_CAL 1
8070#define USE_T_CDL 2
8071#define USE_T_AL 3
8072#define USE_T_CE 4
8073#define USE_T_DL 5
8074#define USE_T_SR 6
8075#define USE_NL 7
8076#define USE_T_CD 8
8077#define USE_REDRAW 9
8078
8079/*
8080 * insert lines on the screen and update ScreenLines[]
8081 * 'end' is the line after the scrolled part. Normally it is Rows.
8082 * When scrolling region used 'off' is the offset from the top for the region.
8083 * 'row' and 'end' are relative to the start of the region.
8084 *
8085 * return FAIL for failure, OK for success.
8086 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008087 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088screen_ins_lines(off, row, line_count, end, wp)
8089 int off;
8090 int row;
8091 int line_count;
8092 int end;
8093 win_T *wp; /* NULL or window to use width from */
8094{
8095 int i;
8096 int j;
8097 unsigned temp;
8098 int cursor_row;
8099 int type;
8100 int result_empty;
8101 int can_ce = can_clear(T_CE);
8102
8103 /*
8104 * FAIL if
8105 * - there is no valid screen
8106 * - the screen has to be redrawn completely
8107 * - the line count is less than one
8108 * - the line count is more than 'ttyscroll'
8109 */
8110 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8111 return FAIL;
8112
8113 /*
8114 * There are seven ways to insert lines:
8115 * 0. When in a vertically split window and t_CV isn't set, redraw the
8116 * characters from ScreenLines[].
8117 * 1. Use T_CD (clear to end of display) if it exists and the result of
8118 * the insert is just empty lines
8119 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8120 * present or line_count > 1. It looks better if we do all the inserts
8121 * at once.
8122 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8123 * insert is just empty lines and T_CE is not present or line_count >
8124 * 1.
8125 * 4. Use T_AL (insert line) if it exists.
8126 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8127 * just empty lines.
8128 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8129 * just empty lines.
8130 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8131 * the 'da' flag is not set or we have clear line capability.
8132 * 8. redraw the characters from ScreenLines[].
8133 *
8134 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8135 * the scrollbar for the window. It does have insert line, use that if it
8136 * exists.
8137 */
8138 result_empty = (row + line_count >= end);
8139#ifdef FEAT_VERTSPLIT
8140 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8141 type = USE_REDRAW;
8142 else
8143#endif
8144 if (can_clear(T_CD) && result_empty)
8145 type = USE_T_CD;
8146 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8147 type = USE_T_CAL;
8148 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8149 type = USE_T_CDL;
8150 else if (*T_AL != NUL)
8151 type = USE_T_AL;
8152 else if (can_ce && result_empty)
8153 type = USE_T_CE;
8154 else if (*T_DL != NUL && result_empty)
8155 type = USE_T_DL;
8156 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8157 type = USE_T_SR;
8158 else
8159 return FAIL;
8160
8161 /*
8162 * For clearing the lines screen_del_lines() is used. This will also take
8163 * care of t_db if necessary.
8164 */
8165 if (type == USE_T_CD || type == USE_T_CDL ||
8166 type == USE_T_CE || type == USE_T_DL)
8167 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8168
8169 /*
8170 * If text is retained below the screen, first clear or delete as many
8171 * lines at the bottom of the window as are about to be inserted so that
8172 * the deleted lines won't later surface during a screen_del_lines.
8173 */
8174 if (*T_DB)
8175 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8176
8177#ifdef FEAT_CLIPBOARD
8178 /* Remove a modeless selection when inserting lines halfway the screen
8179 * or not the full width of the screen. */
8180 if (off + row > 0
8181# ifdef FEAT_VERTSPLIT
8182 || (wp != NULL && wp->w_width != Columns)
8183# endif
8184 )
8185 clip_clear_selection();
8186 else
8187 clip_scroll_selection(-line_count);
8188#endif
8189
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190#ifdef FEAT_GUI
8191 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8192 * scrolling is actually carried out. */
8193 gui_dont_update_cursor();
8194#endif
8195
8196 if (*T_CCS != NUL) /* cursor relative to region */
8197 cursor_row = row;
8198 else
8199 cursor_row = row + off;
8200
8201 /*
8202 * Shift LineOffset[] line_count down to reflect the inserted lines.
8203 * Clear the inserted lines in ScreenLines[].
8204 */
8205 row += off;
8206 end += off;
8207 for (i = 0; i < line_count; ++i)
8208 {
8209#ifdef FEAT_VERTSPLIT
8210 if (wp != NULL && wp->w_width != Columns)
8211 {
8212 /* need to copy part of a line */
8213 j = end - 1 - i;
8214 while ((j -= line_count) >= row)
8215 linecopy(j + line_count, j, wp);
8216 j += line_count;
8217 if (can_clear((char_u *)" "))
8218 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8219 else
8220 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8221 LineWraps[j] = FALSE;
8222 }
8223 else
8224#endif
8225 {
8226 j = end - 1 - i;
8227 temp = LineOffset[j];
8228 while ((j -= line_count) >= row)
8229 {
8230 LineOffset[j + line_count] = LineOffset[j];
8231 LineWraps[j + line_count] = LineWraps[j];
8232 }
8233 LineOffset[j + line_count] = temp;
8234 LineWraps[j + line_count] = FALSE;
8235 if (can_clear((char_u *)" "))
8236 lineclear(temp, (int)Columns);
8237 else
8238 lineinvalid(temp, (int)Columns);
8239 }
8240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241
8242 screen_stop_highlight();
8243 windgoto(cursor_row, 0);
8244
8245#ifdef FEAT_VERTSPLIT
8246 /* redraw the characters */
8247 if (type == USE_REDRAW)
8248 redraw_block(row, end, wp);
8249 else
8250#endif
8251 if (type == USE_T_CAL)
8252 {
8253 term_append_lines(line_count);
8254 screen_start(); /* don't know where cursor is now */
8255 }
8256 else
8257 {
8258 for (i = 0; i < line_count; i++)
8259 {
8260 if (type == USE_T_AL)
8261 {
8262 if (i && cursor_row != 0)
8263 windgoto(cursor_row, 0);
8264 out_str(T_AL);
8265 }
8266 else /* type == USE_T_SR */
8267 out_str(T_SR);
8268 screen_start(); /* don't know where cursor is now */
8269 }
8270 }
8271
8272 /*
8273 * With scroll-reverse and 'da' flag set we need to clear the lines that
8274 * have been scrolled down into the region.
8275 */
8276 if (type == USE_T_SR && *T_DA)
8277 {
8278 for (i = 0; i < line_count; ++i)
8279 {
8280 windgoto(off + i, 0);
8281 out_str(T_CE);
8282 screen_start(); /* don't know where cursor is now */
8283 }
8284 }
8285
8286#ifdef FEAT_GUI
8287 gui_can_update_cursor();
8288 if (gui.in_use)
8289 out_flush(); /* always flush after a scroll */
8290#endif
8291 return OK;
8292}
8293
8294/*
8295 * delete lines on the screen and update ScreenLines[]
8296 * 'end' is the line after the scrolled part. Normally it is Rows.
8297 * When scrolling region used 'off' is the offset from the top for the region.
8298 * 'row' and 'end' are relative to the start of the region.
8299 *
8300 * Return OK for success, FAIL if the lines are not deleted.
8301 */
8302/*ARGSUSED*/
8303 int
8304screen_del_lines(off, row, line_count, end, force, wp)
8305 int off;
8306 int row;
8307 int line_count;
8308 int end;
8309 int force; /* even when line_count > p_ttyscroll */
8310 win_T *wp; /* NULL or window to use width from */
8311{
8312 int j;
8313 int i;
8314 unsigned temp;
8315 int cursor_row;
8316 int cursor_end;
8317 int result_empty; /* result is empty until end of region */
8318 int can_delete; /* deleting line codes can be used */
8319 int type;
8320
8321 /*
8322 * FAIL if
8323 * - there is no valid screen
8324 * - the screen has to be redrawn completely
8325 * - the line count is less than one
8326 * - the line count is more than 'ttyscroll'
8327 */
8328 if (!screen_valid(TRUE) || line_count <= 0 ||
8329 (!force && line_count > p_ttyscroll))
8330 return FAIL;
8331
8332 /*
8333 * Check if the rest of the current region will become empty.
8334 */
8335 result_empty = row + line_count >= end;
8336
8337 /*
8338 * We can delete lines only when 'db' flag not set or when 'ce' option
8339 * available.
8340 */
8341 can_delete = (*T_DB == NUL || can_clear(T_CE));
8342
8343 /*
8344 * There are six ways to delete lines:
8345 * 0. When in a vertically split window and t_CV isn't set, redraw the
8346 * characters from ScreenLines[].
8347 * 1. Use T_CD if it exists and the result is empty.
8348 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8349 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8350 * none of the other ways work.
8351 * 4. Use T_CE (erase line) if the result is empty.
8352 * 5. Use T_DL (delete line) if it exists.
8353 * 6. redraw the characters from ScreenLines[].
8354 */
8355#ifdef FEAT_VERTSPLIT
8356 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8357 type = USE_REDRAW;
8358 else
8359#endif
8360 if (can_clear(T_CD) && result_empty)
8361 type = USE_T_CD;
8362#if defined(__BEOS__) && defined(BEOS_DR8)
8363 /*
8364 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8365 * its internal termcap... this works okay for tests which test *T_DB !=
8366 * NUL. It has the disadvantage that the user cannot use any :set t_*
8367 * command to get T_DB (back) to empty_option, only :set term=... will do
8368 * the trick...
8369 * Anyway, this hack will hopefully go away with the next OS release.
8370 * (Olaf Seibert)
8371 */
8372 else if (row == 0 && T_DB == empty_option
8373 && (line_count == 1 || *T_CDL == NUL))
8374#else
8375 else if (row == 0 && (
8376#ifndef AMIGA
8377 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8378 * up, so use delete-line command */
8379 line_count == 1 ||
8380#endif
8381 *T_CDL == NUL))
8382#endif
8383 type = USE_NL;
8384 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8385 type = USE_T_CDL;
8386 else if (can_clear(T_CE) && result_empty
8387#ifdef FEAT_VERTSPLIT
8388 && (wp == NULL || wp->w_width == Columns)
8389#endif
8390 )
8391 type = USE_T_CE;
8392 else if (*T_DL != NUL && can_delete)
8393 type = USE_T_DL;
8394 else if (*T_CDL != NUL && can_delete)
8395 type = USE_T_CDL;
8396 else
8397 return FAIL;
8398
8399#ifdef FEAT_CLIPBOARD
8400 /* Remove a modeless selection when deleting lines halfway the screen or
8401 * not the full width of the screen. */
8402 if (off + row > 0
8403# ifdef FEAT_VERTSPLIT
8404 || (wp != NULL && wp->w_width != Columns)
8405# endif
8406 )
8407 clip_clear_selection();
8408 else
8409 clip_scroll_selection(line_count);
8410#endif
8411
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412#ifdef FEAT_GUI
8413 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8414 * scrolling is actually carried out. */
8415 gui_dont_update_cursor();
8416#endif
8417
8418 if (*T_CCS != NUL) /* cursor relative to region */
8419 {
8420 cursor_row = row;
8421 cursor_end = end;
8422 }
8423 else
8424 {
8425 cursor_row = row + off;
8426 cursor_end = end + off;
8427 }
8428
8429 /*
8430 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8431 * Clear the inserted lines in ScreenLines[].
8432 */
8433 row += off;
8434 end += off;
8435 for (i = 0; i < line_count; ++i)
8436 {
8437#ifdef FEAT_VERTSPLIT
8438 if (wp != NULL && wp->w_width != Columns)
8439 {
8440 /* need to copy part of a line */
8441 j = row + i;
8442 while ((j += line_count) <= end - 1)
8443 linecopy(j - line_count, j, wp);
8444 j -= line_count;
8445 if (can_clear((char_u *)" "))
8446 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8447 else
8448 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8449 LineWraps[j] = FALSE;
8450 }
8451 else
8452#endif
8453 {
8454 /* whole width, moving the line pointers is faster */
8455 j = row + i;
8456 temp = LineOffset[j];
8457 while ((j += line_count) <= end - 1)
8458 {
8459 LineOffset[j - line_count] = LineOffset[j];
8460 LineWraps[j - line_count] = LineWraps[j];
8461 }
8462 LineOffset[j - line_count] = temp;
8463 LineWraps[j - line_count] = FALSE;
8464 if (can_clear((char_u *)" "))
8465 lineclear(temp, (int)Columns);
8466 else
8467 lineinvalid(temp, (int)Columns);
8468 }
8469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470
8471 screen_stop_highlight();
8472
8473#ifdef FEAT_VERTSPLIT
8474 /* redraw the characters */
8475 if (type == USE_REDRAW)
8476 redraw_block(row, end, wp);
8477 else
8478#endif
8479 if (type == USE_T_CD) /* delete the lines */
8480 {
8481 windgoto(cursor_row, 0);
8482 out_str(T_CD);
8483 screen_start(); /* don't know where cursor is now */
8484 }
8485 else if (type == USE_T_CDL)
8486 {
8487 windgoto(cursor_row, 0);
8488 term_delete_lines(line_count);
8489 screen_start(); /* don't know where cursor is now */
8490 }
8491 /*
8492 * Deleting lines at top of the screen or scroll region: Just scroll
8493 * the whole screen (scroll region) up by outputting newlines on the
8494 * last line.
8495 */
8496 else if (type == USE_NL)
8497 {
8498 windgoto(cursor_end - 1, 0);
8499 for (i = line_count; --i >= 0; )
8500 out_char('\n'); /* cursor will remain on same line */
8501 }
8502 else
8503 {
8504 for (i = line_count; --i >= 0; )
8505 {
8506 if (type == USE_T_DL)
8507 {
8508 windgoto(cursor_row, 0);
8509 out_str(T_DL); /* delete a line */
8510 }
8511 else /* type == USE_T_CE */
8512 {
8513 windgoto(cursor_row + i, 0);
8514 out_str(T_CE); /* erase a line */
8515 }
8516 screen_start(); /* don't know where cursor is now */
8517 }
8518 }
8519
8520 /*
8521 * If the 'db' flag is set, we need to clear the lines that have been
8522 * scrolled up at the bottom of the region.
8523 */
8524 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8525 {
8526 for (i = line_count; i > 0; --i)
8527 {
8528 windgoto(cursor_end - i, 0);
8529 out_str(T_CE); /* erase a line */
8530 screen_start(); /* don't know where cursor is now */
8531 }
8532 }
8533
8534#ifdef FEAT_GUI
8535 gui_can_update_cursor();
8536 if (gui.in_use)
8537 out_flush(); /* always flush after a scroll */
8538#endif
8539
8540 return OK;
8541}
8542
8543/*
8544 * show the current mode and ruler
8545 *
8546 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8547 * If clear_cmdline is FALSE there may be a message there that needs to be
8548 * cleared only if a mode is shown.
8549 * Return the length of the message (0 if no message).
8550 */
8551 int
8552showmode()
8553{
8554 int need_clear;
8555 int length = 0;
8556 int do_mode;
8557 int attr;
8558 int nwr_save;
8559#ifdef FEAT_INS_EXPAND
8560 int sub_attr;
8561#endif
8562
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008563 do_mode = ((p_smd && msg_silent == 0)
8564 && ((State & INSERT)
8565 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566#ifdef FEAT_VISUAL
8567 || VIsual_active
8568#endif
8569 ));
8570 if (do_mode || Recording)
8571 {
8572 /*
8573 * Don't show mode right now, when not redrawing or inside a mapping.
8574 * Call char_avail() only when we are going to show something, because
8575 * it takes a bit of time.
8576 */
8577 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8578 {
8579 redraw_cmdline = TRUE; /* show mode later */
8580 return 0;
8581 }
8582
8583 nwr_save = need_wait_return;
8584
8585 /* wait a bit before overwriting an important message */
8586 check_for_delay(FALSE);
8587
8588 /* if the cmdline is more than one line high, erase top lines */
8589 need_clear = clear_cmdline;
8590 if (clear_cmdline && cmdline_row < Rows - 1)
8591 msg_clr_cmdline(); /* will reset clear_cmdline */
8592
8593 /* Position on the last line in the window, column 0 */
8594 msg_pos_mode();
8595 cursor_off();
8596 attr = hl_attr(HLF_CM); /* Highlight mode */
8597 if (do_mode)
8598 {
8599 MSG_PUTS_ATTR("--", attr);
8600#if defined(FEAT_XIM)
8601 if (xic != NULL && im_get_status() && !p_imdisable
8602 && curbuf->b_p_iminsert == B_IMODE_IM)
8603# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8604 MSG_PUTS_ATTR(" IM", attr);
8605# else
8606 MSG_PUTS_ATTR(" XIM", attr);
8607# endif
8608#endif
8609#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8610 if (gui.in_use)
8611 {
8612 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008613 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 }
8615#endif
8616#ifdef FEAT_INS_EXPAND
8617 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8618 {
8619 /* These messages can get long, avoid a wrap in a narrow
8620 * window. Prefer showing edit_submode_extra. */
8621 length = (Rows - msg_row) * Columns - 3;
8622 if (edit_submode_extra != NULL)
8623 length -= vim_strsize(edit_submode_extra);
8624 if (length > 0)
8625 {
8626 if (edit_submode_pre != NULL)
8627 length -= vim_strsize(edit_submode_pre);
8628 if (length - vim_strsize(edit_submode) > 0)
8629 {
8630 if (edit_submode_pre != NULL)
8631 msg_puts_attr(edit_submode_pre, attr);
8632 msg_puts_attr(edit_submode, attr);
8633 }
8634 if (edit_submode_extra != NULL)
8635 {
8636 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8637 if ((int)edit_submode_highl < (int)HLF_COUNT)
8638 sub_attr = hl_attr(edit_submode_highl);
8639 else
8640 sub_attr = attr;
8641 msg_puts_attr(edit_submode_extra, sub_attr);
8642 }
8643 }
8644 length = 0;
8645 }
8646 else
8647#endif
8648 {
8649#ifdef FEAT_VREPLACE
8650 if (State & VREPLACE_FLAG)
8651 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8652 else
8653#endif
8654 if (State & REPLACE_FLAG)
8655 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8656 else if (State & INSERT)
8657 {
8658#ifdef FEAT_RIGHTLEFT
8659 if (p_ri)
8660 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8661#endif
8662 MSG_PUTS_ATTR(_(" INSERT"), attr);
8663 }
8664 else if (restart_edit == 'I')
8665 MSG_PUTS_ATTR(_(" (insert)"), attr);
8666 else if (restart_edit == 'R')
8667 MSG_PUTS_ATTR(_(" (replace)"), attr);
8668 else if (restart_edit == 'V')
8669 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8670#ifdef FEAT_RIGHTLEFT
8671 if (p_hkmap)
8672 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8673# ifdef FEAT_FKMAP
8674 if (p_fkmap)
8675 MSG_PUTS_ATTR(farsi_text_5, attr);
8676# endif
8677#endif
8678#ifdef FEAT_KEYMAP
8679 if (State & LANGMAP)
8680 {
8681# ifdef FEAT_ARABIC
8682 if (curwin->w_p_arab)
8683 MSG_PUTS_ATTR(_(" Arabic"), attr);
8684 else
8685# endif
8686 MSG_PUTS_ATTR(_(" (lang)"), attr);
8687 }
8688#endif
8689 if ((State & INSERT) && p_paste)
8690 MSG_PUTS_ATTR(_(" (paste)"), attr);
8691
8692#ifdef FEAT_VISUAL
8693 if (VIsual_active)
8694 {
8695 char *p;
8696
8697 /* Don't concatenate separate words to avoid translation
8698 * problems. */
8699 switch ((VIsual_select ? 4 : 0)
8700 + (VIsual_mode == Ctrl_V) * 2
8701 + (VIsual_mode == 'V'))
8702 {
8703 case 0: p = N_(" VISUAL"); break;
8704 case 1: p = N_(" VISUAL LINE"); break;
8705 case 2: p = N_(" VISUAL BLOCK"); break;
8706 case 4: p = N_(" SELECT"); break;
8707 case 5: p = N_(" SELECT LINE"); break;
8708 default: p = N_(" SELECT BLOCK"); break;
8709 }
8710 MSG_PUTS_ATTR(_(p), attr);
8711 }
8712#endif
8713 MSG_PUTS_ATTR(" --", attr);
8714 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008715
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 need_clear = TRUE;
8717 }
8718 if (Recording
8719#ifdef FEAT_INS_EXPAND
8720 && edit_submode == NULL /* otherwise it gets too long */
8721#endif
8722 )
8723 {
8724 MSG_PUTS_ATTR(_("recording"), attr);
8725 need_clear = TRUE;
8726 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008727
8728 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 if (need_clear || clear_cmdline)
8730 msg_clr_eos();
8731 msg_didout = FALSE; /* overwrite this message */
8732 length = msg_col;
8733 msg_col = 0;
8734 need_wait_return = nwr_save; /* never ask for hit-return for this */
8735 }
8736 else if (clear_cmdline && msg_silent == 0)
8737 /* Clear the whole command line. Will reset "clear_cmdline". */
8738 msg_clr_cmdline();
8739
8740#ifdef FEAT_CMDL_INFO
8741# ifdef FEAT_VISUAL
8742 /* In Visual mode the size of the selected area must be redrawn. */
8743 if (VIsual_active)
8744 clear_showcmd();
8745# endif
8746
8747 /* If the last window has no status line, the ruler is after the mode
8748 * message and must be redrawn */
8749 if (redrawing()
8750# ifdef FEAT_WINDOWS
8751 && lastwin->w_status_height == 0
8752# endif
8753 )
8754 win_redr_ruler(lastwin, TRUE);
8755#endif
8756 redraw_cmdline = FALSE;
8757 clear_cmdline = FALSE;
8758
8759 return length;
8760}
8761
8762/*
8763 * Position for a mode message.
8764 */
8765 static void
8766msg_pos_mode()
8767{
8768 msg_col = 0;
8769 msg_row = Rows - 1;
8770}
8771
8772/*
8773 * Delete mode message. Used when ESC is typed which is expected to end
8774 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008775 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 */
8777 void
8778unshowmode(force)
8779 int force;
8780{
8781 /*
8782 * Don't delete it right now, when not redrawing or insided a mapping.
8783 */
8784 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8785 redraw_cmdline = TRUE; /* delete mode later */
8786 else
8787 {
8788 msg_pos_mode();
8789 if (Recording)
8790 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8791 msg_clr_eos();
8792 }
8793}
8794
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008795#if defined(FEAT_WINDOWS)
8796/*
8797 * Draw the tab pages line at the top of the Vim window.
8798 */
8799 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008800draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008801{
8802 int tabcount = 0;
8803 tabpage_T *tp;
8804 int tabwidth;
8805 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008806 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008807 int attr;
8808 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008809 win_T *cwp;
8810 int wincount;
8811 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008812 int c;
8813 int len;
8814 int attr_sel = hl_attr(HLF_TPS);
8815 int attr_nosel = hl_attr(HLF_TP);
8816 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008817 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008818 int room;
8819 int use_sep_chars = (t_colors < 8
8820#ifdef FEAT_GUI
8821 && !gui.in_use
8822#endif
8823 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008824
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008825 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008826
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008827#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00008828 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008829 if (gui_use_tabline())
8830 {
8831 gui_update_tabline();
8832 return;
8833 }
8834#endif
8835
8836 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008837 return;
8838
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008839#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008840
8841 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
8842 for (scol = 0; scol < Columns; ++scol)
8843 TabPageIdxs[scol] = 0;
8844
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008845 /* Use the 'tabline' option if it's set. */
8846 if (*p_tal != NUL)
8847 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008848 int save_called_emsg = called_emsg;
8849
8850 /* Check for an error. If there is one we would loop in redrawing the
8851 * screen. Avoid that by making 'tabline' empty. */
8852 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008853 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008854 if (called_emsg)
8855 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008856 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008857 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008858 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00008859 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008860#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008861 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008862 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
8863 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008864
Bram Moolenaar238a5642006-02-21 22:12:05 +00008865 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
8866 if (tabwidth < 6)
8867 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008868
Bram Moolenaar238a5642006-02-21 22:12:05 +00008869 attr = attr_nosel;
8870 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008871 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008872 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
8873 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008874 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008875 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008876
Bram Moolenaar238a5642006-02-21 22:12:05 +00008877 if (tp->tp_topframe == topframe)
8878 attr = attr_sel;
8879 if (use_sep_chars && col > 0)
8880 screen_putchar('|', 0, col++, attr);
8881
8882 if (tp->tp_topframe != topframe)
8883 attr = attr_nosel;
8884
8885 screen_putchar(' ', 0, col++, attr);
8886
8887 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008888 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008889 cwp = curwin;
8890 wp = firstwin;
8891 }
8892 else
8893 {
8894 cwp = tp->tp_curwin;
8895 wp = tp->tp_firstwin;
8896 }
8897
8898 modified = FALSE;
8899 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
8900 if (bufIsChanged(wp->w_buffer))
8901 modified = TRUE;
8902 if (modified || wincount > 1)
8903 {
8904 if (wincount > 1)
8905 {
8906 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008907 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008908 if (col + len >= Columns - 3)
8909 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008910 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008911#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00008912 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008913#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00008914 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008915#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00008916 );
8917 col += len;
8918 }
8919 if (modified)
8920 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
8921 screen_putchar(' ', 0, col++, attr);
8922 }
8923
8924 room = scol - col + tabwidth - 1;
8925 if (room > 0)
8926 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008927 /* Get buffer name in NameBuff[] */
8928 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008929 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008930 len = vim_strsize(NameBuff);
8931 p = NameBuff;
8932#ifdef FEAT_MBYTE
8933 if (has_mbyte)
8934 while (len > room)
8935 {
8936 len -= ptr2cells(p);
8937 mb_ptr_adv(p);
8938 }
8939 else
8940#endif
8941 if (len > room)
8942 {
8943 p += len - room;
8944 len = room;
8945 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008946 if (len > Columns - col - 1)
8947 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008948
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008949 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008950 col += len;
8951 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00008952 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008953
8954 /* Store the tab page number in TabPageIdxs[], so that
8955 * jump_to_mouse() knows where each one is. */
8956 ++tabcount;
8957 while (scol < col)
8958 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008959 }
8960
Bram Moolenaar238a5642006-02-21 22:12:05 +00008961 if (use_sep_chars)
8962 c = '_';
8963 else
8964 c = ' ';
8965 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008966
8967 /* Put an "X" for closing the current tab if there are several. */
8968 if (first_tabpage->tp_next != NULL)
8969 {
8970 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
8971 TabPageIdxs[Columns - 1] = -999;
8972 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008973 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00008974
8975 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
8976 * set. */
8977 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008978}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008979
8980/*
8981 * Get buffer name for "buf" into NameBuff[].
8982 * Takes care of special buffer names and translates special characters.
8983 */
8984 void
8985get_trans_bufname(buf)
8986 buf_T *buf;
8987{
8988 if (buf_spname(buf) != NULL)
8989 STRCPY(NameBuff, buf_spname(buf));
8990 else
8991 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
8992 trans_characters(NameBuff, MAXPATHL);
8993}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008994#endif
8995
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
8997/*
8998 * Get the character to use in a status line. Get its attributes in "*attr".
8999 */
9000 static int
9001fillchar_status(attr, is_curwin)
9002 int *attr;
9003 int is_curwin;
9004{
9005 int fill;
9006 if (is_curwin)
9007 {
9008 *attr = hl_attr(HLF_S);
9009 fill = fill_stl;
9010 }
9011 else
9012 {
9013 *attr = hl_attr(HLF_SNC);
9014 fill = fill_stlnc;
9015 }
9016 /* Use fill when there is highlighting, and highlighting of current
9017 * window differs, or the fillchars differ, or this is not the
9018 * current window */
9019 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9020 || !is_curwin || firstwin == lastwin)
9021 || (fill_stl != fill_stlnc)))
9022 return fill;
9023 if (is_curwin)
9024 return '^';
9025 return '=';
9026}
9027#endif
9028
9029#ifdef FEAT_VERTSPLIT
9030/*
9031 * Get the character to use in a separator between vertically split windows.
9032 * Get its attributes in "*attr".
9033 */
9034 static int
9035fillchar_vsep(attr)
9036 int *attr;
9037{
9038 *attr = hl_attr(HLF_C);
9039 if (*attr == 0 && fill_vert == ' ')
9040 return '|';
9041 else
9042 return fill_vert;
9043}
9044#endif
9045
9046/*
9047 * Return TRUE if redrawing should currently be done.
9048 */
9049 int
9050redrawing()
9051{
9052 return (!RedrawingDisabled
9053 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9054}
9055
9056/*
9057 * Return TRUE if printing messages should currently be done.
9058 */
9059 int
9060messaging()
9061{
9062 return (!(p_lz && char_avail() && !KeyTyped));
9063}
9064
9065/*
9066 * Show current status info in ruler and various other places
9067 * If always is FALSE, only show ruler if position has changed.
9068 */
9069 void
9070showruler(always)
9071 int always;
9072{
9073 if (!always && !redrawing())
9074 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009075#ifdef FEAT_INS_EXPAND
9076 if (pum_visible())
9077 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009078# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009079 /* Don't redraw right now, do it later. */
9080 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009081# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009082 return;
9083 }
9084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009086 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009087 {
9088 redraw_custum_statusline(curwin);
9089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 else
9091#endif
9092#ifdef FEAT_CMDL_INFO
9093 win_redr_ruler(curwin, always);
9094#endif
9095
9096#ifdef FEAT_TITLE
9097 if (need_maketitle
9098# ifdef FEAT_STL_OPT
9099 || (p_icon && (stl_syntax & STL_IN_ICON))
9100 || (p_title && (stl_syntax & STL_IN_TITLE))
9101# endif
9102 )
9103 maketitle();
9104#endif
9105}
9106
9107#ifdef FEAT_CMDL_INFO
9108 static void
9109win_redr_ruler(wp, always)
9110 win_T *wp;
9111 int always;
9112{
9113 char_u buffer[70];
9114 int row;
9115 int fillchar;
9116 int attr;
9117 int empty_line = FALSE;
9118 colnr_T virtcol;
9119 int i;
9120 int o;
9121#ifdef FEAT_VERTSPLIT
9122 int this_ru_col;
9123 int off = 0;
9124 int width = Columns;
9125# define WITH_OFF(x) x
9126# define WITH_WIDTH(x) x
9127#else
9128# define WITH_OFF(x) 0
9129# define WITH_WIDTH(x) Columns
9130# define this_ru_col ru_col
9131#endif
9132
9133 /* If 'ruler' off or redrawing disabled, don't do anything */
9134 if (!p_ru)
9135 return;
9136
9137 /*
9138 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9139 * after deleting lines, before cursor.lnum is corrected.
9140 */
9141 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9142 return;
9143
9144#ifdef FEAT_INS_EXPAND
9145 /* Don't draw the ruler while doing insert-completion, it might overwrite
9146 * the (long) mode message. */
9147# ifdef FEAT_WINDOWS
9148 if (wp == lastwin && lastwin->w_status_height == 0)
9149# endif
9150 if (edit_submode != NULL)
9151 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009152 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9153 if (pum_visible())
9154 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155#endif
9156
9157#ifdef FEAT_STL_OPT
9158 if (*p_ruf)
9159 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009160 int save_called_emsg = called_emsg;
9161
9162 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009164 if (called_emsg)
9165 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009166 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009167 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 return;
9169 }
9170#endif
9171
9172 /*
9173 * Check if not in Insert mode and the line is empty (will show "0-1").
9174 */
9175 if (!(State & INSERT)
9176 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9177 empty_line = TRUE;
9178
9179 /*
9180 * Only draw the ruler when something changed.
9181 */
9182 validate_virtcol_win(wp);
9183 if ( redraw_cmdline
9184 || always
9185 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9186 || wp->w_cursor.col != wp->w_ru_cursor.col
9187 || wp->w_virtcol != wp->w_ru_virtcol
9188#ifdef FEAT_VIRTUALEDIT
9189 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9190#endif
9191 || wp->w_topline != wp->w_ru_topline
9192 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9193#ifdef FEAT_DIFF
9194 || wp->w_topfill != wp->w_ru_topfill
9195#endif
9196 || empty_line != wp->w_ru_empty)
9197 {
9198 cursor_off();
9199#ifdef FEAT_WINDOWS
9200 if (wp->w_status_height)
9201 {
9202 row = W_WINROW(wp) + wp->w_height;
9203 fillchar = fillchar_status(&attr, wp == curwin);
9204# ifdef FEAT_VERTSPLIT
9205 off = W_WINCOL(wp);
9206 width = W_WIDTH(wp);
9207# endif
9208 }
9209 else
9210#endif
9211 {
9212 row = Rows - 1;
9213 fillchar = ' ';
9214 attr = 0;
9215#ifdef FEAT_VERTSPLIT
9216 width = Columns;
9217 off = 0;
9218#endif
9219 }
9220
9221 /* In list mode virtcol needs to be recomputed */
9222 virtcol = wp->w_virtcol;
9223 if (wp->w_p_list && lcs_tab1 == NUL)
9224 {
9225 wp->w_p_list = FALSE;
9226 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9227 wp->w_p_list = TRUE;
9228 }
9229
9230 /*
9231 * Some sprintfs return the length, some return a pointer.
9232 * To avoid portability problems we use strlen() here.
9233 */
9234 sprintf((char *)buffer, "%ld,",
9235 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9236 ? 0L
9237 : (long)(wp->w_cursor.lnum));
9238 col_print(buffer + STRLEN(buffer),
9239 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9240 (int)virtcol + 1);
9241
9242 /*
9243 * Add a "50%" if there is room for it.
9244 * On the last line, don't print in the last column (scrolls the
9245 * screen up on some terminals).
9246 */
9247 i = (int)STRLEN(buffer);
9248 get_rel_pos(wp, buffer + i + 1);
9249 o = i + vim_strsize(buffer + i + 1);
9250#ifdef FEAT_WINDOWS
9251 if (wp->w_status_height == 0) /* can't use last char of screen */
9252#endif
9253 ++o;
9254#ifdef FEAT_VERTSPLIT
9255 this_ru_col = ru_col - (Columns - width);
9256 if (this_ru_col < 0)
9257 this_ru_col = 0;
9258#endif
9259 /* Never use more than half the window/screen width, leave the other
9260 * half for the filename. */
9261 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9262 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9263 if (this_ru_col + o < WITH_WIDTH(width))
9264 {
9265 while (this_ru_col + o < WITH_WIDTH(width))
9266 {
9267#ifdef FEAT_MBYTE
9268 if (has_mbyte)
9269 i += (*mb_char2bytes)(fillchar, buffer + i);
9270 else
9271#endif
9272 buffer[i++] = fillchar;
9273 ++o;
9274 }
9275 get_rel_pos(wp, buffer + i);
9276 }
9277 /* Truncate at window boundary. */
9278#ifdef FEAT_MBYTE
9279 if (has_mbyte)
9280 {
9281 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009282 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 {
9284 o += (*mb_ptr2cells)(buffer + i);
9285 if (this_ru_col + o > WITH_WIDTH(width))
9286 {
9287 buffer[i] = NUL;
9288 break;
9289 }
9290 }
9291 }
9292 else
9293#endif
9294 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9295 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9296
9297 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9298 i = redraw_cmdline;
9299 screen_fill(row, row + 1,
9300 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9301 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9302 fillchar, fillchar, attr);
9303 /* don't redraw the cmdline because of showing the ruler */
9304 redraw_cmdline = i;
9305 wp->w_ru_cursor = wp->w_cursor;
9306 wp->w_ru_virtcol = wp->w_virtcol;
9307 wp->w_ru_empty = empty_line;
9308 wp->w_ru_topline = wp->w_topline;
9309 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9310#ifdef FEAT_DIFF
9311 wp->w_ru_topfill = wp->w_topfill;
9312#endif
9313 }
9314}
9315#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009316
9317#if defined(FEAT_LINEBREAK) || defined(PROTO)
9318/*
9319 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009320 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009321 * Otherwise it depends on 'numberwidth' and the line count.
9322 */
9323 int
9324number_width(wp)
9325 win_T *wp;
9326{
9327 int n;
9328 linenr_T lnum;
9329
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009330 lnum = wp->w_buffer->b_ml.ml_line_count;
9331 if (lnum == wp->w_nrwidth_line_count)
9332 return wp->w_nrwidth_width;
9333 wp->w_nrwidth_line_count = lnum;
9334
9335 n = 0;
9336 do
9337 {
9338 lnum /= 10;
9339 ++n;
9340 } while (lnum > 0);
9341
9342 /* 'numberwidth' gives the minimal width plus one */
9343 if (n < wp->w_p_nuw - 1)
9344 n = wp->w_p_nuw - 1;
9345
9346 wp->w_nrwidth_width = n;
9347 return n;
9348}
9349#endif