blob: 8c5e20ce29c2c123dc7790fcd048ed02654601ff [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
28 * character without composing chars ScreenLinesUC[] will be 0. When the
29 * character occupies two display cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000030 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 * (drawn on top of the first character). They are 0 when not used.
32 * ScreenLines2[] is only used for euc-jp to store the second byte if the
33 * first byte is 0x8e (single-width character).
34 *
35 * The screen_*() functions write to the screen and handle updating
36 * ScreenLines[].
37 *
38 * update_screen() is the function that updates all windows and status lines.
39 * It is called form the main loop when must_redraw is non-zero. It may be
40 * called from other places when an immediated screen update is needed.
41 *
42 * The part of the buffer that is displayed in a window is set with:
43 * - w_topline (first buffer line in window)
44 * - w_topfill (filler line above the first line)
45 * - w_leftcol (leftmost window cell in window),
46 * - w_skipcol (skipped window cells of first line)
47 *
48 * Commands that only move the cursor around in a window, do not need to take
49 * action to update the display. The main loop will check if w_topline is
50 * valid and update it (scroll the window) when needed.
51 *
52 * Commands that scroll a window change w_topline and must call
53 * check_cursor() to move the cursor into the visible part of the window, and
54 * call redraw_later(VALID) to have the window displayed by update_screen()
55 * later.
56 *
57 * Commands that change text in the buffer must call changed_bytes() or
58 * changed_lines() to mark the area that changed and will require updating
59 * later. The main loop will call update_screen(), which will update each
60 * window that shows the changed buffer. This assumes text above the change
61 * can remain displayed as it is. Text after the change may need updating for
62 * scrolling, folding and syntax highlighting.
63 *
64 * Commands that change how a window is displayed (e.g., setting 'list') or
65 * invalidate the contents of a window in another way (e.g., change fold
66 * settings), must call redraw_later(NOT_VALID) to have the whole window
67 * redisplayed by update_screen() later.
68 *
69 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
70 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
71 * buffer redisplayed by update_screen() later.
72 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000073 * Commands that change highlighting and possibly cause a scroll too must call
74 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
75 * to avoid redrawing everything. But the length of displayed lines must not
76 * change, use NOT_VALID then.
77 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 * Commands that move the window position must call redraw_later(NOT_VALID).
79 * TODO: should minimize redrawing by scrolling when possible.
80 *
81 * Commands that change everything (e.g., resizing the screen) must call
82 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
83 *
84 * Things that are handled indirectly:
85 * - When messages scroll the screen up, msg_scrolled will be set and
86 * update_screen() called to redraw.
87 */
88
89#include "vim.h"
90
91/*
92 * The attributes that are actually active for writing to the screen.
93 */
94static int screen_attr = 0;
95
96/*
97 * Positioning the cursor is reduced by remembering the last position.
98 * Mostly used by windgoto() and screen_char().
99 */
100static int screen_cur_row, screen_cur_col; /* last known cursor position */
101
102#ifdef FEAT_SEARCH_EXTRA
103/*
104 * Struct used for highlighting 'hlsearch' matches for the last use search
105 * pattern or a ":match" item.
106 * For 'hlsearch' there is one pattern for all windows. For ":match" there is
107 * a different pattern for each window.
108 */
109typedef struct
110{
111 regmmatch_T rm; /* points to the regexp program; contains last found
112 match (may continue in next line) */
113 buf_T *buf; /* the buffer to search for a match */
114 linenr_T lnum; /* the line to search for a match */
115 int attr; /* attributes to be used for a match */
116 int attr_cur; /* attributes currently active in win_line() */
117 linenr_T first_lnum; /* first lnum to search for multi-line pat */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000118 colnr_T startcol; /* in win_line() points to char where HL starts */
119 colnr_T endcol; /* in win_line() points to char where HL ends */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120} match_T;
121
122static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000123static match_T match_hl[3]; /* used for ":match" highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#endif
125
126#ifdef FEAT_FOLDING
127static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
128#endif
129
130/*
131 * Buffer for one screen line (characters and attributes).
132 */
133static schar_T *current_ScreenLine;
134
135static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000136static void win_draw_end __ARGS((win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_FOLDING
138static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
139static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
140static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
141#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000142static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
144#ifdef FEAT_RIGHTLEFT
145static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
146# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
147#else
148static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
149# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_VERTSPLIT
152static void draw_vsep_win __ARGS((win_T *wp, int row));
153#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000154#ifdef FEAT_STL_OPT
155static void redraw_custum_statusline __ARGS((win_T *wp));
156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_SEARCH_EXTRA
158static void start_search_hl __ARGS((void));
159static void end_search_hl __ARGS((void));
160static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
161static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
162#endif
163static void screen_start_highlight __ARGS((int attr));
164static void screen_char __ARGS((unsigned off, int row, int col));
165#ifdef FEAT_MBYTE
166static void screen_char_2 __ARGS((unsigned off, int row, int col));
167#endif
168static void screenclear2 __ARGS((void));
169static void lineclear __ARGS((unsigned off, int width));
170static void lineinvalid __ARGS((unsigned off, int width));
171#ifdef FEAT_VERTSPLIT
172static void linecopy __ARGS((int to, int from, win_T *wp));
173static void redraw_block __ARGS((int row, int end, win_T *wp));
174#endif
175static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
176static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000178#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000179static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
182static int fillchar_status __ARGS((int *attr, int is_curwin));
183#endif
184#ifdef FEAT_VERTSPLIT
185static int fillchar_vsep __ARGS((int *attr));
186#endif
187#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000188static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#endif
190#ifdef FEAT_CMDL_INFO
191static void win_redr_ruler __ARGS((win_T *wp, int always));
192#endif
193
194#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
195/* Ugly global: overrule attribute used by screen_char() */
196static int screen_char_attr = 0;
197#endif
198
199/*
200 * Redraw the current window later, with update_screen(type).
201 * Set must_redraw only if not already set to a higher value.
202 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
203 */
204 void
205redraw_later(type)
206 int type;
207{
208 redraw_win_later(curwin, type);
209}
210
211 void
212redraw_win_later(wp, type)
213 win_T *wp;
214 int type;
215{
216 if (wp->w_redr_type < type)
217 {
218 wp->w_redr_type = type;
219 if (type >= NOT_VALID)
220 wp->w_lines_valid = 0;
221 if (must_redraw < type) /* must_redraw is the maximum of all windows */
222 must_redraw = type;
223 }
224}
225
226/*
227 * Force a complete redraw later. Also resets the highlighting. To be used
228 * after executing a shell command that messes up the screen.
229 */
230 void
231redraw_later_clear()
232{
233 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000234#ifdef FEAT_GUI
235 if (gui.in_use)
236 /* Use a code that will reset gui.highlight_mask in
237 * gui_stop_highlight(). */
238 screen_attr = HL_ALL + 1;
239 else
240#endif
241 /* Use attributes that is very unlikely to appear in text. */
242 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
246 * Mark all windows to be redrawn later.
247 */
248 void
249redraw_all_later(type)
250 int type;
251{
252 win_T *wp;
253
254 FOR_ALL_WINDOWS(wp)
255 {
256 redraw_win_later(wp, type);
257 }
258}
259
260/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000261 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 */
263 void
264redraw_curbuf_later(type)
265 int type;
266{
267 redraw_buf_later(curbuf, type);
268}
269
270 void
271redraw_buf_later(buf, type)
272 buf_T *buf;
273 int type;
274{
275 win_T *wp;
276
277 FOR_ALL_WINDOWS(wp)
278 {
279 if (wp->w_buffer == buf)
280 redraw_win_later(wp, type);
281 }
282}
283
284/*
285 * Changed something in the current window, at buffer line "lnum", that
286 * requires that line and possibly other lines to be redrawn.
287 * Used when entering/leaving Insert mode with the cursor on a folded line.
288 * Used to remove the "$" from a change command.
289 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
290 * may become invalid and the whole window will have to be redrawn.
291 */
292/*ARGSUSED*/
293 void
294redrawWinline(lnum, invalid)
295 linenr_T lnum;
296 int invalid; /* window line height is invalid now */
297{
298#ifdef FEAT_FOLDING
299 int i;
300#endif
301
302 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
303 curwin->w_redraw_top = lnum;
304 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
305 curwin->w_redraw_bot = lnum;
306 redraw_later(VALID);
307
308#ifdef FEAT_FOLDING
309 if (invalid)
310 {
311 /* A w_lines[] entry for this lnum has become invalid. */
312 i = find_wl_entry(curwin, lnum);
313 if (i >= 0)
314 curwin->w_lines[i].wl_valid = FALSE;
315 }
316#endif
317}
318
319/*
320 * update all windows that are editing the current buffer
321 */
322 void
323update_curbuf(type)
324 int type;
325{
326 redraw_curbuf_later(type);
327 update_screen(type);
328}
329
330/*
331 * update_screen()
332 *
333 * Based on the current value of curwin->w_topline, transfer a screenfull
334 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
335 */
336 void
337update_screen(type)
338 int type;
339{
340 win_T *wp;
341 static int did_intro = FALSE;
342#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
343 int did_one;
344#endif
345
346 if (!screen_valid(TRUE))
347 return;
348
349 if (must_redraw)
350 {
351 if (type < must_redraw) /* use maximal type */
352 type = must_redraw;
353 must_redraw = 0;
354 }
355
356 /* Need to update w_lines[]. */
357 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
358 type = NOT_VALID;
359
360 if (!redrawing())
361 {
362 redraw_later(type); /* remember type for next time */
363 must_redraw = type;
364 if (type > INVERTED_ALL)
365 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
366 return;
367 }
368
369 updating_screen = TRUE;
370#ifdef FEAT_SYN_HL
371 ++display_tick; /* let syntax code know we're in a next round of
372 * display updating */
373#endif
374
375 /*
376 * if the screen was scrolled up when displaying a message, scroll it down
377 */
378 if (msg_scrolled)
379 {
380 clear_cmdline = TRUE;
381 if (msg_scrolled > Rows - 5) /* clearing is faster */
382 type = CLEAR;
383 else if (type != CLEAR)
384 {
385 check_for_delay(FALSE);
386 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
387 type = CLEAR;
388 FOR_ALL_WINDOWS(wp)
389 {
390 if (W_WINROW(wp) < msg_scrolled)
391 {
392 if (W_WINROW(wp) + wp->w_height > msg_scrolled
393 && wp->w_redr_type < REDRAW_TOP
394 && wp->w_lines_valid > 0
395 && wp->w_topline == wp->w_lines[0].wl_lnum)
396 {
397 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
398 wp->w_redr_type = REDRAW_TOP;
399 }
400 else
401 {
402 wp->w_redr_type = NOT_VALID;
403#ifdef FEAT_WINDOWS
404 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
405 <= msg_scrolled)
406 wp->w_redr_status = TRUE;
407#endif
408 }
409 }
410 }
411 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000412#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000413 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 }
416 msg_scrolled = 0;
417 need_wait_return = FALSE;
418 }
419
420 /* reset cmdline_row now (may have been changed temporarily) */
421 compute_cmdrow();
422
423 /* Check for changed highlighting */
424 if (need_highlight_changed)
425 highlight_changed();
426
427 if (type == CLEAR) /* first clear screen */
428 {
429 screenclear(); /* will reset clear_cmdline */
430 type = NOT_VALID;
431 }
432
433 if (clear_cmdline) /* going to clear cmdline (done below) */
434 check_for_delay(FALSE);
435
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000436#ifdef FEAT_LINEBREAK
437 /* Force redraw when width of 'number' column changes. */
438 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000439 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000440 curwin->w_redr_type = NOT_VALID;
441#endif
442
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 /*
444 * Only start redrawing if there is really something to do.
445 */
446 if (type == INVERTED)
447 update_curswant();
448 if (curwin->w_redr_type < type
449 && !((type == VALID
450 && curwin->w_lines[0].wl_valid
451#ifdef FEAT_DIFF
452 && curwin->w_topfill == curwin->w_old_topfill
453 && curwin->w_botfill == curwin->w_old_botfill
454#endif
455 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
456#ifdef FEAT_VISUAL
457 || (type == INVERTED
458 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
459 && curwin->w_old_visual_mode == VIsual_mode
460 && (curwin->w_valid & VALID_VIRTCOL)
461 && curwin->w_old_curswant == curwin->w_curswant)
462#endif
463 ))
464 curwin->w_redr_type = type;
465
Bram Moolenaar5a305422006-04-28 22:38:25 +0000466#ifdef FEAT_WINDOWS
467 /* Redraw the tab pages line if needed. */
468 if (redraw_tabline || type >= NOT_VALID)
469 draw_tabline();
470#endif
471
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472#ifdef FEAT_SYN_HL
473 /*
474 * Correct stored syntax highlighting info for changes in each displayed
475 * buffer. Each buffer must only be done once.
476 */
477 FOR_ALL_WINDOWS(wp)
478 {
479 if (wp->w_buffer->b_mod_set)
480 {
481# ifdef FEAT_WINDOWS
482 win_T *wwp;
483
484 /* Check if we already did this buffer. */
485 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
486 if (wwp->w_buffer == wp->w_buffer)
487 break;
488# endif
489 if (
490# ifdef FEAT_WINDOWS
491 wwp == wp &&
492# endif
493 syntax_present(wp->w_buffer))
494 syn_stack_apply_changes(wp->w_buffer);
495 }
496 }
497#endif
498
499 /*
500 * Go from top to bottom through the windows, redrawing the ones that need
501 * it.
502 */
503#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
504 did_one = FALSE;
505#endif
506#ifdef FEAT_SEARCH_EXTRA
507 search_hl.rm.regprog = NULL;
508#endif
509 FOR_ALL_WINDOWS(wp)
510 {
511 if (wp->w_redr_type != 0)
512 {
513 cursor_off();
514#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
515 if (!did_one)
516 {
517 did_one = TRUE;
518# ifdef FEAT_SEARCH_EXTRA
519 start_search_hl();
520# endif
521# ifdef FEAT_CLIPBOARD
522 /* When Visual area changed, may have to update selection. */
523 if (clip_star.available && clip_isautosel())
524 clip_update_selection();
525# endif
526#ifdef FEAT_GUI
527 /* Remove the cursor before starting to do anything, because
528 * scrolling may make it difficult to redraw the text under
529 * it. */
530 if (gui.in_use)
531 gui_undraw_cursor();
532#endif
533 }
534#endif
535 win_update(wp);
536 }
537
538#ifdef FEAT_WINDOWS
539 /* redraw status line after the window to minimize cursor movement */
540 if (wp->w_redr_status)
541 {
542 cursor_off();
543 win_redr_status(wp);
544 }
545#endif
546 }
547#if defined(FEAT_SEARCH_EXTRA)
548 end_search_hl();
549#endif
550
551#ifdef FEAT_WINDOWS
552 /* Reset b_mod_set flags. Going through all windows is probably faster
553 * than going through all buffers (there could be many buffers). */
554 for (wp = firstwin; wp != NULL; wp = wp->w_next)
555 wp->w_buffer->b_mod_set = FALSE;
556#else
557 curbuf->b_mod_set = FALSE;
558#endif
559
560 updating_screen = FALSE;
561#ifdef FEAT_GUI
562 gui_may_resize_shell();
563#endif
564
565 /* Clear or redraw the command line. Done last, because scrolling may
566 * mess up the command line. */
567 if (clear_cmdline || redraw_cmdline)
568 showmode();
569
570 /* May put up an introductory message when not editing a file */
571 if (!did_intro && bufempty()
572 && curbuf->b_fname == NULL
573#ifdef FEAT_WINDOWS
574 && firstwin->w_next == NULL
575#endif
576 && vim_strchr(p_shm, SHM_INTRO) == NULL)
577 intro_message(FALSE);
578 did_intro = TRUE;
579
580#ifdef FEAT_GUI
581 /* Redraw the cursor and update the scrollbars when all screen updating is
582 * done. */
583 if (gui.in_use)
584 {
585 out_flush(); /* required before updating the cursor */
586 if (did_one)
587 gui_update_cursor(FALSE, FALSE);
588 gui_update_scrollbars(FALSE);
589 }
590#endif
591}
592
593#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
594static void update_prepare __ARGS((void));
595static void update_finish __ARGS((void));
596
597/*
598 * Prepare for updating one or more windows.
599 */
600 static void
601update_prepare()
602{
603 cursor_off();
604 updating_screen = TRUE;
605#ifdef FEAT_GUI
606 /* Remove the cursor before starting to do anything, because scrolling may
607 * make it difficult to redraw the text under it. */
608 if (gui.in_use)
609 gui_undraw_cursor();
610#endif
611#ifdef FEAT_SEARCH_EXTRA
612 start_search_hl();
613#endif
614}
615
616/*
617 * Finish updating one or more windows.
618 */
619 static void
620update_finish()
621{
622 if (redraw_cmdline)
623 showmode();
624
625# ifdef FEAT_SEARCH_EXTRA
626 end_search_hl();
627# endif
628
629 updating_screen = FALSE;
630
631# ifdef FEAT_GUI
632 gui_may_resize_shell();
633
634 /* Redraw the cursor and update the scrollbars when all screen updating is
635 * done. */
636 if (gui.in_use)
637 {
638 out_flush(); /* required before updating the cursor */
639 gui_update_cursor(FALSE, FALSE);
640 gui_update_scrollbars(FALSE);
641 }
642# endif
643}
644#endif
645
646#if defined(FEAT_SIGNS) || defined(PROTO)
647 void
648update_debug_sign(buf, lnum)
649 buf_T *buf;
650 linenr_T lnum;
651{
652 win_T *wp;
653 int doit = FALSE;
654
655# ifdef FEAT_FOLDING
656 win_foldinfo.fi_level = 0;
657# endif
658
659 /* update/delete a specific mark */
660 FOR_ALL_WINDOWS(wp)
661 {
662 if (buf != NULL && lnum > 0)
663 {
664 if (wp->w_buffer == buf && lnum >= wp->w_topline
665 && lnum < wp->w_botline)
666 {
667 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
668 wp->w_redraw_top = lnum;
669 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
670 wp->w_redraw_bot = lnum;
671 redraw_win_later(wp, VALID);
672 }
673 }
674 else
675 redraw_win_later(wp, VALID);
676 if (wp->w_redr_type != 0)
677 doit = TRUE;
678 }
679
680 if (!doit)
681 return;
682
683 /* update all windows that need updating */
684 update_prepare();
685
686# ifdef FEAT_WINDOWS
687 for (wp = firstwin; wp; wp = wp->w_next)
688 {
689 if (wp->w_redr_type != 0)
690 win_update(wp);
691 if (wp->w_redr_status)
692 win_redr_status(wp);
693 }
694# else
695 if (curwin->w_redr_type != 0)
696 win_update(curwin);
697# endif
698
699 update_finish();
700}
701#endif
702
703
704#if defined(FEAT_GUI) || defined(PROTO)
705/*
706 * Update a single window, its status line and maybe the command line msg.
707 * Used for the GUI scrollbar.
708 */
709 void
710updateWindow(wp)
711 win_T *wp;
712{
713 update_prepare();
714
715#ifdef FEAT_CLIPBOARD
716 /* When Visual area changed, may have to update selection. */
717 if (clip_star.available && clip_isautosel())
718 clip_update_selection();
719#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000724 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000725 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000726 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (wp->w_redr_status
729# ifdef FEAT_CMDL_INFO
730 || p_ru
731# endif
732# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000733 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734# endif
735 )
736 win_redr_status(wp);
737#endif
738
739 update_finish();
740}
741#endif
742
743/*
744 * Update a single window.
745 *
746 * This may cause the windows below it also to be redrawn (when clearing the
747 * screen or scrolling lines).
748 *
749 * How the window is redrawn depends on wp->w_redr_type. Each type also
750 * implies the one below it.
751 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000752 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
754 * INVERTED redraw the changed part of the Visual area
755 * INVERTED_ALL redraw the whole Visual area
756 * VALID 1. scroll up/down to adjust for a changed w_topline
757 * 2. update lines at the top when scrolled down
758 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000759 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 * b_mod_top and b_mod_bot.
761 * - if wp->w_redraw_top non-zero, redraw lines between
762 * wp->w_redraw_top and wp->w_redr_bot.
763 * - continue redrawing when syntax status is invalid.
764 * 4. if scrolled up, update lines at the bottom.
765 * This results in three areas that may need updating:
766 * top: from first row to top_end (when scrolled down)
767 * mid: from mid_start to mid_end (update inversion or changed text)
768 * bot: from bot_start to last row (when scrolled up)
769 */
770 static void
771win_update(wp)
772 win_T *wp;
773{
774 buf_T *buf = wp->w_buffer;
775 int type;
776 int top_end = 0; /* Below last row of the top area that needs
777 updating. 0 when no top area updating. */
778 int mid_start = 999;/* first row of the mid area that needs
779 updating. 999 when no mid area updating. */
780 int mid_end = 0; /* Below last row of the mid area that needs
781 updating. 0 when no mid area updating. */
782 int bot_start = 999;/* first row of the bot area that needs
783 updating. 999 when no bot area updating */
784#ifdef FEAT_VISUAL
785 int scrolled_down = FALSE; /* TRUE when scrolled down when
786 w_topline got smaller a bit */
787#endif
788#ifdef FEAT_SEARCH_EXTRA
789 int top_to_mod = FALSE; /* redraw above mod_top */
790#endif
791
792 int row; /* current window row to display */
793 linenr_T lnum; /* current buffer lnum to display */
794 int idx; /* current index in w_lines[] */
795 int srow; /* starting row of the current line */
796
797 int eof = FALSE; /* if TRUE, we hit the end of the file */
798 int didline = FALSE; /* if TRUE, we finished the last line */
799 int i;
800 long j;
801 static int recursive = FALSE; /* being called recursively */
802 int old_botline = wp->w_botline;
803#ifdef FEAT_FOLDING
804 long fold_count;
805#endif
806#ifdef FEAT_SYN_HL
807 /* remember what happened to the previous line, to know if
808 * check_visual_highlight() can be used */
809#define DID_NONE 1 /* didn't update a line */
810#define DID_LINE 2 /* updated a normal line */
811#define DID_FOLD 3 /* updated a folded line */
812 int did_update = DID_NONE;
813 linenr_T syntax_last_parsed = 0; /* last parsed text line */
814#endif
815 linenr_T mod_top = 0;
816 linenr_T mod_bot = 0;
817#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
818 int save_got_int;
819#endif
820
821 type = wp->w_redr_type;
822
823 if (type == NOT_VALID)
824 {
825#ifdef FEAT_WINDOWS
826 wp->w_redr_status = TRUE;
827#endif
828 wp->w_lines_valid = 0;
829 }
830
831 /* Window is zero-height: nothing to draw. */
832 if (wp->w_height == 0)
833 {
834 wp->w_redr_type = 0;
835 return;
836 }
837
838#ifdef FEAT_VERTSPLIT
839 /* Window is zero-width: Only need to draw the separator. */
840 if (wp->w_width == 0)
841 {
842 /* draw the vertical separator right of this window */
843 draw_vsep_win(wp, 0);
844 wp->w_redr_type = 0;
845 return;
846 }
847#endif
848
849#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000850 /* Setup for ":match" and 'hlsearch' highlighting. Disable any previous
851 * match */
852 for (i = 0; i < 3; ++i)
853 {
854 match_hl[i].rm = wp->w_match[i];
855 if (wp->w_match_id[i] == 0)
856 match_hl[i].attr = 0;
857 else
858 match_hl[i].attr = syn_id2attr(wp->w_match_id[i]);
859 match_hl[i].buf = buf;
860 match_hl[i].lnum = 0;
861 match_hl[i].first_lnum = 0;
862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 search_hl.buf = buf;
864 search_hl.lnum = 0;
865 search_hl.first_lnum = 0;
866#endif
867
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000868#ifdef FEAT_LINEBREAK
869 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000870 i = wp->w_p_nu ? number_width(wp) : 0;
871 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000872 {
873 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000874 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000875 }
876 else
877#endif
878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
880 {
881 /*
882 * When there are both inserted/deleted lines and specific lines to be
883 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
884 * everything (only happens when redrawing is off for while).
885 */
886 type = NOT_VALID;
887 }
888 else
889 {
890 /*
891 * Set mod_top to the first line that needs displaying because of
892 * changes. Set mod_bot to the first line after the changes.
893 */
894 mod_top = wp->w_redraw_top;
895 if (wp->w_redraw_bot != 0)
896 mod_bot = wp->w_redraw_bot + 1;
897 else
898 mod_bot = 0;
899 wp->w_redraw_top = 0; /* reset for next time */
900 wp->w_redraw_bot = 0;
901 if (buf->b_mod_set)
902 {
903 if (mod_top == 0 || mod_top > buf->b_mod_top)
904 {
905 mod_top = buf->b_mod_top;
906#ifdef FEAT_SYN_HL
907 /* Need to redraw lines above the change that may be included
908 * in a pattern match. */
909 if (syntax_present(buf))
910 {
911 mod_top -= buf->b_syn_sync_linebreaks;
912 if (mod_top < 1)
913 mod_top = 1;
914 }
915#endif
916 }
917 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
918 mod_bot = buf->b_mod_bot;
919
920#ifdef FEAT_SEARCH_EXTRA
921 /* When 'hlsearch' is on and using a multi-line search pattern, a
922 * change in one line may make the Search highlighting in a
923 * previous line invalid. Simple solution: redraw all visible
924 * lines above the change.
925 * Same for a ":match" pattern.
926 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000927 if (search_hl.rm.regprog != NULL
928 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000930 else
931 for (i = 0; i < 3; ++i)
932 if (match_hl[i].rm.regprog != NULL
933 && re_multiline(match_hl[i].rm.regprog))
934 {
935 top_to_mod = TRUE;
936 break;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#endif
939 }
940#ifdef FEAT_FOLDING
941 if (mod_top != 0 && hasAnyFolding(wp))
942 {
943 linenr_T lnumt, lnumb;
944
945 /*
946 * A change in a line can cause lines above it to become folded or
947 * unfolded. Find the top most buffer line that may be affected.
948 * If the line was previously folded and displayed, get the first
949 * line of that fold. If the line is folded now, get the first
950 * folded line. Use the minimum of these two.
951 */
952
953 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
954 * the line below it. If there is no valid entry, use w_topline.
955 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
956 * to this line. If there is no valid entry, use MAXLNUM. */
957 lnumt = wp->w_topline;
958 lnumb = MAXLNUM;
959 for (i = 0; i < wp->w_lines_valid; ++i)
960 if (wp->w_lines[i].wl_valid)
961 {
962 if (wp->w_lines[i].wl_lastlnum < mod_top)
963 lnumt = wp->w_lines[i].wl_lastlnum + 1;
964 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
965 {
966 lnumb = wp->w_lines[i].wl_lnum;
967 /* When there is a fold column it might need updating
968 * in the next line ("J" just above an open fold). */
969 if (wp->w_p_fdc > 0)
970 ++lnumb;
971 }
972 }
973
974 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
975 if (mod_top > lnumt)
976 mod_top = lnumt;
977
978 /* Now do the same for the bottom line (one above mod_bot). */
979 --mod_bot;
980 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
981 ++mod_bot;
982 if (mod_bot < lnumb)
983 mod_bot = lnumb;
984 }
985#endif
986
987 /* When a change starts above w_topline and the end is below
988 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000989 * If the end of the change is above w_topline: do like no change was
990 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (mod_top != 0 && mod_top < wp->w_topline)
992 {
993 if (mod_bot > wp->w_topline)
994 mod_top = wp->w_topline;
995#ifdef FEAT_SYN_HL
996 else if (syntax_present(buf))
997 top_end = 1;
998#endif
999 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001000
1001 /* When line numbers are displayed need to redraw all lines below
1002 * inserted/deleted lines. */
1003 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1004 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006
1007 /*
1008 * When only displaying the lines at the top, set top_end. Used when
1009 * window has scrolled down for msg_scrolled.
1010 */
1011 if (type == REDRAW_TOP)
1012 {
1013 j = 0;
1014 for (i = 0; i < wp->w_lines_valid; ++i)
1015 {
1016 j += wp->w_lines[i].wl_size;
1017 if (j >= wp->w_upd_rows)
1018 {
1019 top_end = j;
1020 break;
1021 }
1022 }
1023 if (top_end == 0)
1024 /* not found (cannot happen?): redraw everything */
1025 type = NOT_VALID;
1026 else
1027 /* top area defined, the rest is VALID */
1028 type = VALID;
1029 }
1030
1031 /*
1032 * If there are no changes on the screen that require a complete redraw,
1033 * handle three cases:
1034 * 1: we are off the top of the screen by a few lines: scroll down
1035 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1036 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1037 * w_lines[] that needs updating.
1038 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001039 if ((type == VALID || type == SOME_VALID
1040 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041#ifdef FEAT_DIFF
1042 && !wp->w_botfill && !wp->w_old_botfill
1043#endif
1044 )
1045 {
1046 if (mod_top != 0 && wp->w_topline == mod_top)
1047 {
1048 /*
1049 * w_topline is the first changed line, the scrolling will be done
1050 * further down.
1051 */
1052 }
1053 else if (wp->w_lines[0].wl_valid
1054 && (wp->w_topline < wp->w_lines[0].wl_lnum
1055#ifdef FEAT_DIFF
1056 || (wp->w_topline == wp->w_lines[0].wl_lnum
1057 && wp->w_topfill > wp->w_old_topfill)
1058#endif
1059 ))
1060 {
1061 /*
1062 * New topline is above old topline: May scroll down.
1063 */
1064#ifdef FEAT_FOLDING
1065 if (hasAnyFolding(wp))
1066 {
1067 linenr_T ln;
1068
1069 /* count the number of lines we are off, counting a sequence
1070 * of folded lines as one */
1071 j = 0;
1072 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1073 {
1074 ++j;
1075 if (j >= wp->w_height - 2)
1076 break;
1077 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1078 }
1079 }
1080 else
1081#endif
1082 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1083 if (j < wp->w_height - 2) /* not too far off */
1084 {
1085 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1086#ifdef FEAT_DIFF
1087 /* insert extra lines for previously invisible filler lines */
1088 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1089 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1090 - wp->w_old_topfill;
1091#endif
1092 if (i < wp->w_height - 2) /* less than a screen off */
1093 {
1094 /*
1095 * Try to insert the correct number of lines.
1096 * If not the last window, delete the lines at the bottom.
1097 * win_ins_lines may fail when the terminal can't do it.
1098 */
1099 if (i > 0)
1100 check_for_delay(FALSE);
1101 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1102 {
1103 if (wp->w_lines_valid != 0)
1104 {
1105 /* Need to update rows that are new, stop at the
1106 * first one that scrolled down. */
1107 top_end = i;
1108#ifdef FEAT_VISUAL
1109 scrolled_down = TRUE;
1110#endif
1111
1112 /* Move the entries that were scrolled, disable
1113 * the entries for the lines to be redrawn. */
1114 if ((wp->w_lines_valid += j) > wp->w_height)
1115 wp->w_lines_valid = wp->w_height;
1116 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1117 wp->w_lines[idx] = wp->w_lines[idx - j];
1118 while (idx >= 0)
1119 wp->w_lines[idx--].wl_valid = FALSE;
1120 }
1121 }
1122 else
1123 mid_start = 0; /* redraw all lines */
1124 }
1125 else
1126 mid_start = 0; /* redraw all lines */
1127 }
1128 else
1129 mid_start = 0; /* redraw all lines */
1130 }
1131 else
1132 {
1133 /*
1134 * New topline is at or below old topline: May scroll up.
1135 * When topline didn't change, find first entry in w_lines[] that
1136 * needs updating.
1137 */
1138
1139 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1140 j = -1;
1141 row = 0;
1142 for (i = 0; i < wp->w_lines_valid; i++)
1143 {
1144 if (wp->w_lines[i].wl_valid
1145 && wp->w_lines[i].wl_lnum == wp->w_topline)
1146 {
1147 j = i;
1148 break;
1149 }
1150 row += wp->w_lines[i].wl_size;
1151 }
1152 if (j == -1)
1153 {
1154 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1155 * lines */
1156 mid_start = 0;
1157 }
1158 else
1159 {
1160 /*
1161 * Try to delete the correct number of lines.
1162 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1163 */
1164#ifdef FEAT_DIFF
1165 /* If the topline didn't change, delete old filler lines,
1166 * otherwise delete filler lines of the new topline... */
1167 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1168 row += wp->w_old_topfill;
1169 else
1170 row += diff_check_fill(wp, wp->w_topline);
1171 /* ... but don't delete new filler lines. */
1172 row -= wp->w_topfill;
1173#endif
1174 if (row > 0)
1175 {
1176 check_for_delay(FALSE);
1177 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1178 bot_start = wp->w_height - row;
1179 else
1180 mid_start = 0; /* redraw all lines */
1181 }
1182 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1183 {
1184 /*
1185 * Skip the lines (below the deleted lines) that are still
1186 * valid and don't need redrawing. Copy their info
1187 * upwards, to compensate for the deleted lines. Set
1188 * bot_start to the first row that needs redrawing.
1189 */
1190 bot_start = 0;
1191 idx = 0;
1192 for (;;)
1193 {
1194 wp->w_lines[idx] = wp->w_lines[j];
1195 /* stop at line that didn't fit, unless it is still
1196 * valid (no lines deleted) */
1197 if (row > 0 && bot_start + row
1198 + (int)wp->w_lines[j].wl_size > wp->w_height)
1199 {
1200 wp->w_lines_valid = idx + 1;
1201 break;
1202 }
1203 bot_start += wp->w_lines[idx++].wl_size;
1204
1205 /* stop at the last valid entry in w_lines[].wl_size */
1206 if (++j >= wp->w_lines_valid)
1207 {
1208 wp->w_lines_valid = idx;
1209 break;
1210 }
1211 }
1212#ifdef FEAT_DIFF
1213 /* Correct the first entry for filler lines at the top
1214 * when it won't get updated below. */
1215 if (wp->w_p_diff && bot_start > 0)
1216 wp->w_lines[0].wl_size =
1217 plines_win_nofill(wp, wp->w_topline, TRUE)
1218 + wp->w_topfill;
1219#endif
1220 }
1221 }
1222 }
1223
1224 /* When starting redraw in the first line, redraw all lines. When
1225 * there is only one window it's probably faster to clear the screen
1226 * first. */
1227 if (mid_start == 0)
1228 {
1229 mid_end = wp->w_height;
1230 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001233#ifdef FEAT_WINDOWS
1234 /* The screen was cleared, redraw the tab pages line. */
1235 if (redraw_tabline)
1236 draw_tabline();
1237#endif
1238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240 }
1241 else
1242 {
1243 /* Not VALID or INVERTED: redraw all lines. */
1244 mid_start = 0;
1245 mid_end = wp->w_height;
1246 }
1247
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001248 if (type == SOME_VALID)
1249 {
1250 /* SOME_VALID: redraw all lines. */
1251 mid_start = 0;
1252 mid_end = wp->w_height;
1253 type = NOT_VALID;
1254 }
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#ifdef FEAT_VISUAL
1257 /* check if we are updating or removing the inverted part */
1258 if ((VIsual_active && buf == curwin->w_buffer)
1259 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1260 {
1261 linenr_T from, to;
1262
1263 if (VIsual_active)
1264 {
1265 if (VIsual_active
1266 && (VIsual_mode != wp->w_old_visual_mode
1267 || type == INVERTED_ALL))
1268 {
1269 /*
1270 * If the type of Visual selection changed, redraw the whole
1271 * selection. Also when the ownership of the X selection is
1272 * gained or lost.
1273 */
1274 if (curwin->w_cursor.lnum < VIsual.lnum)
1275 {
1276 from = curwin->w_cursor.lnum;
1277 to = VIsual.lnum;
1278 }
1279 else
1280 {
1281 from = VIsual.lnum;
1282 to = curwin->w_cursor.lnum;
1283 }
1284 /* redraw more when the cursor moved as well */
1285 if (wp->w_old_cursor_lnum < from)
1286 from = wp->w_old_cursor_lnum;
1287 if (wp->w_old_cursor_lnum > to)
1288 to = wp->w_old_cursor_lnum;
1289 if (wp->w_old_visual_lnum < from)
1290 from = wp->w_old_visual_lnum;
1291 if (wp->w_old_visual_lnum > to)
1292 to = wp->w_old_visual_lnum;
1293 }
1294 else
1295 {
1296 /*
1297 * Find the line numbers that need to be updated: The lines
1298 * between the old cursor position and the current cursor
1299 * position. Also check if the Visual position changed.
1300 */
1301 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1302 {
1303 from = curwin->w_cursor.lnum;
1304 to = wp->w_old_cursor_lnum;
1305 }
1306 else
1307 {
1308 from = wp->w_old_cursor_lnum;
1309 to = curwin->w_cursor.lnum;
1310 if (from == 0) /* Visual mode just started */
1311 from = to;
1312 }
1313
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001314 if (VIsual.lnum != wp->w_old_visual_lnum
1315 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
1317 if (wp->w_old_visual_lnum < from
1318 && wp->w_old_visual_lnum != 0)
1319 from = wp->w_old_visual_lnum;
1320 if (wp->w_old_visual_lnum > to)
1321 to = wp->w_old_visual_lnum;
1322 if (VIsual.lnum < from)
1323 from = VIsual.lnum;
1324 if (VIsual.lnum > to)
1325 to = VIsual.lnum;
1326 }
1327 }
1328
1329 /*
1330 * If in block mode and changed column or curwin->w_curswant:
1331 * update all lines.
1332 * First compute the actual start and end column.
1333 */
1334 if (VIsual_mode == Ctrl_V)
1335 {
1336 colnr_T fromc, toc;
1337
1338 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1339 ++toc;
1340 if (curwin->w_curswant == MAXCOL)
1341 toc = MAXCOL;
1342
1343 if (fromc != wp->w_old_cursor_fcol
1344 || toc != wp->w_old_cursor_lcol)
1345 {
1346 if (from > VIsual.lnum)
1347 from = VIsual.lnum;
1348 if (to < VIsual.lnum)
1349 to = VIsual.lnum;
1350 }
1351 wp->w_old_cursor_fcol = fromc;
1352 wp->w_old_cursor_lcol = toc;
1353 }
1354 }
1355 else
1356 {
1357 /* Use the line numbers of the old Visual area. */
1358 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1359 {
1360 from = wp->w_old_cursor_lnum;
1361 to = wp->w_old_visual_lnum;
1362 }
1363 else
1364 {
1365 from = wp->w_old_visual_lnum;
1366 to = wp->w_old_cursor_lnum;
1367 }
1368 }
1369
1370 /*
1371 * There is no need to update lines above the top of the window.
1372 */
1373 if (from < wp->w_topline)
1374 from = wp->w_topline;
1375
1376 /*
1377 * If we know the value of w_botline, use it to restrict the update to
1378 * the lines that are visible in the window.
1379 */
1380 if (wp->w_valid & VALID_BOTLINE)
1381 {
1382 if (from >= wp->w_botline)
1383 from = wp->w_botline - 1;
1384 if (to >= wp->w_botline)
1385 to = wp->w_botline - 1;
1386 }
1387
1388 /*
1389 * Find the minimal part to be updated.
1390 * Watch out for scrolling that made entries in w_lines[] invalid.
1391 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1392 * top_end; need to redraw from top_end to the "to" line.
1393 * A middle mouse click with a Visual selection may change the text
1394 * above the Visual area and reset wl_valid, do count these for
1395 * mid_end (in srow).
1396 */
1397 if (mid_start > 0)
1398 {
1399 lnum = wp->w_topline;
1400 idx = 0;
1401 srow = 0;
1402 if (scrolled_down)
1403 mid_start = top_end;
1404 else
1405 mid_start = 0;
1406 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1407 {
1408 if (wp->w_lines[idx].wl_valid)
1409 mid_start += wp->w_lines[idx].wl_size;
1410 else if (!scrolled_down)
1411 srow += wp->w_lines[idx].wl_size;
1412 ++idx;
1413# ifdef FEAT_FOLDING
1414 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1415 lnum = wp->w_lines[idx].wl_lnum;
1416 else
1417# endif
1418 ++lnum;
1419 }
1420 srow += mid_start;
1421 mid_end = wp->w_height;
1422 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1423 {
1424 if (wp->w_lines[idx].wl_valid
1425 && wp->w_lines[idx].wl_lnum >= to + 1)
1426 {
1427 /* Only update until first row of this line */
1428 mid_end = srow;
1429 break;
1430 }
1431 srow += wp->w_lines[idx].wl_size;
1432 }
1433 }
1434 }
1435
1436 if (VIsual_active && buf == curwin->w_buffer)
1437 {
1438 wp->w_old_visual_mode = VIsual_mode;
1439 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1440 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001441 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 wp->w_old_curswant = curwin->w_curswant;
1443 }
1444 else
1445 {
1446 wp->w_old_visual_mode = 0;
1447 wp->w_old_cursor_lnum = 0;
1448 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001449 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451#endif /* FEAT_VISUAL */
1452
1453#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1454 /* reset got_int, otherwise regexp won't work */
1455 save_got_int = got_int;
1456 got_int = 0;
1457#endif
1458#ifdef FEAT_FOLDING
1459 win_foldinfo.fi_level = 0;
1460#endif
1461
1462 /*
1463 * Update all the window rows.
1464 */
1465 idx = 0; /* first entry in w_lines[].wl_size */
1466 row = 0;
1467 srow = 0;
1468 lnum = wp->w_topline; /* first line shown in window */
1469 for (;;)
1470 {
1471 /* stop updating when reached the end of the window (check for _past_
1472 * the end of the window is at the end of the loop) */
1473 if (row == wp->w_height)
1474 {
1475 didline = TRUE;
1476 break;
1477 }
1478
1479 /* stop updating when hit the end of the file */
1480 if (lnum > buf->b_ml.ml_line_count)
1481 {
1482 eof = TRUE;
1483 break;
1484 }
1485
1486 /* Remember the starting row of the line that is going to be dealt
1487 * with. It is used further down when the line doesn't fit. */
1488 srow = row;
1489
1490 /*
1491 * Update a line when it is in an area that needs updating, when it
1492 * has changes or w_lines[idx] is invalid.
1493 * bot_start may be halfway a wrapped line after using
1494 * win_del_lines(), check if the current line includes it.
1495 * When syntax folding is being used, the saved syntax states will
1496 * already have been updated, we can't see where the syntax state is
1497 * the same again, just update until the end of the window.
1498 */
1499 if (row < top_end
1500 || (row >= mid_start && row < mid_end)
1501#ifdef FEAT_SEARCH_EXTRA
1502 || top_to_mod
1503#endif
1504 || idx >= wp->w_lines_valid
1505 || (row + wp->w_lines[idx].wl_size > bot_start)
1506 || (mod_top != 0
1507 && (lnum == mod_top
1508 || (lnum >= mod_top
1509 && (lnum < mod_bot
1510#ifdef FEAT_SYN_HL
1511 || did_update == DID_FOLD
1512 || (did_update == DID_LINE
1513 && syntax_present(buf)
1514 && (
1515# ifdef FEAT_FOLDING
1516 (foldmethodIsSyntax(wp)
1517 && hasAnyFolding(wp)) ||
1518# endif
1519 syntax_check_changed(lnum)))
1520#endif
1521 )))))
1522 {
1523#ifdef FEAT_SEARCH_EXTRA
1524 if (lnum == mod_top)
1525 top_to_mod = FALSE;
1526#endif
1527
1528 /*
1529 * When at start of changed lines: May scroll following lines
1530 * up or down to minimize redrawing.
1531 * Don't do this when the change continues until the end.
1532 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1533 */
1534 if (lnum == mod_top
1535 && mod_bot != MAXLNUM
1536 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1537 {
1538 int old_rows = 0;
1539 int new_rows = 0;
1540 int xtra_rows;
1541 linenr_T l;
1542
1543 /* Count the old number of window rows, using w_lines[], which
1544 * should still contain the sizes for the lines as they are
1545 * currently displayed. */
1546 for (i = idx; i < wp->w_lines_valid; ++i)
1547 {
1548 /* Only valid lines have a meaningful wl_lnum. Invalid
1549 * lines are part of the changed area. */
1550 if (wp->w_lines[i].wl_valid
1551 && wp->w_lines[i].wl_lnum == mod_bot)
1552 break;
1553 old_rows += wp->w_lines[i].wl_size;
1554#ifdef FEAT_FOLDING
1555 if (wp->w_lines[i].wl_valid
1556 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1557 {
1558 /* Must have found the last valid entry above mod_bot.
1559 * Add following invalid entries. */
1560 ++i;
1561 while (i < wp->w_lines_valid
1562 && !wp->w_lines[i].wl_valid)
1563 old_rows += wp->w_lines[i++].wl_size;
1564 break;
1565 }
1566#endif
1567 }
1568
1569 if (i >= wp->w_lines_valid)
1570 {
1571 /* We can't find a valid line below the changed lines,
1572 * need to redraw until the end of the window.
1573 * Inserting/deleting lines has no use. */
1574 bot_start = 0;
1575 }
1576 else
1577 {
1578 /* Able to count old number of rows: Count new window
1579 * rows, and may insert/delete lines */
1580 j = idx;
1581 for (l = lnum; l < mod_bot; ++l)
1582 {
1583#ifdef FEAT_FOLDING
1584 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1585 ++new_rows;
1586 else
1587#endif
1588#ifdef FEAT_DIFF
1589 if (l == wp->w_topline)
1590 new_rows += plines_win_nofill(wp, l, TRUE)
1591 + wp->w_topfill;
1592 else
1593#endif
1594 new_rows += plines_win(wp, l, TRUE);
1595 ++j;
1596 if (new_rows > wp->w_height - row - 2)
1597 {
1598 /* it's getting too much, must redraw the rest */
1599 new_rows = 9999;
1600 break;
1601 }
1602 }
1603 xtra_rows = new_rows - old_rows;
1604 if (xtra_rows < 0)
1605 {
1606 /* May scroll text up. If there is not enough
1607 * remaining text or scrolling fails, must redraw the
1608 * rest. If scrolling works, must redraw the text
1609 * below the scrolled text. */
1610 if (row - xtra_rows >= wp->w_height - 2)
1611 mod_bot = MAXLNUM;
1612 else
1613 {
1614 check_for_delay(FALSE);
1615 if (win_del_lines(wp, row,
1616 -xtra_rows, FALSE, FALSE) == FAIL)
1617 mod_bot = MAXLNUM;
1618 else
1619 bot_start = wp->w_height + xtra_rows;
1620 }
1621 }
1622 else if (xtra_rows > 0)
1623 {
1624 /* May scroll text down. If there is not enough
1625 * remaining text of scrolling fails, must redraw the
1626 * rest. */
1627 if (row + xtra_rows >= wp->w_height - 2)
1628 mod_bot = MAXLNUM;
1629 else
1630 {
1631 check_for_delay(FALSE);
1632 if (win_ins_lines(wp, row + old_rows,
1633 xtra_rows, FALSE, FALSE) == FAIL)
1634 mod_bot = MAXLNUM;
1635 else if (top_end > row + old_rows)
1636 /* Scrolled the part at the top that requires
1637 * updating down. */
1638 top_end += xtra_rows;
1639 }
1640 }
1641
1642 /* When not updating the rest, may need to move w_lines[]
1643 * entries. */
1644 if (mod_bot != MAXLNUM && i != j)
1645 {
1646 if (j < i)
1647 {
1648 int x = row + new_rows;
1649
1650 /* move entries in w_lines[] upwards */
1651 for (;;)
1652 {
1653 /* stop at last valid entry in w_lines[] */
1654 if (i >= wp->w_lines_valid)
1655 {
1656 wp->w_lines_valid = j;
1657 break;
1658 }
1659 wp->w_lines[j] = wp->w_lines[i];
1660 /* stop at a line that won't fit */
1661 if (x + (int)wp->w_lines[j].wl_size
1662 > wp->w_height)
1663 {
1664 wp->w_lines_valid = j + 1;
1665 break;
1666 }
1667 x += wp->w_lines[j++].wl_size;
1668 ++i;
1669 }
1670 if (bot_start > x)
1671 bot_start = x;
1672 }
1673 else /* j > i */
1674 {
1675 /* move entries in w_lines[] downwards */
1676 j -= i;
1677 wp->w_lines_valid += j;
1678 if (wp->w_lines_valid > wp->w_height)
1679 wp->w_lines_valid = wp->w_height;
1680 for (i = wp->w_lines_valid; i - j >= idx; --i)
1681 wp->w_lines[i] = wp->w_lines[i - j];
1682
1683 /* The w_lines[] entries for inserted lines are
1684 * now invalid, but wl_size may be used above.
1685 * Reset to zero. */
1686 while (i >= idx)
1687 {
1688 wp->w_lines[i].wl_size = 0;
1689 wp->w_lines[i--].wl_valid = FALSE;
1690 }
1691 }
1692 }
1693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
1695
1696#ifdef FEAT_FOLDING
1697 /*
1698 * When lines are folded, display one line for all of them.
1699 * Otherwise, display normally (can be several display lines when
1700 * 'wrap' is on).
1701 */
1702 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1703 if (fold_count != 0)
1704 {
1705 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1706 ++row;
1707 --fold_count;
1708 wp->w_lines[idx].wl_folded = TRUE;
1709 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1710# ifdef FEAT_SYN_HL
1711 did_update = DID_FOLD;
1712# endif
1713 }
1714 else
1715#endif
1716 if (idx < wp->w_lines_valid
1717 && wp->w_lines[idx].wl_valid
1718 && wp->w_lines[idx].wl_lnum == lnum
1719 && lnum > wp->w_topline
1720 && !(dy_flags & DY_LASTLINE)
1721 && srow + wp->w_lines[idx].wl_size > wp->w_height
1722#ifdef FEAT_DIFF
1723 && diff_check_fill(wp, lnum) == 0
1724#endif
1725 )
1726 {
1727 /* This line is not going to fit. Don't draw anything here,
1728 * will draw "@ " lines below. */
1729 row = wp->w_height + 1;
1730 }
1731 else
1732 {
1733#ifdef FEAT_SEARCH_EXTRA
1734 prepare_search_hl(wp, lnum);
1735#endif
1736#ifdef FEAT_SYN_HL
1737 /* Let the syntax stuff know we skipped a few lines. */
1738 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1739 && syntax_present(buf))
1740 syntax_end_parsing(syntax_last_parsed + 1);
1741#endif
1742
1743 /*
1744 * Display one line.
1745 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001746 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748#ifdef FEAT_FOLDING
1749 wp->w_lines[idx].wl_folded = FALSE;
1750 wp->w_lines[idx].wl_lastlnum = lnum;
1751#endif
1752#ifdef FEAT_SYN_HL
1753 did_update = DID_LINE;
1754 syntax_last_parsed = lnum;
1755#endif
1756 }
1757
1758 wp->w_lines[idx].wl_lnum = lnum;
1759 wp->w_lines[idx].wl_valid = TRUE;
1760 if (row > wp->w_height) /* past end of screen */
1761 {
1762 /* we may need the size of that too long line later on */
1763 if (dollar_vcol == 0)
1764 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1765 ++idx;
1766 break;
1767 }
1768 if (dollar_vcol == 0)
1769 wp->w_lines[idx].wl_size = row - srow;
1770 ++idx;
1771#ifdef FEAT_FOLDING
1772 lnum += fold_count + 1;
1773#else
1774 ++lnum;
1775#endif
1776 }
1777 else
1778 {
1779 /* This line does not need updating, advance to the next one */
1780 row += wp->w_lines[idx++].wl_size;
1781 if (row > wp->w_height) /* past end of screen */
1782 break;
1783#ifdef FEAT_FOLDING
1784 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1785#else
1786 ++lnum;
1787#endif
1788#ifdef FEAT_SYN_HL
1789 did_update = DID_NONE;
1790#endif
1791 }
1792
1793 if (lnum > buf->b_ml.ml_line_count)
1794 {
1795 eof = TRUE;
1796 break;
1797 }
1798 }
1799 /*
1800 * End of loop over all window lines.
1801 */
1802
1803
1804 if (idx > wp->w_lines_valid)
1805 wp->w_lines_valid = idx;
1806
1807#ifdef FEAT_SYN_HL
1808 /*
1809 * Let the syntax stuff know we stop parsing here.
1810 */
1811 if (syntax_last_parsed != 0 && syntax_present(buf))
1812 syntax_end_parsing(syntax_last_parsed + 1);
1813#endif
1814
1815 /*
1816 * If we didn't hit the end of the file, and we didn't finish the last
1817 * line we were working on, then the line didn't fit.
1818 */
1819 wp->w_empty_rows = 0;
1820#ifdef FEAT_DIFF
1821 wp->w_filler_rows = 0;
1822#endif
1823 if (!eof && !didline)
1824 {
1825 if (lnum == wp->w_topline)
1826 {
1827 /*
1828 * Single line that does not fit!
1829 * Don't overwrite it, it can be edited.
1830 */
1831 wp->w_botline = lnum + 1;
1832 }
1833#ifdef FEAT_DIFF
1834 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1835 {
1836 /* Window ends in filler lines. */
1837 wp->w_botline = lnum;
1838 wp->w_filler_rows = wp->w_height - srow;
1839 }
1840#endif
1841 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1842 {
1843 /*
1844 * Last line isn't finished: Display "@@@" at the end.
1845 */
1846 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1847 W_WINROW(wp) + wp->w_height,
1848 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1849 '@', '@', hl_attr(HLF_AT));
1850 set_empty_rows(wp, srow);
1851 wp->w_botline = lnum;
1852 }
1853 else
1854 {
1855 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1856 wp->w_botline = lnum;
1857 }
1858 }
1859 else
1860 {
1861#ifdef FEAT_VERTSPLIT
1862 draw_vsep_win(wp, row);
1863#endif
1864 if (eof) /* we hit the end of the file */
1865 {
1866 wp->w_botline = buf->b_ml.ml_line_count + 1;
1867#ifdef FEAT_DIFF
1868 j = diff_check_fill(wp, wp->w_botline);
1869 if (j > 0 && !wp->w_botfill)
1870 {
1871 /*
1872 * Display filler lines at the end of the file
1873 */
1874 if (char2cells(fill_diff) > 1)
1875 i = '-';
1876 else
1877 i = fill_diff;
1878 if (row + j > wp->w_height)
1879 j = wp->w_height - row;
1880 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1881 row += j;
1882 }
1883#endif
1884 }
1885 else if (dollar_vcol == 0)
1886 wp->w_botline = lnum;
1887
1888 /* make sure the rest of the screen is blank */
1889 /* put '~'s on rows that aren't part of the file. */
1890 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1891 }
1892
1893 /* Reset the type of redrawing required, the window has been updated. */
1894 wp->w_redr_type = 0;
1895#ifdef FEAT_DIFF
1896 wp->w_old_topfill = wp->w_topfill;
1897 wp->w_old_botfill = wp->w_botfill;
1898#endif
1899
1900 if (dollar_vcol == 0)
1901 {
1902 /*
1903 * There is a trick with w_botline. If we invalidate it on each
1904 * change that might modify it, this will cause a lot of expensive
1905 * calls to plines() in update_topline() each time. Therefore the
1906 * value of w_botline is often approximated, and this value is used to
1907 * compute the value of w_topline. If the value of w_botline was
1908 * wrong, check that the value of w_topline is correct (cursor is on
1909 * the visible part of the text). If it's not, we need to redraw
1910 * again. Mostly this just means scrolling up a few lines, so it
1911 * doesn't look too bad. Only do this for the current window (where
1912 * changes are relevant).
1913 */
1914 wp->w_valid |= VALID_BOTLINE;
1915 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1916 {
1917 recursive = TRUE;
1918 curwin->w_valid &= ~VALID_TOPLINE;
1919 update_topline(); /* may invalidate w_botline again */
1920 if (must_redraw != 0)
1921 {
1922 /* Don't update for changes in buffer again. */
1923 i = curbuf->b_mod_set;
1924 curbuf->b_mod_set = FALSE;
1925 win_update(curwin);
1926 must_redraw = 0;
1927 curbuf->b_mod_set = i;
1928 }
1929 recursive = FALSE;
1930 }
1931 }
1932
1933#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1934 /* restore got_int, unless CTRL-C was hit while redrawing */
1935 if (!got_int)
1936 got_int = save_got_int;
1937#endif
1938}
1939
1940#ifdef FEAT_SIGNS
1941static int draw_signcolumn __ARGS((win_T *wp));
1942
1943/*
1944 * Return TRUE when window "wp" has a column to draw signs in.
1945 */
1946 static int
1947draw_signcolumn(wp)
1948 win_T *wp;
1949{
1950 return (wp->w_buffer->b_signlist != NULL
1951# ifdef FEAT_NETBEANS_INTG
1952 || usingNetbeans
1953# endif
1954 );
1955}
1956#endif
1957
1958/*
1959 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1960 * as the filler character.
1961 */
1962 static void
1963win_draw_end(wp, c1, c2, row, endrow, hl)
1964 win_T *wp;
1965 int c1;
1966 int c2;
1967 int row;
1968 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001969 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
1971#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1972 int n = 0;
1973# define FDC_OFF n
1974#else
1975# define FDC_OFF 0
1976#endif
1977
1978#ifdef FEAT_RIGHTLEFT
1979 if (wp->w_p_rl)
1980 {
1981 /* No check for cmdline window: should never be right-left. */
1982# ifdef FEAT_FOLDING
1983 n = wp->w_p_fdc;
1984
1985 if (n > 0)
1986 {
1987 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001988 if (n > W_WIDTH(wp))
1989 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1991 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
1992 ' ', ' ', hl_attr(HLF_FC));
1993 }
1994# endif
1995# ifdef FEAT_SIGNS
1996 if (draw_signcolumn(wp))
1997 {
1998 int nn = n + 2;
1999
2000 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002001 if (nn > W_WIDTH(wp))
2002 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2004 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2005 ' ', ' ', hl_attr(HLF_SC));
2006 n = nn;
2007 }
2008# endif
2009 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2010 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2011 c2, c2, hl_attr(hl));
2012 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2013 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2014 c1, c2, hl_attr(hl));
2015 }
2016 else
2017#endif
2018 {
2019#ifdef FEAT_CMDWIN
2020 if (cmdwin_type != 0 && wp == curwin)
2021 {
2022 /* draw the cmdline character in the leftmost column */
2023 n = 1;
2024 if (n > wp->w_width)
2025 n = wp->w_width;
2026 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2027 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2028 cmdwin_type, ' ', hl_attr(HLF_AT));
2029 }
2030#endif
2031#ifdef FEAT_FOLDING
2032 if (wp->w_p_fdc > 0)
2033 {
2034 int nn = n + wp->w_p_fdc;
2035
2036 /* draw the fold column at the left */
2037 if (nn > W_WIDTH(wp))
2038 nn = W_WIDTH(wp);
2039 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2040 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2041 ' ', ' ', hl_attr(HLF_FC));
2042 n = nn;
2043 }
2044#endif
2045#ifdef FEAT_SIGNS
2046 if (draw_signcolumn(wp))
2047 {
2048 int nn = n + 2;
2049
2050 /* draw the sign column after the fold column */
2051 if (nn > W_WIDTH(wp))
2052 nn = W_WIDTH(wp);
2053 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2054 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2055 ' ', ' ', hl_attr(HLF_SC));
2056 n = nn;
2057 }
2058#endif
2059 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2060 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2061 c1, c2, hl_attr(hl));
2062 }
2063 set_empty_rows(wp, row);
2064}
2065
2066#ifdef FEAT_FOLDING
2067/*
2068 * Display one folded line.
2069 */
2070 static void
2071fold_line(wp, fold_count, foldinfo, lnum, row)
2072 win_T *wp;
2073 long fold_count;
2074 foldinfo_T *foldinfo;
2075 linenr_T lnum;
2076 int row;
2077{
2078 char_u buf[51];
2079 pos_T *top, *bot;
2080 linenr_T lnume = lnum + fold_count - 1;
2081 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002082 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 int col;
2085 int txtcol;
2086 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002087 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 /* Build the fold line:
2090 * 1. Add the cmdwin_type for the command-line window
2091 * 2. Add the 'foldcolumn'
2092 * 3. Add the 'number' column
2093 * 4. Compose the text
2094 * 5. Add the text
2095 * 6. set highlighting for the Visual area an other text
2096 */
2097 col = 0;
2098
2099 /*
2100 * 1. Add the cmdwin_type for the command-line window
2101 * Ignores 'rightleft', this window is never right-left.
2102 */
2103#ifdef FEAT_CMDWIN
2104 if (cmdwin_type != 0 && wp == curwin)
2105 {
2106 ScreenLines[off] = cmdwin_type;
2107 ScreenAttrs[off] = hl_attr(HLF_AT);
2108#ifdef FEAT_MBYTE
2109 if (enc_utf8)
2110 ScreenLinesUC[off] = 0;
2111#endif
2112 ++col;
2113 }
2114#endif
2115
2116 /*
2117 * 2. Add the 'foldcolumn'
2118 */
2119 fdc = wp->w_p_fdc;
2120 if (fdc > W_WIDTH(wp) - col)
2121 fdc = W_WIDTH(wp) - col;
2122 if (fdc > 0)
2123 {
2124 fill_foldcolumn(buf, wp, TRUE, lnum);
2125#ifdef FEAT_RIGHTLEFT
2126 if (wp->w_p_rl)
2127 {
2128 int i;
2129
2130 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2131 hl_attr(HLF_FC));
2132 /* reverse the fold column */
2133 for (i = 0; i < fdc; ++i)
2134 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2135 }
2136 else
2137#endif
2138 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2139 col += fdc;
2140 }
2141
2142#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002143# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2144 for (ri = 0; ri < l; ++ri) \
2145 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2146 else \
2147 for (ri = 0; ri < l; ++ri) \
2148 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002150# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2151 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152#endif
2153
2154 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002155 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157#ifdef FEAT_SIGNS
2158 /* If signs are being displayed, add two spaces. */
2159 if (draw_signcolumn(wp))
2160 {
2161 len = W_WIDTH(wp) - col;
2162 if (len > 0)
2163 {
2164 if (len > 2)
2165 len = 2;
2166# ifdef FEAT_RIGHTLEFT
2167 if (wp->w_p_rl)
2168 /* the line number isn't reversed */
2169 copy_text_attr(off + W_WIDTH(wp) - len - col,
2170 (char_u *)" ", len, hl_attr(HLF_FL));
2171 else
2172# endif
2173 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2174 col += len;
2175 }
2176 }
2177#endif
2178
2179 /*
2180 * 3. Add the 'number' column
2181 */
2182 if (wp->w_p_nu)
2183 {
2184 len = W_WIDTH(wp) - col;
2185 if (len > 0)
2186 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002187 int w = number_width(wp);
2188
2189 if (len > w + 1)
2190 len = w + 1;
2191 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#ifdef FEAT_RIGHTLEFT
2193 if (wp->w_p_rl)
2194 /* the line number isn't reversed */
2195 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2196 hl_attr(HLF_FL));
2197 else
2198#endif
2199 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2200 col += len;
2201 }
2202 }
2203
2204 /*
2205 * 4. Compose the folded-line string with 'foldtext', if set.
2206 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002207 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
2209 txtcol = col; /* remember where text starts */
2210
2211 /*
2212 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2213 * Right-left text is put in columns 0 - number-col, normal text is put
2214 * in columns number-col - window-width.
2215 */
2216#ifdef FEAT_MBYTE
2217 if (has_mbyte)
2218 {
2219 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002220 int u8c, u8cc[MAX_MCO];
2221 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 int idx;
2223 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002224 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225# ifdef FEAT_ARABIC
2226 int prev_c = 0; /* previous Arabic character */
2227 int prev_c1 = 0; /* first composing char for prev_c */
2228# endif
2229
2230# ifdef FEAT_RIGHTLEFT
2231 if (wp->w_p_rl)
2232 idx = off;
2233 else
2234# endif
2235 idx = off + col;
2236
2237 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2238 for (p = text; *p != NUL; )
2239 {
2240 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002241 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (col + cells > W_WIDTH(wp)
2243# ifdef FEAT_RIGHTLEFT
2244 - (wp->w_p_rl ? col : 0)
2245# endif
2246 )
2247 break;
2248 ScreenLines[idx] = *p;
2249 if (enc_utf8)
2250 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002251 u8c = utfc_ptr2char(p, u8cc);
2252 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 ScreenLinesUC[idx] = 0;
2255#ifdef FEAT_ARABIC
2256 prev_c = u8c;
2257#endif
2258 }
2259 else
2260 {
2261#ifdef FEAT_ARABIC
2262 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2263 {
2264 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002265 int pc, pc1, nc;
2266 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 int firstbyte = *p;
2268
2269 /* The idea of what is the previous and next
2270 * character depends on 'rightleft'. */
2271 if (wp->w_p_rl)
2272 {
2273 pc = prev_c;
2274 pc1 = prev_c1;
2275 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002276 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
2278 else
2279 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002280 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002282 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284 prev_c = u8c;
2285
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002286 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 pc, pc1, nc);
2288 ScreenLines[idx] = firstbyte;
2289 }
2290 else
2291 prev_c = u8c;
2292#endif
2293 /* Non-BMP character: display as ? or fullwidth ?. */
2294 if (u8c >= 0x10000)
2295 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2296 else
2297 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002298 for (i = 0; i < Screen_mco; ++i)
2299 {
2300 ScreenLinesC[i][idx] = u8cc[i];
2301 if (u8cc[i] == 0)
2302 break;
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 if (cells > 1)
2306 ScreenLines[idx + 1] = 0;
2307 }
2308 else if (cells > 1) /* double-byte character */
2309 {
2310 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2311 ScreenLines2[idx] = p[1];
2312 else
2313 ScreenLines[idx + 1] = p[1];
2314 }
2315 col += cells;
2316 idx += cells;
2317 p += c_len;
2318 }
2319 }
2320 else
2321#endif
2322 {
2323 len = (int)STRLEN(text);
2324 if (len > W_WIDTH(wp) - col)
2325 len = W_WIDTH(wp) - col;
2326 if (len > 0)
2327 {
2328#ifdef FEAT_RIGHTLEFT
2329 if (wp->w_p_rl)
2330 STRNCPY(current_ScreenLine, text, len);
2331 else
2332#endif
2333 STRNCPY(current_ScreenLine + col, text, len);
2334 col += len;
2335 }
2336 }
2337
2338 /* Fill the rest of the line with the fold filler */
2339#ifdef FEAT_RIGHTLEFT
2340 if (wp->w_p_rl)
2341 col -= txtcol;
2342#endif
2343 while (col < W_WIDTH(wp)
2344#ifdef FEAT_RIGHTLEFT
2345 - (wp->w_p_rl ? txtcol : 0)
2346#endif
2347 )
2348 {
2349#ifdef FEAT_MBYTE
2350 if (enc_utf8)
2351 {
2352 if (fill_fold >= 0x80)
2353 {
2354 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002355 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357 else
2358 ScreenLinesUC[off + col] = 0;
2359 }
2360#endif
2361 ScreenLines[off + col++] = fill_fold;
2362 }
2363
2364 if (text != buf)
2365 vim_free(text);
2366
2367 /*
2368 * 6. set highlighting for the Visual area an other text.
2369 * If all folded lines are in the Visual area, highlight the line.
2370 */
2371#ifdef FEAT_VISUAL
2372 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2373 {
2374 if (ltoreq(curwin->w_cursor, VIsual))
2375 {
2376 /* Visual is after curwin->w_cursor */
2377 top = &curwin->w_cursor;
2378 bot = &VIsual;
2379 }
2380 else
2381 {
2382 /* Visual is before curwin->w_cursor */
2383 top = &VIsual;
2384 bot = &curwin->w_cursor;
2385 }
2386 if (lnum >= top->lnum
2387 && lnume <= bot->lnum
2388 && (VIsual_mode != 'v'
2389 || ((lnum > top->lnum
2390 || (lnum == top->lnum
2391 && top->col == 0))
2392 && (lnume < bot->lnum
2393 || (lnume == bot->lnum
2394 && (bot->col - (*p_sel == 'e'))
2395 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2396 {
2397 if (VIsual_mode == Ctrl_V)
2398 {
2399 /* Visual block mode: highlight the chars part of the block */
2400 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2401 {
2402 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2403 len = wp->w_old_cursor_lcol;
2404 else
2405 len = W_WIDTH(wp) - txtcol;
2406 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002407 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 }
2410 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002413 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 }
2417#endif
2418
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002419#ifdef FEAT_SYN_HL
2420 /* Show 'cursorcolumn' in the fold line. */
2421 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2422 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2423 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2427 (int)W_WIDTH(wp), FALSE);
2428
2429 /*
2430 * Update w_cline_height and w_cline_folded if the cursor line was
2431 * updated (saves a call to plines() later).
2432 */
2433 if (wp == curwin
2434 && lnum <= curwin->w_cursor.lnum
2435 && lnume >= curwin->w_cursor.lnum)
2436 {
2437 curwin->w_cline_row = row;
2438 curwin->w_cline_height = 1;
2439 curwin->w_cline_folded = TRUE;
2440 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2441 }
2442}
2443
2444/*
2445 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2446 */
2447 static void
2448copy_text_attr(off, buf, len, attr)
2449 int off;
2450 char_u *buf;
2451 int len;
2452 int attr;
2453{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002454 int i;
2455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 mch_memmove(ScreenLines + off, buf, (size_t)len);
2457# ifdef FEAT_MBYTE
2458 if (enc_utf8)
2459 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2460# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002461 for (i = 0; i < len; ++i)
2462 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463}
2464
2465/*
2466 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002467 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
2469 static void
2470fill_foldcolumn(p, wp, closed, lnum)
2471 char_u *p;
2472 win_T *wp;
2473 int closed; /* TRUE of FALSE */
2474 linenr_T lnum; /* current line number */
2475{
2476 int i = 0;
2477 int level;
2478 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002479 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /* Init to all spaces. */
2482 copy_spaces(p, (size_t)wp->w_p_fdc);
2483
2484 level = win_foldinfo.fi_level;
2485 if (level > 0)
2486 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002487 /* If there is only one column put more info in it. */
2488 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 /* If the column is too narrow, we start at the lowest level that
2491 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002492 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 if (first_level < 1)
2494 first_level = 1;
2495
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002496 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
2498 if (win_foldinfo.fi_lnum == lnum
2499 && first_level + i >= win_foldinfo.fi_low_level)
2500 p[i] = '-';
2501 else if (first_level == 1)
2502 p[i] = '|';
2503 else if (first_level + i <= 9)
2504 p[i] = '0' + first_level + i;
2505 else
2506 p[i] = '>';
2507 if (first_level + i == level)
2508 break;
2509 }
2510 }
2511 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002512 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513}
2514#endif /* FEAT_FOLDING */
2515
2516/*
2517 * Display line "lnum" of window 'wp' on the screen.
2518 * Start at row "startrow", stop when "endrow" is reached.
2519 * wp->w_virtcol needs to be valid.
2520 *
2521 * Return the number of last row the line occupies.
2522 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002523/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002525win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 win_T *wp;
2527 linenr_T lnum;
2528 int startrow;
2529 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002530 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 int col; /* visual column on screen */
2533 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2534 int c = 0; /* init for GCC */
2535 long vcol = 0; /* virtual column (for tabs) */
2536 long vcol_prev = -1; /* "vcol" of previous character */
2537 char_u *line; /* current line */
2538 char_u *ptr; /* current position in "line" */
2539 int row; /* row in the window, excl w_winrow */
2540 int screen_row; /* row on the screen, incl w_winrow */
2541
2542 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2543 int n_extra = 0; /* number of extra chars */
2544 char_u *p_extra = NULL; /* string of extra chars */
2545 int c_extra = NUL; /* extra chars, all the same */
2546 int extra_attr = 0; /* attributes when n_extra != 0 */
2547 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2548 displaying lcs_eol at end-of-line */
2549 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2550 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2551
2552 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2553 int saved_n_extra = 0;
2554 char_u *saved_p_extra = NULL;
2555 int saved_c_extra = 0;
2556 int saved_char_attr = 0;
2557
2558 int n_attr = 0; /* chars with special attr */
2559 int saved_attr2 = 0; /* char_attr saved for n_attr */
2560 int n_attr3 = 0; /* chars with overruling special attr */
2561 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2562
2563 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2564
2565 int fromcol, tocol; /* start/end of inverting */
2566 int fromcol_prev = -2; /* start of inverting after cursor */
2567 int noinvcur = FALSE; /* don't invert the cursor */
2568#ifdef FEAT_VISUAL
2569 pos_T *top, *bot;
2570#endif
2571 pos_T pos;
2572 long v;
2573
2574 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002575 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2577 in this line */
2578 int attr = 0; /* attributes for area highlighting */
2579 int area_attr = 0; /* attributes desired by highlighting */
2580 int search_attr = 0; /* attributes desired by 'hlsearch' */
2581#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002582 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 int syntax_attr = 0; /* attributes desired by syntax */
2584 int has_syntax = FALSE; /* this buffer has syntax highl. */
2585 int save_did_emsg;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002586#endif
2587#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002588 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002589# define SPWORDLEN 150
2590 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002591 int nextlinecol = 0; /* column where nextline[] starts */
2592 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002593 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002594 int spell_attr = 0; /* attributes desired by spelling */
2595 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002596 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2597 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002598 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002599 static int cap_col = -1; /* column to check for Cap word */
2600 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002601 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602#endif
2603 int extra_check; /* has syntax or linebreak */
2604#ifdef FEAT_MBYTE
2605 int multi_attr = 0; /* attributes desired by multibyte */
2606 int mb_l = 1; /* multi-byte byte length */
2607 int mb_c = 0; /* decoded multi-byte character */
2608 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002609 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610#endif
2611#ifdef FEAT_DIFF
2612 int filler_lines; /* nr of filler lines to be drawn */
2613 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002614 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 int change_start = MAXCOL; /* first col of changed area */
2616 int change_end = -1; /* last col of changed area */
2617#endif
2618 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2619#ifdef FEAT_LINEBREAK
2620 int need_showbreak = FALSE;
2621#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002622#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2623 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624# define LINE_ATTR
2625 int line_attr = 0; /* atrribute for the whole line */
2626#endif
2627#ifdef FEAT_SEARCH_EXTRA
2628 match_T *shl; /* points to search_hl or match_hl */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002629#endif
2630#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_MBYTE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002631 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632#endif
2633#ifdef FEAT_ARABIC
2634 int prev_c = 0; /* previous Arabic character */
2635 int prev_c1 = 0; /* first composing char for prev_c */
2636#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002637#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002638 int did_line_attr = 0;
2639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 /* draw_state: items that are drawn in sequence: */
2642#define WL_START 0 /* nothing done yet */
2643#ifdef FEAT_CMDWIN
2644# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2645#else
2646# define WL_CMDLINE WL_START
2647#endif
2648#ifdef FEAT_FOLDING
2649# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2650#else
2651# define WL_FOLD WL_CMDLINE
2652#endif
2653#ifdef FEAT_SIGNS
2654# define WL_SIGN WL_FOLD + 1 /* column for signs */
2655#else
2656# define WL_SIGN WL_FOLD /* column for signs */
2657#endif
2658#define WL_NR WL_SIGN + 1 /* line number */
2659#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2660# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2661#else
2662# define WL_SBR WL_NR
2663#endif
2664#define WL_LINE WL_SBR + 1 /* text in the line */
2665 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002666#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 int feedback_col = 0;
2668 int feedback_old_attr = -1;
2669#endif
2670
2671
2672 if (startrow > endrow) /* past the end already! */
2673 return startrow;
2674
2675 row = startrow;
2676 screen_row = row + W_WINROW(wp);
2677
2678 /*
2679 * To speed up the loop below, set extra_check when there is linebreak,
2680 * trailing white space and/or syntax processing to be done.
2681 */
2682#ifdef FEAT_LINEBREAK
2683 extra_check = wp->w_p_lbr;
2684#else
2685 extra_check = 0;
2686#endif
2687#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002688 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
2690 /* Prepare for syntax highlighting in this line. When there is an
2691 * error, stop syntax highlighting. */
2692 save_did_emsg = did_emsg;
2693 did_emsg = FALSE;
2694 syntax_start(wp, lnum);
2695 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002696 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 else
2698 {
2699 did_emsg = save_did_emsg;
2700 has_syntax = TRUE;
2701 extra_check = TRUE;
2702 }
2703 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002704#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002705
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002706#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002707 if (wp->w_p_spell
2708 && *wp->w_buffer->b_p_spl != NUL
2709 && wp->w_buffer->b_langp.ga_len > 0
2710 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002711 {
2712 /* Prepare for spell checking. */
2713 has_spell = TRUE;
2714 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002715
2716 /* Get the start of the next line, so that words that wrap to the next
2717 * line are found too: "et<line-break>al.".
2718 * Trick: skip a few chars for C/shell/Vim comments */
2719 nextline[SPWORDLEN] = NUL;
2720 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2721 {
2722 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2723 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2724 }
2725
2726 /* When a word wrapped from the previous line the start of the current
2727 * line is valid. */
2728 if (lnum == checked_lnum)
2729 cur_checked_col = checked_col;
2730 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002731
2732 /* When there was a sentence end in the previous line may require a
2733 * word starting with capital in this line. In line 1 always check
2734 * the first word. */
2735 if (lnum != capcol_lnum)
2736 cap_col = -1;
2737 if (lnum == 1)
2738 cap_col = 0;
2739 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741#endif
2742
2743 /*
2744 * handle visual active in this window
2745 */
2746 fromcol = -10;
2747 tocol = MAXCOL;
2748#ifdef FEAT_VISUAL
2749 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2750 {
2751 /* Visual is after curwin->w_cursor */
2752 if (ltoreq(curwin->w_cursor, VIsual))
2753 {
2754 top = &curwin->w_cursor;
2755 bot = &VIsual;
2756 }
2757 else /* Visual is before curwin->w_cursor */
2758 {
2759 top = &VIsual;
2760 bot = &curwin->w_cursor;
2761 }
2762 if (VIsual_mode == Ctrl_V) /* block mode */
2763 {
2764 if (lnum >= top->lnum && lnum <= bot->lnum)
2765 {
2766 fromcol = wp->w_old_cursor_fcol;
2767 tocol = wp->w_old_cursor_lcol;
2768 }
2769 }
2770 else /* non-block mode */
2771 {
2772 if (lnum > top->lnum && lnum <= bot->lnum)
2773 fromcol = 0;
2774 else if (lnum == top->lnum)
2775 {
2776 if (VIsual_mode == 'V') /* linewise */
2777 fromcol = 0;
2778 else
2779 {
2780 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2781 if (gchar_pos(top) == NUL)
2782 tocol = fromcol + 1;
2783 }
2784 }
2785 if (VIsual_mode != 'V' && lnum == bot->lnum)
2786 {
2787 if (*p_sel == 'e' && bot->col == 0
2788#ifdef FEAT_VIRTUALEDIT
2789 && bot->coladd == 0
2790#endif
2791 )
2792 {
2793 fromcol = -10;
2794 tocol = MAXCOL;
2795 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002796 else if (bot->col == MAXCOL)
2797 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else
2799 {
2800 pos = *bot;
2801 if (*p_sel == 'e')
2802 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2803 else
2804 {
2805 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2806 ++tocol;
2807 }
2808 }
2809 }
2810 }
2811
2812#ifndef MSDOS
2813 /* Check if the character under the cursor should not be inverted */
2814 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2815# ifdef FEAT_GUI
2816 && !gui.in_use
2817# endif
2818 )
2819 noinvcur = TRUE;
2820#endif
2821
2822 /* if inverting in this line set area_highlighting */
2823 if (fromcol >= 0)
2824 {
2825 area_highlighting = TRUE;
2826 attr = hl_attr(HLF_V);
2827#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2828 if (clip_star.available && !clip_star.owned && clip_isautosel())
2829 attr = hl_attr(HLF_VNC);
2830#endif
2831 }
2832 }
2833
2834 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002835 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 */
2837 else
2838#endif /* FEAT_VISUAL */
2839 if (highlight_match
2840 && wp == curwin
2841 && lnum >= curwin->w_cursor.lnum
2842 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2843 {
2844 if (lnum == curwin->w_cursor.lnum)
2845 getvcol(curwin, &(curwin->w_cursor),
2846 (colnr_T *)&fromcol, NULL, NULL);
2847 else
2848 fromcol = 0;
2849 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2850 {
2851 pos.lnum = lnum;
2852 pos.col = search_match_endcol;
2853 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2854 }
2855 else
2856 tocol = MAXCOL;
2857 if (fromcol == tocol) /* do at least one character */
2858 tocol = fromcol + 1; /* happens when past end of line */
2859 area_highlighting = TRUE;
2860 attr = hl_attr(HLF_I);
2861 }
2862
2863#ifdef FEAT_DIFF
2864 filler_lines = diff_check(wp, lnum);
2865 if (filler_lines < 0)
2866 {
2867 if (filler_lines == -1)
2868 {
2869 if (diff_find_change(wp, lnum, &change_start, &change_end))
2870 diff_hlf = HLF_ADD; /* added line */
2871 else if (change_start == 0)
2872 diff_hlf = HLF_TXD; /* changed text */
2873 else
2874 diff_hlf = HLF_CHD; /* changed line */
2875 }
2876 else
2877 diff_hlf = HLF_ADD; /* added line */
2878 filler_lines = 0;
2879 area_highlighting = TRUE;
2880 }
2881 if (lnum == wp->w_topline)
2882 filler_lines = wp->w_topfill;
2883 filler_todo = filler_lines;
2884#endif
2885
2886#ifdef LINE_ATTR
2887# ifdef FEAT_SIGNS
2888 /* If this line has a sign with line highlighting set line_attr. */
2889 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2890 if (v != 0)
2891 line_attr = sign_get_attr((int)v, TRUE);
2892# endif
2893# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2894 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002895 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 line_attr = hl_attr(HLF_L);
2897# endif
2898 if (line_attr != 0)
2899 area_highlighting = TRUE;
2900#endif
2901
2902 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2903 ptr = line;
2904
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002905#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002906 if (has_spell)
2907 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002908 /* For checking first word with a capital skip white space. */
2909 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002910 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002911
Bram Moolenaar30abd282005-06-22 22:35:10 +00002912 /* To be able to spell-check over line boundaries copy the end of the
2913 * current line into nextline[]. Above the start of the next line was
2914 * copied to nextline[SPWORDLEN]. */
2915 if (nextline[SPWORDLEN] == NUL)
2916 {
2917 /* No next line or it is empty. */
2918 nextlinecol = MAXCOL;
2919 nextline_idx = 0;
2920 }
2921 else
2922 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002923 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00002924 if (v < SPWORDLEN)
2925 {
2926 /* Short line, use it completely and append the start of the
2927 * next line. */
2928 nextlinecol = 0;
2929 mch_memmove(nextline, line, (size_t)v);
2930 mch_memmove(nextline + v, nextline + SPWORDLEN,
2931 STRLEN(nextline + SPWORDLEN) + 1);
2932 nextline_idx = v + 1;
2933 }
2934 else
2935 {
2936 /* Long line, use only the last SPWORDLEN bytes. */
2937 nextlinecol = v - SPWORDLEN;
2938 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2939 nextline_idx = SPWORDLEN + 1;
2940 }
2941 }
2942 }
2943#endif
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 /* find start of trailing whitespace */
2946 if (wp->w_p_list && lcs_trail)
2947 {
2948 trailcol = (colnr_T)STRLEN(ptr);
2949 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2950 --trailcol;
2951 trailcol += (colnr_T) (ptr - line);
2952 extra_check = TRUE;
2953 }
2954
2955 /*
2956 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2957 * first character to be displayed.
2958 */
2959 if (wp->w_p_wrap)
2960 v = wp->w_skipcol;
2961 else
2962 v = wp->w_leftcol;
2963 if (v > 0)
2964 {
2965#ifdef FEAT_MBYTE
2966 char_u *prev_ptr = ptr;
2967#endif
2968 while (vcol < v && *ptr != NUL)
2969 {
2970 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2971 vcol += c;
2972#ifdef FEAT_MBYTE
2973 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002975 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
2977
2978#ifdef FEAT_VIRTUALEDIT
2979 /* When 'virtualedit' is set the end of the line may be before the
2980 * start of the displayed part. */
2981 if (vcol < v && *ptr == NUL && virtual_active())
2982 vcol = v;
2983#endif
2984
2985 /* Handle a character that's not completely on the screen: Put ptr at
2986 * that character but skip the first few screen characters. */
2987 if (vcol > v)
2988 {
2989 vcol -= c;
2990#ifdef FEAT_MBYTE
2991 ptr = prev_ptr;
2992#else
2993 --ptr;
2994#endif
2995 n_skip = v - vcol;
2996 }
2997
2998 /*
2999 * Adjust for when the inverted text is before the screen,
3000 * and when the start of the inverted text is before the screen.
3001 */
3002 if (tocol <= vcol)
3003 fromcol = 0;
3004 else if (fromcol >= 0 && fromcol < vcol)
3005 fromcol = vcol;
3006
3007#ifdef FEAT_LINEBREAK
3008 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3009 if (wp->w_p_wrap)
3010 need_showbreak = TRUE;
3011#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003012#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003013 /* When spell checking a word we need to figure out the start of the
3014 * word and if it's badly spelled or not. */
3015 if (has_spell)
3016 {
3017 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003018 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003019
3020 pos = wp->w_cursor;
3021 wp->w_cursor.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003022 wp->w_cursor.col = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003023 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003024 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003025 {
3026 /* no bad word found at line start, don't check until end of a
3027 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003028 spell_hlf = HLF_COUNT;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003029 word_end = (int)(spell_to_word_end(ptr, wp->w_buffer) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003030 }
3031 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003032 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003033 /* bad word found, use attributes until end of word */
3034 word_end = wp->w_cursor.col + len + 1;
3035
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003036 /* Turn index into actual attributes. */
3037 if (spell_hlf != HLF_COUNT)
3038 spell_attr = highlight_attr[spell_hlf];
3039 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003040 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003041
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003042# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003043 /* Need to restart syntax highlighting for this line. */
3044 if (has_syntax)
3045 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003046# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003047 }
3048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050
3051 /*
3052 * Correct highlighting for cursor that can't be disabled.
3053 * Avoids having to check this for each character.
3054 */
3055 if (fromcol >= 0)
3056 {
3057 if (noinvcur)
3058 {
3059 if ((colnr_T)fromcol == wp->w_virtcol)
3060 {
3061 /* highlighting starts at cursor, let it start just after the
3062 * cursor */
3063 fromcol_prev = fromcol;
3064 fromcol = -1;
3065 }
3066 else if ((colnr_T)fromcol < wp->w_virtcol)
3067 /* restart highlighting after the cursor */
3068 fromcol_prev = wp->w_virtcol;
3069 }
3070 if (fromcol >= tocol)
3071 fromcol = -1;
3072 }
3073
3074#ifdef FEAT_SEARCH_EXTRA
3075 /*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003076 * Handle highlighting the last used search pattern and ":match".
3077 * Do this for both search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003079 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003081 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003082 shl->startcol = MAXCOL;
3083 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 shl->attr_cur = 0;
3085 if (shl->rm.regprog != NULL)
3086 {
3087 v = (long)(ptr - line);
3088 next_search_hl(wp, shl, lnum, (colnr_T)v);
3089
3090 /* Need to get the line again, a multi-line regexp may have made it
3091 * invalid. */
3092 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3093 ptr = line + v;
3094
3095 if (shl->lnum != 0 && shl->lnum <= lnum)
3096 {
3097 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003098 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003100 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3102 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003103 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003105 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003107 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
3109#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003110 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003111 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 else
3113#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003114 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003116 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 {
3118 shl->attr_cur = shl->attr;
3119 search_attr = shl->attr;
3120 }
3121 area_highlighting = TRUE;
3122 }
3123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 }
3125#endif
3126
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003127#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003128 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3129 * active, because it's not clear what is selected then. */
3130 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003131 {
3132 line_attr = hl_attr(HLF_CUL);
3133 area_highlighting = TRUE;
3134 }
3135#endif
3136
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003137 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 col = 0;
3139#ifdef FEAT_RIGHTLEFT
3140 if (wp->w_p_rl)
3141 {
3142 /* Rightleft window: process the text in the normal direction, but put
3143 * it in current_ScreenLine[] from right to left. Start at the
3144 * rightmost column of the window. */
3145 col = W_WIDTH(wp) - 1;
3146 off += col;
3147 }
3148#endif
3149
3150 /*
3151 * Repeat for the whole displayed line.
3152 */
3153 for (;;)
3154 {
3155 /* Skip this quickly when working on the text. */
3156 if (draw_state != WL_LINE)
3157 {
3158#ifdef FEAT_CMDWIN
3159 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3160 {
3161 draw_state = WL_CMDLINE;
3162 if (cmdwin_type != 0 && wp == curwin)
3163 {
3164 /* Draw the cmdline character. */
3165 *extra = cmdwin_type;
3166 n_extra = 1;
3167 p_extra = extra;
3168 c_extra = NUL;
3169 char_attr = hl_attr(HLF_AT);
3170 }
3171 }
3172#endif
3173
3174#ifdef FEAT_FOLDING
3175 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3176 {
3177 draw_state = WL_FOLD;
3178 if (wp->w_p_fdc > 0)
3179 {
3180 /* Draw the 'foldcolumn'. */
3181 fill_foldcolumn(extra, wp, FALSE, lnum);
3182 n_extra = wp->w_p_fdc;
3183 p_extra = extra;
3184 c_extra = NUL;
3185 char_attr = hl_attr(HLF_FC);
3186 }
3187 }
3188#endif
3189
3190#ifdef FEAT_SIGNS
3191 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3192 {
3193 draw_state = WL_SIGN;
3194 /* Show the sign column when there are any signs in this
3195 * buffer or when using Netbeans. */
3196 if (draw_signcolumn(wp)
3197# ifdef FEAT_DIFF
3198 && filler_todo <= 0
3199# endif
3200 )
3201 {
3202 int_u text_sign;
3203# ifdef FEAT_SIGN_ICONS
3204 int_u icon_sign;
3205# endif
3206
3207 /* Draw two cells with the sign value or blank. */
3208 c_extra = ' ';
3209 char_attr = hl_attr(HLF_SC);
3210 n_extra = 2;
3211
3212 if (row == startrow)
3213 {
3214 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3215 SIGN_TEXT);
3216# ifdef FEAT_SIGN_ICONS
3217 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3218 SIGN_ICON);
3219 if (gui.in_use && icon_sign != 0)
3220 {
3221 /* Use the image in this position. */
3222 c_extra = SIGN_BYTE;
3223# ifdef FEAT_NETBEANS_INTG
3224 if (buf_signcount(wp->w_buffer, lnum) > 1)
3225 c_extra = MULTISIGN_BYTE;
3226# endif
3227 char_attr = icon_sign;
3228 }
3229 else
3230# endif
3231 if (text_sign != 0)
3232 {
3233 p_extra = sign_get_text(text_sign);
3234 if (p_extra != NULL)
3235 {
3236 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003237 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 }
3239 char_attr = sign_get_attr(text_sign, FALSE);
3240 }
3241 }
3242 }
3243 }
3244#endif
3245
3246 if (draw_state == WL_NR - 1 && n_extra == 0)
3247 {
3248 draw_state = WL_NR;
3249 /* Display the line number. After the first fill with blanks
3250 * when the 'n' flag isn't in 'cpo' */
3251 if (wp->w_p_nu
3252 && (row == startrow
3253#ifdef FEAT_DIFF
3254 + filler_lines
3255#endif
3256 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3257 {
3258 /* Draw the line number (empty space after wrapping). */
3259 if (row == startrow
3260#ifdef FEAT_DIFF
3261 + filler_lines
3262#endif
3263 )
3264 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003265 sprintf((char *)extra, "%*ld ",
3266 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 if (wp->w_skipcol > 0)
3268 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3269 *p_extra = '-';
3270#ifdef FEAT_RIGHTLEFT
3271 if (wp->w_p_rl) /* reverse line numbers */
3272 rl_mirror(extra);
3273#endif
3274 p_extra = extra;
3275 c_extra = NUL;
3276 }
3277 else
3278 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003279 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003281#ifdef FEAT_SYN_HL
3282 /* When 'cursorline' is set highlight the line number of
3283 * the current line differently. */
3284 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3285 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288 }
3289
3290#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3291 if (draw_state == WL_SBR - 1 && n_extra == 0)
3292 {
3293 draw_state = WL_SBR;
3294# ifdef FEAT_DIFF
3295 if (filler_todo > 0)
3296 {
3297 /* Draw "deleted" diff line(s). */
3298 if (char2cells(fill_diff) > 1)
3299 c_extra = '-';
3300 else
3301 c_extra = fill_diff;
3302# ifdef FEAT_RIGHTLEFT
3303 if (wp->w_p_rl)
3304 n_extra = col + 1;
3305 else
3306# endif
3307 n_extra = W_WIDTH(wp) - col;
3308 char_attr = hl_attr(HLF_DED);
3309 }
3310# endif
3311# ifdef FEAT_LINEBREAK
3312 if (*p_sbr != NUL && need_showbreak)
3313 {
3314 /* Draw 'showbreak' at the start of each broken line. */
3315 p_extra = p_sbr;
3316 c_extra = NUL;
3317 n_extra = (int)STRLEN(p_sbr);
3318 char_attr = hl_attr(HLF_AT);
3319 need_showbreak = FALSE;
3320 /* Correct end of highlighted area for 'showbreak',
3321 * required when 'linebreak' is also set. */
3322 if (tocol == vcol)
3323 tocol += n_extra;
3324 }
3325# endif
3326 }
3327#endif
3328
3329 if (draw_state == WL_LINE - 1 && n_extra == 0)
3330 {
3331 draw_state = WL_LINE;
3332 if (saved_n_extra)
3333 {
3334 /* Continue item from end of wrapped line. */
3335 n_extra = saved_n_extra;
3336 c_extra = saved_c_extra;
3337 p_extra = saved_p_extra;
3338 char_attr = saved_char_attr;
3339 }
3340 else
3341 char_attr = 0;
3342 }
3343 }
3344
3345 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003346 if (dollar_vcol != 0 && wp == curwin
3347 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#ifdef FEAT_DIFF
3349 && filler_todo <= 0
3350#endif
3351 )
3352 {
3353 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3354 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003355 /* Pretend we have finished updating the window. Except when
3356 * 'cursorcolumn' is set. */
3357#ifdef FEAT_SYN_HL
3358 if (wp->w_p_cuc)
3359 row = wp->w_cline_row + wp->w_cline_height;
3360 else
3361#endif
3362 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 break;
3364 }
3365
3366 if (draw_state == WL_LINE && area_highlighting)
3367 {
3368 /* handle Visual or match highlighting in this line */
3369 if (vcol == fromcol
3370#ifdef FEAT_MBYTE
3371 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3372 && (*mb_ptr2cells)(ptr) > 1)
3373#endif
3374 || ((int)vcol_prev == fromcol_prev
3375 && vcol < tocol))
3376 area_attr = attr; /* start highlighting */
3377 else if (area_attr != 0
3378 && (vcol == tocol
3379 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382#ifdef FEAT_SEARCH_EXTRA
3383 if (!n_extra)
3384 {
3385 /*
3386 * Check for start/end of search pattern match.
3387 * After end, check for start/end of next match.
3388 * When another match, have to check for start again.
3389 * Watch out for matching an empty string!
3390 * Do this first for search_hl, then for match_hl, so that
3391 * ":match" overrules 'hlsearch'.
3392 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003393 v = (long)(ptr - line);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003394 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003396 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 while (shl->rm.regprog != NULL)
3398 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003399 if (shl->startcol != MAXCOL
3400 && v >= (long)shl->startcol
3401 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
3403 shl->attr_cur = shl->attr;
3404 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003405 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
3407 shl->attr_cur = 0;
3408
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 next_search_hl(wp, shl, lnum, (colnr_T)v);
3410
3411 /* Need to get the line again, a multi-line regexp
3412 * may have made it invalid. */
3413 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3414 ptr = line + v;
3415
3416 if (shl->lnum == lnum)
3417 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003418 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003420 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003422 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003424 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
3426 /* highlight empty match, try again after
3427 * it */
3428#ifdef FEAT_MBYTE
3429 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003430 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003431 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 else
3433#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003434 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436
3437 /* Loop to check if the match starts at the
3438 * current position */
3439 continue;
3440 }
3441 }
3442 break;
3443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 /* ":match" highlighting overrules 'hlsearch' */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003447 for (i = 0; i <= 3; ++i)
3448 if (i == 3)
3449 search_attr = search_hl.attr_cur;
3450 else if (match_hl[i].attr_cur != 0)
3451 {
3452 search_attr = match_hl[i].attr_cur;
3453 break;
3454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456#endif
3457
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003459 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 if (diff_hlf == HLF_CHD && ptr - line >= change_start)
3462 diff_hlf = HLF_TXD; /* changed text */
3463 if (diff_hlf == HLF_TXD && ptr - line > change_end)
3464 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003465 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
3467#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003468
3469 /* Decide which of the highlight attributes to use. */
3470 attr_pri = TRUE;
3471 if (area_attr != 0)
3472 char_attr = area_attr;
3473 else if (search_attr != 0)
3474 char_attr = search_attr;
3475#ifdef LINE_ATTR
3476 /* Use line_attr when not in the Visual or 'incsearch' area
3477 * (area_attr may be 0 when "noinvcur" is set). */
3478 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3479 || (vcol < fromcol || vcol >= tocol)))
3480 char_attr = line_attr;
3481#endif
3482 else
3483 {
3484 attr_pri = FALSE;
3485#ifdef FEAT_SYN_HL
3486 if (has_syntax)
3487 char_attr = syntax_attr;
3488 else
3489#endif
3490 char_attr = 0;
3491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493
3494 /*
3495 * Get the next character to put on the screen.
3496 */
3497 /*
3498 * The 'extra' array contains the extra stuff that is inserted to
3499 * represent special characters (non-printable stuff). When all
3500 * characters are the same, c_extra is used.
3501 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3502 */
3503 if (n_extra > 0)
3504 {
3505 if (c_extra != NUL)
3506 {
3507 c = c_extra;
3508#ifdef FEAT_MBYTE
3509 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3510 if (enc_utf8 && (*mb_char2len)(c) > 1)
3511 {
3512 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003513 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003514 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 }
3516 else
3517 mb_utf8 = FALSE;
3518#endif
3519 }
3520 else
3521 {
3522 c = *p_extra;
3523#ifdef FEAT_MBYTE
3524 if (has_mbyte)
3525 {
3526 mb_c = c;
3527 if (enc_utf8)
3528 {
3529 /* If the UTF-8 character is more than one byte:
3530 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003531 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 mb_utf8 = FALSE;
3533 if (mb_l > n_extra)
3534 mb_l = 1;
3535 else if (mb_l > 1)
3536 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003537 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003539 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541 }
3542 else
3543 {
3544 /* if this is a DBCS character, put it in "mb_c" */
3545 mb_l = MB_BYTE2LEN(c);
3546 if (mb_l >= n_extra)
3547 mb_l = 1;
3548 else if (mb_l > 1)
3549 mb_c = (c << 8) + p_extra[1];
3550 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003551 if (mb_l == 0) /* at the NUL at end-of-line */
3552 mb_l = 1;
3553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 /* If a double-width char doesn't fit display a '>' in the
3555 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003556 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557# ifdef FEAT_RIGHTLEFT
3558 wp->w_p_rl ? (col <= 0) :
3559# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003560 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 && (*mb_char2cells)(mb_c) == 2)
3562 {
3563 c = '>';
3564 mb_c = c;
3565 mb_l = 1;
3566 mb_utf8 = FALSE;
3567 multi_attr = hl_attr(HLF_AT);
3568 /* put the pointer back to output the double-width
3569 * character at the start of the next line. */
3570 ++n_extra;
3571 --p_extra;
3572 }
3573 else
3574 {
3575 n_extra -= mb_l - 1;
3576 p_extra += mb_l - 1;
3577 }
3578 }
3579#endif
3580 ++p_extra;
3581 }
3582 --n_extra;
3583 }
3584 else
3585 {
3586 /*
3587 * Get a character from the line itself.
3588 */
3589 c = *ptr;
3590#ifdef FEAT_MBYTE
3591 if (has_mbyte)
3592 {
3593 mb_c = c;
3594 if (enc_utf8)
3595 {
3596 /* If the UTF-8 character is more than one byte: Decode it
3597 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003598 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 mb_utf8 = FALSE;
3600 if (mb_l > 1)
3601 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003602 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 /* Overlong encoded ASCII or ASCII with composing char
3604 * is displayed normally, except a NUL. */
3605 if (mb_c < 0x80)
3606 c = mb_c;
3607 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003608
3609 /* At start of the line we can have a composing char.
3610 * Draw it as a space with a composing char. */
3611 if (utf_iscomposing(mb_c))
3612 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003613 for (i = Screen_mco - 1; i > 0; --i)
3614 u8cc[i] = u8cc[i - 1];
3615 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003616 mb_c = ' ';
3617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619
3620 if ((mb_l == 1 && c >= 0x80)
3621 || (mb_l >= 1 && mb_c == 0)
3622 || (mb_l > 1 && (!vim_isprintc(mb_c)
3623 || mb_c >= 0x10000)))
3624 {
3625 /*
3626 * Illegal UTF-8 byte: display as <xx>.
3627 * Non-BMP character : display as ? or fullwidth ?.
3628 */
3629 if (mb_c < 0x10000)
3630 {
3631 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003632# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (wp->w_p_rl) /* reverse */
3634 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 else if (utf_char2cells(mb_c) != 2)
3638 STRCPY(extra, "?");
3639 else
3640 /* 0xff1f in UTF-8: full-width '?' */
3641 STRCPY(extra, "\357\274\237");
3642
3643 p_extra = extra;
3644 c = *p_extra;
3645 mb_c = mb_ptr2char_adv(&p_extra);
3646 mb_utf8 = (c >= 0x80);
3647 n_extra = (int)STRLEN(p_extra);
3648 c_extra = NUL;
3649 if (area_attr == 0 && search_attr == 0)
3650 {
3651 n_attr = n_extra + 1;
3652 extra_attr = hl_attr(HLF_8);
3653 saved_attr2 = char_attr; /* save current attr */
3654 }
3655 }
3656 else if (mb_l == 0) /* at the NUL at end-of-line */
3657 mb_l = 1;
3658#ifdef FEAT_ARABIC
3659 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3660 {
3661 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003662 int pc, pc1, nc;
3663 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 /* The idea of what is the previous and next
3666 * character depends on 'rightleft'. */
3667 if (wp->w_p_rl)
3668 {
3669 pc = prev_c;
3670 pc1 = prev_c1;
3671 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003672 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
3674 else
3675 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003676 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003678 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 prev_c = mb_c;
3681
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003682 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684 else
3685 prev_c = mb_c;
3686#endif
3687 }
3688 else /* enc_dbcs */
3689 {
3690 mb_l = MB_BYTE2LEN(c);
3691 if (mb_l == 0) /* at the NUL at end-of-line */
3692 mb_l = 1;
3693 else if (mb_l > 1)
3694 {
3695 /* We assume a second byte below 32 is illegal.
3696 * Hopefully this is OK for all double-byte encodings!
3697 */
3698 if (ptr[1] >= 32)
3699 mb_c = (c << 8) + ptr[1];
3700 else
3701 {
3702 if (ptr[1] == NUL)
3703 {
3704 /* head byte at end of line */
3705 mb_l = 1;
3706 transchar_nonprint(extra, c);
3707 }
3708 else
3709 {
3710 /* illegal tail byte */
3711 mb_l = 2;
3712 STRCPY(extra, "XX");
3713 }
3714 p_extra = extra;
3715 n_extra = (int)STRLEN(extra) - 1;
3716 c_extra = NUL;
3717 c = *p_extra++;
3718 if (area_attr == 0 && search_attr == 0)
3719 {
3720 n_attr = n_extra + 1;
3721 extra_attr = hl_attr(HLF_8);
3722 saved_attr2 = char_attr; /* save current attr */
3723 }
3724 mb_c = c;
3725 }
3726 }
3727 }
3728 /* If a double-width char doesn't fit display a '>' in the
3729 * last column; the character is displayed at the start of the
3730 * next line. */
3731 if ((
3732# ifdef FEAT_RIGHTLEFT
3733 wp->w_p_rl ? (col <= 0) :
3734# endif
3735 (col >= W_WIDTH(wp) - 1))
3736 && (*mb_char2cells)(mb_c) == 2)
3737 {
3738 c = '>';
3739 mb_c = c;
3740 mb_utf8 = FALSE;
3741 mb_l = 1;
3742 multi_attr = hl_attr(HLF_AT);
3743 /* Put pointer back so that the character will be
3744 * displayed at the start of the next line. */
3745 --ptr;
3746 }
3747 else if (*ptr != NUL)
3748 ptr += mb_l - 1;
3749
3750 /* If a double-width char doesn't fit at the left side display
3751 * a '<' in the first column. */
3752 if (n_skip > 0 && mb_l > 1)
3753 {
3754 extra[0] = '<';
3755 p_extra = extra;
3756 n_extra = 1;
3757 c_extra = NUL;
3758 c = ' ';
3759 if (area_attr == 0 && search_attr == 0)
3760 {
3761 n_attr = n_extra + 1;
3762 extra_attr = hl_attr(HLF_AT);
3763 saved_attr2 = char_attr; /* save current attr */
3764 }
3765 mb_c = c;
3766 mb_utf8 = FALSE;
3767 mb_l = 1;
3768 }
3769
3770 }
3771#endif
3772 ++ptr;
3773
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003774 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003775 if (wp->w_p_list && (c == 160
3776#ifdef FEAT_MBYTE
3777 || (mb_utf8 && mb_c == 160)
3778#endif
3779 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003780 {
3781 c = lcs_nbsp;
3782 if (area_attr == 0 && search_attr == 0)
3783 {
3784 n_attr = 1;
3785 extra_attr = hl_attr(HLF_8);
3786 saved_attr2 = char_attr; /* save current attr */
3787 }
3788#ifdef FEAT_MBYTE
3789 mb_c = c;
3790 if (enc_utf8 && (*mb_char2len)(c) > 1)
3791 {
3792 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003793 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003794 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003795 }
3796 else
3797 mb_utf8 = FALSE;
3798#endif
3799 }
3800
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (extra_check)
3802 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003803#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003804 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003805#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003806
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003807#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 /* Get syntax attribute, unless still at the start of the line
3809 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003810 v = (long)(ptr - line);
3811 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
3813 /* Get the syntax attribute for the character. If there
3814 * is an error, disable syntax highlighting. */
3815 save_did_emsg = did_emsg;
3816 did_emsg = FALSE;
3817
Bram Moolenaar217ad922005-03-20 22:37:15 +00003818 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003819# ifdef FEAT_SPELL
3820 has_spell ? &can_spell :
3821# endif
3822 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
3824 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003825 {
3826 wp->w_buffer->b_syn_error = TRUE;
3827 has_syntax = FALSE;
3828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 else
3830 did_emsg = save_did_emsg;
3831
3832 /* Need to get the line again, a multi-line regexp may
3833 * have made it invalid. */
3834 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3835 ptr = line + v;
3836
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003837 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003839 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003840 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003842#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003843
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003844#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003845 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003846 * Only do this when there is no syntax highlighting, the
3847 * @Spell cluster is not used or the current syntax item
3848 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003849 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003850 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003851 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003852# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003853 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003854 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003855# endif
3856 if (c != 0 && (
3857# ifdef FEAT_SYN_HL
3858 !has_syntax ||
3859# endif
3860 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003861 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003862 char_u *prev_ptr, *p;
3863 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003864 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003865# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003866 if (has_mbyte)
3867 {
3868 prev_ptr = ptr - mb_l;
3869 v -= mb_l - 1;
3870 }
3871 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003872# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003873 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003874
3875 /* Use nextline[] if possible, it has the start of the
3876 * next line concatenated. */
3877 if ((prev_ptr - line) - nextlinecol >= 0)
3878 p = nextline + (prev_ptr - line) - nextlinecol;
3879 else
3880 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003881 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003882 len = spell_check(wp, p, &spell_hlf, &cap_col,
3883 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003884 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003885
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003886 /* In Insert mode only highlight a word that
3887 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003888 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003889 && (State & INSERT) != 0
3890 && wp->w_cursor.lnum == lnum
3891 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003892 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003893 && wp->w_cursor.col < (colnr_T)word_end)
3894 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003895 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003896 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003897 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003898
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003899 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003900 && (p - nextline) + len > nextline_idx)
3901 {
3902 /* Remember that the good word continues at the
3903 * start of the next line. */
3904 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003905 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003906 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003907
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003908 /* Turn index into actual attributes. */
3909 if (spell_hlf != HLF_COUNT)
3910 spell_attr = highlight_attr[spell_hlf];
3911
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003912 if (cap_col > 0)
3913 {
3914 if (p != prev_ptr
3915 && (p - nextline) + cap_col >= nextline_idx)
3916 {
3917 /* Remember that the word in the next line
3918 * must start with a capital. */
3919 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003920 cap_col = (int)((p - nextline) + cap_col
3921 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003922 }
3923 else
3924 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003925 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003926 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003927 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003928 }
3929 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003930 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003931 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003932 char_attr = hl_combine_attr(char_attr, spell_attr);
3933 else
3934 char_attr = hl_combine_attr(spell_attr, char_attr);
3935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936#endif
3937#ifdef FEAT_LINEBREAK
3938 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00003939 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 */
3941 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003942 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
3944 n_extra = win_lbr_chartabsize(wp, ptr - (
3945# ifdef FEAT_MBYTE
3946 has_mbyte ? mb_l :
3947# endif
3948 1), (colnr_T)vcol, NULL) - 1;
3949 c_extra = ' ';
3950 if (vim_iswhite(c))
3951 c = ' ';
3952 }
3953#endif
3954
3955 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3956 {
3957 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003958 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
3960 n_attr = 1;
3961 extra_attr = hl_attr(HLF_8);
3962 saved_attr2 = char_attr; /* save current attr */
3963 }
3964#ifdef FEAT_MBYTE
3965 mb_c = c;
3966 if (enc_utf8 && (*mb_char2len)(c) > 1)
3967 {
3968 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003969 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003970 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972 else
3973 mb_utf8 = FALSE;
3974#endif
3975 }
3976 }
3977
3978 /*
3979 * Handling of non-printable characters.
3980 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003981 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
3983 /*
3984 * when getting a character from the file, we may have to
3985 * turn it into something else on the way to putting it
3986 * into "ScreenLines".
3987 */
3988 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3989 {
3990 /* tab amount depends on current column */
3991 n_extra = (int)wp->w_buffer->b_p_ts
3992 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3993#ifdef FEAT_MBYTE
3994 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3995#endif
3996 if (wp->w_p_list)
3997 {
3998 c = lcs_tab1;
3999 c_extra = lcs_tab2;
4000 n_attr = n_extra + 1;
4001 extra_attr = hl_attr(HLF_8);
4002 saved_attr2 = char_attr; /* save current attr */
4003#ifdef FEAT_MBYTE
4004 mb_c = c;
4005 if (enc_utf8 && (*mb_char2len)(c) > 1)
4006 {
4007 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004008 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004009 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011#endif
4012 }
4013 else
4014 {
4015 c_extra = ' ';
4016 c = ' ';
4017 }
4018 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004019 else if (c == NUL
4020 && ((wp->w_p_list && lcs_eol > 0)
4021 || ((fromcol >= 0 || fromcol_prev >= 0)
4022 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004023#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004024 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004025#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004026 && (
4027# ifdef FEAT_RIGHTLEFT
4028 wp->w_p_rl ? (col >= 0) :
4029# endif
4030 (col < W_WIDTH(wp)))
4031 && !(noinvcur
4032 && (colnr_T)vcol == wp->w_virtcol)))
4033 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004035 /* Display a '$' after the line or highlight an extra
4036 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4038 /* For a diff line the highlighting continues after the
4039 * "$". */
4040 if (
4041# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004042 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043# ifdef LINE_ATTR
4044 &&
4045# endif
4046# endif
4047# ifdef LINE_ATTR
4048 line_attr == 0
4049# endif
4050 )
4051#endif
4052 {
4053#ifdef FEAT_VIRTUALEDIT
4054 /* In virtualedit, visual selections may extend
4055 * beyond end of line. */
4056 if (area_highlighting && virtual_active()
4057 && tocol != MAXCOL && vcol < tocol)
4058 n_extra = 0;
4059 else
4060#endif
4061 {
4062 p_extra = at_end_str;
4063 n_extra = 1;
4064 c_extra = NUL;
4065 }
4066 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004067 if (wp->w_p_list)
4068 c = lcs_eol;
4069 else
4070 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 lcs_eol_one = -1;
4072 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004073 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
4075 extra_attr = hl_attr(HLF_AT);
4076 n_attr = 1;
4077 }
4078#ifdef FEAT_MBYTE
4079 mb_c = c;
4080 if (enc_utf8 && (*mb_char2len)(c) > 1)
4081 {
4082 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004083 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086 else
4087 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4088#endif
4089 }
4090 else if (c != NUL)
4091 {
4092 p_extra = transchar(c);
4093#ifdef FEAT_RIGHTLEFT
4094 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4095 rl_mirror(p_extra); /* reverse "<12>" */
4096#endif
4097 n_extra = byte2cells(c) - 1;
4098 c_extra = NUL;
4099 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004100 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
4102 n_attr = n_extra + 1;
4103 extra_attr = hl_attr(HLF_8);
4104 saved_attr2 = char_attr; /* save current attr */
4105 }
4106#ifdef FEAT_MBYTE
4107 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4108#endif
4109 }
4110#ifdef FEAT_VIRTUALEDIT
4111 else if (VIsual_active
4112 && (VIsual_mode == Ctrl_V
4113 || VIsual_mode == 'v')
4114 && virtual_active()
4115 && tocol != MAXCOL
4116 && vcol < tocol
4117 && (
4118# ifdef FEAT_RIGHTLEFT
4119 wp->w_p_rl ? (col >= 0) :
4120# endif
4121 (col < W_WIDTH(wp))))
4122 {
4123 c = ' ';
4124 --ptr; /* put it back at the NUL */
4125 }
4126#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004127#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 else if ((
4129# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004130 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 ) && (
4134# ifdef FEAT_RIGHTLEFT
4135 wp->w_p_rl ? (col >= 0) :
4136# endif
4137 (col < W_WIDTH(wp))))
4138 {
4139 /* Highlight until the right side of the window */
4140 c = ' ';
4141 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004142
4143 /* Remember we do the char for line highlighting. */
4144 ++did_line_attr;
4145
4146 /* don't do search HL for the rest of the line */
4147 if (line_attr != 0 && char_attr == search_attr && col > 0)
4148 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149# ifdef FEAT_DIFF
4150 if (diff_hlf == HLF_TXD)
4151 {
4152 diff_hlf = HLF_CHD;
4153 if (attr == 0 || char_attr != attr)
4154 char_attr = hl_attr(diff_hlf);
4155 }
4156# endif
4157 }
4158#endif
4159 }
4160 }
4161
4162 /* Don't override visual selection highlighting. */
4163 if (n_attr > 0
4164 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004165 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 char_attr = extra_attr;
4167
Bram Moolenaar81695252004-12-29 20:58:21 +00004168#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 /* XIM don't send preedit_start and preedit_end, but they send
4170 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4171 * im_is_preediting() here. */
4172 if (xic != NULL
4173 && lnum == curwin->w_cursor.lnum
4174 && (State & INSERT)
4175 && !p_imdisable
4176 && im_is_preediting()
4177 && draw_state == WL_LINE)
4178 {
4179 colnr_T tcol;
4180
4181 if (preedit_end_col == MAXCOL)
4182 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4183 else
4184 tcol = preedit_end_col;
4185 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4186 {
4187 if (feedback_old_attr < 0)
4188 {
4189 feedback_col = 0;
4190 feedback_old_attr = char_attr;
4191 }
4192 char_attr = im_get_feedback_attr(feedback_col);
4193 if (char_attr < 0)
4194 char_attr = feedback_old_attr;
4195 feedback_col++;
4196 }
4197 else if (feedback_old_attr >= 0)
4198 {
4199 char_attr = feedback_old_attr;
4200 feedback_old_attr = -1;
4201 feedback_col = 0;
4202 }
4203 }
4204#endif
4205 /*
4206 * Handle the case where we are in column 0 but not on the first
4207 * character of the line and the user wants us to show us a
4208 * special character (via 'listchars' option "precedes:<char>".
4209 */
4210 if (lcs_prec_todo != NUL
4211 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4212#ifdef FEAT_DIFF
4213 && filler_todo <= 0
4214#endif
4215 && draw_state > WL_NR
4216 && c != NUL)
4217 {
4218 c = lcs_prec;
4219 lcs_prec_todo = NUL;
4220#ifdef FEAT_MBYTE
4221 mb_c = c;
4222 if (enc_utf8 && (*mb_char2len)(c) > 1)
4223 {
4224 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004225 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004226 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228 else
4229 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4230#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004231 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
4233 saved_attr3 = char_attr; /* save current attr */
4234 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4235 n_attr3 = 1;
4236 }
4237 }
4238
4239 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004240 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004242 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004243#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004244 || did_line_attr == 1
4245#endif
4246 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004248#ifdef FEAT_SEARCH_EXTRA
4249 long prevcol = (long)(ptr - line) - (c == NUL);
4250#endif
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 /* invert at least one char, used for Visual and empty line or
4253 * highlight match at end of line. If it's beyond the last
4254 * char on the screen, just overwrite that one (tricky!) Not
4255 * needed when a '$' was displayed for 'list'. */
4256 if (lcs_eol == lcs_eol_one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004257 && ((area_attr != 0 && vcol == fromcol && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258#ifdef FEAT_SEARCH_EXTRA
4259 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004260 || ((prevcol == (long)search_hl.startcol
4261 || prevcol == (long)match_hl[0].startcol
4262 || prevcol == (long)match_hl[1].startcol
4263 || prevcol == (long)match_hl[2].startcol)
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004264# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004265 && did_line_attr <= 1
4266# endif
4267 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268#endif
4269 ))
4270 {
4271 int n = 0;
4272
4273#ifdef FEAT_RIGHTLEFT
4274 if (wp->w_p_rl)
4275 {
4276 if (col < 0)
4277 n = 1;
4278 }
4279 else
4280#endif
4281 {
4282 if (col >= W_WIDTH(wp))
4283 n = -1;
4284 }
4285 if (n != 0)
4286 {
4287 /* At the window boundary, highlight the last character
4288 * instead (better than nothing). */
4289 off += n;
4290 col += n;
4291 }
4292 else
4293 {
4294 /* Add a blank character to highlight. */
4295 ScreenLines[off] = ' ';
4296#ifdef FEAT_MBYTE
4297 if (enc_utf8)
4298 ScreenLinesUC[off] = 0;
4299#endif
4300 }
4301#ifdef FEAT_SEARCH_EXTRA
4302 if (area_attr == 0)
4303 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004304 for (i = 0; i <= 3; ++i)
4305 {
4306 if (i == 3)
4307 char_attr = search_hl.attr;
4308 else if ((ptr - line) - 1 == (long)match_hl[i].startcol)
4309 {
4310 char_attr = match_hl[i].attr;
4311 break;
4312 }
4313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315#endif
4316 ScreenAttrs[off] = char_attr;
4317#ifdef FEAT_RIGHTLEFT
4318 if (wp->w_p_rl)
4319 --col;
4320 else
4321#endif
4322 ++col;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004323 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
Bram Moolenaar91170f82006-05-05 21:15:17 +00004327 /*
4328 * At end of the text line.
4329 */
4330 if (c == NUL)
4331 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004332#ifdef FEAT_SYN_HL
4333 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004334 if (wp->w_p_wrap)
4335 v = wp->w_skipcol;
4336 else
4337 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004338 /* check if line ends before left margin */
4339 if (vcol < v + col - win_col_off(wp))
4340
4341 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004342 if (wp->w_p_cuc
4343 && (int)wp->w_virtcol >= vcol
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004344 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4345 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004346 && lnum != wp->w_cursor.lnum
4347# ifdef FEAT_RIGHTLEFT
4348 && !wp->w_p_rl
4349# endif
4350 )
4351 {
4352 while (col < W_WIDTH(wp))
4353 {
4354 ScreenLines[off] = ' ';
4355#ifdef FEAT_MBYTE
4356 if (enc_utf8)
4357 ScreenLinesUC[off] = 0;
4358#endif
4359 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004360 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004361 {
4362 ScreenAttrs[off] = hl_attr(HLF_CUC);
4363 break;
4364 }
4365 ScreenAttrs[off++] = 0;
4366 ++vcol;
4367 }
4368 }
4369#endif
4370
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4372 wp->w_p_rl);
4373 row++;
4374
4375 /*
4376 * Update w_cline_height and w_cline_folded if the cursor line was
4377 * updated (saves a call to plines() later).
4378 */
4379 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4380 {
4381 curwin->w_cline_row = startrow;
4382 curwin->w_cline_height = row - startrow;
4383#ifdef FEAT_FOLDING
4384 curwin->w_cline_folded = FALSE;
4385#endif
4386 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4387 }
4388
4389 break;
4390 }
4391
4392 /* line continues beyond line end */
4393 if (lcs_ext
4394 && !wp->w_p_wrap
4395#ifdef FEAT_DIFF
4396 && filler_todo <= 0
4397#endif
4398 && (
4399#ifdef FEAT_RIGHTLEFT
4400 wp->w_p_rl ? col == 0 :
4401#endif
4402 col == W_WIDTH(wp) - 1)
4403 && (*ptr != NUL
4404 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4405 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4406 {
4407 c = lcs_ext;
4408 char_attr = hl_attr(HLF_AT);
4409#ifdef FEAT_MBYTE
4410 mb_c = c;
4411 if (enc_utf8 && (*mb_char2len)(c) > 1)
4412 {
4413 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004414 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004415 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417 else
4418 mb_utf8 = FALSE;
4419#endif
4420 }
4421
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004422#ifdef FEAT_SYN_HL
4423 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4424 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004425 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004426 && lnum != wp->w_cursor.lnum
4427 && draw_state == WL_LINE)
4428 {
4429 vcol_save_attr = char_attr;
4430 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4431 }
4432 else
4433 vcol_save_attr = -1;
4434#endif
4435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 /*
4437 * Store character to be displayed.
4438 * Skip characters that are left of the screen for 'nowrap'.
4439 */
4440 vcol_prev = vcol;
4441 if (draw_state < WL_LINE || n_skip <= 0)
4442 {
4443 /*
4444 * Store the character.
4445 */
4446#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4447 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4448 {
4449 /* A double-wide character is: put first halve in left cell. */
4450 --off;
4451 --col;
4452 }
4453#endif
4454 ScreenLines[off] = c;
4455#ifdef FEAT_MBYTE
4456 if (enc_dbcs == DBCS_JPNU)
4457 ScreenLines2[off] = mb_c & 0xff;
4458 else if (enc_utf8)
4459 {
4460 if (mb_utf8)
4461 {
4462 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004463 if ((c & 0xff) == 0)
4464 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004465 for (i = 0; i < Screen_mco; ++i)
4466 {
4467 ScreenLinesC[i][off] = u8cc[i];
4468 if (u8cc[i] == 0)
4469 break;
4470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 else
4473 ScreenLinesUC[off] = 0;
4474 }
4475 if (multi_attr)
4476 {
4477 ScreenAttrs[off] = multi_attr;
4478 multi_attr = 0;
4479 }
4480 else
4481#endif
4482 ScreenAttrs[off] = char_attr;
4483
4484#ifdef FEAT_MBYTE
4485 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4486 {
4487 /* Need to fill two screen columns. */
4488 ++off;
4489 ++col;
4490 if (enc_utf8)
4491 /* UTF-8: Put a 0 in the second screen char. */
4492 ScreenLines[off] = 0;
4493 else
4494 /* DBCS: Put second byte in the second screen char. */
4495 ScreenLines[off] = mb_c & 0xff;
4496 ++vcol;
4497 /* When "tocol" is halfway a character, set it to the end of
4498 * the character, otherwise highlighting won't stop. */
4499 if (tocol == vcol)
4500 ++tocol;
4501#ifdef FEAT_RIGHTLEFT
4502 if (wp->w_p_rl)
4503 {
4504 /* now it's time to backup one cell */
4505 --off;
4506 --col;
4507 }
4508#endif
4509 }
4510#endif
4511#ifdef FEAT_RIGHTLEFT
4512 if (wp->w_p_rl)
4513 {
4514 --off;
4515 --col;
4516 }
4517 else
4518#endif
4519 {
4520 ++off;
4521 ++col;
4522 }
4523 }
4524 else
4525 --n_skip;
4526
4527 /* Only advance the "vcol" when after the 'number' column. */
4528 if (draw_state >= WL_SBR
4529#ifdef FEAT_DIFF
4530 && filler_todo <= 0
4531#endif
4532 )
4533 ++vcol;
4534
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004535#ifdef FEAT_SYN_HL
4536 if (vcol_save_attr >= 0)
4537 char_attr = vcol_save_attr;
4538#endif
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 /* restore attributes after "predeces" in 'listchars' */
4541 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4542 char_attr = saved_attr3;
4543
4544 /* restore attributes after last 'listchars' or 'number' char */
4545 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4546 char_attr = saved_attr2;
4547
4548 /*
4549 * At end of screen line and there is more to come: Display the line
4550 * so far. If there is no more to display it is catched above.
4551 */
4552 if ((
4553#ifdef FEAT_RIGHTLEFT
4554 wp->w_p_rl ? (col < 0) :
4555#endif
4556 (col >= W_WIDTH(wp)))
4557 && (*ptr != NUL
4558#ifdef FEAT_DIFF
4559 || filler_todo > 0
4560#endif
4561 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4562 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4563 )
4564 {
4565 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4566 wp->w_p_rl);
4567 ++row;
4568 ++screen_row;
4569
4570 /* When not wrapping and finished diff lines, or when displayed
4571 * '$' and highlighting until last column, break here. */
4572 if ((!wp->w_p_wrap
4573#ifdef FEAT_DIFF
4574 && filler_todo <= 0
4575#endif
4576 ) || lcs_eol_one == -1)
4577 break;
4578
4579 /* When the window is too narrow draw all "@" lines. */
4580 if (draw_state != WL_LINE
4581#ifdef FEAT_DIFF
4582 && filler_todo <= 0
4583#endif
4584 )
4585 {
4586 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4587#ifdef FEAT_VERTSPLIT
4588 draw_vsep_win(wp, row);
4589#endif
4590 row = endrow;
4591 }
4592
4593 /* When line got too long for screen break here. */
4594 if (row == endrow)
4595 {
4596 ++row;
4597 break;
4598 }
4599
4600 if (screen_cur_row == screen_row - 1
4601#ifdef FEAT_DIFF
4602 && filler_todo <= 0
4603#endif
4604 && W_WIDTH(wp) == Columns)
4605 {
4606 /* Remember that the line wraps, used for modeless copy. */
4607 LineWraps[screen_row - 1] = TRUE;
4608
4609 /*
4610 * Special trick to make copy/paste of wrapped lines work with
4611 * xterm/screen: write an extra character beyond the end of
4612 * the line. This will work with all terminal types
4613 * (regardless of the xn,am settings).
4614 * Only do this on a fast tty.
4615 * Only do this if the cursor is on the current line
4616 * (something has been written in it).
4617 * Don't do this for the GUI.
4618 * Don't do this for double-width characters.
4619 * Don't do this for a window not at the right screen border.
4620 */
4621 if (p_tf
4622#ifdef FEAT_GUI
4623 && !gui.in_use
4624#endif
4625#ifdef FEAT_MBYTE
4626 && !(has_mbyte
4627 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4628 || (*mb_off2cells)(LineOffset[screen_row - 1]
4629 + (int)Columns - 2) == 2))
4630#endif
4631 )
4632 {
4633 /* First make sure we are at the end of the screen line,
4634 * then output the same character again to let the
4635 * terminal know about the wrap. If the terminal doesn't
4636 * auto-wrap, we overwrite the character. */
4637 if (screen_cur_col != W_WIDTH(wp))
4638 screen_char(LineOffset[screen_row - 1]
4639 + (unsigned)Columns - 1,
4640 screen_row - 1, (int)(Columns - 1));
4641
4642#ifdef FEAT_MBYTE
4643 /* When there is a multi-byte character, just output a
4644 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004645 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4646 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 out_char(' ');
4648 else
4649#endif
4650 out_char(ScreenLines[LineOffset[screen_row - 1]
4651 + (Columns - 1)]);
4652 /* force a redraw of the first char on the next line */
4653 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4654 screen_start(); /* don't know where cursor is now */
4655 }
4656 }
4657
4658 col = 0;
4659 off = (unsigned)(current_ScreenLine - ScreenLines);
4660#ifdef FEAT_RIGHTLEFT
4661 if (wp->w_p_rl)
4662 {
4663 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4664 off += col;
4665 }
4666#endif
4667
4668 /* reset the drawing state for the start of a wrapped line */
4669 draw_state = WL_START;
4670 saved_n_extra = n_extra;
4671 saved_p_extra = p_extra;
4672 saved_c_extra = c_extra;
4673 saved_char_attr = char_attr;
4674 n_extra = 0;
4675 lcs_prec_todo = lcs_prec;
4676#ifdef FEAT_LINEBREAK
4677# ifdef FEAT_DIFF
4678 if (filler_todo <= 0)
4679# endif
4680 need_showbreak = TRUE;
4681#endif
4682#ifdef FEAT_DIFF
4683 --filler_todo;
4684 /* When the filler lines are actually below the last line of the
4685 * file, don't draw the line itself, break here. */
4686 if (filler_todo == 0 && wp->w_botfill)
4687 break;
4688#endif
4689 }
4690
4691 } /* for every character in the line */
4692
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004693#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004694 /* After an empty line check first word for capital. */
4695 if (*skipwhite(line) == NUL)
4696 {
4697 capcol_lnum = lnum + 1;
4698 cap_col = 0;
4699 }
4700#endif
4701
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return row;
4703}
4704
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004705#ifdef FEAT_MBYTE
4706static int comp_char_differs __ARGS((int, int));
4707
4708/*
4709 * Return if the composing characters at "off_from" and "off_to" differ.
4710 */
4711 static int
4712comp_char_differs(off_from, off_to)
4713 int off_from;
4714 int off_to;
4715{
4716 int i;
4717
4718 for (i = 0; i < Screen_mco; ++i)
4719 {
4720 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4721 return TRUE;
4722 if (ScreenLinesC[i][off_from] == 0)
4723 break;
4724 }
4725 return FALSE;
4726}
4727#endif
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729/*
4730 * Check whether the given character needs redrawing:
4731 * - the (first byte of the) character is different
4732 * - the attributes are different
4733 * - the character is multi-byte and the next byte is different
4734 */
4735 static int
4736char_needs_redraw(off_from, off_to, cols)
4737 int off_from;
4738 int off_to;
4739 int cols;
4740{
4741 if (cols > 0
4742 && ((ScreenLines[off_from] != ScreenLines[off_to]
4743 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4744
4745#ifdef FEAT_MBYTE
4746 || (enc_dbcs != 0
4747 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4748 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4749 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4750 : (cols > 1 && ScreenLines[off_from + 1]
4751 != ScreenLines[off_to + 1])))
4752 || (enc_utf8
4753 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4754 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004755 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756#endif
4757 ))
4758 return TRUE;
4759 return FALSE;
4760}
4761
4762/*
4763 * Move one "cooked" screen line to the screen, but only the characters that
4764 * have actually changed. Handle insert/delete character.
4765 * "coloff" gives the first column on the screen for this line.
4766 * "endcol" gives the columns where valid characters are.
4767 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4768 * needs to be cleared, negative otherwise.
4769 * "rlflag" is TRUE in a rightleft window:
4770 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4771 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4772 */
4773 static void
4774screen_line(row, coloff, endcol, clear_width
4775#ifdef FEAT_RIGHTLEFT
4776 , rlflag
4777#endif
4778 )
4779 int row;
4780 int coloff;
4781 int endcol;
4782 int clear_width;
4783#ifdef FEAT_RIGHTLEFT
4784 int rlflag;
4785#endif
4786{
4787 unsigned off_from;
4788 unsigned off_to;
4789 int col = 0;
4790#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4791 int hl;
4792#endif
4793 int force = FALSE; /* force update rest of the line */
4794 int redraw_this /* bool: does character need redraw? */
4795#ifdef FEAT_GUI
4796 = TRUE /* For GUI when while-loop empty */
4797#endif
4798 ;
4799 int redraw_next; /* redraw_this for next character */
4800#ifdef FEAT_MBYTE
4801 int clear_next = FALSE;
4802 int char_cells; /* 1: normal char */
4803 /* 2: occupies two display cells */
4804# define CHAR_CELLS char_cells
4805#else
4806# define CHAR_CELLS 1
4807#endif
4808
4809# ifdef FEAT_CLIPBOARD
4810 clip_may_clear_selection(row, row);
4811# endif
4812
4813 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4814 off_to = LineOffset[row] + coloff;
4815
4816#ifdef FEAT_RIGHTLEFT
4817 if (rlflag)
4818 {
4819 /* Clear rest first, because it's left of the text. */
4820 if (clear_width > 0)
4821 {
4822 while (col <= endcol && ScreenLines[off_to] == ' '
4823 && ScreenAttrs[off_to] == 0
4824# ifdef FEAT_MBYTE
4825 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4826# endif
4827 )
4828 {
4829 ++off_to;
4830 ++col;
4831 }
4832 if (col <= endcol)
4833 screen_fill(row, row + 1, col + coloff,
4834 endcol + coloff + 1, ' ', ' ', 0);
4835 }
4836 col = endcol + 1;
4837 off_to = LineOffset[row] + col + coloff;
4838 off_from += col;
4839 endcol = (clear_width > 0 ? clear_width : -clear_width);
4840 }
4841#endif /* FEAT_RIGHTLEFT */
4842
4843 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4844
4845 while (col < endcol)
4846 {
4847#ifdef FEAT_MBYTE
4848 if (has_mbyte && (col + 1 < endcol))
4849 char_cells = (*mb_off2cells)(off_from);
4850 else
4851 char_cells = 1;
4852#endif
4853
4854 redraw_this = redraw_next;
4855 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4856 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4857
4858#ifdef FEAT_GUI
4859 /* If the next character was bold, then redraw the current character to
4860 * remove any pixels that might have spilt over into us. This only
4861 * happens in the GUI.
4862 */
4863 if (redraw_next && gui.in_use)
4864 {
4865 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004866 if (hl > HL_ALL)
4867 hl = syn_attr2attr(hl);
4868 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 redraw_this = TRUE;
4870 }
4871#endif
4872
4873 if (redraw_this)
4874 {
4875 /*
4876 * Special handling when 'xs' termcap flag set (hpterm):
4877 * Attributes for characters are stored at the position where the
4878 * cursor is when writing the highlighting code. The
4879 * start-highlighting code must be written with the cursor on the
4880 * first highlighted character. The stop-highlighting code must
4881 * be written with the cursor just after the last highlighted
4882 * character.
4883 * Overwriting a character doesn't remove it's highlighting. Need
4884 * to clear the rest of the line, and force redrawing it
4885 * completely.
4886 */
4887 if ( p_wiv
4888 && !force
4889#ifdef FEAT_GUI
4890 && !gui.in_use
4891#endif
4892 && ScreenAttrs[off_to] != 0
4893 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4894 {
4895 /*
4896 * Need to remove highlighting attributes here.
4897 */
4898 windgoto(row, col + coloff);
4899 out_str(T_CE); /* clear rest of this screen line */
4900 screen_start(); /* don't know where cursor is now */
4901 force = TRUE; /* force redraw of rest of the line */
4902 redraw_next = TRUE; /* or else next char would miss out */
4903
4904 /*
4905 * If the previous character was highlighted, need to stop
4906 * highlighting at this character.
4907 */
4908 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4909 {
4910 screen_attr = ScreenAttrs[off_to - 1];
4911 term_windgoto(row, col + coloff);
4912 screen_stop_highlight();
4913 }
4914 else
4915 screen_attr = 0; /* highlighting has stopped */
4916 }
4917#ifdef FEAT_MBYTE
4918 if (enc_dbcs != 0)
4919 {
4920 /* Check if overwriting a double-byte with a single-byte or
4921 * the other way around requires another character to be
4922 * redrawn. For UTF-8 this isn't needed, because comparing
4923 * ScreenLinesUC[] is sufficient. */
4924 if (char_cells == 1
4925 && col + 1 < endcol
4926 && (*mb_off2cells)(off_to) > 1)
4927 {
4928 /* Writing a single-cell character over a double-cell
4929 * character: need to redraw the next cell. */
4930 ScreenLines[off_to + 1] = 0;
4931 redraw_next = TRUE;
4932 }
4933 else if (char_cells == 2
4934 && col + 2 < endcol
4935 && (*mb_off2cells)(off_to) == 1
4936 && (*mb_off2cells)(off_to + 1) > 1)
4937 {
4938 /* Writing the second half of a double-cell character over
4939 * a double-cell character: need to redraw the second
4940 * cell. */
4941 ScreenLines[off_to + 2] = 0;
4942 redraw_next = TRUE;
4943 }
4944
4945 if (enc_dbcs == DBCS_JPNU)
4946 ScreenLines2[off_to] = ScreenLines2[off_from];
4947 }
4948 /* When writing a single-width character over a double-width
4949 * character and at the end of the redrawn text, need to clear out
4950 * the right halve of the old character.
4951 * Also required when writing the right halve of a double-width
4952 * char over the left halve of an existing one. */
4953 if (has_mbyte && col + char_cells == endcol
4954 && ((char_cells == 1
4955 && (*mb_off2cells)(off_to) > 1)
4956 || (char_cells == 2
4957 && (*mb_off2cells)(off_to) == 1
4958 && (*mb_off2cells)(off_to + 1) > 1)))
4959 clear_next = TRUE;
4960#endif
4961
4962 ScreenLines[off_to] = ScreenLines[off_from];
4963#ifdef FEAT_MBYTE
4964 if (enc_utf8)
4965 {
4966 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4967 if (ScreenLinesUC[off_from] != 0)
4968 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004969 int i;
4970
4971 for (i = 0; i < Screen_mco; ++i)
4972 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 }
4975 if (char_cells == 2)
4976 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4977#endif
4978
4979#if defined(FEAT_GUI) || defined(UNIX)
4980 /* The bold trick makes a single row of pixels appear in the next
4981 * character. When a bold character is removed, the next
4982 * character should be redrawn too. This happens for our own GUI
4983 * and for some xterms. */
4984 if (
4985# ifdef FEAT_GUI
4986 gui.in_use
4987# endif
4988# if defined(FEAT_GUI) && defined(UNIX)
4989 ||
4990# endif
4991# ifdef UNIX
4992 term_is_xterm
4993# endif
4994 )
4995 {
4996 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004997 if (hl > HL_ALL)
4998 hl = syn_attr2attr(hl);
4999 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 redraw_next = TRUE;
5001 }
5002#endif
5003 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5004#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005005 /* For simplicity set the attributes of second half of a
5006 * double-wide character equal to the first half. */
5007 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005009
5010 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 else
5013#endif
5014 screen_char(off_to, row, col + coloff);
5015 }
5016 else if ( p_wiv
5017#ifdef FEAT_GUI
5018 && !gui.in_use
5019#endif
5020 && col + coloff > 0)
5021 {
5022 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5023 {
5024 /*
5025 * Don't output stop-highlight when moving the cursor, it will
5026 * stop the highlighting when it should continue.
5027 */
5028 screen_attr = 0;
5029 }
5030 else if (screen_attr != 0)
5031 screen_stop_highlight();
5032 }
5033
5034 off_to += CHAR_CELLS;
5035 off_from += CHAR_CELLS;
5036 col += CHAR_CELLS;
5037 }
5038
5039#ifdef FEAT_MBYTE
5040 if (clear_next)
5041 {
5042 /* Clear the second half of a double-wide character of which the left
5043 * half was overwritten with a single-wide character. */
5044 ScreenLines[off_to] = ' ';
5045 if (enc_utf8)
5046 ScreenLinesUC[off_to] = 0;
5047 screen_char(off_to, row, col + coloff);
5048 }
5049#endif
5050
5051 if (clear_width > 0
5052#ifdef FEAT_RIGHTLEFT
5053 && !rlflag
5054#endif
5055 )
5056 {
5057#ifdef FEAT_GUI
5058 int startCol = col;
5059#endif
5060
5061 /* blank out the rest of the line */
5062 while (col < clear_width && ScreenLines[off_to] == ' '
5063 && ScreenAttrs[off_to] == 0
5064#ifdef FEAT_MBYTE
5065 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5066#endif
5067 )
5068 {
5069 ++off_to;
5070 ++col;
5071 }
5072 if (col < clear_width)
5073 {
5074#ifdef FEAT_GUI
5075 /*
5076 * In the GUI, clearing the rest of the line may leave pixels
5077 * behind if the first character cleared was bold. Some bold
5078 * fonts spill over the left. In this case we redraw the previous
5079 * character too. If we didn't skip any blanks above, then we
5080 * only redraw if the character wasn't already redrawn anyway.
5081 */
5082 if (gui.in_use && (col > startCol || !redraw_this)
5083# ifdef FEAT_MBYTE
5084 && enc_dbcs == 0
5085# endif
5086 )
5087 {
5088 hl = ScreenAttrs[off_to];
5089 if (hl > HL_ALL || (hl & HL_BOLD))
5090 screen_char(off_to - 1, row, col + coloff - 1);
5091 }
5092#endif
5093 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5094 ' ', ' ', 0);
5095#ifdef FEAT_VERTSPLIT
5096 off_to += clear_width - col;
5097 col = clear_width;
5098#endif
5099 }
5100 }
5101
5102 if (clear_width > 0)
5103 {
5104#ifdef FEAT_VERTSPLIT
5105 /* For a window that's left of another, draw the separator char. */
5106 if (col + coloff < Columns)
5107 {
5108 int c;
5109
5110 c = fillchar_vsep(&hl);
5111 if (ScreenLines[off_to] != c
5112# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005113 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5114 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115# endif
5116 || ScreenAttrs[off_to] != hl)
5117 {
5118 ScreenLines[off_to] = c;
5119 ScreenAttrs[off_to] = hl;
5120# ifdef FEAT_MBYTE
5121 if (enc_utf8)
5122 {
5123 if (c >= 0x80)
5124 {
5125 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005126 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 else
5129 ScreenLinesUC[off_to] = 0;
5130 }
5131# endif
5132 screen_char(off_to, row, col + coloff);
5133 }
5134 }
5135 else
5136#endif
5137 LineWraps[row] = FALSE;
5138 }
5139}
5140
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005141#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005143 * Mirror text "str" for right-left displaying.
5144 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005146 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147rl_mirror(str)
5148 char_u *str;
5149{
5150 char_u *p1, *p2;
5151 int t;
5152
5153 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5154 {
5155 t = *p1;
5156 *p1 = *p2;
5157 *p2 = t;
5158 }
5159}
5160#endif
5161
5162#if defined(FEAT_WINDOWS) || defined(PROTO)
5163/*
5164 * mark all status lines for redraw; used after first :cd
5165 */
5166 void
5167status_redraw_all()
5168{
5169 win_T *wp;
5170
5171 for (wp = firstwin; wp; wp = wp->w_next)
5172 if (wp->w_status_height)
5173 {
5174 wp->w_redr_status = TRUE;
5175 redraw_later(VALID);
5176 }
5177}
5178
5179/*
5180 * mark all status lines of the current buffer for redraw
5181 */
5182 void
5183status_redraw_curbuf()
5184{
5185 win_T *wp;
5186
5187 for (wp = firstwin; wp; wp = wp->w_next)
5188 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5189 {
5190 wp->w_redr_status = TRUE;
5191 redraw_later(VALID);
5192 }
5193}
5194
5195/*
5196 * Redraw all status lines that need to be redrawn.
5197 */
5198 void
5199redraw_statuslines()
5200{
5201 win_T *wp;
5202
5203 for (wp = firstwin; wp; wp = wp->w_next)
5204 if (wp->w_redr_status)
5205 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005206 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005207 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208}
5209#endif
5210
5211#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5212/*
5213 * Redraw all status lines at the bottom of frame "frp".
5214 */
5215 void
5216win_redraw_last_status(frp)
5217 frame_T *frp;
5218{
5219 if (frp->fr_layout == FR_LEAF)
5220 frp->fr_win->w_redr_status = TRUE;
5221 else if (frp->fr_layout == FR_ROW)
5222 {
5223 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5224 win_redraw_last_status(frp);
5225 }
5226 else /* frp->fr_layout == FR_COL */
5227 {
5228 frp = frp->fr_child;
5229 while (frp->fr_next != NULL)
5230 frp = frp->fr_next;
5231 win_redraw_last_status(frp);
5232 }
5233}
5234#endif
5235
5236#ifdef FEAT_VERTSPLIT
5237/*
5238 * Draw the verticap separator right of window "wp" starting with line "row".
5239 */
5240 static void
5241draw_vsep_win(wp, row)
5242 win_T *wp;
5243 int row;
5244{
5245 int hl;
5246 int c;
5247
5248 if (wp->w_vsep_width)
5249 {
5250 /* draw the vertical separator right of this window */
5251 c = fillchar_vsep(&hl);
5252 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5253 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5254 c, ' ', hl);
5255 }
5256}
5257#endif
5258
5259#ifdef FEAT_WILDMENU
5260static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005261static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
5263/*
5264 * Get the lenght of an item as it will be shown in the status line.
5265 */
5266 static int
5267status_match_len(xp, s)
5268 expand_T *xp;
5269 char_u *s;
5270{
5271 int len = 0;
5272
5273#ifdef FEAT_MENU
5274 int emenu = (xp->xp_context == EXPAND_MENUS
5275 || xp->xp_context == EXPAND_MENUNAMES);
5276
5277 /* Check for menu separators - replace with '|'. */
5278 if (emenu && menu_is_separator(s))
5279 return 1;
5280#endif
5281
5282 while (*s != NUL)
5283 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005284 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005286 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005287 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
5289
5290 return len;
5291}
5292
5293/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005294 * Return TRUE for characters that are not displayed in a status match.
5295 * These are backslashes used for escaping. Do show backslashes in help tags.
5296 */
5297 static int
5298skip_status_match_char(xp, s)
5299 expand_T *xp;
5300 char_u *s;
5301{
5302 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5303#ifdef FEAT_MENU
5304 || ((xp->xp_context == EXPAND_MENUS
5305 || xp->xp_context == EXPAND_MENUNAMES)
5306 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5307#endif
5308 );
5309}
5310
5311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 * Show wildchar matches in the status line.
5313 * Show at least the "match" item.
5314 * We start at item 'first_match' in the list and show all matches that fit.
5315 *
5316 * If inversion is possible we use it. Else '=' characters are used.
5317 */
5318 void
5319win_redr_status_matches(xp, num_matches, matches, match, showtail)
5320 expand_T *xp;
5321 int num_matches;
5322 char_u **matches; /* list of matches */
5323 int match;
5324 int showtail;
5325{
5326#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5327 int row;
5328 char_u *buf;
5329 int len;
5330 int clen; /* lenght in screen cells */
5331 int fillchar;
5332 int attr;
5333 int i;
5334 int highlight = TRUE;
5335 char_u *selstart = NULL;
5336 int selstart_col = 0;
5337 char_u *selend = NULL;
5338 static int first_match = 0;
5339 int add_left = FALSE;
5340 char_u *s;
5341#ifdef FEAT_MENU
5342 int emenu;
5343#endif
5344#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5345 int l;
5346#endif
5347
5348 if (matches == NULL) /* interrupted completion? */
5349 return;
5350
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005351#ifdef FEAT_MBYTE
5352 if (has_mbyte)
5353 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5354 else
5355#endif
5356 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 if (buf == NULL)
5358 return;
5359
5360 if (match == -1) /* don't show match but original text */
5361 {
5362 match = 0;
5363 highlight = FALSE;
5364 }
5365 /* count 1 for the ending ">" */
5366 clen = status_match_len(xp, L_MATCH(match)) + 3;
5367 if (match == 0)
5368 first_match = 0;
5369 else if (match < first_match)
5370 {
5371 /* jumping left, as far as we can go */
5372 first_match = match;
5373 add_left = TRUE;
5374 }
5375 else
5376 {
5377 /* check if match fits on the screen */
5378 for (i = first_match; i < match; ++i)
5379 clen += status_match_len(xp, L_MATCH(i)) + 2;
5380 if (first_match > 0)
5381 clen += 2;
5382 /* jumping right, put match at the left */
5383 if ((long)clen > Columns)
5384 {
5385 first_match = match;
5386 /* if showing the last match, we can add some on the left */
5387 clen = 2;
5388 for (i = match; i < num_matches; ++i)
5389 {
5390 clen += status_match_len(xp, L_MATCH(i)) + 2;
5391 if ((long)clen >= Columns)
5392 break;
5393 }
5394 if (i == num_matches)
5395 add_left = TRUE;
5396 }
5397 }
5398 if (add_left)
5399 while (first_match > 0)
5400 {
5401 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5402 if ((long)clen >= Columns)
5403 break;
5404 --first_match;
5405 }
5406
5407 fillchar = fillchar_status(&attr, TRUE);
5408
5409 if (first_match == 0)
5410 {
5411 *buf = NUL;
5412 len = 0;
5413 }
5414 else
5415 {
5416 STRCPY(buf, "< ");
5417 len = 2;
5418 }
5419 clen = len;
5420
5421 i = first_match;
5422 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5423 {
5424 if (i == match)
5425 {
5426 selstart = buf + len;
5427 selstart_col = clen;
5428 }
5429
5430 s = L_MATCH(i);
5431 /* Check for menu separators - replace with '|' */
5432#ifdef FEAT_MENU
5433 emenu = (xp->xp_context == EXPAND_MENUS
5434 || xp->xp_context == EXPAND_MENUNAMES);
5435 if (emenu && menu_is_separator(s))
5436 {
5437 STRCPY(buf + len, transchar('|'));
5438 l = (int)STRLEN(buf + len);
5439 len += l;
5440 clen += l;
5441 }
5442 else
5443#endif
5444 for ( ; *s != NUL; ++s)
5445 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005446 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 ++s;
5448 clen += ptr2cells(s);
5449#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005450 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 {
5452 STRNCPY(buf + len, s, l);
5453 s += l - 1;
5454 len += l;
5455 }
5456 else
5457#endif
5458 {
5459 STRCPY(buf + len, transchar_byte(*s));
5460 len += (int)STRLEN(buf + len);
5461 }
5462 }
5463 if (i == match)
5464 selend = buf + len;
5465
5466 *(buf + len++) = ' ';
5467 *(buf + len++) = ' ';
5468 clen += 2;
5469 if (++i == num_matches)
5470 break;
5471 }
5472
5473 if (i != num_matches)
5474 {
5475 *(buf + len++) = '>';
5476 ++clen;
5477 }
5478
5479 buf[len] = NUL;
5480
5481 row = cmdline_row - 1;
5482 if (row >= 0)
5483 {
5484 if (wild_menu_showing == 0)
5485 {
5486 if (msg_scrolled > 0)
5487 {
5488 /* Put the wildmenu just above the command line. If there is
5489 * no room, scroll the screen one line up. */
5490 if (cmdline_row == Rows - 1)
5491 {
5492 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5493 ++msg_scrolled;
5494 }
5495 else
5496 {
5497 ++cmdline_row;
5498 ++row;
5499 }
5500 wild_menu_showing = WM_SCROLLED;
5501 }
5502 else
5503 {
5504 /* Create status line if needed by setting 'laststatus' to 2.
5505 * Set 'winminheight' to zero to avoid that the window is
5506 * resized. */
5507 if (lastwin->w_status_height == 0)
5508 {
5509 save_p_ls = p_ls;
5510 save_p_wmh = p_wmh;
5511 p_ls = 2;
5512 p_wmh = 0;
5513 last_status(FALSE);
5514 }
5515 wild_menu_showing = WM_SHOWN;
5516 }
5517 }
5518
5519 screen_puts(buf, row, 0, attr);
5520 if (selstart != NULL && highlight)
5521 {
5522 *selend = NUL;
5523 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5524 }
5525
5526 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5527 }
5528
5529#ifdef FEAT_VERTSPLIT
5530 win_redraw_last_status(topframe);
5531#else
5532 lastwin->w_redr_status = TRUE;
5533#endif
5534 vim_free(buf);
5535}
5536#endif
5537
5538#if defined(FEAT_WINDOWS) || defined(PROTO)
5539/*
5540 * Redraw the status line of window wp.
5541 *
5542 * If inversion is possible we use it. Else '=' characters are used.
5543 */
5544 void
5545win_redr_status(wp)
5546 win_T *wp;
5547{
5548 int row;
5549 char_u *p;
5550 int len;
5551 int fillchar;
5552 int attr;
5553 int this_ru_col;
5554
5555 wp->w_redr_status = FALSE;
5556 if (wp->w_status_height == 0)
5557 {
5558 /* no status line, can only be last window */
5559 redraw_cmdline = TRUE;
5560 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005561 else if (!redrawing()
5562#ifdef FEAT_INS_EXPAND
5563 /* don't update status line when popup menu is visible and may be
5564 * drawn over it */
5565 || pum_visible()
5566#endif
5567 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
5569 /* Don't redraw right now, do it later. */
5570 wp->w_redr_status = TRUE;
5571 }
5572#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005573 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
5575 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005576 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578#endif
5579 else
5580 {
5581 fillchar = fillchar_status(&attr, wp == curwin);
5582
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005583 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 p = NameBuff;
5585 len = (int)STRLEN(p);
5586
5587 if (wp->w_buffer->b_help
5588#ifdef FEAT_QUICKFIX
5589 || wp->w_p_pvw
5590#endif
5591 || bufIsChanged(wp->w_buffer)
5592 || wp->w_buffer->b_p_ro)
5593 *(p + len++) = ' ';
5594 if (wp->w_buffer->b_help)
5595 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005596 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 len += (int)STRLEN(p + len);
5598 }
5599#ifdef FEAT_QUICKFIX
5600 if (wp->w_p_pvw)
5601 {
5602 STRCPY(p + len, _("[Preview]"));
5603 len += (int)STRLEN(p + len);
5604 }
5605#endif
5606 if (bufIsChanged(wp->w_buffer))
5607 {
5608 STRCPY(p + len, "[+]");
5609 len += 3;
5610 }
5611 if (wp->w_buffer->b_p_ro)
5612 {
5613 STRCPY(p + len, "[RO]");
5614 len += 4;
5615 }
5616
5617#ifndef FEAT_VERTSPLIT
5618 this_ru_col = ru_col;
5619 if (this_ru_col < (Columns + 1) / 2)
5620 this_ru_col = (Columns + 1) / 2;
5621#else
5622 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5623 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5624 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5625 if (this_ru_col <= 1)
5626 {
5627 p = (char_u *)"<"; /* No room for file name! */
5628 len = 1;
5629 }
5630 else
5631#endif
5632#ifdef FEAT_MBYTE
5633 if (has_mbyte)
5634 {
5635 int clen = 0, i;
5636
5637 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005638 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 clen += (*mb_ptr2cells)(p + i);
5640 /* Find first character that will fit.
5641 * Going from start to end is much faster for DBCS. */
5642 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005643 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 clen -= (*mb_ptr2cells)(p + i);
5645 len = clen;
5646 if (i > 0)
5647 {
5648 p = p + i - 1;
5649 *p = '<';
5650 ++len;
5651 }
5652
5653 }
5654 else
5655#endif
5656 if (len > this_ru_col - 1)
5657 {
5658 p += len - (this_ru_col - 1);
5659 *p = '<';
5660 len = this_ru_col - 1;
5661 }
5662
5663 row = W_WINROW(wp) + wp->w_height;
5664 screen_puts(p, row, W_WINCOL(wp), attr);
5665 screen_fill(row, row + 1, len + W_WINCOL(wp),
5666 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5667
5668 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5669 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5670 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5671 - 1 + W_WINCOL(wp)), attr);
5672
5673#ifdef FEAT_CMDL_INFO
5674 win_redr_ruler(wp, TRUE);
5675#endif
5676 }
5677
5678#ifdef FEAT_VERTSPLIT
5679 /*
5680 * May need to draw the character below the vertical separator.
5681 */
5682 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5683 {
5684 if (stl_connected(wp))
5685 fillchar = fillchar_status(&attr, wp == curwin);
5686 else
5687 fillchar = fillchar_vsep(&attr);
5688 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5689 attr);
5690 }
5691#endif
5692}
5693
Bram Moolenaar238a5642006-02-21 22:12:05 +00005694#ifdef FEAT_STL_OPT
5695/*
5696 * Redraw the status line according to 'statusline' and take care of any
5697 * errors encountered.
5698 */
5699 static void
5700redraw_custum_statusline(wp)
5701 win_T *wp;
5702{
5703 int save_called_emsg = called_emsg;
5704
5705 called_emsg = FALSE;
5706 win_redr_custom(wp, FALSE);
5707 if (called_emsg)
5708 set_string_option_direct((char_u *)"statusline", -1,
5709 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005710 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005711 called_emsg |= save_called_emsg;
5712}
5713#endif
5714
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715# ifdef FEAT_VERTSPLIT
5716/*
5717 * Return TRUE if the status line of window "wp" is connected to the status
5718 * line of the window right of it. If not, then it's a vertical separator.
5719 * Only call if (wp->w_vsep_width != 0).
5720 */
5721 int
5722stl_connected(wp)
5723 win_T *wp;
5724{
5725 frame_T *fr;
5726
5727 fr = wp->w_frame;
5728 while (fr->fr_parent != NULL)
5729 {
5730 if (fr->fr_parent->fr_layout == FR_COL)
5731 {
5732 if (fr->fr_next != NULL)
5733 break;
5734 }
5735 else
5736 {
5737 if (fr->fr_next != NULL)
5738 return TRUE;
5739 }
5740 fr = fr->fr_parent;
5741 }
5742 return FALSE;
5743}
5744# endif
5745
5746#endif /* FEAT_WINDOWS */
5747
5748#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5749/*
5750 * Get the value to show for the language mappings, active 'keymap'.
5751 */
5752 int
5753get_keymap_str(wp, buf, len)
5754 win_T *wp;
5755 char_u *buf; /* buffer for the result */
5756 int len; /* length of buffer */
5757{
5758 char_u *p;
5759
5760 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5761 return FALSE;
5762
5763 {
5764#ifdef FEAT_EVAL
5765 buf_T *old_curbuf = curbuf;
5766 win_T *old_curwin = curwin;
5767 char_u *s;
5768
5769 curbuf = wp->w_buffer;
5770 curwin = wp;
5771 STRCPY(buf, "b:keymap_name"); /* must be writable */
5772 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005773 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 --emsg_skip;
5775 curbuf = old_curbuf;
5776 curwin = old_curwin;
5777 if (p == NULL || *p == NUL)
5778#endif
5779 {
5780#ifdef FEAT_KEYMAP
5781 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5782 p = wp->w_buffer->b_p_keymap;
5783 else
5784#endif
5785 p = (char_u *)"lang";
5786 }
5787 if ((int)(STRLEN(p) + 3) < len)
5788 sprintf((char *)buf, "<%s>", p);
5789 else
5790 buf[0] = NUL;
5791#ifdef FEAT_EVAL
5792 vim_free(s);
5793#endif
5794 }
5795 return buf[0] != NUL;
5796}
5797#endif
5798
5799#if defined(FEAT_STL_OPT) || defined(PROTO)
5800/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005801 * Redraw the status line or ruler of window "wp".
5802 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 */
5804 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005805win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005807 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
5809 int attr;
5810 int curattr;
5811 int row;
5812 int col = 0;
5813 int maxwidth;
5814 int width;
5815 int n;
5816 int len;
5817 int fillchar;
5818 char_u buf[MAXPATHL];
5819 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005820 struct stl_hlrec hltab[STL_MAX_ITEM];
5821 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005822 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
5824 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005825 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005827 /* Use 'tabline'. Always at the first line of the screen. */
5828 p = p_tal;
5829 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005830 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005831 attr = hl_attr(HLF_TPF);
5832 maxwidth = Columns;
5833# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005834 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005837 else
5838 {
5839 row = W_WINROW(wp) + wp->w_height;
5840 fillchar = fillchar_status(&attr, wp == curwin);
5841 maxwidth = W_WIDTH(wp);
5842
5843 if (draw_ruler)
5844 {
5845 p = p_ruf;
5846 /* advance past any leading group spec - implicit in ru_col */
5847 if (*p == '%')
5848 {
5849 if (*++p == '-')
5850 p++;
5851 if (atoi((char *) p))
5852 while (VIM_ISDIGIT(*p))
5853 p++;
5854 if (*p++ != '(')
5855 p = p_ruf;
5856 }
5857#ifdef FEAT_VERTSPLIT
5858 col = ru_col - (Columns - W_WIDTH(wp));
5859 if (col < (W_WIDTH(wp) + 1) / 2)
5860 col = (W_WIDTH(wp) + 1) / 2;
5861#else
5862 col = ru_col;
5863 if (col > (Columns + 1) / 2)
5864 col = (Columns + 1) / 2;
5865#endif
5866 maxwidth = W_WIDTH(wp) - col;
5867#ifdef FEAT_WINDOWS
5868 if (!wp->w_status_height)
5869#endif
5870 {
5871 row = Rows - 1;
5872 --maxwidth; /* writing in last column may cause scrolling */
5873 fillchar = ' ';
5874 attr = 0;
5875 }
5876
5877# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005878 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005879# endif
5880 }
5881 else
5882 {
5883 if (*wp->w_p_stl != NUL)
5884 p = wp->w_p_stl;
5885 else
5886 p = p_stl;
5887# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005888 use_sandbox = was_set_insecurely((char_u *)"statusline",
5889 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005890# endif
5891 }
5892
5893#ifdef FEAT_VERTSPLIT
5894 col += W_WINCOL(wp);
5895#endif
5896 }
5897
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 if (maxwidth <= 0)
5899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005901 width = build_stl_str_hl(wp == NULL ? curwin : wp,
5902 buf, sizeof(buf),
5903 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005904 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005905 len = (int)STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
5907 while (width < maxwidth && len < sizeof(buf) - 1)
5908 {
5909#ifdef FEAT_MBYTE
5910 len += (*mb_char2bytes)(fillchar, buf + len);
5911#else
5912 buf[len++] = fillchar;
5913#endif
5914 ++width;
5915 }
5916 buf[len] = NUL;
5917
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005918 /*
5919 * Draw each snippet with the specified highlighting.
5920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 curattr = attr;
5922 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005923 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005925 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 screen_puts_len(p, len, row, col, curattr);
5927 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005928 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005930 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005932 else if (hltab[n].userhl < 0)
5933 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00005935 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005936 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937#endif
5938 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005939 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 }
5941 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005942
5943 if (wp == NULL)
5944 {
5945 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
5946 col = 0;
5947 len = 0;
5948 p = buf;
5949 fillchar = 0;
5950 for (n = 0; tabtab[n].start != NULL; n++)
5951 {
5952 len += vim_strnsize(p, (int)(tabtab[n].start - p));
5953 while (col < len)
5954 TabPageIdxs[col++] = fillchar;
5955 p = tabtab[n].start;
5956 fillchar = tabtab[n].userhl;
5957 }
5958 while (col < Columns)
5959 TabPageIdxs[col++] = fillchar;
5960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961}
5962
5963#endif /* FEAT_STL_OPT */
5964
5965/*
5966 * Output a single character directly to the screen and update ScreenLines.
5967 */
5968 void
5969screen_putchar(c, row, col, attr)
5970 int c;
5971 int row, col;
5972 int attr;
5973{
5974#ifdef FEAT_MBYTE
5975 char_u buf[MB_MAXBYTES + 1];
5976
5977 buf[(*mb_char2bytes)(c, buf)] = NUL;
5978#else
5979 char_u buf[2];
5980
5981 buf[0] = c;
5982 buf[1] = NUL;
5983#endif
5984 screen_puts(buf, row, col, attr);
5985}
5986
5987/*
5988 * Get a single character directly from ScreenLines into "bytes[]".
5989 * Also return its attribute in *attrp;
5990 */
5991 void
5992screen_getbytes(row, col, bytes, attrp)
5993 int row, col;
5994 char_u *bytes;
5995 int *attrp;
5996{
5997 unsigned off;
5998
5999 /* safety check */
6000 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6001 {
6002 off = LineOffset[row] + col;
6003 *attrp = ScreenAttrs[off];
6004 bytes[0] = ScreenLines[off];
6005 bytes[1] = NUL;
6006
6007#ifdef FEAT_MBYTE
6008 if (enc_utf8 && ScreenLinesUC[off] != 0)
6009 bytes[utfc_char2bytes(off, bytes)] = NUL;
6010 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6011 {
6012 bytes[0] = ScreenLines[off];
6013 bytes[1] = ScreenLines2[off];
6014 bytes[2] = NUL;
6015 }
6016 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6017 {
6018 bytes[1] = ScreenLines[off + 1];
6019 bytes[2] = NUL;
6020 }
6021#endif
6022 }
6023}
6024
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006025#ifdef FEAT_MBYTE
6026static int screen_comp_differs __ARGS((int, int*));
6027
6028/*
6029 * Return TRUE if composing characters for screen posn "off" differs from
6030 * composing characters in "u8cc".
6031 */
6032 static int
6033screen_comp_differs(off, u8cc)
6034 int off;
6035 int *u8cc;
6036{
6037 int i;
6038
6039 for (i = 0; i < Screen_mco; ++i)
6040 {
6041 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6042 return TRUE;
6043 if (u8cc[i] == 0)
6044 break;
6045 }
6046 return FALSE;
6047}
6048#endif
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050/*
6051 * Put string '*text' on the screen at position 'row' and 'col', with
6052 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6053 * Note: only outputs within one row, message is truncated at screen boundary!
6054 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6055 */
6056 void
6057screen_puts(text, row, col, attr)
6058 char_u *text;
6059 int row;
6060 int col;
6061 int attr;
6062{
6063 screen_puts_len(text, -1, row, col, attr);
6064}
6065
6066/*
6067 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6068 * a NUL.
6069 */
6070 void
6071screen_puts_len(text, len, row, col, attr)
6072 char_u *text;
6073 int len;
6074 int row;
6075 int col;
6076 int attr;
6077{
6078 unsigned off;
6079 char_u *ptr = text;
6080 int c;
6081#ifdef FEAT_MBYTE
6082 int mbyte_blen = 1;
6083 int mbyte_cells = 1;
6084 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006085 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 int clear_next_cell = FALSE;
6087# ifdef FEAT_ARABIC
6088 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006089 int pc, nc, nc1;
6090 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091# endif
6092#endif
6093
6094 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6095 return;
6096
6097 off = LineOffset[row] + col;
6098 while (*ptr != NUL && col < screen_Columns
6099 && (len < 0 || (int)(ptr - text) < len))
6100 {
6101 c = *ptr;
6102#ifdef FEAT_MBYTE
6103 /* check if this is the first byte of a multibyte */
6104 if (has_mbyte)
6105 {
6106 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006107 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006109 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6111 mbyte_cells = 1;
6112 else if (enc_dbcs != 0)
6113 mbyte_cells = mbyte_blen;
6114 else /* enc_utf8 */
6115 {
6116 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006117 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 (int)((text + len) - ptr));
6119 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006120 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 mbyte_cells = utf_char2cells(u8c);
6122 /* Non-BMP character: display as ? or fullwidth ?. */
6123 if (u8c >= 0x10000)
6124 {
6125 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6126 if (attr == 0)
6127 attr = hl_attr(HLF_8);
6128 }
6129# ifdef FEAT_ARABIC
6130 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6131 {
6132 /* Do Arabic shaping. */
6133 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6134 {
6135 /* Past end of string to be displayed. */
6136 nc = NUL;
6137 nc1 = NUL;
6138 }
6139 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006140 {
6141 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6142 nc1 = pcc[0];
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 pc = prev_c;
6145 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006146 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 }
6148 else
6149 prev_c = u8c;
6150# endif
6151 }
6152 }
6153#endif
6154
6155 if (ScreenLines[off] != c
6156#ifdef FEAT_MBYTE
6157 || (mbyte_cells == 2
6158 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6159 || (enc_dbcs == DBCS_JPNU
6160 && c == 0x8e
6161 && ScreenLines2[off] != ptr[1])
6162 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006163 && (ScreenLinesUC[off] != (u8char_T)u8c
6164 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165#endif
6166 || ScreenAttrs[off] != attr
6167 || exmode_active
6168 )
6169 {
6170#if defined(FEAT_GUI) || defined(UNIX)
6171 /* The bold trick makes a single row of pixels appear in the next
6172 * character. When a bold character is removed, the next
6173 * character should be redrawn too. This happens for our own GUI
6174 * and for some xterms.
6175 * Force the redraw by setting the attribute to a different value
6176 * than "attr", the contents of ScreenLines[] may be needed by
6177 * mb_off2cells() further on.
6178 * Don't do this for the last drawn character, because the next
6179 * character may not be redrawn. */
6180 if (
6181# ifdef FEAT_GUI
6182 gui.in_use
6183# endif
6184# if defined(FEAT_GUI) && defined(UNIX)
6185 ||
6186# endif
6187# ifdef UNIX
6188 term_is_xterm
6189# endif
6190 )
6191 {
6192 int n;
6193
6194 n = ScreenAttrs[off];
6195# ifdef FEAT_MBYTE
6196 if (col + mbyte_cells < screen_Columns
6197 && (n > HL_ALL || (n & HL_BOLD))
6198 && (len < 0 ? ptr[mbyte_blen] != NUL
6199 : ptr + mbyte_blen < text + len))
6200 ScreenAttrs[off + mbyte_cells] = attr + 1;
6201# else
6202 if (col + 1 < screen_Columns
6203 && (n > HL_ALL || (n & HL_BOLD))
6204 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6205 ScreenLines[off + 1] = 0;
6206# endif
6207 }
6208#endif
6209#ifdef FEAT_MBYTE
6210 /* When at the end of the text and overwriting a two-cell
6211 * character with a one-cell character, need to clear the next
6212 * cell. Also when overwriting the left halve of a two-cell char
6213 * with the right halve of a two-cell char. Do this only once
6214 * (mb_off2cells() may return 2 on the right halve). */
6215 if (clear_next_cell)
6216 clear_next_cell = FALSE;
6217 else if (has_mbyte
6218 && (len < 0 ? ptr[mbyte_blen] == NUL
6219 : ptr + mbyte_blen >= text + len)
6220 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6221 || (mbyte_cells == 2
6222 && (*mb_off2cells)(off) == 1
6223 && (*mb_off2cells)(off + 1) > 1)))
6224 clear_next_cell = TRUE;
6225
6226 /* Make sure we never leave a second byte of a double-byte behind,
6227 * it confuses mb_off2cells(). */
6228 if (enc_dbcs
6229 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6230 || (mbyte_cells == 2
6231 && (*mb_off2cells)(off) == 1
6232 && (*mb_off2cells)(off + 1) > 1)))
6233 ScreenLines[off + mbyte_blen] = 0;
6234#endif
6235 ScreenLines[off] = c;
6236 ScreenAttrs[off] = attr;
6237#ifdef FEAT_MBYTE
6238 if (enc_utf8)
6239 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006240 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 ScreenLinesUC[off] = 0;
6242 else
6243 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006244 int i;
6245
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006247 for (i = 0; i < Screen_mco; ++i)
6248 {
6249 ScreenLinesC[i][off] = u8cc[i];
6250 if (u8cc[i] == 0)
6251 break;
6252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 }
6254 if (mbyte_cells == 2)
6255 {
6256 ScreenLines[off + 1] = 0;
6257 ScreenAttrs[off + 1] = attr;
6258 }
6259 screen_char(off, row, col);
6260 }
6261 else if (mbyte_cells == 2)
6262 {
6263 ScreenLines[off + 1] = ptr[1];
6264 ScreenAttrs[off + 1] = attr;
6265 screen_char_2(off, row, col);
6266 }
6267 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6268 {
6269 ScreenLines2[off] = ptr[1];
6270 screen_char(off, row, col);
6271 }
6272 else
6273#endif
6274 screen_char(off, row, col);
6275 }
6276#ifdef FEAT_MBYTE
6277 if (has_mbyte)
6278 {
6279 off += mbyte_cells;
6280 col += mbyte_cells;
6281 ptr += mbyte_blen;
6282 if (clear_next_cell)
6283 ptr = (char_u *)" ";
6284 }
6285 else
6286#endif
6287 {
6288 ++off;
6289 ++col;
6290 ++ptr;
6291 }
6292 }
6293}
6294
6295#ifdef FEAT_SEARCH_EXTRA
6296/*
6297 * Prepare for 'searchhl' highlighting.
6298 */
6299 static void
6300start_search_hl()
6301{
6302 if (p_hls && !no_hlsearch)
6303 {
6304 last_pat_prog(&search_hl.rm);
6305 search_hl.attr = hl_attr(HLF_L);
6306 }
6307}
6308
6309/*
6310 * Clean up for 'searchhl' highlighting.
6311 */
6312 static void
6313end_search_hl()
6314{
6315 if (search_hl.rm.regprog != NULL)
6316 {
6317 vim_free(search_hl.rm.regprog);
6318 search_hl.rm.regprog = NULL;
6319 }
6320}
6321
6322/*
6323 * Advance to the match in window "wp" line "lnum" or past it.
6324 */
6325 static void
6326prepare_search_hl(wp, lnum)
6327 win_T *wp;
6328 linenr_T lnum;
6329{
6330 match_T *shl; /* points to search_hl or match_hl */
6331 int n;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006332 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334 /*
6335 * When using a multi-line pattern, start searching at the top
6336 * of the window or just after a closed fold.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006337 * Do this both for search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006339 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006341 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (shl->rm.regprog != NULL
6343 && shl->lnum == 0
6344 && re_multiline(shl->rm.regprog))
6345 {
6346 if (shl->first_lnum == 0)
6347 {
6348# ifdef FEAT_FOLDING
6349 for (shl->first_lnum = lnum;
6350 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6351 if (hasFoldingWin(wp, shl->first_lnum - 1,
6352 NULL, NULL, TRUE, NULL))
6353 break;
6354# else
6355 shl->first_lnum = wp->w_topline;
6356# endif
6357 }
6358 n = 0;
6359 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6360 {
6361 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6362 if (shl->lnum != 0)
6363 {
6364 shl->first_lnum = shl->lnum
6365 + shl->rm.endpos[0].lnum
6366 - shl->rm.startpos[0].lnum;
6367 n = shl->rm.endpos[0].col;
6368 }
6369 else
6370 {
6371 ++shl->first_lnum;
6372 n = 0;
6373 }
6374 }
6375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 }
6377}
6378
6379/*
6380 * Search for a next 'searchl' or ":match" match.
6381 * Uses shl->buf.
6382 * Sets shl->lnum and shl->rm contents.
6383 * Note: Assumes a previous match is always before "lnum", unless
6384 * shl->lnum is zero.
6385 * Careful: Any pointers for buffer lines will become invalid.
6386 */
6387 static void
6388next_search_hl(win, shl, lnum, mincol)
6389 win_T *win;
6390 match_T *shl; /* points to search_hl or match_hl */
6391 linenr_T lnum;
6392 colnr_T mincol; /* minimal column for a match */
6393{
6394 linenr_T l;
6395 colnr_T matchcol;
6396 long nmatched;
6397
6398 if (shl->lnum != 0)
6399 {
6400 /* Check for three situations:
6401 * 1. If the "lnum" is below a previous match, start a new search.
6402 * 2. If the previous match includes "mincol", use it.
6403 * 3. Continue after the previous match.
6404 */
6405 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6406 if (lnum > l)
6407 shl->lnum = 0;
6408 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6409 return;
6410 }
6411
6412 /*
6413 * Repeat searching for a match until one is found that includes "mincol"
6414 * or none is found in this line.
6415 */
6416 called_emsg = FALSE;
6417 for (;;)
6418 {
6419 /* Three situations:
6420 * 1. No useful previous match: search from start of line.
6421 * 2. Not Vi compatible or empty match: continue at next character.
6422 * Break the loop if this is beyond the end of the line.
6423 * 3. Vi compatible searching: continue at end of previous match.
6424 */
6425 if (shl->lnum == 0)
6426 matchcol = 0;
6427 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6428 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006429 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006431 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006432
6433 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006434 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006435 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006437 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 shl->lnum = 0;
6439 break;
6440 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006441#ifdef FEAT_MBYTE
6442 if (has_mbyte)
6443 matchcol += mb_ptr2len(ml);
6444 else
6445#endif
6446 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
6448 else
6449 matchcol = shl->rm.endpos[0].col;
6450
6451 shl->lnum = lnum;
6452 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6453 if (called_emsg)
6454 {
6455 /* Error while handling regexp: stop using this regexp. */
6456 vim_free(shl->rm.regprog);
6457 shl->rm.regprog = NULL;
6458 no_hlsearch = TRUE;
6459 break;
6460 }
6461 if (nmatched == 0)
6462 {
6463 shl->lnum = 0; /* no match found */
6464 break;
6465 }
6466 if (shl->rm.startpos[0].lnum > 0
6467 || shl->rm.startpos[0].col >= mincol
6468 || nmatched > 1
6469 || shl->rm.endpos[0].col > mincol)
6470 {
6471 shl->lnum += shl->rm.startpos[0].lnum;
6472 break; /* useful match found */
6473 }
6474 }
6475}
6476#endif
6477
6478 static void
6479screen_start_highlight(attr)
6480 int attr;
6481{
6482 attrentry_T *aep = NULL;
6483
6484 screen_attr = attr;
6485 if (full_screen
6486#ifdef WIN3264
6487 && termcap_active
6488#endif
6489 )
6490 {
6491#ifdef FEAT_GUI
6492 if (gui.in_use)
6493 {
6494 char buf[20];
6495
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006496 /* The GUI handles this internally. */
6497 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 OUT_STR(buf);
6499 }
6500 else
6501#endif
6502 {
6503 if (attr > HL_ALL) /* special HL attr. */
6504 {
6505 if (t_colors > 1)
6506 aep = syn_cterm_attr2entry(attr);
6507 else
6508 aep = syn_term_attr2entry(attr);
6509 if (aep == NULL) /* did ":syntax clear" */
6510 attr = 0;
6511 else
6512 attr = aep->ae_attr;
6513 }
6514 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6515 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006516 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6517 && cterm_normal_fg_bold)
6518 /* If the Normal FG color has BOLD attribute and the new HL
6519 * has a FG color defined, clear BOLD. */
6520 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6522 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006523 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6524 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 out_str(T_US);
6526 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6527 out_str(T_CZH);
6528 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6529 out_str(T_MR);
6530
6531 /*
6532 * Output the color or start string after bold etc., in case the
6533 * bold etc. override the color setting.
6534 */
6535 if (aep != NULL)
6536 {
6537 if (t_colors > 1)
6538 {
6539 if (aep->ae_u.cterm.fg_color)
6540 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6541 if (aep->ae_u.cterm.bg_color)
6542 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6543 }
6544 else
6545 {
6546 if (aep->ae_u.term.start != NULL)
6547 out_str(aep->ae_u.term.start);
6548 }
6549 }
6550 }
6551 }
6552}
6553
6554 void
6555screen_stop_highlight()
6556{
6557 int do_ME = FALSE; /* output T_ME code */
6558
6559 if (screen_attr != 0
6560#ifdef WIN3264
6561 && termcap_active
6562#endif
6563 )
6564 {
6565#ifdef FEAT_GUI
6566 if (gui.in_use)
6567 {
6568 char buf[20];
6569
6570 /* use internal GUI code */
6571 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6572 OUT_STR(buf);
6573 }
6574 else
6575#endif
6576 {
6577 if (screen_attr > HL_ALL) /* special HL attr. */
6578 {
6579 attrentry_T *aep;
6580
6581 if (t_colors > 1)
6582 {
6583 /*
6584 * Assume that t_me restores the original colors!
6585 */
6586 aep = syn_cterm_attr2entry(screen_attr);
6587 if (aep != NULL && (aep->ae_u.cterm.fg_color
6588 || aep->ae_u.cterm.bg_color))
6589 do_ME = TRUE;
6590 }
6591 else
6592 {
6593 aep = syn_term_attr2entry(screen_attr);
6594 if (aep != NULL && aep->ae_u.term.stop != NULL)
6595 {
6596 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6597 do_ME = TRUE;
6598 else
6599 out_str(aep->ae_u.term.stop);
6600 }
6601 }
6602 if (aep == NULL) /* did ":syntax clear" */
6603 screen_attr = 0;
6604 else
6605 screen_attr = aep->ae_attr;
6606 }
6607
6608 /*
6609 * Often all ending-codes are equal to T_ME. Avoid outputting the
6610 * same sequence several times.
6611 */
6612 if (screen_attr & HL_STANDOUT)
6613 {
6614 if (STRCMP(T_SE, T_ME) == 0)
6615 do_ME = TRUE;
6616 else
6617 out_str(T_SE);
6618 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006619 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 {
6621 if (STRCMP(T_UE, T_ME) == 0)
6622 do_ME = TRUE;
6623 else
6624 out_str(T_UE);
6625 }
6626 if (screen_attr & HL_ITALIC)
6627 {
6628 if (STRCMP(T_CZR, T_ME) == 0)
6629 do_ME = TRUE;
6630 else
6631 out_str(T_CZR);
6632 }
6633 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6634 out_str(T_ME);
6635
6636 if (t_colors > 1)
6637 {
6638 /* set Normal cterm colors */
6639 if (cterm_normal_fg_color != 0)
6640 term_fg_color(cterm_normal_fg_color - 1);
6641 if (cterm_normal_bg_color != 0)
6642 term_bg_color(cterm_normal_bg_color - 1);
6643 if (cterm_normal_fg_bold)
6644 out_str(T_MD);
6645 }
6646 }
6647 }
6648 screen_attr = 0;
6649}
6650
6651/*
6652 * Reset the colors for a cterm. Used when leaving Vim.
6653 * The machine specific code may override this again.
6654 */
6655 void
6656reset_cterm_colors()
6657{
6658 if (t_colors > 1)
6659 {
6660 /* set Normal cterm colors */
6661 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6662 {
6663 out_str(T_OP);
6664 screen_attr = -1;
6665 }
6666 if (cterm_normal_fg_bold)
6667 {
6668 out_str(T_ME);
6669 screen_attr = -1;
6670 }
6671 }
6672}
6673
6674/*
6675 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6676 * using the attributes from ScreenAttrs["off"].
6677 */
6678 static void
6679screen_char(off, row, col)
6680 unsigned off;
6681 int row;
6682 int col;
6683{
6684 int attr;
6685
6686 /* Check for illegal values, just in case (could happen just after
6687 * resizing). */
6688 if (row >= screen_Rows || col >= screen_Columns)
6689 return;
6690
6691 /* Outputting the last character on the screen may scrollup the screen.
6692 * Don't to it! Mark the character invalid (update it when scrolled up) */
6693 if (row == screen_Rows - 1 && col == screen_Columns - 1
6694#ifdef FEAT_RIGHTLEFT
6695 /* account for first command-line character in rightleft mode */
6696 && !cmdmsg_rl
6697#endif
6698 )
6699 {
6700 ScreenAttrs[off] = (sattr_T)-1;
6701 return;
6702 }
6703
6704 /*
6705 * Stop highlighting first, so it's easier to move the cursor.
6706 */
6707#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6708 if (screen_char_attr != 0)
6709 attr = screen_char_attr;
6710 else
6711#endif
6712 attr = ScreenAttrs[off];
6713 if (screen_attr != attr)
6714 screen_stop_highlight();
6715
6716 windgoto(row, col);
6717
6718 if (screen_attr != attr)
6719 screen_start_highlight(attr);
6720
6721#ifdef FEAT_MBYTE
6722 if (enc_utf8 && ScreenLinesUC[off] != 0)
6723 {
6724 char_u buf[MB_MAXBYTES + 1];
6725
6726 /* Convert UTF-8 character to bytes and write it. */
6727
6728 buf[utfc_char2bytes(off, buf)] = NUL;
6729
6730 out_str(buf);
6731 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6732 ++screen_cur_col;
6733 }
6734 else
6735#endif
6736 {
6737#ifdef FEAT_MBYTE
6738 out_flush_check();
6739#endif
6740 out_char(ScreenLines[off]);
6741#ifdef FEAT_MBYTE
6742 /* double-byte character in single-width cell */
6743 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6744 out_char(ScreenLines2[off]);
6745#endif
6746 }
6747
6748 screen_cur_col++;
6749}
6750
6751#ifdef FEAT_MBYTE
6752
6753/*
6754 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6755 * on the screen at position 'row' and 'col'.
6756 * The attributes of the first byte is used for all. This is required to
6757 * output the two bytes of a double-byte character with nothing in between.
6758 */
6759 static void
6760screen_char_2(off, row, col)
6761 unsigned off;
6762 int row;
6763 int col;
6764{
6765 /* Check for illegal values (could be wrong when screen was resized). */
6766 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6767 return;
6768
6769 /* Outputting the last character on the screen may scrollup the screen.
6770 * Don't to it! Mark the character invalid (update it when scrolled up) */
6771 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6772 {
6773 ScreenAttrs[off] = (sattr_T)-1;
6774 return;
6775 }
6776
6777 /* Output the first byte normally (positions the cursor), then write the
6778 * second byte directly. */
6779 screen_char(off, row, col);
6780 out_char(ScreenLines[off + 1]);
6781 ++screen_cur_col;
6782}
6783#endif
6784
6785#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6786/*
6787 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6788 * This uses the contents of ScreenLines[] and doesn't change it.
6789 */
6790 void
6791screen_draw_rectangle(row, col, height, width, invert)
6792 int row;
6793 int col;
6794 int height;
6795 int width;
6796 int invert;
6797{
6798 int r, c;
6799 int off;
6800
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006801 /* Can't use ScreenLines unless initialized */
6802 if (ScreenLines == NULL)
6803 return;
6804
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 if (invert)
6806 screen_char_attr = HL_INVERSE;
6807 for (r = row; r < row + height; ++r)
6808 {
6809 off = LineOffset[r];
6810 for (c = col; c < col + width; ++c)
6811 {
6812#ifdef FEAT_MBYTE
6813 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6814 {
6815 screen_char_2(off + c, r, c);
6816 ++c;
6817 }
6818 else
6819#endif
6820 {
6821 screen_char(off + c, r, c);
6822#ifdef FEAT_MBYTE
6823 if (utf_off2cells(off + c) > 1)
6824 ++c;
6825#endif
6826 }
6827 }
6828 }
6829 screen_char_attr = 0;
6830}
6831#endif
6832
6833#ifdef FEAT_VERTSPLIT
6834/*
6835 * Redraw the characters for a vertically split window.
6836 */
6837 static void
6838redraw_block(row, end, wp)
6839 int row;
6840 int end;
6841 win_T *wp;
6842{
6843 int col;
6844 int width;
6845
6846# ifdef FEAT_CLIPBOARD
6847 clip_may_clear_selection(row, end - 1);
6848# endif
6849
6850 if (wp == NULL)
6851 {
6852 col = 0;
6853 width = Columns;
6854 }
6855 else
6856 {
6857 col = wp->w_wincol;
6858 width = wp->w_width;
6859 }
6860 screen_draw_rectangle(row, col, end - row, width, FALSE);
6861}
6862#endif
6863
6864/*
6865 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6866 * with character 'c1' in first column followed by 'c2' in the other columns.
6867 * Use attributes 'attr'.
6868 */
6869 void
6870screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6871 int start_row, end_row;
6872 int start_col, end_col;
6873 int c1, c2;
6874 int attr;
6875{
6876 int row;
6877 int col;
6878 int off;
6879 int end_off;
6880 int did_delete;
6881 int c;
6882 int norm_term;
6883#if defined(FEAT_GUI) || defined(UNIX)
6884 int force_next = FALSE;
6885#endif
6886
6887 if (end_row > screen_Rows) /* safety check */
6888 end_row = screen_Rows;
6889 if (end_col > screen_Columns) /* safety check */
6890 end_col = screen_Columns;
6891 if (ScreenLines == NULL
6892 || start_row >= end_row
6893 || start_col >= end_col) /* nothing to do */
6894 return;
6895
6896 /* it's a "normal" terminal when not in a GUI or cterm */
6897 norm_term = (
6898#ifdef FEAT_GUI
6899 !gui.in_use &&
6900#endif
6901 t_colors <= 1);
6902 for (row = start_row; row < end_row; ++row)
6903 {
6904 /*
6905 * Try to use delete-line termcap code, when no attributes or in a
6906 * "normal" terminal, where a bold/italic space is just a
6907 * space.
6908 */
6909 did_delete = FALSE;
6910 if (c2 == ' '
6911 && end_col == Columns
6912 && can_clear(T_CE)
6913 && (attr == 0
6914 || (norm_term
6915 && attr <= HL_ALL
6916 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6917 {
6918 /*
6919 * check if we really need to clear something
6920 */
6921 col = start_col;
6922 if (c1 != ' ') /* don't clear first char */
6923 ++col;
6924
6925 off = LineOffset[row] + col;
6926 end_off = LineOffset[row] + end_col;
6927
6928 /* skip blanks (used often, keep it fast!) */
6929#ifdef FEAT_MBYTE
6930 if (enc_utf8)
6931 while (off < end_off && ScreenLines[off] == ' '
6932 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6933 ++off;
6934 else
6935#endif
6936 while (off < end_off && ScreenLines[off] == ' '
6937 && ScreenAttrs[off] == 0)
6938 ++off;
6939 if (off < end_off) /* something to be cleared */
6940 {
6941 col = off - LineOffset[row];
6942 screen_stop_highlight();
6943 term_windgoto(row, col);/* clear rest of this screen line */
6944 out_str(T_CE);
6945 screen_start(); /* don't know where cursor is now */
6946 col = end_col - col;
6947 while (col--) /* clear chars in ScreenLines */
6948 {
6949 ScreenLines[off] = ' ';
6950#ifdef FEAT_MBYTE
6951 if (enc_utf8)
6952 ScreenLinesUC[off] = 0;
6953#endif
6954 ScreenAttrs[off] = 0;
6955 ++off;
6956 }
6957 }
6958 did_delete = TRUE; /* the chars are cleared now */
6959 }
6960
6961 off = LineOffset[row] + start_col;
6962 c = c1;
6963 for (col = start_col; col < end_col; ++col)
6964 {
6965 if (ScreenLines[off] != c
6966#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006967 || (enc_utf8 && (int)ScreenLinesUC[off]
6968 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#endif
6970 || ScreenAttrs[off] != attr
6971#if defined(FEAT_GUI) || defined(UNIX)
6972 || force_next
6973#endif
6974 )
6975 {
6976#if defined(FEAT_GUI) || defined(UNIX)
6977 /* The bold trick may make a single row of pixels appear in
6978 * the next character. When a bold character is removed, the
6979 * next character should be redrawn too. This happens for our
6980 * own GUI and for some xterms. */
6981 if (
6982# ifdef FEAT_GUI
6983 gui.in_use
6984# endif
6985# if defined(FEAT_GUI) && defined(UNIX)
6986 ||
6987# endif
6988# ifdef UNIX
6989 term_is_xterm
6990# endif
6991 )
6992 {
6993 if (ScreenLines[off] != ' '
6994 && (ScreenAttrs[off] > HL_ALL
6995 || ScreenAttrs[off] & HL_BOLD))
6996 force_next = TRUE;
6997 else
6998 force_next = FALSE;
6999 }
7000#endif
7001 ScreenLines[off] = c;
7002#ifdef FEAT_MBYTE
7003 if (enc_utf8)
7004 {
7005 if (c >= 0x80)
7006 {
7007 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007008 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 }
7010 else
7011 ScreenLinesUC[off] = 0;
7012 }
7013#endif
7014 ScreenAttrs[off] = attr;
7015 if (!did_delete || c != ' ')
7016 screen_char(off, row, col);
7017 }
7018 ++off;
7019 if (col == start_col)
7020 {
7021 if (did_delete)
7022 break;
7023 c = c2;
7024 }
7025 }
7026 if (end_col == Columns)
7027 LineWraps[row] = FALSE;
7028 if (row == Rows - 1) /* overwritten the command line */
7029 {
7030 redraw_cmdline = TRUE;
7031 if (c1 == ' ' && c2 == ' ')
7032 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007033 if (start_col == 0)
7034 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 }
7036 }
7037}
7038
7039/*
7040 * Check if there should be a delay. Used before clearing or redrawing the
7041 * screen or the command line.
7042 */
7043 void
7044check_for_delay(check_msg_scroll)
7045 int check_msg_scroll;
7046{
7047 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7048 && !did_wait_return
7049 && emsg_silent == 0)
7050 {
7051 out_flush();
7052 ui_delay(1000L, TRUE);
7053 emsg_on_display = FALSE;
7054 if (check_msg_scroll)
7055 msg_scroll = FALSE;
7056 }
7057}
7058
7059/*
7060 * screen_valid - allocate screen buffers if size changed
7061 * If "clear" is TRUE: clear screen if it has been resized.
7062 * Returns TRUE if there is a valid screen to write to.
7063 * Returns FALSE when starting up and screen not initialized yet.
7064 */
7065 int
7066screen_valid(clear)
7067 int clear;
7068{
7069 screenalloc(clear); /* allocate screen buffers if size changed */
7070 return (ScreenLines != NULL);
7071}
7072
7073/*
7074 * Resize the shell to Rows and Columns.
7075 * Allocate ScreenLines[] and associated items.
7076 *
7077 * There may be some time between setting Rows and Columns and (re)allocating
7078 * ScreenLines[]. This happens when starting up and when (manually) changing
7079 * the shell size. Always use screen_Rows and screen_Columns to access items
7080 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7081 * final size of the shell is needed.
7082 */
7083 void
7084screenalloc(clear)
7085 int clear;
7086{
7087 int new_row, old_row;
7088#ifdef FEAT_GUI
7089 int old_Rows;
7090#endif
7091 win_T *wp;
7092 int outofmem = FALSE;
7093 int len;
7094 schar_T *new_ScreenLines;
7095#ifdef FEAT_MBYTE
7096 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007097 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007099 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100#endif
7101 sattr_T *new_ScreenAttrs;
7102 unsigned *new_LineOffset;
7103 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007104#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007105 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007106 tabpage_T *tp;
7107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007109 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
7111 /*
7112 * Allocation of the screen buffers is done only when the size changes and
7113 * when Rows and Columns have been set and we have started doing full
7114 * screen stuff.
7115 */
7116 if ((ScreenLines != NULL
7117 && Rows == screen_Rows
7118 && Columns == screen_Columns
7119#ifdef FEAT_MBYTE
7120 && enc_utf8 == (ScreenLinesUC != NULL)
7121 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007122 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123#endif
7124 )
7125 || Rows == 0
7126 || Columns == 0
7127 || (!full_screen && ScreenLines == NULL))
7128 return;
7129
7130 /*
7131 * It's possible that we produce an out-of-memory message below, which
7132 * will cause this function to be called again. To break the loop, just
7133 * return here.
7134 */
7135 if (entered)
7136 return;
7137 entered = TRUE;
7138
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007139 /*
7140 * Note that the window sizes are updated before reallocating the arrays,
7141 * thus we must not redraw here!
7142 */
7143 ++RedrawingDisabled;
7144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 win_new_shellsize(); /* fit the windows in the new sized shell */
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 comp_col(); /* recompute columns for shown command and ruler */
7148
7149 /*
7150 * We're changing the size of the screen.
7151 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7152 * - Move lines from the old arrays into the new arrays, clear extra
7153 * lines (unless the screen is going to be cleared).
7154 * - Free the old arrays.
7155 *
7156 * If anything fails, make ScreenLines NULL, so we don't do anything!
7157 * Continuing with the old ScreenLines may result in a crash, because the
7158 * size is wrong.
7159 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007160 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
7163 new_ScreenLines = (schar_T *)lalloc((long_u)(
7164 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7165#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007166 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 if (enc_utf8)
7168 {
7169 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7170 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007171 for (i = 0; i < p_mco; ++i)
7172 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7174 }
7175 if (enc_dbcs == DBCS_JPNU)
7176 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7177 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7178#endif
7179 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7180 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7181 new_LineOffset = (unsigned *)lalloc((long_u)(
7182 Rows * sizeof(unsigned)), FALSE);
7183 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007184#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007185 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007188 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 {
7190 if (win_alloc_lines(wp) == FAIL)
7191 {
7192 outofmem = TRUE;
7193#ifdef FEAT_WINDOWS
7194 break;
7195#endif
7196 }
7197 }
7198
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007199#ifdef FEAT_MBYTE
7200 for (i = 0; i < p_mco; ++i)
7201 if (new_ScreenLinesC[i] == NULL)
7202 break;
7203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 if (new_ScreenLines == NULL
7205#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007206 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7208#endif
7209 || new_ScreenAttrs == NULL
7210 || new_LineOffset == NULL
7211 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007212#ifdef FEAT_WINDOWS
7213 || new_TabPageIdxs == NULL
7214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 || outofmem)
7216 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007217 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007218 {
7219 /* guess the size */
7220 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7221
7222 /* Remember we did this to avoid getting outofmem messages over
7223 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00007224 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 vim_free(new_ScreenLines);
7227 new_ScreenLines = NULL;
7228#ifdef FEAT_MBYTE
7229 vim_free(new_ScreenLinesUC);
7230 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007231 for (i = 0; i < p_mco; ++i)
7232 {
7233 vim_free(new_ScreenLinesC[i]);
7234 new_ScreenLinesC[i] = NULL;
7235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 vim_free(new_ScreenLines2);
7237 new_ScreenLines2 = NULL;
7238#endif
7239 vim_free(new_ScreenAttrs);
7240 new_ScreenAttrs = NULL;
7241 vim_free(new_LineOffset);
7242 new_LineOffset = NULL;
7243 vim_free(new_LineWraps);
7244 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007245#ifdef FEAT_WINDOWS
7246 vim_free(new_TabPageIdxs);
7247 new_TabPageIdxs = NULL;
7248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 }
7250 else
7251 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00007252 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007253
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 for (new_row = 0; new_row < Rows; ++new_row)
7255 {
7256 new_LineOffset[new_row] = new_row * Columns;
7257 new_LineWraps[new_row] = FALSE;
7258
7259 /*
7260 * If the screen is not going to be cleared, copy as much as
7261 * possible from the old screen to the new one and clear the rest
7262 * (used when resizing the window at the "--more--" prompt or when
7263 * executing an external command, for the GUI).
7264 */
7265 if (!clear)
7266 {
7267 (void)vim_memset(new_ScreenLines + new_row * Columns,
7268 ' ', (size_t)Columns * sizeof(schar_T));
7269#ifdef FEAT_MBYTE
7270 if (enc_utf8)
7271 {
7272 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7273 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007274 for (i = 0; i < p_mco; ++i)
7275 (void)vim_memset(new_ScreenLinesC[i]
7276 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 0, (size_t)Columns * sizeof(u8char_T));
7278 }
7279 if (enc_dbcs == DBCS_JPNU)
7280 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7281 0, (size_t)Columns * sizeof(schar_T));
7282#endif
7283 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7284 0, (size_t)Columns * sizeof(sattr_T));
7285 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007286 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 {
7288 if (screen_Columns < Columns)
7289 len = screen_Columns;
7290 else
7291 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007292#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007293 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007294 * may be invalid now. Also when p_mco changes. */
7295 if (!(enc_utf8 && ScreenLinesUC == NULL)
7296 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007297#endif
7298 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7299 ScreenLines + LineOffset[old_row],
7300 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007302 if (enc_utf8 && ScreenLinesUC != NULL
7303 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 {
7305 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7306 ScreenLinesUC + LineOffset[old_row],
7307 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007308 for (i = 0; i < p_mco; ++i)
7309 mch_memmove(new_ScreenLinesC[i]
7310 + new_LineOffset[new_row],
7311 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 (size_t)len * sizeof(u8char_T));
7313 }
7314 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7315 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7316 ScreenLines2 + LineOffset[old_row],
7317 (size_t)len * sizeof(schar_T));
7318#endif
7319 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7320 ScreenAttrs + LineOffset[old_row],
7321 (size_t)len * sizeof(sattr_T));
7322 }
7323 }
7324 }
7325 /* Use the last line of the screen for the current line. */
7326 current_ScreenLine = new_ScreenLines + Rows * Columns;
7327 }
7328
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007329 free_screenlines();
7330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 ScreenLines = new_ScreenLines;
7332#ifdef FEAT_MBYTE
7333 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007334 for (i = 0; i < p_mco; ++i)
7335 ScreenLinesC[i] = new_ScreenLinesC[i];
7336 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 ScreenLines2 = new_ScreenLines2;
7338#endif
7339 ScreenAttrs = new_ScreenAttrs;
7340 LineOffset = new_LineOffset;
7341 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007342#ifdef FEAT_WINDOWS
7343 TabPageIdxs = new_TabPageIdxs;
7344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
7346 /* It's important that screen_Rows and screen_Columns reflect the actual
7347 * size of ScreenLines[]. Set them before calling anything. */
7348#ifdef FEAT_GUI
7349 old_Rows = screen_Rows;
7350#endif
7351 screen_Rows = Rows;
7352 screen_Columns = Columns;
7353
7354 must_redraw = CLEAR; /* need to clear the screen later */
7355 if (clear)
7356 screenclear2();
7357
7358#ifdef FEAT_GUI
7359 else if (gui.in_use
7360 && !gui.starting
7361 && ScreenLines != NULL
7362 && old_Rows != Rows)
7363 {
7364 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7365 /*
7366 * Adjust the position of the cursor, for when executing an external
7367 * command.
7368 */
7369 if (msg_row >= Rows) /* Rows got smaller */
7370 msg_row = Rows - 1; /* put cursor at last row */
7371 else if (Rows > old_Rows) /* Rows got bigger */
7372 msg_row += Rows - old_Rows; /* put cursor in same place */
7373 if (msg_col >= Columns) /* Columns got smaller */
7374 msg_col = Columns - 1; /* put cursor at last column */
7375 }
7376#endif
7377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00007379 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007380
7381#ifdef FEAT_AUTOCMD
7382 if (starting == 0)
7383 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385}
7386
7387 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007388free_screenlines()
7389{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007390#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007391 int i;
7392
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007393 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007394 for (i = 0; i < Screen_mco; ++i)
7395 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007396 vim_free(ScreenLines2);
7397#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007398 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007399 vim_free(ScreenAttrs);
7400 vim_free(LineOffset);
7401 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007402#ifdef FEAT_WINDOWS
7403 vim_free(TabPageIdxs);
7404#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007405}
7406
7407 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408screenclear()
7409{
7410 check_for_delay(FALSE);
7411 screenalloc(FALSE); /* allocate screen buffers if size changed */
7412 screenclear2(); /* clear the screen */
7413}
7414
7415 static void
7416screenclear2()
7417{
7418 int i;
7419
7420 if (starting == NO_SCREEN || ScreenLines == NULL
7421#ifdef FEAT_GUI
7422 || (gui.in_use && gui.starting)
7423#endif
7424 )
7425 return;
7426
7427#ifdef FEAT_GUI
7428 if (!gui.in_use)
7429#endif
7430 screen_attr = -1; /* force setting the Normal colors */
7431 screen_stop_highlight(); /* don't want highlighting here */
7432
7433#ifdef FEAT_CLIPBOARD
7434 /* disable selection without redrawing it */
7435 clip_scroll_selection(9999);
7436#endif
7437
7438 /* blank out ScreenLines */
7439 for (i = 0; i < Rows; ++i)
7440 {
7441 lineclear(LineOffset[i], (int)Columns);
7442 LineWraps[i] = FALSE;
7443 }
7444
7445 if (can_clear(T_CL))
7446 {
7447 out_str(T_CL); /* clear the display */
7448 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007449 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451 else
7452 {
7453 /* can't clear the screen, mark all chars with invalid attributes */
7454 for (i = 0; i < Rows; ++i)
7455 lineinvalid(LineOffset[i], (int)Columns);
7456 clear_cmdline = TRUE;
7457 }
7458
7459 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7460
7461 win_rest_invalid(firstwin);
7462 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007463#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007464 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 if (must_redraw == CLEAR) /* no need to clear again */
7467 must_redraw = NOT_VALID;
7468 compute_cmdrow();
7469 msg_row = cmdline_row; /* put cursor on last line for messages */
7470 msg_col = 0;
7471 screen_start(); /* don't know where cursor is now */
7472 msg_scrolled = 0; /* can't scroll back */
7473 msg_didany = FALSE;
7474 msg_didout = FALSE;
7475}
7476
7477/*
7478 * Clear one line in ScreenLines.
7479 */
7480 static void
7481lineclear(off, width)
7482 unsigned off;
7483 int width;
7484{
7485 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7486#ifdef FEAT_MBYTE
7487 if (enc_utf8)
7488 (void)vim_memset(ScreenLinesUC + off, 0,
7489 (size_t)width * sizeof(u8char_T));
7490#endif
7491 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7492}
7493
7494/*
7495 * Mark one line in ScreenLines invalid by setting the attributes to an
7496 * invalid value.
7497 */
7498 static void
7499lineinvalid(off, width)
7500 unsigned off;
7501 int width;
7502{
7503 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7504}
7505
7506#ifdef FEAT_VERTSPLIT
7507/*
7508 * Copy part of a Screenline for vertically split window "wp".
7509 */
7510 static void
7511linecopy(to, from, wp)
7512 int to;
7513 int from;
7514 win_T *wp;
7515{
7516 unsigned off_to = LineOffset[to] + wp->w_wincol;
7517 unsigned off_from = LineOffset[from] + wp->w_wincol;
7518
7519 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7520 wp->w_width * sizeof(schar_T));
7521# ifdef FEAT_MBYTE
7522 if (enc_utf8)
7523 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007524 int i;
7525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7527 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007528 for (i = 0; i < p_mco; ++i)
7529 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7530 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 }
7532 if (enc_dbcs == DBCS_JPNU)
7533 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7534 wp->w_width * sizeof(schar_T));
7535# endif
7536 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7537 wp->w_width * sizeof(sattr_T));
7538}
7539#endif
7540
7541/*
7542 * Return TRUE if clearing with term string "p" would work.
7543 * It can't work when the string is empty or it won't set the right background.
7544 */
7545 int
7546can_clear(p)
7547 char_u *p;
7548{
7549 return (*p != NUL && (t_colors <= 1
7550#ifdef FEAT_GUI
7551 || gui.in_use
7552#endif
7553 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7554}
7555
7556/*
7557 * Reset cursor position. Use whenever cursor was moved because of outputting
7558 * something directly to the screen (shell commands) or a terminal control
7559 * code.
7560 */
7561 void
7562screen_start()
7563{
7564 screen_cur_row = screen_cur_col = 9999;
7565}
7566
7567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 * Move the cursor to position "row","col" in the screen.
7569 * This tries to find the most efficient way to move, minimizing the number of
7570 * characters sent to the terminal.
7571 */
7572 void
7573windgoto(row, col)
7574 int row;
7575 int col;
7576{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007577 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 int i;
7579 int plan;
7580 int cost;
7581 int wouldbe_col;
7582 int noinvcurs;
7583 char_u *bs;
7584 int goto_cost;
7585 int attr;
7586
7587#define GOTO_COST 7 /* asssume a term_windgoto() takes about 7 chars */
7588#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7589
7590#define PLAN_LE 1
7591#define PLAN_CR 2
7592#define PLAN_NL 3
7593#define PLAN_WRITE 4
7594 /* Can't use ScreenLines unless initialized */
7595 if (ScreenLines == NULL)
7596 return;
7597
7598 if (col != screen_cur_col || row != screen_cur_row)
7599 {
7600 /* Check for valid position. */
7601 if (row < 0) /* window without text lines? */
7602 row = 0;
7603 if (row >= screen_Rows)
7604 row = screen_Rows - 1;
7605 if (col >= screen_Columns)
7606 col = screen_Columns - 1;
7607
7608 /* check if no cursor movement is allowed in highlight mode */
7609 if (screen_attr && *T_MS == NUL)
7610 noinvcurs = HIGHL_COST;
7611 else
7612 noinvcurs = 0;
7613 goto_cost = GOTO_COST + noinvcurs;
7614
7615 /*
7616 * Plan how to do the positioning:
7617 * 1. Use CR to move it to column 0, same row.
7618 * 2. Use T_LE to move it a few columns to the left.
7619 * 3. Use NL to move a few lines down, column 0.
7620 * 4. Move a few columns to the right with T_ND or by writing chars.
7621 *
7622 * Don't do this if the cursor went beyond the last column, the cursor
7623 * position is unknown then (some terminals wrap, some don't )
7624 *
7625 * First check if the highlighting attibutes allow us to write
7626 * characters to move the cursor to the right.
7627 */
7628 if (row >= screen_cur_row && screen_cur_col < Columns)
7629 {
7630 /*
7631 * If the cursor is in the same row, bigger col, we can use CR
7632 * or T_LE.
7633 */
7634 bs = NULL; /* init for GCC */
7635 attr = screen_attr;
7636 if (row == screen_cur_row && col < screen_cur_col)
7637 {
7638 /* "le" is preferred over "bc", because "bc" is obsolete */
7639 if (*T_LE)
7640 bs = T_LE; /* "cursor left" */
7641 else
7642 bs = T_BC; /* "backspace character (old) */
7643 if (*bs)
7644 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7645 else
7646 cost = 999;
7647 if (col + 1 < cost) /* using CR is less characters */
7648 {
7649 plan = PLAN_CR;
7650 wouldbe_col = 0;
7651 cost = 1; /* CR is just one character */
7652 }
7653 else
7654 {
7655 plan = PLAN_LE;
7656 wouldbe_col = col;
7657 }
7658 if (noinvcurs) /* will stop highlighting */
7659 {
7660 cost += noinvcurs;
7661 attr = 0;
7662 }
7663 }
7664
7665 /*
7666 * If the cursor is above where we want to be, we can use CR LF.
7667 */
7668 else if (row > screen_cur_row)
7669 {
7670 plan = PLAN_NL;
7671 wouldbe_col = 0;
7672 cost = (row - screen_cur_row) * 2; /* CR LF */
7673 if (noinvcurs) /* will stop highlighting */
7674 {
7675 cost += noinvcurs;
7676 attr = 0;
7677 }
7678 }
7679
7680 /*
7681 * If the cursor is in the same row, smaller col, just use write.
7682 */
7683 else
7684 {
7685 plan = PLAN_WRITE;
7686 wouldbe_col = screen_cur_col;
7687 cost = 0;
7688 }
7689
7690 /*
7691 * Check if any characters that need to be written have the
7692 * correct attributes. Also avoid UTF-8 characters.
7693 */
7694 i = col - wouldbe_col;
7695 if (i > 0)
7696 cost += i;
7697 if (cost < goto_cost && i > 0)
7698 {
7699 /*
7700 * Check if the attributes are correct without additionally
7701 * stopping highlighting.
7702 */
7703 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7704 while (i && *p++ == attr)
7705 --i;
7706 if (i != 0)
7707 {
7708 /*
7709 * Try if it works when highlighting is stopped here.
7710 */
7711 if (*--p == 0)
7712 {
7713 cost += noinvcurs;
7714 while (i && *p++ == 0)
7715 --i;
7716 }
7717 if (i != 0)
7718 cost = 999; /* different attributes, don't do it */
7719 }
7720#ifdef FEAT_MBYTE
7721 if (enc_utf8)
7722 {
7723 /* Don't use an UTF-8 char for positioning, it's slow. */
7724 for (i = wouldbe_col; i < col; ++i)
7725 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7726 {
7727 cost = 999;
7728 break;
7729 }
7730 }
7731#endif
7732 }
7733
7734 /*
7735 * We can do it without term_windgoto()!
7736 */
7737 if (cost < goto_cost)
7738 {
7739 if (plan == PLAN_LE)
7740 {
7741 if (noinvcurs)
7742 screen_stop_highlight();
7743 while (screen_cur_col > col)
7744 {
7745 out_str(bs);
7746 --screen_cur_col;
7747 }
7748 }
7749 else if (plan == PLAN_CR)
7750 {
7751 if (noinvcurs)
7752 screen_stop_highlight();
7753 out_char('\r');
7754 screen_cur_col = 0;
7755 }
7756 else if (plan == PLAN_NL)
7757 {
7758 if (noinvcurs)
7759 screen_stop_highlight();
7760 while (screen_cur_row < row)
7761 {
7762 out_char('\n');
7763 ++screen_cur_row;
7764 }
7765 screen_cur_col = 0;
7766 }
7767
7768 i = col - screen_cur_col;
7769 if (i > 0)
7770 {
7771 /*
7772 * Use cursor-right if it's one character only. Avoids
7773 * removing a line of pixels from the last bold char, when
7774 * using the bold trick in the GUI.
7775 */
7776 if (T_ND[0] != NUL && T_ND[1] == NUL)
7777 {
7778 while (i-- > 0)
7779 out_char(*T_ND);
7780 }
7781 else
7782 {
7783 int off;
7784
7785 off = LineOffset[row] + screen_cur_col;
7786 while (i-- > 0)
7787 {
7788 if (ScreenAttrs[off] != screen_attr)
7789 screen_stop_highlight();
7790#ifdef FEAT_MBYTE
7791 out_flush_check();
7792#endif
7793 out_char(ScreenLines[off]);
7794#ifdef FEAT_MBYTE
7795 if (enc_dbcs == DBCS_JPNU
7796 && ScreenLines[off] == 0x8e)
7797 out_char(ScreenLines2[off]);
7798#endif
7799 ++off;
7800 }
7801 }
7802 }
7803 }
7804 }
7805 else
7806 cost = 999;
7807
7808 if (cost >= goto_cost)
7809 {
7810 if (noinvcurs)
7811 screen_stop_highlight();
7812 if (row == screen_cur_row && (col > screen_cur_col) &&
7813 *T_CRI != NUL)
7814 term_cursor_right(col - screen_cur_col);
7815 else
7816 term_windgoto(row, col);
7817 }
7818 screen_cur_row = row;
7819 screen_cur_col = col;
7820 }
7821}
7822
7823/*
7824 * Set cursor to its position in the current window.
7825 */
7826 void
7827setcursor()
7828{
7829 if (redrawing())
7830 {
7831 validate_cursor();
7832 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7833 W_WINCOL(curwin) + (
7834#ifdef FEAT_RIGHTLEFT
7835 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7836# ifdef FEAT_MBYTE
7837 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7838# endif
7839 1)) :
7840#endif
7841 curwin->w_wcol));
7842 }
7843}
7844
7845
7846/*
7847 * insert 'line_count' lines at 'row' in window 'wp'
7848 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7849 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7850 * scrolling.
7851 * Returns FAIL if the lines are not inserted, OK for success.
7852 */
7853 int
7854win_ins_lines(wp, row, line_count, invalid, mayclear)
7855 win_T *wp;
7856 int row;
7857 int line_count;
7858 int invalid;
7859 int mayclear;
7860{
7861 int did_delete;
7862 int nextrow;
7863 int lastrow;
7864 int retval;
7865
7866 if (invalid)
7867 wp->w_lines_valid = 0;
7868
7869 if (wp->w_height < 5)
7870 return FAIL;
7871
7872 if (line_count > wp->w_height - row)
7873 line_count = wp->w_height - row;
7874
7875 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7876 if (retval != MAYBE)
7877 return retval;
7878
7879 /*
7880 * If there is a next window or a status line, we first try to delete the
7881 * lines at the bottom to avoid messing what is after the window.
7882 * If this fails and there are following windows, don't do anything to avoid
7883 * messing up those windows, better just redraw.
7884 */
7885 did_delete = FALSE;
7886#ifdef FEAT_WINDOWS
7887 if (wp->w_next != NULL || wp->w_status_height)
7888 {
7889 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7890 line_count, (int)Rows, FALSE, NULL) == OK)
7891 did_delete = TRUE;
7892 else if (wp->w_next)
7893 return FAIL;
7894 }
7895#endif
7896 /*
7897 * if no lines deleted, blank the lines that will end up below the window
7898 */
7899 if (!did_delete)
7900 {
7901#ifdef FEAT_WINDOWS
7902 wp->w_redr_status = TRUE;
7903#endif
7904 redraw_cmdline = TRUE;
7905 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7906 lastrow = nextrow + line_count;
7907 if (lastrow > Rows)
7908 lastrow = Rows;
7909 screen_fill(nextrow - line_count, lastrow - line_count,
7910 W_WINCOL(wp), (int)W_ENDCOL(wp),
7911 ' ', ' ', 0);
7912 }
7913
7914 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7915 == FAIL)
7916 {
7917 /* deletion will have messed up other windows */
7918 if (did_delete)
7919 {
7920#ifdef FEAT_WINDOWS
7921 wp->w_redr_status = TRUE;
7922#endif
7923 win_rest_invalid(W_NEXT(wp));
7924 }
7925 return FAIL;
7926 }
7927
7928 return OK;
7929}
7930
7931/*
7932 * delete "line_count" window lines at "row" in window "wp"
7933 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7934 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7935 * scrolling
7936 * Return OK for success, FAIL if the lines are not deleted.
7937 */
7938 int
7939win_del_lines(wp, row, line_count, invalid, mayclear)
7940 win_T *wp;
7941 int row;
7942 int line_count;
7943 int invalid;
7944 int mayclear;
7945{
7946 int retval;
7947
7948 if (invalid)
7949 wp->w_lines_valid = 0;
7950
7951 if (line_count > wp->w_height - row)
7952 line_count = wp->w_height - row;
7953
7954 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7955 if (retval != MAYBE)
7956 return retval;
7957
7958 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7959 (int)Rows, FALSE, NULL) == FAIL)
7960 return FAIL;
7961
7962#ifdef FEAT_WINDOWS
7963 /*
7964 * If there are windows or status lines below, try to put them at the
7965 * correct place. If we can't do that, they have to be redrawn.
7966 */
7967 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
7968 {
7969 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7970 line_count, (int)Rows, NULL) == FAIL)
7971 {
7972 wp->w_redr_status = TRUE;
7973 win_rest_invalid(wp->w_next);
7974 }
7975 }
7976 /*
7977 * If this is the last window and there is no status line, redraw the
7978 * command line later.
7979 */
7980 else
7981#endif
7982 redraw_cmdline = TRUE;
7983 return OK;
7984}
7985
7986/*
7987 * Common code for win_ins_lines() and win_del_lines().
7988 * Returns OK or FAIL when the work has been done.
7989 * Returns MAYBE when not finished yet.
7990 */
7991 static int
7992win_do_lines(wp, row, line_count, mayclear, del)
7993 win_T *wp;
7994 int row;
7995 int line_count;
7996 int mayclear;
7997 int del;
7998{
7999 int retval;
8000
8001 if (!redrawing() || line_count <= 0)
8002 return FAIL;
8003
8004 /* only a few lines left: redraw is faster */
8005 if (mayclear && Rows - line_count < 5
8006#ifdef FEAT_VERTSPLIT
8007 && wp->w_width == Columns
8008#endif
8009 )
8010 {
8011 screenclear(); /* will set wp->w_lines_valid to 0 */
8012 return FAIL;
8013 }
8014
8015 /*
8016 * Delete all remaining lines
8017 */
8018 if (row + line_count >= wp->w_height)
8019 {
8020 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
8021 W_WINCOL(wp), (int)W_ENDCOL(wp),
8022 ' ', ' ', 0);
8023 return OK;
8024 }
8025
8026 /*
8027 * when scrolling, the message on the command line should be cleared,
8028 * otherwise it will stay there forever.
8029 */
8030 clear_cmdline = TRUE;
8031
8032 /*
8033 * If the terminal can set a scroll region, use that.
8034 * Always do this in a vertically split window. This will redraw from
8035 * ScreenLines[] when t_CV isn't defined. That's faster than using
8036 * win_line().
8037 * Don't use a scroll region when we are going to redraw the text, writing
8038 * a character in the lower right corner of the scroll region causes a
8039 * scroll-up in the DJGPP version.
8040 */
8041 if (scroll_region
8042#ifdef FEAT_VERTSPLIT
8043 || W_WIDTH(wp) != Columns
8044#endif
8045 )
8046 {
8047#ifdef FEAT_VERTSPLIT
8048 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8049#endif
8050 scroll_region_set(wp, row);
8051 if (del)
8052 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8053 wp->w_height - row, FALSE, wp);
8054 else
8055 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8056 wp->w_height - row, wp);
8057#ifdef FEAT_VERTSPLIT
8058 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8059#endif
8060 scroll_region_reset();
8061 return retval;
8062 }
8063
8064#ifdef FEAT_WINDOWS
8065 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8066 return FAIL;
8067#endif
8068
8069 return MAYBE;
8070}
8071
8072/*
8073 * window 'wp' and everything after it is messed up, mark it for redraw
8074 */
8075 static void
8076win_rest_invalid(wp)
8077 win_T *wp;
8078{
8079#ifdef FEAT_WINDOWS
8080 while (wp != NULL)
8081#else
8082 if (wp != NULL)
8083#endif
8084 {
8085 redraw_win_later(wp, NOT_VALID);
8086#ifdef FEAT_WINDOWS
8087 wp->w_redr_status = TRUE;
8088 wp = wp->w_next;
8089#endif
8090 }
8091 redraw_cmdline = TRUE;
8092}
8093
8094/*
8095 * The rest of the routines in this file perform screen manipulations. The
8096 * given operation is performed physically on the screen. The corresponding
8097 * change is also made to the internal screen image. In this way, the editor
8098 * anticipates the effect of editing changes on the appearance of the screen.
8099 * That way, when we call screenupdate a complete redraw isn't usually
8100 * necessary. Another advantage is that we can keep adding code to anticipate
8101 * screen changes, and in the meantime, everything still works.
8102 */
8103
8104/*
8105 * types for inserting or deleting lines
8106 */
8107#define USE_T_CAL 1
8108#define USE_T_CDL 2
8109#define USE_T_AL 3
8110#define USE_T_CE 4
8111#define USE_T_DL 5
8112#define USE_T_SR 6
8113#define USE_NL 7
8114#define USE_T_CD 8
8115#define USE_REDRAW 9
8116
8117/*
8118 * insert lines on the screen and update ScreenLines[]
8119 * 'end' is the line after the scrolled part. Normally it is Rows.
8120 * When scrolling region used 'off' is the offset from the top for the region.
8121 * 'row' and 'end' are relative to the start of the region.
8122 *
8123 * return FAIL for failure, OK for success.
8124 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008125 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126screen_ins_lines(off, row, line_count, end, wp)
8127 int off;
8128 int row;
8129 int line_count;
8130 int end;
8131 win_T *wp; /* NULL or window to use width from */
8132{
8133 int i;
8134 int j;
8135 unsigned temp;
8136 int cursor_row;
8137 int type;
8138 int result_empty;
8139 int can_ce = can_clear(T_CE);
8140
8141 /*
8142 * FAIL if
8143 * - there is no valid screen
8144 * - the screen has to be redrawn completely
8145 * - the line count is less than one
8146 * - the line count is more than 'ttyscroll'
8147 */
8148 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8149 return FAIL;
8150
8151 /*
8152 * There are seven ways to insert lines:
8153 * 0. When in a vertically split window and t_CV isn't set, redraw the
8154 * characters from ScreenLines[].
8155 * 1. Use T_CD (clear to end of display) if it exists and the result of
8156 * the insert is just empty lines
8157 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8158 * present or line_count > 1. It looks better if we do all the inserts
8159 * at once.
8160 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8161 * insert is just empty lines and T_CE is not present or line_count >
8162 * 1.
8163 * 4. Use T_AL (insert line) if it exists.
8164 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8165 * just empty lines.
8166 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8167 * just empty lines.
8168 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8169 * the 'da' flag is not set or we have clear line capability.
8170 * 8. redraw the characters from ScreenLines[].
8171 *
8172 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8173 * the scrollbar for the window. It does have insert line, use that if it
8174 * exists.
8175 */
8176 result_empty = (row + line_count >= end);
8177#ifdef FEAT_VERTSPLIT
8178 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8179 type = USE_REDRAW;
8180 else
8181#endif
8182 if (can_clear(T_CD) && result_empty)
8183 type = USE_T_CD;
8184 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8185 type = USE_T_CAL;
8186 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8187 type = USE_T_CDL;
8188 else if (*T_AL != NUL)
8189 type = USE_T_AL;
8190 else if (can_ce && result_empty)
8191 type = USE_T_CE;
8192 else if (*T_DL != NUL && result_empty)
8193 type = USE_T_DL;
8194 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8195 type = USE_T_SR;
8196 else
8197 return FAIL;
8198
8199 /*
8200 * For clearing the lines screen_del_lines() is used. This will also take
8201 * care of t_db if necessary.
8202 */
8203 if (type == USE_T_CD || type == USE_T_CDL ||
8204 type == USE_T_CE || type == USE_T_DL)
8205 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8206
8207 /*
8208 * If text is retained below the screen, first clear or delete as many
8209 * lines at the bottom of the window as are about to be inserted so that
8210 * the deleted lines won't later surface during a screen_del_lines.
8211 */
8212 if (*T_DB)
8213 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8214
8215#ifdef FEAT_CLIPBOARD
8216 /* Remove a modeless selection when inserting lines halfway the screen
8217 * or not the full width of the screen. */
8218 if (off + row > 0
8219# ifdef FEAT_VERTSPLIT
8220 || (wp != NULL && wp->w_width != Columns)
8221# endif
8222 )
8223 clip_clear_selection();
8224 else
8225 clip_scroll_selection(-line_count);
8226#endif
8227
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228#ifdef FEAT_GUI
8229 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8230 * scrolling is actually carried out. */
8231 gui_dont_update_cursor();
8232#endif
8233
8234 if (*T_CCS != NUL) /* cursor relative to region */
8235 cursor_row = row;
8236 else
8237 cursor_row = row + off;
8238
8239 /*
8240 * Shift LineOffset[] line_count down to reflect the inserted lines.
8241 * Clear the inserted lines in ScreenLines[].
8242 */
8243 row += off;
8244 end += off;
8245 for (i = 0; i < line_count; ++i)
8246 {
8247#ifdef FEAT_VERTSPLIT
8248 if (wp != NULL && wp->w_width != Columns)
8249 {
8250 /* need to copy part of a line */
8251 j = end - 1 - i;
8252 while ((j -= line_count) >= row)
8253 linecopy(j + line_count, j, wp);
8254 j += line_count;
8255 if (can_clear((char_u *)" "))
8256 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8257 else
8258 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8259 LineWraps[j] = FALSE;
8260 }
8261 else
8262#endif
8263 {
8264 j = end - 1 - i;
8265 temp = LineOffset[j];
8266 while ((j -= line_count) >= row)
8267 {
8268 LineOffset[j + line_count] = LineOffset[j];
8269 LineWraps[j + line_count] = LineWraps[j];
8270 }
8271 LineOffset[j + line_count] = temp;
8272 LineWraps[j + line_count] = FALSE;
8273 if (can_clear((char_u *)" "))
8274 lineclear(temp, (int)Columns);
8275 else
8276 lineinvalid(temp, (int)Columns);
8277 }
8278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279
8280 screen_stop_highlight();
8281 windgoto(cursor_row, 0);
8282
8283#ifdef FEAT_VERTSPLIT
8284 /* redraw the characters */
8285 if (type == USE_REDRAW)
8286 redraw_block(row, end, wp);
8287 else
8288#endif
8289 if (type == USE_T_CAL)
8290 {
8291 term_append_lines(line_count);
8292 screen_start(); /* don't know where cursor is now */
8293 }
8294 else
8295 {
8296 for (i = 0; i < line_count; i++)
8297 {
8298 if (type == USE_T_AL)
8299 {
8300 if (i && cursor_row != 0)
8301 windgoto(cursor_row, 0);
8302 out_str(T_AL);
8303 }
8304 else /* type == USE_T_SR */
8305 out_str(T_SR);
8306 screen_start(); /* don't know where cursor is now */
8307 }
8308 }
8309
8310 /*
8311 * With scroll-reverse and 'da' flag set we need to clear the lines that
8312 * have been scrolled down into the region.
8313 */
8314 if (type == USE_T_SR && *T_DA)
8315 {
8316 for (i = 0; i < line_count; ++i)
8317 {
8318 windgoto(off + i, 0);
8319 out_str(T_CE);
8320 screen_start(); /* don't know where cursor is now */
8321 }
8322 }
8323
8324#ifdef FEAT_GUI
8325 gui_can_update_cursor();
8326 if (gui.in_use)
8327 out_flush(); /* always flush after a scroll */
8328#endif
8329 return OK;
8330}
8331
8332/*
8333 * delete lines on the screen and update ScreenLines[]
8334 * 'end' is the line after the scrolled part. Normally it is Rows.
8335 * When scrolling region used 'off' is the offset from the top for the region.
8336 * 'row' and 'end' are relative to the start of the region.
8337 *
8338 * Return OK for success, FAIL if the lines are not deleted.
8339 */
8340/*ARGSUSED*/
8341 int
8342screen_del_lines(off, row, line_count, end, force, wp)
8343 int off;
8344 int row;
8345 int line_count;
8346 int end;
8347 int force; /* even when line_count > p_ttyscroll */
8348 win_T *wp; /* NULL or window to use width from */
8349{
8350 int j;
8351 int i;
8352 unsigned temp;
8353 int cursor_row;
8354 int cursor_end;
8355 int result_empty; /* result is empty until end of region */
8356 int can_delete; /* deleting line codes can be used */
8357 int type;
8358
8359 /*
8360 * FAIL if
8361 * - there is no valid screen
8362 * - the screen has to be redrawn completely
8363 * - the line count is less than one
8364 * - the line count is more than 'ttyscroll'
8365 */
8366 if (!screen_valid(TRUE) || line_count <= 0 ||
8367 (!force && line_count > p_ttyscroll))
8368 return FAIL;
8369
8370 /*
8371 * Check if the rest of the current region will become empty.
8372 */
8373 result_empty = row + line_count >= end;
8374
8375 /*
8376 * We can delete lines only when 'db' flag not set or when 'ce' option
8377 * available.
8378 */
8379 can_delete = (*T_DB == NUL || can_clear(T_CE));
8380
8381 /*
8382 * There are six ways to delete lines:
8383 * 0. When in a vertically split window and t_CV isn't set, redraw the
8384 * characters from ScreenLines[].
8385 * 1. Use T_CD if it exists and the result is empty.
8386 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8387 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8388 * none of the other ways work.
8389 * 4. Use T_CE (erase line) if the result is empty.
8390 * 5. Use T_DL (delete line) if it exists.
8391 * 6. redraw the characters from ScreenLines[].
8392 */
8393#ifdef FEAT_VERTSPLIT
8394 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8395 type = USE_REDRAW;
8396 else
8397#endif
8398 if (can_clear(T_CD) && result_empty)
8399 type = USE_T_CD;
8400#if defined(__BEOS__) && defined(BEOS_DR8)
8401 /*
8402 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8403 * its internal termcap... this works okay for tests which test *T_DB !=
8404 * NUL. It has the disadvantage that the user cannot use any :set t_*
8405 * command to get T_DB (back) to empty_option, only :set term=... will do
8406 * the trick...
8407 * Anyway, this hack will hopefully go away with the next OS release.
8408 * (Olaf Seibert)
8409 */
8410 else if (row == 0 && T_DB == empty_option
8411 && (line_count == 1 || *T_CDL == NUL))
8412#else
8413 else if (row == 0 && (
8414#ifndef AMIGA
8415 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8416 * up, so use delete-line command */
8417 line_count == 1 ||
8418#endif
8419 *T_CDL == NUL))
8420#endif
8421 type = USE_NL;
8422 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8423 type = USE_T_CDL;
8424 else if (can_clear(T_CE) && result_empty
8425#ifdef FEAT_VERTSPLIT
8426 && (wp == NULL || wp->w_width == Columns)
8427#endif
8428 )
8429 type = USE_T_CE;
8430 else if (*T_DL != NUL && can_delete)
8431 type = USE_T_DL;
8432 else if (*T_CDL != NUL && can_delete)
8433 type = USE_T_CDL;
8434 else
8435 return FAIL;
8436
8437#ifdef FEAT_CLIPBOARD
8438 /* Remove a modeless selection when deleting lines halfway the screen or
8439 * not the full width of the screen. */
8440 if (off + row > 0
8441# ifdef FEAT_VERTSPLIT
8442 || (wp != NULL && wp->w_width != Columns)
8443# endif
8444 )
8445 clip_clear_selection();
8446 else
8447 clip_scroll_selection(line_count);
8448#endif
8449
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450#ifdef FEAT_GUI
8451 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8452 * scrolling is actually carried out. */
8453 gui_dont_update_cursor();
8454#endif
8455
8456 if (*T_CCS != NUL) /* cursor relative to region */
8457 {
8458 cursor_row = row;
8459 cursor_end = end;
8460 }
8461 else
8462 {
8463 cursor_row = row + off;
8464 cursor_end = end + off;
8465 }
8466
8467 /*
8468 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8469 * Clear the inserted lines in ScreenLines[].
8470 */
8471 row += off;
8472 end += off;
8473 for (i = 0; i < line_count; ++i)
8474 {
8475#ifdef FEAT_VERTSPLIT
8476 if (wp != NULL && wp->w_width != Columns)
8477 {
8478 /* need to copy part of a line */
8479 j = row + i;
8480 while ((j += line_count) <= end - 1)
8481 linecopy(j - line_count, j, wp);
8482 j -= line_count;
8483 if (can_clear((char_u *)" "))
8484 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8485 else
8486 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8487 LineWraps[j] = FALSE;
8488 }
8489 else
8490#endif
8491 {
8492 /* whole width, moving the line pointers is faster */
8493 j = row + i;
8494 temp = LineOffset[j];
8495 while ((j += line_count) <= end - 1)
8496 {
8497 LineOffset[j - line_count] = LineOffset[j];
8498 LineWraps[j - line_count] = LineWraps[j];
8499 }
8500 LineOffset[j - line_count] = temp;
8501 LineWraps[j - line_count] = FALSE;
8502 if (can_clear((char_u *)" "))
8503 lineclear(temp, (int)Columns);
8504 else
8505 lineinvalid(temp, (int)Columns);
8506 }
8507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
8509 screen_stop_highlight();
8510
8511#ifdef FEAT_VERTSPLIT
8512 /* redraw the characters */
8513 if (type == USE_REDRAW)
8514 redraw_block(row, end, wp);
8515 else
8516#endif
8517 if (type == USE_T_CD) /* delete the lines */
8518 {
8519 windgoto(cursor_row, 0);
8520 out_str(T_CD);
8521 screen_start(); /* don't know where cursor is now */
8522 }
8523 else if (type == USE_T_CDL)
8524 {
8525 windgoto(cursor_row, 0);
8526 term_delete_lines(line_count);
8527 screen_start(); /* don't know where cursor is now */
8528 }
8529 /*
8530 * Deleting lines at top of the screen or scroll region: Just scroll
8531 * the whole screen (scroll region) up by outputting newlines on the
8532 * last line.
8533 */
8534 else if (type == USE_NL)
8535 {
8536 windgoto(cursor_end - 1, 0);
8537 for (i = line_count; --i >= 0; )
8538 out_char('\n'); /* cursor will remain on same line */
8539 }
8540 else
8541 {
8542 for (i = line_count; --i >= 0; )
8543 {
8544 if (type == USE_T_DL)
8545 {
8546 windgoto(cursor_row, 0);
8547 out_str(T_DL); /* delete a line */
8548 }
8549 else /* type == USE_T_CE */
8550 {
8551 windgoto(cursor_row + i, 0);
8552 out_str(T_CE); /* erase a line */
8553 }
8554 screen_start(); /* don't know where cursor is now */
8555 }
8556 }
8557
8558 /*
8559 * If the 'db' flag is set, we need to clear the lines that have been
8560 * scrolled up at the bottom of the region.
8561 */
8562 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8563 {
8564 for (i = line_count; i > 0; --i)
8565 {
8566 windgoto(cursor_end - i, 0);
8567 out_str(T_CE); /* erase a line */
8568 screen_start(); /* don't know where cursor is now */
8569 }
8570 }
8571
8572#ifdef FEAT_GUI
8573 gui_can_update_cursor();
8574 if (gui.in_use)
8575 out_flush(); /* always flush after a scroll */
8576#endif
8577
8578 return OK;
8579}
8580
8581/*
8582 * show the current mode and ruler
8583 *
8584 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8585 * If clear_cmdline is FALSE there may be a message there that needs to be
8586 * cleared only if a mode is shown.
8587 * Return the length of the message (0 if no message).
8588 */
8589 int
8590showmode()
8591{
8592 int need_clear;
8593 int length = 0;
8594 int do_mode;
8595 int attr;
8596 int nwr_save;
8597#ifdef FEAT_INS_EXPAND
8598 int sub_attr;
8599#endif
8600
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008601 do_mode = ((p_smd && msg_silent == 0)
8602 && ((State & INSERT)
8603 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604#ifdef FEAT_VISUAL
8605 || VIsual_active
8606#endif
8607 ));
8608 if (do_mode || Recording)
8609 {
8610 /*
8611 * Don't show mode right now, when not redrawing or inside a mapping.
8612 * Call char_avail() only when we are going to show something, because
8613 * it takes a bit of time.
8614 */
8615 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8616 {
8617 redraw_cmdline = TRUE; /* show mode later */
8618 return 0;
8619 }
8620
8621 nwr_save = need_wait_return;
8622
8623 /* wait a bit before overwriting an important message */
8624 check_for_delay(FALSE);
8625
8626 /* if the cmdline is more than one line high, erase top lines */
8627 need_clear = clear_cmdline;
8628 if (clear_cmdline && cmdline_row < Rows - 1)
8629 msg_clr_cmdline(); /* will reset clear_cmdline */
8630
8631 /* Position on the last line in the window, column 0 */
8632 msg_pos_mode();
8633 cursor_off();
8634 attr = hl_attr(HLF_CM); /* Highlight mode */
8635 if (do_mode)
8636 {
8637 MSG_PUTS_ATTR("--", attr);
8638#if defined(FEAT_XIM)
8639 if (xic != NULL && im_get_status() && !p_imdisable
8640 && curbuf->b_p_iminsert == B_IMODE_IM)
8641# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8642 MSG_PUTS_ATTR(" IM", attr);
8643# else
8644 MSG_PUTS_ATTR(" XIM", attr);
8645# endif
8646#endif
8647#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8648 if (gui.in_use)
8649 {
8650 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008651 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 }
8653#endif
8654#ifdef FEAT_INS_EXPAND
8655 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8656 {
8657 /* These messages can get long, avoid a wrap in a narrow
8658 * window. Prefer showing edit_submode_extra. */
8659 length = (Rows - msg_row) * Columns - 3;
8660 if (edit_submode_extra != NULL)
8661 length -= vim_strsize(edit_submode_extra);
8662 if (length > 0)
8663 {
8664 if (edit_submode_pre != NULL)
8665 length -= vim_strsize(edit_submode_pre);
8666 if (length - vim_strsize(edit_submode) > 0)
8667 {
8668 if (edit_submode_pre != NULL)
8669 msg_puts_attr(edit_submode_pre, attr);
8670 msg_puts_attr(edit_submode, attr);
8671 }
8672 if (edit_submode_extra != NULL)
8673 {
8674 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8675 if ((int)edit_submode_highl < (int)HLF_COUNT)
8676 sub_attr = hl_attr(edit_submode_highl);
8677 else
8678 sub_attr = attr;
8679 msg_puts_attr(edit_submode_extra, sub_attr);
8680 }
8681 }
8682 length = 0;
8683 }
8684 else
8685#endif
8686 {
8687#ifdef FEAT_VREPLACE
8688 if (State & VREPLACE_FLAG)
8689 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8690 else
8691#endif
8692 if (State & REPLACE_FLAG)
8693 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8694 else if (State & INSERT)
8695 {
8696#ifdef FEAT_RIGHTLEFT
8697 if (p_ri)
8698 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8699#endif
8700 MSG_PUTS_ATTR(_(" INSERT"), attr);
8701 }
8702 else if (restart_edit == 'I')
8703 MSG_PUTS_ATTR(_(" (insert)"), attr);
8704 else if (restart_edit == 'R')
8705 MSG_PUTS_ATTR(_(" (replace)"), attr);
8706 else if (restart_edit == 'V')
8707 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8708#ifdef FEAT_RIGHTLEFT
8709 if (p_hkmap)
8710 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8711# ifdef FEAT_FKMAP
8712 if (p_fkmap)
8713 MSG_PUTS_ATTR(farsi_text_5, attr);
8714# endif
8715#endif
8716#ifdef FEAT_KEYMAP
8717 if (State & LANGMAP)
8718 {
8719# ifdef FEAT_ARABIC
8720 if (curwin->w_p_arab)
8721 MSG_PUTS_ATTR(_(" Arabic"), attr);
8722 else
8723# endif
8724 MSG_PUTS_ATTR(_(" (lang)"), attr);
8725 }
8726#endif
8727 if ((State & INSERT) && p_paste)
8728 MSG_PUTS_ATTR(_(" (paste)"), attr);
8729
8730#ifdef FEAT_VISUAL
8731 if (VIsual_active)
8732 {
8733 char *p;
8734
8735 /* Don't concatenate separate words to avoid translation
8736 * problems. */
8737 switch ((VIsual_select ? 4 : 0)
8738 + (VIsual_mode == Ctrl_V) * 2
8739 + (VIsual_mode == 'V'))
8740 {
8741 case 0: p = N_(" VISUAL"); break;
8742 case 1: p = N_(" VISUAL LINE"); break;
8743 case 2: p = N_(" VISUAL BLOCK"); break;
8744 case 4: p = N_(" SELECT"); break;
8745 case 5: p = N_(" SELECT LINE"); break;
8746 default: p = N_(" SELECT BLOCK"); break;
8747 }
8748 MSG_PUTS_ATTR(_(p), attr);
8749 }
8750#endif
8751 MSG_PUTS_ATTR(" --", attr);
8752 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008753
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 need_clear = TRUE;
8755 }
8756 if (Recording
8757#ifdef FEAT_INS_EXPAND
8758 && edit_submode == NULL /* otherwise it gets too long */
8759#endif
8760 )
8761 {
8762 MSG_PUTS_ATTR(_("recording"), attr);
8763 need_clear = TRUE;
8764 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008765
8766 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 if (need_clear || clear_cmdline)
8768 msg_clr_eos();
8769 msg_didout = FALSE; /* overwrite this message */
8770 length = msg_col;
8771 msg_col = 0;
8772 need_wait_return = nwr_save; /* never ask for hit-return for this */
8773 }
8774 else if (clear_cmdline && msg_silent == 0)
8775 /* Clear the whole command line. Will reset "clear_cmdline". */
8776 msg_clr_cmdline();
8777
8778#ifdef FEAT_CMDL_INFO
8779# ifdef FEAT_VISUAL
8780 /* In Visual mode the size of the selected area must be redrawn. */
8781 if (VIsual_active)
8782 clear_showcmd();
8783# endif
8784
8785 /* If the last window has no status line, the ruler is after the mode
8786 * message and must be redrawn */
8787 if (redrawing()
8788# ifdef FEAT_WINDOWS
8789 && lastwin->w_status_height == 0
8790# endif
8791 )
8792 win_redr_ruler(lastwin, TRUE);
8793#endif
8794 redraw_cmdline = FALSE;
8795 clear_cmdline = FALSE;
8796
8797 return length;
8798}
8799
8800/*
8801 * Position for a mode message.
8802 */
8803 static void
8804msg_pos_mode()
8805{
8806 msg_col = 0;
8807 msg_row = Rows - 1;
8808}
8809
8810/*
8811 * Delete mode message. Used when ESC is typed which is expected to end
8812 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008813 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 */
8815 void
8816unshowmode(force)
8817 int force;
8818{
8819 /*
8820 * Don't delete it right now, when not redrawing or insided a mapping.
8821 */
8822 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8823 redraw_cmdline = TRUE; /* delete mode later */
8824 else
8825 {
8826 msg_pos_mode();
8827 if (Recording)
8828 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8829 msg_clr_eos();
8830 }
8831}
8832
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008833#if defined(FEAT_WINDOWS)
8834/*
8835 * Draw the tab pages line at the top of the Vim window.
8836 */
8837 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008838draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008839{
8840 int tabcount = 0;
8841 tabpage_T *tp;
8842 int tabwidth;
8843 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008844 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008845 int attr;
8846 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008847 win_T *cwp;
8848 int wincount;
8849 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008850 int c;
8851 int len;
8852 int attr_sel = hl_attr(HLF_TPS);
8853 int attr_nosel = hl_attr(HLF_TP);
8854 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008855 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008856 int room;
8857 int use_sep_chars = (t_colors < 8
8858#ifdef FEAT_GUI
8859 && !gui.in_use
8860#endif
8861 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008862
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008863 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008864
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008865#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00008866 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008867 if (gui_use_tabline())
8868 {
8869 gui_update_tabline();
8870 return;
8871 }
8872#endif
8873
8874 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008875 return;
8876
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008877#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008878
8879 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
8880 for (scol = 0; scol < Columns; ++scol)
8881 TabPageIdxs[scol] = 0;
8882
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008883 /* Use the 'tabline' option if it's set. */
8884 if (*p_tal != NUL)
8885 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008886 int save_called_emsg = called_emsg;
8887
8888 /* Check for an error. If there is one we would loop in redrawing the
8889 * screen. Avoid that by making 'tabline' empty. */
8890 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008891 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008892 if (called_emsg)
8893 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008894 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008895 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008896 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00008897 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008898#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008899 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008900 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
8901 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008902
Bram Moolenaar238a5642006-02-21 22:12:05 +00008903 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
8904 if (tabwidth < 6)
8905 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008906
Bram Moolenaar238a5642006-02-21 22:12:05 +00008907 attr = attr_nosel;
8908 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008909 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008910 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
8911 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008912 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008913 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008914
Bram Moolenaar238a5642006-02-21 22:12:05 +00008915 if (tp->tp_topframe == topframe)
8916 attr = attr_sel;
8917 if (use_sep_chars && col > 0)
8918 screen_putchar('|', 0, col++, attr);
8919
8920 if (tp->tp_topframe != topframe)
8921 attr = attr_nosel;
8922
8923 screen_putchar(' ', 0, col++, attr);
8924
8925 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008926 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008927 cwp = curwin;
8928 wp = firstwin;
8929 }
8930 else
8931 {
8932 cwp = tp->tp_curwin;
8933 wp = tp->tp_firstwin;
8934 }
8935
8936 modified = FALSE;
8937 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
8938 if (bufIsChanged(wp->w_buffer))
8939 modified = TRUE;
8940 if (modified || wincount > 1)
8941 {
8942 if (wincount > 1)
8943 {
8944 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008945 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008946 if (col + len >= Columns - 3)
8947 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008948 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008949#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00008950 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008951#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00008952 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008953#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00008954 );
8955 col += len;
8956 }
8957 if (modified)
8958 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
8959 screen_putchar(' ', 0, col++, attr);
8960 }
8961
8962 room = scol - col + tabwidth - 1;
8963 if (room > 0)
8964 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008965 /* Get buffer name in NameBuff[] */
8966 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008967 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008968 len = vim_strsize(NameBuff);
8969 p = NameBuff;
8970#ifdef FEAT_MBYTE
8971 if (has_mbyte)
8972 while (len > room)
8973 {
8974 len -= ptr2cells(p);
8975 mb_ptr_adv(p);
8976 }
8977 else
8978#endif
8979 if (len > room)
8980 {
8981 p += len - room;
8982 len = room;
8983 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008984 if (len > Columns - col - 1)
8985 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008986
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008987 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008988 col += len;
8989 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00008990 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008991
8992 /* Store the tab page number in TabPageIdxs[], so that
8993 * jump_to_mouse() knows where each one is. */
8994 ++tabcount;
8995 while (scol < col)
8996 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008997 }
8998
Bram Moolenaar238a5642006-02-21 22:12:05 +00008999 if (use_sep_chars)
9000 c = '_';
9001 else
9002 c = ' ';
9003 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009004
9005 /* Put an "X" for closing the current tab if there are several. */
9006 if (first_tabpage->tp_next != NULL)
9007 {
9008 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
9009 TabPageIdxs[Columns - 1] = -999;
9010 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009011 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +00009012
9013 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
9014 * set. */
9015 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009016}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009017
9018/*
9019 * Get buffer name for "buf" into NameBuff[].
9020 * Takes care of special buffer names and translates special characters.
9021 */
9022 void
9023get_trans_bufname(buf)
9024 buf_T *buf;
9025{
9026 if (buf_spname(buf) != NULL)
9027 STRCPY(NameBuff, buf_spname(buf));
9028 else
9029 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
9030 trans_characters(NameBuff, MAXPATHL);
9031}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009032#endif
9033
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
9035/*
9036 * Get the character to use in a status line. Get its attributes in "*attr".
9037 */
9038 static int
9039fillchar_status(attr, is_curwin)
9040 int *attr;
9041 int is_curwin;
9042{
9043 int fill;
9044 if (is_curwin)
9045 {
9046 *attr = hl_attr(HLF_S);
9047 fill = fill_stl;
9048 }
9049 else
9050 {
9051 *attr = hl_attr(HLF_SNC);
9052 fill = fill_stlnc;
9053 }
9054 /* Use fill when there is highlighting, and highlighting of current
9055 * window differs, or the fillchars differ, or this is not the
9056 * current window */
9057 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9058 || !is_curwin || firstwin == lastwin)
9059 || (fill_stl != fill_stlnc)))
9060 return fill;
9061 if (is_curwin)
9062 return '^';
9063 return '=';
9064}
9065#endif
9066
9067#ifdef FEAT_VERTSPLIT
9068/*
9069 * Get the character to use in a separator between vertically split windows.
9070 * Get its attributes in "*attr".
9071 */
9072 static int
9073fillchar_vsep(attr)
9074 int *attr;
9075{
9076 *attr = hl_attr(HLF_C);
9077 if (*attr == 0 && fill_vert == ' ')
9078 return '|';
9079 else
9080 return fill_vert;
9081}
9082#endif
9083
9084/*
9085 * Return TRUE if redrawing should currently be done.
9086 */
9087 int
9088redrawing()
9089{
9090 return (!RedrawingDisabled
9091 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9092}
9093
9094/*
9095 * Return TRUE if printing messages should currently be done.
9096 */
9097 int
9098messaging()
9099{
9100 return (!(p_lz && char_avail() && !KeyTyped));
9101}
9102
9103/*
9104 * Show current status info in ruler and various other places
9105 * If always is FALSE, only show ruler if position has changed.
9106 */
9107 void
9108showruler(always)
9109 int always;
9110{
9111 if (!always && !redrawing())
9112 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009113#ifdef FEAT_INS_EXPAND
9114 if (pum_visible())
9115 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009116# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009117 /* Don't redraw right now, do it later. */
9118 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009119# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009120 return;
9121 }
9122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009124 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009125 {
9126 redraw_custum_statusline(curwin);
9127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 else
9129#endif
9130#ifdef FEAT_CMDL_INFO
9131 win_redr_ruler(curwin, always);
9132#endif
9133
9134#ifdef FEAT_TITLE
9135 if (need_maketitle
9136# ifdef FEAT_STL_OPT
9137 || (p_icon && (stl_syntax & STL_IN_ICON))
9138 || (p_title && (stl_syntax & STL_IN_TITLE))
9139# endif
9140 )
9141 maketitle();
9142#endif
9143}
9144
9145#ifdef FEAT_CMDL_INFO
9146 static void
9147win_redr_ruler(wp, always)
9148 win_T *wp;
9149 int always;
9150{
9151 char_u buffer[70];
9152 int row;
9153 int fillchar;
9154 int attr;
9155 int empty_line = FALSE;
9156 colnr_T virtcol;
9157 int i;
9158 int o;
9159#ifdef FEAT_VERTSPLIT
9160 int this_ru_col;
9161 int off = 0;
9162 int width = Columns;
9163# define WITH_OFF(x) x
9164# define WITH_WIDTH(x) x
9165#else
9166# define WITH_OFF(x) 0
9167# define WITH_WIDTH(x) Columns
9168# define this_ru_col ru_col
9169#endif
9170
9171 /* If 'ruler' off or redrawing disabled, don't do anything */
9172 if (!p_ru)
9173 return;
9174
9175 /*
9176 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9177 * after deleting lines, before cursor.lnum is corrected.
9178 */
9179 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9180 return;
9181
9182#ifdef FEAT_INS_EXPAND
9183 /* Don't draw the ruler while doing insert-completion, it might overwrite
9184 * the (long) mode message. */
9185# ifdef FEAT_WINDOWS
9186 if (wp == lastwin && lastwin->w_status_height == 0)
9187# endif
9188 if (edit_submode != NULL)
9189 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009190 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9191 if (pum_visible())
9192 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#endif
9194
9195#ifdef FEAT_STL_OPT
9196 if (*p_ruf)
9197 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009198 int save_called_emsg = called_emsg;
9199
9200 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009202 if (called_emsg)
9203 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009204 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009205 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 return;
9207 }
9208#endif
9209
9210 /*
9211 * Check if not in Insert mode and the line is empty (will show "0-1").
9212 */
9213 if (!(State & INSERT)
9214 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9215 empty_line = TRUE;
9216
9217 /*
9218 * Only draw the ruler when something changed.
9219 */
9220 validate_virtcol_win(wp);
9221 if ( redraw_cmdline
9222 || always
9223 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9224 || wp->w_cursor.col != wp->w_ru_cursor.col
9225 || wp->w_virtcol != wp->w_ru_virtcol
9226#ifdef FEAT_VIRTUALEDIT
9227 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9228#endif
9229 || wp->w_topline != wp->w_ru_topline
9230 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9231#ifdef FEAT_DIFF
9232 || wp->w_topfill != wp->w_ru_topfill
9233#endif
9234 || empty_line != wp->w_ru_empty)
9235 {
9236 cursor_off();
9237#ifdef FEAT_WINDOWS
9238 if (wp->w_status_height)
9239 {
9240 row = W_WINROW(wp) + wp->w_height;
9241 fillchar = fillchar_status(&attr, wp == curwin);
9242# ifdef FEAT_VERTSPLIT
9243 off = W_WINCOL(wp);
9244 width = W_WIDTH(wp);
9245# endif
9246 }
9247 else
9248#endif
9249 {
9250 row = Rows - 1;
9251 fillchar = ' ';
9252 attr = 0;
9253#ifdef FEAT_VERTSPLIT
9254 width = Columns;
9255 off = 0;
9256#endif
9257 }
9258
9259 /* In list mode virtcol needs to be recomputed */
9260 virtcol = wp->w_virtcol;
9261 if (wp->w_p_list && lcs_tab1 == NUL)
9262 {
9263 wp->w_p_list = FALSE;
9264 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9265 wp->w_p_list = TRUE;
9266 }
9267
9268 /*
9269 * Some sprintfs return the length, some return a pointer.
9270 * To avoid portability problems we use strlen() here.
9271 */
9272 sprintf((char *)buffer, "%ld,",
9273 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9274 ? 0L
9275 : (long)(wp->w_cursor.lnum));
9276 col_print(buffer + STRLEN(buffer),
9277 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9278 (int)virtcol + 1);
9279
9280 /*
9281 * Add a "50%" if there is room for it.
9282 * On the last line, don't print in the last column (scrolls the
9283 * screen up on some terminals).
9284 */
9285 i = (int)STRLEN(buffer);
9286 get_rel_pos(wp, buffer + i + 1);
9287 o = i + vim_strsize(buffer + i + 1);
9288#ifdef FEAT_WINDOWS
9289 if (wp->w_status_height == 0) /* can't use last char of screen */
9290#endif
9291 ++o;
9292#ifdef FEAT_VERTSPLIT
9293 this_ru_col = ru_col - (Columns - width);
9294 if (this_ru_col < 0)
9295 this_ru_col = 0;
9296#endif
9297 /* Never use more than half the window/screen width, leave the other
9298 * half for the filename. */
9299 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9300 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9301 if (this_ru_col + o < WITH_WIDTH(width))
9302 {
9303 while (this_ru_col + o < WITH_WIDTH(width))
9304 {
9305#ifdef FEAT_MBYTE
9306 if (has_mbyte)
9307 i += (*mb_char2bytes)(fillchar, buffer + i);
9308 else
9309#endif
9310 buffer[i++] = fillchar;
9311 ++o;
9312 }
9313 get_rel_pos(wp, buffer + i);
9314 }
9315 /* Truncate at window boundary. */
9316#ifdef FEAT_MBYTE
9317 if (has_mbyte)
9318 {
9319 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009320 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 {
9322 o += (*mb_ptr2cells)(buffer + i);
9323 if (this_ru_col + o > WITH_WIDTH(width))
9324 {
9325 buffer[i] = NUL;
9326 break;
9327 }
9328 }
9329 }
9330 else
9331#endif
9332 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9333 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9334
9335 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9336 i = redraw_cmdline;
9337 screen_fill(row, row + 1,
9338 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9339 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9340 fillchar, fillchar, attr);
9341 /* don't redraw the cmdline because of showing the ruler */
9342 redraw_cmdline = i;
9343 wp->w_ru_cursor = wp->w_cursor;
9344 wp->w_ru_virtcol = wp->w_virtcol;
9345 wp->w_ru_empty = empty_line;
9346 wp->w_ru_topline = wp->w_topline;
9347 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9348#ifdef FEAT_DIFF
9349 wp->w_ru_topfill = wp->w_topfill;
9350#endif
9351 }
9352}
9353#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009354
9355#if defined(FEAT_LINEBREAK) || defined(PROTO)
9356/*
9357 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009358 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009359 * Otherwise it depends on 'numberwidth' and the line count.
9360 */
9361 int
9362number_width(wp)
9363 win_T *wp;
9364{
9365 int n;
9366 linenr_T lnum;
9367
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009368 lnum = wp->w_buffer->b_ml.ml_line_count;
9369 if (lnum == wp->w_nrwidth_line_count)
9370 return wp->w_nrwidth_width;
9371 wp->w_nrwidth_line_count = lnum;
9372
9373 n = 0;
9374 do
9375 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009376 lnum /= 10;
9377 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009378 } while (lnum > 0);
9379
9380 /* 'numberwidth' gives the minimal width plus one */
9381 if (n < wp->w_p_nuw - 1)
9382 n = wp->w_p_nuw - 1;
9383
9384 wp->w_nrwidth_width = n;
9385 return n;
9386}
9387#endif