blob: b82bd983146981d50a4b69c01074870a702677e1 [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);
234 screen_attr = HL_BOLD | HL_UNDERLINE;
235}
236
237/*
238 * Mark all windows to be redrawn later.
239 */
240 void
241redraw_all_later(type)
242 int type;
243{
244 win_T *wp;
245
246 FOR_ALL_WINDOWS(wp)
247 {
248 redraw_win_later(wp, type);
249 }
250}
251
252/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000253 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 */
255 void
256redraw_curbuf_later(type)
257 int type;
258{
259 redraw_buf_later(curbuf, type);
260}
261
262 void
263redraw_buf_later(buf, type)
264 buf_T *buf;
265 int type;
266{
267 win_T *wp;
268
269 FOR_ALL_WINDOWS(wp)
270 {
271 if (wp->w_buffer == buf)
272 redraw_win_later(wp, type);
273 }
274}
275
276/*
277 * Changed something in the current window, at buffer line "lnum", that
278 * requires that line and possibly other lines to be redrawn.
279 * Used when entering/leaving Insert mode with the cursor on a folded line.
280 * Used to remove the "$" from a change command.
281 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
282 * may become invalid and the whole window will have to be redrawn.
283 */
284/*ARGSUSED*/
285 void
286redrawWinline(lnum, invalid)
287 linenr_T lnum;
288 int invalid; /* window line height is invalid now */
289{
290#ifdef FEAT_FOLDING
291 int i;
292#endif
293
294 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
295 curwin->w_redraw_top = lnum;
296 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
297 curwin->w_redraw_bot = lnum;
298 redraw_later(VALID);
299
300#ifdef FEAT_FOLDING
301 if (invalid)
302 {
303 /* A w_lines[] entry for this lnum has become invalid. */
304 i = find_wl_entry(curwin, lnum);
305 if (i >= 0)
306 curwin->w_lines[i].wl_valid = FALSE;
307 }
308#endif
309}
310
311/*
312 * update all windows that are editing the current buffer
313 */
314 void
315update_curbuf(type)
316 int type;
317{
318 redraw_curbuf_later(type);
319 update_screen(type);
320}
321
322/*
323 * update_screen()
324 *
325 * Based on the current value of curwin->w_topline, transfer a screenfull
326 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
327 */
328 void
329update_screen(type)
330 int type;
331{
332 win_T *wp;
333 static int did_intro = FALSE;
334#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
335 int did_one;
336#endif
337
338 if (!screen_valid(TRUE))
339 return;
340
341 if (must_redraw)
342 {
343 if (type < must_redraw) /* use maximal type */
344 type = must_redraw;
345 must_redraw = 0;
346 }
347
348 /* Need to update w_lines[]. */
349 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
350 type = NOT_VALID;
351
352 if (!redrawing())
353 {
354 redraw_later(type); /* remember type for next time */
355 must_redraw = type;
356 if (type > INVERTED_ALL)
357 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
358 return;
359 }
360
361 updating_screen = TRUE;
362#ifdef FEAT_SYN_HL
363 ++display_tick; /* let syntax code know we're in a next round of
364 * display updating */
365#endif
366
367 /*
368 * if the screen was scrolled up when displaying a message, scroll it down
369 */
370 if (msg_scrolled)
371 {
372 clear_cmdline = TRUE;
373 if (msg_scrolled > Rows - 5) /* clearing is faster */
374 type = CLEAR;
375 else if (type != CLEAR)
376 {
377 check_for_delay(FALSE);
378 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
379 type = CLEAR;
380 FOR_ALL_WINDOWS(wp)
381 {
382 if (W_WINROW(wp) < msg_scrolled)
383 {
384 if (W_WINROW(wp) + wp->w_height > msg_scrolled
385 && wp->w_redr_type < REDRAW_TOP
386 && wp->w_lines_valid > 0
387 && wp->w_topline == wp->w_lines[0].wl_lnum)
388 {
389 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
390 wp->w_redr_type = REDRAW_TOP;
391 }
392 else
393 {
394 wp->w_redr_type = NOT_VALID;
395#ifdef FEAT_WINDOWS
396 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
397 <= msg_scrolled)
398 wp->w_redr_status = TRUE;
399#endif
400 }
401 }
402 }
403 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000404#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000405 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 }
408 msg_scrolled = 0;
409 need_wait_return = FALSE;
410 }
411
412 /* reset cmdline_row now (may have been changed temporarily) */
413 compute_cmdrow();
414
415 /* Check for changed highlighting */
416 if (need_highlight_changed)
417 highlight_changed();
418
419 if (type == CLEAR) /* first clear screen */
420 {
421 screenclear(); /* will reset clear_cmdline */
422 type = NOT_VALID;
423 }
424
425 if (clear_cmdline) /* going to clear cmdline (done below) */
426 check_for_delay(FALSE);
427
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000428#ifdef FEAT_LINEBREAK
429 /* Force redraw when width of 'number' column changes. */
430 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000431 && curwin->w_nrwidth != (curwin->w_p_nu ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000432 curwin->w_redr_type = NOT_VALID;
433#endif
434
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 /*
436 * Only start redrawing if there is really something to do.
437 */
438 if (type == INVERTED)
439 update_curswant();
440 if (curwin->w_redr_type < type
441 && !((type == VALID
442 && curwin->w_lines[0].wl_valid
443#ifdef FEAT_DIFF
444 && curwin->w_topfill == curwin->w_old_topfill
445 && curwin->w_botfill == curwin->w_old_botfill
446#endif
447 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
448#ifdef FEAT_VISUAL
449 || (type == INVERTED
450 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
451 && curwin->w_old_visual_mode == VIsual_mode
452 && (curwin->w_valid & VALID_VIRTCOL)
453 && curwin->w_old_curswant == curwin->w_curswant)
454#endif
455 ))
456 curwin->w_redr_type = type;
457
458#ifdef FEAT_SYN_HL
459 /*
460 * Correct stored syntax highlighting info for changes in each displayed
461 * buffer. Each buffer must only be done once.
462 */
463 FOR_ALL_WINDOWS(wp)
464 {
465 if (wp->w_buffer->b_mod_set)
466 {
467# ifdef FEAT_WINDOWS
468 win_T *wwp;
469
470 /* Check if we already did this buffer. */
471 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
472 if (wwp->w_buffer == wp->w_buffer)
473 break;
474# endif
475 if (
476# ifdef FEAT_WINDOWS
477 wwp == wp &&
478# endif
479 syntax_present(wp->w_buffer))
480 syn_stack_apply_changes(wp->w_buffer);
481 }
482 }
483#endif
484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000485#ifdef FEAT_WINDOWS
486 /* Redraw the tab pages line if needed. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000487 if (redraw_tabline || type >= NOT_VALID)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000488 draw_tabline();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000489#endif
490
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 /*
492 * Go from top to bottom through the windows, redrawing the ones that need
493 * it.
494 */
495#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
496 did_one = FALSE;
497#endif
498#ifdef FEAT_SEARCH_EXTRA
499 search_hl.rm.regprog = NULL;
500#endif
501 FOR_ALL_WINDOWS(wp)
502 {
503 if (wp->w_redr_type != 0)
504 {
505 cursor_off();
506#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
507 if (!did_one)
508 {
509 did_one = TRUE;
510# ifdef FEAT_SEARCH_EXTRA
511 start_search_hl();
512# endif
513# ifdef FEAT_CLIPBOARD
514 /* When Visual area changed, may have to update selection. */
515 if (clip_star.available && clip_isautosel())
516 clip_update_selection();
517# endif
518#ifdef FEAT_GUI
519 /* Remove the cursor before starting to do anything, because
520 * scrolling may make it difficult to redraw the text under
521 * it. */
522 if (gui.in_use)
523 gui_undraw_cursor();
524#endif
525 }
526#endif
527 win_update(wp);
528 }
529
530#ifdef FEAT_WINDOWS
531 /* redraw status line after the window to minimize cursor movement */
532 if (wp->w_redr_status)
533 {
534 cursor_off();
535 win_redr_status(wp);
536 }
537#endif
538 }
539#if defined(FEAT_SEARCH_EXTRA)
540 end_search_hl();
541#endif
542
543#ifdef FEAT_WINDOWS
544 /* Reset b_mod_set flags. Going through all windows is probably faster
545 * than going through all buffers (there could be many buffers). */
546 for (wp = firstwin; wp != NULL; wp = wp->w_next)
547 wp->w_buffer->b_mod_set = FALSE;
548#else
549 curbuf->b_mod_set = FALSE;
550#endif
551
552 updating_screen = FALSE;
553#ifdef FEAT_GUI
554 gui_may_resize_shell();
555#endif
556
557 /* Clear or redraw the command line. Done last, because scrolling may
558 * mess up the command line. */
559 if (clear_cmdline || redraw_cmdline)
560 showmode();
561
562 /* May put up an introductory message when not editing a file */
563 if (!did_intro && bufempty()
564 && curbuf->b_fname == NULL
565#ifdef FEAT_WINDOWS
566 && firstwin->w_next == NULL
567#endif
568 && vim_strchr(p_shm, SHM_INTRO) == NULL)
569 intro_message(FALSE);
570 did_intro = TRUE;
571
572#ifdef FEAT_GUI
573 /* Redraw the cursor and update the scrollbars when all screen updating is
574 * done. */
575 if (gui.in_use)
576 {
577 out_flush(); /* required before updating the cursor */
578 if (did_one)
579 gui_update_cursor(FALSE, FALSE);
580 gui_update_scrollbars(FALSE);
581 }
582#endif
583}
584
585#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
586static void update_prepare __ARGS((void));
587static void update_finish __ARGS((void));
588
589/*
590 * Prepare for updating one or more windows.
591 */
592 static void
593update_prepare()
594{
595 cursor_off();
596 updating_screen = TRUE;
597#ifdef FEAT_GUI
598 /* Remove the cursor before starting to do anything, because scrolling may
599 * make it difficult to redraw the text under it. */
600 if (gui.in_use)
601 gui_undraw_cursor();
602#endif
603#ifdef FEAT_SEARCH_EXTRA
604 start_search_hl();
605#endif
606}
607
608/*
609 * Finish updating one or more windows.
610 */
611 static void
612update_finish()
613{
614 if (redraw_cmdline)
615 showmode();
616
617# ifdef FEAT_SEARCH_EXTRA
618 end_search_hl();
619# endif
620
621 updating_screen = FALSE;
622
623# ifdef FEAT_GUI
624 gui_may_resize_shell();
625
626 /* Redraw the cursor and update the scrollbars when all screen updating is
627 * done. */
628 if (gui.in_use)
629 {
630 out_flush(); /* required before updating the cursor */
631 gui_update_cursor(FALSE, FALSE);
632 gui_update_scrollbars(FALSE);
633 }
634# endif
635}
636#endif
637
638#if defined(FEAT_SIGNS) || defined(PROTO)
639 void
640update_debug_sign(buf, lnum)
641 buf_T *buf;
642 linenr_T lnum;
643{
644 win_T *wp;
645 int doit = FALSE;
646
647# ifdef FEAT_FOLDING
648 win_foldinfo.fi_level = 0;
649# endif
650
651 /* update/delete a specific mark */
652 FOR_ALL_WINDOWS(wp)
653 {
654 if (buf != NULL && lnum > 0)
655 {
656 if (wp->w_buffer == buf && lnum >= wp->w_topline
657 && lnum < wp->w_botline)
658 {
659 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
660 wp->w_redraw_top = lnum;
661 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
662 wp->w_redraw_bot = lnum;
663 redraw_win_later(wp, VALID);
664 }
665 }
666 else
667 redraw_win_later(wp, VALID);
668 if (wp->w_redr_type != 0)
669 doit = TRUE;
670 }
671
672 if (!doit)
673 return;
674
675 /* update all windows that need updating */
676 update_prepare();
677
678# ifdef FEAT_WINDOWS
679 for (wp = firstwin; wp; wp = wp->w_next)
680 {
681 if (wp->w_redr_type != 0)
682 win_update(wp);
683 if (wp->w_redr_status)
684 win_redr_status(wp);
685 }
686# else
687 if (curwin->w_redr_type != 0)
688 win_update(curwin);
689# endif
690
691 update_finish();
692}
693#endif
694
695
696#if defined(FEAT_GUI) || defined(PROTO)
697/*
698 * Update a single window, its status line and maybe the command line msg.
699 * Used for the GUI scrollbar.
700 */
701 void
702updateWindow(wp)
703 win_T *wp;
704{
705 update_prepare();
706
707#ifdef FEAT_CLIPBOARD
708 /* When Visual area changed, may have to update selection. */
709 if (clip_star.available && clip_isautosel())
710 clip_update_selection();
711#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000712
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000716 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000717 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000718 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 if (wp->w_redr_status
721# ifdef FEAT_CMDL_INFO
722 || p_ru
723# endif
724# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000725 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726# endif
727 )
728 win_redr_status(wp);
729#endif
730
731 update_finish();
732}
733#endif
734
735/*
736 * Update a single window.
737 *
738 * This may cause the windows below it also to be redrawn (when clearing the
739 * screen or scrolling lines).
740 *
741 * How the window is redrawn depends on wp->w_redr_type. Each type also
742 * implies the one below it.
743 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000744 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
746 * INVERTED redraw the changed part of the Visual area
747 * INVERTED_ALL redraw the whole Visual area
748 * VALID 1. scroll up/down to adjust for a changed w_topline
749 * 2. update lines at the top when scrolled down
750 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000751 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 * b_mod_top and b_mod_bot.
753 * - if wp->w_redraw_top non-zero, redraw lines between
754 * wp->w_redraw_top and wp->w_redr_bot.
755 * - continue redrawing when syntax status is invalid.
756 * 4. if scrolled up, update lines at the bottom.
757 * This results in three areas that may need updating:
758 * top: from first row to top_end (when scrolled down)
759 * mid: from mid_start to mid_end (update inversion or changed text)
760 * bot: from bot_start to last row (when scrolled up)
761 */
762 static void
763win_update(wp)
764 win_T *wp;
765{
766 buf_T *buf = wp->w_buffer;
767 int type;
768 int top_end = 0; /* Below last row of the top area that needs
769 updating. 0 when no top area updating. */
770 int mid_start = 999;/* first row of the mid area that needs
771 updating. 999 when no mid area updating. */
772 int mid_end = 0; /* Below last row of the mid area that needs
773 updating. 0 when no mid area updating. */
774 int bot_start = 999;/* first row of the bot area that needs
775 updating. 999 when no bot area updating */
776#ifdef FEAT_VISUAL
777 int scrolled_down = FALSE; /* TRUE when scrolled down when
778 w_topline got smaller a bit */
779#endif
780#ifdef FEAT_SEARCH_EXTRA
781 int top_to_mod = FALSE; /* redraw above mod_top */
782#endif
783
784 int row; /* current window row to display */
785 linenr_T lnum; /* current buffer lnum to display */
786 int idx; /* current index in w_lines[] */
787 int srow; /* starting row of the current line */
788
789 int eof = FALSE; /* if TRUE, we hit the end of the file */
790 int didline = FALSE; /* if TRUE, we finished the last line */
791 int i;
792 long j;
793 static int recursive = FALSE; /* being called recursively */
794 int old_botline = wp->w_botline;
795#ifdef FEAT_FOLDING
796 long fold_count;
797#endif
798#ifdef FEAT_SYN_HL
799 /* remember what happened to the previous line, to know if
800 * check_visual_highlight() can be used */
801#define DID_NONE 1 /* didn't update a line */
802#define DID_LINE 2 /* updated a normal line */
803#define DID_FOLD 3 /* updated a folded line */
804 int did_update = DID_NONE;
805 linenr_T syntax_last_parsed = 0; /* last parsed text line */
806#endif
807 linenr_T mod_top = 0;
808 linenr_T mod_bot = 0;
809#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
810 int save_got_int;
811#endif
812
813 type = wp->w_redr_type;
814
815 if (type == NOT_VALID)
816 {
817#ifdef FEAT_WINDOWS
818 wp->w_redr_status = TRUE;
819#endif
820 wp->w_lines_valid = 0;
821 }
822
823 /* Window is zero-height: nothing to draw. */
824 if (wp->w_height == 0)
825 {
826 wp->w_redr_type = 0;
827 return;
828 }
829
830#ifdef FEAT_VERTSPLIT
831 /* Window is zero-width: Only need to draw the separator. */
832 if (wp->w_width == 0)
833 {
834 /* draw the vertical separator right of this window */
835 draw_vsep_win(wp, 0);
836 wp->w_redr_type = 0;
837 return;
838 }
839#endif
840
841#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000842 /* Setup for ":match" and 'hlsearch' highlighting. Disable any previous
843 * match */
844 for (i = 0; i < 3; ++i)
845 {
846 match_hl[i].rm = wp->w_match[i];
847 if (wp->w_match_id[i] == 0)
848 match_hl[i].attr = 0;
849 else
850 match_hl[i].attr = syn_id2attr(wp->w_match_id[i]);
851 match_hl[i].buf = buf;
852 match_hl[i].lnum = 0;
853 match_hl[i].first_lnum = 0;
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 search_hl.buf = buf;
856 search_hl.lnum = 0;
857 search_hl.first_lnum = 0;
858#endif
859
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000860#ifdef FEAT_LINEBREAK
861 /* Force redraw when width of 'number' column changes. */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000862 i = wp->w_p_nu ? number_width(wp) : 0;
863 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000864 {
865 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000866 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000867 }
868 else
869#endif
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
872 {
873 /*
874 * When there are both inserted/deleted lines and specific lines to be
875 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
876 * everything (only happens when redrawing is off for while).
877 */
878 type = NOT_VALID;
879 }
880 else
881 {
882 /*
883 * Set mod_top to the first line that needs displaying because of
884 * changes. Set mod_bot to the first line after the changes.
885 */
886 mod_top = wp->w_redraw_top;
887 if (wp->w_redraw_bot != 0)
888 mod_bot = wp->w_redraw_bot + 1;
889 else
890 mod_bot = 0;
891 wp->w_redraw_top = 0; /* reset for next time */
892 wp->w_redraw_bot = 0;
893 if (buf->b_mod_set)
894 {
895 if (mod_top == 0 || mod_top > buf->b_mod_top)
896 {
897 mod_top = buf->b_mod_top;
898#ifdef FEAT_SYN_HL
899 /* Need to redraw lines above the change that may be included
900 * in a pattern match. */
901 if (syntax_present(buf))
902 {
903 mod_top -= buf->b_syn_sync_linebreaks;
904 if (mod_top < 1)
905 mod_top = 1;
906 }
907#endif
908 }
909 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
910 mod_bot = buf->b_mod_bot;
911
912#ifdef FEAT_SEARCH_EXTRA
913 /* When 'hlsearch' is on and using a multi-line search pattern, a
914 * change in one line may make the Search highlighting in a
915 * previous line invalid. Simple solution: redraw all visible
916 * lines above the change.
917 * Same for a ":match" pattern.
918 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000919 if (search_hl.rm.regprog != NULL
920 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +0000922 else
923 for (i = 0; i < 3; ++i)
924 if (match_hl[i].rm.regprog != NULL
925 && re_multiline(match_hl[i].rm.regprog))
926 {
927 top_to_mod = TRUE;
928 break;
929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#endif
931 }
932#ifdef FEAT_FOLDING
933 if (mod_top != 0 && hasAnyFolding(wp))
934 {
935 linenr_T lnumt, lnumb;
936
937 /*
938 * A change in a line can cause lines above it to become folded or
939 * unfolded. Find the top most buffer line that may be affected.
940 * If the line was previously folded and displayed, get the first
941 * line of that fold. If the line is folded now, get the first
942 * folded line. Use the minimum of these two.
943 */
944
945 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
946 * the line below it. If there is no valid entry, use w_topline.
947 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
948 * to this line. If there is no valid entry, use MAXLNUM. */
949 lnumt = wp->w_topline;
950 lnumb = MAXLNUM;
951 for (i = 0; i < wp->w_lines_valid; ++i)
952 if (wp->w_lines[i].wl_valid)
953 {
954 if (wp->w_lines[i].wl_lastlnum < mod_top)
955 lnumt = wp->w_lines[i].wl_lastlnum + 1;
956 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
957 {
958 lnumb = wp->w_lines[i].wl_lnum;
959 /* When there is a fold column it might need updating
960 * in the next line ("J" just above an open fold). */
961 if (wp->w_p_fdc > 0)
962 ++lnumb;
963 }
964 }
965
966 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
967 if (mod_top > lnumt)
968 mod_top = lnumt;
969
970 /* Now do the same for the bottom line (one above mod_bot). */
971 --mod_bot;
972 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
973 ++mod_bot;
974 if (mod_bot < lnumb)
975 mod_bot = lnumb;
976 }
977#endif
978
979 /* When a change starts above w_topline and the end is below
980 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000981 * If the end of the change is above w_topline: do like no change was
982 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (mod_top != 0 && mod_top < wp->w_topline)
984 {
985 if (mod_bot > wp->w_topline)
986 mod_top = wp->w_topline;
987#ifdef FEAT_SYN_HL
988 else if (syntax_present(buf))
989 top_end = 1;
990#endif
991 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000992
993 /* When line numbers are displayed need to redraw all lines below
994 * inserted/deleted lines. */
995 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
996 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
998
999 /*
1000 * When only displaying the lines at the top, set top_end. Used when
1001 * window has scrolled down for msg_scrolled.
1002 */
1003 if (type == REDRAW_TOP)
1004 {
1005 j = 0;
1006 for (i = 0; i < wp->w_lines_valid; ++i)
1007 {
1008 j += wp->w_lines[i].wl_size;
1009 if (j >= wp->w_upd_rows)
1010 {
1011 top_end = j;
1012 break;
1013 }
1014 }
1015 if (top_end == 0)
1016 /* not found (cannot happen?): redraw everything */
1017 type = NOT_VALID;
1018 else
1019 /* top area defined, the rest is VALID */
1020 type = VALID;
1021 }
1022
1023 /*
1024 * If there are no changes on the screen that require a complete redraw,
1025 * handle three cases:
1026 * 1: we are off the top of the screen by a few lines: scroll down
1027 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1028 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1029 * w_lines[] that needs updating.
1030 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001031 if ((type == VALID || type == SOME_VALID
1032 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033#ifdef FEAT_DIFF
1034 && !wp->w_botfill && !wp->w_old_botfill
1035#endif
1036 )
1037 {
1038 if (mod_top != 0 && wp->w_topline == mod_top)
1039 {
1040 /*
1041 * w_topline is the first changed line, the scrolling will be done
1042 * further down.
1043 */
1044 }
1045 else if (wp->w_lines[0].wl_valid
1046 && (wp->w_topline < wp->w_lines[0].wl_lnum
1047#ifdef FEAT_DIFF
1048 || (wp->w_topline == wp->w_lines[0].wl_lnum
1049 && wp->w_topfill > wp->w_old_topfill)
1050#endif
1051 ))
1052 {
1053 /*
1054 * New topline is above old topline: May scroll down.
1055 */
1056#ifdef FEAT_FOLDING
1057 if (hasAnyFolding(wp))
1058 {
1059 linenr_T ln;
1060
1061 /* count the number of lines we are off, counting a sequence
1062 * of folded lines as one */
1063 j = 0;
1064 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1065 {
1066 ++j;
1067 if (j >= wp->w_height - 2)
1068 break;
1069 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1070 }
1071 }
1072 else
1073#endif
1074 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1075 if (j < wp->w_height - 2) /* not too far off */
1076 {
1077 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1078#ifdef FEAT_DIFF
1079 /* insert extra lines for previously invisible filler lines */
1080 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1081 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1082 - wp->w_old_topfill;
1083#endif
1084 if (i < wp->w_height - 2) /* less than a screen off */
1085 {
1086 /*
1087 * Try to insert the correct number of lines.
1088 * If not the last window, delete the lines at the bottom.
1089 * win_ins_lines may fail when the terminal can't do it.
1090 */
1091 if (i > 0)
1092 check_for_delay(FALSE);
1093 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1094 {
1095 if (wp->w_lines_valid != 0)
1096 {
1097 /* Need to update rows that are new, stop at the
1098 * first one that scrolled down. */
1099 top_end = i;
1100#ifdef FEAT_VISUAL
1101 scrolled_down = TRUE;
1102#endif
1103
1104 /* Move the entries that were scrolled, disable
1105 * the entries for the lines to be redrawn. */
1106 if ((wp->w_lines_valid += j) > wp->w_height)
1107 wp->w_lines_valid = wp->w_height;
1108 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1109 wp->w_lines[idx] = wp->w_lines[idx - j];
1110 while (idx >= 0)
1111 wp->w_lines[idx--].wl_valid = FALSE;
1112 }
1113 }
1114 else
1115 mid_start = 0; /* redraw all lines */
1116 }
1117 else
1118 mid_start = 0; /* redraw all lines */
1119 }
1120 else
1121 mid_start = 0; /* redraw all lines */
1122 }
1123 else
1124 {
1125 /*
1126 * New topline is at or below old topline: May scroll up.
1127 * When topline didn't change, find first entry in w_lines[] that
1128 * needs updating.
1129 */
1130
1131 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1132 j = -1;
1133 row = 0;
1134 for (i = 0; i < wp->w_lines_valid; i++)
1135 {
1136 if (wp->w_lines[i].wl_valid
1137 && wp->w_lines[i].wl_lnum == wp->w_topline)
1138 {
1139 j = i;
1140 break;
1141 }
1142 row += wp->w_lines[i].wl_size;
1143 }
1144 if (j == -1)
1145 {
1146 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1147 * lines */
1148 mid_start = 0;
1149 }
1150 else
1151 {
1152 /*
1153 * Try to delete the correct number of lines.
1154 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1155 */
1156#ifdef FEAT_DIFF
1157 /* If the topline didn't change, delete old filler lines,
1158 * otherwise delete filler lines of the new topline... */
1159 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1160 row += wp->w_old_topfill;
1161 else
1162 row += diff_check_fill(wp, wp->w_topline);
1163 /* ... but don't delete new filler lines. */
1164 row -= wp->w_topfill;
1165#endif
1166 if (row > 0)
1167 {
1168 check_for_delay(FALSE);
1169 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1170 bot_start = wp->w_height - row;
1171 else
1172 mid_start = 0; /* redraw all lines */
1173 }
1174 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1175 {
1176 /*
1177 * Skip the lines (below the deleted lines) that are still
1178 * valid and don't need redrawing. Copy their info
1179 * upwards, to compensate for the deleted lines. Set
1180 * bot_start to the first row that needs redrawing.
1181 */
1182 bot_start = 0;
1183 idx = 0;
1184 for (;;)
1185 {
1186 wp->w_lines[idx] = wp->w_lines[j];
1187 /* stop at line that didn't fit, unless it is still
1188 * valid (no lines deleted) */
1189 if (row > 0 && bot_start + row
1190 + (int)wp->w_lines[j].wl_size > wp->w_height)
1191 {
1192 wp->w_lines_valid = idx + 1;
1193 break;
1194 }
1195 bot_start += wp->w_lines[idx++].wl_size;
1196
1197 /* stop at the last valid entry in w_lines[].wl_size */
1198 if (++j >= wp->w_lines_valid)
1199 {
1200 wp->w_lines_valid = idx;
1201 break;
1202 }
1203 }
1204#ifdef FEAT_DIFF
1205 /* Correct the first entry for filler lines at the top
1206 * when it won't get updated below. */
1207 if (wp->w_p_diff && bot_start > 0)
1208 wp->w_lines[0].wl_size =
1209 plines_win_nofill(wp, wp->w_topline, TRUE)
1210 + wp->w_topfill;
1211#endif
1212 }
1213 }
1214 }
1215
1216 /* When starting redraw in the first line, redraw all lines. When
1217 * there is only one window it's probably faster to clear the screen
1218 * first. */
1219 if (mid_start == 0)
1220 {
1221 mid_end = wp->w_height;
1222 if (lastwin == firstwin)
1223 screenclear();
1224 }
1225 }
1226 else
1227 {
1228 /* Not VALID or INVERTED: redraw all lines. */
1229 mid_start = 0;
1230 mid_end = wp->w_height;
1231 }
1232
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001233 if (type == SOME_VALID)
1234 {
1235 /* SOME_VALID: redraw all lines. */
1236 mid_start = 0;
1237 mid_end = wp->w_height;
1238 type = NOT_VALID;
1239 }
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241#ifdef FEAT_VISUAL
1242 /* check if we are updating or removing the inverted part */
1243 if ((VIsual_active && buf == curwin->w_buffer)
1244 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1245 {
1246 linenr_T from, to;
1247
1248 if (VIsual_active)
1249 {
1250 if (VIsual_active
1251 && (VIsual_mode != wp->w_old_visual_mode
1252 || type == INVERTED_ALL))
1253 {
1254 /*
1255 * If the type of Visual selection changed, redraw the whole
1256 * selection. Also when the ownership of the X selection is
1257 * gained or lost.
1258 */
1259 if (curwin->w_cursor.lnum < VIsual.lnum)
1260 {
1261 from = curwin->w_cursor.lnum;
1262 to = VIsual.lnum;
1263 }
1264 else
1265 {
1266 from = VIsual.lnum;
1267 to = curwin->w_cursor.lnum;
1268 }
1269 /* redraw more when the cursor moved as well */
1270 if (wp->w_old_cursor_lnum < from)
1271 from = wp->w_old_cursor_lnum;
1272 if (wp->w_old_cursor_lnum > to)
1273 to = wp->w_old_cursor_lnum;
1274 if (wp->w_old_visual_lnum < from)
1275 from = wp->w_old_visual_lnum;
1276 if (wp->w_old_visual_lnum > to)
1277 to = wp->w_old_visual_lnum;
1278 }
1279 else
1280 {
1281 /*
1282 * Find the line numbers that need to be updated: The lines
1283 * between the old cursor position and the current cursor
1284 * position. Also check if the Visual position changed.
1285 */
1286 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1287 {
1288 from = curwin->w_cursor.lnum;
1289 to = wp->w_old_cursor_lnum;
1290 }
1291 else
1292 {
1293 from = wp->w_old_cursor_lnum;
1294 to = curwin->w_cursor.lnum;
1295 if (from == 0) /* Visual mode just started */
1296 from = to;
1297 }
1298
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001299 if (VIsual.lnum != wp->w_old_visual_lnum
1300 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 {
1302 if (wp->w_old_visual_lnum < from
1303 && wp->w_old_visual_lnum != 0)
1304 from = wp->w_old_visual_lnum;
1305 if (wp->w_old_visual_lnum > to)
1306 to = wp->w_old_visual_lnum;
1307 if (VIsual.lnum < from)
1308 from = VIsual.lnum;
1309 if (VIsual.lnum > to)
1310 to = VIsual.lnum;
1311 }
1312 }
1313
1314 /*
1315 * If in block mode and changed column or curwin->w_curswant:
1316 * update all lines.
1317 * First compute the actual start and end column.
1318 */
1319 if (VIsual_mode == Ctrl_V)
1320 {
1321 colnr_T fromc, toc;
1322
1323 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1324 ++toc;
1325 if (curwin->w_curswant == MAXCOL)
1326 toc = MAXCOL;
1327
1328 if (fromc != wp->w_old_cursor_fcol
1329 || toc != wp->w_old_cursor_lcol)
1330 {
1331 if (from > VIsual.lnum)
1332 from = VIsual.lnum;
1333 if (to < VIsual.lnum)
1334 to = VIsual.lnum;
1335 }
1336 wp->w_old_cursor_fcol = fromc;
1337 wp->w_old_cursor_lcol = toc;
1338 }
1339 }
1340 else
1341 {
1342 /* Use the line numbers of the old Visual area. */
1343 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1344 {
1345 from = wp->w_old_cursor_lnum;
1346 to = wp->w_old_visual_lnum;
1347 }
1348 else
1349 {
1350 from = wp->w_old_visual_lnum;
1351 to = wp->w_old_cursor_lnum;
1352 }
1353 }
1354
1355 /*
1356 * There is no need to update lines above the top of the window.
1357 */
1358 if (from < wp->w_topline)
1359 from = wp->w_topline;
1360
1361 /*
1362 * If we know the value of w_botline, use it to restrict the update to
1363 * the lines that are visible in the window.
1364 */
1365 if (wp->w_valid & VALID_BOTLINE)
1366 {
1367 if (from >= wp->w_botline)
1368 from = wp->w_botline - 1;
1369 if (to >= wp->w_botline)
1370 to = wp->w_botline - 1;
1371 }
1372
1373 /*
1374 * Find the minimal part to be updated.
1375 * Watch out for scrolling that made entries in w_lines[] invalid.
1376 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1377 * top_end; need to redraw from top_end to the "to" line.
1378 * A middle mouse click with a Visual selection may change the text
1379 * above the Visual area and reset wl_valid, do count these for
1380 * mid_end (in srow).
1381 */
1382 if (mid_start > 0)
1383 {
1384 lnum = wp->w_topline;
1385 idx = 0;
1386 srow = 0;
1387 if (scrolled_down)
1388 mid_start = top_end;
1389 else
1390 mid_start = 0;
1391 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1392 {
1393 if (wp->w_lines[idx].wl_valid)
1394 mid_start += wp->w_lines[idx].wl_size;
1395 else if (!scrolled_down)
1396 srow += wp->w_lines[idx].wl_size;
1397 ++idx;
1398# ifdef FEAT_FOLDING
1399 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1400 lnum = wp->w_lines[idx].wl_lnum;
1401 else
1402# endif
1403 ++lnum;
1404 }
1405 srow += mid_start;
1406 mid_end = wp->w_height;
1407 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1408 {
1409 if (wp->w_lines[idx].wl_valid
1410 && wp->w_lines[idx].wl_lnum >= to + 1)
1411 {
1412 /* Only update until first row of this line */
1413 mid_end = srow;
1414 break;
1415 }
1416 srow += wp->w_lines[idx].wl_size;
1417 }
1418 }
1419 }
1420
1421 if (VIsual_active && buf == curwin->w_buffer)
1422 {
1423 wp->w_old_visual_mode = VIsual_mode;
1424 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1425 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001426 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 wp->w_old_curswant = curwin->w_curswant;
1428 }
1429 else
1430 {
1431 wp->w_old_visual_mode = 0;
1432 wp->w_old_cursor_lnum = 0;
1433 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001434 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
1436#endif /* FEAT_VISUAL */
1437
1438#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1439 /* reset got_int, otherwise regexp won't work */
1440 save_got_int = got_int;
1441 got_int = 0;
1442#endif
1443#ifdef FEAT_FOLDING
1444 win_foldinfo.fi_level = 0;
1445#endif
1446
1447 /*
1448 * Update all the window rows.
1449 */
1450 idx = 0; /* first entry in w_lines[].wl_size */
1451 row = 0;
1452 srow = 0;
1453 lnum = wp->w_topline; /* first line shown in window */
1454 for (;;)
1455 {
1456 /* stop updating when reached the end of the window (check for _past_
1457 * the end of the window is at the end of the loop) */
1458 if (row == wp->w_height)
1459 {
1460 didline = TRUE;
1461 break;
1462 }
1463
1464 /* stop updating when hit the end of the file */
1465 if (lnum > buf->b_ml.ml_line_count)
1466 {
1467 eof = TRUE;
1468 break;
1469 }
1470
1471 /* Remember the starting row of the line that is going to be dealt
1472 * with. It is used further down when the line doesn't fit. */
1473 srow = row;
1474
1475 /*
1476 * Update a line when it is in an area that needs updating, when it
1477 * has changes or w_lines[idx] is invalid.
1478 * bot_start may be halfway a wrapped line after using
1479 * win_del_lines(), check if the current line includes it.
1480 * When syntax folding is being used, the saved syntax states will
1481 * already have been updated, we can't see where the syntax state is
1482 * the same again, just update until the end of the window.
1483 */
1484 if (row < top_end
1485 || (row >= mid_start && row < mid_end)
1486#ifdef FEAT_SEARCH_EXTRA
1487 || top_to_mod
1488#endif
1489 || idx >= wp->w_lines_valid
1490 || (row + wp->w_lines[idx].wl_size > bot_start)
1491 || (mod_top != 0
1492 && (lnum == mod_top
1493 || (lnum >= mod_top
1494 && (lnum < mod_bot
1495#ifdef FEAT_SYN_HL
1496 || did_update == DID_FOLD
1497 || (did_update == DID_LINE
1498 && syntax_present(buf)
1499 && (
1500# ifdef FEAT_FOLDING
1501 (foldmethodIsSyntax(wp)
1502 && hasAnyFolding(wp)) ||
1503# endif
1504 syntax_check_changed(lnum)))
1505#endif
1506 )))))
1507 {
1508#ifdef FEAT_SEARCH_EXTRA
1509 if (lnum == mod_top)
1510 top_to_mod = FALSE;
1511#endif
1512
1513 /*
1514 * When at start of changed lines: May scroll following lines
1515 * up or down to minimize redrawing.
1516 * Don't do this when the change continues until the end.
1517 * Don't scroll when dollar_vcol is non-zero, keep the "$".
1518 */
1519 if (lnum == mod_top
1520 && mod_bot != MAXLNUM
1521 && !(dollar_vcol != 0 && mod_bot == mod_top + 1))
1522 {
1523 int old_rows = 0;
1524 int new_rows = 0;
1525 int xtra_rows;
1526 linenr_T l;
1527
1528 /* Count the old number of window rows, using w_lines[], which
1529 * should still contain the sizes for the lines as they are
1530 * currently displayed. */
1531 for (i = idx; i < wp->w_lines_valid; ++i)
1532 {
1533 /* Only valid lines have a meaningful wl_lnum. Invalid
1534 * lines are part of the changed area. */
1535 if (wp->w_lines[i].wl_valid
1536 && wp->w_lines[i].wl_lnum == mod_bot)
1537 break;
1538 old_rows += wp->w_lines[i].wl_size;
1539#ifdef FEAT_FOLDING
1540 if (wp->w_lines[i].wl_valid
1541 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1542 {
1543 /* Must have found the last valid entry above mod_bot.
1544 * Add following invalid entries. */
1545 ++i;
1546 while (i < wp->w_lines_valid
1547 && !wp->w_lines[i].wl_valid)
1548 old_rows += wp->w_lines[i++].wl_size;
1549 break;
1550 }
1551#endif
1552 }
1553
1554 if (i >= wp->w_lines_valid)
1555 {
1556 /* We can't find a valid line below the changed lines,
1557 * need to redraw until the end of the window.
1558 * Inserting/deleting lines has no use. */
1559 bot_start = 0;
1560 }
1561 else
1562 {
1563 /* Able to count old number of rows: Count new window
1564 * rows, and may insert/delete lines */
1565 j = idx;
1566 for (l = lnum; l < mod_bot; ++l)
1567 {
1568#ifdef FEAT_FOLDING
1569 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1570 ++new_rows;
1571 else
1572#endif
1573#ifdef FEAT_DIFF
1574 if (l == wp->w_topline)
1575 new_rows += plines_win_nofill(wp, l, TRUE)
1576 + wp->w_topfill;
1577 else
1578#endif
1579 new_rows += plines_win(wp, l, TRUE);
1580 ++j;
1581 if (new_rows > wp->w_height - row - 2)
1582 {
1583 /* it's getting too much, must redraw the rest */
1584 new_rows = 9999;
1585 break;
1586 }
1587 }
1588 xtra_rows = new_rows - old_rows;
1589 if (xtra_rows < 0)
1590 {
1591 /* May scroll text up. If there is not enough
1592 * remaining text or scrolling fails, must redraw the
1593 * rest. If scrolling works, must redraw the text
1594 * below the scrolled text. */
1595 if (row - xtra_rows >= wp->w_height - 2)
1596 mod_bot = MAXLNUM;
1597 else
1598 {
1599 check_for_delay(FALSE);
1600 if (win_del_lines(wp, row,
1601 -xtra_rows, FALSE, FALSE) == FAIL)
1602 mod_bot = MAXLNUM;
1603 else
1604 bot_start = wp->w_height + xtra_rows;
1605 }
1606 }
1607 else if (xtra_rows > 0)
1608 {
1609 /* May scroll text down. If there is not enough
1610 * remaining text of scrolling fails, must redraw the
1611 * rest. */
1612 if (row + xtra_rows >= wp->w_height - 2)
1613 mod_bot = MAXLNUM;
1614 else
1615 {
1616 check_for_delay(FALSE);
1617 if (win_ins_lines(wp, row + old_rows,
1618 xtra_rows, FALSE, FALSE) == FAIL)
1619 mod_bot = MAXLNUM;
1620 else if (top_end > row + old_rows)
1621 /* Scrolled the part at the top that requires
1622 * updating down. */
1623 top_end += xtra_rows;
1624 }
1625 }
1626
1627 /* When not updating the rest, may need to move w_lines[]
1628 * entries. */
1629 if (mod_bot != MAXLNUM && i != j)
1630 {
1631 if (j < i)
1632 {
1633 int x = row + new_rows;
1634
1635 /* move entries in w_lines[] upwards */
1636 for (;;)
1637 {
1638 /* stop at last valid entry in w_lines[] */
1639 if (i >= wp->w_lines_valid)
1640 {
1641 wp->w_lines_valid = j;
1642 break;
1643 }
1644 wp->w_lines[j] = wp->w_lines[i];
1645 /* stop at a line that won't fit */
1646 if (x + (int)wp->w_lines[j].wl_size
1647 > wp->w_height)
1648 {
1649 wp->w_lines_valid = j + 1;
1650 break;
1651 }
1652 x += wp->w_lines[j++].wl_size;
1653 ++i;
1654 }
1655 if (bot_start > x)
1656 bot_start = x;
1657 }
1658 else /* j > i */
1659 {
1660 /* move entries in w_lines[] downwards */
1661 j -= i;
1662 wp->w_lines_valid += j;
1663 if (wp->w_lines_valid > wp->w_height)
1664 wp->w_lines_valid = wp->w_height;
1665 for (i = wp->w_lines_valid; i - j >= idx; --i)
1666 wp->w_lines[i] = wp->w_lines[i - j];
1667
1668 /* The w_lines[] entries for inserted lines are
1669 * now invalid, but wl_size may be used above.
1670 * Reset to zero. */
1671 while (i >= idx)
1672 {
1673 wp->w_lines[i].wl_size = 0;
1674 wp->w_lines[i--].wl_valid = FALSE;
1675 }
1676 }
1677 }
1678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 }
1680
1681#ifdef FEAT_FOLDING
1682 /*
1683 * When lines are folded, display one line for all of them.
1684 * Otherwise, display normally (can be several display lines when
1685 * 'wrap' is on).
1686 */
1687 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1688 if (fold_count != 0)
1689 {
1690 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1691 ++row;
1692 --fold_count;
1693 wp->w_lines[idx].wl_folded = TRUE;
1694 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1695# ifdef FEAT_SYN_HL
1696 did_update = DID_FOLD;
1697# endif
1698 }
1699 else
1700#endif
1701 if (idx < wp->w_lines_valid
1702 && wp->w_lines[idx].wl_valid
1703 && wp->w_lines[idx].wl_lnum == lnum
1704 && lnum > wp->w_topline
1705 && !(dy_flags & DY_LASTLINE)
1706 && srow + wp->w_lines[idx].wl_size > wp->w_height
1707#ifdef FEAT_DIFF
1708 && diff_check_fill(wp, lnum) == 0
1709#endif
1710 )
1711 {
1712 /* This line is not going to fit. Don't draw anything here,
1713 * will draw "@ " lines below. */
1714 row = wp->w_height + 1;
1715 }
1716 else
1717 {
1718#ifdef FEAT_SEARCH_EXTRA
1719 prepare_search_hl(wp, lnum);
1720#endif
1721#ifdef FEAT_SYN_HL
1722 /* Let the syntax stuff know we skipped a few lines. */
1723 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
1724 && syntax_present(buf))
1725 syntax_end_parsing(syntax_last_parsed + 1);
1726#endif
1727
1728 /*
1729 * Display one line.
1730 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001731 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733#ifdef FEAT_FOLDING
1734 wp->w_lines[idx].wl_folded = FALSE;
1735 wp->w_lines[idx].wl_lastlnum = lnum;
1736#endif
1737#ifdef FEAT_SYN_HL
1738 did_update = DID_LINE;
1739 syntax_last_parsed = lnum;
1740#endif
1741 }
1742
1743 wp->w_lines[idx].wl_lnum = lnum;
1744 wp->w_lines[idx].wl_valid = TRUE;
1745 if (row > wp->w_height) /* past end of screen */
1746 {
1747 /* we may need the size of that too long line later on */
1748 if (dollar_vcol == 0)
1749 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
1750 ++idx;
1751 break;
1752 }
1753 if (dollar_vcol == 0)
1754 wp->w_lines[idx].wl_size = row - srow;
1755 ++idx;
1756#ifdef FEAT_FOLDING
1757 lnum += fold_count + 1;
1758#else
1759 ++lnum;
1760#endif
1761 }
1762 else
1763 {
1764 /* This line does not need updating, advance to the next one */
1765 row += wp->w_lines[idx++].wl_size;
1766 if (row > wp->w_height) /* past end of screen */
1767 break;
1768#ifdef FEAT_FOLDING
1769 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
1770#else
1771 ++lnum;
1772#endif
1773#ifdef FEAT_SYN_HL
1774 did_update = DID_NONE;
1775#endif
1776 }
1777
1778 if (lnum > buf->b_ml.ml_line_count)
1779 {
1780 eof = TRUE;
1781 break;
1782 }
1783 }
1784 /*
1785 * End of loop over all window lines.
1786 */
1787
1788
1789 if (idx > wp->w_lines_valid)
1790 wp->w_lines_valid = idx;
1791
1792#ifdef FEAT_SYN_HL
1793 /*
1794 * Let the syntax stuff know we stop parsing here.
1795 */
1796 if (syntax_last_parsed != 0 && syntax_present(buf))
1797 syntax_end_parsing(syntax_last_parsed + 1);
1798#endif
1799
1800 /*
1801 * If we didn't hit the end of the file, and we didn't finish the last
1802 * line we were working on, then the line didn't fit.
1803 */
1804 wp->w_empty_rows = 0;
1805#ifdef FEAT_DIFF
1806 wp->w_filler_rows = 0;
1807#endif
1808 if (!eof && !didline)
1809 {
1810 if (lnum == wp->w_topline)
1811 {
1812 /*
1813 * Single line that does not fit!
1814 * Don't overwrite it, it can be edited.
1815 */
1816 wp->w_botline = lnum + 1;
1817 }
1818#ifdef FEAT_DIFF
1819 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
1820 {
1821 /* Window ends in filler lines. */
1822 wp->w_botline = lnum;
1823 wp->w_filler_rows = wp->w_height - srow;
1824 }
1825#endif
1826 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
1827 {
1828 /*
1829 * Last line isn't finished: Display "@@@" at the end.
1830 */
1831 screen_fill(W_WINROW(wp) + wp->w_height - 1,
1832 W_WINROW(wp) + wp->w_height,
1833 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
1834 '@', '@', hl_attr(HLF_AT));
1835 set_empty_rows(wp, srow);
1836 wp->w_botline = lnum;
1837 }
1838 else
1839 {
1840 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
1841 wp->w_botline = lnum;
1842 }
1843 }
1844 else
1845 {
1846#ifdef FEAT_VERTSPLIT
1847 draw_vsep_win(wp, row);
1848#endif
1849 if (eof) /* we hit the end of the file */
1850 {
1851 wp->w_botline = buf->b_ml.ml_line_count + 1;
1852#ifdef FEAT_DIFF
1853 j = diff_check_fill(wp, wp->w_botline);
1854 if (j > 0 && !wp->w_botfill)
1855 {
1856 /*
1857 * Display filler lines at the end of the file
1858 */
1859 if (char2cells(fill_diff) > 1)
1860 i = '-';
1861 else
1862 i = fill_diff;
1863 if (row + j > wp->w_height)
1864 j = wp->w_height - row;
1865 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
1866 row += j;
1867 }
1868#endif
1869 }
1870 else if (dollar_vcol == 0)
1871 wp->w_botline = lnum;
1872
1873 /* make sure the rest of the screen is blank */
1874 /* put '~'s on rows that aren't part of the file. */
1875 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
1876 }
1877
1878 /* Reset the type of redrawing required, the window has been updated. */
1879 wp->w_redr_type = 0;
1880#ifdef FEAT_DIFF
1881 wp->w_old_topfill = wp->w_topfill;
1882 wp->w_old_botfill = wp->w_botfill;
1883#endif
1884
1885 if (dollar_vcol == 0)
1886 {
1887 /*
1888 * There is a trick with w_botline. If we invalidate it on each
1889 * change that might modify it, this will cause a lot of expensive
1890 * calls to plines() in update_topline() each time. Therefore the
1891 * value of w_botline is often approximated, and this value is used to
1892 * compute the value of w_topline. If the value of w_botline was
1893 * wrong, check that the value of w_topline is correct (cursor is on
1894 * the visible part of the text). If it's not, we need to redraw
1895 * again. Mostly this just means scrolling up a few lines, so it
1896 * doesn't look too bad. Only do this for the current window (where
1897 * changes are relevant).
1898 */
1899 wp->w_valid |= VALID_BOTLINE;
1900 if (wp == curwin && wp->w_botline != old_botline && !recursive)
1901 {
1902 recursive = TRUE;
1903 curwin->w_valid &= ~VALID_TOPLINE;
1904 update_topline(); /* may invalidate w_botline again */
1905 if (must_redraw != 0)
1906 {
1907 /* Don't update for changes in buffer again. */
1908 i = curbuf->b_mod_set;
1909 curbuf->b_mod_set = FALSE;
1910 win_update(curwin);
1911 must_redraw = 0;
1912 curbuf->b_mod_set = i;
1913 }
1914 recursive = FALSE;
1915 }
1916 }
1917
1918#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1919 /* restore got_int, unless CTRL-C was hit while redrawing */
1920 if (!got_int)
1921 got_int = save_got_int;
1922#endif
1923}
1924
1925#ifdef FEAT_SIGNS
1926static int draw_signcolumn __ARGS((win_T *wp));
1927
1928/*
1929 * Return TRUE when window "wp" has a column to draw signs in.
1930 */
1931 static int
1932draw_signcolumn(wp)
1933 win_T *wp;
1934{
1935 return (wp->w_buffer->b_signlist != NULL
1936# ifdef FEAT_NETBEANS_INTG
1937 || usingNetbeans
1938# endif
1939 );
1940}
1941#endif
1942
1943/*
1944 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
1945 * as the filler character.
1946 */
1947 static void
1948win_draw_end(wp, c1, c2, row, endrow, hl)
1949 win_T *wp;
1950 int c1;
1951 int c2;
1952 int row;
1953 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001954 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
1956#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
1957 int n = 0;
1958# define FDC_OFF n
1959#else
1960# define FDC_OFF 0
1961#endif
1962
1963#ifdef FEAT_RIGHTLEFT
1964 if (wp->w_p_rl)
1965 {
1966 /* No check for cmdline window: should never be right-left. */
1967# ifdef FEAT_FOLDING
1968 n = wp->w_p_fdc;
1969
1970 if (n > 0)
1971 {
1972 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001973 if (n > W_WIDTH(wp))
1974 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1976 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
1977 ' ', ' ', hl_attr(HLF_FC));
1978 }
1979# endif
1980# ifdef FEAT_SIGNS
1981 if (draw_signcolumn(wp))
1982 {
1983 int nn = n + 2;
1984
1985 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00001986 if (nn > W_WIDTH(wp))
1987 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1989 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
1990 ' ', ' ', hl_attr(HLF_SC));
1991 n = nn;
1992 }
1993# endif
1994 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1995 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
1996 c2, c2, hl_attr(hl));
1997 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
1998 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
1999 c1, c2, hl_attr(hl));
2000 }
2001 else
2002#endif
2003 {
2004#ifdef FEAT_CMDWIN
2005 if (cmdwin_type != 0 && wp == curwin)
2006 {
2007 /* draw the cmdline character in the leftmost column */
2008 n = 1;
2009 if (n > wp->w_width)
2010 n = wp->w_width;
2011 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2012 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2013 cmdwin_type, ' ', hl_attr(HLF_AT));
2014 }
2015#endif
2016#ifdef FEAT_FOLDING
2017 if (wp->w_p_fdc > 0)
2018 {
2019 int nn = n + wp->w_p_fdc;
2020
2021 /* draw the fold column at the left */
2022 if (nn > W_WIDTH(wp))
2023 nn = W_WIDTH(wp);
2024 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2025 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2026 ' ', ' ', hl_attr(HLF_FC));
2027 n = nn;
2028 }
2029#endif
2030#ifdef FEAT_SIGNS
2031 if (draw_signcolumn(wp))
2032 {
2033 int nn = n + 2;
2034
2035 /* draw the sign column after the fold column */
2036 if (nn > W_WIDTH(wp))
2037 nn = W_WIDTH(wp);
2038 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2039 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2040 ' ', ' ', hl_attr(HLF_SC));
2041 n = nn;
2042 }
2043#endif
2044 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2045 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2046 c1, c2, hl_attr(hl));
2047 }
2048 set_empty_rows(wp, row);
2049}
2050
2051#ifdef FEAT_FOLDING
2052/*
2053 * Display one folded line.
2054 */
2055 static void
2056fold_line(wp, fold_count, foldinfo, lnum, row)
2057 win_T *wp;
2058 long fold_count;
2059 foldinfo_T *foldinfo;
2060 linenr_T lnum;
2061 int row;
2062{
2063 char_u buf[51];
2064 pos_T *top, *bot;
2065 linenr_T lnume = lnum + fold_count - 1;
2066 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002067 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 int col;
2070 int txtcol;
2071 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002072 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 /* Build the fold line:
2075 * 1. Add the cmdwin_type for the command-line window
2076 * 2. Add the 'foldcolumn'
2077 * 3. Add the 'number' column
2078 * 4. Compose the text
2079 * 5. Add the text
2080 * 6. set highlighting for the Visual area an other text
2081 */
2082 col = 0;
2083
2084 /*
2085 * 1. Add the cmdwin_type for the command-line window
2086 * Ignores 'rightleft', this window is never right-left.
2087 */
2088#ifdef FEAT_CMDWIN
2089 if (cmdwin_type != 0 && wp == curwin)
2090 {
2091 ScreenLines[off] = cmdwin_type;
2092 ScreenAttrs[off] = hl_attr(HLF_AT);
2093#ifdef FEAT_MBYTE
2094 if (enc_utf8)
2095 ScreenLinesUC[off] = 0;
2096#endif
2097 ++col;
2098 }
2099#endif
2100
2101 /*
2102 * 2. Add the 'foldcolumn'
2103 */
2104 fdc = wp->w_p_fdc;
2105 if (fdc > W_WIDTH(wp) - col)
2106 fdc = W_WIDTH(wp) - col;
2107 if (fdc > 0)
2108 {
2109 fill_foldcolumn(buf, wp, TRUE, lnum);
2110#ifdef FEAT_RIGHTLEFT
2111 if (wp->w_p_rl)
2112 {
2113 int i;
2114
2115 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2116 hl_attr(HLF_FC));
2117 /* reverse the fold column */
2118 for (i = 0; i < fdc; ++i)
2119 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2120 }
2121 else
2122#endif
2123 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2124 col += fdc;
2125 }
2126
2127#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002128# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2129 for (ri = 0; ri < l; ++ri) \
2130 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2131 else \
2132 for (ri = 0; ri < l; ++ri) \
2133 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002135# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2136 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137#endif
2138
2139 /* Set all attributes of the 'number' column and the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002140 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142#ifdef FEAT_SIGNS
2143 /* If signs are being displayed, add two spaces. */
2144 if (draw_signcolumn(wp))
2145 {
2146 len = W_WIDTH(wp) - col;
2147 if (len > 0)
2148 {
2149 if (len > 2)
2150 len = 2;
2151# ifdef FEAT_RIGHTLEFT
2152 if (wp->w_p_rl)
2153 /* the line number isn't reversed */
2154 copy_text_attr(off + W_WIDTH(wp) - len - col,
2155 (char_u *)" ", len, hl_attr(HLF_FL));
2156 else
2157# endif
2158 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2159 col += len;
2160 }
2161 }
2162#endif
2163
2164 /*
2165 * 3. Add the 'number' column
2166 */
2167 if (wp->w_p_nu)
2168 {
2169 len = W_WIDTH(wp) - col;
2170 if (len > 0)
2171 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002172 int w = number_width(wp);
2173
2174 if (len > w + 1)
2175 len = w + 1;
2176 sprintf((char *)buf, "%*ld ", w, (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#ifdef FEAT_RIGHTLEFT
2178 if (wp->w_p_rl)
2179 /* the line number isn't reversed */
2180 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2181 hl_attr(HLF_FL));
2182 else
2183#endif
2184 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2185 col += len;
2186 }
2187 }
2188
2189 /*
2190 * 4. Compose the folded-line string with 'foldtext', if set.
2191 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002192 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 txtcol = col; /* remember where text starts */
2195
2196 /*
2197 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2198 * Right-left text is put in columns 0 - number-col, normal text is put
2199 * in columns number-col - window-width.
2200 */
2201#ifdef FEAT_MBYTE
2202 if (has_mbyte)
2203 {
2204 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002205 int u8c, u8cc[MAX_MCO];
2206 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 int idx;
2208 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002209 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210# ifdef FEAT_ARABIC
2211 int prev_c = 0; /* previous Arabic character */
2212 int prev_c1 = 0; /* first composing char for prev_c */
2213# endif
2214
2215# ifdef FEAT_RIGHTLEFT
2216 if (wp->w_p_rl)
2217 idx = off;
2218 else
2219# endif
2220 idx = off + col;
2221
2222 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2223 for (p = text; *p != NUL; )
2224 {
2225 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002226 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 if (col + cells > W_WIDTH(wp)
2228# ifdef FEAT_RIGHTLEFT
2229 - (wp->w_p_rl ? col : 0)
2230# endif
2231 )
2232 break;
2233 ScreenLines[idx] = *p;
2234 if (enc_utf8)
2235 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002236 u8c = utfc_ptr2char(p, u8cc);
2237 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 ScreenLinesUC[idx] = 0;
2240#ifdef FEAT_ARABIC
2241 prev_c = u8c;
2242#endif
2243 }
2244 else
2245 {
2246#ifdef FEAT_ARABIC
2247 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2248 {
2249 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002250 int pc, pc1, nc;
2251 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 int firstbyte = *p;
2253
2254 /* The idea of what is the previous and next
2255 * character depends on 'rightleft'. */
2256 if (wp->w_p_rl)
2257 {
2258 pc = prev_c;
2259 pc1 = prev_c1;
2260 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002261 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263 else
2264 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002265 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002267 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
2269 prev_c = u8c;
2270
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002271 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 pc, pc1, nc);
2273 ScreenLines[idx] = firstbyte;
2274 }
2275 else
2276 prev_c = u8c;
2277#endif
2278 /* Non-BMP character: display as ? or fullwidth ?. */
2279 if (u8c >= 0x10000)
2280 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2281 else
2282 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002283 for (i = 0; i < Screen_mco; ++i)
2284 {
2285 ScreenLinesC[i][idx] = u8cc[i];
2286 if (u8cc[i] == 0)
2287 break;
2288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290 if (cells > 1)
2291 ScreenLines[idx + 1] = 0;
2292 }
2293 else if (cells > 1) /* double-byte character */
2294 {
2295 if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2296 ScreenLines2[idx] = p[1];
2297 else
2298 ScreenLines[idx + 1] = p[1];
2299 }
2300 col += cells;
2301 idx += cells;
2302 p += c_len;
2303 }
2304 }
2305 else
2306#endif
2307 {
2308 len = (int)STRLEN(text);
2309 if (len > W_WIDTH(wp) - col)
2310 len = W_WIDTH(wp) - col;
2311 if (len > 0)
2312 {
2313#ifdef FEAT_RIGHTLEFT
2314 if (wp->w_p_rl)
2315 STRNCPY(current_ScreenLine, text, len);
2316 else
2317#endif
2318 STRNCPY(current_ScreenLine + col, text, len);
2319 col += len;
2320 }
2321 }
2322
2323 /* Fill the rest of the line with the fold filler */
2324#ifdef FEAT_RIGHTLEFT
2325 if (wp->w_p_rl)
2326 col -= txtcol;
2327#endif
2328 while (col < W_WIDTH(wp)
2329#ifdef FEAT_RIGHTLEFT
2330 - (wp->w_p_rl ? txtcol : 0)
2331#endif
2332 )
2333 {
2334#ifdef FEAT_MBYTE
2335 if (enc_utf8)
2336 {
2337 if (fill_fold >= 0x80)
2338 {
2339 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002340 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342 else
2343 ScreenLinesUC[off + col] = 0;
2344 }
2345#endif
2346 ScreenLines[off + col++] = fill_fold;
2347 }
2348
2349 if (text != buf)
2350 vim_free(text);
2351
2352 /*
2353 * 6. set highlighting for the Visual area an other text.
2354 * If all folded lines are in the Visual area, highlight the line.
2355 */
2356#ifdef FEAT_VISUAL
2357 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2358 {
2359 if (ltoreq(curwin->w_cursor, VIsual))
2360 {
2361 /* Visual is after curwin->w_cursor */
2362 top = &curwin->w_cursor;
2363 bot = &VIsual;
2364 }
2365 else
2366 {
2367 /* Visual is before curwin->w_cursor */
2368 top = &VIsual;
2369 bot = &curwin->w_cursor;
2370 }
2371 if (lnum >= top->lnum
2372 && lnume <= bot->lnum
2373 && (VIsual_mode != 'v'
2374 || ((lnum > top->lnum
2375 || (lnum == top->lnum
2376 && top->col == 0))
2377 && (lnume < bot->lnum
2378 || (lnume == bot->lnum
2379 && (bot->col - (*p_sel == 'e'))
2380 >= STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
2381 {
2382 if (VIsual_mode == Ctrl_V)
2383 {
2384 /* Visual block mode: highlight the chars part of the block */
2385 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2386 {
2387 if (wp->w_old_cursor_lcol + txtcol < (colnr_T)W_WIDTH(wp))
2388 len = wp->w_old_cursor_lcol;
2389 else
2390 len = W_WIDTH(wp) - txtcol;
2391 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002392 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394 }
2395 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002398 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401 }
2402#endif
2403
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002404#ifdef FEAT_SYN_HL
2405 /* Show 'cursorcolumn' in the fold line. */
2406 if (wp->w_p_cuc && (int)wp->w_virtcol + txtcol < W_WIDTH(wp))
2407 ScreenAttrs[off + wp->w_virtcol + txtcol] = hl_combine_attr(
2408 ScreenAttrs[off + wp->w_virtcol + txtcol], hl_attr(HLF_CUC));
2409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2412 (int)W_WIDTH(wp), FALSE);
2413
2414 /*
2415 * Update w_cline_height and w_cline_folded if the cursor line was
2416 * updated (saves a call to plines() later).
2417 */
2418 if (wp == curwin
2419 && lnum <= curwin->w_cursor.lnum
2420 && lnume >= curwin->w_cursor.lnum)
2421 {
2422 curwin->w_cline_row = row;
2423 curwin->w_cline_height = 1;
2424 curwin->w_cline_folded = TRUE;
2425 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2426 }
2427}
2428
2429/*
2430 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2431 */
2432 static void
2433copy_text_attr(off, buf, len, attr)
2434 int off;
2435 char_u *buf;
2436 int len;
2437 int attr;
2438{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002439 int i;
2440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 mch_memmove(ScreenLines + off, buf, (size_t)len);
2442# ifdef FEAT_MBYTE
2443 if (enc_utf8)
2444 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2445# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002446 for (i = 0; i < len; ++i)
2447 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448}
2449
2450/*
2451 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002452 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
2454 static void
2455fill_foldcolumn(p, wp, closed, lnum)
2456 char_u *p;
2457 win_T *wp;
2458 int closed; /* TRUE of FALSE */
2459 linenr_T lnum; /* current line number */
2460{
2461 int i = 0;
2462 int level;
2463 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002464 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 /* Init to all spaces. */
2467 copy_spaces(p, (size_t)wp->w_p_fdc);
2468
2469 level = win_foldinfo.fi_level;
2470 if (level > 0)
2471 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002472 /* If there is only one column put more info in it. */
2473 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 /* If the column is too narrow, we start at the lowest level that
2476 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002477 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 if (first_level < 1)
2479 first_level = 1;
2480
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002481 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
2483 if (win_foldinfo.fi_lnum == lnum
2484 && first_level + i >= win_foldinfo.fi_low_level)
2485 p[i] = '-';
2486 else if (first_level == 1)
2487 p[i] = '|';
2488 else if (first_level + i <= 9)
2489 p[i] = '0' + first_level + i;
2490 else
2491 p[i] = '>';
2492 if (first_level + i == level)
2493 break;
2494 }
2495 }
2496 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002497 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498}
2499#endif /* FEAT_FOLDING */
2500
2501/*
2502 * Display line "lnum" of window 'wp' on the screen.
2503 * Start at row "startrow", stop when "endrow" is reached.
2504 * wp->w_virtcol needs to be valid.
2505 *
2506 * Return the number of last row the line occupies.
2507 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002508/* ARGSUSED */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002510win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 win_T *wp;
2512 linenr_T lnum;
2513 int startrow;
2514 int endrow;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002515 int nochange; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 int col; /* visual column on screen */
2518 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2519 int c = 0; /* init for GCC */
2520 long vcol = 0; /* virtual column (for tabs) */
2521 long vcol_prev = -1; /* "vcol" of previous character */
2522 char_u *line; /* current line */
2523 char_u *ptr; /* current position in "line" */
2524 int row; /* row in the window, excl w_winrow */
2525 int screen_row; /* row on the screen, incl w_winrow */
2526
2527 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2528 int n_extra = 0; /* number of extra chars */
2529 char_u *p_extra = NULL; /* string of extra chars */
2530 int c_extra = NUL; /* extra chars, all the same */
2531 int extra_attr = 0; /* attributes when n_extra != 0 */
2532 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2533 displaying lcs_eol at end-of-line */
2534 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2535 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2536
2537 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2538 int saved_n_extra = 0;
2539 char_u *saved_p_extra = NULL;
2540 int saved_c_extra = 0;
2541 int saved_char_attr = 0;
2542
2543 int n_attr = 0; /* chars with special attr */
2544 int saved_attr2 = 0; /* char_attr saved for n_attr */
2545 int n_attr3 = 0; /* chars with overruling special attr */
2546 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2547
2548 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2549
2550 int fromcol, tocol; /* start/end of inverting */
2551 int fromcol_prev = -2; /* start of inverting after cursor */
2552 int noinvcur = FALSE; /* don't invert the cursor */
2553#ifdef FEAT_VISUAL
2554 pos_T *top, *bot;
2555#endif
2556 pos_T pos;
2557 long v;
2558
2559 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002560 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2562 in this line */
2563 int attr = 0; /* attributes for area highlighting */
2564 int area_attr = 0; /* attributes desired by highlighting */
2565 int search_attr = 0; /* attributes desired by 'hlsearch' */
2566#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002567 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 int syntax_attr = 0; /* attributes desired by syntax */
2569 int has_syntax = FALSE; /* this buffer has syntax highl. */
2570 int save_did_emsg;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002571#endif
2572#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002573 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002574# define SPWORDLEN 150
2575 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002576 int nextlinecol = 0; /* column where nextline[] starts */
2577 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002578 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002579 int spell_attr = 0; /* attributes desired by spelling */
2580 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002581 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2582 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002583 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002584 static int cap_col = -1; /* column to check for Cap word */
2585 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002586 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587#endif
2588 int extra_check; /* has syntax or linebreak */
2589#ifdef FEAT_MBYTE
2590 int multi_attr = 0; /* attributes desired by multibyte */
2591 int mb_l = 1; /* multi-byte byte length */
2592 int mb_c = 0; /* decoded multi-byte character */
2593 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002594 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595#endif
2596#ifdef FEAT_DIFF
2597 int filler_lines; /* nr of filler lines to be drawn */
2598 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002599 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 int change_start = MAXCOL; /* first col of changed area */
2601 int change_end = -1; /* last col of changed area */
2602#endif
2603 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2604#ifdef FEAT_LINEBREAK
2605 int need_showbreak = FALSE;
2606#endif
2607#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS))
2608# define LINE_ATTR
2609 int line_attr = 0; /* atrribute for the whole line */
2610#endif
2611#ifdef FEAT_SEARCH_EXTRA
2612 match_T *shl; /* points to search_hl or match_hl */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002613#endif
2614#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_MBYTE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002615 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#endif
2617#ifdef FEAT_ARABIC
2618 int prev_c = 0; /* previous Arabic character */
2619 int prev_c1 = 0; /* first composing char for prev_c */
2620#endif
2621
2622 /* draw_state: items that are drawn in sequence: */
2623#define WL_START 0 /* nothing done yet */
2624#ifdef FEAT_CMDWIN
2625# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2626#else
2627# define WL_CMDLINE WL_START
2628#endif
2629#ifdef FEAT_FOLDING
2630# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2631#else
2632# define WL_FOLD WL_CMDLINE
2633#endif
2634#ifdef FEAT_SIGNS
2635# define WL_SIGN WL_FOLD + 1 /* column for signs */
2636#else
2637# define WL_SIGN WL_FOLD /* column for signs */
2638#endif
2639#define WL_NR WL_SIGN + 1 /* line number */
2640#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2641# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2642#else
2643# define WL_SBR WL_NR
2644#endif
2645#define WL_LINE WL_SBR + 1 /* text in the line */
2646 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002647#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 int feedback_col = 0;
2649 int feedback_old_attr = -1;
2650#endif
2651
2652
2653 if (startrow > endrow) /* past the end already! */
2654 return startrow;
2655
2656 row = startrow;
2657 screen_row = row + W_WINROW(wp);
2658
2659 /*
2660 * To speed up the loop below, set extra_check when there is linebreak,
2661 * trailing white space and/or syntax processing to be done.
2662 */
2663#ifdef FEAT_LINEBREAK
2664 extra_check = wp->w_p_lbr;
2665#else
2666 extra_check = 0;
2667#endif
2668#ifdef FEAT_SYN_HL
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002669 if (syntax_present(wp->w_buffer) && !wp->w_buffer->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
2671 /* Prepare for syntax highlighting in this line. When there is an
2672 * error, stop syntax highlighting. */
2673 save_did_emsg = did_emsg;
2674 did_emsg = FALSE;
2675 syntax_start(wp, lnum);
2676 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00002677 wp->w_buffer->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 else
2679 {
2680 did_emsg = save_did_emsg;
2681 has_syntax = TRUE;
2682 extra_check = TRUE;
2683 }
2684 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002685#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00002686
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002687#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00002688 if (wp->w_p_spell
2689 && *wp->w_buffer->b_p_spl != NUL
2690 && wp->w_buffer->b_langp.ga_len > 0
2691 && *(char **)(wp->w_buffer->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00002692 {
2693 /* Prepare for spell checking. */
2694 has_spell = TRUE;
2695 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00002696
2697 /* Get the start of the next line, so that words that wrap to the next
2698 * line are found too: "et<line-break>al.".
2699 * Trick: skip a few chars for C/shell/Vim comments */
2700 nextline[SPWORDLEN] = NUL;
2701 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2702 {
2703 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
2704 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
2705 }
2706
2707 /* When a word wrapped from the previous line the start of the current
2708 * line is valid. */
2709 if (lnum == checked_lnum)
2710 cur_checked_col = checked_col;
2711 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002712
2713 /* When there was a sentence end in the previous line may require a
2714 * word starting with capital in this line. In line 1 always check
2715 * the first word. */
2716 if (lnum != capcol_lnum)
2717 cap_col = -1;
2718 if (lnum == 1)
2719 cap_col = 0;
2720 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00002721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722#endif
2723
2724 /*
2725 * handle visual active in this window
2726 */
2727 fromcol = -10;
2728 tocol = MAXCOL;
2729#ifdef FEAT_VISUAL
2730 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2731 {
2732 /* Visual is after curwin->w_cursor */
2733 if (ltoreq(curwin->w_cursor, VIsual))
2734 {
2735 top = &curwin->w_cursor;
2736 bot = &VIsual;
2737 }
2738 else /* Visual is before curwin->w_cursor */
2739 {
2740 top = &VIsual;
2741 bot = &curwin->w_cursor;
2742 }
2743 if (VIsual_mode == Ctrl_V) /* block mode */
2744 {
2745 if (lnum >= top->lnum && lnum <= bot->lnum)
2746 {
2747 fromcol = wp->w_old_cursor_fcol;
2748 tocol = wp->w_old_cursor_lcol;
2749 }
2750 }
2751 else /* non-block mode */
2752 {
2753 if (lnum > top->lnum && lnum <= bot->lnum)
2754 fromcol = 0;
2755 else if (lnum == top->lnum)
2756 {
2757 if (VIsual_mode == 'V') /* linewise */
2758 fromcol = 0;
2759 else
2760 {
2761 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
2762 if (gchar_pos(top) == NUL)
2763 tocol = fromcol + 1;
2764 }
2765 }
2766 if (VIsual_mode != 'V' && lnum == bot->lnum)
2767 {
2768 if (*p_sel == 'e' && bot->col == 0
2769#ifdef FEAT_VIRTUALEDIT
2770 && bot->coladd == 0
2771#endif
2772 )
2773 {
2774 fromcol = -10;
2775 tocol = MAXCOL;
2776 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002777 else if (bot->col == MAXCOL)
2778 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else
2780 {
2781 pos = *bot;
2782 if (*p_sel == 'e')
2783 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
2784 else
2785 {
2786 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
2787 ++tocol;
2788 }
2789 }
2790 }
2791 }
2792
2793#ifndef MSDOS
2794 /* Check if the character under the cursor should not be inverted */
2795 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
2796# ifdef FEAT_GUI
2797 && !gui.in_use
2798# endif
2799 )
2800 noinvcur = TRUE;
2801#endif
2802
2803 /* if inverting in this line set area_highlighting */
2804 if (fromcol >= 0)
2805 {
2806 area_highlighting = TRUE;
2807 attr = hl_attr(HLF_V);
2808#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
2809 if (clip_star.available && !clip_star.owned && clip_isautosel())
2810 attr = hl_attr(HLF_VNC);
2811#endif
2812 }
2813 }
2814
2815 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002816 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 */
2818 else
2819#endif /* FEAT_VISUAL */
2820 if (highlight_match
2821 && wp == curwin
2822 && lnum >= curwin->w_cursor.lnum
2823 && lnum <= curwin->w_cursor.lnum + search_match_lines)
2824 {
2825 if (lnum == curwin->w_cursor.lnum)
2826 getvcol(curwin, &(curwin->w_cursor),
2827 (colnr_T *)&fromcol, NULL, NULL);
2828 else
2829 fromcol = 0;
2830 if (lnum == curwin->w_cursor.lnum + search_match_lines)
2831 {
2832 pos.lnum = lnum;
2833 pos.col = search_match_endcol;
2834 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
2835 }
2836 else
2837 tocol = MAXCOL;
2838 if (fromcol == tocol) /* do at least one character */
2839 tocol = fromcol + 1; /* happens when past end of line */
2840 area_highlighting = TRUE;
2841 attr = hl_attr(HLF_I);
2842 }
2843
2844#ifdef FEAT_DIFF
2845 filler_lines = diff_check(wp, lnum);
2846 if (filler_lines < 0)
2847 {
2848 if (filler_lines == -1)
2849 {
2850 if (diff_find_change(wp, lnum, &change_start, &change_end))
2851 diff_hlf = HLF_ADD; /* added line */
2852 else if (change_start == 0)
2853 diff_hlf = HLF_TXD; /* changed text */
2854 else
2855 diff_hlf = HLF_CHD; /* changed line */
2856 }
2857 else
2858 diff_hlf = HLF_ADD; /* added line */
2859 filler_lines = 0;
2860 area_highlighting = TRUE;
2861 }
2862 if (lnum == wp->w_topline)
2863 filler_lines = wp->w_topfill;
2864 filler_todo = filler_lines;
2865#endif
2866
2867#ifdef LINE_ATTR
2868# ifdef FEAT_SIGNS
2869 /* If this line has a sign with line highlighting set line_attr. */
2870 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
2871 if (v != 0)
2872 line_attr = sign_get_attr((int)v, TRUE);
2873# endif
2874# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
2875 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002876 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 line_attr = hl_attr(HLF_L);
2878# endif
2879 if (line_attr != 0)
2880 area_highlighting = TRUE;
2881#endif
2882
2883 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2884 ptr = line;
2885
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002886#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00002887 if (has_spell)
2888 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002889 /* For checking first word with a capital skip white space. */
2890 if (cap_col == 0)
2891 cap_col = skipwhite(line) - line;
2892
Bram Moolenaar30abd282005-06-22 22:35:10 +00002893 /* To be able to spell-check over line boundaries copy the end of the
2894 * current line into nextline[]. Above the start of the next line was
2895 * copied to nextline[SPWORDLEN]. */
2896 if (nextline[SPWORDLEN] == NUL)
2897 {
2898 /* No next line or it is empty. */
2899 nextlinecol = MAXCOL;
2900 nextline_idx = 0;
2901 }
2902 else
2903 {
2904 v = STRLEN(line);
2905 if (v < SPWORDLEN)
2906 {
2907 /* Short line, use it completely and append the start of the
2908 * next line. */
2909 nextlinecol = 0;
2910 mch_memmove(nextline, line, (size_t)v);
2911 mch_memmove(nextline + v, nextline + SPWORDLEN,
2912 STRLEN(nextline + SPWORDLEN) + 1);
2913 nextline_idx = v + 1;
2914 }
2915 else
2916 {
2917 /* Long line, use only the last SPWORDLEN bytes. */
2918 nextlinecol = v - SPWORDLEN;
2919 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
2920 nextline_idx = SPWORDLEN + 1;
2921 }
2922 }
2923 }
2924#endif
2925
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 /* find start of trailing whitespace */
2927 if (wp->w_p_list && lcs_trail)
2928 {
2929 trailcol = (colnr_T)STRLEN(ptr);
2930 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
2931 --trailcol;
2932 trailcol += (colnr_T) (ptr - line);
2933 extra_check = TRUE;
2934 }
2935
2936 /*
2937 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
2938 * first character to be displayed.
2939 */
2940 if (wp->w_p_wrap)
2941 v = wp->w_skipcol;
2942 else
2943 v = wp->w_leftcol;
2944 if (v > 0)
2945 {
2946#ifdef FEAT_MBYTE
2947 char_u *prev_ptr = ptr;
2948#endif
2949 while (vcol < v && *ptr != NUL)
2950 {
2951 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
2952 vcol += c;
2953#ifdef FEAT_MBYTE
2954 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002956 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
2958
2959#ifdef FEAT_VIRTUALEDIT
2960 /* When 'virtualedit' is set the end of the line may be before the
2961 * start of the displayed part. */
2962 if (vcol < v && *ptr == NUL && virtual_active())
2963 vcol = v;
2964#endif
2965
2966 /* Handle a character that's not completely on the screen: Put ptr at
2967 * that character but skip the first few screen characters. */
2968 if (vcol > v)
2969 {
2970 vcol -= c;
2971#ifdef FEAT_MBYTE
2972 ptr = prev_ptr;
2973#else
2974 --ptr;
2975#endif
2976 n_skip = v - vcol;
2977 }
2978
2979 /*
2980 * Adjust for when the inverted text is before the screen,
2981 * and when the start of the inverted text is before the screen.
2982 */
2983 if (tocol <= vcol)
2984 fromcol = 0;
2985 else if (fromcol >= 0 && fromcol < vcol)
2986 fromcol = vcol;
2987
2988#ifdef FEAT_LINEBREAK
2989 /* When w_skipcol is non-zero, first line needs 'showbreak' */
2990 if (wp->w_p_wrap)
2991 need_showbreak = TRUE;
2992#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002993#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00002994 /* When spell checking a word we need to figure out the start of the
2995 * word and if it's badly spelled or not. */
2996 if (has_spell)
2997 {
2998 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002999 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003000
3001 pos = wp->w_cursor;
3002 wp->w_cursor.lnum = lnum;
3003 wp->w_cursor.col = ptr - line;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003004 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003005 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003006 {
3007 /* no bad word found at line start, don't check until end of a
3008 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003009 spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003010 word_end = spell_to_word_end(ptr, wp->w_buffer) - line + 1;
3011 }
3012 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003013 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003014 /* bad word found, use attributes until end of word */
3015 word_end = wp->w_cursor.col + len + 1;
3016
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003017 /* Turn index into actual attributes. */
3018 if (spell_hlf != HLF_COUNT)
3019 spell_attr = highlight_attr[spell_hlf];
3020 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003021 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003022
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003023# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003024 /* Need to restart syntax highlighting for this line. */
3025 if (has_syntax)
3026 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003027# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003028 }
3029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 }
3031
3032 /*
3033 * Correct highlighting for cursor that can't be disabled.
3034 * Avoids having to check this for each character.
3035 */
3036 if (fromcol >= 0)
3037 {
3038 if (noinvcur)
3039 {
3040 if ((colnr_T)fromcol == wp->w_virtcol)
3041 {
3042 /* highlighting starts at cursor, let it start just after the
3043 * cursor */
3044 fromcol_prev = fromcol;
3045 fromcol = -1;
3046 }
3047 else if ((colnr_T)fromcol < wp->w_virtcol)
3048 /* restart highlighting after the cursor */
3049 fromcol_prev = wp->w_virtcol;
3050 }
3051 if (fromcol >= tocol)
3052 fromcol = -1;
3053 }
3054
3055#ifdef FEAT_SEARCH_EXTRA
3056 /*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003057 * Handle highlighting the last used search pattern and ":match".
3058 * Do this for both search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003060 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003062 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003063 shl->startcol = MAXCOL;
3064 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 shl->attr_cur = 0;
3066 if (shl->rm.regprog != NULL)
3067 {
3068 v = (long)(ptr - line);
3069 next_search_hl(wp, shl, lnum, (colnr_T)v);
3070
3071 /* Need to get the line again, a multi-line regexp may have made it
3072 * invalid. */
3073 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3074 ptr = line + v;
3075
3076 if (shl->lnum != 0 && shl->lnum <= lnum)
3077 {
3078 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003079 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003081 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3083 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003084 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003086 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003088 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
3090#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003091 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003092 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 else
3094#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003095 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003097 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 {
3099 shl->attr_cur = shl->attr;
3100 search_attr = shl->attr;
3101 }
3102 area_highlighting = TRUE;
3103 }
3104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106#endif
3107
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003108#ifdef FEAT_SYN_HL
Bram Moolenaare2f98b92006-03-29 21:18:24 +00003109 /* Cursor line highlighting for 'cursorline'. Not when Visual mode is
3110 * active, because it's not clear what is selected then. */
3111 if (wp->w_p_cul && lnum == wp->w_cursor.lnum && !VIsual_active)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003112 {
3113 line_attr = hl_attr(HLF_CUL);
3114 area_highlighting = TRUE;
3115 }
3116#endif
3117
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003118 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 col = 0;
3120#ifdef FEAT_RIGHTLEFT
3121 if (wp->w_p_rl)
3122 {
3123 /* Rightleft window: process the text in the normal direction, but put
3124 * it in current_ScreenLine[] from right to left. Start at the
3125 * rightmost column of the window. */
3126 col = W_WIDTH(wp) - 1;
3127 off += col;
3128 }
3129#endif
3130
3131 /*
3132 * Repeat for the whole displayed line.
3133 */
3134 for (;;)
3135 {
3136 /* Skip this quickly when working on the text. */
3137 if (draw_state != WL_LINE)
3138 {
3139#ifdef FEAT_CMDWIN
3140 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3141 {
3142 draw_state = WL_CMDLINE;
3143 if (cmdwin_type != 0 && wp == curwin)
3144 {
3145 /* Draw the cmdline character. */
3146 *extra = cmdwin_type;
3147 n_extra = 1;
3148 p_extra = extra;
3149 c_extra = NUL;
3150 char_attr = hl_attr(HLF_AT);
3151 }
3152 }
3153#endif
3154
3155#ifdef FEAT_FOLDING
3156 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3157 {
3158 draw_state = WL_FOLD;
3159 if (wp->w_p_fdc > 0)
3160 {
3161 /* Draw the 'foldcolumn'. */
3162 fill_foldcolumn(extra, wp, FALSE, lnum);
3163 n_extra = wp->w_p_fdc;
3164 p_extra = extra;
3165 c_extra = NUL;
3166 char_attr = hl_attr(HLF_FC);
3167 }
3168 }
3169#endif
3170
3171#ifdef FEAT_SIGNS
3172 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3173 {
3174 draw_state = WL_SIGN;
3175 /* Show the sign column when there are any signs in this
3176 * buffer or when using Netbeans. */
3177 if (draw_signcolumn(wp)
3178# ifdef FEAT_DIFF
3179 && filler_todo <= 0
3180# endif
3181 )
3182 {
3183 int_u text_sign;
3184# ifdef FEAT_SIGN_ICONS
3185 int_u icon_sign;
3186# endif
3187
3188 /* Draw two cells with the sign value or blank. */
3189 c_extra = ' ';
3190 char_attr = hl_attr(HLF_SC);
3191 n_extra = 2;
3192
3193 if (row == startrow)
3194 {
3195 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3196 SIGN_TEXT);
3197# ifdef FEAT_SIGN_ICONS
3198 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3199 SIGN_ICON);
3200 if (gui.in_use && icon_sign != 0)
3201 {
3202 /* Use the image in this position. */
3203 c_extra = SIGN_BYTE;
3204# ifdef FEAT_NETBEANS_INTG
3205 if (buf_signcount(wp->w_buffer, lnum) > 1)
3206 c_extra = MULTISIGN_BYTE;
3207# endif
3208 char_attr = icon_sign;
3209 }
3210 else
3211# endif
3212 if (text_sign != 0)
3213 {
3214 p_extra = sign_get_text(text_sign);
3215 if (p_extra != NULL)
3216 {
3217 c_extra = NUL;
3218 n_extra = STRLEN(p_extra);
3219 }
3220 char_attr = sign_get_attr(text_sign, FALSE);
3221 }
3222 }
3223 }
3224 }
3225#endif
3226
3227 if (draw_state == WL_NR - 1 && n_extra == 0)
3228 {
3229 draw_state = WL_NR;
3230 /* Display the line number. After the first fill with blanks
3231 * when the 'n' flag isn't in 'cpo' */
3232 if (wp->w_p_nu
3233 && (row == startrow
3234#ifdef FEAT_DIFF
3235 + filler_lines
3236#endif
3237 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3238 {
3239 /* Draw the line number (empty space after wrapping). */
3240 if (row == startrow
3241#ifdef FEAT_DIFF
3242 + filler_lines
3243#endif
3244 )
3245 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003246 sprintf((char *)extra, "%*ld ",
3247 number_width(wp), (long)lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (wp->w_skipcol > 0)
3249 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3250 *p_extra = '-';
3251#ifdef FEAT_RIGHTLEFT
3252 if (wp->w_p_rl) /* reverse line numbers */
3253 rl_mirror(extra);
3254#endif
3255 p_extra = extra;
3256 c_extra = NUL;
3257 }
3258 else
3259 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003260 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003262#ifdef FEAT_SYN_HL
3263 /* When 'cursorline' is set highlight the line number of
3264 * the current line differently. */
3265 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3266 char_attr = hl_combine_attr(hl_attr(HLF_CUL), char_attr);
3267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
3269 }
3270
3271#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3272 if (draw_state == WL_SBR - 1 && n_extra == 0)
3273 {
3274 draw_state = WL_SBR;
3275# ifdef FEAT_DIFF
3276 if (filler_todo > 0)
3277 {
3278 /* Draw "deleted" diff line(s). */
3279 if (char2cells(fill_diff) > 1)
3280 c_extra = '-';
3281 else
3282 c_extra = fill_diff;
3283# ifdef FEAT_RIGHTLEFT
3284 if (wp->w_p_rl)
3285 n_extra = col + 1;
3286 else
3287# endif
3288 n_extra = W_WIDTH(wp) - col;
3289 char_attr = hl_attr(HLF_DED);
3290 }
3291# endif
3292# ifdef FEAT_LINEBREAK
3293 if (*p_sbr != NUL && need_showbreak)
3294 {
3295 /* Draw 'showbreak' at the start of each broken line. */
3296 p_extra = p_sbr;
3297 c_extra = NUL;
3298 n_extra = (int)STRLEN(p_sbr);
3299 char_attr = hl_attr(HLF_AT);
3300 need_showbreak = FALSE;
3301 /* Correct end of highlighted area for 'showbreak',
3302 * required when 'linebreak' is also set. */
3303 if (tocol == vcol)
3304 tocol += n_extra;
3305 }
3306# endif
3307 }
3308#endif
3309
3310 if (draw_state == WL_LINE - 1 && n_extra == 0)
3311 {
3312 draw_state = WL_LINE;
3313 if (saved_n_extra)
3314 {
3315 /* Continue item from end of wrapped line. */
3316 n_extra = saved_n_extra;
3317 c_extra = saved_c_extra;
3318 p_extra = saved_p_extra;
3319 char_attr = saved_char_attr;
3320 }
3321 else
3322 char_attr = 0;
3323 }
3324 }
3325
3326 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003327 if (dollar_vcol != 0 && wp == curwin
3328 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#ifdef FEAT_DIFF
3330 && filler_todo <= 0
3331#endif
3332 )
3333 {
3334 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3335 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003336 /* Pretend we have finished updating the window. Except when
3337 * 'cursorcolumn' is set. */
3338#ifdef FEAT_SYN_HL
3339 if (wp->w_p_cuc)
3340 row = wp->w_cline_row + wp->w_cline_height;
3341 else
3342#endif
3343 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 break;
3345 }
3346
3347 if (draw_state == WL_LINE && area_highlighting)
3348 {
3349 /* handle Visual or match highlighting in this line */
3350 if (vcol == fromcol
3351#ifdef FEAT_MBYTE
3352 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3353 && (*mb_ptr2cells)(ptr) > 1)
3354#endif
3355 || ((int)vcol_prev == fromcol_prev
3356 && vcol < tocol))
3357 area_attr = attr; /* start highlighting */
3358 else if (area_attr != 0
3359 && (vcol == tocol
3360 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363#ifdef FEAT_SEARCH_EXTRA
3364 if (!n_extra)
3365 {
3366 /*
3367 * Check for start/end of search pattern match.
3368 * After end, check for start/end of next match.
3369 * When another match, have to check for start again.
3370 * Watch out for matching an empty string!
3371 * Do this first for search_hl, then for match_hl, so that
3372 * ":match" overrules 'hlsearch'.
3373 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003374 v = (long)(ptr - line);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003375 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003377 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 while (shl->rm.regprog != NULL)
3379 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003380 if (shl->startcol != MAXCOL
3381 && v >= (long)shl->startcol
3382 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 shl->attr_cur = shl->attr;
3385 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003386 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
3388 shl->attr_cur = 0;
3389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 next_search_hl(wp, shl, lnum, (colnr_T)v);
3391
3392 /* Need to get the line again, a multi-line regexp
3393 * may have made it invalid. */
3394 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3395 ptr = line + v;
3396
3397 if (shl->lnum == lnum)
3398 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003399 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003401 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003403 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003405 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
3407 /* highlight empty match, try again after
3408 * it */
3409#ifdef FEAT_MBYTE
3410 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003411 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003412 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 else
3414#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003415 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417
3418 /* Loop to check if the match starts at the
3419 * current position */
3420 continue;
3421 }
3422 }
3423 break;
3424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 /* ":match" highlighting overrules 'hlsearch' */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003428 for (i = 0; i <= 3; ++i)
3429 if (i == 3)
3430 search_attr = search_hl.attr_cur;
3431 else if (match_hl[i].attr_cur != 0)
3432 {
3433 search_attr = match_hl[i].attr_cur;
3434 break;
3435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437#endif
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003440 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 {
3442 if (diff_hlf == HLF_CHD && ptr - line >= change_start)
3443 diff_hlf = HLF_TXD; /* changed text */
3444 if (diff_hlf == HLF_TXD && ptr - line > change_end)
3445 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003446 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003449
3450 /* Decide which of the highlight attributes to use. */
3451 attr_pri = TRUE;
3452 if (area_attr != 0)
3453 char_attr = area_attr;
3454 else if (search_attr != 0)
3455 char_attr = search_attr;
3456#ifdef LINE_ATTR
3457 /* Use line_attr when not in the Visual or 'incsearch' area
3458 * (area_attr may be 0 when "noinvcur" is set). */
3459 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
3460 || (vcol < fromcol || vcol >= tocol)))
3461 char_attr = line_attr;
3462#endif
3463 else
3464 {
3465 attr_pri = FALSE;
3466#ifdef FEAT_SYN_HL
3467 if (has_syntax)
3468 char_attr = syntax_attr;
3469 else
3470#endif
3471 char_attr = 0;
3472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474
3475 /*
3476 * Get the next character to put on the screen.
3477 */
3478 /*
3479 * The 'extra' array contains the extra stuff that is inserted to
3480 * represent special characters (non-printable stuff). When all
3481 * characters are the same, c_extra is used.
3482 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3483 */
3484 if (n_extra > 0)
3485 {
3486 if (c_extra != NUL)
3487 {
3488 c = c_extra;
3489#ifdef FEAT_MBYTE
3490 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3491 if (enc_utf8 && (*mb_char2len)(c) > 1)
3492 {
3493 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003494 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003495 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497 else
3498 mb_utf8 = FALSE;
3499#endif
3500 }
3501 else
3502 {
3503 c = *p_extra;
3504#ifdef FEAT_MBYTE
3505 if (has_mbyte)
3506 {
3507 mb_c = c;
3508 if (enc_utf8)
3509 {
3510 /* If the UTF-8 character is more than one byte:
3511 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003512 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 mb_utf8 = FALSE;
3514 if (mb_l > n_extra)
3515 mb_l = 1;
3516 else if (mb_l > 1)
3517 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003518 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003520 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 }
3523 else
3524 {
3525 /* if this is a DBCS character, put it in "mb_c" */
3526 mb_l = MB_BYTE2LEN(c);
3527 if (mb_l >= n_extra)
3528 mb_l = 1;
3529 else if (mb_l > 1)
3530 mb_c = (c << 8) + p_extra[1];
3531 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003532 if (mb_l == 0) /* at the NUL at end-of-line */
3533 mb_l = 1;
3534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 /* If a double-width char doesn't fit display a '>' in the
3536 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003537 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538# ifdef FEAT_RIGHTLEFT
3539 wp->w_p_rl ? (col <= 0) :
3540# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003541 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 && (*mb_char2cells)(mb_c) == 2)
3543 {
3544 c = '>';
3545 mb_c = c;
3546 mb_l = 1;
3547 mb_utf8 = FALSE;
3548 multi_attr = hl_attr(HLF_AT);
3549 /* put the pointer back to output the double-width
3550 * character at the start of the next line. */
3551 ++n_extra;
3552 --p_extra;
3553 }
3554 else
3555 {
3556 n_extra -= mb_l - 1;
3557 p_extra += mb_l - 1;
3558 }
3559 }
3560#endif
3561 ++p_extra;
3562 }
3563 --n_extra;
3564 }
3565 else
3566 {
3567 /*
3568 * Get a character from the line itself.
3569 */
3570 c = *ptr;
3571#ifdef FEAT_MBYTE
3572 if (has_mbyte)
3573 {
3574 mb_c = c;
3575 if (enc_utf8)
3576 {
3577 /* If the UTF-8 character is more than one byte: Decode it
3578 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003579 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 mb_utf8 = FALSE;
3581 if (mb_l > 1)
3582 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003583 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 /* Overlong encoded ASCII or ASCII with composing char
3585 * is displayed normally, except a NUL. */
3586 if (mb_c < 0x80)
3587 c = mb_c;
3588 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003589
3590 /* At start of the line we can have a composing char.
3591 * Draw it as a space with a composing char. */
3592 if (utf_iscomposing(mb_c))
3593 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003594 for (i = Screen_mco - 1; i > 0; --i)
3595 u8cc[i] = u8cc[i - 1];
3596 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003597 mb_c = ' ';
3598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
3600
3601 if ((mb_l == 1 && c >= 0x80)
3602 || (mb_l >= 1 && mb_c == 0)
3603 || (mb_l > 1 && (!vim_isprintc(mb_c)
3604 || mb_c >= 0x10000)))
3605 {
3606 /*
3607 * Illegal UTF-8 byte: display as <xx>.
3608 * Non-BMP character : display as ? or fullwidth ?.
3609 */
3610 if (mb_c < 0x10000)
3611 {
3612 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003613# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (wp->w_p_rl) /* reverse */
3615 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
3618 else if (utf_char2cells(mb_c) != 2)
3619 STRCPY(extra, "?");
3620 else
3621 /* 0xff1f in UTF-8: full-width '?' */
3622 STRCPY(extra, "\357\274\237");
3623
3624 p_extra = extra;
3625 c = *p_extra;
3626 mb_c = mb_ptr2char_adv(&p_extra);
3627 mb_utf8 = (c >= 0x80);
3628 n_extra = (int)STRLEN(p_extra);
3629 c_extra = NUL;
3630 if (area_attr == 0 && search_attr == 0)
3631 {
3632 n_attr = n_extra + 1;
3633 extra_attr = hl_attr(HLF_8);
3634 saved_attr2 = char_attr; /* save current attr */
3635 }
3636 }
3637 else if (mb_l == 0) /* at the NUL at end-of-line */
3638 mb_l = 1;
3639#ifdef FEAT_ARABIC
3640 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
3641 {
3642 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003643 int pc, pc1, nc;
3644 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646 /* The idea of what is the previous and next
3647 * character depends on 'rightleft'. */
3648 if (wp->w_p_rl)
3649 {
3650 pc = prev_c;
3651 pc1 = prev_c1;
3652 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003653 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 }
3655 else
3656 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003657 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003659 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
3661 prev_c = mb_c;
3662
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003663 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665 else
3666 prev_c = mb_c;
3667#endif
3668 }
3669 else /* enc_dbcs */
3670 {
3671 mb_l = MB_BYTE2LEN(c);
3672 if (mb_l == 0) /* at the NUL at end-of-line */
3673 mb_l = 1;
3674 else if (mb_l > 1)
3675 {
3676 /* We assume a second byte below 32 is illegal.
3677 * Hopefully this is OK for all double-byte encodings!
3678 */
3679 if (ptr[1] >= 32)
3680 mb_c = (c << 8) + ptr[1];
3681 else
3682 {
3683 if (ptr[1] == NUL)
3684 {
3685 /* head byte at end of line */
3686 mb_l = 1;
3687 transchar_nonprint(extra, c);
3688 }
3689 else
3690 {
3691 /* illegal tail byte */
3692 mb_l = 2;
3693 STRCPY(extra, "XX");
3694 }
3695 p_extra = extra;
3696 n_extra = (int)STRLEN(extra) - 1;
3697 c_extra = NUL;
3698 c = *p_extra++;
3699 if (area_attr == 0 && search_attr == 0)
3700 {
3701 n_attr = n_extra + 1;
3702 extra_attr = hl_attr(HLF_8);
3703 saved_attr2 = char_attr; /* save current attr */
3704 }
3705 mb_c = c;
3706 }
3707 }
3708 }
3709 /* If a double-width char doesn't fit display a '>' in the
3710 * last column; the character is displayed at the start of the
3711 * next line. */
3712 if ((
3713# ifdef FEAT_RIGHTLEFT
3714 wp->w_p_rl ? (col <= 0) :
3715# endif
3716 (col >= W_WIDTH(wp) - 1))
3717 && (*mb_char2cells)(mb_c) == 2)
3718 {
3719 c = '>';
3720 mb_c = c;
3721 mb_utf8 = FALSE;
3722 mb_l = 1;
3723 multi_attr = hl_attr(HLF_AT);
3724 /* Put pointer back so that the character will be
3725 * displayed at the start of the next line. */
3726 --ptr;
3727 }
3728 else if (*ptr != NUL)
3729 ptr += mb_l - 1;
3730
3731 /* If a double-width char doesn't fit at the left side display
3732 * a '<' in the first column. */
3733 if (n_skip > 0 && mb_l > 1)
3734 {
3735 extra[0] = '<';
3736 p_extra = extra;
3737 n_extra = 1;
3738 c_extra = NUL;
3739 c = ' ';
3740 if (area_attr == 0 && search_attr == 0)
3741 {
3742 n_attr = n_extra + 1;
3743 extra_attr = hl_attr(HLF_AT);
3744 saved_attr2 = char_attr; /* save current attr */
3745 }
3746 mb_c = c;
3747 mb_utf8 = FALSE;
3748 mb_l = 1;
3749 }
3750
3751 }
3752#endif
3753 ++ptr;
3754
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003755 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003756 if (wp->w_p_list && (c == 160
3757#ifdef FEAT_MBYTE
3758 || (mb_utf8 && mb_c == 160)
3759#endif
3760 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003761 {
3762 c = lcs_nbsp;
3763 if (area_attr == 0 && search_attr == 0)
3764 {
3765 n_attr = 1;
3766 extra_attr = hl_attr(HLF_8);
3767 saved_attr2 = char_attr; /* save current attr */
3768 }
3769#ifdef FEAT_MBYTE
3770 mb_c = c;
3771 if (enc_utf8 && (*mb_char2len)(c) > 1)
3772 {
3773 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003774 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003775 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003776 }
3777 else
3778 mb_utf8 = FALSE;
3779#endif
3780 }
3781
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 if (extra_check)
3783 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003784#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003785 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003786#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003787
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003788#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 /* Get syntax attribute, unless still at the start of the line
3790 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003791 v = (long)(ptr - line);
3792 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 {
3794 /* Get the syntax attribute for the character. If there
3795 * is an error, disable syntax highlighting. */
3796 save_did_emsg = did_emsg;
3797 did_emsg = FALSE;
3798
Bram Moolenaar217ad922005-03-20 22:37:15 +00003799 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003800# ifdef FEAT_SPELL
3801 has_spell ? &can_spell :
3802# endif
3803 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804
3805 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003806 {
3807 wp->w_buffer->b_syn_error = TRUE;
3808 has_syntax = FALSE;
3809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 else
3811 did_emsg = save_did_emsg;
3812
3813 /* Need to get the line again, a multi-line regexp may
3814 * have made it invalid. */
3815 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3816 ptr = line + v;
3817
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003818 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003820 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00003821 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003823#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003824
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003825#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003826 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00003827 * Only do this when there is no syntax highlighting, the
3828 * @Spell cluster is not used or the current syntax item
3829 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003830 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003831 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003832 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003833# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003834 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003835 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003836# endif
3837 if (c != 0 && (
3838# ifdef FEAT_SYN_HL
3839 !has_syntax ||
3840# endif
3841 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00003842 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00003843 char_u *prev_ptr, *p;
3844 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003845 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003846# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00003847 if (has_mbyte)
3848 {
3849 prev_ptr = ptr - mb_l;
3850 v -= mb_l - 1;
3851 }
3852 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00003853# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00003854 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003855
3856 /* Use nextline[] if possible, it has the start of the
3857 * next line concatenated. */
3858 if ((prev_ptr - line) - nextlinecol >= 0)
3859 p = nextline + (prev_ptr - line) - nextlinecol;
3860 else
3861 p = prev_ptr;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003862 cap_col -= (prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003863 len = spell_check(wp, p, &spell_hlf, &cap_col,
3864 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003865 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003866
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003867 /* In Insert mode only highlight a word that
3868 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003869 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003870 && (State & INSERT) != 0
3871 && wp->w_cursor.lnum == lnum
3872 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00003873 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003874 && wp->w_cursor.col < (colnr_T)word_end)
3875 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003876 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00003877 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003878 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00003879
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003880 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00003881 && (p - nextline) + len > nextline_idx)
3882 {
3883 /* Remember that the good word continues at the
3884 * start of the next line. */
3885 checked_lnum = lnum + 1;
3886 checked_col = (p - nextline) + len - nextline_idx;
3887 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003888
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003889 /* Turn index into actual attributes. */
3890 if (spell_hlf != HLF_COUNT)
3891 spell_attr = highlight_attr[spell_hlf];
3892
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003893 if (cap_col > 0)
3894 {
3895 if (p != prev_ptr
3896 && (p - nextline) + cap_col >= nextline_idx)
3897 {
3898 /* Remember that the word in the next line
3899 * must start with a capital. */
3900 capcol_lnum = lnum + 1;
3901 cap_col = (p - nextline) + cap_col
3902 - nextline_idx;
3903 }
3904 else
3905 /* Compute the actual column. */
3906 cap_col += (prev_ptr - line);
3907 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003908 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00003909 }
3910 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003911 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003912 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003913 char_attr = hl_combine_attr(char_attr, spell_attr);
3914 else
3915 char_attr = hl_combine_attr(spell_attr, char_attr);
3916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917#endif
3918#ifdef FEAT_LINEBREAK
3919 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00003920 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 */
3922 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003923 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
3925 n_extra = win_lbr_chartabsize(wp, ptr - (
3926# ifdef FEAT_MBYTE
3927 has_mbyte ? mb_l :
3928# endif
3929 1), (colnr_T)vcol, NULL) - 1;
3930 c_extra = ' ';
3931 if (vim_iswhite(c))
3932 c = ' ';
3933 }
3934#endif
3935
3936 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
3937 {
3938 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003939 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 {
3941 n_attr = 1;
3942 extra_attr = hl_attr(HLF_8);
3943 saved_attr2 = char_attr; /* save current attr */
3944 }
3945#ifdef FEAT_MBYTE
3946 mb_c = c;
3947 if (enc_utf8 && (*mb_char2len)(c) > 1)
3948 {
3949 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003950 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003951 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953 else
3954 mb_utf8 = FALSE;
3955#endif
3956 }
3957 }
3958
3959 /*
3960 * Handling of non-printable characters.
3961 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003962 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
3964 /*
3965 * when getting a character from the file, we may have to
3966 * turn it into something else on the way to putting it
3967 * into "ScreenLines".
3968 */
3969 if (c == TAB && (!wp->w_p_list || lcs_tab1))
3970 {
3971 /* tab amount depends on current column */
3972 n_extra = (int)wp->w_buffer->b_p_ts
3973 - vcol % (int)wp->w_buffer->b_p_ts - 1;
3974#ifdef FEAT_MBYTE
3975 mb_utf8 = FALSE; /* don't draw as UTF-8 */
3976#endif
3977 if (wp->w_p_list)
3978 {
3979 c = lcs_tab1;
3980 c_extra = lcs_tab2;
3981 n_attr = n_extra + 1;
3982 extra_attr = hl_attr(HLF_8);
3983 saved_attr2 = char_attr; /* save current attr */
3984#ifdef FEAT_MBYTE
3985 mb_c = c;
3986 if (enc_utf8 && (*mb_char2len)(c) > 1)
3987 {
3988 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003989 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003990 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992#endif
3993 }
3994 else
3995 {
3996 c_extra = ' ';
3997 c = ' ';
3998 }
3999 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004000 else if (c == NUL
4001 && ((wp->w_p_list && lcs_eol > 0)
4002 || ((fromcol >= 0 || fromcol_prev >= 0)
4003 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004004#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004005 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004006#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004007 && (
4008# ifdef FEAT_RIGHTLEFT
4009 wp->w_p_rl ? (col >= 0) :
4010# endif
4011 (col < W_WIDTH(wp)))
4012 && !(noinvcur
4013 && (colnr_T)vcol == wp->w_virtcol)))
4014 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004016 /* Display a '$' after the line or highlight an extra
4017 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4019 /* For a diff line the highlighting continues after the
4020 * "$". */
4021 if (
4022# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004023 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024# ifdef LINE_ATTR
4025 &&
4026# endif
4027# endif
4028# ifdef LINE_ATTR
4029 line_attr == 0
4030# endif
4031 )
4032#endif
4033 {
4034#ifdef FEAT_VIRTUALEDIT
4035 /* In virtualedit, visual selections may extend
4036 * beyond end of line. */
4037 if (area_highlighting && virtual_active()
4038 && tocol != MAXCOL && vcol < tocol)
4039 n_extra = 0;
4040 else
4041#endif
4042 {
4043 p_extra = at_end_str;
4044 n_extra = 1;
4045 c_extra = NUL;
4046 }
4047 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004048 if (wp->w_p_list)
4049 c = lcs_eol;
4050 else
4051 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 lcs_eol_one = -1;
4053 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004054 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
4056 extra_attr = hl_attr(HLF_AT);
4057 n_attr = 1;
4058 }
4059#ifdef FEAT_MBYTE
4060 mb_c = c;
4061 if (enc_utf8 && (*mb_char2len)(c) > 1)
4062 {
4063 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004064 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004065 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 else
4068 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4069#endif
4070 }
4071 else if (c != NUL)
4072 {
4073 p_extra = transchar(c);
4074#ifdef FEAT_RIGHTLEFT
4075 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4076 rl_mirror(p_extra); /* reverse "<12>" */
4077#endif
4078 n_extra = byte2cells(c) - 1;
4079 c_extra = NUL;
4080 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004081 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
4083 n_attr = n_extra + 1;
4084 extra_attr = hl_attr(HLF_8);
4085 saved_attr2 = char_attr; /* save current attr */
4086 }
4087#ifdef FEAT_MBYTE
4088 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4089#endif
4090 }
4091#ifdef FEAT_VIRTUALEDIT
4092 else if (VIsual_active
4093 && (VIsual_mode == Ctrl_V
4094 || VIsual_mode == 'v')
4095 && virtual_active()
4096 && tocol != MAXCOL
4097 && vcol < tocol
4098 && (
4099# ifdef FEAT_RIGHTLEFT
4100 wp->w_p_rl ? (col >= 0) :
4101# endif
4102 (col < W_WIDTH(wp))))
4103 {
4104 c = ' ';
4105 --ptr; /* put it back at the NUL */
4106 }
4107#endif
4108#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4109 else if ((
4110# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004111 diff_hlf != (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112# ifdef LINE_ATTR
4113 ||
4114# endif
4115# endif
4116# ifdef LINE_ATTR
4117 line_attr != 0
4118# endif
4119 ) && (
4120# ifdef FEAT_RIGHTLEFT
4121 wp->w_p_rl ? (col >= 0) :
4122# endif
4123 (col < W_WIDTH(wp))))
4124 {
4125 /* Highlight until the right side of the window */
4126 c = ' ';
4127 --ptr; /* put it back at the NUL */
4128# ifdef FEAT_DIFF
4129 if (diff_hlf == HLF_TXD)
4130 {
4131 diff_hlf = HLF_CHD;
4132 if (attr == 0 || char_attr != attr)
4133 char_attr = hl_attr(diff_hlf);
4134 }
4135# endif
4136 }
4137#endif
4138 }
4139 }
4140
4141 /* Don't override visual selection highlighting. */
4142 if (n_attr > 0
4143 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004144 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 char_attr = extra_attr;
4146
Bram Moolenaar81695252004-12-29 20:58:21 +00004147#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 /* XIM don't send preedit_start and preedit_end, but they send
4149 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4150 * im_is_preediting() here. */
4151 if (xic != NULL
4152 && lnum == curwin->w_cursor.lnum
4153 && (State & INSERT)
4154 && !p_imdisable
4155 && im_is_preediting()
4156 && draw_state == WL_LINE)
4157 {
4158 colnr_T tcol;
4159
4160 if (preedit_end_col == MAXCOL)
4161 getvcol(curwin, &(curwin->w_cursor), &tcol, NULL, NULL);
4162 else
4163 tcol = preedit_end_col;
4164 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4165 {
4166 if (feedback_old_attr < 0)
4167 {
4168 feedback_col = 0;
4169 feedback_old_attr = char_attr;
4170 }
4171 char_attr = im_get_feedback_attr(feedback_col);
4172 if (char_attr < 0)
4173 char_attr = feedback_old_attr;
4174 feedback_col++;
4175 }
4176 else if (feedback_old_attr >= 0)
4177 {
4178 char_attr = feedback_old_attr;
4179 feedback_old_attr = -1;
4180 feedback_col = 0;
4181 }
4182 }
4183#endif
4184 /*
4185 * Handle the case where we are in column 0 but not on the first
4186 * character of the line and the user wants us to show us a
4187 * special character (via 'listchars' option "precedes:<char>".
4188 */
4189 if (lcs_prec_todo != NUL
4190 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4191#ifdef FEAT_DIFF
4192 && filler_todo <= 0
4193#endif
4194 && draw_state > WL_NR
4195 && c != NUL)
4196 {
4197 c = lcs_prec;
4198 lcs_prec_todo = NUL;
4199#ifdef FEAT_MBYTE
4200 mb_c = c;
4201 if (enc_utf8 && (*mb_char2len)(c) > 1)
4202 {
4203 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004204 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004205 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207 else
4208 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4209#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004210 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
4212 saved_attr3 = char_attr; /* save current attr */
4213 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4214 n_attr3 = 1;
4215 }
4216 }
4217
4218 /*
4219 * At end of the text line.
4220 */
4221 if (c == NUL)
4222 {
4223 /* invert at least one char, used for Visual and empty line or
4224 * highlight match at end of line. If it's beyond the last
4225 * char on the screen, just overwrite that one (tricky!) Not
4226 * needed when a '$' was displayed for 'list'. */
4227 if (lcs_eol == lcs_eol_one
4228 && ((area_attr != 0 && vcol == fromcol)
4229#ifdef FEAT_SEARCH_EXTRA
4230 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004231 || (ptr - line) - 1 == (long)search_hl.startcol
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004232 || (ptr - line) - 1 == (long)match_hl[0].startcol
4233 || (ptr - line) - 1 == (long)match_hl[1].startcol
4234 || (ptr - line) - 1 == (long)match_hl[2].startcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235#endif
4236 ))
4237 {
4238 int n = 0;
4239
4240#ifdef FEAT_RIGHTLEFT
4241 if (wp->w_p_rl)
4242 {
4243 if (col < 0)
4244 n = 1;
4245 }
4246 else
4247#endif
4248 {
4249 if (col >= W_WIDTH(wp))
4250 n = -1;
4251 }
4252 if (n != 0)
4253 {
4254 /* At the window boundary, highlight the last character
4255 * instead (better than nothing). */
4256 off += n;
4257 col += n;
4258 }
4259 else
4260 {
4261 /* Add a blank character to highlight. */
4262 ScreenLines[off] = ' ';
4263#ifdef FEAT_MBYTE
4264 if (enc_utf8)
4265 ScreenLinesUC[off] = 0;
4266#endif
4267 }
4268#ifdef FEAT_SEARCH_EXTRA
4269 if (area_attr == 0)
4270 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004271 for (i = 0; i <= 3; ++i)
4272 {
4273 if (i == 3)
4274 char_attr = search_hl.attr;
4275 else if ((ptr - line) - 1 == (long)match_hl[i].startcol)
4276 {
4277 char_attr = match_hl[i].attr;
4278 break;
4279 }
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282#endif
4283 ScreenAttrs[off] = char_attr;
4284#ifdef FEAT_RIGHTLEFT
4285 if (wp->w_p_rl)
4286 --col;
4287 else
4288#endif
4289 ++col;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004290 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004293#ifdef FEAT_SYN_HL
4294 /* Highlight 'cursorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004295 if (wp->w_p_wrap)
4296 v = wp->w_skipcol;
4297 else
4298 v = wp->w_leftcol;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004299 /* check if line ends before left margin */
4300 if (vcol < v + col - win_col_off(wp))
4301
4302 vcol = v + col - win_col_off(wp);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004303 if (wp->w_p_cuc
4304 && (int)wp->w_virtcol >= vcol
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004305 && (int)wp->w_virtcol < W_WIDTH(wp) * (row - startrow + 1)
4306 + v
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004307 && lnum != wp->w_cursor.lnum
4308# ifdef FEAT_RIGHTLEFT
4309 && !wp->w_p_rl
4310# endif
4311 )
4312 {
4313 while (col < W_WIDTH(wp))
4314 {
4315 ScreenLines[off] = ' ';
4316#ifdef FEAT_MBYTE
4317 if (enc_utf8)
4318 ScreenLinesUC[off] = 0;
4319#endif
4320 ++col;
Bram Moolenaarca003e12006-03-17 23:19:38 +00004321 if (vcol == (long)wp->w_virtcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004322 {
4323 ScreenAttrs[off] = hl_attr(HLF_CUC);
4324 break;
4325 }
4326 ScreenAttrs[off++] = 0;
4327 ++vcol;
4328 }
4329 }
4330#endif
4331
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4333 wp->w_p_rl);
4334 row++;
4335
4336 /*
4337 * Update w_cline_height and w_cline_folded if the cursor line was
4338 * updated (saves a call to plines() later).
4339 */
4340 if (wp == curwin && lnum == curwin->w_cursor.lnum)
4341 {
4342 curwin->w_cline_row = startrow;
4343 curwin->w_cline_height = row - startrow;
4344#ifdef FEAT_FOLDING
4345 curwin->w_cline_folded = FALSE;
4346#endif
4347 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
4348 }
4349
4350 break;
4351 }
4352
4353 /* line continues beyond line end */
4354 if (lcs_ext
4355 && !wp->w_p_wrap
4356#ifdef FEAT_DIFF
4357 && filler_todo <= 0
4358#endif
4359 && (
4360#ifdef FEAT_RIGHTLEFT
4361 wp->w_p_rl ? col == 0 :
4362#endif
4363 col == W_WIDTH(wp) - 1)
4364 && (*ptr != NUL
4365 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4366 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
4367 {
4368 c = lcs_ext;
4369 char_attr = hl_attr(HLF_AT);
4370#ifdef FEAT_MBYTE
4371 mb_c = c;
4372 if (enc_utf8 && (*mb_char2len)(c) > 1)
4373 {
4374 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004375 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004376 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
4378 else
4379 mb_utf8 = FALSE;
4380#endif
4381 }
4382
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004383#ifdef FEAT_SYN_HL
4384 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
4385 * highlight the cursor position itself. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00004386 if (wp->w_p_cuc && vcol == (long)wp->w_virtcol
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004387 && lnum != wp->w_cursor.lnum
4388 && draw_state == WL_LINE)
4389 {
4390 vcol_save_attr = char_attr;
4391 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
4392 }
4393 else
4394 vcol_save_attr = -1;
4395#endif
4396
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 /*
4398 * Store character to be displayed.
4399 * Skip characters that are left of the screen for 'nowrap'.
4400 */
4401 vcol_prev = vcol;
4402 if (draw_state < WL_LINE || n_skip <= 0)
4403 {
4404 /*
4405 * Store the character.
4406 */
4407#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
4408 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
4409 {
4410 /* A double-wide character is: put first halve in left cell. */
4411 --off;
4412 --col;
4413 }
4414#endif
4415 ScreenLines[off] = c;
4416#ifdef FEAT_MBYTE
4417 if (enc_dbcs == DBCS_JPNU)
4418 ScreenLines2[off] = mb_c & 0xff;
4419 else if (enc_utf8)
4420 {
4421 if (mb_utf8)
4422 {
4423 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004424 if ((c & 0xff) == 0)
4425 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004426 for (i = 0; i < Screen_mco; ++i)
4427 {
4428 ScreenLinesC[i][off] = u8cc[i];
4429 if (u8cc[i] == 0)
4430 break;
4431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433 else
4434 ScreenLinesUC[off] = 0;
4435 }
4436 if (multi_attr)
4437 {
4438 ScreenAttrs[off] = multi_attr;
4439 multi_attr = 0;
4440 }
4441 else
4442#endif
4443 ScreenAttrs[off] = char_attr;
4444
4445#ifdef FEAT_MBYTE
4446 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4447 {
4448 /* Need to fill two screen columns. */
4449 ++off;
4450 ++col;
4451 if (enc_utf8)
4452 /* UTF-8: Put a 0 in the second screen char. */
4453 ScreenLines[off] = 0;
4454 else
4455 /* DBCS: Put second byte in the second screen char. */
4456 ScreenLines[off] = mb_c & 0xff;
4457 ++vcol;
4458 /* When "tocol" is halfway a character, set it to the end of
4459 * the character, otherwise highlighting won't stop. */
4460 if (tocol == vcol)
4461 ++tocol;
4462#ifdef FEAT_RIGHTLEFT
4463 if (wp->w_p_rl)
4464 {
4465 /* now it's time to backup one cell */
4466 --off;
4467 --col;
4468 }
4469#endif
4470 }
4471#endif
4472#ifdef FEAT_RIGHTLEFT
4473 if (wp->w_p_rl)
4474 {
4475 --off;
4476 --col;
4477 }
4478 else
4479#endif
4480 {
4481 ++off;
4482 ++col;
4483 }
4484 }
4485 else
4486 --n_skip;
4487
4488 /* Only advance the "vcol" when after the 'number' column. */
4489 if (draw_state >= WL_SBR
4490#ifdef FEAT_DIFF
4491 && filler_todo <= 0
4492#endif
4493 )
4494 ++vcol;
4495
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004496#ifdef FEAT_SYN_HL
4497 if (vcol_save_attr >= 0)
4498 char_attr = vcol_save_attr;
4499#endif
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 /* restore attributes after "predeces" in 'listchars' */
4502 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
4503 char_attr = saved_attr3;
4504
4505 /* restore attributes after last 'listchars' or 'number' char */
4506 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
4507 char_attr = saved_attr2;
4508
4509 /*
4510 * At end of screen line and there is more to come: Display the line
4511 * so far. If there is no more to display it is catched above.
4512 */
4513 if ((
4514#ifdef FEAT_RIGHTLEFT
4515 wp->w_p_rl ? (col < 0) :
4516#endif
4517 (col >= W_WIDTH(wp)))
4518 && (*ptr != NUL
4519#ifdef FEAT_DIFF
4520 || filler_todo > 0
4521#endif
4522 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
4523 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
4524 )
4525 {
4526 SCREEN_LINE(screen_row, W_WINCOL(wp), col, (int)W_WIDTH(wp),
4527 wp->w_p_rl);
4528 ++row;
4529 ++screen_row;
4530
4531 /* When not wrapping and finished diff lines, or when displayed
4532 * '$' and highlighting until last column, break here. */
4533 if ((!wp->w_p_wrap
4534#ifdef FEAT_DIFF
4535 && filler_todo <= 0
4536#endif
4537 ) || lcs_eol_one == -1)
4538 break;
4539
4540 /* When the window is too narrow draw all "@" lines. */
4541 if (draw_state != WL_LINE
4542#ifdef FEAT_DIFF
4543 && filler_todo <= 0
4544#endif
4545 )
4546 {
4547 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
4548#ifdef FEAT_VERTSPLIT
4549 draw_vsep_win(wp, row);
4550#endif
4551 row = endrow;
4552 }
4553
4554 /* When line got too long for screen break here. */
4555 if (row == endrow)
4556 {
4557 ++row;
4558 break;
4559 }
4560
4561 if (screen_cur_row == screen_row - 1
4562#ifdef FEAT_DIFF
4563 && filler_todo <= 0
4564#endif
4565 && W_WIDTH(wp) == Columns)
4566 {
4567 /* Remember that the line wraps, used for modeless copy. */
4568 LineWraps[screen_row - 1] = TRUE;
4569
4570 /*
4571 * Special trick to make copy/paste of wrapped lines work with
4572 * xterm/screen: write an extra character beyond the end of
4573 * the line. This will work with all terminal types
4574 * (regardless of the xn,am settings).
4575 * Only do this on a fast tty.
4576 * Only do this if the cursor is on the current line
4577 * (something has been written in it).
4578 * Don't do this for the GUI.
4579 * Don't do this for double-width characters.
4580 * Don't do this for a window not at the right screen border.
4581 */
4582 if (p_tf
4583#ifdef FEAT_GUI
4584 && !gui.in_use
4585#endif
4586#ifdef FEAT_MBYTE
4587 && !(has_mbyte
4588 && ((*mb_off2cells)(LineOffset[screen_row]) == 2
4589 || (*mb_off2cells)(LineOffset[screen_row - 1]
4590 + (int)Columns - 2) == 2))
4591#endif
4592 )
4593 {
4594 /* First make sure we are at the end of the screen line,
4595 * then output the same character again to let the
4596 * terminal know about the wrap. If the terminal doesn't
4597 * auto-wrap, we overwrite the character. */
4598 if (screen_cur_col != W_WIDTH(wp))
4599 screen_char(LineOffset[screen_row - 1]
4600 + (unsigned)Columns - 1,
4601 screen_row - 1, (int)(Columns - 1));
4602
4603#ifdef FEAT_MBYTE
4604 /* When there is a multi-byte character, just output a
4605 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00004606 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
4607 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 out_char(' ');
4609 else
4610#endif
4611 out_char(ScreenLines[LineOffset[screen_row - 1]
4612 + (Columns - 1)]);
4613 /* force a redraw of the first char on the next line */
4614 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
4615 screen_start(); /* don't know where cursor is now */
4616 }
4617 }
4618
4619 col = 0;
4620 off = (unsigned)(current_ScreenLine - ScreenLines);
4621#ifdef FEAT_RIGHTLEFT
4622 if (wp->w_p_rl)
4623 {
4624 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
4625 off += col;
4626 }
4627#endif
4628
4629 /* reset the drawing state for the start of a wrapped line */
4630 draw_state = WL_START;
4631 saved_n_extra = n_extra;
4632 saved_p_extra = p_extra;
4633 saved_c_extra = c_extra;
4634 saved_char_attr = char_attr;
4635 n_extra = 0;
4636 lcs_prec_todo = lcs_prec;
4637#ifdef FEAT_LINEBREAK
4638# ifdef FEAT_DIFF
4639 if (filler_todo <= 0)
4640# endif
4641 need_showbreak = TRUE;
4642#endif
4643#ifdef FEAT_DIFF
4644 --filler_todo;
4645 /* When the filler lines are actually below the last line of the
4646 * file, don't draw the line itself, break here. */
4647 if (filler_todo == 0 && wp->w_botfill)
4648 break;
4649#endif
4650 }
4651
4652 } /* for every character in the line */
4653
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004654#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004655 /* After an empty line check first word for capital. */
4656 if (*skipwhite(line) == NUL)
4657 {
4658 capcol_lnum = lnum + 1;
4659 cap_col = 0;
4660 }
4661#endif
4662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 return row;
4664}
4665
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004666#ifdef FEAT_MBYTE
4667static int comp_char_differs __ARGS((int, int));
4668
4669/*
4670 * Return if the composing characters at "off_from" and "off_to" differ.
4671 */
4672 static int
4673comp_char_differs(off_from, off_to)
4674 int off_from;
4675 int off_to;
4676{
4677 int i;
4678
4679 for (i = 0; i < Screen_mco; ++i)
4680 {
4681 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
4682 return TRUE;
4683 if (ScreenLinesC[i][off_from] == 0)
4684 break;
4685 }
4686 return FALSE;
4687}
4688#endif
4689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690/*
4691 * Check whether the given character needs redrawing:
4692 * - the (first byte of the) character is different
4693 * - the attributes are different
4694 * - the character is multi-byte and the next byte is different
4695 */
4696 static int
4697char_needs_redraw(off_from, off_to, cols)
4698 int off_from;
4699 int off_to;
4700 int cols;
4701{
4702 if (cols > 0
4703 && ((ScreenLines[off_from] != ScreenLines[off_to]
4704 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
4705
4706#ifdef FEAT_MBYTE
4707 || (enc_dbcs != 0
4708 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
4709 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
4710 ? ScreenLines2[off_from] != ScreenLines2[off_to]
4711 : (cols > 1 && ScreenLines[off_from + 1]
4712 != ScreenLines[off_to + 1])))
4713 || (enc_utf8
4714 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
4715 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004716 && comp_char_differs(off_from, off_to))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#endif
4718 ))
4719 return TRUE;
4720 return FALSE;
4721}
4722
4723/*
4724 * Move one "cooked" screen line to the screen, but only the characters that
4725 * have actually changed. Handle insert/delete character.
4726 * "coloff" gives the first column on the screen for this line.
4727 * "endcol" gives the columns where valid characters are.
4728 * "clear_width" is the width of the window. It's > 0 if the rest of the line
4729 * needs to be cleared, negative otherwise.
4730 * "rlflag" is TRUE in a rightleft window:
4731 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
4732 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
4733 */
4734 static void
4735screen_line(row, coloff, endcol, clear_width
4736#ifdef FEAT_RIGHTLEFT
4737 , rlflag
4738#endif
4739 )
4740 int row;
4741 int coloff;
4742 int endcol;
4743 int clear_width;
4744#ifdef FEAT_RIGHTLEFT
4745 int rlflag;
4746#endif
4747{
4748 unsigned off_from;
4749 unsigned off_to;
4750 int col = 0;
4751#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
4752 int hl;
4753#endif
4754 int force = FALSE; /* force update rest of the line */
4755 int redraw_this /* bool: does character need redraw? */
4756#ifdef FEAT_GUI
4757 = TRUE /* For GUI when while-loop empty */
4758#endif
4759 ;
4760 int redraw_next; /* redraw_this for next character */
4761#ifdef FEAT_MBYTE
4762 int clear_next = FALSE;
4763 int char_cells; /* 1: normal char */
4764 /* 2: occupies two display cells */
4765# define CHAR_CELLS char_cells
4766#else
4767# define CHAR_CELLS 1
4768#endif
4769
4770# ifdef FEAT_CLIPBOARD
4771 clip_may_clear_selection(row, row);
4772# endif
4773
4774 off_from = (unsigned)(current_ScreenLine - ScreenLines);
4775 off_to = LineOffset[row] + coloff;
4776
4777#ifdef FEAT_RIGHTLEFT
4778 if (rlflag)
4779 {
4780 /* Clear rest first, because it's left of the text. */
4781 if (clear_width > 0)
4782 {
4783 while (col <= endcol && ScreenLines[off_to] == ' '
4784 && ScreenAttrs[off_to] == 0
4785# ifdef FEAT_MBYTE
4786 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
4787# endif
4788 )
4789 {
4790 ++off_to;
4791 ++col;
4792 }
4793 if (col <= endcol)
4794 screen_fill(row, row + 1, col + coloff,
4795 endcol + coloff + 1, ' ', ' ', 0);
4796 }
4797 col = endcol + 1;
4798 off_to = LineOffset[row] + col + coloff;
4799 off_from += col;
4800 endcol = (clear_width > 0 ? clear_width : -clear_width);
4801 }
4802#endif /* FEAT_RIGHTLEFT */
4803
4804 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
4805
4806 while (col < endcol)
4807 {
4808#ifdef FEAT_MBYTE
4809 if (has_mbyte && (col + 1 < endcol))
4810 char_cells = (*mb_off2cells)(off_from);
4811 else
4812 char_cells = 1;
4813#endif
4814
4815 redraw_this = redraw_next;
4816 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
4817 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
4818
4819#ifdef FEAT_GUI
4820 /* If the next character was bold, then redraw the current character to
4821 * remove any pixels that might have spilt over into us. This only
4822 * happens in the GUI.
4823 */
4824 if (redraw_next && gui.in_use)
4825 {
4826 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004827 if (hl > HL_ALL)
4828 hl = syn_attr2attr(hl);
4829 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 redraw_this = TRUE;
4831 }
4832#endif
4833
4834 if (redraw_this)
4835 {
4836 /*
4837 * Special handling when 'xs' termcap flag set (hpterm):
4838 * Attributes for characters are stored at the position where the
4839 * cursor is when writing the highlighting code. The
4840 * start-highlighting code must be written with the cursor on the
4841 * first highlighted character. The stop-highlighting code must
4842 * be written with the cursor just after the last highlighted
4843 * character.
4844 * Overwriting a character doesn't remove it's highlighting. Need
4845 * to clear the rest of the line, and force redrawing it
4846 * completely.
4847 */
4848 if ( p_wiv
4849 && !force
4850#ifdef FEAT_GUI
4851 && !gui.in_use
4852#endif
4853 && ScreenAttrs[off_to] != 0
4854 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
4855 {
4856 /*
4857 * Need to remove highlighting attributes here.
4858 */
4859 windgoto(row, col + coloff);
4860 out_str(T_CE); /* clear rest of this screen line */
4861 screen_start(); /* don't know where cursor is now */
4862 force = TRUE; /* force redraw of rest of the line */
4863 redraw_next = TRUE; /* or else next char would miss out */
4864
4865 /*
4866 * If the previous character was highlighted, need to stop
4867 * highlighting at this character.
4868 */
4869 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
4870 {
4871 screen_attr = ScreenAttrs[off_to - 1];
4872 term_windgoto(row, col + coloff);
4873 screen_stop_highlight();
4874 }
4875 else
4876 screen_attr = 0; /* highlighting has stopped */
4877 }
4878#ifdef FEAT_MBYTE
4879 if (enc_dbcs != 0)
4880 {
4881 /* Check if overwriting a double-byte with a single-byte or
4882 * the other way around requires another character to be
4883 * redrawn. For UTF-8 this isn't needed, because comparing
4884 * ScreenLinesUC[] is sufficient. */
4885 if (char_cells == 1
4886 && col + 1 < endcol
4887 && (*mb_off2cells)(off_to) > 1)
4888 {
4889 /* Writing a single-cell character over a double-cell
4890 * character: need to redraw the next cell. */
4891 ScreenLines[off_to + 1] = 0;
4892 redraw_next = TRUE;
4893 }
4894 else if (char_cells == 2
4895 && col + 2 < endcol
4896 && (*mb_off2cells)(off_to) == 1
4897 && (*mb_off2cells)(off_to + 1) > 1)
4898 {
4899 /* Writing the second half of a double-cell character over
4900 * a double-cell character: need to redraw the second
4901 * cell. */
4902 ScreenLines[off_to + 2] = 0;
4903 redraw_next = TRUE;
4904 }
4905
4906 if (enc_dbcs == DBCS_JPNU)
4907 ScreenLines2[off_to] = ScreenLines2[off_from];
4908 }
4909 /* When writing a single-width character over a double-width
4910 * character and at the end of the redrawn text, need to clear out
4911 * the right halve of the old character.
4912 * Also required when writing the right halve of a double-width
4913 * char over the left halve of an existing one. */
4914 if (has_mbyte && col + char_cells == endcol
4915 && ((char_cells == 1
4916 && (*mb_off2cells)(off_to) > 1)
4917 || (char_cells == 2
4918 && (*mb_off2cells)(off_to) == 1
4919 && (*mb_off2cells)(off_to + 1) > 1)))
4920 clear_next = TRUE;
4921#endif
4922
4923 ScreenLines[off_to] = ScreenLines[off_from];
4924#ifdef FEAT_MBYTE
4925 if (enc_utf8)
4926 {
4927 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
4928 if (ScreenLinesUC[off_from] != 0)
4929 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004930 int i;
4931
4932 for (i = 0; i < Screen_mco; ++i)
4933 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
4935 }
4936 if (char_cells == 2)
4937 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
4938#endif
4939
4940#if defined(FEAT_GUI) || defined(UNIX)
4941 /* The bold trick makes a single row of pixels appear in the next
4942 * character. When a bold character is removed, the next
4943 * character should be redrawn too. This happens for our own GUI
4944 * and for some xterms. */
4945 if (
4946# ifdef FEAT_GUI
4947 gui.in_use
4948# endif
4949# if defined(FEAT_GUI) && defined(UNIX)
4950 ||
4951# endif
4952# ifdef UNIX
4953 term_is_xterm
4954# endif
4955 )
4956 {
4957 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004958 if (hl > HL_ALL)
4959 hl = syn_attr2attr(hl);
4960 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 redraw_next = TRUE;
4962 }
4963#endif
4964 ScreenAttrs[off_to] = ScreenAttrs[off_from];
4965#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004966 /* For simplicity set the attributes of second half of a
4967 * double-wide character equal to the first half. */
4968 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004970
4971 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 else
4974#endif
4975 screen_char(off_to, row, col + coloff);
4976 }
4977 else if ( p_wiv
4978#ifdef FEAT_GUI
4979 && !gui.in_use
4980#endif
4981 && col + coloff > 0)
4982 {
4983 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
4984 {
4985 /*
4986 * Don't output stop-highlight when moving the cursor, it will
4987 * stop the highlighting when it should continue.
4988 */
4989 screen_attr = 0;
4990 }
4991 else if (screen_attr != 0)
4992 screen_stop_highlight();
4993 }
4994
4995 off_to += CHAR_CELLS;
4996 off_from += CHAR_CELLS;
4997 col += CHAR_CELLS;
4998 }
4999
5000#ifdef FEAT_MBYTE
5001 if (clear_next)
5002 {
5003 /* Clear the second half of a double-wide character of which the left
5004 * half was overwritten with a single-wide character. */
5005 ScreenLines[off_to] = ' ';
5006 if (enc_utf8)
5007 ScreenLinesUC[off_to] = 0;
5008 screen_char(off_to, row, col + coloff);
5009 }
5010#endif
5011
5012 if (clear_width > 0
5013#ifdef FEAT_RIGHTLEFT
5014 && !rlflag
5015#endif
5016 )
5017 {
5018#ifdef FEAT_GUI
5019 int startCol = col;
5020#endif
5021
5022 /* blank out the rest of the line */
5023 while (col < clear_width && ScreenLines[off_to] == ' '
5024 && ScreenAttrs[off_to] == 0
5025#ifdef FEAT_MBYTE
5026 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5027#endif
5028 )
5029 {
5030 ++off_to;
5031 ++col;
5032 }
5033 if (col < clear_width)
5034 {
5035#ifdef FEAT_GUI
5036 /*
5037 * In the GUI, clearing the rest of the line may leave pixels
5038 * behind if the first character cleared was bold. Some bold
5039 * fonts spill over the left. In this case we redraw the previous
5040 * character too. If we didn't skip any blanks above, then we
5041 * only redraw if the character wasn't already redrawn anyway.
5042 */
5043 if (gui.in_use && (col > startCol || !redraw_this)
5044# ifdef FEAT_MBYTE
5045 && enc_dbcs == 0
5046# endif
5047 )
5048 {
5049 hl = ScreenAttrs[off_to];
5050 if (hl > HL_ALL || (hl & HL_BOLD))
5051 screen_char(off_to - 1, row, col + coloff - 1);
5052 }
5053#endif
5054 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5055 ' ', ' ', 0);
5056#ifdef FEAT_VERTSPLIT
5057 off_to += clear_width - col;
5058 col = clear_width;
5059#endif
5060 }
5061 }
5062
5063 if (clear_width > 0)
5064 {
5065#ifdef FEAT_VERTSPLIT
5066 /* For a window that's left of another, draw the separator char. */
5067 if (col + coloff < Columns)
5068 {
5069 int c;
5070
5071 c = fillchar_vsep(&hl);
5072 if (ScreenLines[off_to] != c
5073# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005074 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5075 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076# endif
5077 || ScreenAttrs[off_to] != hl)
5078 {
5079 ScreenLines[off_to] = c;
5080 ScreenAttrs[off_to] = hl;
5081# ifdef FEAT_MBYTE
5082 if (enc_utf8)
5083 {
5084 if (c >= 0x80)
5085 {
5086 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005087 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
5089 else
5090 ScreenLinesUC[off_to] = 0;
5091 }
5092# endif
5093 screen_char(off_to, row, col + coloff);
5094 }
5095 }
5096 else
5097#endif
5098 LineWraps[row] = FALSE;
5099 }
5100}
5101
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005102#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005104 * Mirror text "str" for right-left displaying.
5105 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005107 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108rl_mirror(str)
5109 char_u *str;
5110{
5111 char_u *p1, *p2;
5112 int t;
5113
5114 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5115 {
5116 t = *p1;
5117 *p1 = *p2;
5118 *p2 = t;
5119 }
5120}
5121#endif
5122
5123#if defined(FEAT_WINDOWS) || defined(PROTO)
5124/*
5125 * mark all status lines for redraw; used after first :cd
5126 */
5127 void
5128status_redraw_all()
5129{
5130 win_T *wp;
5131
5132 for (wp = firstwin; wp; wp = wp->w_next)
5133 if (wp->w_status_height)
5134 {
5135 wp->w_redr_status = TRUE;
5136 redraw_later(VALID);
5137 }
5138}
5139
5140/*
5141 * mark all status lines of the current buffer for redraw
5142 */
5143 void
5144status_redraw_curbuf()
5145{
5146 win_T *wp;
5147
5148 for (wp = firstwin; wp; wp = wp->w_next)
5149 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5150 {
5151 wp->w_redr_status = TRUE;
5152 redraw_later(VALID);
5153 }
5154}
5155
5156/*
5157 * Redraw all status lines that need to be redrawn.
5158 */
5159 void
5160redraw_statuslines()
5161{
5162 win_T *wp;
5163
5164 for (wp = firstwin; wp; wp = wp->w_next)
5165 if (wp->w_redr_status)
5166 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005167 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005168 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169}
5170#endif
5171
5172#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5173/*
5174 * Redraw all status lines at the bottom of frame "frp".
5175 */
5176 void
5177win_redraw_last_status(frp)
5178 frame_T *frp;
5179{
5180 if (frp->fr_layout == FR_LEAF)
5181 frp->fr_win->w_redr_status = TRUE;
5182 else if (frp->fr_layout == FR_ROW)
5183 {
5184 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
5185 win_redraw_last_status(frp);
5186 }
5187 else /* frp->fr_layout == FR_COL */
5188 {
5189 frp = frp->fr_child;
5190 while (frp->fr_next != NULL)
5191 frp = frp->fr_next;
5192 win_redraw_last_status(frp);
5193 }
5194}
5195#endif
5196
5197#ifdef FEAT_VERTSPLIT
5198/*
5199 * Draw the verticap separator right of window "wp" starting with line "row".
5200 */
5201 static void
5202draw_vsep_win(wp, row)
5203 win_T *wp;
5204 int row;
5205{
5206 int hl;
5207 int c;
5208
5209 if (wp->w_vsep_width)
5210 {
5211 /* draw the vertical separator right of this window */
5212 c = fillchar_vsep(&hl);
5213 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
5214 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
5215 c, ' ', hl);
5216 }
5217}
5218#endif
5219
5220#ifdef FEAT_WILDMENU
5221static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005222static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
5224/*
5225 * Get the lenght of an item as it will be shown in the status line.
5226 */
5227 static int
5228status_match_len(xp, s)
5229 expand_T *xp;
5230 char_u *s;
5231{
5232 int len = 0;
5233
5234#ifdef FEAT_MENU
5235 int emenu = (xp->xp_context == EXPAND_MENUS
5236 || xp->xp_context == EXPAND_MENUNAMES);
5237
5238 /* Check for menu separators - replace with '|'. */
5239 if (emenu && menu_is_separator(s))
5240 return 1;
5241#endif
5242
5243 while (*s != NUL)
5244 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005245 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 ++s;
Bram Moolenaar81695252004-12-29 20:58:21 +00005247 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005248 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250
5251 return len;
5252}
5253
5254/*
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005255 * Return TRUE for characters that are not displayed in a status match.
5256 * These are backslashes used for escaping. Do show backslashes in help tags.
5257 */
5258 static int
5259skip_status_match_char(xp, s)
5260 expand_T *xp;
5261 char_u *s;
5262{
5263 return ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
5264#ifdef FEAT_MENU
5265 || ((xp->xp_context == EXPAND_MENUS
5266 || xp->xp_context == EXPAND_MENUNAMES)
5267 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
5268#endif
5269 );
5270}
5271
5272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 * Show wildchar matches in the status line.
5274 * Show at least the "match" item.
5275 * We start at item 'first_match' in the list and show all matches that fit.
5276 *
5277 * If inversion is possible we use it. Else '=' characters are used.
5278 */
5279 void
5280win_redr_status_matches(xp, num_matches, matches, match, showtail)
5281 expand_T *xp;
5282 int num_matches;
5283 char_u **matches; /* list of matches */
5284 int match;
5285 int showtail;
5286{
5287#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
5288 int row;
5289 char_u *buf;
5290 int len;
5291 int clen; /* lenght in screen cells */
5292 int fillchar;
5293 int attr;
5294 int i;
5295 int highlight = TRUE;
5296 char_u *selstart = NULL;
5297 int selstart_col = 0;
5298 char_u *selend = NULL;
5299 static int first_match = 0;
5300 int add_left = FALSE;
5301 char_u *s;
5302#ifdef FEAT_MENU
5303 int emenu;
5304#endif
5305#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
5306 int l;
5307#endif
5308
5309 if (matches == NULL) /* interrupted completion? */
5310 return;
5311
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005312#ifdef FEAT_MBYTE
5313 if (has_mbyte)
5314 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
5315 else
5316#endif
5317 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (buf == NULL)
5319 return;
5320
5321 if (match == -1) /* don't show match but original text */
5322 {
5323 match = 0;
5324 highlight = FALSE;
5325 }
5326 /* count 1 for the ending ">" */
5327 clen = status_match_len(xp, L_MATCH(match)) + 3;
5328 if (match == 0)
5329 first_match = 0;
5330 else if (match < first_match)
5331 {
5332 /* jumping left, as far as we can go */
5333 first_match = match;
5334 add_left = TRUE;
5335 }
5336 else
5337 {
5338 /* check if match fits on the screen */
5339 for (i = first_match; i < match; ++i)
5340 clen += status_match_len(xp, L_MATCH(i)) + 2;
5341 if (first_match > 0)
5342 clen += 2;
5343 /* jumping right, put match at the left */
5344 if ((long)clen > Columns)
5345 {
5346 first_match = match;
5347 /* if showing the last match, we can add some on the left */
5348 clen = 2;
5349 for (i = match; i < num_matches; ++i)
5350 {
5351 clen += status_match_len(xp, L_MATCH(i)) + 2;
5352 if ((long)clen >= Columns)
5353 break;
5354 }
5355 if (i == num_matches)
5356 add_left = TRUE;
5357 }
5358 }
5359 if (add_left)
5360 while (first_match > 0)
5361 {
5362 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
5363 if ((long)clen >= Columns)
5364 break;
5365 --first_match;
5366 }
5367
5368 fillchar = fillchar_status(&attr, TRUE);
5369
5370 if (first_match == 0)
5371 {
5372 *buf = NUL;
5373 len = 0;
5374 }
5375 else
5376 {
5377 STRCPY(buf, "< ");
5378 len = 2;
5379 }
5380 clen = len;
5381
5382 i = first_match;
5383 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
5384 {
5385 if (i == match)
5386 {
5387 selstart = buf + len;
5388 selstart_col = clen;
5389 }
5390
5391 s = L_MATCH(i);
5392 /* Check for menu separators - replace with '|' */
5393#ifdef FEAT_MENU
5394 emenu = (xp->xp_context == EXPAND_MENUS
5395 || xp->xp_context == EXPAND_MENUNAMES);
5396 if (emenu && menu_is_separator(s))
5397 {
5398 STRCPY(buf + len, transchar('|'));
5399 l = (int)STRLEN(buf + len);
5400 len += l;
5401 clen += l;
5402 }
5403 else
5404#endif
5405 for ( ; *s != NUL; ++s)
5406 {
Bram Moolenaar35c54e52005-05-20 21:25:31 +00005407 if (skip_status_match_char(xp, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 ++s;
5409 clen += ptr2cells(s);
5410#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005411 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
5413 STRNCPY(buf + len, s, l);
5414 s += l - 1;
5415 len += l;
5416 }
5417 else
5418#endif
5419 {
5420 STRCPY(buf + len, transchar_byte(*s));
5421 len += (int)STRLEN(buf + len);
5422 }
5423 }
5424 if (i == match)
5425 selend = buf + len;
5426
5427 *(buf + len++) = ' ';
5428 *(buf + len++) = ' ';
5429 clen += 2;
5430 if (++i == num_matches)
5431 break;
5432 }
5433
5434 if (i != num_matches)
5435 {
5436 *(buf + len++) = '>';
5437 ++clen;
5438 }
5439
5440 buf[len] = NUL;
5441
5442 row = cmdline_row - 1;
5443 if (row >= 0)
5444 {
5445 if (wild_menu_showing == 0)
5446 {
5447 if (msg_scrolled > 0)
5448 {
5449 /* Put the wildmenu just above the command line. If there is
5450 * no room, scroll the screen one line up. */
5451 if (cmdline_row == Rows - 1)
5452 {
5453 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
5454 ++msg_scrolled;
5455 }
5456 else
5457 {
5458 ++cmdline_row;
5459 ++row;
5460 }
5461 wild_menu_showing = WM_SCROLLED;
5462 }
5463 else
5464 {
5465 /* Create status line if needed by setting 'laststatus' to 2.
5466 * Set 'winminheight' to zero to avoid that the window is
5467 * resized. */
5468 if (lastwin->w_status_height == 0)
5469 {
5470 save_p_ls = p_ls;
5471 save_p_wmh = p_wmh;
5472 p_ls = 2;
5473 p_wmh = 0;
5474 last_status(FALSE);
5475 }
5476 wild_menu_showing = WM_SHOWN;
5477 }
5478 }
5479
5480 screen_puts(buf, row, 0, attr);
5481 if (selstart != NULL && highlight)
5482 {
5483 *selend = NUL;
5484 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
5485 }
5486
5487 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
5488 }
5489
5490#ifdef FEAT_VERTSPLIT
5491 win_redraw_last_status(topframe);
5492#else
5493 lastwin->w_redr_status = TRUE;
5494#endif
5495 vim_free(buf);
5496}
5497#endif
5498
5499#if defined(FEAT_WINDOWS) || defined(PROTO)
5500/*
5501 * Redraw the status line of window wp.
5502 *
5503 * If inversion is possible we use it. Else '=' characters are used.
5504 */
5505 void
5506win_redr_status(wp)
5507 win_T *wp;
5508{
5509 int row;
5510 char_u *p;
5511 int len;
5512 int fillchar;
5513 int attr;
5514 int this_ru_col;
5515
5516 wp->w_redr_status = FALSE;
5517 if (wp->w_status_height == 0)
5518 {
5519 /* no status line, can only be last window */
5520 redraw_cmdline = TRUE;
5521 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005522 else if (!redrawing()
5523#ifdef FEAT_INS_EXPAND
5524 /* don't update status line when popup menu is visible and may be
5525 * drawn over it */
5526 || pum_visible()
5527#endif
5528 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 {
5530 /* Don't redraw right now, do it later. */
5531 wp->w_redr_status = TRUE;
5532 }
5533#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00005534 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
5536 /* redraw custom status line */
Bram Moolenaar238a5642006-02-21 22:12:05 +00005537 redraw_custum_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539#endif
5540 else
5541 {
5542 fillchar = fillchar_status(&attr, wp == curwin);
5543
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005544 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 p = NameBuff;
5546 len = (int)STRLEN(p);
5547
5548 if (wp->w_buffer->b_help
5549#ifdef FEAT_QUICKFIX
5550 || wp->w_p_pvw
5551#endif
5552 || bufIsChanged(wp->w_buffer)
5553 || wp->w_buffer->b_p_ro)
5554 *(p + len++) = ' ';
5555 if (wp->w_buffer->b_help)
5556 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005557 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 len += (int)STRLEN(p + len);
5559 }
5560#ifdef FEAT_QUICKFIX
5561 if (wp->w_p_pvw)
5562 {
5563 STRCPY(p + len, _("[Preview]"));
5564 len += (int)STRLEN(p + len);
5565 }
5566#endif
5567 if (bufIsChanged(wp->w_buffer))
5568 {
5569 STRCPY(p + len, "[+]");
5570 len += 3;
5571 }
5572 if (wp->w_buffer->b_p_ro)
5573 {
5574 STRCPY(p + len, "[RO]");
5575 len += 4;
5576 }
5577
5578#ifndef FEAT_VERTSPLIT
5579 this_ru_col = ru_col;
5580 if (this_ru_col < (Columns + 1) / 2)
5581 this_ru_col = (Columns + 1) / 2;
5582#else
5583 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
5584 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
5585 this_ru_col = (W_WIDTH(wp) + 1) / 2;
5586 if (this_ru_col <= 1)
5587 {
5588 p = (char_u *)"<"; /* No room for file name! */
5589 len = 1;
5590 }
5591 else
5592#endif
5593#ifdef FEAT_MBYTE
5594 if (has_mbyte)
5595 {
5596 int clen = 0, i;
5597
5598 /* Count total number of display cells. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005599 for (i = 0; p[i] != NUL; i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 clen += (*mb_ptr2cells)(p + i);
5601 /* Find first character that will fit.
5602 * Going from start to end is much faster for DBCS. */
5603 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005604 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 clen -= (*mb_ptr2cells)(p + i);
5606 len = clen;
5607 if (i > 0)
5608 {
5609 p = p + i - 1;
5610 *p = '<';
5611 ++len;
5612 }
5613
5614 }
5615 else
5616#endif
5617 if (len > this_ru_col - 1)
5618 {
5619 p += len - (this_ru_col - 1);
5620 *p = '<';
5621 len = this_ru_col - 1;
5622 }
5623
5624 row = W_WINROW(wp) + wp->w_height;
5625 screen_puts(p, row, W_WINCOL(wp), attr);
5626 screen_fill(row, row + 1, len + W_WINCOL(wp),
5627 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
5628
5629 if (get_keymap_str(wp, NameBuff, MAXPATHL)
5630 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
5631 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
5632 - 1 + W_WINCOL(wp)), attr);
5633
5634#ifdef FEAT_CMDL_INFO
5635 win_redr_ruler(wp, TRUE);
5636#endif
5637 }
5638
5639#ifdef FEAT_VERTSPLIT
5640 /*
5641 * May need to draw the character below the vertical separator.
5642 */
5643 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
5644 {
5645 if (stl_connected(wp))
5646 fillchar = fillchar_status(&attr, wp == curwin);
5647 else
5648 fillchar = fillchar_vsep(&attr);
5649 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
5650 attr);
5651 }
5652#endif
5653}
5654
Bram Moolenaar238a5642006-02-21 22:12:05 +00005655#ifdef FEAT_STL_OPT
5656/*
5657 * Redraw the status line according to 'statusline' and take care of any
5658 * errors encountered.
5659 */
5660 static void
5661redraw_custum_statusline(wp)
5662 win_T *wp;
5663{
5664 int save_called_emsg = called_emsg;
5665
5666 called_emsg = FALSE;
5667 win_redr_custom(wp, FALSE);
5668 if (called_emsg)
5669 set_string_option_direct((char_u *)"statusline", -1,
5670 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005671 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00005672 called_emsg |= save_called_emsg;
5673}
5674#endif
5675
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676# ifdef FEAT_VERTSPLIT
5677/*
5678 * Return TRUE if the status line of window "wp" is connected to the status
5679 * line of the window right of it. If not, then it's a vertical separator.
5680 * Only call if (wp->w_vsep_width != 0).
5681 */
5682 int
5683stl_connected(wp)
5684 win_T *wp;
5685{
5686 frame_T *fr;
5687
5688 fr = wp->w_frame;
5689 while (fr->fr_parent != NULL)
5690 {
5691 if (fr->fr_parent->fr_layout == FR_COL)
5692 {
5693 if (fr->fr_next != NULL)
5694 break;
5695 }
5696 else
5697 {
5698 if (fr->fr_next != NULL)
5699 return TRUE;
5700 }
5701 fr = fr->fr_parent;
5702 }
5703 return FALSE;
5704}
5705# endif
5706
5707#endif /* FEAT_WINDOWS */
5708
5709#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
5710/*
5711 * Get the value to show for the language mappings, active 'keymap'.
5712 */
5713 int
5714get_keymap_str(wp, buf, len)
5715 win_T *wp;
5716 char_u *buf; /* buffer for the result */
5717 int len; /* length of buffer */
5718{
5719 char_u *p;
5720
5721 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
5722 return FALSE;
5723
5724 {
5725#ifdef FEAT_EVAL
5726 buf_T *old_curbuf = curbuf;
5727 win_T *old_curwin = curwin;
5728 char_u *s;
5729
5730 curbuf = wp->w_buffer;
5731 curwin = wp;
5732 STRCPY(buf, "b:keymap_name"); /* must be writable */
5733 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005734 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 --emsg_skip;
5736 curbuf = old_curbuf;
5737 curwin = old_curwin;
5738 if (p == NULL || *p == NUL)
5739#endif
5740 {
5741#ifdef FEAT_KEYMAP
5742 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
5743 p = wp->w_buffer->b_p_keymap;
5744 else
5745#endif
5746 p = (char_u *)"lang";
5747 }
5748 if ((int)(STRLEN(p) + 3) < len)
5749 sprintf((char *)buf, "<%s>", p);
5750 else
5751 buf[0] = NUL;
5752#ifdef FEAT_EVAL
5753 vim_free(s);
5754#endif
5755 }
5756 return buf[0] != NUL;
5757}
5758#endif
5759
5760#if defined(FEAT_STL_OPT) || defined(PROTO)
5761/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005762 * Redraw the status line or ruler of window "wp".
5763 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 */
5765 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00005766win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00005768 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 int attr;
5771 int curattr;
5772 int row;
5773 int col = 0;
5774 int maxwidth;
5775 int width;
5776 int n;
5777 int len;
5778 int fillchar;
5779 char_u buf[MAXPATHL];
5780 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005781 struct stl_hlrec hltab[STL_MAX_ITEM];
5782 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005783 int use_sandbox = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005786 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005788 /* Use 'tabline'. Always at the first line of the screen. */
5789 p = p_tal;
5790 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00005791 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005792 attr = hl_attr(HLF_TPF);
5793 maxwidth = Columns;
5794# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005795 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005798 else
5799 {
5800 row = W_WINROW(wp) + wp->w_height;
5801 fillchar = fillchar_status(&attr, wp == curwin);
5802 maxwidth = W_WIDTH(wp);
5803
5804 if (draw_ruler)
5805 {
5806 p = p_ruf;
5807 /* advance past any leading group spec - implicit in ru_col */
5808 if (*p == '%')
5809 {
5810 if (*++p == '-')
5811 p++;
5812 if (atoi((char *) p))
5813 while (VIM_ISDIGIT(*p))
5814 p++;
5815 if (*p++ != '(')
5816 p = p_ruf;
5817 }
5818#ifdef FEAT_VERTSPLIT
5819 col = ru_col - (Columns - W_WIDTH(wp));
5820 if (col < (W_WIDTH(wp) + 1) / 2)
5821 col = (W_WIDTH(wp) + 1) / 2;
5822#else
5823 col = ru_col;
5824 if (col > (Columns + 1) / 2)
5825 col = (Columns + 1) / 2;
5826#endif
5827 maxwidth = W_WIDTH(wp) - col;
5828#ifdef FEAT_WINDOWS
5829 if (!wp->w_status_height)
5830#endif
5831 {
5832 row = Rows - 1;
5833 --maxwidth; /* writing in last column may cause scrolling */
5834 fillchar = ' ';
5835 attr = 0;
5836 }
5837
5838# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005839 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005840# endif
5841 }
5842 else
5843 {
5844 if (*wp->w_p_stl != NUL)
5845 p = wp->w_p_stl;
5846 else
5847 p = p_stl;
5848# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005849 use_sandbox = was_set_insecurely((char_u *)"statusline",
5850 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005851# endif
5852 }
5853
5854#ifdef FEAT_VERTSPLIT
5855 col += W_WINCOL(wp);
5856#endif
5857 }
5858
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (maxwidth <= 0)
5860 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005862 width = build_stl_str_hl(wp == NULL ? curwin : wp,
5863 buf, sizeof(buf),
5864 p, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005865 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 len = STRLEN(buf);
5867
5868 while (width < maxwidth && len < sizeof(buf) - 1)
5869 {
5870#ifdef FEAT_MBYTE
5871 len += (*mb_char2bytes)(fillchar, buf + len);
5872#else
5873 buf[len++] = fillchar;
5874#endif
5875 ++width;
5876 }
5877 buf[len] = NUL;
5878
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005879 /*
5880 * Draw each snippet with the specified highlighting.
5881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 curattr = attr;
5883 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005884 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005886 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 screen_puts_len(p, len, row, col, curattr);
5888 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005889 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005891 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005893 else if (hltab[n].userhl < 0)
5894 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00005896 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005897 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898#endif
5899 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005900 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005903
5904 if (wp == NULL)
5905 {
5906 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
5907 col = 0;
5908 len = 0;
5909 p = buf;
5910 fillchar = 0;
5911 for (n = 0; tabtab[n].start != NULL; n++)
5912 {
5913 len += vim_strnsize(p, (int)(tabtab[n].start - p));
5914 while (col < len)
5915 TabPageIdxs[col++] = fillchar;
5916 p = tabtab[n].start;
5917 fillchar = tabtab[n].userhl;
5918 }
5919 while (col < Columns)
5920 TabPageIdxs[col++] = fillchar;
5921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922}
5923
5924#endif /* FEAT_STL_OPT */
5925
5926/*
5927 * Output a single character directly to the screen and update ScreenLines.
5928 */
5929 void
5930screen_putchar(c, row, col, attr)
5931 int c;
5932 int row, col;
5933 int attr;
5934{
5935#ifdef FEAT_MBYTE
5936 char_u buf[MB_MAXBYTES + 1];
5937
5938 buf[(*mb_char2bytes)(c, buf)] = NUL;
5939#else
5940 char_u buf[2];
5941
5942 buf[0] = c;
5943 buf[1] = NUL;
5944#endif
5945 screen_puts(buf, row, col, attr);
5946}
5947
5948/*
5949 * Get a single character directly from ScreenLines into "bytes[]".
5950 * Also return its attribute in *attrp;
5951 */
5952 void
5953screen_getbytes(row, col, bytes, attrp)
5954 int row, col;
5955 char_u *bytes;
5956 int *attrp;
5957{
5958 unsigned off;
5959
5960 /* safety check */
5961 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
5962 {
5963 off = LineOffset[row] + col;
5964 *attrp = ScreenAttrs[off];
5965 bytes[0] = ScreenLines[off];
5966 bytes[1] = NUL;
5967
5968#ifdef FEAT_MBYTE
5969 if (enc_utf8 && ScreenLinesUC[off] != 0)
5970 bytes[utfc_char2bytes(off, bytes)] = NUL;
5971 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
5972 {
5973 bytes[0] = ScreenLines[off];
5974 bytes[1] = ScreenLines2[off];
5975 bytes[2] = NUL;
5976 }
5977 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
5978 {
5979 bytes[1] = ScreenLines[off + 1];
5980 bytes[2] = NUL;
5981 }
5982#endif
5983 }
5984}
5985
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005986#ifdef FEAT_MBYTE
5987static int screen_comp_differs __ARGS((int, int*));
5988
5989/*
5990 * Return TRUE if composing characters for screen posn "off" differs from
5991 * composing characters in "u8cc".
5992 */
5993 static int
5994screen_comp_differs(off, u8cc)
5995 int off;
5996 int *u8cc;
5997{
5998 int i;
5999
6000 for (i = 0; i < Screen_mco; ++i)
6001 {
6002 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6003 return TRUE;
6004 if (u8cc[i] == 0)
6005 break;
6006 }
6007 return FALSE;
6008}
6009#endif
6010
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011/*
6012 * Put string '*text' on the screen at position 'row' and 'col', with
6013 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6014 * Note: only outputs within one row, message is truncated at screen boundary!
6015 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6016 */
6017 void
6018screen_puts(text, row, col, attr)
6019 char_u *text;
6020 int row;
6021 int col;
6022 int attr;
6023{
6024 screen_puts_len(text, -1, row, col, attr);
6025}
6026
6027/*
6028 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6029 * a NUL.
6030 */
6031 void
6032screen_puts_len(text, len, row, col, attr)
6033 char_u *text;
6034 int len;
6035 int row;
6036 int col;
6037 int attr;
6038{
6039 unsigned off;
6040 char_u *ptr = text;
6041 int c;
6042#ifdef FEAT_MBYTE
6043 int mbyte_blen = 1;
6044 int mbyte_cells = 1;
6045 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006046 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 int clear_next_cell = FALSE;
6048# ifdef FEAT_ARABIC
6049 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006050 int pc, nc, nc1;
6051 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052# endif
6053#endif
6054
6055 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6056 return;
6057
6058 off = LineOffset[row] + col;
6059 while (*ptr != NUL && col < screen_Columns
6060 && (len < 0 || (int)(ptr - text) < len))
6061 {
6062 c = *ptr;
6063#ifdef FEAT_MBYTE
6064 /* check if this is the first byte of a multibyte */
6065 if (has_mbyte)
6066 {
6067 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006068 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006070 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6072 mbyte_cells = 1;
6073 else if (enc_dbcs != 0)
6074 mbyte_cells = mbyte_blen;
6075 else /* enc_utf8 */
6076 {
6077 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006078 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 (int)((text + len) - ptr));
6080 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006081 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 mbyte_cells = utf_char2cells(u8c);
6083 /* Non-BMP character: display as ? or fullwidth ?. */
6084 if (u8c >= 0x10000)
6085 {
6086 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
6087 if (attr == 0)
6088 attr = hl_attr(HLF_8);
6089 }
6090# ifdef FEAT_ARABIC
6091 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
6092 {
6093 /* Do Arabic shaping. */
6094 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
6095 {
6096 /* Past end of string to be displayed. */
6097 nc = NUL;
6098 nc1 = NUL;
6099 }
6100 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006101 {
6102 nc = utfc_ptr2char(ptr + mbyte_blen, pcc);
6103 nc1 = pcc[0];
6104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 pc = prev_c;
6106 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006107 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 }
6109 else
6110 prev_c = u8c;
6111# endif
6112 }
6113 }
6114#endif
6115
6116 if (ScreenLines[off] != c
6117#ifdef FEAT_MBYTE
6118 || (mbyte_cells == 2
6119 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
6120 || (enc_dbcs == DBCS_JPNU
6121 && c == 0x8e
6122 && ScreenLines2[off] != ptr[1])
6123 || (enc_utf8
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006124 && (ScreenLinesUC[off] != (u8char_T)u8c
6125 || screen_comp_differs(off, u8cc)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126#endif
6127 || ScreenAttrs[off] != attr
6128 || exmode_active
6129 )
6130 {
6131#if defined(FEAT_GUI) || defined(UNIX)
6132 /* The bold trick makes a single row of pixels appear in the next
6133 * character. When a bold character is removed, the next
6134 * character should be redrawn too. This happens for our own GUI
6135 * and for some xterms.
6136 * Force the redraw by setting the attribute to a different value
6137 * than "attr", the contents of ScreenLines[] may be needed by
6138 * mb_off2cells() further on.
6139 * Don't do this for the last drawn character, because the next
6140 * character may not be redrawn. */
6141 if (
6142# ifdef FEAT_GUI
6143 gui.in_use
6144# endif
6145# if defined(FEAT_GUI) && defined(UNIX)
6146 ||
6147# endif
6148# ifdef UNIX
6149 term_is_xterm
6150# endif
6151 )
6152 {
6153 int n;
6154
6155 n = ScreenAttrs[off];
6156# ifdef FEAT_MBYTE
6157 if (col + mbyte_cells < screen_Columns
6158 && (n > HL_ALL || (n & HL_BOLD))
6159 && (len < 0 ? ptr[mbyte_blen] != NUL
6160 : ptr + mbyte_blen < text + len))
6161 ScreenAttrs[off + mbyte_cells] = attr + 1;
6162# else
6163 if (col + 1 < screen_Columns
6164 && (n > HL_ALL || (n & HL_BOLD))
6165 && (len < 0 ? ptr[1] != NUL : ptr + 1 < text + len))
6166 ScreenLines[off + 1] = 0;
6167# endif
6168 }
6169#endif
6170#ifdef FEAT_MBYTE
6171 /* When at the end of the text and overwriting a two-cell
6172 * character with a one-cell character, need to clear the next
6173 * cell. Also when overwriting the left halve of a two-cell char
6174 * with the right halve of a two-cell char. Do this only once
6175 * (mb_off2cells() may return 2 on the right halve). */
6176 if (clear_next_cell)
6177 clear_next_cell = FALSE;
6178 else if (has_mbyte
6179 && (len < 0 ? ptr[mbyte_blen] == NUL
6180 : ptr + mbyte_blen >= text + len)
6181 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6182 || (mbyte_cells == 2
6183 && (*mb_off2cells)(off) == 1
6184 && (*mb_off2cells)(off + 1) > 1)))
6185 clear_next_cell = TRUE;
6186
6187 /* Make sure we never leave a second byte of a double-byte behind,
6188 * it confuses mb_off2cells(). */
6189 if (enc_dbcs
6190 && ((mbyte_cells == 1 && (*mb_off2cells)(off) > 1)
6191 || (mbyte_cells == 2
6192 && (*mb_off2cells)(off) == 1
6193 && (*mb_off2cells)(off + 1) > 1)))
6194 ScreenLines[off + mbyte_blen] = 0;
6195#endif
6196 ScreenLines[off] = c;
6197 ScreenAttrs[off] = attr;
6198#ifdef FEAT_MBYTE
6199 if (enc_utf8)
6200 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006201 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 ScreenLinesUC[off] = 0;
6203 else
6204 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006205 int i;
6206
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006208 for (i = 0; i < Screen_mco; ++i)
6209 {
6210 ScreenLinesC[i][off] = u8cc[i];
6211 if (u8cc[i] == 0)
6212 break;
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
6215 if (mbyte_cells == 2)
6216 {
6217 ScreenLines[off + 1] = 0;
6218 ScreenAttrs[off + 1] = attr;
6219 }
6220 screen_char(off, row, col);
6221 }
6222 else if (mbyte_cells == 2)
6223 {
6224 ScreenLines[off + 1] = ptr[1];
6225 ScreenAttrs[off + 1] = attr;
6226 screen_char_2(off, row, col);
6227 }
6228 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6229 {
6230 ScreenLines2[off] = ptr[1];
6231 screen_char(off, row, col);
6232 }
6233 else
6234#endif
6235 screen_char(off, row, col);
6236 }
6237#ifdef FEAT_MBYTE
6238 if (has_mbyte)
6239 {
6240 off += mbyte_cells;
6241 col += mbyte_cells;
6242 ptr += mbyte_blen;
6243 if (clear_next_cell)
6244 ptr = (char_u *)" ";
6245 }
6246 else
6247#endif
6248 {
6249 ++off;
6250 ++col;
6251 ++ptr;
6252 }
6253 }
6254}
6255
6256#ifdef FEAT_SEARCH_EXTRA
6257/*
6258 * Prepare for 'searchhl' highlighting.
6259 */
6260 static void
6261start_search_hl()
6262{
6263 if (p_hls && !no_hlsearch)
6264 {
6265 last_pat_prog(&search_hl.rm);
6266 search_hl.attr = hl_attr(HLF_L);
6267 }
6268}
6269
6270/*
6271 * Clean up for 'searchhl' highlighting.
6272 */
6273 static void
6274end_search_hl()
6275{
6276 if (search_hl.rm.regprog != NULL)
6277 {
6278 vim_free(search_hl.rm.regprog);
6279 search_hl.rm.regprog = NULL;
6280 }
6281}
6282
6283/*
6284 * Advance to the match in window "wp" line "lnum" or past it.
6285 */
6286 static void
6287prepare_search_hl(wp, lnum)
6288 win_T *wp;
6289 linenr_T lnum;
6290{
6291 match_T *shl; /* points to search_hl or match_hl */
6292 int n;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006293 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
6295 /*
6296 * When using a multi-line pattern, start searching at the top
6297 * of the window or just after a closed fold.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006298 * Do this both for search_hl and match_hl[3].
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006300 for (i = 3; i >= 0; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00006302 shl = (i == 3) ? &search_hl : &match_hl[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (shl->rm.regprog != NULL
6304 && shl->lnum == 0
6305 && re_multiline(shl->rm.regprog))
6306 {
6307 if (shl->first_lnum == 0)
6308 {
6309# ifdef FEAT_FOLDING
6310 for (shl->first_lnum = lnum;
6311 shl->first_lnum > wp->w_topline; --shl->first_lnum)
6312 if (hasFoldingWin(wp, shl->first_lnum - 1,
6313 NULL, NULL, TRUE, NULL))
6314 break;
6315# else
6316 shl->first_lnum = wp->w_topline;
6317# endif
6318 }
6319 n = 0;
6320 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
6321 {
6322 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
6323 if (shl->lnum != 0)
6324 {
6325 shl->first_lnum = shl->lnum
6326 + shl->rm.endpos[0].lnum
6327 - shl->rm.startpos[0].lnum;
6328 n = shl->rm.endpos[0].col;
6329 }
6330 else
6331 {
6332 ++shl->first_lnum;
6333 n = 0;
6334 }
6335 }
6336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 }
6338}
6339
6340/*
6341 * Search for a next 'searchl' or ":match" match.
6342 * Uses shl->buf.
6343 * Sets shl->lnum and shl->rm contents.
6344 * Note: Assumes a previous match is always before "lnum", unless
6345 * shl->lnum is zero.
6346 * Careful: Any pointers for buffer lines will become invalid.
6347 */
6348 static void
6349next_search_hl(win, shl, lnum, mincol)
6350 win_T *win;
6351 match_T *shl; /* points to search_hl or match_hl */
6352 linenr_T lnum;
6353 colnr_T mincol; /* minimal column for a match */
6354{
6355 linenr_T l;
6356 colnr_T matchcol;
6357 long nmatched;
6358
6359 if (shl->lnum != 0)
6360 {
6361 /* Check for three situations:
6362 * 1. If the "lnum" is below a previous match, start a new search.
6363 * 2. If the previous match includes "mincol", use it.
6364 * 3. Continue after the previous match.
6365 */
6366 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
6367 if (lnum > l)
6368 shl->lnum = 0;
6369 else if (lnum < l || shl->rm.endpos[0].col > mincol)
6370 return;
6371 }
6372
6373 /*
6374 * Repeat searching for a match until one is found that includes "mincol"
6375 * or none is found in this line.
6376 */
6377 called_emsg = FALSE;
6378 for (;;)
6379 {
6380 /* Three situations:
6381 * 1. No useful previous match: search from start of line.
6382 * 2. Not Vi compatible or empty match: continue at next character.
6383 * Break the loop if this is beyond the end of the line.
6384 * 3. Vi compatible searching: continue at end of previous match.
6385 */
6386 if (shl->lnum == 0)
6387 matchcol = 0;
6388 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
6389 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006390 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006392 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006393
6394 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006395 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006396 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006398 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 shl->lnum = 0;
6400 break;
6401 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006402#ifdef FEAT_MBYTE
6403 if (has_mbyte)
6404 matchcol += mb_ptr2len(ml);
6405 else
6406#endif
6407 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
6409 else
6410 matchcol = shl->rm.endpos[0].col;
6411
6412 shl->lnum = lnum;
6413 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol);
6414 if (called_emsg)
6415 {
6416 /* Error while handling regexp: stop using this regexp. */
6417 vim_free(shl->rm.regprog);
6418 shl->rm.regprog = NULL;
6419 no_hlsearch = TRUE;
6420 break;
6421 }
6422 if (nmatched == 0)
6423 {
6424 shl->lnum = 0; /* no match found */
6425 break;
6426 }
6427 if (shl->rm.startpos[0].lnum > 0
6428 || shl->rm.startpos[0].col >= mincol
6429 || nmatched > 1
6430 || shl->rm.endpos[0].col > mincol)
6431 {
6432 shl->lnum += shl->rm.startpos[0].lnum;
6433 break; /* useful match found */
6434 }
6435 }
6436}
6437#endif
6438
6439 static void
6440screen_start_highlight(attr)
6441 int attr;
6442{
6443 attrentry_T *aep = NULL;
6444
6445 screen_attr = attr;
6446 if (full_screen
6447#ifdef WIN3264
6448 && termcap_active
6449#endif
6450 )
6451 {
6452#ifdef FEAT_GUI
6453 if (gui.in_use)
6454 {
6455 char buf[20];
6456
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006457 /* The GUI handles this internally. */
6458 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 OUT_STR(buf);
6460 }
6461 else
6462#endif
6463 {
6464 if (attr > HL_ALL) /* special HL attr. */
6465 {
6466 if (t_colors > 1)
6467 aep = syn_cterm_attr2entry(attr);
6468 else
6469 aep = syn_term_attr2entry(attr);
6470 if (aep == NULL) /* did ":syntax clear" */
6471 attr = 0;
6472 else
6473 attr = aep->ae_attr;
6474 }
6475 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
6476 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006477 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
6478 && cterm_normal_fg_bold)
6479 /* If the Normal FG color has BOLD attribute and the new HL
6480 * has a FG color defined, clear BOLD. */
6481 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
6483 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006484 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
6485 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 out_str(T_US);
6487 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
6488 out_str(T_CZH);
6489 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
6490 out_str(T_MR);
6491
6492 /*
6493 * Output the color or start string after bold etc., in case the
6494 * bold etc. override the color setting.
6495 */
6496 if (aep != NULL)
6497 {
6498 if (t_colors > 1)
6499 {
6500 if (aep->ae_u.cterm.fg_color)
6501 term_fg_color(aep->ae_u.cterm.fg_color - 1);
6502 if (aep->ae_u.cterm.bg_color)
6503 term_bg_color(aep->ae_u.cterm.bg_color - 1);
6504 }
6505 else
6506 {
6507 if (aep->ae_u.term.start != NULL)
6508 out_str(aep->ae_u.term.start);
6509 }
6510 }
6511 }
6512 }
6513}
6514
6515 void
6516screen_stop_highlight()
6517{
6518 int do_ME = FALSE; /* output T_ME code */
6519
6520 if (screen_attr != 0
6521#ifdef WIN3264
6522 && termcap_active
6523#endif
6524 )
6525 {
6526#ifdef FEAT_GUI
6527 if (gui.in_use)
6528 {
6529 char buf[20];
6530
6531 /* use internal GUI code */
6532 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
6533 OUT_STR(buf);
6534 }
6535 else
6536#endif
6537 {
6538 if (screen_attr > HL_ALL) /* special HL attr. */
6539 {
6540 attrentry_T *aep;
6541
6542 if (t_colors > 1)
6543 {
6544 /*
6545 * Assume that t_me restores the original colors!
6546 */
6547 aep = syn_cterm_attr2entry(screen_attr);
6548 if (aep != NULL && (aep->ae_u.cterm.fg_color
6549 || aep->ae_u.cterm.bg_color))
6550 do_ME = TRUE;
6551 }
6552 else
6553 {
6554 aep = syn_term_attr2entry(screen_attr);
6555 if (aep != NULL && aep->ae_u.term.stop != NULL)
6556 {
6557 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
6558 do_ME = TRUE;
6559 else
6560 out_str(aep->ae_u.term.stop);
6561 }
6562 }
6563 if (aep == NULL) /* did ":syntax clear" */
6564 screen_attr = 0;
6565 else
6566 screen_attr = aep->ae_attr;
6567 }
6568
6569 /*
6570 * Often all ending-codes are equal to T_ME. Avoid outputting the
6571 * same sequence several times.
6572 */
6573 if (screen_attr & HL_STANDOUT)
6574 {
6575 if (STRCMP(T_SE, T_ME) == 0)
6576 do_ME = TRUE;
6577 else
6578 out_str(T_SE);
6579 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006580 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {
6582 if (STRCMP(T_UE, T_ME) == 0)
6583 do_ME = TRUE;
6584 else
6585 out_str(T_UE);
6586 }
6587 if (screen_attr & HL_ITALIC)
6588 {
6589 if (STRCMP(T_CZR, T_ME) == 0)
6590 do_ME = TRUE;
6591 else
6592 out_str(T_CZR);
6593 }
6594 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
6595 out_str(T_ME);
6596
6597 if (t_colors > 1)
6598 {
6599 /* set Normal cterm colors */
6600 if (cterm_normal_fg_color != 0)
6601 term_fg_color(cterm_normal_fg_color - 1);
6602 if (cterm_normal_bg_color != 0)
6603 term_bg_color(cterm_normal_bg_color - 1);
6604 if (cterm_normal_fg_bold)
6605 out_str(T_MD);
6606 }
6607 }
6608 }
6609 screen_attr = 0;
6610}
6611
6612/*
6613 * Reset the colors for a cterm. Used when leaving Vim.
6614 * The machine specific code may override this again.
6615 */
6616 void
6617reset_cterm_colors()
6618{
6619 if (t_colors > 1)
6620 {
6621 /* set Normal cterm colors */
6622 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
6623 {
6624 out_str(T_OP);
6625 screen_attr = -1;
6626 }
6627 if (cterm_normal_fg_bold)
6628 {
6629 out_str(T_ME);
6630 screen_attr = -1;
6631 }
6632 }
6633}
6634
6635/*
6636 * Put character ScreenLines["off"] on the screen at position "row" and "col",
6637 * using the attributes from ScreenAttrs["off"].
6638 */
6639 static void
6640screen_char(off, row, col)
6641 unsigned off;
6642 int row;
6643 int col;
6644{
6645 int attr;
6646
6647 /* Check for illegal values, just in case (could happen just after
6648 * resizing). */
6649 if (row >= screen_Rows || col >= screen_Columns)
6650 return;
6651
6652 /* Outputting the last character on the screen may scrollup the screen.
6653 * Don't to it! Mark the character invalid (update it when scrolled up) */
6654 if (row == screen_Rows - 1 && col == screen_Columns - 1
6655#ifdef FEAT_RIGHTLEFT
6656 /* account for first command-line character in rightleft mode */
6657 && !cmdmsg_rl
6658#endif
6659 )
6660 {
6661 ScreenAttrs[off] = (sattr_T)-1;
6662 return;
6663 }
6664
6665 /*
6666 * Stop highlighting first, so it's easier to move the cursor.
6667 */
6668#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
6669 if (screen_char_attr != 0)
6670 attr = screen_char_attr;
6671 else
6672#endif
6673 attr = ScreenAttrs[off];
6674 if (screen_attr != attr)
6675 screen_stop_highlight();
6676
6677 windgoto(row, col);
6678
6679 if (screen_attr != attr)
6680 screen_start_highlight(attr);
6681
6682#ifdef FEAT_MBYTE
6683 if (enc_utf8 && ScreenLinesUC[off] != 0)
6684 {
6685 char_u buf[MB_MAXBYTES + 1];
6686
6687 /* Convert UTF-8 character to bytes and write it. */
6688
6689 buf[utfc_char2bytes(off, buf)] = NUL;
6690
6691 out_str(buf);
6692 if (utf_char2cells(ScreenLinesUC[off]) > 1)
6693 ++screen_cur_col;
6694 }
6695 else
6696#endif
6697 {
6698#ifdef FEAT_MBYTE
6699 out_flush_check();
6700#endif
6701 out_char(ScreenLines[off]);
6702#ifdef FEAT_MBYTE
6703 /* double-byte character in single-width cell */
6704 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6705 out_char(ScreenLines2[off]);
6706#endif
6707 }
6708
6709 screen_cur_col++;
6710}
6711
6712#ifdef FEAT_MBYTE
6713
6714/*
6715 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
6716 * on the screen at position 'row' and 'col'.
6717 * The attributes of the first byte is used for all. This is required to
6718 * output the two bytes of a double-byte character with nothing in between.
6719 */
6720 static void
6721screen_char_2(off, row, col)
6722 unsigned off;
6723 int row;
6724 int col;
6725{
6726 /* Check for illegal values (could be wrong when screen was resized). */
6727 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
6728 return;
6729
6730 /* Outputting the last character on the screen may scrollup the screen.
6731 * Don't to it! Mark the character invalid (update it when scrolled up) */
6732 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
6733 {
6734 ScreenAttrs[off] = (sattr_T)-1;
6735 return;
6736 }
6737
6738 /* Output the first byte normally (positions the cursor), then write the
6739 * second byte directly. */
6740 screen_char(off, row, col);
6741 out_char(ScreenLines[off + 1]);
6742 ++screen_cur_col;
6743}
6744#endif
6745
6746#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
6747/*
6748 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
6749 * This uses the contents of ScreenLines[] and doesn't change it.
6750 */
6751 void
6752screen_draw_rectangle(row, col, height, width, invert)
6753 int row;
6754 int col;
6755 int height;
6756 int width;
6757 int invert;
6758{
6759 int r, c;
6760 int off;
6761
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006762 /* Can't use ScreenLines unless initialized */
6763 if (ScreenLines == NULL)
6764 return;
6765
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (invert)
6767 screen_char_attr = HL_INVERSE;
6768 for (r = row; r < row + height; ++r)
6769 {
6770 off = LineOffset[r];
6771 for (c = col; c < col + width; ++c)
6772 {
6773#ifdef FEAT_MBYTE
6774 if (enc_dbcs != 0 && dbcs_off2cells(off + c) > 1)
6775 {
6776 screen_char_2(off + c, r, c);
6777 ++c;
6778 }
6779 else
6780#endif
6781 {
6782 screen_char(off + c, r, c);
6783#ifdef FEAT_MBYTE
6784 if (utf_off2cells(off + c) > 1)
6785 ++c;
6786#endif
6787 }
6788 }
6789 }
6790 screen_char_attr = 0;
6791}
6792#endif
6793
6794#ifdef FEAT_VERTSPLIT
6795/*
6796 * Redraw the characters for a vertically split window.
6797 */
6798 static void
6799redraw_block(row, end, wp)
6800 int row;
6801 int end;
6802 win_T *wp;
6803{
6804 int col;
6805 int width;
6806
6807# ifdef FEAT_CLIPBOARD
6808 clip_may_clear_selection(row, end - 1);
6809# endif
6810
6811 if (wp == NULL)
6812 {
6813 col = 0;
6814 width = Columns;
6815 }
6816 else
6817 {
6818 col = wp->w_wincol;
6819 width = wp->w_width;
6820 }
6821 screen_draw_rectangle(row, col, end - row, width, FALSE);
6822}
6823#endif
6824
6825/*
6826 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
6827 * with character 'c1' in first column followed by 'c2' in the other columns.
6828 * Use attributes 'attr'.
6829 */
6830 void
6831screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
6832 int start_row, end_row;
6833 int start_col, end_col;
6834 int c1, c2;
6835 int attr;
6836{
6837 int row;
6838 int col;
6839 int off;
6840 int end_off;
6841 int did_delete;
6842 int c;
6843 int norm_term;
6844#if defined(FEAT_GUI) || defined(UNIX)
6845 int force_next = FALSE;
6846#endif
6847
6848 if (end_row > screen_Rows) /* safety check */
6849 end_row = screen_Rows;
6850 if (end_col > screen_Columns) /* safety check */
6851 end_col = screen_Columns;
6852 if (ScreenLines == NULL
6853 || start_row >= end_row
6854 || start_col >= end_col) /* nothing to do */
6855 return;
6856
6857 /* it's a "normal" terminal when not in a GUI or cterm */
6858 norm_term = (
6859#ifdef FEAT_GUI
6860 !gui.in_use &&
6861#endif
6862 t_colors <= 1);
6863 for (row = start_row; row < end_row; ++row)
6864 {
6865 /*
6866 * Try to use delete-line termcap code, when no attributes or in a
6867 * "normal" terminal, where a bold/italic space is just a
6868 * space.
6869 */
6870 did_delete = FALSE;
6871 if (c2 == ' '
6872 && end_col == Columns
6873 && can_clear(T_CE)
6874 && (attr == 0
6875 || (norm_term
6876 && attr <= HL_ALL
6877 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
6878 {
6879 /*
6880 * check if we really need to clear something
6881 */
6882 col = start_col;
6883 if (c1 != ' ') /* don't clear first char */
6884 ++col;
6885
6886 off = LineOffset[row] + col;
6887 end_off = LineOffset[row] + end_col;
6888
6889 /* skip blanks (used often, keep it fast!) */
6890#ifdef FEAT_MBYTE
6891 if (enc_utf8)
6892 while (off < end_off && ScreenLines[off] == ' '
6893 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
6894 ++off;
6895 else
6896#endif
6897 while (off < end_off && ScreenLines[off] == ' '
6898 && ScreenAttrs[off] == 0)
6899 ++off;
6900 if (off < end_off) /* something to be cleared */
6901 {
6902 col = off - LineOffset[row];
6903 screen_stop_highlight();
6904 term_windgoto(row, col);/* clear rest of this screen line */
6905 out_str(T_CE);
6906 screen_start(); /* don't know where cursor is now */
6907 col = end_col - col;
6908 while (col--) /* clear chars in ScreenLines */
6909 {
6910 ScreenLines[off] = ' ';
6911#ifdef FEAT_MBYTE
6912 if (enc_utf8)
6913 ScreenLinesUC[off] = 0;
6914#endif
6915 ScreenAttrs[off] = 0;
6916 ++off;
6917 }
6918 }
6919 did_delete = TRUE; /* the chars are cleared now */
6920 }
6921
6922 off = LineOffset[row] + start_col;
6923 c = c1;
6924 for (col = start_col; col < end_col; ++col)
6925 {
6926 if (ScreenLines[off] != c
6927#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006928 || (enc_utf8 && (int)ScreenLinesUC[off]
6929 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930#endif
6931 || ScreenAttrs[off] != attr
6932#if defined(FEAT_GUI) || defined(UNIX)
6933 || force_next
6934#endif
6935 )
6936 {
6937#if defined(FEAT_GUI) || defined(UNIX)
6938 /* The bold trick may make a single row of pixels appear in
6939 * the next character. When a bold character is removed, the
6940 * next character should be redrawn too. This happens for our
6941 * own GUI and for some xterms. */
6942 if (
6943# ifdef FEAT_GUI
6944 gui.in_use
6945# endif
6946# if defined(FEAT_GUI) && defined(UNIX)
6947 ||
6948# endif
6949# ifdef UNIX
6950 term_is_xterm
6951# endif
6952 )
6953 {
6954 if (ScreenLines[off] != ' '
6955 && (ScreenAttrs[off] > HL_ALL
6956 || ScreenAttrs[off] & HL_BOLD))
6957 force_next = TRUE;
6958 else
6959 force_next = FALSE;
6960 }
6961#endif
6962 ScreenLines[off] = c;
6963#ifdef FEAT_MBYTE
6964 if (enc_utf8)
6965 {
6966 if (c >= 0x80)
6967 {
6968 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006969 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
6971 else
6972 ScreenLinesUC[off] = 0;
6973 }
6974#endif
6975 ScreenAttrs[off] = attr;
6976 if (!did_delete || c != ' ')
6977 screen_char(off, row, col);
6978 }
6979 ++off;
6980 if (col == start_col)
6981 {
6982 if (did_delete)
6983 break;
6984 c = c2;
6985 }
6986 }
6987 if (end_col == Columns)
6988 LineWraps[row] = FALSE;
6989 if (row == Rows - 1) /* overwritten the command line */
6990 {
6991 redraw_cmdline = TRUE;
6992 if (c1 == ' ' && c2 == ' ')
6993 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00006994 if (start_col == 0)
6995 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
6997 }
6998}
6999
7000/*
7001 * Check if there should be a delay. Used before clearing or redrawing the
7002 * screen or the command line.
7003 */
7004 void
7005check_for_delay(check_msg_scroll)
7006 int check_msg_scroll;
7007{
7008 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
7009 && !did_wait_return
7010 && emsg_silent == 0)
7011 {
7012 out_flush();
7013 ui_delay(1000L, TRUE);
7014 emsg_on_display = FALSE;
7015 if (check_msg_scroll)
7016 msg_scroll = FALSE;
7017 }
7018}
7019
7020/*
7021 * screen_valid - allocate screen buffers if size changed
7022 * If "clear" is TRUE: clear screen if it has been resized.
7023 * Returns TRUE if there is a valid screen to write to.
7024 * Returns FALSE when starting up and screen not initialized yet.
7025 */
7026 int
7027screen_valid(clear)
7028 int clear;
7029{
7030 screenalloc(clear); /* allocate screen buffers if size changed */
7031 return (ScreenLines != NULL);
7032}
7033
7034/*
7035 * Resize the shell to Rows and Columns.
7036 * Allocate ScreenLines[] and associated items.
7037 *
7038 * There may be some time between setting Rows and Columns and (re)allocating
7039 * ScreenLines[]. This happens when starting up and when (manually) changing
7040 * the shell size. Always use screen_Rows and screen_Columns to access items
7041 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
7042 * final size of the shell is needed.
7043 */
7044 void
7045screenalloc(clear)
7046 int clear;
7047{
7048 int new_row, old_row;
7049#ifdef FEAT_GUI
7050 int old_Rows;
7051#endif
7052 win_T *wp;
7053 int outofmem = FALSE;
7054 int len;
7055 schar_T *new_ScreenLines;
7056#ifdef FEAT_MBYTE
7057 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007058 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007060 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061#endif
7062 sattr_T *new_ScreenAttrs;
7063 unsigned *new_LineOffset;
7064 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007065#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007066 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007067 tabpage_T *tp;
7068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007070 static int did_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
7072 /*
7073 * Allocation of the screen buffers is done only when the size changes and
7074 * when Rows and Columns have been set and we have started doing full
7075 * screen stuff.
7076 */
7077 if ((ScreenLines != NULL
7078 && Rows == screen_Rows
7079 && Columns == screen_Columns
7080#ifdef FEAT_MBYTE
7081 && enc_utf8 == (ScreenLinesUC != NULL)
7082 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007083 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084#endif
7085 )
7086 || Rows == 0
7087 || Columns == 0
7088 || (!full_screen && ScreenLines == NULL))
7089 return;
7090
7091 /*
7092 * It's possible that we produce an out-of-memory message below, which
7093 * will cause this function to be called again. To break the loop, just
7094 * return here.
7095 */
7096 if (entered)
7097 return;
7098 entered = TRUE;
7099
7100 win_new_shellsize(); /* fit the windows in the new sized shell */
7101
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 comp_col(); /* recompute columns for shown command and ruler */
7103
7104 /*
7105 * We're changing the size of the screen.
7106 * - Allocate new arrays for ScreenLines and ScreenAttrs.
7107 * - Move lines from the old arrays into the new arrays, clear extra
7108 * lines (unless the screen is going to be cleared).
7109 * - Free the old arrays.
7110 *
7111 * If anything fails, make ScreenLines NULL, so we don't do anything!
7112 * Continuing with the old ScreenLines may result in a crash, because the
7113 * size is wrong.
7114 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007115 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 win_free_lsize(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
7118 new_ScreenLines = (schar_T *)lalloc((long_u)(
7119 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7120#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007121 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 if (enc_utf8)
7123 {
7124 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
7125 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007126 for (i = 0; i < p_mco; ++i)
7127 new_ScreenLinesC[i] = (u8char_T *)lalloc((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
7129 }
7130 if (enc_dbcs == DBCS_JPNU)
7131 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
7132 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
7133#endif
7134 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
7135 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
7136 new_LineOffset = (unsigned *)lalloc((long_u)(
7137 Rows * sizeof(unsigned)), FALSE);
7138 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007139#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007140 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007143 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 {
7145 if (win_alloc_lines(wp) == FAIL)
7146 {
7147 outofmem = TRUE;
7148#ifdef FEAT_WINDOWS
7149 break;
7150#endif
7151 }
7152 }
7153
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007154#ifdef FEAT_MBYTE
7155 for (i = 0; i < p_mco; ++i)
7156 if (new_ScreenLinesC[i] == NULL)
7157 break;
7158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 if (new_ScreenLines == NULL
7160#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007161 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
7163#endif
7164 || new_ScreenAttrs == NULL
7165 || new_LineOffset == NULL
7166 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00007167#ifdef FEAT_WINDOWS
7168 || new_TabPageIdxs == NULL
7169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 || outofmem)
7171 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007172 if (ScreenLines != NULL || !did_outofmem_msg)
7173 {
7174 /* guess the size */
7175 do_outofmem_msg((long_u)((Rows + 1) * Columns));
7176
7177 /* Remember we did this to avoid getting outofmem messages over
7178 * and over again. */
7179 did_outofmem_msg = TRUE;
7180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 vim_free(new_ScreenLines);
7182 new_ScreenLines = NULL;
7183#ifdef FEAT_MBYTE
7184 vim_free(new_ScreenLinesUC);
7185 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007186 for (i = 0; i < p_mco; ++i)
7187 {
7188 vim_free(new_ScreenLinesC[i]);
7189 new_ScreenLinesC[i] = NULL;
7190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 vim_free(new_ScreenLines2);
7192 new_ScreenLines2 = NULL;
7193#endif
7194 vim_free(new_ScreenAttrs);
7195 new_ScreenAttrs = NULL;
7196 vim_free(new_LineOffset);
7197 new_LineOffset = NULL;
7198 vim_free(new_LineWraps);
7199 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007200#ifdef FEAT_WINDOWS
7201 vim_free(new_TabPageIdxs);
7202 new_TabPageIdxs = NULL;
7203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
7205 else
7206 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007207 did_outofmem_msg = FALSE;
7208
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 for (new_row = 0; new_row < Rows; ++new_row)
7210 {
7211 new_LineOffset[new_row] = new_row * Columns;
7212 new_LineWraps[new_row] = FALSE;
7213
7214 /*
7215 * If the screen is not going to be cleared, copy as much as
7216 * possible from the old screen to the new one and clear the rest
7217 * (used when resizing the window at the "--more--" prompt or when
7218 * executing an external command, for the GUI).
7219 */
7220 if (!clear)
7221 {
7222 (void)vim_memset(new_ScreenLines + new_row * Columns,
7223 ' ', (size_t)Columns * sizeof(schar_T));
7224#ifdef FEAT_MBYTE
7225 if (enc_utf8)
7226 {
7227 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
7228 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007229 for (i = 0; i < p_mco; ++i)
7230 (void)vim_memset(new_ScreenLinesC[i]
7231 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 0, (size_t)Columns * sizeof(u8char_T));
7233 }
7234 if (enc_dbcs == DBCS_JPNU)
7235 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
7236 0, (size_t)Columns * sizeof(schar_T));
7237#endif
7238 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
7239 0, (size_t)Columns * sizeof(sattr_T));
7240 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007241 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
7243 if (screen_Columns < Columns)
7244 len = screen_Columns;
7245 else
7246 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007247#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00007248 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007249 * may be invalid now. Also when p_mco changes. */
7250 if (!(enc_utf8 && ScreenLinesUC == NULL)
7251 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00007252#endif
7253 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
7254 ScreenLines + LineOffset[old_row],
7255 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007257 if (enc_utf8 && ScreenLinesUC != NULL
7258 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
7260 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
7261 ScreenLinesUC + LineOffset[old_row],
7262 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007263 for (i = 0; i < p_mco; ++i)
7264 mch_memmove(new_ScreenLinesC[i]
7265 + new_LineOffset[new_row],
7266 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 (size_t)len * sizeof(u8char_T));
7268 }
7269 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
7270 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
7271 ScreenLines2 + LineOffset[old_row],
7272 (size_t)len * sizeof(schar_T));
7273#endif
7274 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
7275 ScreenAttrs + LineOffset[old_row],
7276 (size_t)len * sizeof(sattr_T));
7277 }
7278 }
7279 }
7280 /* Use the last line of the screen for the current line. */
7281 current_ScreenLine = new_ScreenLines + Rows * Columns;
7282 }
7283
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007284 free_screenlines();
7285
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 ScreenLines = new_ScreenLines;
7287#ifdef FEAT_MBYTE
7288 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007289 for (i = 0; i < p_mco; ++i)
7290 ScreenLinesC[i] = new_ScreenLinesC[i];
7291 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 ScreenLines2 = new_ScreenLines2;
7293#endif
7294 ScreenAttrs = new_ScreenAttrs;
7295 LineOffset = new_LineOffset;
7296 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007297#ifdef FEAT_WINDOWS
7298 TabPageIdxs = new_TabPageIdxs;
7299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
7301 /* It's important that screen_Rows and screen_Columns reflect the actual
7302 * size of ScreenLines[]. Set them before calling anything. */
7303#ifdef FEAT_GUI
7304 old_Rows = screen_Rows;
7305#endif
7306 screen_Rows = Rows;
7307 screen_Columns = Columns;
7308
7309 must_redraw = CLEAR; /* need to clear the screen later */
7310 if (clear)
7311 screenclear2();
7312
7313#ifdef FEAT_GUI
7314 else if (gui.in_use
7315 && !gui.starting
7316 && ScreenLines != NULL
7317 && old_Rows != Rows)
7318 {
7319 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
7320 /*
7321 * Adjust the position of the cursor, for when executing an external
7322 * command.
7323 */
7324 if (msg_row >= Rows) /* Rows got smaller */
7325 msg_row = Rows - 1; /* put cursor at last row */
7326 else if (Rows > old_Rows) /* Rows got bigger */
7327 msg_row += Rows - old_Rows; /* put cursor in same place */
7328 if (msg_col >= Columns) /* Columns got smaller */
7329 msg_col = Columns - 1; /* put cursor at last column */
7330 }
7331#endif
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 entered = FALSE;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00007334
7335#ifdef FEAT_AUTOCMD
7336 if (starting == 0)
7337 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
7338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339}
7340
7341 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007342free_screenlines()
7343{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007344#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007345 int i;
7346
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007347 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007348 for (i = 0; i < Screen_mco; ++i)
7349 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007350 vim_free(ScreenLines2);
7351#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007352 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007353 vim_free(ScreenAttrs);
7354 vim_free(LineOffset);
7355 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007356#ifdef FEAT_WINDOWS
7357 vim_free(TabPageIdxs);
7358#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00007359}
7360
7361 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362screenclear()
7363{
7364 check_for_delay(FALSE);
7365 screenalloc(FALSE); /* allocate screen buffers if size changed */
7366 screenclear2(); /* clear the screen */
7367}
7368
7369 static void
7370screenclear2()
7371{
7372 int i;
7373
7374 if (starting == NO_SCREEN || ScreenLines == NULL
7375#ifdef FEAT_GUI
7376 || (gui.in_use && gui.starting)
7377#endif
7378 )
7379 return;
7380
7381#ifdef FEAT_GUI
7382 if (!gui.in_use)
7383#endif
7384 screen_attr = -1; /* force setting the Normal colors */
7385 screen_stop_highlight(); /* don't want highlighting here */
7386
7387#ifdef FEAT_CLIPBOARD
7388 /* disable selection without redrawing it */
7389 clip_scroll_selection(9999);
7390#endif
7391
7392 /* blank out ScreenLines */
7393 for (i = 0; i < Rows; ++i)
7394 {
7395 lineclear(LineOffset[i], (int)Columns);
7396 LineWraps[i] = FALSE;
7397 }
7398
7399 if (can_clear(T_CL))
7400 {
7401 out_str(T_CL); /* clear the display */
7402 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00007403 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 }
7405 else
7406 {
7407 /* can't clear the screen, mark all chars with invalid attributes */
7408 for (i = 0; i < Rows; ++i)
7409 lineinvalid(LineOffset[i], (int)Columns);
7410 clear_cmdline = TRUE;
7411 }
7412
7413 screen_cleared = TRUE; /* can use contents of ScreenLines now */
7414
7415 win_rest_invalid(firstwin);
7416 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007417#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00007418 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00007419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if (must_redraw == CLEAR) /* no need to clear again */
7421 must_redraw = NOT_VALID;
7422 compute_cmdrow();
7423 msg_row = cmdline_row; /* put cursor on last line for messages */
7424 msg_col = 0;
7425 screen_start(); /* don't know where cursor is now */
7426 msg_scrolled = 0; /* can't scroll back */
7427 msg_didany = FALSE;
7428 msg_didout = FALSE;
7429}
7430
7431/*
7432 * Clear one line in ScreenLines.
7433 */
7434 static void
7435lineclear(off, width)
7436 unsigned off;
7437 int width;
7438{
7439 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
7440#ifdef FEAT_MBYTE
7441 if (enc_utf8)
7442 (void)vim_memset(ScreenLinesUC + off, 0,
7443 (size_t)width * sizeof(u8char_T));
7444#endif
7445 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
7446}
7447
7448/*
7449 * Mark one line in ScreenLines invalid by setting the attributes to an
7450 * invalid value.
7451 */
7452 static void
7453lineinvalid(off, width)
7454 unsigned off;
7455 int width;
7456{
7457 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
7458}
7459
7460#ifdef FEAT_VERTSPLIT
7461/*
7462 * Copy part of a Screenline for vertically split window "wp".
7463 */
7464 static void
7465linecopy(to, from, wp)
7466 int to;
7467 int from;
7468 win_T *wp;
7469{
7470 unsigned off_to = LineOffset[to] + wp->w_wincol;
7471 unsigned off_from = LineOffset[from] + wp->w_wincol;
7472
7473 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
7474 wp->w_width * sizeof(schar_T));
7475# ifdef FEAT_MBYTE
7476 if (enc_utf8)
7477 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007478 int i;
7479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
7481 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007482 for (i = 0; i < p_mco; ++i)
7483 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
7484 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486 if (enc_dbcs == DBCS_JPNU)
7487 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
7488 wp->w_width * sizeof(schar_T));
7489# endif
7490 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
7491 wp->w_width * sizeof(sattr_T));
7492}
7493#endif
7494
7495/*
7496 * Return TRUE if clearing with term string "p" would work.
7497 * It can't work when the string is empty or it won't set the right background.
7498 */
7499 int
7500can_clear(p)
7501 char_u *p;
7502{
7503 return (*p != NUL && (t_colors <= 1
7504#ifdef FEAT_GUI
7505 || gui.in_use
7506#endif
7507 || cterm_normal_bg_color == 0 || *T_UT != NUL));
7508}
7509
7510/*
7511 * Reset cursor position. Use whenever cursor was moved because of outputting
7512 * something directly to the screen (shell commands) or a terminal control
7513 * code.
7514 */
7515 void
7516screen_start()
7517{
7518 screen_cur_row = screen_cur_col = 9999;
7519}
7520
7521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 * Move the cursor to position "row","col" in the screen.
7523 * This tries to find the most efficient way to move, minimizing the number of
7524 * characters sent to the terminal.
7525 */
7526 void
7527windgoto(row, col)
7528 int row;
7529 int col;
7530{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007531 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 int i;
7533 int plan;
7534 int cost;
7535 int wouldbe_col;
7536 int noinvcurs;
7537 char_u *bs;
7538 int goto_cost;
7539 int attr;
7540
7541#define GOTO_COST 7 /* asssume a term_windgoto() takes about 7 chars */
7542#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
7543
7544#define PLAN_LE 1
7545#define PLAN_CR 2
7546#define PLAN_NL 3
7547#define PLAN_WRITE 4
7548 /* Can't use ScreenLines unless initialized */
7549 if (ScreenLines == NULL)
7550 return;
7551
7552 if (col != screen_cur_col || row != screen_cur_row)
7553 {
7554 /* Check for valid position. */
7555 if (row < 0) /* window without text lines? */
7556 row = 0;
7557 if (row >= screen_Rows)
7558 row = screen_Rows - 1;
7559 if (col >= screen_Columns)
7560 col = screen_Columns - 1;
7561
7562 /* check if no cursor movement is allowed in highlight mode */
7563 if (screen_attr && *T_MS == NUL)
7564 noinvcurs = HIGHL_COST;
7565 else
7566 noinvcurs = 0;
7567 goto_cost = GOTO_COST + noinvcurs;
7568
7569 /*
7570 * Plan how to do the positioning:
7571 * 1. Use CR to move it to column 0, same row.
7572 * 2. Use T_LE to move it a few columns to the left.
7573 * 3. Use NL to move a few lines down, column 0.
7574 * 4. Move a few columns to the right with T_ND or by writing chars.
7575 *
7576 * Don't do this if the cursor went beyond the last column, the cursor
7577 * position is unknown then (some terminals wrap, some don't )
7578 *
7579 * First check if the highlighting attibutes allow us to write
7580 * characters to move the cursor to the right.
7581 */
7582 if (row >= screen_cur_row && screen_cur_col < Columns)
7583 {
7584 /*
7585 * If the cursor is in the same row, bigger col, we can use CR
7586 * or T_LE.
7587 */
7588 bs = NULL; /* init for GCC */
7589 attr = screen_attr;
7590 if (row == screen_cur_row && col < screen_cur_col)
7591 {
7592 /* "le" is preferred over "bc", because "bc" is obsolete */
7593 if (*T_LE)
7594 bs = T_LE; /* "cursor left" */
7595 else
7596 bs = T_BC; /* "backspace character (old) */
7597 if (*bs)
7598 cost = (screen_cur_col - col) * (int)STRLEN(bs);
7599 else
7600 cost = 999;
7601 if (col + 1 < cost) /* using CR is less characters */
7602 {
7603 plan = PLAN_CR;
7604 wouldbe_col = 0;
7605 cost = 1; /* CR is just one character */
7606 }
7607 else
7608 {
7609 plan = PLAN_LE;
7610 wouldbe_col = col;
7611 }
7612 if (noinvcurs) /* will stop highlighting */
7613 {
7614 cost += noinvcurs;
7615 attr = 0;
7616 }
7617 }
7618
7619 /*
7620 * If the cursor is above where we want to be, we can use CR LF.
7621 */
7622 else if (row > screen_cur_row)
7623 {
7624 plan = PLAN_NL;
7625 wouldbe_col = 0;
7626 cost = (row - screen_cur_row) * 2; /* CR LF */
7627 if (noinvcurs) /* will stop highlighting */
7628 {
7629 cost += noinvcurs;
7630 attr = 0;
7631 }
7632 }
7633
7634 /*
7635 * If the cursor is in the same row, smaller col, just use write.
7636 */
7637 else
7638 {
7639 plan = PLAN_WRITE;
7640 wouldbe_col = screen_cur_col;
7641 cost = 0;
7642 }
7643
7644 /*
7645 * Check if any characters that need to be written have the
7646 * correct attributes. Also avoid UTF-8 characters.
7647 */
7648 i = col - wouldbe_col;
7649 if (i > 0)
7650 cost += i;
7651 if (cost < goto_cost && i > 0)
7652 {
7653 /*
7654 * Check if the attributes are correct without additionally
7655 * stopping highlighting.
7656 */
7657 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
7658 while (i && *p++ == attr)
7659 --i;
7660 if (i != 0)
7661 {
7662 /*
7663 * Try if it works when highlighting is stopped here.
7664 */
7665 if (*--p == 0)
7666 {
7667 cost += noinvcurs;
7668 while (i && *p++ == 0)
7669 --i;
7670 }
7671 if (i != 0)
7672 cost = 999; /* different attributes, don't do it */
7673 }
7674#ifdef FEAT_MBYTE
7675 if (enc_utf8)
7676 {
7677 /* Don't use an UTF-8 char for positioning, it's slow. */
7678 for (i = wouldbe_col; i < col; ++i)
7679 if (ScreenLinesUC[LineOffset[row] + i] != 0)
7680 {
7681 cost = 999;
7682 break;
7683 }
7684 }
7685#endif
7686 }
7687
7688 /*
7689 * We can do it without term_windgoto()!
7690 */
7691 if (cost < goto_cost)
7692 {
7693 if (plan == PLAN_LE)
7694 {
7695 if (noinvcurs)
7696 screen_stop_highlight();
7697 while (screen_cur_col > col)
7698 {
7699 out_str(bs);
7700 --screen_cur_col;
7701 }
7702 }
7703 else if (plan == PLAN_CR)
7704 {
7705 if (noinvcurs)
7706 screen_stop_highlight();
7707 out_char('\r');
7708 screen_cur_col = 0;
7709 }
7710 else if (plan == PLAN_NL)
7711 {
7712 if (noinvcurs)
7713 screen_stop_highlight();
7714 while (screen_cur_row < row)
7715 {
7716 out_char('\n');
7717 ++screen_cur_row;
7718 }
7719 screen_cur_col = 0;
7720 }
7721
7722 i = col - screen_cur_col;
7723 if (i > 0)
7724 {
7725 /*
7726 * Use cursor-right if it's one character only. Avoids
7727 * removing a line of pixels from the last bold char, when
7728 * using the bold trick in the GUI.
7729 */
7730 if (T_ND[0] != NUL && T_ND[1] == NUL)
7731 {
7732 while (i-- > 0)
7733 out_char(*T_ND);
7734 }
7735 else
7736 {
7737 int off;
7738
7739 off = LineOffset[row] + screen_cur_col;
7740 while (i-- > 0)
7741 {
7742 if (ScreenAttrs[off] != screen_attr)
7743 screen_stop_highlight();
7744#ifdef FEAT_MBYTE
7745 out_flush_check();
7746#endif
7747 out_char(ScreenLines[off]);
7748#ifdef FEAT_MBYTE
7749 if (enc_dbcs == DBCS_JPNU
7750 && ScreenLines[off] == 0x8e)
7751 out_char(ScreenLines2[off]);
7752#endif
7753 ++off;
7754 }
7755 }
7756 }
7757 }
7758 }
7759 else
7760 cost = 999;
7761
7762 if (cost >= goto_cost)
7763 {
7764 if (noinvcurs)
7765 screen_stop_highlight();
7766 if (row == screen_cur_row && (col > screen_cur_col) &&
7767 *T_CRI != NUL)
7768 term_cursor_right(col - screen_cur_col);
7769 else
7770 term_windgoto(row, col);
7771 }
7772 screen_cur_row = row;
7773 screen_cur_col = col;
7774 }
7775}
7776
7777/*
7778 * Set cursor to its position in the current window.
7779 */
7780 void
7781setcursor()
7782{
7783 if (redrawing())
7784 {
7785 validate_cursor();
7786 windgoto(W_WINROW(curwin) + curwin->w_wrow,
7787 W_WINCOL(curwin) + (
7788#ifdef FEAT_RIGHTLEFT
7789 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
7790# ifdef FEAT_MBYTE
7791 has_mbyte ? (*mb_ptr2cells)(ml_get_cursor()) :
7792# endif
7793 1)) :
7794#endif
7795 curwin->w_wcol));
7796 }
7797}
7798
7799
7800/*
7801 * insert 'line_count' lines at 'row' in window 'wp'
7802 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
7803 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
7804 * scrolling.
7805 * Returns FAIL if the lines are not inserted, OK for success.
7806 */
7807 int
7808win_ins_lines(wp, row, line_count, invalid, mayclear)
7809 win_T *wp;
7810 int row;
7811 int line_count;
7812 int invalid;
7813 int mayclear;
7814{
7815 int did_delete;
7816 int nextrow;
7817 int lastrow;
7818 int retval;
7819
7820 if (invalid)
7821 wp->w_lines_valid = 0;
7822
7823 if (wp->w_height < 5)
7824 return FAIL;
7825
7826 if (line_count > wp->w_height - row)
7827 line_count = wp->w_height - row;
7828
7829 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
7830 if (retval != MAYBE)
7831 return retval;
7832
7833 /*
7834 * If there is a next window or a status line, we first try to delete the
7835 * lines at the bottom to avoid messing what is after the window.
7836 * If this fails and there are following windows, don't do anything to avoid
7837 * messing up those windows, better just redraw.
7838 */
7839 did_delete = FALSE;
7840#ifdef FEAT_WINDOWS
7841 if (wp->w_next != NULL || wp->w_status_height)
7842 {
7843 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7844 line_count, (int)Rows, FALSE, NULL) == OK)
7845 did_delete = TRUE;
7846 else if (wp->w_next)
7847 return FAIL;
7848 }
7849#endif
7850 /*
7851 * if no lines deleted, blank the lines that will end up below the window
7852 */
7853 if (!did_delete)
7854 {
7855#ifdef FEAT_WINDOWS
7856 wp->w_redr_status = TRUE;
7857#endif
7858 redraw_cmdline = TRUE;
7859 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
7860 lastrow = nextrow + line_count;
7861 if (lastrow > Rows)
7862 lastrow = Rows;
7863 screen_fill(nextrow - line_count, lastrow - line_count,
7864 W_WINCOL(wp), (int)W_ENDCOL(wp),
7865 ' ', ' ', 0);
7866 }
7867
7868 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
7869 == FAIL)
7870 {
7871 /* deletion will have messed up other windows */
7872 if (did_delete)
7873 {
7874#ifdef FEAT_WINDOWS
7875 wp->w_redr_status = TRUE;
7876#endif
7877 win_rest_invalid(W_NEXT(wp));
7878 }
7879 return FAIL;
7880 }
7881
7882 return OK;
7883}
7884
7885/*
7886 * delete "line_count" window lines at "row" in window "wp"
7887 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
7888 * If "mayclear" is TRUE the screen will be cleared if it is faster than
7889 * scrolling
7890 * Return OK for success, FAIL if the lines are not deleted.
7891 */
7892 int
7893win_del_lines(wp, row, line_count, invalid, mayclear)
7894 win_T *wp;
7895 int row;
7896 int line_count;
7897 int invalid;
7898 int mayclear;
7899{
7900 int retval;
7901
7902 if (invalid)
7903 wp->w_lines_valid = 0;
7904
7905 if (line_count > wp->w_height - row)
7906 line_count = wp->w_height - row;
7907
7908 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
7909 if (retval != MAYBE)
7910 return retval;
7911
7912 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
7913 (int)Rows, FALSE, NULL) == FAIL)
7914 return FAIL;
7915
7916#ifdef FEAT_WINDOWS
7917 /*
7918 * If there are windows or status lines below, try to put them at the
7919 * correct place. If we can't do that, they have to be redrawn.
7920 */
7921 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
7922 {
7923 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
7924 line_count, (int)Rows, NULL) == FAIL)
7925 {
7926 wp->w_redr_status = TRUE;
7927 win_rest_invalid(wp->w_next);
7928 }
7929 }
7930 /*
7931 * If this is the last window and there is no status line, redraw the
7932 * command line later.
7933 */
7934 else
7935#endif
7936 redraw_cmdline = TRUE;
7937 return OK;
7938}
7939
7940/*
7941 * Common code for win_ins_lines() and win_del_lines().
7942 * Returns OK or FAIL when the work has been done.
7943 * Returns MAYBE when not finished yet.
7944 */
7945 static int
7946win_do_lines(wp, row, line_count, mayclear, del)
7947 win_T *wp;
7948 int row;
7949 int line_count;
7950 int mayclear;
7951 int del;
7952{
7953 int retval;
7954
7955 if (!redrawing() || line_count <= 0)
7956 return FAIL;
7957
7958 /* only a few lines left: redraw is faster */
7959 if (mayclear && Rows - line_count < 5
7960#ifdef FEAT_VERTSPLIT
7961 && wp->w_width == Columns
7962#endif
7963 )
7964 {
7965 screenclear(); /* will set wp->w_lines_valid to 0 */
7966 return FAIL;
7967 }
7968
7969 /*
7970 * Delete all remaining lines
7971 */
7972 if (row + line_count >= wp->w_height)
7973 {
7974 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
7975 W_WINCOL(wp), (int)W_ENDCOL(wp),
7976 ' ', ' ', 0);
7977 return OK;
7978 }
7979
7980 /*
7981 * when scrolling, the message on the command line should be cleared,
7982 * otherwise it will stay there forever.
7983 */
7984 clear_cmdline = TRUE;
7985
7986 /*
7987 * If the terminal can set a scroll region, use that.
7988 * Always do this in a vertically split window. This will redraw from
7989 * ScreenLines[] when t_CV isn't defined. That's faster than using
7990 * win_line().
7991 * Don't use a scroll region when we are going to redraw the text, writing
7992 * a character in the lower right corner of the scroll region causes a
7993 * scroll-up in the DJGPP version.
7994 */
7995 if (scroll_region
7996#ifdef FEAT_VERTSPLIT
7997 || W_WIDTH(wp) != Columns
7998#endif
7999 )
8000 {
8001#ifdef FEAT_VERTSPLIT
8002 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8003#endif
8004 scroll_region_set(wp, row);
8005 if (del)
8006 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
8007 wp->w_height - row, FALSE, wp);
8008 else
8009 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
8010 wp->w_height - row, wp);
8011#ifdef FEAT_VERTSPLIT
8012 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
8013#endif
8014 scroll_region_reset();
8015 return retval;
8016 }
8017
8018#ifdef FEAT_WINDOWS
8019 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
8020 return FAIL;
8021#endif
8022
8023 return MAYBE;
8024}
8025
8026/*
8027 * window 'wp' and everything after it is messed up, mark it for redraw
8028 */
8029 static void
8030win_rest_invalid(wp)
8031 win_T *wp;
8032{
8033#ifdef FEAT_WINDOWS
8034 while (wp != NULL)
8035#else
8036 if (wp != NULL)
8037#endif
8038 {
8039 redraw_win_later(wp, NOT_VALID);
8040#ifdef FEAT_WINDOWS
8041 wp->w_redr_status = TRUE;
8042 wp = wp->w_next;
8043#endif
8044 }
8045 redraw_cmdline = TRUE;
8046}
8047
8048/*
8049 * The rest of the routines in this file perform screen manipulations. The
8050 * given operation is performed physically on the screen. The corresponding
8051 * change is also made to the internal screen image. In this way, the editor
8052 * anticipates the effect of editing changes on the appearance of the screen.
8053 * That way, when we call screenupdate a complete redraw isn't usually
8054 * necessary. Another advantage is that we can keep adding code to anticipate
8055 * screen changes, and in the meantime, everything still works.
8056 */
8057
8058/*
8059 * types for inserting or deleting lines
8060 */
8061#define USE_T_CAL 1
8062#define USE_T_CDL 2
8063#define USE_T_AL 3
8064#define USE_T_CE 4
8065#define USE_T_DL 5
8066#define USE_T_SR 6
8067#define USE_NL 7
8068#define USE_T_CD 8
8069#define USE_REDRAW 9
8070
8071/*
8072 * insert lines on the screen and update ScreenLines[]
8073 * 'end' is the line after the scrolled part. Normally it is Rows.
8074 * When scrolling region used 'off' is the offset from the top for the region.
8075 * 'row' and 'end' are relative to the start of the region.
8076 *
8077 * return FAIL for failure, OK for success.
8078 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008079 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080screen_ins_lines(off, row, line_count, end, wp)
8081 int off;
8082 int row;
8083 int line_count;
8084 int end;
8085 win_T *wp; /* NULL or window to use width from */
8086{
8087 int i;
8088 int j;
8089 unsigned temp;
8090 int cursor_row;
8091 int type;
8092 int result_empty;
8093 int can_ce = can_clear(T_CE);
8094
8095 /*
8096 * FAIL if
8097 * - there is no valid screen
8098 * - the screen has to be redrawn completely
8099 * - the line count is less than one
8100 * - the line count is more than 'ttyscroll'
8101 */
8102 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
8103 return FAIL;
8104
8105 /*
8106 * There are seven ways to insert lines:
8107 * 0. When in a vertically split window and t_CV isn't set, redraw the
8108 * characters from ScreenLines[].
8109 * 1. Use T_CD (clear to end of display) if it exists and the result of
8110 * the insert is just empty lines
8111 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
8112 * present or line_count > 1. It looks better if we do all the inserts
8113 * at once.
8114 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
8115 * insert is just empty lines and T_CE is not present or line_count >
8116 * 1.
8117 * 4. Use T_AL (insert line) if it exists.
8118 * 5. Use T_CE (erase line) if it exists and the result of the insert is
8119 * just empty lines.
8120 * 6. Use T_DL (delete line) if it exists and the result of the insert is
8121 * just empty lines.
8122 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
8123 * the 'da' flag is not set or we have clear line capability.
8124 * 8. redraw the characters from ScreenLines[].
8125 *
8126 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
8127 * the scrollbar for the window. It does have insert line, use that if it
8128 * exists.
8129 */
8130 result_empty = (row + line_count >= end);
8131#ifdef FEAT_VERTSPLIT
8132 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8133 type = USE_REDRAW;
8134 else
8135#endif
8136 if (can_clear(T_CD) && result_empty)
8137 type = USE_T_CD;
8138 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
8139 type = USE_T_CAL;
8140 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
8141 type = USE_T_CDL;
8142 else if (*T_AL != NUL)
8143 type = USE_T_AL;
8144 else if (can_ce && result_empty)
8145 type = USE_T_CE;
8146 else if (*T_DL != NUL && result_empty)
8147 type = USE_T_DL;
8148 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
8149 type = USE_T_SR;
8150 else
8151 return FAIL;
8152
8153 /*
8154 * For clearing the lines screen_del_lines() is used. This will also take
8155 * care of t_db if necessary.
8156 */
8157 if (type == USE_T_CD || type == USE_T_CDL ||
8158 type == USE_T_CE || type == USE_T_DL)
8159 return screen_del_lines(off, row, line_count, end, FALSE, wp);
8160
8161 /*
8162 * If text is retained below the screen, first clear or delete as many
8163 * lines at the bottom of the window as are about to be inserted so that
8164 * the deleted lines won't later surface during a screen_del_lines.
8165 */
8166 if (*T_DB)
8167 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
8168
8169#ifdef FEAT_CLIPBOARD
8170 /* Remove a modeless selection when inserting lines halfway the screen
8171 * or not the full width of the screen. */
8172 if (off + row > 0
8173# ifdef FEAT_VERTSPLIT
8174 || (wp != NULL && wp->w_width != Columns)
8175# endif
8176 )
8177 clip_clear_selection();
8178 else
8179 clip_scroll_selection(-line_count);
8180#endif
8181
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182#ifdef FEAT_GUI
8183 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8184 * scrolling is actually carried out. */
8185 gui_dont_update_cursor();
8186#endif
8187
8188 if (*T_CCS != NUL) /* cursor relative to region */
8189 cursor_row = row;
8190 else
8191 cursor_row = row + off;
8192
8193 /*
8194 * Shift LineOffset[] line_count down to reflect the inserted lines.
8195 * Clear the inserted lines in ScreenLines[].
8196 */
8197 row += off;
8198 end += off;
8199 for (i = 0; i < line_count; ++i)
8200 {
8201#ifdef FEAT_VERTSPLIT
8202 if (wp != NULL && wp->w_width != Columns)
8203 {
8204 /* need to copy part of a line */
8205 j = end - 1 - i;
8206 while ((j -= line_count) >= row)
8207 linecopy(j + line_count, j, wp);
8208 j += line_count;
8209 if (can_clear((char_u *)" "))
8210 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8211 else
8212 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8213 LineWraps[j] = FALSE;
8214 }
8215 else
8216#endif
8217 {
8218 j = end - 1 - i;
8219 temp = LineOffset[j];
8220 while ((j -= line_count) >= row)
8221 {
8222 LineOffset[j + line_count] = LineOffset[j];
8223 LineWraps[j + line_count] = LineWraps[j];
8224 }
8225 LineOffset[j + line_count] = temp;
8226 LineWraps[j + line_count] = FALSE;
8227 if (can_clear((char_u *)" "))
8228 lineclear(temp, (int)Columns);
8229 else
8230 lineinvalid(temp, (int)Columns);
8231 }
8232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
8234 screen_stop_highlight();
8235 windgoto(cursor_row, 0);
8236
8237#ifdef FEAT_VERTSPLIT
8238 /* redraw the characters */
8239 if (type == USE_REDRAW)
8240 redraw_block(row, end, wp);
8241 else
8242#endif
8243 if (type == USE_T_CAL)
8244 {
8245 term_append_lines(line_count);
8246 screen_start(); /* don't know where cursor is now */
8247 }
8248 else
8249 {
8250 for (i = 0; i < line_count; i++)
8251 {
8252 if (type == USE_T_AL)
8253 {
8254 if (i && cursor_row != 0)
8255 windgoto(cursor_row, 0);
8256 out_str(T_AL);
8257 }
8258 else /* type == USE_T_SR */
8259 out_str(T_SR);
8260 screen_start(); /* don't know where cursor is now */
8261 }
8262 }
8263
8264 /*
8265 * With scroll-reverse and 'da' flag set we need to clear the lines that
8266 * have been scrolled down into the region.
8267 */
8268 if (type == USE_T_SR && *T_DA)
8269 {
8270 for (i = 0; i < line_count; ++i)
8271 {
8272 windgoto(off + i, 0);
8273 out_str(T_CE);
8274 screen_start(); /* don't know where cursor is now */
8275 }
8276 }
8277
8278#ifdef FEAT_GUI
8279 gui_can_update_cursor();
8280 if (gui.in_use)
8281 out_flush(); /* always flush after a scroll */
8282#endif
8283 return OK;
8284}
8285
8286/*
8287 * delete lines on the screen and update ScreenLines[]
8288 * 'end' is the line after the scrolled part. Normally it is Rows.
8289 * When scrolling region used 'off' is the offset from the top for the region.
8290 * 'row' and 'end' are relative to the start of the region.
8291 *
8292 * Return OK for success, FAIL if the lines are not deleted.
8293 */
8294/*ARGSUSED*/
8295 int
8296screen_del_lines(off, row, line_count, end, force, wp)
8297 int off;
8298 int row;
8299 int line_count;
8300 int end;
8301 int force; /* even when line_count > p_ttyscroll */
8302 win_T *wp; /* NULL or window to use width from */
8303{
8304 int j;
8305 int i;
8306 unsigned temp;
8307 int cursor_row;
8308 int cursor_end;
8309 int result_empty; /* result is empty until end of region */
8310 int can_delete; /* deleting line codes can be used */
8311 int type;
8312
8313 /*
8314 * FAIL if
8315 * - there is no valid screen
8316 * - the screen has to be redrawn completely
8317 * - the line count is less than one
8318 * - the line count is more than 'ttyscroll'
8319 */
8320 if (!screen_valid(TRUE) || line_count <= 0 ||
8321 (!force && line_count > p_ttyscroll))
8322 return FAIL;
8323
8324 /*
8325 * Check if the rest of the current region will become empty.
8326 */
8327 result_empty = row + line_count >= end;
8328
8329 /*
8330 * We can delete lines only when 'db' flag not set or when 'ce' option
8331 * available.
8332 */
8333 can_delete = (*T_DB == NUL || can_clear(T_CE));
8334
8335 /*
8336 * There are six ways to delete lines:
8337 * 0. When in a vertically split window and t_CV isn't set, redraw the
8338 * characters from ScreenLines[].
8339 * 1. Use T_CD if it exists and the result is empty.
8340 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
8341 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
8342 * none of the other ways work.
8343 * 4. Use T_CE (erase line) if the result is empty.
8344 * 5. Use T_DL (delete line) if it exists.
8345 * 6. redraw the characters from ScreenLines[].
8346 */
8347#ifdef FEAT_VERTSPLIT
8348 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
8349 type = USE_REDRAW;
8350 else
8351#endif
8352 if (can_clear(T_CD) && result_empty)
8353 type = USE_T_CD;
8354#if defined(__BEOS__) && defined(BEOS_DR8)
8355 /*
8356 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
8357 * its internal termcap... this works okay for tests which test *T_DB !=
8358 * NUL. It has the disadvantage that the user cannot use any :set t_*
8359 * command to get T_DB (back) to empty_option, only :set term=... will do
8360 * the trick...
8361 * Anyway, this hack will hopefully go away with the next OS release.
8362 * (Olaf Seibert)
8363 */
8364 else if (row == 0 && T_DB == empty_option
8365 && (line_count == 1 || *T_CDL == NUL))
8366#else
8367 else if (row == 0 && (
8368#ifndef AMIGA
8369 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
8370 * up, so use delete-line command */
8371 line_count == 1 ||
8372#endif
8373 *T_CDL == NUL))
8374#endif
8375 type = USE_NL;
8376 else if (*T_CDL != NUL && line_count > 1 && can_delete)
8377 type = USE_T_CDL;
8378 else if (can_clear(T_CE) && result_empty
8379#ifdef FEAT_VERTSPLIT
8380 && (wp == NULL || wp->w_width == Columns)
8381#endif
8382 )
8383 type = USE_T_CE;
8384 else if (*T_DL != NUL && can_delete)
8385 type = USE_T_DL;
8386 else if (*T_CDL != NUL && can_delete)
8387 type = USE_T_CDL;
8388 else
8389 return FAIL;
8390
8391#ifdef FEAT_CLIPBOARD
8392 /* Remove a modeless selection when deleting lines halfway the screen or
8393 * not the full width of the screen. */
8394 if (off + row > 0
8395# ifdef FEAT_VERTSPLIT
8396 || (wp != NULL && wp->w_width != Columns)
8397# endif
8398 )
8399 clip_clear_selection();
8400 else
8401 clip_scroll_selection(line_count);
8402#endif
8403
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404#ifdef FEAT_GUI
8405 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
8406 * scrolling is actually carried out. */
8407 gui_dont_update_cursor();
8408#endif
8409
8410 if (*T_CCS != NUL) /* cursor relative to region */
8411 {
8412 cursor_row = row;
8413 cursor_end = end;
8414 }
8415 else
8416 {
8417 cursor_row = row + off;
8418 cursor_end = end + off;
8419 }
8420
8421 /*
8422 * Now shift LineOffset[] line_count up to reflect the deleted lines.
8423 * Clear the inserted lines in ScreenLines[].
8424 */
8425 row += off;
8426 end += off;
8427 for (i = 0; i < line_count; ++i)
8428 {
8429#ifdef FEAT_VERTSPLIT
8430 if (wp != NULL && wp->w_width != Columns)
8431 {
8432 /* need to copy part of a line */
8433 j = row + i;
8434 while ((j += line_count) <= end - 1)
8435 linecopy(j - line_count, j, wp);
8436 j -= line_count;
8437 if (can_clear((char_u *)" "))
8438 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
8439 else
8440 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
8441 LineWraps[j] = FALSE;
8442 }
8443 else
8444#endif
8445 {
8446 /* whole width, moving the line pointers is faster */
8447 j = row + i;
8448 temp = LineOffset[j];
8449 while ((j += line_count) <= end - 1)
8450 {
8451 LineOffset[j - line_count] = LineOffset[j];
8452 LineWraps[j - line_count] = LineWraps[j];
8453 }
8454 LineOffset[j - line_count] = temp;
8455 LineWraps[j - line_count] = FALSE;
8456 if (can_clear((char_u *)" "))
8457 lineclear(temp, (int)Columns);
8458 else
8459 lineinvalid(temp, (int)Columns);
8460 }
8461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462
8463 screen_stop_highlight();
8464
8465#ifdef FEAT_VERTSPLIT
8466 /* redraw the characters */
8467 if (type == USE_REDRAW)
8468 redraw_block(row, end, wp);
8469 else
8470#endif
8471 if (type == USE_T_CD) /* delete the lines */
8472 {
8473 windgoto(cursor_row, 0);
8474 out_str(T_CD);
8475 screen_start(); /* don't know where cursor is now */
8476 }
8477 else if (type == USE_T_CDL)
8478 {
8479 windgoto(cursor_row, 0);
8480 term_delete_lines(line_count);
8481 screen_start(); /* don't know where cursor is now */
8482 }
8483 /*
8484 * Deleting lines at top of the screen or scroll region: Just scroll
8485 * the whole screen (scroll region) up by outputting newlines on the
8486 * last line.
8487 */
8488 else if (type == USE_NL)
8489 {
8490 windgoto(cursor_end - 1, 0);
8491 for (i = line_count; --i >= 0; )
8492 out_char('\n'); /* cursor will remain on same line */
8493 }
8494 else
8495 {
8496 for (i = line_count; --i >= 0; )
8497 {
8498 if (type == USE_T_DL)
8499 {
8500 windgoto(cursor_row, 0);
8501 out_str(T_DL); /* delete a line */
8502 }
8503 else /* type == USE_T_CE */
8504 {
8505 windgoto(cursor_row + i, 0);
8506 out_str(T_CE); /* erase a line */
8507 }
8508 screen_start(); /* don't know where cursor is now */
8509 }
8510 }
8511
8512 /*
8513 * If the 'db' flag is set, we need to clear the lines that have been
8514 * scrolled up at the bottom of the region.
8515 */
8516 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
8517 {
8518 for (i = line_count; i > 0; --i)
8519 {
8520 windgoto(cursor_end - i, 0);
8521 out_str(T_CE); /* erase a line */
8522 screen_start(); /* don't know where cursor is now */
8523 }
8524 }
8525
8526#ifdef FEAT_GUI
8527 gui_can_update_cursor();
8528 if (gui.in_use)
8529 out_flush(); /* always flush after a scroll */
8530#endif
8531
8532 return OK;
8533}
8534
8535/*
8536 * show the current mode and ruler
8537 *
8538 * If clear_cmdline is TRUE, clear the rest of the cmdline.
8539 * If clear_cmdline is FALSE there may be a message there that needs to be
8540 * cleared only if a mode is shown.
8541 * Return the length of the message (0 if no message).
8542 */
8543 int
8544showmode()
8545{
8546 int need_clear;
8547 int length = 0;
8548 int do_mode;
8549 int attr;
8550 int nwr_save;
8551#ifdef FEAT_INS_EXPAND
8552 int sub_attr;
8553#endif
8554
Bram Moolenaar7df351e2006-01-23 22:30:28 +00008555 do_mode = ((p_smd && msg_silent == 0)
8556 && ((State & INSERT)
8557 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558#ifdef FEAT_VISUAL
8559 || VIsual_active
8560#endif
8561 ));
8562 if (do_mode || Recording)
8563 {
8564 /*
8565 * Don't show mode right now, when not redrawing or inside a mapping.
8566 * Call char_avail() only when we are going to show something, because
8567 * it takes a bit of time.
8568 */
8569 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
8570 {
8571 redraw_cmdline = TRUE; /* show mode later */
8572 return 0;
8573 }
8574
8575 nwr_save = need_wait_return;
8576
8577 /* wait a bit before overwriting an important message */
8578 check_for_delay(FALSE);
8579
8580 /* if the cmdline is more than one line high, erase top lines */
8581 need_clear = clear_cmdline;
8582 if (clear_cmdline && cmdline_row < Rows - 1)
8583 msg_clr_cmdline(); /* will reset clear_cmdline */
8584
8585 /* Position on the last line in the window, column 0 */
8586 msg_pos_mode();
8587 cursor_off();
8588 attr = hl_attr(HLF_CM); /* Highlight mode */
8589 if (do_mode)
8590 {
8591 MSG_PUTS_ATTR("--", attr);
8592#if defined(FEAT_XIM)
8593 if (xic != NULL && im_get_status() && !p_imdisable
8594 && curbuf->b_p_iminsert == B_IMODE_IM)
8595# ifdef HAVE_GTK2 /* most of the time, it's not XIM being used */
8596 MSG_PUTS_ATTR(" IM", attr);
8597# else
8598 MSG_PUTS_ATTR(" XIM", attr);
8599# endif
8600#endif
8601#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
8602 if (gui.in_use)
8603 {
8604 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008605 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 }
8607#endif
8608#ifdef FEAT_INS_EXPAND
8609 if (edit_submode != NULL) /* CTRL-X in Insert mode */
8610 {
8611 /* These messages can get long, avoid a wrap in a narrow
8612 * window. Prefer showing edit_submode_extra. */
8613 length = (Rows - msg_row) * Columns - 3;
8614 if (edit_submode_extra != NULL)
8615 length -= vim_strsize(edit_submode_extra);
8616 if (length > 0)
8617 {
8618 if (edit_submode_pre != NULL)
8619 length -= vim_strsize(edit_submode_pre);
8620 if (length - vim_strsize(edit_submode) > 0)
8621 {
8622 if (edit_submode_pre != NULL)
8623 msg_puts_attr(edit_submode_pre, attr);
8624 msg_puts_attr(edit_submode, attr);
8625 }
8626 if (edit_submode_extra != NULL)
8627 {
8628 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
8629 if ((int)edit_submode_highl < (int)HLF_COUNT)
8630 sub_attr = hl_attr(edit_submode_highl);
8631 else
8632 sub_attr = attr;
8633 msg_puts_attr(edit_submode_extra, sub_attr);
8634 }
8635 }
8636 length = 0;
8637 }
8638 else
8639#endif
8640 {
8641#ifdef FEAT_VREPLACE
8642 if (State & VREPLACE_FLAG)
8643 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
8644 else
8645#endif
8646 if (State & REPLACE_FLAG)
8647 MSG_PUTS_ATTR(_(" REPLACE"), attr);
8648 else if (State & INSERT)
8649 {
8650#ifdef FEAT_RIGHTLEFT
8651 if (p_ri)
8652 MSG_PUTS_ATTR(_(" REVERSE"), attr);
8653#endif
8654 MSG_PUTS_ATTR(_(" INSERT"), attr);
8655 }
8656 else if (restart_edit == 'I')
8657 MSG_PUTS_ATTR(_(" (insert)"), attr);
8658 else if (restart_edit == 'R')
8659 MSG_PUTS_ATTR(_(" (replace)"), attr);
8660 else if (restart_edit == 'V')
8661 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
8662#ifdef FEAT_RIGHTLEFT
8663 if (p_hkmap)
8664 MSG_PUTS_ATTR(_(" Hebrew"), attr);
8665# ifdef FEAT_FKMAP
8666 if (p_fkmap)
8667 MSG_PUTS_ATTR(farsi_text_5, attr);
8668# endif
8669#endif
8670#ifdef FEAT_KEYMAP
8671 if (State & LANGMAP)
8672 {
8673# ifdef FEAT_ARABIC
8674 if (curwin->w_p_arab)
8675 MSG_PUTS_ATTR(_(" Arabic"), attr);
8676 else
8677# endif
8678 MSG_PUTS_ATTR(_(" (lang)"), attr);
8679 }
8680#endif
8681 if ((State & INSERT) && p_paste)
8682 MSG_PUTS_ATTR(_(" (paste)"), attr);
8683
8684#ifdef FEAT_VISUAL
8685 if (VIsual_active)
8686 {
8687 char *p;
8688
8689 /* Don't concatenate separate words to avoid translation
8690 * problems. */
8691 switch ((VIsual_select ? 4 : 0)
8692 + (VIsual_mode == Ctrl_V) * 2
8693 + (VIsual_mode == 'V'))
8694 {
8695 case 0: p = N_(" VISUAL"); break;
8696 case 1: p = N_(" VISUAL LINE"); break;
8697 case 2: p = N_(" VISUAL BLOCK"); break;
8698 case 4: p = N_(" SELECT"); break;
8699 case 5: p = N_(" SELECT LINE"); break;
8700 default: p = N_(" SELECT BLOCK"); break;
8701 }
8702 MSG_PUTS_ATTR(_(p), attr);
8703 }
8704#endif
8705 MSG_PUTS_ATTR(" --", attr);
8706 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 need_clear = TRUE;
8709 }
8710 if (Recording
8711#ifdef FEAT_INS_EXPAND
8712 && edit_submode == NULL /* otherwise it gets too long */
8713#endif
8714 )
8715 {
8716 MSG_PUTS_ATTR(_("recording"), attr);
8717 need_clear = TRUE;
8718 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008719
8720 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 if (need_clear || clear_cmdline)
8722 msg_clr_eos();
8723 msg_didout = FALSE; /* overwrite this message */
8724 length = msg_col;
8725 msg_col = 0;
8726 need_wait_return = nwr_save; /* never ask for hit-return for this */
8727 }
8728 else if (clear_cmdline && msg_silent == 0)
8729 /* Clear the whole command line. Will reset "clear_cmdline". */
8730 msg_clr_cmdline();
8731
8732#ifdef FEAT_CMDL_INFO
8733# ifdef FEAT_VISUAL
8734 /* In Visual mode the size of the selected area must be redrawn. */
8735 if (VIsual_active)
8736 clear_showcmd();
8737# endif
8738
8739 /* If the last window has no status line, the ruler is after the mode
8740 * message and must be redrawn */
8741 if (redrawing()
8742# ifdef FEAT_WINDOWS
8743 && lastwin->w_status_height == 0
8744# endif
8745 )
8746 win_redr_ruler(lastwin, TRUE);
8747#endif
8748 redraw_cmdline = FALSE;
8749 clear_cmdline = FALSE;
8750
8751 return length;
8752}
8753
8754/*
8755 * Position for a mode message.
8756 */
8757 static void
8758msg_pos_mode()
8759{
8760 msg_col = 0;
8761 msg_row = Rows - 1;
8762}
8763
8764/*
8765 * Delete mode message. Used when ESC is typed which is expected to end
8766 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008767 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 */
8769 void
8770unshowmode(force)
8771 int force;
8772{
8773 /*
8774 * Don't delete it right now, when not redrawing or insided a mapping.
8775 */
8776 if (!redrawing() || (!force && char_avail() && !KeyTyped))
8777 redraw_cmdline = TRUE; /* delete mode later */
8778 else
8779 {
8780 msg_pos_mode();
8781 if (Recording)
8782 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
8783 msg_clr_eos();
8784 }
8785}
8786
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008787#if defined(FEAT_WINDOWS)
8788/*
8789 * Draw the tab pages line at the top of the Vim window.
8790 */
8791 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008792draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008793{
8794 int tabcount = 0;
8795 tabpage_T *tp;
8796 int tabwidth;
8797 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008798 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008799 int attr;
8800 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008801 win_T *cwp;
8802 int wincount;
8803 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008804 int c;
8805 int len;
8806 int attr_sel = hl_attr(HLF_TPS);
8807 int attr_nosel = hl_attr(HLF_TP);
8808 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008809 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008810 int room;
8811 int use_sep_chars = (t_colors < 8
8812#ifdef FEAT_GUI
8813 && !gui.in_use
8814#endif
8815 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008816
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008817 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008818
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008819#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00008820 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008821 if (gui_use_tabline())
8822 {
8823 gui_update_tabline();
8824 return;
8825 }
8826#endif
8827
8828 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008829 return;
8830
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008831#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008832
8833 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
8834 for (scol = 0; scol < Columns; ++scol)
8835 TabPageIdxs[scol] = 0;
8836
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008837 /* Use the 'tabline' option if it's set. */
8838 if (*p_tal != NUL)
8839 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008840 int save_called_emsg = called_emsg;
8841
8842 /* Check for an error. If there is one we would loop in redrawing the
8843 * screen. Avoid that by making 'tabline' empty. */
8844 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008845 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008846 if (called_emsg)
8847 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008848 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008849 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008850 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00008851 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008852#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008853 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008854 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
8855 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008856
Bram Moolenaar238a5642006-02-21 22:12:05 +00008857 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
8858 if (tabwidth < 6)
8859 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008860
Bram Moolenaar238a5642006-02-21 22:12:05 +00008861 attr = attr_nosel;
8862 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008863 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008864 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
8865 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008866 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008867 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008868
Bram Moolenaar238a5642006-02-21 22:12:05 +00008869 if (tp->tp_topframe == topframe)
8870 attr = attr_sel;
8871 if (use_sep_chars && col > 0)
8872 screen_putchar('|', 0, col++, attr);
8873
8874 if (tp->tp_topframe != topframe)
8875 attr = attr_nosel;
8876
8877 screen_putchar(' ', 0, col++, attr);
8878
8879 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00008880 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00008881 cwp = curwin;
8882 wp = firstwin;
8883 }
8884 else
8885 {
8886 cwp = tp->tp_curwin;
8887 wp = tp->tp_firstwin;
8888 }
8889
8890 modified = FALSE;
8891 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
8892 if (bufIsChanged(wp->w_buffer))
8893 modified = TRUE;
8894 if (modified || wincount > 1)
8895 {
8896 if (wincount > 1)
8897 {
8898 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
8899 len = STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008900 if (col + len >= Columns - 3)
8901 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008902 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008903#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00008904 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008905#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00008906 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008907#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00008908 );
8909 col += len;
8910 }
8911 if (modified)
8912 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
8913 screen_putchar(' ', 0, col++, attr);
8914 }
8915
8916 room = scol - col + tabwidth - 1;
8917 if (room > 0)
8918 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008919 /* Get buffer name in NameBuff[] */
8920 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008921 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008922 len = vim_strsize(NameBuff);
8923 p = NameBuff;
8924#ifdef FEAT_MBYTE
8925 if (has_mbyte)
8926 while (len > room)
8927 {
8928 len -= ptr2cells(p);
8929 mb_ptr_adv(p);
8930 }
8931 else
8932#endif
8933 if (len > room)
8934 {
8935 p += len - room;
8936 len = room;
8937 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00008938 if (len > Columns - col - 1)
8939 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +00008940
8941 screen_puts_len(p, STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008942 col += len;
8943 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00008944 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +00008945
8946 /* Store the tab page number in TabPageIdxs[], so that
8947 * jump_to_mouse() knows where each one is. */
8948 ++tabcount;
8949 while (scol < col)
8950 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008951 }
8952
Bram Moolenaar238a5642006-02-21 22:12:05 +00008953 if (use_sep_chars)
8954 c = '_';
8955 else
8956 c = ' ';
8957 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008958
8959 /* Put an "X" for closing the current tab if there are several. */
8960 if (first_tabpage->tp_next != NULL)
8961 {
8962 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
8963 TabPageIdxs[Columns - 1] = -999;
8964 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008965 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008966}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008967
8968/*
8969 * Get buffer name for "buf" into NameBuff[].
8970 * Takes care of special buffer names and translates special characters.
8971 */
8972 void
8973get_trans_bufname(buf)
8974 buf_T *buf;
8975{
8976 if (buf_spname(buf) != NULL)
8977 STRCPY(NameBuff, buf_spname(buf));
8978 else
8979 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
8980 trans_characters(NameBuff, MAXPATHL);
8981}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008982#endif
8983
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
8985/*
8986 * Get the character to use in a status line. Get its attributes in "*attr".
8987 */
8988 static int
8989fillchar_status(attr, is_curwin)
8990 int *attr;
8991 int is_curwin;
8992{
8993 int fill;
8994 if (is_curwin)
8995 {
8996 *attr = hl_attr(HLF_S);
8997 fill = fill_stl;
8998 }
8999 else
9000 {
9001 *attr = hl_attr(HLF_SNC);
9002 fill = fill_stlnc;
9003 }
9004 /* Use fill when there is highlighting, and highlighting of current
9005 * window differs, or the fillchars differ, or this is not the
9006 * current window */
9007 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
9008 || !is_curwin || firstwin == lastwin)
9009 || (fill_stl != fill_stlnc)))
9010 return fill;
9011 if (is_curwin)
9012 return '^';
9013 return '=';
9014}
9015#endif
9016
9017#ifdef FEAT_VERTSPLIT
9018/*
9019 * Get the character to use in a separator between vertically split windows.
9020 * Get its attributes in "*attr".
9021 */
9022 static int
9023fillchar_vsep(attr)
9024 int *attr;
9025{
9026 *attr = hl_attr(HLF_C);
9027 if (*attr == 0 && fill_vert == ' ')
9028 return '|';
9029 else
9030 return fill_vert;
9031}
9032#endif
9033
9034/*
9035 * Return TRUE if redrawing should currently be done.
9036 */
9037 int
9038redrawing()
9039{
9040 return (!RedrawingDisabled
9041 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
9042}
9043
9044/*
9045 * Return TRUE if printing messages should currently be done.
9046 */
9047 int
9048messaging()
9049{
9050 return (!(p_lz && char_avail() && !KeyTyped));
9051}
9052
9053/*
9054 * Show current status info in ruler and various other places
9055 * If always is FALSE, only show ruler if position has changed.
9056 */
9057 void
9058showruler(always)
9059 int always;
9060{
9061 if (!always && !redrawing())
9062 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +00009063#ifdef FEAT_INS_EXPAND
9064 if (pum_visible())
9065 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009066# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +00009067 /* Don't redraw right now, do it later. */
9068 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00009069# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +00009070 return;
9071 }
9072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009074 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009075 {
9076 redraw_custum_statusline(curwin);
9077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 else
9079#endif
9080#ifdef FEAT_CMDL_INFO
9081 win_redr_ruler(curwin, always);
9082#endif
9083
9084#ifdef FEAT_TITLE
9085 if (need_maketitle
9086# ifdef FEAT_STL_OPT
9087 || (p_icon && (stl_syntax & STL_IN_ICON))
9088 || (p_title && (stl_syntax & STL_IN_TITLE))
9089# endif
9090 )
9091 maketitle();
9092#endif
9093}
9094
9095#ifdef FEAT_CMDL_INFO
9096 static void
9097win_redr_ruler(wp, always)
9098 win_T *wp;
9099 int always;
9100{
9101 char_u buffer[70];
9102 int row;
9103 int fillchar;
9104 int attr;
9105 int empty_line = FALSE;
9106 colnr_T virtcol;
9107 int i;
9108 int o;
9109#ifdef FEAT_VERTSPLIT
9110 int this_ru_col;
9111 int off = 0;
9112 int width = Columns;
9113# define WITH_OFF(x) x
9114# define WITH_WIDTH(x) x
9115#else
9116# define WITH_OFF(x) 0
9117# define WITH_WIDTH(x) Columns
9118# define this_ru_col ru_col
9119#endif
9120
9121 /* If 'ruler' off or redrawing disabled, don't do anything */
9122 if (!p_ru)
9123 return;
9124
9125 /*
9126 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
9127 * after deleting lines, before cursor.lnum is corrected.
9128 */
9129 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
9130 return;
9131
9132#ifdef FEAT_INS_EXPAND
9133 /* Don't draw the ruler while doing insert-completion, it might overwrite
9134 * the (long) mode message. */
9135# ifdef FEAT_WINDOWS
9136 if (wp == lastwin && lastwin->w_status_height == 0)
9137# endif
9138 if (edit_submode != NULL)
9139 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00009140 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
9141 if (pum_visible())
9142 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#endif
9144
9145#ifdef FEAT_STL_OPT
9146 if (*p_ruf)
9147 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009148 int save_called_emsg = called_emsg;
9149
9150 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009152 if (called_emsg)
9153 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009154 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009155 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 return;
9157 }
9158#endif
9159
9160 /*
9161 * Check if not in Insert mode and the line is empty (will show "0-1").
9162 */
9163 if (!(State & INSERT)
9164 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
9165 empty_line = TRUE;
9166
9167 /*
9168 * Only draw the ruler when something changed.
9169 */
9170 validate_virtcol_win(wp);
9171 if ( redraw_cmdline
9172 || always
9173 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
9174 || wp->w_cursor.col != wp->w_ru_cursor.col
9175 || wp->w_virtcol != wp->w_ru_virtcol
9176#ifdef FEAT_VIRTUALEDIT
9177 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
9178#endif
9179 || wp->w_topline != wp->w_ru_topline
9180 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
9181#ifdef FEAT_DIFF
9182 || wp->w_topfill != wp->w_ru_topfill
9183#endif
9184 || empty_line != wp->w_ru_empty)
9185 {
9186 cursor_off();
9187#ifdef FEAT_WINDOWS
9188 if (wp->w_status_height)
9189 {
9190 row = W_WINROW(wp) + wp->w_height;
9191 fillchar = fillchar_status(&attr, wp == curwin);
9192# ifdef FEAT_VERTSPLIT
9193 off = W_WINCOL(wp);
9194 width = W_WIDTH(wp);
9195# endif
9196 }
9197 else
9198#endif
9199 {
9200 row = Rows - 1;
9201 fillchar = ' ';
9202 attr = 0;
9203#ifdef FEAT_VERTSPLIT
9204 width = Columns;
9205 off = 0;
9206#endif
9207 }
9208
9209 /* In list mode virtcol needs to be recomputed */
9210 virtcol = wp->w_virtcol;
9211 if (wp->w_p_list && lcs_tab1 == NUL)
9212 {
9213 wp->w_p_list = FALSE;
9214 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
9215 wp->w_p_list = TRUE;
9216 }
9217
9218 /*
9219 * Some sprintfs return the length, some return a pointer.
9220 * To avoid portability problems we use strlen() here.
9221 */
9222 sprintf((char *)buffer, "%ld,",
9223 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
9224 ? 0L
9225 : (long)(wp->w_cursor.lnum));
9226 col_print(buffer + STRLEN(buffer),
9227 empty_line ? 0 : (int)wp->w_cursor.col + 1,
9228 (int)virtcol + 1);
9229
9230 /*
9231 * Add a "50%" if there is room for it.
9232 * On the last line, don't print in the last column (scrolls the
9233 * screen up on some terminals).
9234 */
9235 i = (int)STRLEN(buffer);
9236 get_rel_pos(wp, buffer + i + 1);
9237 o = i + vim_strsize(buffer + i + 1);
9238#ifdef FEAT_WINDOWS
9239 if (wp->w_status_height == 0) /* can't use last char of screen */
9240#endif
9241 ++o;
9242#ifdef FEAT_VERTSPLIT
9243 this_ru_col = ru_col - (Columns - width);
9244 if (this_ru_col < 0)
9245 this_ru_col = 0;
9246#endif
9247 /* Never use more than half the window/screen width, leave the other
9248 * half for the filename. */
9249 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
9250 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
9251 if (this_ru_col + o < WITH_WIDTH(width))
9252 {
9253 while (this_ru_col + o < WITH_WIDTH(width))
9254 {
9255#ifdef FEAT_MBYTE
9256 if (has_mbyte)
9257 i += (*mb_char2bytes)(fillchar, buffer + i);
9258 else
9259#endif
9260 buffer[i++] = fillchar;
9261 ++o;
9262 }
9263 get_rel_pos(wp, buffer + i);
9264 }
9265 /* Truncate at window boundary. */
9266#ifdef FEAT_MBYTE
9267 if (has_mbyte)
9268 {
9269 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009270 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 {
9272 o += (*mb_ptr2cells)(buffer + i);
9273 if (this_ru_col + o > WITH_WIDTH(width))
9274 {
9275 buffer[i] = NUL;
9276 break;
9277 }
9278 }
9279 }
9280 else
9281#endif
9282 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
9283 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
9284
9285 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
9286 i = redraw_cmdline;
9287 screen_fill(row, row + 1,
9288 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
9289 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
9290 fillchar, fillchar, attr);
9291 /* don't redraw the cmdline because of showing the ruler */
9292 redraw_cmdline = i;
9293 wp->w_ru_cursor = wp->w_cursor;
9294 wp->w_ru_virtcol = wp->w_virtcol;
9295 wp->w_ru_empty = empty_line;
9296 wp->w_ru_topline = wp->w_topline;
9297 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
9298#ifdef FEAT_DIFF
9299 wp->w_ru_topfill = wp->w_topfill;
9300#endif
9301 }
9302}
9303#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009304
9305#if defined(FEAT_LINEBREAK) || defined(PROTO)
9306/*
9307 * Return the width of the 'number' column.
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009308 * Caller may need to check if 'number' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009309 * Otherwise it depends on 'numberwidth' and the line count.
9310 */
9311 int
9312number_width(wp)
9313 win_T *wp;
9314{
9315 int n;
9316 linenr_T lnum;
9317
Bram Moolenaar592e0a22004-07-03 16:05:59 +00009318 lnum = wp->w_buffer->b_ml.ml_line_count;
9319 if (lnum == wp->w_nrwidth_line_count)
9320 return wp->w_nrwidth_width;
9321 wp->w_nrwidth_line_count = lnum;
9322
9323 n = 0;
9324 do
9325 {
9326 lnum /= 10;
9327 ++n;
9328 } while (lnum > 0);
9329
9330 /* 'numberwidth' gives the minimal width plus one */
9331 if (n < wp->w_p_nuw - 1)
9332 n = wp->w_p_nuw - 1;
9333
9334 wp->w_nrwidth_width = n;
9335 return n;
9336}
9337#endif